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