polarssl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  9. * Copyright (C) 2012 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at http://curl.haxx.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. /*
  24. * Source file for all PolarSSL-specific code for the TLS/SSL layer. No code
  25. * but vtls.c should ever call or use these functions.
  26. *
  27. */
  28. #include "curl_setup.h"
  29. #ifdef USE_POLARSSL
  30. #include <polarssl/net.h>
  31. #include <polarssl/ssl.h>
  32. #include <polarssl/certs.h>
  33. #include <polarssl/x509.h>
  34. #include <polarssl/version.h>
  35. #if POLARSSL_VERSION_NUMBER < 0x01030000
  36. #error too old PolarSSL
  37. #endif
  38. #include <polarssl/error.h>
  39. #include <polarssl/entropy.h>
  40. #include <polarssl/ctr_drbg.h>
  41. #include "urldata.h"
  42. #include "sendf.h"
  43. #include "inet_pton.h"
  44. #include "polarssl.h"
  45. #include "vtls.h"
  46. #include "parsedate.h"
  47. #include "connect.h" /* for the connect timeout */
  48. #include "select.h"
  49. #include "rawstr.h"
  50. #include "polarssl_threadlock.h"
  51. #define _MPRINTF_REPLACE /* use our functions only */
  52. #include <curl/mprintf.h>
  53. #include "curl_memory.h"
  54. /* The last #include file should be: */
  55. #include "memdebug.h"
  56. /* apply threading? */
  57. #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
  58. #define THREADING_SUPPORT
  59. #endif
  60. #if defined(THREADING_SUPPORT)
  61. static entropy_context entropy;
  62. static int entropy_init_initialized = 0;
  63. /* start of entropy_init_mutex() */
  64. static void entropy_init_mutex(entropy_context *ctx)
  65. {
  66. /* lock 0 = entropy_init_mutex() */
  67. polarsslthreadlock_lock_function(0);
  68. if(entropy_init_initialized == 0) {
  69. entropy_init(ctx);
  70. entropy_init_initialized = 1;
  71. }
  72. polarsslthreadlock_unlock_function(0);
  73. }
  74. /* end of entropy_init_mutex() */
  75. /* start of entropy_func_mutex() */
  76. static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
  77. {
  78. int ret;
  79. /* lock 1 = entropy_func_mutex() */
  80. polarsslthreadlock_lock_function(1);
  81. ret = entropy_func(data, output, len);
  82. polarsslthreadlock_unlock_function(1);
  83. return ret;
  84. }
  85. /* end of entropy_func_mutex() */
  86. #endif /* THREADING_SUPPORT */
  87. /* Define this to enable lots of debugging for PolarSSL */
  88. #undef POLARSSL_DEBUG
  89. #ifdef POLARSSL_DEBUG
  90. static void polarssl_debug(void *context, int level, const char *line)
  91. {
  92. struct SessionHandle *data = NULL;
  93. if(!context)
  94. return;
  95. data = (struct SessionHandle *)context;
  96. infof(data, "%s", line);
  97. (void) level;
  98. }
  99. #else
  100. #endif
  101. /* ALPN for http2? */
  102. #ifdef USE_NGHTTP2
  103. # undef HAS_ALPN
  104. # ifdef POLARSSL_SSL_ALPN
  105. # define HAS_ALPN
  106. # endif
  107. #endif
  108. static Curl_recv polarssl_recv;
  109. static Curl_send polarssl_send;
  110. static CURLcode
  111. polarssl_connect_step1(struct connectdata *conn,
  112. int sockindex)
  113. {
  114. struct SessionHandle *data = conn->data;
  115. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  116. bool sni = TRUE; /* default is SNI enabled */
  117. int ret = -1;
  118. #ifdef ENABLE_IPV6
  119. struct in6_addr addr;
  120. #else
  121. struct in_addr addr;
  122. #endif
  123. void *old_session = NULL;
  124. size_t old_session_size = 0;
  125. char errorbuf[128];
  126. memset(errorbuf, 0, sizeof(errorbuf));
  127. /* PolarSSL only supports SSLv3 and TLSv1 */
  128. if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
  129. failf(data, "PolarSSL does not support SSLv2");
  130. return CURLE_SSL_CONNECT_ERROR;
  131. }
  132. else if(data->set.ssl.version == CURL_SSLVERSION_SSLv3)
  133. sni = FALSE; /* SSLv3 has no SNI */
  134. #ifdef THREADING_SUPPORT
  135. entropy_init_mutex(&entropy);
  136. if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func_mutex, &entropy,
  137. connssl->ssn.id, connssl->ssn.length)) != 0) {
  138. #ifdef POLARSSL_ERROR_C
  139. error_strerror(ret, errorbuf, sizeof(errorbuf));
  140. #endif /* POLARSSL_ERROR_C */
  141. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  142. -ret, errorbuf);
  143. }
  144. #else
  145. entropy_init(&connssl->entropy);
  146. if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func, &connssl->entropy,
  147. connssl->ssn.id, connssl->ssn.length)) != 0) {
  148. #ifdef POLARSSL_ERROR_C
  149. error_strerror(ret, errorbuf, sizeof(errorbuf));
  150. #endif /* POLARSSL_ERROR_C */
  151. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  152. -ret, errorbuf);
  153. }
  154. #endif /* THREADING_SUPPORT */
  155. /* Load the trusted CA */
  156. memset(&connssl->cacert, 0, sizeof(x509_crt));
  157. if(data->set.str[STRING_SSL_CAFILE]) {
  158. ret = x509_crt_parse_file(&connssl->cacert,
  159. data->set.str[STRING_SSL_CAFILE]);
  160. if(ret<0) {
  161. #ifdef POLARSSL_ERROR_C
  162. error_strerror(ret, errorbuf, sizeof(errorbuf));
  163. #endif /* POLARSSL_ERROR_C */
  164. failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s",
  165. data->set.str[STRING_SSL_CAFILE], -ret, errorbuf);
  166. if(data->set.ssl.verifypeer)
  167. return CURLE_SSL_CACERT_BADFILE;
  168. }
  169. }
  170. /* Load the client certificate */
  171. memset(&connssl->clicert, 0, sizeof(x509_crt));
  172. if(data->set.str[STRING_CERT]) {
  173. ret = x509_crt_parse_file(&connssl->clicert,
  174. data->set.str[STRING_CERT]);
  175. if(ret) {
  176. #ifdef POLARSSL_ERROR_C
  177. error_strerror(ret, errorbuf, sizeof(errorbuf));
  178. #endif /* POLARSSL_ERROR_C */
  179. failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
  180. data->set.str[STRING_CERT], -ret, errorbuf);
  181. return CURLE_SSL_CERTPROBLEM;
  182. }
  183. }
  184. /* Load the client private key */
  185. if(data->set.str[STRING_KEY]) {
  186. pk_context pk;
  187. pk_init(&pk);
  188. ret = pk_parse_keyfile(&pk, data->set.str[STRING_KEY],
  189. data->set.str[STRING_KEY_PASSWD]);
  190. if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA))
  191. ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
  192. if(ret == 0)
  193. rsa_copy(&connssl->rsa, pk_rsa(pk));
  194. else
  195. rsa_free(&connssl->rsa);
  196. pk_free(&pk);
  197. if(ret) {
  198. #ifdef POLARSSL_ERROR_C
  199. error_strerror(ret, errorbuf, sizeof(errorbuf));
  200. #endif /* POLARSSL_ERROR_C */
  201. failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
  202. data->set.str[STRING_KEY], -ret, errorbuf);
  203. return CURLE_SSL_CERTPROBLEM;
  204. }
  205. }
  206. /* Load the CRL */
  207. memset(&connssl->crl, 0, sizeof(x509_crl));
  208. if(data->set.str[STRING_SSL_CRLFILE]) {
  209. ret = x509_crl_parse_file(&connssl->crl,
  210. data->set.str[STRING_SSL_CRLFILE]);
  211. if(ret) {
  212. #ifdef POLARSSL_ERROR_C
  213. error_strerror(ret, errorbuf, sizeof(errorbuf));
  214. #endif /* POLARSSL_ERROR_C */
  215. failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
  216. data->set.str[STRING_SSL_CRLFILE], -ret, errorbuf);
  217. return CURLE_SSL_CRL_BADFILE;
  218. }
  219. }
  220. infof(data, "PolarSSL: Connecting to %s:%d\n",
  221. conn->host.name, conn->remote_port);
  222. if(ssl_init(&connssl->ssl)) {
  223. failf(data, "PolarSSL: ssl_init failed");
  224. return CURLE_SSL_CONNECT_ERROR;
  225. }
  226. ssl_set_endpoint(&connssl->ssl, SSL_IS_CLIENT);
  227. ssl_set_authmode(&connssl->ssl, SSL_VERIFY_OPTIONAL);
  228. ssl_set_rng(&connssl->ssl, ctr_drbg_random,
  229. &connssl->ctr_drbg);
  230. ssl_set_bio(&connssl->ssl,
  231. net_recv, &conn->sock[sockindex],
  232. net_send, &conn->sock[sockindex]);
  233. ssl_set_ciphersuites(&connssl->ssl, ssl_list_ciphersuites());
  234. if(!Curl_ssl_getsessionid(conn, &old_session, &old_session_size)) {
  235. memcpy(&connssl->ssn, old_session, old_session_size);
  236. infof(data, "PolarSSL re-using session\n");
  237. }
  238. ssl_set_session(&connssl->ssl,
  239. &connssl->ssn);
  240. ssl_set_ca_chain(&connssl->ssl,
  241. &connssl->cacert,
  242. &connssl->crl,
  243. conn->host.name);
  244. ssl_set_own_cert_rsa(&connssl->ssl,
  245. &connssl->clicert, &connssl->rsa);
  246. if(!Curl_inet_pton(AF_INET, conn->host.name, &addr) &&
  247. #ifdef ENABLE_IPV6
  248. !Curl_inet_pton(AF_INET6, conn->host.name, &addr) &&
  249. #endif
  250. sni && ssl_set_hostname(&connssl->ssl, conn->host.name)) {
  251. infof(data, "WARNING: failed to configure "
  252. "server name indication (SNI) TLS extension\n");
  253. }
  254. #ifdef HAS_ALPN
  255. if(data->set.httpversion == CURL_HTTP_VERSION_2_0) {
  256. if(data->set.ssl_enable_alpn) {
  257. static const char* protocols[] = {
  258. NGHTTP2_PROTO_VERSION_ID, ALPN_HTTP_1_1, NULL
  259. };
  260. ssl_set_alpn_protocols(&connssl->ssl, protocols);
  261. infof(data, "ALPN, offering %s, %s\n", protocols[0],
  262. protocols[1]);
  263. }
  264. }
  265. #endif
  266. #ifdef POLARSSL_DEBUG
  267. ssl_set_dbg(&connssl->ssl, polarssl_debug, data);
  268. #endif
  269. connssl->connecting_state = ssl_connect_2;
  270. return CURLE_OK;
  271. }
  272. static CURLcode
  273. polarssl_connect_step2(struct connectdata *conn,
  274. int sockindex)
  275. {
  276. int ret;
  277. struct SessionHandle *data = conn->data;
  278. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  279. char buffer[1024];
  280. #ifdef HAS_ALPN
  281. const char* next_protocol;
  282. #endif
  283. char errorbuf[128];
  284. memset(errorbuf, 0, sizeof(errorbuf));
  285. conn->recv[sockindex] = polarssl_recv;
  286. conn->send[sockindex] = polarssl_send;
  287. for(;;) {
  288. if(!(ret = ssl_handshake(&connssl->ssl)))
  289. break;
  290. else if(ret != POLARSSL_ERR_NET_WANT_READ &&
  291. ret != POLARSSL_ERR_NET_WANT_WRITE) {
  292. #ifdef POLARSSL_ERROR_C
  293. error_strerror(ret, errorbuf, sizeof(errorbuf));
  294. #endif /* POLARSSL_ERROR_C */
  295. failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
  296. -ret, errorbuf);
  297. return CURLE_SSL_CONNECT_ERROR;
  298. }
  299. else {
  300. if(ret == POLARSSL_ERR_NET_WANT_READ) {
  301. connssl->connecting_state = ssl_connect_2_reading;
  302. return CURLE_OK;
  303. }
  304. if(ret == POLARSSL_ERR_NET_WANT_WRITE) {
  305. connssl->connecting_state = ssl_connect_2_writing;
  306. return CURLE_OK;
  307. }
  308. failf(data, "SSL_connect failed with error %d.", ret);
  309. return CURLE_SSL_CONNECT_ERROR;
  310. }
  311. }
  312. infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
  313. ssl_get_ciphersuite(&conn->ssl[sockindex].ssl)
  314. );
  315. ret = ssl_get_verify_result(&conn->ssl[sockindex].ssl);
  316. if(ret && data->set.ssl.verifypeer) {
  317. if(ret & BADCERT_EXPIRED)
  318. failf(data, "Cert verify failed: BADCERT_EXPIRED");
  319. if(ret & BADCERT_REVOKED) {
  320. failf(data, "Cert verify failed: BADCERT_REVOKED");
  321. return CURLE_SSL_CACERT;
  322. }
  323. if(ret & BADCERT_CN_MISMATCH)
  324. failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
  325. if(ret & BADCERT_NOT_TRUSTED)
  326. failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
  327. return CURLE_PEER_FAILED_VERIFICATION;
  328. }
  329. if(ssl_get_peer_cert(&(connssl->ssl))) {
  330. /* If the session was resumed, there will be no peer certs */
  331. memset(buffer, 0, sizeof(buffer));
  332. if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ",
  333. ssl_get_peer_cert(&(connssl->ssl))) != -1)
  334. infof(data, "Dumping cert info:\n%s\n", buffer);
  335. }
  336. #ifdef HAS_ALPN
  337. if(data->set.ssl_enable_alpn) {
  338. next_protocol = ssl_get_alpn_protocol(&connssl->ssl);
  339. if(next_protocol != NULL) {
  340. infof(data, "ALPN, server accepted to use %s\n", next_protocol);
  341. if(strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
  342. NGHTTP2_PROTO_VERSION_ID_LEN)) {
  343. conn->negnpn = NPN_HTTP2;
  344. }
  345. else if(strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) {
  346. conn->negnpn = NPN_HTTP1_1;
  347. }
  348. }
  349. else {
  350. infof(data, "ALPN, server did not agree to a protocol\n");
  351. }
  352. }
  353. #endif
  354. connssl->connecting_state = ssl_connect_3;
  355. infof(data, "SSL connected\n");
  356. return CURLE_OK;
  357. }
  358. static CURLcode
  359. polarssl_connect_step3(struct connectdata *conn,
  360. int sockindex)
  361. {
  362. CURLcode retcode = CURLE_OK;
  363. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  364. struct SessionHandle *data = conn->data;
  365. void *old_ssl_sessionid = NULL;
  366. ssl_session *our_ssl_sessionid = &conn->ssl[sockindex].ssn ;
  367. int incache;
  368. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  369. /* Save the current session data for possible re-use */
  370. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
  371. if(incache) {
  372. if(old_ssl_sessionid != our_ssl_sessionid) {
  373. infof(data, "old SSL session ID is stale, removing\n");
  374. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  375. incache = FALSE;
  376. }
  377. }
  378. if(!incache) {
  379. void *new_session = malloc(sizeof(ssl_session));
  380. if(new_session) {
  381. memcpy(new_session, our_ssl_sessionid,
  382. sizeof(ssl_session));
  383. retcode = Curl_ssl_addsessionid(conn, new_session,
  384. sizeof(ssl_session));
  385. }
  386. else {
  387. retcode = CURLE_OUT_OF_MEMORY;
  388. }
  389. if(retcode) {
  390. failf(data, "failed to store ssl session");
  391. return retcode;
  392. }
  393. }
  394. connssl->connecting_state = ssl_connect_done;
  395. return CURLE_OK;
  396. }
  397. static ssize_t polarssl_send(struct connectdata *conn,
  398. int sockindex,
  399. const void *mem,
  400. size_t len,
  401. CURLcode *curlcode)
  402. {
  403. int ret = -1;
  404. ret = ssl_write(&conn->ssl[sockindex].ssl,
  405. (unsigned char *)mem, len);
  406. if(ret < 0) {
  407. *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
  408. CURLE_AGAIN : CURLE_SEND_ERROR;
  409. ret = -1;
  410. }
  411. return ret;
  412. }
  413. void Curl_polarssl_close_all(struct SessionHandle *data)
  414. {
  415. (void)data;
  416. }
  417. void Curl_polarssl_close(struct connectdata *conn, int sockindex)
  418. {
  419. rsa_free(&conn->ssl[sockindex].rsa);
  420. x509_crt_free(&conn->ssl[sockindex].clicert);
  421. x509_crt_free(&conn->ssl[sockindex].cacert);
  422. x509_crl_free(&conn->ssl[sockindex].crl);
  423. ssl_free(&conn->ssl[sockindex].ssl);
  424. }
  425. static ssize_t polarssl_recv(struct connectdata *conn,
  426. int num,
  427. char *buf,
  428. size_t buffersize,
  429. CURLcode *curlcode)
  430. {
  431. int ret = -1;
  432. ssize_t len = -1;
  433. memset(buf, 0, buffersize);
  434. ret = ssl_read(&conn->ssl[num].ssl, (unsigned char *)buf, buffersize);
  435. if(ret <= 0) {
  436. if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
  437. return 0;
  438. *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
  439. CURLE_AGAIN : CURLE_RECV_ERROR;
  440. return -1;
  441. }
  442. len = ret;
  443. return len;
  444. }
  445. void Curl_polarssl_session_free(void *ptr)
  446. {
  447. free(ptr);
  448. }
  449. size_t Curl_polarssl_version(char *buffer, size_t size)
  450. {
  451. unsigned int version = version_get_number();
  452. return snprintf(buffer, size, "PolarSSL/%d.%d.%d", version>>24,
  453. (version>>16)&0xff, (version>>8)&0xff);
  454. }
  455. static CURLcode
  456. polarssl_connect_common(struct connectdata *conn,
  457. int sockindex,
  458. bool nonblocking,
  459. bool *done)
  460. {
  461. CURLcode retcode;
  462. struct SessionHandle *data = conn->data;
  463. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  464. curl_socket_t sockfd = conn->sock[sockindex];
  465. long timeout_ms;
  466. int what;
  467. /* check if the connection has already been established */
  468. if(ssl_connection_complete == connssl->state) {
  469. *done = TRUE;
  470. return CURLE_OK;
  471. }
  472. if(ssl_connect_1==connssl->connecting_state) {
  473. /* Find out how much more time we're allowed */
  474. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  475. if(timeout_ms < 0) {
  476. /* no need to continue if time already is up */
  477. failf(data, "SSL connection timeout");
  478. return CURLE_OPERATION_TIMEDOUT;
  479. }
  480. retcode = polarssl_connect_step1(conn, sockindex);
  481. if(retcode)
  482. return retcode;
  483. }
  484. while(ssl_connect_2 == connssl->connecting_state ||
  485. ssl_connect_2_reading == connssl->connecting_state ||
  486. ssl_connect_2_writing == connssl->connecting_state) {
  487. /* check allowed time left */
  488. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  489. if(timeout_ms < 0) {
  490. /* no need to continue if time already is up */
  491. failf(data, "SSL connection timeout");
  492. return CURLE_OPERATION_TIMEDOUT;
  493. }
  494. /* if ssl is expecting something, check if it's available. */
  495. if(connssl->connecting_state == ssl_connect_2_reading
  496. || connssl->connecting_state == ssl_connect_2_writing) {
  497. curl_socket_t writefd = ssl_connect_2_writing==
  498. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  499. curl_socket_t readfd = ssl_connect_2_reading==
  500. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  501. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  502. if(what < 0) {
  503. /* fatal error */
  504. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  505. return CURLE_SSL_CONNECT_ERROR;
  506. }
  507. else if(0 == what) {
  508. if(nonblocking) {
  509. *done = FALSE;
  510. return CURLE_OK;
  511. }
  512. else {
  513. /* timeout */
  514. failf(data, "SSL connection timeout");
  515. return CURLE_OPERATION_TIMEDOUT;
  516. }
  517. }
  518. /* socket is readable or writable */
  519. }
  520. /* Run transaction, and return to the caller if it failed or if
  521. * this connection is part of a multi handle and this loop would
  522. * execute again. This permits the owner of a multi handle to
  523. * abort a connection attempt before step2 has completed while
  524. * ensuring that a client using select() or epoll() will always
  525. * have a valid fdset to wait on.
  526. */
  527. retcode = polarssl_connect_step2(conn, sockindex);
  528. if(retcode || (nonblocking &&
  529. (ssl_connect_2 == connssl->connecting_state ||
  530. ssl_connect_2_reading == connssl->connecting_state ||
  531. ssl_connect_2_writing == connssl->connecting_state)))
  532. return retcode;
  533. } /* repeat step2 until all transactions are done. */
  534. if(ssl_connect_3==connssl->connecting_state) {
  535. retcode = polarssl_connect_step3(conn, sockindex);
  536. if(retcode)
  537. return retcode;
  538. }
  539. if(ssl_connect_done==connssl->connecting_state) {
  540. connssl->state = ssl_connection_complete;
  541. conn->recv[sockindex] = polarssl_recv;
  542. conn->send[sockindex] = polarssl_send;
  543. *done = TRUE;
  544. }
  545. else
  546. *done = FALSE;
  547. /* Reset our connect state machine */
  548. connssl->connecting_state = ssl_connect_1;
  549. return CURLE_OK;
  550. }
  551. CURLcode
  552. Curl_polarssl_connect_nonblocking(struct connectdata *conn,
  553. int sockindex,
  554. bool *done)
  555. {
  556. return polarssl_connect_common(conn, sockindex, TRUE, done);
  557. }
  558. CURLcode
  559. Curl_polarssl_connect(struct connectdata *conn,
  560. int sockindex)
  561. {
  562. CURLcode retcode;
  563. bool done = FALSE;
  564. retcode = polarssl_connect_common(conn, sockindex, FALSE, &done);
  565. if(retcode)
  566. return retcode;
  567. DEBUGASSERT(done);
  568. return CURLE_OK;
  569. }
  570. /*
  571. * return 0 error initializing SSL
  572. * return 1 SSL initialized successfully
  573. */
  574. int polarssl_init(void)
  575. {
  576. return polarsslthreadlock_thread_setup();
  577. }
  578. void polarssl_cleanup(void)
  579. {
  580. (void)polarsslthreadlock_thread_cleanup();
  581. }
  582. #endif /* USE_POLARSSL */