yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use |
| 5 | * this file except in compliance with the License. You can obtain a copy |
| 6 | * in the file LICENSE in the source distribution or at |
| 7 | * https://www.openssl.org/source/license.html |
| 8 | */ |
| 9 | |
| 10 | #include <stdio.h> |
| 11 | #include "crypto/ctype.h" |
| 12 | #include <string.h> |
| 13 | #include "internal/cryptlib.h" |
| 14 | #include <openssl/buffer.h> |
| 15 | #include <openssl/objects.h> |
| 16 | #include <openssl/evp.h> |
| 17 | #include <openssl/rand.h> |
| 18 | #include <openssl/x509.h> |
| 19 | #include <openssl/pem.h> |
| 20 | #include <openssl/pkcs12.h> |
| 21 | #include "crypto/asn1.h" |
| 22 | #include <openssl/des.h> |
| 23 | #include <openssl/engine.h> |
| 24 | |
| 25 | #define MIN_LENGTH 4 |
| 26 | |
| 27 | static int load_iv(char **fromp, unsigned char *to, int num); |
| 28 | static int check_pem(const char *nm, const char *name); |
| 29 | int pem_check_suffix(const char *pem_str, const char *suffix); |
| 30 | |
| 31 | int PEM_def_callback(char *buf, int num, int rwflag, void *userdata) |
| 32 | { |
| 33 | int i, min_len; |
| 34 | const char *prompt; |
| 35 | |
| 36 | /* We assume that the user passes a default password as userdata */ |
| 37 | if (userdata) { |
| 38 | i = strlen(userdata); |
| 39 | i = (i > num) ? num : i; |
| 40 | memcpy(buf, userdata, i); |
| 41 | return i; |
| 42 | } |
| 43 | |
| 44 | prompt = EVP_get_pw_prompt(); |
| 45 | if (prompt == NULL) |
| 46 | prompt = "Enter PEM pass phrase:"; |
| 47 | |
| 48 | /* |
| 49 | * rwflag == 0 means decryption |
| 50 | * rwflag == 1 means encryption |
| 51 | * |
| 52 | * We assume that for encryption, we want a minimum length, while for |
| 53 | * decryption, we cannot know any minimum length, so we assume zero. |
| 54 | */ |
| 55 | min_len = rwflag ? MIN_LENGTH : 0; |
| 56 | |
| 57 | i = EVP_read_pw_string_min(buf, min_len, num, prompt, rwflag); |
| 58 | if (i != 0) { |
| 59 | PEMerr(PEM_F_PEM_DEF_CALLBACK, PEM_R_PROBLEMS_GETTING_PASSWORD); |
| 60 | memset(buf, 0, (unsigned int)num); |
| 61 | return -1; |
| 62 | } |
| 63 | return strlen(buf); |
| 64 | } |
| 65 | |
| 66 | void PEM_proc_type(char *buf, int type) |
| 67 | { |
| 68 | const char *str; |
| 69 | char *p = buf + strlen(buf); |
| 70 | |
| 71 | if (type == PEM_TYPE_ENCRYPTED) |
| 72 | str = "ENCRYPTED"; |
| 73 | else if (type == PEM_TYPE_MIC_CLEAR) |
| 74 | str = "MIC-CLEAR"; |
| 75 | else if (type == PEM_TYPE_MIC_ONLY) |
| 76 | str = "MIC-ONLY"; |
| 77 | else |
| 78 | str = "BAD-TYPE"; |
| 79 | |
| 80 | BIO_snprintf(p, PEM_BUFSIZE - (size_t)(p - buf), "Proc-Type: 4,%s\n", str); |
| 81 | } |
| 82 | |
| 83 | void PEM_dek_info(char *buf, const char *type, int len, char *str) |
| 84 | { |
| 85 | long i; |
| 86 | char *p = buf + strlen(buf); |
| 87 | int j = PEM_BUFSIZE - (size_t)(p - buf), n; |
| 88 | |
| 89 | n = BIO_snprintf(p, j, "DEK-Info: %s,", type); |
| 90 | if (n > 0) { |
| 91 | j -= n; |
| 92 | p += n; |
| 93 | for (i = 0; i < len; i++) { |
| 94 | n = BIO_snprintf(p, j, "%02X", 0xff & str[i]); |
| 95 | if (n <= 0) |
| 96 | return; |
| 97 | j -= n; |
| 98 | p += n; |
| 99 | } |
| 100 | if (j > 1) |
| 101 | strcpy(p, "\n"); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | #ifndef OPENSSL_NO_STDIO |
| 106 | void *PEM_ASN1_read(d2i_of_void *d2i, const char *name, FILE *fp, void **x, |
| 107 | pem_password_cb *cb, void *u) |
| 108 | { |
| 109 | BIO *b; |
| 110 | void *ret; |
| 111 | |
| 112 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
| 113 | PEMerr(PEM_F_PEM_ASN1_READ, ERR_R_BUF_LIB); |
| 114 | return 0; |
| 115 | } |
| 116 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
| 117 | ret = PEM_ASN1_read_bio(d2i, name, b, x, cb, u); |
| 118 | BIO_free(b); |
| 119 | return ret; |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | static int check_pem(const char *nm, const char *name) |
| 124 | { |
| 125 | /* Normal matching nm and name */ |
| 126 | if (strcmp(nm, name) == 0) |
| 127 | return 1; |
| 128 | |
| 129 | /* Make PEM_STRING_EVP_PKEY match any private key */ |
| 130 | |
| 131 | if (strcmp(name, PEM_STRING_EVP_PKEY) == 0) { |
| 132 | int slen; |
| 133 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 134 | if (strcmp(nm, PEM_STRING_PKCS8) == 0) |
| 135 | return 1; |
| 136 | if (strcmp(nm, PEM_STRING_PKCS8INF) == 0) |
| 137 | return 1; |
| 138 | slen = pem_check_suffix(nm, "PRIVATE KEY"); |
| 139 | if (slen > 0) { |
| 140 | /* |
| 141 | * NB: ENGINE implementations won't contain a deprecated old |
| 142 | * private key decode function so don't look for them. |
| 143 | */ |
| 144 | ameth = EVP_PKEY_asn1_find_str(NULL, nm, slen); |
| 145 | if (ameth && ameth->old_priv_decode) |
| 146 | return 1; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | if (strcmp(name, PEM_STRING_PARAMETERS) == 0) { |
| 152 | int slen; |
| 153 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 154 | slen = pem_check_suffix(nm, "PARAMETERS"); |
| 155 | if (slen > 0) { |
| 156 | ENGINE *e; |
| 157 | ameth = EVP_PKEY_asn1_find_str(&e, nm, slen); |
| 158 | if (ameth) { |
| 159 | int r; |
| 160 | if (ameth->param_decode) |
| 161 | r = 1; |
| 162 | else |
| 163 | r = 0; |
| 164 | #ifndef OPENSSL_NO_ENGINE |
| 165 | ENGINE_finish(e); |
| 166 | #endif |
| 167 | return r; |
| 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | /* If reading DH parameters handle X9.42 DH format too */ |
| 173 | if (strcmp(nm, PEM_STRING_DHXPARAMS) == 0 |
| 174 | && strcmp(name, PEM_STRING_DHPARAMS) == 0) |
| 175 | return 1; |
| 176 | |
| 177 | /* Permit older strings */ |
| 178 | |
| 179 | if (strcmp(nm, PEM_STRING_X509_OLD) == 0 |
| 180 | && strcmp(name, PEM_STRING_X509) == 0) |
| 181 | return 1; |
| 182 | |
| 183 | if (strcmp(nm, PEM_STRING_X509_REQ_OLD) == 0 |
| 184 | && strcmp(name, PEM_STRING_X509_REQ) == 0) |
| 185 | return 1; |
| 186 | |
| 187 | /* Allow normal certs to be read as trusted certs */ |
| 188 | if (strcmp(nm, PEM_STRING_X509) == 0 |
| 189 | && strcmp(name, PEM_STRING_X509_TRUSTED) == 0) |
| 190 | return 1; |
| 191 | |
| 192 | if (strcmp(nm, PEM_STRING_X509_OLD) == 0 |
| 193 | && strcmp(name, PEM_STRING_X509_TRUSTED) == 0) |
| 194 | return 1; |
| 195 | |
| 196 | /* Some CAs use PKCS#7 with CERTIFICATE headers */ |
| 197 | if (strcmp(nm, PEM_STRING_X509) == 0 |
| 198 | && strcmp(name, PEM_STRING_PKCS7) == 0) |
| 199 | return 1; |
| 200 | |
| 201 | if (strcmp(nm, PEM_STRING_PKCS7_SIGNED) == 0 |
| 202 | && strcmp(name, PEM_STRING_PKCS7) == 0) |
| 203 | return 1; |
| 204 | |
| 205 | #ifndef OPENSSL_NO_CMS |
| 206 | if (strcmp(nm, PEM_STRING_X509) == 0 |
| 207 | && strcmp(name, PEM_STRING_CMS) == 0) |
| 208 | return 1; |
| 209 | /* Allow CMS to be read from PKCS#7 headers */ |
| 210 | if (strcmp(nm, PEM_STRING_PKCS7) == 0 |
| 211 | && strcmp(name, PEM_STRING_CMS) == 0) |
| 212 | return 1; |
| 213 | #endif |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static void pem_free(void *p, unsigned int flags, size_t num) |
| 219 | { |
| 220 | if (flags & PEM_FLAG_SECURE) |
| 221 | OPENSSL_secure_clear_free(p, num); |
| 222 | else |
| 223 | OPENSSL_free(p); |
| 224 | } |
| 225 | |
| 226 | static void *pem_malloc(int num, unsigned int flags) |
| 227 | { |
| 228 | return (flags & PEM_FLAG_SECURE) ? OPENSSL_secure_malloc(num) |
| 229 | : OPENSSL_malloc(num); |
| 230 | } |
| 231 | |
| 232 | static int pem_bytes_read_bio_flags(unsigned char **pdata, long *plen, |
| 233 | char **pnm, const char *name, BIO *bp, |
| 234 | pem_password_cb *cb, void *u, |
| 235 | unsigned int flags) |
| 236 | { |
| 237 | EVP_CIPHER_INFO cipher; |
| 238 | char *nm = NULL, *header = NULL; |
| 239 | unsigned char *data = NULL; |
| 240 | long len = 0; |
| 241 | int ret = 0; |
| 242 | |
| 243 | do { |
| 244 | pem_free(nm, flags, 0); |
| 245 | pem_free(header, flags, 0); |
| 246 | pem_free(data, flags, len); |
| 247 | if (!PEM_read_bio_ex(bp, &nm, &header, &data, &len, flags)) { |
| 248 | if (ERR_GET_REASON(ERR_peek_error()) == PEM_R_NO_START_LINE) |
| 249 | ERR_add_error_data(2, "Expecting: ", name); |
| 250 | return 0; |
| 251 | } |
| 252 | } while (!check_pem(nm, name)); |
| 253 | if (!PEM_get_EVP_CIPHER_INFO(header, &cipher)) |
| 254 | goto err; |
| 255 | if (!PEM_do_header(&cipher, data, &len, cb, u)) |
| 256 | goto err; |
| 257 | |
| 258 | *pdata = data; |
| 259 | *plen = len; |
| 260 | |
| 261 | if (pnm != NULL) |
| 262 | *pnm = nm; |
| 263 | |
| 264 | ret = 1; |
| 265 | |
| 266 | err: |
| 267 | if (!ret || pnm == NULL) |
| 268 | pem_free(nm, flags, 0); |
| 269 | pem_free(header, flags, 0); |
| 270 | if (!ret) |
| 271 | pem_free(data, flags, len); |
| 272 | return ret; |
| 273 | } |
| 274 | |
| 275 | int PEM_bytes_read_bio(unsigned char **pdata, long *plen, char **pnm, |
| 276 | const char *name, BIO *bp, pem_password_cb *cb, |
| 277 | void *u) { |
| 278 | return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u, |
| 279 | PEM_FLAG_EAY_COMPATIBLE); |
| 280 | } |
| 281 | |
| 282 | int PEM_bytes_read_bio_secmem(unsigned char **pdata, long *plen, char **pnm, |
| 283 | const char *name, BIO *bp, pem_password_cb *cb, |
| 284 | void *u) { |
| 285 | return pem_bytes_read_bio_flags(pdata, plen, pnm, name, bp, cb, u, |
| 286 | PEM_FLAG_SECURE | PEM_FLAG_EAY_COMPATIBLE); |
| 287 | } |
| 288 | |
| 289 | #ifndef OPENSSL_NO_STDIO |
| 290 | int PEM_ASN1_write(i2d_of_void *i2d, const char *name, FILE *fp, |
| 291 | void *x, const EVP_CIPHER *enc, unsigned char *kstr, |
| 292 | int klen, pem_password_cb *callback, void *u) |
| 293 | { |
| 294 | BIO *b; |
| 295 | int ret; |
| 296 | |
| 297 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
| 298 | PEMerr(PEM_F_PEM_ASN1_WRITE, ERR_R_BUF_LIB); |
| 299 | return 0; |
| 300 | } |
| 301 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
| 302 | ret = PEM_ASN1_write_bio(i2d, name, b, x, enc, kstr, klen, callback, u); |
| 303 | BIO_free(b); |
| 304 | return ret; |
| 305 | } |
| 306 | #endif |
| 307 | |
| 308 | int PEM_ASN1_write_bio(i2d_of_void *i2d, const char *name, BIO *bp, |
| 309 | void *x, const EVP_CIPHER *enc, unsigned char *kstr, |
| 310 | int klen, pem_password_cb *callback, void *u) |
| 311 | { |
| 312 | EVP_CIPHER_CTX *ctx = NULL; |
| 313 | int dsize = 0, i = 0, j = 0, ret = 0; |
| 314 | unsigned char *p, *data = NULL; |
| 315 | const char *objstr = NULL; |
| 316 | char buf[PEM_BUFSIZE]; |
| 317 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
| 318 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
| 319 | |
| 320 | if (enc != NULL) { |
| 321 | objstr = OBJ_nid2sn(EVP_CIPHER_nid(enc)); |
| 322 | if (objstr == NULL || EVP_CIPHER_iv_length(enc) == 0 |
| 323 | || EVP_CIPHER_iv_length(enc) > (int)sizeof(iv) |
| 324 | /* |
| 325 | * Check "Proc-Type: 4,Encrypted\nDEK-Info: objstr,hex-iv\n" |
| 326 | * fits into buf |
| 327 | */ |
| 328 | || (strlen(objstr) + 23 + 2 * EVP_CIPHER_iv_length(enc) + 13) |
| 329 | > sizeof(buf)) { |
| 330 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_UNSUPPORTED_CIPHER); |
| 331 | goto err; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | if ((dsize = i2d(x, NULL)) <= 0) { |
| 336 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_ASN1_LIB); |
| 337 | dsize = 0; |
| 338 | goto err; |
| 339 | } |
| 340 | /* dsize + 8 bytes are needed */ |
| 341 | /* actually it needs the cipher block size extra... */ |
| 342 | data = OPENSSL_malloc((unsigned int)dsize + 20); |
| 343 | if (data == NULL) { |
| 344 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, ERR_R_MALLOC_FAILURE); |
| 345 | goto err; |
| 346 | } |
| 347 | p = data; |
| 348 | i = i2d(x, &p); |
| 349 | |
| 350 | if (enc != NULL) { |
| 351 | if (kstr == NULL) { |
| 352 | if (callback == NULL) |
| 353 | klen = PEM_def_callback(buf, PEM_BUFSIZE, 1, u); |
| 354 | else |
| 355 | klen = (*callback) (buf, PEM_BUFSIZE, 1, u); |
| 356 | if (klen <= 0) { |
| 357 | PEMerr(PEM_F_PEM_ASN1_WRITE_BIO, PEM_R_READ_KEY); |
| 358 | goto err; |
| 359 | } |
| 360 | #ifdef CHARSET_EBCDIC |
| 361 | /* Convert the pass phrase from EBCDIC */ |
| 362 | ebcdic2ascii(buf, buf, klen); |
| 363 | #endif |
| 364 | kstr = (unsigned char *)buf; |
| 365 | } |
| 366 | if (RAND_bytes(iv, EVP_CIPHER_iv_length(enc)) <= 0) /* Generate a salt */ |
| 367 | goto err; |
| 368 | /* |
| 369 | * The 'iv' is used as the iv and as a salt. It is NOT taken from |
| 370 | * the BytesToKey function |
| 371 | */ |
| 372 | if (!EVP_BytesToKey(enc, EVP_md5(), iv, kstr, klen, 1, key, NULL)) |
| 373 | goto err; |
| 374 | |
| 375 | if (kstr == (unsigned char *)buf) |
| 376 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
| 377 | |
| 378 | buf[0] = '\0'; |
| 379 | PEM_proc_type(buf, PEM_TYPE_ENCRYPTED); |
| 380 | PEM_dek_info(buf, objstr, EVP_CIPHER_iv_length(enc), (char *)iv); |
| 381 | /* k=strlen(buf); */ |
| 382 | |
| 383 | ret = 1; |
| 384 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL |
| 385 | || !EVP_EncryptInit_ex(ctx, enc, NULL, key, iv) |
| 386 | || !EVP_EncryptUpdate(ctx, data, &j, data, i) |
| 387 | || !EVP_EncryptFinal_ex(ctx, &(data[j]), &i)) |
| 388 | ret = 0; |
| 389 | if (ret == 0) |
| 390 | goto err; |
| 391 | i += j; |
| 392 | } else { |
| 393 | ret = 1; |
| 394 | buf[0] = '\0'; |
| 395 | } |
| 396 | i = PEM_write_bio(bp, name, buf, data, i); |
| 397 | if (i <= 0) |
| 398 | ret = 0; |
| 399 | err: |
| 400 | OPENSSL_cleanse(key, sizeof(key)); |
| 401 | OPENSSL_cleanse(iv, sizeof(iv)); |
| 402 | EVP_CIPHER_CTX_free(ctx); |
| 403 | OPENSSL_cleanse(buf, PEM_BUFSIZE); |
| 404 | OPENSSL_clear_free(data, (unsigned int)dsize); |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | int PEM_do_header(EVP_CIPHER_INFO *cipher, unsigned char *data, long *plen, |
| 409 | pem_password_cb *callback, void *u) |
| 410 | { |
| 411 | int ok; |
| 412 | int keylen; |
| 413 | long len = *plen; |
| 414 | int ilen = (int) len; /* EVP_DecryptUpdate etc. take int lengths */ |
| 415 | EVP_CIPHER_CTX *ctx; |
| 416 | unsigned char key[EVP_MAX_KEY_LENGTH]; |
| 417 | char buf[PEM_BUFSIZE]; |
| 418 | |
| 419 | #if LONG_MAX > INT_MAX |
| 420 | /* Check that we did not truncate the length */ |
| 421 | if (len > INT_MAX) { |
| 422 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_HEADER_TOO_LONG); |
| 423 | return 0; |
| 424 | } |
| 425 | #endif |
| 426 | |
| 427 | if (cipher->cipher == NULL) |
| 428 | return 1; |
| 429 | if (callback == NULL) |
| 430 | keylen = PEM_def_callback(buf, PEM_BUFSIZE, 0, u); |
| 431 | else |
| 432 | keylen = callback(buf, PEM_BUFSIZE, 0, u); |
| 433 | if (keylen < 0) { |
| 434 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_PASSWORD_READ); |
| 435 | return 0; |
| 436 | } |
| 437 | #ifdef CHARSET_EBCDIC |
| 438 | /* Convert the pass phrase from EBCDIC */ |
| 439 | ebcdic2ascii(buf, buf, keylen); |
| 440 | #endif |
| 441 | |
| 442 | if (!EVP_BytesToKey(cipher->cipher, EVP_md5(), &(cipher->iv[0]), |
| 443 | (unsigned char *)buf, keylen, 1, key, NULL)) |
| 444 | return 0; |
| 445 | |
| 446 | ctx = EVP_CIPHER_CTX_new(); |
| 447 | if (ctx == NULL) |
| 448 | return 0; |
| 449 | |
| 450 | ok = EVP_DecryptInit_ex(ctx, cipher->cipher, NULL, key, &(cipher->iv[0])); |
| 451 | if (ok) |
| 452 | ok = EVP_DecryptUpdate(ctx, data, &ilen, data, ilen); |
| 453 | if (ok) { |
| 454 | /* Squirrel away the length of data decrypted so far. */ |
| 455 | *plen = ilen; |
| 456 | ok = EVP_DecryptFinal_ex(ctx, &(data[ilen]), &ilen); |
| 457 | } |
| 458 | if (ok) |
| 459 | *plen += ilen; |
| 460 | else |
| 461 | PEMerr(PEM_F_PEM_DO_HEADER, PEM_R_BAD_DECRYPT); |
| 462 | |
| 463 | EVP_CIPHER_CTX_free(ctx); |
| 464 | OPENSSL_cleanse((char *)buf, sizeof(buf)); |
| 465 | OPENSSL_cleanse((char *)key, sizeof(key)); |
| 466 | return ok; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * This implements a very limited PEM header parser that does not support the |
| 471 | * full grammar of rfc1421. In particular, folded headers are not supported, |
| 472 | * nor is additional whitespace. |
| 473 | * |
| 474 | * A robust implementation would make use of a library that turns the headers |
| 475 | * into a BIO from which one folded line is read at a time, and is then split |
| 476 | * into a header label and content. We would then parse the content of the |
| 477 | * headers we care about. This is overkill for just this limited use-case, but |
| 478 | * presumably we also parse rfc822-style headers for S/MIME, so a common |
| 479 | * abstraction might well be more generally useful. |
| 480 | */ |
| 481 | int PEM_get_EVP_CIPHER_INFO(char *header, EVP_CIPHER_INFO *cipher) |
| 482 | { |
| 483 | static const char ProcType[] = "Proc-Type:"; |
| 484 | static const char ENCRYPTED[] = "ENCRYPTED"; |
| 485 | static const char DEKInfo[] = "DEK-Info:"; |
| 486 | const EVP_CIPHER *enc = NULL; |
| 487 | int ivlen; |
| 488 | char *dekinfostart, c; |
| 489 | |
| 490 | cipher->cipher = NULL; |
| 491 | memset(cipher->iv, 0, sizeof(cipher->iv)); |
| 492 | if ((header == NULL) || (*header == '\0') || (*header == '\n')) |
| 493 | return 1; |
| 494 | |
| 495 | if (strncmp(header, ProcType, sizeof(ProcType)-1) != 0) { |
| 496 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_PROC_TYPE); |
| 497 | return 0; |
| 498 | } |
| 499 | header += sizeof(ProcType)-1; |
| 500 | header += strspn(header, " \t"); |
| 501 | |
| 502 | if (*header++ != '4' || *header++ != ',') |
| 503 | return 0; |
| 504 | header += strspn(header, " \t"); |
| 505 | |
| 506 | /* We expect "ENCRYPTED" followed by optional white-space + line break */ |
| 507 | if (strncmp(header, ENCRYPTED, sizeof(ENCRYPTED)-1) != 0 || |
| 508 | strspn(header+sizeof(ENCRYPTED)-1, " \t\r\n") == 0) { |
| 509 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_ENCRYPTED); |
| 510 | return 0; |
| 511 | } |
| 512 | header += sizeof(ENCRYPTED)-1; |
| 513 | header += strspn(header, " \t\r"); |
| 514 | if (*header++ != '\n') { |
| 515 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_SHORT_HEADER); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /*- |
| 520 | * https://tools.ietf.org/html/rfc1421#section-4.6.1.3 |
| 521 | * We expect "DEK-Info: algo[,hex-parameters]" |
| 522 | */ |
| 523 | if (strncmp(header, DEKInfo, sizeof(DEKInfo)-1) != 0) { |
| 524 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_NOT_DEK_INFO); |
| 525 | return 0; |
| 526 | } |
| 527 | header += sizeof(DEKInfo)-1; |
| 528 | header += strspn(header, " \t"); |
| 529 | |
| 530 | /* |
| 531 | * DEK-INFO is a comma-separated combination of algorithm name and optional |
| 532 | * parameters. |
| 533 | */ |
| 534 | dekinfostart = header; |
| 535 | header += strcspn(header, " \t,"); |
| 536 | c = *header; |
| 537 | *header = '\0'; |
| 538 | cipher->cipher = enc = EVP_get_cipherbyname(dekinfostart); |
| 539 | *header = c; |
| 540 | header += strspn(header, " \t"); |
| 541 | |
| 542 | if (enc == NULL) { |
| 543 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNSUPPORTED_ENCRYPTION); |
| 544 | return 0; |
| 545 | } |
| 546 | ivlen = EVP_CIPHER_iv_length(enc); |
| 547 | if (ivlen > 0 && *header++ != ',') { |
| 548 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_MISSING_DEK_IV); |
| 549 | return 0; |
| 550 | } else if (ivlen == 0 && *header == ',') { |
| 551 | PEMerr(PEM_F_PEM_GET_EVP_CIPHER_INFO, PEM_R_UNEXPECTED_DEK_IV); |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | if (!load_iv(&header, cipher->iv, EVP_CIPHER_iv_length(enc))) |
| 556 | return 0; |
| 557 | |
| 558 | return 1; |
| 559 | } |
| 560 | |
| 561 | static int load_iv(char **fromp, unsigned char *to, int num) |
| 562 | { |
| 563 | int v, i; |
| 564 | char *from; |
| 565 | |
| 566 | from = *fromp; |
| 567 | for (i = 0; i < num; i++) |
| 568 | to[i] = 0; |
| 569 | num *= 2; |
| 570 | for (i = 0; i < num; i++) { |
| 571 | v = OPENSSL_hexchar2int(*from); |
| 572 | if (v < 0) { |
| 573 | PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS); |
| 574 | return 0; |
| 575 | } |
| 576 | from++; |
| 577 | to[i / 2] |= v << (long)((!(i & 1)) * 4); |
| 578 | } |
| 579 | |
| 580 | *fromp = from; |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | #ifndef OPENSSL_NO_STDIO |
| 585 | int PEM_write(FILE *fp, const char *name, const char *header, |
| 586 | const unsigned char *data, long len) |
| 587 | { |
| 588 | BIO *b; |
| 589 | int ret; |
| 590 | |
| 591 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
| 592 | PEMerr(PEM_F_PEM_WRITE, ERR_R_BUF_LIB); |
| 593 | return 0; |
| 594 | } |
| 595 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
| 596 | ret = PEM_write_bio(b, name, header, data, len); |
| 597 | BIO_free(b); |
| 598 | return ret; |
| 599 | } |
| 600 | #endif |
| 601 | |
| 602 | int PEM_write_bio(BIO *bp, const char *name, const char *header, |
| 603 | const unsigned char *data, long len) |
| 604 | { |
| 605 | int nlen, n, i, j, outl; |
| 606 | unsigned char *buf = NULL; |
| 607 | EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new(); |
| 608 | int reason = ERR_R_BUF_LIB; |
| 609 | int retval = 0; |
| 610 | |
| 611 | if (ctx == NULL) { |
| 612 | reason = ERR_R_MALLOC_FAILURE; |
| 613 | goto err; |
| 614 | } |
| 615 | |
| 616 | EVP_EncodeInit(ctx); |
| 617 | nlen = strlen(name); |
| 618 | |
| 619 | if ((BIO_write(bp, "-----BEGIN ", 11) != 11) || |
| 620 | (BIO_write(bp, name, nlen) != nlen) || |
| 621 | (BIO_write(bp, "-----\n", 6) != 6)) |
| 622 | goto err; |
| 623 | |
| 624 | i = strlen(header); |
| 625 | if (i > 0) { |
| 626 | if ((BIO_write(bp, header, i) != i) || (BIO_write(bp, "\n", 1) != 1)) |
| 627 | goto err; |
| 628 | } |
| 629 | |
| 630 | buf = OPENSSL_malloc(PEM_BUFSIZE * 8); |
| 631 | if (buf == NULL) { |
| 632 | reason = ERR_R_MALLOC_FAILURE; |
| 633 | goto err; |
| 634 | } |
| 635 | |
| 636 | i = j = 0; |
| 637 | while (len > 0) { |
| 638 | n = (int)((len > (PEM_BUFSIZE * 5)) ? (PEM_BUFSIZE * 5) : len); |
| 639 | if (!EVP_EncodeUpdate(ctx, buf, &outl, &(data[j]), n)) |
| 640 | goto err; |
| 641 | if ((outl) && (BIO_write(bp, (char *)buf, outl) != outl)) |
| 642 | goto err; |
| 643 | i += outl; |
| 644 | len -= n; |
| 645 | j += n; |
| 646 | } |
| 647 | EVP_EncodeFinal(ctx, buf, &outl); |
| 648 | if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) |
| 649 | goto err; |
| 650 | if ((BIO_write(bp, "-----END ", 9) != 9) || |
| 651 | (BIO_write(bp, name, nlen) != nlen) || |
| 652 | (BIO_write(bp, "-----\n", 6) != 6)) |
| 653 | goto err; |
| 654 | retval = i + outl; |
| 655 | |
| 656 | err: |
| 657 | if (retval == 0) |
| 658 | PEMerr(PEM_F_PEM_WRITE_BIO, reason); |
| 659 | EVP_ENCODE_CTX_free(ctx); |
| 660 | OPENSSL_clear_free(buf, PEM_BUFSIZE * 8); |
| 661 | return retval; |
| 662 | } |
| 663 | |
| 664 | #ifndef OPENSSL_NO_STDIO |
| 665 | int PEM_read(FILE *fp, char **name, char **header, unsigned char **data, |
| 666 | long *len) |
| 667 | { |
| 668 | BIO *b; |
| 669 | int ret; |
| 670 | |
| 671 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
| 672 | PEMerr(PEM_F_PEM_READ, ERR_R_BUF_LIB); |
| 673 | return 0; |
| 674 | } |
| 675 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
| 676 | ret = PEM_read_bio(b, name, header, data, len); |
| 677 | BIO_free(b); |
| 678 | return ret; |
| 679 | } |
| 680 | #endif |
| 681 | |
| 682 | /* Some helpers for PEM_read_bio_ex(). */ |
| 683 | static int sanitize_line(char *linebuf, int len, unsigned int flags) |
| 684 | { |
| 685 | int i; |
| 686 | |
| 687 | if (flags & PEM_FLAG_EAY_COMPATIBLE) { |
| 688 | /* Strip trailing whitespace */ |
| 689 | while ((len >= 0) && (linebuf[len] <= ' ')) |
| 690 | len--; |
| 691 | /* Go back to whitespace before applying uniform line ending. */ |
| 692 | len++; |
| 693 | } else if (flags & PEM_FLAG_ONLY_B64) { |
| 694 | for (i = 0; i < len; ++i) { |
| 695 | if (!ossl_isbase64(linebuf[i]) || linebuf[i] == '\n' |
| 696 | || linebuf[i] == '\r') |
| 697 | break; |
| 698 | } |
| 699 | len = i; |
| 700 | } else { |
| 701 | /* EVP_DecodeBlock strips leading and trailing whitespace, so just strip |
| 702 | * control characters in-place and let everything through. */ |
| 703 | for (i = 0; i < len; ++i) { |
| 704 | if (linebuf[i] == '\n' || linebuf[i] == '\r') |
| 705 | break; |
| 706 | if (ossl_iscntrl(linebuf[i])) |
| 707 | linebuf[i] = ' '; |
| 708 | } |
| 709 | len = i; |
| 710 | } |
| 711 | /* The caller allocated LINESIZE+1, so this is safe. */ |
| 712 | linebuf[len++] = '\n'; |
| 713 | linebuf[len] = '\0'; |
| 714 | return len; |
| 715 | } |
| 716 | |
| 717 | #define LINESIZE 255 |
| 718 | /* Note trailing spaces for begin and end. */ |
| 719 | static const char beginstr[] = "-----BEGIN "; |
| 720 | static const char endstr[] = "-----END "; |
| 721 | static const char tailstr[] = "-----\n"; |
| 722 | #define BEGINLEN ((int)(sizeof(beginstr) - 1)) |
| 723 | #define ENDLEN ((int)(sizeof(endstr) - 1)) |
| 724 | #define TAILLEN ((int)(sizeof(tailstr) - 1)) |
| 725 | static int get_name(BIO *bp, char **name, unsigned int flags) |
| 726 | { |
| 727 | char *linebuf; |
| 728 | int ret = 0; |
| 729 | int len; |
| 730 | |
| 731 | /* |
| 732 | * Need to hold trailing NUL (accounted for by BIO_gets() and the newline |
| 733 | * that will be added by sanitize_line() (the extra '1'). |
| 734 | */ |
| 735 | linebuf = pem_malloc(LINESIZE + 1, flags); |
| 736 | if (linebuf == NULL) { |
| 737 | PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE); |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | do { |
| 742 | len = BIO_gets(bp, linebuf, LINESIZE); |
| 743 | |
| 744 | if (len <= 0) { |
| 745 | PEMerr(PEM_F_GET_NAME, PEM_R_NO_START_LINE); |
| 746 | goto err; |
| 747 | } |
| 748 | |
| 749 | /* Strip trailing garbage and standardize ending. */ |
| 750 | len = sanitize_line(linebuf, len, flags & ~PEM_FLAG_ONLY_B64); |
| 751 | |
| 752 | /* Allow leading empty or non-matching lines. */ |
| 753 | } while (strncmp(linebuf, beginstr, BEGINLEN) != 0 |
| 754 | || len < TAILLEN |
| 755 | || strncmp(linebuf + len - TAILLEN, tailstr, TAILLEN) != 0); |
| 756 | linebuf[len - TAILLEN] = '\0'; |
| 757 | len = len - BEGINLEN - TAILLEN + 1; |
| 758 | *name = pem_malloc(len, flags); |
| 759 | if (*name == NULL) { |
| 760 | PEMerr(PEM_F_GET_NAME, ERR_R_MALLOC_FAILURE); |
| 761 | goto err; |
| 762 | } |
| 763 | memcpy(*name, linebuf + BEGINLEN, len); |
| 764 | ret = 1; |
| 765 | |
| 766 | err: |
| 767 | pem_free(linebuf, flags, LINESIZE + 1); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | /* Keep track of how much of a header we've seen. */ |
| 772 | enum header_status { |
| 773 | MAYBE_HEADER, |
| 774 | IN_HEADER, |
| 775 | POST_HEADER |
| 776 | }; |
| 777 | |
| 778 | /** |
| 779 | * Extract the optional PEM header, with details on the type of content and |
| 780 | * any encryption used on the contents, and the bulk of the data from the bio. |
| 781 | * The end of the header is marked by a blank line; if the end-of-input marker |
| 782 | * is reached prior to a blank line, there is no header. |
| 783 | * |
| 784 | * The header and data arguments are BIO** since we may have to swap them |
| 785 | * if there is no header, for efficiency. |
| 786 | * |
| 787 | * We need the name of the PEM-encoded type to verify the end string. |
| 788 | */ |
| 789 | static int get_header_and_data(BIO *bp, BIO **header, BIO **data, char *name, |
| 790 | unsigned int flags) |
| 791 | { |
| 792 | BIO *tmp = *header; |
| 793 | char *linebuf, *p; |
| 794 | int len, line, ret = 0, end = 0, prev_partial_line_read = 0, partial_line_read = 0; |
| 795 | /* 0 if not seen (yet), 1 if reading header, 2 if finished header */ |
| 796 | enum header_status got_header = MAYBE_HEADER; |
| 797 | unsigned int flags_mask; |
| 798 | size_t namelen; |
| 799 | |
| 800 | /* Need to hold trailing NUL (accounted for by BIO_gets() and the newline |
| 801 | * that will be added by sanitize_line() (the extra '1'). */ |
| 802 | linebuf = pem_malloc(LINESIZE + 1, flags); |
| 803 | if (linebuf == NULL) { |
| 804 | PEMerr(PEM_F_GET_HEADER_AND_DATA, ERR_R_MALLOC_FAILURE); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | for (line = 0; ; line++) { |
| 809 | flags_mask = ~0u; |
| 810 | len = BIO_gets(bp, linebuf, LINESIZE); |
| 811 | if (len <= 0) { |
| 812 | PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE); |
| 813 | goto err; |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * Check if line has been read completely or if only part of the line |
| 818 | * has been read. Keep the previous value to ignore newlines that |
| 819 | * appear due to reading a line up until the char before the newline. |
| 820 | */ |
| 821 | prev_partial_line_read = partial_line_read; |
| 822 | partial_line_read = len == LINESIZE-1 && linebuf[LINESIZE-2] != '\n'; |
| 823 | |
| 824 | if (got_header == MAYBE_HEADER) { |
| 825 | if (memchr(linebuf, ':', len) != NULL) |
| 826 | got_header = IN_HEADER; |
| 827 | } |
| 828 | if (!strncmp(linebuf, endstr, ENDLEN) || got_header == IN_HEADER) |
| 829 | flags_mask &= ~PEM_FLAG_ONLY_B64; |
| 830 | len = sanitize_line(linebuf, len, flags & flags_mask); |
| 831 | |
| 832 | /* Check for end of header. */ |
| 833 | if (linebuf[0] == '\n') { |
| 834 | /* |
| 835 | * If previous line has been read only partially this newline is a |
| 836 | * regular newline at the end of a line and not an empty line. |
| 837 | */ |
| 838 | if (!prev_partial_line_read) { |
| 839 | if (got_header == POST_HEADER) { |
| 840 | /* Another blank line is an error. */ |
| 841 | PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE); |
| 842 | goto err; |
| 843 | } |
| 844 | got_header = POST_HEADER; |
| 845 | tmp = *data; |
| 846 | } |
| 847 | continue; |
| 848 | } |
| 849 | |
| 850 | /* Check for end of stream (which means there is no header). */ |
| 851 | if (strncmp(linebuf, endstr, ENDLEN) == 0) { |
| 852 | p = linebuf + ENDLEN; |
| 853 | namelen = strlen(name); |
| 854 | if (strncmp(p, name, namelen) != 0 || |
| 855 | strncmp(p + namelen, tailstr, TAILLEN) != 0) { |
| 856 | PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE); |
| 857 | goto err; |
| 858 | } |
| 859 | if (got_header == MAYBE_HEADER) { |
| 860 | *header = *data; |
| 861 | *data = tmp; |
| 862 | } |
| 863 | break; |
| 864 | } else if (end) { |
| 865 | /* Malformed input; short line not at end of data. */ |
| 866 | PEMerr(PEM_F_GET_HEADER_AND_DATA, PEM_R_BAD_END_LINE); |
| 867 | goto err; |
| 868 | } |
| 869 | /* |
| 870 | * Else, a line of text -- could be header or data; we don't |
| 871 | * know yet. Just pass it through. |
| 872 | */ |
| 873 | if (BIO_puts(tmp, linebuf) < 0) |
| 874 | goto err; |
| 875 | /* |
| 876 | * Only encrypted files need the line length check applied. |
| 877 | */ |
| 878 | if (got_header == POST_HEADER) { |
| 879 | /* 65 includes the trailing newline */ |
| 880 | if (len > 65) |
| 881 | goto err; |
| 882 | if (len < 65) |
| 883 | end = 1; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | ret = 1; |
| 888 | err: |
| 889 | pem_free(linebuf, flags, LINESIZE + 1); |
| 890 | return ret; |
| 891 | } |
| 892 | |
| 893 | /** |
| 894 | * Read in PEM-formatted data from the given BIO. |
| 895 | * |
| 896 | * By nature of the PEM format, all content must be printable ASCII (except |
| 897 | * for line endings). Other characters are malformed input and will be rejected. |
| 898 | */ |
| 899 | int PEM_read_bio_ex(BIO *bp, char **name_out, char **header, |
| 900 | unsigned char **data, long *len_out, unsigned int flags) |
| 901 | { |
| 902 | EVP_ENCODE_CTX *ctx = NULL; |
| 903 | const BIO_METHOD *bmeth; |
| 904 | BIO *headerB = NULL, *dataB = NULL; |
| 905 | char *name = NULL; |
| 906 | int len, taillen, headerlen, ret = 0; |
| 907 | BUF_MEM * buf_mem; |
| 908 | |
| 909 | *len_out = 0; |
| 910 | *name_out = *header = NULL; |
| 911 | *data = NULL; |
| 912 | if ((flags & PEM_FLAG_EAY_COMPATIBLE) && (flags & PEM_FLAG_ONLY_B64)) { |
| 913 | /* These two are mutually incompatible; bail out. */ |
| 914 | PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_PASSED_INVALID_ARGUMENT); |
| 915 | goto end; |
| 916 | } |
| 917 | bmeth = (flags & PEM_FLAG_SECURE) ? BIO_s_secmem() : BIO_s_mem(); |
| 918 | |
| 919 | headerB = BIO_new(bmeth); |
| 920 | dataB = BIO_new(bmeth); |
| 921 | if (headerB == NULL || dataB == NULL) { |
| 922 | PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE); |
| 923 | goto end; |
| 924 | } |
| 925 | |
| 926 | if (!get_name(bp, &name, flags)) |
| 927 | goto end; |
| 928 | if (!get_header_and_data(bp, &headerB, &dataB, name, flags)) |
| 929 | goto end; |
| 930 | |
| 931 | BIO_get_mem_ptr(dataB, &buf_mem); |
| 932 | len = buf_mem->length; |
| 933 | |
| 934 | /* There was no data in the PEM file */ |
| 935 | if (len == 0) |
| 936 | goto end; |
| 937 | |
| 938 | ctx = EVP_ENCODE_CTX_new(); |
| 939 | if (ctx == NULL) { |
| 940 | PEMerr(PEM_F_PEM_READ_BIO_EX, ERR_R_MALLOC_FAILURE); |
| 941 | goto end; |
| 942 | } |
| 943 | |
| 944 | EVP_DecodeInit(ctx); |
| 945 | if (EVP_DecodeUpdate(ctx, (unsigned char*)buf_mem->data, &len, |
| 946 | (unsigned char*)buf_mem->data, len) < 0 |
| 947 | || EVP_DecodeFinal(ctx, (unsigned char*)&(buf_mem->data[len]), |
| 948 | &taillen) < 0) { |
| 949 | PEMerr(PEM_F_PEM_READ_BIO_EX, PEM_R_BAD_BASE64_DECODE); |
| 950 | goto end; |
| 951 | } |
| 952 | len += taillen; |
| 953 | buf_mem->length = len; |
| 954 | |
| 955 | headerlen = BIO_get_mem_data(headerB, NULL); |
| 956 | *header = pem_malloc(headerlen + 1, flags); |
| 957 | *data = pem_malloc(len, flags); |
| 958 | if (*header == NULL || *data == NULL) { |
| 959 | pem_free(*header, flags, 0); |
| 960 | pem_free(*data, flags, 0); |
| 961 | goto end; |
| 962 | } |
| 963 | BIO_read(headerB, *header, headerlen); |
| 964 | (*header)[headerlen] = '\0'; |
| 965 | BIO_read(dataB, *data, len); |
| 966 | *len_out = len; |
| 967 | *name_out = name; |
| 968 | name = NULL; |
| 969 | ret = 1; |
| 970 | |
| 971 | end: |
| 972 | EVP_ENCODE_CTX_free(ctx); |
| 973 | pem_free(name, flags, 0); |
| 974 | BIO_free(headerB); |
| 975 | BIO_free(dataB); |
| 976 | return ret; |
| 977 | } |
| 978 | |
| 979 | int PEM_read_bio(BIO *bp, char **name, char **header, unsigned char **data, |
| 980 | long *len) |
| 981 | { |
| 982 | return PEM_read_bio_ex(bp, name, header, data, len, PEM_FLAG_EAY_COMPATIBLE); |
| 983 | } |
| 984 | |
| 985 | /* |
| 986 | * Check pem string and return prefix length. If for example the pem_str == |
| 987 | * "RSA PRIVATE KEY" and suffix = "PRIVATE KEY" the return value is 3 for the |
| 988 | * string "RSA". |
| 989 | */ |
| 990 | |
| 991 | int pem_check_suffix(const char *pem_str, const char *suffix) |
| 992 | { |
| 993 | int pem_len = strlen(pem_str); |
| 994 | int suffix_len = strlen(suffix); |
| 995 | const char *p; |
| 996 | if (suffix_len + 1 >= pem_len) |
| 997 | return 0; |
| 998 | p = pem_str + pem_len - suffix_len; |
| 999 | if (strcmp(p, suffix)) |
| 1000 | return 0; |
| 1001 | p--; |
| 1002 | if (*p != ' ') |
| 1003 | return 0; |
| 1004 | return p - pem_str; |
| 1005 | } |