polarssl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  9. * Copyright (C) 2012 - 2013, 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 sslgen.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 >= 0x01000000
  36. #include <polarssl/error.h>
  37. #endif /* POLARSSL_VERSION_NUMBER >= 0x01000000 */
  38. #if POLARSSL_VERSION_NUMBER>0x01010000
  39. #include <polarssl/entropy.h>
  40. #include <polarssl/ctr_drbg.h>
  41. #else
  42. #include <polarssl/havege.h>
  43. #endif /* POLARSSL_VERSION_NUMBER>0x01010000 */
  44. #if POLARSSL_VERSION_NUMBER<0x01000000
  45. /*
  46. Earlier versions of polarssl had no WANT_READ or WANT_WRITE, only TRY_AGAIN
  47. */
  48. #define POLARSSL_ERR_NET_WANT_READ POLARSSL_ERR_NET_TRY_AGAIN
  49. #define POLARSSL_ERR_NET_WANT_WRITE POLARSSL_ERR_NET_TRY_AGAIN
  50. #endif
  51. #include "urldata.h"
  52. #include "sendf.h"
  53. #include "inet_pton.h"
  54. #include "polarssl.h"
  55. #include "sslgen.h"
  56. #include "parsedate.h"
  57. #include "connect.h" /* for the connect timeout */
  58. #include "select.h"
  59. #include "rawstr.h"
  60. #include "polarssl_threadlock.h"
  61. #define _MPRINTF_REPLACE /* use our functions only */
  62. #include <curl/mprintf.h>
  63. #include "curl_memory.h"
  64. /* The last #include file should be: */
  65. #include "memdebug.h"
  66. /* apply threading? */
  67. #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
  68. #define THREADING_SUPPORT
  69. #endif
  70. #if defined(THREADING_SUPPORT) && POLARSSL_VERSION_NUMBER>0x01010000
  71. static entropy_context entropy;
  72. static int entropy_init_initialized = 0;
  73. /* start of entropy_init_mutex() */
  74. static void entropy_init_mutex(entropy_context *ctx)
  75. {
  76. /* lock 0 = entropy_init_mutex() */
  77. polarsslthreadlock_lock_function(0);
  78. if(entropy_init_initialized == 0) {
  79. entropy_init(ctx);
  80. entropy_init_initialized = 1;
  81. }
  82. polarsslthreadlock_unlock_function(0);
  83. }
  84. /* end of entropy_init_mutex() */
  85. /* start of entropy_func_mutex() */
  86. static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
  87. {
  88. int ret;
  89. /* lock 1 = entropy_func_mutex() */
  90. polarsslthreadlock_lock_function(1);
  91. ret = entropy_func(data, output, len);
  92. polarsslthreadlock_unlock_function(1);
  93. return ret;
  94. }
  95. /* end of entropy_func_mutex() */
  96. #endif /* THREADING_SUPPORT && POLARSSL_VERSION_NUMBER>0x01010000 */
  97. /* Define this to enable lots of debugging for PolarSSL */
  98. #undef POLARSSL_DEBUG
  99. #ifdef POLARSSL_DEBUG
  100. static void polarssl_debug(void *context, int level, char *line)
  101. {
  102. struct SessionHandle *data = NULL;
  103. if(!context)
  104. return;
  105. data = (struct SessionHandle *)context;
  106. infof(data, "%s\n", line);
  107. }
  108. #else
  109. #endif
  110. static Curl_recv polarssl_recv;
  111. static Curl_send polarssl_send;
  112. static CURLcode
  113. polarssl_connect_step1(struct connectdata *conn,
  114. int sockindex)
  115. {
  116. struct SessionHandle *data = conn->data;
  117. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  118. bool sni = TRUE; /* default is SNI enabled */
  119. int ret = -1;
  120. #ifdef ENABLE_IPV6
  121. struct in6_addr addr;
  122. #else
  123. struct in_addr addr;
  124. #endif
  125. void *old_session = NULL;
  126. size_t old_session_size = 0;
  127. char errorbuf[128];
  128. memset(errorbuf, 0, sizeof(errorbuf));
  129. /* PolarSSL only supports SSLv3 and TLSv1 */
  130. if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
  131. failf(data, "PolarSSL does not support SSLv2");
  132. return CURLE_SSL_CONNECT_ERROR;
  133. }
  134. else if(data->set.ssl.version == CURL_SSLVERSION_SSLv3)
  135. sni = FALSE; /* SSLv3 has no SNI */
  136. #if POLARSSL_VERSION_NUMBER<0x01010000
  137. havege_init(&connssl->hs);
  138. #else
  139. #ifdef THREADING_SUPPORT
  140. entropy_init_mutex(&entropy);
  141. if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func_mutex, &entropy,
  142. connssl->ssn.id, connssl->ssn.length)) != 0) {
  143. #ifdef POLARSSL_ERROR_C
  144. error_strerror(ret, errorbuf, sizeof(errorbuf));
  145. #endif /* POLARSSL_ERROR_C */
  146. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  147. -ret, errorbuf);
  148. }
  149. #else
  150. entropy_init(&connssl->entropy);
  151. if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func, &connssl->entropy,
  152. connssl->ssn.id, connssl->ssn.length)) != 0) {
  153. #ifdef POLARSSL_ERROR_C
  154. error_strerror(ret, errorbuf, sizeof(errorbuf));
  155. #endif /* POLARSSL_ERROR_C */
  156. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  157. -ret, errorbuf);
  158. }
  159. #endif /* THREADING_SUPPORT */
  160. #endif /* POLARSSL_VERSION_NUMBER<0x01010000 */
  161. /* Load the trusted CA */
  162. memset(&connssl->cacert, 0, sizeof(x509_cert));
  163. if(data->set.str[STRING_SSL_CAFILE]) {
  164. ret = x509parse_crtfile(&connssl->cacert,
  165. data->set.str[STRING_SSL_CAFILE]);
  166. if(ret<0) {
  167. #ifdef POLARSSL_ERROR_C
  168. error_strerror(ret, errorbuf, sizeof(errorbuf));
  169. #endif /* POLARSSL_ERROR_C */
  170. failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s",
  171. data->set.str[STRING_SSL_CAFILE], -ret, errorbuf);
  172. if(data->set.ssl.verifypeer)
  173. return CURLE_SSL_CACERT_BADFILE;
  174. }
  175. }
  176. /* Load the client certificate */
  177. memset(&connssl->clicert, 0, sizeof(x509_cert));
  178. if(data->set.str[STRING_CERT]) {
  179. ret = x509parse_crtfile(&connssl->clicert,
  180. data->set.str[STRING_CERT]);
  181. if(ret) {
  182. #ifdef POLARSSL_ERROR_C
  183. error_strerror(ret, errorbuf, sizeof(errorbuf));
  184. #endif /* POLARSSL_ERROR_C */
  185. failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
  186. data->set.str[STRING_CERT], -ret, errorbuf);
  187. return CURLE_SSL_CERTPROBLEM;
  188. }
  189. }
  190. /* Load the client private key */
  191. if(data->set.str[STRING_KEY]) {
  192. ret = x509parse_keyfile(&connssl->rsa,
  193. data->set.str[STRING_KEY],
  194. data->set.str[STRING_KEY_PASSWD]);
  195. if(ret) {
  196. #ifdef POLARSSL_ERROR_C
  197. error_strerror(ret, errorbuf, sizeof(errorbuf));
  198. #endif /* POLARSSL_ERROR_C */
  199. failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
  200. data->set.str[STRING_KEY], -ret, errorbuf);
  201. return CURLE_SSL_CERTPROBLEM;
  202. }
  203. }
  204. /* Load the CRL */
  205. memset(&connssl->crl, 0, sizeof(x509_crl));
  206. if(data->set.str[STRING_SSL_CRLFILE]) {
  207. ret = x509parse_crlfile(&connssl->crl,
  208. data->set.str[STRING_SSL_CRLFILE]);
  209. if(ret) {
  210. #ifdef POLARSSL_ERROR_C
  211. error_strerror(ret, errorbuf, sizeof(errorbuf));
  212. #endif /* POLARSSL_ERROR_C */
  213. failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
  214. data->set.str[STRING_SSL_CRLFILE], -ret, errorbuf);
  215. return CURLE_SSL_CRL_BADFILE;
  216. }
  217. }
  218. infof(data, "PolarSSL: Connecting to %s:%d\n",
  219. conn->host.name, conn->remote_port);
  220. if(ssl_init(&connssl->ssl)) {
  221. failf(data, "PolarSSL: ssl_init failed");
  222. return CURLE_SSL_CONNECT_ERROR;
  223. }
  224. ssl_set_endpoint(&connssl->ssl, SSL_IS_CLIENT);
  225. ssl_set_authmode(&connssl->ssl, SSL_VERIFY_OPTIONAL);
  226. #if POLARSSL_VERSION_NUMBER<0x01010000
  227. ssl_set_rng(&connssl->ssl, havege_rand,
  228. &connssl->hs);
  229. #else
  230. ssl_set_rng(&connssl->ssl, ctr_drbg_random,
  231. &connssl->ctr_drbg);
  232. #endif /* POLARSSL_VERSION_NUMBER<0x01010000 */
  233. ssl_set_bio(&connssl->ssl,
  234. net_recv, &conn->sock[sockindex],
  235. net_send, &conn->sock[sockindex]);
  236. #if POLARSSL_VERSION_NUMBER<0x01000000
  237. ssl_set_ciphers(&connssl->ssl, ssl_default_ciphers);
  238. #else
  239. ssl_set_ciphersuites(&connssl->ssl, ssl_default_ciphersuites);
  240. #endif
  241. if(!Curl_ssl_getsessionid(conn, &old_session, &old_session_size)) {
  242. memcpy(&connssl->ssn, old_session, old_session_size);
  243. infof(data, "PolarSSL re-using session\n");
  244. }
  245. /* PolarSSL SVN revision r1316 to r1317, matching <1.2.0 is to cover Ubuntu's
  246. 1.1.4 version and the like */
  247. #if POLARSSL_VERSION_NUMBER<0x01020000
  248. ssl_set_session(&connssl->ssl, 1, 600,
  249. &connssl->ssn);
  250. #else
  251. ssl_set_session(&connssl->ssl,
  252. &connssl->ssn);
  253. #endif
  254. ssl_set_ca_chain(&connssl->ssl,
  255. &connssl->cacert,
  256. &connssl->crl,
  257. conn->host.name);
  258. ssl_set_own_cert(&connssl->ssl,
  259. &connssl->clicert, &connssl->rsa);
  260. if(!Curl_inet_pton(AF_INET, conn->host.name, &addr) &&
  261. #ifdef ENABLE_IPV6
  262. !Curl_inet_pton(AF_INET6, conn->host.name, &addr) &&
  263. #endif
  264. sni && ssl_set_hostname(&connssl->ssl, conn->host.name)) {
  265. infof(data, "WARNING: failed to configure "
  266. "server name indication (SNI) TLS extension\n");
  267. }
  268. #ifdef POLARSSL_DEBUG
  269. ssl_set_dbg(&connssl->ssl, polarssl_debug, data);
  270. #endif
  271. connssl->connecting_state = ssl_connect_2;
  272. return CURLE_OK;
  273. }
  274. static CURLcode
  275. polarssl_connect_step2(struct connectdata *conn,
  276. int sockindex)
  277. {
  278. int ret;
  279. struct SessionHandle *data = conn->data;
  280. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  281. char buffer[1024];
  282. char errorbuf[128];
  283. memset(errorbuf, 0, sizeof(errorbuf));
  284. conn->recv[sockindex] = polarssl_recv;
  285. conn->send[sockindex] = polarssl_send;
  286. for(;;) {
  287. if(!(ret = ssl_handshake(&connssl->ssl)))
  288. break;
  289. else if(ret != POLARSSL_ERR_NET_WANT_READ &&
  290. ret != POLARSSL_ERR_NET_WANT_WRITE) {
  291. #ifdef POLARSSL_ERROR_C
  292. error_strerror(ret, errorbuf, sizeof(errorbuf));
  293. #endif /* POLARSSL_ERROR_C */
  294. failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
  295. -ret, errorbuf);
  296. return CURLE_SSL_CONNECT_ERROR;
  297. }
  298. else {
  299. if(ret == POLARSSL_ERR_NET_WANT_READ) {
  300. connssl->connecting_state = ssl_connect_2_reading;
  301. return CURLE_OK;
  302. }
  303. if(ret == POLARSSL_ERR_NET_WANT_WRITE) {
  304. connssl->connecting_state = ssl_connect_2_writing;
  305. return CURLE_OK;
  306. }
  307. failf(data, "SSL_connect failed with error %d.", ret);
  308. return CURLE_SSL_CONNECT_ERROR;
  309. }
  310. }
  311. infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
  312. #if POLARSSL_VERSION_NUMBER<0x01000000
  313. ssl_get_cipher(&conn->ssl[sockindex].ssl)
  314. #elif POLARSSL_VERSION_NUMBER >= 0x01010000
  315. ssl_get_ciphersuite(&conn->ssl[sockindex].ssl)
  316. #else
  317. ssl_get_ciphersuite_name(&conn->ssl[sockindex].ssl)
  318. #endif
  319. );
  320. ret = ssl_get_verify_result(&conn->ssl[sockindex].ssl);
  321. if(ret && data->set.ssl.verifypeer) {
  322. if(ret & BADCERT_EXPIRED)
  323. failf(data, "Cert verify failed: BADCERT_EXPIRED");
  324. if(ret & BADCERT_REVOKED) {
  325. failf(data, "Cert verify failed: BADCERT_REVOKED");
  326. return CURLE_SSL_CACERT;
  327. }
  328. if(ret & BADCERT_CN_MISMATCH)
  329. failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
  330. if(ret & BADCERT_NOT_TRUSTED)
  331. failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
  332. return CURLE_PEER_FAILED_VERIFICATION;
  333. }
  334. /* PolarSSL SVN revision r1316 to r1317, matching <1.2.0 is to cover Ubuntu's
  335. 1.1.4 version and the like */
  336. #if POLARSSL_VERSION_NUMBER<0x01020000
  337. if(conn->ssl[sockindex].ssl.peer_cert) {
  338. #else
  339. if(ssl_get_peer_cert(&(connssl->ssl))) {
  340. #endif
  341. /* If the session was resumed, there will be no peer certs */
  342. memset(buffer, 0, sizeof(buffer));
  343. /* PolarSSL SVN revision r1316 to r1317, matching <1.2.0 is to cover Ubuntu's
  344. 1.1.4 version and the like */
  345. #if POLARSSL_VERSION_NUMBER<0x01020000
  346. if(x509parse_cert_info(buffer, sizeof(buffer), (char *)"* ",
  347. conn->ssl[sockindex].ssl.peer_cert) != -1)
  348. #else
  349. if(x509parse_cert_info(buffer, sizeof(buffer), (char *)"* ",
  350. ssl_get_peer_cert(&(connssl->ssl))) != -1)
  351. #endif
  352. infof(data, "Dumping cert info:\n%s\n", buffer);
  353. }
  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_free(&conn->ssl[sockindex].clicert);
  421. x509_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 */