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