/src/nss/fuzz/targets/certDN.cc
Line | Count | Source |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
3 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
4 | | |
5 | | #include <cstddef> |
6 | | #include <cstdint> |
7 | | #include <string> |
8 | | |
9 | | #include "cert.h" |
10 | | #include "nss_scoped_ptrs.h" |
11 | | |
12 | | #include "base/database.h" |
13 | | #include "seccomon.h" |
14 | | |
15 | | template <typename F, typename... Args> |
16 | 36.0k | void TestFunction(F f, Args... args) { |
17 | 36.0k | PORT_Free(f(args...)); |
18 | 36.0k | } void TestFunction<char* (*)(CERTNameStr*), CERTNameStr*>(char* (*)(CERTNameStr*), CERTNameStr*) Line | Count | Source | 16 | 5.15k | void TestFunction(F f, Args... args) { | 17 | 5.15k | PORT_Free(f(args...)); | 18 | 5.15k | } |
void TestFunction<char* (*)(CERTNameStr const*), CERTNameStr*>(char* (*)(CERTNameStr const*), CERTNameStr*) Line | Count | Source | 16 | 23.1k | void TestFunction(F f, Args... args) { | 17 | 23.1k | PORT_Free(f(args...)); | 18 | 23.1k | } |
void TestFunction<char* (*)(CERTNameStr*, CertStrictnessLevels), CERTNameStr*, CertStrictnessLevels>(char* (*)(CERTNameStr*, CertStrictnessLevels), CERTNameStr*, CertStrictnessLevels) Line | Count | Source | 16 | 7.73k | void TestFunction(F f, Args... args) { | 17 | 7.73k | PORT_Free(f(args...)); | 18 | 7.73k | } |
|
19 | | |
20 | 3.27k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
21 | 3.27k | static NSSDatabase db = NSSDatabase(); |
22 | 3.27k | static CERTName* refName = CERT_AsciiToName("CN=Fuzz,O=Fuzz,C=US"); |
23 | 3.27k | assert(refName); |
24 | | |
25 | 3.27k | std::string name(data, data + size); |
26 | | |
27 | 3.27k | ScopedCERTName certName(CERT_AsciiToName(name.c_str())); |
28 | 3.27k | if (!certName) { |
29 | 696 | return 0; |
30 | 696 | } |
31 | | |
32 | 2.57k | TestFunction(CERT_FormatName, certName.get()); |
33 | 2.57k | TestFunction(CERT_NameToAscii, certName.get()); |
34 | 2.57k | TestFunction(CERT_GetCertEmailAddress, certName.get()); |
35 | | |
36 | | // These functions call CERT_GetNameElement or CERT_GetLastNameElement |
37 | | // with different OIDs. CERT_GetNameElement itself is static and not |
38 | | // accessible from here. |
39 | 2.57k | TestFunction(CERT_GetCertUid, certName.get()); |
40 | 2.57k | TestFunction(CERT_GetCommonName, certName.get()); |
41 | 2.57k | TestFunction(CERT_GetCountryName, certName.get()); |
42 | 2.57k | TestFunction(CERT_GetDomainComponentName, certName.get()); |
43 | 2.57k | TestFunction(CERT_GetLocalityName, certName.get()); |
44 | 2.57k | TestFunction(CERT_GetOrgName, certName.get()); |
45 | 2.57k | TestFunction(CERT_GetOrgUnitName, certName.get()); |
46 | 2.57k | TestFunction(CERT_GetStateName, certName.get()); |
47 | | |
48 | 2.57k | TestFunction(CERT_NameToAsciiInvertible, certName.get(), CERT_N2A_READABLE); |
49 | 2.57k | TestFunction(CERT_NameToAsciiInvertible, certName.get(), CERT_N2A_STRICT); |
50 | 2.57k | TestFunction(CERT_NameToAsciiInvertible, certName.get(), CERT_N2A_INVERTIBLE); |
51 | | |
52 | | // Exercise CERT_CopyName round-trip. |
53 | 2.57k | PORTCheapArenaPool pool; |
54 | 2.57k | PORT_InitCheapArena(&pool, DER_DEFAULT_CHUNKSIZE); |
55 | | |
56 | 2.57k | CERTName copied = {0}; |
57 | 2.57k | if (CERT_CopyName(&pool.arena, &copied, certName.get()) == SECSuccess) { |
58 | 2.57k | (void)CERT_CompareName(certName.get(), &copied); |
59 | 2.57k | } |
60 | | |
61 | 2.57k | PORT_DestroyCheapArena(&pool); |
62 | | |
63 | | // Compare against a fixed reference name to exercise mismatch paths. |
64 | 2.57k | (void)CERT_CompareName(certName.get(), refName); |
65 | | |
66 | 2.57k | return 0; |
67 | 3.27k | } |