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