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

randombit / botan / 5587580523

18 Jul 2023 12:45PM UTC coverage: 91.72% (+0.03%) from 91.691%
5587580523

Pull #3618

github

web-flow
Merge b6d23d19e into 65b754862
Pull Request #3618: [TLS 1.3] PSK Support

78438 of 85519 relevant lines covered (91.72%)

12184016.91 hits per line

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

77.2
/src/cli/tls_client.cpp
1
/*
2
* (C) 2014,2015 Jack Lloyd
3
*     2016 Matthias Gierlings
4
*     2017 René Korthaus, Rohde & Schwarz Cybersecurity
5
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6
*     2023 René Meusel, Rohde & Schwarz Cybersecurity
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include "cli.h"
12

13
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM) && defined(BOTAN_TARGET_OS_HAS_SOCKETS)
14

15
   #include <botan/hex.h>
16
   #include <botan/ocsp.h>
17
   #include <botan/tls_callbacks.h>
18
   #include <botan/tls_client.h>
19
   #include <botan/tls_policy.h>
20
   #include <botan/tls_session_manager_memory.h>
21
   #include <botan/x509path.h>
22
   #include <fstream>
23

24
   #if defined(BOTAN_HAS_TLS_SQLITE3_SESSION_MANAGER)
25
      #include <botan/tls_session_manager_sqlite.h>
26
   #endif
27

28
   #include <memory>
29
   #include <string>
30

31
   #include "socket_utils.h"
32
   #include "tls_helpers.h"
33

34
namespace Botan_CLI {
35

36
class TLS_Client;
37

38
namespace {
39

40
class Callbacks : public Botan::TLS::Callbacks {
5✔
41
   public:
42
      Callbacks(TLS_Client& client_command) : m_client_command(client_command) {}
5✔
43

44
      std::ostream& output();
45
      bool flag_set(const std::string& flag_name) const;
46
      void send(std::span<const uint8_t> buffer);
47

48
      void tls_verify_cert_chain(const std::vector<Botan::X509_Certificate>& cert_chain,
3✔
49
                                 const std::vector<std::optional<Botan::OCSP::Response>>& ocsp,
50
                                 const std::vector<Botan::Certificate_Store*>& trusted_roots,
51
                                 Botan::Usage_Type usage,
52
                                 std::string_view hostname,
53
                                 const Botan::TLS::Policy& policy) override {
54
         if(cert_chain.empty()) {
3✔
55
            throw Botan::Invalid_Argument("Certificate chain was empty");
×
56
         }
57

58
         Botan::Path_Validation_Restrictions restrictions(policy.require_cert_revocation_info(),
3✔
59
                                                          policy.minimum_signature_strength());
9✔
60

61
         auto ocsp_timeout = std::chrono::milliseconds(1000);
3✔
62

63
         Botan::Path_Validation_Result result = Botan::x509_path_validate(
3✔
64
            cert_chain, restrictions, trusted_roots, hostname, usage, tls_current_timestamp(), ocsp_timeout, ocsp);
3✔
65

66
         output() << "Certificate validation status: " << result.result_string() << "\n";
6✔
67
         if(result.successful_validation()) {
3✔
68
            auto status = result.all_statuses();
3✔
69

70
            if(!status.empty() && status[0].contains(Botan::Certificate_Status_Code::OCSP_RESPONSE_GOOD)) {
6✔
71
               output() << "Valid OCSP response for this server\n";
×
72
            }
73
         }
3✔
74
      }
3✔
75

76
      void tls_session_activated() override { output() << "Handshake complete\n"; }
5✔
77

78
      void tls_session_established(const Botan::TLS::Session_Summary& session) override {
5✔
79
         output() << "Handshake complete, " << session.version().to_string() << " using "
5✔
80
                  << session.ciphersuite().to_string();
20✔
81

82
         if(const auto& psk = session.external_psk_identity()) {
5✔
83
            output() << " (utilized PSK identity: " << maybe_hex_encode(psk.value()) << ")";
4✔
84
         }
85

86
         output() << std::endl;
5✔
87

88
         if(const auto& session_id = session.session_id(); !session_id.empty()) {
5✔
89
            output() << "Session ID " << Botan::hex_encode(session_id.get()) << "\n";
10✔
90
         }
91

92
         if(const auto& session_ticket = session.session_ticket()) {
5✔
93
            output() << "Session ticket " << Botan::hex_encode(session_ticket->get()) << "\n";
×
94
         }
95

96
         if(flag_set("print-certs")) {
10✔
97
            const std::vector<Botan::X509_Certificate>& certs = session.peer_certs();
98

99
            for(size_t i = 0; i != certs.size(); ++i) {
×
100
               output() << "Certificate " << i + 1 << "/" << certs.size() << "\n";
×
101
               output() << certs[i].to_string();
×
102
               output() << certs[i].PEM_encode();
×
103
            }
104
         }
105
      }
5✔
106

107
      void tls_emit_data(std::span<const uint8_t> buf) override {
24✔
108
         if(flag_set("debug")) {
48✔
109
            output() << "<< " << Botan::hex_encode(buf) << "\n";
×
110
         }
111

112
         send(buf);
48✔
113
      }
24✔
114

115
      void tls_alert(Botan::TLS::Alert alert) override { output() << "Alert: " << alert.type_string() << "\n"; }
×
116

117
      void tls_record_received(uint64_t /*seq_no*/, std::span<const uint8_t> buf) override {
8✔
118
         for(const auto c : buf) {
1,143✔
119
            output() << c;
1,135✔
120
         }
121
      }
