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