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