| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-09-02 21:05:45 +08:00
										 |  |  |  * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 21:07:47 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License"); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							|  |  |  |  * or in the file LICENSE in the source distribution. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 23:59:25 +08:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | #include <openssl/rand.h>
 | 
					
						
							|  |  |  | #include <openssl/ssl.h>
 | 
					
						
							|  |  |  | #include <openssl/rsa.h>
 | 
					
						
							| 
									
										
										
										
											2017-01-06 03:18:29 +08:00
										 |  |  | #include <openssl/dsa.h>
 | 
					
						
							|  |  |  | #include <openssl/ec.h>
 | 
					
						
							|  |  |  | #include <openssl/dh.h>
 | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | #include <openssl/err.h>
 | 
					
						
							|  |  |  | #include "fuzzer.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* unused, to avoid warning. */ | 
					
						
							|  |  |  | static int idx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-19 23:59:25 +08:00
										 |  |  | #define FUZZTIME 1485898104
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define TIME_IMPL(t) { if (t != NULL) *t = FUZZTIME; return FUZZTIME; }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2017-04-26 21:52:57 +08:00
										 |  |  |  * This might not work in all cases (and definitely not on Windows | 
					
						
							|  |  |  |  * because of the way linkers are) and callees can still get the | 
					
						
							|  |  |  |  * current time instead of the fixed time. This will just result | 
					
						
							|  |  |  |  * in things not being fully reproducible and have a slightly | 
					
						
							|  |  |  |  * different coverage. | 
					
						
							| 
									
										
										
										
											2017-02-19 23:59:25 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-26 21:52:57 +08:00
										 |  |  | #if !defined(_WIN32)
 | 
					
						
							| 
									
										
										
										
											2017-02-19 23:59:25 +08:00
										 |  |  | time_t time(time_t *t) TIME_IMPL(t) | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | int FuzzerInitialize(int *argc, char ***argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     STACK_OF(SSL_COMP) *comp_methods; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 10:04:58 +08:00
										 |  |  |     FuzzerSetRand(); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS | OPENSSL_INIT_ASYNC, NULL); | 
					
						
							|  |  |  |     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL); | 
					
						
							| 
									
										
										
										
											2019-07-27 00:11:55 +08:00
										 |  |  |     ERR_clear_error(); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     CRYPTO_free_ex_index(0, -1); | 
					
						
							|  |  |  |     idx = SSL_get_ex_data_X509_STORE_CTX_idx(); | 
					
						
							|  |  |  |     comp_methods = SSL_COMP_get_compression_methods(); | 
					
						
							| 
									
										
										
										
											2018-08-08 18:00:55 +08:00
										 |  |  |     if (comp_methods != NULL) | 
					
						
							|  |  |  |         sk_SSL_COMP_sort(comp_methods); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int FuzzerTestOneInput(const uint8_t *buf, size_t len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |     SSL *client = NULL; | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     BIO *in; | 
					
						
							|  |  |  |     BIO *out; | 
					
						
							|  |  |  |     SSL_CTX *ctx; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-18 16:50:27 +08:00
										 |  |  |     if (len == 0 || len > INT_MAX) | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* This only fuzzes the initial flow from the client so far. */ | 
					
						
							|  |  |  |     ctx = SSL_CTX_new(SSLv23_method()); | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |     if (ctx == NULL) | 
					
						
							|  |  |  |         goto end; | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     client = SSL_new(ctx); | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |     if (client == NULL) | 
					
						
							|  |  |  |         goto end; | 
					
						
							| 
									
										
										
										
											2018-07-15 19:49:53 +08:00
										 |  |  |     OPENSSL_assert(SSL_set_min_proto_version(client, 0) == 1); | 
					
						
							| 
									
										
										
										
											2016-12-16 03:11:18 +08:00
										 |  |  |     OPENSSL_assert(SSL_set_cipher_list(client, "ALL:eNULL:@SECLEVEL=0") == 1); | 
					
						
							|  |  |  |     SSL_set_tlsext_host_name(client, "localhost"); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     in = BIO_new(BIO_s_mem()); | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |     if (in == NULL) | 
					
						
							|  |  |  |         goto end; | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     out = BIO_new(BIO_s_mem()); | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |     if (out == NULL) { | 
					
						
							|  |  |  |         BIO_free(in); | 
					
						
							|  |  |  |         goto end; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     SSL_set_bio(client, in, out); | 
					
						
							|  |  |  |     SSL_set_connect_state(client); | 
					
						
							| 
									
										
										
										
											2025-06-18 16:50:27 +08:00
										 |  |  |     OPENSSL_assert((size_t)BIO_write(in, buf, (int)len) == len); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     if (SSL_do_handshake(client) == 1) { | 
					
						
							|  |  |  |         /* Keep reading application data until error or EOF. */ | 
					
						
							|  |  |  |         uint8_t tmp[1024]; | 
					
						
							|  |  |  |         for (;;) { | 
					
						
							|  |  |  |             if (SSL_read(client, tmp, sizeof(tmp)) <= 0) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-03-09 18:06:41 +08:00
										 |  |  |  end: | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  |     SSL_free(client); | 
					
						
							|  |  |  |     ERR_clear_error(); | 
					
						
							|  |  |  |     SSL_CTX_free(ctx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void FuzzerCleanup(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-12-10 10:04:58 +08:00
										 |  |  |     FuzzerClearRand(); | 
					
						
							| 
									
										
										
										
											2016-12-08 06:12:04 +08:00
										 |  |  | } |