8✔
122

123
      std::vector<uint8_t> tls_sign_message(const Botan::Private_Key& key,
×
124
                                            Botan::RandomNumberGenerator& rng,
125
                                            const std::string_view padding,
126
                                            Botan::Signature_Format format,
127
                                            const std::vector<uint8_t>& msg) override {
128
         output() << "Performing client authentication\n";
×
129
         return Botan::TLS::Callbacks::tls_sign_message(key, rng, padding, format, msg);
×
130
      }
131

132
   private:
133
      TLS_Client& m_client_command;
134
};
135

136
}  // namespace
137

138
class TLS_Client final : public Command {
139
   public:
140
      TLS_Client() :
6✔
141
            Command(
142
               "tls_client host --port=443 --print-certs --policy=default "
143
               "--skip-system-cert-store --trusted-cas= --tls-version=default "
144
               "--session-db= --session-db-pass= --next-protocols= --type=tcp "
145
               "--client-cert= --client-cert-key= --psk= --psk-identity= --psk-prf=SHA-256 --debug") {
12✔
146
         init_sockets();
6✔
147
      }
6✔
148

149
      ~TLS_Client() override { stop_sockets(); }
6✔
150

151
      TLS_Client(const TLS_Client& other) = delete;
152
      TLS_Client(TLS_Client&& other) = delete;
153
      TLS_Client& operator=(const TLS_Client& other) = delete;
154
      TLS_Client& operator=(TLS_Client&& other) = delete;
155

156
      std::string group() const override { return "tls"; }
2✔
157

158
      std::string description() const override { return "Connect to a host using TLS/DTLS"; }
1✔
159

160
      void go() override {
5✔
161
         std::shared_ptr<Botan::TLS::Session_Manager> session_mgr;
5✔
162

163
         auto callbacks = std::make_shared<Callbacks>(*this);
5✔
164

165
         const std::string sessions_db = get_arg("session-db");
5✔
166
         const std::string host = get_arg("host");
5✔
167
         const uint16_t port = get_arg_u16("port");
5✔
168
         const std::string transport = get_arg("type");
5✔
169
         const std::string next_protos = get_arg("next-protocols");
5✔
170
         const bool use_system_cert_store = flag_set("skip-system-cert-store") == false;
5✔
171
         const std::string trusted_CAs = get_arg("trusted-cas");
5✔
172
         const auto tls_version = get_arg("tls-version");
5✔
173

174
         if(!sessions_db.empty()) {
5✔
175
   #if defined(BOTAN_HAS_TLS_SQLITE3_SESSION_MANAGER)
176
            const std::string sessions_passphrase = get_passphrase_arg("Session DB passphrase", "session-db-pass");
×
177
            session_mgr.reset(
×
178
               new Botan::TLS::Session_Manager_SQLite(sessions_passphrase, rng_as_shared(), sessions_db));
×
179
   #else
180
            error_output() << "Ignoring session DB file, sqlite not enabled\n";
181
   #endif
182
         }
×
183

184
         if(!session_mgr) {
5✔
185
            session_mgr = std::make_shared<Botan::TLS::Session_Manager_In_Memory>(rng_as_shared());
15✔
186
         }
187

188
         auto policy = load_tls_policy(get_arg("policy"));
10✔
189

190
         if(transport != "tcp" && transport != "udp") {
5✔
191
            throw CLI_Usage_Error("Invalid transport type '" + transport + "' for TLS");
×
192
         }
193

194
         const std::vector<std::string> protocols_to_offer = Command::split_on(next_protos, ',');
5✔
195

196
         if(!policy) {
5✔
197
            policy = std::make_shared<Botan::TLS::Policy>();
×
198
         }
199

200
         const bool use_tcp = (transport == "tcp");
5✔
201
         Botan::TLS::Protocol_Version version = policy->latest_supported_version(!use_tcp);
5✔
202

203
         if(tls_version != "default") {
5✔
204
            if(tls_version == "1.2") {
5✔
205
               version = use_tcp ? Botan::TLS::Protocol_Version::TLS_V12 : Botan::TLS::Protocol_Version::DTLS_V12;
2✔
206
            } else if(tls_version == "1.3") {
3✔
207
               version = use_tcp ? Botan::TLS::Protocol_Version::TLS_V13 : Botan::TLS::Protocol_Version::DTLS_V13;
3✔
208
            } else {
209
               error_output() << "Unknown TLS protocol version " << tls_version << '\n';
×
210
            }
211
         }
212

213
         struct sockaddr_storage addrbuf;
5✔
214
         std::string hostname;
5✔
215
         if(!host.empty() && inet_pton(AF_INET, host.c_str(), &addrbuf) != 1 &&
10✔
216
            inet_pton(AF_INET6, host.c_str(), &addrbuf) != 1) {
5✔
217
            hostname = host;
5✔
218
         }
219

220
         m_sockfd = connect_to_host(host, port, use_tcp);
5✔
221

222
         const auto client_crt_path = get_arg_maybe("client-cert");
5✔
223
         const auto client_key_path = get_arg_maybe("client-cert-key");
5✔
224

225
         auto psk = [this]() -> std::optional<Botan::secure_vector<uint8_t>> {
10✔
226
            auto psk_hex = get_arg_maybe("psk");
5✔
227
            if(psk_hex) {
5✔
228
               return Botan::hex_decode_locked(psk_hex.value());
2✔
229
            } else {
230
               return {};
5✔
231
            }
232
         }();
10✔
233
         const std::optional<std::string> psk_identity = get_arg_maybe("psk-identity");
5✔
234
         const std::optional<std::string> psk_prf = get_arg_maybe("psk-prf");
5✔
235

236
         auto creds = std::make_shared<Basic_Credentials_Manager>(use_system_cert_store,
5✔
237
                                                                  trusted_CAs,
238
                                                                  client_crt_path,
239
                                                                  client_key_path,
240
                                                                  std::move(psk),
5✔
241
                                                                  psk_identity,
242
                                                                  psk_prf);
5✔
243

244
         Botan::TLS::Client client(callbacks,
5✔
245
                                   session_mgr,
246
                                   creds,
247
                                   policy,
248
                                   rng_as_shared(),
10✔
249
                                   Botan::TLS::Server_Information(hostname, port),
5✔
250
                                   version,
251
                                   protocols_to_offer);
20✔
252

253
         bool first_active = true;
5✔
254

255
         while(!client.is_closed()) {
24✔
256
            fd_set readfds;
257
            FD_ZERO(&readfds);
408✔
258
            FD_SET(m_sockfd, &readfds);
24✔
259

260
            if(client.is_active()) {
24✔
261
               FD_SET(STDIN_FILENO, &readfds);
15✔
262
               if(first_active && !protocols_to_offer.empty()) {
15✔
263
                  std::string app = client.application_protocol();
×
264
                  if(!app.empty()) {
×
265
                     output() << "Server choose protocol: " << client.application_protocol() << "\n";
×
266
                  }
267
                  first_active = false;
×
268
               }
×
269
            }
270

271
            struct timeval timeout = {1, 0};
24✔
272

273
            ::select(static_cast<int>(m_sockfd + 1), &readfds, nullptr, nullptr, &timeout);
24✔
274

275
            if(FD_ISSET(m_sockfd, &readfds)) {
24✔
276
               uint8_t buf[4 * 1024] = {0};
17✔
277

278
               ssize_t got = ::read(m_sockfd, buf, sizeof(buf));
17✔
279

280
               if(got == 0) {
17✔
281
                  output() << "EOF on socket\n";
×
282
                  break;
×
283
               } else if(got == -1) {
17✔
284
                  output() << "Socket error: " << errno << " " << err_to_string(errno) << "\n";
×
285
                  continue;
×
286
               }
287

288
               if(flag_set("debug")) {
17✔
289
                  output() << ">> " << Botan::hex_encode(buf, got) << "\n";
×
290
               }
291

292
               client.received_data(buf, got);
17✔
293
            }
294

295
            if(FD_ISSET(STDIN_FILENO, &readfds)) {
24✔
296
               uint8_t buf[1024] = {0};
10✔
297
               ssize_t got = read(STDIN_FILENO, buf, sizeof(buf));
10✔
298

299
               if(got == 0) {
10✔
300
                  output() << "EOF on stdin\n";
5✔
301
                  client.close();
5✔
302
                  break;
5✔
303
               } else if(got == -1) {
5✔
304
                  output() << "Stdin error: " << errno << " " << err_to_string(errno) << "\n";
×
305
                  continue;
×
306
               }
307

308
               if(got == 2 && buf[1] == '\n') {
5✔
309
                  char cmd = buf[0];
×
310

311
                  if(cmd == 'R' || cmd == 'r') {
×
312
                     output() << "Client initiated renegotiation\n";
×
313
                     client.renegotiate(cmd == 'R');
×
314
                  } else if(cmd == 'Q') {
×
315
                     output() << "Client initiated close\n";
×
316
                     client.close();
×
317
                  }
318
               } else {
319
                  client.send(buf, got);
10✔
320
               }
321
            }
322

323
            if(client.timeout_check()) {
19✔
324
               output() << "Timeout detected\n";
×
325
            }
326
         }
327

328
         ::close(m_sockfd);
5✔
329
      }
30✔
330

331
   public:
332
      using Command::flag_set;
333
      using Command::output;
334

335
      void send(std::span<const uint8_t> buf) const {
24✔
336
         while(!buf.empty()) {
48✔
337
            ssize_t sent = ::send(m_sockfd, buf.data(), buf.size(), MSG_NOSIGNAL);
24✔
338

339
            if(sent == -1) {
24✔
340
               if(errno == EINTR) {
×
341
                  sent = 0;
342
               } else {
343
                  throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
×
344
               }
345
            }
346

347
            buf = buf.subspan(sent);
24✔
348
         }
349
      }
24✔
350

351
   private:
352
      static socket_type connect_to_host(const std::string& host, uint16_t port, bool tcp) {
5✔
353
         addrinfo hints;
5✔
354
         Botan::clear_mem(&hints, 1);
5✔
355
         hints.ai_family = AF_UNSPEC;
5✔
356
         hints.ai_socktype = tcp ? SOCK_STREAM : SOCK_DGRAM;
5✔
357
         addrinfo *res, *rp = nullptr;
5✔
358

359
         if(::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res) != 0) {
5✔
360
            throw CLI_Error("getaddrinfo failed for " + host);
×
361
         }
362

363
         socket_type fd = 0;
5✔
364

365
         for(rp = res; rp != nullptr; rp = rp->ai_next) {
10✔
366
            fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
10✔
367

368
            if(fd == invalid_socket()) {
10✔
369
               continue;
×
370
            }
371

372
            if(::connect(fd, rp->ai_addr, static_cast<socklen_t>(rp->ai_addrlen)) != 0) {
10✔
373
               ::close(fd);
5✔
374
               continue;
5✔
375
            }
376

377
            break;
378
         }
379

380
         ::freeaddrinfo(res);
5✔
381

382
         if(rp == nullptr)  // no address succeeded
5✔
383
         {
384
            throw CLI_Error("connect failed");
×
385
         }
386

387
         return fd;
5✔
388
      }
389

390
      static void dgram_socket_write(int sockfd, const uint8_t buf[], size_t length) {
391
         auto r = ::send(sockfd, buf, length, MSG_NOSIGNAL);
392

393
         if(r == -1) {
394
            throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
395
         }
396
      }
397

398
      socket_type m_sockfd = invalid_socket();
399
};
400

401
namespace {
402

403
std::ostream& Callbacks::output() {
1,160✔
404
   return m_client_command.output();
1,160✔
405
}
406

407
bool Callbacks::flag_set(const std::string& flag_name) const {
29✔
408
   return m_client_command.flag_set(flag_name);
29✔
409
}
410

411
void Callbacks::send(std::span<const uint8_t> buffer) {
24✔
412
   m_client_command.send(buffer);
24✔
413
}
414

415
}  // namespace
416

417
BOTAN_REGISTER_COMMAND("tls_client", TLS_Client);
6✔
418

419
}  // namespace Botan_CLI
420

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

© 2025 Coveralls, Inc