2 * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
19 static X509 *cert = NULL;
20 static EVP_PKEY *privkey = NULL;
22 static int test_encrypt_decrypt(void)
25 STACK_OF(X509) *certstack = sk_X509_new_null();
26 const char *msg = "Hello world";
27 BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
28 BIO *outmsgbio = BIO_new(BIO_s_mem());
29 CMS_ContentInfo* content = NULL;
32 if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
35 if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
38 content = CMS_encrypt(certstack, msgbio, EVP_aes_128_cbc(), CMS_TEXT);
39 if (!TEST_ptr(content))
42 if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
46 /* Check we got the message we first started with */
47 if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
48 || !TEST_int_eq(strcmp(buf, msg), 0))
53 sk_X509_free(certstack);
56 CMS_ContentInfo_free(content);
61 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
65 char *certin = NULL, *privkeyin = NULL;
66 BIO *certbio = NULL, *privkeybio = NULL;
68 if (!TEST_ptr(certin = test_get_argument(0))
69 || !TEST_ptr(privkeyin = test_get_argument(1)))
72 certbio = BIO_new_file(certin, "r");
73 if (!TEST_ptr(certbio))
75 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
81 privkeybio = BIO_new_file(privkeyin, "r");
82 if (!TEST_ptr(privkeybio)) {
87 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
95 ADD_TEST(test_encrypt_decrypt);
100 void cleanup_tests(void)
103 EVP_PKEY_free(privkey);