]>
Commit | Line | Data |
---|---|---|
cfcefcbe DSH |
1 | /* evp_pkey.c */ |
2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | |
3 | * project 1999. | |
4 | */ | |
5 | /* ==================================================================== | |
4d94ae00 | 6 | * Copyright (c) 1999-2002 The OpenSSL Project. All rights reserved. |
cfcefcbe DSH |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * licensing@OpenSSL.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
59 | #include <stdio.h> | |
60 | #include <stdlib.h> | |
61 | #include "cryptlib.h" | |
ec577822 BM |
62 | #include <openssl/x509.h> |
63 | #include <openssl/rand.h> | |
cfcefcbe | 64 | |
40928698 | 65 | #ifndef OPENSSL_NO_DSA |
66430207 | 66 | static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey); |
40928698 | 67 | #endif |
4d94ae00 BM |
68 | #ifndef OPENSSL_NO_ECDSA |
69 | static int ecdsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey); | |
70 | #endif | |
66430207 | 71 | |
cfcefcbe DSH |
72 | /* Extract a private key from a PKCS8 structure */ |
73 | ||
6b691a5c | 74 | EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8) |
cfcefcbe | 75 | { |
66430207 | 76 | EVP_PKEY *pkey = NULL; |
cf1b7d96 | 77 | #ifndef OPENSSL_NO_RSA |
66430207 | 78 | RSA *rsa = NULL; |
f5d7a031 | 79 | #endif |
cf1b7d96 | 80 | #ifndef OPENSSL_NO_DSA |
66430207 | 81 | DSA *dsa = NULL; |
4d94ae00 BM |
82 | #endif |
83 | #ifndef OPENSSL_NO_ECDSA | |
84 | ECDSA *ecdsa = NULL; | |
85 | #endif | |
86 | #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_ECDSA) | |
66430207 | 87 | ASN1_INTEGER *privkey; |
4d94ae00 BM |
88 | ASN1_TYPE *t1, *t2, *param = NULL; |
89 | STACK_OF(ASN1_TYPE) *n_stack = NULL; | |
66430207 | 90 | BN_CTX *ctx = NULL; |
f5d7a031 UM |
91 | int plen; |
92 | #endif | |
93 | X509_ALGOR *a; | |
cfcefcbe | 94 | unsigned char *p; |
40928698 | 95 | #ifndef OPENSSL_NO_RSA |
0413ba42 | 96 | const unsigned char *cp; |
40928698 | 97 | #endif |
f5d7a031 | 98 | int pkeylen; |
4d94ae00 | 99 | int nid; |
cfcefcbe DSH |
100 | char obj_tmp[80]; |
101 | ||
66430207 DSH |
102 | if(p8->pkey->type == V_ASN1_OCTET_STRING) { |
103 | p8->broken = PKCS8_OK; | |
cfcefcbe DSH |
104 | p = p8->pkey->value.octet_string->data; |
105 | pkeylen = p8->pkey->value.octet_string->length; | |
66430207 DSH |
106 | } else { |
107 | p8->broken = PKCS8_NO_OCTET; | |
cfcefcbe DSH |
108 | p = p8->pkey->value.sequence->data; |
109 | pkeylen = p8->pkey->value.sequence->length; | |
cfcefcbe DSH |
110 | } |
111 | if (!(pkey = EVP_PKEY_new())) { | |
112 | EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE); | |
113 | return NULL; | |
114 | } | |
115 | a = p8->pkeyalg; | |
4d94ae00 BM |
116 | nid = OBJ_obj2nid(a->algorithm); |
117 | switch(nid) | |
cfcefcbe | 118 | { |
cf1b7d96 | 119 | #ifndef OPENSSL_NO_RSA |
cfcefcbe | 120 | case NID_rsaEncryption: |
0413ba42 RL |
121 | cp = p; |
122 | if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) { | |
cfcefcbe DSH |
123 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); |
124 | return NULL; | |
125 | } | |
126 | EVP_PKEY_assign_RSA (pkey, rsa); | |
127 | break; | |
f5d7a031 | 128 | #endif |
4d94ae00 BM |
129 | #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_ECDSA) |
130 | case NID_ecdsa_with_SHA1: | |
cfcefcbe | 131 | case NID_dsa: |
4d94ae00 | 132 | /* PKCS#8 DSA/ECDSA is weird: you just get a private key integer |
cfcefcbe DSH |
133 | * and parameters in the AlgorithmIdentifier the pubkey must |
134 | * be recalculated. | |
135 | */ | |
136 | ||
4d94ae00 BM |
137 | /* Check for broken DSA/ECDSA PKCS#8, UGH! */ |
138 | if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) | |
139 | { | |
140 | if(!(n_stack = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen, | |
371acb22 | 141 | d2i_ASN1_TYPE, |
4d94ae00 BM |
142 | ASN1_TYPE_free))) |
143 | { | |
144 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
145 | goto err; | |
146 | } | |
147 | if(sk_ASN1_TYPE_num(n_stack) != 2 ) | |
148 | { | |
149 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
150 | goto err; | |
151 | } | |
66430207 DSH |
152 | /* Handle Two broken types: |
153 | * SEQUENCE {parameters, priv_key} | |
154 | * SEQUENCE {pub_key, priv_key} | |
155 | */ | |
48fe0eec | 156 | |
4d94ae00 BM |
157 | t1 = sk_ASN1_TYPE_value(n_stack, 0); |
158 | t2 = sk_ASN1_TYPE_value(n_stack, 1); | |
159 | if(t1->type == V_ASN1_SEQUENCE) | |
160 | { | |
66430207 DSH |
161 | p8->broken = PKCS8_EMBEDDED_PARAM; |
162 | param = t1; | |
4d94ae00 BM |
163 | } |
164 | else if(a->parameter->type == V_ASN1_SEQUENCE) | |
165 | { | |
66430207 DSH |
166 | p8->broken = PKCS8_NS_DB; |
167 | param = a->parameter; | |
4d94ae00 BM |
168 | } |
169 | else | |
170 | { | |
cfcefcbe | 171 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); |
4d94ae00 | 172 | goto err; |
66430207 DSH |
173 | } |
174 | ||
175 | if(t2->type != V_ASN1_INTEGER) { | |
176 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
4d94ae00 | 177 | goto err; |
66430207 DSH |
178 | } |
179 | privkey = t2->value.integer; | |
4d94ae00 BM |
180 | } |
181 | else | |
182 | { | |
183 | if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) | |
184 | { | |
48fe0eec | 185 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); |
4d94ae00 | 186 | goto err; |
48fe0eec DSH |
187 | } |
188 | param = p8->pkeyalg->parameter; | |
189 | } | |
4d94ae00 BM |
190 | if (!param || (param->type != V_ASN1_SEQUENCE)) |
191 | { | |
66430207 | 192 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); |
4d94ae00 | 193 | goto err; |
cfcefcbe | 194 | } |
a4aba800 | 195 | cp = p = param->value.sequence->data; |
66430207 | 196 | plen = param->value.sequence->length; |
4d94ae00 BM |
197 | if (!(ctx = BN_CTX_new())) |
198 | { | |
cfcefcbe | 199 | EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE); |
4d94ae00 | 200 | goto err; |
cfcefcbe | 201 | } |
4d94ae00 BM |
202 | if (nid == NID_dsa) |
203 | { | |
204 | #ifndef OPENSSL_NO_DSA | |
205 | if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) | |
206 | { | |
207 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
208 | goto err; | |
209 | } | |
210 | /* We have parameters now set private key */ | |
211 | if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) | |
212 | { | |
213 | EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR); | |
214 | goto err; | |
215 | } | |
216 | /* Calculate public key (ouch!) */ | |
217 | if (!(dsa->pub_key = BN_new())) | |
218 | { | |
219 | EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE); | |
220 | goto err; | |
221 | } | |
222 | if (!BN_mod_exp(dsa->pub_key, dsa->g, | |
223 | dsa->priv_key, dsa->p, ctx)) | |
224 | { | |
225 | EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR); | |
226 | goto err; | |
227 | } | |
228 | ||
229 | EVP_PKEY_assign_DSA(pkey, dsa); | |
230 | BN_CTX_free(ctx); | |
231 | if(n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free); | |
232 | else ASN1_INTEGER_free(privkey); | |
233 | #else | |
234 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | |
235 | goto err; | |
236 | #endif | |
237 | } | |
238 | else /* nid == NID_ecdsa_with_SHA1 */ | |
239 | { | |
240 | #ifndef OPENSSL_NO_ECDSA | |
241 | if ((ecdsa = d2i_ECDSAParameters(NULL, &cp, plen)) == NULL) | |
242 | { | |
243 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
244 | goto err; | |
245 | } | |
246 | if ((ecdsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)) == NULL) | |
247 | { | |
248 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR); | |
249 | goto err; | |
250 | } | |
251 | if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL) | |
252 | { | |
253 | EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB); | |
254 | goto err; | |
255 | } | |
256 | if (!EC_POINT_copy(ecdsa->pub_key, EC_GROUP_get0_generator(ecdsa->group))) | |
257 | { | |
258 | EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB); | |
259 | goto err; | |
260 | } | |
261 | if (!EC_POINT_mul(ecdsa->group, ecdsa->pub_key, ecdsa->priv_key, | |
262 | NULL, NULL, ctx)) | |
263 | { | |
264 | EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB); | |
265 | goto err; | |
266 | } | |
cfcefcbe | 267 | |
4d94ae00 BM |
268 | EVP_PKEY_assign_ECDSA(pkey, ecdsa); |
269 | BN_CTX_free(ctx); | |
270 | if (n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free); | |
271 | else | |
272 | ASN1_INTEGER_free(privkey); | |
273 | #else | |
274 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | |
275 | goto err; | |
276 | #endif | |
cfcefcbe | 277 | } |
66430207 | 278 | break; |
4d94ae00 BM |
279 | err: |
280 | if (ctx) BN_CTX_free(ctx); | |
281 | sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free); | |
282 | if (dsa) DSA_free(dsa); | |
283 | if (ecdsa) ECDSA_free(ecdsa); | |
284 | if (pkey) EVP_PKEY_free(pkey); | |
66430207 | 285 | return NULL; |
cfcefcbe | 286 | break; |
f5d7a031 | 287 | #endif |
cfcefcbe DSH |
288 | default: |
289 | EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | |
290 | if (!a->algorithm) strcpy (obj_tmp, "NULL"); | |
291 | else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm); | |
292 | ERR_add_error_data(2, "TYPE=", obj_tmp); | |
293 | EVP_PKEY_free (pkey); | |
294 | return NULL; | |
295 | } | |
296 | return pkey; | |
297 | } | |
298 | ||
66430207 DSH |
299 | PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey) |
300 | { | |
301 | return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK); | |
302 | } | |
303 | ||
cfcefcbe DSH |
304 | /* Turn a private key into a PKCS8 structure */ |
305 | ||
66430207 | 306 | PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken) |
cfcefcbe DSH |
307 | { |
308 | PKCS8_PRIV_KEY_INFO *p8; | |
66430207 | 309 | |
cfcefcbe DSH |
310 | if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) { |
311 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
312 | return NULL; | |
313 | } | |
66430207 | 314 | p8->broken = broken; |
cfcefcbe DSH |
315 | ASN1_INTEGER_set (p8->version, 0); |
316 | if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) { | |
317 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
318 | PKCS8_PRIV_KEY_INFO_free (p8); | |
319 | return NULL; | |
320 | } | |
66430207 | 321 | p8->pkey->type = V_ASN1_OCTET_STRING; |
cfcefcbe | 322 | switch (EVP_PKEY_type(pkey->type)) { |
cf1b7d96 | 323 | #ifndef OPENSSL_NO_RSA |
cfcefcbe DSH |
324 | case EVP_PKEY_RSA: |
325 | ||
66430207 DSH |
326 | if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE; |
327 | ||
cfcefcbe DSH |
328 | p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption); |
329 | p8->pkeyalg->parameter->type = V_ASN1_NULL; | |
330 | if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey, | |
331 | &p8->pkey->value.octet_string)) { | |
332 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
333 | PKCS8_PRIV_KEY_INFO_free (p8); | |
334 | return NULL; | |
335 | } | |
336 | break; | |
f5d7a031 | 337 | #endif |
cf1b7d96 | 338 | #ifndef OPENSSL_NO_DSA |
cfcefcbe | 339 | case EVP_PKEY_DSA: |
66430207 | 340 | if(!dsa_pkey2pkcs8(p8, pkey)) { |
cfcefcbe DSH |
341 | PKCS8_PRIV_KEY_INFO_free (p8); |
342 | return NULL; | |
343 | } | |
66430207 | 344 | |
cfcefcbe | 345 | break; |
f5d7a031 | 346 | #endif |
4d94ae00 BM |
347 | #ifndef OPENSSL_NO_ECDSA |
348 | case EVP_PKEY_ECDSA: | |
349 | if (!ecdsa_pkey2pkcs8(p8, pkey)) | |
350 | { | |
351 | PKCS8_PRIV_KEY_INFO_free(p8); | |
352 | return(NULL); | |
353 | } | |
354 | break; | |
355 | #endif | |
cfcefcbe DSH |
356 | default: |
357 | EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM); | |
358 | PKCS8_PRIV_KEY_INFO_free (p8); | |
359 | return NULL; | |
360 | } | |
eb952088 UM |
361 | RAND_add(p8->pkey->value.octet_string->data, |
362 | p8->pkey->value.octet_string->length, 0); | |
cfcefcbe DSH |
363 | return p8; |
364 | } | |
365 | ||
6b691a5c | 366 | PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken) |
cfcefcbe DSH |
367 | { |
368 | switch (broken) { | |
369 | ||
370 | case PKCS8_OK: | |
371 | p8->broken = PKCS8_OK; | |
372 | return p8; | |
373 | break; | |
374 | ||
375 | case PKCS8_NO_OCTET: | |
376 | p8->broken = PKCS8_NO_OCTET; | |
377 | p8->pkey->type = V_ASN1_SEQUENCE; | |
378 | return p8; | |
379 | break; | |
380 | ||
381 | default: | |
382 | EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE); | |
383 | return NULL; | |
384 | break; | |
385 | ||
386 | } | |
387 | } | |
388 | ||
cf1b7d96 | 389 | #ifndef OPENSSL_NO_DSA |
66430207 DSH |
390 | static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey) |
391 | { | |
392 | ASN1_STRING *params; | |
393 | ASN1_INTEGER *prkey; | |
394 | ASN1_TYPE *ttmp; | |
371acb22 | 395 | STACK_OF(ASN1_TYPE) *ndsa; |
66430207 DSH |
396 | unsigned char *p, *q; |
397 | int len; | |
371acb22 | 398 | |
66430207 DSH |
399 | p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa); |
400 | len = i2d_DSAparams (pkey->pkey.dsa, NULL); | |
26a3a48d | 401 | if (!(p = OPENSSL_malloc(len))) { |
66430207 DSH |
402 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); |
403 | PKCS8_PRIV_KEY_INFO_free (p8); | |
404 | return 0; | |
405 | } | |
406 | q = p; | |
407 | i2d_DSAparams (pkey->pkey.dsa, &q); | |
408 | params = ASN1_STRING_new(); | |
409 | ASN1_STRING_set(params, p, len); | |
26a3a48d | 410 | OPENSSL_free(p); |
66430207 DSH |
411 | /* Get private key into integer */ |
412 | if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) { | |
413 | EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR); | |
414 | return 0; | |
415 | } | |
416 | ||
417 | switch(p8->broken) { | |
cfcefcbe | 418 | |
66430207 DSH |
419 | case PKCS8_OK: |
420 | case PKCS8_NO_OCTET: | |
421 | ||
422 | if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER, | |
423 | &p8->pkey->value.octet_string)) { | |
424 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
425 | M_ASN1_INTEGER_free (prkey); | |
426 | return 0; | |
427 | } | |
428 | ||
429 | M_ASN1_INTEGER_free (prkey); | |
430 | p8->pkeyalg->parameter->value.sequence = params; | |
431 | p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE; | |
432 | ||
433 | break; | |
434 | ||
435 | case PKCS8_NS_DB: | |
436 | ||
437 | p8->pkeyalg->parameter->value.sequence = params; | |
438 | p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE; | |
371acb22 | 439 | ndsa = sk_ASN1_TYPE_new_null(); |
66430207 DSH |
440 | ttmp = ASN1_TYPE_new(); |
441 | if (!(ttmp->value.integer = BN_to_ASN1_INTEGER (pkey->pkey.dsa->pub_key, NULL))) { | |
442 | EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR); | |
443 | PKCS8_PRIV_KEY_INFO_free(p8); | |
444 | return 0; | |
445 | } | |
446 | ttmp->type = V_ASN1_INTEGER; | |
371acb22 | 447 | sk_ASN1_TYPE_push(ndsa, ttmp); |
66430207 DSH |
448 | |
449 | ttmp = ASN1_TYPE_new(); | |
450 | ttmp->value.integer = prkey; | |
451 | ttmp->type = V_ASN1_INTEGER; | |
371acb22 | 452 | sk_ASN1_TYPE_push(ndsa, ttmp); |
66430207 DSH |
453 | |
454 | p8->pkey->value.octet_string = ASN1_OCTET_STRING_new(); | |
455 | ||
371acb22 | 456 | if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE, |
66430207 DSH |
457 | &p8->pkey->value.octet_string->data, |
458 | &p8->pkey->value.octet_string->length)) { | |
459 | ||
460 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
371acb22 | 461 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); |
66430207 DSH |
462 | M_ASN1_INTEGER_free(prkey); |
463 | return 0; | |
464 | } | |
371acb22 | 465 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); |
66430207 DSH |
466 | break; |
467 | ||
468 | case PKCS8_EMBEDDED_PARAM: | |
469 | ||
470 | p8->pkeyalg->parameter->type = V_ASN1_NULL; | |
371acb22 | 471 | ndsa = sk_ASN1_TYPE_new_null(); |
66430207 DSH |
472 | ttmp = ASN1_TYPE_new(); |
473 | ttmp->value.sequence = params; | |
474 | ttmp->type = V_ASN1_SEQUENCE; | |
371acb22 | 475 | sk_ASN1_TYPE_push(ndsa, ttmp); |
66430207 DSH |
476 | |
477 | ttmp = ASN1_TYPE_new(); | |
478 | ttmp->value.integer = prkey; | |
479 | ttmp->type = V_ASN1_INTEGER; | |
371acb22 | 480 | sk_ASN1_TYPE_push(ndsa, ttmp); |
66430207 DSH |
481 | |
482 | p8->pkey->value.octet_string = ASN1_OCTET_STRING_new(); | |
483 | ||
371acb22 | 484 | if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE, |
66430207 DSH |
485 | &p8->pkey->value.octet_string->data, |
486 | &p8->pkey->value.octet_string->length)) { | |
487 | ||
488 | EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE); | |
371acb22 | 489 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); |
66430207 DSH |
490 | M_ASN1_INTEGER_free (prkey); |
491 | return 0; | |
492 | } | |
371acb22 | 493 | sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free); |
66430207 DSH |
494 | break; |
495 | } | |
496 | return 1; | |
497 | } | |
498 | #endif | |
4d94ae00 BM |
499 | |
500 | #ifndef OPENSSL_NO_ECDSA | |
501 | static int ecdsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey) | |
502 | { | |
503 | ASN1_STRING *params=NULL; | |
504 | ASN1_INTEGER *prkey=NULL; | |
505 | ASN1_TYPE *ttmp=NULL; | |
506 | STACK_OF(ASN1_TYPE) *necdsa=NULL; | |
507 | unsigned char *p=NULL, *q=NULL; | |
508 | int len=0; | |
509 | EC_POINT *point=NULL; | |
510 | ||
511 | if (pkey->pkey.ecdsa == NULL || pkey->pkey.ecdsa->group == NULL) | |
512 | { | |
513 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS); | |
514 | return 0; | |
515 | } | |
516 | p8->pkeyalg->algorithm = OBJ_nid2obj(NID_ecdsa_with_SHA1); | |
517 | len = i2d_ECDSAParameters(pkey->pkey.ecdsa, NULL); | |
518 | if ((p = OPENSSL_malloc(len)) == NULL) | |
519 | { | |
520 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
521 | return 0; | |
522 | } | |
523 | q = p; | |
524 | if (!i2d_ECDSAParameters(pkey->pkey.ecdsa, &q)) | |
525 | { | |
526 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ECDSA_LIB); | |
527 | OPENSSL_free(p); | |
528 | return 0; | |
529 | } | |
530 | if ((params = ASN1_STRING_new()) == NULL) | |
531 | { | |
532 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
533 | OPENSSL_free(p); | |
534 | return 0; | |
535 | ||
536 | } | |
537 | if (!ASN1_STRING_set(params, p, len)) | |
538 | { | |
539 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ASN1_LIB); | |
540 | OPENSSL_free(p); | |
541 | return 0; | |
542 | } | |
543 | OPENSSL_free(p); | |
544 | if ((prkey = BN_to_ASN1_INTEGER(pkey->pkey.ecdsa->priv_key, NULL)) == NULL) | |
545 | { | |
546 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ASN1_LIB); | |
547 | return 0; | |
548 | } | |
549 | ||
550 | switch(p8->broken) { | |
551 | ||
552 | case PKCS8_OK: | |
553 | case PKCS8_NO_OCTET: | |
554 | ||
555 | if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER, | |
556 | &p8->pkey->value.octet_string)) | |
557 | { | |
558 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
559 | M_ASN1_INTEGER_free(prkey); | |
560 | return 0; | |
561 | } | |
562 | ||
563 | ASN1_INTEGER_free(prkey); | |
564 | p8->pkeyalg->parameter->value.sequence = params; | |
565 | p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE; | |
566 | ||
567 | break; | |
568 | ||
569 | case PKCS8_NS_DB: | |
570 | ||
571 | p8->pkeyalg->parameter->value.sequence = params; | |
572 | p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE; | |
573 | necdsa = sk_ASN1_TYPE_new_null(); | |
574 | if (necdsa == NULL || (ttmp = ASN1_TYPE_new()) == NULL) | |
575 | { | |
576 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
577 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
578 | return 0; | |
579 | } | |
580 | ||
581 | if ((point = EC_GROUP_get0_generator(pkey->pkey.ecdsa->group)) == NULL) | |
582 | { | |
583 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_EC_LIB); | |
584 | return 0; | |
585 | } | |
586 | len = EC_POINT_point2oct(pkey->pkey.ecdsa->group, point, POINT_CONVERSION_COMPRESSED, | |
587 | NULL, 0, NULL); | |
588 | p = OPENSSL_malloc(len); | |
589 | if (!len || !p || !EC_POINT_point2oct(pkey->pkey.ecdsa->group, point, | |
590 | POINT_CONVERSION_COMPRESSED, p, len, NULL)) | |
591 | { | |
592 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_EC_LIB); | |
593 | OPENSSL_free(p); | |
594 | return 0; | |
595 | } | |
596 | if ((ttmp->value.octet_string = ASN1_OCTET_STRING_new()) == NULL) | |
597 | { | |
598 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
599 | return 0; | |
600 | } | |
601 | if (!ASN1_OCTET_STRING_set(ttmp->value.octet_string, p, len)) | |
602 | { | |
603 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, EVP_R_ASN1_LIB); | |
604 | return 0; | |
605 | } | |
606 | OPENSSL_free(p); | |
607 | ||
608 | ttmp->type = V_ASN1_OCTET_STRING; | |
609 | if (!sk_ASN1_TYPE_push(necdsa, ttmp)) | |
610 | { | |
611 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
612 | ASN1_INTEGER_free(prkey); | |
613 | return 0; | |
614 | } | |
615 | ||
616 | if ((ttmp = ASN1_TYPE_new()) == NULL) | |
617 | { | |
618 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
619 | return 0; | |
620 | } | |
621 | ttmp->value.integer = prkey; | |
622 | ttmp->type = V_ASN1_INTEGER; | |
623 | if (!sk_ASN1_TYPE_push(necdsa, ttmp)) | |
624 | { | |
625 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
626 | ASN1_INTEGER_free(prkey); | |
627 | return 0; | |
628 | } | |
629 | ||
630 | if ((p8->pkey->value.octet_string = ASN1_OCTET_STRING_new()) == NULL) | |
631 | { | |
632 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
633 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
634 | return 0; | |
635 | } | |
636 | ||
637 | if (!ASN1_seq_pack_ASN1_TYPE(necdsa, i2d_ASN1_TYPE, | |
638 | &p8->pkey->value.octet_string->data, | |
639 | &p8->pkey->value.octet_string->length)) | |
640 | { | |
641 | ||
642 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
643 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
644 | return 0; | |
645 | } | |
646 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
647 | break; | |
648 | ||
649 | case PKCS8_EMBEDDED_PARAM: | |
650 | ||
651 | p8->pkeyalg->parameter->type = V_ASN1_NULL; | |
652 | necdsa = sk_ASN1_TYPE_new_null(); | |
653 | if ((ttmp = ASN1_TYPE_new()) == NULL) | |
654 | { | |
655 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
656 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
657 | ASN1_INTEGER_free(prkey); | |
658 | return 0; | |
659 | } | |
660 | ttmp->value.sequence = params; | |
661 | ttmp->type = V_ASN1_SEQUENCE; | |
662 | if (!sk_ASN1_TYPE_push(necdsa, ttmp)) | |
663 | { | |
664 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
665 | ASN1_INTEGER_free(prkey); | |
666 | return 0; | |
667 | } | |
668 | ||
669 | if ((ttmp = ASN1_TYPE_new()) == NULL) | |
670 | { | |
671 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
672 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
673 | ASN1_INTEGER_free(prkey); | |
674 | return 0; | |
675 | } | |
676 | ttmp->value.integer = prkey; | |
677 | ttmp->type = V_ASN1_INTEGER; | |
678 | if (!sk_ASN1_TYPE_push(necdsa, ttmp)) | |
679 | { | |
680 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
681 | ASN1_INTEGER_free(prkey); | |
682 | return 0; | |
683 | } | |
684 | ||
685 | if ((p8->pkey->value.octet_string = ASN1_OCTET_STRING_new()) == NULL) | |
686 | { | |
687 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
688 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
689 | return 0; | |
690 | } | |
691 | ||
692 | if (!ASN1_seq_pack_ASN1_TYPE(necdsa, i2d_ASN1_TYPE, | |
693 | &p8->pkey->value.octet_string->data, | |
694 | &p8->pkey->value.octet_string->length)) | |
695 | { | |
696 | EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE); | |
697 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
698 | return 0; | |
699 | } | |
700 | sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free); | |
701 | break; | |
702 | } | |
703 | return 1; | |
704 | } | |
705 | #endif |