]>
Commit | Line | Data |
---|---|---|
a43ce58f SL |
1 | /* |
2 | * Copyright 2018 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the OpenSSL license (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 | ||
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 | ||
19 | static X509 *cert = NULL; | |
20 | static EVP_PKEY *privkey = NULL; | |
21 | ||
22 | static int test_encrypt_decrypt(void) | |
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, EVP_aes_128_cbc(), 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 | ||
a43ce58f SL |
61 | OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") |
62 | ||
e15e92db MC |
63 | int setup_tests(void) |
64 | { | |
65 | char *certin = NULL, *privkeyin = NULL; | |
66 | BIO *certbio = NULL, *privkeybio = NULL; | |
67 | ||
8d242823 MC |
68 | if (!test_skip_common_options()) { |
69 | TEST_error("Error parsing test options\n"); | |
70 | return 0; | |
71 | } | |
72 | ||
e15e92db MC |
73 | if (!TEST_ptr(certin = test_get_argument(0)) |
74 | || !TEST_ptr(privkeyin = test_get_argument(1))) | |
75 | return 0; | |
76 | ||
77 | certbio = BIO_new_file(certin, "r"); | |
78 | if (!TEST_ptr(certbio)) | |
79 | return 0; | |
80 | if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) { | |
81 | BIO_free(certbio); | |
82 | return 0; | |
83 | } | |
84 | BIO_free(certbio); | |
85 | ||
86 | privkeybio = BIO_new_file(privkeyin, "r"); | |
87 | if (!TEST_ptr(privkeybio)) { | |
88 | X509_free(cert); | |
89 | cert = NULL; | |
90 | return 0; | |
91 | } | |
92 | if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) { | |
93 | BIO_free(privkeybio); | |
94 | X509_free(cert); | |
95 | cert = NULL; | |
96 | return 0; | |
97 | } | |
98 | BIO_free(privkeybio); | |
99 | ||
100 | ADD_TEST(test_encrypt_decrypt); | |
101 | ||
102 | return 1; | |
103 | } | |
104 | ||
105 | void cleanup_tests(void) | |
106 | { | |
107 | X509_free(cert); | |
108 | EVP_PKEY_free(privkey); | |
109 | } |