15d4d667213a50927e5230adaf304282290ff54f
[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 #ifndef OPENSSL_NO_RSA
96         const unsigned char *cp;
97 #endif
98         int pkeylen;
99         int  nid;
100         char obj_tmp[80];
101
102         if(p8->pkey->type == V_ASN1_OCTET_STRING) {
103                 p8->broken = PKCS8_OK;
104                 p = p8->pkey->value.octet_string->data;
105                 pkeylen = p8->pkey->value.octet_string->length;
106         } else {
107                 p8->broken = PKCS8_NO_OCTET;
108                 p = p8->pkey->value.sequence->data;
109                 pkeylen = p8->pkey->value.sequence->length;
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;
116         nid = OBJ_obj2nid(a->algorithm);
117         switch(nid)
118         {
119 #ifndef OPENSSL_NO_RSA
120                 case NID_rsaEncryption:
121                 cp = p;
122                 if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
123                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
124                         return NULL;
125                 }
126                 EVP_PKEY_assign_RSA (pkey, rsa);
127                 break;
128 #endif
129 #if !defined(OPENSSL_NO_DSA) && !defined(OPENSSL_NO_ECDSA)
130                 case NID_ecdsa_with_SHA1:
131                 case NID_dsa:
132                 /* PKCS#8 DSA/ECDSA is weird: you just get a private key integer
133                  * and parameters in the AlgorithmIdentifier the pubkey must
134                  * be recalculated.
135                  */
136         
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, 
141                                                           d2i_ASN1_TYPE,
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                         }
152                     /* Handle Two broken types:
153                      * SEQUENCE {parameters, priv_key}
154                      * SEQUENCE {pub_key, priv_key}
155                      */
156
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                     {
161                         p8->broken = PKCS8_EMBEDDED_PARAM;
162                         param = t1;
163                     } 
164                     else if(a->parameter->type == V_ASN1_SEQUENCE) 
165                     {
166                         p8->broken = PKCS8_NS_DB;
167                         param = a->parameter;
168                     } 
169                     else 
170                     {
171                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
172                         goto err;
173                     }
174
175                     if(t2->type != V_ASN1_INTEGER) {
176                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
177                         goto err;
178                     }
179                     privkey = t2->value.integer;
180                 } 
181                 else 
182                 {
183                         if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) 
184                         {
185                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
186                                 goto err;
187                         }
188                         param = p8->pkeyalg->parameter;
189                 }
190                 if (!param || (param->type != V_ASN1_SEQUENCE)) 
191                 {
192                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
193                         goto err;
194                 }
195                 cp = p = param->value.sequence->data;
196                 plen = param->value.sequence->length;
197                 if (!(ctx = BN_CTX_new())) 
198                 {
199                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
200                         goto err;
201                 }
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                         }
267                         
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
277                 }
278                 break;
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);
285                 return NULL;
286                 break;
287 #endif
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
299 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
300 {
301         return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
302 }
303
304 /* Turn a private key into a PKCS8 structure */
305
306 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
307 {
308         PKCS8_PRIV_KEY_INFO *p8;
309
310         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
311                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
312                 return NULL;
313         }
314         p8->broken = broken;
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         }
321         p8->pkey->type = V_ASN1_OCTET_STRING;
322         switch (EVP_PKEY_type(pkey->type)) {
323 #ifndef OPENSSL_NO_RSA
324                 case EVP_PKEY_RSA:
325
326                 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
327
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;
337 #endif
338 #ifndef OPENSSL_NO_DSA
339                 case EVP_PKEY_DSA:
340                 if(!dsa_pkey2pkcs8(p8, pkey)) {
341                         PKCS8_PRIV_KEY_INFO_free (p8);
342                         return NULL;
343                 }
344
345                 break;
346 #endif
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
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         }
361         RAND_add(p8->pkey->value.octet_string->data,
362                  p8->pkey->value.octet_string->length, 0);
363         return p8;
364 }
365
366 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
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
389 #ifndef OPENSSL_NO_DSA
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;
395         STACK_OF(ASN1_TYPE) *ndsa;
396         unsigned char *p, *q;
397         int len;
398
399         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
400         len = i2d_DSAparams (pkey->pkey.dsa, NULL);
401         if (!(p = OPENSSL_malloc(len))) {
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);
410         OPENSSL_free(p);
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) {
418
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;
439                 ndsa = sk_ASN1_TYPE_new_null();
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;
447                 sk_ASN1_TYPE_push(ndsa, ttmp);
448
449                 ttmp = ASN1_TYPE_new();
450                 ttmp->value.integer = prkey;
451                 ttmp->type = V_ASN1_INTEGER;
452                 sk_ASN1_TYPE_push(ndsa, ttmp);
453
454                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
455
456                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
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);
461                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
462                         M_ASN1_INTEGER_free(prkey);
463                         return 0;
464                 }
465                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
466                 break;
467
468                 case PKCS8_EMBEDDED_PARAM:
469
470                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
471                 ndsa = sk_ASN1_TYPE_new_null();
472                 ttmp = ASN1_TYPE_new();
473                 ttmp->value.sequence = params;
474                 ttmp->type = V_ASN1_SEQUENCE;
475                 sk_ASN1_TYPE_push(ndsa, ttmp);
476
477                 ttmp = ASN1_TYPE_new();
478                 ttmp->value.integer = prkey;
479                 ttmp->type = V_ASN1_INTEGER;
480                 sk_ASN1_TYPE_push(ndsa, ttmp);
481
482                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
483
484                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
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);
489                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
490                         M_ASN1_INTEGER_free (prkey);
491                         return 0;
492                 }
493                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
494                 break;
495         }
496         return 1;
497 }
498 #endif
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