diff options
Diffstat (limited to 'src/lib/libcrypto/pem/pem_lib.c')
-rw-r--r-- | src/lib/libcrypto/pem/pem_lib.c | 871 |
1 files changed, 0 insertions, 871 deletions
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c deleted file mode 100644 index 48768a4467..0000000000 --- a/src/lib/libcrypto/pem/pem_lib.c +++ /dev/null | |||
@@ -1,871 +0,0 @@ | |||
1 | /* $OpenBSD: pem_lib.c,v 1.39 2015/02/11 04:05:14 beck Exp $ */ | ||
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 <ctype.h> | ||
60 | #include <stdio.h> | ||
61 | #include <stdlib.h> | ||
62 | #include <string.h> | ||
63 | |||
64 | #include <openssl/opensslconf.h> | ||
65 | |||
66 | #include <openssl/buffer.h> | ||
67 | #include <openssl/err.h> | ||
68 | #include <openssl/evp.h> | ||
69 | #include <openssl/objects.h> | ||
70 | #include <openssl/pem.h> | ||
71 | #include <openssl/pkcs12.h> | ||
72 | #include <openssl/x509.h> | ||
73 | |||
74 | #ifndef OPENSSL_NO_DES | ||
75 | #include <openssl/des.h> | ||
76 | #endif | ||
77 | #ifndef OPENSSL_NO_ENGINE | ||
78 | #include <openssl/engine.h> | ||
79 | #endif | ||
80 | |||
81 | #include "asn1_locl.h" | ||
82 | |||
83 | #define MIN_LENGTH 4 | ||
84 | |||
85 | static int load_iv(char **fromp, unsigned char *to, int num); | ||
86 | static int check_pem(const char *nm, const char *name); | ||
87 | int pem_check_suffix(const char *pem_str, const char *suffix); | ||
88 | |||
89 | /* XXX LSSL ABI XXX return value and `num' ought to be size_t */ | ||
90 | int | ||
91 | PEM_def_callback(char *buf, int num, int w, void *key) | ||
92 | { | ||
93 | size_t l; | ||
94 | int i; | ||
95 | const char *prompt; | ||
96 | |||
97 | if (key) { | ||
98 | l = strlen(key); | ||
99 | if (num < 0) | ||
100 | return -1; | ||
101 | if (l > (size_t)num) | ||
102 | l = (size_t)num; | ||
103 | memcpy(buf, key, l); | ||
104 | return (int)l; | ||
105 | } | ||
106 | |||
107 | prompt = EVP_get_pw_prompt(); | ||
108 | if (prompt == NULL) | ||
109 | prompt = "Enter PEM pass phrase:"; | ||
110 | |||
111 | for (;;) { | ||
112 | i = EVP_read_pw_string_min(buf, MIN_LENGTH, num, prompt, w); | ||
113 | if (i != 0) { | ||
114 | PEMerr(PEM_F_PEM_DEF_CALLBACK, | ||
115 | PEM_R_PROBLEMS_GETTING_PASSWORD); | ||
116 | memset(buf, 0, num); | ||
117 | return (-1); | ||
118 | } | ||
119 | l = strlen(buf); | ||
120 | if (l < MIN_LENGTH) { | ||
121 | fprintf(stderr, "phrase is too short, " | ||
122 | "needs to be at least %zu chars\n", | ||
123 | (size_t)MIN_LENGTH); | ||
124 | } else | ||
125 | break; | ||
126 | } | ||
127 | return (int)l; | ||
128 | } | ||
129 | |||
130 | void | ||
131 | PEM_proc_type(char *buf, int type) | ||
132 | { | ||
133 | const char *str; | ||
134 | |||
135 | if (type == PEM_TYPE_ENCRYPTED) | ||
136 | str = "ENCRYPTED"; | ||
137 | else if (type == PEM_TYPE_MIC_CLEAR) | ||
138 | str = "MIC-CLEAR"; | ||
139 | else if (type == PEM_TYPE_MIC_ONLY) | ||
140 | str = "MIC-ONLY"; | ||
141 | else | ||
142 | str = "BAD-TYPE"; | ||
143 | |||
144 | strlcat(buf, "Proc-Type: 4,", PEM_BUFSIZE); | ||
145 | strlcat(buf, str, PEM_BUFSIZE); | ||
146 | strlcat(buf, "\n", PEM_BUFSIZE); | ||
147 | } | ||
148 | |||
149 | void | ||
150 | PEM_dek_info(char *buf, const char *type, int len, char *str) | ||
151 | { | ||
152 | static const unsigned char map[17] = "0123456789ABCDEF"; | ||
153 | long i; | ||
154 | int j; | ||
155 | |||
156 | strlcat(buf, "DEK-Info: ", PEM_BUFSIZE); | ||
157 | strlcat(buf, type, PEM_BUFSIZE); | ||
158 | strlcat(buf, ",", PEM_BUFSIZE); | ||
159 | j = strlen(buf); | ||
160 | if (j + (len * 2) + 1 > PEM_BUFSIZE) | ||
161 | return; | ||
162 | for (i = 0; i < len; i++) { | ||
163 | buf[j + i * 2] = map[(str[i] >> 4) & 0x0f]; | ||
164 | buf[j + i * 2 + 1] = map[(str[i]) & 0x0f]; | ||
165 | } | ||
166 | buf[j + i * 2] = '\n'; | ||
167 | buf[j + i * 2 + 1] = '\0'; | ||
168 | } | ||
169 | |||
170 | void * | ||
171 | PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, | ||
172 | pem_password_cb *cb, void *u) | ||
173 | { | ||
174 | BIO *b; | ||
175 | void *ret; | ||
176 | |||
177 | if ((b = BIO_new(BIO_s_file())) == NULL) { | ||
178 | PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB); | ||
179 | return (0); | ||
180 | } | ||
181 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
182 | ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); | ||
183 | BIO_free(b); | ||
184 | return (ret); | ||
185 | } | ||
186 | |||
187 | static int | ||
188 | check_pem(const char *nm, const char *name) | ||
189 | { | ||
190 | /* Normal matching nm and name */ | ||
191 | if (!strcmp(nm, name)) | ||
192 | return 1; | ||
193 | |||
194 | /* Make PEM_STRING_EVP_PKEY match any private key */ | ||
195 | |||
196 | if (!strcmp(name, PEM_STRING_EVP_PKEY)) { | ||
197 | int slen; | ||
198 | const EVP_PKEY_ASN1_METHOD *ameth; | ||
199 | if (!strcmp(nm, PEM_STRING_PKCS8)) | ||
200 | return 1; | ||
201 | if (!strcmp(nm, PEM_STRING_PKCS8INF)) | ||
202 | return 1; | ||
203 | slen = pem_check_suffix(nm, "PRIVATE KEY"); | ||
204 | if (slen > 0) { | ||
205 | /* NB: ENGINE implementations wont contain | ||
206 | * a deprecated old private key decode function | ||
207 | * so don't look for them. | ||
208 | */ | ||
209 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); | ||
210 | if (ameth && ameth->old_priv_decode) | ||
211 | return 1; | ||
212 | } | ||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | if (!strcmp(name, PEM_STRING_PARAMETERS)) { | ||
217 | int slen; | ||
218 | const EVP_PKEY_ASN1_METHOD *ameth; | ||
219 | slen = pem_check_suffix(nm, "PARAMETERS"); | ||
220 | if (slen > 0) { | ||
221 | ENGINE *e; | ||
222 | ameth = EVP_PKEY_asn1_find_str(&e, nm, slen); | ||
223 | if (ameth) { | ||
224 | int r; | ||
225 | if (ameth->param_decode) | ||
226 | r = 1; | ||
227 | else | ||
228 | r = 0; | ||
229 | #ifndef OPENSSL_NO_ENGINE | ||
230 | if (e) | ||
231 | ENGINE_finish(e); | ||
232 | #endif | ||
233 | return r; | ||
234 | } | ||
235 | } | ||
236 | return 0; | ||
237 | } | ||
238 | |||
239 | /* Permit older strings */ | ||
240 | |||
241 | if (!strcmp(nm, PEM_STRING_X509_OLD) && | ||
242 | !strcmp(name, PEM_STRING_X509)) | ||
243 | return 1; | ||
244 | |||
245 | if (!strcmp(nm, PEM_STRING_X509_REQ_OLD) && | ||
246 | !strcmp(name, PEM_STRING_X509_REQ)) | ||
247 | return 1; | ||
248 | |||
249 | /* Allow normal certs to be read as trusted certs */ | ||
250 | if (!strcmp(nm, PEM_STRING_X509) && | ||
251 | !strcmp(name, PEM_STRING_X509_TRUSTED)) | ||
252 | return 1; | ||
253 | |||
254 | if (!strcmp(nm, PEM_STRING_X509_OLD) && | ||
255 | !strcmp(name, PEM_STRING_X509_TRUSTED)) | ||
256 | return 1; | ||
257 | |||
258 | /* Some CAs use PKCS#7 with CERTIFICATE headers */ | ||
259 | if (!strcmp(nm, PEM_STRING_X509) && | ||
260 | !strcmp(name, PEM_STRING_PKCS7)) | ||
261 | return 1; | ||
262 | |||
263 | if (!strcmp(nm, PEM_STRING_PKCS7_SIGNED) && | ||
264 | !strcmp(name, PEM_STRING_PKCS7)) | ||
265 | return 1; | ||
266 | |||
267 | #ifndef OPENSSL_NO_CMS | ||
268 | if (!strcmp(nm, PEM_STRING_X509) && | ||
269 | !strcmp(name, PEM_STRING_CMS)) | ||
270 | return 1; | ||
271 | /* Allow CMS to be read from PKCS#7 headers */ | ||
272 | if (!strcmp(nm, PEM_STRING_PKCS7) && | ||
273 | !strcmp(name, PEM_STRING_CMS)) | ||
274 | return 1; | ||
275 | #endif | ||
276 | |||
277 | return 0; | ||
278 | } | ||
279 | |||
280 | int | ||
281 | PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, | ||
282 | const char *name, BIO *bp, pem_password_cb *cb, void *u) | ||
283 | { | ||
284 | EVP_CIPHER_INFO cipher; | ||
285 | char *nm = NULL, *header = NULL; | ||
286 | unsigned char *data = NULL; | ||
287 | long len; | ||
288 | int ret = 0; | ||
289 | |||
290 | for (;;) { | ||
291 | if (!PEM_read_bio(bp, &nm, &header, &data, &len)) { | ||
292 | if (ERR_GET_REASON(ERR_peek_error()) == | ||
293 | PEM_R_NO_START_LINE) | ||
294 | ERR_asprintf_error_data("Expecting: %s", name); | ||
295 | return 0; | ||
296 | } | ||
297 | if (check_pem(nm, name)) | ||
298 | break; | ||
299 | free(nm); | ||
300 | free(header); | ||
301 | free(data); | ||
302 | } | ||
303 | if (!PEM_get_EVP_CIPHER_INFO(header, &cipher)) | ||
304 | goto err; | ||
305 | if (!PEM_do_header(&cipher, data, &len, cb, u)) | ||
306 | goto err; | ||
307 | |||
308 | *pdata = data; | ||
309 | *plen = len; | ||
310 | |||
311 | if (pnm) | ||
312 | *pnm = nm; | ||
313 | |||
314 | ret = 1; | ||
315 | |||
316 | err: | ||
317 | if (!ret || !pnm) | ||
318 | free(nm); | ||
319 | free(header); | ||
320 | if (!ret) | ||
321 | free(data); | ||
322 | return ret; | ||
323 | } | ||
324 | |||
325 | int | ||
326 | PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, void *x, | ||
327 | const EVP_CIPHER *enc, unsigned char *kstr, int klen, | ||
328 | pem_password_cb *callback, void *u) | ||
329 | { | ||
330 | BIO *b; | ||
331 | int ret; | ||
332 | |||
333 | if ((b = BIO_new(BIO_s_file())) == NULL) { | ||
334 | PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB); | ||
335 | return (0); | ||
336 | } | ||
337 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
338 | ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u); | ||
339 | BIO_free(b); | ||
340 | return (ret); | ||
341 | } | ||
342 | |||
343 | int | ||
344 | PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, void *x, | ||
345 | const EVP_CIPHER *enc, unsigned char *kstr, int klen, | ||
346 | pem_password_cb *callback, void *u) | ||
347 | { | ||
348 | EVP_CIPHER_CTX ctx; | ||
349 | int dsize = 0, i, j, ret = 0; | ||
350 | unsigned char *p, *data = NULL; | ||
351 | const char *objstr = NULL; | ||
352 | char buf[PEM_BUFSIZE]; | ||
353 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
354 | unsigned char iv[EVP_MAX_IV_LENGTH]; | ||
355 | |||
356 | if (enc != NULL) { | ||
357 | objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); | ||
358 | if (objstr == NULL) { | ||
359 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, | ||
360 | PEM_R_UNSUPPORTED_CIPHER); | ||
361 | goto err; | ||
362 | } | ||
363 | } | ||
364 | |||
365 | if ((dsize = i2d(x, NULL)) < 0) { | ||
366 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB); | ||
367 | dsize = 0; | ||
368 | goto err; | ||
369 | } | ||
370 | /* dzise + 8 bytes are needed */ | ||
371 | /* actually it needs the cipher block size extra... */ | ||
372 | data = malloc((unsigned int)dsize + 20); | ||
373 | if (data == NULL) { | ||
374 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE); | ||
375 | goto err; | ||
376 | } | ||
377 | p = data; | ||
378 | i = i2d(x, &p); | ||
379 | |||
380 | if (enc != NULL) { | ||
381 | if (kstr == NULL) { | ||
382 | if (callback == NULL) | ||
383 | klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u); | ||
384 | else | ||
385 | klen = (*callback)(buf, PEM_BUFSIZE, 1, u); | ||
386 | if (klen <= 0) { | ||
387 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, | ||
388 | PEM_R_READ_KEY); | ||
389 | goto err; | ||
390 | } | ||
391 | kstr = (unsigned char *)buf; | ||
392 | } | ||
393 | if ((size_t)enc->iv_len > sizeof(iv)) { | ||
394 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, EVP_R_IV_TOO_LARGE); | ||
395 | goto err; | ||
396 | } | ||
397 | arc4random_buf(iv, enc->iv_len); /* Generate a salt */ | ||
398 | /* The 'iv' is used as the iv and as a salt. It is | ||
399 | * NOT taken from the BytesToKey function */ | ||
400 | if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, | ||
401 | key, NULL)) | ||
402 | goto err; | ||
403 | |||
404 | if (kstr == (unsigned char *)buf) | ||
405 | OPENSSL_cleanse(buf, PEM_BUFSIZE); | ||
406 | |||
407 | if (strlen(objstr) + 23 + 2 * enc->iv_len + 13 > sizeof buf) { | ||
408 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, | ||
409 | ASN1_R_BUFFER_TOO_SMALL); | ||
410 | goto err; | ||
411 | } | ||
412 | |||
413 | buf[0] = '\0'; | ||
414 | PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); | ||
415 | PEM_dek_info(buf, objstr, enc->iv_len, (char *)iv); | ||
416 | /* k=strlen(buf); */ | ||
417 | |||
418 | EVP_CIPHER_CTX_init(&ctx); | ||
419 | ret = 1; | ||
420 | if (!EVP_EncryptInit_ex(&ctx, enc, NULL, key, iv) || | ||
421 | !EVP_EncryptUpdate(&ctx, data, &j, data, i) || | ||
422 | !EVP_EncryptFinal_ex(&ctx, &(data[j]), &i)) | ||
423 | ret = 0; | ||
424 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
425 | if (ret == 0) | ||
426 | goto err; | ||
427 | i += j; | ||
428 | } else { | ||
429 | ret = 1; | ||
430 | buf[0] = '\0'; | ||
431 | } | ||
432 | i = PEM_write_bio(bp, name, buf, data, i); | ||
433 | if (i <= 0) | ||
434 | ret = 0; | ||
435 | err: | ||
436 | OPENSSL_cleanse(key, sizeof(key)); | ||
437 | OPENSSL_cleanse(iv, sizeof(iv)); | ||
438 | OPENSSL_cleanse((char *)&ctx, sizeof(ctx)); | ||
439 | OPENSSL_cleanse(buf, PEM_BUFSIZE); | ||
440 | if (data != NULL) { | ||
441 | OPENSSL_cleanse(data, (unsigned int)dsize); | ||
442 | free(data); | ||
443 | } | ||
444 | return (ret); | ||
445 | } | ||
446 | |||
447 | int | ||
448 | PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, | ||
449 | pem_password_cb *callback, void *u) | ||
450 | { | ||
451 | int i, j, o, klen; | ||
452 | long len; | ||
453 | EVP_CIPHER_CTX ctx; | ||
454 | unsigned char key[EVP_MAX_KEY_LENGTH]; | ||
455 | char buf[PEM_BUFSIZE]; | ||
456 | |||
457 | len = *plen; | ||
458 | |||
459 | if (cipher->cipher == NULL) | ||
460 | return (1); | ||
461 | if (callback == NULL) | ||
462 | klen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u); | ||
463 | else | ||
464 | klen = callback(buf, PEM_BUFSIZE, 0, u); | ||
465 | if (klen <= 0) { | ||
466 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ); | ||
467 | return (0); | ||
468 | } | ||
469 | if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]), | ||
470 | (unsigned char *)buf, klen, 1, key, NULL)) | ||
471 | return 0; | ||
472 | |||
473 | j = (int)len; | ||
474 | EVP_CIPHER_CTX_init(&ctx); | ||
475 | o = EVP_DecryptInit_ex(&ctx, cipher->cipher, NULL, key, | ||
476 | &(cipher->iv[0])); | ||
477 | if (o) | ||
478 | o = EVP_DecryptUpdate(&ctx, data, &i, data, j); | ||
479 | if (o) | ||
480 | o = EVP_DecryptFinal_ex(&ctx, &(data[i]), &j); | ||
481 | EVP_CIPHER_CTX_cleanup(&ctx); | ||
482 | OPENSSL_cleanse((char *)buf, sizeof(buf)); | ||
483 | OPENSSL_cleanse((char *)key, sizeof(key)); | ||
484 | if (!o) { | ||
485 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT); | ||
486 | return (0); | ||
487 | } | ||
488 | *plen = j + i; | ||
489 | return (1); | ||
490 | } | ||
491 | |||
492 | int | ||
493 | PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) | ||
494 | { | ||
495 | const EVP_CIPHER *enc = NULL; | ||
496 | char *p, c; | ||
497 | char **header_pp = &header; | ||
498 | |||
499 | cipher->cipher = NULL; | ||
500 | if ((header == NULL) || (*header == '\0') || (*header == '\n')) | ||
501 | return (1); | ||
502 | if (strncmp(header, "Proc-Type: ", 11) != 0) { | ||
503 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE); | ||
504 | return (0); | ||
505 | } | ||
506 | header += 11; | ||
507 | if (*header != '4') | ||
508 | return (0); | ||
509 | header++; | ||
510 | if (*header != ',') | ||
511 | return (0); | ||
512 | header++; | ||
513 | if (strncmp(header, "ENCRYPTED", 9) != 0) { | ||
514 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED); | ||
515 | return (0); | ||
516 | } | ||
517 | for (; (*header != '\n') && (*header != '\0'); header++) | ||
518 | ; | ||
519 | if (*header == '\0') { | ||
520 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER); | ||
521 | return (0); | ||
522 | } | ||
523 | header++; | ||
524 | if (strncmp(header, "DEK-Info: ", 10) != 0) { | ||
525 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO); | ||
526 | return (0); | ||
527 | } | ||
528 | header += 10; | ||
529 | |||
530 | p = header; | ||
531 | for (;;) { | ||
532 | c= *header; | ||
533 | if (!( ((c >= 'A') && (c <= 'Z')) || (c == '-') || | ||
534 | ((c >= '0') && (c <= '9')))) | ||
535 | break; | ||
536 | header++; | ||
537 | } | ||
538 | *header = '\0'; | ||
539 | cipher->cipher = enc = EVP_get_cipherbyname(p); | ||
540 | *header = c; | ||
541 | header++; | ||
542 | |||
543 | if (enc == NULL) { | ||
544 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, | ||
545 | PEM_R_UNSUPPORTED_ENCRYPTION); | ||
546 | return (0); | ||
547 | } | ||
548 | if (!load_iv(header_pp, &(cipher->iv[0]), enc->iv_len)) | ||
549 | return (0); | ||
550 | |||
551 | return (1); | ||
552 | } | ||
553 | |||
554 | static int | ||
555 | load_iv(char **fromp, unsigned char *to, int num) | ||
556 | { | ||
557 | int v, i; | ||
558 | char *from; | ||
559 | |||
560 | from= *fromp; | ||
561 | for (i = 0; i < num; i++) | ||
562 | to[i] = 0; | ||
563 | num *= 2; | ||
564 | for (i = 0; i < num; i++) { | ||
565 | if ((*from >= '0') && (*from <= '9')) | ||
566 | v = *from - '0'; | ||
567 | else if ((*from >= 'A') && (*from <= 'F')) | ||
568 | v = *from - 'A' + 10; | ||
569 | else if ((*from >= 'a') && (*from <= 'f')) | ||
570 | v = *from - 'a' + 10; | ||
571 | else { | ||
572 | PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS); | ||
573 | return (0); | ||
574 | } | ||
575 | from++; | ||
576 | to[i / 2] |= v << (long)((!(i & 1)) * 4); | ||
577 | } | ||
578 | |||
579 | *fromp = from; | ||
580 | return (1); | ||
581 | } | ||
582 | |||
583 | int | ||
584 | PEM_write(FILE *fp, char *name, char *header, unsigned char *data, long len) | ||
585 | { | ||
586 | BIO *b; | ||
587 | int ret; | ||
588 | |||
589 | if ((b = BIO_new(BIO_s_file())) == NULL) { | ||
590 | PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB); | ||
591 | return (0); | ||
592 | } | ||
593 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
594 | ret = PEM_write_bio(b, name, header, data, len); | ||
595 | BIO_free(b); | ||
596 | return (ret); | ||
597 | } | ||
598 | |||
599 | int | ||
600 | PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data, | ||
601 | long len) | ||
602 | { | ||
603 | int nlen, n, i, j, outl; | ||
604 | unsigned char *buf = NULL; | ||
605 | EVP_ENCODE_CTX ctx; | ||
606 | int reason = ERR_R_BUF_LIB; | ||
607 | |||
608 | EVP_EncodeInit(&ctx); | ||
609 | nlen = strlen(name); | ||
610 | |||
611 | if ((BIO_write(bp, "-----BEGIN ", 11) != 11) || | ||
612 | (BIO_write(bp, name, nlen) != nlen) || | ||
613 | (BIO_write(bp, "-----\n", 6) != 6)) | ||
614 | goto err; | ||
615 | |||
616 | i = strlen(header); | ||
617 | if (i > 0) { | ||
618 | if ((BIO_write(bp, header, i) != i) || | ||
619 | (BIO_write(bp, "\n", 1) != 1)) | ||
620 | goto err; | ||
621 | } | ||
622 | |||
623 | buf = reallocarray(NULL, PEM_BUFSIZE, 8); | ||
624 | if (buf == NULL) { | ||
625 | reason = ERR_R_MALLOC_FAILURE; | ||
626 | goto err; | ||
627 | } | ||
628 | |||
629 | i = j = 0; | ||
630 | while (len > 0) { | ||
631 | n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len); | ||
632 | EVP_EncodeUpdate(&ctx, buf, &outl, &(data[j]), n); | ||
633 | if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) | ||
634 | goto err; | ||
635 | i += outl; | ||
636 | len -= n; | ||
637 | j += n; | ||
638 | } | ||
639 | EVP_EncodeFinal(&ctx, buf, &outl); | ||
640 | if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) | ||
641 | goto err; | ||
642 | OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); | ||
643 | free(buf); | ||
644 | buf = NULL; | ||
645 | if ((BIO_write(bp, "-----END ", 9) != 9) || | ||
646 | (BIO_write(bp, name, nlen) != nlen) || | ||
647 | (BIO_write(bp, "-----\n", 6) != 6)) | ||
648 | goto err; | ||
649 | return (i + outl); | ||
650 | |||
651 | err: | ||
652 | if (buf) { | ||
653 | OPENSSL_cleanse(buf, PEM_BUFSIZE * 8); | ||
654 | free(buf); | ||
655 | } | ||
656 | PEMerr(PEM_F_PEM_WRITE_BIO, reason); | ||
657 | return (0); | ||
658 | } | ||
659 | |||
660 | int | ||
661 | PEM_read(FILE *fp, char **name, char **header, unsigned char **data, long *len) | ||
662 | { | ||
663 | BIO *b; | ||
664 | int ret; | ||
665 | |||
666 | if ((b = BIO_new(BIO_s_file())) == NULL) { | ||
667 | PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB); | ||
668 | return (0); | ||
669 | } | ||
670 | BIO_set_fp(b, fp, BIO_NOCLOSE); | ||
671 | ret = PEM_read_bio(b, name, header, data, len); | ||
672 | BIO_free(b); | ||
673 | return (ret); | ||
674 | } | ||
675 | |||
676 | int | ||
677 | PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, | ||
678 | long *len) | ||
679 | { | ||
680 | EVP_ENCODE_CTX ctx; | ||
681 | int end = 0, i, k, bl = 0, hl = 0, nohead = 0; | ||
682 | char buf[256]; | ||
683 | BUF_MEM *nameB; | ||
684 | BUF_MEM *headerB; | ||
685 | BUF_MEM *dataB, *tmpB; | ||
686 | |||
687 | nameB = BUF_MEM_new(); | ||
688 | headerB = BUF_MEM_new(); | ||
689 | dataB = BUF_MEM_new(); | ||
690 | if ((nameB == NULL) || (headerB == NULL) || (dataB == NULL)) { | ||
691 | BUF_MEM_free(nameB); | ||
692 | BUF_MEM_free(headerB); | ||
693 | BUF_MEM_free(dataB); | ||
694 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
695 | return (0); | ||
696 | } | ||
697 | |||
698 | buf[254] = '\0'; | ||
699 | for (;;) { | ||
700 | i = BIO_gets(bp, buf, 254); | ||
701 | |||
702 | if (i <= 0) { | ||
703 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_NO_START_LINE); | ||
704 | goto err; | ||
705 | } | ||
706 | |||
707 | while ((i >= 0) && (buf[i] <= ' ')) | ||
708 | i--; | ||
709 | buf[++i] = '\n'; | ||
710 | buf[++i] = '\0'; | ||
711 | |||
712 | if (strncmp(buf, "-----BEGIN ", 11) == 0) { | ||
713 | i = strlen(&(buf[11])); | ||
714 | |||
715 | if (strncmp(&(buf[11 + i - 6]), "-----\n", 6) != 0) | ||
716 | continue; | ||
717 | if (!BUF_MEM_grow(nameB, i + 9)) { | ||
718 | PEMerr(PEM_F_PEM_READ_BIO, | ||
719 | ERR_R_MALLOC_FAILURE); | ||
720 | goto err; | ||
721 | } | ||
722 | memcpy(nameB->data, &(buf[11]), i - 6); | ||
723 | nameB->data[i - 6] = '\0'; | ||
724 | break; | ||
725 | } | ||
726 | } | ||
727 | hl = 0; | ||
728 | if (!BUF_MEM_grow(headerB, 256)) { | ||
729 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
730 | goto err; | ||
731 | } | ||
732 | headerB->data[0] = '\0'; | ||
733 | for (;;) { | ||
734 | i = BIO_gets(bp, buf, 254); | ||
735 | if (i <= 0) | ||
736 | break; | ||
737 | |||
738 | while ((i >= 0) && (buf[i] <= ' ')) | ||
739 | i--; | ||
740 | buf[++i] = '\n'; | ||
741 | buf[++i] = '\0'; | ||
742 | |||
743 | if (buf[0] == '\n') | ||
744 | break; | ||
745 | if (!BUF_MEM_grow(headerB, hl + i + 9)) { | ||
746 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
747 | goto err; | ||
748 | } | ||
749 | if (strncmp(buf, "-----END ", 9) == 0) { | ||
750 | nohead = 1; | ||
751 | break; | ||
752 | } | ||
753 | memcpy(&(headerB->data[hl]), buf, i); | ||
754 | headerB->data[hl + i] = '\0'; | ||
755 | hl += i; | ||
756 | } | ||
757 | |||
758 | bl = 0; | ||
759 | if (!BUF_MEM_grow(dataB, 1024)) { | ||
760 | PEMerr(PEM_F_PEM_READ_BIO, ERR_R_MALLOC_FAILURE); | ||
761 | goto err; | ||
762 | } | ||
763 | dataB->data[0] = '\0'; | ||
764 | if (!nohead) { | ||
765 | for (;;) { | ||
766 | i = BIO_gets(bp, buf, 254); | ||
767 | if (i <= 0) | ||
768 | break; | ||
769 | |||
770 | while ((i >= 0) && (buf[i] <= ' ')) | ||
771 | i--; | ||
772 | buf[++i] = '\n'; | ||
773 | buf[++i] = '\0'; | ||
774 | |||
775 | if (i != 65) | ||
776 | end = 1; | ||
777 | if (strncmp(buf, "-----END ", 9) == 0) | ||
778 | break; | ||
779 | if (i > 65) | ||
780 | break; | ||
781 | if (!BUF_MEM_grow_clean(dataB, i + bl + 9)) { | ||
782 | PEMerr(PEM_F_PEM_READ_BIO, | ||
783 | ERR_R_MALLOC_FAILURE); | ||
784 | goto err; | ||
785 | } | ||
786 | memcpy(&(dataB->data[bl]), buf, i); | ||
787 | dataB->data[bl + i] = '\0'; | ||
788 | bl += i; | ||
789 | if (end) { | ||
790 | buf[0] = '\0'; | ||
791 | i = BIO_gets(bp, buf, 254); | ||
792 | if (i <= 0) | ||
793 | break; | ||
794 | |||
795 | while ((i >= 0) && (buf[i] <= ' ')) | ||
796 | i--; | ||
797 | buf[++i] = '\n'; | ||
798 | buf[++i] = '\0'; | ||
799 | |||
800 | break; | ||
801 | } | ||
802 | } | ||
803 | } else { | ||
804 | tmpB = headerB; | ||
805 | headerB = dataB; | ||
806 | dataB = tmpB; | ||
807 | bl = hl; | ||
808 | } | ||
809 | i = strlen(nameB->data); | ||
810 | if ((strncmp(buf, "-----END ", 9) != 0) || | ||
811 | (strncmp(nameB->data, &(buf[9]), i) != 0) || | ||
812 | (strncmp(&(buf[9 + i]), "-----\n", 6) != 0)) { | ||
813 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_END_LINE); | ||
814 | goto err; | ||
815 | } | ||
816 | |||
817 | EVP_DecodeInit(&ctx); | ||
818 | i = EVP_DecodeUpdate(&ctx, | ||
819 | (unsigned char *)dataB->data, &bl, | ||
820 | (unsigned char *)dataB->data, bl); | ||
821 | if (i < 0) { | ||
822 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); | ||
823 | goto err; | ||
824 | } | ||
825 | i = EVP_DecodeFinal(&ctx, (unsigned char *)&(dataB->data[bl]), &k); | ||
826 | if (i < 0) { | ||
827 | PEMerr(PEM_F_PEM_READ_BIO, PEM_R_BAD_BASE64_DECODE); | ||
828 | goto err; | ||
829 | } | ||
830 | bl += k; | ||
831 | |||
832 | if (bl == 0) | ||
833 | goto err; | ||
834 | *name = nameB->data; | ||
835 | *header = headerB->data; | ||
836 | *data = (unsigned char *)dataB->data; | ||
837 | *len = bl; | ||
838 | free(nameB); | ||
839 | free(headerB); | ||
840 | free(dataB); | ||
841 | return (1); | ||
842 | |||
843 | err: | ||
844 | BUF_MEM_free(nameB); | ||
845 | BUF_MEM_free(headerB); | ||
846 | BUF_MEM_free(dataB); | ||
847 | return (0); | ||
848 | } | ||
849 | |||
850 | /* Check pem string and return prefix length. | ||
851 | * If for example the pem_str == "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" | ||
852 | * the return value is 3 for the string "RSA". | ||
853 | */ | ||
854 | |||
855 | int | ||
856 | pem_check_suffix(const char *pem_str, const char *suffix) | ||
857 | { | ||
858 | int pem_len = strlen(pem_str); | ||
859 | int suffix_len = strlen(suffix); | ||
860 | const char *p; | ||
861 | |||
862 | if (suffix_len + 1 >= pem_len) | ||
863 | return 0; | ||
864 | p = pem_str + pem_len - suffix_len; | ||
865 | if (strcmp(p, suffix)) | ||
866 | return 0; | ||
867 | p--; | ||
868 | if (*p != ' ') | ||
869 | return 0; | ||
870 | return p - pem_str; | ||
871 | } | ||