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