Fix a memory leak in RSA_padding_add_PKCS1_OAEP_mgf1
[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 <openssl/rand.h>
12 #include <openssl/ssl.h>
13 #include <openssl/rsa.h>
14 #include <openssl/dsa.h>
15 #include <openssl/ec.h>
16 #include <openssl/dh.h>
17 #include <openssl/err.h>
18 #include "fuzzer.h"
19
20 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
21 extern int rand_predictable;
22 #endif
23 #define ENTROPY_NEEDED 32
24
25 /* unused, to avoid warning. */
26 static int idx;
27
28 int FuzzerInitialize(int *argc, char ***argv)
29 {
30     STACK_OF(SSL_COMP) *comp_methods;
31
32     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
33     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
34     ERR_get_state();
35     CRYPTO_free_ex_index(0, -1);
36     idx = SSL_get_ex_data_X509_STORE_CTX_idx();
37     RAND_add("", 1, ENTROPY_NEEDED);
38     RAND_status();
39     RSA_get_default_method();
40     DSA_get_default_method();
41     EC_KEY_get_default_method();
42     DH_get_default_method();
43     comp_methods = SSL_COMP_get_compression_methods();
44     OPENSSL_sk_sort((OPENSSL_STACK *)comp_methods);
45
46
47 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
48     rand_predictable = 1;
49 #endif
50
51     return 1;
52 }
53
54 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
55 {
56     SSL *client;
57     BIO *in;
58     BIO *out;
59     SSL_CTX *ctx;
60
61     if (len == 0)
62         return 0;
63
64     /*
65      * TODO: use the ossltest engine (optionally?) to disable crypto checks.
66      */
67
68     /* This only fuzzes the initial flow from the client so far. */
69     ctx = SSL_CTX_new(SSLv23_method());
70
71     client = SSL_new(ctx);
72     OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
73     SSL_set_tlsext_host_name(client, "localhost");
74     in = BIO_new(BIO_s_mem());
75     out = BIO_new(BIO_s_mem());
76     SSL_set_bio(client, in, out);
77     SSL_set_connect_state(client);
78     OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
79     if (SSL_do_handshake(client) == 1) {
80         /* Keep reading application data until error or EOF. */
81         uint8_t tmp[1024];
82         for (;;) {
83             if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
84                 break;
85             }
86         }
87     }
88     SSL_free(client);
89     ERR_clear_error();
90     SSL_CTX_free(ctx);
91
92     return 0;
93 }
94
95 void FuzzerCleanup(void)
96 {
97 }