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

randombit / botan / 21756362022

06 Feb 2026 03:40PM UTC coverage: 90.069% (-0.004%) from 90.073%
21756362022

Pull #5293

github

web-flow
Merge 5018e3dec into 8ea0ca252
Pull Request #5293: Refactor: Disentangle Client_Hello implementations

102251 of 113525 relevant lines covered (90.07%)

11438009.49 hits per line

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

85.59
/src/cli/tls_utils.cpp
1
/*
2
* (C) 2016 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include "cli.h"
8

9
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
10

11
   #include <botan/hex.h>
12
   #include <botan/tls_messages.h>
13
   #include <botan/tls_version.h>
14
   #include <botan/internal/fmt.h>
15
   #include <botan/internal/loadstor.h>
16
   #include <botan/internal/stl_util.h>
17
   #include <sstream>
18

19
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13)
20
      #include <botan/tls_messages_12.h>
21
      #include <botan/tls_messages_13.h>
22
   #endif
23

24
   #include "tls_helpers.h"
25

26
namespace Botan_CLI {
27

28
class TLS_Ciphersuites final : public Command {
29
   public:
30
      TLS_Ciphersuites() : Command("tls_ciphers --policy=default --version=tls1.2") {}
12✔
31

32
      static Botan::TLS::Protocol_Version tls_version_from_str(const std::string& str) {
5✔
33
         if(str == "tls1.2" || str == "TLS1.2" || str == "TLS-1.2") {
5✔
34
            return Botan::TLS::Protocol_Version::TLS_V12;
5✔
35
         }
36
         if(str == "dtls1.2" || str == "DTLS1.2" || str == "DTLS-1.2") {
×
37
            return Botan::TLS::Protocol_Version::DTLS_V12;
×
38
         } else {
39
            throw CLI_Error("Unknown TLS version '" + str + "'");
×
40
         }
41
      }
42

43
      std::string group() const override { return "tls"; }
1✔
44

45
      std::string description() const override { return "Lists all ciphersuites for a policy and TLS version"; }
1✔
46

47
      void go() override {
5✔
48
         const std::string policy_type = get_arg("policy");
5✔
49
         const Botan::TLS::Protocol_Version version = tls_version_from_str(get_arg("version"));
10✔
50

51
         auto policy = load_tls_policy(policy_type);
5✔
52

53
         if(!policy->acceptable_protocol_version(version)) {
5✔
54
            error_output() << "Error: the policy specified does not allow the given TLS version\n";
×
55
            return;
×
56
         }
57

58
         for(const uint16_t suite_id : policy->ciphersuite_list(version)) {
119✔
59
            const auto s = Botan::TLS::Ciphersuite::by_id(suite_id);
114✔
60
            output() << ((s) ? s->to_string() : "unknown cipher suite") << "\n";
456✔
61
         }
5✔
62
      }
5✔
63
};
64

65
BOTAN_REGISTER_COMMAND("tls_ciphers", TLS_Ciphersuites);
6✔
66

67
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13)
68

69
class TLS_Client_Hello_Reader final : public Command {
70
   public:
71
      TLS_Client_Hello_Reader() : Command("tls_client_hello --hex input") {}
6✔
72

73
      std::string group() const override { return "tls"; }
1✔
74

75
      std::string description() const override { return "Parse a TLS client hello message"; }
1✔
76

77
      void go() override {
2✔
78
         const std::string input_file = get_arg("input");
2✔
79
         std::vector<uint8_t> input;
2✔
80

81
         if(flag_set("hex")) {
2✔
82
            input = Botan::hex_decode(slurp_file_as_str(input_file));
4✔
83
         } else {
84
            input = slurp_file(input_file);
×
85
         }
86

87
         if(input.size() < 45) {
2✔
88
            error_output() << "Input too short to be valid\n";
×
89
            return;
90
         }
91

92
         // Input also contains the record layer header, strip it
93
         if(input[0] == 22) {
2✔
94
            const size_t len = Botan::make_uint16(input[3], input[4]);
2✔
95

96
            if(input.size() != len + 5) {
2✔
97
               error_output() << "Record layer length invalid\n";
×
98
               return;
99
            }
100

101
            input = std::vector<uint8_t>(input.begin() + 5, input.end());
4✔
102
         }
103

104
         // Assume the handshake header is there, strip it
105
         if(input[0] == 1) {
2✔
106
            const size_t hs_len = Botan::make_uint32(0, input[1], input[2], input[3]);
2✔
107

108
            if(input.size() != hs_len + 4) {
2✔
109
               error_output() << "Handshake layer length invalid\n";
×
110
               return;
111
            }
112

113
            input = std::vector<uint8_t>(input.begin() + 4, input.end());
4✔
114
         }
115

116
         try {
2✔
117
            output() << format_hello([&]() -> std::variant<Botan::TLS::Client_Hello_13, Botan::TLS::Client_Hello_12> {
6✔
118
               auto data = Botan::TLS::Client_Hello_13::parse(input);
2✔
119
               if(std::holds_alternative<Botan::TLS::Client_Hello_13>(data)) {
2✔
120
                  return std::get<Botan::TLS::Client_Hello_13>(std::move(data));
1✔
121
               } else {
122
                  return Botan::TLS::Client_Hello_12(input);
1✔
123
               }
124
            }());
8✔
125
         } catch(std::exception& e) {
×
126
            error_output() << "Parsing client hello failed: " << e.what() << "\n";
×
127
         }
×
128
      }
2✔
129

130
   private:
131
      static std::string format_hello(
2✔
132
         const std::variant<Botan::TLS::Client_Hello_13, Botan::TLS::Client_Hello_12>& hello) {
133
         std::ostringstream oss;
2✔
134

135
         const auto* hello_base =
2✔
136
            std::visit([](const auto& ch) -> const Botan::TLS::Client_Hello* { return &ch; }, hello);
2✔
137

138
         const std::string version = std::visit(Botan::overloaded{
4✔
139
                                                   [](const Botan::TLS::Client_Hello_12&) { return "1.2"; },
1✔
140
                                                   [](const Botan::TLS::Client_Hello_13&) { return "1.3"; },
141
                                                },
142
                                                hello);
3✔
143

144
         oss << "Version: " << version << "\n"
2✔
145
             << "Random: " << Botan::hex_encode(hello_base->random()) << "\n";
2✔
146

147
         if(!hello_base->session_id().empty()) {
2✔
148
            oss << "SessionID: " << Botan::hex_encode(hello_base->session_id().get()) << "\n";
2✔
149
         }
150
         for(const uint16_t csuite_id : hello_base->ciphersuites()) {
43✔
151
            const auto csuite = Botan::TLS::Ciphersuite::by_id(csuite_id);
41✔
152
            if(csuite && csuite->valid()) {
41✔
153
               oss << "Cipher: " << csuite->to_string() << "\n";
111✔
154
            } else if(csuite_id == 0x00FF) {
4✔
155
               oss << "Cipher: EMPTY_RENEGOTIATION_INFO_SCSV\n";
×
156
            } else {
157
               oss << "Cipher: Unknown (" << std::hex << csuite_id << ")\n";
4✔
158
            }
159
         }
160

161
         oss << "Supported signature schemes: ";
2✔
162

163
         if(hello_base->signature_schemes().empty()) {
2✔
164
            oss << "Did not send signature_algorithms extension\n";
×
165
         } else {
166
            for(const Botan::TLS::Signature_Scheme scheme : hello_base->signature_schemes()) {
20✔
167
               try {
18✔
168
                  auto s = scheme.to_string();
18✔
169
                  oss << s << " ";
18✔
170
               } catch(...) {
18✔
171
                  oss << "(" << std::hex << static_cast<unsigned int>(scheme.wire_code()) << ") ";
×
172
               }
×
173
            }
2✔
174
            oss << "\n";
2✔
175
         }
176

177
         if(auto* sg = hello_base->extensions().get<Botan::TLS::Supported_Groups>()) {
2✔
178
            oss << "Supported Groups: ";
2✔
179
            for(const auto group : sg->groups()) {
26✔
180
               oss << group.to_string().value_or(Botan::fmt("Unknown group: {}", group.wire_code())) << " ";
96✔
181
            }
182
            oss << "\n";
2✔
183
         }
184

185
         std::map<std::string, bool> hello_flags;
2✔
186
         hello_flags["ALPN"] = hello_base->supports_alpn();
2✔
187

188
         std::visit(Botan::overloaded{
2✔
189
                       [&](const Botan::TLS::Client_Hello_12& ch12) {
1✔
190
                          hello_flags["Encrypt Then Mac"] = ch12.supports_encrypt_then_mac();
1✔
191
                          hello_flags["Extended Master Secret"] = ch12.supports_extended_master_secret();
1✔
192
                          hello_flags["Session Ticket"] = ch12.supports_session_ticket();
1✔
193
                       },
1✔
194
                       [&](const Botan::TLS::Client_Hello_13& ch13) {
1✔
195
                          if(auto* ks = ch13.extensions().get<Botan::TLS::Key_Share>()) {
1✔
196
                             oss << "Key Shares: ";
1✔
197
                             for(const auto group : ks->offered_groups()) {
2✔
198
                                oss << group.to_string().value_or(Botan::fmt("Unknown group: {}", group.wire_code()))
4✔
199
                                    << " ";
2✔
200
                             }
1✔
201
                             oss << "\n";
1✔
202
                          }
203
                       },
1✔
204
                    },
205
                    hello);
206

207
         for(auto&& i : hello_flags) {
7✔
208
            oss << "Supports " << i.first << "? " << (i.second ? "yes" : "no") << "\n";
7✔
209
         }
210

211
         return oss.str();
4✔
212
      }
2✔
213
};
214

215
BOTAN_REGISTER_COMMAND("tls_client_hello", TLS_Client_Hello_Reader);
3✔
216

217
   #endif
218

219
}  // namespace Botan_CLI
220

221
#endif
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