/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 | 5.91k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len) { |
22 | 5.91k | bssl::UniquePtr<X509> x509(d2i_X509(nullptr, &buf, len)); |
23 | 5.91k | if (x509 != nullptr) { |
24 | | // Extract the public key. |
25 | 2.79k | EVP_PKEY_free(X509_get_pubkey(x509.get())); |
26 | | |
27 | | // Fuzz some deferred parsing. |
28 | 2.79k | x509v3_cache_extensions(x509.get()); |
29 | | |
30 | | // Fuzz every supported extension. |
31 | 32.9k | for (int i = 0; i < X509_get_ext_count(x509.get()); i++) { |
32 | 30.1k | const X509_EXTENSION *ext = X509_get_ext(x509.get(), i); |
33 | 30.1k | void *parsed = X509V3_EXT_d2i(ext); |
34 | 30.1k | if (parsed != nullptr) { |
35 | 5.49k | int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ext)); |
36 | 5.49k | 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 | 5.49k | bssl::UniquePtr<X509_EXTENSION> new_ext( |
43 | 5.49k | X509V3_EXT_i2d(nid, X509_EXTENSION_get_critical(ext), parsed)); |
44 | 5.49k | 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 | 5.49k | BSSL_CHECK(X509V3_EXT_free(nid, parsed)); |
49 | 5.49k | } |
50 | 30.1k | } |
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 | 2.79k | uint8_t *der = nullptr; |
56 | 2.79k | int der_len = i2d_X509(x509.get(), &der); |
57 | 2.79k | BSSL_CHECK(der_len > 0); |
58 | 2.79k | 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 | 2.79k | der = nullptr; |
64 | 2.79k | der_len = i2d_re_X509_tbs(x509.get(), &der); |
65 | 2.79k | BSSL_CHECK(der_len > 0); |
66 | 2.79k | OPENSSL_free(der); |
67 | | |
68 | 2.79k | BIO *bio = BIO_new(BIO_s_mem()); |
69 | 2.79k | X509_print(bio, x509.get()); |
70 | 2.79k | BIO_free(bio); |
71 | 2.79k | } |
72 | 5.91k | ERR_clear_error(); |
73 | 5.91k | return 0; |
74 | 5.91k | } |