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