1 // Copyright 2016 The BoringSSL Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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