diff options
author | Matt Caswell <matt@openssl.org> | 2018-05-01 09:32:30 +0100 |
---|---|---|
committer | Matt Caswell <matt@openssl.org> | 2018-05-08 08:43:39 +0100 |
commit | e15e92dbd5248bc8dbd95d2c0af33a6daf8f7255 (patch) | |
tree | 2ce5cbf2beed6a188787f7a0532b1409cdeb9b9f /test/cmsapitest.c | |
parent | 3d551b20df1acd01f80d3ae00d37177e0fdf344a (diff) |
Add a CMS API test
Previous tests only invoked CMS via the command line app. This test uses
the CMS API directly to do and encrypt and decrypt operation. This test
would have caught the memory leak fixed by the previous commit (when
building with enable-crypto-mdebug).
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/6142)
Diffstat (limited to 'test/cmsapitest.c')
-rw-r--r-- | test/cmsapitest.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/test/cmsapitest.c b/test/cmsapitest.c new file mode 100644 index 0000000000..a79ae8c043 --- /dev/null +++ b/test/cmsapitest.c @@ -0,0 +1,93 @@ +#include <string.h> + +#include <openssl/cms.h> +#include <openssl/bio.h> +#include <openssl/x509.h> +#include <openssl/pem.h> + +#include "testutil.h" + +static X509 *cert = NULL; +static EVP_PKEY *privkey = NULL; + +static int test_encrypt_decrypt(void) +{ + int testresult = 0; + STACK_OF(X509) *certstack = sk_X509_new_null(); + const char *msg = "Hello world"; + BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg)); + BIO *outmsgbio = BIO_new(BIO_s_mem()); + CMS_ContentInfo* content = NULL; + char buf[80]; + + if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio)) + goto end; + + if (!TEST_int_gt(sk_X509_push(certstack, cert), 0)) + goto end; + + content = CMS_encrypt(certstack, msgbio, EVP_aes_128_cbc(), CMS_TEXT); + if (!TEST_ptr(content)) + goto end; + + if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio, + CMS_TEXT))) + goto end; + + /* Check we got the message we first started with */ + if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg)) + || !TEST_int_eq(strcmp(buf, msg), 0)) + goto end; + + testresult = 1; + end: + sk_X509_free(certstack); + BIO_free(msgbio); + BIO_free(outmsgbio); + CMS_ContentInfo_free(content); + + return testresult; +} + +int setup_tests(void) +{ + char *certin = NULL, *privkeyin = NULL; + BIO *certbio = NULL, *privkeybio = NULL; + + if (!TEST_ptr(certin = test_get_argument(0)) + || !TEST_ptr(privkeyin = test_get_argument(1))) + return 0; + + certbio = BIO_new_file(certin, "r"); + if (!TEST_ptr(certbio)) + return 0; + if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) { + BIO_free(certbio); + return 0; + } + BIO_free(certbio); + + privkeybio = BIO_new_file(privkeyin, "r"); + if (!TEST_ptr(privkeybio)) { + X509_free(cert); + cert = NULL; + return 0; + } + if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) { + BIO_free(privkeybio); + X509_free(cert); + cert = NULL; + return 0; + } + BIO_free(privkeybio); + + ADD_TEST(test_encrypt_decrypt); + + return 1; +} + +void cleanup_tests(void) +{ + X509_free(cert); + EVP_PKEY_free(privkey); +} |