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