xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/pem/pem_info.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson ([email protected]).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young ([email protected])"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson ([email protected])"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 
58 #include <openssl/pem.h>
59 
60 #include <assert.h>
61 #include <stdio.h>
62 #include <string.h>
63 
64 #include <openssl/dsa.h>
65 #include <openssl/err.h>
66 #include <openssl/evp.h>
67 #include <openssl/mem.h>
68 #include <openssl/obj.h>
69 #include <openssl/rsa.h>
70 #include <openssl/x509.h>
71 
72 
X509_PKEY_new(void)73 static X509_PKEY *X509_PKEY_new(void) {
74   return OPENSSL_zalloc(sizeof(X509_PKEY));
75 }
76 
X509_PKEY_free(X509_PKEY * x)77 static void X509_PKEY_free(X509_PKEY *x) {
78   if (x == NULL) {
79     return;
80   }
81 
82   EVP_PKEY_free(x->dec_pkey);
83   OPENSSL_free(x);
84 }
85 
X509_INFO_new(void)86 static X509_INFO *X509_INFO_new(void) {
87   return OPENSSL_zalloc(sizeof(X509_INFO));
88 }
89 
X509_INFO_free(X509_INFO * x)90 void X509_INFO_free(X509_INFO *x) {
91   if (x == NULL) {
92     return;
93   }
94 
95   X509_free(x->x509);
96   X509_CRL_free(x->crl);
97   X509_PKEY_free(x->x_pkey);
98   OPENSSL_free(x->enc_data);
99   OPENSSL_free(x);
100 }
101 
102 
STACK_OF(X509_INFO)103 STACK_OF(X509_INFO) *PEM_X509_INFO_read(FILE *fp, STACK_OF(X509_INFO) *sk,
104                                         pem_password_cb *cb, void *u) {
105   BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
106   if (b == NULL) {
107     OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB);
108     return 0;
109   }
110   STACK_OF(X509_INFO) *ret = PEM_X509_INFO_read_bio(b, sk, cb, u);
111   BIO_free(b);
112   return ret;
113 }
114 
115 enum parse_result_t {
116   parse_ok,
117   parse_error,
118   parse_new_entry,
119 };
120 
parse_x509(X509_INFO * info,const uint8_t * data,size_t len,int key_type)121 static enum parse_result_t parse_x509(X509_INFO *info, const uint8_t *data,
122                                       size_t len, int key_type) {
123   if (info->x509 != NULL) {
124     return parse_new_entry;
125   }
126   info->x509 = d2i_X509(NULL, &data, len);
127   return info->x509 != NULL ? parse_ok : parse_error;
128 }
129 
parse_x509_aux(X509_INFO * info,const uint8_t * data,size_t len,int key_type)130 static enum parse_result_t parse_x509_aux(X509_INFO *info, const uint8_t *data,
131                                           size_t len, int key_type) {
132   if (info->x509 != NULL) {
133     return parse_new_entry;
134   }
135   info->x509 = d2i_X509_AUX(NULL, &data, len);
136   return info->x509 != NULL ? parse_ok : parse_error;
137 }
138 
parse_crl(X509_INFO * info,const uint8_t * data,size_t len,int key_type)139 static enum parse_result_t parse_crl(X509_INFO *info, const uint8_t *data,
140                                      size_t len, int key_type) {
141   if (info->crl != NULL) {
142     return parse_new_entry;
143   }
144   info->crl = d2i_X509_CRL(NULL, &data, len);
145   return info->crl != NULL ? parse_ok : parse_error;
146 }
147 
parse_key(X509_INFO * info,const uint8_t * data,size_t len,int key_type)148 static enum parse_result_t parse_key(X509_INFO *info, const uint8_t *data,
149                                      size_t len, int key_type) {
150   if (info->x_pkey != NULL) {
151     return parse_new_entry;
152   }
153   info->x_pkey = X509_PKEY_new();
154   if (info->x_pkey == NULL) {
155     return parse_error;
156   }
157   info->x_pkey->dec_pkey = d2i_PrivateKey(key_type, NULL, &data, len);
158   return info->x_pkey->dec_pkey != NULL ? parse_ok : parse_error;
159 }
160 
STACK_OF(X509_INFO)161 STACK_OF(X509_INFO) *PEM_X509_INFO_read_bio(BIO *bp, STACK_OF(X509_INFO) *sk,
162                                             pem_password_cb *cb, void *u) {
163   X509_INFO *info = NULL;
164   char *name = NULL, *header = NULL;
165   unsigned char *data = NULL;
166   long len;
167   int ok = 0;
168   STACK_OF(X509_INFO) *ret = NULL;
169 
170   if (sk == NULL) {
171     ret = sk_X509_INFO_new_null();
172     if (ret == NULL) {
173       return NULL;
174     }
175   } else {
176     ret = sk;
177   }
178   size_t orig_num = sk_X509_INFO_num(ret);
179 
180   info = X509_INFO_new();
181   if (info == NULL) {
182     goto err;
183   }
184 
185   for (;;) {
186     if (!PEM_read_bio(bp, &name, &header, &data, &len)) {
187       uint32_t error = ERR_peek_last_error();
188       if (ERR_GET_LIB(error) == ERR_LIB_PEM &&
189           ERR_GET_REASON(error) == PEM_R_NO_START_LINE) {
190         ERR_clear_error();
191         break;
192       }
193       goto err;
194     }
195 
196     enum parse_result_t (*parse_function)(X509_INFO *, const uint8_t *, size_t,
197                                           int) = NULL;
198     int key_type = EVP_PKEY_NONE;
199     if (strcmp(name, PEM_STRING_X509) == 0 ||
200         strcmp(name, PEM_STRING_X509_OLD) == 0) {
201       parse_function = parse_x509;
202     } else if (strcmp(name, PEM_STRING_X509_TRUSTED) == 0) {
203       parse_function = parse_x509_aux;
204     } else if (strcmp(name, PEM_STRING_X509_CRL) == 0) {
205       parse_function = parse_crl;
206     } else if (strcmp(name, PEM_STRING_RSA) == 0) {
207       parse_function = parse_key;
208       key_type = EVP_PKEY_RSA;
209     } else if (strcmp(name, PEM_STRING_DSA) == 0) {
210       parse_function = parse_key;
211       key_type = EVP_PKEY_DSA;
212     } else if (strcmp(name, PEM_STRING_ECPRIVATEKEY) == 0) {
213       parse_function = parse_key;
214       key_type = EVP_PKEY_EC;
215     }
216 
217     // If a private key has a header, assume it is encrypted.
218     if (key_type != EVP_PKEY_NONE && strlen(header) > 10) {
219       if (info->x_pkey != NULL) {
220         if (!sk_X509_INFO_push(ret, info)) {
221           goto err;
222         }
223         info = X509_INFO_new();
224         if (info == NULL) {
225           goto err;
226         }
227       }
228       // Historically, raw entries pushed an empty key.
229       info->x_pkey = X509_PKEY_new();
230       if (info->x_pkey == NULL ||
231           !PEM_get_EVP_CIPHER_INFO(header, &info->enc_cipher)) {
232         goto err;
233       }
234       info->enc_data = (char *)data;
235       info->enc_len = (int)len;
236       data = NULL;
237     } else if (parse_function != NULL) {
238       EVP_CIPHER_INFO cipher;
239       if (!PEM_get_EVP_CIPHER_INFO(header, &cipher) ||
240           !PEM_do_header(&cipher, data, &len, cb, u)) {
241         goto err;
242       }
243       enum parse_result_t result = parse_function(info, data, len, key_type);
244       if (result == parse_new_entry) {
245         if (!sk_X509_INFO_push(ret, info)) {
246           goto err;
247         }
248         info = X509_INFO_new();
249         if (info == NULL) {
250           goto err;
251         }
252         result = parse_function(info, data, len, key_type);
253       }
254       if (result != parse_ok) {
255         OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB);
256         goto err;
257       }
258     }
259     OPENSSL_free(name);
260     OPENSSL_free(header);
261     OPENSSL_free(data);
262     name = NULL;
263     header = NULL;
264     data = NULL;
265   }
266 
267   // Push the last entry on the stack if not empty.
268   if (info->x509 != NULL || info->crl != NULL || info->x_pkey != NULL ||
269       info->enc_data != NULL) {
270     if (!sk_X509_INFO_push(ret, info)) {
271       goto err;
272     }
273     info = NULL;
274   }
275 
276   ok = 1;
277 
278 err:
279   X509_INFO_free(info);
280   if (!ok) {
281     while (sk_X509_INFO_num(ret) > orig_num) {
282       X509_INFO_free(sk_X509_INFO_pop(ret));
283     }
284     if (ret != sk) {
285       sk_X509_INFO_free(ret);
286     }
287     ret = NULL;
288   }
289 
290   OPENSSL_free(name);
291   OPENSSL_free(header);
292   OPENSSL_free(data);
293   return ret;
294 }
295