diff options
Diffstat (limited to 'src/lib/libcrypto/pkcs12/p12_kiss.c')
-rw-r--r-- | src/lib/libcrypto/pkcs12/p12_kiss.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/src/lib/libcrypto/pkcs12/p12_kiss.c b/src/lib/libcrypto/pkcs12/p12_kiss.c new file mode 100644 index 0000000000..767e1303da --- /dev/null +++ b/src/lib/libcrypto/pkcs12/p12_kiss.c | |||
@@ -0,0 +1,238 @@ | |||
1 | /* p12_kiss.c */ | ||
2 | /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL | ||
3 | * project 1999. | ||
4 | */ | ||
5 | /* ==================================================================== | ||
6 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions | ||
10 | * are met: | ||
11 | * | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * | ||
15 | * 2. Redistributions in binary form must reproduce the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer in | ||
17 | * the documentation and/or other materials provided with the | ||
18 | * distribution. | ||
19 | * | ||
20 | * 3. All advertising materials mentioning features or use of this | ||
21 | * software must display the following acknowledgment: | ||
22 | * "This product includes software developed by the OpenSSL Project | ||
23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
24 | * | ||
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
26 | * endorse or promote products derived from this software without | ||
27 | * prior written permission. For written permission, please contact | ||
28 | * licensing@OpenSSL.org. | ||
29 | * | ||
30 | * 5. Products derived from this software may not be called "OpenSSL" | ||
31 | * nor may "OpenSSL" appear in their names without prior written | ||
32 | * permission of the OpenSSL Project. | ||
33 | * | ||
34 | * 6. Redistributions of any form whatsoever must retain the following | ||
35 | * acknowledgment: | ||
36 | * "This product includes software developed by the OpenSSL Project | ||
37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
38 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
51 | * ==================================================================== | ||
52 | * | ||
53 | * This product includes cryptographic software written by Eric Young | ||
54 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
55 | * Hudson (tjh@cryptsoft.com). | ||
56 | * | ||
57 | */ | ||
58 | |||
59 | #include <stdio.h> | ||
60 | #include "cryptlib.h" | ||
61 | #include <openssl/pkcs12.h> | ||
62 | |||
63 | /* Simplified PKCS#12 routines */ | ||
64 | |||
65 | static int parse_pk12( PKCS12 *p12, const char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca); | ||
66 | static int parse_bags( STACK *bags, const char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch); | ||
67 | static int parse_bag( PKCS12_SAFEBAG *bag, const char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch); | ||
68 | /* Parse and decrypt a PKCS#12 structure returning user key, user cert | ||
69 | * and other (CA) certs. Note either ca should be NULL, *ca should be NULL, | ||
70 | * or it should point to a valid STACK structure. pkey and cert can be | ||
71 | * passed unitialised. | ||
72 | */ | ||
73 | |||
74 | int PKCS12_parse (PKCS12 *p12, const char *pass, EVP_PKEY **pkey, X509 **cert, | ||
75 | STACK **ca) | ||
76 | { | ||
77 | |||
78 | /* Check for NULL PKCS12 structure */ | ||
79 | |||
80 | if(!p12) { | ||
81 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER); | ||
82 | return 0; | ||
83 | } | ||
84 | |||
85 | /* Allocate stack for ca certificates if needed */ | ||
86 | if ((ca != NULL) && (*ca == NULL)) { | ||
87 | if (!(*ca = sk_new(NULL))) { | ||
88 | PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE); | ||
89 | return 0; | ||
90 | } | ||
91 | } | ||
92 | |||
93 | if(pkey) *pkey = NULL; | ||
94 | if(cert) *cert = NULL; | ||
95 | |||
96 | /* Check the mac */ | ||
97 | |||
98 | if (!PKCS12_verify_mac (p12, pass, -1)) { | ||
99 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE); | ||
100 | goto err; | ||
101 | } | ||
102 | |||
103 | if (!parse_pk12 (p12, pass, -1, pkey, cert, ca)) { | ||
104 | PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR); | ||
105 | goto err; | ||
106 | } | ||
107 | |||
108 | return 1; | ||
109 | |||
110 | err: | ||
111 | |||
112 | if (pkey && *pkey) EVP_PKEY_free (*pkey); | ||
113 | if (cert && *cert) X509_free (*cert); | ||
114 | if (ca) sk_pop_free (*ca, X509_free); | ||
115 | return 0; | ||
116 | |||
117 | } | ||
118 | |||
119 | /* Parse the outer PKCS#12 structure */ | ||
120 | |||
121 | static int parse_pk12 (PKCS12 *p12, const char *pass, int passlen, | ||
122 | EVP_PKEY **pkey, X509 **cert, STACK **ca) | ||
123 | { | ||
124 | STACK *asafes, *bags; | ||
125 | int i, bagnid; | ||
126 | PKCS7 *p7; | ||
127 | ASN1_OCTET_STRING *keyid = NULL; | ||
128 | char keymatch = 0; | ||
129 | if (!( asafes = M_PKCS12_unpack_authsafes (p12))) return 0; | ||
130 | for (i = 0; i < sk_num (asafes); i++) { | ||
131 | p7 = (PKCS7 *) sk_value (asafes, i); | ||
132 | bagnid = OBJ_obj2nid (p7->type); | ||
133 | if (bagnid == NID_pkcs7_data) { | ||
134 | bags = M_PKCS12_unpack_p7data (p7); | ||
135 | } else if (bagnid == NID_pkcs7_encrypted) { | ||
136 | bags = M_PKCS12_unpack_p7encdata (p7, pass, passlen); | ||
137 | } else continue; | ||
138 | if (!bags) { | ||
139 | sk_pop_free (asafes, PKCS7_free); | ||
140 | return 0; | ||
141 | } | ||
142 | if (!parse_bags (bags, pass, passlen, pkey, cert, ca, | ||
143 | &keyid, &keymatch)) { | ||
144 | sk_pop_free (bags, PKCS12_SAFEBAG_free); | ||
145 | sk_pop_free (asafes, PKCS7_free); | ||
146 | return 0; | ||
147 | } | ||
148 | sk_pop_free (bags, PKCS12_SAFEBAG_free); | ||
149 | } | ||
150 | sk_pop_free (asafes, PKCS7_free); | ||
151 | if (keyid) ASN1_OCTET_STRING_free (keyid); | ||
152 | return 1; | ||
153 | } | ||
154 | |||
155 | |||
156 | static int parse_bags (STACK *bags, const char *pass, int passlen, | ||
157 | EVP_PKEY **pkey, X509 **cert, STACK **ca, | ||
158 | ASN1_OCTET_STRING **keyid, char *keymatch) | ||
159 | { | ||
160 | int i; | ||
161 | for (i = 0; i < sk_num (bags); i++) { | ||
162 | if (!parse_bag ((PKCS12_SAFEBAG *)sk_value (bags, i), | ||
163 | pass, passlen, pkey, cert, ca, keyid, | ||
164 | keymatch)) return 0; | ||
165 | } | ||
166 | return 1; | ||
167 | } | ||
168 | |||
169 | #define MATCH_KEY 0x1 | ||
170 | #define MATCH_CERT 0x2 | ||
171 | #define MATCH_ALL 0x3 | ||
172 | |||
173 | static int parse_bag (PKCS12_SAFEBAG *bag, const char *pass, int passlen, | ||
174 | EVP_PKEY **pkey, X509 **cert, STACK **ca, | ||
175 | ASN1_OCTET_STRING **keyid, | ||
176 | char *keymatch) | ||
177 | { | ||
178 | PKCS8_PRIV_KEY_INFO *p8; | ||
179 | X509 *x509; | ||
180 | ASN1_OCTET_STRING *lkey = NULL; | ||
181 | ASN1_TYPE *attrib; | ||
182 | |||
183 | |||
184 | if ((attrib = PKCS12_get_attr (bag, NID_localKeyID))) | ||
185 | lkey = attrib->value.octet_string; | ||
186 | |||
187 | /* Check for any local key id matching (if needed) */ | ||
188 | if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) { | ||
189 | if (*keyid) { | ||
190 | if (ASN1_OCTET_STRING_cmp (*keyid, lkey)) lkey = NULL; | ||
191 | } else { | ||
192 | if (!(*keyid = ASN1_OCTET_STRING_dup (lkey))) { | ||
193 | PKCS12err(PKCS12_F_PARSE_BAGS,ERR_R_MALLOC_FAILURE); | ||
194 | return 0; | ||
195 | } | ||
196 | } | ||
197 | } | ||
198 | |||
199 | switch (M_PKCS12_bag_type(bag)) | ||
200 | { | ||
201 | case NID_keyBag: | ||
202 | if (!lkey || !pkey) return 1; | ||
203 | if (!(*pkey = EVP_PKCS82PKEY (bag->value.keybag))) return 0; | ||
204 | *keymatch |= MATCH_KEY; | ||
205 | break; | ||
206 | |||
207 | case NID_pkcs8ShroudedKeyBag: | ||
208 | if (!lkey || !pkey) return 1; | ||
209 | if (!(p8 = M_PKCS12_decrypt_skey (bag, pass, passlen))) | ||
210 | return 0; | ||
211 | *pkey = EVP_PKCS82PKEY (p8); | ||
212 | PKCS8_PRIV_KEY_INFO_free (p8); | ||
213 | if (!(*pkey)) return 0; | ||
214 | *keymatch |= MATCH_KEY; | ||
215 | break; | ||
216 | |||
217 | case NID_certBag: | ||
218 | if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate ) | ||
219 | return 1; | ||
220 | if (!(x509 = M_PKCS12_certbag2x509(bag))) return 0; | ||
221 | if (lkey) { | ||
222 | *keymatch |= MATCH_CERT; | ||
223 | if (cert) *cert = x509; | ||
224 | } else if (ca) sk_push (*ca, (char *)x509); | ||
225 | break; | ||
226 | |||
227 | case NID_safeContentsBag: | ||
228 | return parse_bags(bag->value.safes, pass, passlen, | ||
229 | pkey, cert, ca, keyid, keymatch); | ||
230 | break; | ||
231 | |||
232 | default: | ||
233 | return 1; | ||
234 | break; | ||
235 | } | ||
236 | return 1; | ||
237 | } | ||
238 | |||