/src/openssl30/crypto/pkcs12/p12_kiss.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1999-2021 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 |  | #include <stdio.h> | 
| 11 |  | #include "internal/cryptlib.h" | 
| 12 |  | #include <openssl/pkcs12.h> | 
| 13 |  | #include "crypto/x509.h" /* for ossl_x509_add_cert_new() */ | 
| 14 |  |  | 
| 15 |  | /* Simplified PKCS#12 routines */ | 
| 16 |  |  | 
| 17 |  | static int parse_pk12(PKCS12 *p12, const char *pass, int passlen, | 
| 18 |  |                       EVP_PKEY **pkey, STACK_OF(X509) *ocerts); | 
| 19 |  |  | 
| 20 |  | static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass, | 
| 21 |  |                       int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts); | 
| 22 |  |  | 
| 23 |  | static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen, | 
| 24 |  |                      EVP_PKEY **pkey, STACK_OF(X509) *ocerts); | 
| 25 |  |  | 
| 26 |  | /* | 
| 27 |  |  * Parse and decrypt a PKCS#12 structure returning user key, user cert and | 
| 28 |  |  * other (CA) certs. Note either ca should be NULL, *ca should be NULL, or it | 
| 29 |  |  * should point to a valid STACK structure. pkey and/or cert may be NULL; | 
| 30 |  |  * if non-NULL the variables they point to can be passed uninitialised. | 
| 31 |  |  */ | 
| 32 |  |  | 
| 33 |  | int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert, | 
| 34 |  |                  STACK_OF(X509) **ca) | 
| 35 | 0 | { | 
| 36 | 0 |     STACK_OF(X509) *ocerts = NULL; | 
| 37 | 0 |     X509 *x = NULL; | 
| 38 |  | 
 | 
| 39 | 0 |     if (pkey != NULL) | 
| 40 | 0 |         *pkey = NULL; | 
| 41 | 0 |     if (cert != NULL) | 
| 42 | 0 |         *cert = NULL; | 
| 43 |  |  | 
| 44 |  |     /* Check for NULL PKCS12 structure */ | 
| 45 |  | 
 | 
| 46 | 0 |     if (p12 == NULL) { | 
| 47 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_NULL_PKCS12_POINTER); | 
| 48 | 0 |         return 0; | 
| 49 | 0 |     } | 
| 50 |  |  | 
| 51 |  |     /* Check the mac */ | 
| 52 |  |  | 
| 53 |  |     /* | 
| 54 |  |      * If password is zero length or NULL then try verifying both cases to | 
| 55 |  |      * determine which password is correct. The reason for this is that under | 
| 56 |  |      * PKCS#12 password based encryption no password and a zero length | 
| 57 |  |      * password are two different things... | 
| 58 |  |      */ | 
| 59 |  |  | 
| 60 | 0 |     if (pass == NULL || *pass == '\0') { | 
| 61 | 0 |         if (!PKCS12_mac_present(p12) | 
| 62 | 0 |             || PKCS12_verify_mac(p12, NULL, 0)) | 
| 63 | 0 |             pass = NULL; | 
| 64 | 0 |         else if (PKCS12_verify_mac(p12, "", 0)) | 
| 65 | 0 |             pass = ""; | 
| 66 | 0 |         else { | 
| 67 | 0 |             ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE); | 
| 68 | 0 |             goto err; | 
| 69 | 0 |         } | 
| 70 | 0 |     } else if (!PKCS12_verify_mac(p12, pass, -1)) { | 
| 71 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_VERIFY_FAILURE); | 
| 72 | 0 |         goto err; | 
| 73 | 0 |     } | 
| 74 |  |  | 
| 75 |  |     /* If needed, allocate stack for other certificates */ | 
| 76 | 0 |     if ((cert != NULL || ca != NULL) | 
| 77 | 0 |             && (ocerts = sk_X509_new_null()) == NULL) { | 
| 78 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 79 | 0 |         goto err; | 
| 80 | 0 |     } | 
| 81 |  |  | 
| 82 | 0 |     if (!parse_pk12(p12, pass, -1, pkey, ocerts)) { | 
| 83 | 0 |         int err = ERR_peek_last_error(); | 
| 84 |  | 
 | 
| 85 | 0 |         if (ERR_GET_LIB(err) != ERR_LIB_EVP | 
| 86 | 0 |                 && ERR_GET_REASON(err) != EVP_R_UNSUPPORTED_ALGORITHM) | 
| 87 | 0 |             ERR_raise(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR); | 
| 88 | 0 |         goto err; | 
| 89 | 0 |     } | 
| 90 |  |  | 
| 91 |  |     /* Split the certs in ocerts over *cert and *ca as far as requested */ | 
| 92 | 0 |     while ((x = sk_X509_shift(ocerts)) != NULL) { | 
| 93 | 0 |         if (pkey != NULL && *pkey != NULL | 
| 94 | 0 |                 && cert != NULL && *cert == NULL) { | 
| 95 | 0 |             int match; | 
| 96 |  | 
 | 
| 97 | 0 |             ERR_set_mark(); | 
| 98 | 0 |             match = X509_check_private_key(x, *pkey); | 
| 99 | 0 |             ERR_pop_to_mark(); | 
| 100 | 0 |             if (match) { | 
| 101 | 0 |                 *cert = x; | 
| 102 | 0 |                 continue; | 
| 103 | 0 |             } | 
| 104 | 0 |         } | 
| 105 |  |  | 
| 106 | 0 |         if (ca != NULL) { | 
| 107 | 0 |             if (!ossl_x509_add_cert_new(ca, x, X509_ADD_FLAG_DEFAULT)) | 
| 108 | 0 |                 goto err; | 
| 109 | 0 |             continue; | 
| 110 | 0 |         } | 
| 111 | 0 |         X509_free(x); | 
| 112 | 0 |     } | 
| 113 | 0 |     sk_X509_free(ocerts); | 
| 114 |  | 
 | 
