1 /* $OpenBSD: ssl_kex.c,v 1.2 2020/04/18 14:07:56 jsing Exp $ */ 2 /* 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <stdlib.h> 19 20 #include <openssl/ec.h> 21 #include <openssl/ecdh.h> 22 #include <openssl/evp.h> 23 #include <openssl/objects.h> 24 25 #include "bytestring.h" 26 27 int 28 ssl_kex_dummy_ecdhe_x25519(EVP_PKEY *pkey) 29 { 30 EC_GROUP *group = NULL; 31 EC_POINT *point = NULL; 32 EC_KEY *ec_key = NULL; 33 BIGNUM *order = NULL; 34 int ret = 0; 35 36 /* Fudge up an EC_KEY that looks like X25519... */ 37 if ((group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)) == NULL) 38 goto err; 39 if ((point = EC_POINT_new(group)) == NULL) 40 goto err; 41 if ((order = BN_new()) == NULL) 42 goto err; 43 if (!BN_set_bit(order, 252)) 44 goto err; 45 if (!EC_GROUP_set_generator(group, point, order, NULL)) 46 goto err; 47 EC_GROUP_set_curve_name(group, NID_X25519); 48 if ((ec_key = EC_KEY_new()) == NULL) 49 goto err; 50 if (!EC_KEY_set_group(ec_key, group)) 51 goto err; 52 if (!EVP_PKEY_set1_EC_KEY(pkey, ec_key)) 53 goto err; 54 55 ret = 1; 56 57 err: 58 EC_GROUP_free(group); 59 EC_POINT_free(point); 60 EC_KEY_free(ec_key); 61 BN_free(order); 62 63 return ret; 64 } 65 66 int 67 ssl_kex_generate_ecdhe_ecp(EC_KEY *ecdh, int nid) 68 { 69 EC_GROUP *group; 70 int ret = 0; 71 72 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) 73 goto err; 74 75 if (!EC_KEY_set_group(ecdh, group)) 76 goto err; 77 if (!EC_KEY_generate_key(ecdh)) 78 goto err; 79 80 ret = 1; 81 82 err: 83 EC_GROUP_free(group); 84 85 return ret; 86 } 87 88 int 89 ssl_kex_public_ecdhe_ecp(EC_KEY *ecdh, CBB *cbb) 90 { 91 const EC_GROUP *group; 92 const EC_POINT *point; 93 uint8_t *ecp; 94 size_t ecp_len; 95 int ret = 0; 96 97 if ((group = EC_KEY_get0_group(ecdh)) == NULL) 98 goto err; 99 if ((point = EC_KEY_get0_public_key(ecdh)) == NULL) 100 goto err; 101 102 if ((ecp_len = EC_POINT_point2oct(group, point, 103 POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL)) == 0) 104 goto err; 105 if (!CBB_add_space(cbb, &ecp, ecp_len)) 106 goto err; 107 if ((EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED, 108 ecp, ecp_len, NULL)) == 0) 109 goto err; 110 111 ret = 1; 112 113 err: 114 return ret; 115 } 116 117 int 118 ssl_kex_peer_public_ecdhe_ecp(EC_KEY *ecdh, int nid, CBS *cbs) 119 { 120 EC_GROUP *group = NULL; 121 EC_POINT *point = NULL; 122 int ret = 0; 123 124 if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) 125 goto err; 126 127 if (!EC_KEY_set_group(ecdh, group)) 128 goto err; 129 130 if ((point = EC_POINT_new(group)) == NULL) 131 goto err; 132 if (EC_POINT_oct2point(group, point, CBS_data(cbs), CBS_len(cbs), 133 NULL) == 0) 134 goto err; 135 if (!EC_KEY_set_public_key(ecdh, point)) 136 goto err; 137 138 ret = 1; 139 140 err: 141 EC_GROUP_free(group); 142 EC_POINT_free(point); 143 144 return ret; 145 } 146 147 int 148 ssl_kex_derive_ecdhe_ecp(EC_KEY *ecdh, EC_KEY *ecdh_peer, 149 uint8_t **shared_key, size_t *shared_key_len) 150 { 151 const EC_POINT *point; 152 uint8_t *sk = NULL; 153 int sk_len = 0; 154 int ret = 0; 155 156 if (!EC_GROUP_check(EC_KEY_get0_group(ecdh), NULL)) 157 goto err; 158 if (!EC_GROUP_check(EC_KEY_get0_group(ecdh_peer), NULL)) 159 goto err; 160 161 if ((point = EC_KEY_get0_public_key(ecdh_peer)) == NULL) 162 goto err; 163 164 if ((sk_len = ECDH_size(ecdh)) <= 0) 165 goto err; 166 if ((sk = calloc(1, sk_len)) == NULL) 167 goto err; 168 169 if (ECDH_compute_key(sk, sk_len, point, ecdh, NULL) <= 0) 170 goto err; 171 172 *shared_key = sk; 173 *shared_key_len = sk_len; 174 sk = NULL; 175 176 ret = 1; 177 178 err: 179 freezero(sk, sk_len); 180 181 return ret; 182 } 183