xref: /freebsd/crypto/openssl/test/helpers/pkcs12.c (revision e0c4386e)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert  *
4*e0c4386eSCy Schubert  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert  * this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert  * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert  * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert  */
9*e0c4386eSCy Schubert 
10*e0c4386eSCy Schubert #include <stdio.h>
11*e0c4386eSCy Schubert #include <string.h>
12*e0c4386eSCy Schubert #include <stdlib.h>
13*e0c4386eSCy Schubert 
14*e0c4386eSCy Schubert #include "internal/nelem.h"
15*e0c4386eSCy Schubert 
16*e0c4386eSCy Schubert #include <openssl/pkcs12.h>
17*e0c4386eSCy Schubert #include <openssl/x509.h>
18*e0c4386eSCy Schubert #include <openssl/x509v3.h>
19*e0c4386eSCy Schubert #include <openssl/pem.h>
20*e0c4386eSCy Schubert 
21*e0c4386eSCy Schubert #include "../testutil.h"
22*e0c4386eSCy Schubert #include "pkcs12.h" /* from the same directory */
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert /* Set this to > 0 write test data to file */
25*e0c4386eSCy Schubert static int write_files = 0;
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert static int legacy = 0;
28*e0c4386eSCy Schubert 
29*e0c4386eSCy Schubert static OSSL_LIB_CTX *test_ctx = NULL;
30*e0c4386eSCy Schubert static const char *test_propq = NULL;
31*e0c4386eSCy Schubert 
32*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
33*e0c4386eSCy Schubert  * Local function declarations
34*e0c4386eSCy Schubert  */
35*e0c4386eSCy Schubert 
36*e0c4386eSCy Schubert static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs);
37*e0c4386eSCy Schubert 
38*e0c4386eSCy Schubert static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac);
39*e0c4386eSCy Schubert static int write_p12(PKCS12 *p12, const char *outfile);
40*e0c4386eSCy Schubert 
41*e0c4386eSCy Schubert static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac);
42*e0c4386eSCy Schubert static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac);
43*e0c4386eSCy Schubert static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac);
44*e0c4386eSCy Schubert static int check_asn1_string(const ASN1_TYPE *av, const char *txt);
45*e0c4386eSCy Schubert static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs);
46*e0c4386eSCy Schubert 
47*e0c4386eSCy Schubert 
48*e0c4386eSCy Schubert /* --------------------------------------------------------------------------
49*e0c4386eSCy Schubert  * Global settings
50*e0c4386eSCy Schubert  */
51*e0c4386eSCy Schubert 
PKCS12_helper_set_write_files(int enable)52*e0c4386eSCy Schubert void PKCS12_helper_set_write_files(int enable)
53*e0c4386eSCy Schubert {
54*e0c4386eSCy Schubert     write_files = enable;
55*e0c4386eSCy Schubert }
56*e0c4386eSCy Schubert 
PKCS12_helper_set_legacy(int enable)57*e0c4386eSCy Schubert void PKCS12_helper_set_legacy(int enable)
58*e0c4386eSCy Schubert {
59*e0c4386eSCy Schubert     legacy = enable;
60*e0c4386eSCy Schubert }
61*e0c4386eSCy Schubert 
PKCS12_helper_set_libctx(OSSL_LIB_CTX * libctx)62*e0c4386eSCy Schubert void PKCS12_helper_set_libctx(OSSL_LIB_CTX *libctx)
63*e0c4386eSCy Schubert {
64*e0c4386eSCy Schubert     test_ctx = libctx;
65*e0c4386eSCy Schubert }
66*e0c4386eSCy Schubert 
PKCS12_helper_set_propq(const char * propq)67*e0c4386eSCy Schubert void PKCS12_helper_set_propq(const char *propq)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert     test_propq = propq;
70*e0c4386eSCy Schubert }
71*e0c4386eSCy Schubert 
72*e0c4386eSCy Schubert 
73*e0c4386eSCy Schubert /* --------------------------------------------------------------------------
74*e0c4386eSCy Schubert  * Test data load functions
75*e0c4386eSCy Schubert  */
76*e0c4386eSCy Schubert 
load_cert_asn1(const unsigned char * bytes,int len)77*e0c4386eSCy Schubert static X509 *load_cert_asn1(const unsigned char *bytes, int len)
78*e0c4386eSCy Schubert {
79*e0c4386eSCy Schubert     X509 *cert = NULL;
80*e0c4386eSCy Schubert 
81*e0c4386eSCy Schubert     cert = d2i_X509(NULL, &bytes, len);
82*e0c4386eSCy Schubert     if (!TEST_ptr(cert))
83*e0c4386eSCy Schubert         goto err;
84*e0c4386eSCy Schubert err:
85*e0c4386eSCy Schubert     return cert;
86*e0c4386eSCy Schubert }
87*e0c4386eSCy Schubert 
load_pkey_asn1(const unsigned char * bytes,int len)88*e0c4386eSCy Schubert static EVP_PKEY *load_pkey_asn1(const unsigned char *bytes, int len)
89*e0c4386eSCy Schubert {
90*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
91*e0c4386eSCy Schubert 
92*e0c4386eSCy Schubert     pkey = d2i_AutoPrivateKey(NULL, &bytes, len);
93*e0c4386eSCy Schubert     if (!TEST_ptr(pkey))
94*e0c4386eSCy Schubert         goto err;
95*e0c4386eSCy Schubert err:
96*e0c4386eSCy Schubert     return pkey;
97*e0c4386eSCy Schubert }
98*e0c4386eSCy Schubert 
99*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
100*e0c4386eSCy Schubert  * PKCS12 builder
101*e0c4386eSCy Schubert  */
102*e0c4386eSCy Schubert 
new_pkcs12_builder(const char * filename)103*e0c4386eSCy Schubert PKCS12_BUILDER *new_pkcs12_builder(const char *filename)
104*e0c4386eSCy Schubert {
105*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = OPENSSL_malloc(sizeof(PKCS12_BUILDER));
106*e0c4386eSCy Schubert     if (!TEST_ptr(pb))
107*e0c4386eSCy Schubert         return NULL;
108*e0c4386eSCy Schubert 
109*e0c4386eSCy Schubert     pb->filename = filename;
110*e0c4386eSCy Schubert     pb->success = 1;
111*e0c4386eSCy Schubert     return pb;
112*e0c4386eSCy Schubert }
113*e0c4386eSCy Schubert 
end_pkcs12_builder(PKCS12_BUILDER * pb)114*e0c4386eSCy Schubert int end_pkcs12_builder(PKCS12_BUILDER *pb)
115*e0c4386eSCy Schubert {
116*e0c4386eSCy Schubert     int result = pb->success;
117*e0c4386eSCy Schubert 
118*e0c4386eSCy Schubert     OPENSSL_free(pb);
119*e0c4386eSCy Schubert     return result;
120*e0c4386eSCy Schubert }
121*e0c4386eSCy Schubert 
122*e0c4386eSCy Schubert 
start_pkcs12(PKCS12_BUILDER * pb)123*e0c4386eSCy Schubert void start_pkcs12(PKCS12_BUILDER *pb)
124*e0c4386eSCy Schubert {
125*e0c4386eSCy Schubert     pb->safes = NULL;
126*e0c4386eSCy Schubert }
127*e0c4386eSCy Schubert 
128*e0c4386eSCy Schubert 
end_pkcs12(PKCS12_BUILDER * pb)129*e0c4386eSCy Schubert void end_pkcs12(PKCS12_BUILDER *pb)
130*e0c4386eSCy Schubert {
131*e0c4386eSCy Schubert     if (!pb->success)
132*e0c4386eSCy Schubert         return;
133*e0c4386eSCy Schubert     generate_p12(pb, NULL);
134*e0c4386eSCy Schubert }
135*e0c4386eSCy Schubert 
136*e0c4386eSCy Schubert 
end_pkcs12_with_mac(PKCS12_BUILDER * pb,const PKCS12_ENC * mac)137*e0c4386eSCy Schubert void end_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
138*e0c4386eSCy Schubert {
139*e0c4386eSCy Schubert     if (!pb->success)
140*e0c4386eSCy Schubert         return;
141*e0c4386eSCy Schubert     generate_p12(pb, mac);
142*e0c4386eSCy Schubert }
143*e0c4386eSCy Schubert 
144*e0c4386eSCy Schubert 
145*e0c4386eSCy Schubert /* Generate the PKCS12 encoding and write to memory bio */
generate_p12(PKCS12_BUILDER * pb,const PKCS12_ENC * mac)146*e0c4386eSCy Schubert static void generate_p12(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
147*e0c4386eSCy Schubert {
148*e0c4386eSCy Schubert     PKCS12 *p12;
149*e0c4386eSCy Schubert     EVP_MD *md = NULL;
150*e0c4386eSCy Schubert 
151*e0c4386eSCy Schubert     if (!pb->success)
152*e0c4386eSCy Schubert         return;
153*e0c4386eSCy Schubert 
154*e0c4386eSCy Schubert     pb->p12bio = BIO_new(BIO_s_mem());
155*e0c4386eSCy Schubert     if (!TEST_ptr(pb->p12bio)) {
156*e0c4386eSCy Schubert         pb->success = 0;
157*e0c4386eSCy Schubert         return;
158*e0c4386eSCy Schubert     }
159*e0c4386eSCy Schubert     if (legacy)
160*e0c4386eSCy Schubert         p12 = PKCS12_add_safes(pb->safes, 0);
161*e0c4386eSCy Schubert     else
162*e0c4386eSCy Schubert         p12 = PKCS12_add_safes_ex(pb->safes, 0, test_ctx, test_propq);
163*e0c4386eSCy Schubert     if (!TEST_ptr(p12)) {
164*e0c4386eSCy Schubert         pb->success = 0;
165*e0c4386eSCy Schubert         goto err;
166*e0c4386eSCy Schubert     }
167*e0c4386eSCy Schubert     sk_PKCS7_pop_free(pb->safes, PKCS7_free);
168*e0c4386eSCy Schubert 
169*e0c4386eSCy Schubert     if (mac != NULL) {
170*e0c4386eSCy Schubert         if (legacy)
171*e0c4386eSCy Schubert             md = (EVP_MD *)EVP_get_digestbynid(mac->nid);
172*e0c4386eSCy Schubert         else
173*e0c4386eSCy Schubert             md = EVP_MD_fetch(test_ctx, OBJ_nid2sn(mac->nid), test_propq);
174*e0c4386eSCy Schubert 
175*e0c4386eSCy Schubert         if (!TEST_true(PKCS12_set_mac(p12, mac->pass, strlen(mac->pass),
176*e0c4386eSCy Schubert                                       NULL, 0, mac->iter, md))) {
177*e0c4386eSCy Schubert             pb->success = 0;
178*e0c4386eSCy Schubert             goto err;
179*e0c4386eSCy Schubert         }
180*e0c4386eSCy Schubert     }
181*e0c4386eSCy Schubert     i2d_PKCS12_bio(pb->p12bio, p12);
182*e0c4386eSCy Schubert 
183*e0c4386eSCy Schubert     /* Can write to file here for debug */
184*e0c4386eSCy Schubert     if (write_files)
185*e0c4386eSCy Schubert         write_p12(p12, pb->filename);
186*e0c4386eSCy Schubert err:
187*e0c4386eSCy Schubert     if (!legacy && md != NULL)
188*e0c4386eSCy Schubert         EVP_MD_free(md);
189*e0c4386eSCy Schubert     PKCS12_free(p12);
190*e0c4386eSCy Schubert }
191*e0c4386eSCy Schubert 
192*e0c4386eSCy Schubert 
write_p12(PKCS12 * p12,const char * outfile)193*e0c4386eSCy Schubert static int write_p12(PKCS12 *p12, const char *outfile)
194*e0c4386eSCy Schubert {
195*e0c4386eSCy Schubert     int ret = 0;
196*e0c4386eSCy Schubert     BIO *out = BIO_new_file(outfile, "w");
197*e0c4386eSCy Schubert 
198*e0c4386eSCy Schubert     if (out == NULL)
199*e0c4386eSCy Schubert         goto err;
200*e0c4386eSCy Schubert 
201*e0c4386eSCy Schubert     if (!TEST_int_eq(i2d_PKCS12_bio(out, p12), 1))
202*e0c4386eSCy Schubert         goto err;
203*e0c4386eSCy Schubert     ret = 1;
204*e0c4386eSCy Schubert err:
205*e0c4386eSCy Schubert     BIO_free(out);
206*e0c4386eSCy Schubert     return ret;
207*e0c4386eSCy Schubert }
208*e0c4386eSCy Schubert 
from_bio_p12(BIO * bio,const PKCS12_ENC * mac)209*e0c4386eSCy Schubert static PKCS12 *from_bio_p12(BIO *bio, const PKCS12_ENC *mac)
210*e0c4386eSCy Schubert {
211*e0c4386eSCy Schubert     PKCS12 *p12 = NULL;
212*e0c4386eSCy Schubert 
213*e0c4386eSCy Schubert     /* Supply a p12 with library context/propq to the d2i decoder*/
214*e0c4386eSCy Schubert     if (!legacy) {
215*e0c4386eSCy Schubert         p12 = PKCS12_init_ex(NID_pkcs7_data, test_ctx, test_propq);
216*e0c4386eSCy Schubert         if (!TEST_ptr(p12))
217*e0c4386eSCy Schubert             goto err;
218*e0c4386eSCy Schubert     }
219*e0c4386eSCy Schubert     p12 = d2i_PKCS12_bio(bio, &p12);
220*e0c4386eSCy Schubert     BIO_free(bio);
221*e0c4386eSCy Schubert     if (!TEST_ptr(p12))
222*e0c4386eSCy Schubert         goto err;
223*e0c4386eSCy Schubert     if (mac == NULL) {
224*e0c4386eSCy Schubert         if (!TEST_false(PKCS12_mac_present(p12)))
225*e0c4386eSCy Schubert             goto err;
226*e0c4386eSCy Schubert     } else {
227*e0c4386eSCy Schubert         if (!check_p12_mac(p12, mac))
228*e0c4386eSCy Schubert             goto err;
229*e0c4386eSCy Schubert     }
230*e0c4386eSCy Schubert     return p12;
231*e0c4386eSCy Schubert err:
232*e0c4386eSCy Schubert     PKCS12_free(p12);
233*e0c4386eSCy Schubert     return NULL;
234*e0c4386eSCy Schubert }
235*e0c4386eSCy Schubert 
236*e0c4386eSCy Schubert 
237*e0c4386eSCy Schubert /* For use with existing files */
read_p12(const char * infile,const PKCS12_ENC * mac)238*e0c4386eSCy Schubert static PKCS12 *read_p12(const char *infile, const PKCS12_ENC *mac)
239*e0c4386eSCy Schubert {
240*e0c4386eSCy Schubert     PKCS12 *p12 = NULL;
241*e0c4386eSCy Schubert     BIO *in = BIO_new_file(infile, "r");
242*e0c4386eSCy Schubert 
243*e0c4386eSCy Schubert     if (in == NULL)
244*e0c4386eSCy Schubert         goto err;
245*e0c4386eSCy Schubert     p12 = d2i_PKCS12_bio(in, NULL);
246*e0c4386eSCy Schubert     BIO_free(in);
247*e0c4386eSCy Schubert     if (!TEST_ptr(p12))
248*e0c4386eSCy Schubert         goto err;
249*e0c4386eSCy Schubert     if (mac == NULL) {
250*e0c4386eSCy Schubert         if (!TEST_false(PKCS12_mac_present(p12)))
251*e0c4386eSCy Schubert             goto err;
252*e0c4386eSCy Schubert     } else {
253*e0c4386eSCy Schubert         if (!check_p12_mac(p12, mac))
254*e0c4386eSCy Schubert             goto err;
255*e0c4386eSCy Schubert     }
256*e0c4386eSCy Schubert     return p12;
257*e0c4386eSCy Schubert err:
258*e0c4386eSCy Schubert     PKCS12_free(p12);
259*e0c4386eSCy Schubert     return NULL;
260*e0c4386eSCy Schubert }
261*e0c4386eSCy Schubert 
check_p12_mac(PKCS12 * p12,const PKCS12_ENC * mac)262*e0c4386eSCy Schubert static int check_p12_mac(PKCS12 *p12, const PKCS12_ENC *mac)
263*e0c4386eSCy Schubert {
264*e0c4386eSCy Schubert     return TEST_true(PKCS12_mac_present(p12))
265*e0c4386eSCy Schubert         && TEST_true(PKCS12_verify_mac(p12, mac->pass, strlen(mac->pass)));
266*e0c4386eSCy Schubert }
267*e0c4386eSCy Schubert 
268*e0c4386eSCy Schubert 
269*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
270*e0c4386eSCy Schubert  * PKCS7 content info builder
271*e0c4386eSCy Schubert  */
272*e0c4386eSCy Schubert 
start_contentinfo(PKCS12_BUILDER * pb)273*e0c4386eSCy Schubert void start_contentinfo(PKCS12_BUILDER *pb)
274*e0c4386eSCy Schubert {
275*e0c4386eSCy Schubert     pb->bags = NULL;
276*e0c4386eSCy Schubert }
277*e0c4386eSCy Schubert 
278*e0c4386eSCy Schubert 
end_contentinfo(PKCS12_BUILDER * pb)279*e0c4386eSCy Schubert void end_contentinfo(PKCS12_BUILDER *pb)
280*e0c4386eSCy Schubert {
281*e0c4386eSCy Schubert     if (pb->success && pb->bags != NULL) {
282*e0c4386eSCy Schubert         if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, -1, 0, NULL)))
283*e0c4386eSCy Schubert             pb->success = 0;
284*e0c4386eSCy Schubert     }
285*e0c4386eSCy Schubert     sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
286*e0c4386eSCy Schubert     pb->bags = NULL;
287*e0c4386eSCy Schubert }
288*e0c4386eSCy Schubert 
289*e0c4386eSCy Schubert 
end_contentinfo_encrypted(PKCS12_BUILDER * pb,const PKCS12_ENC * enc)290*e0c4386eSCy Schubert void end_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
291*e0c4386eSCy Schubert {
292*e0c4386eSCy Schubert     if (pb->success && pb->bags != NULL) {
293*e0c4386eSCy Schubert         if (legacy) {
294*e0c4386eSCy Schubert             if (!TEST_true(PKCS12_add_safe(&pb->safes, pb->bags, enc->nid,
295*e0c4386eSCy Schubert                                            enc->iter, enc->pass)))
296*e0c4386eSCy Schubert                 pb->success = 0;
297*e0c4386eSCy Schubert         } else {
298*e0c4386eSCy Schubert             if (!TEST_true(PKCS12_add_safe_ex(&pb->safes, pb->bags, enc->nid,
299*e0c4386eSCy Schubert                                               enc->iter, enc->pass, test_ctx,
300*e0c4386eSCy Schubert                                               test_propq)))
301*e0c4386eSCy Schubert                 pb->success = 0;
302*e0c4386eSCy Schubert         }
303*e0c4386eSCy Schubert     }
304*e0c4386eSCy Schubert     sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
305*e0c4386eSCy Schubert     pb->bags = NULL;
306*e0c4386eSCy Schubert }
307*e0c4386eSCy Schubert 
308*e0c4386eSCy Schubert 
STACK_OF(PKCS12_SAFEBAG)309*e0c4386eSCy Schubert static STACK_OF(PKCS12_SAFEBAG) *decode_contentinfo(STACK_OF(PKCS7) *safes, int idx, const PKCS12_ENC *enc)
310*e0c4386eSCy Schubert {
311*e0c4386eSCy Schubert     STACK_OF(PKCS12_SAFEBAG) *bags = NULL;
312*e0c4386eSCy Schubert     int bagnid;
313*e0c4386eSCy Schubert     PKCS7 *p7 = sk_PKCS7_value(safes, idx);
314*e0c4386eSCy Schubert 
315*e0c4386eSCy Schubert     if (!TEST_ptr(p7))
316*e0c4386eSCy Schubert         goto err;
317*e0c4386eSCy Schubert 
318*e0c4386eSCy Schubert     bagnid = OBJ_obj2nid(p7->type);
319*e0c4386eSCy Schubert     if (enc) {
320*e0c4386eSCy Schubert         if (!TEST_int_eq(bagnid, NID_pkcs7_encrypted))
321*e0c4386eSCy Schubert             goto err;
322*e0c4386eSCy Schubert         bags = PKCS12_unpack_p7encdata(p7, enc->pass, strlen(enc->pass));
323*e0c4386eSCy Schubert     } else {
324*e0c4386eSCy Schubert         if (!TEST_int_eq(bagnid, NID_pkcs7_data))
325*e0c4386eSCy Schubert             goto err;
326*e0c4386eSCy Schubert         bags = PKCS12_unpack_p7data(p7);
327*e0c4386eSCy Schubert     }
328*e0c4386eSCy Schubert     if (!TEST_ptr(bags))
329*e0c4386eSCy Schubert         goto err;
330*e0c4386eSCy Schubert 
331*e0c4386eSCy Schubert     return bags;
332*e0c4386eSCy Schubert err:
333*e0c4386eSCy Schubert     return NULL;
334*e0c4386eSCy Schubert }
335*e0c4386eSCy Schubert 
336*e0c4386eSCy Schubert 
337*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
338*e0c4386eSCy Schubert  * PKCS12 safeBag/attribute builder
339*e0c4386eSCy Schubert  */
340*e0c4386eSCy Schubert 
add_attributes(PKCS12_SAFEBAG * bag,const PKCS12_ATTR * attrs)341*e0c4386eSCy Schubert static int add_attributes(PKCS12_SAFEBAG *bag, const PKCS12_ATTR *attrs)
342*e0c4386eSCy Schubert {
343*e0c4386eSCy Schubert     int ret = 0;
344*e0c4386eSCy Schubert     int attr_nid;
345*e0c4386eSCy Schubert     const PKCS12_ATTR *p_attr = attrs;
346*e0c4386eSCy Schubert 
347*e0c4386eSCy Schubert     if (attrs == NULL)
348*e0c4386eSCy Schubert         return 1;
349*e0c4386eSCy Schubert 
350*e0c4386eSCy Schubert     while (p_attr->oid != NULL) {
351*e0c4386eSCy Schubert         TEST_info("Adding attribute %s = %s", p_attr->oid, p_attr->value);
352*e0c4386eSCy Schubert         attr_nid = OBJ_txt2nid(p_attr->oid);
353*e0c4386eSCy Schubert 
354*e0c4386eSCy Schubert         if (attr_nid == NID_friendlyName) {
355*e0c4386eSCy Schubert             if (!TEST_true(PKCS12_add_friendlyname(bag, p_attr->value, -1)))
356*e0c4386eSCy Schubert                 goto err;
357*e0c4386eSCy Schubert         } else if (attr_nid == NID_localKeyID) {
358*e0c4386eSCy Schubert             if (!TEST_true(PKCS12_add_localkeyid(bag, (unsigned char *)p_attr->value,
359*e0c4386eSCy Schubert                                                  strlen(p_attr->value))))
360*e0c4386eSCy Schubert                 goto err;
361*e0c4386eSCy Schubert         } else {
362*e0c4386eSCy Schubert             /* Custom attribute values limited to ASCII in these tests */
363*e0c4386eSCy Schubert             if (!TEST_true(PKCS12_add1_attr_by_txt(bag, p_attr->oid, MBSTRING_ASC,
364*e0c4386eSCy Schubert                                                    (unsigned char *)p_attr->value,
365*e0c4386eSCy Schubert                                                    strlen(p_attr->value))))
366*e0c4386eSCy Schubert                 goto err;
367*e0c4386eSCy Schubert         }
368*e0c4386eSCy Schubert         p_attr++;
369*e0c4386eSCy Schubert     }
370*e0c4386eSCy Schubert     ret = 1;
371*e0c4386eSCy Schubert err:
372*e0c4386eSCy Schubert     return ret;
373*e0c4386eSCy Schubert }
374*e0c4386eSCy Schubert 
add_certbag(PKCS12_BUILDER * pb,const unsigned char * bytes,int len,const PKCS12_ATTR * attrs)375*e0c4386eSCy Schubert void add_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
376*e0c4386eSCy Schubert                  const PKCS12_ATTR *attrs)
377*e0c4386eSCy Schubert {
378*e0c4386eSCy Schubert     PKCS12_SAFEBAG *bag = NULL;
379*e0c4386eSCy Schubert     X509 *cert = NULL;
380*e0c4386eSCy Schubert     char *name;
381*e0c4386eSCy Schubert 
382*e0c4386eSCy Schubert     if (!pb->success)
383*e0c4386eSCy Schubert         return;
384*e0c4386eSCy Schubert 
385*e0c4386eSCy Schubert     cert = load_cert_asn1(bytes, len);
386*e0c4386eSCy Schubert     if (!TEST_ptr(cert)) {
387*e0c4386eSCy Schubert         pb->success = 0;
388*e0c4386eSCy Schubert         return;
389*e0c4386eSCy Schubert     }
390*e0c4386eSCy Schubert 
391*e0c4386eSCy Schubert     name = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
392*e0c4386eSCy Schubert     TEST_info("Adding certificate <%s>", name);
393*e0c4386eSCy Schubert     OPENSSL_free(name);
394*e0c4386eSCy Schubert 
395*e0c4386eSCy Schubert     bag = PKCS12_add_cert(&pb->bags, cert);
396*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
397*e0c4386eSCy Schubert         pb->success = 0;
398*e0c4386eSCy Schubert         goto err;
399*e0c4386eSCy Schubert     }
400*e0c4386eSCy Schubert 
401*e0c4386eSCy Schubert     if (!TEST_true(add_attributes(bag, attrs))) {
402*e0c4386eSCy Schubert         pb->success = 0;
403*e0c4386eSCy Schubert         goto err;
404*e0c4386eSCy Schubert     }
405*e0c4386eSCy Schubert err:
406*e0c4386eSCy Schubert     X509_free(cert);
407*e0c4386eSCy Schubert }
408*e0c4386eSCy Schubert 
add_keybag(PKCS12_BUILDER * pb,const unsigned char * bytes,int len,const PKCS12_ATTR * attrs,const PKCS12_ENC * enc)409*e0c4386eSCy Schubert void add_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
410*e0c4386eSCy Schubert                 const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
411*e0c4386eSCy Schubert {
412*e0c4386eSCy Schubert     PKCS12_SAFEBAG *bag = NULL;
413*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
414*e0c4386eSCy Schubert 
415*e0c4386eSCy Schubert     if (!pb->success)
416*e0c4386eSCy Schubert         return;
417*e0c4386eSCy Schubert 
418*e0c4386eSCy Schubert     TEST_info("Adding key");
419*e0c4386eSCy Schubert 
420*e0c4386eSCy Schubert     pkey = load_pkey_asn1(bytes, len);
421*e0c4386eSCy Schubert     if (!TEST_ptr(pkey)) {
422*e0c4386eSCy Schubert         pb->success = 0;
423*e0c4386eSCy Schubert         return;
424*e0c4386eSCy Schubert     }
425*e0c4386eSCy Schubert 
426*e0c4386eSCy Schubert     if (legacy)
427*e0c4386eSCy Schubert         bag = PKCS12_add_key(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass);
428*e0c4386eSCy Schubert     else
429*e0c4386eSCy Schubert         bag = PKCS12_add_key_ex(&pb->bags, pkey, 0 /*keytype*/, enc->iter, enc->nid, enc->pass,
430*e0c4386eSCy Schubert                                 test_ctx, test_propq);
431*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
432*e0c4386eSCy Schubert         pb->success = 0;
433*e0c4386eSCy Schubert         goto err;
434*e0c4386eSCy Schubert     }
435*e0c4386eSCy Schubert     if (!add_attributes(bag, attrs))
436*e0c4386eSCy Schubert         pb->success = 0;
437*e0c4386eSCy Schubert err:
438*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
439*e0c4386eSCy Schubert }
440*e0c4386eSCy Schubert 
add_secretbag(PKCS12_BUILDER * pb,int secret_nid,const char * secret,const PKCS12_ATTR * attrs)441*e0c4386eSCy Schubert void add_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret,
442*e0c4386eSCy Schubert                                const PKCS12_ATTR *attrs)
443*e0c4386eSCy Schubert {
444*e0c4386eSCy Schubert     PKCS12_SAFEBAG *bag = NULL;
445*e0c4386eSCy Schubert 
446*e0c4386eSCy Schubert     if (!pb->success)
447*e0c4386eSCy Schubert         return;
448*e0c4386eSCy Schubert 
449*e0c4386eSCy Schubert     TEST_info("Adding secret <%s>", secret);
450*e0c4386eSCy Schubert 
451*e0c4386eSCy Schubert     bag = PKCS12_add_secret(&pb->bags, secret_nid, (const unsigned char *)secret, strlen(secret));
452*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
453*e0c4386eSCy Schubert         pb->success = 0;
454*e0c4386eSCy Schubert         return;
455*e0c4386eSCy Schubert     }
456*e0c4386eSCy Schubert     if (!add_attributes(bag, attrs))
457*e0c4386eSCy Schubert         pb->success = 0;
458*e0c4386eSCy Schubert }
459*e0c4386eSCy Schubert 
460*e0c4386eSCy Schubert 
461*e0c4386eSCy Schubert /* -------------------------------------------------------------------------
462*e0c4386eSCy Schubert  * PKCS12 structure checking
463*e0c4386eSCy Schubert  */
464*e0c4386eSCy Schubert 
check_asn1_string(const ASN1_TYPE * av,const char * txt)465*e0c4386eSCy Schubert static int check_asn1_string(const ASN1_TYPE *av, const char *txt)
466*e0c4386eSCy Schubert {
467*e0c4386eSCy Schubert     int ret = 0;
468*e0c4386eSCy Schubert     char *value = NULL;
469*e0c4386eSCy Schubert 
470*e0c4386eSCy Schubert     if (!TEST_ptr(av))
471*e0c4386eSCy Schubert         goto err;
472*e0c4386eSCy Schubert 
473*e0c4386eSCy Schubert     switch (av->type) {
474*e0c4386eSCy Schubert     case V_ASN1_BMPSTRING:
475*e0c4386eSCy Schubert         value = OPENSSL_uni2asc(av->value.bmpstring->data,
476*e0c4386eSCy Schubert                                 av->value.bmpstring->length);
477*e0c4386eSCy Schubert         if (!TEST_str_eq(txt, (char *)value))
478*e0c4386eSCy Schubert             goto err;
479*e0c4386eSCy Schubert         break;
480*e0c4386eSCy Schubert 
481*e0c4386eSCy Schubert     case V_ASN1_UTF8STRING:
482*e0c4386eSCy Schubert         if (!TEST_mem_eq(txt, strlen(txt), (char *)av->value.utf8string->data,
483*e0c4386eSCy Schubert                          av->value.utf8string->length))
484*e0c4386eSCy Schubert             goto err;
485*e0c4386eSCy Schubert         break;
486*e0c4386eSCy Schubert 
487*e0c4386eSCy Schubert     case V_ASN1_OCTET_STRING:
488*e0c4386eSCy Schubert         if (!TEST_mem_eq(txt, strlen(txt),
489*e0c4386eSCy Schubert                          (char *)av->value.octet_string->data,
490*e0c4386eSCy Schubert                          av->value.octet_string->length))
491*e0c4386eSCy Schubert             goto err;
492*e0c4386eSCy Schubert         break;
493*e0c4386eSCy Schubert 
494*e0c4386eSCy Schubert     default:
495*e0c4386eSCy Schubert         /* Tests do not support other attribute types currently */
496*e0c4386eSCy Schubert         goto err;
497*e0c4386eSCy Schubert     }
498*e0c4386eSCy Schubert     ret = 1;
499*e0c4386eSCy Schubert err:
500*e0c4386eSCy Schubert     OPENSSL_free(value);
501*e0c4386eSCy Schubert     return ret;
502*e0c4386eSCy Schubert }
503*e0c4386eSCy Schubert 
check_attrs(const STACK_OF (X509_ATTRIBUTE)* bag_attrs,const PKCS12_ATTR * attrs)504*e0c4386eSCy Schubert static int check_attrs(const STACK_OF(X509_ATTRIBUTE) *bag_attrs, const PKCS12_ATTR *attrs)
505*e0c4386eSCy Schubert {
506*e0c4386eSCy Schubert     int ret = 0;
507*e0c4386eSCy Schubert     X509_ATTRIBUTE *attr;
508*e0c4386eSCy Schubert     ASN1_TYPE *av;
509*e0c4386eSCy Schubert     int i, j;
510*e0c4386eSCy Schubert     char attr_txt[100];
511*e0c4386eSCy Schubert 
512*e0c4386eSCy Schubert     for (i = 0; i < sk_X509_ATTRIBUTE_num(bag_attrs); i++) {
513*e0c4386eSCy Schubert         const PKCS12_ATTR *p_attr = attrs;
514*e0c4386eSCy Schubert         ASN1_OBJECT *attr_obj;
515*e0c4386eSCy Schubert 
516*e0c4386eSCy Schubert         attr = sk_X509_ATTRIBUTE_value(bag_attrs, i);
517*e0c4386eSCy Schubert         attr_obj = X509_ATTRIBUTE_get0_object(attr);
518*e0c4386eSCy Schubert         OBJ_obj2txt(attr_txt, 100, attr_obj, 0);
519*e0c4386eSCy Schubert 
520*e0c4386eSCy Schubert         while(p_attr->oid != NULL) {
521*e0c4386eSCy Schubert             /* Find a matching attribute type */
522*e0c4386eSCy Schubert             if (strcmp(p_attr->oid, attr_txt) == 0) {
523*e0c4386eSCy Schubert                 if (!TEST_int_eq(X509_ATTRIBUTE_count(attr), 1))
524*e0c4386eSCy Schubert                     goto err;
525*e0c4386eSCy Schubert 
526*e0c4386eSCy Schubert                 for (j = 0; j < X509_ATTRIBUTE_count(attr); j++)
527*e0c4386eSCy Schubert                 {
528*e0c4386eSCy Schubert                     av = X509_ATTRIBUTE_get0_type(attr, j);
529*e0c4386eSCy Schubert                     if (!TEST_true(check_asn1_string(av, p_attr->value)))
530*e0c4386eSCy Schubert                         goto err;
531*e0c4386eSCy Schubert                 }
532*e0c4386eSCy Schubert                 break;
533*e0c4386eSCy Schubert             }
534*e0c4386eSCy Schubert             p_attr++;
535*e0c4386eSCy Schubert         }
536*e0c4386eSCy Schubert     }
537*e0c4386eSCy Schubert     ret = 1;
538*e0c4386eSCy Schubert err:
539*e0c4386eSCy Schubert     return ret;
540*e0c4386eSCy Schubert }
541*e0c4386eSCy Schubert 
check_certbag(PKCS12_BUILDER * pb,const unsigned char * bytes,int len,const PKCS12_ATTR * attrs)542*e0c4386eSCy Schubert void check_certbag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
543*e0c4386eSCy Schubert                    const PKCS12_ATTR *attrs)
544*e0c4386eSCy Schubert {
545*e0c4386eSCy Schubert     X509 *x509 = NULL;
546*e0c4386eSCy Schubert     X509 *ref_x509 = NULL;
547*e0c4386eSCy Schubert     const PKCS12_SAFEBAG *bag;
548*e0c4386eSCy Schubert 
549*e0c4386eSCy Schubert     if (!pb->success)
550*e0c4386eSCy Schubert         return;
551*e0c4386eSCy Schubert 
552*e0c4386eSCy Schubert     bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
553*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
554*e0c4386eSCy Schubert         pb->success = 0;
555*e0c4386eSCy Schubert         return;
556*e0c4386eSCy Schubert     }
557*e0c4386eSCy Schubert     if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
558*e0c4386eSCy Schubert         || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_certBag)
559*e0c4386eSCy Schubert         || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), NID_x509Certificate)) {
560*e0c4386eSCy Schubert         pb->success = 0;
561*e0c4386eSCy Schubert         return;
562*e0c4386eSCy Schubert     }
563*e0c4386eSCy Schubert     x509 = PKCS12_SAFEBAG_get1_cert(bag);
564*e0c4386eSCy Schubert     if (!TEST_ptr(x509)) {
565*e0c4386eSCy Schubert         pb->success = 0;
566*e0c4386eSCy Schubert         goto err;
567*e0c4386eSCy Schubert     }
568*e0c4386eSCy Schubert     ref_x509 = load_cert_asn1(bytes, len);
569*e0c4386eSCy Schubert     if (!TEST_false(X509_cmp(x509, ref_x509)))
570*e0c4386eSCy Schubert         pb->success = 0;
571*e0c4386eSCy Schubert err:
572*e0c4386eSCy Schubert     X509_free(x509);
573*e0c4386eSCy Schubert     X509_free(ref_x509);
574*e0c4386eSCy Schubert }
575*e0c4386eSCy Schubert 
check_keybag(PKCS12_BUILDER * pb,const unsigned char * bytes,int len,const PKCS12_ATTR * attrs,const PKCS12_ENC * enc)576*e0c4386eSCy Schubert void check_keybag(PKCS12_BUILDER *pb, const unsigned char *bytes, int len,
577*e0c4386eSCy Schubert                   const PKCS12_ATTR *attrs, const PKCS12_ENC *enc)
578*e0c4386eSCy Schubert {
579*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
580*e0c4386eSCy Schubert     EVP_PKEY *ref_pkey = NULL;
581*e0c4386eSCy Schubert     PKCS8_PRIV_KEY_INFO *p8;
582*e0c4386eSCy Schubert     const PKCS8_PRIV_KEY_INFO *p8c;
583*e0c4386eSCy Schubert     const PKCS12_SAFEBAG *bag;
584*e0c4386eSCy Schubert 
585*e0c4386eSCy Schubert     if (!pb->success)
586*e0c4386eSCy Schubert         return;
587*e0c4386eSCy Schubert 
588*e0c4386eSCy Schubert     bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
589*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
590*e0c4386eSCy Schubert         pb->success = 0;
591*e0c4386eSCy Schubert         return;
592*e0c4386eSCy Schubert     }
593*e0c4386eSCy Schubert 
594*e0c4386eSCy Schubert     if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)) {
595*e0c4386eSCy Schubert         pb->success = 0;
596*e0c4386eSCy Schubert         return;
597*e0c4386eSCy Schubert     }
598*e0c4386eSCy Schubert 
599*e0c4386eSCy Schubert     switch (PKCS12_SAFEBAG_get_nid(bag)) {
600*e0c4386eSCy Schubert     case NID_keyBag:
601*e0c4386eSCy Schubert         p8c = PKCS12_SAFEBAG_get0_p8inf(bag);
602*e0c4386eSCy Schubert         if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8c))) {
603*e0c4386eSCy Schubert             pb->success = 0;
604*e0c4386eSCy Schubert             goto err;
605*e0c4386eSCy Schubert         }
606*e0c4386eSCy Schubert         break;
607*e0c4386eSCy Schubert 
608*e0c4386eSCy Schubert     case NID_pkcs8ShroudedKeyBag:
609*e0c4386eSCy Schubert         if (legacy)
610*e0c4386eSCy Schubert             p8 = PKCS12_decrypt_skey(bag, enc->pass, strlen(enc->pass));
611*e0c4386eSCy Schubert         else
612*e0c4386eSCy Schubert             p8 = PKCS12_decrypt_skey_ex(bag, enc->pass, strlen(enc->pass), test_ctx, test_propq);
613*e0c4386eSCy Schubert         if (!TEST_ptr(p8)) {
614*e0c4386eSCy Schubert             pb->success = 0;
615*e0c4386eSCy Schubert             goto err;
616*e0c4386eSCy Schubert         }
617*e0c4386eSCy Schubert         if (!TEST_ptr(pkey = EVP_PKCS82PKEY(p8))) {
618*e0c4386eSCy Schubert             PKCS8_PRIV_KEY_INFO_free(p8);
619*e0c4386eSCy Schubert             pb->success = 0;
620*e0c4386eSCy Schubert             goto err;
621*e0c4386eSCy Schubert         }
622*e0c4386eSCy Schubert         PKCS8_PRIV_KEY_INFO_free(p8);
623*e0c4386eSCy Schubert         break;
624*e0c4386eSCy Schubert 
625*e0c4386eSCy Schubert     default:
626*e0c4386eSCy Schubert         pb->success = 0;
627*e0c4386eSCy Schubert         goto err;
628*e0c4386eSCy Schubert     }
629*e0c4386eSCy Schubert 
630*e0c4386eSCy Schubert     /* PKEY compare returns 1 for match */
631*e0c4386eSCy Schubert     ref_pkey = load_pkey_asn1(bytes, len);
632*e0c4386eSCy Schubert     if (!TEST_true(EVP_PKEY_eq(pkey, ref_pkey)))
633*e0c4386eSCy Schubert         pb->success = 0;
634*e0c4386eSCy Schubert err:
635*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
636*e0c4386eSCy Schubert     EVP_PKEY_free(ref_pkey);
637*e0c4386eSCy Schubert }
638*e0c4386eSCy Schubert 
check_secretbag(PKCS12_BUILDER * pb,int secret_nid,const char * secret,const PKCS12_ATTR * attrs)639*e0c4386eSCy Schubert void check_secretbag(PKCS12_BUILDER *pb, int secret_nid, const char *secret, const PKCS12_ATTR *attrs)
640*e0c4386eSCy Schubert {
641*e0c4386eSCy Schubert     const PKCS12_SAFEBAG *bag;
642*e0c4386eSCy Schubert 
643*e0c4386eSCy Schubert     if (!pb->success)
644*e0c4386eSCy Schubert         return;
645*e0c4386eSCy Schubert 
646*e0c4386eSCy Schubert     bag = sk_PKCS12_SAFEBAG_value(pb->bags, pb->bag_idx++);
647*e0c4386eSCy Schubert     if (!TEST_ptr(bag)) {
648*e0c4386eSCy Schubert         pb->success = 0;
649*e0c4386eSCy Schubert         return;
650*e0c4386eSCy Schubert     }
651*e0c4386eSCy Schubert 
652*e0c4386eSCy Schubert     if (!check_attrs(PKCS12_SAFEBAG_get0_attrs(bag), attrs)
653*e0c4386eSCy Schubert         || !TEST_int_eq(PKCS12_SAFEBAG_get_nid(bag), NID_secretBag)
654*e0c4386eSCy Schubert         || !TEST_int_eq(PKCS12_SAFEBAG_get_bag_nid(bag), secret_nid)
655*e0c4386eSCy Schubert         || !TEST_true(check_asn1_string(PKCS12_SAFEBAG_get0_bag_obj(bag), secret)))
656*e0c4386eSCy Schubert         pb->success = 0;
657*e0c4386eSCy Schubert }
658*e0c4386eSCy Schubert 
659*e0c4386eSCy Schubert 
start_check_pkcs12(PKCS12_BUILDER * pb)660*e0c4386eSCy Schubert void start_check_pkcs12(PKCS12_BUILDER *pb)
661*e0c4386eSCy Schubert {
662*e0c4386eSCy Schubert     PKCS12 *p12;
663*e0c4386eSCy Schubert 
664*e0c4386eSCy Schubert     if (!pb->success)
665*e0c4386eSCy Schubert         return;
666*e0c4386eSCy Schubert 
667*e0c4386eSCy Schubert     p12 = from_bio_p12(pb->p12bio, NULL);
668*e0c4386eSCy Schubert     if (!TEST_ptr(p12)) {
669*e0c4386eSCy Schubert         pb->success = 0;
670*e0c4386eSCy Schubert         return;
671*e0c4386eSCy Schubert     }
672*e0c4386eSCy Schubert     pb->safes = PKCS12_unpack_authsafes(p12);
673*e0c4386eSCy Schubert     if (!TEST_ptr(pb->safes))
674*e0c4386eSCy Schubert         pb->success = 0;
675*e0c4386eSCy Schubert 
676*e0c4386eSCy Schubert     pb->safe_idx = 0;
677*e0c4386eSCy Schubert     PKCS12_free(p12);
678*e0c4386eSCy Schubert }
679*e0c4386eSCy Schubert 
start_check_pkcs12_with_mac(PKCS12_BUILDER * pb,const PKCS12_ENC * mac)680*e0c4386eSCy Schubert void start_check_pkcs12_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
681*e0c4386eSCy Schubert {
682*e0c4386eSCy Schubert     PKCS12 *p12;
683*e0c4386eSCy Schubert 
684*e0c4386eSCy Schubert     if (!pb->success)
685*e0c4386eSCy Schubert         return;
686*e0c4386eSCy Schubert 
687*e0c4386eSCy Schubert     p12 = from_bio_p12(pb->p12bio, mac);
688*e0c4386eSCy Schubert     if (!TEST_ptr(p12)) {
689*e0c4386eSCy Schubert         pb->success = 0;
690*e0c4386eSCy Schubert         return;
691*e0c4386eSCy Schubert     }
692*e0c4386eSCy Schubert     pb->safes = PKCS12_unpack_authsafes(p12);
693*e0c4386eSCy Schubert     if (!TEST_ptr(pb->safes))
694*e0c4386eSCy Schubert         pb->success = 0;
695*e0c4386eSCy Schubert 
696*e0c4386eSCy Schubert     pb->safe_idx = 0;
697*e0c4386eSCy Schubert     PKCS12_free(p12);
698*e0c4386eSCy Schubert }
699*e0c4386eSCy Schubert 
start_check_pkcs12_file(PKCS12_BUILDER * pb)700*e0c4386eSCy Schubert void start_check_pkcs12_file(PKCS12_BUILDER *pb)
701*e0c4386eSCy Schubert {
702*e0c4386eSCy Schubert     PKCS12 *p12;
703*e0c4386eSCy Schubert 
704*e0c4386eSCy Schubert     if (!pb->success)
705*e0c4386eSCy Schubert         return;
706*e0c4386eSCy Schubert 
707*e0c4386eSCy Schubert     p12 = read_p12(pb->filename, NULL);
708*e0c4386eSCy Schubert     if (!TEST_ptr(p12)) {
709*e0c4386eSCy Schubert         pb->success = 0;
710*e0c4386eSCy Schubert         return;
711*e0c4386eSCy Schubert     }
712*e0c4386eSCy Schubert     pb->safes = PKCS12_unpack_authsafes(p12);
713*e0c4386eSCy Schubert     if (!TEST_ptr(pb->safes))
714*e0c4386eSCy Schubert         pb->success = 0;
715*e0c4386eSCy Schubert 
716*e0c4386eSCy Schubert     pb->safe_idx = 0;
717*e0c4386eSCy Schubert     PKCS12_free(p12);
718*e0c4386eSCy Schubert }
719*e0c4386eSCy Schubert 
start_check_pkcs12_file_with_mac(PKCS12_BUILDER * pb,const PKCS12_ENC * mac)720*e0c4386eSCy Schubert void start_check_pkcs12_file_with_mac(PKCS12_BUILDER *pb, const PKCS12_ENC *mac)
721*e0c4386eSCy Schubert {
722*e0c4386eSCy Schubert     PKCS12 *p12;
723*e0c4386eSCy Schubert 
724*e0c4386eSCy Schubert     if (!pb->success)
725*e0c4386eSCy Schubert         return;
726*e0c4386eSCy Schubert 
727*e0c4386eSCy Schubert     p12 = read_p12(pb->filename, mac);
728*e0c4386eSCy Schubert     if (!TEST_ptr(p12)) {
729*e0c4386eSCy Schubert         pb->success = 0;
730*e0c4386eSCy Schubert         return;
731*e0c4386eSCy Schubert     }
732*e0c4386eSCy Schubert     pb->safes = PKCS12_unpack_authsafes(p12);
733*e0c4386eSCy Schubert     if (!TEST_ptr(pb->safes))
734*e0c4386eSCy Schubert         pb->success = 0;
735*e0c4386eSCy Schubert 
736*e0c4386eSCy Schubert     pb->safe_idx = 0;
737*e0c4386eSCy Schubert     PKCS12_free(p12);
738*e0c4386eSCy Schubert }
739*e0c4386eSCy Schubert 
end_check_pkcs12(PKCS12_BUILDER * pb)740*e0c4386eSCy Schubert void end_check_pkcs12(PKCS12_BUILDER *pb)
741*e0c4386eSCy Schubert {
742*e0c4386eSCy Schubert     if (!pb->success)
743*e0c4386eSCy Schubert         return;
744*e0c4386eSCy Schubert 
745*e0c4386eSCy Schubert     sk_PKCS7_pop_free(pb->safes, PKCS7_free);
746*e0c4386eSCy Schubert }
747*e0c4386eSCy Schubert 
748*e0c4386eSCy Schubert 
start_check_contentinfo(PKCS12_BUILDER * pb)749*e0c4386eSCy Schubert void start_check_contentinfo(PKCS12_BUILDER *pb)
750*e0c4386eSCy Schubert {
751*e0c4386eSCy Schubert     if (!pb->success)
752*e0c4386eSCy Schubert         return;
753*e0c4386eSCy Schubert 
754*e0c4386eSCy Schubert     pb->bag_idx = 0;
755*e0c4386eSCy Schubert     pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, NULL);
756*e0c4386eSCy Schubert     if (!TEST_ptr(pb->bags)) {
757*e0c4386eSCy Schubert         pb->success = 0;
758*e0c4386eSCy Schubert         return;
759*e0c4386eSCy Schubert     }
760*e0c4386eSCy Schubert     TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
761*e0c4386eSCy Schubert }
762*e0c4386eSCy Schubert 
start_check_contentinfo_encrypted(PKCS12_BUILDER * pb,const PKCS12_ENC * enc)763*e0c4386eSCy Schubert void start_check_contentinfo_encrypted(PKCS12_BUILDER *pb, const PKCS12_ENC *enc)
764*e0c4386eSCy Schubert {
765*e0c4386eSCy Schubert     if (!pb->success)
766*e0c4386eSCy Schubert         return;
767*e0c4386eSCy Schubert 
768*e0c4386eSCy Schubert     pb->bag_idx = 0;
769*e0c4386eSCy Schubert     pb->bags = decode_contentinfo(pb->safes, pb->safe_idx++, enc);
770*e0c4386eSCy Schubert     if (!TEST_ptr(pb->bags)) {
771*e0c4386eSCy Schubert         pb->success = 0;
772*e0c4386eSCy Schubert         return;
773*e0c4386eSCy Schubert     }
774*e0c4386eSCy Schubert     TEST_info("Decoding %d bags", sk_PKCS12_SAFEBAG_num(pb->bags));
775*e0c4386eSCy Schubert }
776*e0c4386eSCy Schubert 
777*e0c4386eSCy Schubert 
end_check_contentinfo(PKCS12_BUILDER * pb)778*e0c4386eSCy Schubert void end_check_contentinfo(PKCS12_BUILDER *pb)
779*e0c4386eSCy Schubert {
780*e0c4386eSCy Schubert     if (!pb->success)
781*e0c4386eSCy Schubert         return;
782*e0c4386eSCy Schubert 
783*e0c4386eSCy Schubert     if (!TEST_int_eq(sk_PKCS12_SAFEBAG_num(pb->bags), pb->bag_idx))
784*e0c4386eSCy Schubert         pb->success = 0;
785*e0c4386eSCy Schubert     sk_PKCS12_SAFEBAG_pop_free(pb->bags, PKCS12_SAFEBAG_free);
786*e0c4386eSCy Schubert     pb->bags = NULL;
787*e0c4386eSCy Schubert }
788*e0c4386eSCy Schubert 
789*e0c4386eSCy Schubert 
790