63
63
/* Simplified PKCS#12 routines */
65
65
static int parse_pk12( PKCS12 *p12, const char *pass, int passlen,
66
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca);
66
EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
68
68
static int parse_bags( STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
69
int passlen, EVP_PKEY **pkey, X509 **cert,
70
STACK_OF(X509) **ca, ASN1_OCTET_STRING **keyid,
69
int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
73
71
static int parse_bag( PKCS12_SAFEBAG *bag, const char *pass, int passlen,
74
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca,
75
ASN1_OCTET_STRING **keyid, char *keymatch);
72
EVP_PKEY **pkey, STACK_OF(X509) *ocerts);
77
74
/* Parse and decrypt a PKCS#12 structure returning user key, user cert
78
75
* and other (CA) certs. Note either ca should be NULL, *ca should be NULL,
83
80
int PKCS12_parse(PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert,
84
81
STACK_OF(X509) **ca)
83
STACK_OF(X509) *ocerts = NULL;
87
85
/* Check for NULL PKCS12 structure */
90
89
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
94
/* Allocate stack for ca certificates if needed */
95
if ((ca != NULL) && (*ca == NULL)) {
96
if (!(*ca = sk_X509_new_null())) {
97
PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
102
if(pkey) *pkey = NULL;
103
if(cert) *cert = NULL;
105
98
/* Check the mac */
125
if (!parse_pk12 (p12, pass, -1, pkey, cert, ca))
118
/* Allocate stack for other certificates */
119
ocerts = sk_X509_new_null();
123
PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
127
if (!parse_pk12 (p12, pass, -1, pkey, ocerts))
127
129
PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR);
133
while ((x = sk_X509_pop(ocerts)))
135
if (pkey && *pkey && cert && !*cert)
137
if (X509_check_private_key(x, *pkey))
147
*ca = sk_X509_new_null();
150
if (!sk_X509_push(*ca, x))
159
sk_X509_pop_free(ocerts, X509_free);
135
if (pkey && *pkey) EVP_PKEY_free(*pkey);
136
if (cert && *cert) X509_free(*cert);
137
if (ca) sk_X509_pop_free(*ca, X509_free);
166
EVP_PKEY_free(*pkey);
172
sk_X509_pop_free(ocerts, X509_free);
142
177
/* Parse the outer PKCS#12 structure */
144
179
static int parse_pk12(PKCS12 *p12, const char *pass, int passlen,
145
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca)
180
EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
147
182
STACK_OF(PKCS7) *asafes;
148
183
STACK_OF(PKCS12_SAFEBAG) *bags;
151
ASN1_OCTET_STRING *keyid = NULL;
154
187
if (!(asafes = PKCS12_unpack_authsafes (p12))) return 0;
155
188
for (i = 0; i < sk_PKCS7_num (asafes); i++) {
156
189
p7 = sk_PKCS7_value (asafes, i);
164
197
sk_PKCS7_pop_free(asafes, PKCS7_free);
167
if (!parse_bags(bags, pass, passlen, pkey, cert, ca,
168
&keyid, &keymatch)) {
200
if (!parse_bags(bags, pass, passlen, pkey, ocerts)) {
169
201
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
170
202
sk_PKCS7_pop_free(asafes, PKCS7_free);
173
205
sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
175
207
sk_PKCS7_pop_free(asafes, PKCS7_free);
176
if (keyid) M_ASN1_OCTET_STRING_free(keyid);
181
212
static int parse_bags(STACK_OF(PKCS12_SAFEBAG) *bags, const char *pass,
182
int passlen, EVP_PKEY **pkey, X509 **cert,
183
STACK_OF(X509) **ca, ASN1_OCTET_STRING **keyid,
213
int passlen, EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
187
216
for (i = 0; i < sk_PKCS12_SAFEBAG_num(bags); i++) {
188
217
if (!parse_bag(sk_PKCS12_SAFEBAG_value (bags, i),
189
pass, passlen, pkey, cert, ca, keyid,
218
pass, passlen, pkey, ocerts))
195
#define MATCH_KEY 0x1
196
#define MATCH_CERT 0x2
197
#define MATCH_ALL 0x3
199
224
static int parse_bag(PKCS12_SAFEBAG *bag, const char *pass, int passlen,
200
EVP_PKEY **pkey, X509 **cert, STACK_OF(X509) **ca,
201
ASN1_OCTET_STRING **keyid,
225
EVP_PKEY **pkey, STACK_OF(X509) *ocerts)
204
227
PKCS8_PRIV_KEY_INFO *p8;
206
ASN1_OCTET_STRING *lkey = NULL, *ckid = NULL;
207
229
ASN1_TYPE *attrib;
208
230
ASN1_BMPSTRING *fname = NULL;
231
ASN1_OCTET_STRING *lkid = NULL;
210
233
if ((attrib = PKCS12_get_attr (bag, NID_friendlyName)))
211
234
fname = attrib->value.bmpstring;
213
if ((attrib = PKCS12_get_attr (bag, NID_localKeyID))) {
214
lkey = attrib->value.octet_string;
236
if ((attrib = PKCS12_get_attr (bag, NID_localKeyID)))
237
lkid = attrib->value.octet_string;
218
/* Check for any local key id matching (if needed) */
219
if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) {
221
if (M_ASN1_OCTET_STRING_cmp(*keyid, lkey)) lkey = NULL;
223
if (!(*keyid = M_ASN1_OCTET_STRING_dup(lkey))) {
224
PKCS12err(PKCS12_F_PARSE_BAG,ERR_R_MALLOC_FAILURE);
230
239
switch (M_PKCS12_bag_type(bag))
233
if (!lkey || !pkey) return 1;
234
if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag))) return 0;
235
*keymatch |= MATCH_KEY;
244
if (!(*pkey = EVP_PKCS82PKEY(bag->value.keybag)))
238
248
case NID_pkcs8ShroudedKeyBag:
239
if (!lkey || !pkey) return 1;
240
251
if (!(p8 = PKCS12_decrypt_skey(bag, pass, passlen)))
242
253
*pkey = EVP_PKCS82PKEY(p8);
243
254
PKCS8_PRIV_KEY_INFO_free(p8);
244
255
if (!(*pkey)) return 0;
245
*keymatch |= MATCH_KEY;
248
258
case NID_certBag:
249
259
if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate )
251
if (!(x509 = PKCS12_certbag2x509(bag))) return 0;
261
if (!(x509 = PKCS12_certbag2x509(bag)))
263
if(lkid && !X509_keyid_set1(x509, lkid->data, lkid->length))
254
if (!X509_keyid_set1(x509, ckid->data, ckid->length))