]>
Commit | Line | Data |
---|---|---|
7e31164a | 1 | /* |
33388b44 | 2 | * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved. |
012c86ab | 3 | * |
a7f182b7 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
aa6bb135 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
012c86ab BM |
8 | */ |
9 | ||
579422c8 P |
10 | /* |
11 | * ECDSA low level APIs are deprecated for public use, but still ok for | |
12 | * internal use. | |
13 | */ | |
14 | #include "internal/deprecated.h" | |
15 | ||
5454829a | 16 | #include <string.h> |
706457b7 | 17 | #include "ec_local.h" |
0bee0e62 | 18 | #include <openssl/err.h> |
012c86ab BM |
19 | #include <openssl/asn1t.h> |
20 | #include <openssl/objects.h> | |
677963e5 | 21 | #include "internal/nelem.h" |
25f2138b | 22 | #include "crypto/asn1_dsa.h" |
012c86ab | 23 | |
f844f9eb | 24 | #ifndef FIPS_MODULE |
a9612d6c | 25 | |
012c86ab | 26 | /* some structures needed for the asn1 encoding */ |
012c86ab | 27 | typedef struct x9_62_pentanomial_st { |
6a32a3c0 RL |
28 | int32_t k1; |
29 | int32_t k2; | |
30 | int32_t k3; | |
0f113f3e | 31 | } X9_62_PENTANOMIAL; |
012c86ab | 32 | |
37af03d3 | 33 | typedef struct x9_62_characteristic_two_st { |
6a32a3c0 | 34 | int32_t m; |
0f113f3e MC |
35 | ASN1_OBJECT *type; |
36 | union { | |
37 | char *ptr; | |
38 | /* NID_X9_62_onBasis */ | |
39 | ASN1_NULL *onBasis; | |
40 | /* NID_X9_62_tpBasis */ | |
41 | ASN1_INTEGER *tpBasis; | |
42 | /* NID_X9_62_ppBasis */ | |
43 | X9_62_PENTANOMIAL *ppBasis; | |
44 | /* anything else */ | |
45 | ASN1_TYPE *other; | |
46 | } p; | |
47 | } X9_62_CHARACTERISTIC_TWO; | |
37af03d3 GT |
48 | |
49 | typedef struct x9_62_fieldid_st { | |
0f113f3e MC |
50 | ASN1_OBJECT *fieldType; |
51 | union { | |
52 | char *ptr; | |
53 | /* NID_X9_62_prime_field */ | |
54 | ASN1_INTEGER *prime; | |
55 | /* NID_X9_62_characteristic_two_field */ | |
56 | X9_62_CHARACTERISTIC_TWO *char_two; | |
57 | /* anything else */ | |
58 | ASN1_TYPE *other; | |
59 | } p; | |
60 | } X9_62_FIELDID; | |
37af03d3 | 61 | |
012c86ab | 62 | typedef struct x9_62_curve_st { |
0f113f3e MC |
63 | ASN1_OCTET_STRING *a; |
64 | ASN1_OCTET_STRING *b; | |
65 | ASN1_BIT_STRING *seed; | |
66 | } X9_62_CURVE; | |
012c86ab | 67 | |
03f880e4 | 68 | struct ec_parameters_st { |
6a32a3c0 | 69 | int32_t version; |
0f113f3e MC |
70 | X9_62_FIELDID *fieldID; |
71 | X9_62_CURVE *curve; | |
72 | ASN1_OCTET_STRING *base; | |
73 | ASN1_INTEGER *order; | |
74 | ASN1_INTEGER *cofactor; | |
03f880e4 | 75 | } /* ECPARAMETERS */ ; |
012c86ab | 76 | |
fe2f8aec TM |
77 | typedef enum { |
78 | ECPKPARAMETERS_TYPE_NAMED = 0, | |
79 | ECPKPARAMETERS_TYPE_EXPLICIT, | |
80 | ECPKPARAMETERS_TYPE_IMPLICIT | |
81 | } ecpk_parameters_type_t; | |
82 | ||
012c86ab | 83 | struct ecpk_parameters_st { |
0f113f3e MC |
84 | int type; |
85 | union { | |
86 | ASN1_OBJECT *named_curve; | |
87 | ECPARAMETERS *parameters; | |
88 | ASN1_NULL *implicitlyCA; | |
89 | } value; | |
90 | } /* ECPKPARAMETERS */ ; | |
012c86ab | 91 | |
14a7cfb3 BM |
92 | /* SEC1 ECPrivateKey */ |
93 | typedef struct ec_privatekey_st { | |
6a32a3c0 | 94 | int32_t version; |
0f113f3e MC |
95 | ASN1_OCTET_STRING *privateKey; |
96 | ECPKPARAMETERS *parameters; | |
97 | ASN1_BIT_STRING *publicKey; | |
98 | } EC_PRIVATEKEY; | |
14a7cfb3 | 99 | |
37af03d3 GT |
100 | /* the OpenSSL ASN.1 definitions */ |
101 | ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { | |
9612e157 RL |
102 | ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32), |
103 | ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32), | |
104 | ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32) | |
df2ee0e2 | 105 | } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL) |
012c86ab | 106 | |
37af03d3 GT |
107 | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
108 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) | |
109 | ||
110 | ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY); | |
012c86ab | 111 | |
37af03d3 | 112 | ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = { |
0f113f3e MC |
113 | ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)), |
114 | ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)), | |
115 | ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL)) | |
37af03d3 | 116 | } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL); |
012c86ab BM |
117 | |
118 | ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = { | |
9612e157 | 119 | ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32), |
0f113f3e MC |
120 | ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), |
121 | ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) | |
df2ee0e2 | 122 | } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) |
012c86ab | 123 | |
37af03d3 GT |
124 | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
125 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) | |
012c86ab | 126 | |
37af03d3 GT |
127 | ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); |
128 | ||
129 | ASN1_ADB(X9_62_FIELDID) = { | |
0f113f3e MC |
130 | ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)), |
131 | ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO)) | |
37af03d3 | 132 | } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL); |
012c86ab | 133 | |
37af03d3 | 134 | ASN1_SEQUENCE(X9_62_FIELDID) = { |
0f113f3e MC |
135 | ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT), |
136 | ASN1_ADB_OBJECT(X9_62_FIELDID) | |
df2ee0e2 | 137 | } static_ASN1_SEQUENCE_END(X9_62_FIELDID) |
012c86ab BM |
138 | |
139 | ASN1_SEQUENCE(X9_62_CURVE) = { | |
0f113f3e MC |
140 | ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING), |
141 | ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING), | |
142 | ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING) | |
df2ee0e2 | 143 | } static_ASN1_SEQUENCE_END(X9_62_CURVE) |
012c86ab | 144 | |
012c86ab | 145 | ASN1_SEQUENCE(ECPARAMETERS) = { |
9612e157 | 146 | ASN1_EMBED(ECPARAMETERS, version, INT32), |
0f113f3e MC |
147 | ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID), |
148 | ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE), | |
149 | ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING), | |
150 | ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), | |
151 | ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) | |
60b350a3 | 152 | } ASN1_SEQUENCE_END(ECPARAMETERS) |
012c86ab | 153 | |
37af03d3 GT |
154 | DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
155 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) | |
012c86ab BM |
156 | |
157 | ASN1_CHOICE(ECPKPARAMETERS) = { | |
0f113f3e MC |
158 | ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT), |
159 | ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), | |
160 | ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) | |
60b350a3 | 161 | } ASN1_CHOICE_END(ECPKPARAMETERS) |
012c86ab | 162 | |
9fdcc21f DO |
163 | DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS) |
164 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS) | |
165 | IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS) | |
012c86ab | 166 | |
0bee0e62 | 167 | ASN1_SEQUENCE(EC_PRIVATEKEY) = { |
9612e157 | 168 | ASN1_EMBED(EC_PRIVATEKEY, version, INT32), |
0f113f3e MC |
169 | ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), |
170 | ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), | |
171 | ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) | |
df2ee0e2 | 172 | } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY) |
0bee0e62 | 173 | |
9fdcc21f DO |
174 | DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY) |
175 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY) | |
176 | IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY) | |
0bee0e62 | 177 | |
7e31164a | 178 | /* some declarations of internal function */ |
012c86ab | 179 | |
0f113f3e | 180 | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ |
37af03d3 | 181 | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); |
0f113f3e | 182 | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ |
37af03d3 | 183 | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); |
7e31164a BM |
184 | |
185 | /* the function definitions */ | |
012c86ab | 186 | |
37af03d3 | 187 | static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) |
0f113f3e MC |
188 | { |
189 | int ok = 0, nid; | |
190 | BIGNUM *tmp = NULL; | |
191 | ||
192 | if (group == NULL || field == NULL) | |
193 | return 0; | |
194 | ||
195 | /* clear the old values (if necessary) */ | |
0dfb9398 | 196 | ASN1_OBJECT_free(field->fieldType); |
2ace7450 | 197 | ASN1_TYPE_free(field->p.other); |
0f113f3e | 198 | |
23ccae80 | 199 | nid = EC_GROUP_get_field_type(group); |
0f113f3e MC |
200 | /* set OID for the field */ |
201 | if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { | |
9311d0c4 | 202 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
0f113f3e MC |
203 | goto err; |
204 | } | |
205 | ||
206 | if (nid == NID_X9_62_prime_field) { | |
207 | if ((tmp = BN_new()) == NULL) { | |
9311d0c4 | 208 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
209 | goto err; |
210 | } | |
211 | /* the parameters are specified by the prime number p */ | |
9cc570d4 | 212 | if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { |
9311d0c4 | 213 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
214 | goto err; |
215 | } | |
216 | /* set the prime number */ | |
217 | field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); | |
218 | if (field->p.prime == NULL) { | |
9311d0c4 | 219 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
220 | goto err; |
221 | } | |
6903e2e7 | 222 | } else if (nid == NID_X9_62_characteristic_two_field) |
b3310161 | 223 | #ifdef OPENSSL_NO_EC2M |
0f113f3e | 224 | { |
9311d0c4 | 225 | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
0f113f3e MC |
226 | goto err; |
227 | } | |
b3310161 | 228 | #else |
0f113f3e MC |
229 | { |
230 | int field_type; | |
231 | X9_62_CHARACTERISTIC_TWO *char_two; | |
232 | ||
233 | field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); | |
234 | char_two = field->p.char_two; | |
235 | ||
236 | if (char_two == NULL) { | |
9311d0c4 | 237 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
238 | goto err; |
239 | } | |
240 | ||
241 | char_two->m = (long)EC_GROUP_get_degree(group); | |
242 | ||
243 | field_type = EC_GROUP_get_basis_type(group); | |
244 | ||
245 | if (field_type == 0) { | |
9311d0c4 | 246 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
247 | goto err; |
248 | } | |
249 | /* set base type OID */ | |
250 | if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { | |
9311d0c4 | 251 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
0f113f3e MC |
252 | goto err; |
253 | } | |
254 | ||
255 | if (field_type == NID_X9_62_tpBasis) { | |
256 | unsigned int k; | |
257 | ||
258 | if (!EC_GROUP_get_trinomial_basis(group, &k)) | |
259 | goto err; | |
260 | ||
261 | char_two->p.tpBasis = ASN1_INTEGER_new(); | |
90945fa3 | 262 | if (char_two->p.tpBasis == NULL) { |
9311d0c4 | 263 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
264 | goto err; |
265 | } | |
266 | if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { | |
9311d0c4 | 267 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
268 | goto err; |
269 | } | |
270 | } else if (field_type == NID_X9_62_ppBasis) { | |
271 | unsigned int k1, k2, k3; | |
272 | ||
273 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) | |
274 | goto err; | |
275 | ||
276 | char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); | |
90945fa3 | 277 | if (char_two->p.ppBasis == NULL) { |
9311d0c4 | 278 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
279 | goto err; |
280 | } | |
281 | ||
282 | /* set k? values */ | |
283 | char_two->p.ppBasis->k1 = (long)k1; | |
284 | char_two->p.ppBasis->k2 = (long)k2; | |
285 | char_two->p.ppBasis->k3 = (long)k3; | |
286 | } else { /* field_type == NID_X9_62_onBasis */ | |
287 | ||
288 | /* for ONB the parameters are (asn1) NULL */ | |
289 | char_two->p.onBasis = ASN1_NULL_new(); | |
90945fa3 | 290 | if (char_two->p.onBasis == NULL) { |
9311d0c4 | 291 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
292 | goto err; |
293 | } | |
294 | } | |
295 | } | |
b3310161 | 296 | #endif |
6903e2e7 | 297 | else { |
9311d0c4 | 298 | ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); |
6903e2e7 DSH |
299 | goto err; |
300 | } | |
012c86ab | 301 | |
0f113f3e | 302 | ok = 1; |
012c86ab | 303 | |
23a1d5e9 RS |
304 | err: |
305 | BN_free(tmp); | |
26a7d938 | 306 | return ok; |
012c86ab BM |
307 | } |
308 | ||
37af03d3 | 309 | static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) |
0f113f3e | 310 | { |
9cc570d4 | 311 | int ok = 0; |
0f113f3e | 312 | BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; |
fc6f579a DB |
313 | unsigned char *a_buf = NULL, *b_buf = NULL; |
314 | size_t len; | |
0f113f3e MC |
315 | |
316 | if (!group || !curve || !curve->a || !curve->b) | |
317 | return 0; | |
318 | ||
319 | if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { | |
9311d0c4 | 320 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
321 | goto err; |
322 | } | |
323 | ||
0f113f3e | 324 | /* get a and b */ |
9cc570d4 | 325 | if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { |
9311d0c4 | 326 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
9cc570d4 | 327 | goto err; |
0f113f3e | 328 | } |
0f113f3e | 329 | |
fc6f579a DB |
330 | /* |
331 | * Per SEC 1, the curve coefficients must be padded up to size. See C.2's | |
332 | * definition of Curve, C.1's definition of FieldElement, and 2.3.5's | |
333 | * definition of how to encode the field elements. | |
334 | */ | |
335 | len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8; | |
336 | if ((a_buf = OPENSSL_malloc(len)) == NULL | |
337 | || (b_buf = OPENSSL_malloc(len)) == NULL) { | |
9311d0c4 | 338 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
fc6f579a | 339 | goto err; |
0f113f3e | 340 | } |
fc6f579a DB |
341 | if (BN_bn2binpad(tmp_1, a_buf, len) < 0 |
342 | || BN_bn2binpad(tmp_2, b_buf, len) < 0) { | |
9311d0c4 | 343 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
fc6f579a | 344 | goto err; |
0f113f3e MC |
345 | } |
346 | ||
347 | /* set a and b */ | |
fc6f579a DB |
348 | if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len) |
349 | || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) { | |
9311d0c4 | 350 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
351 | goto err; |
352 | } | |
353 | ||
354 | /* set the seed (optional) */ | |
355 | if (group->seed) { | |
356 | if (!curve->seed) | |
357 | if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { | |
9311d0c4 | 358 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
359 | goto err; |
360 | } | |
361 | curve->seed->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
362 | curve->seed->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
363 | if (!ASN1_BIT_STRING_set(curve->seed, group->seed, | |
364 | (int)group->seed_len)) { | |
9311d0c4 | 365 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
366 | goto err; |
367 | } | |
368 | } else { | |
2ace7450 RS |
369 | ASN1_BIT_STRING_free(curve->seed); |
370 | curve->seed = NULL; | |
0f113f3e MC |
371 | } |
372 | ||
373 | ok = 1; | |
374 | ||
23a1d5e9 | 375 | err: |
fc6f579a DB |
376 | OPENSSL_free(a_buf); |
377 | OPENSSL_free(b_buf); | |
23a1d5e9 RS |
378 | BN_free(tmp_1); |
379 | BN_free(tmp_2); | |
26a7d938 | 380 | return ok; |
0f113f3e | 381 | } |
012c86ab | 382 | |
60b350a3 | 383 | ECPARAMETERS *EC_GROUP_get_ecparameters(const EC_GROUP *group, |
c0f39ded | 384 | ECPARAMETERS *params) |
0f113f3e | 385 | { |
0f113f3e MC |
386 | size_t len = 0; |
387 | ECPARAMETERS *ret = NULL; | |
be2e334f | 388 | const BIGNUM *tmp; |
0f113f3e MC |
389 | unsigned char *buffer = NULL; |
390 | const EC_POINT *point = NULL; | |
391 | point_conversion_form_t form; | |
f28bc7d3 | 392 | ASN1_INTEGER *orig; |
0f113f3e | 393 | |
60b350a3 | 394 | if (params == NULL) { |
0f113f3e | 395 | if ((ret = ECPARAMETERS_new()) == NULL) { |
9311d0c4 | 396 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
397 | goto err; |
398 | } | |
399 | } else | |
60b350a3 | 400 | ret = params; |
0f113f3e MC |
401 | |
402 | /* set the version (always one) */ | |
403 | ret->version = (long)0x1; | |
404 | ||
405 | /* set the fieldID */ | |
406 | if (!ec_asn1_group2fieldid(group, ret->fieldID)) { | |
9311d0c4 | 407 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
408 | goto err; |
409 | } | |
410 | ||
411 | /* set the curve */ | |
412 | if (!ec_asn1_group2curve(group, ret->curve)) { | |
9311d0c4 | 413 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
414 | goto err; |
415 | } | |
416 | ||
417 | /* set the base point */ | |
418 | if ((point = EC_GROUP_get0_generator(group)) == NULL) { | |
9311d0c4 | 419 | ERR_raise(ERR_LIB_EC, EC_R_UNDEFINED_GENERATOR); |
0f113f3e MC |
420 | goto err; |
421 | } | |
422 | ||
423 | form = EC_GROUP_get_point_conversion_form(group); | |
424 | ||
981bd8a2 | 425 | len = EC_POINT_point2buf(group, point, form, &buffer, NULL); |
0f113f3e | 426 | if (len == 0) { |
9311d0c4 | 427 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
428 | goto err; |
429 | } | |
0f113f3e | 430 | if (ret->base == NULL && (ret->base = ASN1_OCTET_STRING_new()) == NULL) { |
0110a470 | 431 | OPENSSL_free(buffer); |
9311d0c4 | 432 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
433 | goto err; |
434 | } | |
0110a470 | 435 | ASN1_STRING_set0(ret->base, buffer, len); |
0f113f3e MC |
436 | |
437 | /* set the order */ | |
be2e334f DSH |
438 | tmp = EC_GROUP_get0_order(group); |
439 | if (tmp == NULL) { | |
9311d0c4 | 440 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
441 | goto err; |
442 | } | |
f28bc7d3 | 443 | ret->order = BN_to_ASN1_INTEGER(tmp, orig = ret->order); |
0f113f3e | 444 | if (ret->order == NULL) { |
f28bc7d3 | 445 | ret->order = orig; |
9311d0c4 | 446 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
447 | goto err; |
448 | } | |
449 | ||
450 | /* set the cofactor (optional) */ | |
be2e334f DSH |
451 | tmp = EC_GROUP_get0_cofactor(group); |
452 | if (tmp != NULL) { | |
f28bc7d3 | 453 | ret->cofactor = BN_to_ASN1_INTEGER(tmp, orig = ret->cofactor); |
0f113f3e | 454 | if (ret->cofactor == NULL) { |
f28bc7d3 | 455 | ret->cofactor = orig; |
9311d0c4 | 456 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
457 | goto err; |
458 | } | |
459 | } | |
460 | ||
23a1d5e9 | 461 | return ret; |
0f113f3e | 462 | |
23a1d5e9 | 463 | err: |
60b350a3 | 464 | if (params == NULL) |
23a1d5e9 | 465 | ECPARAMETERS_free(ret); |
23a1d5e9 | 466 | return NULL; |
0f113f3e MC |
467 | } |
468 | ||
60b350a3 RS |
469 | ECPKPARAMETERS *EC_GROUP_get_ecpkparameters(const EC_GROUP *group, |
470 | ECPKPARAMETERS *params) | |
0f113f3e MC |
471 | { |
472 | int ok = 1, tmp; | |
473 | ECPKPARAMETERS *ret = params; | |
474 | ||
475 | if (ret == NULL) { | |
476 | if ((ret = ECPKPARAMETERS_new()) == NULL) { | |
9311d0c4 | 477 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
478 | return NULL; |
479 | } | |
480 | } else { | |
fe2f8aec | 481 | if (ret->type == ECPKPARAMETERS_TYPE_NAMED) |
0f113f3e | 482 | ASN1_OBJECT_free(ret->value.named_curve); |
fe2f8aec TM |
483 | else if (ret->type == ECPKPARAMETERS_TYPE_EXPLICIT |
484 | && ret->value.parameters != NULL) | |
0f113f3e MC |
485 | ECPARAMETERS_free(ret->value.parameters); |
486 | } | |
487 | ||
488 | if (EC_GROUP_get_asn1_flag(group)) { | |
489 | /* | |
436ad81f | 490 | * use the asn1 OID to describe the elliptic curve parameters |
0f113f3e MC |
491 | */ |
492 | tmp = EC_GROUP_get_curve_name(group); | |
493 | if (tmp) { | |
e0137ca9 NT |
494 | ASN1_OBJECT *asn1obj = OBJ_nid2obj(tmp); |
495 | ||
496 | if (asn1obj == NULL || OBJ_length(asn1obj) == 0) { | |
497 | ASN1_OBJECT_free(asn1obj); | |
9311d0c4 | 498 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_OID); |
0f113f3e | 499 | ok = 0; |
e0137ca9 | 500 | } else { |
fe2f8aec | 501 | ret->type = ECPKPARAMETERS_TYPE_NAMED; |
e0137ca9 NT |
502 | ret->value.named_curve = asn1obj; |
503 | } | |
0f113f3e | 504 | } else |
0d4fb843 | 505 | /* we don't know the nid => ERROR */ |
0f113f3e MC |
506 | ok = 0; |
507 | } else { | |
508 | /* use the ECPARAMETERS structure */ | |
fe2f8aec | 509 | ret->type = ECPKPARAMETERS_TYPE_EXPLICIT; |
0f113f3e | 510 | if ((ret->value.parameters = |
60b350a3 | 511 | EC_GROUP_get_ecparameters(group, NULL)) == NULL) |
0f113f3e MC |
512 | ok = 0; |
513 | } | |
514 | ||
515 | if (!ok) { | |
516 | ECPKPARAMETERS_free(ret); | |
517 | return NULL; | |
518 | } | |
519 | return ret; | |
520 | } | |
012c86ab | 521 | |
60b350a3 | 522 | EC_GROUP *EC_GROUP_new_from_ecparameters(const ECPARAMETERS *params) |
0f113f3e MC |
523 | { |
524 | int ok = 0, tmp; | |
bacaa618 | 525 | EC_GROUP *ret = NULL, *dup = NULL; |
0f113f3e MC |
526 | BIGNUM *p = NULL, *a = NULL, *b = NULL; |
527 | EC_POINT *point = NULL; | |
528 | long field_bits; | |
bacaa618 NT |
529 | int curve_name = NID_undef; |
530 | BN_CTX *ctx = NULL; | |
0f113f3e | 531 | |
12a765a5 RS |
532 | if (params->fieldID == NULL |
533 | || params->fieldID->fieldType == NULL | |
534 | || params->fieldID->p.ptr == NULL) { | |
9311d0c4 | 535 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
536 | goto err; |
537 | } | |
538 | ||
fc6f579a DB |
539 | /* |
540 | * Now extract the curve parameters a and b. Note that, although SEC 1 | |
541 | * specifies the length of their encodings, historical versions of OpenSSL | |
542 | * encoded them incorrectly, so we must accept any length for backwards | |
543 | * compatibility. | |
544 | */ | |
12a765a5 RS |
545 | if (params->curve == NULL |
546 | || params->curve->a == NULL || params->curve->a->data == NULL | |
547 | || params->curve->b == NULL || params->curve->b->data == NULL) { | |
9311d0c4 | 548 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
549 | goto err; |
550 | } | |
551 | a = BN_bin2bn(params->curve->a->data, params->curve->a->length, NULL); | |
552 | if (a == NULL) { | |
9311d0c4 | 553 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
0f113f3e MC |
554 | goto err; |
555 | } | |
556 | b = BN_bin2bn(params->curve->b->data, params->curve->b->length, NULL); | |
557 | if (b == NULL) { | |
9311d0c4 | 558 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
0f113f3e MC |
559 | goto err; |
560 | } | |
561 | ||
562 | /* get the field parameters */ | |
563 | tmp = OBJ_obj2nid(params->fieldID->fieldType); | |
564 | if (tmp == NID_X9_62_characteristic_two_field) | |
b3310161 | 565 | #ifdef OPENSSL_NO_EC2M |
0f113f3e | 566 | { |
9311d0c4 | 567 | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
0f113f3e MC |
568 | goto err; |
569 | } | |
b3310161 | 570 | #else |
0f113f3e MC |
571 | { |
572 | X9_62_CHARACTERISTIC_TWO *char_two; | |
573 | ||
574 | char_two = params->fieldID->p.char_two; | |
575 | ||
576 | field_bits = char_two->m; | |
577 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
9311d0c4 | 578 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
0f113f3e MC |
579 | goto err; |
580 | } | |
581 | ||
582 | if ((p = BN_new()) == NULL) { | |
9311d0c4 | 583 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
584 | goto err; |
585 | } | |
586 | ||
587 | /* get the base type */ | |
588 | tmp = OBJ_obj2nid(char_two->type); | |
589 | ||
590 | if (tmp == NID_X9_62_tpBasis) { | |
591 | long tmp_long; | |
592 | ||
593 | if (!char_two->p.tpBasis) { | |
9311d0c4 | 594 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
595 | goto err; |
596 | } | |
597 | ||
598 | tmp_long = ASN1_INTEGER_get(char_two->p.tpBasis); | |
599 | ||
600 | if (!(char_two->m > tmp_long && tmp_long > 0)) { | |
9311d0c4 | 601 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_TRINOMIAL_BASIS); |
0f113f3e MC |
602 | goto err; |
603 | } | |
604 | ||
605 | /* create the polynomial */ | |
606 | if (!BN_set_bit(p, (int)char_two->m)) | |
607 | goto err; | |
608 | if (!BN_set_bit(p, (int)tmp_long)) | |
609 | goto err; | |
610 | if (!BN_set_bit(p, 0)) | |
611 | goto err; | |
612 | } else if (tmp == NID_X9_62_ppBasis) { | |
613 | X9_62_PENTANOMIAL *penta; | |
614 | ||
615 | penta = char_two->p.ppBasis; | |
12a765a5 | 616 | if (penta == NULL) { |
9311d0c4 | 617 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
618 | goto err; |
619 | } | |
620 | ||
621 | if (! | |
622 | (char_two->m > penta->k3 && penta->k3 > penta->k2 | |
623 | && penta->k2 > penta->k1 && penta->k1 > 0)) { | |
9311d0c4 | 624 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_PENTANOMIAL_BASIS); |
0f113f3e MC |
625 | goto err; |
626 | } | |
627 | ||
628 | /* create the polynomial */ | |
629 | if (!BN_set_bit(p, (int)char_two->m)) | |
630 | goto err; | |
631 | if (!BN_set_bit(p, (int)penta->k1)) | |
632 | goto err; | |
633 | if (!BN_set_bit(p, (int)penta->k2)) | |
634 | goto err; | |
635 | if (!BN_set_bit(p, (int)penta->k3)) | |
636 | goto err; | |
637 | if (!BN_set_bit(p, 0)) | |
638 | goto err; | |
639 | } else if (tmp == NID_X9_62_onBasis) { | |
9311d0c4 | 640 | ERR_raise(ERR_LIB_EC, EC_R_NOT_IMPLEMENTED); |
0f113f3e MC |
641 | goto err; |
642 | } else { /* error */ | |
643 | ||
9311d0c4 | 644 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
645 | goto err; |
646 | } | |
647 | ||
648 | /* create the EC_GROUP structure */ | |
649 | ret = EC_GROUP_new_curve_GF2m(p, a, b, NULL); | |
650 | } | |
b3310161 | 651 | #endif |
0f113f3e MC |
652 | else if (tmp == NID_X9_62_prime_field) { |
653 | /* we have a curve over a prime field */ | |
654 | /* extract the prime number */ | |
12a765a5 | 655 | if (params->fieldID->p.prime == NULL) { |
9311d0c4 | 656 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
657 | goto err; |
658 | } | |
659 | p = ASN1_INTEGER_to_BN(params->fieldID->p.prime, NULL); | |
660 | if (p == NULL) { | |
9311d0c4 | 661 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
662 | goto err; |
663 | } | |
664 | ||
665 | if (BN_is_negative(p) || BN_is_zero(p)) { | |
9311d0c4 | 666 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
0f113f3e MC |
667 | goto err; |
668 | } | |
669 | ||
670 | field_bits = BN_num_bits(p); | |
671 | if (field_bits > OPENSSL_ECC_MAX_FIELD_BITS) { | |
9311d0c4 | 672 | ERR_raise(ERR_LIB_EC, EC_R_FIELD_TOO_LARGE); |
0f113f3e MC |
673 | goto err; |
674 | } | |
675 | ||
676 | /* create the EC_GROUP structure */ | |
677 | ret = EC_GROUP_new_curve_GFp(p, a, b, NULL); | |
678 | } else { | |
9311d0c4 | 679 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_FIELD); |
0f113f3e MC |
680 | goto err; |
681 | } | |
682 | ||
683 | if (ret == NULL) { | |
9311d0c4 | 684 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
685 | goto err; |
686 | } | |
687 | ||
688 | /* extract seed (optional) */ | |
689 | if (params->curve->seed != NULL) { | |
b548a1f1 | 690 | OPENSSL_free(ret->seed); |
75ebbd9a | 691 | if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) { |
9311d0c4 | 692 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
693 | goto err; |
694 | } | |
695 | memcpy(ret->seed, params->curve->seed->data, | |
696 | params->curve->seed->length); | |
697 | ret->seed_len = params->curve->seed->length; | |
698 | } | |
699 | ||
12a765a5 RS |
700 | if (params->order == NULL |
701 | || params->base == NULL | |
702 | || params->base->data == NULL) { | |
9311d0c4 | 703 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
704 | goto err; |
705 | } | |
706 | ||
707 | if ((point = EC_POINT_new(ret)) == NULL) | |
708 | goto err; | |
709 | ||
710 | /* set the point conversion form */ | |
711 | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) | |
712 | (params->base->data[0] & ~0x01)); | |
713 | ||
714 | /* extract the ec point */ | |
715 | if (!EC_POINT_oct2point(ret, point, params->base->data, | |
716 | params->base->length, NULL)) { | |
9311d0c4 | 717 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
718 | goto err; |
719 | } | |
720 | ||
721 | /* extract the order */ | |
722 | if ((a = ASN1_INTEGER_to_BN(params->order, a)) == NULL) { | |
9311d0c4 | 723 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
724 | goto err; |
725 | } | |
726 | if (BN_is_negative(a) || BN_is_zero(a)) { | |
9311d0c4 | 727 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
0f113f3e MC |
728 | goto err; |
729 | } | |
730 | if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ | |
9311d0c4 | 731 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
0f113f3e MC |
732 | goto err; |
733 | } | |
734 | ||
735 | /* extract the cofactor (optional) */ | |
736 | if (params->cofactor == NULL) { | |
23a1d5e9 RS |
737 | BN_free(b); |
738 | b = NULL; | |
0f113f3e | 739 | } else if ((b = ASN1_INTEGER_to_BN(params->cofactor, b)) == NULL) { |
9311d0c4 | 740 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
741 | goto err; |
742 | } | |
743 | /* set the generator, order and cofactor (if present) */ | |
744 | if (!EC_GROUP_set_generator(ret, point, a, b)) { | |
9311d0c4 | 745 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
746 | goto err; |
747 | } | |
748 | ||
bacaa618 NT |
749 | /* |
750 | * Check if the explicit parameters group just created matches one of the | |
751 | * built-in curves. | |
752 | * | |
753 | * We create a copy of the group just built, so that we can remove optional | |
754 | * fields for the lookup: we do this to avoid the possibility that one of | |
755 | * the optional parameters is used to force the library into using a less | |
756 | * performant and less secure EC_METHOD instead of the specialized one. | |
757 | * In any case, `seed` is not really used in any computation, while a | |
758 | * cofactor different from the one in the built-in table is just | |
759 | * mathematically wrong anyway and should not be used. | |
760 | */ | |
761 | if ((ctx = BN_CTX_new()) == NULL) { | |
9311d0c4 | 762 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
bacaa618 NT |
763 | goto err; |
764 | } | |
765 | if ((dup = EC_GROUP_dup(ret)) == NULL | |
766 | || EC_GROUP_set_seed(dup, NULL, 0) != 1 | |
767 | || !EC_GROUP_set_generator(dup, point, a, NULL)) { | |
9311d0c4 | 768 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
bacaa618 NT |
769 | goto err; |
770 | } | |
32ab57cb | 771 | if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { |
bacaa618 NT |
772 | /* |
773 | * The input explicit parameters successfully matched one of the | |
774 | * built-in curves: often for built-in curves we have specialized | |
775 | * methods with better performance and hardening. | |
776 | * | |
777 | * In this case we replace the `EC_GROUP` created through explicit | |
778 | * parameters with one created from a named group. | |
779 | */ | |
780 | EC_GROUP *named_group = NULL; | |
781 | ||
782 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | |
783 | /* | |
784 | * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for | |
785 | * the same curve, we prefer the SECP nid when matching explicit | |
786 | * parameters as that is associated with a specialized EC_METHOD. | |
787 | */ | |
788 | if (curve_name == NID_wap_wsg_idm_ecid_wtls12) | |
789 | curve_name = NID_secp224r1; | |
790 | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ | |
791 | ||
792 | if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) { | |
9311d0c4 | 793 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
bacaa618 NT |
794 | goto err; |
795 | } | |
796 | EC_GROUP_free(ret); | |
797 | ret = named_group; | |
798 | ||
799 | /* | |
800 | * Set the flag so that EC_GROUPs created from explicit parameters are | |
801 | * serialized using explicit parameters by default. | |
802 | */ | |
803 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); | |
f97a8af2 NT |
804 | |
805 | /* | |
806 | * If the input params do not contain the optional seed field we make | |
807 | * sure it is not added to the returned group. | |
808 | * | |
809 | * The seed field is not really used inside libcrypto anyway, and | |
810 | * adding it to parsed explicit parameter keys would alter their DER | |
811 | * encoding output (because of the extra field) which could impact | |
812 | * applications fingerprinting keys by their DER encoding. | |
813 | */ | |
814 | if (params->curve->seed == NULL) { | |
815 | if (EC_GROUP_set_seed(ret, NULL, 0) != 1) | |
816 | goto err; | |
817 | } | |
bacaa618 NT |
818 | } |
819 | ||
0f113f3e MC |
820 | ok = 1; |
821 | ||
8fdc3734 RS |
822 | err: |
823 | if (!ok) { | |
bacaa618 | 824 | EC_GROUP_free(ret); |
0f113f3e MC |
825 | ret = NULL; |
826 | } | |
bacaa618 | 827 | EC_GROUP_free(dup); |
0f113f3e | 828 | |
23a1d5e9 RS |
829 | BN_free(p); |
830 | BN_free(a); | |
831 | BN_free(b); | |
8fdc3734 | 832 | EC_POINT_free(point); |
bacaa618 NT |
833 | |
834 | BN_CTX_free(ctx); | |
835 | ||
26a7d938 | 836 | return ret; |
012c86ab BM |
837 | } |
838 | ||
60b350a3 | 839 | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) |
0f113f3e MC |
840 | { |
841 | EC_GROUP *ret = NULL; | |
842 | int tmp = 0; | |
843 | ||
844 | if (params == NULL) { | |
9311d0c4 | 845 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
0f113f3e MC |
846 | return NULL; |
847 | } | |
848 | ||
fe2f8aec TM |
849 | if (params->type == ECPKPARAMETERS_TYPE_NAMED) { |
850 | /* the curve is given by an OID */ | |
0f113f3e MC |
851 | tmp = OBJ_obj2nid(params->value.named_curve); |
852 | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { | |
9311d0c4 | 853 | ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
0f113f3e MC |
854 | return NULL; |
855 | } | |
856 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); | |
fe2f8aec TM |
857 | } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) { |
858 | /* the parameters are given by an ECPARAMETERS structure */ | |
60b350a3 | 859 | ret = EC_GROUP_new_from_ecparameters(params->value.parameters); |
0f113f3e | 860 | if (!ret) { |
9311d0c4 | 861 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
862 | return NULL; |
863 | } | |
e363534c | 864 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
fe2f8aec TM |
865 | } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) { |
866 | /* implicit parameters inherited from CA - unsupported */ | |
0f113f3e MC |
867 | return NULL; |
868 | } else { | |
9311d0c4 | 869 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
870 | return NULL; |
871 | } | |
872 | ||
873 | return ret; | |
874 | } | |
012c86ab | 875 | |
14a7cfb3 | 876 | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
012c86ab | 877 | |
6343829a | 878 | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) |
0f113f3e MC |
879 | { |
880 | EC_GROUP *group = NULL; | |
881 | ECPKPARAMETERS *params = NULL; | |
a46c9789 | 882 | const unsigned char *p = *in; |
0f113f3e | 883 | |
a46c9789 | 884 | if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { |
0f113f3e MC |
885 | ECPKPARAMETERS_free(params); |
886 | return NULL; | |
887 | } | |
888 | ||
60b350a3 | 889 | if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { |
0f113f3e MC |
890 | ECPKPARAMETERS_free(params); |
891 | return NULL; | |
892 | } | |
893 | ||
fe2f8aec TM |
894 | if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
895 | group->decoded_from_explicit_params = 1; | |
896 | ||
8fdc3734 | 897 | if (a) { |
bacaa618 | 898 | EC_GROUP_free(*a); |
0f113f3e | 899 | *a = group; |
8fdc3734 | 900 | } |
0f113f3e MC |
901 | |
902 | ECPKPARAMETERS_free(params); | |
a46c9789 | 903 | *in = p; |
26a7d938 | 904 | return group; |
0f113f3e | 905 | } |
012c86ab | 906 | |
14a7cfb3 | 907 | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) |
0f113f3e MC |
908 | { |
909 | int ret = 0; | |
60b350a3 | 910 | ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); |
0f113f3e | 911 | if (tmp == NULL) { |
9311d0c4 | 912 | ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE); |
0f113f3e MC |
913 | return 0; |
914 | } | |
915 | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { | |
9311d0c4 | 916 | ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE); |
0f113f3e MC |
917 | ECPKPARAMETERS_free(tmp); |
918 | return 0; | |
919 | } | |
920 | ECPKPARAMETERS_free(tmp); | |
26a7d938 | 921 | return ret; |
0f113f3e | 922 | } |
012c86ab | 923 | |
14a7cfb3 BM |
924 | /* some EC_KEY functions */ |
925 | ||
6343829a | 926 | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) |
0f113f3e | 927 | { |
0f113f3e MC |
928 | EC_KEY *ret = NULL; |
929 | EC_PRIVATEKEY *priv_key = NULL; | |
a46c9789 | 930 | const unsigned char *p = *in; |
0f113f3e | 931 | |
29844ea5 | 932 | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) |
0f113f3e | 933 | return NULL; |
0f113f3e MC |
934 | |
935 | if (a == NULL || *a == NULL) { | |
936 | if ((ret = EC_KEY_new()) == NULL) { | |
9311d0c4 | 937 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
938 | goto err; |
939 | } | |
0f113f3e MC |
940 | } else |
941 | ret = *a; | |
942 | ||
943 | if (priv_key->parameters) { | |
bacaa618 | 944 | EC_GROUP_free(ret->group); |
60b350a3 | 945 | ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); |
fe2f8aec TM |
946 | if (ret->group != NULL |
947 | && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT) | |
948 | ret->group->decoded_from_explicit_params = 1; | |
0f113f3e MC |
949 | } |
950 | ||
951 | if (ret->group == NULL) { | |
9311d0c4 | 952 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
953 | goto err; |
954 | } | |
955 | ||
956 | ret->version = priv_key->version; | |
957 | ||
958 | if (priv_key->privateKey) { | |
d810700b | 959 | ASN1_OCTET_STRING *pkey = priv_key->privateKey; |
17ebf85a | 960 | if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), |
d810700b | 961 | ASN1_STRING_length(pkey)) == 0) |
0f113f3e | 962 | goto err; |
0f113f3e | 963 | } else { |
9311d0c4 | 964 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
0f113f3e MC |
965 | goto err; |
966 | } | |
967 | ||
8fdc3734 | 968 | EC_POINT_clear_free(ret->pub_key); |
0f113f3e MC |
969 | ret->pub_key = EC_POINT_new(ret->group); |
970 | if (ret->pub_key == NULL) { | |
9311d0c4 | 971 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
972 | goto err; |
973 | } | |
974 | ||
975 | if (priv_key->publicKey) { | |
976 | const unsigned char *pub_oct; | |
977 | int pub_oct_len; | |
978 | ||
17ebf85a | 979 | pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); |
f422a514 | 980 | pub_oct_len = ASN1_STRING_length(priv_key->publicKey); |
6ea04154 | 981 | if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) { |
9311d0c4 | 982 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
983 | goto err; |
984 | } | |
985 | } else { | |
77470e98 DSH |
986 | if (ret->group->meth->keygenpub == NULL |
987 | || ret->group->meth->keygenpub(ret) == 0) | |
6903e2e7 | 988 | goto err; |
0f113f3e MC |
989 | /* Remember the original private-key-only encoding. */ |
990 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; | |
991 | } | |
992 | ||
9e442d48 MC |
993 | if (a) |
994 | *a = ret; | |
25aaa98a | 995 | EC_PRIVATEKEY_free(priv_key); |
a46c9789 | 996 | *in = p; |
4fe54d67 | 997 | ret->dirty_cnt++; |
26a7d938 | 998 | return ret; |
25aaa98a RS |
999 | |
1000 | err: | |
1001 | if (a == NULL || *a != ret) | |
1002 | EC_KEY_free(ret); | |
1003 | EC_PRIVATEKEY_free(priv_key); | |
1004 | return NULL; | |
0f113f3e MC |
1005 | } |
1006 | ||
9fdcc21f | 1007 | int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1008 | { |
1009 | int ret = 0, ok = 0; | |
7fc7d1a7 | 1010 | unsigned char *priv= NULL, *pub= NULL; |
8a41fa6f | 1011 | size_t privlen = 0, publen = 0; |
7fc7d1a7 | 1012 | |
0f113f3e MC |
1013 | EC_PRIVATEKEY *priv_key = NULL; |
1014 | ||
d810700b | 1015 | if (a == NULL || a->group == NULL || |
0f113f3e | 1016 | (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
9311d0c4 | 1017 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1018 | goto err; |
1019 | } | |
1020 | ||
1021 | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { | |
9311d0c4 | 1022 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1023 | goto err; |
1024 | } | |
1025 | ||
1026 | priv_key->version = a->version; | |
1027 | ||
7fc7d1a7 | 1028 | privlen = EC_KEY_priv2buf(a, &priv); |
30cd4ff2 | 1029 | |
7fc7d1a7 | 1030 | if (privlen == 0) { |
9311d0c4 | 1031 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
30cd4ff2 DE |
1032 | goto err; |
1033 | } | |
1034 | ||
7fc7d1a7 DSH |
1035 | ASN1_STRING_set0(priv_key->privateKey, priv, privlen); |
1036 | priv = NULL; | |
0f113f3e MC |
1037 | |
1038 | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { | |
1039 | if ((priv_key->parameters = | |
60b350a3 | 1040 | EC_GROUP_get_ecpkparameters(a->group, |
0f113f3e | 1041 | priv_key->parameters)) == NULL) { |
9311d0c4 | 1042 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1043 | goto err; |
1044 | } | |
1045 | } | |
1046 | ||
1047 | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { | |
f422a514 | 1048 | priv_key->publicKey = ASN1_BIT_STRING_new(); |
0f113f3e | 1049 | if (priv_key->publicKey == NULL) { |
9311d0c4 | 1050 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1051 | goto err; |
1052 | } | |
1053 | ||
7fc7d1a7 | 1054 | publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); |
0f113f3e | 1055 | |
7fc7d1a7 | 1056 | if (publen == 0) { |
9311d0c4 | 1057 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1058 | goto err; |
1059 | } | |
1060 | ||
1061 | priv_key->publicKey->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07); | |
1062 | priv_key->publicKey->flags |= ASN1_STRING_FLAG_BITS_LEFT; | |
7fc7d1a7 DSH |
1063 | ASN1_STRING_set0(priv_key->publicKey, pub, publen); |
1064 | pub = NULL; | |
0f113f3e MC |
1065 | } |
1066 | ||
1067 | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { | |
9311d0c4 | 1068 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1069 | goto err; |
1070 | } | |
1071 | ok = 1; | |
1072 | err: | |
7fc7d1a7 DSH |
1073 | OPENSSL_clear_free(priv, privlen); |
1074 | OPENSSL_free(pub); | |
25aaa98a | 1075 | EC_PRIVATEKEY_free(priv_key); |
0f113f3e MC |
1076 | return (ok ? ret : 0); |
1077 | } | |
14a7cfb3 | 1078 | |
9fdcc21f | 1079 | int i2d_ECParameters(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1080 | { |
1081 | if (a == NULL) { | |
9311d0c4 | 1082 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1083 | return 0; |
1084 | } | |
1085 | return i2d_ECPKParameters(a->group, out); | |
1086 | } | |
14a7cfb3 BM |
1087 | |
1088 | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) | |
0f113f3e MC |
1089 | { |
1090 | EC_KEY *ret; | |
1091 | ||
1092 | if (in == NULL || *in == NULL) { | |
9311d0c4 | 1093 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1094 | return NULL; |
1095 | } | |
1096 | ||
1097 | if (a == NULL || *a == NULL) { | |
1098 | if ((ret = EC_KEY_new()) == NULL) { | |
9311d0c4 | 1099 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1100 | return NULL; |
1101 | } | |
0f113f3e MC |
1102 | } else |
1103 | ret = *a; | |
1104 | ||
1105 | if (!d2i_ECPKParameters(&ret->group, in, len)) { | |
5e5d53d3 MC |
1106 | if (a == NULL || *a != ret) |
1107 | EC_KEY_free(ret); | |
4fe54d67 NT |
1108 | else |
1109 | ret->dirty_cnt++; | |
0f113f3e MC |
1110 | return NULL; |
1111 | } | |
4fe54d67 | 1112 | ret->dirty_cnt++; |
0f113f3e | 1113 | |
5e5d53d3 MC |
1114 | if (a) |
1115 | *a = ret; | |
1116 | ||
0f113f3e MC |
1117 | return ret; |
1118 | } | |
14a7cfb3 | 1119 | |
62e3163b | 1120 | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) |
0f113f3e MC |
1121 | { |
1122 | EC_KEY *ret = NULL; | |
1123 | ||
1124 | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { | |
1125 | /* | |
0d4fb843 | 1126 | * sorry, but a EC_GROUP-structure is necessary to set the public key |
0f113f3e | 1127 | */ |
9311d0c4 | 1128 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1129 | return 0; |
1130 | } | |
1131 | ret = *a; | |
4fe54d67 | 1132 | /* EC_KEY_opt2key updates dirty_cnt */ |
6ea04154 | 1133 | if (!EC_KEY_oct2key(ret, *in, len, NULL)) { |
9311d0c4 | 1134 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1135 | return 0; |
1136 | } | |
0f113f3e MC |
1137 | *in += len; |
1138 | return ret; | |
1139 | } | |
14a7cfb3 | 1140 | |
60c25873 | 1141 | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1142 | { |
1143 | size_t buf_len = 0; | |
1144 | int new_buffer = 0; | |
1145 | ||
1146 | if (a == NULL) { | |
9311d0c4 | 1147 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1148 | return 0; |
1149 | } | |
1150 | ||
1151 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, | |
1152 | a->conv_form, NULL, 0, NULL); | |
1153 | ||
1154 | if (out == NULL || buf_len == 0) | |
1155 | /* out == NULL => just return the length of the octet string */ | |
1156 | return buf_len; | |
1157 | ||
1158 | if (*out == NULL) { | |
1159 | if ((*out = OPENSSL_malloc(buf_len)) == NULL) { | |
9311d0c4 | 1160 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1161 | return 0; |
1162 | } | |
1163 | new_buffer = 1; | |
1164 | } | |
1165 | if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, | |
1166 | *out, buf_len, NULL)) { | |
9311d0c4 | 1167 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1168 | if (new_buffer) { |
1169 | OPENSSL_free(*out); | |
1170 | *out = NULL; | |
1171 | } | |
1172 | return 0; | |
1173 | } | |
1174 | if (!new_buffer) | |
1175 | *out += buf_len; | |
1176 | return buf_len; | |
1177 | } | |
2d3d00dc | 1178 | |
9fdcc21f DO |
1179 | DECLARE_ASN1_FUNCTIONS(ECDSA_SIG) |
1180 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG) | |
8cc44d97 | 1181 | |
f844f9eb | 1182 | #endif /* FIPS_MODULE */ |
a9612d6c | 1183 | |
8cc44d97 DSH |
1184 | ECDSA_SIG *ECDSA_SIG_new(void) |
1185 | { | |
1186 | ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); | |
1187 | if (sig == NULL) | |
9311d0c4 | 1188 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
8cc44d97 DSH |
1189 | return sig; |
1190 | } | |
1191 | ||
1192 | void ECDSA_SIG_free(ECDSA_SIG *sig) | |
1193 | { | |
1194 | if (sig == NULL) | |
1195 | return; | |
1196 | BN_clear_free(sig->r); | |
1197 | BN_clear_free(sig->s); | |
1198 | OPENSSL_free(sig); | |
1199 | } | |
7236e3c8 | 1200 | |
54846b7c DM |
1201 | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len) |
1202 | { | |
1203 | ECDSA_SIG *sig; | |
1204 | ||
1205 | if (len < 0) | |
1206 | return NULL; | |
1207 | if (psig != NULL && *psig != NULL) { | |
1208 | sig = *psig; | |
1209 | } else { | |
1210 | sig = ECDSA_SIG_new(); | |
1211 | if (sig == NULL) | |
1212 | return NULL; | |
1213 | } | |
1214 | if (sig->r == NULL) | |
1215 | sig->r = BN_new(); | |
1216 | if (sig->s == NULL) | |
1217 | sig->s = BN_new(); | |
a55b00bd | 1218 | if (ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { |
54846b7c DM |
1219 | if (psig == NULL || *psig == NULL) |
1220 | ECDSA_SIG_free(sig); | |
1221 | return NULL; | |
1222 | } | |
1223 | if (psig != NULL && *psig == NULL) | |
1224 | *psig = sig; | |
1225 | return sig; | |
1226 | } | |
1227 | ||
1228 | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout) | |
1229 | { | |
b8805834 | 1230 | BUF_MEM *buf = NULL; |
54846b7c | 1231 | size_t encoded_len; |
b8805834 | 1232 | WPACKET pkt; |
54846b7c | 1233 | |
b8805834 MC |
1234 | if (ppout == NULL) { |
1235 | if (!WPACKET_init_null(&pkt, 0)) | |
54846b7c | 1236 | return -1; |
b8805834 MC |
1237 | } else if (*ppout == NULL) { |
1238 | if ((buf = BUF_MEM_new()) == NULL | |
1239 | || !WPACKET_init_len(&pkt, buf, 0)) { | |
1240 | BUF_MEM_free(buf); | |
54846b7c | 1241 | return -1; |
b8805834 | 1242 | } |
54846b7c | 1243 | } else { |
b8805834 MC |
1244 | if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) |
1245 | return -1; | |
54846b7c | 1246 | } |
b8805834 MC |
1247 | |
1248 | if (!encode_der_dsa_sig(&pkt, sig->r, sig->s) | |
1249 | || !WPACKET_get_total_written(&pkt, &encoded_len) | |
1250 | || !WPACKET_finish(&pkt)) { | |
1251 | BUF_MEM_free(buf); | |
1252 | WPACKET_cleanup(&pkt); | |
54846b7c DM |
1253 | return -1; |
1254 | } | |
b8805834 MC |
1255 | |
1256 | if (ppout != NULL) { | |
1257 | if (*ppout == NULL) { | |
1258 | *ppout = (unsigned char *)buf->data; | |
1259 | buf->data = NULL; | |
1260 | BUF_MEM_free(buf); | |
1261 | } else { | |
1262 | *ppout += encoded_len; | |
1263 | } | |
1264 | } | |
1265 | ||
54846b7c DM |
1266 | return (int)encoded_len; |
1267 | } | |
1268 | ||
9267c11b | 1269 | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
7236e3c8 | 1270 | { |
91e7bcc2 | 1271 | if (pr != NULL) |
7236e3c8 | 1272 | *pr = sig->r; |
91e7bcc2 | 1273 | if (ps != NULL) |
7236e3c8 DSH |
1274 | *ps = sig->s; |
1275 | } | |
cf517a6d | 1276 | |
0396401d DMSP |
1277 | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) |
1278 | { | |
1279 | return sig->r; | |
1280 | } | |
1281 | ||
1282 | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) | |
1283 | { | |
1284 | return sig->s; | |
1285 | } | |
1286 | ||
7ca3ea22 | 1287 | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
6a571a18 | 1288 | { |
bbaa9dd8 TS |
1289 | if (r == NULL || s == NULL) |
1290 | return 0; | |
6a571a18 TS |
1291 | BN_clear_free(sig->r); |
1292 | BN_clear_free(sig->s); | |
1293 | sig->r = r; | |
1294 | sig->s = s; | |
1295 | return 1; | |
1296 | } | |
1297 | ||
88b4c612 | 1298 | int ECDSA_size(const EC_KEY *ec) |
cf517a6d | 1299 | { |
88b4c612 SL |
1300 | int ret; |
1301 | ECDSA_SIG sig; | |
cf517a6d | 1302 | const EC_GROUP *group; |
88b4c612 | 1303 | const BIGNUM *bn; |
cf517a6d | 1304 | |
88b4c612 | 1305 | if (ec == NULL) |
cf517a6d | 1306 | return 0; |
88b4c612 | 1307 | group = EC_KEY_get0_group(ec); |
cf517a6d DSH |
1308 | if (group == NULL) |
1309 | return 0; | |
1310 | ||
88b4c612 SL |
1311 | bn = EC_GROUP_get0_order(group); |
1312 | if (bn == NULL) | |
cf517a6d | 1313 | return 0; |
88b4c612 SL |
1314 | |
1315 | sig.r = sig.s = (BIGNUM *)bn; | |
1316 | ret = i2d_ECDSA_SIG(&sig, NULL); | |
1317 | ||
1318 | if (ret < 0) | |
1319 | ret = 0; | |
26a7d938 | 1320 | return ret; |
cf517a6d | 1321 | } |