Debian Codegraph @

crt/s2n/tests/fuzz/s2n_openssl_diff_pem_parsing_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 /* Target Functions: s2n_parse_cert_chain s2n_create_cert_chain_from_stuffer
17 s2n_cert_chain_and_key_free */
18
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stddef.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include <openssl/ssl.h>
28 #include <openssl/conf.h>
29 #include <openssl/rand.h>
30 #include <openssl/err.h>
31 #include <openssl/x509.h>
32 #include <openssl/dh.h>
33 #include <openssl/ec.h>
34
35 #include "api/s2n.h"
36 #include "stuffer/s2n_stuffer.h"
37 #include "tls/s2n_config.h"
38 #include "utils/s2n_blob.h"
39 #include "utils/s2n_mem.h"
40 #include "utils/s2n_safety.h"
41 #include "s2n_test.h"
42 #include "crypto/s2n_certificate.h"
43
44 static int openssl_parse_cert_chain(struct s2n_stuffer *in)
45 {
46 uint8_t chain_len = 0;
47 BIO *membio = BIO_new_mem_buf((void *) in->blob.data, in->blob.size - 1);
48 X509 *cert = NULL;
49
50 while (1) {
51 /* Try parsing Cert PEM with OpenSSL */
52 cert = PEM_read_bio_X509(membio, NULL, 0, NULL);
53 if (cert != NULL) {
54 X509_free(cert);
55 chain_len++;
56 } else {
57 break;
58 }
59 }
60 BIO_free(membio);
61
62 return chain_len;
63
64 }
65
66 static int s2n_parse_cert_chain(struct s2n_stuffer *in)
67 {
68 struct s2n_cert_chain_and_key *chain_and_key = s2n_cert_chain_and_key_new();
69
70 /* Allocate the memory for the chain and key */
71 if (s2n_create_cert_chain_from_stuffer(chain_and_key->cert_chain, in) != S2N_SUCCESS) {
72 POSIX_GUARD(s2n_cert_chain_and_key_free(chain_and_key));
73 return S2N_SUCCESS;
74 }
75
76 int chain_len = 0;
77 struct s2n_cert *next = chain_and_key->cert_chain->head;
78 while(next != NULL) {
79 chain_len++;
80 next = next->next;
81 }
82
83 s2n_cert_chain_and_key_free(chain_and_key);
84
85 return chain_len;
86 }
87
88 int s2n_fuzz_test(const uint8_t *buf, size_t len)
89 {
90 struct s2n_stuffer in = {0};
91 POSIX_GUARD(s2n_stuffer_alloc(&in, len + 1));
92 POSIX_GUARD(s2n_stuffer_write_bytes(&in, buf, len));
93 in.blob.data[len] = 0;
94
95 uint8_t openssl_chain_len = openssl_parse_cert_chain(&in);
96 POSIX_GUARD(s2n_stuffer_reread(&in));
97
98 uint8_t s2n_chain_len = s2n_parse_cert_chain(&in);
99 POSIX_GUARD(s2n_stuffer_free(&in));
100
101 if (openssl_chain_len > s2n_chain_len) {
102 /* If we return -1 here, then this fuzz test will fail if OpenSSL is able to parse a messy PEM file that s2n
103 * isn't able to. All well formed PEM files that follow the RFC are still parsable by both, but we should
104 * leave this commented out for now until we update our PEM parser to be more lenient,
105 * or decide to do something else. */
106
107 /* return -1; */
108 }
109
110 return S2N_SUCCESS;
111 }
112
113 static void s2n_fuzz_cleanup()
114 {
115 ERR_free_strings();
116 CRYPTO_cleanup_all_ex_data();
117 CONF_modules_free();
118 ERR_clear_error();
119 }
120
121 S2N_FUZZ_TARGET(NULL, s2n_fuzz_test, s2n_fuzz_cleanup)