When RSA or DSA are disabled, do not include the stuff that's specific
[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 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
69 /* Extract a private key from a PKCS8 structure */
70
71 EVP_PKEY *EVP_PKCS82PKEY (PKCS8_PRIV_KEY_INFO *p8)
72 {
73         EVP_PKEY *pkey = NULL;
74 #ifndef OPENSSL_NO_RSA
75         RSA *rsa = NULL;
76 #endif
77 #ifndef OPENSSL_NO_DSA
78         DSA *dsa = NULL;
79         ASN1_INTEGER *privkey;
80         ASN1_TYPE *t1, *t2, *param = NULL;
81         STACK_OF(ASN1_TYPE) *ndsa = NULL;
82         BN_CTX *ctx = NULL;
83         int plen;
84 #endif
85         X509_ALGOR *a;
86         unsigned char *p;
87 #ifndef OPENSSL_NO_RSA
88         const unsigned char *cp;
89 #endif
90         int pkeylen;
91         char obj_tmp[80];
92
93         if(p8->pkey->type == V_ASN1_OCTET_STRING) {
94                 p8->broken = PKCS8_OK;
95                 p = p8->pkey->value.octet_string->data;
96                 pkeylen = p8->pkey->value.octet_string->length;
97         } else {
98                 p8->broken = PKCS8_NO_OCTET;
99                 p = p8->pkey->value.sequence->data;
100                 pkeylen = p8->pkey->value.sequence->length;
101         }
102         if (!(pkey = EVP_PKEY_new())) {
103                 EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
104                 return NULL;
105         }
106         a = p8->pkeyalg;
107         switch (OBJ_obj2nid(a->algorithm))
108         {
109 #ifndef OPENSSL_NO_RSA
110                 case NID_rsaEncryption:
111                 cp = p;
112                 if (!(rsa = d2i_RSAPrivateKey (NULL,&cp, pkeylen))) {
113                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
114                         return NULL;
115                 }
116                 EVP_PKEY_assign_RSA (pkey, rsa);
117                 break;
118 #endif
119 #ifndef OPENSSL_NO_DSA
120                 case NID_dsa:
121                 /* PKCS#8 DSA is weird: you just get a private key integer
122                  * and parameters in the AlgorithmIdentifier the pubkey must
123                  * be recalculated.
124                  */
125         
126                 /* Check for broken DSA PKCS#8, UGH! */
127                 if(*p == (V_ASN1_SEQUENCE|V_ASN1_CONSTRUCTED)) {
128                     if(!(ndsa = ASN1_seq_unpack_ASN1_TYPE(p, pkeylen, 
129                                                           d2i_ASN1_TYPE,
130                                                           ASN1_TYPE_free))) {
131                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
132                         goto dsaerr;
133                     }
134                     if(sk_ASN1_TYPE_num(ndsa) != 2 ) {
135                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
136                         goto dsaerr;
137                     }
138                     /* Handle Two broken types:
139                      * SEQUENCE {parameters, priv_key}
140                      * SEQUENCE {pub_key, priv_key}
141                      */
142
143                     t1 = sk_ASN1_TYPE_value(ndsa, 0);
144                     t2 = sk_ASN1_TYPE_value(ndsa, 1);
145                     if(t1->type == V_ASN1_SEQUENCE) {
146                         p8->broken = PKCS8_EMBEDDED_PARAM;
147                         param = t1;
148                     } else if(a->parameter->type == V_ASN1_SEQUENCE) {
149                         p8->broken = PKCS8_NS_DB;
150                         param = a->parameter;
151                     } else {
152                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
153                         goto dsaerr;
154                     }
155
156                     if(t2->type != V_ASN1_INTEGER) {
157                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
158                         goto dsaerr;
159                     }
160                     privkey = t2->value.integer;
161                 } else {
162                         if (!(privkey=d2i_ASN1_INTEGER (NULL, &p, pkeylen))) {
163                                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
164                                 goto dsaerr;
165                         }
166                         param = p8->pkeyalg->parameter;
167                 }
168                 if (!param || (param->type != V_ASN1_SEQUENCE)) {
169                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
170                         goto dsaerr;
171                 }
172                 cp = p = param->value.sequence->data;
173                 plen = param->value.sequence->length;
174                 if (!(dsa = d2i_DSAparams (NULL, &cp, plen))) {
175                         EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_DECODE_ERROR);
176                         goto dsaerr;
177                 }
178                 /* We have parameters now set private key */
179                 if (!(dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL))) {
180                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_DECODE_ERROR);
181                         goto dsaerr;
182                 }
183                 /* Calculate public key (ouch!) */
184                 if (!(dsa->pub_key = BN_new())) {
185                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
186                         goto dsaerr;
187                 }
188                 if (!(ctx = BN_CTX_new())) {
189                         EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
190                         goto dsaerr;
191                 }
192                         
193                 if (!BN_mod_exp(dsa->pub_key, dsa->g,
194                                                  dsa->priv_key, dsa->p, ctx)) {
195                         
196                         EVPerr(EVP_F_EVP_PKCS82PKEY,EVP_R_BN_PUBKEY_ERROR);
197                         goto dsaerr;
198                 }
199
200                 EVP_PKEY_assign_DSA(pkey, dsa);
201                 BN_CTX_free (ctx);
202                 if(ndsa) sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
203                 else ASN1_INTEGER_free(privkey);
204                 break;
205                 dsaerr:
206                 BN_CTX_free (ctx);
207                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
208                 DSA_free(dsa);
209                 EVP_PKEY_free(pkey);
210                 return NULL;
211                 break;
212 #endif
213                 default:
214                 EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
215                 if (!a->algorithm) strcpy (obj_tmp, "NULL");
216                 else i2t_ASN1_OBJECT(obj_tmp, 80, a->algorithm);
217                 ERR_add_error_data(2, "TYPE=", obj_tmp);
218                 EVP_PKEY_free (pkey);
219                 return NULL;
220         }
221         return pkey;
222 }
223
224 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8(EVP_PKEY *pkey)
225 {
226         return EVP_PKEY2PKCS8_broken(pkey, PKCS8_OK);
227 }
228
229 /* Turn a private key into a PKCS8 structure */
230
231 PKCS8_PRIV_KEY_INFO *EVP_PKEY2PKCS8_broken(EVP_PKEY *pkey, int broken)
232 {
233         PKCS8_PRIV_KEY_INFO *p8;
234
235         if (!(p8 = PKCS8_PRIV_KEY_INFO_new())) {        
236                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
237                 return NULL;
238         }
239         p8->broken = broken;
240         ASN1_INTEGER_set (p8->version, 0);
241         if (!(p8->pkeyalg->parameter = ASN1_TYPE_new ())) {
242                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
243                 PKCS8_PRIV_KEY_INFO_free (p8);
244                 return NULL;
245         }
246         p8->pkey->type = V_ASN1_OCTET_STRING;
247         switch (EVP_PKEY_type(pkey->type)) {
248 #ifndef OPENSSL_NO_RSA
249                 case EVP_PKEY_RSA:
250
251                 if(p8->broken == PKCS8_NO_OCTET) p8->pkey->type = V_ASN1_SEQUENCE;
252
253                 p8->pkeyalg->algorithm = OBJ_nid2obj(NID_rsaEncryption);
254                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
255                 if (!ASN1_pack_string ((char *)pkey, i2d_PrivateKey,
256                                          &p8->pkey->value.octet_string)) {
257                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
258                         PKCS8_PRIV_KEY_INFO_free (p8);
259                         return NULL;
260                 }
261                 break;
262 #endif
263 #ifndef OPENSSL_NO_DSA
264                 case EVP_PKEY_DSA:
265                 if(!dsa_pkey2pkcs8(p8, pkey)) {
266                         PKCS8_PRIV_KEY_INFO_free (p8);
267                         return NULL;
268                 }
269
270                 break;
271 #endif
272                 default:
273                 EVPerr(EVP_F_EVP_PKEY2PKCS8, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
274                 PKCS8_PRIV_KEY_INFO_free (p8);
275                 return NULL;
276         }
277         RAND_add(p8->pkey->value.octet_string->data,
278                  p8->pkey->value.octet_string->length, 0);
279         return p8;
280 }
281
282 PKCS8_PRIV_KEY_INFO *PKCS8_set_broken(PKCS8_PRIV_KEY_INFO *p8, int broken)
283 {
284         switch (broken) {
285
286                 case PKCS8_OK:
287                 p8->broken = PKCS8_OK;
288                 return p8;
289                 break;
290
291                 case PKCS8_NO_OCTET:
292                 p8->broken = PKCS8_NO_OCTET;
293                 p8->pkey->type = V_ASN1_SEQUENCE;
294                 return p8;
295                 break;
296
297                 default:
298                 EVPerr(EVP_F_EVP_PKCS8_SET_BROKEN,EVP_R_PKCS8_UNKNOWN_BROKEN_TYPE);
299                 return NULL;
300                 break;
301                 
302         }
303 }
304
305 #ifndef OPENSSL_NO_DSA
306 static int dsa_pkey2pkcs8(PKCS8_PRIV_KEY_INFO *p8, EVP_PKEY *pkey)
307 {
308         ASN1_STRING *params;
309         ASN1_INTEGER *prkey;
310         ASN1_TYPE *ttmp;
311         STACK_OF(ASN1_TYPE) *ndsa;
312         unsigned char *p, *q;
313         int len;
314
315         p8->pkeyalg->algorithm = OBJ_nid2obj(NID_dsa);
316         len = i2d_DSAparams (pkey->pkey.dsa, NULL);
317         if (!(p = OPENSSL_malloc(len))) {
318                 EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
319                 PKCS8_PRIV_KEY_INFO_free (p8);
320                 return 0;
321         }
322         q = p;
323         i2d_DSAparams (pkey->pkey.dsa, &q);
324         params = ASN1_STRING_new();
325         ASN1_STRING_set(params, p, len);
326         OPENSSL_free(p);
327         /* Get private key into integer */
328         if (!(prkey = BN_to_ASN1_INTEGER (pkey->pkey.dsa->priv_key, NULL))) {
329                 EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
330                 return 0;
331         }
332
333         switch(p8->broken) {
334
335                 case PKCS8_OK:
336                 case PKCS8_NO_OCTET:
337
338                 if (!ASN1_pack_string((char *)prkey, i2d_ASN1_INTEGER,
339                                          &p8->pkey->value.octet_string)) {
340                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
341                         M_ASN1_INTEGER_free (prkey);
342                         return 0;
343                 }
344
345                 M_ASN1_INTEGER_free (prkey);
346                 p8->pkeyalg->parameter->value.sequence = params;
347                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
348
349                 break;
350
351                 case PKCS8_NS_DB:
352
353                 p8->pkeyalg->parameter->value.sequence = params;
354                 p8->pkeyalg->parameter->type = V_ASN1_SEQUENCE;
355                 ndsa = sk_ASN1_TYPE_new_null();
356                 ttmp = ASN1_TYPE_new();
357                 if (!(ttmp->value.integer = BN_to_ASN1_INTEGER (pkey->pkey.dsa->pub_key, NULL))) {
358                         EVPerr(EVP_F_EVP_PKEY2PKCS8,EVP_R_ENCODE_ERROR);
359                         PKCS8_PRIV_KEY_INFO_free(p8);
360                         return 0;
361                 }
362                 ttmp->type = V_ASN1_INTEGER;
363                 sk_ASN1_TYPE_push(ndsa, ttmp);
364
365                 ttmp = ASN1_TYPE_new();
366                 ttmp->value.integer = prkey;
367                 ttmp->type = V_ASN1_INTEGER;
368                 sk_ASN1_TYPE_push(ndsa, ttmp);
369
370                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
371
372                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
373                                          &p8->pkey->value.octet_string->data,
374                                          &p8->pkey->value.octet_string->length)) {
375
376                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
377                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
378                         M_ASN1_INTEGER_free(prkey);
379                         return 0;
380                 }
381                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
382                 break;
383
384                 case PKCS8_EMBEDDED_PARAM:
385
386                 p8->pkeyalg->parameter->type = V_ASN1_NULL;
387                 ndsa = sk_ASN1_TYPE_new_null();
388                 ttmp = ASN1_TYPE_new();
389                 ttmp->value.sequence = params;
390                 ttmp->type = V_ASN1_SEQUENCE;
391                 sk_ASN1_TYPE_push(ndsa, ttmp);
392
393                 ttmp = ASN1_TYPE_new();
394                 ttmp->value.integer = prkey;
395                 ttmp->type = V_ASN1_INTEGER;
396                 sk_ASN1_TYPE_push(ndsa, ttmp);
397
398                 p8->pkey->value.octet_string = ASN1_OCTET_STRING_new();
399
400                 if (!ASN1_seq_pack_ASN1_TYPE(ndsa, i2d_ASN1_TYPE,
401                                          &p8->pkey->value.octet_string->data,
402                                          &p8->pkey->value.octet_string->length)) {
403
404                         EVPerr(EVP_F_EVP_PKEY2PKCS8,ERR_R_MALLOC_FAILURE);
405                         sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
406                         M_ASN1_INTEGER_free (prkey);
407                         return 0;
408                 }
409                 sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
410                 break;
411         }
412         return 1;
413 }
414 #endif