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