Coverage Report

Created: 2026-10-28 06:28

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