summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/n_pkey.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/n_pkey.c')
-rw-r--r--src/lib/libcrypto/asn1/n_pkey.c430
1 files changed, 0 insertions, 430 deletions
diff --git a/src/lib/libcrypto/asn1/n_pkey.c b/src/lib/libcrypto/asn1/n_pkey.c
deleted file mode 100644
index d3a7431356..0000000000
--- a/src/lib/libcrypto/asn1/n_pkey.c
+++ /dev/null
@@ -1,430 +0,0 @@
1/* $OpenBSD: n_pkey.c,v 1.26 2015/03/19 14:00:22 tedu Exp $ */
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 <string.h>
61
62#include <openssl/opensslconf.h>
63
64#ifndef OPENSSL_NO_RSA
65#include <openssl/asn1t.h>
66#include <openssl/err.h>
67#include <openssl/evp.h>
68#include <openssl/objects.h>
69#include <openssl/rsa.h>
70#include <openssl/x509.h>
71
72#ifndef OPENSSL_NO_RC4
73
74typedef struct netscape_pkey_st {
75 long version;
76 X509_ALGOR *algor;
77 ASN1_OCTET_STRING *private_key;
78} NETSCAPE_PKEY;
79
80typedef struct netscape_encrypted_pkey_st {
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
90static const ASN1_AUX NETSCAPE_ENCRYPTED_PKEY_aux = {
91 .flags = ASN1_AFLG_BROKEN,
92};
93static const ASN1_TEMPLATE NETSCAPE_ENCRYPTED_PKEY_seq_tt[] = {
94 {
95 .offset = offsetof(NETSCAPE_ENCRYPTED_PKEY, os),
96 .field_name = "os",
97 .item = &ASN1_OCTET_STRING_it,
98 },
99 {
100 .offset = offsetof(NETSCAPE_ENCRYPTED_PKEY, enckey),
101 .field_name = "enckey",
102 .item = &X509_SIG_it,
103 },
104};
105
106const ASN1_ITEM NETSCAPE_ENCRYPTED_PKEY_it = {
107 .itype = ASN1_ITYPE_SEQUENCE,
108 .utype = V_ASN1_SEQUENCE,
109 .templates = NETSCAPE_ENCRYPTED_PKEY_seq_tt,
110 .tcount = sizeof(NETSCAPE_ENCRYPTED_PKEY_seq_tt) / sizeof(ASN1_TEMPLATE),
111 .funcs = &NETSCAPE_ENCRYPTED_PKEY_aux,
112 .size = sizeof(NETSCAPE_ENCRYPTED_PKEY),
113 .sname = "NETSCAPE_ENCRYPTED_PKEY",
114};
115
116DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY)
117DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_ENCRYPTED_PKEY, NETSCAPE_ENCRYPTED_PKEY)
118
119NETSCAPE_ENCRYPTED_PKEY *
120d2i_NETSCAPE_ENCRYPTED_PKEY(NETSCAPE_ENCRYPTED_PKEY **a, const unsigned char **in, long len)
121{
122 return (NETSCAPE_ENCRYPTED_PKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
123 &NETSCAPE_ENCRYPTED_PKEY_it);
124}
125
126int
127i2d_NETSCAPE_ENCRYPTED_PKEY(const NETSCAPE_ENCRYPTED_PKEY *a, unsigned char **out)
128{
129 return ASN1_item_i2d((ASN1_VALUE *)a, out, &NETSCAPE_ENCRYPTED_PKEY_it);
130}
131
132NETSCAPE_ENCRYPTED_PKEY *
133NETSCAPE_ENCRYPTED_PKEY_new(void)
134{
135 return (NETSCAPE_ENCRYPTED_PKEY *)ASN1_item_new(&NETSCAPE_ENCRYPTED_PKEY_it);
136}
137
138void
139NETSCAPE_ENCRYPTED_PKEY_free(NETSCAPE_ENCRYPTED_PKEY *a)
140{
141 ASN1_item_free((ASN1_VALUE *)a, &NETSCAPE_ENCRYPTED_PKEY_it);
142}
143
144static const ASN1_TEMPLATE NETSCAPE_PKEY_seq_tt[] = {
145 {
146 .offset = offsetof(NETSCAPE_PKEY, version),
147 .field_name = "version",
148 .item = &LONG_it,
149 },
150 {
151 .offset = offsetof(NETSCAPE_PKEY, algor),
152 .field_name = "algor",
153 .item = &X509_ALGOR_it,
154 },
155 {
156 .offset = offsetof(NETSCAPE_PKEY, private_key),
157 .field_name = "private_key",
158 .item = &ASN1_OCTET_STRING_it,
159 },
160};
161
162const ASN1_ITEM NETSCAPE_PKEY_it = {
163 .itype = ASN1_ITYPE_SEQUENCE,
164 .utype = V_ASN1_SEQUENCE,
165 .templates = NETSCAPE_PKEY_seq_tt,
166 .tcount = sizeof(NETSCAPE_PKEY_seq_tt) / sizeof(ASN1_TEMPLATE),
167 .size = sizeof(NETSCAPE_PKEY),
168 .sname = "NETSCAPE_PKEY",
169};
170
171DECLARE_ASN1_FUNCTIONS_const(NETSCAPE_PKEY)
172DECLARE_ASN1_ENCODE_FUNCTIONS_const(NETSCAPE_PKEY, NETSCAPE_PKEY)
173
174NETSCAPE_PKEY *
175d2i_NETSCAPE_PKEY(NETSCAPE_PKEY **a, const unsigned char **in, long len)
176{
177 return (NETSCAPE_PKEY *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
178 &NETSCAPE_PKEY_it);
179}
180
181int
182i2d_NETSCAPE_PKEY(const NETSCAPE_PKEY *a, unsigned char **out)
183{
184 return ASN1_item_i2d((ASN1_VALUE *)a, out, &NETSCAPE_PKEY_it);
185}
186
187NETSCAPE_PKEY *
188NETSCAPE_PKEY_new(void)
189{
190 return (NETSCAPE_PKEY *)ASN1_item_new(&NETSCAPE_PKEY_it);
191}
192
193void
194NETSCAPE_PKEY_free(NETSCAPE_PKEY *a)
195{
196 ASN1_item_free((ASN1_VALUE *)a, &NETSCAPE_PKEY_it);
197}
198
199static RSA *d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
200 int (*cb)(char *buf, int len, const char *prompt, int verify), int sgckey);
201
202int
203i2d_Netscape_RSA(const RSA *a, unsigned char **pp,
204 int (*cb)(char *buf, int len, const char *prompt, int verify))
205{
206 return i2d_RSA_NET(a, pp, cb, 0);
207}
208
209int
210i2d_RSA_NET(const RSA *a, unsigned char **pp,
211 int (*cb)(char *buf, int len, const char *prompt, int verify), int sgckey)
212{
213 int i, j, ret = 0;
214 int rsalen, pkeylen, olen;
215 NETSCAPE_PKEY *pkey = NULL;
216 NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
217 unsigned char buf[256], *zz;
218 unsigned char key[EVP_MAX_KEY_LENGTH];
219 EVP_CIPHER_CTX ctx;
220 EVP_CIPHER_CTX_init(&ctx);
221
222 if (a == NULL)
223 return (0);
224
225 if ((pkey = NETSCAPE_PKEY_new()) == NULL)
226 goto err;
227 if ((enckey = NETSCAPE_ENCRYPTED_PKEY_new()) == NULL)
228 goto err;
229 pkey->version = 0;
230
231 pkey->algor->algorithm = OBJ_nid2obj(NID_rsaEncryption);
232 if ((pkey->algor->parameter = ASN1_TYPE_new()) == NULL)
233 goto err;
234 pkey->algor->parameter->type = V_ASN1_NULL;
235
236 rsalen = i2d_RSAPrivateKey(a, NULL);
237
238 /* Fake some octet strings just for the initial length
239 * calculation.
240 */
241 pkey->private_key->length = rsalen;
242 pkeylen = i2d_NETSCAPE_PKEY(pkey, NULL);
243 enckey->enckey->digest->length = pkeylen;
244 enckey->os->length = 11; /* "private-key" */
245 enckey->enckey->algor->algorithm = OBJ_nid2obj(NID_rc4);
246 if ((enckey->enckey->algor->parameter = ASN1_TYPE_new()) == NULL)
247 goto err;
248 enckey->enckey->algor->parameter->type = V_ASN1_NULL;
249
250 if (pp == NULL) {
251 olen = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, NULL);
252 NETSCAPE_PKEY_free(pkey);
253 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
254 return olen;
255 }
256
257 /* Since its RC4 encrypted length is actual length */
258 if ((zz = malloc(rsalen)) == NULL) {
259 ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE);
260 goto err;
261 }
262
263 pkey->private_key->data = zz;
264 /* Write out private key encoding */
265 i2d_RSAPrivateKey(a, &zz);
266
267 if ((zz = malloc(pkeylen)) == NULL) {
268 ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE);
269 goto err;
270 }
271
272 if (!ASN1_STRING_set(enckey->os, "private-key", -1)) {
273 ASN1err(ASN1_F_I2D_RSA_NET, ERR_R_MALLOC_FAILURE);
274 goto err;
275 }
276 enckey->enckey->digest->data = zz;
277 i2d_NETSCAPE_PKEY(pkey, &zz);
278
279 /* Wipe the private key encoding */
280 OPENSSL_cleanse(pkey->private_key->data, rsalen);
281
282 if (cb == NULL)
283 cb = EVP_read_pw_string;
284 i = cb((char *)buf, sizeof(buf), "Enter Private Key password:", 1);
285 if (i != 0) {
286 ASN1err(ASN1_F_I2D_RSA_NET, ASN1_R_BAD_PASSWORD_READ);
287 goto err;
288 }
289 i = strlen((char *)buf);
290 /* If the key is used for SGC the algorithm is modified a little. */
291 if (sgckey) {
292 if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
293 goto err;
294 memcpy(buf + 16, "SGCKEYSALT", 10);
295 i = 26;
296 }
297
298 if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i,1, key, NULL))
299 goto err;
300 OPENSSL_cleanse(buf, sizeof(buf));
301
302 /* Encrypt private key in place */
303 zz = enckey->enckey->digest->data;
304 if (!EVP_EncryptInit_ex(&ctx, EVP_rc4(), NULL, key, NULL))
305 goto err;
306 if (!EVP_EncryptUpdate(&ctx, zz, &i, zz, pkeylen))
307 goto err;
308 if (!EVP_EncryptFinal_ex(&ctx, zz + i, &j))
309 goto err;
310
311 ret = i2d_NETSCAPE_ENCRYPTED_PKEY(enckey, pp);
312err:
313 EVP_CIPHER_CTX_cleanup(&ctx);
314 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
315 NETSCAPE_PKEY_free(pkey);
316 return (ret);
317}
318
319
320RSA *
321d2i_Netscape_RSA(RSA **a, const unsigned char **pp, long length,
322 int (*cb)(char *buf, int len, const char *prompt, int verify))
323{
324 return d2i_RSA_NET(a, pp, length, cb, 0);
325}
326
327RSA *
328d2i_RSA_NET(RSA **a, const unsigned char **pp, long length,
329 int (*cb)(char *buf, int len, const char *prompt, int verify), int sgckey)
330{
331 RSA *ret = NULL;
332 const unsigned char *p;
333 NETSCAPE_ENCRYPTED_PKEY *enckey = NULL;
334
335 p = *pp;
336
337 enckey = d2i_NETSCAPE_ENCRYPTED_PKEY(NULL, &p, length);
338 if (!enckey) {
339 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_DECODING_ERROR);
340 return NULL;
341 }
342
343 /* XXX 11 == strlen("private-key") */
344 if (enckey->os->length != 11 ||
345 memcmp("private-key", enckey->os->data, 11) != 0) {
346 ASN1err(ASN1_F_D2I_RSA_NET, ASN1_R_PRIVATE_KEY_HEADER_MISSING);
347 goto err;
348 }
349 if (OBJ_obj2nid(enckey->enckey->algor->algorithm) != NID_rc4) {
350 ASN1err(ASN1_F_D2I_RSA_NET,
351 ASN1_R_UNSUPPORTED_ENCRYPTION_ALGORITHM);
352 goto err;
353 }
354 if (cb == NULL)
355 cb = EVP_read_pw_string;
356 if ((ret = d2i_RSA_NET_2(a, enckey->enckey->digest, cb,
357 sgckey)) == NULL)
358 goto err;
359
360 *pp = p;
361
362err:
363 NETSCAPE_ENCRYPTED_PKEY_free(enckey);
364 return ret;
365
366}
367
368static RSA *
369d2i_RSA_NET_2(RSA **a, ASN1_OCTET_STRING *os,
370 int (*cb)(char *buf, int len, const char *prompt, int verify), int sgckey)
371{
372 NETSCAPE_PKEY *pkey = NULL;
373 RSA *ret = NULL;
374 int i, j;
375 unsigned char buf[256];
376 const unsigned char *zz;
377 unsigned char key[EVP_MAX_KEY_LENGTH];
378 EVP_CIPHER_CTX ctx;
379 EVP_CIPHER_CTX_init(&ctx);
380
381 i=cb((char *)buf, sizeof(buf), "Enter Private Key password:",0);
382 if (i != 0) {
383 ASN1err(ASN1_F_D2I_RSA_NET_2, ASN1_R_BAD_PASSWORD_READ);
384 goto err;
385 }
386
387 i = strlen((char *)buf);
388 if (sgckey){
389 if (!EVP_Digest(buf, i, buf, NULL, EVP_md5(), NULL))
390 goto err;
391 memcpy(buf + 16, "SGCKEYSALT", 10);
392 i = 26;
393 }
394
395 if (!EVP_BytesToKey(EVP_rc4(), EVP_md5(), NULL, buf, i,1, key, NULL))
396 goto err;
397 OPENSSL_cleanse(buf, sizeof(buf));
398
399 if (!EVP_DecryptInit_ex(&ctx, EVP_rc4(), NULL, key, NULL))
400 goto err;
401 if (!EVP_DecryptUpdate(&ctx, os->data, &i, os->data, os->length))
402 goto err;
403 if (!EVP_DecryptFinal_ex(&ctx, &(os->data[i]), &j))
404 goto err;
405 os->length = i + j;
406
407 zz = os->data;
408
409 if ((pkey = d2i_NETSCAPE_PKEY(NULL, &zz, os->length)) == NULL) {
410 ASN1err(ASN1_F_D2I_RSA_NET_2,
411 ASN1_R_UNABLE_TO_DECODE_RSA_PRIVATE_KEY);
412 goto err;
413 }
414
415 zz = pkey->private_key->data;
416 if ((ret = d2i_RSAPrivateKey(a, &zz,
417 pkey->private_key->length)) == NULL) {
418 ASN1err(ASN1_F_D2I_RSA_NET_2, ASN1_R_UNABLE_TO_DECODE_RSA_KEY);
419 goto err;
420 }
421
422err:
423 EVP_CIPHER_CTX_cleanup(&ctx);
424 NETSCAPE_PKEY_free(pkey);
425 return (ret);
426}
427
428#endif /* OPENSSL_NO_RC4 */
429
430#endif