openssl: Fix potential crash with ECDH on Windows
[strongswan.git] / src / libstrongswan / plugins / openssl / openssl_ec_diffie_hellman.c
1 /*
2 * Copyright (C) 2008-2021 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include <openssl/opensslconf.h>
17
18 #ifndef OPENSSL_NO_EC
19
20 #include <openssl/evp.h>
21 #include <openssl/ec.h>
22 #include <openssl/objects.h>
23
24 #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_BORINGSSL)
25 #include <openssl/bn.h>
26 #endif
27
28 #include "openssl_ec_diffie_hellman.h"
29 #include "openssl_util.h"
30
31 #include <utils/debug.h>
32
33 typedef struct private_openssl_ec_diffie_hellman_t private_openssl_ec_diffie_hellman_t;
34
35 /**
36 * Private data of an openssl_ec_diffie_hellman_t object.
37 */
38 struct private_openssl_ec_diffie_hellman_t {
39 /**
40 * Public openssl_ec_diffie_hellman_t interface.
41 */
42 openssl_ec_diffie_hellman_t public;
43
44 /**
45 * Diffie Hellman group number.
46 */
47 diffie_hellman_group_t group;
48
49 /**
50 * EC private (public) key
51 */
52 EVP_PKEY *key;
53
54 /**
55 * EC group
56 */
57 EC_GROUP *ec_group;
58
59 /**
60 * Shared secret
61 */
62 chunk_t shared_secret;
63
64 /**
65 * True if shared secret is computed
66 */
67 bool computed;
68 };
69
70 #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_BORINGSSL)
71 /**
72 * Convert a chunk to an EC_POINT and set it on the given key. The x and y
73 * coordinates of the point have to be concatenated in the chunk.
74 */
75 static bool chunk2ecp(const EC_GROUP *group, chunk_t chunk, EVP_PKEY *key)
76 {
77 EC_POINT *point = NULL;
78 EC_KEY *pub = NULL;
79 BN_CTX *ctx;
80 BIGNUM *x, *y;
81 bool ret = FALSE;
82
83 ctx = BN_CTX_new();
84 if (!ctx)
85 {
86 return FALSE;
87 }
88
89 BN_CTX_start(ctx);
90 x = BN_CTX_get(ctx);
91 y = BN_CTX_get(ctx);
92 if (!x || !y)
93 {
94 goto error;
95 }
96
97 if (!openssl_bn_split(chunk, x, y))
98 {
99 goto error;
100 }
101
102 point = EC_POINT_new(group);
103 if (!point || !EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx))
104 {
105 goto error;
106 }
107
108 if (!EC_POINT_is_on_curve(group, point, ctx))
109 {
110 goto error;
111 }
112
113 pub = EC_KEY_new();
114 if (!pub || !EC_KEY_set_group(pub, group))
115 {
116 goto error;
117 }
118
119 if (EC_KEY_set_public_key(pub, point) != 1)
120 {
121 goto error;
122 }
123
124 if (EVP_PKEY_set1_EC_KEY(key, pub) != 1)
125 {
126 goto error;
127 }
128
129 ret = TRUE;
130
131 error:
132 EC_POINT_clear_free(point);
133 EC_KEY_free(pub);
134 BN_CTX_end(ctx);
135 BN_CTX_free(ctx);
136 return ret;
137 }
138
139 /**
140 * Convert a key to a chunk by concatenating the x and y coordinates of
141 * the underlying EC point. This function allocates memory for the chunk.
142 */
143 static bool ecp2chunk(const EC_GROUP *group, EVP_PKEY *key, chunk_t *chunk)
144 {
145 EC_KEY *ec_key = NULL;
146 const EC_POINT *point;
147 BN_CTX *ctx;
148 BIGNUM *x, *y;
149 bool ret = FALSE;
150
151 ctx = BN_CTX_new();
152 if (!ctx)
153 {
154 return FALSE;
155 }
156
157 BN_CTX_start(ctx);
158 x = BN_CTX_get(ctx);
159 y = BN_CTX_get(ctx);
160 if (!x || !y)
161 {
162 goto error;
163 }
164
165 ec_key = EVP_PKEY_get1_EC_KEY(key);
166 point = EC_KEY_get0_public_key(ec_key);
167 if (!point || !EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx))
168 {
169 goto error;
170 }
171
172 if (!openssl_bn_cat(EC_FIELD_ELEMENT_LEN(group), x, y, chunk))
173 {
174 goto error;
175 }
176
177 ret = chunk->len != 0;
178 error:
179 EC_KEY_free(ec_key);
180 BN_CTX_end(ctx);
181 BN_CTX_free(ctx);
182 return ret;
183 }
184 #endif /* OPENSSL_VERSION_NUMBER < ... */
185
186 METHOD(diffie_hellman_t, set_other_public_value, bool,
187 private_openssl_ec_diffie_hellman_t *this, chunk_t value)
188 {
189 EVP_PKEY *pub = NULL;
190
191 chunk_clear(&this->shared_secret);
192 this->computed = FALSE;
193
194 if (!diffie_hellman_verify_value(this->group, value))
195 {
196 return FALSE;
197 }
198
199 pub = EVP_PKEY_new();
200 if (!pub)
201 {
202 goto error;
203 }
204
205 #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_BORINGSSL)
206 if (!chunk2ecp(this->ec_group, value, pub))
207 {
208 DBG1(DBG_LIB, "ECDH public value is malformed");
209 goto error;
210 }
211 #else
212 /* OpenSSL expects the pubkey in the format specified in section 2.3.4 of
213 * SECG SEC 1, i.e. prefixed with 0x04 to indicate an uncompressed point */
214 value = chunk_cata("cc", chunk_from_chars(0x04), value);
215 if (EVP_PKEY_copy_parameters(pub, this->key) <= 0 ||
216 EVP_PKEY_set1_tls_encodedpoint(pub, value.ptr, value.len) <= 0)
217 {
218 DBG1(DBG_LIB, "ECDH public value is malformed");
219 goto error;
220 }
221 #endif
222
223 if (!openssl_compute_shared_key(this->key, pub, &this->shared_secret))
224 {
225 DBG1(DBG_LIB, "ECDH shared secret computation failed");
226 goto error;
227 }
228 this->computed = TRUE;
229
230 error:
231 EVP_PKEY_free(pub);
232 return this->computed;
233 }
234
235 METHOD(diffie_hellman_t, get_my_public_value, bool,
236 private_openssl_ec_diffie_hellman_t *this, chunk_t *value)
237 {
238 #if OPENSSL_VERSION_NUMBER < 0x1010000fL || defined(OPENSSL_IS_BORINGSSL)
239 return ecp2chunk(this->ec_group, this->key, value);
240 #else
241 chunk_t pub;
242
243 /* OpenSSL returns the pubkey in the format specified in section 2.3.4 of
244 * SECG SEC 1, i.e. prefixed with 0x04 to indicate an uncompressed point */
245 pub.len = EVP_PKEY_get1_tls_encodedpoint(this->key, &pub.ptr);
246 if (pub.len != 0)
247 {
248 *value = chunk_clone(chunk_skip(pub, 1));
249 OPENSSL_free(pub.ptr);
250 return value->len != 0;
251 }
252 return FALSE;
253 #endif
254 }
255
256 METHOD(diffie_hellman_t, set_private_value, bool,
257 private_openssl_ec_diffie_hellman_t *this, chunk_t value)
258 {
259 EC_KEY *key = NULL;
260 EC_POINT *pub = NULL;
261 BIGNUM *priv = NULL;
262 bool ret = FALSE;
263
264 priv = BN_bin2bn(value.ptr, value.len, NULL);
265 if (!priv)
266 {
267 goto error;
268 }
269 pub = EC_POINT_new(this->ec_group);
270 if (!pub)
271 {
272 goto error;
273 }
274 if (EC_POINT_mul(this->ec_group, pub, priv, NULL, NULL, NULL) != 1)
275 {
276 goto error;
277 }
278 key = EC_KEY_new();
279 if (!key || !EC_KEY_set_group(key, this->ec_group))
280 {
281 goto error;
282 }
283 if (EC_KEY_set_private_key(key, priv) != 1)
284 {
285 goto error;
286 }
287 if (EC_KEY_set_public_key(key, pub) != 1)
288 {
289 goto error;
290 }
291 if (EVP_PKEY_set1_EC_KEY(this->key, key) != 1)
292 {
293 goto error;
294 }
295 ret = TRUE;
296
297 error:
298 EC_POINT_free(pub);
299 BN_free(priv);
300 EC_KEY_free(key);
301 return ret;
302 }
303
304 METHOD(diffie_hellman_t, get_shared_secret, bool,
305 private_openssl_ec_diffie_hellman_t *this, chunk_t *secret)
306 {
307 if (!this->computed)
308 {
309 return FALSE;
310 }
311 *secret = chunk_clone(this->shared_secret);
312 return TRUE;
313 }
314
315 METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
316 private_openssl_ec_diffie_hellman_t *this)
317 {
318 return this->group;
319 }
320
321 METHOD(diffie_hellman_t, destroy, void,
322 private_openssl_ec_diffie_hellman_t *this)
323 {
324 EC_GROUP_free(this->ec_group);
325 EVP_PKEY_free(this->key);
326 chunk_clear(&this->shared_secret);
327 free(this);
328 }
329
330 /**
331 * ECC Brainpool curves are not available in OpenSSL releases < 1.0.2, but we
332 * don't check the version in case somebody backported them.
333 *
334 * BoringSSL defines the constants but not the curves.
335 */
336 #if (!defined(NID_brainpoolP224r1) || !defined(NID_brainpoolP256r1) || \
337 !defined(NID_brainpoolP384r1) || !defined(NID_brainpoolP512r1) || \
338 defined(OPENSSL_IS_BORINGSSL))
339
340 /**
341 * Parameters for ECC Brainpool curves
342 */
343 typedef struct {
344 /** DH group */
345 diffie_hellman_group_t group;
346
347 /** The prime p specifying the base field */
348 const chunk_t p;
349
350 /** Coefficient a of the elliptic curve E: y^2 = x^3 + ax + b (mod p) */
351 const chunk_t a;
352
353 /** Coefficient b */
354 const chunk_t b;
355
356 /** x coordinate of base point G (a point in E of prime order) */
357 const chunk_t x;
358
359 /** y coordinate of base point G */
360 const chunk_t y;
361
362 /** Prime order q of the group generated by G */
363 const chunk_t q;
364
365 } bp_curve;
366
367 /**
368 * List of ECC Brainpool curves
369 */
370 static bp_curve bp_curves[] = {
371 {
372 /* ECC Brainpool 224-bit curve (RFC 5639), brainpoolP224r1 */
373 .group = ECP_224_BP,
374 .p = chunk_from_chars(
375 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD1,0xD7,0x87,
376 0xB0,0x9F,0x07,0x57,0x97,0xDA,0x89,0xF5,0x7E,0xC8,0xC0,0xFF),
377 .a = chunk_from_chars(
378 0x68,0xA5,0xE6,0x2C,0xA9,0xCE,0x6C,0x1C,0x29,0x98,0x03,0xA6,0xC1,0x53,0x0B,0x51,
379 0x4E,0x18,0x2A,0xD8,0xB0,0x04,0x2A,0x59,0xCA,0xD2,0x9F,0x43),
380 .b = chunk_from_chars(
381 0x25,0x80,0xF6,0x3C,0xCF,0xE4,0x41,0x38,0x87,0x07,0x13,0xB1,0xA9,0x23,0x69,0xE3,
382 0x3E,0x21,0x35,0xD2,0x66,0xDB,0xB3,0x72,0x38,0x6C,0x40,0x0B),
383 .x = chunk_from_chars(
384 0x0D,0x90,0x29,0xAD,0x2C,0x7E,0x5C,0xF4,0x34,0x08,0x23,0xB2,0xA8,0x7D,0xC6,0x8C,
385 0x9E,0x4C,0xE3,0x17,0x4C,0x1E,0x6E,0xFD,0xEE,0x12,0xC0,0x7D),
386 .y = chunk_from_chars(
387 0x58,0xAA,0x56,0xF7,0x72,0xC0,0x72,0x6F,0x24,0xC6,0xB8,0x9E,0x4E,0xCD,0xAC,0x24,
388 0x35,0x4B,0x9E,0x99,0xCA,0xA3,0xF6,0xD3,0x76,0x14,0x02,0xCD),
389 .q = chunk_from_chars(
390 0xD7,0xC1,0x34,0xAA,0x26,0x43,0x66,0x86,0x2A,0x18,0x30,0x25,0x75,0xD0,0xFB,0x98,
391 0xD1,0x16,0xBC,0x4B,0x6D,0xDE,0xBC,0xA3,0xA5,0xA7,0x93,0x9F),
392 },
393 {
394 /* ECC Brainpool 256-bit curve (RFC 5639), brainpoolP256r1 */
395 .group = ECP_256_BP,
396 .p = chunk_from_chars(
397 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x72,
398 0x6E,0x3B,0xF6,0x23,0xD5,0x26,0x20,0x28,0x20,0x13,0x48,0x1D,0x1F,0x6E,0x53,0x77),
399 .a = chunk_from_chars(
400 0x7D,0x5A,0x09,0x75,0xFC,0x2C,0x30,0x57,0xEE,0xF6,0x75,0x30,0x41,0x7A,0xFF,0xE7,
401 0xFB,0x80,0x55,0xC1,0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9),
402 .b = chunk_from_chars(
403 0x26,0xDC,0x5C,0x6C,0xE9,0x4A,0x4B,0x44,0xF3,0x30,0xB5,0xD9,0xBB,0xD7,0x7C,0xBF,
404 0x95,0x84,0x16,0x29,0x5C,0xF7,0xE1,0xCE,0x6B,0xCC,0xDC,0x18,0xFF,0x8C,0x07,0xB6),
405 .x = chunk_from_chars(
406 0x8B,0xD2,0xAE,0xB9,0xCB,0x7E,0x57,0xCB,0x2C,0x4B,0x48,0x2F,0xFC,0x81,0xB7,0xAF,
407 0xB9,0xDE,0x27,0xE1,0xE3,0xBD,0x23,0xC2,0x3A,0x44,0x53,0xBD,0x9A,0xCE,0x32,0x62),
408 .y = chunk_from_chars(
409 0x54,0x7E,0xF8,0x35,0xC3,0xDA,0xC4,0xFD,0x97,0xF8,0x46,0x1A,0x14,0x61,0x1D,0xC9,
410 0xC2,0x77,0x45,0x13,0x2D,0xED,0x8E,0x54,0x5C,0x1D,0x54,0xC7,0x2F,0x04,0x69,0x97),
411 .q = chunk_from_chars(
412 0xA9,0xFB,0x57,0xDB,0xA1,0xEE,0xA9,0xBC,0x3E,0x66,0x0A,0x90,0x9D,0x83,0x8D,0x71,
413 0x8C,0x39,0x7A,0xA3,0xB5,0x61,0xA6,0xF7,0x90,0x1E,0x0E,0x82,0x97,0x48,0x56,0xA7),
414 },
415 {
416 /* ECC Brainpool 384-bit curve (RFC 5639), brainpoolP384r1 */
417 .group = ECP_384_BP,
418 .p = chunk_from_chars(
419 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
420 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB4,0x12,0xB1,0xDA,0x19,0x7F,0xB7,0x11,0x23,
421 0xAC,0xD3,0xA7,0x29,0x90,0x1D,0x1A,0x71,0x87,0x47,0x00,0x13,0x31,0x07,0xEC,0x53),
422 .a = chunk_from_chars(
423 0x7B,0xC3,0x82,0xC6,0x3D,0x8C,0x15,0x0C,0x3C,0x72,0x08,0x0A,0xCE,0x05,0xAF,0xA0,
424 0xC2,0xBE,0xA2,0x8E,0x4F,0xB2,0x27,0x87,0x13,0x91,0x65,0xEF,0xBA,0x91,0xF9,0x0F,
425 0x8A,0xA5,0x81,0x4A,0x50,0x3A,0xD4,0xEB,0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26),
426 .b = chunk_from_chars(
427 0x04,0xA8,0xC7,0xDD,0x22,0xCE,0x28,0x26,0x8B,0x39,0xB5,0x54,0x16,0xF0,0x44,0x7C,
428 0x2F,0xB7,0x7D,0xE1,0x07,0xDC,0xD2,0xA6,0x2E,0x88,0x0E,0xA5,0x3E,0xEB,0x62,0xD5,
429 0x7C,0xB4,0x39,0x02,0x95,0xDB,0xC9,0x94,0x3A,0xB7,0x86,0x96,0xFA,0x50,0x4C,0x11),
430 .x = chunk_from_chars(
431 0x1D,0x1C,0x64,0xF0,0x68,0xCF,0x45,0xFF,0xA2,0xA6,0x3A,0x81,0xB7,0xC1,0x3F,0x6B,
432 0x88,0x47,0xA3,0xE7,0x7E,0xF1,0x4F,0xE3,0xDB,0x7F,0xCA,0xFE,0x0C,0xBD,0x10,0xE8,
433 0xE8,0x26,0xE0,0x34,0x36,0xD6,0x46,0xAA,0xEF,0x87,0xB2,0xE2,0x47,0xD4,0xAF,0x1E),
434 .y = chunk_from_chars(
435 0x8A,0xBE,0x1D,0x75,0x20,0xF9,0xC2,0xA4,0x5C,0xB1,0xEB,0x8E,0x95,0xCF,0xD5,0x52,
436 0x62,0xB7,0x0B,0x29,0xFE,0xEC,0x58,0x64,0xE1,0x9C,0x05,0x4F,0xF9,0x91,0x29,0x28,
437 0x0E,0x46,0x46,0x21,0x77,0x91,0x81,0x11,0x42,0x82,0x03,0x41,0x26,0x3C,0x53,0x15),
438 .q = chunk_from_chars(
439 0x8C,0xB9,0x1E,0x82,0xA3,0x38,0x6D,0x28,0x0F,0x5D,0x6F,0x7E,0x50,0xE6,0x41,0xDF,
440 0x15,0x2F,0x71,0x09,0xED,0x54,0x56,0xB3,0x1F,0x16,0x6E,0x6C,0xAC,0x04,0x25,0xA7,
441 0xCF,0x3A,0xB6,0xAF,0x6B,0x7F,0xC3,0x10,0x3B,0x88,0x32,0x02,0xE9,0x04,0x65,0x65),
442 },
443 {
444 /* ECC Brainpool 512-bit curve (RFC 5639), brainpoolP512r1 */
445 .group = ECP_512_BP,
446 .p = chunk_from_chars(
447 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
448 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x71,
449 0x7D,0x4D,0x9B,0x00,0x9B,0xC6,0x68,0x42,0xAE,0xCD,0xA1,0x2A,0xE6,0xA3,0x80,0xE6,
450 0x28,0x81,0xFF,0x2F,0x2D,0x82,0xC6,0x85,0x28,0xAA,0x60,0x56,0x58,0x3A,0x48,0xF3),
451 .a = chunk_from_chars(
452 0x78,0x30,0xA3,0x31,0x8B,0x60,0x3B,0x89,0xE2,0x32,0x71,0x45,0xAC,0x23,0x4C,0xC5,
453 0x94,0xCB,0xDD,0x8D,0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,
454 0x2D,0xED,0x5D,0x5A,0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,
455 0x7F,0x11,0x17,0xA7,0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA),
456 .b = chunk_from_chars(
457 0x3D,0xF9,0x16,0x10,0xA8,0x34,0x41,0xCA,0xEA,0x98,0x63,0xBC,0x2D,0xED,0x5D,0x5A,
458 0xA8,0x25,0x3A,0xA1,0x0A,0x2E,0xF1,0xC9,0x8B,0x9A,0xC8,0xB5,0x7F,0x11,0x17,0xA7,
459 0x2B,0xF2,0xC7,0xB9,0xE7,0xC1,0xAC,0x4D,0x77,0xFC,0x94,0xCA,0xDC,0x08,0x3E,0x67,
460 0x98,0x40,0x50,0xB7,0x5E,0xBA,0xE5,0xDD,0x28,0x09,0xBD,0x63,0x80,0x16,0xF7,0x23),
461 .x = chunk_from_chars(
462 0x81,0xAE,0xE4,0xBD,0xD8,0x2E,0xD9,0x64,0x5A,0x21,0x32,0x2E,0x9C,0x4C,0x6A,0x93,
463 0x85,0xED,0x9F,0x70,0xB5,0xD9,0x16,0xC1,0xB4,0x3B,0x62,0xEE,0xF4,0xD0,0x09,0x8E,
464 0xFF,0x3B,0x1F,0x78,0xE2,0xD0,0xD4,0x8D,0x50,0xD1,0x68,0x7B,0x93,0xB9,0x7D,0x5F,
465 0x7C,0x6D,0x50,0x47,0x40,0x6A,0x5E,0x68,0x8B,0x35,0x22,0x09,0xBC,0xB9,0xF8,0x22),
466 .y = chunk_from_chars(
467 0x7D,0xDE,0x38,0x5D,0x56,0x63,0x32,0xEC,0xC0,0xEA,0xBF,0xA9,0xCF,0x78,0x22,0xFD,
468 0xF2,0x09,0xF7,0x00,0x24,0xA5,0x7B,0x1A,0xA0,0x00,0xC5,0x5B,0x88,0x1F,0x81,0x11,
469 0xB2,0xDC,0xDE,0x49,0x4A,0x5F,0x48,0x5E,0x5B,0xCA,0x4B,0xD8,0x8A,0x27,0x63,0xAE,
470 0xD1,0xCA,0x2B,0x2F,0xA8,0xF0,0x54,0x06,0x78,0xCD,0x1E,0x0F,0x3A,0xD8,0x08,0x92),
471 .q = chunk_from_chars(
472 0xAA,0xDD,0x9D,0xB8,0xDB,0xE9,0xC4,0x8B,0x3F,0xD4,0xE6,0xAE,0x33,0xC9,0xFC,0x07,
473 0xCB,0x30,0x8D,0xB3,0xB3,0xC9,0xD2,0x0E,0xD6,0x63,0x9C,0xCA,0x70,0x33,0x08,0x70,
474 0x55,0x3E,0x5C,0x41,0x4C,0xA9,0x26,0x19,0x41,0x86,0x61,0x19,0x7F,0xAC,0x10,0x47,
475 0x1D,0xB1,0xD3,0x81,0x08,0x5D,0xDA,0xDD,0xB5,0x87,0x96,0x82,0x9C,0xA9,0x00,0x69),
476 },
477 };
478
479 /**
480 * Create an EC_GROUP object for an ECC Brainpool curve
481 */
482 EC_GROUP *ec_group_new_brainpool(bp_curve *curve)
483 {
484 BIGNUM *p, *a, *b, *x, *y, *q;
485 const BIGNUM *h;
486 EC_POINT *G = NULL;
487 EC_GROUP *group = NULL, *result = NULL;
488 BN_CTX *ctx = NULL;
489
490 ctx = BN_CTX_new();
491 p = BN_bin2bn(curve->p.ptr, curve->p.len, NULL);
492 a = BN_bin2bn(curve->a.ptr, curve->a.len, NULL);
493 b = BN_bin2bn(curve->b.ptr, curve->b.len, NULL);
494 x = BN_bin2bn(curve->x.ptr, curve->x.len, NULL);
495 y = BN_bin2bn(curve->y.ptr, curve->y.len, NULL);
496 q = BN_bin2bn(curve->q.ptr, curve->q.len, NULL);
497 /* all supported groups have a cofactor of 1 */
498 h = BN_value_one();
499 if (!ctx || !p || !a || !b || !x || !y || !q)
500 {
501 goto failed;
502 }
503 group = EC_GROUP_new_curve_GFp(p, a, b, ctx);
504 if (!group)
505 {
506 goto failed;
507 }
508 G = EC_POINT_new(group);
509 if (!G || !EC_POINT_set_affine_coordinates_GFp(group, G, x, y, ctx))
510 {
511 goto failed;
512 }
513 if (!EC_GROUP_set_generator(group, G, q, h))
514 {
515 goto failed;
516 }
517 result = group;
518
519 failed:
520 if (!result && group)
521 {
522 EC_GROUP_free(group);
523 }
524 if (G)
525 {
526 EC_POINT_free(G);
527 }
528 BN_CTX_free(ctx);
529 BN_free(p);
530 BN_free(a);
531 BN_free(b);
532 BN_free(x);
533 BN_free(y);
534 BN_free(q);
535 return result;
536 }
537
538 /**
539 * Create an EC_KEY for ECC Brainpool curves as defined above
540 */
541 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
542 {
543 bp_curve *curve = NULL;
544 EC_GROUP *ec_group;
545 EC_KEY *key = NULL;
546 int i;
547
548 for (i = 0; i < countof(bp_curves); i++)
549 {
550 if (bp_curves[i].group == group)
551 {
552 curve = &bp_curves[i];
553 }
554 }
555 if (!curve)
556 {
557 return NULL;
558 }
559 ec_group = ec_group_new_brainpool(curve);
560 if (!ec_group)
561 {
562 return NULL;
563 }
564 key = EC_KEY_new();
565 if (!key || !EC_KEY_set_group(key, ec_group))
566 {
567 EC_KEY_free(key);
568 key = NULL;
569 }
570 EC_GROUP_free(ec_group);
571 return key;
572 }
573
574 #else /* !NID_brainpoolP224r1 || ... */
575
576 /**
577 * Create an EC_KEY for ECC Brainpool curves as defined by OpenSSL
578 */
579 static EC_KEY *ec_key_new_brainpool(diffie_hellman_group_t group)
580 {
581 switch (group)
582 {
583 case ECP_224_BP:
584 return EC_KEY_new_by_curve_name(NID_brainpoolP224r1);
585 case ECP_256_BP:
586 return EC_KEY_new_by_curve_name(NID_brainpoolP256r1);
587 case ECP_384_BP:
588 return EC_KEY_new_by_curve_name(NID_brainpoolP384r1);
589 case ECP_512_BP:
590 return EC_KEY_new_by_curve_name(NID_brainpoolP512r1);
591 default:
592 return NULL;
593 }
594 }
595
596 #endif /* !NID_brainpoolP224r1 || ... */
597
598 /*
599 * Described in header.
600 */
601 openssl_ec_diffie_hellman_t *openssl_ec_diffie_hellman_create(diffie_hellman_group_t group)
602 {
603 private_openssl_ec_diffie_hellman_t *this;
604 EC_KEY *key = NULL;
605
606 switch (group)
607 {
608 case ECP_192_BIT:
609 key = EC_KEY_new_by_curve_name(NID_X9_62_prime192v1);
610 break;
611 case ECP_224_BIT:
612 key = EC_KEY_new_by_curve_name(NID_secp224r1);
613 break;
614 case ECP_256_BIT:
615 key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
616 break;
617 case ECP_384_BIT:
618 key = EC_KEY_new_by_curve_name(NID_secp384r1);
619 break;
620 case ECP_521_BIT:
621 key = EC_KEY_new_by_curve_name(NID_secp521r1);
622 break;
623 case ECP_224_BP:
624 case ECP_256_BP:
625 case ECP_384_BP:
626 case ECP_512_BP:
627 key = ec_key_new_brainpool(group);
628 break;
629 default:
630 break;
631 }
632
633 if (!key)
634 {
635 return NULL;
636 }
637
638 INIT(this,
639 .public = {
640 .dh = {
641 .get_shared_secret = _get_shared_secret,
642 .set_other_public_value = _set_other_public_value,
643 .get_my_public_value = _get_my_public_value,
644 .set_private_value = _set_private_value,
645 .get_dh_group = _get_dh_group,
646 .destroy = _destroy,
647 },
648 },
649 .group = group,
650 .ec_group = EC_GROUP_dup(EC_KEY_get0_group(key)),
651 );
652
653 /* generate an EC private (public) key */
654 if (!EC_KEY_generate_key(key))
655 {
656 EC_KEY_free(key);
657 destroy(this);
658 return NULL;
659 }
660
661 this->key = EVP_PKEY_new();
662 if (!this->key || !EVP_PKEY_assign_EC_KEY(this->key, key))
663 {
664 EC_KEY_free(key);
665 destroy(this);
666 return NULL;
667 }
668 return &this->public;
669 }
670 #endif /* OPENSSL_NO_EC */