aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_fuzzer.cpp
blob: 85dde58592758ddd6b5014328f6e2bb8228eacfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* (C) 2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"
#include <chrono>
#include <botan/internal/filesystem.h>

#if defined(BOTAN_HAS_X509_CERTIFICATES)
   #include <botan/x509cert.h>
   #include <botan/x509_crl.h>
   #include <botan/base64.h>
#endif

#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
   #include <botan/pkcs8.h>
#endif

namespace Botan_Tests {

namespace {

class Fuzzer_Input_Tests : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         std::vector<Test::Result> results;
#if defined(BOTAN_HAS_X509_CERTIFICATES)
         results.push_back(test_x509_fuzz());
#endif

#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
         results.push_back(test_pkcs8());
#endif
         return results;
         }

   private:

#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
      Test::Result test_pkcs8()
         {
         std::vector<std::string> files;

         Test::Result result("PKCS #8 fuzzing");

         try
            {
            files = Botan::get_files_recursive(Test::data_dir() + "/fuzz/pkcs8");
            }
         catch(Botan::No_Filesystem_Access)
            {
            result.note_missing("Filesystem readdir wrapper not implemented");
            return result;
            }

         for(auto vec_file : files)
            {
            try
               {
               std::unique_ptr<Botan::Private_Key> key(
                  Botan::PKCS8::load_key(vec_file, Test::rng()));
               }
            catch(std::exception&) {}

            result.test_success();
            }

         return result;
         }
#endif

#if defined(BOTAN_HAS_X509_CERTIFICATES)
      Test::Result test_x509_fuzz()
         {
         Test::Result result("X.509 fuzzing");

         std::vector<std::string> files;

         try
            {
            files = Botan::get_files_recursive(Test::data_dir() + "/fuzz/x509");
            }
         catch(Botan::No_Filesystem_Access)
            {
            result.note_missing("Filesystem access");
            return result;
            }

         for(auto vec_file : files)
            {
            auto start = std::chrono::steady_clock::now();

            try
               {
               // TODO: check for memory consumption?
               Botan::X509_Certificate cert(vec_file);
               }
            catch(std::exception&)
               {
               }

            result.test_success();

            auto end = std::chrono::steady_clock::now();

            uint64_t duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();

            if(duration > 100)
               {
               result.test_note("Fuzzer test " + vec_file + " took " + std::to_string(duration) + " ms");
               }
            }

         return result;
         }
#endif
   };

BOTAN_REGISTER_TEST("fuzzer", Fuzzer_Input_Tests);


}

}