/src/boringssl/crypto/pem/pem_lib.cc
Line | Count | Source |
1 | | // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <assert.h> |
16 | | #include <limits.h> |
17 | | #include <stdio.h> |
18 | | #include <string.h> |
19 | | |
20 | | #include <string_view> |
21 | | |
22 | | #include <openssl/asn1.h> |
23 | | #include <openssl/base.h> |
24 | | #include <openssl/base64.h> |
25 | | #include <openssl/bio.h> |
26 | | #include <openssl/buf.h> |
27 | | #include <openssl/cipher.h> |
28 | | #include <openssl/des.h> |
29 | | #include <openssl/err.h> |
30 | | #include <openssl/evp.h> |
31 | | #include <openssl/mem.h> |
32 | | #include <openssl/obj.h> |
33 | | #include <openssl/pem.h> |
34 | | #include <openssl/rand.h> |
35 | | #include <openssl/x509.h> |
36 | | |
37 | | #include "../internal.h" |
38 | | #include "internal.h" |
39 | | |
40 | | |
41 | | #define MIN_LENGTH 4 |
42 | | |
43 | | using namespace bssl; |
44 | | |
45 | | static int load_iv(const char **fromp, unsigned char *to, size_t num); |
46 | | static int check_pem(const std::string_view nm, const std::string_view name); |
47 | | |
48 | | // PEM_proc_type appends a Proc-Type header to |buf|, determined by |type|. |
49 | 0 | static void PEM_proc_type(char buf[PEM_BUFSIZE], int type) { |
50 | 0 | const char *str; |
51 | |
|
52 | 0 | if (type == PEM_TYPE_ENCRYPTED) { |
53 | 0 | str = "ENCRYPTED"; |
54 | 0 | } else if (type == PEM_TYPE_MIC_CLEAR) { |
55 | 0 | str = "MIC-CLEAR"; |
56 | 0 | } else if (type == PEM_TYPE_MIC_ONLY) { |
57 | 0 | str = "MIC-ONLY"; |
58 | 0 | } else { |
59 | 0 | str = "BAD-TYPE"; |
60 | 0 | } |
61 | |
|
62 | 0 | OPENSSL_strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE); |
63 | 0 | OPENSSL_strlcat(buf, str, PEM_BUFSIZE); |
64 | 0 | OPENSSL_strlcat(buf, "\n", PEM_BUFSIZE); |
65 | 0 | } |
66 | | |
67 | | // PEM_dek_info appends a DEK-Info header to |buf|, with an algorithm of |type| |
68 | | // and a single parameter, specified by hex-encoding |len| bytes from |str|. |
69 | | static void PEM_dek_info(char buf[PEM_BUFSIZE], const char *type, size_t len, |
70 | 0 | char *str) { |
71 | 0 | static const unsigned char map[17] = "0123456789ABCDEF"; |
72 | |
|
73 | 0 | OPENSSL_strlcat(buf, "DEK-Info: ", PEM_BUFSIZE); |
74 | 0 | OPENSSL_strlcat(buf, type, PEM_BUFSIZE); |
75 | 0 | OPENSSL_strlcat(buf, ",", PEM_BUFSIZE); |
76 | |
|
77 | 0 | const size_t used = strlen(buf); |
78 | 0 | const size_t available = PEM_BUFSIZE - used; |
79 | 0 | if (len * 2 < len || len * 2 + 2 < len || available < len * 2 + 2) { |
80 | 0 | return; |
81 | 0 | } |
82 | | |
83 | 0 | for (size_t i = 0; i < len; i++) { |
84 | 0 | buf[used + i * 2] = map[(str[i] >> 4) & 0x0f]; |
85 | 0 | buf[used + i * 2 + 1] = map[(str[i]) & 0x0f]; |
86 | 0 | } |
87 | 0 | buf[used + len * 2] = '\n'; |
88 | 0 | buf[used + len * 2 + 1] = '\0'; |
89 | 0 | } |
90 | | |
91 | | void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, |
92 | 0 | pem_password_cb *cb, void *u) { |
93 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
94 | 0 | if (b == nullptr) { |
95 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); |
96 | 0 | return nullptr; |
97 | 0 | } |
98 | 0 | void *ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); |
99 | 0 | BIO_free(b); |
100 | 0 | return ret; |
101 | 0 | } |
102 | | |
103 | 0 | static int check_pem(const std::string_view nm, const std::string_view name) { |
104 | | // Normal matching nm and name |
105 | 0 | if (nm == name) { |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | // Make PEM_STRING_EVP_PKEY match any private key |
110 | | |
111 | 0 | if (name == PEM_STRING_EVP_PKEY) { |
112 | 0 | return nm == PEM_STRING_PKCS8 || nm == PEM_STRING_PKCS8INF || |
113 | 0 | nm == PEM_STRING_RSA || nm == PEM_STRING_EC || nm == PEM_STRING_DSA; |
114 | 0 | } |
115 | | |
116 | | // Permit older strings |
117 | | |
118 | 0 | if (nm == PEM_STRING_X509_OLD && name == PEM_STRING_X509) { |
119 | 0 | return 1; |
120 | 0 | } |
121 | | |
122 | 0 | if (nm == PEM_STRING_X509_REQ_OLD && name == PEM_STRING_X509_REQ) { |
123 | 0 | return 1; |
124 | 0 | } |
125 | | |
126 | | // Allow normal certs to be read as trusted certs |
127 | 0 | if (nm == PEM_STRING_X509 && name == PEM_STRING_X509_TRUSTED) { |
128 | 0 | return 1; |
129 | 0 | } |
130 | | |
131 | 0 | if (nm == PEM_STRING_X509_OLD && name == PEM_STRING_X509_TRUSTED) { |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | | // Some CAs use PKCS#7 with CERTIFICATE headers |
136 | 0 | if (nm == PEM_STRING_X509 && name == PEM_STRING_PKCS7) { |
137 | 0 | return 1; |
138 | 0 | } |
139 | | |
140 | 0 | if (nm == PEM_STRING_PKCS7_SIGNED && name == PEM_STRING_PKCS7) { |
141 | 0 | return 1; |
142 | 0 | } |
143 | | |
144 | | #ifndef OPENSSL_NO_CMS |
145 | | if (nm == PEM_STRING_X509 && name == PEM_STRING_CMS) { |
146 | | return 1; |
147 | | } |
148 | | // Allow CMS to be read from PKCS#7 headers |
149 | | if (nm == PEM_STRING_PKCS7 && name == PEM_STRING_CMS) { |
150 | | return 1; |
151 | | } |
152 | | #endif |
153 | | |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | 0 | static const EVP_CIPHER *cipher_by_name(const std::string_view name) { |
158 | | // This is similar to the (deprecated) function |EVP_get_cipherbyname|. Note |
159 | | // the PEM code assumes that ciphers have at least 8 bytes of IV, at most 20 |
160 | | // bytes of overhead and generally behave like CBC mode. |
161 | 0 | if (name == SN_des_cbc) { |
162 | 0 | return EVP_des_cbc(); |
163 | 0 | } else if (name == SN_des_ede3_cbc) { |
164 | 0 | return EVP_des_ede3_cbc(); |
165 | 0 | } else if (name == SN_aes_128_cbc) { |
166 | 0 | return EVP_aes_128_cbc(); |
167 | 0 | } else if (name == SN_aes_192_cbc) { |
168 | 0 | return EVP_aes_192_cbc(); |
169 | 0 | } else if (name == SN_aes_256_cbc) { |
170 | 0 | return EVP_aes_256_cbc(); |
171 | 0 | } else { |
172 | 0 | return nullptr; |
173 | 0 | } |
174 | 0 | } |
175 | | |
176 | | int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, |
177 | | const char *name, BIO *bp, pem_password_cb *cb, |
178 | 0 | void *u) { |
179 | 0 | EVP_CIPHER_INFO cipher; |
180 | 0 | UniquePtr<char> nm; |
181 | 0 | UniquePtr<char> header; |
182 | 0 | Array<uint8_t> data; |
183 | 0 | size_t ulen; |
184 | 0 | size_t unused = 0; |
185 | |
|
186 | 0 | for (;;) { |
187 | 0 | if (!PEM_read_bio_inner(bp, &nm, &header, &data)) { |
188 | 0 | if (ERR_equals(ERR_peek_error(), ERR_LIB_PEM, PEM_R_NO_START_LINE)) { |
189 | 0 | ERR_add_error_data(2, "Expecting: ", name); |
190 | 0 | } |
191 | 0 | return 0; |
192 | 0 | } |
193 | 0 | if (data.size() > LONG_MAX) { |
194 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_OVERFLOW); |
195 | 0 | return 0; |
196 | 0 | } |
197 | 0 | if (check_pem(nm.get(), name)) { |
198 | 0 | break; |
199 | 0 | } |
200 | 0 | } |
201 | 0 | if (!PEM_get_EVP_CIPHER_INFO(header.get(), &cipher)) { |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | ulen = data.size(); |
205 | 0 | if (!PEM_do_header(&cipher, data.data(), &ulen, cb, u)) { |
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | | // Release the buffer to the caller. |
210 | | // Note that |PEM_do_header| may have reduced the length after decrypting |
211 | | // in-place. |
212 | | // This will not overflow because |data.size()| was checked to fit in |long| |
213 | | // above. |
214 | 0 | data.Release(pdata, &unused); |
215 | 0 | *plen = static_cast<long>(ulen); |
216 | |
|
217 | 0 | if (pnm) { |
218 | 0 | *pnm = nm.release(); |
219 | 0 | } |
220 | |
|
221 | 0 | return 1; |
222 | 0 | } |
223 | | |
224 | | int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, |
225 | | const EVP_CIPHER *enc, const unsigned char *pass, |
226 | 0 | int pass_len, pem_password_cb *callback, void *u) { |
227 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
228 | 0 | if (b == nullptr) { |
229 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); |
230 | 0 | return 0; |
231 | 0 | } |
232 | 0 | int ret = |
233 | 0 | PEM_ASN1_write_bio(i2d, name, b, x, enc, pass, pass_len, callback, u); |
234 | 0 | BIO_free(b); |
235 | 0 | return ret; |
236 | 0 | } |
237 | | |
238 | | int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, |
239 | | const EVP_CIPHER *enc, const unsigned char *pass, |
240 | 0 | int pass_len, pem_password_cb *callback, void *u) { |
241 | 0 | ScopedEVP_CIPHER_CTX ctx; |
242 | 0 | int dsize = 0, ret = 0; |
243 | 0 | size_t i, j, data_size; |
244 | 0 | unsigned char *p, *data = nullptr; |
245 | 0 | const char *objstr = nullptr; |
246 | 0 | char buf[PEM_BUFSIZE]; |
247 | 0 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
248 | 0 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
249 | |
|
250 | 0 | if (enc != nullptr) { |
251 | 0 | objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); |
252 | 0 | if (objstr == nullptr || cipher_by_name(objstr) == nullptr || |
253 | 0 | EVP_CIPHER_iv_length(enc) < 8) { |
254 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_CIPHER); |
255 | 0 | goto err; |
256 | 0 | } |
257 | 0 | } |
258 | | |
259 | 0 | if ((dsize = i2d(x, nullptr)) < 0) { |
260 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_ASN1_LIB); |
261 | 0 | dsize = 0; |
262 | 0 | goto err; |
263 | 0 | } |
264 | | // dzise + 8 bytes are needed |
265 | | // actually it needs the cipher block size extra... |
266 | 0 | data_size = static_cast<size_t>(dsize) + 20; |
267 | 0 | data = (unsigned char *)OPENSSL_malloc(data_size); |
268 | 0 | if (data == nullptr) { |
269 | 0 | goto err; |
270 | 0 | } |
271 | 0 | p = data; |
272 | 0 | i = i2d(x, &p); |
273 | |
|
274 | 0 | if (enc != nullptr) { |
275 | 0 | const unsigned iv_len = EVP_CIPHER_iv_length(enc); |
276 | |
|
277 | 0 | if (pass == nullptr) { |
278 | 0 | if (!callback) { |
279 | 0 | callback = PEM_def_callback; |
280 | 0 | } |
281 | 0 | pass_len = (*callback)(buf, PEM_BUFSIZE, 1, u); |
282 | 0 | if (pass_len < 0) { |
283 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_READ_KEY); |
284 | 0 | goto err; |
285 | 0 | } |
286 | 0 | pass = (const unsigned char *)buf; |
287 | 0 | } |
288 | 0 | assert(iv_len <= sizeof(iv)); |
289 | 0 | if (!RAND_bytes(iv, iv_len)) { // Generate a salt |
290 | 0 | goto err; |
291 | 0 | } |
292 | | // The 'iv' is used as the iv and as a salt. It is NOT taken from |
293 | | // the BytesToKey function |
294 | 0 | if (!EVP_BytesToKey(enc, EVP_md5(), iv, pass, pass_len, 1, key, nullptr)) { |
295 | 0 | goto err; |
296 | 0 | } |
297 | | |
298 | 0 | if (pass == (const unsigned char *)buf) { |
299 | 0 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
300 | 0 | } |
301 | |
|
302 | 0 | assert(strlen(objstr) + 23 + 2 * iv_len + 13 <= sizeof(buf)); |
303 | | |
304 | 0 | buf[0] = '\0'; |
305 | 0 | PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); |
306 | 0 | PEM_dek_info(buf, objstr, iv_len, (char *)iv); |
307 | | // k=strlen(buf); |
308 | |
|
309 | 0 | ret = 1; |
310 | 0 | if (!EVP_EncryptInit_ex(ctx.get(), enc, nullptr, key, iv) || |
311 | 0 | !EVP_EncryptUpdate_ex(ctx.get(), data, &j, data_size, data, i) || |
312 | 0 | !EVP_EncryptFinal_ex2(ctx.get(), &(data[j]), &i, data_size - j)) { |
313 | 0 | ret = 0; |
314 | 0 | } else { |
315 | 0 | i += j; |
316 | 0 | } |
317 | 0 | if (ret == 0) { |
318 | 0 | goto err; |
319 | 0 | } |
320 | 0 | } else { |
321 | 0 | ret = 1; |
322 | 0 | buf[0] = '\0'; |
323 | 0 | } |
324 | 0 | i = PEM_write_bio(bp, name, buf, data, i); |
325 | 0 | if (i <= 0) { |
326 | 0 | ret = 0; |
327 | 0 | } |
328 | 0 | err: |
329 | 0 | OPENSSL_cleanse(key, sizeof(key)); |
330 | 0 | OPENSSL_cleanse(iv, sizeof(iv)); |
331 | 0 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
332 | 0 | OPENSSL_free(data); |
333 | 0 | return ret; |
334 | 0 | } |
335 | | |
336 | | int bssl::PEM_do_header(const EVP_CIPHER_INFO *cipher, unsigned char *data, |
337 | 0 | size_t *len, pem_password_cb *callback, void *u) { |
338 | 0 | int pass_len; |
339 | 0 | ScopedEVP_CIPHER_CTX ctx; |
340 | 0 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
341 | 0 | char buf[PEM_BUFSIZE]; |
342 | 0 | const size_t in_len = *len; |
343 | |
|
344 | 0 | if (cipher->cipher == nullptr) { |
345 | 0 | return 1; |
346 | 0 | } |
347 | | |
348 | 0 | pass_len = 0; |
349 | 0 | if (!callback) { |
350 | 0 | callback = PEM_def_callback; |
351 | 0 | } |
352 | 0 | pass_len = callback(buf, PEM_BUFSIZE, 0, u); |
353 | 0 | if (pass_len < 0) { |
354 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_PASSWORD_READ); |
355 | 0 | return 0; |
356 | 0 | } |
357 | | |
358 | 0 | if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), cipher->iv, |
359 | 0 | (unsigned char *)buf, pass_len, 1, key, nullptr)) { |
360 | 0 | return 0; |
361 | 0 | } |
362 | | |
363 | | // Safety: we have checked |*len| before narrowing so that |EVP_DecryptUpdate| |
364 | | // can safely work with it. |
365 | 0 | size_t out_len1 = 0; |
366 | 0 | size_t out_len2 = 0; |
367 | 0 | if (!EVP_DecryptInit_ex(ctx.get(), cipher->cipher, nullptr, key, |
368 | 0 | cipher->iv) || |
369 | 0 | !EVP_DecryptUpdate_ex(ctx.get(), data, &out_len1, in_len, data, in_len) || |
370 | 0 | !EVP_DecryptFinal_ex2(ctx.get(), data + out_len1, &out_len2, |
371 | 0 | in_len - out_len1)) { |
372 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_DECRYPT); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 0 | *len = out_len1 + out_len2; |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | 0 | int bssl::PEM_get_EVP_CIPHER_INFO(const char *header, EVP_CIPHER_INFO *cipher) { |
380 | 0 | cipher->cipher = nullptr; |
381 | 0 | OPENSSL_memset(cipher->iv, 0, sizeof(cipher->iv)); |
382 | 0 | if ((header == nullptr) || (*header == '\0') || (*header == '\n')) { |
383 | 0 | return 1; |
384 | 0 | } |
385 | 0 | if (strncmp(header, "Proc-Type: ", 11) != 0) { |
386 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_PROC_TYPE); |
387 | 0 | return 0; |
388 | 0 | } |
389 | 0 | header += 11; |
390 | 0 | if (header[0] != '4' || header[1] != ',') { |
391 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_PROC_TYPE_VERSION); |
392 | 0 | return 0; |
393 | 0 | } |
394 | 0 | header += 2; |
395 | 0 | if (strncmp(header, "ENCRYPTED", 9) != 0) { |
396 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_ENCRYPTED); |
397 | 0 | return 0; |
398 | 0 | } |
399 | 0 | for (; (*header != '\n') && (*header != '\0'); header++) { |
400 | 0 | ; |
401 | 0 | } |
402 | 0 | if (*header == '\0') { |
403 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_SHORT_HEADER); |
404 | 0 | return 0; |
405 | 0 | } |
406 | 0 | header++; |
407 | 0 | if (strncmp(header, "DEK-Info: ", 10) != 0) { |
408 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_NOT_DEK_INFO); |
409 | 0 | return 0; |
410 | 0 | } |
411 | 0 | header += 10; |
412 | |
|
413 | 0 | const char *p = header; |
414 | 0 | for (;;) { |
415 | 0 | char c = *header; |
416 | 0 | if (!((c >= 'A' && c <= 'Z') || c == '-' || OPENSSL_isdigit(c))) { |
417 | 0 | break; |
418 | 0 | } |
419 | 0 | header++; |
420 | 0 | } |
421 | 0 | cipher->cipher = cipher_by_name(std::string_view(p, header - p)); |
422 | 0 | header++; |
423 | 0 | if (cipher->cipher == nullptr) { |
424 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_ENCRYPTION); |
425 | 0 | return 0; |
426 | 0 | } |
427 | | // The IV parameter must be at least 8 bytes long to be used as the salt in |
428 | | // the KDF. (This should not happen given |cipher_by_name|.) |
429 | 0 | if (EVP_CIPHER_iv_length(cipher->cipher) < 8) { |
430 | 0 | assert(0); |
431 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_UNSUPPORTED_ENCRYPTION); |
432 | 0 | return 0; |
433 | 0 | } |
434 | 0 | const char **header_pp = &header; |
435 | 0 | if (!load_iv(header_pp, cipher->iv, EVP_CIPHER_iv_length(cipher->cipher))) { |
436 | 0 | return 0; |
437 | 0 | } |
438 | | |
439 | 0 | return 1; |
440 | 0 | } |
441 | | |
442 | 0 | static int load_iv(const char **fromp, unsigned char *to, size_t num) { |
443 | 0 | uint8_t v; |
444 | 0 | const char *from; |
445 | |
|
446 | 0 | from = *fromp; |
447 | 0 | for (size_t i = 0; i < num; i++) { |
448 | 0 | to[i] = 0; |
449 | 0 | } |
450 | 0 | num *= 2; |
451 | 0 | for (size_t i = 0; i < num; i++) { |
452 | 0 | if (!OPENSSL_fromxdigit(&v, *from)) { |
453 | 0 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_IV_CHARS); |
454 | 0 | return 0; |
455 | 0 | } |
456 | 0 | from++; |
457 | 0 | to[i / 2] |= v << (!(i & 1)) * 4; |
458 | 0 | } |
459 | | |
460 | 0 | *fromp = from; |
461 | 0 | return 1; |
462 | 0 | } |
463 | | |
464 | | int PEM_write(FILE *fp, const char *name, const char *header, |
465 | 0 | const unsigned char *data, long len) { |
466 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
467 | 0 | if (b == nullptr) { |
468 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); |
469 | 0 | return 0; |
470 | 0 | } |
471 | 0 | int ret = PEM_write_bio(b, name, header, data, len); |
472 | 0 | BIO_free(b); |
473 | 0 | return ret; |
474 | 0 | } |
475 | | |
476 | | int PEM_write_bio(BIO *bp, const char *name, const char *header, |
477 | 0 | const unsigned char *data, long len) { |
478 | 0 | int nlen, n, i, j, outl; |
479 | 0 | unsigned char *buf = nullptr; |
480 | 0 | EVP_ENCODE_CTX ctx; |
481 | 0 | int reason = ERR_R_BUF_LIB; |
482 | 0 | int retval = 0; |
483 | |
|
484 | 0 | EVP_EncodeInit(&ctx); |
485 | 0 | nlen = strlen(name); |
486 | |
|
487 | 0 | if ((BIO_write(bp, "-----BEGIN ", 11) != 11) || |
488 | 0 | (BIO_write(bp, name, nlen) != nlen) || |
489 | 0 | (BIO_write(bp, "-----\n", 6) != 6)) { |
490 | 0 | goto err; |
491 | 0 | } |
492 | | |
493 | 0 | i = strlen(header); |
494 | 0 | if (i > 0) { |
495 | 0 | if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1)) { |
496 | 0 | goto err; |
497 | 0 | } |
498 | 0 | } |
499 | | |
500 | 0 | buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(PEM_BUFSIZE * 8)); |
501 | 0 | if (buf == nullptr) { |
502 | 0 | goto err; |
503 | 0 | } |
504 | | |
505 | 0 | i = j = 0; |
506 | 0 | while (len > 0) { |
507 | 0 | n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len); |
508 | 0 | EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n); |
509 | 0 | if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) { |
510 | 0 | goto err; |
511 | 0 | } |
512 | 0 | i += outl; |
513 | 0 | len -= n; |
514 | 0 | j += n; |
515 | 0 | } |
516 | 0 | EVP_EncodeFinal(&ctx, buf, &outl); |
517 | 0 | if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) { |
518 | 0 | goto err; |
519 | 0 | } |
520 | 0 | if ((BIO_write(bp, "-----END ", 9) != 9) || |
521 | 0 | (BIO_write(bp, name, nlen) != nlen) || |
522 | 0 | (BIO_write(bp, "-----\n", 6) != 6)) { |
523 | 0 | goto err; |
524 | 0 | } |
525 | 0 | retval = i + outl; |
526 | |
|
527 | 0 | err: |
528 | 0 | if (retval == 0) { |
529 | 0 | OPENSSL_PUT_ERROR(PEM, reason); |
530 | 0 | } |
531 | 0 | OPENSSL_free(buf); |
532 | 0 | return retval; |
533 | 0 | } |
534 | | |
535 | | int PEM_read(FILE *fp, char **name, char **header, unsigned char **data, |
536 | 0 | long *len) { |
537 | 0 | BIO *b = BIO_new_fp(fp, BIO_NOCLOSE); |
538 | 0 | if (b == nullptr) { |
539 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_BUF_LIB); |
540 | 0 | return 0; |
541 | 0 | } |
542 | 0 | int ret = PEM_read_bio(b, name, header, data, len); |
543 | 0 | BIO_free(b); |
544 | 0 | return ret; |
545 | 0 | } |
546 | | |
547 | | int bssl::PEM_read_bio_inner(BIO *bp, UniquePtr<char> *name, |
548 | 1.17k | UniquePtr<char> *header, Array<uint8_t> *data) { |
549 | 1.17k | EVP_ENCODE_CTX ctx; |
550 | 1.17k | int end = 0, i, k, bl = 0, hl = 0, nohead = 0; |
551 | 1.17k | char buf[256]; |
552 | 1.17k | BUF_MEM *nameB; |
553 | 1.17k | BUF_MEM *headerB; |
554 | 1.17k | BUF_MEM *dataB, *tmpB; |
555 | | |
556 | 1.17k | nameB = BUF_MEM_new(); |
557 | 1.17k | headerB = BUF_MEM_new(); |
558 | 1.17k | dataB = BUF_MEM_new(); |
559 | 1.17k | if ((nameB == nullptr) || (headerB == nullptr) || (dataB == nullptr)) { |
560 | 0 | goto err; |
561 | 0 | } |
562 | | |
563 | 1.17k | buf[254] = '\0'; |
564 | 100k | for (;;) { |
565 | 100k | i = BIO_gets(bp, buf, 254); |
566 | | |
567 | 100k | if (i <= 0) { |
568 | 250 | OPENSSL_PUT_ERROR(PEM, PEM_R_NO_START_LINE); |
569 | 250 | goto err; |
570 | 250 | } |
571 | | |
572 | 2.17M | while ((i >= 0) && (buf[i] <= ' ')) { |
573 | 2.07M | i--; |
574 | 2.07M | } |
575 | 100k | buf[++i] = '\n'; |
576 | 100k | buf[++i] = '\0'; |
577 | | |
578 | 100k | if (strncmp(buf, "-----BEGIN ", 11) == 0) { |
579 | 1.19k | i = strlen(&(buf[11])); |
580 | | |
581 | 1.19k | if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0) { |
582 | 270 | continue; |
583 | 270 | } |
584 | 929 | if (!BUF_MEM_grow(nameB, i + 9)) { |
585 | 0 | goto err; |
586 | 0 | } |
587 | 929 | OPENSSL_memcpy(nameB->data, &(buf[11]), i - 6); |
588 | 929 | nameB->data[i - 6] = '\0'; |
589 | 929 | break; |
590 | 929 | } |
591 | 100k | } |
592 | 929 | hl = 0; |
593 | 929 | if (!BUF_MEM_grow(headerB, 256)) { |
594 | 0 | goto err; |
595 | 0 | } |
596 | 929 | headerB->data[0] = '\0'; |
597 | 159k | for (;;) { |
598 | 159k | i = BIO_gets(bp, buf, 254); |
599 | 159k | if (i <= 0) { |
600 | 260 | break; |
601 | 260 | } |
602 | | |
603 | 451k | while ((i >= 0) && (buf[i] <= ' ')) { |
604 | 292k | i--; |
605 | 292k | } |
606 | 159k | buf[++i] = '\n'; |
607 | 159k | buf[++i] = '\0'; |
608 | | |
609 | 159k | if (buf[0] == '\n') { |
610 | 278 | break; |
611 | 278 | } |
612 | 159k | if (!BUF_MEM_grow(headerB, hl + i + 9)) { |
613 | 0 | goto err; |
614 | 0 | } |
615 | 159k | if (strncmp(buf, "-----END ", 9) == 0) { |
616 | 391 | nohead = 1; |
617 | 391 | break; |
618 | 391 | } |
619 | 158k | OPENSSL_memcpy(&(headerB->data[hl]), buf, i); |
620 | 158k | headerB->data[hl + i] = '\0'; |
621 | 158k | hl += i; |
622 | 158k | } |
623 | | |
624 | 929 | bl = 0; |
625 | 929 | if (!BUF_MEM_grow(dataB, 1024)) { |
626 | 0 | goto err; |
627 | 0 | } |
628 | 929 | dataB->data[0] = '\0'; |
629 | 929 | if (!nohead) { |
630 | 5.38k | for (;;) { |
631 | 5.38k | i = BIO_gets(bp, buf, 254); |
632 | 5.38k | if (i <= 0) { |
633 | 283 | break; |
634 | 283 | } |
635 | | |
636 | 15.5k | while ((i >= 0) && (buf[i] <= ' ')) { |
637 | 10.4k | i--; |
638 | 10.4k | } |
639 | 5.10k | buf[++i] = '\n'; |
640 | 5.10k | buf[++i] = '\0'; |
641 | | |
642 | 5.10k | if (i != 65) { |
643 | 255 | end = 1; |
644 | 255 | } |
645 | 5.10k | if (strncmp(buf, "-----END ", 9) == 0) { |
646 | 1 | break; |
647 | 1 | } |
648 | 5.10k | if (i > 65) { |
649 | 8 | break; |
650 | 8 | } |
651 | 5.09k | if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) { |
652 | 0 | goto err; |
653 | 0 | } |
654 | 5.09k | OPENSSL_memcpy(&(dataB->data[bl]), buf, i); |
655 | 5.09k | dataB->data[bl + i] = '\0'; |
656 | 5.09k | bl += i; |
657 | 5.09k | if (end) { |
658 | 246 | buf[0] = '\0'; |
659 | 246 | i = BIO_gets(bp, buf, 254); |
660 | 246 | if (i <= 0) { |
661 | 137 | break; |
662 | 137 | } |
663 | | |
664 | 778 | while ((i >= 0) && (buf[i] <= ' ')) { |
665 | 669 | i--; |
666 | 669 | } |
667 | 109 | buf[++i] = '\n'; |
668 | 109 | buf[++i] = '\0'; |
669 | | |
670 | 109 | break; |
671 | 246 | } |
672 | 5.09k | } |
673 | 538 | } else { |
674 | 391 | tmpB = headerB; |
675 | 391 | headerB = dataB; |
676 | 391 | dataB = tmpB; |
677 | 391 | bl = hl; |
678 | 391 | } |
679 | 929 | i = strlen(nameB->data); |
680 | 929 | if ((strncmp(buf, "-----END ", 9) != 0) || |
681 | 393 | (strncmp(nameB->data, &(buf[9]), i) != 0) || |
682 | 741 | (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) { |
683 | 741 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_END_LINE); |
684 | 741 | goto err; |
685 | 741 | } |
686 | | |
687 | 188 | EVP_DecodeInit(&ctx); |
688 | 188 | i = EVP_DecodeUpdate(&ctx, (unsigned char *)dataB->data, &bl, |
689 | 188 | (unsigned char *)dataB->data, bl); |
690 | 188 | if (i < 0) { |
691 | 94 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_BASE64_DECODE); |
692 | 94 | goto err; |
693 | 94 | } |
694 | 94 | i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k); |
695 | 94 | if (i < 0) { |
696 | 41 | OPENSSL_PUT_ERROR(PEM, PEM_R_BAD_BASE64_DECODE); |
697 | 41 | goto err; |
698 | 41 | } |
699 | 53 | bl += k; |
700 | | |
701 | 53 | if (bl == 0) { |
702 | 1 | goto err; |
703 | 1 | } |
704 | | // Transfer ownership of buffers |
705 | 52 | name->reset(nameB->data); |
706 | 52 | header->reset(headerB->data); |
707 | 52 | data->Reset((uint8_t *)dataB->data, bl); |
708 | 52 | OPENSSL_free(nameB); |
709 | 52 | OPENSSL_free(headerB); |
710 | 52 | OPENSSL_free(dataB); |
711 | 52 | return 1; |
712 | 1.12k | err: |
713 | 1.12k | BUF_MEM_free(nameB); |
714 | 1.12k | BUF_MEM_free(headerB); |
715 | 1.12k | BUF_MEM_free(dataB); |
716 | 1.12k | return 0; |
717 | 53 | } |
718 | | |
719 | | int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, |
720 | 1.17k | long *len) { |
721 | 1.17k | UniquePtr<char> owned_name; |
722 | 1.17k | UniquePtr<char> owned_header; |
723 | 1.17k | Array<uint8_t> owned_data; |
724 | 1.17k | if (!PEM_read_bio_inner(bp, &owned_name, &owned_header, &owned_data)) { |
725 | 1.12k | return 0; |
726 | 1.12k | } |
727 | 52 | if (owned_data.size() > LONG_MAX) { |
728 | 0 | OPENSSL_PUT_ERROR(PEM, ERR_R_OVERFLOW); |
729 | 0 | return 0; |
730 | 0 | } |
731 | 52 | size_t ulen = 0; |
732 | 52 | *name = owned_name.release(); |
733 | 52 | *header = owned_header.release(); |
734 | 52 | owned_data.Release(data, &ulen); |
735 | | // Safety: we checked that |ulen| <= |LONG_MAX|. |
736 | 52 | *len = static_cast<long>(ulen); |
737 | 52 | return 1; |
738 | 52 | } |
739 | | |
740 | 0 | int PEM_def_callback(char *buf, int size, int rwflag, void *userdata) { |
741 | 0 | if (!buf || !userdata || size < 0) { |
742 | 0 | return -1; |
743 | 0 | } |
744 | 0 | size_t len = strlen((char *)userdata); |
745 | 0 | if (len >= (size_t)size) { |
746 | 0 | return -1; |
747 | 0 | } |
748 | 0 | OPENSSL_strlcpy(buf, reinterpret_cast<char *>(userdata), (size_t)size); |
749 | 0 | return (int)len; |
750 | 0 | } |