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