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

Definition at line 145 of file httplowlevelclient.cpp.

◆ SocketClient

◆ SocketConnection

Function Documentation

◆ getClient()

SocketClient tls::getClient ( )
private

Definition at line 148 of file httplowlevelclient.cpp.

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

References core::socket::stream::SocketClient< SocketConnectorT, SocketContextFactoryT, Args >::connect(), core::socket::State::DISABLED, core::socket::State::ERROR, core::socket::State::FATAL, core::socket::Socket< ConfigT >::getConfig(), net::config::ConfigInstance::getInstanceName(), core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::getLocalAddress(), core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT >::getRemoteAddress(), core::socket::stream::tls::SocketConnection< PhysicalSocketT >::getSSL(), core::socket::State::OK, core::socket::stream::SocketConnection::sendToPeer(), net::in::SocketAddress::SocketAddress(), net::in::SocketAddress::toString(), and core::socket::State::what().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: