Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/fuzz/client.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 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
/* unused, to avoid warning. */
22
static int idx;
23
24
1.52k
#define FUZZTIME 1485898104
25
26
#define TIME_IMPL(t)       \
27
930
    {                      \
28
930
        if (t != NULL)     \
29
930
            *t = FUZZTIME; \
30
930
        return FUZZTIME;   \
31
930
    }
32
33
/*
34
 * This might not work in all cases (and definitely not on Windows
35
 * because of the way linkers are) and callees can still get the
36
 * current time instead of the fixed time. This will just result
37
 * in things not being fully reproducible and have a slightly
38
 * different coverage.
39
 */
40
#if !defined(_WIN32)
41
930
time_t time(time_t *t) TIME_IMPL(t)
42
#endif
43
44
    int FuzzerInitialize(int *argc, char ***argv)
45
64
{
46
64
    STACK_OF(SSL_COMP) *comp_methods;
47
48
64
    FuzzerSetRand();
49
64
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL);
50
64
    OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
51
64
    ERR_clear_error();
52
64
    CRYPTO_free_ex_index(0, -1);
53
64
    idx = SSL_get_ex_data_X509_STORE_CTX_idx();
54
64
    comp_methods = SSL_COMP_get_compression_methods();
55
64
    if (comp_methods != NULL)
56
64
        sk_SSL_COMP_sort(comp_methods);
57
58
64
    return 1;
59
64
}
60
61
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
62
5.73k
{
63
5.73k
    SSL *client = NULL;
64
5.73k
    BIO *in;
65
5.73k
    BIO *out;
66
5.73k
    SSL_CTX *ctx;
67
68
5.73k
    if (len == 0 || len > INT_MAX)
69
0
        return 0;
70
71
    /* This only fuzzes the initial flow from the client so far. */
72
5.73k
    ctx = SSL_CTX_new(TLS_method());
73
5.73k
    if (ctx == NULL)
74
0
        goto end;
75
76
5.73k
    client = SSL_new(ctx);
77
5.73k
    if (client == NULL)
78
0
        goto end;
79
5.73k
    if (SSL_set_min_proto_version(client, 0) != 1)
80
0
        goto end;
81
5.73k
    if (SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") != 1)
82
0
        goto end;
83
5.73k
    SSL_set_tlsext_host_name(client, "localhost");
84
5.73k
    in = BIO_new(BIO_s_mem());
85
5.73k
    if (in == NULL)
86
0
        goto end;
87
5.73k
    out = BIO_new(BIO_s_mem());
88
5.73k
    if (out == NULL) {
89
0
        BIO_free(in);
90
0
        goto end;
91
0
    }
92
5.73k
    SSL_set_bio(client, in, out);
93
5.73k
    SSL_set_connect_state(client);
94
5.73k
    if ((size_t)BIO_write(in, buf, (int)len) != len)
95
0
        goto end;
96
5.73k
    if (SSL_do_handshake(client) == 1) {
97
        /* Keep reading application data until error or EOF. */
98
278
        uint8_t tmp[1024];
99
365
        for (;;) {
100
365
            if (SSL_read(client, tmp, sizeof(tmp)) <= 0) {
101
278
                break;
102
278
            }
103
365
        }
104
278
    }
105
5.73k
end:
106
5.73k
    SSL_free(client);
107
5.73k
    ERR_clear_error();
108
5.73k
    SSL_CTX_free(ctx);
109
110
5.73k
    return 0;
111
5.73k
}
112
113
void FuzzerCleanup(void)
114
0
{
115
0
    FuzzerClearRand();
116
0
}