This repository was archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection_info_libtls.cpp
More file actions
135 lines (106 loc) · 3.46 KB
/
Copy pathconnection_info_libtls.cpp
File metadata and controls
135 lines (106 loc) · 3.46 KB
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
/**
* Copyright (C) 2020 Tristan. All Rights Reserved.
* This file is licensed under the BSD 2-Clause license.
* See the COPYING file for licensing information.
*/
#include "connection_info.hpp"
#include <chrono>
#include <iostream>
#include <sstream>
#include <vector>
#include <tls.h>
#include "logger.hpp"
namespace Net {
void
ConnectionInfo::TLSDestroy() {
Logger::Debug(static_cast<const char *>(__PRETTY_FUNCTION__), "Called");
tls_close(static_cast<struct tls *>(tlsContext));
tls_free(static_cast<struct tls *>(tlsContext));
tlsContext = nullptr;
}
bool
ConnectionInfo::TLSRead(char *buf, std::size_t len) {
do {
auto ret = tls_read(static_cast<struct tls *>(tlsContext), buf, len);
if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
continue;
if (ret == -1)
return false;
buf += ret;
len -= ret;
} while (len > 0);
return true;
}
std::optional<char>
ConnectionInfo::TLSReadChar() {
char character;
if (tls_read(static_cast<struct tls *>(tlsContext), &character, 1) == -1)
return std::optional<char>();
return std::optional<char>(character);
}
bool
ConnectionInfo::TLSSetup() {
struct tls_config *config = tls_config_new();
if (config == nullptr) {
Logger::Severe("ConnectionInfo::TLSSetup[libtls]", "Failed to create configuration! (Out of memory!)");
return false;
}
if (tls_config_set_alpn(config, tlsALPNProtocols.c_str()) == -1) {
Logger::Severe(
"ConnectionInfo::TLSSetup[libtls]", std::string("Failed to set ALPN: ") + tls_config_error(config));
tls_config_free(config);
return false;
}
struct tls *context = tls_client();
if (context == nullptr) {
Logger::Severe("ConnectionInfo::TLSSetup[libtls]", "Failed to create tls_client (Out of memory!)");
tls_config_free(config);
return false;
}
if (tls_configure(context, config) == -1) {
Logger::Severe("ConnectionInfo::TLSSetup[libtls]",
std::string("Failed to configure TLS context: ") + tls_error(context));
tls_free(context);
tls_config_free(config);
return false;
}
tls_config_free(config);
config = nullptr;
if (tls_connect_socket(context, socket, hostName.c_str()) == -1) {
Logger::Warning("ConnectionInfo::TLSSetup[libtls]",
std::string("Failed to setup secure connection with ") + hostName + ": " + tls_error(context));
tls_free(context);
return false;
}
if (tls_handshake(context) == -1) {
Logger::Warning(
"ConnectionInfo::TLSSetup[libtls]", std::string("Failed to setup secure connection (handshake) with ")
+ hostName + ": " + tls_error(context));
tls_free(context);
return false;
}
std::cout << "TLS Information:"
<< "\nTLSVersion=" << tls_conn_version(context) << "\nCipher=" << tls_conn_cipher(context)
<< "\nCipherStrength=" << tls_conn_cipher_strength(context)
<< "\nServerName=" << tls_conn_servername(context)
<< "\nCertificateIssuer=" << tls_peer_cert_issuer(context)
<< "\nCertificateSubject=" << tls_peer_cert_subject(context)
<< "\nCertificateHash=" << tls_peer_cert_hash(context) << std::endl;
tlsContext = context;
isAuthenticated = true;
return true;
}
bool
ConnectionInfo::TLSWrite(const char *buf, std::size_t len) {
do {
auto ret = tls_write(static_cast<struct tls *>(tlsContext), buf, len);
if (ret == TLS_WANT_POLLIN || ret == TLS_WANT_POLLOUT)
continue;
if (ret == -1)
return false;
buf += ret;
len -= ret;
} while (len > 0);
return true;
}
} // namespace Net