1f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * All rights reserved.
3f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
4f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * This package is an SSL implementation written
5f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * by Eric Young (eay@cryptsoft.com).
6f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * The implementation was written so as to conform with Netscapes SSL.
7f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
8f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * This library is free for commercial and non-commercial use as long as
9f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * the following conditions are aheared to.  The following conditions
10f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * apply to all code found in this distribution, be it the RC4, RSA,
11f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * included with this distribution is covered by the same copyright terms
13f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
15f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * Copyright remains Eric Young's, and as such any Copyright notices in
16f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * the code are not to be removed.
17f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * If this package is used in a product, Eric Young should be given attribution
18f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * as the author of the parts of the library used.
19f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * This can be in the form of a textual message at program startup or
20f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * in documentation (online or textual) provided with the package.
21f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
227242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci * Redistribution and use in source and binary forms, with or without
23f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * modification, are permitted provided that the following conditions
24f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * are met:
25f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * 1. Redistributions of source code must retain the copyright
26f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
27f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
28f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
29f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
30f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * 3. All advertising materials mentioning features or use of this software
31f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    must display the following acknowledgement:
32f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    "This product includes cryptographic software written by
337242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci *     Eric Young (eay@cryptsoft.com)"
34f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    The word 'cryptographic' can be left out if the rouines from the library
35f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    being used are not cryptographic related :-).
36f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * 4. If you include any Windows specific code (or a derivative thereof) from
37f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    the apps directory (application code) you must include an acknowledgement:
38f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
40f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
457242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * SUCH DAMAGE.
51f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) *
52f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * The licence and distribution terms for any publically available version or
53f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * derivative of this code cannot be changed.  i.e. this code cannot simply be
54f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * copied and put under another distribution licence
55f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles) * [including the GNU Public Licence.] */
56f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
577242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include <openssl/pem.h>
58f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
59f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <stdio.h>
60f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
61f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/buf.h>
62f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/dh.h>
63f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/err.h>
64f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/evp.h>
65f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/mem.h>
66f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/obj.h>
67f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/pkcs8.h>
68f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/rand.h>
69f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include <openssl/x509.h>
70f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
71f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)#include "../evp/internal.h"
72f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
73f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
74f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)int pem_check_suffix(const char *pem_str, const char *suffix);
75f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
76f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)EVP_PKEY *PEM_read_bio_PrivateKey(BIO *bp, EVP_PKEY **x, pem_password_cb *cb, void *u)
77f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	{
78f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	char *nm=NULL;
79f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	const unsigned char *p=NULL;
80f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	unsigned char *data=NULL;
81f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	long len;
82f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	int slen;
83f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	EVP_PKEY *ret=NULL;
84f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
85f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_EVP_PKEY, bp, cb, u))
86f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		return NULL;
87f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	p = data;
88f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
89f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)	if (strcmp(nm,PEM_STRING_PKCS8INF) == 0) {
90f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		PKCS8_PRIV_KEY_INFO *p8inf;
91f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		p8inf=d2i_PKCS8_PRIV_KEY_INFO(NULL, &p, len);
92f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		if(!p8inf) goto p8err;
93f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		ret = EVP_PKCS82PKEY(p8inf);
94f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)		if(x) {
95f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)			if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
96f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)			*x = ret;
97		}
98		PKCS8_PRIV_KEY_INFO_free(p8inf);
99	} else if (strcmp(nm,PEM_STRING_PKCS8) == 0) {
100		PKCS8_PRIV_KEY_INFO *p8inf;
101		X509_SIG *p8;
102		int klen;
103		char psbuf[PEM_BUFSIZE];
104		p8 = d2i_X509_SIG(NULL, &p, len);
105		if(!p8) goto p8err;
106
107		klen = 0;
108		if (cb) klen=cb(psbuf,PEM_BUFSIZE,0,u);
109		if (klen <= 0) {
110			OPENSSL_PUT_ERROR(PEM, PEM_read_bio_PrivateKey, PEM_R_BAD_PASSWORD_READ);
111			X509_SIG_free(p8);
112			goto err;
113		}
114		p8inf = PKCS8_decrypt(p8, psbuf, klen);
115		X509_SIG_free(p8);
116		if(!p8inf) goto p8err;
117		ret = EVP_PKCS82PKEY(p8inf);
118		if(x) {
119			if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
120			*x = ret;
121		}
122		PKCS8_PRIV_KEY_INFO_free(p8inf);
123	} else if ((slen = pem_check_suffix(nm, "PRIVATE KEY")) > 0)
124		{
125		const EVP_PKEY_ASN1_METHOD *ameth;
126		ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen);
127		if (!ameth || !ameth->old_priv_decode)
128			goto p8err;
129		ret=d2i_PrivateKey(ameth->pkey_id,x,&p,len);
130		}
131p8err:
132	if (ret == NULL)
133		OPENSSL_PUT_ERROR(PEM, PEM_read_bio_PrivateKey, ERR_R_ASN1_LIB);
134
135err:
136	OPENSSL_free(nm);
137	OPENSSL_cleanse(data, len);
138	OPENSSL_free(data);
139	return(ret);
140	}
141
142int PEM_write_bio_PrivateKey(BIO *bp, EVP_PKEY *x, const EVP_CIPHER *enc,
143                                               unsigned char *kstr, int klen,
144                                               pem_password_cb *cb, void *u)
145	{
146	char pem_str[80];
147	if (!x->ameth || x->ameth->priv_encode)
148		return PEM_write_bio_PKCS8PrivateKey(bp, x, enc,
149							(char *)kstr, klen,
150							cb, u);
151
152	BIO_snprintf(pem_str, 80, "%s PRIVATE KEY", x->ameth->pem_str);
153	return PEM_ASN1_write_bio((i2d_of_void *)i2d_PrivateKey,
154				pem_str,bp,x,enc,kstr,klen,cb,u);
155	}
156
157static int public_key_type_from_str(const char *name, size_t len) {
158  if (len == 3 && memcmp(name, "RSA", 3) == 0) {
159    return EVP_PKEY_RSA;
160  } else if (len == 2 && memcmp(name, "DH", 2) == 0) {
161    return EVP_PKEY_DH;
162  } else if (len == 2 && memcmp(name, "EC", 2) == 0) {
163    return EVP_PKEY_EC;
164  }
165  return NID_undef;
166}
167
168static int set_pkey_type_from_str(EVP_PKEY *pkey, const char *name, size_t len) {
169  int nid = public_key_type_from_str(name, len);
170  if (nid == NID_undef) {
171    return 0;
172  }
173  return EVP_PKEY_set_type(pkey, nid);
174}
175
176EVP_PKEY *PEM_read_bio_Parameters(BIO *bp, EVP_PKEY **x)
177	{
178	char *nm=NULL;
179	const unsigned char *p=NULL;
180	unsigned char *data=NULL;
181	long len;
182	int slen;
183	EVP_PKEY *ret=NULL;
184
185	if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_PARAMETERS,
186								bp, 0, NULL))
187		return NULL;
188	p = data;
189
190	if ((slen = pem_check_suffix(nm, "PARAMETERS")) > 0)
191		{
192		ret = EVP_PKEY_new();
193		if (!ret)
194			goto err;
195		if (!set_pkey_type_from_str(ret, nm, slen)
196			|| !ret->ameth->param_decode
197			|| !ret->ameth->param_decode(ret, &p, len))
198			{
199			EVP_PKEY_free(ret);
200			ret = NULL;
201			goto err;
202			}
203		if(x)
204			{
205			if(*x) EVP_PKEY_free((EVP_PKEY *)*x);
206			*x = ret;
207			}
208		}
209err:
210	if (ret == NULL)
211		OPENSSL_PUT_ERROR(PEM, PEM_read_bio_Parameters, ERR_R_ASN1_LIB);
212	OPENSSL_free(nm);
213	OPENSSL_free(data);
214	return(ret);
215	}
216
217int PEM_write_bio_Parameters(BIO *bp, EVP_PKEY *x)
218	{
219	char pem_str[80];
220	if (!x->ameth || !x->ameth->param_encode)
221		return 0;
222
223	BIO_snprintf(pem_str, 80, "%s PARAMETERS", x->ameth->pem_str);
224	return PEM_ASN1_write_bio(
225		(i2d_of_void *)x->ameth->param_encode,
226				pem_str,bp,x,NULL,NULL,0,0,NULL);
227	}
228
229#ifndef OPENSSL_NO_FP_API
230EVP_PKEY *PEM_read_PrivateKey(FILE *fp, EVP_PKEY **x, pem_password_cb *cb, void *u)
231	{
232        BIO *b;
233        EVP_PKEY *ret;
234
235        if ((b=BIO_new(BIO_s_file())) == NULL)
236		{
237		OPENSSL_PUT_ERROR(PEM, PEM_read_PrivateKey, ERR_R_BUF_LIB);
238                return(0);
239		}
240        BIO_set_fp(b,fp,BIO_NOCLOSE);
241        ret=PEM_read_bio_PrivateKey(b,x,cb,u);
242        BIO_free(b);
243        return(ret);
244	}
245
246int PEM_write_PrivateKey(FILE *fp, EVP_PKEY *x, const EVP_CIPHER *enc,
247                                               unsigned char *kstr, int klen,
248                                               pem_password_cb *cb, void *u)
249	{
250        BIO *b;
251        int ret;
252
253        if ((b=BIO_new_fp(fp, BIO_NOCLOSE)) == NULL)
254		{
255		OPENSSL_PUT_ERROR(PEM, PEM_write_PrivateKey, ERR_R_BUF_LIB);
256                return 0;
257		}
258        ret=PEM_write_bio_PrivateKey(b, x, enc, kstr, klen, cb, u);
259        BIO_free(b);
260        return ret;
261	}
262
263#endif
264
265
266/* Transparently read in PKCS#3 or X9.42 DH parameters */
267
268DH *PEM_read_bio_DHparams(BIO *bp, DH **x, pem_password_cb *cb, void *u)
269	{
270	char *nm=NULL;
271	const unsigned char *p=NULL;
272	unsigned char *data=NULL;
273	long len;
274	DH *ret=NULL;
275
276	if (!PEM_bytes_read_bio(&data, &len, &nm, PEM_STRING_DHPARAMS,
277								bp, cb, u))
278		return NULL;
279	p = data;
280
281        /* TODO(fork): remove? */
282	/*if (!strcmp(nm, PEM_STRING_DHXPARAMS))
283		ret = d2i_DHxparams(x, &p, len);
284	else */
285		ret = d2i_DHparams(x, &p, len);
286
287	if (ret == NULL)
288		OPENSSL_PUT_ERROR(PEM, PEM_read_bio_DHparams, ERR_R_ASN1_LIB);
289	OPENSSL_free(nm);
290	OPENSSL_free(data);
291	return ret;
292	}
293
294#ifndef OPENSSL_NO_FP_API
295DH *PEM_read_DHparams(FILE *fp, DH **x, pem_password_cb *cb, void *u)
296	{
297        BIO *b;
298        DH *ret;
299
300        if ((b=BIO_new(BIO_s_file())) == NULL)
301		{
302		OPENSSL_PUT_ERROR(PEM, PEM_read_DHparams, ERR_R_BUF_LIB);
303                return(0);
304		}
305        BIO_set_fp(b,fp,BIO_NOCLOSE);
306        ret=PEM_read_bio_DHparams(b,x,cb,u);
307        BIO_free(b);
308        return(ret);
309	}
310#endif
311
312