82a621224bb15947abd079ae97497dccc4d25cab
[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 #ifndef NO_RSA
60 #include <stdio.h>
61 #include "cryptlib.h"
62 #include <openssl/rsa.h>
63 #include <openssl/objects.h>
64 #include <openssl/asn1t.h>
65 #include <openssl/asn1_mac.h>
66 #include <openssl/evp.h>
67 #include <openssl/x509.h>
68
69
70 #ifndef NO_RC4
71
72 typedef struct netscape_pkey_st
73         {
74         long version;
75         X509_ALGOR *algor;
76         ASN1_OCTET_STRING *private_key;
77         } NETSCAPE_PKEY;
78
79 typedef struct netscape_encrypted_pkey_st
80         {
81         ASN1_OCTET_STRING *os;
82         /* This is the same structure as DigestInfo so use it:
83          * although this isn't really anything to do with
84          * digests.
85          */
86         X509_SIG *enckey;
87         } NETSCAPE_ENCRYPTED_PKEY;
88
89
90 ASN1_BROKEN_SEQUENCE(NETSCAPE_ENCRYPTED_PKEY) = {
91         ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, os, ASN1_OCTET_STRING),
92         ASN1_SIMPLE(NETSCAPE_ENCRYPTED_PKEY, enckey, X509_SIG)
93 } ASN1_BROKEN_SEQUENCE_END(NETSCAPE_ENCRYPTED_PKEY);
94
95 IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
96
97 ASN1_SEQUENCE(NETSCAPE_PKEY) = {
98         ASN1_SIMPLE(NETSCAPE_PKEY, version, LONG),
99         ASN1_SIMPLE(NETSCAPE_PKEY, algor, X509_ALGOR),
100         ASN1_SIMPLE(NETSCAPE_PKEY, private_key, ASN1_OCTET_STRING)
101 } ASN1_SEQUENCE_END(NETSCAPE_PKEY);
102
103 IMPLEMENT_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
104
105 static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
106              int (*cb)(), int sgckey);
107
108 int i2d_Netscape_RSA(const RSA *a, unsigned char **pp, int (*cb)())
109 {
110         return i2d_RSA_NET(a, pp, cb, 0);
111 }
112
113 int i2d_RSA_NET(const RSA *a, unsigned char **pp, int (*cb)(), int sgckey)
114         {
115         int i, j, ret = 0;
116         int rsalen, pkeylen, olen;
117         NETSCAPE_PKEY *pkey = NULL;
118         NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
119         unsigned char buf[256],*zz;
120         unsigned char key[EVP_MAX_KEY_LENGTH];
121         EVP_CIPHER_CTX ctx;
122
123         if (a == NULL) return(0);
124
125         if ((pkey=NETSCAPE_PKEY_new()) == NULL) goto err;
126         if ((enckey=NETSCAPE_ENCRYPTED_PKEY_new()) == NULL) goto err;
127         pkey->version = 0;
128
129         pkey->algor->algorithm=OBJ_nid2obj(NID_rsaEncryption);
130         if ((pkey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
131         pkey->algor->parameter->type=V_ASN1_NULL;
132
133         rsalen = i2d_RSAPrivateKey(a, NULL);
134
135         /* Fake some octet strings just for the initial length
136          * calculation.
137          */
138
139         pkey->private_key->length=rsalen;
140
141         pkeylen=i2d_NETSCAPE_PKEY(pkey,NULL);
142
143         enckey->enckey->digest->length = pkeylen;
144
145         enckey->os->length = 11;        /* "private-key" */
146
147         enckey->enckey->algor->algorithm=OBJ_nid2obj(NID_rc4);
148         if ((enckey->enckey->algor->parameter=ASN1_TYPE_new()) == NULL) goto err;
149         enckey->enckey->algor->parameter->type=V_ASN1_NULL;
150
151         if (pp == NULL)
152                 {
153                 olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL);
154                 NETSCAPE_PKEY_free(pkey);
155                 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
156                 return olen;
157                 }
158
159
160         /* Since its RC4 encrypted length is actual length */
161         if ((zz=(unsigned char *)OPENSSL_malloc(rsalen)) == NULL)
162                 {
163                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
164                 goto err;
165                 }
166
167         pkey->private_key->data = zz;
168         /* Write out private key encoding */
169         i2d_RSAPrivateKey(a,&zz);
170
171         if ((zz=OPENSSL_malloc(pkeylen)) == NULL)
172                 {
173                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
174                 goto err;
175                 }
176
177         if (!ASN1_STRING_set(enckey->os, "private-key", -1)) 
178                 {
179                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ERR_R_MALLOC_FAILURE);
180                 goto err;
181                 }
182         enckey->enckey->digest->data = zz;
183         i2d_NETSCAPE_PKEY(pkey,&zz);
184
185         /* Wipe the private key encoding */
186         memset(pkey->private_key->data, 0, rsalen);
187                 
188         if (cb == NULL)
189                 cb=EVP_read_pw_string;
190         i=cb(buf,256,"Enter Private Key password:",1);
191         if (i != 0)
192                 {
193                 ASN1err(ASN1_F_I2D_NETSCAPE_RSA,ASN1_R_BAD_PASSWORD_READ);
194                 goto err;
195                 }
196         i = strlen((char *)buf);
197         /* If the key is used for SGC the algorithm is modified a little. */
198         if(sgckey) {
199                 EVP_MD_CTX mctx;
200                 EVP_DigestInit(&mctx, EVP_md5());
201                 EVP_DigestUpdate(&mctx, buf, i);
202                 EVP_DigestFinal(&mctx, buf, NULL);
203                 memcpy(buf + 16, "SGCKEYSALT", 10);
204                 i = 26;
205         }
206                 
207         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
208         memset(buf,0,256);
209
210         /* Encrypt private key in place */
211         zz = enckey->enckey->digest->data;
212         EVP_CIPHER_CTX_init(&ctx);
213         EVP_EncryptInit(&ctx,EVP_rc4(),key,NULL);
214         EVP_EncryptUpdate(&ctx,zz,&i,zz,pkeylen);
215         EVP_EncryptFinal(&ctx,zz + i,&j);
216         EVP_CIPHER_CTX_cleanup(&ctx);
217
218         ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);
219 err:
220         NETSCAPE_ENCRYPTED_PKEY_free(enckey);
221         NETSCAPE_PKEY_free(pkey);
222         return(ret);
223         }
224
225
226 RSA *d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length, int (*cb)())
227 {
228         return d2i_RSA_NET(a, pp, length, cb, 0);
229 }
230
231 RSA *d2i_RSA_NET(RSA **a, const unsigned char **pp, long length, int (*cb)(), int sgckey)
232         {
233         RSA *ret=NULL;
234         const unsigned char *p, *kp;
235         NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
236
237         p = *pp;
238
239         enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length);
240         if(!enckey) {
241                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_DECODING_ERROR);
242                 return NULL;
243         }
244
245         if ((enckey->os->length != 11) || (strncmp("private-key",
246                 (char *)enckey->os->data,11) != 0))
247                 {
248                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA,ASN1_R_PRIVATE_KEY_HEADER_MISSING);
249                 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
250                 return NULL;
251                 }
252         if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4)
253                 {
254                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
255                 goto err;
256         }
257         kp = enckey->enckey->digest->data;
258         if (cb == NULL)
259                 cb=EVP_read_pw_string;
260         if ((ret=d2i_RSA_NET_2(a, enckey->enckey->digest,cb, sgckey)) == NULL) goto err;
261
262         *pp = p;
263
264         err:
265         NETSCAPE_ENCRYPTED_PKEY_free(enckey);
266         return ret;
267
268         }
269
270 static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
271              int (*cb)(), int sgckey)
272         {
273         NETSCAPE_PKEY *pkey=NULL;
274         RSA *ret=NULL;
275         int i,j;
276         unsigned char buf[256];
277         const unsigned char *zz;
278         unsigned char key[EVP_MAX_KEY_LENGTH];
279         EVP_CIPHER_CTX ctx;
280
281         i=cb(buf,256,"Enter Private Key password:",0);
282         if (i != 0)
283                 {
284                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_BAD_PASSWORD_READ);
285                 goto err;
286                 }
287
288         i = strlen((char *)buf);
289         if(sgckey){
290                 EVP_MD_CTX mctx;
291                 EVP_DigestInit(&mctx, EVP_md5());
292                 EVP_DigestUpdate(&mctx, buf, i);
293                 EVP_DigestFinal(&mctx, buf, NULL);
294                 memcpy(buf + 16, "SGCKEYSALT", 10);
295                 i = 26;
296         }
297                 
298         EVP_BytesToKey(EVP_rc4(),EVP_md5(),NULL,buf,i,1,key,NULL);
299         memset(buf,0,256);
300
301         EVP_CIPHER_CTX_init(&ctx);
302         EVP_DecryptInit(&ctx,EVP_rc4(),key,NULL);
303         EVP_DecryptUpdate(&ctx,os->data,&i,os->data,os->length);
304         EVP_DecryptFinal(&ctx,&(os->data[i]),&j);
305         EVP_CIPHER_CTX_cleanup(&ctx);
306         os->length=i+j;
307
308         zz=os->data;
309
310         if ((pkey=d2i_NETSCAPE_PKEY(NULL,&zz,os->length)) == NULL)
311                 {
312                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
313                 goto err;
314                 }
315                 
316         zz=pkey->private_key->data;
317         if ((ret=d2i_RSAPrivateKey(a,&zz,pkey->private_key->length)) == NULL)
318                 {
319                 ASN1err(ASN1_F_D2I_NETSCAPE_RSA_2,ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
320                 goto err;
321                 }
322 err:
323         NETSCAPE_PKEY_free(pkey);
324         return(ret);
325         }
326
327 #endif /* NO_RC4 */
328
329 #else /* !NO_RSA */
330
331 # if PEDANTIC
332 static void *dummy=&dummy;
333 # endif
334
335 #endif