148 {
150 "tls",
152 VLOG(1) << "OnConnect";
153
154 VLOG(1) << "\tServer: " << socketConnection->getRemoteAddress().toString();
155 VLOG(1) << "\tClient: " << socketConnection->getLocalAddress().toString();
156
157
158
159
160
161
162
163
164
165 },
166 [](SocketConnection* socketConnection) {
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
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");
245 },
247 VLOG(1) << "OnDisconnect";
248
249 VLOG(1) << "\tServer: " + socketConnection->getRemoteAddress().toString();
250 VLOG(1) << "\tClient: " + socketConnection->getLocalAddress().toString();
251
252 });
253
255
256 tlsClient.connect(remoteAddress,
257 [instanceName = tlsClient.getConfig().getInstanceName()](
258 const SocketAddress& socketAddress,
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
SocketClient::SocketConnection SocketConnection
SocketClient::SocketAddress SocketAddress