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 <openssl/err.h>
16 #include <openssl/mem.h>
17 #include <openssl/x509.h>
18
19 #include "../crypto/x509/internal.h"
20
LLVMFuzzerTestOneInput(const uint8_t * buf,size_t len)21 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) {
22 bssl::UniquePtr<X509> x509(d2i_X509(nullptr, &buf, len));
23 if (x509 != nullptr) {
24 // Extract the public key.
25 EVP_PKEY_free(X509_get_pubkey(x509.get()));
26
27 // Fuzz some deferred parsing.
28 x509v3_cache_extensions(x509.get());
29
30 // Fuzz every supported extension.
31 for (int i = 0; i < X509_get_ext_count(x509.get()); i++) {
32 const X509_EXTENSION *ext = X509_get_ext(x509.get(), i);
33 void *parsed = X509V3_EXT_d2i(ext);
34 if (parsed != nullptr) {
35 int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext));
36 BSSL_CHECK(nid != NID_undef);
37
38 // Reserialize the extension. This should succeed if we were able to
39 // parse it.
40 // TODO(crbug.com/boringssl/352): Ideally we would also assert that
41 // |new_ext| is identical to |ext|, but our parser is not strict enough.
42 bssl::UniquePtr<X509_EXTENSION> new_ext(
43 X509V3_EXT_i2d(nid, X509_EXTENSION_get_critical(ext), parsed));
44 BSSL_CHECK(new_ext != nullptr);
45
46 // This can only fail if |ext| was not a supported type, but then
47 // |X509V3_EXT_d2i| should have failed.
48 BSSL_CHECK(X509V3_EXT_free(nid, parsed));
49 }
50 }
51
52 // Reserialize |x509|. This should succeed if we were able to parse it.
53 // TODO(crbug.com/boringssl/352): Ideally we would also assert the output
54 // matches the input, but our parser is not strict enough.
55 uint8_t *der = nullptr;
56 int der_len = i2d_X509(x509.get(), &der);
57 BSSL_CHECK(der_len > 0);
58 OPENSSL_free(der);
59
60 // Reserialize |x509|'s TBSCertificate without reusing the cached encoding.
61 // TODO(crbug.com/boringssl/352): Ideally we would also assert the output
62 // matches the input TBSCertificate, but our parser is not strict enough.
63 der = nullptr;
64 der_len = i2d_re_X509_tbs(x509.get(), &der);
65 BSSL_CHECK(der_len > 0);
66 OPENSSL_free(der);
67
68 BIO *bio = BIO_new(BIO_s_mem());
69 X509_print(bio, x509.get());
70 BIO_free(bio);
71 }
72 ERR_clear_error();
73 return 0;
74 }
75