• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.8
/src/fuzzer/tls_server.cpp
1
/*
2
* (C) 2015,2016 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "fuzzers.h"
8

9
#include <botan/data_src.h>
10
#include <botan/hex.h>
11
#include <botan/pkcs8.h>
12
#include <botan/tls_callbacks.h>
13
#include <botan/tls_policy.h>
14
#include <botan/tls_server.h>
15
#include <botan/tls_session_manager_noop.h>
16

17
#include <memory>
18

19
namespace {
20

21
const char* const fixed_ecdsa_key =
22
   "-----BEGIN PRIVATE KEY-----"
23
   "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgfUjnfxgvrIyrqa5N"
24
   "47X1W50cVStDPbASwRcY6zqehjyhRANCAAQTNF0poMBM4tuCY50NrDJU8za/SK45"
25
   "erOdFpGK7KRWtBE9zNj6J0f1UB+K8GdekFD2me+iL63v+uBqo/PHRPT9"
26
   "-----END PRIVATE KEY-----";
27

28
const char* const fixed_ecdsa_cert =
29
   "-----BEGIN CERTIFICATE-----"
30
   "MIIB3zCCAYWgAwIBAgIRAPFi6dun9OY7YLuZHqKzdEMwCgYIKoZIzj0EAwIwOTEa"
31
   "MBgGA1UEAwwRSXQncyBGdXp6aW5nIFRpbWUxCzAJBgNVBAYTAlZUMQ4wDAYDVQQK"
32
   "EwVCb3RhbjAeFw0yNTAxMjAxMzI0MjdaFw0zODAxMTgxMzI0MjdaMDkxGjAYBgNV"
33
   "BAMMEUl0J3MgRnV6emluZyBUaW1lMQswCQYDVQQGEwJWVDEOMAwGA1UEChMFQm90"
34
   "YW4wWTATBgcqhkjOPQIBBggqhkjOPQMBBwNCAAQTNF0poMBM4tuCY50NrDJU8za/"
35
   "SK45erOdFpGK7KRWtBE9zNj6J0f1UB+K8GdekFD2me+iL63v+uBqo/PHRPT9o24w"
36
   "bDAhBgNVHQ4EGgQYevp/SCkZVWPKNAUSez17HTOyneXEWwpEMBQGA1UdEQQNMAuC"
37
   "CWxvY2FsaG9zdDAMBgNVHRMBAf8EAjAAMCMGA1UdIwQcMBqAGHr6f0gpGVVjyjQF"
38
   "Ens9ex0zsp3lxFsKRDAKBggqhkjOPQQDAgNIADBFAiEApqVCYhySxK/8GLq8wlPh"
39
   "MeBg8CwKO83s1h/GYQZD4CUCID5Mzh5mwrkkAuSENjLXAD4dtiu91Zsoye5J0uuU"
40
   "60v7"
41
   "-----END CERTIFICATE-----";
42

43
class Fuzzer_TLS_Server_Creds : public Botan::Credentials_Manager {
44
   public:
45
      Fuzzer_TLS_Server_Creds() {
1,128✔
46
         Botan::DataSource_Memory cert_in(fixed_ecdsa_cert);
1,128✔
47
         m_ecdsa_cert = std::make_unique<Botan::X509_Certificate>(cert_in);
1,128✔
48

49
         Botan::DataSource_Memory key_in(fixed_ecdsa_key);
1,128✔
50
         m_ecdsa_key.reset(Botan::PKCS8::load_key(key_in).release());
2,256✔
51
      }
2,256✔
52

53
      std::vector<Botan::X509_Certificate> cert_chain(
×
54
         const std::vector<std::string>& algos,
55
         const std::vector<Botan::AlgorithmIdentifier>& /*signature_schemes*/,
56
         const std::string& /*type*/,
57
         const std::string& /*hostname*/) override {
58
         std::vector<Botan::X509_Certificate> v;
×
59

60
         for(const auto& algo : algos) {
×
61
            if(algo == "ECDSA") {
×
62
               v.push_back(*m_ecdsa_cert);
×
63
               break;
64
            }
65
         }
66

67
         return v;
×
68
      }
×
69

70
      std::shared_ptr<Botan::Private_Key> private_key_for(const Botan::X509_Certificate& /*cert*/,
×
71
                                                          const std::string& type,
72
                                                          const std::string& /*context*/) override {
73
         if(type == "ECDSA") {
×
74
            return m_ecdsa_key;
×
75
         }
76
         return nullptr;
×
77
      }
78

79
      Botan::secure_vector<uint8_t> session_ticket_key() override {
×
80
         return Botan::hex_decode_locked("AABBCCDDEEFF00112233445566778899");
×
81
      }
82

83
      Botan::secure_vector<uint8_t> dtls_cookie_secret() override {
×
84
         return Botan::hex_decode_locked("AABBCCDDEEFF00112233445566778899");
×
85
      }
