summaryrefslogtreecommitdiffstats
path: root/test/pkcs12_api_test.c
blob: 5afef5a16ad3aa5da3ae82b9c3c43b1a56952b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "internal/nelem.h"

#include <openssl/pkcs12.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>

#include "testutil.h"
#include "helpers/pkcs12.h"

static OSSL_LIB_CTX *testctx = NULL;
static OSSL_PROVIDER *nullprov = NULL;

static int test_null_args(void)
{
    return TEST_false(PKCS12_parse(NULL, NULL, NULL, NULL, NULL));
}

static PKCS12 *PKCS12_load(const char *fpath)
{
    BIO *bio = NULL;
    PKCS12 *p12 = NULL;

    bio = BIO_new_file(fpath, "rb");
    if (!TEST_ptr(bio))
        goto err;

    p12 = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default");
    if (!TEST_ptr(p12))
        goto err;

    if (!TEST_true(p12 == d2i_PKCS12_bio(bio, &p12)))
        goto err;

    BIO_free(bio);

    return p12;

err:
    BIO_free(bio);
    PKCS12_free(p12);
    return NULL;
}

static const char *in_file = NULL;
static const char *in_pass = "";
static int has_key = 0;
static int has_cert = 0;
static int has_ca = 0;

static int changepass(PKCS12 *p12, EVP_PKEY *key, X509 *cert, STACK_OF(X509) *ca)
{
    int ret = 0;
    PKCS12 *p12new = NULL;
    EVP_PKEY *key2 = NULL;
    X509 *cert2 = NULL;
    STACK_OF(X509) *ca2 = NULL;
    BIO *bio = NULL;

    if (!TEST_true(PKCS12_newpass(p12, in_pass, "NEWPASS")))
        goto err;
    if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
        goto err;
    if (!TEST_true(i2d_PKCS12_bio(bio, p12)))
        goto err;
    if (!TEST_ptr(p12new = PKCS12_init_ex(NID_pkcs7_data, testctx, "provider=default")))
        goto err;
    if (!TEST_ptr(d2i_PKCS12_bio(bio, &p12new)))
        goto err;
    if (!TEST_true(PKCS12_parse(p12new, "NEWPASS", &key2, &cert2, &ca2)))
        goto err;
    if (has_key) {
        if (!TEST_ptr(key2) || !TEST_int_eq(EVP_PKEY_eq(key, key2), 1))
            goto err;
    }
    if (has_cert) {
        if (!TEST_ptr(cert2) || !TEST_int_eq(X509_cmp(cert, cert2), 0))
            goto err;
    }
    ret = 1;
err:
    BIO_free(bio);
    PKCS12_free(p12new);
    EVP_PKEY_free(key2);
    X509_free(cert2);
    OSSL_STACK_OF_X509_free(ca2);
    return ret;
}

static int pkcs12_parse_test(void)
{
    int ret = 0;
    PKCS12 *p12 = NULL;
    EVP_PKEY *key = NULL;
    X509 *cert = NULL;
    STACK_OF(X509) *ca = NULL;

    if (in_file != NULL) {
        p12 = PKCS12_load(in_file);
        if (!TEST_ptr(p12))
            goto err;

        if (!TEST_true(PKCS12_parse(p12, in_pass, &key, &cert, &ca)))
            goto err;

        if ((has_key && !TEST_ptr(key)) || (!has_key && !TEST_ptr_null(key)))
            goto err;
        if ((has_cert && !TEST_ptr(cert)) || (!has_cert && !TEST_ptr_null(cert)))
            goto err;
        if ((has_ca && !TEST_ptr(ca)) || (!has_ca && !TEST_ptr_null(ca)))
            goto err;
        if (has_key && !changepass(p12, key, cert, ca))
            goto err;
    }
    ret = 1;
err:
    PKCS12_free(p12);
    EVP_PKEY_free(key);
    X509_free(cert);
    OSSL_STACK_OF_X509_free(ca);
    return TEST_true(ret);
}

static int pkcs12_create_cb(PKCS12_SAFEBAG *bag, void *cbarg)
{
    int cb_ret = *((int*)cbarg);
    return cb_ret;
}

static PKCS12 *pkcs12_create_ex2_setup(EVP_PKEY **key, X509 **cert, STACK_OF(X509) **ca)
{
    PKCS12 *p12 = NULL;
    p12 = PKCS12_load("out6.p12");
    if (!TEST_ptr(p12))
        goto err;

    if (!TEST_true(PKCS12_parse(p12, "", key, cert, ca)))
        goto err;

    return p12;
err:
    PKCS12_free(p12);
    return NULL;
}

static int pkcs12_create_ex2_test(int test)
{
    int ret = 0, cb_ret = 0;
    PKCS12 *ptr = NULL, *p12 = NULL;
    EVP_PKEY *key = NULL;
    X509 *cert = NULL;
    STACK_OF(X509) *ca = NULL;

    p12 = pkcs12_create_ex2_setup(&key, &cert, &ca);
    if (!TEST_ptr(p12))
        goto err;

    if (test == 0) {
        /* Confirm PKCS12_create_ex2 returns NULL */
        ptr = PKCS12_create_ex2(NULL, NULL, NULL,
                                NULL, NULL, NID_undef, NID_undef,
                                0, 0, 0,
                                testctx, NULL,
                                NULL, NULL);
        if (TEST_ptr(ptr))
            goto err;

        /* Can't proceed without a valid cert at least */
        if (!TEST_ptr(cert))
            goto err;

        /* Specified call back called - return success */
        cb_ret = 1;
        ptr = PKCS12_create_ex2(NULL, NULL, NULL,
                                cert, NULL, NID_undef, NID_undef,
                                0, 0, 0,
                                testctx, NULL,
                                pkcs12_create_cb, (void*)&cb_ret);
        /* PKCS12 successfully created */
        if (!TEST_ptr(ptr))
            goto err;
    } else if (test == 1) {
        /* Specified call back called - return error*/
        cb_ret = -1;
        ptr = PKCS12_create_ex2(NULL, NULL, NULL,
                                cert, NULL, NID_undef, NID_undef,
                                0, 0, 0,
                                testctx, NULL,
                                pkcs12_create_cb, (void*)&cb_ret);
        /* PKCS12 not created */
       if (TEST_ptr(ptr))
            goto err;
    } else if (test == 2) {
        /* Specified call back called - return failure */
        cb_ret = 0;
        ptr = PKCS12_create_ex2(NULL, NULL, NULL,
                                cert, NULL, NID_undef, NID_undef,
                                0, 0, 0,
                                testctx, NULL,
                                pkcs12_create_cb, (void*)&cb_ret);
        /* PKCS12 successfully created */
        if (!TEST_ptr(ptr))
            goto err;
    }

    ret = 1;
err:
    PKCS12_free(p12);
    PKCS12_free(ptr);
    EVP_PKEY_free(key);
    X509_free(cert);
    OSSL_STACK_OF_X509_free(ca);
    return TEST_true(ret);
}

typedef enum OPTION_choice {
    OPT_ERR = -1,
    OPT_EOF = 0,
    OPT_IN_FILE,
    OPT_IN_PASS,
    OPT_IN_HAS_KEY,
    OPT_IN_HAS_CERT,
    OPT_IN_HAS_CA,
    OPT_LEGACY,
    OPT_TEST_ENUM
} OPTION_CHOICE;

const OPTIONS *test_get_options(void)
{
    static const OPTIONS options[] = {
        OPT_TEST_OPTIONS_DEFAULT_USAGE,
        { "in",   OPT_IN_FILE,   '<', "PKCS12 input file" },
        { "pass",   OPT_IN_PASS,   's', "PKCS12 input file password" },
        { "has-key",   OPT_IN_HAS_KEY,  'n', "Whether the input file does contain an user key" },
        { "has-cert",   OPT_IN_HAS_CERT, 'n', "Whether the input file does contain an user certificate" },
        { "has-ca",   OPT_IN_HAS_CA,   'n', "Whether the input file does contain other certificate" },
        { "legacy",  OPT_LEGACY,  '-', "Test the legacy APIs" },
        { NULL }
    };
    return options;
}

int setup_tests(void)
{
    OPTION_CHOICE o;

    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        case OPT_IN_FILE:
            in_file = opt_arg();
            break;
        case OPT_IN_PASS:
            in_pass = opt_arg();
            break;
        case OPT_LEGACY:
            break;
        case OPT_IN_HAS_KEY:
            has_key = opt_int_arg();
            break;
        case OPT_IN_HAS_CERT:
            has_cert = opt_int_arg();
            break;
        case OPT_IN_HAS_CA:
            has_ca = opt_int_arg();
            break;
        case OPT_TEST_CASES:
            break;
        default:
            return 0;
        }
    }

    if (!test_get_libctx(&testctx, &nullprov, NULL, NULL, NULL)) {
        OSSL_LIB_CTX_free(testctx);
        testctx = NULL;
        return 0;
    }

    ADD_TEST(test_null_args);
    ADD_TEST(pkcs12_parse_test);
    ADD_ALL_TESTS(pkcs12_create_ex2_test, 3);
    return 1;
}

void cleanup_tests(void)
{
    OSSL_LIB_CTX_free(testctx);
    OSSL_PROVIDER_unload(nullprov);
}