BOOL netconn_close( netconn_t *conn ) { int res; #ifdef SONAME_LIBSSL if (conn->secure) { heap_free( conn->peek_msg_mem ); conn->peek_msg_mem = NULL; conn->peek_msg = NULL; conn->peek_len = 0; pSSL_shutdown( conn->ssl_conn ); pSSL_free( conn->ssl_conn ); conn->ssl_conn = NULL; conn->secure = FALSE; } #endif res = closesocket( conn->socket ); conn->socket = -1; if (res == -1) { set_last_error( sock_get_error( errno ) ); return FALSE; } return TRUE; }
/****************************************************************************** * NETCON_close * Basically calls 'close()' unless we should use SSL */ BOOL NETCON_close(WININET_NETCONNECTION *connection) { int result; if (!NETCON_connected(connection)) return FALSE; #ifdef SONAME_LIBSSL if (connection->useSSL) { HeapFree(GetProcessHeap(),0,connection->peek_msg_mem); connection->peek_msg = NULL; connection->peek_msg_mem = NULL; connection->peek_len = 0; pSSL_shutdown(connection->ssl_s); pSSL_free(connection->ssl_s); connection->ssl_s = NULL; connection->useSSL = FALSE; } #endif result = closesocket(connection->socketFD); connection->socketFD = -1; if (result == -1) { INTERNET_SetLastError(sock_get_error(errno)); return FALSE; } return TRUE; }
/****************************************************************************** * NETCON_secure_connect * Initiates a secure connection over an existing plaintext connection. */ DWORD NETCON_secure_connect(netconn_t *connection) { DWORD res = ERROR_NOT_SUPPORTED; #ifdef SONAME_LIBSSL void *ssl_s; /* can't connect if we are already connected */ if (connection->ssl_s) { ERR("already connected\n"); return ERROR_INTERNET_CANNOT_CONNECT; } ssl_s = pSSL_new(ctx); if (!ssl_s) { ERR("SSL_new failed: %s\n", pERR_error_string(pERR_get_error(), 0)); return ERROR_OUTOFMEMORY; } if (!pSSL_set_fd(ssl_s, connection->socketFD)) { ERR("SSL_set_fd failed: %s\n", pERR_error_string(pERR_get_error(), 0)); res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR; goto fail; } if (!pSSL_set_ex_data(ssl_s, conn_idx, connection)) { ERR("SSL_set_ex_data failed: %s\n", pERR_error_string(pERR_get_error(), 0)); res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR; goto fail; } if (pSSL_connect(ssl_s) <= 0) { res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx); if (!res) res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR; ERR("SSL_connect failed: %d\n", res); goto fail; } connection->ssl_s = ssl_s; return ERROR_SUCCESS; fail: if (ssl_s) { pSSL_shutdown(ssl_s); pSSL_free(ssl_s); } #endif return res; }
BOOL netconn_secure_connect( netconn_t *conn ) { #ifdef SONAME_LIBSSL X509 *cert; long res; ctx = pSSL_CTX_new( method ); if (!pSSL_CTX_set_default_verify_paths( ctx )) { ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_OUTOFMEMORY ); return FALSE; } if (!(conn->ssl_conn = pSSL_new( ctx ))) { ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_OUTOFMEMORY ); goto fail; } if (!pSSL_set_fd( conn->ssl_conn, conn->socket )) { ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); goto fail; } if (pSSL_connect( conn->ssl_conn ) <= 0) { ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); goto fail; } if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) { ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); goto fail; } if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK) { /* FIXME: we should set an error and return, but we only print an error at the moment */ ERR("couldn't verify server certificate (%ld)\n", res); } TRACE("established SSL connection\n"); conn->secure = TRUE; return TRUE; fail: if (conn->ssl_conn) { pSSL_shutdown( conn->ssl_conn ); pSSL_free( conn->ssl_conn ); conn->ssl_conn = NULL; } #endif return FALSE; }
BOOL netconn_secure_connect( netconn_t *conn, WCHAR *hostname ) { #ifdef SONAME_LIBSSL if (!(conn->ssl_conn = pSSL_new( ctx ))) { ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_OUTOFMEMORY ); goto fail; } if (!pSSL_set_ex_data( conn->ssl_conn, hostname_idx, hostname )) { ERR("SSL_set_ex_data failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); goto fail; } if (!pSSL_set_ex_data( conn->ssl_conn, conn_idx, conn )) { ERR("SSL_set_ex_data failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); return FALSE; } if (!pSSL_set_fd( conn->ssl_conn, conn->socket )) { ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 )); set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); goto fail; } if (pSSL_connect( conn->ssl_conn ) <= 0) { DWORD err; err = (DWORD_PTR)pSSL_get_ex_data( conn->ssl_conn, error_idx ); if (!err) err = ERROR_WINHTTP_SECURE_CHANNEL_ERROR; ERR("couldn't verify server certificate (%d)\n", err); set_last_error( err ); goto fail; } TRACE("established SSL connection\n"); conn->secure = TRUE; return TRUE; fail: if (conn->ssl_conn) { pSSL_shutdown( conn->ssl_conn ); pSSL_free( conn->ssl_conn ); conn->ssl_conn = NULL; } #endif return FALSE; }
void free_netconn(netconn_t *netconn) { server_release(netconn->server); #ifdef SONAME_LIBSSL if (netconn->ssl_s) { pSSL_shutdown(netconn->ssl_s); pSSL_free(netconn->ssl_s); } #endif closesocket(netconn->socketFD); heap_free(netconn); }
/****************************************************************************** * NETCON_secure_connect * Initiates a secure connection over an existing plaintext connection. */ BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname) { #ifdef SONAME_LIBSSL long verify_res; X509 *cert; int len; char *hostname_unix; /* can't connect if we are already connected */ if (connection->useSSL) { ERR("already connected\n"); return FALSE; } ctx = pSSL_CTX_new(meth); if (!pSSL_CTX_set_default_verify_paths(ctx)) { ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string(pERR_get_error(), 0)); INTERNET_SetLastError(ERROR_OUTOFMEMORY); return FALSE; } connection->ssl_s = pSSL_new(ctx); if (!connection->ssl_s) { ERR("SSL_new failed: %s\n", pERR_error_string(pERR_get_error(), 0)); INTERNET_SetLastError(ERROR_OUTOFMEMORY); goto fail; } if (!pSSL_set_fd(connection->ssl_s, connection->socketFD)) { ERR("SSL_set_fd failed: %s\n", pERR_error_string(pERR_get_error(), 0)); INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); goto fail; } if (pSSL_connect(connection->ssl_s) <= 0) { ERR("SSL_connect failed: %s\n", pERR_error_string(pERR_get_error(), 0)); INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); goto fail; } cert = pSSL_get_peer_certificate(connection->ssl_s); if (!cert) { ERR("no certificate for server %s\n", debugstr_w(hostname)); /* FIXME: is this the best error? */ INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA); goto fail; } verify_res = pSSL_get_verify_result(connection->ssl_s); if (verify_res != X509_V_OK) { ERR("couldn't verify the security of the connection, %ld\n", verify_res); /* FIXME: we should set an error and return, but we only warn at * the moment */ } len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL); hostname_unix = HeapAlloc(GetProcessHeap(), 0, len); if (!hostname_unix) { INTERNET_SetLastError(ERROR_OUTOFMEMORY); goto fail; } WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL); if (!check_hostname(cert, hostname_unix)) { HeapFree(GetProcessHeap(), 0, hostname_unix); INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID); goto fail; } HeapFree(GetProcessHeap(), 0, hostname_unix); connection->useSSL = TRUE; return TRUE; fail: if (connection->ssl_s) { pSSL_shutdown(connection->ssl_s); pSSL_free(connection->ssl_s); connection->ssl_s = NULL; } #endif return FALSE; }