Debian Codegraph @

crt/s2n/tests/unit/s2n_openssl_x509_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 #include "crypto/s2n_openssl_x509.h"
16
17 #include <openssl/pem.h>
18 #include <openssl/x509.h>
19
20 #include "crypto/s2n_rsa_pss.h"
21 #include "s2n_test.h"
22 #include "testlib/s2n_testlib.h"
23
24 S2N_RESULT s2n_x509_validator_read_asn1_cert(struct s2n_stuffer *cert_chain_in_stuffer,
25 struct s2n_blob *asn1_cert);
26
27 static S2N_RESULT s2n_test_assert_s2n_cert_info_equality(const struct s2n_cert_info *info_a,
28 const struct s2n_cert_info *info_b)
29 {
30 RESULT_ENSURE_EQ(info_a->public_key_bits, info_b->public_key_bits);
31 RESULT_ENSURE_EQ(info_a->public_key_nid, info_b->public_key_nid);
32 RESULT_ENSURE_EQ(info_a->signature_nid, info_b->signature_nid);
33 RESULT_ENSURE_EQ(info_a->signature_digest_nid, info_b->signature_digest_nid);
34 RESULT_ENSURE_EQ(info_a->self_signed, info_b->self_signed);
35 return S2N_RESULT_OK;
36 }
37
38 int main(int argc, char **argv)
39 {
40 BEGIN_TEST();
41
42 /* A certificate with one trailing byte is parsed successfully */
43 {
44 uint8_t cert_chain_data[S2N_MAX_TEST_PEM_SIZE] = { 0 };
45 uint32_t cert_chain_len = 0;
46 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_ONE_TRAILING_BYTE_CERT_BIN, cert_chain_data,
47 &cert_chain_len, S2N_MAX_TEST_PEM_SIZE));
48
49 struct s2n_blob cert_chain_blob = { 0 };
50 EXPECT_SUCCESS(s2n_blob_init(&cert_chain_blob, cert_chain_data, cert_chain_len));
51
52 DEFER_CLEANUP(struct s2n_stuffer cert_chain_stuffer = { 0 }, s2n_stuffer_free);
53 EXPECT_SUCCESS(s2n_stuffer_init_written(&cert_chain_stuffer, &cert_chain_blob));
54
55 struct s2n_blob cert_asn1_der = { 0 };
56 EXPECT_OK(s2n_x509_validator_read_asn1_cert(&cert_chain_stuffer, &cert_asn1_der));
57
58 {
59 DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer);
60 EXPECT_OK(s2n_openssl_x509_parse(&cert_asn1_der, &cert));
61 }
62 {
63 DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer);
64 EXPECT_OK(s2n_openssl_x509_parse_without_length_validation(&cert_asn1_der, &cert));
65 }
66 }
67
68 /* A certificate with too many trailing bytes errors */
69 {
70 uint8_t cert_chain_data[S2N_MAX_TEST_PEM_SIZE] = { 0 };
71 uint32_t cert_chain_len = 0;
72 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_FOUR_TRAILING_BYTE_CERT_BIN, cert_chain_data,
73 &cert_chain_len, S2N_MAX_TEST_PEM_SIZE));
74
75 struct s2n_blob cert_chain_blob = { 0 };
76 EXPECT_SUCCESS(s2n_blob_init(&cert_chain_blob, cert_chain_data, cert_chain_len));
77
78 DEFER_CLEANUP(struct s2n_stuffer cert_chain_stuffer = { 0 }, s2n_stuffer_free);
79 EXPECT_SUCCESS(s2n_stuffer_init_written(&cert_chain_stuffer, &cert_chain_blob));
80
81 struct s2n_blob cert_asn1_der = { 0 };
82 EXPECT_OK(s2n_x509_validator_read_asn1_cert(&cert_chain_stuffer, &cert_asn1_der));
83
84 {
85 DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer);
86 EXPECT_ERROR(s2n_openssl_x509_parse(&cert_asn1_der, &cert));
87 }
88 {
89 DEFER_CLEANUP(X509 *cert = NULL, X509_free_pointer);
90 EXPECT_OK(s2n_openssl_x509_parse_without_length_validation(&cert_asn1_der, &cert));
91 }
92 }
93
94 /* s2n_openssl_x509_get_cert_info */
95 struct {
96 const char *key_type;
97 const char *signature;
98 const char *key_size;
99 const char *digest;
100 int expected_signature_nid;
101 int expected_digest_nid;
102 int expected_public_key_nid;
103 int expected_public_key_bits;
104 } test_cases[] = {
105 {
106 .key_type = "ec",
107 .signature = "ecdsa",
108 .key_size = "p384",
109 .digest = "sha256",
110 .expected_signature_nid = NID_ecdsa_with_SHA256,
111 .expected_digest_nid = NID_sha256,
112 .expected_public_key_nid = NID_secp384r1,
113 .expected_public_key_bits = 384,
114 },
115 {
116 .key_type = "ec",
117 .signature = "ecdsa",
118 .key_size = "p256",
119 .digest = "sha384",
120 .expected_signature_nid = NID_ecdsa_with_SHA384,
121 .expected_digest_nid = NID_sha384,
122 .expected_public_key_nid = NID_X9_62_prime256v1,
123 .expected_public_key_bits = 256,
124 },
125 {
126 .key_type = "ec",
127 .signature = "ecdsa",
128 .key_size = "p521",
129 .digest = "sha512",
130 .expected_signature_nid = NID_ecdsa_with_SHA512,
131 .expected_digest_nid = NID_sha512,
132 .expected_public_key_nid = NID_secp521r1,
133 .expected_public_key_bits = 521,
134 },
135 {
136 .key_type = "rsae",
137 .signature = "pkcs",
138 .key_size = "2048",
139 .digest = "sha1",
140 .expected_signature_nid = NID_sha1WithRSAEncryption,
141 .expected_digest_nid = NID_sha1,
142 .expected_public_key_nid = NID_rsaEncryption,
143 .expected_public_key_bits = 2048,
144 },
145 {
146 .key_type = "rsae",
147 .signature = "pkcs",
148 .key_size = "2048",
149 .digest = "sha224",
150 .expected_signature_nid = NID_sha224WithRSAEncryption,
151 .expected_digest_nid = NID_sha224,
152 .expected_public_key_nid = NID_rsaEncryption,
153 .expected_public_key_bits = 2048,
154 },
155 {
156 .key_type = "rsae",
157 .signature = "pkcs",
158 .key_size = "3072",
159 .digest = "sha384",
160 .expected_signature_nid = NID_sha384WithRSAEncryption,
161 .expected_digest_nid = NID_sha384,
162 .expected_public_key_nid = NID_rsaEncryption,
163 .expected_public_key_bits = 3072,
164 },
165 #if RSA_PSS_CERTS_SUPPORTED
166 {
167 .key_type = "rsae",
168 .signature = "pss",
169 .key_size = "4096",
170 .digest = "sha384",
171 .expected_signature_nid = NID_rsassaPss,
172 .expected_digest_nid = NID_undef,
173 .expected_public_key_nid = NID_rsaEncryption,
174 .expected_public_key_bits = 4096,
175 },
176 {
177 .key_type = "rsapss",
178 .signature = "pss",
179 .key_size = "2048",
180 .digest = "sha256",
181 .expected_signature_nid = NID_rsassaPss,
182 .expected_digest_nid = NID_undef,
183 .expected_public_key_nid = NID_rsassaPss,
184 .expected_public_key_bits = 2048,
185 },
186 #endif
187 };
188
189 for (size_t i = 0; i < s2n_array_len(test_cases); i++) {
190 /* initialize variables and read in certificates */
191 char pathbuffer[S2N_MAX_TEST_PEM_PATH_LENGTH] = { 0 };
192 uint8_t cert_file[S2N_MAX_TEST_PEM_SIZE] = { 0 };
193 EXPECT_OK(s2n_test_cert_permutation_get_server_chain_path(&pathbuffer[0],
194 test_cases[i].key_type, test_cases[i].signature, test_cases[i].key_size,
195 test_cases[i].digest));
196 EXPECT_SUCCESS(s2n_read_test_pem(pathbuffer, (char *) cert_file, S2N_MAX_TEST_PEM_SIZE));
197
198 DEFER_CLEANUP(X509 *leaf = NULL, X509_free_pointer);
199 DEFER_CLEANUP(X509 *intermediate = NULL, X509_free_pointer);
200 DEFER_CLEANUP(X509 *root = NULL, X509_free_pointer);
201
202 /* read in cert chain */
203 size_t chain_len = strlen((const char *) cert_file);
204 BIO *cert_bio = NULL;
205 EXPECT_NOT_NULL(cert_bio = BIO_new(BIO_s_mem()));
206 EXPECT_TRUE(BIO_write(cert_bio, cert_file, chain_len) > 0);
207 EXPECT_NOT_NULL(leaf = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL));
208 EXPECT_NOT_NULL(intermediate = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL));
209 EXPECT_NOT_NULL(root = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL));
210 EXPECT_SUCCESS(BIO_free(cert_bio));
211
212 /* retrieve cert info from test case certificates */
213 struct s2n_cert_info leaf_info = { 0 };
214 struct s2n_cert_info intermediate_info = { 0 };
215 struct s2n_cert_info root_info = { 0 };
216
217 EXPECT_OK(s2n_openssl_x509_get_cert_info(leaf, &leaf_info));
218 EXPECT_OK(s2n_openssl_x509_get_cert_info(intermediate, &intermediate_info));
219 EXPECT_OK(s2n_openssl_x509_get_cert_info(root, &root_info));
220
221 struct s2n_cert_info expected_info = {
222 .signature_nid = test_cases[i].expected_signature_nid,
223 .signature_digest_nid = test_cases[i].expected_digest_nid,
224 .public_key_nid = test_cases[i].expected_public_key_nid,
225 .public_key_bits = test_cases[i].expected_public_key_bits,
226 .self_signed = false,
227 };
228
229 /* assert that cert info matches expected values */
230 EXPECT_OK(s2n_test_assert_s2n_cert_info_equality(&leaf_info, &expected_info));
231 EXPECT_OK(s2n_test_assert_s2n_cert_info_equality(&intermediate_info, &expected_info));
232 /* root should be self-signed, but otherwise equal */
233 expected_info.self_signed = true;
234 EXPECT_OK(s2n_test_assert_s2n_cert_info_equality(&root_info, &expected_info));
235 }
236
237 END_TEST();
238 }