2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include "internal/nelem.h"
16 #include <openssl/pkcs12.h>
17 #include <openssl/x509.h>
18 #include <openssl/x509v3.h>
19 #include <openssl/pem.h>
22 #include "helpers/pkcs12.h"
24 static OSSL_LIB_CTX *testctx = NULL;
25 static OSSL_PROVIDER *nullprov = NULL;
26 static OSSL_PROVIDER *deflprov = NULL;
28 static int test_null_args(void)
30 return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL));
33 static PKCS12 *PKCS12_load(const char *fpath)
38 bio = BIO_new_file(fpath, "rb");
42 p12 = PKCS12_init(NID_pkcs7_data);
46 if (!TEST_true(p12 == d2i_PKCS12_bio(bio, &p12)))
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;
64 static int pkcs12_parse_test(void)
70 STACK_OF(X509) *ca = NULL;
72 if (in_file != NULL) {
73 p12 = PKCS12_load(in_file);
77 if (!TEST_true(PKCS12_parse(p12, in_pass, &key, &cert, &ca)))
80 if ((has_key && !TEST_ptr(key)) || (!has_key && !TEST_ptr_null(key)))
82 if ((has_cert && !TEST_ptr(cert)) || (!has_cert && !TEST_ptr_null(cert)))
84 if ((has_ca && !TEST_ptr(ca)) || (!has_ca && !TEST_ptr_null(ca)))
93 OSSL_STACK_OF_X509_free(ca);
94 return TEST_true(ret);
97 static int pkcs12_create_cb(PKCS12_SAFEBAG *bag, void *cbarg)
99 int cb_ret = *((int*)cbarg);
103 static PKCS12 *pkcs12_create_ex2_setup(EVP_PKEY **key, X509 **cert, STACK_OF(X509) **ca)
106 p12 = PKCS12_load("out6.p12");
110 if (!TEST_true(PKCS12_parse(p12, "", key, cert, ca)))
119 static int pkcs12_create_ex2_test(int test)
121 int ret = 0, cb_ret = 0;
122 PKCS12 *ptr = NULL, *p12 = NULL;
123 EVP_PKEY *key = NULL;
125 STACK_OF(X509) *ca = NULL;
127 p12 = pkcs12_create_ex2_setup(&key, &cert, &ca);
132 /* Confirm PKCS12_create_ex2 returns NULL */
133 ptr = PKCS12_create_ex2(NULL, NULL, NULL,
134 NULL, NULL, NID_undef, NID_undef,
141 /* Can't proceed without a valid cert at least */
145 /* Specified call back called - return success */
147 ptr = PKCS12_create_ex2(NULL, NULL, NULL,
148 cert, NULL, NID_undef, NID_undef,
151 pkcs12_create_cb, (void*)&cb_ret);
152 /* PKCS12 successfully created */
155 } else if (test == 1) {
156 /* Specified call back called - return error*/
158 ptr = PKCS12_create_ex2(NULL, NULL, NULL,
159 cert, NULL, NID_undef, NID_undef,
162 pkcs12_create_cb, (void*)&cb_ret);
163 /* PKCS12 not created */
166 } else if (test == 2) {
167 /* Specified call back called - return failure */
169 ptr = PKCS12_create_ex2(NULL, NULL, NULL,
170 cert, NULL, NID_undef, NID_undef,
173 pkcs12_create_cb, (void*)&cb_ret);
174 /* PKCS12 successfully created */
185 OSSL_STACK_OF_X509_free(ca);
186 return TEST_true(ret);
189 typedef enum OPTION_choice {
201 const OPTIONS *test_get_options(void)
203 static const OPTIONS options[] = {
204 OPT_TEST_OPTIONS_DEFAULT_USAGE,
205 { "in", OPT_IN_FILE, '<', "PKCS12 input file" },
206 { "pass", OPT_IN_PASS, 's', "PKCS12 input file password" },
207 { "has-key", OPT_IN_HAS_KEY, 'n', "Whether the input file does contain an user key" },
208 { "has-cert", OPT_IN_HAS_CERT, 'n', "Whether the input file does contain an user certificate" },
209 { "has-ca", OPT_IN_HAS_CA, 'n', "Whether the input file does contain other certificate" },
210 { "legacy", OPT_LEGACY, '-', "Test the legacy APIs" },
216 int setup_tests(void)
220 while ((o = opt_next()) != OPT_EOF) {
231 has_key = opt_int_arg();
233 case OPT_IN_HAS_CERT:
234 has_cert = opt_int_arg();
237 has_ca = opt_int_arg();
246 deflprov = OSSL_PROVIDER_load(testctx, "default");
247 if (!TEST_ptr(deflprov))
250 ADD_TEST(test_null_args);
251 ADD_TEST(pkcs12_parse_test);
252 ADD_ALL_TESTS(pkcs12_create_ex2_test, 3);
256 void cleanup_tests(void)
258 OSSL_PROVIDER_unload(nullprov);
259 OSSL_PROVIDER_unload(deflprov);
260 OSSL_LIB_CTX_free(testctx);