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 147 of file httplowlevelclient.cpp.

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