1 /* $OpenBSD: pem_info.c,v 1.24 2020/07/25 11:53:37 schwarze 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 #include <openssl/buffer.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/objects.h>
68 #include <openssl/pem.h>
69 #include <openssl/x509.h>
70 
71 #ifndef OPENSSL_NO_DSA
72 #include <openssl/dsa.h>
73 #endif
74 #ifndef OPENSSL_NO_RSA
75 #include <openssl/rsa.h>
76 #endif
77 
78 STACK_OF(X509_INFO) *
79 PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb,
80     void *u)
81 {
82 	BIO *b;
83 	STACK_OF(X509_INFO) *ret;
84 
85 	if ((b = BIO_new(BIO_s_file())) == NULL) {
86 		PEMerror(ERR_R_BUF_LIB);
87 		return (0);
88 	}
89 	BIO_set_fp(b, fp, BIO_NOCLOSE);
90 	ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
91 	BIO_free(b);
92 	return (ret);
93 }
94 
95 STACK_OF(X509_INFO) *
96 PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk, pem_password_cb *cb,
97     void *u)
98 {
99 	X509_INFO *xi = NULL;
100 	char *name = NULL, *header = NULL;
101 	void *pp;
102 	unsigned char *data = NULL;
103 	const unsigned char *p;
104 	long len;
105 	int ok = 0;
106 	int num_in, ptype, raw;
107 	STACK_OF(X509_INFO) *ret = sk;
108 	d2i_of_void *d2i = NULL;
109 
110 	if (ret == NULL) {
111 		if ((ret = sk_X509_INFO_new_null()) == NULL) {
112 			PEMerror(ERR_R_MALLOC_FAILURE);
113 			return NULL;
114 		}
115 	}
116 	num_in = sk_X509_INFO_num(ret);
117 
118 	if ((xi = X509_INFO_new()) == NULL)
119 		goto err;
120 	for (;;) {
121 		raw = 0;
122 		ptype = 0;
123 		if (!PEM_read_bio(bp, &name, &header, &data, &len)) {
124 			if (ERR_GET_REASON(ERR_peek_last_error()) ==
125 			    PEM_R_NO_START_LINE) {
126 				ERR_clear_error();
127 				break;
128 			}
129 			goto err;
130 		}
131 		if ((strcmp(name, PEM_STRING_X509) == 0) ||
132 		    (strcmp(name, PEM_STRING_X509_OLD) == 0)) {
133 			d2i = (D2I_OF(void))d2i_X509;
134 			if (xi->x509 != NULL) {
135 				if (!sk_X509_INFO_push(ret, xi))
136 					goto err;
137 				if ((xi = X509_INFO_new()) == NULL)
138 					goto err;
139 			}
140 			pp = &(xi->x509);
141 		} else if ((strcmp(name, PEM_STRING_X509_TRUSTED) == 0)) {
142 			d2i = (D2I_OF(void))d2i_X509_AUX;
143 			if (xi->x509 != NULL) {
144 				if (!sk_X509_INFO_push(ret, xi))
145 					goto err;
146 				if ((xi = X509_INFO_new()) == NULL)
147 					goto err;
148 			}
149 			pp = &(xi->x509);
150 		} else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
151 			d2i = (D2I_OF(void))d2i_X509_CRL;
152 			if (xi->crl != NULL) {
153 				if (!sk_X509_INFO_push(ret, xi))
154 					goto err;
155 				if ((xi = X509_INFO_new()) == NULL)
156 					goto err;
157 			}
158 			pp = &(xi->crl);
159 		} else
160 #ifndef OPENSSL_NO_RSA
161 		if (strcmp(name, PEM_STRING_RSA) == 0) {
162 			d2i = (D2I_OF(void))d2i_RSAPrivateKey;
163 			if (xi->x_pkey != NULL) {
164 				if (!sk_X509_INFO_push(ret, xi))
165 					goto err;
166 				if ((xi = X509_INFO_new()) == NULL)
167 					goto err;
168 			}
169 			xi->enc_data = NULL;
170 			xi->enc_len = 0;
171 			xi->x_pkey = X509_PKEY_new();
172 			if (xi->x_pkey == NULL)
173 				goto err;
174 			ptype = EVP_PKEY_RSA;
175 			pp = &xi->x_pkey->dec_pkey;
176 			if (strlen(header) > 10) /* assume encrypted */
177 				raw = 1;
178 		} else
179 #endif
180 #ifndef OPENSSL_NO_DSA
181 		if (strcmp(name, PEM_STRING_DSA) == 0) {
182 			d2i = (D2I_OF(void))d2i_DSAPrivateKey;
183 			if (xi->x_pkey != NULL) {
184 				if (!sk_X509_INFO_push(ret, xi))
185 					goto err;
186 				if ((xi = X509_INFO_new()) == NULL)
187 					goto err;
188 			}
189 			xi->enc_data = NULL;
190 			xi->enc_len = 0;
191 			xi->x_pkey = X509_PKEY_new();
192 			if (xi->x_pkey == NULL)
193 				goto err;
194 			ptype = EVP_PKEY_DSA;
195 			pp = &xi->x_pkey->dec_pkey;
196 			if (strlen(header) > 10) /* assume encrypted */
197 				raw = 1;
198 		} else
199 #endif
200 #ifndef OPENSSL_NO_EC
201 		if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
202 			d2i = (D2I_OF(void))d2i_ECPrivateKey;
203 			if (xi->x_pkey != NULL) {
204 				if (!sk_X509_INFO_push(ret, xi))
205 					goto err;
206 				if ((xi = X509_INFO_new()) == NULL)
207 					goto err;
208 			}
209 			xi->enc_data = NULL;
210 			xi->enc_len = 0;
211 			xi->x_pkey = X509_PKEY_new();
212 			if (xi->x_pkey == NULL)
213 				goto err;
214 			ptype = EVP_PKEY_EC;
215 			pp = &xi->x_pkey->dec_pkey;
216 			if (strlen(header) > 10) /* assume encrypted */
217 				raw = 1;
218 		} else
219 #endif
220 		{
221 			d2i = NULL;
222 			pp = NULL;
223 		}
224 
225 		if (d2i != NULL) {
226 			if (!raw) {
227 				EVP_CIPHER_INFO cipher;
228 
229 				if (!PEM_get_EVP_CIPHER_INFO(header, &cipher))
230 					goto err;
231 				if (!PEM_do_header(&cipher, data, &len, cb, u))
232 					goto err;
233 				p = data;
234 				if (ptype) {
235 					if (!d2i_PrivateKey(ptype, pp, &p,
236 					    len)) {
237 						PEMerror(ERR_R_ASN1_LIB);
238 						goto err;
239 					}
240 				} else if (d2i(pp, &p, len) == NULL) {
241 					PEMerror(ERR_R_ASN1_LIB);
242 					goto err;
243 				}
244 			} else { /* encrypted RSA data */
245 				if (!PEM_get_EVP_CIPHER_INFO(header,
246 				    &xi->enc_cipher))
247 					goto err;
248 				xi->enc_data = (char *)data;
249 				xi->enc_len = (int)len;
250 				data = NULL;
251 			}
252 		} else {
253 			/* unknown */
254 		}
255 		free(name);
256 		free(header);
257 		free(data);
258 		name = NULL;
259 		header = NULL;
260 		data = NULL;
261 	}
262 
263 	/* if the last one hasn't been pushed yet and there is anything
264 	 * in it then add it to the stack ...
265 	 */
266 	if ((xi->x509 != NULL) || (xi->crl != NULL) ||
267 	    (xi->x_pkey != NULL) || (xi->enc_data != NULL)) {
268 		if (!sk_X509_INFO_push(ret, xi))
269 			goto err;
270 		xi = NULL;
271 	}
272 	ok = 1;
273 
274 err:
275 	if (!ok) {
276 		while (sk_X509_INFO_num(ret) > num_in)
277 			X509_INFO_free(sk_X509_INFO_pop(ret));
278 		if (ret != sk)
279 			sk_X509_INFO_free(ret);
280 		ret = NULL;
281 	}
282 	X509_INFO_free(xi);
283 	free(name);
284 	free(header);
285 	free(data);
286 
287 	return ret;
288 }
289 
290 
291 /* A TJH addition */
292 int
293 PEM_X509_INFO_write_bio(BIO *bp, X509_INFO *xi, EVP_CIPHER *enc,
294     unsigned char *kstr, int klen, pem_password_cb *cb, void *u)
295 {
296 	EVP_CIPHER_CTX ctx;
297 	int i, ret = 0;
298 	unsigned char *data = NULL;
299 	const char *objstr = NULL;
300 	char buf[PEM_BUFSIZE];
301 	unsigned char *iv = NULL;
302 
303 	if (enc != NULL) {
304 		objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc));
305 		if (objstr == NULL) {
306 			PEMerror(PEM_R_UNSUPPORTED_CIPHER);
307 			goto err;
308 		}
309 	}
310 
311 	/* now for the fun part ... if we have a private key then
312 	 * we have to be able to handle a not-yet-decrypted key
313 	 * being written out correctly ... if it is decrypted or
314 	 * it is non-encrypted then we use the base code
315 	 */
316 	if (xi->x_pkey != NULL) {
317 		if ((xi->enc_data != NULL) && (xi->enc_len > 0) ) {
318 			if (enc == NULL) {
319 				PEMerror(PEM_R_CIPHER_IS_NULL);
320 				goto err;
321 			}
322 
323 			/* copy from weirdo names into more normal things */
324 			iv = xi->enc_cipher.iv;
325 			data = (unsigned char *)xi->enc_data;
326 			i = xi->enc_len;
327 
328 			/* we take the encryption data from the
329 			 * internal stuff rather than what the
330 			 * user has passed us ... as we have to
331 			 * match exactly for some strange reason
332 			 */
333 			objstr = OBJ_nid2sn(
334 			    EVP_CIPHER_nid(xi->enc_cipher.cipher));
335 			if (objstr == NULL) {
336 				PEMerror(PEM_R_UNSUPPORTED_CIPHER);
337 				goto err;
338 			}
339 
340 			/* create the right magic header stuff */
341 			if (strlen(objstr) + 23 + 2 * enc->iv_len + 13 >
342 			    sizeof buf) {
343 				PEMerror(ASN1_R_BUFFER_TOO_SMALL);
344 				goto err;
345 			}
346 			buf[0] = '\0';
347 			PEM_proc_type(buf, PEM_TYPE_ENCRYPTED);
348 			PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv);
349 
350 			/* use the normal code to write things out */
351 			i = PEM_write_bio(bp, PEM_STRING_RSA, buf, data, i);
352 			if (i <= 0)
353 				goto err;
354 		} else {
355 			/* Add DSA/DH */
356 #ifndef OPENSSL_NO_RSA
357 			/* normal optionally encrypted stuff */
358 			if (PEM_write_bio_RSAPrivateKey(bp,
359 			    xi->x_pkey->dec_pkey->pkey.rsa,
360 			    enc, kstr, klen, cb, u) <= 0)
361 				goto err;
362 #endif
363 		}
364 	}
365 
366 	/* if we have a certificate then write it out now */
367 	if ((xi->x509 != NULL) && (PEM_write_bio_X509(bp, xi->x509) <= 0))
368 		goto err;
369 
370 	/* we are ignoring anything else that is loaded into the X509_INFO
371 	 * structure for the moment ... as I don't need it so I'm not
372 	 * coding it here and Eric can do it when this makes it into the
373 	 * base library --tjh
374 	 */
375 
376 	ret = 1;
377 
378 err:
379 	explicit_bzero((char *)&ctx, sizeof(ctx));
380 	explicit_bzero(buf, PEM_BUFSIZE);
381 	return (ret);
382 }
383