]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/cmsapitest.c
Add CMS AuthEnvelopedData with AES-GCM support
[thirdparty/openssl.git] / test / cmsapitest.c
CommitLineData
a43ce58f 1/*
33388b44 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
a43ce58f 3 *
a6ed19dc 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
a43ce58f
SL
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
8 */
9
e15e92db
MC
10#include <string.h>
11
12#include <openssl/cms.h>
13#include <openssl/bio.h>
14#include <openssl/x509.h>
15#include <openssl/pem.h>
16
17#include "testutil.h"
18
852c2ed2
RS
19DEFINE_STACK_OF(X509)
20
e15e92db
MC
21static X509 *cert = NULL;
22static EVP_PKEY *privkey = NULL;
23
924663c3 24static int test_encrypt_decrypt(const EVP_CIPHER *cipher)
e15e92db
MC
25{
26 int testresult = 0;
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;
32 char buf[80];
33
34 if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
35 goto end;
36
37 if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
38 goto end;
39
924663c3 40 content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
e15e92db
MC
41 if (!TEST_ptr(content))
42 goto end;
43
44 if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
45 CMS_TEXT)))
46 goto end;
47
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))
51 goto end;
52
53 testresult = 1;
54 end:
55 sk_X509_free(certstack);
56 BIO_free(msgbio);
57 BIO_free(outmsgbio);
58 CMS_ContentInfo_free(content);
59
60 return testresult;
61}
62
924663c3
JZ
63static int test_encrypt_decrypt_aes_cbc(void)
64{
65 return test_encrypt_decrypt(EVP_aes_128_cbc());
66}
67
68static int test_encrypt_decrypt_aes_128_gcm(void)
69{
70 return test_encrypt_decrypt(EVP_aes_128_gcm());
71}
72
73static int test_encrypt_decrypt_aes_192_gcm(void)
74{
75 return test_encrypt_decrypt(EVP_aes_192_gcm());
76}
77
78static int test_encrypt_decrypt_aes_256_gcm(void)
79{
80 return test_encrypt_decrypt(EVP_aes_256_gcm());
81}
82
a43ce58f
SL
83OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
84
e15e92db
MC
85int setup_tests(void)
86{
87 char *certin = NULL, *privkeyin = NULL;
88 BIO *certbio = NULL, *privkeybio = NULL;
89
8d242823
MC
90 if (!test_skip_common_options()) {
91 TEST_error("Error parsing test options\n");
92 return 0;
93 }
94
e15e92db
MC
95 if (!TEST_ptr(certin = test_get_argument(0))
96 || !TEST_ptr(privkeyin = test_get_argument(1)))
97 return 0;
98
99 certbio = BIO_new_file(certin, "r");
100 if (!TEST_ptr(certbio))
101 return 0;
102 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
103 BIO_free(certbio);
104 return 0;
105 }
106 BIO_free(certbio);
107
108 privkeybio = BIO_new_file(privkeyin, "r");
109 if (!TEST_ptr(privkeybio)) {
110 X509_free(cert);
111 cert = NULL;
112 return 0;
113 }
114 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
115 BIO_free(privkeybio);
116 X509_free(cert);
117 cert = NULL;
118 return 0;
119 }
120 BIO_free(privkeybio);
121
924663c3
JZ
122 ADD_TEST(test_encrypt_decrypt_aes_cbc);
123 ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
124 ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
125 ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
e15e92db
MC
126
127 return 1;
128}
129
130void cleanup_tests(void)
131{
132 X509_free(cert);
133 EVP_PKEY_free(privkey);
134}