]>
Commit | Line | Data |
---|---|---|
7e31164a | 1 | /* |
8020d79b | 2 | * Copyright 2002-2021 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" |
7c310e87 | 22 | #include "crypto/asn1.h" |
25f2138b | 23 | #include "crypto/asn1_dsa.h" |
012c86ab | 24 | |
f844f9eb | 25 | #ifndef FIPS_MODULE |
a9612d6c | 26 | |
012c86ab | 27 | /* some structures needed for the asn1 encoding */ |
012c86ab | 28 | typedef struct x9_62_pentanomial_st { |
6a32a3c0 RL |
29 | int32_t k1; |
30 | int32_t k2; | |
31 | int32_t k3; | |
0f113f3e | 32 | } X9_62_PENTANOMIAL; |
012c86ab | 33 | |
37af03d3 | 34 | typedef struct x9_62_characteristic_two_st { |
6a32a3c0 | 35 | int32_t m; |
0f113f3e MC |
36 | ASN1_OBJECT *type; |
37 | union { | |
38 | char *ptr; | |
39 | /* NID_X9_62_onBasis */ | |
40 | ASN1_NULL *onBasis; | |
41 | /* NID_X9_62_tpBasis */ | |
42 | ASN1_INTEGER *tpBasis; | |
43 | /* NID_X9_62_ppBasis */ | |
44 | X9_62_PENTANOMIAL *ppBasis; | |
45 | /* anything else */ | |
46 | ASN1_TYPE *other; | |
47 | } p; | |
48 | } X9_62_CHARACTERISTIC_TWO; | |
37af03d3 GT |
49 | |
50 | typedef struct x9_62_fieldid_st { | |
0f113f3e MC |
51 | ASN1_OBJECT *fieldType; |
52 | union { | |
53 | char *ptr; | |
54 | /* NID_X9_62_prime_field */ | |
55 | ASN1_INTEGER *prime; | |
56 | /* NID_X9_62_characteristic_two_field */ | |
57 | X9_62_CHARACTERISTIC_TWO *char_two; | |
58 | /* anything else */ | |
59 | ASN1_TYPE *other; | |
60 | } p; | |
61 | } X9_62_FIELDID; | |
37af03d3 | 62 | |
012c86ab | 63 | typedef struct x9_62_curve_st { |
0f113f3e MC |
64 | ASN1_OCTET_STRING *a; |
65 | ASN1_OCTET_STRING *b; | |
66 | ASN1_BIT_STRING *seed; | |
67 | } X9_62_CURVE; | |
012c86ab | 68 | |
03f880e4 | 69 | struct ec_parameters_st { |
6a32a3c0 | 70 | int32_t version; |
0f113f3e MC |
71 | X9_62_FIELDID *fieldID; |
72 | X9_62_CURVE *curve; | |
73 | ASN1_OCTET_STRING *base; | |
74 | ASN1_INTEGER *order; | |
75 | ASN1_INTEGER *cofactor; | |
03f880e4 | 76 | } /* ECPARAMETERS */ ; |
012c86ab | 77 | |
fe2f8aec TM |
78 | typedef enum { |
79 | ECPKPARAMETERS_TYPE_NAMED = 0, | |
80 | ECPKPARAMETERS_TYPE_EXPLICIT, | |
81 | ECPKPARAMETERS_TYPE_IMPLICIT | |
82 | } ecpk_parameters_type_t; | |
83 | ||
012c86ab | 84 | struct ecpk_parameters_st { |
0f113f3e MC |
85 | int type; |
86 | union { | |
87 | ASN1_OBJECT *named_curve; | |
88 | ECPARAMETERS *parameters; | |
89 | ASN1_NULL *implicitlyCA; | |
90 | } value; | |
91 | } /* ECPKPARAMETERS */ ; | |
012c86ab | 92 | |
14a7cfb3 BM |
93 | /* SEC1 ECPrivateKey */ |
94 | typedef struct ec_privatekey_st { | |
6a32a3c0 | 95 | int32_t version; |
0f113f3e MC |
96 | ASN1_OCTET_STRING *privateKey; |
97 | ECPKPARAMETERS *parameters; | |
98 | ASN1_BIT_STRING *publicKey; | |
99 | } EC_PRIVATEKEY; | |
14a7cfb3 | 100 | |
37af03d3 GT |
101 | /* the OpenSSL ASN.1 definitions */ |
102 | ASN1_SEQUENCE(X9_62_PENTANOMIAL) = { | |
9612e157 RL |
103 | ASN1_EMBED(X9_62_PENTANOMIAL, k1, INT32), |
104 | ASN1_EMBED(X9_62_PENTANOMIAL, k2, INT32), | |
105 | ASN1_EMBED(X9_62_PENTANOMIAL, k3, INT32) | |
df2ee0e2 | 106 | } static_ASN1_SEQUENCE_END(X9_62_PENTANOMIAL) |
012c86ab | 107 | |
37af03d3 GT |
108 | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) |
109 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_PENTANOMIAL) | |
110 | ||
111 | ASN1_ADB_TEMPLATE(char_two_def) = ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.other, ASN1_ANY); | |
012c86ab | 112 | |
37af03d3 | 113 | ASN1_ADB(X9_62_CHARACTERISTIC_TWO) = { |
0f113f3e MC |
114 | ADB_ENTRY(NID_X9_62_onBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.onBasis, ASN1_NULL)), |
115 | ADB_ENTRY(NID_X9_62_tpBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.tpBasis, ASN1_INTEGER)), | |
116 | ADB_ENTRY(NID_X9_62_ppBasis, ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, p.ppBasis, X9_62_PENTANOMIAL)) | |
37af03d3 | 117 | } ASN1_ADB_END(X9_62_CHARACTERISTIC_TWO, 0, type, 0, &char_two_def_tt, NULL); |
012c86ab BM |
118 | |
119 | ASN1_SEQUENCE(X9_62_CHARACTERISTIC_TWO) = { | |
9612e157 | 120 | ASN1_EMBED(X9_62_CHARACTERISTIC_TWO, m, INT32), |
0f113f3e MC |
121 | ASN1_SIMPLE(X9_62_CHARACTERISTIC_TWO, type, ASN1_OBJECT), |
122 | ASN1_ADB_OBJECT(X9_62_CHARACTERISTIC_TWO) | |
df2ee0e2 | 123 | } static_ASN1_SEQUENCE_END(X9_62_CHARACTERISTIC_TWO) |
012c86ab | 124 | |
37af03d3 GT |
125 | DECLARE_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) |
126 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(X9_62_CHARACTERISTIC_TWO) | |
012c86ab | 127 | |
37af03d3 GT |
128 | ASN1_ADB_TEMPLATE(fieldID_def) = ASN1_SIMPLE(X9_62_FIELDID, p.other, ASN1_ANY); |
129 | ||
130 | ASN1_ADB(X9_62_FIELDID) = { | |
0f113f3e MC |
131 | ADB_ENTRY(NID_X9_62_prime_field, ASN1_SIMPLE(X9_62_FIELDID, p.prime, ASN1_INTEGER)), |
132 | ADB_ENTRY(NID_X9_62_characteristic_two_field, ASN1_SIMPLE(X9_62_FIELDID, p.char_two, X9_62_CHARACTERISTIC_TWO)) | |
37af03d3 | 133 | } ASN1_ADB_END(X9_62_FIELDID, 0, fieldType, 0, &fieldID_def_tt, NULL); |
012c86ab | 134 | |
37af03d3 | 135 | ASN1_SEQUENCE(X9_62_FIELDID) = { |
0f113f3e MC |
136 | ASN1_SIMPLE(X9_62_FIELDID, fieldType, ASN1_OBJECT), |
137 | ASN1_ADB_OBJECT(X9_62_FIELDID) | |
df2ee0e2 | 138 | } static_ASN1_SEQUENCE_END(X9_62_FIELDID) |
012c86ab BM |
139 | |
140 | ASN1_SEQUENCE(X9_62_CURVE) = { | |
0f113f3e MC |
141 | ASN1_SIMPLE(X9_62_CURVE, a, ASN1_OCTET_STRING), |
142 | ASN1_SIMPLE(X9_62_CURVE, b, ASN1_OCTET_STRING), | |
143 | ASN1_OPT(X9_62_CURVE, seed, ASN1_BIT_STRING) | |
df2ee0e2 | 144 | } static_ASN1_SEQUENCE_END(X9_62_CURVE) |
012c86ab | 145 | |
012c86ab | 146 | ASN1_SEQUENCE(ECPARAMETERS) = { |
9612e157 | 147 | ASN1_EMBED(ECPARAMETERS, version, INT32), |
0f113f3e MC |
148 | ASN1_SIMPLE(ECPARAMETERS, fieldID, X9_62_FIELDID), |
149 | ASN1_SIMPLE(ECPARAMETERS, curve, X9_62_CURVE), | |
150 | ASN1_SIMPLE(ECPARAMETERS, base, ASN1_OCTET_STRING), | |
151 | ASN1_SIMPLE(ECPARAMETERS, order, ASN1_INTEGER), | |
152 | ASN1_OPT(ECPARAMETERS, cofactor, ASN1_INTEGER) | |
60b350a3 | 153 | } ASN1_SEQUENCE_END(ECPARAMETERS) |
012c86ab | 154 | |
37af03d3 GT |
155 | DECLARE_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) |
156 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ECPARAMETERS) | |
012c86ab BM |
157 | |
158 | ASN1_CHOICE(ECPKPARAMETERS) = { | |
0f113f3e MC |
159 | ASN1_SIMPLE(ECPKPARAMETERS, value.named_curve, ASN1_OBJECT), |
160 | ASN1_SIMPLE(ECPKPARAMETERS, value.parameters, ECPARAMETERS), | |
161 | ASN1_SIMPLE(ECPKPARAMETERS, value.implicitlyCA, ASN1_NULL) | |
60b350a3 | 162 | } ASN1_CHOICE_END(ECPKPARAMETERS) |
012c86ab | 163 | |
9fdcc21f DO |
164 | DECLARE_ASN1_FUNCTIONS(ECPKPARAMETERS) |
165 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECPKPARAMETERS, ECPKPARAMETERS) | |
166 | IMPLEMENT_ASN1_FUNCTIONS(ECPKPARAMETERS) | |
012c86ab | 167 | |
0bee0e62 | 168 | ASN1_SEQUENCE(EC_PRIVATEKEY) = { |
9612e157 | 169 | ASN1_EMBED(EC_PRIVATEKEY, version, INT32), |
0f113f3e MC |
170 | ASN1_SIMPLE(EC_PRIVATEKEY, privateKey, ASN1_OCTET_STRING), |
171 | ASN1_EXP_OPT(EC_PRIVATEKEY, parameters, ECPKPARAMETERS, 0), | |
172 | ASN1_EXP_OPT(EC_PRIVATEKEY, publicKey, ASN1_BIT_STRING, 1) | |
df2ee0e2 | 173 | } static_ASN1_SEQUENCE_END(EC_PRIVATEKEY) |
0bee0e62 | 174 | |
9fdcc21f DO |
175 | DECLARE_ASN1_FUNCTIONS(EC_PRIVATEKEY) |
176 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(EC_PRIVATEKEY, EC_PRIVATEKEY) | |
177 | IMPLEMENT_ASN1_FUNCTIONS(EC_PRIVATEKEY) | |
0bee0e62 | 178 | |
7e31164a | 179 | /* some declarations of internal function */ |
012c86ab | 180 | |
0f113f3e | 181 | /* ec_asn1_group2field() sets the values in a X9_62_FIELDID object */ |
37af03d3 | 182 | static int ec_asn1_group2fieldid(const EC_GROUP *, X9_62_FIELDID *); |
0f113f3e | 183 | /* ec_asn1_group2curve() sets the values in a X9_62_CURVE object */ |
37af03d3 | 184 | static int ec_asn1_group2curve(const EC_GROUP *, X9_62_CURVE *); |
7e31164a BM |
185 | |
186 | /* the function definitions */ | |
012c86ab | 187 | |
37af03d3 | 188 | static int ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) |
0f113f3e MC |
189 | { |
190 | int ok = 0, nid; | |
191 | BIGNUM *tmp = NULL; | |
192 | ||
193 | if (group == NULL || field == NULL) | |
194 | return 0; | |
195 | ||
196 | /* clear the old values (if necessary) */ | |
0dfb9398 | 197 | ASN1_OBJECT_free(field->fieldType); |
2ace7450 | 198 | ASN1_TYPE_free(field->p.other); |
0f113f3e | 199 | |
23ccae80 | 200 | nid = EC_GROUP_get_field_type(group); |
0f113f3e MC |
201 | /* set OID for the field */ |
202 | if ((field->fieldType = OBJ_nid2obj(nid)) == NULL) { | |
9311d0c4 | 203 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
0f113f3e MC |
204 | goto err; |
205 | } | |
206 | ||
207 | if (nid == NID_X9_62_prime_field) { | |
208 | if ((tmp = BN_new()) == NULL) { | |
9311d0c4 | 209 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
210 | goto err; |
211 | } | |
212 | /* the parameters are specified by the prime number p */ | |
9cc570d4 | 213 | if (!EC_GROUP_get_curve(group, tmp, NULL, NULL, NULL)) { |
9311d0c4 | 214 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
215 | goto err; |
216 | } | |
217 | /* set the prime number */ | |
218 | field->p.prime = BN_to_ASN1_INTEGER(tmp, NULL); | |
219 | if (field->p.prime == NULL) { | |
9311d0c4 | 220 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
221 | goto err; |
222 | } | |
6903e2e7 | 223 | } else if (nid == NID_X9_62_characteristic_two_field) |
b3310161 | 224 | #ifdef OPENSSL_NO_EC2M |
0f113f3e | 225 | { |
9311d0c4 | 226 | ERR_raise(ERR_LIB_EC, EC_R_GF2M_NOT_SUPPORTED); |
0f113f3e MC |
227 | goto err; |
228 | } | |
b3310161 | 229 | #else |
0f113f3e MC |
230 | { |
231 | int field_type; | |
232 | X9_62_CHARACTERISTIC_TWO *char_two; | |
233 | ||
234 | field->p.char_two = X9_62_CHARACTERISTIC_TWO_new(); | |
235 | char_two = field->p.char_two; | |
236 | ||
237 | if (char_two == NULL) { | |
9311d0c4 | 238 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
239 | goto err; |
240 | } | |
241 | ||
242 | char_two->m = (long)EC_GROUP_get_degree(group); | |
243 | ||
244 | field_type = EC_GROUP_get_basis_type(group); | |
245 | ||
246 | if (field_type == 0) { | |
9311d0c4 | 247 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
248 | goto err; |
249 | } | |
250 | /* set base type OID */ | |
251 | if ((char_two->type = OBJ_nid2obj(field_type)) == NULL) { | |
9311d0c4 | 252 | ERR_raise(ERR_LIB_EC, ERR_R_OBJ_LIB); |
0f113f3e MC |
253 | goto err; |
254 | } | |
255 | ||
256 | if (field_type == NID_X9_62_tpBasis) { | |
257 | unsigned int k; | |
258 | ||
259 | if (!EC_GROUP_get_trinomial_basis(group, &k)) | |
260 | goto err; | |
261 | ||
262 | char_two->p.tpBasis = ASN1_INTEGER_new(); | |
90945fa3 | 263 | if (char_two->p.tpBasis == NULL) { |
9311d0c4 | 264 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
265 | goto err; |
266 | } | |
267 | if (!ASN1_INTEGER_set(char_two->p.tpBasis, (long)k)) { | |
9311d0c4 | 268 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
269 | goto err; |
270 | } | |
271 | } else if (field_type == NID_X9_62_ppBasis) { | |
272 | unsigned int k1, k2, k3; | |
273 | ||
274 | if (!EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)) | |
275 | goto err; | |
276 | ||
277 | char_two->p.ppBasis = X9_62_PENTANOMIAL_new(); | |
90945fa3 | 278 | if (char_two->p.ppBasis == NULL) { |
9311d0c4 | 279 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
280 | goto err; |
281 | } | |
282 | ||
283 | /* set k? values */ | |
284 | char_two->p.ppBasis->k1 = (long)k1; | |
285 | char_two->p.ppBasis->k2 = (long)k2; | |
286 | char_two->p.ppBasis->k3 = (long)k3; | |
287 | } else { /* field_type == NID_X9_62_onBasis */ | |
288 | ||
289 | /* for ONB the parameters are (asn1) NULL */ | |
290 | char_two->p.onBasis = ASN1_NULL_new(); | |
90945fa3 | 291 | if (char_two->p.onBasis == NULL) { |
9311d0c4 | 292 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
293 | goto err; |
294 | } | |
295 | } | |
296 | } | |
b3310161 | 297 | #endif |
6903e2e7 | 298 | else { |
9311d0c4 | 299 | ERR_raise(ERR_LIB_EC, EC_R_UNSUPPORTED_FIELD); |
6903e2e7 DSH |
300 | goto err; |
301 | } | |
012c86ab | 302 | |
0f113f3e | 303 | ok = 1; |
012c86ab | 304 | |
23a1d5e9 RS |
305 | err: |
306 | BN_free(tmp); | |
26a7d938 | 307 | return ok; |
012c86ab BM |
308 | } |
309 | ||
37af03d3 | 310 | static int ec_asn1_group2curve(const EC_GROUP *group, X9_62_CURVE *curve) |
0f113f3e | 311 | { |
9cc570d4 | 312 | int ok = 0; |
0f113f3e | 313 | BIGNUM *tmp_1 = NULL, *tmp_2 = NULL; |
fc6f579a DB |
314 | unsigned char *a_buf = NULL, *b_buf = NULL; |
315 | size_t len; | |
0f113f3e MC |
316 | |
317 | if (!group || !curve || !curve->a || !curve->b) | |
318 | return 0; | |
319 | ||
320 | if ((tmp_1 = BN_new()) == NULL || (tmp_2 = BN_new()) == NULL) { | |
9311d0c4 | 321 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
322 | goto err; |
323 | } | |
324 | ||
0f113f3e | 325 | /* get a and b */ |
9cc570d4 | 326 | if (!EC_GROUP_get_curve(group, NULL, tmp_1, tmp_2, NULL)) { |
9311d0c4 | 327 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
9cc570d4 | 328 | goto err; |
0f113f3e | 329 | } |
0f113f3e | 330 | |
fc6f579a DB |
331 | /* |
332 | * Per SEC 1, the curve coefficients must be padded up to size. See C.2's | |
333 | * definition of Curve, C.1's definition of FieldElement, and 2.3.5's | |
334 | * definition of how to encode the field elements. | |
335 | */ | |
336 | len = ((size_t)EC_GROUP_get_degree(group) + 7) / 8; | |
337 | if ((a_buf = OPENSSL_malloc(len)) == NULL | |
338 | || (b_buf = OPENSSL_malloc(len)) == NULL) { | |
9311d0c4 | 339 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
fc6f579a | 340 | goto err; |
0f113f3e | 341 | } |
fc6f579a DB |
342 | if (BN_bn2binpad(tmp_1, a_buf, len) < 0 |
343 | || BN_bn2binpad(tmp_2, b_buf, len) < 0) { | |
9311d0c4 | 344 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
fc6f579a | 345 | goto err; |
0f113f3e MC |
346 | } |
347 | ||
348 | /* set a and b */ | |
fc6f579a DB |
349 | if (!ASN1_OCTET_STRING_set(curve->a, a_buf, len) |
350 | || !ASN1_OCTET_STRING_set(curve->b, b_buf, len)) { | |
9311d0c4 | 351 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
352 | goto err; |
353 | } | |
354 | ||
355 | /* set the seed (optional) */ | |
356 | if (group->seed) { | |
357 | if (!curve->seed) | |
358 | if ((curve->seed = ASN1_BIT_STRING_new()) == NULL) { | |
9311d0c4 | 359 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
360 | goto err; |
361 | } | |
7c310e87 | 362 | ossl_asn1_string_set_bits_left(curve->seed, 0); |
0f113f3e MC |
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 | ||
7aa3dfc4 | 488 | if (EC_GROUP_get_asn1_flag(group) == OPENSSL_EC_NAMED_CURVE) { |
0f113f3e | 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) { | |
97de6145 BE |
690 | /* |
691 | * This happens for instance with | |
692 | * fuzz/corpora/asn1/65cf44e85614c62f10cf3b7a7184c26293a19e4a | |
693 | * and causes the OPENSSL_malloc below to choke on the | |
694 | * zero length allocation request. | |
695 | */ | |
696 | if (params->curve->seed->length == 0) { | |
697 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); | |
698 | goto err; | |
699 | } | |
b548a1f1 | 700 | OPENSSL_free(ret->seed); |
75ebbd9a | 701 | if ((ret->seed = OPENSSL_malloc(params->curve->seed->length)) == NULL) { |
9311d0c4 | 702 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
703 | goto err; |
704 | } | |
705 | memcpy(ret->seed, params->curve->seed->data, | |
706 | params->curve->seed->length); | |
707 | ret->seed_len = params->curve->seed->length; | |
708 | } | |
709 | ||
12a765a5 RS |
710 | if (params->order == NULL |
711 | || params->base == NULL | |
030c5aba MC |
712 | || params->base->data == NULL |
713 | || params->base->length == 0) { | |
9311d0c4 | 714 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
715 | goto err; |
716 | } | |
717 | ||
718 | if ((point = EC_POINT_new(ret)) == NULL) | |
719 | goto err; | |
720 | ||
721 | /* set the point conversion form */ | |
722 | EC_GROUP_set_point_conversion_form(ret, (point_conversion_form_t) | |
723 | (params->base->data[0] & ~0x01)); | |
724 | ||
725 | /* extract the ec point */ | |
726 | if (!EC_POINT_oct2point(ret, point, params->base->data, | |
727 | params->base->length, NULL)) { | |
9311d0c4 | 728 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
729 | goto err; |
730 | } | |
731 | ||
732 | /* extract the order */ | |
be50862e | 733 | if (ASN1_INTEGER_to_BN(params->order, a) == NULL) { |
9311d0c4 | 734 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
735 | goto err; |
736 | } | |
737 | if (BN_is_negative(a) || BN_is_zero(a)) { | |
9311d0c4 | 738 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
0f113f3e MC |
739 | goto err; |
740 | } | |
741 | if (BN_num_bits(a) > (int)field_bits + 1) { /* Hasse bound */ | |
9311d0c4 | 742 | ERR_raise(ERR_LIB_EC, EC_R_INVALID_GROUP_ORDER); |
0f113f3e MC |
743 | goto err; |
744 | } | |
745 | ||
746 | /* extract the cofactor (optional) */ | |
747 | if (params->cofactor == NULL) { | |
23a1d5e9 RS |
748 | BN_free(b); |
749 | b = NULL; | |
be50862e | 750 | } else if (ASN1_INTEGER_to_BN(params->cofactor, b) == NULL) { |
9311d0c4 | 751 | ERR_raise(ERR_LIB_EC, ERR_R_ASN1_LIB); |
0f113f3e MC |
752 | goto err; |
753 | } | |
754 | /* set the generator, order and cofactor (if present) */ | |
755 | if (!EC_GROUP_set_generator(ret, point, a, b)) { | |
9311d0c4 | 756 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
757 | goto err; |
758 | } | |
759 | ||
bacaa618 NT |
760 | /* |
761 | * Check if the explicit parameters group just created matches one of the | |
762 | * built-in curves. | |
763 | * | |
764 | * We create a copy of the group just built, so that we can remove optional | |
765 | * fields for the lookup: we do this to avoid the possibility that one of | |
766 | * the optional parameters is used to force the library into using a less | |
767 | * performant and less secure EC_METHOD instead of the specialized one. | |
768 | * In any case, `seed` is not really used in any computation, while a | |
769 | * cofactor different from the one in the built-in table is just | |
770 | * mathematically wrong anyway and should not be used. | |
771 | */ | |
772 | if ((ctx = BN_CTX_new()) == NULL) { | |
9311d0c4 | 773 | ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB); |
bacaa618 NT |
774 | goto err; |
775 | } | |
776 | if ((dup = EC_GROUP_dup(ret)) == NULL | |
777 | || EC_GROUP_set_seed(dup, NULL, 0) != 1 | |
778 | || !EC_GROUP_set_generator(dup, point, a, NULL)) { | |
9311d0c4 | 779 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
bacaa618 NT |
780 | goto err; |
781 | } | |
32ab57cb | 782 | if ((curve_name = ossl_ec_curve_nid_from_params(dup, ctx)) != NID_undef) { |
bacaa618 NT |
783 | /* |
784 | * The input explicit parameters successfully matched one of the | |
785 | * built-in curves: often for built-in curves we have specialized | |
786 | * methods with better performance and hardening. | |
787 | * | |
788 | * In this case we replace the `EC_GROUP` created through explicit | |
789 | * parameters with one created from a named group. | |
790 | */ | |
791 | EC_GROUP *named_group = NULL; | |
792 | ||
793 | #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 | |
794 | /* | |
795 | * NID_wap_wsg_idm_ecid_wtls12 and NID_secp224r1 are both aliases for | |
796 | * the same curve, we prefer the SECP nid when matching explicit | |
797 | * parameters as that is associated with a specialized EC_METHOD. | |
798 | */ | |
799 | if (curve_name == NID_wap_wsg_idm_ecid_wtls12) | |
800 | curve_name = NID_secp224r1; | |
801 | #endif /* !def(OPENSSL_NO_EC_NISTP_64_GCC_128) */ | |
802 | ||
803 | if ((named_group = EC_GROUP_new_by_curve_name(curve_name)) == NULL) { | |
9311d0c4 | 804 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
bacaa618 NT |
805 | goto err; |
806 | } | |
807 | EC_GROUP_free(ret); | |
808 | ret = named_group; | |
809 | ||
810 | /* | |
811 | * Set the flag so that EC_GROUPs created from explicit parameters are | |
812 | * serialized using explicit parameters by default. | |
813 | */ | |
814 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); | |
f97a8af2 NT |
815 | |
816 | /* | |
817 | * If the input params do not contain the optional seed field we make | |
818 | * sure it is not added to the returned group. | |
819 | * | |
820 | * The seed field is not really used inside libcrypto anyway, and | |
821 | * adding it to parsed explicit parameter keys would alter their DER | |
822 | * encoding output (because of the extra field) which could impact | |
823 | * applications fingerprinting keys by their DER encoding. | |
824 | */ | |
825 | if (params->curve->seed == NULL) { | |
826 | if (EC_GROUP_set_seed(ret, NULL, 0) != 1) | |
827 | goto err; | |
828 | } | |
bacaa618 NT |
829 | } |
830 | ||
0f113f3e MC |
831 | ok = 1; |
832 | ||
8fdc3734 RS |
833 | err: |
834 | if (!ok) { | |
bacaa618 | 835 | EC_GROUP_free(ret); |
0f113f3e MC |
836 | ret = NULL; |
837 | } | |
bacaa618 | 838 | EC_GROUP_free(dup); |
0f113f3e | 839 | |
23a1d5e9 RS |
840 | BN_free(p); |
841 | BN_free(a); | |
842 | BN_free(b); | |
8fdc3734 | 843 | EC_POINT_free(point); |
bacaa618 NT |
844 | |
845 | BN_CTX_free(ctx); | |
846 | ||
26a7d938 | 847 | return ret; |
012c86ab BM |
848 | } |
849 | ||
60b350a3 | 850 | EC_GROUP *EC_GROUP_new_from_ecpkparameters(const ECPKPARAMETERS *params) |
0f113f3e MC |
851 | { |
852 | EC_GROUP *ret = NULL; | |
853 | int tmp = 0; | |
854 | ||
855 | if (params == NULL) { | |
9311d0c4 | 856 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS); |
0f113f3e MC |
857 | return NULL; |
858 | } | |
859 | ||
fe2f8aec TM |
860 | if (params->type == ECPKPARAMETERS_TYPE_NAMED) { |
861 | /* the curve is given by an OID */ | |
0f113f3e MC |
862 | tmp = OBJ_obj2nid(params->value.named_curve); |
863 | if ((ret = EC_GROUP_new_by_curve_name(tmp)) == NULL) { | |
9311d0c4 | 864 | ERR_raise(ERR_LIB_EC, EC_R_EC_GROUP_NEW_BY_NAME_FAILURE); |
0f113f3e MC |
865 | return NULL; |
866 | } | |
867 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_NAMED_CURVE); | |
fe2f8aec TM |
868 | } else if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) { |
869 | /* the parameters are given by an ECPARAMETERS structure */ | |
60b350a3 | 870 | ret = EC_GROUP_new_from_ecparameters(params->value.parameters); |
0f113f3e | 871 | if (!ret) { |
9311d0c4 | 872 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
873 | return NULL; |
874 | } | |
e363534c | 875 | EC_GROUP_set_asn1_flag(ret, OPENSSL_EC_EXPLICIT_CURVE); |
fe2f8aec TM |
876 | } else if (params->type == ECPKPARAMETERS_TYPE_IMPLICIT) { |
877 | /* implicit parameters inherited from CA - unsupported */ | |
0f113f3e MC |
878 | return NULL; |
879 | } else { | |
9311d0c4 | 880 | ERR_raise(ERR_LIB_EC, EC_R_ASN1_ERROR); |
0f113f3e MC |
881 | return NULL; |
882 | } | |
883 | ||
884 | return ret; | |
885 | } | |
012c86ab | 886 | |
14a7cfb3 | 887 | /* EC_GROUP <-> DER encoding of ECPKPARAMETERS */ |
012c86ab | 888 | |
6343829a | 889 | EC_GROUP *d2i_ECPKParameters(EC_GROUP **a, const unsigned char **in, long len) |
0f113f3e MC |
890 | { |
891 | EC_GROUP *group = NULL; | |
892 | ECPKPARAMETERS *params = NULL; | |
a46c9789 | 893 | const unsigned char *p = *in; |
0f113f3e | 894 | |
a46c9789 | 895 | if ((params = d2i_ECPKPARAMETERS(NULL, &p, len)) == NULL) { |
0f113f3e MC |
896 | ECPKPARAMETERS_free(params); |
897 | return NULL; | |
898 | } | |
899 | ||
60b350a3 | 900 | if ((group = EC_GROUP_new_from_ecpkparameters(params)) == NULL) { |
0f113f3e MC |
901 | ECPKPARAMETERS_free(params); |
902 | return NULL; | |
903 | } | |
904 | ||
fe2f8aec TM |
905 | if (params->type == ECPKPARAMETERS_TYPE_EXPLICIT) |
906 | group->decoded_from_explicit_params = 1; | |
907 | ||
8fdc3734 | 908 | if (a) { |
bacaa618 | 909 | EC_GROUP_free(*a); |
0f113f3e | 910 | *a = group; |
8fdc3734 | 911 | } |
0f113f3e MC |
912 | |
913 | ECPKPARAMETERS_free(params); | |
a46c9789 | 914 | *in = p; |
26a7d938 | 915 | return group; |
0f113f3e | 916 | } |
012c86ab | 917 | |
14a7cfb3 | 918 | int i2d_ECPKParameters(const EC_GROUP *a, unsigned char **out) |
0f113f3e MC |
919 | { |
920 | int ret = 0; | |
60b350a3 | 921 | ECPKPARAMETERS *tmp = EC_GROUP_get_ecpkparameters(a, NULL); |
0f113f3e | 922 | if (tmp == NULL) { |
9311d0c4 | 923 | ERR_raise(ERR_LIB_EC, EC_R_GROUP2PKPARAMETERS_FAILURE); |
0f113f3e MC |
924 | return 0; |
925 | } | |
926 | if ((ret = i2d_ECPKPARAMETERS(tmp, out)) == 0) { | |
9311d0c4 | 927 | ERR_raise(ERR_LIB_EC, EC_R_I2D_ECPKPARAMETERS_FAILURE); |
0f113f3e MC |
928 | ECPKPARAMETERS_free(tmp); |
929 | return 0; | |
930 | } | |
931 | ECPKPARAMETERS_free(tmp); | |
26a7d938 | 932 | return ret; |
0f113f3e | 933 | } |
012c86ab | 934 | |
14a7cfb3 BM |
935 | /* some EC_KEY functions */ |
936 | ||
6343829a | 937 | EC_KEY *d2i_ECPrivateKey(EC_KEY **a, const unsigned char **in, long len) |
0f113f3e | 938 | { |
0f113f3e MC |
939 | EC_KEY *ret = NULL; |
940 | EC_PRIVATEKEY *priv_key = NULL; | |
a46c9789 | 941 | const unsigned char *p = *in; |
0f113f3e | 942 | |
29844ea5 | 943 | if ((priv_key = d2i_EC_PRIVATEKEY(NULL, &p, len)) == NULL) |
0f113f3e | 944 | return NULL; |
0f113f3e MC |
945 | |
946 | if (a == NULL || *a == NULL) { | |
947 | if ((ret = EC_KEY_new()) == NULL) { | |
9311d0c4 | 948 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
949 | goto err; |
950 | } | |
0f113f3e MC |
951 | } else |
952 | ret = *a; | |
953 | ||
954 | if (priv_key->parameters) { | |
bacaa618 | 955 | EC_GROUP_free(ret->group); |
60b350a3 | 956 | ret->group = EC_GROUP_new_from_ecpkparameters(priv_key->parameters); |
fe2f8aec TM |
957 | if (ret->group != NULL |
958 | && priv_key->parameters->type == ECPKPARAMETERS_TYPE_EXPLICIT) | |
959 | ret->group->decoded_from_explicit_params = 1; | |
0f113f3e MC |
960 | } |
961 | ||
962 | if (ret->group == NULL) { | |
9311d0c4 | 963 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
964 | goto err; |
965 | } | |
966 | ||
967 | ret->version = priv_key->version; | |
968 | ||
969 | if (priv_key->privateKey) { | |
d810700b | 970 | ASN1_OCTET_STRING *pkey = priv_key->privateKey; |
17ebf85a | 971 | if (EC_KEY_oct2priv(ret, ASN1_STRING_get0_data(pkey), |
d810700b | 972 | ASN1_STRING_length(pkey)) == 0) |
0f113f3e | 973 | goto err; |
0f113f3e | 974 | } else { |
9311d0c4 | 975 | ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY); |
0f113f3e MC |
976 | goto err; |
977 | } | |
978 | ||
b4c4a2c6 TM |
979 | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) |
980 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); | |
981 | ||
8fdc3734 | 982 | EC_POINT_clear_free(ret->pub_key); |
0f113f3e MC |
983 | ret->pub_key = EC_POINT_new(ret->group); |
984 | if (ret->pub_key == NULL) { | |
9311d0c4 | 985 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
986 | goto err; |
987 | } | |
988 | ||
989 | if (priv_key->publicKey) { | |
990 | const unsigned char *pub_oct; | |
991 | int pub_oct_len; | |
992 | ||
17ebf85a | 993 | pub_oct = ASN1_STRING_get0_data(priv_key->publicKey); |
f422a514 | 994 | pub_oct_len = ASN1_STRING_length(priv_key->publicKey); |
6ea04154 | 995 | if (!EC_KEY_oct2key(ret, pub_oct, pub_oct_len, NULL)) { |
9311d0c4 | 996 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
997 | goto err; |
998 | } | |
999 | } else { | |
77470e98 DSH |
1000 | if (ret->group->meth->keygenpub == NULL |
1001 | || ret->group->meth->keygenpub(ret) == 0) | |
6903e2e7 | 1002 | goto err; |
0f113f3e MC |
1003 | /* Remember the original private-key-only encoding. */ |
1004 | ret->enc_flag |= EC_PKEY_NO_PUBKEY; | |
1005 | } | |
1006 | ||
9e442d48 MC |
1007 | if (a) |
1008 | *a = ret; | |
25aaa98a | 1009 | EC_PRIVATEKEY_free(priv_key); |
a46c9789 | 1010 | *in = p; |
4fe54d67 | 1011 | ret->dirty_cnt++; |
26a7d938 | 1012 | return ret; |
25aaa98a RS |
1013 | |
1014 | err: | |
1015 | if (a == NULL || *a != ret) | |
1016 | EC_KEY_free(ret); | |
1017 | EC_PRIVATEKEY_free(priv_key); | |
1018 | return NULL; | |
0f113f3e MC |
1019 | } |
1020 | ||
9fdcc21f | 1021 | int i2d_ECPrivateKey(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1022 | { |
1023 | int ret = 0, ok = 0; | |
7fc7d1a7 | 1024 | unsigned char *priv= NULL, *pub= NULL; |
8a41fa6f | 1025 | size_t privlen = 0, publen = 0; |
7fc7d1a7 | 1026 | |
0f113f3e MC |
1027 | EC_PRIVATEKEY *priv_key = NULL; |
1028 | ||
d810700b | 1029 | if (a == NULL || a->group == NULL || |
0f113f3e | 1030 | (!(a->enc_flag & EC_PKEY_NO_PUBKEY) && a->pub_key == NULL)) { |
9311d0c4 | 1031 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1032 | goto err; |
1033 | } | |
1034 | ||
1035 | if ((priv_key = EC_PRIVATEKEY_new()) == NULL) { | |
9311d0c4 | 1036 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1037 | goto err; |
1038 | } | |
1039 | ||
1040 | priv_key->version = a->version; | |
1041 | ||
7fc7d1a7 | 1042 | privlen = EC_KEY_priv2buf(a, &priv); |
30cd4ff2 | 1043 | |
7fc7d1a7 | 1044 | if (privlen == 0) { |
9311d0c4 | 1045 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
30cd4ff2 DE |
1046 | goto err; |
1047 | } | |
1048 | ||
7fc7d1a7 DSH |
1049 | ASN1_STRING_set0(priv_key->privateKey, priv, privlen); |
1050 | priv = NULL; | |
0f113f3e MC |
1051 | |
1052 | if (!(a->enc_flag & EC_PKEY_NO_PARAMETERS)) { | |
1053 | if ((priv_key->parameters = | |
60b350a3 | 1054 | EC_GROUP_get_ecpkparameters(a->group, |
0f113f3e | 1055 | priv_key->parameters)) == NULL) { |
9311d0c4 | 1056 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1057 | goto err; |
1058 | } | |
1059 | } | |
1060 | ||
1061 | if (!(a->enc_flag & EC_PKEY_NO_PUBKEY)) { | |
f422a514 | 1062 | priv_key->publicKey = ASN1_BIT_STRING_new(); |
0f113f3e | 1063 | if (priv_key->publicKey == NULL) { |
9311d0c4 | 1064 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1065 | goto err; |
1066 | } | |
1067 | ||
7fc7d1a7 | 1068 | publen = EC_KEY_key2buf(a, a->conv_form, &pub, NULL); |
0f113f3e | 1069 | |
7fc7d1a7 | 1070 | if (publen == 0) { |
9311d0c4 | 1071 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1072 | goto err; |
1073 | } | |
1074 | ||
7c310e87 | 1075 | ossl_asn1_string_set_bits_left(priv_key->publicKey, 0); |
7fc7d1a7 DSH |
1076 | ASN1_STRING_set0(priv_key->publicKey, pub, publen); |
1077 | pub = NULL; | |
0f113f3e MC |
1078 | } |
1079 | ||
1080 | if ((ret = i2d_EC_PRIVATEKEY(priv_key, out)) == 0) { | |
9311d0c4 | 1081 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1082 | goto err; |
1083 | } | |
1084 | ok = 1; | |
1085 | err: | |
7fc7d1a7 DSH |
1086 | OPENSSL_clear_free(priv, privlen); |
1087 | OPENSSL_free(pub); | |
25aaa98a | 1088 | EC_PRIVATEKEY_free(priv_key); |
0f113f3e MC |
1089 | return (ok ? ret : 0); |
1090 | } | |
14a7cfb3 | 1091 | |
9fdcc21f | 1092 | int i2d_ECParameters(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1093 | { |
1094 | if (a == NULL) { | |
9311d0c4 | 1095 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1096 | return 0; |
1097 | } | |
1098 | return i2d_ECPKParameters(a->group, out); | |
1099 | } | |
14a7cfb3 BM |
1100 | |
1101 | EC_KEY *d2i_ECParameters(EC_KEY **a, const unsigned char **in, long len) | |
0f113f3e MC |
1102 | { |
1103 | EC_KEY *ret; | |
1104 | ||
1105 | if (in == NULL || *in == NULL) { | |
9311d0c4 | 1106 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1107 | return NULL; |
1108 | } | |
1109 | ||
1110 | if (a == NULL || *a == NULL) { | |
1111 | if ((ret = EC_KEY_new()) == NULL) { | |
9311d0c4 | 1112 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1113 | return NULL; |
1114 | } | |
0f113f3e MC |
1115 | } else |
1116 | ret = *a; | |
1117 | ||
1118 | if (!d2i_ECPKParameters(&ret->group, in, len)) { | |
5e5d53d3 MC |
1119 | if (a == NULL || *a != ret) |
1120 | EC_KEY_free(ret); | |
4fe54d67 NT |
1121 | else |
1122 | ret->dirty_cnt++; | |
0f113f3e MC |
1123 | return NULL; |
1124 | } | |
b4c4a2c6 TM |
1125 | |
1126 | if (EC_GROUP_get_curve_name(ret->group) == NID_sm2) | |
1127 | EC_KEY_set_flags(ret, EC_FLAG_SM2_RANGE); | |
1128 | ||
4fe54d67 | 1129 | ret->dirty_cnt++; |
0f113f3e | 1130 | |
5e5d53d3 MC |
1131 | if (a) |
1132 | *a = ret; | |
1133 | ||
0f113f3e MC |
1134 | return ret; |
1135 | } | |
14a7cfb3 | 1136 | |
62e3163b | 1137 | EC_KEY *o2i_ECPublicKey(EC_KEY **a, const unsigned char **in, long len) |
0f113f3e MC |
1138 | { |
1139 | EC_KEY *ret = NULL; | |
1140 | ||
1141 | if (a == NULL || (*a) == NULL || (*a)->group == NULL) { | |
1142 | /* | |
0d4fb843 | 1143 | * sorry, but a EC_GROUP-structure is necessary to set the public key |
0f113f3e | 1144 | */ |
9311d0c4 | 1145 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1146 | return 0; |
1147 | } | |
1148 | ret = *a; | |
4fe54d67 | 1149 | /* EC_KEY_opt2key updates dirty_cnt */ |
6ea04154 | 1150 | if (!EC_KEY_oct2key(ret, *in, len, NULL)) { |
9311d0c4 | 1151 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1152 | return 0; |
1153 | } | |
0f113f3e MC |
1154 | *in += len; |
1155 | return ret; | |
1156 | } | |
14a7cfb3 | 1157 | |
60c25873 | 1158 | int i2o_ECPublicKey(const EC_KEY *a, unsigned char **out) |
0f113f3e MC |
1159 | { |
1160 | size_t buf_len = 0; | |
1161 | int new_buffer = 0; | |
1162 | ||
1163 | if (a == NULL) { | |
9311d0c4 | 1164 | ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER); |
0f113f3e MC |
1165 | return 0; |
1166 | } | |
1167 | ||
1168 | buf_len = EC_POINT_point2oct(a->group, a->pub_key, | |
1169 | a->conv_form, NULL, 0, NULL); | |
1170 | ||
1171 | if (out == NULL || buf_len == 0) | |
1172 | /* out == NULL => just return the length of the octet string */ | |
1173 | return buf_len; | |
1174 | ||
1175 | if (*out == NULL) { | |
1176 | if ((*out = OPENSSL_malloc(buf_len)) == NULL) { | |
9311d0c4 | 1177 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
1178 | return 0; |
1179 | } | |
1180 | new_buffer = 1; | |
1181 | } | |
1182 | if (!EC_POINT_point2oct(a->group, a->pub_key, a->conv_form, | |
1183 | *out, buf_len, NULL)) { | |
9311d0c4 | 1184 | ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB); |
0f113f3e MC |
1185 | if (new_buffer) { |
1186 | OPENSSL_free(*out); | |
1187 | *out = NULL; | |
1188 | } | |
1189 | return 0; | |
1190 | } | |
1191 | if (!new_buffer) | |
1192 | *out += buf_len; | |
1193 | return buf_len; | |
1194 | } | |
2d3d00dc | 1195 | |
9fdcc21f DO |
1196 | DECLARE_ASN1_FUNCTIONS(ECDSA_SIG) |
1197 | DECLARE_ASN1_ENCODE_FUNCTIONS_name(ECDSA_SIG, ECDSA_SIG) | |
8cc44d97 | 1198 | |
f844f9eb | 1199 | #endif /* FIPS_MODULE */ |
a9612d6c | 1200 | |
8cc44d97 DSH |
1201 | ECDSA_SIG *ECDSA_SIG_new(void) |
1202 | { | |
1203 | ECDSA_SIG *sig = OPENSSL_zalloc(sizeof(*sig)); | |
1204 | if (sig == NULL) | |
9311d0c4 | 1205 | ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE); |
8cc44d97 DSH |
1206 | return sig; |
1207 | } | |
1208 | ||
1209 | void ECDSA_SIG_free(ECDSA_SIG *sig) | |
1210 | { | |
1211 | if (sig == NULL) | |
1212 | return; | |
1213 | BN_clear_free(sig->r); | |
1214 | BN_clear_free(sig->s); | |
1215 | OPENSSL_free(sig); | |
1216 | } | |
7236e3c8 | 1217 | |
54846b7c DM |
1218 | ECDSA_SIG *d2i_ECDSA_SIG(ECDSA_SIG **psig, const unsigned char **ppin, long len) |
1219 | { | |
1220 | ECDSA_SIG *sig; | |
1221 | ||
1222 | if (len < 0) | |
1223 | return NULL; | |
1224 | if (psig != NULL && *psig != NULL) { | |
1225 | sig = *psig; | |
1226 | } else { | |
1227 | sig = ECDSA_SIG_new(); | |
1228 | if (sig == NULL) | |
1229 | return NULL; | |
1230 | } | |
1231 | if (sig->r == NULL) | |
1232 | sig->r = BN_new(); | |
1233 | if (sig->s == NULL) | |
1234 | sig->s = BN_new(); | |
9dddcd90 | 1235 | if (sig->r == NULL || sig->s == NULL |
1236 | || ossl_decode_der_dsa_sig(sig->r, sig->s, ppin, (size_t)len) == 0) { | |
54846b7c DM |
1237 | if (psig == NULL || *psig == NULL) |
1238 | ECDSA_SIG_free(sig); | |
1239 | return NULL; | |
1240 | } | |
1241 | if (psig != NULL && *psig == NULL) | |
1242 | *psig = sig; | |
1243 | return sig; | |
1244 | } | |
1245 | ||
1246 | int i2d_ECDSA_SIG(const ECDSA_SIG *sig, unsigned char **ppout) | |
1247 | { | |
b8805834 | 1248 | BUF_MEM *buf = NULL; |
54846b7c | 1249 | size_t encoded_len; |
b8805834 | 1250 | WPACKET pkt; |
54846b7c | 1251 | |
b8805834 MC |
1252 | if (ppout == NULL) { |
1253 | if (!WPACKET_init_null(&pkt, 0)) | |
54846b7c | 1254 | return -1; |
b8805834 MC |
1255 | } else if (*ppout == NULL) { |
1256 | if ((buf = BUF_MEM_new()) == NULL | |
1257 | || !WPACKET_init_len(&pkt, buf, 0)) { | |
1258 | BUF_MEM_free(buf); | |
54846b7c | 1259 | return -1; |
b8805834 | 1260 | } |
54846b7c | 1261 | } else { |
b8805834 MC |
1262 | if (!WPACKET_init_static_len(&pkt, *ppout, SIZE_MAX, 0)) |
1263 | return -1; | |
54846b7c | 1264 | } |
b8805834 | 1265 | |
2858156e | 1266 | if (!ossl_encode_der_dsa_sig(&pkt, sig->r, sig->s) |
b8805834 MC |
1267 | || !WPACKET_get_total_written(&pkt, &encoded_len) |
1268 | || !WPACKET_finish(&pkt)) { | |
1269 | BUF_MEM_free(buf); | |
1270 | WPACKET_cleanup(&pkt); | |
54846b7c DM |
1271 | return -1; |
1272 | } | |
b8805834 MC |
1273 | |
1274 | if (ppout != NULL) { | |
1275 | if (*ppout == NULL) { | |
1276 | *ppout = (unsigned char *)buf->data; | |
1277 | buf->data = NULL; | |
1278 | BUF_MEM_free(buf); | |
1279 | } else { | |
1280 | *ppout += encoded_len; | |
1281 | } | |
1282 | } | |
1283 | ||
54846b7c DM |
1284 | return (int)encoded_len; |
1285 | } | |
1286 | ||
9267c11b | 1287 | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
7236e3c8 | 1288 | { |
91e7bcc2 | 1289 | if (pr != NULL) |
7236e3c8 | 1290 | *pr = sig->r; |
91e7bcc2 | 1291 | if (ps != NULL) |
7236e3c8 DSH |
1292 | *ps = sig->s; |
1293 | } | |
cf517a6d | 1294 | |
0396401d DMSP |
1295 | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) |
1296 | { | |
1297 | return sig->r; | |
1298 | } | |
1299 | ||
1300 | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) | |
1301 | { | |
1302 | return sig->s; | |
1303 | } | |
1304 | ||
7ca3ea22 | 1305 | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
6a571a18 | 1306 | { |
bbaa9dd8 TS |
1307 | if (r == NULL || s == NULL) |
1308 | return 0; | |
6a571a18 TS |
1309 | BN_clear_free(sig->r); |
1310 | BN_clear_free(sig->s); | |
1311 | sig->r = r; | |
1312 | sig->s = s; | |
1313 | return 1; | |
1314 | } | |
1315 | ||
88b4c612 | 1316 | int ECDSA_size(const EC_KEY *ec) |
cf517a6d | 1317 | { |
88b4c612 SL |
1318 | int ret; |
1319 | ECDSA_SIG sig; | |
cf517a6d | 1320 | const EC_GROUP *group; |
88b4c612 | 1321 | const BIGNUM *bn; |
cf517a6d | 1322 | |
88b4c612 | 1323 | if (ec == NULL) |
cf517a6d | 1324 | return 0; |
88b4c612 | 1325 | group = EC_KEY_get0_group(ec); |
cf517a6d DSH |
1326 | if (group == NULL) |
1327 | return 0; | |
1328 | ||
88b4c612 SL |
1329 | bn = EC_GROUP_get0_order(group); |
1330 | if (bn == NULL) | |
cf517a6d | 1331 | return 0; |
88b4c612 SL |
1332 | |
1333 | sig.r = sig.s = (BIGNUM *)bn; | |
1334 | ret = i2d_ECDSA_SIG(&sig, NULL); | |
1335 | ||
1336 | if (ret < 0) | |
1337 | ret = 0; | |
26a7d938 | 1338 | return ret; |
cf517a6d | 1339 | } |