2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
11 #include <openssl/ssl.h>
12 #include <openssl/err.h>
13 #include <openssl/bio.h>
15 #include "internal/sockets.h"
16 #include "internal/time.h"
17 #include "internal/quic_ssl.h"
19 /* unused, to avoid warning. */
22 static OSSL_TIME fake_now;
24 static OSSL_TIME fake_now_cb(void *arg)
29 int FuzzerInitialize(int *argc, char ***argv)
31 STACK_OF(SSL_COMP) *comp_methods;
34 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
35 OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
37 CRYPTO_free_ex_index(0, -1);
38 idx = SSL_get_ex_data_X509_STORE_CTX_idx();
39 comp_methods = SSL_COMP_get_compression_methods();
40 if (comp_methods != NULL)
41 sk_SSL_COMP_sort(comp_methods);
46 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
52 BIO_ADDR *peer_addr = NULL;
53 struct in_addr ina = {0};
59 /* This only fuzzes the initial flow from the client so far. */
60 ctx = SSL_CTX_new(OSSL_QUIC_client_method());
64 client = SSL_new(ctx);
68 fake_now = ossl_ms2time(1);
69 if (!ossl_quic_conn_set_override_now_cb(client, fake_now_cb, NULL))
72 peer_addr = BIO_ADDR_new();
73 if (peer_addr == NULL)
76 ina.s_addr = htonl(0x7f000001UL);
78 if (!BIO_ADDR_rawmake(peer_addr, AF_INET, &ina, sizeof(ina), htons(4433)))
81 SSL_set_tlsext_host_name(client, "localhost");
82 in = BIO_new(BIO_s_dgram_mem());
85 out = BIO_new(BIO_s_dgram_mem());
90 if (!BIO_dgram_set_caps(out, BIO_DGRAM_CAP_HANDLES_DST_ADDR)) {
95 SSL_set_bio(client, in, out);
96 if (SSL_set_alpn_protos(client, (const unsigned char *)"\x08ossltest", 9) != 0)
98 if (SSL_set1_initial_peer_addr(client, peer_addr) != 1)
100 SSL_set_connect_state(client);
104 uint64_t nxtpktms = 0;
105 OSSL_TIME nxtpkt = ossl_time_zero(), nxttimeout;
109 nxtpktms = buf[0] + (buf[1] << 8);
110 nxtpkt = ossl_time_add(fake_now, ossl_ms2time(nxtpktms));
116 if ((ret = SSL_do_handshake(client)) == 1) {
118 * Keep reading application data until there are no more
119 * datagrams to inject or a fatal error occurs
123 ret = SSL_read(client, tmp, sizeof(tmp));
126 switch (SSL_get_error(client, ret)) {
127 case SSL_ERROR_WANT_READ:
128 case SSL_ERROR_WANT_WRITE:
135 if (!SSL_get_event_timeout(client, &tv, &isinf))
142 nxttimeout = ossl_time_add(fake_now,
143 ossl_time_from_timeval(tv));
144 if (len > 3 && ossl_time_compare(nxttimeout, nxtpkt) >= 0) {
148 fake_now = nxttimeout;
155 size = buf[0] + (buf[1] << 8);
160 BIO_write(in, buf+2, size);
168 BIO_ADDR_free(peer_addr);
173 void FuzzerCleanup(void)