GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcrypto/pkcs12/p12_crt.c Lines: 90 136 66.2 %
Date: 2017-11-13 Branches: 55 132 41.7 %

Line Branch Exec Source
1
/* $OpenBSD: p12_crt.c,v 1.17 2017/01/29 17:49:23 beck Exp $ */
2
/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3
 * project.
4
 */
5
/* ====================================================================
6
 * Copyright (c) 1999-2002 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
61
#include <openssl/err.h>
62
#include <openssl/pkcs12.h>
63
64
static int pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags,
65
    PKCS12_SAFEBAG *bag);
66
67
static int
68
copy_bag_attr(PKCS12_SAFEBAG *bag, EVP_PKEY *pkey, int nid)
69
{
70
	int idx;
71
	X509_ATTRIBUTE *attr;
72
73
8
	idx = EVP_PKEY_get_attr_by_NID(pkey, nid, -1);
74
4
	if (idx < 0)
75
4
		return 1;
76
	attr = EVP_PKEY_get_attr(pkey, idx);
77
	if (!X509at_add1_attr(&bag->attrib, attr))
78
		return 0;
79
	return 1;
80
4
}
81
82
PKCS12 *
83
PKCS12_create(char *pass, char *name, EVP_PKEY *pkey, X509 *cert,
84
    STACK_OF(X509) *ca, int nid_key, int nid_cert, int iter, int mac_iter,
85
    int keytype)
86
{
87
	PKCS12 *p12 = NULL;
88
4
	STACK_OF(PKCS7) *safes = NULL;
89
2
	STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
90
	PKCS12_SAFEBAG *bag = NULL;
91
	int i;
92
2
	unsigned char keyid[EVP_MAX_MD_SIZE];
93
2
	unsigned int keyidlen = 0;
94
95
	/* Set defaults */
96
2
	if (!nid_cert) {
97
		nid_cert = NID_pbe_WithSHA1And40BitRC2_CBC;
98
	}
99
2
	if (!nid_key)
100
		nid_key = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
101
2
	if (!iter)
102
		iter = PKCS12_DEFAULT_ITER;
103
2
	if (!mac_iter)
104
		mac_iter = 1;
105
106
2
	if (!pkey && !cert && !ca) {
107
		PKCS12error(PKCS12_R_INVALID_NULL_ARGUMENT);
108
		return NULL;
109
	}
110
111
2
	if (pkey && cert) {
112
2
		if (!X509_check_private_key(cert, pkey))
113
			return NULL;
114
2
		X509_digest(cert, EVP_sha1(), keyid, &keyidlen);
115
2
	}
116
117
2
	if (cert) {
118
2
		bag = PKCS12_add_cert(&bags, cert);
119

2
		if (name && !PKCS12_add_friendlyname(bag, name, -1))
120
			goto err;
121

4
		if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
122
			goto err;
123
	}
124
125
	/* Add all other certificates */
126
12
	for (i = 0; i < sk_X509_num(ca); i++) {
127
4
		if (!PKCS12_add_cert(&bags, sk_X509_value(ca, i)))
128
			goto err;
129
	}
130
131

4
	if (bags && !PKCS12_add_safe(&safes, bags, nid_cert, iter, pass))
132
		goto err;
133
134
2
	sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
135
2
	bags = NULL;
136
137
2
	if (pkey) {
138
2
		bag = PKCS12_add_key(&bags, pkey, keytype, iter, nid_key, pass);
139
140
2
		if (!bag)
141
			goto err;
142
143
2
		if (!copy_bag_attr(bag, pkey, NID_ms_csp_name))
144
			goto err;
145
2
		if (!copy_bag_attr(bag, pkey, NID_LocalKeySet))
146
			goto err;
147
148

2
		if (name && !PKCS12_add_friendlyname(bag, name, -1))
149
			goto err;
150

4
		if (keyidlen && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
151
			goto err;
152
	}
153
154

4
	if (bags && !PKCS12_add_safe(&safes, bags, -1, 0, NULL))
155
		goto err;
156
157
2
	sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
158
2
	bags = NULL;
159
160
2
	p12 = PKCS12_add_safes(safes, 0);
161
162
2
	if (!p12)
163
		goto err;
164
165
2
	sk_PKCS7_pop_free(safes, PKCS7_free);
166
167
2
	safes = NULL;
168
169

2
	if ((mac_iter != -1) &&
170
	    !PKCS12_set_mac(p12, pass, -1, NULL, 0, mac_iter, NULL))
171
		goto err;
172
173
2
	return p12;
174
175
err:
176
	if (p12)
177
		PKCS12_free(p12);
178
	if (safes)
179
		sk_PKCS7_pop_free(safes, PKCS7_free);
180
	if (bags)
181
		sk_PKCS12_SAFEBAG_pop_free(bags, PKCS12_SAFEBAG_free);
182
	return NULL;
183
2
}
184
185
PKCS12_SAFEBAG *
186
PKCS12_add_cert(STACK_OF(PKCS12_SAFEBAG) **pbags, X509 *cert)
187
{
188
	PKCS12_SAFEBAG *bag = NULL;
189
	char *name;
190
12
	int namelen = -1;
191
	unsigned char *keyid;
192
6
	int keyidlen = -1;
193
194
	/* Add user certificate */
195
6
	if (!(bag = PKCS12_x5092certbag(cert)))
196
		goto err;
197
198
	/* Use friendlyName and localKeyID in certificate.
199
	 * (if present)
200
	 */
201
6
	name = (char *)X509_alias_get0(cert, &namelen);
202

8
	if (name && !PKCS12_add_friendlyname(bag, name, namelen))
203
		goto err;
204
205
6
	keyid = X509_keyid_get0(cert, &keyidlen);
206
207

6
	if (keyid && !PKCS12_add_localkeyid(bag, keyid, keyidlen))
208
		goto err;
209
210
6
	if (!pkcs12_add_bag(pbags, bag))
211
		goto err;
212
213
6
	return bag;
214
215
err:
216
	if (bag)
217
		PKCS12_SAFEBAG_free(bag);
218
219
	return NULL;
220
6
}
221
222
PKCS12_SAFEBAG *
223
PKCS12_add_key(STACK_OF(PKCS12_SAFEBAG) **pbags, EVP_PKEY *key, int key_usage,
224
    int iter, int nid_key, char *pass)
