3577837fa9bfbced78858ee58320c3d41ea72c86
[openssl.git] / crypto / evp / evp_pkey.c
1 /* evp_pkey.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
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"
62 #include <openssl/x509.h>
63 #include <openssl/rand.h>
64
65 #ifndef OPENSSL_NO_DSA
66 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
67 #endif
68 #ifndef OPENSSL_NO_ECDSA
69 static int ecdsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8inf, EVP_PKEY *pkey);
70 #endif
71
72 /* Extract a private key from a PKCS8 structure */
73
74 EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)
75 {
76         EVP_PKEY *pkey = NULL;
77 #ifndef OPENSSL_NO_RSA
78         RSA *rsa = NULL;
79 #endif
80 #ifndef OPENSSL_NO_DSA
81         DSA *dsa = NULL;
82 #endif
83 #ifndef OPENSSL_NO_ECDSA
84         ECDSA    *ecdsa = NULL;
85 #endif
86 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
87         ASN1_INTEGER *privkey;
88         ASN1_TYPE    *t1, *t2, *param = NULL;
89         STACK_OF(ASN1_TYPE) *n_stack = NULL;
90         BN_CTX *ctx = NULL;
91         int plen;
92 #endif
93         X509_ALGOR *a;
94         unsigned char *p;
95         const unsigned char *cp;
96         int pkeylen;
97         int  nid;
98         char obj_tmp[80];
99
100         if(p8->pkey->type == V_ASN1_OCTET_STRING) {
101                 p8->broken = PKCS8_OK;
102                 p = p8->pkey->value.octet_string->data;
103                 pkeylen = p8->pkey->value.octet_string->length;
104         } else {
105                 p8->broken = PKCS8_NO_OCTET;
106                 p = p8->pkey->value.sequence->data;
107                 pkeylen = p8->pkey->value.sequence->length;
108         }
109         if (!(pkey = EVP_PKEY_new())) {
110                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
111                 return NULL;
112         }
113         a = p8->pkeyalg;
114         nid = OBJ_obj2nid(a->algorithm);
115         switch(nid)
116         {
117 #ifndef OPENSSL_NO_RSA
118                 case NID_rsaEncryption:
119                 cp = p;
120                 if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
121                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
122                         return NULL;
123                 }
124                 EVP_PKEY_assign_RSA (pkey, rsa);
125                 break;
126 #endif
127 #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_ECDSA)
128                 case NID_ecdsa_with_SHA1:
129                 case NID_dsa:
130                 /* PKCS#8 DSA/ECDSA is weird: you just get a private key integer
131                  * and parameters in the AlgorithmIdentifier the pubkey must
132                  * be recalculated.
133                  */
134         
135                 /* Check for broken DSA/ECDSA PKCS#8, UGH! */
136                 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) 
137                 {
138                         if(!(n_stack = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen, 
139                                                           d2i_ASN1_TYPE,
140                                                           ASN1_TYPE_free))) 
141                         {
142                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
143                                 goto err;
144                         }
145                         if(sk_ASN1_TYPE_num(n_stack) != 2 ) 
146                         {
147                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
148                                 goto err;
149                         }
150                     /* Handle Two broken types:
151                      * SEQUENCE {parameters, priv_key}
152                      * SEQUENCE {pub_key, priv_key}
153                      */
154
155                     t1 = sk_ASN1_TYPE_value(n_stack, 0);
156                     t2 = sk_ASN1_TYPE_value(n_stack, 1);
157                     if(t1->type == V_ASN1_SEQUENCE) 
158                     {
159                         p8->broken = PKCS8_EMBEDDED_PARAM;
160                         param = t1;
161                     } 
162                     else if(a->parameter->type == V_ASN1_SEQUENCE) 
163                     {
164                         p8->broken = PKCS8_NS_DB;
165                         param = a->parameter;
166                     } 
167                     else 
168                     {
169                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
170                         goto err;
171                     }
172
173                     if(t2->type != V_ASN1_INTEGER) {
174                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
175                         goto err;
176                     }
177                     privkey = t2->value.integer;
178                 } 
179                 else 
180                 {
181                         if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) 
182                         {
183                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
184                                 goto err;
185                         }
186                         param = p8->pkeyalg->parameter;
187                 }
188                 if (!param || (param->type != V_ASN1_SEQUENCE)) 
189                 {
190                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
191                         goto err;
192                 }
193                 cp = p = param->value.sequence->data;
194                 plen = param->value.sequence->length;
195                 if (!(ctx = BN_CTX_new())) 
196                 {
197                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
198                         goto err;
199                 }
200                 if (nid == NID_dsa)
201                 {
202 #ifndef OPENSSL_NO_DSA
203                         if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) 
204                         {
205                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
206                                 goto err;
207                         }
208                         /* We have parameters now set private key */
209                         if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) 
210                         {
211                                 EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
212                                 goto err;
213                         }
214                         /* Calculate public key (ouch!) */
215                         if (!(dsa->pub_key = BN_new())) 
216                         {
217                                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
218                                 goto err;
219                         }
220                         if (!BN_mod_exp(dsa->pub_key, dsa->g,
221                                                  dsa->priv_key, dsa->p, ctx)) 
222                         {
223                                 EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
224                                 goto err;
225                         }
226
227                         EVP_PKEY_assign_DSA(pkey, dsa);
228                         BN_CTX_free(ctx);
229                         if(n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);
230                         else ASN1_INTEGER_free(privkey);
231 #else
232                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
233                         goto err;
234 #endif 
235                 } 
236                 else /* nid == NID_ecdsa_with_SHA1 */
237                 {
238 #ifndef OPENSSL_NO_ECDSA
239                         if ((ecdsa = d2i_ECDSAParameters(NULL, &cp, plen)) == NULL)
240                         {
241                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
242                                 goto err;
243                         }
244                         if ((ecdsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)) == NULL)
245                         {
246                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
247                                 goto err;
248                         }
249                         if ((ecdsa->pub_key = EC_POINT_new(ecdsa->group)) == NULL)
250                         {
251                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
252                                 goto err;
253                         }
254                         if (!EC_POINT_copy(ecdsa->pub_key, EC_GROUP_get0_generator(ecdsa->group)))
255                         {
256                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
257                                 goto err;
258                         }
259                         if (!EC_POINT_mul(ecdsa->group, ecdsa->pub_key, ecdsa->priv_key,
260                                           NULL, NULL, ctx))
261                         {
262                                 EVPerr(EVP_F_EVP_PKCS82PKEY, ERR_R_EC_LIB);
263                                 goto err;
264                         }
265                         
266                         EVP_PKEY_assign_ECDSA(pkey, ecdsa);
267                         BN_CTX_free(ctx);
268                         if (n_stack) sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);
269                         else
270                                 ASN1_INTEGER_free(privkey);
271 #else
272                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
273                         goto err;
274 #endif
275                 }
276                 break;
277 err:
278                 if (ctx)   BN_CTX_free(ctx);
279                 sk_ASN1_TYPE_pop_free(n_stack, ASN1_TYPE_free);
280 #ifndef OPENSSL_NO_DSA
281                 if (dsa)   DSA_free(dsa);
282 #endif
283 #ifndef OPENSSL_NO_ECDSA
284                 if (ecdsa) ECDSA_free(ecdsa);
285 #endif
286                 if (pkey)  EVP_PKEY_free(pkey);
287                 return NULL;
288                 break;
289 #endif
290                 default:
291                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
292                 if (!a->algorithm) strcpy (obj_tmp, "NULL");
293                 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
294                 ERR_add_error_data(2, "TYPE=", obj_tmp);
295                 EVP_PKEY_free (pkey);
296                 return NULL;
297         }
298         return pkey;
299 }
300
301 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
302 {
303         return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
304 }
305
306 /* Turn a private key into a PKCS8 structure */
307
308 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
309 {
310         PKCS8_PRIV_KEY_INFO *p8;
311
312         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
313                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
314                 return NULL;
315         }
316         p8->broken = broken;
317         ASN1_INTEGER_set (p8->version, 0);
318         if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
319                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
320                 PKCS8_PRIV_KEY_INFO_free (p8);
321                 return NULL;
322         }
323         p8->pkey->type = V_ASN1_OCTET_STRING;
324         switch (EVP_PKEY_type(pkey->type)) {
325 #ifndef OPENSSL_NO_RSA
326                 case EVP_PKEY_RSA:
327
328                 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
329
330                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
331                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
332                 if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
333                                          &p8->pkey->value.octet_string)) {
334                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
335                         PKCS8_PRIV_KEY_INFO_free (p8);
336                         return NULL;
337                 }
338                 break;
339 #endif
340 #ifndef OPENSSL_NO_DSA
341                 case EVP_PKEY_DSA:
342                 if(!dsa_pkey2pkcs8(p8, pkey)) {
343                         PKCS8_PRIV_KEY_INFO_free (p8);
344                         return NULL;
345                 }
346
347                 break;
348 #endif
349 #ifndef OPENSSL_NO_ECDSA
350                 case EVP_PKEY_ECDSA:
351                 if (!ecdsa_pkey2pkcs8(p8, pkey))
352                 {
353                         PKCS8_PRIV_KEY_INFO_free(p8);
354                         return(NULL);
355                 }
356                 break;
357 #endif
358                 default:
359                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
360                 PKCS8_PRIV_KEY_INFO_free (p8);
361                 return NULL;
362         }
363         RAND_add(p8->pkey->value.octet_string->data,
364                  p8->pkey->value.octet_string->length, 0);
365         return p8;
366 }
367
368 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
369 {
370         switch (broken) {
371
372                 case PKCS8_OK:
373                 p8->broken = PKCS8_OK;
374                 return p8;
375                 break;
376
377                 case PKCS8_NO_OCTET:
378                 p8->broken = PKCS8_NO_OCTET;
379                 p8->pkey->type = V_ASN1_SEQUENCE;
380                 return p8;
381                 break;
382
383                 default:
384                 EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
385                 return NULL;
386                 break;
387                 
388         }
389 }
390
391 #ifndef OPENSSL_NO_DSA
392 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
393 {
394         ASN1_STRING *params;
395         ASN1_INTEGER *prkey;
396         ASN1_TYPE *ttmp;
397         STACK_OF(ASN1_TYPE) *ndsa;
398         unsigned char *p, *q;
399         int len;
400
401         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
402         len = i2d_DSAparams (pkey->pkey.dsa, NULL);
403         if (!(p = OPENSSL_malloc(len))) {
404                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
405                 PKCS8_PRIV_KEY_INFO_free (p8);
406                 return 0;
407         }
408         q = p;
409         i2d_DSAparams (pkey->pkey.dsa, &q);
410         params = ASN1_STRING_new();
411         ASN1_STRING_set(params, p, len);
412         OPENSSL_free(p);
413         /* Get private key into integer */
414         if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
415                 EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
416                 return 0;
417         }
418
419         switch(p8->broken) {
420
421                 case PKCS8_OK:
422                 case PKCS8_NO_OCTET:
423
424                 if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
425                                          &p8->pkey->value.octet_string)) {
426                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
427                         M_ASN1_INTEGER_free (prkey);
428                         return 0;
429                 }
430
431                 M_ASN1_INTEGER_free (prkey);
432                 p8->pkeyalg->parameter->value.sequence = params;
433                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
434
435                 break;
436
437                 case PKCS8_NS_DB:
438
439                 p8->pkeyalg->parameter->value.sequence = params;
440                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
441                 ndsa = sk_ASN1_TYPE_new_null();
442                 ttmp = ASN1_TYPE_new();
443                 if (!(ttmp->value.integer = BN_to_ASN1_INTEGER (pkey->pkey.dsa->pub_key, NULL))) {
444                         EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
445                         PKCS8_PRIV_KEY_INFO_free(p8);
446                         return 0;
447                 }
448                 ttmp->type = V_ASN1_INTEGER;
449                 sk_ASN1_TYPE_push(ndsa, ttmp);
450
451                 ttmp = ASN1_TYPE_new();
452                 ttmp->value.integer = prkey;
453                 ttmp->type = V_ASN1_INTEGER;
454                 sk_ASN1_TYPE_push(ndsa, ttmp);
455
456                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
457
458                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
459                                          &p8->pkey->value.octet_string->data,
460                                          &p8->pkey->value.octet_string->length)) {
461
462                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
463                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
464                         M_ASN1_INTEGER_free(prkey);
465                         return 0;
466                 }
467                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
468                 break;
469
470                 case PKCS8_EMBEDDED_PARAM:
471
472                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
473                 ndsa = sk_ASN1_TYPE_new_null();
474                 ttmp = ASN1_TYPE_new();
475                 ttmp->value.sequence = params;
476                 ttmp->type = V_ASN1_SEQUENCE;
477                 sk_ASN1_TYPE_push(ndsa, ttmp);
478
479                 ttmp = ASN1_TYPE_new();
480                 ttmp->value.integer = prkey;
481                 ttmp->type = V_ASN1_INTEGER;
482                 sk_ASN1_TYPE_push(ndsa, ttmp);
483
484                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
485
486                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
487                                          &p8->pkey->value.octet_string->data,
488                                          &p8->pkey->value.octet_string->length)) {
489
490                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
491                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
492                         M_ASN1_INTEGER_free (prkey);
493                         return 0;
494                 }
495                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
496                 break;
497         }
498         return 1;
499 }
500 #endif
501
502 #ifndef OPENSSL_NO_ECDSA
503 static int ecdsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
504 {
505         ASN1_STRING       *params=NULL;
506         ASN1_INTEGER      *prkey=NULL;
507         ASN1_TYPE         *ttmp=NULL;
508         STACK_OF(ASN1_TYPE) *necdsa=NULL;
509         unsigned char     *p=NULL, *q=NULL;
510         int len=0;
511         EC_POINT          *point=NULL;
512
513         if (pkey->pkey.ecdsa == NULL || pkey->pkey.ecdsa->group == NULL)
514         {
515                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, EVP_R_MISSING_PARAMETERS);
516                 return 0;
517         }
518         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_ecdsa_with_SHA1);
519         len = i2d_ECDSAParameters(pkey->pkey.ecdsa, NULL);
520         if ((p = OPENSSL_malloc(len)) == NULL)
521         {
522                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
523                 return 0;
524         }
525         q = p;
526         if (!i2d_ECDSAParameters(pkey->pkey.ecdsa, &q))
527         {
528                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ECDSA_LIB);
529                 OPENSSL_free(p);
530                 return 0;
531         }
532         if ((params = ASN1_STRING_new()) == NULL)
533         {
534                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
535                 OPENSSL_free(p);
536                 return 0;
537                 
538         }
539         if (!ASN1_STRING_set(params, p, len))
540         {
541                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ASN1_LIB);
542                 OPENSSL_free(p);
543                 return 0;
544         }
545         OPENSSL_free(p);
546         if ((prkey = BN_to_ASN1_INTEGER(pkey->pkey.ecdsa->priv_key, NULL)) == NULL)
547         {
548                 EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_ASN1_LIB);
549                 return 0;
550         }
551
552         switch(p8->broken) {
553
554                 case PKCS8_OK:
555                 case PKCS8_NO_OCTET:
556
557                 if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
558                                          &p8->pkey->value.octet_string)) 
559                 {
560                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
561                         M_ASN1_INTEGER_free(prkey);
562                         return 0;
563                 }
564
565                 ASN1_INTEGER_free(prkey);
566                 p8->pkeyalg->parameter->value.sequence = params;
567                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
568
569                 break;
570
571                 case PKCS8_NS_DB:
572
573                 p8->pkeyalg->parameter->value.sequence = params;
574                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
575                 necdsa = sk_ASN1_TYPE_new_null();
576                 if (necdsa == NULL || (ttmp = ASN1_TYPE_new()) == NULL)
577                 {
578                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
579                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
580                         return 0;
581                 }
582
583                 if ((point = EC_GROUP_get0_generator(pkey->pkey.ecdsa->group)) == NULL)
584                 {
585                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_EC_LIB);
586                         return 0;
587                 }
588                 len = EC_POINT_point2oct(pkey->pkey.ecdsa->group, point, POINT_CONVERSION_COMPRESSED,
589                                          NULL, 0, NULL);
590                 p = OPENSSL_malloc(len);
591                 if (!len || !p || !EC_POINT_point2oct(pkey->pkey.ecdsa->group, point,
592                         POINT_CONVERSION_COMPRESSED, p, len, NULL))
593                 {
594                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_EC_LIB);
595                         OPENSSL_free(p);
596                         return 0;
597                 }
598                 if ((ttmp->value.octet_string = ASN1_OCTET_STRING_new()) == NULL)
599                 {
600                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
601                         return 0;
602                 }
603                 if (!ASN1_OCTET_STRING_set(ttmp->value.octet_string, p, len))
604                 {
605                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, EVP_R_ASN1_LIB);
606                         return 0;
607                 }
608                 OPENSSL_free(p);
609                 
610                 ttmp->type = V_ASN1_OCTET_STRING;
611                 if (!sk_ASN1_TYPE_push(necdsa, ttmp))
612                 {
613                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
614                         ASN1_INTEGER_free(prkey);
615                         return 0;
616                 }
617
618                 if ((ttmp = ASN1_TYPE_new()) == NULL)
619                 {
620                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
621                         return 0;
622                 }
623                 ttmp->value.integer = prkey;
624                 ttmp->type = V_ASN1_INTEGER;
625                 if (!sk_ASN1_TYPE_push(necdsa, ttmp))
626                 {
627                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
628                         ASN1_INTEGER_free(prkey);
629                         return 0;
630                 }
631
632                 if ((p8->pkey->value.octet_string = ASN1_OCTET_STRING_new()) == NULL)
633                 {       
634                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
635                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
636                         return 0;
637                 }
638
639                 if (!ASN1_seq_pack_ASN1_TYPE(necdsa, i2d_ASN1_TYPE,
640                                          &p8->pkey->value.octet_string->data,
641                                          &p8->pkey->value.octet_string->length)) 
642                 {
643
644                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
645                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
646                         return 0;
647                 }
648                 sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
649                 break;
650
651                 case PKCS8_EMBEDDED_PARAM:
652
653                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
654                 necdsa = sk_ASN1_TYPE_new_null();
655                 if ((ttmp = ASN1_TYPE_new()) == NULL)
656                 {
657                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
658                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
659                         ASN1_INTEGER_free(prkey);
660                         return 0;
661                 }
662                 ttmp->value.sequence = params;
663                 ttmp->type = V_ASN1_SEQUENCE;
664                 if (!sk_ASN1_TYPE_push(necdsa, ttmp))
665                 {
666                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
667                         ASN1_INTEGER_free(prkey);
668                         return 0;
669                 }
670
671                 if ((ttmp = ASN1_TYPE_new()) == NULL)
672                 {
673                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
674                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
675                         ASN1_INTEGER_free(prkey);
676                         return 0;
677                 }
678                 ttmp->value.integer = prkey;
679                 ttmp->type = V_ASN1_INTEGER;
680                 if (!sk_ASN1_TYPE_push(necdsa, ttmp))
681                 {
682                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
683                         ASN1_INTEGER_free(prkey);
684                         return 0;
685                 }
686
687                 if ((p8->pkey->value.octet_string = ASN1_OCTET_STRING_new()) == NULL)
688                 {
689                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
690                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
691                         return 0;
692                 }
693
694                 if (!ASN1_seq_pack_ASN1_TYPE(necdsa, i2d_ASN1_TYPE,
695                                          &p8->pkey->value.octet_string->data,
696                                          &p8->pkey->value.octet_string->length)) 
697                 {
698                         EVPerr(EVP_F_ECDSA_PKEY2PKCS8, ERR_R_MALLOC_FAILURE);
699                         sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
700                         return 0;
701                 }
702                 sk_ASN1_TYPE_pop_free(necdsa, ASN1_TYPE_free);
703                 break;
704         }
705         return 1;
706 }
707 #endif