Home | History | Annotate | Line # | Download | only in fuzz
client.c revision 1.1.1.1.2.2
      1  1.1.1.1.2.2  pgoyette /*
      2  1.1.1.1.2.2  pgoyette  * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
      3  1.1.1.1.2.2  pgoyette  *
      4  1.1.1.1.2.2  pgoyette  * Licensed under the OpenSSL licenses, (the "License");
      5  1.1.1.1.2.2  pgoyette  * you may not use this file except in compliance with the License.
      6  1.1.1.1.2.2  pgoyette  * You may obtain a copy of the License at
      7  1.1.1.1.2.2  pgoyette  * https://www.openssl.org/source/license.html
      8  1.1.1.1.2.2  pgoyette  * or in the file LICENSE in the source distribution.
      9  1.1.1.1.2.2  pgoyette  */
     10  1.1.1.1.2.2  pgoyette 
     11  1.1.1.1.2.2  pgoyette #include <time.h>
     12  1.1.1.1.2.2  pgoyette #include <openssl/rand.h>
     13  1.1.1.1.2.2  pgoyette #include <openssl/ssl.h>
     14  1.1.1.1.2.2  pgoyette #include <openssl/rsa.h>
     15  1.1.1.1.2.2  pgoyette #include <openssl/dsa.h>
     16  1.1.1.1.2.2  pgoyette #include <openssl/ec.h>
     17  1.1.1.1.2.2  pgoyette #include <openssl/dh.h>
     18  1.1.1.1.2.2  pgoyette #include <openssl/err.h>
     19  1.1.1.1.2.2  pgoyette #include "fuzzer.h"
     20  1.1.1.1.2.2  pgoyette 
     21  1.1.1.1.2.2  pgoyette #include "rand.inc"
     22  1.1.1.1.2.2  pgoyette 
     23  1.1.1.1.2.2  pgoyette /* unused, to avoid warning. */
     24  1.1.1.1.2.2  pgoyette static int idx;
     25  1.1.1.1.2.2  pgoyette 
     26  1.1.1.1.2.2  pgoyette #define FUZZTIME 1485898104
     27  1.1.1.1.2.2  pgoyette 
     28  1.1.1.1.2.2  pgoyette #define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
     29  1.1.1.1.2.2  pgoyette 
     30  1.1.1.1.2.2  pgoyette /*
     31  1.1.1.1.2.2  pgoyette  * This might not work in all cases (and definitely not on Windows
     32  1.1.1.1.2.2  pgoyette  * because of the way linkers are) and callees can still get the
     33  1.1.1.1.2.2  pgoyette  * current time instead of the fixed time. This will just result
     34  1.1.1.1.2.2  pgoyette  * in things not being fully reproducible and have a slightly
     35  1.1.1.1.2.2  pgoyette  * different coverage.
     36  1.1.1.1.2.2  pgoyette  */
     37  1.1.1.1.2.2  pgoyette #if !defined(_WIN32)
     38  1.1.1.1.2.2  pgoyette time_t time(time_t *t) TIME_IMPL(t)
     39  1.1.1.1.2.2  pgoyette #endif
     40  1.1.1.1.2.2  pgoyette 
     41  1.1.1.1.2.2  pgoyette int FuzzerInitialize(int *argc, char ***argv)
     42  1.1.1.1.2.2  pgoyette {
     43  1.1.1.1.2.2  pgoyette     STACK_OF(SSL_COMP) *comp_methods;
     44  1.1.1.1.2.2  pgoyette 
     45  1.1.1.1.2.2  pgoyette     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
     46  1.1.1.1.2.2  pgoyette     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
     47  1.1.1.1.2.2  pgoyette     ERR_get_state();
     48  1.1.1.1.2.2  pgoyette     CRYPTO_free_ex_index(0, -1);
     49  1.1.1.1.2.2  pgoyette     idx = SSL_get_ex_data_X509_STORE_CTX_idx();
     50  1.1.1.1.2.2  pgoyette     FuzzerSetRand();
     51  1.1.1.1.2.2  pgoyette     comp_methods = SSL_COMP_get_compression_methods();
     52  1.1.1.1.2.2  pgoyette     if (comp_methods != NULL)
     53  1.1.1.1.2.2  pgoyette         sk_SSL_COMP_sort(comp_methods);
     54  1.1.1.1.2.2  pgoyette 
     55  1.1.1.1.2.2  pgoyette     return 1;
     56  1.1.1.1.2.2  pgoyette }
     57  1.1.1.1.2.2  pgoyette 
     58  1.1.1.1.2.2  pgoyette int FuzzerTestOneInput(const uint8_t *buf, size_t len)
     59  1.1.1.1.2.2  pgoyette {
     60  1.1.1.1.2.2  pgoyette     SSL *client;
     61  1.1.1.1.2.2  pgoyette     BIO *in;
     62  1.1.1.1.2.2  pgoyette     BIO *out;
     63  1.1.1.1.2.2  pgoyette     SSL_CTX *ctx;
     64  1.1.1.1.2.2  pgoyette 
     65  1.1.1.1.2.2  pgoyette     if (len == 0)
     66  1.1.1.1.2.2  pgoyette         return 0;
     67  1.1.1.1.2.2  pgoyette 
     68  1.1.1.1.2.2  pgoyette     /*
     69  1.1.1.1.2.2  pgoyette      * TODO: use the ossltest engine (optionally?) to disable crypto checks.
     70  1.1.1.1.2.2  pgoyette      */
     71  1.1.1.1.2.2  pgoyette 
     72  1.1.1.1.2.2  pgoyette     /* This only fuzzes the initial flow from the client so far. */
     73  1.1.1.1.2.2  pgoyette     ctx = SSL_CTX_new(SSLv23_method());
     74  1.1.1.1.2.2  pgoyette 
     75  1.1.1.1.2.2  pgoyette     client = SSL_new(ctx);
     76  1.1.1.1.2.2  pgoyette     OPENSSL_assert(SSL_set_min_proto_version(client, 0) == 1);
     77  1.1.1.1.2.2  pgoyette     OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1);
     78  1.1.1.1.2.2  pgoyette     SSL_set_tlsext_host_name(client, "localhost");
     79  1.1.1.1.2.2  pgoyette     in = BIO_new(BIO_s_mem());
     80  1.1.1.1.2.2  pgoyette     out = BIO_new(BIO_s_mem());
     81  1.1.1.1.2.2  pgoyette     SSL_set_bio(client, in, out);
     82  1.1.1.1.2.2  pgoyette     SSL_set_connect_state(client);
     83  1.1.1.1.2.2  pgoyette     OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
     84  1.1.1.1.2.2  pgoyette     if (SSL_do_handshake(client) == 1) {
     85  1.1.1.1.2.2  pgoyette         /* Keep reading application data until error or EOF. */
     86  1.1.1.1.2.2  pgoyette         uint8_t tmp[1024];
     87  1.1.1.1.2.2  pgoyette         for (;;) {
     88  1.1.1.1.2.2  pgoyette             if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
     89  1.1.1.1.2.2  pgoyette                 break;
     90  1.1.1.1.2.2  pgoyette             }
     91  1.1.1.1.2.2  pgoyette         }
     92  1.1.1.1.2.2  pgoyette     }
     93  1.1.1.1.2.2  pgoyette     SSL_free(client);
     94  1.1.1.1.2.2  pgoyette     ERR_clear_error();
     95  1.1.1.1.2.2  pgoyette     SSL_CTX_free(ctx);
     96  1.1.1.1.2.2  pgoyette 
     97  1.1.1.1.2.2  pgoyette     return 0;
     98  1.1.1.1.2.2  pgoyette }
     99  1.1.1.1.2.2  pgoyette 
    100  1.1.1.1.2.2  pgoyette void FuzzerCleanup(void)
    101  1.1.1.1.2.2  pgoyette {
    102  1.1.1.1.2.2  pgoyette }
    103