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 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
188 const int32_t altNameCount = sk_GENERAL_NAME_num(subjectAltNames);
189
190 VLOG(1) << "\t Subject alternative name count: " << altNameCount;
191 for (int32_t i = 0; i < altNameCount; ++i) {
192 GENERAL_NAME* generalName = sk_GENERAL_NAME_value(subjectAltNames, i);
193 if (generalName->type == GEN_URI) {
194 const std::string subjectAltName =
195 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.uniformResourceIdentifier)),
196 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.uniformResourceIdentifier)));
197 VLOG(1) << "\t SAN (URI): '" + subjectAltName;
198 } else if (generalName->type == GEN_DNS) {
199 const std::string subjectAltName =
200 std::string(reinterpret_cast<const char*>(ASN1_STRING_get0_data(generalName->d.dNSName)),
201 static_cast<std::size_t>(ASN1_STRING_length(generalName->d.dNSName)));
202 VLOG(1) << "\t SAN (DNS): '" + subjectAltName;
203 } else {
204 VLOG(1) << "\t SAN (Type): '" + std::to_string(generalName->type);
205 }
206 }
207
208 sk_GENERAL_NAME_pop_free(subjectAltNames, GENERAL_NAME_free);
209
210 X509_free(server_cert);
211 } else {
212 VLOG(1) << " Server certificate: no certificate";
213 }
214
215 socketConnection->sendToPeer("GET /index.html HTTP/1.1\r\nConnection: close\r\n\r\n"); // Connection: close\r\n\r\n");
216 },
217 [](SocketConnection* socketConnection) { // onDisconnect
218 VLOG(1) << "OnDisconnect";
219
220 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
221 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
222
223 });
224
225 const SocketAddress remoteAddress("localhost", 8088);
226
227 tlsClient.connect(remoteAddress,
228 [instanceName = tlsClient.getConfig().getInstanceName()](
229 const SocketAddress& socketAddress,
230 const core::socket::State& state) { // example.com:81 simulate connnect timeout
231 switch (state) {
232 case core::socket::State::OK:
233 VLOG(1) << instanceName << ": connected to '" << socketAddress.toString() << "'";
234 break;
235 case core::socket::State::DISABLED:
236 VLOG(1) << instanceName << ": disabled";
237 break;
238 case core::socket::State::ERROR:
239 LOG(ERROR) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
240 break;
241 case core::socket::State::FATAL:
242 LOG(FATAL) << instanceName << ": " << socketAddress.toString() << ": " << state.what();
243 break;
244 }
245 });
246 return tlsClient;
247 }
net::in::stream::tls::SocketClient< apps::http::SimpleSocketProtocolFactory > SocketClient
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, ConfigT >::getLocalAddress(), core::socket::stream::SocketConnectionT< PhysicalSocketT, SocketReaderT, SocketWriterT, ConfigT >::getRemoteAddress(), core::socket::stream::tls::SocketConnection< PhysicalSocketT, ConfigT >::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: