]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/cmsapitest.c
Amend references to "OpenSSL license"
[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
24static int test_encrypt_decrypt(void)
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
40 content = CMS_encrypt(certstack, msgbio, EVP_aes_128_cbc(), CMS_TEXT);
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
a43ce58f
SL
63OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
64
e15e92db
MC
65int setup_tests(void)
66{
67 char *certin = NULL, *privkeyin = NULL;
68 BIO *certbio = NULL, *privkeybio = NULL;
69
8d242823
MC
70 if (!test_skip_common_options()) {
71 TEST_error("Error parsing test options\n");
72 return 0;
73 }
74
e15e92db
MC
75 if (!TEST_ptr(certin = test_get_argument(0))
76 || !TEST_ptr(privkeyin = test_get_argument(1)))
77 return 0;
78
79 certbio = BIO_new_file(certin, "r");
80 if (!TEST_ptr(certbio))
81 return 0;
82 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
83 BIO_free(certbio);
84 return 0;
85 }
86 BIO_free(certbio);
87
88 privkeybio = BIO_new_file(privkeyin, "r");
89 if (!TEST_ptr(privkeybio)) {
90 X509_free(cert);
91 cert = NULL;
92 return 0;
93 }
94 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
95 BIO_free(privkeybio);
96 X509_free(cert);
97 cert = NULL;
98 return 0;
99 }
100 BIO_free(privkeybio);
101
102 ADD_TEST(test_encrypt_decrypt);
103
104 return 1;
105}
106
107void cleanup_tests(void)
108{
109 X509_free(cert);
110 EVP_PKEY_free(privkey);
111}