xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/fuzz/conf.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Copyright (c) 2022, 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 <openssl/bio.h>
16 #include <openssl/conf.h>
17 #include <openssl/x509.h>
18 
19 #include <algorithm>
20 
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
22   // The string-based extensions APIs routinely produce output quadratic in
23   // their input. Cap the input size to mitigate this. See also
24   // https://crbug.com/boringssl/611.
25   len = std::min(len, size_t{8 * 1024});
26 
27   bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(buf, len));
28   bssl::UniquePtr<CONF> conf(NCONF_new(nullptr));
29   if (NCONF_load_bio(conf.get(), bio.get(), nullptr)) {
30     // Run with and without |X509V3_CTX| information.
31     bssl::UniquePtr<X509> cert(X509_new());
32     X509V3_CTX ctx;
33     X509V3_set_ctx(&ctx, /*subject=*/cert.get(), /*issuer=*/cert.get(), nullptr,
34                    nullptr, 0);
35     X509V3_EXT_add_nconf(conf.get(), &ctx, "default", cert.get());
36 
37     cert.reset(X509_new());
38     X509V3_EXT_add_nconf(conf.get(), nullptr, "default", cert.get());
39   }
40   return 0;
41 }
42