Use a fixed time when fuzzing.
[openssl.git] / fuzz / client.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL licenses, (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.
9  */
10
11 #include <time.h>
12 #include <openssl/rand.h>
13 #include <openssl/ssl.h>
14 #include <openssl/rsa.h>
15 #include <openssl/dsa.h>
16 #include <openssl/ec.h>
17 #include <openssl/dh.h>
18 #include <openssl/err.h>
19 #include "fuzzer.h"
20
21 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
22 extern int rand_predictable;
23 #endif
24 #define ENTROPY_NEEDED 32
25
26 /* unused, to avoid warning. */
27 static int idx;
28
29 #define FUZZTIME 1485898104
30
31 #define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
32
33 /*
34  * This might not in all cases and still get the current time
35  * instead of the fixed time. This will just result in things
36  * not being fully reproducible and have a slightly different
37  * coverage.
38  */
39 #if defined(_WIN32) && defined(_TIME64_T_DEFINED)
40 time64_t _time64(time64_t *t) TIME_IMPL(t)
41 #endif
42 #if !defined(_WIN32) || !defined(_MSC_VER)
43 time_t time(time_t *t) TIME_IMPL(t)
44 #endif
45
46 int FuzzerInitialize(int *argc, char ***argv)
47 {
48     STACK_OF(SSL_COMP) *comp_methods;
49
50     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
51     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
52     ERR_get_state();
53     CRYPTO_free_ex_index(0, -1);
54     idx = SSL_get_ex_data_X509_STORE_CTX_idx();
55     RAND_add("", 1, ENTROPY_NEEDED);
56     RAND_status();
57     comp_methods = SSL_COMP_get_compression_methods();
58     OPENSSL_sk_sort((OPENSSL_STACK *)comp_methods);
59
60
61 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
62     rand_predictable = 1;
63 #endif
64
65     return 1;
66 }
67
68 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
69 {
70     SSL *client;
71     BIO *in;
72     BIO *out;
73     SSL_CTX *ctx;
74
75     if (len == 0)
76         return 0;
77
78     /*
79      * TODO: use the ossltest engine (optionally?) to disable crypto checks.
80      */
81
82     /* This only fuzzes the initial flow from the client so far. */
83     ctx = SSL_CTX_new(SSLv23_method());
84
85     client = SSL_new(ctx);
86     OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
87     SSL_set_tlsext_host_name(client, "localhost");
88     in = BIO_new(BIO_s_mem());
89     out = BIO_new(BIO_s_mem());
90     SSL_set_bio(client, in, out);
91     SSL_set_connect_state(client);
92     OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
93     if (SSL_do_handshake(client) == 1) {
94         /* Keep reading application data until error or EOF. */
95         uint8_t tmp[1024];
96         for (;;) {
97             if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
98                 break;
99             }
100         }
101     }
102     SSL_free(client);
103     ERR_clear_error();
104     SSL_CTX_free(ctx);
105
106     return 0;
107 }
108
109 void FuzzerCleanup(void)
110 {
111 }