/src/openssl41/fuzz/dtlsclient.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.72k | { |
63 | 5.72k | SSL *client = NULL; |
64 | 5.72k | BIO *in; |
65 | 5.72k | BIO *out; |
66 | 5.72k | SSL_CTX *ctx; |
67 | | |
68 | 5.72k | 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.72k | ctx = SSL_CTX_new(DTLS_client_method()); |
73 | 5.72k | if (ctx == NULL) |
74 | 0 | goto end; |
75 | | |
76 | 5.72k | client = SSL_new(ctx); |
77 | 5.72k | if (client == NULL) |
78 | 0 | goto end; |
79 | 5.72k | if (SSL_set_min_proto_version(client, 0) != 1) |
80 | 0 | goto end; |
81 | 5.72k | if (SSL_set_max_proto_version(client, 0) != 1) |
82 | 0 | goto end; |
83 | 5.72k | if (SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") != 1) |
84 | 0 | goto end; |
85 | 5.72k | SSL_set_tlsext_host_name(client, "localhost"); |
86 | 5.72k | in = BIO_new(BIO_s_mem()); |
87 | 5.72k | if (in == NULL) |
88 | 0 | goto end; |
89 | 5.72k | out = BIO_new(BIO_s_mem()); |
90 | 5.72k | if (out == NULL) { |
91 | 0 | BIO_free(in); |
92 | 0 | goto end; |
93 | 0 | } |
94 | 5.72k | SSL_set_bio(client, in, out); |
95 | 5.72k | SSL_set_connect_state(client); |
96 | 5.72k | if ((size_t)BIO_write(in, buf, (int)len) != len) |
97 | 0 | goto end; |
98 | 5.72k | if (SSL_do_handshake(client) == 1) { |
99 | | /* Keep reading application data until error or EOF. */ |
100 | 0 | uint8_t tmp[1024]; |
101 | 0 | for (;;) { |
102 | 0 | if (SSL_read(client, tmp, sizeof(tmp)) <= 0) { |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | } |
107 | 5.72k | end: |
108 | 5.72k | SSL_free(client); |
109 | 5.72k | ERR_clear_error(); |
110 | 5.72k | SSL_CTX_free(ctx); |
111 | | |
112 | 5.72k | return 0; |
113 | 5.72k | } |
114 | | |
115 | | void FuzzerCleanup(void) |
116 | 0 | { |
117 | 0 | FuzzerClearRand(); |
118 | 0 | } |