| 115 | 0 |     return 1; | 
| 116 |  |  | 
| 117 | 0 |  err: | 
| 118 |  | 
 | 
| 119 | 0 |     if (pkey != NULL) { | 
| 120 | 0 |         EVP_PKEY_free(*pkey); | 
| 121 | 0 |         *pkey = NULL; | 
| 122 | 0 |     } | 
| 123 | 0 |     if (cert != NULL) { | 
| 124 | 0 |         X509_free(*cert); | 
| 125 | 0 |         *cert = NULL; | 
| 126 | 0 |     } | 
| 127 | 0 |     X509_free(x); | 
| 128 | 0 |     sk_X509_pop_free(ocerts, X509_free); | 
| 129 | 0 |     return 0; | 
| 130 |  | 
 | 
| 131 | 0 | } | 
| 132 |  |  | 
| 133 |  | /* Parse the outer PKCS#12 structure */ | 
| 134 |  |  | 
| 135 |  | /* pkey and/or ocerts may be NULL */ | 
| 136 |  | static int parse_pk12(PKCS12 *p12, const char *pass, int passlen, | 
| 137 |  |                       EVP_PKEY **pkey, STACK_OF(X509) *ocerts) | 
| 138 | 0 | { | 
| 139 | 0 |     STACK_OF(PKCS7) *asafes; | 
| 140 | 0 |     STACK_OF(PKCS12_SAFEBAG) *bags; | 
| 141 | 0 |     int i, bagnid; | 
| 142 | 0 |     PKCS7 *p7; | 
| 143 |  | 
 | 
| 144 | 0 |     if ((asafes = PKCS12_unpack_authsafes(p12)) == NULL) | 
| 145 | 0 |         return 0; | 
| 146 | 0 |     for (i = 0; i < sk_PKCS7_num(asafes); i++) { | 
| 147 | 0 |         p7 = sk_PKCS7_value(asafes, i); | 
| 148 | 0 |         bagnid = OBJ_obj2nid(p7->type); | 
| 149 | 0 |         if (bagnid == NID_pkcs7_data) { | 
| 150 | 0 |             bags = PKCS12_unpack_p7data(p7); | 
| 151 | 0 |         } else if (bagnid == NID_pkcs7_encrypted) { | 
| 152 | 0 |             bags = PKCS12_unpack_p7encdata(p7, pass, passlen); | 
| 153 | 0 |         } else | 
| 154 | 0 |             continue; | 
| 155 | 0 |         if (!bags) { | 
| 156 | 0 |             sk_PKCS7_pop_free(asafes, PKCS7_free); | 
| 157 | 0 |             return 0; | 
| 158 | 0 |         } | 
| 159 | 0 |         if (!parse_bags(bags, pass, passlen, pkey, ocerts)) { | 
| 160 | 0 |             sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | 
| 161 | 0 |             sk_PKCS7_pop_free(asafes, PKCS7_free); | 
| 162 | 0 |             return 0; | 
| 163 | 0 |         } | 
| 164 | 0 |         sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free); | 
| 165 | 0 |     } | 
| 166 | 0 |     sk_PKCS7_pop_free(asafes, PKCS7_free); | 
| 167 | 0 |     return 1; | 
| 168 | 0 | } | 
| 169 |  |  | 
| 170 |  | /* pkey and/or ocerts may be NULL */ | 
| 171 |  | static int parse_bags(const STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass, | 
| 172 |  |                       int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts) | 
| 173 | 0 | { | 
| 174 | 0 |     int i; | 
| 175 | 0 |     for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) { | 
| 176 | 0 |         if (!parse_bag(sk_PKCS12_SAFEBAG_value(bags, i), | 
| 177 | 0 |                        pass, passlen, pkey, ocerts)) | 
| 178 | 0 |             return 0; | 
| 179 | 0 |     } | 
| 180 | 0 |     return 1; | 
| 181 | 0 | } | 
| 182 |  |  | 
| 183 |  | /* pkey and/or ocerts may be NULL */ | 
| 184 |  | static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen, | 
| 185 |  |                      EVP_PKEY **pkey, STACK_OF(X509) *ocerts) | 
| 186 | 0 | { | 
| 187 | 0 |     PKCS8_PRIV_KEY_INFO *p8; | 
| 188 | 0 |     X509 *x509; | 
| 189 | 0 |     const ASN1_TYPE *attrib; | 
| 190 | 0 |     ASN1_BMPSTRING *fname = NULL; | 
| 191 | 0 |     ASN1_OCTET_STRING *lkid = NULL; | 
| 192 |  | 
 | 
| 193 | 0 |     if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName))) | 
| 194 | 0 |         fname = attrib->value.bmpstring; | 
| 195 |  | 
 | 
