1139747Simp/*
24Srgrimes * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved.
34Srgrimes *
44Srgrimes * Licensed under the Apache License 2.0 (the "License").  You may not use
58876Srgrimes * this file except in compliance with the License.  You can obtain a copy
64Srgrimes * in the file LICENSE in the source distribution or at
74Srgrimes * https://www.openssl.org/source/license.html
84Srgrimes */
94Srgrimes
104Srgrimes#include <string.h>
118876Srgrimes
128876Srgrimes#include <openssl/cms.h>
134Srgrimes#include <openssl/bio.h>
144Srgrimes#include <openssl/x509.h>
158876Srgrimes#include <openssl/pem.h>
164Srgrimes
178876Srgrimes#include "testutil.h"
184Srgrimes
194Srgrimesstatic X509 *cert = NULL;
204Srgrimesstatic EVP_PKEY *privkey = NULL;
214Srgrimesstatic char *derin = NULL;
228876Srgrimes
234Srgrimesstatic int test_encrypt_decrypt(const EVP_CIPHER *cipher)
244Srgrimes{
254Srgrimes    int testresult = 0;
264Srgrimes    STACK_OF(X509) *certstack = sk_X509_new_null();
274Srgrimes    const char *msg = "Hello world";
284Srgrimes    BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
294Srgrimes    BIO *outmsgbio = BIO_new(BIO_s_mem());
304Srgrimes    CMS_ContentInfo* content = NULL;
314Srgrimes    char buf[80];
324Srgrimes
33116176Sobrien    if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
34116176Sobrien        goto end;
35116176Sobrien
36116176Sobrien    if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
372056Swollman        goto end;
3842654Sjdp
3986998Sdd    content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
40131952Smarcel    if (!TEST_ptr(content))
4186998Sdd        goto end;
4286998Sdd
4317848Spst    if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
4486998Sdd                               CMS_TEXT)))
452056Swollman        goto end;
4649558Sphk
47126399Sphk    /* Check we got the message we first started with */
4812734Sbde    if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
492056Swollman            || !TEST_int_eq(strcmp(buf, msg), 0))
5012473Sbde        goto end;
514Srgrimes
524Srgrimes    testresult = 1;
534Srgrimes end:
54118990Smarcel    sk_X509_free(certstack);
5579418Sjulian    BIO_free(msgbio);
564Srgrimes    BIO_free(outmsgbio);
574Srgrimes    CMS_ContentInfo_free(content);
584Srgrimes
594Srgrimes    return testresult && TEST_int_eq(ERR_peek_error(), 0);
604Srgrimes}
6118296Sbde
624Srgrimesstatic int test_encrypt_decrypt_aes_cbc(void)
634Srgrimes{
644Srgrimes    return test_encrypt_decrypt(EVP_aes_128_cbc());
654Srgrimes}
6678161Speter
6778161Speterstatic int test_encrypt_decrypt_aes_128_gcm(void)
6878161Speter{
6912515Sphk    return test_encrypt_decrypt(EVP_aes_128_gcm());
70132002Smarcel}
7186998Sdd
7285944Speterstatic int test_encrypt_decrypt_aes_192_gcm(void)
73132482Smarcel{
74126399Sphk    return test_encrypt_decrypt(EVP_aes_192_gcm());
7517848Spst}
7618296Sbde
7718296Sbdestatic int test_encrypt_decrypt_aes_256_gcm(void)
7818296Sbde{
794Srgrimes    return test_encrypt_decrypt(EVP_aes_256_gcm());
804Srgrimes}
814Srgrimes
824Srgrimesstatic int test_d2i_CMS_bio_NULL(void)
834Srgrimes{
8412515Sphk    BIO *bio;
854Srgrimes    CMS_ContentInfo *cms = NULL;
864Srgrimes    int ret = 0;
874Srgrimes
884Srgrimes    /*
894Srgrimes     * Test data generated using:
904Srgrimes     * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \
914Srgrimes     * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \
924Srgrimes     * -nosmimecap
934Srgrimes     */
944Srgrimes    static const unsigned char cms_data[] = {
954Srgrimes        0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86,
964Srgrimes        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
974Srgrimes        0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02,
984Srgrimes        0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09,
994Srgrimes        0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
1004Srgrimes        0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48,
1014Srgrimes        0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f,
1024Srgrimes        0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
1034Srgrimes        0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0,
1044Srgrimes        0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30,
1054Srgrimes        0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02,
1064Srgrimes        0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2,
10793009Sbde        0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
10893009Sbde        0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
10992756Salfred        0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
11093009Sbde        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
11193009Sbde        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
11292756Salfred        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
11393009Sbde        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
11493009Sbde        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
11512473Sbde        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
1164Srgrimes        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
1174Srgrimes        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
1184Srgrimes        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
11912515Sphk        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
12078161Speter        0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
1214Srgrimes        0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35,
1224Srgrimes        0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30,
12318296Sbde        0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31,
12478161Speter        0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
1254Srgrimes        0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
1264Srgrimes        0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
1274Srgrimes        0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
12818296Sbde        0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
1294Srgrimes        0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
1304Srgrimes        0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
1314Srgrimes        0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
1324Srgrimes        0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
1334Srgrimes        0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
1344Srgrimes        0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
1354Srgrimes        0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
1364Srgrimes        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
1374Srgrimes        0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
1384Srgrimes        0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
1394Srgrimes        0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88,
1404Srgrimes        0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92,
1414Srgrimes        0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10,
1424Srgrimes        0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b,
1434Srgrimes        0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b,
1444Srgrimes        0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97,
1454Srgrimes        0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2,
1464Srgrimes        0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d,
1474Srgrimes        0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7,
1484Srgrimes        0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e,
1494Srgrimes        0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e,
1504Srgrimes        0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00,
1514Srgrimes        0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f,
1524Srgrimes        0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e,
1534Srgrimes        0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c,
1544Srgrimes        0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91,
1554Srgrimes        0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3,
1564Srgrimes        0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb,
1574Srgrimes        0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec,
1584Srgrimes        0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9,
1594Srgrimes        0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97,
1604Srgrimes        0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46,
16118296Sbde        0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9,
16218296Sbde        0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0,
16378161Speter        0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf,
16418296Sbde        0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21,
16518296Sbde        0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93,
16618296Sbde        0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc,
16718296Sbde        0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75,
16818296Sbde        0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f,
16918296Sbde        0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37,
17018296Sbde        0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c,
17118296Sbde        0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01,
17218296Sbde        0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03,
17318296Sbde        0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
17418296Sbde        0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
17518296Sbde        0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89,
17618296Sbde        0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03,
17718296Sbde        0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d,
17818296Sbde        0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
17918296Sbde        0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56,
18018296Sbde        0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86,
18118296Sbde        0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33,
18218296Sbde        0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
18318296Sbde        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
18418296Sbde        0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a,
18518296Sbde        0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce,
18618296Sbde        0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb,
18718296Sbde        0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69,
18818296Sbde        0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6,
18918296Sbde        0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe,
19018296Sbde        0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17,
19118296Sbde        0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95,
19218296Sbde        0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20,
1934Srgrimes        0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08,
1944Srgrimes        0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab,
1954Srgrimes        0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3,
1964Srgrimes        0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c,
1974Srgrimes        0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79,
1984Srgrimes        0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc,
1994Srgrimes        0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a,
2004Srgrimes        0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe,
2014Srgrimes        0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d,
20212515Sphk        0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1,
20378161Speter        0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba,
2044Srgrimes        0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49,
20518296Sbde        0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87,
20678161Speter        0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c,
2074Srgrimes        0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97,
2084Srgrimes        0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44,
20918296Sbde        0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5,
2104Srgrimes        0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1,
2114Srgrimes        0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b,
2124Srgrimes        0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6,
2134Srgrimes        0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5,
2144Srgrimes        0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12,
21518296Sbde        0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d,
21618296Sbde        0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82,
21778161Speter        0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01,
21818296Sbde        0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30,
21918296Sbde        0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
22018296Sbde        0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
2214Srgrimes        0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
2224Srgrimes        0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
22312515Sphk        0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
22478161Speter        0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
2254Srgrimes        0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
2264Srgrimes        0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
22718296Sbde        0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
22878161Speter        0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f,
2294Srgrimes        0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43,
2304Srgrimes        0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b,
2314Srgrimes        0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
2324Srgrimes        0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06,
2334Srgrimes        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
234798Swollman        0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86,
2354Srgrimes        0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30,
2364Srgrimes        0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
2374Srgrimes        0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d,
2384Srgrimes        0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39,
2394Srgrimes        0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06,
2404Srgrimes        0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
2414Srgrimes        0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80,
2424Srgrimes        0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26,
2434Srgrimes        0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53,
2444Srgrimes        0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49,
2454Srgrimes        0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d,
2464Srgrimes        0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
24710348Sbde        0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
2484Srgrimes        0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52,
2494Srgrimes        0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c,
2504Srgrimes        0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a,
2514Srgrimes        0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05,
2524Srgrimes        0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9,
2534Srgrimes        0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e,
2544Srgrimes        0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2,
2554Srgrimes        0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71,
2564Srgrimes        0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73,
2574Srgrimes        0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5,
2584Srgrimes        0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43,
2594Srgrimes        0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c,
2604Srgrimes        0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7,
2614Srgrimes        0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99,
26218296Sbde        0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6,
26378161Speter        0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe,
2644Srgrimes        0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a,
2654Srgrimes        0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37,
2664Srgrimes        0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7,
2674Srgrimes        0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb,
2684Srgrimes        0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde,
2694Srgrimes        0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49,
2704Srgrimes        0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2,
2714Srgrimes        0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85,
2724Srgrimes        0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75,
2734Srgrimes        0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01,
2744Srgrimes        0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f,
27578161Speter        0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30,
2764Srgrimes        0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a,
2774Srgrimes        0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5,
2784Srgrimes        0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74,
2794Srgrimes        0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3,
2804Srgrimes        0x6a
2814Srgrimes    };
28218296Sbde
28318296Sbde    ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data)))
284104321Sphk          && TEST_ptr(cms = d2i_CMS_bio(bio, NULL))
28578161Speter          && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL,
28678161Speter                                  CMS_NO_SIGNER_CERT_VERIFY));
287104321Sphk    CMS_ContentInfo_free(cms);
28818296Sbde    BIO_free(bio);
2894Srgrimes    return ret && TEST_int_eq(ERR_peek_error(), 0);
2904Srgrimes}
29178161Speter
2924Srgrimesstatic unsigned char *read_all(BIO *bio, long *p_len)
2934Srgrimes{
2944Srgrimes    const int step = 256;
2954Srgrimes    unsigned char *buf = NULL;
2964Srgrimes    unsigned char *tmp = NULL;
2974Srgrimes    int ret;
2984Srgrimes
2994Srgrimes    *p_len = 0;
3004Srgrimes    for (;;) {
3014Srgrimes        tmp = OPENSSL_realloc(buf, *p_len + step);
3024Srgrimes        if (tmp == NULL)
3034Srgrimes            break;
3044Srgrimes        buf = tmp;
3054Srgrimes        ret = BIO_read(bio, buf + *p_len, step);
3064Srgrimes        if (ret < 0)
3074Srgrimes            break;
3084Srgrimes
3094Srgrimes        *p_len += ret;
3104Srgrimes
3114Srgrimes        if (ret < step)
3124Srgrimes            return buf;
3134Srgrimes    }
3144Srgrimes
3154Srgrimes    /* Error */
3164Srgrimes    OPENSSL_free(buf);
3174Srgrimes    *p_len = 0;
3184Srgrimes    return NULL;
3194Srgrimes}
3204Srgrimes
3214Srgrimesstatic int test_d2i_CMS_decode(const int idx)
3224Srgrimes{
3234Srgrimes    BIO *bio = NULL;
3244Srgrimes    CMS_ContentInfo *cms = NULL;
3254Srgrimes    unsigned char *buf = NULL;
3264Srgrimes    const unsigned char *tmp = NULL;
3274Srgrimes    long buf_len = 0;
3284Srgrimes    int ret = 0;
3294Srgrimes
3304Srgrimes    if (!TEST_ptr(bio = BIO_new_file(derin, "r")))
3314Srgrimes      goto end;
3324Srgrimes
3334Srgrimes    switch (idx) {
3344Srgrimes    case 0:
3354Srgrimes        if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL)))
3364Srgrimes            goto end;
3374Srgrimes        break;
3384Srgrimes    case 1:
3394Srgrimes        if (!TEST_ptr(buf = read_all(bio, &buf_len)))
3404Srgrimes            goto end;
3414Srgrimes        tmp = buf;
3424Srgrimes        if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len)))
3434Srgrimes            goto end;
3444Srgrimes        break;
3454Srgrimes    }
3464Srgrimes
3474Srgrimes    if (!TEST_int_eq(ERR_peek_error(), 0))
3484Srgrimes        goto end;
3494Srgrimes
350118268Sjhb    ret = 1;
3514Srgrimesend:
3524Srgrimes    CMS_ContentInfo_free(cms);
3534Srgrimes    BIO_free(bio);
3544Srgrimes    OPENSSL_free(buf);
3554Srgrimes
3564Srgrimes    return ret;
3574Srgrimes}
3584Srgrimes
3594SrgrimesOPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n")
3604Srgrimes
3614Srgrimesint setup_tests(void)
3624Srgrimes{
3634Srgrimes    char *certin = NULL, *privkeyin = NULL;
3644Srgrimes    BIO *certbio = NULL, *privkeybio = NULL;
3654Srgrimes
3664Srgrimes    if (!test_skip_common_options()) {
3674Srgrimes        TEST_error("Error parsing test options\n");
3684Srgrimes        return 0;
3694Srgrimes    }
3704Srgrimes
3714Srgrimes    if (!TEST_ptr(certin = test_get_argument(0))
3724Srgrimes            || !TEST_ptr(privkeyin = test_get_argument(1))
3734Srgrimes            || !TEST_ptr(derin = test_get_argument(2)))
3744Srgrimes        return 0;
3754Srgrimes
3764Srgrimes    certbio = BIO_new_file(certin, "r");
3772112Swollman    if (!TEST_ptr(certbio))
37812515Sphk        return 0;
3791549Srgrimes    if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
3804Srgrimes        BIO_free(certbio);
3814Srgrimes        return 0;
3824Srgrimes    }
38312515Sphk    BIO_free(certbio);
3844Srgrimes
3854Srgrimes    privkeybio = BIO_new_file(privkeyin, "r");
3864Srgrimes    if (!TEST_ptr(privkeybio)) {
387131952Smarcel        X509_free(cert);
3884Srgrimes        cert = NULL;
3894Srgrimes        return 0;
3904Srgrimes    }
39112515Sphk    if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
3924Srgrimes        BIO_free(privkeybio);
3936920Sjoerg        X509_free(cert);
3944Srgrimes        cert = NULL;
3954Srgrimes        return 0;
3964Srgrimes    }
3974Srgrimes    BIO_free(privkeybio);
3984Srgrimes
3994Srgrimes    ADD_TEST(test_encrypt_decrypt_aes_cbc);
4004Srgrimes    ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
4014Srgrimes    ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
4024Srgrimes    ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
4034Srgrimes    ADD_TEST(test_d2i_CMS_bio_NULL);
4044Srgrimes    ADD_ALL_TESTS(test_d2i_CMS_decode, 2);
40579573Sbsd    return 1;
40679573Sbsd}
4074Srgrimes
4084Srgrimesvoid cleanup_tests(void)
4094Srgrimes{
4104Srgrimes    X509_free(cert);
4114Srgrimes    EVP_PKEY_free(privkey);
4124Srgrimes}
4134Srgrimes