3 #include <openssl/cms.h>
4 #include <openssl/bio.h>
5 #include <openssl/x509.h>
6 #include <openssl/pem.h>
10 static X509 *cert = NULL;
11 static EVP_PKEY *privkey = NULL;
13 static int test_encrypt_decrypt(void)
16 STACK_OF(X509) *certstack = sk_X509_new_null();
17 const char *msg = "Hello world";
18 BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
19 BIO *outmsgbio = BIO_new(BIO_s_mem());
20 CMS_ContentInfo* content = NULL;
23 if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
26 if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
29 content = CMS_encrypt(certstack, msgbio, EVP_aes_128_cbc(), CMS_TEXT);
30 if (!TEST_ptr(content))
33 if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
37 /* Check we got the message we first started with */
38 if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
39 || !TEST_int_eq(strcmp(buf, msg), 0))
44 sk_X509_free(certstack);
47 CMS_ContentInfo_free(content);
54 char *certin = NULL, *privkeyin = NULL;
55 BIO *certbio = NULL, *privkeybio = NULL;
57 if (!TEST_ptr(certin = test_get_argument(0))
58 || !TEST_ptr(privkeyin = test_get_argument(1)))
61 certbio = BIO_new_file(certin, "r");
62 if (!TEST_ptr(certbio))
64 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
70 privkeybio = BIO_new_file(privkeyin, "r");
71 if (!TEST_ptr(privkeybio)) {
76 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
84 ADD_TEST(test_encrypt_decrypt);
89 void cleanup_tests(void)
92 EVP_PKEY_free(privkey);