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