86

87
      std::string psk_identity_hint(const std::string& /*type*/, const std::string& /*context*/) override {
×
88
         return "psk_hint";
×
89
      }
90

91
      std::string psk_identity(const std::string& /*type*/,
×
92
                               const std::string& /*context*/,
93
                               const std::string& /*hint*/) override {
94
         return "psk_id";
×
95
      }
96

97
      std::vector<Botan::TLS::ExternalPSK> find_preshared_keys(
×
98
         std::string_view host,
99
         Botan::TLS::Connection_Side whoami,
100
         const std::vector<std::string>& identities = {},
101
         const std::optional<std::string>& prf = std::nullopt) override {
102
         if(!identities.empty() && std::find(identities.begin(), identities.end(), "psk_id") == identities.end()) {
×
103
            return Botan::Credentials_Manager::find_preshared_keys(host, whoami, identities, prf);
×
104
         }
105

106
         std::vector<Botan::TLS::ExternalPSK> psks;
×
107
         psks.emplace_back("psk_id", "SHA-256", Botan::hex_decode_locked("AABBCCDDEEFF00112233445566778899"));
×
108
         return psks;
×
109
      }
×
110

111
   private:
112
      std::unique_ptr<Botan::X509_Certificate> m_ecdsa_cert;
113
      std::shared_ptr<Botan::Private_Key> m_ecdsa_key;
114
};
115

116
class Fuzzer_TLS_Policy : public Botan::TLS::Policy {
2,256✔
117
   public:
118
      std::vector<uint16_t> ciphersuite_list(Botan::TLS::Protocol_Version version) const override {
×
119
         std::vector<uint16_t> ciphersuites;
×
120

121
         for(auto&& suite : Botan::TLS::Ciphersuite::all_known_ciphersuites()) {
×
122
            if(suite.valid() and suite.usable_in_version(version)) {
×
123
               ciphersuites.push_back(suite.ciphersuite_code());
×
124
            }
125
         }
126

127
         return ciphersuites;
×
128
      }
×
129
};
130

131
class Fuzzer_TLS_Server_Callbacks : public Botan::TLS::Callbacks {
2,256✔
132
   public:
133
      void tls_emit_data(std::span<const uint8_t> /*data*/) override {
1,105✔
134
         // discard
135
      }
1,105✔
136

137
      void tls_record_received(uint64_t /*rec*/, std::span<const uint8_t> /*data*/) override {
×
138
         // ignore peer data
139
      }
×
140

141
      void tls_alert(Botan::TLS::Alert /*alert*/) override {
×
142
         // ignore alert
143
      }
×
144

145
      std::string tls_server_choose_app_protocol(const std::vector<std::string>& client_protos) override {
×
146
         if(client_protos.size() > 1) {
×
147
            return client_protos[0];
×
148
         } else {
149
            return "fuzzy";
×
150
         }
151
      }
152

153
      void tls_verify_cert_chain(const std::vector<Botan::X509_Certificate>& cert_chain,
×
154
                                 const std::vector<std::optional<Botan::OCSP::Response>>& ocsp_responses,
155
                                 const std::vector<Botan::Certificate_Store*>& trusted_roots,
156
                                 Botan::Usage_Type usage,
157
                                 std::string_view hostname,
158
                                 const Botan::TLS::Policy& policy) override {
159
         try {
×
160
            // try to validate to exercise those code paths
161
            Botan::TLS::Callbacks::tls_verify_cert_chain(
×
162
               cert_chain, ocsp_responses, trusted_roots, usage, hostname, policy);
163
         } catch(...) {
×
164
            // ignore validation result
165
         }
×
166
      }
×
167
};
168

169
}  // namespace
170

171
void fuzz(std::span<const uint8_t> in) {
1,131✔
172
   if(in.size() <= 1) {
1,131✔
173
      return;
3✔
174
   }
175

176
   auto session_manager = std::make_shared<Botan::TLS::Session_Manager_Noop>();
1,128✔
177
   auto policy = std::make_shared<Fuzzer_TLS_Policy>();
1,128✔
178
   const Botan::TLS::Server_Information info("server.name", 443);
1,128✔
179
   auto creds = std::make_shared<Fuzzer_TLS_Server_Creds>();
1,128✔
180
   auto callbacks = std::make_shared<Fuzzer_TLS_Server_Callbacks>();
1,128✔
181

182
   const bool is_datagram = (in[0] & 1) == 1;
1,128✔
183

184
   Botan::TLS::Server server(callbacks, session_manager, creds, policy, fuzzer_rng_as_shared(), is_datagram);
6,768✔
185

186
   try {
1,128✔
187
      server.received_data(in.subspan(1, in.size() - 1));
1,128✔
188
   } catch(std::exception& e) {}
1,105✔
189
}
6,768✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc