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