Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/pem/pem_info.c

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