Fix segfault in openssl app called with no args.
[openssl.git] / fuzz / client.c
1 /*
2  * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
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.
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 #include "rand.inc"
22
23 DEFINE_STACK_OF(SSL_COMP)
24
25 /* unused, to avoid warning. */
26 static int idx;
27
28 #define FUZZTIME 1485898104
29
30 #define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
31
32 /*
33  * This might not work in all cases (and definitely not on Windows
34  * because of the way linkers are) and callees can still get the
35  * current time instead of the fixed time. This will just result
36  * in things not being fully reproducible and have a slightly
37  * different coverage.
38  */
39 #if !defined(_WIN32)
40 time_t time(time_t *t) TIME_IMPL(t)
41 #endif
42
43 int FuzzerInitialize(int *argc, char ***argv)
44 {
45     STACK_OF(SSL_COMP) *comp_methods;
46
47     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
48     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
49     ERR_clear_error();
50     CRYPTO_free_ex_index(0, -1);
51     idx = SSL_get_ex_data_X509_STORE_CTX_idx();
52     FuzzerSetRand();
53     comp_methods = SSL_COMP_get_compression_methods();
54     if (comp_methods != NULL)
55         sk_SSL_COMP_sort(comp_methods);
56
57     return 1;
58 }
59
60 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
61 {
62     SSL *client;
63     BIO *in;
64     BIO *out;
65     SSL_CTX *ctx;
66
67     if (len == 0)
68         return 0;
69
70     /*
71      * TODO: use the ossltest engine (optionally?) to disable crypto checks.
72      */
73
74     /* This only fuzzes the initial flow from the client so far. */
75     ctx = SSL_CTX_new(SSLv23_method());
76
77     client = SSL_new(ctx);
78     OPENSSL_assert(SSL_set_min_proto_version(client, 0) == 1);
79     OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
80     SSL_set_tlsext_host_name(client, "localhost");
81     in = BIO_new(BIO_s_mem());
82     out = BIO_new(BIO_s_mem());
83     SSL_set_bio(client, in, out);
84     SSL_set_connect_state(client);
85     OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
86     if (SSL_do_handshake(client) == 1) {
87         /* Keep reading application data until error or EOF. */
88         uint8_t tmp[1024];
89         for (;;) {
90             if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
91                 break;
92             }
93         }
94     }
95     SSL_free(client);
96     ERR_clear_error();
97     SSL_CTX_free(ctx);
98
99     return 0;
100 }
101
102 void FuzzerCleanup(void)
103 {
104 }