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