Debian Codegraph @

crt/s2n/tests/unit/s2n_pem_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 <stdio.h>
17 #include <stdlib.h>
18
19 #include "api/s2n.h"
20 #include "s2n_test.h"
21 #include "testlib/s2n_testlib.h"
22 #include "utils/s2n_safety.h"
23
24 /* The ECDSA private key is missing the "publicKey" field, which is optional.
25 * The missing field makes the cert type difficult to detect via ASN1 parsing */
26 #define S2N_MISSING_ECDSA_PUB_CERT_KEY "../pems/missing_public_key_ecdsa_key.pem"
27 #define S2N_MISSING_ECDSA_PUB_CERT_CERT_CHAIN "../pems/missing_public_key_ecdsa_cert.pem"
28
29 static const char *valid_pem_pairs[][2] = {
30 { S2N_RSA_2048_PKCS8_CERT_CHAIN, S2N_RSA_2048_PKCS8_KEY },
31 { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
32 { S2N_RSA_2048_PKCS1_LEAF_CERT, S2N_RSA_2048_PKCS1_KEY },
33 { S2N_RSA_CERT_CHAIN_CRLF, S2N_RSA_KEY_CRLF },
34 /* PEMs with no-op data before/after entries are still valid */
35 { S2N_LEAF_WHITESPACE_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
36 { S2N_INTERMEDIATE_WHITESPACE_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
37 { S2N_ROOT_WHITESPACE_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
38 { S2N_TRAILING_WHITESPACE_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
39 { S2N_LEADING_COMMENT_TEXT_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
40 { S2N_LONG_BASE64_LINES_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
41 { S2N_MISSING_LINE_ENDINGS_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
42 { S2N_MISSING_ECDSA_PUB_CERT_CERT_CHAIN, S2N_MISSING_ECDSA_PUB_CERT_KEY },
43
44 /* Technically Invalid according to RFC, but that we are lenient towards */
45 { S2N_INVALID_HEADER_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
46 { S2N_INVALID_TRAILER_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
47 { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_INVALID_TRAILER_KEY },
48 { S2N_WEIRD_DASHES_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
49 };
50
51 static const char *invalid_pem_pairs[][2] = {
52 /* Invalid cert PEMs and valid key PEMs */
53 { S2N_UNKNOWN_KEYWORD_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
54 /* Valid cert PEMs and invalid key PEMs */
55 { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_INVALID_HEADER_KEY },
56 { S2N_RSA_2048_PKCS1_CERT_CHAIN, S2N_UNKNOWN_KEYWORD_KEY },
57 /* For good measure an invalid cert and invalid key */
58 { S2N_UNKNOWN_KEYWORD_CERT_CHAIN, S2N_UNKNOWN_KEYWORD_KEY },
59 { S2N_NO_DASHES_CERT_CHAIN, S2N_RSA_2048_PKCS1_KEY },
60 };
61
62 const struct {
63 const char *path;
64 uint16_t length;
65 } valid_cert_chains[] = {
66 { .path = S2N_TEST_TRUST_STORE, .length = 179 },
67 };
68
69 int main(int argc, char **argv)
70 {
71 struct s2n_config *config = NULL;
72 char *cert_chain_pem = NULL;
73 char *private_key_pem = NULL;
74 struct s2n_cert_chain_and_key *chain_and_key = NULL;
75
76 BEGIN_TEST();
77 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
78 EXPECT_NOT_NULL(cert_chain_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
79 EXPECT_NOT_NULL(private_key_pem = malloc(S2N_MAX_TEST_PEM_SIZE));
80
81 for (size_t i = 0; i < s2n_array_len(valid_pem_pairs); i++) {
82 EXPECT_NOT_NULL(config = s2n_config_new());
83 EXPECT_SUCCESS(s2n_read_test_pem(valid_pem_pairs[i][0], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
84 EXPECT_SUCCESS(s2n_read_test_pem(valid_pem_pairs[i][1], private_key_pem, S2N_MAX_TEST_PEM_SIZE));
85 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
86 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
87 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, chain_and_key));
88 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
89 EXPECT_SUCCESS(s2n_config_free(config));
90 }
91
92 for (size_t i = 0; i < s2n_array_len(invalid_pem_pairs); i++) {
93 EXPECT_SUCCESS(s2n_read_test_pem(invalid_pem_pairs[i][0], cert_chain_pem, S2N_MAX_TEST_PEM_SIZE));
94 EXPECT_SUCCESS(s2n_read_test_pem(invalid_pem_pairs[i][1], private_key_pem, S2N_MAX_TEST_PEM_SIZE));
95 EXPECT_NOT_NULL(chain_and_key = s2n_cert_chain_and_key_new());
96 EXPECT_FAILURE(s2n_cert_chain_and_key_load_pem(chain_and_key, cert_chain_pem, private_key_pem));
97 EXPECT_SUCCESS(s2n_cert_chain_and_key_free(chain_and_key));
98 }
99
100 char large_cert_chain_pem[500000] = { 0 };
101 for (size_t i = 0; i < s2n_array_len(valid_cert_chains); i++) {
102 DEFER_CLEANUP(struct s2n_cert_chain_and_key *chain = s2n_cert_chain_and_key_new(),
103 s2n_cert_chain_and_key_ptr_free);
104 EXPECT_NOT_NULL(chain);
105 EXPECT_SUCCESS(s2n_read_test_pem(valid_cert_chains[i].path,
106 large_cert_chain_pem, sizeof(large_cert_chain_pem)));
107
108 uint32_t length = 0;
109 EXPECT_SUCCESS(s2n_cert_chain_and_key_load_public_pem_bytes(chain,
110 (uint8_t *) large_cert_chain_pem, strlen(large_cert_chain_pem)));
111 EXPECT_SUCCESS(s2n_cert_chain_get_length(chain, &length));
112 EXPECT_EQUAL(length, valid_cert_chains[i].length);
113
114 DEFER_CLEANUP(struct s2n_config *test_config = s2n_config_new(), s2n_config_ptr_free);
115 EXPECT_NOT_NULL(test_config);
116 EXPECT_SUCCESS(s2n_config_add_pem_to_trust_store(test_config, large_cert_chain_pem));
117 EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(test_config, chain));
118 }
119
120 free(cert_chain_pem);
121 free(private_key_pem);
122 END_TEST();
123 }