1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*
* (C) 2014,2015 Jack Lloyd
* 2016 Matthias Gierlings
* 2017 René Korthaus, Rohde & Schwarz Cybersecurity
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "cli.h"
#if defined(BOTAN_HAS_TLS) && defined(BOTAN_TARGET_OS_HAS_SOCKETS)
#include <botan/tls_client.h>
#include <botan/tls_policy.h>
#include <botan/x509path.h>
#include <botan/ocsp.h>
#include <botan/hex.h>
#include <botan/parsing.h>
#include <fstream>
#if defined(BOTAN_HAS_TLS_SQLITE3_SESSION_MANAGER)
#include <botan/tls_session_manager_sqlite.h>
#endif
#include <string>
#include <memory>
#include "socket_utils.h"
#include "credentials.h"
namespace Botan_CLI {
class TLS_Client final : public Command, public Botan::TLS::Callbacks
{
public:
TLS_Client()
: Command("tls_client host --port=443 --print-certs --policy= "
"--tls1.0 --tls1.1 --tls1.2 "
"--session-db= --session-db-pass= --next-protocols= --type=tcp")
{
init_sockets();
}
~TLS_Client()
{
stop_sockets();
}
std::string group() const override
{
return "tls";
}
std::string description() const override
{
return "Connect to a host using TLS/DTLS";
}
void go() override
{
// TODO client cert auth
std::unique_ptr<Botan::TLS::Session_Manager> session_mgr;
const std::string sessions_db = get_arg("session-db");
if(!sessions_db.empty())
{
#if defined(BOTAN_HAS_TLS_SQLITE3_SESSION_MANAGER)
const std::string sessions_passphrase = get_arg("session-db-pass");
session_mgr.reset(new Botan::TLS::Session_Manager_SQLite(sessions_passphrase, rng(), sessions_db));
#else
error_output() << "Ignoring session DB file, sqlite not enabled\n";
#endif
}
if(!session_mgr)
{
session_mgr.reset(new Botan::TLS::Session_Manager_In_Memory(rng()));
}
std::string policy_file = get_arg("policy");
std::unique_ptr<Botan::TLS::Policy> policy;
if(policy_file.size() > 0)
{
std::ifstream policy_stream(policy_file);
if(!policy_stream.good())
{
error_output() << "Failed reading policy file\n";
return;
}
policy.reset(new Botan::TLS::Text_Policy(policy_stream));
}
if(!policy)
{
policy.reset(new Botan::TLS::Policy);
}
Basic_Credentials_Manager creds;
const std::string host = get_arg("host");
const uint16_t port = get_arg_sz("port");
const std::string transport = get_arg("type");
const std::string next_protos = get_arg("next-protocols");
if(transport != "tcp" && transport != "udp")
{
throw CLI_Usage_Error("Invalid transport type '" + transport + "' for TLS");
}
const bool use_tcp = (transport == "tcp");
const std::vector<std::string> protocols_to_offer = Botan::split_on(next_protos, ',');
m_sockfd = connect_to_host(host, port, use_tcp);
using namespace std::placeholders;
auto version = policy->latest_supported_version(!use_tcp);
if(flag_set("tls1.0"))
{
version = Botan::TLS::Protocol_Version::TLS_V10;
}
else if(flag_set("tls1.1"))
{
version = Botan::TLS::Protocol_Version::TLS_V11;
}
struct sockaddr_storage addrbuf;
std::string hostname;
if(!host.empty() &&
inet_pton(AF_INET, host.c_str(), &addrbuf) != 1 &&
inet_pton(AF_INET6, host.c_str(), &addrbuf) != 1)
{
hostname = host;
}
Botan::TLS::Client client(*this, *session_mgr, creds, *policy, rng(),
Botan::TLS::Server_Information(hostname, port),
version, protocols_to_offer);
bool first_active = true;
while(!client.is_closed())
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(m_sockfd, &readfds);
if(client.is_active())
{
FD_SET(STDIN_FILENO, &readfds);
if(first_active && !protocols_to_offer.empty())
{
std::string app = client.application_protocol();
if(app != "")
{
output() << "Server choose protocol: " << client.application_protocol() << "\n";
}
first_active = false;
}
}
struct timeval timeout = { 1, 0 };
::select(m_sockfd + 1, &readfds, nullptr, nullptr, &timeout);
if(FD_ISSET(m_sockfd, &readfds))
{
uint8_t buf[4 * 1024] = { 0 };
ssize_t got = ::read(m_sockfd, buf, sizeof(buf));
if(got == 0)
{
output() << "EOF on socket\n";
break;
}
else if(got == -1)
{
output() << "Socket error: " << errno << " " << std::strerror(errno) << "\n";
continue;
}
client.received_data(buf, got);
}
if(FD_ISSET(STDIN_FILENO, &readfds))
{
uint8_t buf[1024] = { 0 };
ssize_t got = read(STDIN_FILENO, buf, sizeof(buf));
if(got == 0)
{
output() << "EOF on stdin\n";
client.close();
break;
}
else if(got == -1)
{
output() << "Stdin error: " << errno << " " << std::strerror(errno) << "\n";
continue;
}
if(got == 2 && buf[1] == '\n')
{
char cmd = buf[0];
if(cmd == 'R' || cmd == 'r')
{
output() << "Client initiated renegotiation\n";
client.renegotiate(cmd == 'R');
}
else if(cmd == 'Q')
{
output() << "Client initiated close\n";
client.close();
}
}
else
{
client.send(buf, got);
}
}
if(client.timeout_check())
{
output() << "Timeout detected\n";
}
}
::close(m_sockfd);
}
private:
int connect_to_host(const std::string& host, uint16_t port, bool tcp)
{
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = tcp ? SOCK_STREAM : SOCK_DGRAM;
addrinfo* res, *rp = nullptr;
if(::getaddrinfo(host.c_str(), std::to_string(port).c_str(), &hints, &res) != 0)
{
throw CLI_Error("getaddrinfo failed for " + host);
}
int fd = 0;
for(rp = res; rp != nullptr; rp = rp->ai_next)
{
fd = ::socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if(fd == -1)
{
continue;
}
if(::connect(fd, rp->ai_addr, rp->ai_addrlen) != 0)
{
::close(fd);
continue;
}
break;
}
::freeaddrinfo(res);
if(rp == nullptr) // no address succeeded
{
throw CLI_Error("connect failed");
}
return fd;
}
void tls_verify_cert_chain(
const std::vector<Botan::X509_Certificate>& cert_chain,
const std::vector<std::shared_ptr<const Botan::OCSP::Response>>& ocsp,
const std::vector<Botan::Certificate_Store*>& trusted_roots,
Botan::Usage_Type usage,
const std::string& hostname,
const Botan::TLS::Policy& policy) override
{
if(cert_chain.empty())
{
throw std::invalid_argument("Certificate chain was empty");
}
Botan::Path_Validation_Restrictions restrictions(
policy.require_cert_revocation_info(),
policy.minimum_signature_strength());
auto ocsp_timeout = std::chrono::milliseconds(1000);
Botan::Path_Validation_Result result = Botan::x509_path_validate(
cert_chain,
restrictions,
trusted_roots,
hostname,
usage,
std::chrono::system_clock::now(),
ocsp_timeout,
ocsp);
output() << "Certificate validation status: " << result.result_string() << "\n";
if(result.successful_validation())
{
auto status = result.all_statuses();
if(status.size() > 0 && status[0].count(Botan::Certificate_Status_Code::OCSP_RESPONSE_GOOD))
{
output() << "Valid OCSP response for this server\n";
}
}
}
bool tls_session_established(const Botan::TLS::Session& session) override
{
output() << "Handshake complete, " << session.version().to_string()
<< " using " << session.ciphersuite().to_string() << "\n";
if(!session.session_id().empty())
{
output() << "Session ID " << Botan::hex_encode(session.session_id()) << "\n";
}
if(!session.session_ticket().empty())
{
output() << "Session ticket " << Botan::hex_encode(session.session_ticket()) << "\n";
}
if(flag_set("print-certs"))
{
const std::vector<Botan::X509_Certificate>& certs = session.peer_certs();
for(size_t i = 0; i != certs.size(); ++i)
{
output() << "Certificate " << i + 1 << "/" << certs.size() << "\n";
output() << certs[i].to_string();
output() << certs[i].PEM_encode();
}
}
return true;
}
static void dgram_socket_write(int sockfd, const uint8_t buf[], size_t length)
{
int r = ::send(sockfd, buf, length, MSG_NOSIGNAL);
if(r == -1)
{
throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
}
}
void tls_emit_data(const uint8_t buf[], size_t length) override
{
size_t offset = 0;
while(length)
{
ssize_t sent = ::send(m_sockfd, buf + offset, length, MSG_NOSIGNAL);
if(sent == -1)
{
if(errno == EINTR)
{
sent = 0;
}
else
{
throw CLI_Error("Socket write failed errno=" + std::to_string(errno));
}
}
offset += sent;
length -= sent;
}
}
void tls_alert(Botan::TLS::Alert alert) override
{
output() << "Alert: " << alert.type_string() << "\n";
}
void tls_record_received(uint64_t /*seq_no*/, const uint8_t buf[], size_t buf_size) override
{
for(size_t i = 0; i != buf_size; ++i)
{
output() << buf[i];
}
}
private:
int m_sockfd = -1;
};
BOTAN_REGISTER_COMMAND("tls_client", TLS_Client);
}
#endif
|