1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2020-2023 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 "helpers/pkcs12.h"
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert static int default_libctx = 1;
25*e0c4386eSCy Schubert 
26*e0c4386eSCy Schubert static OSSL_LIB_CTX *testctx = NULL;
27*e0c4386eSCy Schubert static OSSL_PROVIDER *nullprov = NULL;
28*e0c4386eSCy Schubert static OSSL_PROVIDER *deflprov = NULL;
29*e0c4386eSCy Schubert static OSSL_PROVIDER *lgcyprov = NULL;
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert /* --------------------------------------------------------------------------
32*e0c4386eSCy Schubert  * PKCS12 component test data
33*e0c4386eSCy Schubert  */
34*e0c4386eSCy Schubert 
35*e0c4386eSCy Schubert static const unsigned char CERT1[] =
36*e0c4386eSCy Schubert {
37*e0c4386eSCy Schubert     0x30, 0x82, 0x01, 0xed, 0x30, 0x82, 0x01, 0x56, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
38*e0c4386eSCy Schubert     0x8b, 0x4b, 0x5e, 0x6c, 0x03, 0x28, 0x4e, 0xe6, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
39*e0c4386eSCy Schubert     0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x19, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
40*e0c4386eSCy Schubert     0x04, 0x03, 0x0c, 0x0e, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x52, 0x6f, 0x6f, 0x74,
41*e0c4386eSCy Schubert     0x2d, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x39, 0x33, 0x30, 0x30, 0x30, 0x34, 0x36,
42*e0c4386eSCy Schubert     0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x39, 0x30, 0x39, 0x32, 0x37, 0x30, 0x30, 0x34, 0x36, 0x35,
43*e0c4386eSCy Schubert     0x36, 0x5a, 0x30, 0x1b, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x10, 0x50,
44*e0c4386eSCy Schubert     0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2d, 0x31, 0x30,
45*e0c4386eSCy Schubert     0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
46*e0c4386eSCy Schubert     0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xbc, 0xdc, 0x6f, 0x8c,
47*e0c4386eSCy Schubert     0x7a, 0x2a, 0x4b, 0xea, 0x66, 0x66, 0x04, 0xa9, 0x05, 0x92, 0x53, 0xd7, 0x13, 0x3c, 0x49, 0xe1,
48*e0c4386eSCy Schubert     0xc8, 0xbb, 0xdf, 0x3d, 0xcb, 0x88, 0x31, 0x07, 0x20, 0x59, 0x93, 0x24, 0x7f, 0x7d, 0xc6, 0x84,
49*e0c4386eSCy Schubert     0x81, 0x16, 0x64, 0x4a, 0x52, 0xa6, 0x30, 0x44, 0xdc, 0x1a, 0x30, 0xde, 0xae, 0x29, 0x18, 0xcf,
50*e0c4386eSCy Schubert     0xc7, 0xf3, 0xcf, 0x0c, 0xb7, 0x8e, 0x2b, 0x1e, 0x21, 0x01, 0x0b, 0xfb, 0xe5, 0xe6, 0xcf, 0x2b,
51*e0c4386eSCy Schubert     0x84, 0xe1, 0x33, 0xf8, 0xba, 0x02, 0xfc, 0x30, 0xfa, 0xc4, 0x33, 0xc7, 0x37, 0xc6, 0x7f, 0x72,
52*e0c4386eSCy Schubert     0x31, 0x92, 0x1d, 0x8f, 0xa0, 0xfb, 0xe5, 0x4a, 0x08, 0x31, 0x78, 0x80, 0x9c, 0x23, 0xb4, 0xe9,
53*e0c4386eSCy Schubert     0x19, 0x56, 0x04, 0xfa, 0x0d, 0x07, 0x04, 0xb7, 0x43, 0xac, 0x4c, 0x49, 0x7c, 0xc2, 0xa1, 0x44,
54*e0c4386eSCy Schubert     0xc1, 0x48, 0x7d, 0x28, 0xe5, 0x23, 0x66, 0x07, 0x22, 0xd5, 0xf0, 0xf1, 0x02, 0x03, 0x01, 0x00,
55*e0c4386eSCy Schubert     0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16,
56*e0c4386eSCy Schubert     0x80, 0x14, 0xdb, 0xbb, 0xb8, 0x92, 0x4e, 0x24, 0x0b, 0x1b, 0xbb, 0x78, 0x33, 0xf9, 0x01, 0x02,
57*e0c4386eSCy Schubert     0x23, 0x0d, 0x96, 0x18, 0x30, 0x47, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30,
58*e0c4386eSCy Schubert     0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x0d,
59*e0c4386eSCy Schubert     0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81,
60*e0c4386eSCy Schubert     0x00, 0x1c, 0x13, 0xdc, 0x02, 0xf1, 0x44, 0x36, 0x65, 0xa9, 0xbe, 0x30, 0x1c, 0x66, 0x14, 0x20,
61*e0c4386eSCy Schubert     0x86, 0x5a, 0xa8, 0x69, 0x25, 0xf8, 0x1a, 0xb6, 0x9e, 0x5e, 0xe9, 0x89, 0xb8, 0x67, 0x70, 0x19,
62*e0c4386eSCy Schubert     0x87, 0x60, 0xeb, 0x4b, 0x11, 0x71, 0x85, 0xf8, 0xe9, 0xa7, 0x3e, 0x20, 0x42, 0xec, 0x43, 0x25,
63*e0c4386eSCy Schubert     0x01, 0x03, 0xe5, 0x4d, 0x83, 0x22, 0xf5, 0x8e, 0x3a, 0x1a, 0x1b, 0xd4, 0x1c, 0xda, 0x6b, 0x9d,
64*e0c4386eSCy Schubert     0x10, 0x1b, 0xee, 0x67, 0x4e, 0x1f, 0x69, 0xab, 0xbc, 0xaa, 0x62, 0x8e, 0x9e, 0xc6, 0xee, 0xd6,
65*e0c4386eSCy Schubert     0x09, 0xc0, 0xca, 0xe0, 0xaa, 0x9f, 0x07, 0xb2, 0xc2, 0xbb, 0x31, 0x96, 0xa2, 0x04, 0x62, 0xd3,
66*e0c4386eSCy Schubert     0x13, 0x32, 0x29, 0x67, 0x6e, 0xad, 0x2e, 0x0b, 0xea, 0x04, 0x7c, 0x8c, 0x5a, 0x5d, 0xac, 0x14,
67*e0c4386eSCy Schubert     0xaa, 0x61, 0x7f, 0x28, 0x6c, 0x2d, 0x64, 0x2d, 0xc3, 0xaf, 0x77, 0x52, 0x90, 0xb4, 0x37, 0xc0,
68*e0c4386eSCy Schubert     0x30,
69*e0c4386eSCy Schubert };
70*e0c4386eSCy Schubert 
71*e0c4386eSCy Schubert static const unsigned char CERT2[] =
72*e0c4386eSCy Schubert {
73*e0c4386eSCy Schubert     0x30, 0x82, 0x01, 0xed, 0x30, 0x82, 0x01, 0x56, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
74*e0c4386eSCy Schubert     0x8b, 0x4b, 0x5e, 0x6c, 0x03, 0x28, 0x4e, 0xe7, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
75*e0c4386eSCy Schubert     0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x19, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
76*e0c4386eSCy Schubert     0x04, 0x03, 0x0c, 0x0e, 0x50, 0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x52, 0x6f, 0x6f, 0x74,
77*e0c4386eSCy Schubert     0x2d, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x39, 0x33, 0x30, 0x30, 0x30, 0x34, 0x36,
78*e0c4386eSCy Schubert     0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x39, 0x30, 0x39, 0x32, 0x37, 0x30, 0x30, 0x34, 0x36, 0x35,
79*e0c4386eSCy Schubert     0x36, 0x5a, 0x30, 0x1b, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x10, 0x50,
80*e0c4386eSCy Schubert     0x31, 0x32, 0x54, 0x65, 0x73, 0x74, 0x2d, 0x43, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x2d, 0x31, 0x30,
81*e0c4386eSCy Schubert     0x81, 0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
82*e0c4386eSCy Schubert     0x00, 0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xa8, 0x6e, 0x40, 0x86,
83*e0c4386eSCy Schubert     0x9f, 0x98, 0x59, 0xfb, 0x57, 0xbf, 0xc1, 0x55, 0x12, 0x38, 0xeb, 0xb3, 0x46, 0x34, 0xc9, 0x35,
84*e0c4386eSCy Schubert     0x4d, 0xfd, 0x03, 0xe9, 0x3a, 0x88, 0x9e, 0x97, 0x8f, 0xf4, 0xec, 0x36, 0x7b, 0x3f, 0xba, 0xb8,
85*e0c4386eSCy Schubert     0xa5, 0x96, 0x30, 0x03, 0xc5, 0xc6, 0xd9, 0xa8, 0x4e, 0xbc, 0x23, 0x51, 0xa1, 0x96, 0xd2, 0x03,
86*e0c4386eSCy Schubert     0x98, 0x73, 0xb6, 0x17, 0x9c, 0x77, 0xd4, 0x95, 0x1e, 0x1b, 0xb3, 0x1b, 0xc8, 0x71, 0xd1, 0x2e,
87*e0c4386eSCy Schubert     0x31, 0xc7, 0x6a, 0x75, 0x57, 0x08, 0x7f, 0xba, 0x70, 0x76, 0xf7, 0x67, 0xf4, 0x4e, 0xbe, 0xfc,
88*e0c4386eSCy Schubert     0x70, 0x61, 0x41, 0x07, 0x2b, 0x7c, 0x3c, 0x3b, 0xb3, 0xbc, 0xd5, 0xa8, 0xbd, 0x28, 0xd8, 0x49,
89*e0c4386eSCy Schubert     0xd3, 0xe1, 0x78, 0xc8, 0xc1, 0x42, 0x5e, 0x18, 0x36, 0xa8, 0x41, 0xf7, 0xc8, 0xaa, 0x35, 0xfe,
90*e0c4386eSCy Schubert     0x2d, 0xd1, 0xb4, 0xcc, 0x00, 0x67, 0xae, 0x79, 0xd3, 0x28, 0xd5, 0x5b, 0x02, 0x03, 0x01, 0x00,
91*e0c4386eSCy Schubert     0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16,
92*e0c4386eSCy Schubert     0x80, 0x14, 0xdb, 0xbb, 0xb8, 0x92, 0x4e, 0x24, 0x0b, 0x1b, 0xbb, 0x78, 0x33, 0xf9, 0x01, 0x02,
93*e0c4386eSCy Schubert     0x23, 0x0d, 0x96, 0x18, 0x30, 0x47, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30,
94*e0c4386eSCy Schubert     0x00, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x04, 0xf0, 0x30, 0x0d,
95*e0c4386eSCy Schubert     0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x81, 0x81,
96*e0c4386eSCy Schubert     0x00, 0x3b, 0xa6, 0x73, 0xbe, 0xe0, 0x28, 0xed, 0x1f, 0x29, 0x78, 0x4c, 0xc0, 0x1f, 0xe9, 0x85,
97*e0c4386eSCy Schubert     0xc6, 0x8f, 0xe3, 0x87, 0x7c, 0xd9, 0xe7, 0x0a, 0x37, 0xe8, 0xaa, 0xb5, 0xd2, 0x7f, 0xf8, 0x90,
98*e0c4386eSCy Schubert     0x20, 0x80, 0x35, 0xa7, 0x79, 0x2b, 0x04, 0xa7, 0xbf, 0xe6, 0x7b, 0x58, 0xcb, 0xec, 0x0e, 0x58,
99*e0c4386eSCy Schubert     0xef, 0x2a, 0x70, 0x8a, 0x56, 0x8a, 0xcf, 0x6b, 0x7a, 0x74, 0x0c, 0xf4, 0x15, 0x37, 0x93, 0xcd,
100*e0c4386eSCy Schubert     0xe6, 0xb2, 0xa1, 0x83, 0x09, 0xdb, 0x9e, 0x4f, 0xff, 0x6a, 0x17, 0x4f, 0x33, 0xc9, 0xcc, 0x90,
101*e0c4386eSCy Schubert     0x2a, 0x67, 0xff, 0x16, 0x78, 0xa8, 0x2c, 0x10, 0xe0, 0x52, 0x8c, 0xe6, 0xe9, 0x90, 0x8d, 0xe0,
102*e0c4386eSCy Schubert     0x62, 0x04, 0x9a, 0x0f, 0x44, 0x01, 0x82, 0x14, 0x92, 0x44, 0x25, 0x69, 0x22, 0xb7, 0xb8, 0xc5,
103*e0c4386eSCy Schubert     0x94, 0x4c, 0x4b, 0x1c, 0x9b, 0x92, 0x60, 0x66, 0x90, 0x4e, 0xb9, 0xa8, 0x4c, 0x89, 0xbb, 0x0f,
104*e0c4386eSCy Schubert     0x0b,
105*e0c4386eSCy Schubert };
106*e0c4386eSCy Schubert 
107*e0c4386eSCy Schubert static const unsigned char KEY1[] =
108*e0c4386eSCy Schubert {
109*e0c4386eSCy Schubert     0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbc, 0xdc, 0x6f, 0x8c, 0x7a,
110*e0c4386eSCy Schubert     0x2a, 0x4b, 0xea, 0x66, 0x66, 0x04, 0xa9, 0x05, 0x92, 0x53, 0xd7, 0x13, 0x3c, 0x49, 0xe1, 0xc8,
111*e0c4386eSCy Schubert     0xbb, 0xdf, 0x3d, 0xcb, 0x88, 0x31, 0x07, 0x20, 0x59, 0x93, 0x24, 0x7f, 0x7d, 0xc6, 0x84, 0x81,
112*e0c4386eSCy Schubert     0x16, 0x64, 0x4a, 0x52, 0xa6, 0x30, 0x44, 0xdc, 0x1a, 0x30, 0xde, 0xae, 0x29, 0x18, 0xcf, 0xc7,
113*e0c4386eSCy Schubert     0xf3, 0xcf, 0x0c, 0xb7, 0x8e, 0x2b, 0x1e, 0x21, 0x01, 0x0b, 0xfb, 0xe5, 0xe6, 0xcf, 0x2b, 0x84,
114*e0c4386eSCy Schubert     0xe1, 0x33, 0xf8, 0xba, 0x02, 0xfc, 0x30, 0xfa, 0xc4, 0x33, 0xc7, 0x37, 0xc6, 0x7f, 0x72, 0x31,
115*e0c4386eSCy Schubert     0x92, 0x1d, 0x8f, 0xa0, 0xfb, 0xe5, 0x4a, 0x08, 0x31, 0x78, 0x80, 0x9c, 0x23, 0xb4, 0xe9, 0x19,
116*e0c4386eSCy Schubert     0x56, 0x04, 0xfa, 0x0d, 0x07, 0x04, 0xb7, 0x43, 0xac, 0x4c, 0x49, 0x7c, 0xc2, 0xa1, 0x44, 0xc1,
117*e0c4386eSCy Schubert     0x48, 0x7d, 0x28, 0xe5, 0x23, 0x66, 0x07, 0x22, 0xd5, 0xf0, 0xf1, 0x02, 0x03, 0x01, 0x00, 0x01,
118*e0c4386eSCy Schubert     0x02, 0x81, 0x81, 0x00, 0xa5, 0x6d, 0xf9, 0x8f, 0xf5, 0x5a, 0xa3, 0x50, 0xd9, 0x0d, 0x37, 0xbb,
119*e0c4386eSCy Schubert     0xce, 0x13, 0x94, 0xb8, 0xea, 0x32, 0x7f, 0x0c, 0xf5, 0x46, 0x0b, 0x90, 0x17, 0x7e, 0x5e, 0x63,
120*e0c4386eSCy Schubert     0xbd, 0xa4, 0x78, 0xcd, 0x19, 0x97, 0xd4, 0x92, 0x30, 0x78, 0xaa, 0xb4, 0xa7, 0x9c, 0xc6, 0xdf,
121*e0c4386eSCy Schubert     0x2a, 0x65, 0x0e, 0xb5, 0x9f, 0x9c, 0x84, 0x0d, 0x4d, 0x3a, 0x74, 0xfc, 0xd0, 0xb4, 0x09, 0x74,
122*e0c4386eSCy Schubert     0xc4, 0xb8, 0x24, 0x03, 0xa8, 0xf0, 0xf8, 0x0d, 0x5c, 0x8e, 0xdf, 0x4b, 0xe1, 0x0a, 0x8f, 0x4f,
123*e0c4386eSCy Schubert     0xd5, 0xc7, 0x9b, 0x54, 0x55, 0x8f, 0x00, 0x5c, 0xea, 0x4c, 0x73, 0xf9, 0x1b, 0xbf, 0xb8, 0x93,
124*e0c4386eSCy Schubert     0x33, 0x20, 0xce, 0x45, 0xd9, 0x03, 0x02, 0xb2, 0x36, 0xc5, 0x0a, 0x30, 0x50, 0x78, 0x80, 0x66,
125*e0c4386eSCy Schubert     0x00, 0x22, 0x38, 0x86, 0xcf, 0x63, 0x4a, 0x5c, 0xbf, 0x2b, 0xd9, 0x6e, 0xe6, 0xf0, 0x39, 0xad,
126*e0c4386eSCy Schubert     0x12, 0x25, 0x41, 0xb9, 0x02, 0x41, 0x00, 0xf3, 0x7c, 0x07, 0x99, 0x64, 0x3a, 0x28, 0x8c, 0x8d,
127*e0c4386eSCy Schubert     0x05, 0xfe, 0x32, 0xb5, 0x4c, 0x8c, 0x6d, 0xde, 0x3d, 0x16, 0x08, 0xa0, 0x01, 0x61, 0x4f, 0x8e,
128*e0c4386eSCy Schubert     0xa0, 0xf7, 0x26, 0x26, 0xb5, 0x8e, 0xc0, 0x7a, 0xce, 0x86, 0x34, 0xde, 0xb8, 0xef, 0x86, 0x01,
129*e0c4386eSCy Schubert     0xbe, 0x24, 0xaa, 0x9b, 0x36, 0x93, 0x72, 0x9b, 0xf9, 0xc6, 0xcb, 0x76, 0x84, 0x67, 0x06, 0x06,
130*e0c4386eSCy Schubert     0x30, 0x50, 0xdf, 0x42, 0x17, 0xe0, 0xa7, 0x02, 0x41, 0x00, 0xc6, 0x91, 0xa0, 0x41, 0x34, 0x11,
131*e0c4386eSCy Schubert     0x67, 0x4b, 0x08, 0x0f, 0xda, 0xa7, 0x99, 0xec, 0x58, 0x11, 0xa5, 0x82, 0xdb, 0x50, 0xfe, 0x77,
132*e0c4386eSCy Schubert     0xe2, 0xd1, 0x53, 0x9c, 0x7d, 0xe8, 0xbf, 0xe7, 0x7c, 0xa9, 0x01, 0xb1, 0x87, 0xc3, 0x52, 0x79,
133*e0c4386eSCy Schubert     0x9e, 0x2c, 0xa7, 0x6f, 0x02, 0x37, 0x32, 0xef, 0x24, 0x31, 0x21, 0x0b, 0x86, 0x05, 0x32, 0x4a,
134*e0c4386eSCy Schubert     0x2e, 0x0b, 0x65, 0x05, 0xd3, 0xd6, 0x30, 0xb2, 0xfc, 0xa7, 0x02, 0x41, 0x00, 0xc2, 0xed, 0x31,
135*e0c4386eSCy Schubert     0xdc, 0x40, 0x9c, 0x3a, 0xe8, 0x42, 0xe2, 0x60, 0x5e, 0x52, 0x3c, 0xc5, 0x54, 0x14, 0x0e, 0x8d,
136*e0c4386eSCy Schubert     0x7c, 0x3c, 0x34, 0xbe, 0xa6, 0x05, 0x86, 0xa2, 0x36, 0x5d, 0xd9, 0x0e, 0x3e, 0xd4, 0x52, 0x50,
137*e0c4386eSCy Schubert     0xa9, 0x35, 0x01, 0x93, 0x68, 0x92, 0x2e, 0x9a, 0x86, 0x27, 0x1a, 0xab, 0x32, 0x9e, 0xe2, 0x79,
138*e0c4386eSCy Schubert     0x9f, 0x5b, 0xf3, 0xa5, 0xd2, 0xf1, 0xd3, 0x6e, 0x7b, 0x3e, 0x1b, 0x85, 0x93, 0x02, 0x40, 0x68,
139*e0c4386eSCy Schubert     0xb8, 0xb6, 0x7e, 0x8c, 0xba, 0x3c, 0xf2, 0x8a, 0x2e, 0xea, 0x4f, 0x07, 0xd3, 0x68, 0x62, 0xee,
140*e0c4386eSCy Schubert     0x1a, 0x04, 0x16, 0x44, 0x0d, 0xef, 0xf6, 0x1b, 0x95, 0x65, 0xa5, 0xd1, 0x47, 0x81, 0x2c, 0x14,
141*e0c4386eSCy Schubert     0xb3, 0x8e, 0xf9, 0x08, 0xcf, 0x11, 0x07, 0x55, 0xca, 0x2a, 0xad, 0xf7, 0xd3, 0xbd, 0x0f, 0x97,
142*e0c4386eSCy Schubert     0xf0, 0xde, 0xde, 0x70, 0xb6, 0x44, 0x70, 0x47, 0xf7, 0xf9, 0xcf, 0x75, 0x61, 0x7f, 0xf3, 0x02,
143*e0c4386eSCy Schubert     0x40, 0x38, 0x4a, 0x67, 0xaf, 0xae, 0xb6, 0xb2, 0x6a, 0x00, 0x25, 0x5a, 0xa4, 0x65, 0x20, 0xb1,
144*e0c4386eSCy Schubert     0x13, 0xbd, 0x83, 0xff, 0xb4, 0xbc, 0xf4, 0xdd, 0xa1, 0xbb, 0x1c, 0x96, 0x37, 0x35, 0xf4, 0xbf,
145*e0c4386eSCy Schubert     0xed, 0x4c, 0xed, 0x92, 0xe8, 0xac, 0xc9, 0xc1, 0xa5, 0xa3, 0x23, 0x66, 0x40, 0x8a, 0xa1, 0xe6,
146*e0c4386eSCy Schubert     0xe3, 0x95, 0xfe, 0xc4, 0x53, 0xf5, 0x7d, 0x6e, 0xca, 0x45, 0x42, 0xe4, 0xc2, 0x9f, 0xe5, 0x1e,
147*e0c4386eSCy Schubert     0xb5,
148*e0c4386eSCy Schubert };
149*e0c4386eSCy Schubert 
150*e0c4386eSCy Schubert 
151*e0c4386eSCy Schubert static const unsigned char KEY2[] =
152*e0c4386eSCy Schubert {
153*e0c4386eSCy Schubert     0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xa8, 0x6e, 0x40, 0x86, 0x9f,
154*e0c4386eSCy Schubert     0x98, 0x59, 0xfb, 0x57, 0xbf, 0xc1, 0x55, 0x12, 0x38, 0xeb, 0xb3, 0x46, 0x34, 0xc9, 0x35, 0x4d,
155*e0c4386eSCy Schubert     0xfd, 0x03, 0xe9, 0x3a, 0x88, 0x9e, 0x97, 0x8f, 0xf4, 0xec, 0x36, 0x7b, 0x3f, 0xba, 0xb8, 0xa5,
156*e0c4386eSCy Schubert     0x96, 0x30, 0x03, 0xc5, 0xc6, 0xd9, 0xa8, 0x4e, 0xbc, 0x23, 0x51, 0xa1, 0x96, 0xd2, 0x03, 0x98,
157*e0c4386eSCy Schubert     0x73, 0xb6, 0x17, 0x9c, 0x77, 0xd4, 0x95, 0x1e, 0x1b, 0xb3, 0x1b, 0xc8, 0x71, 0xd1, 0x2e, 0x31,
158*e0c4386eSCy Schubert     0xc7, 0x6a, 0x75, 0x57, 0x08, 0x7f, 0xba, 0x70, 0x76, 0xf7, 0x67, 0xf4, 0x4e, 0xbe, 0xfc, 0x70,
159*e0c4386eSCy Schubert     0x61, 0x41, 0x07, 0x2b, 0x7c, 0x3c, 0x3b, 0xb3, 0xbc, 0xd5, 0xa8, 0xbd, 0x28, 0xd8, 0x49, 0xd3,
160*e0c4386eSCy Schubert     0xe1, 0x78, 0xc8, 0xc1, 0x42, 0x5e, 0x18, 0x36, 0xa8, 0x41, 0xf7, 0xc8, 0xaa, 0x35, 0xfe, 0x2d,
161*e0c4386eSCy Schubert     0xd1, 0xb4, 0xcc, 0x00, 0x67, 0xae, 0x79, 0xd3, 0x28, 0xd5, 0x5b, 0x02, 0x03, 0x01, 0x00, 0x01,
162*e0c4386eSCy Schubert     0x02, 0x81, 0x81, 0x00, 0xa6, 0x00, 0x83, 0xf8, 0x2b, 0x33, 0xac, 0xfb, 0xdb, 0xf0, 0x52, 0x4b,
163*e0c4386eSCy Schubert     0xd6, 0x39, 0xe3, 0x94, 0x3d, 0x8d, 0xa9, 0x01, 0xb0, 0x6b, 0xbe, 0x7f, 0x10, 0x01, 0xb6, 0xcd,
164*e0c4386eSCy Schubert     0x0a, 0x45, 0x0a, 0xca, 0x67, 0x8e, 0xd8, 0x29, 0x44, 0x8a, 0x51, 0xa8, 0x66, 0x35, 0x26, 0x30,
165*e0c4386eSCy Schubert     0x8b, 0xe9, 0x41, 0xa6, 0x22, 0xec, 0xd2, 0xf0, 0x58, 0x41, 0x33, 0x26, 0xf2, 0x3f, 0xe8, 0x75,
166*e0c4386eSCy Schubert     0x4f, 0xc7, 0x5d, 0x2e, 0x5a, 0xa8, 0x7a, 0xd2, 0xbf, 0x59, 0xa0, 0x86, 0x79, 0x0b, 0x92, 0x6c,
167*e0c4386eSCy Schubert     0x95, 0x5d, 0x87, 0x63, 0x5c, 0xd6, 0x1a, 0xc0, 0xf6, 0x7a, 0x15, 0x8d, 0xc7, 0x3c, 0xb6, 0x9e,
168*e0c4386eSCy Schubert     0xa6, 0x58, 0x46, 0x9b, 0xbf, 0x3e, 0x28, 0x8c, 0xdf, 0x1a, 0x87, 0xaa, 0x7e, 0xf5, 0xf2, 0xcb,
169*e0c4386eSCy Schubert     0x5e, 0x84, 0x2d, 0xf6, 0x82, 0x7e, 0x89, 0x4e, 0xf5, 0xe6, 0x3c, 0x92, 0x80, 0x1e, 0x98, 0x1c,
170*e0c4386eSCy Schubert     0x6a, 0x7b, 0x57, 0x01, 0x02, 0x41, 0x00, 0xdd, 0x60, 0x95, 0xd7, 0xa1, 0x9d, 0x0c, 0xa1, 0x84,
171*e0c4386eSCy Schubert     0xc5, 0x39, 0xca, 0x67, 0x4c, 0x1c, 0x06, 0x71, 0x5b, 0x5c, 0x2d, 0x8d, 0xce, 0xcd, 0xe2, 0x79,
172*e0c4386eSCy Schubert     0xc8, 0x33, 0xbe, 0x50, 0x37, 0x60, 0x9f, 0x3b, 0xb9, 0x59, 0x55, 0x22, 0x1f, 0xa5, 0x4b, 0x1d,
173*e0c4386eSCy Schubert     0xca, 0x38, 0xa0, 0xab, 0x87, 0x9c, 0x86, 0x0e, 0xdb, 0x1c, 0x4f, 0x4f, 0x07, 0xed, 0x18, 0x3f,
174*e0c4386eSCy Schubert     0x05, 0x3c, 0xec, 0x78, 0x11, 0xf6, 0x99, 0x02, 0x41, 0x00, 0xc2, 0xc5, 0xcf, 0xbe, 0x95, 0x91,
175*e0c4386eSCy Schubert     0xeb, 0xcf, 0x47, 0xf3, 0x33, 0x32, 0xc7, 0x7e, 0x93, 0x56, 0xf7, 0xd8, 0xf9, 0xd4, 0xb6, 0xd6,
176*e0c4386eSCy Schubert     0x20, 0xac, 0xba, 0x8a, 0x20, 0x19, 0x14, 0xab, 0xc5, 0x5d, 0xb2, 0x08, 0xcc, 0x77, 0x7c, 0x65,
177*e0c4386eSCy Schubert     0xa8, 0xdb, 0x66, 0x97, 0x36, 0x44, 0x2c, 0x63, 0xc0, 0x6a, 0x7e, 0xb0, 0x0b, 0x5c, 0x90, 0x12,
178*e0c4386eSCy Schubert     0x50, 0xb4, 0x36, 0x60, 0xc3, 0x1f, 0x22, 0x0c, 0xc8, 0x13, 0x02, 0x40, 0x33, 0xc8, 0x7e, 0x04,
179*e0c4386eSCy Schubert     0x7c, 0x97, 0x61, 0xf6, 0xfe, 0x39, 0xac, 0x34, 0xfe, 0x48, 0xbd, 0x5d, 0x7c, 0x72, 0xa4, 0x73,
180*e0c4386eSCy Schubert     0x3b, 0x72, 0x9e, 0x92, 0x55, 0x6e, 0x51, 0x3c, 0x39, 0x43, 0x5a, 0xe4, 0xa4, 0x71, 0xcc, 0xc5,
181*e0c4386eSCy Schubert     0xaf, 0x3f, 0xbb, 0xc8, 0x80, 0x65, 0x67, 0x2d, 0x9e, 0x32, 0x10, 0x99, 0x03, 0x2c, 0x99, 0xc8,
182*e0c4386eSCy Schubert     0xab, 0x71, 0xed, 0x31, 0xf8, 0xbb, 0xde, 0xee, 0x69, 0x7f, 0xba, 0x31, 0x02, 0x40, 0x7e, 0xbc,
183*e0c4386eSCy Schubert     0x60, 0x55, 0x4e, 0xd5, 0xc8, 0x6e, 0xf4, 0x0e, 0x57, 0xbe, 0x2e, 0xf9, 0x39, 0xbe, 0x59, 0x3f,
184*e0c4386eSCy Schubert     0xa2, 0x30, 0xbb, 0x57, 0xd1, 0xa3, 0x13, 0x2e, 0x55, 0x7c, 0x7c, 0x6a, 0xd8, 0xde, 0x02, 0xbe,
185*e0c4386eSCy Schubert     0x9e, 0xed, 0x10, 0xd0, 0xc5, 0x73, 0x1d, 0xea, 0x3e, 0xb1, 0x55, 0x81, 0x02, 0xef, 0x48, 0xc8,
186*e0c4386eSCy Schubert     0x1c, 0x5c, 0x7a, 0x92, 0xb0, 0x58, 0xd3, 0x19, 0x5b, 0x5d, 0xa2, 0xb6, 0x56, 0x69, 0x02, 0x40,
187*e0c4386eSCy Schubert     0x1e, 0x00, 0x6a, 0x9f, 0xba, 0xee, 0x46, 0x5a, 0xc5, 0xb5, 0x9f, 0x91, 0x33, 0xdd, 0xc9, 0x96,
188*e0c4386eSCy Schubert     0x75, 0xb7, 0x87, 0xcf, 0x18, 0x1c, 0xb7, 0xb9, 0x3f, 0x04, 0x10, 0xb8, 0x75, 0xa9, 0xb8, 0xa0,
189*e0c4386eSCy Schubert     0x31, 0x35, 0x03, 0x30, 0x89, 0xc8, 0x37, 0x68, 0x20, 0x30, 0x99, 0x39, 0x96, 0xd6, 0x2b, 0x3d,
190*e0c4386eSCy Schubert     0x5e, 0x45, 0x84, 0xf7, 0xd2, 0x61, 0x50, 0xc9, 0x50, 0xba, 0x8d, 0x08, 0xaa, 0xd0, 0x08, 0x1e,
191*e0c4386eSCy Schubert };
192*e0c4386eSCy Schubert 
193*e0c4386eSCy Schubert 
194*e0c4386eSCy Schubert static const PKCS12_ATTR ATTRS1[] = {
195*e0c4386eSCy Schubert     { "friendlyName", "george" },
196*e0c4386eSCy Schubert     { "localKeyID", "1234567890" },
197*e0c4386eSCy Schubert     { "1.2.3.4.5", "MyCustomAttribute" },
198*e0c4386eSCy Schubert     { NULL, NULL }
199*e0c4386eSCy Schubert };
200*e0c4386eSCy Schubert 
201*e0c4386eSCy Schubert static const PKCS12_ATTR ATTRS2[] = {
202*e0c4386eSCy Schubert     { "friendlyName", "janet" },
203*e0c4386eSCy Schubert     { "localKeyID", "987654321" },
204*e0c4386eSCy Schubert     { "1.2.3.5.8.13", "AnotherCustomAttribute" },
205*e0c4386eSCy Schubert     { NULL, NULL }
206*e0c4386eSCy Schubert };
207*e0c4386eSCy Schubert 
208*e0c4386eSCy Schubert static const PKCS12_ENC enc_default = {
209*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
210*e0c4386eSCy Schubert     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
211*e0c4386eSCy Schubert #else
212*e0c4386eSCy Schubert     NID_aes_128_cbc,
213*e0c4386eSCy Schubert #endif
214*e0c4386eSCy Schubert     "Password1",
215*e0c4386eSCy Schubert     1000
216*e0c4386eSCy Schubert };
217*e0c4386eSCy Schubert 
218*e0c4386eSCy Schubert static const PKCS12_ENC mac_default = {
219*e0c4386eSCy Schubert     NID_sha1,
220*e0c4386eSCy Schubert     "Password1",
221*e0c4386eSCy Schubert     1000
222*e0c4386eSCy Schubert };
223*e0c4386eSCy Schubert 
224*e0c4386eSCy Schubert static const int enc_nids_all[] = {
225*e0c4386eSCy Schubert     /* NOTE: To use PBES2 we pass the desired cipher NID instead of NID_pbes2 */
226*e0c4386eSCy Schubert     NID_aes_128_cbc,
227*e0c4386eSCy Schubert     NID_aes_256_cbc,
228*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
229*e0c4386eSCy Schubert     NID_des_ede3_cbc,
230*e0c4386eSCy Schubert     NID_des_cbc,
231*e0c4386eSCy Schubert #endif
232*e0c4386eSCy Schubert #ifndef OPENSSL_NO_RC5
233*e0c4386eSCy Schubert     NID_rc5_cbc,
234*e0c4386eSCy Schubert #endif
235*e0c4386eSCy Schubert #ifndef OPENSSL_NO_RC4
236*e0c4386eSCy Schubert     NID_rc4,
237*e0c4386eSCy Schubert #endif
238*e0c4386eSCy Schubert #ifndef OPENSSL_NO_RC2
239*e0c4386eSCy Schubert     NID_rc2_cbc,
240*e0c4386eSCy Schubert #endif
241*e0c4386eSCy Schubert 
242*e0c4386eSCy Schubert #ifndef OPENSSL_NO_MD2
243*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DES
244*e0c4386eSCy Schubert     NID_pbeWithMD2AndDES_CBC,
245*e0c4386eSCy Schubert # endif
246*e0c4386eSCy Schubert # ifndef OPENSSL_NO_RC2
247*e0c4386eSCy Schubert     NID_pbeWithMD2AndRC2_CBC,
248*e0c4386eSCy Schubert # endif
249*e0c4386eSCy Schubert #endif
250*e0c4386eSCy Schubert 
251*e0c4386eSCy Schubert #ifndef OPENSSL_NO_MD5
252*e0c4386eSCy Schubert # ifndef OPENSSL_NO_DES
253*e0c4386eSCy Schubert     NID_pbeWithMD5AndDES_CBC,
254*e0c4386eSCy Schubert # endif
255*e0c4386eSCy Schubert # ifndef OPENSSL_NO_RC2
256*e0c4386eSCy Schubert     NID_pbeWithMD5AndRC2_CBC,
257*e0c4386eSCy Schubert # endif
258*e0c4386eSCy Schubert #endif
259*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
260*e0c4386eSCy Schubert     NID_pbeWithSHA1AndDES_CBC,
261*e0c4386eSCy Schubert #endif
262*e0c4386eSCy Schubert #ifndef OPENSSL_NO_RC2
263*e0c4386eSCy Schubert     NID_pbe_WithSHA1And128BitRC2_CBC,
264*e0c4386eSCy Schubert     NID_pbe_WithSHA1And40BitRC2_CBC,
265*e0c4386eSCy Schubert     NID_pbeWithSHA1AndRC2_CBC,
266*e0c4386eSCy Schubert #endif
267*e0c4386eSCy Schubert #ifndef OPENSSL_NO_RC4
268*e0c4386eSCy Schubert     NID_pbe_WithSHA1And128BitRC4,
269*e0c4386eSCy Schubert     NID_pbe_WithSHA1And40BitRC4,
270*e0c4386eSCy Schubert #endif
271*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
272*e0c4386eSCy Schubert     NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
273*e0c4386eSCy Schubert     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
274*e0c4386eSCy Schubert #endif
275*e0c4386eSCy Schubert };
276*e0c4386eSCy Schubert 
277*e0c4386eSCy Schubert static const int enc_nids_no_legacy[] = {
278*e0c4386eSCy Schubert     /* NOTE: To use PBES2 we pass the desired cipher NID instead of NID_pbes2 */
279*e0c4386eSCy Schubert     NID_aes_128_cbc,
280*e0c4386eSCy Schubert     NID_aes_256_cbc,
281*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
282*e0c4386eSCy Schubert     NID_des_ede3_cbc,
283*e0c4386eSCy Schubert     NID_pbe_WithSHA1And2_Key_TripleDES_CBC,
284*e0c4386eSCy Schubert     NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
285*e0c4386eSCy Schubert #endif
286*e0c4386eSCy Schubert };
287*e0c4386eSCy Schubert 
288*e0c4386eSCy Schubert static const int mac_nids[] = {
289*e0c4386eSCy Schubert     NID_sha1,
290*e0c4386eSCy Schubert     NID_md5,
291*e0c4386eSCy Schubert     NID_sha256,
292*e0c4386eSCy Schubert     NID_sha512,
293*e0c4386eSCy Schubert     NID_sha3_256,
294*e0c4386eSCy Schubert     NID_sha3_512
295*e0c4386eSCy Schubert };
296*e0c4386eSCy Schubert 
297*e0c4386eSCy Schubert static const int iters[] = {
298*e0c4386eSCy Schubert     1,
299*e0c4386eSCy Schubert     1000
300*e0c4386eSCy Schubert };
301*e0c4386eSCy Schubert 
302*e0c4386eSCy Schubert static const char *passwords[] = {
303*e0c4386eSCy Schubert     "Password1",
304*e0c4386eSCy Schubert     "",
305*e0c4386eSCy Schubert };
306*e0c4386eSCy Schubert 
307*e0c4386eSCy Schubert /* --------------------------------------------------------------------------
308*e0c4386eSCy Schubert  * Local functions
309*e0c4386eSCy Schubert  */
310*e0c4386eSCy Schubert 
get_custom_oid(void)311*e0c4386eSCy Schubert static int get_custom_oid(void)
312*e0c4386eSCy Schubert {
313*e0c4386eSCy Schubert     static int sec_nid = -1;
314*e0c4386eSCy Schubert 
315*e0c4386eSCy Schubert     if (sec_nid != -1)
316*e0c4386eSCy Schubert         return sec_nid;
317*e0c4386eSCy Schubert     if (!TEST_true(OBJ_create("1.3.5.7.9", "CustomSecretOID", "My custom secret OID")))
318*e0c4386eSCy Schubert         return -1;
319*e0c4386eSCy Schubert     return sec_nid = OBJ_txt2nid("CustomSecretOID");
320*e0c4386eSCy Schubert }
321*e0c4386eSCy Schubert 
322*e0c4386eSCy Schubert 
323*e0c4386eSCy Schubert /* --------------------------------------------------------------------------
324*e0c4386eSCy Schubert  * PKCS12 format tests
325*e0c4386eSCy Schubert  */
326*e0c4386eSCy Schubert 
test_single_cert_no_attrs(void)327*e0c4386eSCy Schubert static int test_single_cert_no_attrs(void)
328*e0c4386eSCy Schubert {
329*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert.p12");
330*e0c4386eSCy Schubert 
331*e0c4386eSCy Schubert     /* Generate/encode */
332*e0c4386eSCy Schubert     start_pkcs12(pb);
333*e0c4386eSCy Schubert 
334*e0c4386eSCy Schubert         start_contentinfo(pb);
335*e0c4386eSCy Schubert 
336*e0c4386eSCy Schubert             add_certbag(pb, CERT1, sizeof(CERT1), NULL);
337*e0c4386eSCy Schubert 
338*e0c4386eSCy Schubert         end_contentinfo(pb);
339*e0c4386eSCy Schubert 
340*e0c4386eSCy Schubert     end_pkcs12(pb);
341*e0c4386eSCy Schubert 
342*e0c4386eSCy Schubert     /* Read/decode */
343*e0c4386eSCy Schubert     start_check_pkcs12(pb);
344*e0c4386eSCy Schubert 
345*e0c4386eSCy Schubert         start_check_contentinfo(pb);
346*e0c4386eSCy Schubert 
347*e0c4386eSCy Schubert             check_certbag(pb, CERT1, sizeof(CERT1), NULL);
348*e0c4386eSCy Schubert 
349*e0c4386eSCy Schubert         end_check_contentinfo(pb);
350*e0c4386eSCy Schubert 
351*e0c4386eSCy Schubert     end_check_pkcs12(pb);
352*e0c4386eSCy Schubert 
353*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
354*e0c4386eSCy Schubert }
355*e0c4386eSCy Schubert 
test_single_key(PKCS12_ENC * enc)356*e0c4386eSCy Schubert static int test_single_key(PKCS12_ENC *enc)
357*e0c4386eSCy Schubert {
358*e0c4386eSCy Schubert     char fname[80];
359*e0c4386eSCy Schubert     PKCS12_BUILDER *pb;
360*e0c4386eSCy Schubert 
361*e0c4386eSCy Schubert     sprintf(fname, "1key_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);
362*e0c4386eSCy Schubert 
363*e0c4386eSCy Schubert     pb = new_pkcs12_builder(fname);
364*e0c4386eSCy Schubert 
365*e0c4386eSCy Schubert     /* Generate/encode */
366*e0c4386eSCy Schubert     start_pkcs12(pb);
367*e0c4386eSCy Schubert 
368*e0c4386eSCy Schubert         start_contentinfo(pb);
369*e0c4386eSCy Schubert 
370*e0c4386eSCy Schubert             add_keybag(pb, KEY1, sizeof(KEY1), NULL, enc);
371*e0c4386eSCy Schubert 
372*e0c4386eSCy Schubert         end_contentinfo(pb);
373*e0c4386eSCy Schubert 
374*e0c4386eSCy Schubert     end_pkcs12(pb);
375*e0c4386eSCy Schubert 
376*e0c4386eSCy Schubert     /* Read/decode */
377*e0c4386eSCy Schubert     start_check_pkcs12(pb);
378*e0c4386eSCy Schubert 
379*e0c4386eSCy Schubert         start_check_contentinfo(pb);
380*e0c4386eSCy Schubert 
381*e0c4386eSCy Schubert             check_keybag(pb, KEY1, sizeof(KEY1), NULL, enc);
382*e0c4386eSCy Schubert 
383*e0c4386eSCy Schubert         end_check_contentinfo(pb);
384*e0c4386eSCy Schubert 
385*e0c4386eSCy Schubert     end_check_pkcs12(pb);
386*e0c4386eSCy Schubert 
387*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
388*e0c4386eSCy Schubert }
389*e0c4386eSCy Schubert 
test_single_key_enc_alg(int z)390*e0c4386eSCy Schubert static int test_single_key_enc_alg(int z)
391*e0c4386eSCy Schubert {
392*e0c4386eSCy Schubert     PKCS12_ENC enc;
393*e0c4386eSCy Schubert 
394*e0c4386eSCy Schubert     if (lgcyprov == NULL)
395*e0c4386eSCy Schubert         enc.nid = enc_nids_no_legacy[z];
396*e0c4386eSCy Schubert     else
397*e0c4386eSCy Schubert         enc.nid = enc_nids_all[z];
398*e0c4386eSCy Schubert     enc.pass = enc_default.pass;
399*e0c4386eSCy Schubert     enc.iter = enc_default.iter;
400*e0c4386eSCy Schubert 
401*e0c4386eSCy Schubert     return test_single_key(&enc);
402*e0c4386eSCy Schubert }
403*e0c4386eSCy Schubert 
test_single_key_enc_pass(int z)404*e0c4386eSCy Schubert static int test_single_key_enc_pass(int z)
405*e0c4386eSCy Schubert {
406*e0c4386eSCy Schubert     PKCS12_ENC enc;
407*e0c4386eSCy Schubert 
408*e0c4386eSCy Schubert     enc.nid = enc_default.nid;
409*e0c4386eSCy Schubert     enc.pass = passwords[z];
410*e0c4386eSCy Schubert     enc.iter = enc_default.iter;
411*e0c4386eSCy Schubert 
412*e0c4386eSCy Schubert     return test_single_key(&enc);
413*e0c4386eSCy Schubert }
414*e0c4386eSCy Schubert 
test_single_key_enc_iter(int z)415*e0c4386eSCy Schubert static int test_single_key_enc_iter(int z)
416*e0c4386eSCy Schubert {
417*e0c4386eSCy Schubert     PKCS12_ENC enc;
418*e0c4386eSCy Schubert 
419*e0c4386eSCy Schubert     enc.nid = enc_default.nid;
420*e0c4386eSCy Schubert     enc.pass = enc_default.pass;
421*e0c4386eSCy Schubert     enc.iter = iters[z];
422*e0c4386eSCy Schubert 
423*e0c4386eSCy Schubert     return test_single_key(&enc);
424*e0c4386eSCy Schubert }
425*e0c4386eSCy Schubert 
test_single_key_with_attrs(void)426*e0c4386eSCy Schubert static int test_single_key_with_attrs(void)
427*e0c4386eSCy Schubert {
428*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("1keyattrs.p12");
429*e0c4386eSCy Schubert 
430*e0c4386eSCy Schubert     /* Generate/encode */
431*e0c4386eSCy Schubert     start_pkcs12(pb);
432*e0c4386eSCy Schubert 
433*e0c4386eSCy Schubert         start_contentinfo(pb);
434*e0c4386eSCy Schubert 
435*e0c4386eSCy Schubert             add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
436*e0c4386eSCy Schubert 
437*e0c4386eSCy Schubert         end_contentinfo(pb);
438*e0c4386eSCy Schubert 
439*e0c4386eSCy Schubert     end_pkcs12(pb);
440*e0c4386eSCy Schubert 
441*e0c4386eSCy Schubert     /* Read/decode */
442*e0c4386eSCy Schubert     start_check_pkcs12(pb);
443*e0c4386eSCy Schubert 
444*e0c4386eSCy Schubert         start_check_contentinfo(pb);
445*e0c4386eSCy Schubert 
446*e0c4386eSCy Schubert             check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
447*e0c4386eSCy Schubert 
448*e0c4386eSCy Schubert         end_check_contentinfo(pb);
449*e0c4386eSCy Schubert 
450*e0c4386eSCy Schubert     end_check_pkcs12(pb);
451*e0c4386eSCy Schubert 
452*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
453*e0c4386eSCy Schubert }
454*e0c4386eSCy Schubert 
test_single_cert_mac(PKCS12_ENC * mac)455*e0c4386eSCy Schubert static int test_single_cert_mac(PKCS12_ENC *mac)
456*e0c4386eSCy Schubert {
457*e0c4386eSCy Schubert     char fname[80];
458*e0c4386eSCy Schubert     PKCS12_BUILDER *pb;
459*e0c4386eSCy Schubert 
460*e0c4386eSCy Schubert     sprintf(fname, "1cert_mac-%s_iter-%d.p12", OBJ_nid2sn(mac->nid), mac->iter);
461*e0c4386eSCy Schubert 
462*e0c4386eSCy Schubert     pb = new_pkcs12_builder(fname);
463*e0c4386eSCy Schubert 
464*e0c4386eSCy Schubert     /* Generate/encode */
465*e0c4386eSCy Schubert     start_pkcs12(pb);
466*e0c4386eSCy Schubert 
467*e0c4386eSCy Schubert         start_contentinfo(pb);
468*e0c4386eSCy Schubert 
469*e0c4386eSCy Schubert             add_certbag(pb, CERT1, sizeof(CERT1), NULL);
470*e0c4386eSCy Schubert 
471*e0c4386eSCy Schubert         end_contentinfo(pb);
472*e0c4386eSCy Schubert 
473*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, mac);
474*e0c4386eSCy Schubert 
475*e0c4386eSCy Schubert     /* Read/decode */
476*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, mac);
477*e0c4386eSCy Schubert 
478*e0c4386eSCy Schubert         start_check_contentinfo(pb);
479*e0c4386eSCy Schubert 
480*e0c4386eSCy Schubert             check_certbag(pb, CERT1, sizeof(CERT1), NULL);
481*e0c4386eSCy Schubert 
482*e0c4386eSCy Schubert         end_check_contentinfo(pb);
483*e0c4386eSCy Schubert 
484*e0c4386eSCy Schubert     end_check_pkcs12(pb);
485*e0c4386eSCy Schubert 
486*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
487*e0c4386eSCy Schubert }
488*e0c4386eSCy Schubert 
test_single_cert_mac_alg(int z)489*e0c4386eSCy Schubert static int test_single_cert_mac_alg(int z)
490*e0c4386eSCy Schubert {
491*e0c4386eSCy Schubert     PKCS12_ENC mac;
492*e0c4386eSCy Schubert 
493*e0c4386eSCy Schubert     mac.nid = mac_nids[z];
494*e0c4386eSCy Schubert     mac.pass = mac_default.pass;
495*e0c4386eSCy Schubert     mac.iter = mac_default.iter;
496*e0c4386eSCy Schubert 
497*e0c4386eSCy Schubert     return test_single_cert_mac(&mac);
498*e0c4386eSCy Schubert }
499*e0c4386eSCy Schubert 
test_single_cert_mac_pass(int z)500*e0c4386eSCy Schubert static int test_single_cert_mac_pass(int z)
501*e0c4386eSCy Schubert {
502*e0c4386eSCy Schubert     PKCS12_ENC mac;
503*e0c4386eSCy Schubert 
504*e0c4386eSCy Schubert     mac.nid = mac_default.nid;
505*e0c4386eSCy Schubert     mac.pass = passwords[z];
506*e0c4386eSCy Schubert     mac.iter = mac_default.iter;
507*e0c4386eSCy Schubert 
508*e0c4386eSCy Schubert     return test_single_cert_mac(&mac);
509*e0c4386eSCy Schubert }
510*e0c4386eSCy Schubert 
test_single_cert_mac_iter(int z)511*e0c4386eSCy Schubert static int test_single_cert_mac_iter(int z)
512*e0c4386eSCy Schubert {
513*e0c4386eSCy Schubert     PKCS12_ENC mac;
514*e0c4386eSCy Schubert 
515*e0c4386eSCy Schubert     mac.nid = mac_default.nid;
516*e0c4386eSCy Schubert     mac.pass = mac_default.pass;
517*e0c4386eSCy Schubert     mac.iter = iters[z];
518*e0c4386eSCy Schubert 
519*e0c4386eSCy Schubert     return test_single_cert_mac(&mac);
520*e0c4386eSCy Schubert }
521*e0c4386eSCy Schubert 
test_cert_key_with_attrs_and_mac(void)522*e0c4386eSCy Schubert static int test_cert_key_with_attrs_and_mac(void)
523*e0c4386eSCy Schubert {
524*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key.p12");
525*e0c4386eSCy Schubert 
526*e0c4386eSCy Schubert     /* Generate/encode */
527*e0c4386eSCy Schubert     start_pkcs12(pb);
528*e0c4386eSCy Schubert 
529*e0c4386eSCy Schubert         start_contentinfo(pb);
530*e0c4386eSCy Schubert 
531*e0c4386eSCy Schubert             add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
532*e0c4386eSCy Schubert             add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
533*e0c4386eSCy Schubert 
534*e0c4386eSCy Schubert         end_contentinfo(pb);
535*e0c4386eSCy Schubert 
536*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, &mac_default);
537*e0c4386eSCy Schubert 
538*e0c4386eSCy Schubert     /* Read/decode */
539*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, &mac_default);
540*e0c4386eSCy Schubert 
541*e0c4386eSCy Schubert         start_check_contentinfo(pb);
542*e0c4386eSCy Schubert 
543*e0c4386eSCy Schubert             check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
544*e0c4386eSCy Schubert             check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
545*e0c4386eSCy Schubert 
546*e0c4386eSCy Schubert         end_check_contentinfo(pb);
547*e0c4386eSCy Schubert 
548*e0c4386eSCy Schubert     end_check_pkcs12(pb);
549*e0c4386eSCy Schubert 
550*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
551*e0c4386eSCy Schubert }
552*e0c4386eSCy Schubert 
test_cert_key_encrypted_content(void)553*e0c4386eSCy Schubert static int test_cert_key_encrypted_content(void)
554*e0c4386eSCy Schubert {
555*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("1cert1key_enc.p12");
556*e0c4386eSCy Schubert 
557*e0c4386eSCy Schubert     /* Generate/encode */
558*e0c4386eSCy Schubert     start_pkcs12(pb);
559*e0c4386eSCy Schubert 
560*e0c4386eSCy Schubert         start_contentinfo(pb);
561*e0c4386eSCy Schubert 
562*e0c4386eSCy Schubert             add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
563*e0c4386eSCy Schubert             add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
564*e0c4386eSCy Schubert 
565*e0c4386eSCy Schubert         end_contentinfo_encrypted(pb, &enc_default);
566*e0c4386eSCy Schubert 
567*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, &mac_default);
568*e0c4386eSCy Schubert 
569*e0c4386eSCy Schubert     /* Read/decode */
570*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, &mac_default);
571*e0c4386eSCy Schubert 
572*e0c4386eSCy Schubert         start_check_contentinfo_encrypted(pb, &enc_default);
573*e0c4386eSCy Schubert 
574*e0c4386eSCy Schubert             check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
575*e0c4386eSCy Schubert             check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
576*e0c4386eSCy Schubert 
577*e0c4386eSCy Schubert         end_check_contentinfo(pb);
578*e0c4386eSCy Schubert 
579*e0c4386eSCy Schubert     end_check_pkcs12(pb);
580*e0c4386eSCy Schubert 
581*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
582*e0c4386eSCy Schubert }
583*e0c4386eSCy Schubert 
test_single_secret_encrypted_content(void)584*e0c4386eSCy Schubert static int test_single_secret_encrypted_content(void)
585*e0c4386eSCy Schubert {
586*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("1secret.p12");
587*e0c4386eSCy Schubert     int custom_nid = get_custom_oid();
588*e0c4386eSCy Schubert 
589*e0c4386eSCy Schubert     /* Generate/encode */
590*e0c4386eSCy Schubert     start_pkcs12(pb);
591*e0c4386eSCy Schubert 
592*e0c4386eSCy Schubert         start_contentinfo(pb);
593*e0c4386eSCy Schubert 
594*e0c4386eSCy Schubert             add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
595*e0c4386eSCy Schubert 
596*e0c4386eSCy Schubert         end_contentinfo_encrypted(pb, &enc_default);
597*e0c4386eSCy Schubert 
598*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, &mac_default);
599*e0c4386eSCy Schubert 
600*e0c4386eSCy Schubert     /* Read/decode */
601*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, &mac_default);
602*e0c4386eSCy Schubert 
603*e0c4386eSCy Schubert         start_check_contentinfo_encrypted(pb, &enc_default);
604*e0c4386eSCy Schubert 
605*e0c4386eSCy Schubert             check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
606*e0c4386eSCy Schubert 
607*e0c4386eSCy Schubert         end_check_contentinfo(pb);
608*e0c4386eSCy Schubert 
609*e0c4386eSCy Schubert     end_check_pkcs12(pb);
610*e0c4386eSCy Schubert 
611*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
612*e0c4386eSCy Schubert }
613*e0c4386eSCy Schubert 
test_single_secret(PKCS12_ENC * enc)614*e0c4386eSCy Schubert static int test_single_secret(PKCS12_ENC *enc)
615*e0c4386eSCy Schubert {
616*e0c4386eSCy Schubert     int custom_nid;
617*e0c4386eSCy Schubert     char fname[80];
618*e0c4386eSCy Schubert     PKCS12_BUILDER *pb;
619*e0c4386eSCy Schubert 
620*e0c4386eSCy Schubert     sprintf(fname, "1secret_ciph-%s_iter-%d.p12", OBJ_nid2sn(enc->nid), enc->iter);
621*e0c4386eSCy Schubert     pb = new_pkcs12_builder(fname);
622*e0c4386eSCy Schubert     custom_nid = get_custom_oid();
623*e0c4386eSCy Schubert 
624*e0c4386eSCy Schubert     /* Generate/encode */
625*e0c4386eSCy Schubert     start_pkcs12(pb);
626*e0c4386eSCy Schubert 
627*e0c4386eSCy Schubert         start_contentinfo(pb);
628*e0c4386eSCy Schubert 
629*e0c4386eSCy Schubert             add_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
630*e0c4386eSCy Schubert 
631*e0c4386eSCy Schubert         end_contentinfo_encrypted(pb, enc);
632*e0c4386eSCy Schubert 
633*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, &mac_default);
634*e0c4386eSCy Schubert 
635*e0c4386eSCy Schubert     /* Read/decode */
636*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, &mac_default);
637*e0c4386eSCy Schubert 
638*e0c4386eSCy Schubert         start_check_contentinfo_encrypted(pb, enc);
639*e0c4386eSCy Schubert 
640*e0c4386eSCy Schubert             check_secretbag(pb, custom_nid, "VerySecretMessage", ATTRS1);
641*e0c4386eSCy Schubert 
642*e0c4386eSCy Schubert         end_check_contentinfo(pb);
643*e0c4386eSCy Schubert 
644*e0c4386eSCy Schubert     end_check_pkcs12(pb);
645*e0c4386eSCy Schubert 
646*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
647*e0c4386eSCy Schubert }
648*e0c4386eSCy Schubert 
test_single_secret_enc_alg(int z)649*e0c4386eSCy Schubert static int test_single_secret_enc_alg(int z)
650*e0c4386eSCy Schubert {
651*e0c4386eSCy Schubert     PKCS12_ENC enc;
652*e0c4386eSCy Schubert 
653*e0c4386eSCy Schubert     if (lgcyprov == NULL)
654*e0c4386eSCy Schubert         enc.nid = enc_nids_no_legacy[z];
655*e0c4386eSCy Schubert     else
656*e0c4386eSCy Schubert         enc.nid = enc_nids_all[z];
657*e0c4386eSCy Schubert     enc.pass = enc_default.pass;
658*e0c4386eSCy Schubert     enc.iter = enc_default.iter;
659*e0c4386eSCy Schubert 
660*e0c4386eSCy Schubert     return test_single_secret(&enc);
661*e0c4386eSCy Schubert }
662*e0c4386eSCy Schubert 
test_multiple_contents(void)663*e0c4386eSCy Schubert static int test_multiple_contents(void)
664*e0c4386eSCy Schubert {
665*e0c4386eSCy Schubert     PKCS12_BUILDER *pb = new_pkcs12_builder("multi_contents.p12");
666*e0c4386eSCy Schubert     int custom_nid = get_custom_oid();
667*e0c4386eSCy Schubert 
668*e0c4386eSCy Schubert     /* Generate/encode */
669*e0c4386eSCy Schubert     start_pkcs12(pb);
670*e0c4386eSCy Schubert 
671*e0c4386eSCy Schubert         start_contentinfo(pb);
672*e0c4386eSCy Schubert 
673*e0c4386eSCy Schubert             add_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
674*e0c4386eSCy Schubert             add_certbag(pb, CERT2, sizeof(CERT2), ATTRS2);
675*e0c4386eSCy Schubert             add_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
676*e0c4386eSCy Schubert             add_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default);
677*e0c4386eSCy Schubert 
678*e0c4386eSCy Schubert         end_contentinfo(pb);
679*e0c4386eSCy Schubert 
680*e0c4386eSCy Schubert         start_contentinfo(pb);
681*e0c4386eSCy Schubert 
682*e0c4386eSCy Schubert             add_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1);
683*e0c4386eSCy Schubert 
684*e0c4386eSCy Schubert         end_contentinfo_encrypted(pb, &enc_default);
685*e0c4386eSCy Schubert 
686*e0c4386eSCy Schubert     end_pkcs12_with_mac(pb, &mac_default);
687*e0c4386eSCy Schubert 
688*e0c4386eSCy Schubert     /* Read/decode */
689*e0c4386eSCy Schubert     start_check_pkcs12_with_mac(pb, &mac_default);
690*e0c4386eSCy Schubert 
691*e0c4386eSCy Schubert         start_check_contentinfo(pb);
692*e0c4386eSCy Schubert 
693*e0c4386eSCy Schubert             check_certbag(pb, CERT1, sizeof(CERT1), ATTRS1);
694*e0c4386eSCy Schubert             check_certbag(pb, CERT2, sizeof(CERT2), ATTRS2);
695*e0c4386eSCy Schubert             check_keybag(pb, KEY1, sizeof(KEY1), ATTRS1, &enc_default);
696*e0c4386eSCy Schubert             check_keybag(pb, KEY2, sizeof(KEY2), ATTRS2, &enc_default);
697*e0c4386eSCy Schubert 
698*e0c4386eSCy Schubert         end_check_contentinfo(pb);
699*e0c4386eSCy Schubert 
700*e0c4386eSCy Schubert         start_check_contentinfo_encrypted(pb, &enc_default);
701*e0c4386eSCy Schubert 
702*e0c4386eSCy Schubert             check_secretbag(pb, custom_nid, "VeryVerySecretMessage", ATTRS1);
703*e0c4386eSCy Schubert 
704*e0c4386eSCy Schubert         end_check_contentinfo(pb);
705*e0c4386eSCy Schubert 
706*e0c4386eSCy Schubert     end_check_pkcs12(pb);
707*e0c4386eSCy Schubert 
708*e0c4386eSCy Schubert     return end_pkcs12_builder(pb);
709*e0c4386eSCy Schubert }
710*e0c4386eSCy Schubert 
711*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
pkcs12_create_test(void)712*e0c4386eSCy Schubert static int pkcs12_create_test(void)
713*e0c4386eSCy Schubert {
714*e0c4386eSCy Schubert     int ret = 0;
715*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
716*e0c4386eSCy Schubert     PKCS12 *p12 = NULL;
717*e0c4386eSCy Schubert     const unsigned char *p;
718*e0c4386eSCy Schubert 
719*e0c4386eSCy Schubert     static const unsigned char rsa_key[] = {
720*e0c4386eSCy Schubert         0x30, 0x82, 0x02, 0x5d, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xbb,
721*e0c4386eSCy Schubert         0x24, 0x7a, 0x09, 0x7e, 0x0e, 0xb2, 0x37, 0x32, 0xcc, 0x39, 0x67, 0xad,
722*e0c4386eSCy Schubert         0xf1, 0x9e, 0x3d, 0x6b, 0x82, 0x83, 0xd1, 0xd0, 0xac, 0xa4, 0xc0, 0x18,
723*e0c4386eSCy Schubert         0xbe, 0x8d, 0x98, 0x00, 0xc0, 0x7b, 0xff, 0x07, 0x44, 0xc9, 0xca, 0x1c,
724*e0c4386eSCy Schubert         0xba, 0x36, 0xe1, 0x27, 0x69, 0xff, 0xb1, 0xe3, 0x8d, 0x8b, 0xee, 0x57,
725*e0c4386eSCy Schubert         0xa9, 0x3a, 0xaa, 0x16, 0x43, 0x39, 0x54, 0x19, 0x7c, 0xae, 0x69, 0x24,
726*e0c4386eSCy Schubert         0x14, 0xf6, 0x64, 0xff, 0xbc, 0x74, 0xc6, 0x67, 0x6c, 0x4c, 0xf1, 0x02,
727*e0c4386eSCy Schubert         0x49, 0x69, 0xc7, 0x2b, 0xe1, 0xe1, 0xa1, 0xa3, 0x43, 0x14, 0xf4, 0x77,
728*e0c4386eSCy Schubert         0x8f, 0xc8, 0xd0, 0x85, 0x5a, 0x35, 0x95, 0xac, 0x62, 0xa9, 0xc1, 0x21,
729*e0c4386eSCy Schubert         0x00, 0x77, 0xa0, 0x8b, 0x97, 0x30, 0xb4, 0x5a, 0x2c, 0xb8, 0x90, 0x2f,
730*e0c4386eSCy Schubert         0x48, 0xa0, 0x05, 0x28, 0x4b, 0xf2, 0x0f, 0x8d, 0xec, 0x8b, 0x4d, 0x03,
731*e0c4386eSCy Schubert         0x42, 0x75, 0xd6, 0xad, 0x81, 0xc0, 0x11, 0x02, 0x03, 0x01, 0x00, 0x01,
732*e0c4386eSCy Schubert         0x02, 0x81, 0x80, 0x00, 0xfc, 0xb9, 0x4a, 0x26, 0x07, 0x89, 0x51, 0x2b,
733*e0c4386eSCy Schubert         0x53, 0x72, 0x91, 0xe0, 0x18, 0x3e, 0xa6, 0x5e, 0x31, 0xef, 0x9c, 0x0c,
734*e0c4386eSCy Schubert         0x16, 0x24, 0x42, 0xd0, 0x28, 0x33, 0xf9, 0xfa, 0xd0, 0x3c, 0x54, 0x04,
735*e0c4386eSCy Schubert         0x06, 0xc0, 0x15, 0xf5, 0x1b, 0x9a, 0xb3, 0x24, 0x31, 0xab, 0x3c, 0x6b,
736*e0c4386eSCy Schubert         0x47, 0x43, 0xb0, 0xd2, 0xa9, 0xdc, 0x05, 0xe1, 0x81, 0x59, 0xb6, 0x04,
737*e0c4386eSCy Schubert         0xe9, 0x66, 0x61, 0xaa, 0xd7, 0x0b, 0x00, 0x8f, 0x3d, 0xe5, 0xbf, 0xa2,
738*e0c4386eSCy Schubert         0xf8, 0x5e, 0x25, 0x6c, 0x1e, 0x22, 0x0f, 0xb4, 0xfd, 0x41, 0xe2, 0x03,
739*e0c4386eSCy Schubert         0x31, 0x5f, 0xda, 0x20, 0xc5, 0xc0, 0xf3, 0x55, 0x0e, 0xe1, 0xc9, 0xec,
740*e0c4386eSCy Schubert         0xd7, 0x3e, 0x2a, 0x0c, 0x01, 0xca, 0x7b, 0x22, 0xcb, 0xac, 0xf4, 0x2b,
741*e0c4386eSCy Schubert         0x27, 0xf0, 0x78, 0x5f, 0xb5, 0xc2, 0xf9, 0xe8, 0x14, 0x5a, 0x6e, 0x7e,
742*e0c4386eSCy Schubert         0x86, 0xbd, 0x6a, 0x9b, 0x20, 0x0c, 0xba, 0xcc, 0x97, 0x20, 0x11, 0x02,
743*e0c4386eSCy Schubert         0x41, 0x00, 0xc9, 0x59, 0x9f, 0x29, 0x8a, 0x5b, 0x9f, 0xe3, 0x2a, 0xd8,
744*e0c4386eSCy Schubert         0x7e, 0xc2, 0x40, 0x9f, 0xa8, 0x45, 0xe5, 0x3e, 0x11, 0x8d, 0x3c, 0xed,
745*e0c4386eSCy Schubert         0x6e, 0xab, 0xce, 0xd0, 0x65, 0x46, 0xd8, 0xc7, 0x07, 0x63, 0xb5, 0x23,
746*e0c4386eSCy Schubert         0x34, 0xf4, 0x9f, 0x7e, 0x1c, 0xc7, 0xc7, 0xf9, 0x65, 0xd1, 0xf4, 0x04,
747*e0c4386eSCy Schubert         0x42, 0x38, 0xbe, 0x3a, 0x0c, 0x9d, 0x08, 0x25, 0xfc, 0xa3, 0x71, 0xd9,
748*e0c4386eSCy Schubert         0xae, 0x0c, 0x39, 0x61, 0xf4, 0x89, 0x02, 0x41, 0x00, 0xed, 0xef, 0xab,
749*e0c4386eSCy Schubert         0xa9, 0xd5, 0x39, 0x9c, 0xee, 0x59, 0x1b, 0xff, 0xcf, 0x48, 0x44, 0x1b,
750*e0c4386eSCy Schubert         0xb6, 0x32, 0xe7, 0x46, 0x24, 0xf3, 0x04, 0x7f, 0xde, 0x95, 0x08, 0x6d,
751*e0c4386eSCy Schubert         0x75, 0x9e, 0x67, 0x17, 0xba, 0x5c, 0xa4, 0xd4, 0xe2, 0xe2, 0x4d, 0x77,
752*e0c4386eSCy Schubert         0xce, 0xeb, 0x66, 0x29, 0xc5, 0x96, 0xe0, 0x62, 0xbb, 0xe5, 0xac, 0xdc,
753*e0c4386eSCy Schubert         0x44, 0x62, 0x54, 0x86, 0xed, 0x64, 0x0c, 0xce, 0xd0, 0x60, 0x03, 0x9d,
754*e0c4386eSCy Schubert         0x49, 0x02, 0x40, 0x54, 0xd9, 0x18, 0x72, 0x27, 0xe4, 0xbe, 0x76, 0xbb,
755*e0c4386eSCy Schubert         0x1a, 0x6a, 0x28, 0x2f, 0x95, 0x58, 0x12, 0xc4, 0x2c, 0xa8, 0xb6, 0xcc,
756*e0c4386eSCy Schubert         0xe2, 0xfd, 0x0d, 0x17, 0x64, 0xc8, 0x18, 0xd7, 0xc6, 0xdf, 0x3d, 0x4c,
757*e0c4386eSCy Schubert         0x1a, 0x9e, 0xf9, 0x2a, 0xb0, 0xb9, 0x2e, 0x12, 0xfd, 0xec, 0xc3, 0x51,
758*e0c4386eSCy Schubert         0xc1, 0xed, 0xa9, 0xfd, 0xb7, 0x76, 0x93, 0x41, 0xd8, 0xc8, 0x22, 0x94,
759*e0c4386eSCy Schubert         0x1a, 0x77, 0xf6, 0x9c, 0xc3, 0xc3, 0x89, 0x02, 0x41, 0x00, 0x8e, 0xf9,
760*e0c4386eSCy Schubert         0xa7, 0x08, 0xad, 0xb5, 0x2a, 0x04, 0xdb, 0x8d, 0x04, 0xa1, 0xb5, 0x06,
761*e0c4386eSCy Schubert         0x20, 0x34, 0xd2, 0xcf, 0xc0, 0x89, 0xb1, 0x72, 0x31, 0xb8, 0x39, 0x8b,
762*e0c4386eSCy Schubert         0xcf, 0xe2, 0x8e, 0xa5, 0xda, 0x4f, 0x45, 0x1e, 0x53, 0x42, 0x66, 0xc4,
763*e0c4386eSCy Schubert         0x30, 0x4b, 0x29, 0x8e, 0xc1, 0x69, 0x17, 0x29, 0x8c, 0x8a, 0xe6, 0x0f,
764*e0c4386eSCy Schubert         0x82, 0x68, 0xa1, 0x41, 0xb3, 0xb6, 0x70, 0x99, 0x75, 0xa9, 0x27, 0x18,
765*e0c4386eSCy Schubert         0xe4, 0xe9, 0x02, 0x41, 0x00, 0x89, 0xea, 0x6e, 0x6d, 0x70, 0xdf, 0x25,
766*e0c4386eSCy Schubert         0x5f, 0x18, 0x3f, 0x48, 0xda, 0x63, 0x10, 0x8b, 0xfe, 0xa8, 0x0c, 0x94,
767*e0c4386eSCy Schubert         0x0f, 0xde, 0x97, 0x56, 0x53, 0x89, 0x94, 0xe2, 0x1e, 0x2c, 0x74, 0x3c,
768*e0c4386eSCy Schubert         0x91, 0x81, 0x34, 0x0b, 0xa6, 0x40, 0xf8, 0xcb, 0x2a, 0x60, 0x8c, 0xe0,
769*e0c4386eSCy Schubert         0x02, 0xb7, 0x89, 0x93, 0xcf, 0x18, 0x9f, 0x49, 0x54, 0xfd, 0x7d, 0x3f,
770*e0c4386eSCy Schubert         0x9a, 0xef, 0xd4, 0xa4, 0x4f, 0xc1, 0x45, 0x99, 0x91,
771*e0c4386eSCy Schubert     };
772*e0c4386eSCy Schubert 
773*e0c4386eSCy Schubert     p = rsa_key;
774*e0c4386eSCy Schubert     if (!TEST_ptr(pkey = d2i_PrivateKey_ex(EVP_PKEY_RSA, NULL, &p,
775*e0c4386eSCy Schubert                                            sizeof(rsa_key), NULL, NULL)))
776*e0c4386eSCy Schubert         goto err;
777*e0c4386eSCy Schubert     if (!TEST_int_eq(ERR_peek_error(), 0))
778*e0c4386eSCy Schubert         goto err;
779*e0c4386eSCy Schubert     p12 = PKCS12_create(NULL, NULL, pkey, NULL, NULL,
780*e0c4386eSCy Schubert                         NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
781*e0c4386eSCy Schubert                         NID_pbe_WithSHA1And3_Key_TripleDES_CBC, 2, 1, 0);
782*e0c4386eSCy Schubert     if (!TEST_ptr(p12))
783*e0c4386eSCy Schubert         goto err;
784*e0c4386eSCy Schubert 
785*e0c4386eSCy Schubert     if (!TEST_int_eq(ERR_peek_error(), 0))
786*e0c4386eSCy Schubert         goto err;
787*e0c4386eSCy Schubert     ret = 1;
788*e0c4386eSCy Schubert err:
789*e0c4386eSCy Schubert     PKCS12_free(p12);
790*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
791*e0c4386eSCy Schubert     return ret;
792*e0c4386eSCy Schubert }
793*e0c4386eSCy Schubert #endif
794*e0c4386eSCy Schubert 
pkcs12_recreate_test(void)795*e0c4386eSCy Schubert static int pkcs12_recreate_test(void)
796*e0c4386eSCy Schubert {
797*e0c4386eSCy Schubert     int ret = 0;
798*e0c4386eSCy Schubert     X509 *cert = NULL;
799*e0c4386eSCy Schubert     X509 *cert_parsed = NULL;
800*e0c4386eSCy Schubert     EVP_PKEY *pkey = NULL;
801*e0c4386eSCy Schubert     EVP_PKEY *pkey_parsed = NULL;
802*e0c4386eSCy Schubert     PKCS12 *p12 = NULL;
803*e0c4386eSCy Schubert     PKCS12 *p12_parsed = NULL;
804*e0c4386eSCy Schubert     PKCS12 *p12_recreated = NULL;
805*e0c4386eSCy Schubert     const unsigned char *cert_bytes = CERT1;
806*e0c4386eSCy Schubert     const unsigned char *key_bytes = KEY1;
807*e0c4386eSCy Schubert     BIO *bio = NULL;
808*e0c4386eSCy Schubert 
809*e0c4386eSCy Schubert     cert = d2i_X509(NULL, &cert_bytes, sizeof(CERT1));
810*e0c4386eSCy Schubert     if (!TEST_ptr(cert))
811*e0c4386eSCy Schubert         goto err;
812*e0c4386eSCy Schubert     pkey = d2i_AutoPrivateKey(NULL, &key_bytes, sizeof(KEY1));
813*e0c4386eSCy Schubert     if (!TEST_ptr(pkey))
814*e0c4386eSCy Schubert         goto err;
815*e0c4386eSCy Schubert     p12 = PKCS12_create("pass", NULL, pkey, cert, NULL, NID_aes_256_cbc,
816*e0c4386eSCy Schubert                         NID_aes_256_cbc, 2, 1, 0);
817*e0c4386eSCy Schubert     if (!TEST_ptr(p12))
818*e0c4386eSCy Schubert         goto err;
819*e0c4386eSCy Schubert     if (!TEST_int_eq(ERR_peek_error(), 0))
820*e0c4386eSCy Schubert         goto err;
821*e0c4386eSCy Schubert 
822*e0c4386eSCy Schubert     bio = BIO_new(BIO_s_mem());
823*e0c4386eSCy Schubert     if (!TEST_ptr(bio))
824*e0c4386eSCy Schubert         goto err;
825*e0c4386eSCy Schubert     if (!TEST_int_eq(i2d_PKCS12_bio(bio, p12), 1))
826*e0c4386eSCy Schubert         goto err;
827*e0c4386eSCy Schubert     p12_parsed = PKCS12_init_ex(NID_pkcs7_data, testctx, NULL);
828*e0c4386eSCy Schubert     if (!TEST_ptr(p12_parsed))
829*e0c4386eSCy Schubert         goto err;
830*e0c4386eSCy Schubert     p12_parsed = d2i_PKCS12_bio(bio, &p12_parsed);
831*e0c4386eSCy Schubert     if (!TEST_ptr(p12_parsed))
832*e0c4386eSCy Schubert         goto err;
833*e0c4386eSCy Schubert     if (!TEST_int_eq(PKCS12_parse(p12_parsed, "pass", &pkey_parsed,
834*e0c4386eSCy Schubert                                   &cert_parsed, NULL), 1))
835*e0c4386eSCy Schubert         goto err;
836*e0c4386eSCy Schubert 
837*e0c4386eSCy Schubert     /* cert_parsed also contains auxiliary data */
838*e0c4386eSCy Schubert     p12_recreated = PKCS12_create("new_pass", NULL, pkey_parsed, cert_parsed,
839*e0c4386eSCy Schubert                                   NULL, NID_aes_256_cbc, NID_aes_256_cbc,
840*e0c4386eSCy Schubert                                   2, 1, 0);
841*e0c4386eSCy Schubert     if (!TEST_ptr(p12_recreated))
842*e0c4386eSCy Schubert         goto err;
843*e0c4386eSCy Schubert     if (!TEST_int_eq(ERR_peek_error(), 0))
844*e0c4386eSCy Schubert         goto err;
845*e0c4386eSCy Schubert 
846*e0c4386eSCy Schubert     ret = 1;
847*e0c4386eSCy Schubert err:
848*e0c4386eSCy Schubert     BIO_free(bio);
849*e0c4386eSCy Schubert     PKCS12_free(p12);
850*e0c4386eSCy Schubert     PKCS12_free(p12_parsed);
851*e0c4386eSCy Schubert     PKCS12_free(p12_recreated);
852*e0c4386eSCy Schubert     EVP_PKEY_free(pkey);
853*e0c4386eSCy Schubert     EVP_PKEY_free(pkey_parsed);
854*e0c4386eSCy Schubert     X509_free(cert);
855*e0c4386eSCy Schubert     X509_free(cert_parsed);
856*e0c4386eSCy Schubert     return ret;
857*e0c4386eSCy Schubert }
858*e0c4386eSCy Schubert 
859*e0c4386eSCy Schubert typedef enum OPTION_choice {
860*e0c4386eSCy Schubert     OPT_ERR = -1,
861*e0c4386eSCy Schubert     OPT_EOF = 0,
862*e0c4386eSCy Schubert     OPT_WRITE,
863*e0c4386eSCy Schubert     OPT_LEGACY,
864*e0c4386eSCy Schubert     OPT_CONTEXT,
865*e0c4386eSCy Schubert     OPT_TEST_ENUM
866*e0c4386eSCy Schubert } OPTION_CHOICE;
867*e0c4386eSCy Schubert 
test_get_options(void)868*e0c4386eSCy Schubert const OPTIONS *test_get_options(void)
869*e0c4386eSCy Schubert {
870*e0c4386eSCy Schubert     static const OPTIONS options[] = {
871*e0c4386eSCy Schubert         OPT_TEST_OPTIONS_DEFAULT_USAGE,
872*e0c4386eSCy Schubert         { "write",   OPT_WRITE,   '-', "Write PKCS12 objects to file" },
873*e0c4386eSCy Schubert         { "legacy",  OPT_LEGACY,  '-', "Test the legacy APIs" },
874*e0c4386eSCy Schubert         { "context", OPT_CONTEXT, '-', "Explicitly use a non-default library context" },
875*e0c4386eSCy Schubert         { NULL }
876*e0c4386eSCy Schubert     };
877*e0c4386eSCy Schubert     return options;
878*e0c4386eSCy Schubert }
879*e0c4386eSCy Schubert 
setup_tests(void)880*e0c4386eSCy Schubert int setup_tests(void)
881*e0c4386eSCy Schubert {
882*e0c4386eSCy Schubert     OPTION_CHOICE o;
883*e0c4386eSCy Schubert 
884*e0c4386eSCy Schubert     while ((o = opt_next()) != OPT_EOF) {
885*e0c4386eSCy Schubert         switch (o) {
886*e0c4386eSCy Schubert         case OPT_WRITE:
887*e0c4386eSCy Schubert             PKCS12_helper_set_write_files(1);
888*e0c4386eSCy Schubert             break;
889*e0c4386eSCy Schubert         case OPT_LEGACY:
890*e0c4386eSCy Schubert             PKCS12_helper_set_legacy(1);
891*e0c4386eSCy Schubert             break;
892*e0c4386eSCy Schubert         case OPT_CONTEXT:
893*e0c4386eSCy Schubert             default_libctx = 0;
894*e0c4386eSCy Schubert             break;
895*e0c4386eSCy Schubert         case OPT_TEST_CASES:
896*e0c4386eSCy Schubert             break;
897*e0c4386eSCy Schubert         default:
898*e0c4386eSCy Schubert             return 0;
899*e0c4386eSCy Schubert         }
900*e0c4386eSCy Schubert     }
901*e0c4386eSCy Schubert 
902*e0c4386eSCy Schubert     if (!default_libctx) {
903*e0c4386eSCy Schubert         testctx = OSSL_LIB_CTX_new();
904*e0c4386eSCy Schubert         if (!TEST_ptr(testctx))
905*e0c4386eSCy Schubert             return 0;
906*e0c4386eSCy Schubert         nullprov = OSSL_PROVIDER_load(NULL, "null");
907*e0c4386eSCy Schubert         if (!TEST_ptr(nullprov))
908*e0c4386eSCy Schubert             return 0;
909*e0c4386eSCy Schubert     }
910*e0c4386eSCy Schubert 
911*e0c4386eSCy Schubert     deflprov = OSSL_PROVIDER_load(testctx, "default");
912*e0c4386eSCy Schubert     if (!TEST_ptr(deflprov))
913*e0c4386eSCy Schubert         return 0;
914*e0c4386eSCy Schubert     lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
915*e0c4386eSCy Schubert 
916*e0c4386eSCy Schubert     PKCS12_helper_set_libctx(testctx);
917*e0c4386eSCy Schubert 
918*e0c4386eSCy Schubert     /*
919*e0c4386eSCy Schubert      * Verify that the default and fips providers in the default libctx are not
920*e0c4386eSCy Schubert      * available if we are using a standalone context
921*e0c4386eSCy Schubert      */
922*e0c4386eSCy Schubert     if (!default_libctx) {
923*e0c4386eSCy Schubert         if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
924*e0c4386eSCy Schubert                 || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
925*e0c4386eSCy Schubert             return 0;
926*e0c4386eSCy Schubert     }
927*e0c4386eSCy Schubert 
928*e0c4386eSCy Schubert     ADD_TEST(test_single_cert_no_attrs);
929*e0c4386eSCy Schubert     if (lgcyprov == NULL) {
930*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_no_legacy));
931*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_no_legacy));
932*e0c4386eSCy Schubert     } else {
933*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_single_key_enc_alg, OSSL_NELEM(enc_nids_all));
934*e0c4386eSCy Schubert         ADD_ALL_TESTS(test_single_secret_enc_alg, OSSL_NELEM(enc_nids_all));
935*e0c4386eSCy Schubert     }
936*e0c4386eSCy Schubert #ifndef OPENSSL_NO_DES
937*e0c4386eSCy Schubert     if (default_libctx)
938*e0c4386eSCy Schubert         ADD_TEST(pkcs12_create_test);
939*e0c4386eSCy Schubert #endif
940*e0c4386eSCy Schubert     if (default_libctx)
941*e0c4386eSCy Schubert         ADD_TEST(pkcs12_recreate_test);
942*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_single_key_enc_pass, OSSL_NELEM(passwords));
943*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_single_key_enc_iter, OSSL_NELEM(iters));
944*e0c4386eSCy Schubert     ADD_TEST(test_single_key_with_attrs);
945*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_single_cert_mac_alg, OSSL_NELEM(mac_nids));
946*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_single_cert_mac_pass, OSSL_NELEM(passwords));
947*e0c4386eSCy Schubert     ADD_ALL_TESTS(test_single_cert_mac_iter, OSSL_NELEM(iters));
948*e0c4386eSCy Schubert     ADD_TEST(test_cert_key_with_attrs_and_mac);
949*e0c4386eSCy Schubert     ADD_TEST(test_cert_key_encrypted_content);
950*e0c4386eSCy Schubert     ADD_TEST(test_single_secret_encrypted_content);
951*e0c4386eSCy Schubert     ADD_TEST(test_multiple_contents);
952*e0c4386eSCy Schubert     return 1;
953*e0c4386eSCy Schubert }
954*e0c4386eSCy Schubert 
cleanup_tests(void)955*e0c4386eSCy Schubert void cleanup_tests(void)
956*e0c4386eSCy Schubert {
957*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(nullprov);
958*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(deflprov);
959*e0c4386eSCy Schubert     OSSL_PROVIDER_unload(lgcyprov);
960*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(testctx);
961*e0c4386eSCy Schubert }
962