blob: d25fb826ba0cde167e457732897daf36d94ec1cb [file] [log] [blame]
Robert Sloan15073af2016-11-29 10:52:24 -08001/* 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
Adam Langley46db7af2017-02-01 15:49:37 -080020struct GlobalState {
21 GlobalState() : ctx(SSL_CTX_new(TLS_method())) {}
22
23 bssl::UniquePtr<SSL_CTX> ctx;
24};
25
26static GlobalState g_state;
27
Adam Langleyb7d53ba2017-02-06 09:37:05 -080028extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
Robert Sloan15073af2016-11-29 10:52:24 -080029 // Parse in our session.
Adam Langley46db7af2017-02-01 15:49:37 -080030 bssl::UniquePtr<SSL_SESSION> session(
31 SSL_SESSION_from_bytes(buf, len, g_state.ctx.get()));
Robert Sloan15073af2016-11-29 10:52:24 -080032
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}