225
{
226
	PKCS12_SAFEBAG *bag = NULL;
227
	PKCS8_PRIV_KEY_INFO *p8 = NULL;
228
229
	/* Make a PKCS#8 structure */
230
4
	if (!(p8 = EVP_PKEY2PKCS8(key)))
231
		goto err;
232

2
	if (key_usage && !PKCS8_add_keyusage(p8, key_usage))
233
		goto err;
234
2
	if (nid_key != -1) {
235
2
		bag = PKCS12_MAKE_SHKEYBAG(nid_key, pass, -1, NULL, 0,
236
		    iter, p8);
237
2
		PKCS8_PRIV_KEY_INFO_free(p8);
238
		p8 = NULL;
239
2
	} else {
240
		bag = PKCS12_MAKE_KEYBAG(p8);
241
		if (bag != NULL)
242
			p8 = NULL;
243
	}
244
245
2
	if (!bag)
246
		goto err;
247
248
2
	if (!pkcs12_add_bag(pbags, bag))
249
		goto err;
250
251
2
	return bag;
252
253
err:
254
	if (bag)
255
		PKCS12_SAFEBAG_free(bag);
256
	if (p8)
257
		PKCS8_PRIV_KEY_INFO_free(p8);
258
259
	return NULL;
260
2
}
261
262
int
263
PKCS12_add_safe(STACK_OF(PKCS7) **psafes, STACK_OF(PKCS12_SAFEBAG) *bags,
264
    int nid_safe, int iter, char *pass)
265
{
266
	PKCS7 *p7 = NULL;
267
	int free_safes = 0;
268
269
8
	if (!*psafes) {
270
2
		*psafes = sk_PKCS7_new_null();
271
2
		if (!*psafes)
272
			return 0;
273
		free_safes = 1;
274
2
	} else
275
		free_safes = 0;
276
277
4
	if (nid_safe == 0)
278
		nid_safe = NID_pbe_WithSHA1And40BitRC2_CBC;
279
280
4
	if (nid_safe == -1)
281
2
		p7 = PKCS12_pack_p7data(bags);
282
	else
283
2
		p7 = PKCS12_pack_p7encdata(nid_safe, pass, -1, NULL, 0,
284
		    iter, bags);
285
4
	if (!p7)
286
		goto err;
287
288
4
	if (!sk_PKCS7_push(*psafes, p7))
289
		goto err;
290
291
4
	return 1;
292
293
err:
294
	if (free_safes) {
295
		sk_PKCS7_free(*psafes);
296
		*psafes = NULL;
297
	}
298
299
	if (p7)
300
		PKCS7_free(p7);
301
302
	return 0;
303
4
}
304
305
static int
306
pkcs12_add_bag(STACK_OF(PKCS12_SAFEBAG) **pbags, PKCS12_SAFEBAG *bag)
307
{
308
	int free_bags;
309
310
16
	if (!pbags)
311
		return 1;
312
8
	if (!*pbags) {
313
4
		*pbags = sk_PKCS12_SAFEBAG_new_null();
314
4
		if (!*pbags)
315
			return 0;
316
		free_bags = 1;
317
4
	} else
318
		free_bags = 0;
319
320
8
	if (!sk_PKCS12_SAFEBAG_push(*pbags, bag)) {
321
		if (free_bags) {
322
			sk_PKCS12_SAFEBAG_free(*pbags);
323
			*pbags = NULL;
324
		}
325
		return 0;
326
	}
327
328
8
	return 1;
329
8
}
330
331
PKCS12 *
332
PKCS12_add_safes(STACK_OF(PKCS7) *safes, int nid_p7)
333
{
334
	PKCS12 *p12;
335
336
4
	if (nid_p7 <= 0)
337
2
		nid_p7 = NID_pkcs7_data;
338
2
	p12 = PKCS12_init(nid_p7);
339
340
2
	if (!p12)
341
		return NULL;
342
343
2
	if (!PKCS12_pack_authsafes(p12, safes)) {
344
		PKCS12_free(p12);
345
		return NULL;
346
	}
347
348
2
	return p12;
349
2
}