1 /*
2 * Copyright 2022 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 <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 #include "internal/nelem.h"
15
16 #include <openssl/pkcs12.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
20
21 #include "testutil.h"
22 #include "helpers/pkcs12.h"
23
24 static OSSL_LIB_CTX *testctx = NULL;
25 static OSSL_PROVIDER *nullprov = NULL;
26 static OSSL_PROVIDER *deflprov = NULL;
27
test_null_args(void)28 static int test_null_args(void)
29 {
30 return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL));
31 }
32
PKCS12_load(const char * fpath)33 static PKCS12 *PKCS12_load(const char *fpath)
34 {
35 BIO *bio = NULL;
36 PKCS12 *p12 = NULL;
37
38 bio = BIO_new_file(fpath, "r");
39 if (!TEST_ptr(bio))
40 goto err;
41
42 p12 = PKCS12_init(NID_pkcs7_data);
43 if (!TEST_ptr(p12))
44 goto err;
45
46 if (!TEST_true(p12 == d2i_PKCS12_bio(bio, &p12)))
47 goto err;
48
49 BIO_free(bio);
50
51 return p12;
52
53 err:
54 BIO_free(bio);
55 PKCS12_free(p12);
56 return NULL;
57 }
58
59 static const char *in_file = NULL;
60 static const char *in_pass = "";
61 static int has_key = 0;
62 static int has_cert = 0;
63 static int has_ca = 0;
pkcs12_parse_test(void)64 static int pkcs12_parse_test(void)
65 {
66 int ret = 0;
67 PKCS12 *p12 = NULL;
68 EVP_PKEY *key = NULL;
69 X509 *cert = NULL;
70 STACK_OF(X509) *ca = NULL;
71
72 if (in_file != NULL) {
73 p12 = PKCS12_load(in_file);
74 if (!TEST_ptr(p12))
75 goto err;
76
77 if (!TEST_true(PKCS12_parse(p12, in_pass, &key, &cert, &ca)))
78 goto err;
79
80 if ((has_key && !TEST_ptr(key)) || (!has_key && !TEST_ptr_null(key)))
81 goto err;
82 if ((has_cert && !TEST_ptr(cert)) || (!has_cert && !TEST_ptr_null(cert)))
83 goto err;
84 if ((has_ca && !TEST_ptr(ca)) || (!has_ca && !TEST_ptr_null(ca)))
85 goto err;
86 }
87
88 ret = 1;
89 err:
90 PKCS12_free(p12);
91 EVP_PKEY_free(key);
92 X509_free(cert);
93 OSSL_STACK_OF_X509_free(ca);
94 return TEST_true(ret);
95 }
96
97 typedef enum OPTION_choice {
98 OPT_ERR = -1,
99 OPT_EOF = 0,
100 OPT_IN_FILE,
101 OPT_IN_PASS,
102 OPT_IN_HAS_KEY,
103 OPT_IN_HAS_CERT,
104 OPT_IN_HAS_CA,
105 OPT_LEGACY,
106 OPT_TEST_ENUM
107 } OPTION_CHOICE;
108
test_get_options(void)109 const OPTIONS *test_get_options(void)
110 {
111 static const OPTIONS options[] = {
112 OPT_TEST_OPTIONS_DEFAULT_USAGE,
113 { "in", OPT_IN_FILE, '<', "PKCS12 input file" },
114 { "pass", OPT_IN_PASS, 's', "PKCS12 input file password" },
115 { "has-key", OPT_IN_HAS_KEY, 'n', "Whether the input file does contain an user key" },
116 { "has-cert", OPT_IN_HAS_CERT, 'n', "Whether the input file does contain an user certificate" },
117 { "has-ca", OPT_IN_HAS_CA, 'n', "Whether the input file does contain other certificate" },
118 { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" },
119 { NULL }
120 };
121 return options;
122 }
123
setup_tests(void)124 int setup_tests(void)
125 {
126 OPTION_CHOICE o;
127
128 while ((o = opt_next()) != OPT_EOF) {
129 switch (o) {
130 case OPT_IN_FILE:
131 in_file = opt_arg();
132 break;
133 case OPT_IN_PASS:
134 in_pass = opt_arg();
135 break;
136 case OPT_LEGACY:
137 break;
138 case OPT_IN_HAS_KEY:
139 has_key = opt_int_arg();
140 break;
141 case OPT_IN_HAS_CERT:
142 has_cert = opt_int_arg();
143 break;
144 case OPT_IN_HAS_CA:
145 has_ca = opt_int_arg();
146 break;
147 case OPT_TEST_CASES:
148 break;
149 default:
150 return 0;
151 }
152 }
153
154 deflprov = OSSL_PROVIDER_load(testctx, "default");
155 if (!TEST_ptr(deflprov))
156 return 0;
157
158 ADD_TEST(test_null_args);
159 ADD_TEST(pkcs12_parse_test);
160
161 return 1;
162 }
163
cleanup_tests(void)164 void cleanup_tests(void)
165 {
166 OSSL_PROVIDER_unload(nullprov);
167 OSSL_PROVIDER_unload(deflprov);
168 OSSL_LIB_CTX_free(testctx);
169 }
170