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