Adam Langley | b70cd92 | 2016-04-25 10:48:19 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2016, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 15 | #include <assert.h> |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 16 | #include <stdlib.h> |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 17 | |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 18 | #include <openssl/bio.h> |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 19 | #include <openssl/bytestring.h> |
David Benjamin | 4c0e6c6 | 2016-10-13 19:03:17 -0400 | [diff] [blame] | 20 | #include <openssl/err.h> |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 21 | #include <openssl/evp.h> |
David Benjamin | bc5b2a2 | 2016-03-01 22:57:32 -0500 | [diff] [blame] | 22 | #include <openssl/rand.h> |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 23 | #include <openssl/rsa.h> |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 24 | #include <openssl/ssl.h> |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 25 | #include <openssl/x509.h> |
| 26 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 27 | #include "../ssl/test/fuzzer.h" |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 28 | |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 29 | |
| 30 | static const uint8_t kALPNProtocols[] = { |
| 31 | 0x01, 'a', 0x02, 'a', 'a', 0x03, 'a', 'a', 'a', |
| 32 | }; |
| 33 | |
| 34 | static const uint8_t kP256KeyPKCS8[] = { |
| 35 | 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, |
| 36 | 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, |
| 37 | 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20, |
| 38 | 0x43, 0x09, 0xc0, 0x67, 0x75, 0x21, 0x47, 0x9d, 0xa8, 0xfa, 0x16, 0xdf, |
| 39 | 0x15, 0x73, 0x61, 0x34, 0x68, 0x6f, 0xe3, 0x8e, 0x47, 0x91, 0x95, 0xab, |
| 40 | 0x79, 0x4a, 0x72, 0x14, 0xcb, 0xe2, 0x49, 0x4f, 0xa1, 0x44, 0x03, 0x42, |
| 41 | 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, 0x2e, 0x8f, 0x37, 0x9a, 0xd5, |
| 42 | 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, 0x4a, 0xe7, 0x20, 0xcb, 0x90, |
| 43 | 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, 0x88, 0x38, 0xc0, 0xdb, 0xba, |
| 44 | 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, 0xe3, 0xd5, 0x61, 0x99, 0x73, |
| 45 | 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, 0x41, 0x3b, 0x0d, 0x10, 0xa7, |
| 46 | 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec, |
| 47 | }; |
| 48 | |
| 49 | static int NPNSelectCallback(SSL *ssl, uint8_t **out, uint8_t *out_len, |
| 50 | const uint8_t *in, unsigned in_len, void *arg) { |
| 51 | static const uint8_t kProtocol[] = {'a', 'a'}; |
| 52 | *out = const_cast<uint8_t*>(kProtocol); |
| 53 | *out_len = sizeof(kProtocol); |
| 54 | return SSL_TLSEXT_ERR_OK; |
| 55 | } |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 56 | |
| 57 | struct GlobalState { |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 58 | GlobalState() : ctx(SSL_CTX_new(SSLv23_method())) { |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 59 | debug = getenv("BORINGSSL_FUZZER_DEBUG") != nullptr; |
| 60 | |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 61 | const uint8_t *bufp = kRSAPrivateKeyDER; |
| 62 | RSA *privkey = d2i_RSAPrivateKey(NULL, &bufp, sizeof(kRSAPrivateKeyDER)); |
| 63 | assert(privkey != nullptr); |
| 64 | |
| 65 | EVP_PKEY *pkey = EVP_PKEY_new(); |
| 66 | EVP_PKEY_assign_RSA(pkey, privkey); |
| 67 | |
| 68 | SSL_CTX_use_PrivateKey(ctx, pkey); |
| 69 | EVP_PKEY_free(pkey); |
| 70 | |
| 71 | bufp = kCertificateDER; |
| 72 | X509 *cert = d2i_X509(NULL, &bufp, sizeof(kCertificateDER)); |
| 73 | assert(cert != nullptr); |
| 74 | |
| 75 | SSL_CTX_use_certificate(ctx, cert); |
| 76 | X509_free(cert); |
| 77 | |
| 78 | SSL_CTX_set_next_proto_select_cb(ctx, NPNSelectCallback, nullptr); |
| 79 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 80 | SSL_CTX_set_early_data_enabled(ctx, 1); |
| 81 | |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 82 | CBS cbs; |
| 83 | CBS_init(&cbs, kP256KeyPKCS8, sizeof(kP256KeyPKCS8)); |
| 84 | pkey = EVP_parse_private_key(&cbs); |
| 85 | assert(pkey != nullptr); |
| 86 | SSL_CTX_set1_tls_channel_id(ctx, pkey); |
| 87 | EVP_PKEY_free(pkey); |
| 88 | } |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 89 | |
| 90 | ~GlobalState() { |
| 91 | SSL_CTX_free(ctx); |
| 92 | } |
| 93 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 94 | bool debug; |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 95 | SSL_CTX *const ctx; |
| 96 | }; |
| 97 | |
| 98 | static GlobalState g_state; |
| 99 | |
David Benjamin | 0939f80 | 2016-10-12 10:35:18 -0400 | [diff] [blame] | 100 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) { |
David Benjamin | bc5b2a2 | 2016-03-01 22:57:32 -0500 | [diff] [blame] | 101 | RAND_reset_for_fuzzing(); |
| 102 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 103 | CBS cbs; |
| 104 | CBS_init(&cbs, buf, len); |
| 105 | bssl::UniquePtr<SSL> client = SetupTest(&cbs, g_state.ctx, false); |
| 106 | if (!client) { |
| 107 | if (g_state.debug) { |
| 108 | fprintf(stderr, "Error parsing parameters.\n"); |
| 109 | } |
| 110 | return 0; |
| 111 | } |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 112 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 113 | SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely); |
| 114 | SSL_set_max_proto_version(client.get(), TLS1_3_VERSION); |
David Benjamin | 09114ae | 2017-07-01 00:39:14 -0400 | [diff] [blame] | 115 | SSL_set_min_proto_version(client.get(), SSL3_VERSION); |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 116 | SSL_enable_ocsp_stapling(client.get()); |
| 117 | SSL_enable_signed_cert_timestamps(client.get()); |
| 118 | SSL_set_tlsext_host_name(client.get(), "hostname"); |
| 119 | SSL_set_alpn_protos(client.get(), kALPNProtocols, sizeof(kALPNProtocols)); |
David Benjamin | 25f4422 | 2016-09-22 10:14:35 -0400 | [diff] [blame] | 120 | |
| 121 | // Enable ciphers that are off by default. |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 122 | SSL_set_strict_cipher_list(client.get(), "ALL:NULL-SHA"); |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 123 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 124 | BIO *in = BIO_new(BIO_s_mem()); |
| 125 | BIO *out = BIO_new(BIO_s_mem()); |
| 126 | SSL_set_bio(client.get(), in, out); // Takes ownership of |in| and |out|. |
| 127 | |
| 128 | BIO_write(in, CBS_data(&cbs), CBS_len(&cbs)); |
| 129 | if (SSL_do_handshake(client.get()) == 1) { |
David Benjamin | d86c8a4 | 2016-03-02 14:53:11 -0500 | [diff] [blame] | 130 | // Keep reading application data until error or EOF. |
| 131 | uint8_t tmp[1024]; |
| 132 | for (;;) { |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 133 | if (SSL_read(client.get(), tmp, sizeof(tmp)) <= 0) { |
David Benjamin | d86c8a4 | 2016-03-02 14:53:11 -0500 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 137 | } else if (g_state.debug) { |
| 138 | fprintf(stderr, "Handshake failed.\n"); |
David Benjamin | d86c8a4 | 2016-03-02 14:53:11 -0500 | [diff] [blame] | 139 | } |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 140 | |
David Benjamin | 0fde2eb | 2017-06-30 19:11:22 -0400 | [diff] [blame] | 141 | if (g_state.debug) { |
| 142 | ERR_print_errors_fp(stderr); |
| 143 | } |
David Benjamin | 4c0e6c6 | 2016-10-13 19:03:17 -0400 | [diff] [blame] | 144 | ERR_clear_error(); |
Adam Langley | 9a4beb8 | 2015-11-09 13:57:26 -0800 | [diff] [blame] | 145 | return 0; |
| 146 | } |