Update fips version check to be more robust
[openssl.git] / test / pkcs12_api_test.c
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
28 static int test_null_args(void)
29 {
30     return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL));
31 }
32
33 static PKCS12 *PKCS12_load(const char *fpath)
34 {
35     BIO *bio = NULL;
36     PKCS12 *p12 = NULL;
37
38     bio = BIO_new_file(fpath, "rb");
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;
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 static int pkcs12_create_cb(PKCS12_SAFEBAG *bag, void *cbarg)
98 {
99     int cb_ret = *((int*)cbarg);
100     return cb_ret;
101 }
102
103 static PKCS12 *pkcs12_create_ex2_setup(EVP_PKEY **key, X509 **cert, STACK_OF(X509) **ca)
104 {
105     PKCS12 *p12 = NULL;
106     p12 = PKCS12_load("out6.p12");
107     if (!TEST_ptr(p12))
108         goto err;
109
110     if (!TEST_true(PKCS12_parse(p12, "", key, cert, ca)))
111         goto err;
112
113     return p12;
114 err:
115     PKCS12_free(p12);
116     return NULL;
117 }
118
119 static int pkcs12_create_ex2_test(int test)
120 {
121     int ret = 0, cb_ret = 0;
122     PKCS12 *ptr = NULL, *p12 = NULL;
123     EVP_PKEY *key = NULL;
124     X509 *cert = NULL;
125     STACK_OF(X509) *ca = NULL;
126
127     p12 = pkcs12_create_ex2_setup(&key, &cert, &ca);
128     if (!TEST_ptr(p12))
129         goto err;
130
131     if (test == 0) {
132         /* Confirm PKCS12_create_ex2 returns NULL */
133         ptr = PKCS12_create_ex2(NULL, NULL, NULL,
134                                 NULL, NULL, NID_undef, NID_undef,
135                                 0, 0, 0,
136                                 NULL, NULL,
137                                 NULL, NULL);
138         if (TEST_ptr(ptr))
139             goto err;
140
141         /* Can't proceed without a valid cert at least */
142         if (!TEST_ptr(cert))
143             goto err;
144
145         /* Specified call back called - return success */
146         cb_ret = 1;
147         ptr = PKCS12_create_ex2(NULL, NULL, NULL,
148                                 cert, NULL, NID_undef, NID_undef,
149                                 0, 0, 0,
150                                 NULL, NULL,
151                                 pkcs12_create_cb, (void*)&cb_ret);
152         /* PKCS12 successfully created */
153         if (!TEST_ptr(ptr))
154             goto err;
155     } else if (test == 1) {
156         /* Specified call back called - return error*/
157         cb_ret = -1;
158         ptr = PKCS12_create_ex2(NULL, NULL, NULL,
159                                 cert, NULL, NID_undef, NID_undef,
160                                 0, 0, 0,
161                                 NULL, NULL,
162                                 pkcs12_create_cb, (void*)&cb_ret);
163         /* PKCS12 not created */
164        if (TEST_ptr(ptr))
165             goto err;
166     } else if (test == 2) {
167         /* Specified call back called - return failure */
168         cb_ret = 0;
169         ptr = PKCS12_create_ex2(NULL, NULL, NULL,
170                                 cert, NULL, NID_undef, NID_undef,
171                                 0, 0, 0,
172                                 NULL, NULL,
173                                 pkcs12_create_cb, (void*)&cb_ret);
174         /* PKCS12 successfully created */
175         if (!TEST_ptr(ptr))
176             goto err;
177     }
178
179     ret = 1;
180 err:
181     PKCS12_free(p12);
182     PKCS12_free(ptr);
183     EVP_PKEY_free(key);
184     X509_free(cert);
185     OSSL_STACK_OF_X509_free(ca);
186     return TEST_true(ret);
187 }
188
189 typedef enum OPTION_choice {
190     OPT_ERR = -1,
191     OPT_EOF = 0,
192     OPT_IN_FILE,
193     OPT_IN_PASS,
194     OPT_IN_HAS_KEY,
195     OPT_IN_HAS_CERT,
196     OPT_IN_HAS_CA,
197     OPT_LEGACY,
198     OPT_TEST_ENUM
199 } OPTION_CHOICE;
200
201 const OPTIONS *test_get_options(void)
202 {
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" },
211         { NULL }
212     };
213     return options;
214 }
215
216 int setup_tests(void)
217 {
218     OPTION_CHOICE o;
219
220     while ((o = opt_next()) != OPT_EOF) {
221         switch (o) {
222         case OPT_IN_FILE:
223             in_file = opt_arg();
224             break;
225         case OPT_IN_PASS:
226             in_pass = opt_arg();
227             break;
228         case OPT_LEGACY:
229             break;
230         case OPT_IN_HAS_KEY:
231             has_key = opt_int_arg();
232             break;
233         case OPT_IN_HAS_CERT:
234             has_cert = opt_int_arg();
235             break;
236         case OPT_IN_HAS_CA:
237             has_ca = opt_int_arg();
238             break;
239         case OPT_TEST_CASES:
240             break;
241         default:
242             return 0;
243         }
244     }
245
246     deflprov = OSSL_PROVIDER_load(testctx, "default");
247     if (!TEST_ptr(deflprov))
248         return 0;
249
250     ADD_TEST(test_null_args);
251     ADD_TEST(pkcs12_parse_test);
252     ADD_ALL_TESTS(pkcs12_create_ex2_test, 3);
253     return 1;
254 }
255
256 void cleanup_tests(void)
257 {
258     OSSL_PROVIDER_unload(nullprov);
259     OSSL_PROVIDER_unload(deflprov);
260     OSSL_LIB_CTX_free(testctx);
261 }