SNode.C
Loading...
Searching...
No Matches
tls Namespace Reference

Typedefs

using SocketClient = net::in::stream::tls::SocketClient<apps::http::SimpleSocketProtocolFactory>
 
using SocketAddress = SocketClient::SocketAddress
 
using SocketConnection = SocketClient::SocketConnection
 

Functions

SocketClient getClient ()
 

Typedef Documentation

◆ SocketAddress

◆ SocketClient

◆ SocketConnection

Function Documentation

◆ getClient()

SocketClient tls::getClient ( )

Definition at line 126 of file httplowlevelclient.cpp.

126 {
127 SocketClient tlsClient(
128 "tls",
129 [](SocketConnection* socketConnection) { // onConnect
130 VLOG(1) << "OnConnect";
131
132 VLOG(1) << "\tServer: " << socketConnection->getRemoteAddress().toString();
133 VLOG(1) << "\tClient: " << socketConnection->getLocalAddress().toString();
134
135 /* Enable automatic hostname checks */
136 // X509_VERIFY_PARAM* param = SSL_get0_param(socketConnection->getSSL());
137
138 // X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
139 // if (!X509_VERIFY_PARAM_set1_host(param, "localhost", sizeof("localhost") - 1)) {
140 // // handle error
141 // socketConnection->close();
142 // }
143 },
144 [](SocketConnection* socketConnection) { // onConnected
145 VLOG(1) << "OnConnected";
146
147 X509* server_cert = SSL_get_peer_certificate(socketConnection->getSSL());
148 if (server_cert != nullptr) {
149 const long verifyErr = SSL_get_verify_result(socketConnection->getSSL());
150
151 VLOG(1) << " Server certificate: " + std::string(X509_verify_cert_error_string(verifyErr));
152
153 char* str = X509_NAME_oneline(X509_get_subject_name(server_cert), nullptr, 0);
154 VLOG(1) << " Subject: " + std::string(str);
155 OPENSSL_free(str);
156
157 str = X509_NAME_oneline(X509_get_issuer_name(server_cert), nullptr, 0);
158 VLOG(1) << " Issuer: " + std::string(str);
159 OPENSSL_free(str);
160
161 // We could do all sorts of certificate verification stuff here before deallocating the certificate.
162
163 GENERAL_NAMES* subjectAltNames =
164 static_cast<GENERAL_NAMES*>(X509_get_ext_d2i(server_cert, NID_subject_alt_name, nullptr, nullptr));
165#ifdef __GNUC__
166#pragma GCC diagnostic push
167#ifdef __has_warning
168#if __has_warning("-Wused-but-marked-unused")
169#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
170#endif
171#endif
172#endif
173 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
174#ifdef __GNUC_
175#pragma GCC diagnostic pop
176#endif
177 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
178 for (int32_t i = 0; i < altNameCount; ++i) {
179#ifdef __GNUC__
180#pragma GCC diagnostic push
181#ifdef __has_warning
182#if __has_warning("-Wused-but-marked-unused")
183#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
184#endif
185#endif
186#endif
187 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
188#ifdef __GNUC_
189#pragma GCC diagnostic pop
190#endif
191 if (generalName->type == GEN_URI) {
192 const std::string subjectAltName =
193 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
194 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
195 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
196 } else if (generalName->type == GEN_DNS) {
197 const std::string subjectAltName =
198 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
199 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
200 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
201 } else {
202 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
203 }
204 }
205#ifdef __GNUC__
206#pragma GCC diagnostic push
207#ifdef __has_warning
208#if __has_warning("-Wused-but-marked-unused")
209#pragma GCC diagnostic ignored "-Wused-but-marked-unused"
210#endif
211#endif
212#endif
213 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
214#ifdef __GNUC_
215#pragma GCC diagnostic pop
216#endif
217 X509_free(server_cert);
218 } else {
219 VLOG(1) << " Server certificate: no certificate";
220 }
221
222 socketConnection->sendToPeer("GET /index.html HTTP/1.1\r\nConnection: close\r\n\r\n"); // Connection: close\r\n\r\n");
223 },
224 [](SocketConnection* socketConnection) { // onDisconnect
225 VLOG(1) << "OnDisconnect";
226
227 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
228 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
229
230 });
231
232 const SocketAddress remoteAddress("localhost", 8088);
233
234 tlsClient.connect(remoteAddress,
235 [instanceName = tlsClient.getConfig().getInstanceName()](
236 const SocketAddress& socketAddress,
237 const core::socket::State& state) { // example.com:81 simulate connnect timeout
238 switch (state) {
239 case core::socket::State::OK:
240 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
241 break;
242 case core::socket::State::DISABLED:
243 VLOG(1) << instanceName << ": disabled";
244 break;
245 case core::socket::State::ERROR:
246 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
247 break;
248 case core::socket::State::FATAL:
249 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
250 break;
251 }
252 });
253 return tlsClient;
254 }
Client::SocketConnection SocketConnection
Definition clients.h:66
SocketClient::SocketConnection SocketConnection
SocketClient::SocketAddress SocketAddress