]>
Commit | Line | Data |
---|---|---|
448be743 DSH |
1 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL |
2 | * project 2006. | |
3 | */ | |
4 | /* ==================================================================== | |
35208f36 | 5 | * Copyright (c) 2006 The OpenSSL Project. All rights reserved. |
448be743 DSH |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | |
15 | * notice, this list of conditions and the following disclaimer in | |
16 | * the documentation and/or other materials provided with the | |
17 | * distribution. | |
18 | * | |
19 | * 3. All advertising materials mentioning features or use of this | |
20 | * software must display the following acknowledgment: | |
21 | * "This product includes software developed by the OpenSSL Project | |
22 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
23 | * | |
24 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
25 | * endorse or promote products derived from this software without | |
26 | * prior written permission. For written permission, please contact | |
27 | * licensing@OpenSSL.org. | |
28 | * | |
29 | * 5. Products derived from this software may not be called "OpenSSL" | |
30 | * nor may "OpenSSL" appear in their names without prior written | |
31 | * permission of the OpenSSL Project. | |
32 | * | |
33 | * 6. Redistributions of any form whatsoever must retain the following | |
34 | * acknowledgment: | |
35 | * "This product includes software developed by the OpenSSL Project | |
36 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
37 | * | |
38 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
39 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
40 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
41 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
42 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
43 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
44 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
45 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
46 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
47 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
49 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
50 | * ==================================================================== | |
51 | * | |
52 | * This product includes cryptographic software written by Eric Young | |
53 | * (eay@cryptsoft.com). This product includes software written by Tim | |
54 | * Hudson (tjh@cryptsoft.com). | |
55 | * | |
56 | */ | |
57 | ||
58 | #include <stdio.h> | |
59 | #include "cryptlib.h" | |
60 | #include <openssl/x509.h> | |
61 | #include <openssl/ec.h> | |
8931b30d DSH |
62 | #ifndef OPENSSL_NO_CMS |
63 | #include <openssl/cms.h> | |
64 | #endif | |
18e377b4 | 65 | #include "asn1_locl.h" |
448be743 DSH |
66 | |
67 | static int eckey_param2type(int *pptype, void **ppval, EC_KEY *ec_key) | |
68 | { | |
69 | const EC_GROUP *group; | |
70 | int nid; | |
71 | if (ec_key == NULL || (group = EC_KEY_get0_group(ec_key)) == NULL) | |
72 | { | |
73 | ECerr(EC_F_ECKEY_PARAM2TYPE, EC_R_MISSING_PARAMETERS); | |
74 | return 0; | |
75 | } | |
76 | if (EC_GROUP_get_asn1_flag(group) | |
77 | && (nid = EC_GROUP_get_curve_name(group))) | |
78 | /* we have a 'named curve' => just set the OID */ | |
79 | { | |
80 | *ppval = OBJ_nid2obj(nid); | |
81 | *pptype = V_ASN1_OBJECT; | |
82 | } | |
83 | else /* explicit parameters */ | |
84 | { | |
85 | ASN1_STRING *pstr = NULL; | |
86 | pstr = ASN1_STRING_new(); | |
87 | if (!pstr) | |
88 | return 0; | |
89 | pstr->length = i2d_ECParameters(ec_key, &pstr->data); | |
90 | if (pstr->length < 0) | |
91 | { | |
92 | ASN1_STRING_free(pstr); | |
93 | ECerr(EC_F_ECKEY_PARAM2TYPE, ERR_R_EC_LIB); | |
94 | return 0; | |
95 | } | |
96 | *ppval = pstr; | |
97 | *pptype = V_ASN1_SEQUENCE; | |
98 | } | |
99 | return 1; | |
100 | } | |
101 | ||
6f81892e | 102 | static int eckey_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
448be743 DSH |
103 | { |
104 | EC_KEY *ec_key = pkey->pkey.ec; | |
105 | void *pval = NULL; | |
106 | int ptype; | |
107 | unsigned char *penc = NULL, *p; | |
108 | int penclen; | |
109 | ||
110 | if (!eckey_param2type(&ptype, &pval, ec_key)) | |
111 | { | |
112 | ECerr(EC_F_ECKEY_PUB_ENCODE, ERR_R_EC_LIB); | |
113 | return 0; | |
114 | } | |
115 | penclen = i2o_ECPublicKey(ec_key, NULL); | |
116 | if (penclen <= 0) | |
117 | goto err; | |
118 | penc = OPENSSL_malloc(penclen); | |
119 | if (!penc) | |
120 | goto err; | |
121 | p = penc; | |
122 | penclen = i2o_ECPublicKey(ec_key, &p); | |
123 | if (penclen <= 0) | |
124 | goto err; | |
bd50e313 | 125 | if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_EC), |
448be743 DSH |
126 | ptype, pval, penc, penclen)) |
127 | return 1; | |
128 | err: | |
129 | if (ptype == V_ASN1_OBJECT) | |
130 | ASN1_OBJECT_free(pval); | |
131 | else | |
132 | ASN1_STRING_free(pval); | |
133 | if (penc) | |
134 | OPENSSL_free(penc); | |
135 | return 0; | |
136 | } | |
137 | ||
138 | static EC_KEY *eckey_type2param(int ptype, void *pval) | |
139 | { | |
140 | EC_KEY *eckey = NULL; | |
141 | if (ptype == V_ASN1_SEQUENCE) | |
142 | { | |
143 | ASN1_STRING *pstr = pval; | |
144 | const unsigned char *pm = NULL; | |
145 | int pmlen; | |
146 | pm = pstr->data; | |
147 | pmlen = pstr->length; | |
148 | if (!(eckey = d2i_ECParameters(NULL, &pm, pmlen))) | |
149 | { | |
150 | ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); | |
151 | goto ecerr; | |
152 | } | |
153 | } | |
154 | else if (ptype == V_ASN1_OBJECT) | |
155 | { | |
156 | ASN1_OBJECT *poid = pval; | |
157 | EC_GROUP *group; | |
158 | ||
159 | /* type == V_ASN1_OBJECT => the parameters are given | |
160 | * by an asn1 OID | |
161 | */ | |
162 | if ((eckey = EC_KEY_new()) == NULL) | |
163 | { | |
164 | ECerr(EC_F_ECKEY_TYPE2PARAM, ERR_R_MALLOC_FAILURE); | |
165 | goto ecerr; | |
166 | } | |
167 | group = EC_GROUP_new_by_curve_name(OBJ_obj2nid(poid)); | |
168 | if (group == NULL) | |
169 | goto ecerr; | |
170 | EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE); | |
171 | if (EC_KEY_set_group(eckey, group) == 0) | |
172 | goto ecerr; | |
173 | EC_GROUP_free(group); | |
174 | } | |
175 | else | |
176 | { | |
177 | ECerr(EC_F_ECKEY_TYPE2PARAM, EC_R_DECODE_ERROR); | |
178 | goto ecerr; | |
179 | } | |
180 | ||
181 | return eckey; | |
182 | ||
183 | ecerr: | |
184 | if (eckey) | |
185 | EC_KEY_free(eckey); | |
186 | return NULL; | |
187 | } | |
188 | ||
189 | static int eckey_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) | |
190 | { | |
191 | const unsigned char *p = NULL; | |
192 | void *pval; | |
193 | int ptype, pklen; | |
194 | EC_KEY *eckey = NULL; | |
195 | X509_ALGOR *palg; | |
196 | ||
197 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
198 | return 0; | |
199 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
200 | ||
201 | eckey = eckey_type2param(ptype, pval); | |
202 | ||
203 | if (!eckey) | |
204 | { | |
205 | ECerr(EC_F_ECKEY_PUB_DECODE, ERR_R_EC_LIB); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | /* We have parameters now set public key */ | |
210 | if (!o2i_ECPublicKey(&eckey, &p, pklen)) | |
211 | { | |
212 | ECerr(EC_F_ECKEY_PUB_DECODE, EC_R_DECODE_ERROR); | |
213 | goto ecerr; | |
214 | } | |
215 | ||
216 | EVP_PKEY_assign_EC_KEY(pkey, eckey); | |
217 | return 1; | |
218 | ||
219 | ecerr: | |
220 | if (eckey) | |
221 | EC_KEY_free(eckey); | |
222 | return 0; | |
223 | } | |
224 | ||
6f81892e DSH |
225 | static int eckey_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
226 | { | |
227 | int r; | |
228 | const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); | |
229 | const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), | |
230 | *pb = EC_KEY_get0_public_key(b->pkey.ec); | |
231 | r = EC_POINT_cmp(group, pa, pb, NULL); | |
232 | if (r == 0) | |
233 | return 1; | |
234 | if (r == 1) | |
235 | return 0; | |
236 | return -2; | |
237 | } | |
238 | ||
448be743 DSH |
239 | static int eckey_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) |
240 | { | |
241 | const unsigned char *p = NULL; | |
242 | void *pval; | |
243 | int ptype, pklen; | |
244 | EC_KEY *eckey = NULL; | |
245 | X509_ALGOR *palg; | |
246 | ||
247 | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) | |
248 | return 0; | |
249 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
250 | ||
251 | eckey = eckey_type2param(ptype, pval); | |
252 | ||
253 | if (!eckey) | |
254 | goto ecliberr; | |
255 | ||
256 | /* We have parameters now set private key */ | |
257 | if (!d2i_ECPrivateKey(&eckey, &p, pklen)) | |
258 | { | |
259 | ECerr(EC_F_ECKEY_PRIV_DECODE, EC_R_DECODE_ERROR); | |
260 | goto ecerr; | |
261 | } | |
262 | ||
263 | /* calculate public key (if necessary) */ | |
264 | if (EC_KEY_get0_public_key(eckey) == NULL) | |
265 | { | |
266 | const BIGNUM *priv_key; | |
267 | const EC_GROUP *group; | |
268 | EC_POINT *pub_key; | |
269 | /* the public key was not included in the SEC1 private | |
270 | * key => calculate the public key */ | |
271 | group = EC_KEY_get0_group(eckey); | |
272 | pub_key = EC_POINT_new(group); | |
273 | if (pub_key == NULL) | |
274 | { | |
275 | ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); | |
276 | goto ecliberr; | |
277 | } | |
278 | if (!EC_POINT_copy(pub_key, EC_GROUP_get0_generator(group))) | |
279 | { | |
280 | EC_POINT_free(pub_key); | |
281 | ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); | |
282 | goto ecliberr; | |
283 | } | |
284 | priv_key = EC_KEY_get0_private_key(eckey); | |
285 | if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, NULL)) | |
286 | { | |
287 | EC_POINT_free(pub_key); | |
288 | ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); | |
289 | goto ecliberr; | |
290 | } | |
291 | if (EC_KEY_set_public_key(eckey, pub_key) == 0) | |
292 | { | |
293 | EC_POINT_free(pub_key); | |
294 | ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); | |
295 | goto ecliberr; | |
296 | } | |
297 | EC_POINT_free(pub_key); | |
298 | } | |
299 | ||
300 | EVP_PKEY_assign_EC_KEY(pkey, eckey); | |
301 | return 1; | |
302 | ||
303 | ecliberr: | |
304 | ECerr(EC_F_ECKEY_PRIV_DECODE, ERR_R_EC_LIB); | |
305 | ecerr: | |
306 | if (eckey) | |
307 | EC_KEY_free(eckey); | |
308 | return 0; | |
309 | } | |
310 | ||
6f81892e | 311 | static int eckey_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
448be743 DSH |
312 | { |
313 | EC_KEY *ec_key; | |
314 | unsigned char *ep, *p; | |
315 | int eplen, ptype; | |
316 | void *pval; | |
317 | unsigned int tmp_flags, old_flags; | |
318 | ||
319 | ec_key = pkey->pkey.ec; | |
320 | ||
321 | if (!eckey_param2type(&ptype, &pval, ec_key)) | |
322 | { | |
323 | ECerr(EC_F_ECKEY_PRIV_ENCODE, EC_R_DECODE_ERROR); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | /* set the private key */ | |
328 | ||
329 | /* do not include the parameters in the SEC1 private key | |
330 | * see PKCS#11 12.11 */ | |
331 | old_flags = EC_KEY_get_enc_flags(ec_key); | |
332 | tmp_flags = old_flags | EC_PKEY_NO_PARAMETERS; | |
333 | EC_KEY_set_enc_flags(ec_key, tmp_flags); | |
334 | eplen = i2d_ECPrivateKey(ec_key, NULL); | |
335 | if (!eplen) | |
336 | { | |
337 | EC_KEY_set_enc_flags(ec_key, old_flags); | |
338 | ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); | |
339 | return 0; | |
340 | } | |
341 | ep = (unsigned char *) OPENSSL_malloc(eplen); | |
342 | if (!ep) | |
343 | { | |
344 | EC_KEY_set_enc_flags(ec_key, old_flags); | |
345 | ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_MALLOC_FAILURE); | |
346 | return 0; | |
347 | } | |
348 | p = ep; | |
349 | if (!i2d_ECPrivateKey(ec_key, &p)) | |
350 | { | |
351 | EC_KEY_set_enc_flags(ec_key, old_flags); | |
352 | OPENSSL_free(ep); | |
353 | ECerr(EC_F_ECKEY_PRIV_ENCODE, ERR_R_EC_LIB); | |
354 | } | |
355 | /* restore old encoding flags */ | |
356 | EC_KEY_set_enc_flags(ec_key, old_flags); | |
357 | ||
358 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_X9_62_id_ecPublicKey), 0, | |
359 | ptype, pval, ep, eplen)) | |
360 | return 0; | |
361 | ||
362 | return 1; | |
363 | } | |
364 | ||
6f81892e DSH |
365 | static int int_ec_size(const EVP_PKEY *pkey) |
366 | { | |
367 | return ECDSA_size(pkey->pkey.ec); | |
368 | } | |
369 | ||
370 | static int ec_bits(const EVP_PKEY *pkey) | |
371 | { | |
372 | BIGNUM *order = BN_new(); | |
373 | const EC_GROUP *group; | |
374 | int ret; | |
375 | ||
376 | if (!order) | |
377 | { | |
378 | ERR_clear_error(); | |
379 | return 0; | |
380 | } | |
381 | group = EC_KEY_get0_group(pkey->pkey.ec); | |
382 | if (!EC_GROUP_get_order(group, order, NULL)) | |
383 | { | |
384 | ERR_clear_error(); | |
385 | return 0; | |
386 | } | |
387 | ||
388 | ret = BN_num_bits(order); | |
389 | BN_free(order); | |
390 | return ret; | |
391 | } | |
392 | ||
393 | static int ec_missing_parameters(const EVP_PKEY *pkey) | |
394 | { | |
395 | if (EC_KEY_get0_group(pkey->pkey.ec) == NULL) | |
396 | return 1; | |
397 | return 0; | |
398 | } | |
399 | ||
930b0c4b | 400 | static int ec_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) |
6f81892e DSH |
401 | { |
402 | EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); | |
403 | if (group == NULL) | |
404 | return 0; | |
405 | if (EC_KEY_set_group(to->pkey.ec, group) == 0) | |
406 | return 0; | |
407 | EC_GROUP_free(group); | |
408 | return 1; | |
409 | } | |
410 | ||
930b0c4b | 411 | static int ec_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) |
6f81892e DSH |
412 | { |
413 | const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), | |
414 | *group_b = EC_KEY_get0_group(b->pkey.ec); | |
415 | if (EC_GROUP_cmp(group_a, group_b, NULL)) | |
416 | return 0; | |
417 | else | |
418 | return 1; | |
419 | } | |
420 | ||
421 | static void int_ec_free(EVP_PKEY *pkey) | |
422 | { | |
423 | EC_KEY_free(pkey->pkey.ec); | |
424 | } | |
425 | ||
35208f36 DSH |
426 | static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) |
427 | { | |
428 | unsigned char *buffer=NULL; | |
429 | const char *ecstr; | |
430 | size_t buf_len=0, i; | |
431 | int ret=0, reason=ERR_R_BIO_LIB; | |
432 | BIGNUM *pub_key=NULL, *order=NULL; | |
433 | BN_CTX *ctx=NULL; | |
434 | const EC_GROUP *group; | |
435 | const EC_POINT *public_key; | |
436 | const BIGNUM *priv_key; | |
437 | ||
438 | if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) | |
439 | { | |
440 | reason = ERR_R_PASSED_NULL_PARAMETER; | |
441 | goto err; | |
442 | } | |
443 | ||
444 | ctx = BN_CTX_new(); | |
445 | if (ctx == NULL) | |
446 | { | |
447 | reason = ERR_R_MALLOC_FAILURE; | |
448 | goto err; | |
449 | } | |
450 | ||
35208f36 DSH |
451 | if (ktype > 0) |
452 | { | |
453 | public_key = EC_KEY_get0_public_key(x); | |
454 | if ((pub_key = EC_POINT_point2bn(group, public_key, | |
455 | EC_KEY_get_conv_form(x), NULL, ctx)) == NULL) | |
456 | { | |
457 | reason = ERR_R_EC_LIB; | |
458 | goto err; | |
459 | } | |
b2c0518e DSH |
460 | if (pub_key) |
461 | buf_len = (size_t)BN_num_bytes(pub_key); | |
35208f36 | 462 | } |
35208f36 DSH |
463 | |
464 | if (ktype == 2) | |
465 | { | |
35208f36 | 466 | priv_key = EC_KEY_get0_private_key(x); |
b2c0518e DSH |
467 | if (priv_key && (i = (size_t)BN_num_bytes(priv_key)) > buf_len) |
468 | buf_len = i; | |
35208f36 DSH |
469 | } |
470 | else | |
471 | priv_key = NULL; | |
472 | ||
473 | if (ktype > 0) | |
474 | { | |
475 | buf_len += 10; | |
476 | if ((buffer = OPENSSL_malloc(buf_len)) == NULL) | |
477 | { | |
478 | reason = ERR_R_MALLOC_FAILURE; | |
479 | goto err; | |
480 | } | |
481 | } | |
482 | if (ktype == 2) | |
483 | ecstr = "Private-Key"; | |
484 | else if (ktype == 1) | |
485 | ecstr = "Public-Key"; | |
486 | else | |
487 | ecstr = "ECDSA-Parameters"; | |
488 | ||
489 | if (!BIO_indent(bp, off, 128)) | |
490 | goto err; | |
491 | if ((order = BN_new()) == NULL) | |
492 | goto err; | |
493 | if (!EC_GROUP_get_order(group, order, NULL)) | |
494 | goto err; | |
495 | if (BIO_printf(bp, "%s: (%d bit)\n", ecstr, | |
496 | BN_num_bits(order)) <= 0) goto err; | |
497 | ||
498 | if ((priv_key != NULL) && !ASN1_bn_print(bp, "priv:", priv_key, | |
499 | buffer, off)) | |
500 | goto err; | |
501 | if ((pub_key != NULL) && !ASN1_bn_print(bp, "pub: ", pub_key, | |
502 | buffer, off)) | |
503 | goto err; | |
504 | if (!ECPKParameters_print(bp, group, off)) | |
505 | goto err; | |
506 | ret=1; | |
507 | err: | |
508 | if (!ret) | |
5c95c2ac | 509 | ECerr(EC_F_DO_EC_KEY_PRINT, reason); |
35208f36 DSH |
510 | if (pub_key) |
511 | BN_free(pub_key); | |
512 | if (order) | |
513 | BN_free(order); | |
514 | if (ctx) | |
515 | BN_CTX_free(ctx); | |
516 | if (buffer != NULL) | |
517 | OPENSSL_free(buffer); | |
518 | return(ret); | |
519 | } | |
520 | ||
3e4585c8 DSH |
521 | static int eckey_param_decode(EVP_PKEY *pkey, |
522 | const unsigned char **pder, int derlen) | |
523 | { | |
524 | EC_KEY *eckey; | |
525 | if (!(eckey = d2i_ECParameters(NULL, pder, derlen))) | |
526 | { | |
527 | ECerr(EC_F_ECKEY_PARAM_DECODE, ERR_R_EC_LIB); | |
528 | return 0; | |
529 | } | |
530 | EVP_PKEY_assign_EC_KEY(pkey, eckey); | |
531 | return 1; | |
532 | } | |
533 | ||
534 | static int eckey_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
535 | { | |
536 | return i2d_ECParameters(pkey->pkey.ec, pder); | |
537 | } | |
538 | ||
35208f36 DSH |
539 | static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, |
540 | ASN1_PCTX *ctx) | |
541 | { | |
542 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 0); | |
543 | } | |
544 | ||
545 | static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
546 | ASN1_PCTX *ctx) | |
547 | { | |
548 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 1); | |
549 | } | |
550 | ||
551 | ||
552 | static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
553 | ASN1_PCTX *ctx) | |
554 | { | |
555 | return do_EC_KEY_print(bp, pkey->pkey.ec, indent, 2); | |
556 | } | |
557 | ||
e4263314 DSH |
558 | static int old_ec_priv_decode(EVP_PKEY *pkey, |
559 | const unsigned char **pder, int derlen) | |
560 | { | |
561 | EC_KEY *ec; | |
562 | if (!(ec = d2i_ECPrivateKey (NULL, pder, derlen))) | |
563 | { | |
5c95c2ac | 564 | ECerr(EC_F_OLD_EC_PRIV_DECODE, EC_R_DECODE_ERROR); |
e4263314 DSH |
565 | return 0; |
566 | } | |
567 | EVP_PKEY_assign_EC_KEY(pkey, ec); | |
568 | return 1; | |
569 | } | |
570 | ||
571 | static int old_ec_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
572 | { | |
573 | return i2d_ECPrivateKey(pkey->pkey.ec, pder); | |
574 | } | |
575 | ||
492a9e24 DSH |
576 | static int ec_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
577 | { | |
578 | switch (op) | |
579 | { | |
580 | case ASN1_PKEY_CTRL_PKCS7_SIGN: | |
581 | if (arg1 == 0) | |
582 | { | |
06e2dd03 | 583 | int snid, hnid; |
492a9e24 DSH |
584 | X509_ALGOR *alg1, *alg2; |
585 | PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2); | |
06e2dd03 NL |
586 | if (alg1 == NULL || alg1->algorithm == NULL) |
587 | return -1; | |
588 | hnid = OBJ_obj2nid(alg1->algorithm); | |
589 | if (hnid == NID_undef) | |
590 | return -1; | |
591 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
592 | return -1; | |
593 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
492a9e24 DSH |
594 | } |
595 | return 1; | |
8931b30d DSH |
596 | #ifndef OPENSSL_NO_CMS |
597 | case ASN1_PKEY_CTRL_CMS_SIGN: | |
598 | if (arg1 == 0) | |
599 | { | |
600 | int snid, hnid; | |
601 | X509_ALGOR *alg1, *alg2; | |
602 | CMS_SignerInfo_get0_algs(arg2, NULL, NULL, | |
603 | &alg1, &alg2); | |
604 | if (alg1 == NULL || alg1->algorithm == NULL) | |
605 | return -1; | |
606 | hnid = OBJ_obj2nid(alg1->algorithm); | |
607 | if (hnid == NID_undef) | |
608 | return -1; | |
609 | if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey))) | |
610 | return -1; | |
611 | X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0); | |
612 | } | |
613 | return 1; | |
614 | #endif | |
492a9e24 | 615 | |
03919683 DSH |
616 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
617 | *(int *)arg2 = NID_sha1; | |
618 | return 2; | |
619 | ||
492a9e24 DSH |
620 | default: |
621 | return -2; | |
622 | ||
623 | } | |
624 | ||
625 | } | |
626 | ||
560b79cb | 627 | const EVP_PKEY_ASN1_METHOD eckey_asn1_meth = |
448be743 DSH |
628 | { |
629 | EVP_PKEY_EC, | |
d82e2718 | 630 | EVP_PKEY_EC, |
448be743 | 631 | 0, |
e4263314 | 632 | "EC", |
d82e2718 | 633 | "OpenSSL EC algorithm", |
6f81892e | 634 | |
448be743 DSH |
635 | eckey_pub_decode, |
636 | eckey_pub_encode, | |
6f81892e | 637 | eckey_pub_cmp, |
35208f36 | 638 | eckey_pub_print, |
6f81892e | 639 | |
448be743 DSH |
640 | eckey_priv_decode, |
641 | eckey_priv_encode, | |
35208f36 | 642 | eckey_priv_print, |
6f81892e DSH |
643 | |
644 | int_ec_size, | |
645 | ec_bits, | |
646 | ||
3e4585c8 DSH |
647 | eckey_param_decode, |
648 | eckey_param_encode, | |
6f81892e DSH |
649 | ec_missing_parameters, |
650 | ec_copy_parameters, | |
651 | ec_cmp_parameters, | |
35208f36 | 652 | eckey_param_print, |
6f81892e DSH |
653 | |
654 | int_ec_free, | |
492a9e24 | 655 | ec_pkey_ctrl, |
e4263314 DSH |
656 | old_ec_priv_decode, |
657 | old_ec_priv_encode | |
448be743 | 658 | }; |