2 * Copyright 2022-2023 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
;
27 static int test_null_args(void)
29 return TEST_false(PKCS12_parse(NULL
, NULL
, NULL
, NULL
, NULL
));
32 static PKCS12
*PKCS12_load(const char *fpath
)
37 bio
= BIO_new_file(fpath
, "rb");
41 p12
= PKCS12_init_ex(NID_pkcs7_data
, testctx
, "provider=default");
45 if (!TEST_true(p12
== d2i_PKCS12_bio(bio
, &p12
)))
58 static const char *in_file
= NULL
;
59 static const char *in_pass
= "";
60 static int has_key
= 0;
61 static int has_cert
= 0;
62 static int has_ca
= 0;
64 static int changepass(PKCS12
*p12
, EVP_PKEY
*key
, X509
*cert
, STACK_OF(X509
) *ca
)
67 PKCS12
*p12new
= NULL
;
68 EVP_PKEY
*key2
= NULL
;
70 STACK_OF(X509
) *ca2
= NULL
;
73 if (!TEST_true(PKCS12_newpass(p12
, in_pass
, "NEWPASS")))
75 if (!TEST_ptr(bio
= BIO_new(BIO_s_mem())))
77 if (!TEST_true(i2d_PKCS12_bio(bio
, p12
)))
79 if (!TEST_ptr(p12new
= PKCS12_init_ex(NID_pkcs7_data
, testctx
, "provider=default")))
81 if (!TEST_ptr(d2i_PKCS12_bio(bio
, &p12new
)))
83 if (!TEST_true(PKCS12_parse(p12new
, "NEWPASS", &key2
, &cert2
, &ca2
)))
86 if (!TEST_ptr(key2
) || !TEST_int_eq(EVP_PKEY_eq(key
, key2
), 1))
90 if (!TEST_ptr(cert2
) || !TEST_int_eq(X509_cmp(cert
, cert2
), 0))
99 OSSL_STACK_OF_X509_free(ca2
);
103 static int pkcs12_parse_test(void)
107 EVP_PKEY
*key
= NULL
;
109 STACK_OF(X509
) *ca
= NULL
;
111 if (in_file
!= NULL
) {
112 p12
= PKCS12_load(in_file
);
116 if (!TEST_true(PKCS12_parse(p12
, in_pass
, &key
, &cert
, &ca
)))
119 if ((has_key
&& !TEST_ptr(key
)) || (!has_key
&& !TEST_ptr_null(key
)))
121 if ((has_cert
&& !TEST_ptr(cert
)) || (!has_cert
&& !TEST_ptr_null(cert
)))
123 if ((has_ca
&& !TEST_ptr(ca
)) || (!has_ca
&& !TEST_ptr_null(ca
)))
125 if (has_key
&& !changepass(p12
, key
, cert
, ca
))
133 OSSL_STACK_OF_X509_free(ca
);
134 return TEST_true(ret
);
137 static int pkcs12_create_cb(PKCS12_SAFEBAG
*bag
, void *cbarg
)
139 int cb_ret
= *((int*)cbarg
);
143 static PKCS12
*pkcs12_create_ex2_setup(EVP_PKEY
**key
, X509
**cert
, STACK_OF(X509
) **ca
)
146 p12
= PKCS12_load("out6.p12");
150 if (!TEST_true(PKCS12_parse(p12
, "", key
, cert
, ca
)))
159 static int pkcs12_create_ex2_test(int test
)
161 int ret
= 0, cb_ret
= 0;
162 PKCS12
*ptr
= NULL
, *p12
= NULL
;
163 EVP_PKEY
*key
= NULL
;
165 STACK_OF(X509
) *ca
= NULL
;
167 p12
= pkcs12_create_ex2_setup(&key
, &cert
, &ca
);
172 /* Confirm PKCS12_create_ex2 returns NULL */
173 ptr
= PKCS12_create_ex2(NULL
, NULL
, NULL
,
174 NULL
, NULL
, NID_undef
, NID_undef
,
181 /* Can't proceed without a valid cert at least */
185 /* Specified call back called - return success */
187 ptr
= PKCS12_create_ex2(NULL
, NULL
, NULL
,
188 cert
, NULL
, NID_undef
, NID_undef
,
191 pkcs12_create_cb
, (void*)&cb_ret
);
192 /* PKCS12 successfully created */
195 } else if (test
== 1) {
196 /* Specified call back called - return error*/
198 ptr
= PKCS12_create_ex2(NULL
, NULL
, NULL
,
199 cert
, NULL
, NID_undef
, NID_undef
,
202 pkcs12_create_cb
, (void*)&cb_ret
);
203 /* PKCS12 not created */
206 } else if (test
== 2) {
207 /* Specified call back called - return failure */
209 ptr
= PKCS12_create_ex2(NULL
, NULL
, NULL
,
210 cert
, NULL
, NID_undef
, NID_undef
,
213 pkcs12_create_cb
, (void*)&cb_ret
);
214 /* PKCS12 successfully created */
225 OSSL_STACK_OF_X509_free(ca
);
226 return TEST_true(ret
);
229 typedef enum OPTION_choice
{
241 const OPTIONS
*test_get_options(void)
243 static const OPTIONS options
[] = {
244 OPT_TEST_OPTIONS_DEFAULT_USAGE
,
245 { "in", OPT_IN_FILE
, '<', "PKCS12 input file" },
246 { "pass", OPT_IN_PASS
, 's', "PKCS12 input file password" },
247 { "has-key", OPT_IN_HAS_KEY
, 'n', "Whether the input file does contain an user key" },
248 { "has-cert", OPT_IN_HAS_CERT
, 'n', "Whether the input file does contain an user certificate" },
249 { "has-ca", OPT_IN_HAS_CA
, 'n', "Whether the input file does contain other certificate" },
250 { "legacy", OPT_LEGACY
, '-', "Test the legacy APIs" },
256 int setup_tests(void)
260 while ((o
= opt_next()) != OPT_EOF
) {
271 has_key
= opt_int_arg();
273 case OPT_IN_HAS_CERT
:
274 has_cert
= opt_int_arg();
277 has_ca
= opt_int_arg();
286 if (!test_get_libctx(&testctx
, &nullprov
, NULL
, NULL
, NULL
)) {
287 OSSL_LIB_CTX_free(testctx
);
292 ADD_TEST(test_null_args
);
293 ADD_TEST(pkcs12_parse_test
);
294 ADD_ALL_TESTS(pkcs12_create_ex2_test
, 3);
298 void cleanup_tests(void)
300 OSSL_LIB_CTX_free(testctx
);
301 OSSL_PROVIDER_unload(nullprov
);