9133bc6d297ef33878e71eefdc3f72b5181868ea
[openssl.git] / crypto / asn1 / n_pkey.c
1 /* crypto/asn1/n_pkey.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include <openssl/rsa.h>
62 #include <openssl/objects.h>
63 #include <openssl/asn1_mac.h>
64 #include <openssl/evp.h>
65 #include <openssl/x509.h>
66
67
68 #ifndef NO_RC4
69
70 typedef struct netscape_pkey_st
71         {
72         ASN1_INTEGER *version;
73         X509_ALGOR *algor;
74         ASN1_OCTET_STRING *private_key;
75         } NETSCAPE_PKEY;
76
77 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp);
78 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a,unsigned char **pp, long length);
79 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void);
80 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *);
81 int i2d_Netscape_RSA(RSA *a, unsigned char **pp, int (*cb)())
82         {
83         int i,j,l[6];
84         NETSCAPE_PKEY *pkey;
85         unsigned char buf[256],*zz;
86         unsigned char key[EVP_MAX_KEY_LENGTH];
87         EVP_CIPHER_CTX ctx;
88         X509_ALGOR *alg=NULL;
89         ASN1_OCTET_STRING os,os2;
90         M_ASN1_I2D_vars(a);
91
92         if (a == NULL) return(0);
93
94 #ifdef WIN32
95         r=r; /* shut the damn compiler up :-) */
96 #endif
97
98         os.data=os2.data=NULL;
99         if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
100         if (!ASN1_INTEGER_set(pkey->version,0)) goto err;
101
102         if (pkey->algor->algorithm != NULL)
103                 ASN1_OBJECT_free(pkey->algor->algorithm);
104         pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
105         if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
106         pkey->algor->parameter->type=V_ASN1_NULL;
107
108         l[0]=i2d_RSAPrivateKey(a,NULL);
109         pkey->private_key->length=l[0];
110
111         os2.length=i2d_NETSCAPE_PKEY(pkey,NULL);
112         l[1]=i2d_ASN1_OCTET_STRING(&os2,NULL);
113
114         if ((alg=X509_ALGOR_new()) == NULL) goto err;
115         if (alg->algorithm != NULL)
116                 ASN1_OBJECT_free(alg->algorithm);
117         alg->algorithm=OBJ_nid2obj(NID_rc4);
118         if ((alg->parameter=ASN1_TYPE_new()) == NULL) goto err;
119         alg->parameter->type=V_ASN1_NULL;
120
121         l[2]=i2d_X509_ALGOR(alg,NULL);
122         l[3]=ASN1_object_size(1,l[2]+l[1],V_ASN1_SEQUENCE);
123
124 #ifndef CONST_STRICT
125         os.data=(unsigned char *)"private-key";
126 #endif
127         os.length=11;
128         l[4]=i2d_ASN1_OCTET_STRING(&os,NULL);
129
130         l[5]=ASN1_object_size(1,l[4]+l[3],V_ASN1_SEQUENCE);
131
132         if (pp == NULL)
133                 {
134                 if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
135                 if (alg != NULL) X509_ALGOR_free(alg);
136                 return(l[5]);
137                 }
138
139         if (pkey->private_key->data != NULL)
140                 Free((char *)pkey->private_key->data);
141         if ((pkey->private_key->data=(unsigned char *)Malloc(l[0])) == NULL)
142                 {
143                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
144                 goto err;
145                 }
146         zz=pkey->private_key->data;
147         i2d_RSAPrivateKey(a,&zz);
148
149         if ((os2.data=(unsigned char *)Malloc(os2.length)) == NULL)
150                 {
151                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
152                 goto err;
153                 }
154         zz=os2.data;
155         i2d_NETSCAPE_PKEY(pkey,&zz);
156                 
157         if (cb == NULL)
158                 cb=EVP_read_pw_string;
159         i=cb(buf,256,"Enter Private Key password:",1);
160         if (i != 0)
161                 {
162                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
163                 goto err;
164                 }
165         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
166                 strlen((char *)buf),1,key,NULL);
167         memset(buf,0,256);
168
169         EVP_CIPHER_CTX_init(&ctx);
170         EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
171         EVP_EncryptUpdate(&ctx,os2.data,&i,os2.data,os2.length);
172         EVP_EncryptFinal(&ctx,&(os2.data[i]),&j);
173         EVP_CIPHER_CTX_cleanup(&ctx);
174
175         p= *pp;
176         ASN1_put_object(&p,1,l[4]+l[3],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
177         i2d_ASN1_OCTET_STRING(&os,&p);
178         ASN1_put_object(&p,1,l[2]+l[1],V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL);
179         i2d_X509_ALGOR(alg,&p);
180         i2d_ASN1_OCTET_STRING(&os2,&p);
181         ret=l[5];
182 err:
183         if (os2.data != NULL) Free(os2.data);
184         if (alg != NULL) X509_ALGOR_free(alg);
185         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
186         r=r;
187         return(ret);
188         }
189
190 RSA *d2i_Netscape_RSA(RSA **a, unsigned char **pp, long length, int (*cb)())
191         {
192         RSA *ret=NULL;
193         ASN1_OCTET_STRING *os=NULL;
194         ASN1_CTX c;
195
196         c.pp=pp;
197         c.error=ASN1_R_DECODING_ERROR;
198
199         M_ASN1_D2I_Init();
200         M_ASN1_D2I_start_sequence();
201         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
202         if ((os->length != 11) || (strncmp("private-key",
203                 (char *)os->data,os->length) != 0))
204                 {
205                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
206                 ASN1_BIT_STRING_free(os);
207                 goto err;
208                 }
209         ASN1_BIT_STRING_free(os);
210         c.q=c.p;
211         if ((ret=d2i_Netscape_RSA_2(a,&c.p,c.slen,cb)) == NULL) goto err;
212         c.slen-=(c.p-c.q);
213
214         M_ASN1_D2I_Finish(a,RSA_free,ASN1_F_D2I_NETSCAPE_RSA);
215         }
216
217 RSA *d2i_Netscape_RSA_2(RSA **a, unsigned char **pp, long length,
218              int (*cb)())
219         {
220         NETSCAPE_PKEY *pkey=NULL;
221         RSA *ret=NULL;
222         int i,j;
223         unsigned char buf[256],*zz;
224         unsigned char key[EVP_MAX_KEY_LENGTH];
225         EVP_CIPHER_CTX ctx;
226         X509_ALGOR *alg=NULL;
227         ASN1_OCTET_STRING *os=NULL;
228         ASN1_CTX c;
229
230         c.error=ERR_R_NESTED_ASN1_ERROR;
231         c.pp=pp;
232
233         M_ASN1_D2I_Init();
234         M_ASN1_D2I_start_sequence();
235         M_ASN1_D2I_get(alg,d2i_X509_ALGOR);
236         if (OBJ_obj2nid(alg->algorithm) != NID_rc4)
237                 {
238                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
239                 goto err;
240                 }
241         M_ASN1_D2I_get(os,d2i_ASN1_OCTET_STRING);
242         if (cb == NULL)
243                 cb=EVP_read_pw_string;
244         i=cb(buf,256,"Enter Private Key password:",0);
245         if (i != 0)
246                 {
247                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
248                 goto err;
249                 }
250
251         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,
252                 strlen((char *)buf),1,key,NULL);
253         memset(buf,0,256);
254
255         EVP_CIPHER_CTX_init(&ctx);
256         EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
257         EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
258         EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
259         EVP_CIPHER_CTX_cleanup(&ctx);
260         os->length=i+j;
261
262         zz=os->data;
263
264         if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
265                 {
266                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
267                 goto err;
268                 }
269                 
270         zz=pkey->private_key->data;
271         if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
272                 {
273                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
274                 goto err;
275                 }
276         if (!asn1_Finish(&c)) goto err;
277         *pp=c.p;
278 err:
279         if (pkey != NULL) NETSCAPE_PKEY_free(pkey);
280         if (os != NULL) ASN1_BIT_STRING_free(os);
281         if (alg != NULL) X509_ALGOR_free(alg);
282         return(ret);
283         }
284
285 static int i2d_NETSCAPE_PKEY(NETSCAPE_PKEY *a, unsigned char **pp)
286         {
287         M_ASN1_I2D_vars(a);
288
289
290         M_ASN1_I2D_len(a->version,      i2d_ASN1_INTEGER);
291         M_ASN1_I2D_len(a->algor,        i2d_X509_ALGOR);
292         M_ASN1_I2D_len(a->private_key,  i2d_ASN1_OCTET_STRING);
293
294         M_ASN1_I2D_seq_total();
295
296         M_ASN1_I2D_put(a->version,      i2d_ASN1_INTEGER);
297         M_ASN1_I2D_put(a->algor,        i2d_X509_ALGOR);
298         M_ASN1_I2D_put(a->private_key,  i2d_ASN1_OCTET_STRING);
299
300         M_ASN1_I2D_finish();
301         }
302
303 static NETSCAPE_PKEY *d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a, unsigned char **pp,
304              long length)
305         {
306         M_ASN1_D2I_vars(a,NETSCAPE_PKEY *,NETSCAPE_PKEY_new);
307
308         M_ASN1_D2I_Init();
309         M_ASN1_D2I_start_sequence();
310         M_ASN1_D2I_get(ret->version,d2i_ASN1_INTEGER);
311         M_ASN1_D2I_get(ret->algor,d2i_X509_ALGOR);
312         M_ASN1_D2I_get(ret->private_key,d2i_ASN1_OCTET_STRING);
313         M_ASN1_D2I_Finish(a,NETSCAPE_PKEY_free,ASN1_F_D2I_NETSCAPE_PKEY);
314         }
315
316 static NETSCAPE_PKEY *NETSCAPE_PKEY_new(void)
317         {
318         NETSCAPE_PKEY *ret=NULL;
319         ASN1_CTX c;
320
321         M_ASN1_New_Malloc(ret,NETSCAPE_PKEY);
322         M_ASN1_New(ret->version,ASN1_INTEGER_new);
323         M_ASN1_New(ret->algor,X509_ALGOR_new);
324         M_ASN1_New(ret->private_key,ASN1_OCTET_STRING_new);
325         return(ret);
326         M_ASN1_New_Error(ASN1_F_NETSCAPE_PKEY_NEW);
327         }
328
329 static void NETSCAPE_PKEY_free(NETSCAPE_PKEY *a)
330         {
331         if (a == NULL) return;
332         ASN1_INTEGER_free(a->version);
333         X509_ALGOR_free(a->algor);
334         ASN1_OCTET_STRING_free(a->private_key);
335         Free((char *)a);
336         }
337
338 #endif /* NO_RC4 */
339