| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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) |