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