blob: 32027d5124a275b4945b5d8055320181801847df [file] [log] [blame]
Adam Langleyb70cd922016-04-25 10:48:19 -07001/* 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 Langley9a4beb82015-11-09 13:57:26 -080015#include <assert.h>
David Benjamin0fde2eb2017-06-30 19:11:22 -040016#include <stdlib.h>
Adam Langley9a4beb82015-11-09 13:57:26 -080017
David Benjamin25f44222016-09-22 10:14:35 -040018#include <openssl/bio.h>
David Benjamin0fde2eb2017-06-30 19:11:22 -040019#include <openssl/bytestring.h>
David Benjamin4c0e6c62016-10-13 19:03:17 -040020#include <openssl/err.h>
David Benjamin25f44222016-09-22 10:14:35 -040021#include <openssl/evp.h>
David Benjaminbc5b2a22016-03-01 22:57:32 -050022#include <openssl/rand.h>
David Benjamin25f44222016-09-22 10:14:35 -040023#include <openssl/rsa.h>
Adam Langley9a4beb82015-11-09 13:57:26 -080024#include <openssl/ssl.h>
David Benjamin25f44222016-09-22 10:14:35 -040025#include <openssl/x509.h>
26
David Benjamin0fde2eb2017-06-30 19:11:22 -040027#include "../ssl/test/fuzzer.h"
David Benjamin25f44222016-09-22 10:14:35 -040028
David Benjamin25f44222016-09-22 10:14:35 -040029
30static const uint8_t kALPNProtocols[] = {
31 0x01, 'a', 0x02, 'a', 'a', 0x03, 'a', 'a', 'a',
32};
33
34static 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
49static 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 Langley9a4beb82015-11-09 13:57:26 -080056
57struct GlobalState {
David Benjamin65b87ce2017-08-17 13:11:23 -040058 GlobalState() : ctx(SSL_CTX_new(TLS_method())) {
David Benjamin0fde2eb2017-06-30 19:11:22 -040059 debug = getenv("BORINGSSL_FUZZER_DEBUG") != nullptr;
60
David Benjamin25f44222016-09-22 10:14:35 -040061 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 Benjamin0fde2eb2017-06-30 19:11:22 -040080 SSL_CTX_set_early_data_enabled(ctx, 1);
81
David Benjamin25f44222016-09-22 10:14:35 -040082 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 Langley9a4beb82015-11-09 13:57:26 -080089
90 ~GlobalState() {
91 SSL_CTX_free(ctx);
92 }
93
David Benjamin0fde2eb2017-06-30 19:11:22 -040094 bool debug;
Adam Langley9a4beb82015-11-09 13:57:26 -080095 SSL_CTX *const ctx;
96};
97
98static GlobalState g_state;
99
David Benjamin0939f802016-10-12 10:35:18 -0400100extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
David Benjaminbc5b2a22016-03-01 22:57:32 -0500101 RAND_reset_for_fuzzing();
102
David Benjamin0fde2eb2017-06-30 19:11:22 -0400103 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 Benjamin25f44222016-09-22 10:14:35 -0400112
David Benjamin0fde2eb2017-06-30 19:11:22 -0400113 SSL_set_renegotiate_mode(client.get(), ssl_renegotiate_freely);
114 SSL_set_max_proto_version(client.get(), TLS1_3_VERSION);
David Benjamin09114ae2017-07-01 00:39:14 -0400115 SSL_set_min_proto_version(client.get(), SSL3_VERSION);
David Benjamin0fde2eb2017-06-30 19:11:22 -0400116 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 Benjamin25f44222016-09-22 10:14:35 -0400120
121 // Enable ciphers that are off by default.
David Benjamin0fde2eb2017-06-30 19:11:22 -0400122 SSL_set_strict_cipher_list(client.get(), "ALL:NULL-SHA");
Adam Langley9a4beb82015-11-09 13:57:26 -0800123
David Benjamin0fde2eb2017-06-30 19:11:22 -0400124 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 Benjamind86c8a42016-03-02 14:53:11 -0500130 // Keep reading application data until error or EOF.
131 uint8_t tmp[1024];
132 for (;;) {
David Benjamin0fde2eb2017-06-30 19:11:22 -0400133 if (SSL_read(client.get(), tmp, sizeof(tmp)) <= 0) {
David Benjamind86c8a42016-03-02 14:53:11 -0500134 break;
135 }
136 }
David Benjamin0fde2eb2017-06-30 19:11:22 -0400137 } else if (g_state.debug) {
138 fprintf(stderr, "Handshake failed.\n");
David Benjamind86c8a42016-03-02 14:53:11 -0500139 }
Adam Langley9a4beb82015-11-09 13:57:26 -0800140
David Benjamin0fde2eb2017-06-30 19:11:22 -0400141 if (g_state.debug) {
142 ERR_print_errors_fp(stderr);
143 }
David Benjamin4c0e6c62016-10-13 19:03:17 -0400144 ERR_clear_error();
Adam Langley9a4beb82015-11-09 13:57:26 -0800145 return 0;
146}