| 196 | 0 |     if ((attrib = PKCS12_SAFEBAG_get0_attr(bag, NID_localKeyID))) | 
| 197 | 0 |         lkid = attrib->value.octet_string; | 
| 198 |  | 
 | 
| 199 | 0 |     switch (PKCS12_SAFEBAG_get_nid(bag)) { | 
| 200 | 0 |     case NID_keyBag: | 
| 201 | 0 |         if (pkey == NULL || *pkey != NULL) | 
| 202 | 0 |             return 1; | 
| 203 | 0 |         *pkey = EVP_PKCS82PKEY(PKCS12_SAFEBAG_get0_p8inf(bag)); | 
| 204 | 0 |         if (*pkey == NULL) | 
| 205 | 0 |             return 0; | 
| 206 | 0 |         break; | 
| 207 |  |  | 
| 208 | 0 |     case NID_pkcs8ShroudedKeyBag: | 
| 209 | 0 |         if (pkey == NULL || *pkey != NULL) | 
| 210 | 0 |             return 1; | 
| 211 | 0 |         if ((p8 = PKCS12_decrypt_skey(bag, pass, passlen)) == NULL) | 
| 212 | 0 |             return 0; | 
| 213 | 0 |         *pkey = EVP_PKCS82PKEY(p8); | 
| 214 | 0 |         PKCS8_PRIV_KEY_INFO_free(p8); | 
| 215 | 0 |         if (!(*pkey)) | 
| 216 | 0 |             return 0; | 
| 217 | 0 |         break; | 
| 218 |  |  | 
| 219 | 0 |     case NID_certBag: | 
| 220 | 0 |         if (ocerts == NULL | 
| 221 | 0 |                 || PKCS12_SAFEBAG_get_bag_nid(bag) != NID_x509Certificate) | 
| 222 | 0 |             return 1; | 
| 223 | 0 |         if ((x509 = PKCS12_SAFEBAG_get1_cert(bag)) == NULL) | 
| 224 | 0 |             return 0; | 
| 225 | 0 |         if (lkid && !X509_keyid_set1(x509, lkid->data, lkid->length)) { | 
| 226 | 0 |             X509_free(x509); | 
| 227 | 0 |             return 0; | 
| 228 | 0 |         } | 
| 229 | 0 |         if (fname) { | 
| 230 | 0 |             int len, r; | 
| 231 | 0 |             unsigned char *data; | 
| 232 |  | 
 | 
| 233 | 0 |             len = ASN1_STRING_to_UTF8(&data, fname); | 
| 234 | 0 |             if (len >= 0) { | 
| 235 | 0 |                 r = X509_alias_set1(x509, data, len); | 
| 236 | 0 |                 OPENSSL_free(data); | 
| 237 | 0 |                 if (!r) { | 
| 238 | 0 |                     X509_free(x509); | 
| 239 | 0 |                     return 0; | 
| 240 | 0 |                 } | 
| 241 | 0 |             } | 
| 242 | 0 |         } | 
| 243 |  |  | 
| 244 | 0 |         if (!sk_X509_push(ocerts, x509)) { | 
| 245 | 0 |             X509_free(x509); | 
| 246 | 0 |             return 0; | 
| 247 | 0 |         } | 
| 248 |  |  | 
| 249 | 0 |         break; | 
| 250 |  |  | 
| 251 | 0 |     case NID_safeContentsBag: | 
| 252 | 0 |         return parse_bags(PKCS12_SAFEBAG_get0_safes(bag), pass, passlen, pkey, | 
| 253 | 0 |                           ocerts); | 
| 254 |  |  | 
| 255 | 0 |     default: | 
| 256 | 0 |         return 1; | 
| 257 | 0 |     } | 
| 258 | 0 |     return 1; | 
| 259 | 0 | } |