2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include <openssl/cms.h>
13 #include <openssl/bio.h>
14 #include <openssl/x509.h>
15 #include <openssl/pem.h>
21 static X509 *cert = NULL;
22 static EVP_PKEY *privkey = NULL;
24 static int test_encrypt_decrypt(void)
27 STACK_OF(X509) *certstack = sk_X509_new_null();
28 const char *msg = "Hello world";
29 BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
30 BIO *outmsgbio = BIO_new(BIO_s_mem());
31 CMS_ContentInfo* content = NULL;
34 if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
37 if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
40 content = CMS_encrypt(certstack, msgbio, EVP_aes_128_cbc(), CMS_TEXT);
41 if (!TEST_ptr(content))
44 if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
48 /* Check we got the message we first started with */
49 if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
50 || !TEST_int_eq(strcmp(buf, msg), 0))
55 sk_X509_free(certstack);
58 CMS_ContentInfo_free(content);
63 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
67 char *certin = NULL, *privkeyin = NULL;
68 BIO *certbio = NULL, *privkeybio = NULL;
70 if (!test_skip_common_options()) {
71 TEST_error("Error parsing test options\n");
75 if (!TEST_ptr(certin = test_get_argument(0))
76 || !TEST_ptr(privkeyin = test_get_argument(1)))
79 certbio = BIO_new_file(certin, "r");
80 if (!TEST_ptr(certbio))
82 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
88 privkeybio = BIO_new_file(privkeyin, "r");
89 if (!TEST_ptr(privkeybio)) {
94 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
100 BIO_free(privkeybio);
102 ADD_TEST(test_encrypt_decrypt);
107 void cleanup_tests(void)
110 EVP_PKEY_free(privkey);