1 /* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16
17 #include <openssl/mem.h>
18 #include <openssl/ssl.h>
19
20 struct GlobalState {
GlobalStateGlobalState21 GlobalState() : ctx(SSL_CTX_new(TLS_method())) {}
22
23 bssl::UniquePtr<SSL_CTX> ctx;
24 };
25
26 static GlobalState g_state;
27
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)28 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
29 // Parse in our session.
30 bssl::UniquePtr<SSL_SESSION> session(
31 SSL_SESSION_from_bytes(buf, len, g_state.ctx.get()));
32
33 // If the format was invalid, just return.
34 if (!session) {
35 return 0;
36 }
37
38 // Stress the encoder.
39 size_t encoded_len;
40 uint8_t *encoded;
41 if (!SSL_SESSION_to_bytes(session.get(), &encoded, &encoded_len)) {
42 fprintf(stderr, "SSL_SESSION_to_bytes failed.\n");
43 return 1;
44 }
45
46 OPENSSL_free(encoded);
47 return 0;
48 }
49