xref: /third_party/openssl/test/cmsapitest.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <string.h>
11e1051a39Sopenharmony_ci
12e1051a39Sopenharmony_ci#include <openssl/cms.h>
13e1051a39Sopenharmony_ci#include <openssl/bio.h>
14e1051a39Sopenharmony_ci#include <openssl/x509.h>
15e1051a39Sopenharmony_ci#include <openssl/pem.h>
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci#include "testutil.h"
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_cistatic X509 *cert = NULL;
20e1051a39Sopenharmony_cistatic EVP_PKEY *privkey = NULL;
21e1051a39Sopenharmony_cistatic char *derin = NULL;
22e1051a39Sopenharmony_ci
23e1051a39Sopenharmony_cistatic int test_encrypt_decrypt(const EVP_CIPHER *cipher)
24e1051a39Sopenharmony_ci{
25e1051a39Sopenharmony_ci    int testresult = 0;
26e1051a39Sopenharmony_ci    STACK_OF(X509) *certstack = sk_X509_new_null();
27e1051a39Sopenharmony_ci    const char *msg = "Hello world";
28e1051a39Sopenharmony_ci    BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
29e1051a39Sopenharmony_ci    BIO *outmsgbio = BIO_new(BIO_s_mem());
30e1051a39Sopenharmony_ci    CMS_ContentInfo* content = NULL;
31e1051a39Sopenharmony_ci    char buf[80];
32e1051a39Sopenharmony_ci
33e1051a39Sopenharmony_ci    if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
34e1051a39Sopenharmony_ci        goto end;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
37e1051a39Sopenharmony_ci        goto end;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_ci    content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
40e1051a39Sopenharmony_ci    if (!TEST_ptr(content))
41e1051a39Sopenharmony_ci        goto end;
42e1051a39Sopenharmony_ci
43e1051a39Sopenharmony_ci    if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
44e1051a39Sopenharmony_ci                               CMS_TEXT)))
45e1051a39Sopenharmony_ci        goto end;
46e1051a39Sopenharmony_ci
47e1051a39Sopenharmony_ci    /* Check we got the message we first started with */
48e1051a39Sopenharmony_ci    if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
49e1051a39Sopenharmony_ci            || !TEST_int_eq(strcmp(buf, msg), 0))
50e1051a39Sopenharmony_ci        goto end;
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ci    testresult = 1;
53e1051a39Sopenharmony_ci end:
54e1051a39Sopenharmony_ci    sk_X509_free(certstack);
55e1051a39Sopenharmony_ci    BIO_free(msgbio);
56e1051a39Sopenharmony_ci    BIO_free(outmsgbio);
57e1051a39Sopenharmony_ci    CMS_ContentInfo_free(content);
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_ci    return testresult;
60e1051a39Sopenharmony_ci}
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_cistatic int test_encrypt_decrypt_aes_cbc(void)
63e1051a39Sopenharmony_ci{
64e1051a39Sopenharmony_ci    return test_encrypt_decrypt(EVP_aes_128_cbc());
65e1051a39Sopenharmony_ci}
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_cistatic int test_encrypt_decrypt_aes_128_gcm(void)
68e1051a39Sopenharmony_ci{
69e1051a39Sopenharmony_ci    return test_encrypt_decrypt(EVP_aes_128_gcm());
70e1051a39Sopenharmony_ci}
71e1051a39Sopenharmony_ci
72e1051a39Sopenharmony_cistatic int test_encrypt_decrypt_aes_192_gcm(void)
73e1051a39Sopenharmony_ci{
74e1051a39Sopenharmony_ci    return test_encrypt_decrypt(EVP_aes_192_gcm());
75e1051a39Sopenharmony_ci}
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_cistatic int test_encrypt_decrypt_aes_256_gcm(void)
78e1051a39Sopenharmony_ci{
79e1051a39Sopenharmony_ci    return test_encrypt_decrypt(EVP_aes_256_gcm());
80e1051a39Sopenharmony_ci}
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_cistatic int test_d2i_CMS_bio_NULL(void)
83e1051a39Sopenharmony_ci{
84e1051a39Sopenharmony_ci    BIO *bio;
85e1051a39Sopenharmony_ci    CMS_ContentInfo *cms = NULL;
86e1051a39Sopenharmony_ci    int ret = 0;
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci    /*
89e1051a39Sopenharmony_ci     * Test data generated using:
90e1051a39Sopenharmony_ci     * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \
91e1051a39Sopenharmony_ci     * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \
92e1051a39Sopenharmony_ci     * -nosmimecap
93e1051a39Sopenharmony_ci     */
94e1051a39Sopenharmony_ci    static const unsigned char cms_data[] = {
95e1051a39Sopenharmony_ci        0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86,
96e1051a39Sopenharmony_ci        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
97e1051a39Sopenharmony_ci        0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02,
98e1051a39Sopenharmony_ci        0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09,
99e1051a39Sopenharmony_ci        0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
100e1051a39Sopenharmony_ci        0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48,
101e1051a39Sopenharmony_ci        0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f,
102e1051a39Sopenharmony_ci        0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
103e1051a39Sopenharmony_ci        0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0,
104e1051a39Sopenharmony_ci        0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30,
105e1051a39Sopenharmony_ci        0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02,
106e1051a39Sopenharmony_ci        0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2,
107e1051a39Sopenharmony_ci        0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
108e1051a39Sopenharmony_ci        0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
109e1051a39Sopenharmony_ci        0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
110e1051a39Sopenharmony_ci        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
111e1051a39Sopenharmony_ci        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
112e1051a39Sopenharmony_ci        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
113e1051a39Sopenharmony_ci        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
114e1051a39Sopenharmony_ci        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
115e1051a39Sopenharmony_ci        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
116e1051a39Sopenharmony_ci        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
117e1051a39Sopenharmony_ci        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
118e1051a39Sopenharmony_ci        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
119e1051a39Sopenharmony_ci        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
120e1051a39Sopenharmony_ci        0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
121e1051a39Sopenharmony_ci        0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35,
122e1051a39Sopenharmony_ci        0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30,
123e1051a39Sopenharmony_ci        0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31,
124e1051a39Sopenharmony_ci        0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
125e1051a39Sopenharmony_ci        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
126e1051a39Sopenharmony_ci        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
127e1051a39Sopenharmony_ci        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
128e1051a39Sopenharmony_ci        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
129e1051a39Sopenharmony_ci        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
130e1051a39Sopenharmony_ci        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
131e1051a39Sopenharmony_ci        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
132e1051a39Sopenharmony_ci        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
133e1051a39Sopenharmony_ci        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
134e1051a39Sopenharmony_ci        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
135e1051a39Sopenharmony_ci        0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
136e1051a39Sopenharmony_ci        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
137e1051a39Sopenharmony_ci        0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
138e1051a39Sopenharmony_ci        0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
139e1051a39Sopenharmony_ci        0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88,
140e1051a39Sopenharmony_ci        0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92,
141e1051a39Sopenharmony_ci        0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10,
142e1051a39Sopenharmony_ci        0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b,
143e1051a39Sopenharmony_ci        0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b,
144e1051a39Sopenharmony_ci        0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97,
145e1051a39Sopenharmony_ci        0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2,
146e1051a39Sopenharmony_ci        0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d,
147e1051a39Sopenharmony_ci        0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7,
148e1051a39Sopenharmony_ci        0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e,
149e1051a39Sopenharmony_ci        0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e,
150e1051a39Sopenharmony_ci        0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00,
151e1051a39Sopenharmony_ci        0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f,
152e1051a39Sopenharmony_ci        0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e,
153e1051a39Sopenharmony_ci        0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c,
154e1051a39Sopenharmony_ci        0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91,
155e1051a39Sopenharmony_ci        0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3,
156e1051a39Sopenharmony_ci        0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb,
157e1051a39Sopenharmony_ci        0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec,
158e1051a39Sopenharmony_ci        0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9,
159e1051a39Sopenharmony_ci        0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97,
160e1051a39Sopenharmony_ci        0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46,
161e1051a39Sopenharmony_ci        0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9,
162e1051a39Sopenharmony_ci        0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0,
163e1051a39Sopenharmony_ci        0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf,
164e1051a39Sopenharmony_ci        0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21,
165e1051a39Sopenharmony_ci        0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93,
166e1051a39Sopenharmony_ci        0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc,
167e1051a39Sopenharmony_ci        0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75,
168e1051a39Sopenharmony_ci        0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f,
169e1051a39Sopenharmony_ci        0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37,
170e1051a39Sopenharmony_ci        0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c,
171e1051a39Sopenharmony_ci        0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01,
172e1051a39Sopenharmony_ci        0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03,
173e1051a39Sopenharmony_ci        0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
174e1051a39Sopenharmony_ci        0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
175e1051a39Sopenharmony_ci        0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89,
176e1051a39Sopenharmony_ci        0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03,
177e1051a39Sopenharmony_ci        0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d,
178e1051a39Sopenharmony_ci        0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
179e1051a39Sopenharmony_ci        0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56,
180e1051a39Sopenharmony_ci        0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86,
181e1051a39Sopenharmony_ci        0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33,
182e1051a39Sopenharmony_ci        0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
183e1051a39Sopenharmony_ci        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
184e1051a39Sopenharmony_ci        0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a,
185e1051a39Sopenharmony_ci        0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce,
186e1051a39Sopenharmony_ci        0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb,
187e1051a39Sopenharmony_ci        0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69,
188e1051a39Sopenharmony_ci        0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6,
189e1051a39Sopenharmony_ci        0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe,
190e1051a39Sopenharmony_ci        0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17,
191e1051a39Sopenharmony_ci        0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95,
192e1051a39Sopenharmony_ci        0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20,
193e1051a39Sopenharmony_ci        0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08,
194e1051a39Sopenharmony_ci        0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab,
195e1051a39Sopenharmony_ci        0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3,
196e1051a39Sopenharmony_ci        0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c,
197e1051a39Sopenharmony_ci        0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79,
198e1051a39Sopenharmony_ci        0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc,
199e1051a39Sopenharmony_ci        0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a,
200e1051a39Sopenharmony_ci        0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe,
201e1051a39Sopenharmony_ci        0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d,
202e1051a39Sopenharmony_ci        0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1,
203e1051a39Sopenharmony_ci        0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba,
204e1051a39Sopenharmony_ci        0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49,
205e1051a39Sopenharmony_ci        0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87,
206e1051a39Sopenharmony_ci        0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c,
207e1051a39Sopenharmony_ci        0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97,
208e1051a39Sopenharmony_ci        0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44,
209e1051a39Sopenharmony_ci        0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5,
210e1051a39Sopenharmony_ci        0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1,
211e1051a39Sopenharmony_ci        0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b,
212e1051a39Sopenharmony_ci        0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6,
213e1051a39Sopenharmony_ci        0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5,
214e1051a39Sopenharmony_ci        0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12,
215e1051a39Sopenharmony_ci        0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d,
216e1051a39Sopenharmony_ci        0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82,
217e1051a39Sopenharmony_ci        0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01,
218e1051a39Sopenharmony_ci        0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30,
219e1051a39Sopenharmony_ci        0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
220e1051a39Sopenharmony_ci        0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
221e1051a39Sopenharmony_ci        0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
222e1051a39Sopenharmony_ci        0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
223e1051a39Sopenharmony_ci        0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
224e1051a39Sopenharmony_ci        0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
225e1051a39Sopenharmony_ci        0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
226e1051a39Sopenharmony_ci        0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
227e1051a39Sopenharmony_ci        0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
228e1051a39Sopenharmony_ci        0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f,
229e1051a39Sopenharmony_ci        0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43,
230e1051a39Sopenharmony_ci        0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b,
231e1051a39Sopenharmony_ci        0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
232e1051a39Sopenharmony_ci        0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06,
233e1051a39Sopenharmony_ci        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
234e1051a39Sopenharmony_ci        0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86,
235e1051a39Sopenharmony_ci        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30,
236e1051a39Sopenharmony_ci        0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
237e1051a39Sopenharmony_ci        0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d,
238e1051a39Sopenharmony_ci        0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39,
239e1051a39Sopenharmony_ci        0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06,
240e1051a39Sopenharmony_ci        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
241e1051a39Sopenharmony_ci        0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80,
242e1051a39Sopenharmony_ci        0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26,
243e1051a39Sopenharmony_ci        0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53,
244e1051a39Sopenharmony_ci        0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49,
245e1051a39Sopenharmony_ci        0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d,
246e1051a39Sopenharmony_ci        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
247e1051a39Sopenharmony_ci        0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
248e1051a39Sopenharmony_ci        0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52,
249e1051a39Sopenharmony_ci        0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c,
250e1051a39Sopenharmony_ci        0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a,
251e1051a39Sopenharmony_ci        0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05,
252e1051a39Sopenharmony_ci        0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9,
253e1051a39Sopenharmony_ci        0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e,
254e1051a39Sopenharmony_ci        0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2,
255e1051a39Sopenharmony_ci        0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71,
256e1051a39Sopenharmony_ci        0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73,
257e1051a39Sopenharmony_ci        0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5,
258e1051a39Sopenharmony_ci        0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43,
259e1051a39Sopenharmony_ci        0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c,
260e1051a39Sopenharmony_ci        0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7,
261e1051a39Sopenharmony_ci        0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99,
262e1051a39Sopenharmony_ci        0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6,
263e1051a39Sopenharmony_ci        0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe,
264e1051a39Sopenharmony_ci        0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a,
265e1051a39Sopenharmony_ci        0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37,
266e1051a39Sopenharmony_ci        0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7,
267e1051a39Sopenharmony_ci        0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb,
268e1051a39Sopenharmony_ci        0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde,
269e1051a39Sopenharmony_ci        0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49,
270e1051a39Sopenharmony_ci        0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2,
271e1051a39Sopenharmony_ci        0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85,
272e1051a39Sopenharmony_ci        0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75,
273e1051a39Sopenharmony_ci        0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01,
274e1051a39Sopenharmony_ci        0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f,
275e1051a39Sopenharmony_ci        0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30,
276e1051a39Sopenharmony_ci        0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a,
277e1051a39Sopenharmony_ci        0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5,
278e1051a39Sopenharmony_ci        0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74,
279e1051a39Sopenharmony_ci        0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3,
280e1051a39Sopenharmony_ci        0x6a
281e1051a39Sopenharmony_ci    };
282e1051a39Sopenharmony_ci
283e1051a39Sopenharmony_ci    ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data)))
284e1051a39Sopenharmony_ci          && TEST_ptr(cms = d2i_CMS_bio(bio, NULL))
285e1051a39Sopenharmony_ci          && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL,
286e1051a39Sopenharmony_ci                                  CMS_NO_SIGNER_CERT_VERIFY));
287e1051a39Sopenharmony_ci    CMS_ContentInfo_free(cms);
288e1051a39Sopenharmony_ci    BIO_free(bio);
289e1051a39Sopenharmony_ci    return ret;
290e1051a39Sopenharmony_ci}
291e1051a39Sopenharmony_ci
292e1051a39Sopenharmony_cistatic unsigned char *read_all(BIO *bio, long *p_len)
293e1051a39Sopenharmony_ci{
294e1051a39Sopenharmony_ci    const int step = 256;
295e1051a39Sopenharmony_ci    unsigned char *buf = NULL;
296e1051a39Sopenharmony_ci    unsigned char *tmp = NULL;
297e1051a39Sopenharmony_ci    int ret;
298e1051a39Sopenharmony_ci
299e1051a39Sopenharmony_ci    *p_len = 0;
300e1051a39Sopenharmony_ci    for (;;) {
301e1051a39Sopenharmony_ci        tmp = OPENSSL_realloc(buf, *p_len + step);
302e1051a39Sopenharmony_ci        if (tmp == NULL)
303e1051a39Sopenharmony_ci            break;
304e1051a39Sopenharmony_ci        buf = tmp;
305e1051a39Sopenharmony_ci        ret = BIO_read(bio, buf + *p_len, step);
306e1051a39Sopenharmony_ci        if (ret < 0)
307e1051a39Sopenharmony_ci            break;
308e1051a39Sopenharmony_ci
309e1051a39Sopenharmony_ci        *p_len += ret;
310e1051a39Sopenharmony_ci
311e1051a39Sopenharmony_ci        if (ret < step)
312e1051a39Sopenharmony_ci            return buf;
313e1051a39Sopenharmony_ci    }
314e1051a39Sopenharmony_ci
315e1051a39Sopenharmony_ci    /* Error */
316e1051a39Sopenharmony_ci    OPENSSL_free(buf);
317e1051a39Sopenharmony_ci    *p_len = 0;
318e1051a39Sopenharmony_ci    return NULL;
319e1051a39Sopenharmony_ci}
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_cistatic int test_d2i_CMS_decode(const int idx)
322e1051a39Sopenharmony_ci{
323e1051a39Sopenharmony_ci    BIO *bio = NULL;
324e1051a39Sopenharmony_ci    CMS_ContentInfo *cms = NULL;
325e1051a39Sopenharmony_ci    unsigned char *buf = NULL;
326e1051a39Sopenharmony_ci    const unsigned char *tmp = NULL;
327e1051a39Sopenharmony_ci    long buf_len = 0;
328e1051a39Sopenharmony_ci    int ret = 0;
329e1051a39Sopenharmony_ci
330e1051a39Sopenharmony_ci    if (!TEST_ptr(bio = BIO_new_file(derin, "r")))
331e1051a39Sopenharmony_ci      goto end;
332e1051a39Sopenharmony_ci
333e1051a39Sopenharmony_ci    switch (idx) {
334e1051a39Sopenharmony_ci    case 0:
335e1051a39Sopenharmony_ci        if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL)))
336e1051a39Sopenharmony_ci            goto end;
337e1051a39Sopenharmony_ci        break;
338e1051a39Sopenharmony_ci    case 1:
339e1051a39Sopenharmony_ci        if (!TEST_ptr(buf = read_all(bio, &buf_len)))
340e1051a39Sopenharmony_ci            goto end;
341e1051a39Sopenharmony_ci        tmp = buf;
342e1051a39Sopenharmony_ci        if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len)))
343e1051a39Sopenharmony_ci            goto end;
344e1051a39Sopenharmony_ci        break;
345e1051a39Sopenharmony_ci    }
346e1051a39Sopenharmony_ci
347e1051a39Sopenharmony_ci    if (!TEST_int_eq(ERR_peek_error(), 0))
348e1051a39Sopenharmony_ci        goto end;
349e1051a39Sopenharmony_ci
350e1051a39Sopenharmony_ci    ret = 1;
351e1051a39Sopenharmony_ciend:
352e1051a39Sopenharmony_ci    CMS_ContentInfo_free(cms);
353e1051a39Sopenharmony_ci    BIO_free(bio);
354e1051a39Sopenharmony_ci    OPENSSL_free(buf);
355e1051a39Sopenharmony_ci
356e1051a39Sopenharmony_ci    return ret;
357e1051a39Sopenharmony_ci}
358e1051a39Sopenharmony_ci
359e1051a39Sopenharmony_ciOPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n")
360e1051a39Sopenharmony_ci
361e1051a39Sopenharmony_ciint setup_tests(void)
362e1051a39Sopenharmony_ci{
363e1051a39Sopenharmony_ci    char *certin = NULL, *privkeyin = NULL;
364e1051a39Sopenharmony_ci    BIO *certbio = NULL, *privkeybio = NULL;
365e1051a39Sopenharmony_ci
366e1051a39Sopenharmony_ci    if (!test_skip_common_options()) {
367e1051a39Sopenharmony_ci        TEST_error("Error parsing test options\n");
368e1051a39Sopenharmony_ci        return 0;
369e1051a39Sopenharmony_ci    }
370e1051a39Sopenharmony_ci
371e1051a39Sopenharmony_ci    if (!TEST_ptr(certin = test_get_argument(0))
372e1051a39Sopenharmony_ci            || !TEST_ptr(privkeyin = test_get_argument(1))
373e1051a39Sopenharmony_ci            || !TEST_ptr(derin = test_get_argument(2)))
374e1051a39Sopenharmony_ci        return 0;
375e1051a39Sopenharmony_ci
376e1051a39Sopenharmony_ci    certbio = BIO_new_file(certin, "r");
377e1051a39Sopenharmony_ci    if (!TEST_ptr(certbio))
378e1051a39Sopenharmony_ci        return 0;
379e1051a39Sopenharmony_ci    if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
380e1051a39Sopenharmony_ci        BIO_free(certbio);
381e1051a39Sopenharmony_ci        return 0;
382e1051a39Sopenharmony_ci    }
383e1051a39Sopenharmony_ci    BIO_free(certbio);
384e1051a39Sopenharmony_ci
385e1051a39Sopenharmony_ci    privkeybio = BIO_new_file(privkeyin, "r");
386e1051a39Sopenharmony_ci    if (!TEST_ptr(privkeybio)) {
387e1051a39Sopenharmony_ci        X509_free(cert);
388e1051a39Sopenharmony_ci        cert = NULL;
389e1051a39Sopenharmony_ci        return 0;
390e1051a39Sopenharmony_ci    }
391e1051a39Sopenharmony_ci    if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
392e1051a39Sopenharmony_ci        BIO_free(privkeybio);
393e1051a39Sopenharmony_ci        X509_free(cert);
394e1051a39Sopenharmony_ci        cert = NULL;
395e1051a39Sopenharmony_ci        return 0;
396e1051a39Sopenharmony_ci    }
397e1051a39Sopenharmony_ci    BIO_free(privkeybio);
398e1051a39Sopenharmony_ci
399e1051a39Sopenharmony_ci    ADD_TEST(test_encrypt_decrypt_aes_cbc);
400e1051a39Sopenharmony_ci    ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
401e1051a39Sopenharmony_ci    ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
402e1051a39Sopenharmony_ci    ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
403e1051a39Sopenharmony_ci    ADD_TEST(test_d2i_CMS_bio_NULL);
404e1051a39Sopenharmony_ci    ADD_ALL_TESTS(test_d2i_CMS_decode, 2);
405e1051a39Sopenharmony_ci    return 1;
406e1051a39Sopenharmony_ci}
407e1051a39Sopenharmony_ci
408e1051a39Sopenharmony_civoid cleanup_tests(void)
409e1051a39Sopenharmony_ci{
410e1051a39Sopenharmony_ci    X509_free(cert);
411e1051a39Sopenharmony_ci    EVP_PKEY_free(privkey);
412e1051a39Sopenharmony_ci}
413