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

randombit / botan / 21746428601

06 Feb 2026 07:47AM UTC coverage: 90.074% (+0.002%) from 90.072%
21746428601

push

github

web-flow
Merge pull request #5288 from Rohde-Schwarz/feature/disentangle_tls12_from_tls13

Refactor: Organize most TLS handshake messages into TLS 1.2 and TLS 1.3 modules

102235 of 113501 relevant lines covered (90.07%)

11561717.77 hits per line

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

85.98
/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_13)
20
      #include <botan/tls_messages_13.h>
21
   #endif
22

23
   #include "tls_helpers.h"
24

25
namespace Botan_CLI {
26

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

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

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

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

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

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

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

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

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

66
   #if defined(BOTAN_HAS_TLS_13)
67

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

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

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

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

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

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

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

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

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

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

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

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

115
         try {
2✔
116
            auto hello = Botan::TLS::Client_Hello_13::parse(input);
2✔
117

118
            output() << format_hello(hello);
4✔
119
         } catch(std::exception& e) {
2✔
120
            error_output() << "Parsing client hello failed: " << e.what() << "\n";
×
121
         }
×
122
      }
2✔
123

124
   private:
125
      static std::string format_hello(
2✔
126
         const std::variant<Botan::TLS::Client_Hello_13, Botan::TLS::Client_Hello_12>& hello) {
127
         std::ostringstream oss;
2✔
128

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

132
         const std::string version = std::visit(Botan::overloaded{
4✔
133
                                                   [](const Botan::TLS::Client_Hello_12&) { return "1.2"; },
1✔
134
                                                   [](const Botan::TLS::Client_Hello_13&) { return "1.3"; },
135
                                                },
136
                                                hello);
3✔
137

138
         oss << "Version: " << version << "\n"
2✔
139
             << "Random: " << Botan::hex_encode(hello_base->random()) << "\n";
2✔
140

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

155
         oss << "Supported signature schemes: ";
2✔
156

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

171
         if(auto* sg = hello_base->extensions().get<Botan::TLS::Supported_Groups>()) {
2✔
172
            oss << "Supported Groups: ";
2✔
173
            for(const auto group : sg->groups()) {
26✔
174
               oss << group.to_string().value_or(Botan::fmt("Unknown group: {}", group.wire_code())) << " ";
96✔
175
            }
176
            oss << "\n";
2✔
177
         }
178

179
         std::map<std::string, bool> hello_flags;
2✔
180
         hello_flags["ALPN"] = hello_base->supports_alpn();
2✔
181

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

201
         for(auto&& i : hello_flags) {
7✔
202
            oss << "Supports " << i.first << "? " << (i.second ? "yes" : "no") << "\n";
7✔
203
         }
204

205
         return oss.str();
4✔
206
      }
2✔
207
};
208

209
BOTAN_REGISTER_COMMAND("tls_client_hello", TLS_Client_Hello_Reader);
3✔
210

211
   #endif
212

213
}  // namespace Botan_CLI
214

215
#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