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 typedef enum OPTION_choice {
109 const OPTIONS *test_get_options(void)
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" },
124 int setup_tests(void)
128 while ((o = opt_next()) != OPT_EOF) {
139 has_key = opt_int_arg();
141 case OPT_IN_HAS_CERT:
142 has_cert = opt_int_arg();
145 has_ca = opt_int_arg();
154 deflprov = OSSL_PROVIDER_load(testctx, "default");
155 if (!TEST_ptr(deflprov))
158 ADD_TEST(test_null_args);
159 ADD_TEST(pkcs12_parse_test);
164 void cleanup_tests(void)
166 OSSL_PROVIDER_unload(nullprov);
167 OSSL_PROVIDER_unload(deflprov);
168 OSSL_LIB_CTX_free(testctx);