polarssl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <[email protected]>
  9. * Copyright (C) 2012 - 2014, Daniel Stenberg, <[email protected]>, 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. errorbuf[0]=0;
  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. if(data->set.str[STRING_SSL_CAPATH]) {
  171. ret = x509_crt_parse_path(&connssl->cacert,
  172. data->set.str[STRING_SSL_CAPATH]);
  173. if(ret<0) {
  174. #ifdef POLARSSL_ERROR_C
  175. error_strerror(ret, errorbuf, sizeof(errorbuf));
  176. #endif /* POLARSSL_ERROR_C */
  177. failf(data, "Error reading ca cert path %s - PolarSSL: (-0x%04X) %s",
  178. data->set.str[STRING_SSL_CAPATH], -ret, errorbuf);
  179. if(data->set.ssl.verifypeer)
  180. return CURLE_SSL_CACERT_BADFILE;
  181. }
  182. }
  183. /* Load the client certificate */
  184. memset(&connssl->clicert, 0, sizeof(x509_crt));
  185. if(data->set.str[STRING_CERT]) {
  186. ret = x509_crt_parse_file(&connssl->clicert,
  187. data->set.str[STRING_CERT]);
  188. if(ret) {
  189. #ifdef POLARSSL_ERROR_C
  190. error_strerror(ret, errorbuf, sizeof(errorbuf));
  191. #endif /* POLARSSL_ERROR_C */
  192. failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
  193. data->set.str[STRING_CERT], -ret, errorbuf);
  194. return CURLE_SSL_CERTPROBLEM;
  195. }
  196. }
  197. /* Load the client private key */
  198. if(data->set.str[STRING_KEY]) {
  199. pk_context pk;
  200. pk_init(&pk);
  201. ret = pk_parse_keyfile(&pk, data->set.str[STRING_KEY],
  202. data->set.str[STRING_KEY_PASSWD]);
  203. if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA))
  204. ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
  205. if(ret == 0)
  206. rsa_copy(&connssl->rsa, pk_rsa(pk));
  207. else
  208. rsa_free(&connssl->rsa);
  209. pk_free(&pk);
  210. if(ret) {
  211. #ifdef POLARSSL_ERROR_C
  212. error_strerror(ret, errorbuf, sizeof(errorbuf));
  213. #endif /* POLARSSL_ERROR_C */
  214. failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
  215. data->set.str[STRING_KEY], -ret, errorbuf);
  216. return CURLE_SSL_CERTPROBLEM;
  217. }
  218. }
  219. /* Load the CRL */
  220. memset(&connssl->crl, 0, sizeof(x509_crl));
  221. if(data->set.str[STRING_SSL_CRLFILE]) {
  222. ret = x509_crl_parse_file(&connssl->crl,
  223. data->set.str[STRING_SSL_CRLFILE]);
  224. if(ret) {
  225. #ifdef POLARSSL_ERROR_C
  226. error_strerror(ret, errorbuf, sizeof(errorbuf));
  227. #endif /* POLARSSL_ERROR_C */
  228. failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
  229. data->set.str[STRING_SSL_CRLFILE], -ret, errorbuf);
  230. return CURLE_SSL_CRL_BADFILE;
  231. }
  232. }
  233. infof(data, "PolarSSL: Connecting to %s:%d\n",
  234. conn->host.name, conn->remote_port);
  235. if(ssl_init(&connssl->ssl)) {
  236. failf(data, "PolarSSL: ssl_init failed");
  237. return CURLE_SSL_CONNECT_ERROR;
  238. }
  239. switch(data->set.ssl.version) {
  240. default:
  241. case CURL_SSLVERSION_DEFAULT:
  242. ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
  243. SSL_MINOR_VERSION_1);
  244. break;
  245. case CURL_SSLVERSION_SSLv3:
  246. ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
  247. SSL_MINOR_VERSION_0);
  248. infof(data, "PolarSSL: Forced min. SSL Version to be SSLv3\n");
  249. break;
  250. case CURL_SSLVERSION_TLSv1_0:
  251. ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
  252. SSL_MINOR_VERSION_1);
  253. infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.0\n");
  254. break;
  255. case CURL_SSLVERSION_TLSv1_1:
  256. ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
  257. SSL_MINOR_VERSION_2);
  258. infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.1\n");
  259. break;
  260. case CURL_SSLVERSION_TLSv1_2:
  261. ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
  262. SSL_MINOR_VERSION_3);
  263. infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.2\n");
  264. break;
  265. }
  266. ssl_set_endpoint(&connssl->ssl, SSL_IS_CLIENT);
  267. ssl_set_authmode(&connssl->ssl, SSL_VERIFY_OPTIONAL);
  268. ssl_set_rng(&connssl->ssl, ctr_drbg_random,
  269. &connssl->ctr_drbg);
  270. ssl_set_bio(&connssl->ssl,
  271. net_recv, &conn->sock[sockindex],
  272. net_send, &conn->sock[sockindex]);
  273. ssl_set_ciphersuites(&connssl->ssl, ssl_list_ciphersuites());
  274. if(!Curl_ssl_getsessionid(conn, &old_session, &old_session_size)) {
  275. memcpy(&connssl->ssn, old_session, old_session_size);
  276. infof(data, "PolarSSL re-using session\n");
  277. }
  278. ssl_set_session(&connssl->ssl,
  279. &connssl->ssn);
  280. ssl_set_ca_chain(&connssl->ssl,
  281. &connssl->cacert,
  282. &connssl->crl,
  283. conn->host.name);
  284. ssl_set_own_cert_rsa(&connssl->ssl,
  285. &connssl->clicert, &connssl->rsa);
  286. if(!Curl_inet_pton(AF_INET, conn->host.name, &addr) &&
  287. #ifdef ENABLE_IPV6
  288. !Curl_inet_pton(AF_INET6, conn->host.name, &addr) &&
  289. #endif
  290. sni && ssl_set_hostname(&connssl->ssl, conn->host.name)) {
  291. infof(data, "WARNING: failed to configure "
  292. "server name indication (SNI) TLS extension\n");
  293. }
  294. #ifdef HAS_ALPN
  295. if(data->set.httpversion == CURL_HTTP_VERSION_2_0) {
  296. if(data->set.ssl_enable_alpn) {
  297. static const char* protocols[] = {
  298. NGHTTP2_PROTO_VERSION_ID, ALPN_HTTP_1_1, NULL
  299. };
  300. ssl_set_alpn_protocols(&connssl->ssl, protocols);
  301. infof(data, "ALPN, offering %s, %s\n", protocols[0],
  302. protocols[1]);
  303. connssl->asked_for_h2 = TRUE;
  304. }
  305. }
  306. #endif
  307. #ifdef POLARSSL_DEBUG
  308. ssl_set_dbg(&connssl->ssl, polarssl_debug, data);
  309. #endif
  310. connssl->connecting_state = ssl_connect_2;
  311. return CURLE_OK;
  312. }
  313. static CURLcode
  314. polarssl_connect_step2(struct connectdata *conn,
  315. int sockindex)
  316. {
  317. int ret;
  318. struct SessionHandle *data = conn->data;
  319. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  320. char buffer[1024];
  321. #ifdef HAS_ALPN
  322. const char* next_protocol;
  323. #endif
  324. char errorbuf[128];
  325. errorbuf[0] = 0;
  326. conn->recv[sockindex] = polarssl_recv;
  327. conn->send[sockindex] = polarssl_send;
  328. for(;;) {
  329. if(!(ret = ssl_handshake(&connssl->ssl)))
  330. break;
  331. else if(ret != POLARSSL_ERR_NET_WANT_READ &&
  332. ret != POLARSSL_ERR_NET_WANT_WRITE) {
  333. #ifdef POLARSSL_ERROR_C
  334. error_strerror(ret, errorbuf, sizeof(errorbuf));
  335. #endif /* POLARSSL_ERROR_C */
  336. failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
  337. -ret, errorbuf);
  338. return CURLE_SSL_CONNECT_ERROR;
  339. }
  340. else {
  341. if(ret == POLARSSL_ERR_NET_WANT_READ) {
  342. connssl->connecting_state = ssl_connect_2_reading;
  343. return CURLE_OK;
  344. }
  345. if(ret == POLARSSL_ERR_NET_WANT_WRITE) {
  346. connssl->connecting_state = ssl_connect_2_writing;
  347. return CURLE_OK;
  348. }
  349. failf(data, "SSL_connect failed with error %d.", ret);
  350. return CURLE_SSL_CONNECT_ERROR;
  351. }
  352. }
  353. infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
  354. ssl_get_ciphersuite(&conn->ssl[sockindex].ssl)
  355. );
  356. ret = ssl_get_verify_result(&conn->ssl[sockindex].ssl);
  357. if(ret && data->set.ssl.verifypeer) {
  358. if(ret & BADCERT_EXPIRED)
  359. failf(data, "Cert verify failed: BADCERT_EXPIRED");
  360. if(ret & BADCERT_REVOKED) {
  361. failf(data, "Cert verify failed: BADCERT_REVOKED");
  362. return CURLE_SSL_CACERT;
  363. }
  364. if(ret & BADCERT_CN_MISMATCH)
  365. failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
  366. if(ret & BADCERT_NOT_TRUSTED)
  367. failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
  368. return CURLE_PEER_FAILED_VERIFICATION;
  369. }
  370. if(ssl_get_peer_cert(&(connssl->ssl))) {
  371. /* If the session was resumed, there will be no peer certs */
  372. memset(buffer, 0, sizeof(buffer));
  373. if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ",
  374. ssl_get_peer_cert(&(connssl->ssl))) != -1)
  375. infof(data, "Dumping cert info:\n%s\n", buffer);
  376. }
  377. #ifdef HAS_ALPN
  378. if(data->set.ssl_enable_alpn) {
  379. next_protocol = ssl_get_alpn_protocol(&connssl->ssl);
  380. if(next_protocol != NULL) {
  381. infof(data, "ALPN, server accepted to use %s\n", next_protocol);
  382. if(strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
  383. NGHTTP2_PROTO_VERSION_ID_LEN)) {
  384. conn->negnpn = NPN_HTTP2;
  385. }
  386. else if(strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) {
  387. conn->negnpn = NPN_HTTP1_1;
  388. }
  389. }
  390. else if(connssl->asked_for_h2) {
  391. infof(data, "ALPN, server did not agree to a protocol\n");
  392. }
  393. }
  394. #endif
  395. connssl->connecting_state = ssl_connect_3;
  396. infof(data, "SSL connected\n");
  397. return CURLE_OK;
  398. }
  399. static CURLcode
  400. polarssl_connect_step3(struct connectdata *conn,
  401. int sockindex)
  402. {
  403. CURLcode result = CURLE_OK;
  404. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  405. struct SessionHandle *data = conn->data;
  406. void *old_ssl_sessionid = NULL;
  407. ssl_session *our_ssl_sessionid = &conn->ssl[sockindex].ssn ;
  408. bool incache;
  409. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  410. /* Save the current session data for possible re-use */
  411. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
  412. if(incache) {
  413. if(old_ssl_sessionid != our_ssl_sessionid) {
  414. infof(data, "old SSL session ID is stale, removing\n");
  415. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  416. incache = FALSE;
  417. }
  418. }
  419. if(!incache) {
  420. void *new_session = malloc(sizeof(ssl_session));
  421. if(new_session) {
  422. memcpy(new_session, our_ssl_sessionid, sizeof(ssl_session));
  423. result = Curl_ssl_addsessionid(conn, new_session, sizeof(ssl_session));
  424. }
  425. else
  426. result = CURLE_OUT_OF_MEMORY;
  427. if(result) {
  428. failf(data, "failed to store ssl session");
  429. return result;
  430. }
  431. }
  432. connssl->connecting_state = ssl_connect_done;
  433. return CURLE_OK;
  434. }
  435. static ssize_t polarssl_send(struct connectdata *conn,
  436. int sockindex,
  437. const void *mem,
  438. size_t len,
  439. CURLcode *curlcode)
  440. {
  441. int ret = -1;
  442. ret = ssl_write(&conn->ssl[sockindex].ssl,
  443. (unsigned char *)mem, len);
  444. if(ret < 0) {
  445. *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
  446. CURLE_AGAIN : CURLE_SEND_ERROR;
  447. ret = -1;
  448. }
  449. return ret;
  450. }
  451. void Curl_polarssl_close_all(struct SessionHandle *data)
  452. {
  453. (void)data;
  454. }
  455. void Curl_polarssl_close(struct connectdata *conn, int sockindex)
  456. {
  457. rsa_free(&conn->ssl[sockindex].rsa);
  458. x509_crt_free(&conn->ssl[sockindex].clicert);
  459. x509_crt_free(&conn->ssl[sockindex].cacert);
  460. x509_crl_free(&conn->ssl[sockindex].crl);
  461. ssl_free(&conn->ssl[sockindex].ssl);
  462. }
  463. static ssize_t polarssl_recv(struct connectdata *conn,
  464. int num,
  465. char *buf,
  466. size_t buffersize,
  467. CURLcode *curlcode)
  468. {
  469. int ret = -1;
  470. ssize_t len = -1;
  471. memset(buf, 0, buffersize);
  472. ret = ssl_read(&conn->ssl[num].ssl, (unsigned char *)buf, buffersize);
  473. if(ret <= 0) {
  474. if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
  475. return 0;
  476. *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
  477. CURLE_AGAIN : CURLE_RECV_ERROR;
  478. return -1;
  479. }
  480. len = ret;
  481. return len;
  482. }
  483. void Curl_polarssl_session_free(void *ptr)
  484. {
  485. free(ptr);
  486. }
  487. size_t Curl_polarssl_version(char *buffer, size_t size)
  488. {
  489. unsigned int version = version_get_number();
  490. return snprintf(buffer, size, "PolarSSL/%d.%d.%d", version>>24,
  491. (version>>16)&0xff, (version>>8)&0xff);
  492. }
  493. static CURLcode
  494. polarssl_connect_common(struct connectdata *conn,
  495. int sockindex,
  496. bool nonblocking,
  497. bool *done)
  498. {
  499. CURLcode result;
  500. struct SessionHandle *data = conn->data;
  501. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  502. curl_socket_t sockfd = conn->sock[sockindex];
  503. long timeout_ms;
  504. int what;
  505. /* check if the connection has already been established */
  506. if(ssl_connection_complete == connssl->state) {
  507. *done = TRUE;
  508. return CURLE_OK;
  509. }
  510. if(ssl_connect_1 == connssl->connecting_state) {
  511. /* Find out how much more time we're allowed */
  512. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  513. if(timeout_ms < 0) {
  514. /* no need to continue if time already is up */
  515. failf(data, "SSL connection timeout");
  516. return CURLE_OPERATION_TIMEDOUT;
  517. }
  518. result = polarssl_connect_step1(conn, sockindex);
  519. if(result)
  520. return result;
  521. }
  522. while(ssl_connect_2 == connssl->connecting_state ||
  523. ssl_connect_2_reading == connssl->connecting_state ||
  524. ssl_connect_2_writing == connssl->connecting_state) {
  525. /* check allowed time left */
  526. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  527. if(timeout_ms < 0) {
  528. /* no need to continue if time already is up */
  529. failf(data, "SSL connection timeout");
  530. return CURLE_OPERATION_TIMEDOUT;
  531. }
  532. /* if ssl is expecting something, check if it's available. */
  533. if(connssl->connecting_state == ssl_connect_2_reading ||
  534. connssl->connecting_state == ssl_connect_2_writing) {
  535. curl_socket_t writefd = ssl_connect_2_writing==
  536. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  537. curl_socket_t readfd = ssl_connect_2_reading==
  538. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  539. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  540. if(what < 0) {
  541. /* fatal error */
  542. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  543. return CURLE_SSL_CONNECT_ERROR;
  544. }
  545. else if(0 == what) {
  546. if(nonblocking) {
  547. *done = FALSE;
  548. return CURLE_OK;
  549. }
  550. else {
  551. /* timeout */
  552. failf(data, "SSL connection timeout");
  553. return CURLE_OPERATION_TIMEDOUT;
  554. }
  555. }
  556. /* socket is readable or writable */
  557. }
  558. /* Run transaction, and return to the caller if it failed or if
  559. * this connection is part of a multi handle and this loop would
  560. * execute again. This permits the owner of a multi handle to
  561. * abort a connection attempt before step2 has completed while
  562. * ensuring that a client using select() or epoll() will always
  563. * have a valid fdset to wait on.
  564. */
  565. result = polarssl_connect_step2(conn, sockindex);
  566. if(result || (nonblocking &&
  567. (ssl_connect_2 == connssl->connecting_state ||
  568. ssl_connect_2_reading == connssl->connecting_state ||
  569. ssl_connect_2_writing == connssl->connecting_state)))
  570. return result;
  571. } /* repeat step2 until all transactions are done. */
  572. if(ssl_connect_3 == connssl->connecting_state) {
  573. result = polarssl_connect_step3(conn, sockindex);
  574. if(result)
  575. return result;
  576. }
  577. if(ssl_connect_done == connssl->connecting_state) {
  578. connssl->state = ssl_connection_complete;
  579. conn->recv[sockindex] = polarssl_recv;
  580. conn->send[sockindex] = polarssl_send;
  581. *done = TRUE;
  582. }
  583. else
  584. *done = FALSE;
  585. /* Reset our connect state machine */
  586. connssl->connecting_state = ssl_connect_1;
  587. return CURLE_OK;
  588. }
  589. CURLcode
  590. Curl_polarssl_connect_nonblocking(struct connectdata *conn,
  591. int sockindex,
  592. bool *done)
  593. {
  594. return polarssl_connect_common(conn, sockindex, TRUE, done);
  595. }
  596. CURLcode
  597. Curl_polarssl_connect(struct connectdata *conn,
  598. int sockindex)
  599. {
  600. CURLcode result;
  601. bool done = FALSE;
  602. result = polarssl_connect_common(conn, sockindex, FALSE, &done);
  603. if(result)
  604. return result;
  605. DEBUGASSERT(done);
  606. return CURLE_OK;
  607. }
  608. /*
  609. * return 0 error initializing SSL
  610. * return 1 SSL initialized successfully
  611. */
  612. int polarssl_init(void)
  613. {
  614. return polarsslthreadlock_thread_setup();
  615. }
  616. void polarssl_cleanup(void)
  617. {
  618. (void)polarsslthreadlock_thread_cleanup();
  619. }
  620. #endif /* USE_POLARSSL */