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