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