s_client.pod: Fix grammar in NOTES section.
[openssl.git] / test / cmsapitest.c
1 /*
2  * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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
19 static X509 *cert = NULL;
20 static EVP_PKEY *privkey = NULL;
21
22 static int test_encrypt_decrypt(const EVP_CIPHER *cipher)
23 {
24     int testresult = 0;
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;
30     char buf[80];
31
32     if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
33         goto end;
34
35     if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
36         goto end;
37
38     content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
39     if (!TEST_ptr(content))
40         goto end;
41
42     if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
43                                CMS_TEXT)))
44         goto end;
45
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))
49         goto end;
50
51     testresult = 1;
52  end:
53     sk_X509_free(certstack);
54     BIO_free(msgbio);
55     BIO_free(outmsgbio);
56     CMS_ContentInfo_free(content);
57
58     return testresult;
59 }
60
61 static int test_encrypt_decrypt_aes_cbc(void)
62 {
63     return test_encrypt_decrypt(EVP_aes_128_cbc());
64 }
65
66 static int test_encrypt_decrypt_aes_128_gcm(void)
67 {
68     return test_encrypt_decrypt(EVP_aes_128_gcm());
69 }
70
71 static int test_encrypt_decrypt_aes_192_gcm(void)
72 {
73     return test_encrypt_decrypt(EVP_aes_192_gcm());
74 }
75
76 static int test_encrypt_decrypt_aes_256_gcm(void)
77 {
78     return test_encrypt_decrypt(EVP_aes_256_gcm());
79 }
80
81 OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
82
83 int setup_tests(void)
84 {
85     char *certin = NULL, *privkeyin = NULL;
86     BIO *certbio = NULL, *privkeybio = NULL;
87
88     if (!test_skip_common_options()) {
89         TEST_error("Error parsing test options\n");
90         return 0;
91     }
92
93     if (!TEST_ptr(certin = test_get_argument(0))
94             || !TEST_ptr(privkeyin = test_get_argument(1)))
95         return 0;
96
97     certbio = BIO_new_file(certin, "r");
98     if (!TEST_ptr(certbio))
99         return 0;
100     if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
101         BIO_free(certbio);
102         return 0;
103     }
104     BIO_free(certbio);
105
106     privkeybio = BIO_new_file(privkeyin, "r");
107     if (!TEST_ptr(privkeybio)) {
108         X509_free(cert);
109         cert = NULL;
110         return 0;
111     }
112     if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
113         BIO_free(privkeybio);
114         X509_free(cert);
115         cert = NULL;
116         return 0;
117     }
118     BIO_free(privkeybio);
119
120     ADD_TEST(test_encrypt_decrypt_aes_cbc);
121     ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
122     ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
123     ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
124
125     return 1;
126 }
127
128 void cleanup_tests(void)
129 {
130     X509_free(cert);
131     EVP_PKEY_free(privkey);
132 }