Debian Codegraph @

crt/s2n/tests/unit/s2n_x509_validator_certificate_signatures_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
16 #include <openssl/pem.h>
17 #include <openssl/x509.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "api/s2n.h"
22 #include "crypto/s2n_openssl_x509.h"
23 #include "error/s2n_errno.h"
24 #include "s2n_test.h"
25 #include "testlib/s2n_testlib.h"
26 #include "tls/s2n_signature_scheme.h"
27 #include "tls/s2n_tls.h"
28 #include "tls/s2n_tls13.h"
29 #include "tls/s2n_x509_validator.h"
30 #include "utils/s2n_safety.h"
31
32 /* forward declaration */
33 S2N_RESULT s2n_x509_validator_check_cert_preferences(struct s2n_connection *conn, X509 *cert);
34
35 DEFINE_POINTER_CLEANUP_FUNC(BIO *, BIO_free);
36
37 int main(int argc, char **argv)
38 {
39 BEGIN_TEST();
40
41 uint8_t cert_file[S2N_MAX_TEST_PEM_SIZE] = { 0 };
42 X509 *cert = NULL;
43 BIO *certBio = NULL;
44 size_t certLen = 0;
45
46 const struct s2n_signature_scheme *const test_sig_scheme_list[] = {
47 &s2n_ecdsa_sha256,
48 &s2n_rsa_pkcs1_sha1,
49 };
50
51 const struct s2n_signature_preferences test_certificate_signature_preferences = {
52 .count = s2n_array_len(test_sig_scheme_list),
53 .signature_schemes = test_sig_scheme_list,
54 };
55
56 const struct s2n_security_policy test_sp = {
57 .minimum_protocol_version = S2N_TLS12,
58 .certificate_signature_preferences = &test_certificate_signature_preferences,
59 };
60
61 /* s2n_x509_validator_check_cert_preferences */
62 {
63 /* Connection using a security policy with no certificate_signature_preferences allows SHA-1 signatures in certificates */
64 {
65 struct s2n_connection *conn = NULL;
66 struct s2n_config *config = s2n_config_new();
67
68 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
69 /* 20140601 is a security policy with no certificate_signature_preferences list */
70 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "20140601"));
71 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
72
73 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_2048_PKCS1_CERT_CHAIN, (char *) cert_file, S2N_MAX_TEST_PEM_SIZE));
74 certLen = strlen((const char *) cert_file);
75
76 /* Read the test certificates into an Openssl X509 struct */
77 EXPECT_NOT_NULL(certBio = BIO_new(BIO_s_mem()));
78 EXPECT_TRUE(BIO_write(certBio, cert_file, certLen) > 0);
79 EXPECT_NOT_NULL(cert = PEM_read_bio_X509(certBio, NULL, NULL, NULL));
80
81 EXPECT_OK(s2n_x509_validator_check_cert_preferences(conn, cert));
82
83 EXPECT_SUCCESS(s2n_config_free(config));
84 EXPECT_SUCCESS(s2n_connection_free(conn));
85 EXPECT_SUCCESS(BIO_free(certBio));
86 X509_free(cert);
87 };
88
89 /* Connection using the default_tls13 security policy does not validate SHA-1 signatures in certificates */
90 {
91 struct s2n_connection *conn = NULL;
92 struct s2n_config *config = s2n_config_new();
93
94 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
95 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
96 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
97
98 EXPECT_SUCCESS(s2n_read_test_pem(S2N_RSA_2048_PKCS1_CERT_CHAIN, (char *) cert_file, S2N_MAX_TEST_PEM_SIZE));
99 certLen = strlen((const char *) cert_file);
100
101 /* Read the test certificates into an Openssl X509 struct */
102 EXPECT_NOT_NULL(certBio = BIO_new(BIO_s_mem()));
103 EXPECT_TRUE(BIO_write(certBio, cert_file, certLen) > 0);
104 EXPECT_NOT_NULL(cert = PEM_read_bio_X509(certBio, NULL, NULL, NULL));
105
106 EXPECT_ERROR_WITH_ERRNO(s2n_x509_validator_check_cert_preferences(conn, cert),
107 S2N_ERR_CERT_UNTRUSTED);
108
109 EXPECT_SUCCESS(s2n_config_free(config));
110 EXPECT_SUCCESS(s2n_connection_free(conn));
111 EXPECT_SUCCESS(BIO_free(certBio));
112 X509_free(cert);
113 };
114
115 /* Connection using the default_tls13 security policy ignores a SHA-1 signature on a root certificate */
116 {
117 struct s2n_connection *conn = NULL;
118 struct s2n_config *config = s2n_config_new();
119
120 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_CLIENT));
121 EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "default_tls13"));
122 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
123
124 EXPECT_SUCCESS(s2n_read_test_pem(S2N_SHA1_ROOT_SIGNATURE_CA_CERT, (char *) cert_file, S2N_MAX_TEST_PEM_SIZE));
125 certLen = strlen((const char *) cert_file);
126
127 /* Read the test certificates into an Openssl X509 struct */
128 EXPECT_NOT_NULL(certBio = BIO_new(BIO_s_mem()));
129 EXPECT_TRUE(BIO_write(certBio, cert_file, certLen) > 0);
130 EXPECT_NOT_NULL(cert = PEM_read_bio_X509(certBio, NULL, NULL, NULL));
131
132 EXPECT_OK(s2n_x509_validator_check_cert_preferences(conn, cert));
133
134 EXPECT_SUCCESS(s2n_config_free(config));
135 EXPECT_SUCCESS(s2n_connection_free(conn));
136 EXPECT_SUCCESS(BIO_free(certBio));
137 X509_free(cert);
138 };
139
140 /* When the certificate signature algorithm is not in the preferences list, then an S2N_ERR_CERT_UNTRUSTED err
141 * is returned.
142 */
143 {
144 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
145 EXPECT_NOT_NULL(config);
146 config->security_policy = &test_sp;
147
148 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
149 EXPECT_NOT_NULL(conn);
150 conn->actual_protocol_version = S2N_TLS12;
151 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
152
153 DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_p384_sha256 = NULL,
154 s2n_cert_chain_and_key_ptr_free);
155 EXPECT_SUCCESS(s2n_test_cert_permutation_load_server_chain(&ecdsa_p384_sha256, "ec",
156 "ecdsa", "p384", "sha384"));
157 DEFER_CLEANUP(X509 *test_cert = NULL, X509_free_pointer);
158 EXPECT_OK(s2n_openssl_x509_parse(&ecdsa_p384_sha256->cert_chain->head->raw, &test_cert));
159
160 EXPECT_ERROR_WITH_ERRNO(s2n_x509_validator_check_cert_preferences(conn, test_cert), S2N_ERR_SECURITY_POLICY_INCOMPATIBLE_CERT);
161 };
162
163 /* Certificate signature algorithm is in the test certificate signature preferences list but signature is SHA-1
164 * and TLS 1.3 has been negotiated.
165 */
166 {
167 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
168 EXPECT_NOT_NULL(config);
169 config->security_policy = &test_sp;
170
171 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
172 EXPECT_NOT_NULL(conn);
173 conn->actual_protocol_version = S2N_TLS13;
174 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
175
176 uint32_t cert_len = 0;
177 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_RSA_2048_PKCS1_CERT_CHAIN, cert_file,
178 &cert_len, S2N_MAX_TEST_PEM_SIZE));
179
180 /* Read the test certificates into an Openssl X509 struct */
181 DEFER_CLEANUP(BIO *cert_bio = BIO_new(BIO_s_mem()), BIO_free_pointer);
182 EXPECT_NOT_NULL(cert_bio);
183 EXPECT_TRUE(BIO_write(cert_bio, cert_file, cert_len) > 0);
184 DEFER_CLEANUP(X509 *test_cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL), X509_free_pointer);
185 EXPECT_NOT_NULL(test_cert);
186
187 EXPECT_ERROR_WITH_ERRNO(s2n_x509_validator_check_cert_preferences(conn, test_cert), S2N_ERR_CERT_UNTRUSTED);
188 };
189
190 /* Certificate signature algorithm is in the test certificate signature preferences list and signature is SHA-1
191 * and TLS 1.2 has been negotiated.
192 */
193 {
194 DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free);
195 EXPECT_NOT_NULL(config);
196 config->security_policy = &test_sp;
197
198 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_CLIENT), s2n_connection_ptr_free);
199 EXPECT_NOT_NULL(conn);
200 conn->actual_protocol_version = S2N_TLS12;
201 EXPECT_SUCCESS(s2n_connection_set_config(conn, config));
202
203 uint32_t cert_len = 0;
204 EXPECT_SUCCESS(s2n_read_test_pem_and_len(S2N_RSA_2048_PKCS1_CERT_CHAIN, cert_file,
205 &cert_len, S2N_MAX_TEST_PEM_SIZE));
206
207 /* Read the test certificates into an Openssl X509 struct */
208 DEFER_CLEANUP(BIO *cert_bio = BIO_new(BIO_s_mem()), BIO_free_pointer);
209 EXPECT_NOT_NULL(cert_bio);
210 EXPECT_TRUE(BIO_write(cert_bio, cert_file, cert_len) > 0);
211 DEFER_CLEANUP(X509 *test_cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL), X509_free_pointer);
212 EXPECT_NOT_NULL(test_cert);
213
214 EXPECT_OK(s2n_x509_validator_check_cert_preferences(conn, test_cert));
215 };
216 };
217 END_TEST();
218 return S2N_SUCCESS;
219 }