| 290 | } |
| 291 | |
| 292 | Result New(dmSocket::Socket socket, const char* host, uint64_t timeout, Socket* sslsocket) |
| 293 | { |
| 294 | uint64_t handshakestart = dmTime::GetMonotonicTime(); |
| 295 | |
| 296 | Socket c = (Socket)malloc(sizeof(SSLSocket)); |
| 297 | memset(c, 0, sizeof(SSLSocket)); |
| 298 | |
| 299 | #define MBED_CALLOC(_TYPE) (_TYPE*)calloc(1, sizeof(_TYPE)) |
| 300 | c->m_MbedConf = MBED_CALLOC(mbedtls_ssl_config); |
| 301 | c->m_SSLContext = MBED_CALLOC(mbedtls_ssl_context); |
| 302 | c->m_SSLNetContext = MBED_CALLOC(CustomNetContext); |
| 303 | #undef MBED_CALLOC |
| 304 | |
| 305 | mbedtls_ssl_config_init(c->m_MbedConf); |
| 306 | |
| 307 | #if defined(MBEDTLS_DEBUG_C) |
| 308 | mbedtls_ssl_conf_dbg(c->m_MbedConf, mbedtls_debug, 0); |
| 309 | #endif |
| 310 | |
| 311 | int ret = 0; |
| 312 | if ((ret = mbedtls_ssl_config_defaults(c->m_MbedConf, |
| 313 | MBEDTLS_SSL_IS_CLIENT, |
| 314 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 315 | MBEDTLS_SSL_PRESET_DEFAULT)) != 0) |
| 316 | { |
| 317 | SSL_LOGE("mbedtls_ssl_config_defaults failed", ret); |
| 318 | Delete(c); |
| 319 | return RESULT_SSL_INIT_FAILED; |
| 320 | } |
| 321 | |
| 322 | mbedtls_ssl_conf_authmode(c->m_MbedConf, MBEDTLS_SSL_VERIFY_NONE); |
| 323 | |
| 324 | dmSocket::SetSendTimeout(socket, (int)timeout); |
| 325 | dmSocket::SetReceiveTimeout(socket, (int)timeout); |
| 326 | |
| 327 | #if defined(MBEDTLS_SSL_PROTO_DTLS) |
| 328 | if (timeout != 0) |
| 329 | { |
| 330 | int mbed_ssl_timeout = dmMath::Max((int)timeout, dmSocket::SOCKET_TIMEOUT) / 1000; |
| 331 | mbed_ssl_timeout = dmMath::Max(1, mbed_ssl_timeout); |
| 332 | mbedtls_ssl_conf_handshake_timeout(c->m_MbedConf, 1, mbed_ssl_timeout); |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | c->m_SSLNetContext->m_Timeout = timeout; |
| 337 | mbedtls_ssl_init(c->m_SSLContext); |
| 338 | |
| 339 | bool verify_certificate = g_MbedTlsContext.m_x509CertChain != 0; |
| 340 | ConfigureCertificateChain(c->m_MbedConf); |
| 341 | |
| 342 | if ((ret = mbedtls_ssl_setup(c->m_SSLContext, c->m_MbedConf)) != 0) |
| 343 | { |
| 344 | SSL_LOGE("mbedtls_ssl_setup failed", ret); |
| 345 | Delete(c); |
| 346 | return RESULT_HANDSHAKE_FAILED; |
| 347 | } |
| 348 | |
| 349 | if ((ret = mbedtls_ssl_set_hostname(c->m_SSLContext, host)) != 0) |
no test coverage detected