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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 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_ciphersuite.h>
13
   #include <botan/tls_messages.h>
14
   #include <botan/tls_version.h>
15
   #include <botan/internal/fmt.h>
16
   #include <botan/internal/loadstor.h>
17
   #include <botan/internal/stl_util.h>
18
   #include <sstream>
19

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

26
   #include "tls_helpers.h"
27

28
namespace Botan_CLI {
29

30
namespace {
31

32
class TLS_Ciphersuites final : public Command {
33
   public:
34
      TLS_Ciphersuites() : Command("tls_ciphers --policy=default --version=tls1.2") {}
12✔
35

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

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

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

51
      void go() override {
5✔
52
         const std::string policy_type = get_arg("policy");
5✔
53
         const Botan::TLS::Protocol_Version version = tls_version_from_str(get_arg("version"));
10✔
54

55
         auto policy = load_tls_policy(policy_type);
5✔
56

57
         if(!policy->acceptable_protocol_version(version)) {
5✔
58
            error_output() << "Error: the policy specified does not allow the given TLS version\n";
×
59
            return;
×
60
         }
61

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

69
BOTAN_REGISTER_COMMAND("tls_ciphers", TLS_Ciphersuites);
6✔
70

71
   #if defined(BOTAN_HAS_TLS_12) && defined(BOTAN_HAS_TLS_13)
72

73
class TLS_Client_Hello_Reader final : public Command {
74
   public:
75
      TLS_Client_Hello_Reader() : Command("tls_client_hello --hex input") {}
6✔
76

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

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

81
      void go() override {
2✔
82
         const std::string input_file = get_arg("input");
2✔
83
         std::vector<uint8_t> input;
2✔
84

85
         if(flag_set("hex")) {
2✔
86
            input = Botan::hex_decode(slurp_file_as_str(input_file));
4✔
87
         } else {
88
            input = slurp_file(input_file);
×
89
         }
90

91
         if(input.size() < 45) {
2✔
92
            error_output() << "Input too short to be valid\n";
×
93
            return;
94
         }
95

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

100
            if(input.size() != len + 5) {
2✔
101
               error_output() << "Record layer length invalid\n";
×
102
               return;
103
            }
104

105
            input = std::vector<uint8_t>(input.begin() + 5, input.end());
4✔
106
         }
107

108
         // Assume the handshake header is there, strip it
109
         if(input[0] == 1) {
2✔
110
            const size_t hs_len = Botan::make_uint32(0, input[1], input[2], input[3]);
2✔
111

112
            if(input.size() != hs_len + 4) {
2✔
113
               error_output() << "Handshake layer length invalid\n";
×
114
               return;
115
            }
116

117
            input = std::vector<uint8_t>(input.begin() + 4, input.end());
4✔
118
         }
119

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

134
   private:
135
      static std::string format_hello(
2✔
136
         const std::variant<Botan::TLS::Client_Hello_13, Botan::TLS::Client_Hello_12>& hello) {
137
         std::ostringstream oss;
2✔
138

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

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

148
         oss << "Version: " << version << "\n"
2✔
149
             << "Random: " << Botan::hex_encode(hello_base->random()) << "\n";
2✔
150

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

165
         oss << "Supported signature schemes: ";
2✔
166

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

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

189
         std::map<std::string, bool> hello_flags;
2✔
190
         hello_flags["ALPN"] = hello_base->supports_alpn();
2✔
191

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

211
         for(auto&& i : hello_flags) {
7✔
212
            oss << "Supports " << i.first << "? " << (i.second ? "yes" : "no") << "\n";
7✔
213
         }
214

215
         return oss.str();
4✔
216
      }
2✔
217
};
218

219
BOTAN_REGISTER_COMMAND("tls_client_hello", TLS_Client_Hello_Reader);
3✔
220

221
   #endif
222

223
}  // namespace
224

225
}  // namespace Botan_CLI
226

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