]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_kiss.c
Change functions to ANSI C.
[thirdparty/openssl.git] / crypto / pkcs12 / p12_kiss.c
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 "pkcs12.h"
62
63 /* Simplified PKCS#12 routines */
64
65 #ifndef NOPROTO
66 static int parse_pk12( PKCS12 *p12, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca);
67 static int parse_bags( STACK *bags, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch);
68 static int parse_bag( PKCS12_SAFEBAG *bag, unsigned char *pass, int passlen, EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid, char *keymatch);
69 #else
70 static int parse_pk12();
71 static int parse_bags();
72 static int parse_bag();
73 #endif
74
75 /* Parse and decrypt a PKCS#12 structure returning user key, user cert
76 * and other (CA) certs. Note either ca should be NULL, *ca should be NULL,
77 * or it should point to a valid STACK structure. pkey and cert can be
78 * passed unitialised.
79 */
80
81 int PKCS12_parse (PKCS12 *p12, char *pass, EVP_PKEY **pkey, X509 **cert,
82 STACK **ca)
83 {
84
85 /* Check for NULL PKCS12 structure */
86
87 if(!p12) {
88 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_INVALID_NULL_PKCS12_POINTER);
89 return 0;
90 }
91
92 /* Allocate stack for ca certificates if needed */
93 if ((ca != NULL) && (*ca == NULL)) {
94 if (!(*ca = sk_new(NULL))) {
95 PKCS12err(PKCS12_F_PKCS12_PARSE,ERR_R_MALLOC_FAILURE);
96 return 0;
97 }
98 }
99
100 if(pkey) *pkey = NULL;
101 if(cert) *cert = NULL;
102
103 /* Check the mac */
104
105 if (!PKCS12_verify_mac (p12, pass, -1)) {
106 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_MAC_VERIFY_FAILURE);
107 goto err;
108 }
109
110 if (!parse_pk12 (p12, pass, -1, pkey, cert, ca)) {
111 PKCS12err(PKCS12_F_PKCS12_PARSE,PKCS12_R_PARSE_ERROR);
112 goto err;
113 }
114
115 return 1;
116
117 err:
118
119 if (pkey && *pkey) EVP_PKEY_free (*pkey);
120 if (cert && *cert) X509_free (*cert);
121 if (ca) sk_pop_free (*ca, X509_free);
122 return 0;
123
124 }
125
126 /* Parse the outer PKCS#12 structure */
127
128 static int parse_pk12 (PKCS12 *p12, unsigned char *pass, int passlen,
129 EVP_PKEY **pkey, X509 **cert, STACK **ca)
130 {
131 STACK *asafes, *bags;
132 int i, bagnid;
133 PKCS7 *p7;
134 ASN1_OCTET_STRING *keyid = NULL;
135 char keymatch = 0;
136 if (!( asafes = M_PKCS12_unpack_authsafes (p12))) return 0;
137 for (i = 0; i < sk_num (asafes); i++) {
138 p7 = (PKCS7 *) sk_value (asafes, i);
139 bagnid = OBJ_obj2nid (p7->type);
140 if (bagnid == NID_pkcs7_data) {
141 bags = M_PKCS12_unpack_p7data (p7);
142 } else if (bagnid == NID_pkcs7_encrypted) {
143 bags = M_PKCS12_unpack_p7encdata (p7, pass, passlen);
144 } else continue;
145 if (!bags) {
146 sk_pop_free (asafes, PKCS7_free);
147 return 0;
148 }
149 if (!parse_bags (bags, pass, passlen, pkey, cert, ca,
150 &keyid, &keymatch)) {
151 sk_pop_free (bags, PKCS12_SAFEBAG_free);
152 sk_pop_free (asafes, PKCS7_free);
153 return 0;
154 }
155 sk_pop_free (bags, PKCS12_SAFEBAG_free);
156 }
157 sk_pop_free (asafes, PKCS7_free);
158 if (keyid) ASN1_OCTET_STRING_free (keyid);
159 return 1;
160 }
161
162
163 static int parse_bags (STACK *bags, unsigned char *pass, int passlen,
164 EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid,
165 char *keymatch)
166 {
167 int i;
168 for (i = 0; i < sk_num (bags); i++) {
169 if (!parse_bag ((PKCS12_SAFEBAG *)sk_value (bags, i),
170 pass, passlen, pkey, cert, ca, keyid,
171 keymatch)) return 0;
172 }
173 return 1;
174 }
175
176 #define MATCH_KEY 0x1
177 #define MATCH_CERT 0x2
178 #define MATCH_ALL 0x3
179
180 static int parse_bag (PKCS12_SAFEBAG *bag, unsigned char *pass, int passlen,
181 EVP_PKEY **pkey, X509 **cert, STACK **ca, ASN1_OCTET_STRING **keyid,
182 char *keymatch)
183 {
184 PKCS8_PRIV_KEY_INFO *p8;
185 X509 *x509;
186 ASN1_OCTET_STRING *lkey = NULL;
187 ASN1_TYPE *attrib;
188
189
190 if ((attrib = PKCS12_get_attr (bag, NID_localKeyID)))
191 lkey = attrib->value.octet_string;
192
193 /* Check for any local key id matching (if needed) */
194 if (lkey && ((*keymatch & MATCH_ALL) != MATCH_ALL)) {
195 if (*keyid) {
196 if (ASN1_OCTET_STRING_cmp (*keyid, lkey)) lkey = NULL;
197 } else {
198 if (!(*keyid = ASN1_OCTET_STRING_dup (lkey))) {
199 PKCS12err(PKCS12_F_PARSE_BAGS,ERR_R_MALLOC_FAILURE);
200 return 0;
201 }
202 }
203 }
204
205 switch (M_PKCS12_bag_type(bag))
206 {
207 case NID_keyBag:
208 if (!lkey || !pkey) return 1;
209 if (!(*pkey = EVP_PKCS82PKEY (bag->value.keybag))) return 0;
210 *keymatch |= MATCH_KEY;
211 break;
212
213 case NID_pkcs8ShroudedKeyBag:
214 if (!lkey || !pkey) return 1;
215 if (!(p8 = M_PKCS12_decrypt_skey (bag, pass, passlen)))
216 return 0;
217 *pkey = EVP_PKCS82PKEY (p8);
218 PKCS8_PRIV_KEY_INFO_free (p8);
219 if (!(*pkey)) return 0;
220 *keymatch |= MATCH_KEY;
221 break;
222
223 case NID_certBag:
224 if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate )
225 return 1;
226 if (!(x509 = M_PKCS12_certbag2x509(bag))) return 0;
227 if (lkey) {
228 *keymatch |= MATCH_CERT;
229 if (cert) *cert = x509;
230 } else if (ca) sk_push (*ca, (char *)x509);
231 break;
232
233 case NID_safeContentsBag:
234 return parse_bags(bag->value.safes, pass, passlen,
235 pkey, cert, ca, keyid, keymatch);
236 break;
237
238 default:
239 return 1;
240 break;
241 }
242 return 1;
243 }
244