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

randombit / botan / 20337522608

18 Dec 2025 12:50PM UTC coverage: 90.555% (+0.2%) from 90.358%
20337522608

Pull #5167

github

web-flow
Merge 4a0197138 into 1fd0d0cf5
Pull Request #5167: Changes to reduce unnecessary inclusions

101518 of 112106 relevant lines covered (90.56%)

12598813.82 hits per line

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

34.78
/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&, const std::string&) override { return "psk_hint"; }
×
88

89
      std::string psk_identity(const std::string&, const std::string&, const std::string&) override { return "psk_id"; }
×
90

91
      std::vector<Botan::TLS::ExternalPSK> find_preshared_keys(
×
92
         std::string_view host,
93
         Botan::TLS::Connection_Side whoami,
94
         const std::vector<std::string>& identities = {},
95
         const std::optional<std::string>& prf = std::nullopt) override {
96
         if(!identities.empty() && std::find(identities.begin(), identities.end(), "psk_id") == identities.end()) {
×
97
            return Botan::Credentials_Manager::find_preshared_keys(host, whoami, identities, prf);
×
98
         }
99

100
         std::vector<Botan::TLS::ExternalPSK> psks;
×
101
         psks.emplace_back("psk_id", "SHA-256", Botan::hex_decode_locked("AABBCCDDEEFF00112233445566778899"));
×
102
         return psks;
×
103
      }
×
104

105
   private:
106
      std::unique_ptr<Botan::X509_Certificate> m_ecdsa_cert;
107
      std::shared_ptr<Botan::Private_Key> m_ecdsa_key;
108
};
109

110
class Fuzzer_TLS_Policy : public Botan::TLS::Policy {
2,256✔
111
   public:
112
      std::vector<uint16_t> ciphersuite_list(Botan::TLS::Protocol_Version) const override {
×
113
         std::vector<uint16_t> ciphersuites;
×
114

115
         for(auto&& suite : Botan::TLS::Ciphersuite::all_known_ciphersuites()) {
×
116
            if(suite.valid()) {
×
117
               ciphersuites.push_back(suite.ciphersuite_code());
×
118
            }
119
         }
120

121
         return ciphersuites;
×
122
      }
×
123
};
124

125
class Fuzzer_TLS_Server_Callbacks : public Botan::TLS::Callbacks {
2,256✔
126
   public:
127
      void tls_emit_data(std::span<const uint8_t>) override {
1,105✔
128
         // discard
129
      }
1,105✔
130

131
      void tls_record_received(uint64_t, std::span<const uint8_t>) override {
×
132
         // ignore peer data
133
      }
×
134

135
      void tls_alert(Botan::TLS::Alert) override {
×
136
         // ignore alert
137
      }
×
138

139
      std::string tls_server_choose_app_protocol(const std::vector<std::string>& client_protos) override {
×
140
         if(client_protos.size() > 1) {
×
141
            return client_protos[0];
×
142
         } else {
143
            return "fuzzy";
×
144
         }
145
      }
146

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

163
}  // namespace
164

165
void fuzz(std::span<const uint8_t> in) {
1,131✔
166
   if(in.size() <= 1) {
1,131✔
167
      return;
3✔
168
   }
169

170
   auto session_manager = std::make_shared<Botan::TLS::Session_Manager_Noop>();
1,128✔
171
   auto policy = std::make_shared<Fuzzer_TLS_Policy>();
1,128✔
172
   const Botan::TLS::Server_Information info("server.name", 443);
1,128✔
173
   auto creds = std::make_shared<Fuzzer_TLS_Server_Creds>();
1,128✔
174
   auto callbacks = std::make_shared<Fuzzer_TLS_Server_Callbacks>();
1,128✔
175

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

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

180
   try {
1,128✔
181
      server.received_data(in.subspan(1, in.size() - 1));
1,128✔
182
   } catch(std::exception& e) {}
1,105✔
183
}
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