1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <string.h>
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ci#include <openssl/pem.h>
14e1051a39Sopenharmony_ci#include <openssl/x509.h>
15e1051a39Sopenharmony_ci#include "testutil.h"
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_ci/*
18e1051a39Sopenharmony_ci * c: path of a cert in PEM format
19e1051a39Sopenharmony_ci * k: path of a key in PEM format
20e1051a39Sopenharmony_ci * t: API type, "cert" for X509_ and "req" for X509_REQ_ APIs.
21e1051a39Sopenharmony_ci * e: expected, "ok" for success, "failed" for what should fail.
22e1051a39Sopenharmony_ci */
23e1051a39Sopenharmony_cistatic const char *c;
24e1051a39Sopenharmony_cistatic const char *k;
25e1051a39Sopenharmony_cistatic const char *t;
26e1051a39Sopenharmony_cistatic const char *e;
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cistatic int test_x509_check_cert_pkey(void)
29e1051a39Sopenharmony_ci{
30e1051a39Sopenharmony_ci    BIO *bio = NULL;
31e1051a39Sopenharmony_ci    X509 *x509 = NULL;
32e1051a39Sopenharmony_ci    X509_REQ *x509_req = NULL;
33e1051a39Sopenharmony_ci    EVP_PKEY *pkey = NULL;
34e1051a39Sopenharmony_ci    int ret = 0, type = 0, expected = 0, result = 0;
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ci    /*
37e1051a39Sopenharmony_ci     * we check them first thus if fails we don't need to do
38e1051a39Sopenharmony_ci     * those PEM parsing operations.
39e1051a39Sopenharmony_ci     */
40e1051a39Sopenharmony_ci    if (strcmp(t, "cert") == 0) {
41e1051a39Sopenharmony_ci        type = 1;
42e1051a39Sopenharmony_ci    } else if (strcmp(t, "req") == 0) {
43e1051a39Sopenharmony_ci        type = 2;
44e1051a39Sopenharmony_ci    } else {
45e1051a39Sopenharmony_ci        TEST_error("invalid 'type'");
46e1051a39Sopenharmony_ci        goto failed;
47e1051a39Sopenharmony_ci    }
48e1051a39Sopenharmony_ci
49e1051a39Sopenharmony_ci    if (strcmp(e, "ok") == 0) {
50e1051a39Sopenharmony_ci        expected = 1;
51e1051a39Sopenharmony_ci    } else if (strcmp(e, "failed") == 0) {
52e1051a39Sopenharmony_ci        expected = 0;
53e1051a39Sopenharmony_ci    } else {
54e1051a39Sopenharmony_ci        TEST_error("invalid 'expected'");
55e1051a39Sopenharmony_ci        goto failed;
56e1051a39Sopenharmony_ci    }
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ci    /* process private key */
59e1051a39Sopenharmony_ci    if (!TEST_ptr(bio = BIO_new_file(k, "r")))
60e1051a39Sopenharmony_ci        goto failed;
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_ci    if (!TEST_ptr(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)))
63e1051a39Sopenharmony_ci        goto failed;
64e1051a39Sopenharmony_ci
65e1051a39Sopenharmony_ci    BIO_free(bio);
66e1051a39Sopenharmony_ci
67e1051a39Sopenharmony_ci    /* process cert or cert request, use the same local var */
68e1051a39Sopenharmony_ci    if (!TEST_ptr(bio = BIO_new_file(c, "r")))
69e1051a39Sopenharmony_ci        goto failed;
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_ci    switch (type) {
72e1051a39Sopenharmony_ci    case 1:
73e1051a39Sopenharmony_ci        x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
74e1051a39Sopenharmony_ci        if (x509 == NULL) {
75e1051a39Sopenharmony_ci            TEST_error("read PEM x509 failed");
76e1051a39Sopenharmony_ci            goto failed;
77e1051a39Sopenharmony_ci        }
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_ci        result = X509_check_private_key(x509, pkey);
80e1051a39Sopenharmony_ci        break;
81e1051a39Sopenharmony_ci    case 2:
82e1051a39Sopenharmony_ci        x509_req = PEM_read_bio_X509_REQ(bio, NULL, NULL, NULL);
83e1051a39Sopenharmony_ci        if (x509_req == NULL) {
84e1051a39Sopenharmony_ci            TEST_error("read PEM x509 req failed");
85e1051a39Sopenharmony_ci            goto failed;
86e1051a39Sopenharmony_ci        }
87e1051a39Sopenharmony_ci
88e1051a39Sopenharmony_ci        result = X509_REQ_check_private_key(x509_req, pkey);
89e1051a39Sopenharmony_ci        break;
90e1051a39Sopenharmony_ci    default:
91e1051a39Sopenharmony_ci        /* should never be here */
92e1051a39Sopenharmony_ci        break;
93e1051a39Sopenharmony_ci    }
94e1051a39Sopenharmony_ci
95e1051a39Sopenharmony_ci    if (!TEST_int_eq(result, expected)) {
96e1051a39Sopenharmony_ci        TEST_error("check private key: expected: %d, got: %d", expected, result);
97e1051a39Sopenharmony_ci        goto failed;
98e1051a39Sopenharmony_ci    }
99e1051a39Sopenharmony_ci
100e1051a39Sopenharmony_ci    ret = 1;
101e1051a39Sopenharmony_cifailed:
102e1051a39Sopenharmony_ci    BIO_free(bio);
103e1051a39Sopenharmony_ci    X509_free(x509);
104e1051a39Sopenharmony_ci    X509_REQ_free(x509_req);
105e1051a39Sopenharmony_ci    EVP_PKEY_free(pkey);
106e1051a39Sopenharmony_ci    return ret;
107e1051a39Sopenharmony_ci}
108e1051a39Sopenharmony_ci
109e1051a39Sopenharmony_cistatic const char *file; /* path of a cert/CRL/key file in PEM format */
110e1051a39Sopenharmony_cistatic const char *num;  /* expected number of certs/CRLs/keys included */
111e1051a39Sopenharmony_ci
112e1051a39Sopenharmony_cistatic int test_PEM_X509_INFO_read_bio(void)
113e1051a39Sopenharmony_ci{
114e1051a39Sopenharmony_ci    BIO *in;
115e1051a39Sopenharmony_ci    STACK_OF(X509_INFO) *sk;
116e1051a39Sopenharmony_ci    X509_INFO *it;
117e1051a39Sopenharmony_ci    int i, count = 0;
118e1051a39Sopenharmony_ci    int expected = 0;
119e1051a39Sopenharmony_ci
120e1051a39Sopenharmony_ci    if (!TEST_ptr((in = BIO_new_file(file, "r"))))
121e1051a39Sopenharmony_ci        return 0;
122e1051a39Sopenharmony_ci    sk = PEM_X509_INFO_read_bio(in, NULL, NULL, "");
123e1051a39Sopenharmony_ci    BIO_free(in);
124e1051a39Sopenharmony_ci    sscanf(num, "%d", &expected);
125e1051a39Sopenharmony_ci    for (i = 0; i < sk_X509_INFO_num(sk); i++) {
126e1051a39Sopenharmony_ci        it = sk_X509_INFO_value(sk, i);
127e1051a39Sopenharmony_ci        if (it->x509 != NULL)
128e1051a39Sopenharmony_ci            count++;
129e1051a39Sopenharmony_ci        if (it->crl != NULL)
130e1051a39Sopenharmony_ci            count++;
131e1051a39Sopenharmony_ci        if (it->x_pkey != NULL)
132e1051a39Sopenharmony_ci            count++;
133e1051a39Sopenharmony_ci    }
134e1051a39Sopenharmony_ci    sk_X509_INFO_pop_free(sk, X509_INFO_free);
135e1051a39Sopenharmony_ci    return TEST_int_eq(count, expected);
136e1051a39Sopenharmony_ci}
137e1051a39Sopenharmony_ci
138e1051a39Sopenharmony_ciconst OPTIONS *test_get_options(void)
139e1051a39Sopenharmony_ci{
140e1051a39Sopenharmony_ci    enum { OPT_TEST_ENUM };
141e1051a39Sopenharmony_ci    static const OPTIONS test_options[] = {
142e1051a39Sopenharmony_ci        OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("cert key type expected\n"
143e1051a39Sopenharmony_ci                                          "     or [options] file num\n"),
144e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "cert\tcertificate or CSR filename in PEM\n" },
145e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "key\tprivate key filename in PEM\n" },
146e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "type\t\tvalue must be 'cert' or 'req'\n" },
147e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "expected\tthe expected return value, either 'ok' or 'failed'\n" },
148e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "file\tPEM format file containing certs, keys, and/OR CRLs\n" },
149e1051a39Sopenharmony_ci        { OPT_HELP_STR, 1, '-', "num\texpected number of credentials to be loaded from file\n" },
150e1051a39Sopenharmony_ci        { NULL }
151e1051a39Sopenharmony_ci    };
152e1051a39Sopenharmony_ci    return test_options;
153e1051a39Sopenharmony_ci}
154e1051a39Sopenharmony_ci
155e1051a39Sopenharmony_ciint setup_tests(void)
156e1051a39Sopenharmony_ci{
157e1051a39Sopenharmony_ci    if (!test_skip_common_options()) {
158e1051a39Sopenharmony_ci        TEST_error("Error parsing test options\n");
159e1051a39Sopenharmony_ci        return 0;
160e1051a39Sopenharmony_ci    }
161e1051a39Sopenharmony_ci
162e1051a39Sopenharmony_ci    if (test_get_argument_count() == 2) {
163e1051a39Sopenharmony_ci        if (!TEST_ptr(file = test_get_argument(0))
164e1051a39Sopenharmony_ci                || !TEST_ptr(num = test_get_argument(1)))
165e1051a39Sopenharmony_ci            return 0;
166e1051a39Sopenharmony_ci        ADD_TEST(test_PEM_X509_INFO_read_bio);
167e1051a39Sopenharmony_ci        return 1;
168e1051a39Sopenharmony_ci    }
169e1051a39Sopenharmony_ci
170e1051a39Sopenharmony_ci    if (!TEST_ptr(c = test_get_argument(0))
171e1051a39Sopenharmony_ci            || !TEST_ptr(k = test_get_argument(1))
172e1051a39Sopenharmony_ci            || !TEST_ptr(t = test_get_argument(2))
173e1051a39Sopenharmony_ci            || !TEST_ptr(e = test_get_argument(3))) {
174e1051a39Sopenharmony_ci        return 0;
175e1051a39Sopenharmony_ci    }
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_ci    ADD_TEST(test_x509_check_cert_pkey);
178e1051a39Sopenharmony_ci    return 1;
179e1051a39Sopenharmony_ci}
180