cyassl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /*
  23. * Source file for all CyaSSL-specific code for the TLS/SSL layer. No code
  24. * but vtls.c should ever call or use these functions.
  25. *
  26. */
  27. #include "curl_setup.h"
  28. #ifdef USE_CYASSL
  29. #ifdef HAVE_LIMITS_H
  30. #include <limits.h>
  31. #endif
  32. #include "urldata.h"
  33. #include "sendf.h"
  34. #include "inet_pton.h"
  35. #include "cyassl.h"
  36. #include "vtls.h"
  37. #include "parsedate.h"
  38. #include "connect.h" /* for the connect timeout */
  39. #include "select.h"
  40. #include "rawstr.h"
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. #include "curl_memory.h"
  44. #include <cyassl/ssl.h>
  45. #ifdef HAVE_CYASSL_ERROR_SSL_H
  46. #include <cyassl/error-ssl.h>
  47. #else
  48. #include <cyassl/error.h>
  49. #endif
  50. /* The last #include file should be: */
  51. #include "memdebug.h"
  52. static Curl_recv cyassl_recv;
  53. static Curl_send cyassl_send;
  54. static int do_file_type(const char *type)
  55. {
  56. if(!type || !type[0])
  57. return SSL_FILETYPE_PEM;
  58. if(Curl_raw_equal(type, "PEM"))
  59. return SSL_FILETYPE_PEM;
  60. if(Curl_raw_equal(type, "DER"))
  61. return SSL_FILETYPE_ASN1;
  62. return -1;
  63. }
  64. /*
  65. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  66. * layer and do all necessary magic.
  67. */
  68. static CURLcode
  69. cyassl_connect_step1(struct connectdata *conn,
  70. int sockindex)
  71. {
  72. struct SessionHandle *data = conn->data;
  73. struct ssl_connect_data* conssl = &conn->ssl[sockindex];
  74. SSL_METHOD* req_method = NULL;
  75. void* ssl_sessionid = NULL;
  76. curl_socket_t sockfd = conn->sock[sockindex];
  77. if(conssl->state == ssl_connection_complete)
  78. return CURLE_OK;
  79. /* CyaSSL doesn't support SSLv2 */
  80. if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
  81. failf(data, "CyaSSL does not support SSLv2");
  82. return CURLE_SSL_CONNECT_ERROR;
  83. }
  84. /* check to see if we've been told to use an explicit SSL/TLS version */
  85. switch(data->set.ssl.version) {
  86. case CURL_SSLVERSION_DEFAULT:
  87. /* we try to figure out version */
  88. req_method = SSLv23_client_method();
  89. break;
  90. case CURL_SSLVERSION_TLSv1:
  91. infof(data, "CyaSSL cannot be configured to use TLS 1.0-1.2, "
  92. "TLS 1.0 is used exclusively\n");
  93. req_method = TLSv1_client_method();
  94. break;
  95. case CURL_SSLVERSION_TLSv1_0:
  96. req_method = TLSv1_client_method();
  97. break;
  98. case CURL_SSLVERSION_TLSv1_1:
  99. req_method = TLSv1_1_client_method();
  100. break;
  101. case CURL_SSLVERSION_TLSv1_2:
  102. req_method = TLSv1_2_client_method();
  103. break;
  104. case CURL_SSLVERSION_SSLv3:
  105. req_method = SSLv3_client_method();
  106. break;
  107. default:
  108. req_method = TLSv1_client_method();
  109. }
  110. if(!req_method) {
  111. failf(data, "SSL: couldn't create a method!");
  112. return CURLE_OUT_OF_MEMORY;
  113. }
  114. if(conssl->ctx)
  115. SSL_CTX_free(conssl->ctx);
  116. conssl->ctx = SSL_CTX_new(req_method);
  117. if(!conssl->ctx) {
  118. failf(data, "SSL: couldn't create a context!");
  119. return CURLE_OUT_OF_MEMORY;
  120. }
  121. #ifndef NO_FILESYSTEM
  122. /* load trusted cacert */
  123. if(data->set.str[STRING_SSL_CAFILE]) {
  124. if(!SSL_CTX_load_verify_locations(conssl->ctx,
  125. data->set.str[STRING_SSL_CAFILE],
  126. data->set.str[STRING_SSL_CAPATH])) {
  127. if(data->set.ssl.verifypeer) {
  128. /* Fail if we insiste on successfully verifying the server. */
  129. failf(data,"error setting certificate verify locations:\n"
  130. " CAfile: %s\n CApath: %s",
  131. data->set.str[STRING_SSL_CAFILE]?
  132. data->set.str[STRING_SSL_CAFILE]: "none",
  133. data->set.str[STRING_SSL_CAPATH]?
  134. data->set.str[STRING_SSL_CAPATH] : "none");
  135. return CURLE_SSL_CACERT_BADFILE;
  136. }
  137. else {
  138. /* Just continue with a warning if no strict certificate
  139. verification is required. */
  140. infof(data, "error setting certificate verify locations,"
  141. " continuing anyway:\n");
  142. }
  143. }
  144. else {
  145. /* Everything is fine. */
  146. infof(data, "successfully set certificate verify locations:\n");
  147. }
  148. infof(data,
  149. " CAfile: %s\n"
  150. " CApath: %s\n",
  151. data->set.str[STRING_SSL_CAFILE] ? data->set.str[STRING_SSL_CAFILE]:
  152. "none",
  153. data->set.str[STRING_SSL_CAPATH] ? data->set.str[STRING_SSL_CAPATH]:
  154. "none");
  155. }
  156. /* Load the client certificate, and private key */
  157. if(data->set.str[STRING_CERT] && data->set.str[STRING_KEY]) {
  158. int file_type = do_file_type(data->set.str[STRING_CERT_TYPE]);
  159. if(SSL_CTX_use_certificate_file(conssl->ctx, data->set.str[STRING_CERT],
  160. file_type) != 1) {
  161. failf(data, "unable to use client certificate (no key or wrong pass"
  162. " phrase?)");
  163. return CURLE_SSL_CONNECT_ERROR;
  164. }
  165. file_type = do_file_type(data->set.str[STRING_KEY_TYPE]);
  166. if(SSL_CTX_use_PrivateKey_file(conssl->ctx, data->set.str[STRING_KEY],
  167. file_type) != 1) {
  168. failf(data, "unable to set private key");
  169. return CURLE_SSL_CONNECT_ERROR;
  170. }
  171. }
  172. #else
  173. if(CyaSSL_no_filesystem_verify(conssl->ctx)!= SSL_SUCCESS) {
  174. return CURLE_SSL_CONNECT_ERROR;
  175. }
  176. #endif /* NO_FILESYSTEM */
  177. /* SSL always tries to verify the peer, this only says whether it should
  178. * fail to connect if the verification fails, or if it should continue
  179. * anyway. In the latter case the result of the verification is checked with
  180. * SSL_get_verify_result() below. */
  181. SSL_CTX_set_verify(conssl->ctx,
  182. data->set.ssl.verifypeer?SSL_VERIFY_PEER:SSL_VERIFY_NONE,
  183. NULL);
  184. /* Let's make an SSL structure */
  185. if(conssl->handle)
  186. SSL_free(conssl->handle);
  187. conssl->handle = SSL_new(conssl->ctx);
  188. if(!conssl->handle) {
  189. failf(data, "SSL: couldn't create a context (handle)!");
  190. return CURLE_OUT_OF_MEMORY;
  191. }
  192. /* Check if there's a cached ID we can/should use here! */
  193. if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL)) {
  194. /* we got a session id, use it! */
  195. if(!SSL_set_session(conssl->handle, ssl_sessionid)) {
  196. failf(data, "SSL: SSL_set_session failed: %s",
  197. ERR_error_string(SSL_get_error(conssl->handle, 0),NULL));
  198. return CURLE_SSL_CONNECT_ERROR;
  199. }
  200. /* Informational message */
  201. infof (data, "SSL re-using session ID\n");
  202. }
  203. /* pass the raw socket into the SSL layer */
  204. if(!SSL_set_fd(conssl->handle, (int)sockfd)) {
  205. failf(data, "SSL: SSL_set_fd failed");
  206. return CURLE_SSL_CONNECT_ERROR;
  207. }
  208. conssl->connecting_state = ssl_connect_2;
  209. return CURLE_OK;
  210. }
  211. static CURLcode
  212. cyassl_connect_step2(struct connectdata *conn,
  213. int sockindex)
  214. {
  215. int ret = -1;
  216. struct SessionHandle *data = conn->data;
  217. struct ssl_connect_data* conssl = &conn->ssl[sockindex];
  218. infof(data, "CyaSSL: Connecting to %s:%d\n",
  219. conn->host.name, conn->remote_port);
  220. conn->recv[sockindex] = cyassl_recv;
  221. conn->send[sockindex] = cyassl_send;
  222. /* Enable RFC2818 checks */
  223. if(data->set.ssl.verifyhost) {
  224. ret = CyaSSL_check_domain_name(conssl->handle, conn->host.name);
  225. if(ret == SSL_FAILURE)
  226. return CURLE_OUT_OF_MEMORY;
  227. }
  228. ret = SSL_connect(conssl->handle);
  229. if(ret != 1) {
  230. char error_buffer[80];
  231. int detail = SSL_get_error(conssl->handle, ret);
  232. if(SSL_ERROR_WANT_READ == detail) {
  233. conssl->connecting_state = ssl_connect_2_reading;
  234. return CURLE_OK;
  235. }
  236. else if(SSL_ERROR_WANT_WRITE == detail) {
  237. conssl->connecting_state = ssl_connect_2_writing;
  238. return CURLE_OK;
  239. }
  240. /* There is no easy way to override only the CN matching.
  241. * This will enable the override of both mismatching SubjectAltNames
  242. * as also mismatching CN fields */
  243. else if(DOMAIN_NAME_MISMATCH == detail) {
  244. #if 1
  245. failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
  246. conn->host.dispname);
  247. return CURLE_PEER_FAILED_VERIFICATION;
  248. #else
  249. /* When the CyaSSL_check_domain_name() is used and you desire to continue
  250. * on a DOMAIN_NAME_MISMATCH, i.e. 'data->set.ssl.verifyhost == 0',
  251. * CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA error. The only
  252. * way to do this is currently to switch the CyaSSL_check_domain_name()
  253. * in and out based on the 'data->set.ssl.verifyhost' value. */
  254. if(data->set.ssl.verifyhost) {
  255. failf(data,
  256. "\tsubject alt name(s) or common name do not match \"%s\"\n",
  257. conn->host.dispname);
  258. return CURLE_PEER_FAILED_VERIFICATION;
  259. }
  260. else {
  261. infof(data,
  262. "\tsubject alt name(s) and/or common name do not match \"%s\"\n",
  263. conn->host.dispname);
  264. return CURLE_OK;
  265. }
  266. #endif
  267. }
  268. else {
  269. failf(data, "SSL_connect failed with error %d: %s", detail,
  270. ERR_error_string(detail, error_buffer));
  271. return CURLE_SSL_CONNECT_ERROR;
  272. }
  273. }
  274. conssl->connecting_state = ssl_connect_3;
  275. infof(data, "SSL connected\n");
  276. return CURLE_OK;
  277. }
  278. static CURLcode
  279. cyassl_connect_step3(struct connectdata *conn,
  280. int sockindex)
  281. {
  282. CURLcode retcode = CURLE_OK;
  283. void *old_ssl_sessionid=NULL;
  284. struct SessionHandle *data = conn->data;
  285. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  286. int incache;
  287. SSL_SESSION *our_ssl_sessionid;
  288. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  289. our_ssl_sessionid = SSL_get_session(connssl->handle);
  290. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
  291. if(incache) {
  292. if(old_ssl_sessionid != our_ssl_sessionid) {
  293. infof(data, "old SSL session ID is stale, removing\n");
  294. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  295. incache = FALSE;
  296. }
  297. }
  298. if(!incache) {
  299. retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
  300. 0 /* unknown size */);
  301. if(retcode) {
  302. failf(data, "failed to store ssl session");
  303. return retcode;
  304. }
  305. }
  306. connssl->connecting_state = ssl_connect_done;
  307. return retcode;
  308. }
  309. static ssize_t cyassl_send(struct connectdata *conn,
  310. int sockindex,
  311. const void *mem,
  312. size_t len,
  313. CURLcode *curlcode)
  314. {
  315. char error_buffer[80];
  316. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  317. int rc = SSL_write(conn->ssl[sockindex].handle, mem, memlen);
  318. if(rc < 0) {
  319. int err = SSL_get_error(conn->ssl[sockindex].handle, rc);
  320. switch(err) {
  321. case SSL_ERROR_WANT_READ:
  322. case SSL_ERROR_WANT_WRITE:
  323. /* there's data pending, re-invoke SSL_write() */
  324. *curlcode = CURLE_AGAIN;
  325. return -1;
  326. default:
  327. failf(conn->data, "SSL write: %s, errno %d",
  328. ERR_error_string(err, error_buffer),
  329. SOCKERRNO);
  330. *curlcode = CURLE_SEND_ERROR;
  331. return -1;
  332. }
  333. }
  334. return rc;
  335. }
  336. void Curl_cyassl_close_all(struct SessionHandle *data)
  337. {
  338. (void)data;
  339. }
  340. void Curl_cyassl_close(struct connectdata *conn, int sockindex)
  341. {
  342. struct ssl_connect_data *conssl = &conn->ssl[sockindex];
  343. if(conssl->handle) {
  344. (void)SSL_shutdown(conssl->handle);
  345. SSL_free (conssl->handle);
  346. conssl->handle = NULL;
  347. }
  348. if(conssl->ctx) {
  349. SSL_CTX_free (conssl->ctx);
  350. conssl->ctx = NULL;
  351. }
  352. }
  353. static ssize_t cyassl_recv(struct connectdata *conn,
  354. int num,
  355. char *buf,
  356. size_t buffersize,
  357. CURLcode *curlcode)
  358. {
  359. char error_buffer[80];
  360. int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  361. int nread = SSL_read(conn->ssl[num].handle, buf, buffsize);
  362. if(nread < 0) {
  363. int err = SSL_get_error(conn->ssl[num].handle, nread);
  364. switch(err) {
  365. case SSL_ERROR_ZERO_RETURN: /* no more data */
  366. break;
  367. case SSL_ERROR_WANT_READ:
  368. case SSL_ERROR_WANT_WRITE:
  369. /* there's data pending, re-invoke SSL_read() */
  370. *curlcode = CURLE_AGAIN;
  371. return -1;
  372. default:
  373. failf(conn->data, "SSL read: %s, errno %d",
  374. ERR_error_string(err, error_buffer),
  375. SOCKERRNO);
  376. *curlcode = CURLE_RECV_ERROR;
  377. return -1;
  378. }
  379. }
  380. return nread;
  381. }
  382. void Curl_cyassl_session_free(void *ptr)
  383. {
  384. (void)ptr;
  385. /* CyaSSL reuses sessions on own, no free */
  386. }
  387. size_t Curl_cyassl_version(char *buffer, size_t size)
  388. {
  389. #ifdef CYASSL_VERSION
  390. return snprintf(buffer, size, "CyaSSL/%s", CYASSL_VERSION);
  391. #else
  392. return snprintf(buffer, size, "CyaSSL/%s", "<1.8.8");
  393. #endif
  394. }
  395. int Curl_cyassl_init(void)
  396. {
  397. if(CyaSSL_Init() == 0)
  398. return 1;
  399. return -1;
  400. }
  401. bool Curl_cyassl_data_pending(const struct connectdata* conn, int connindex)
  402. {
  403. if(conn->ssl[connindex].handle) /* SSL is in use */
  404. return (0 != SSL_pending(conn->ssl[connindex].handle)) ? TRUE : FALSE;
  405. else
  406. return FALSE;
  407. }
  408. /*
  409. * This function is called to shut down the SSL layer but keep the
  410. * socket open (CCC - Clear Command Channel)
  411. */
  412. int Curl_cyassl_shutdown(struct connectdata *conn, int sockindex)
  413. {
  414. int retval = 0;
  415. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  416. if(connssl->handle) {
  417. SSL_free (connssl->handle);
  418. connssl->handle = NULL;
  419. }
  420. return retval;
  421. }
  422. static CURLcode
  423. cyassl_connect_common(struct connectdata *conn,
  424. int sockindex,
  425. bool nonblocking,
  426. bool *done)
  427. {
  428. CURLcode retcode;
  429. struct SessionHandle *data = conn->data;
  430. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  431. curl_socket_t sockfd = conn->sock[sockindex];
  432. long timeout_ms;
  433. int what;
  434. /* check if the connection has already been established */
  435. if(ssl_connection_complete == connssl->state) {
  436. *done = TRUE;
  437. return CURLE_OK;
  438. }
  439. if(ssl_connect_1==connssl->connecting_state) {
  440. /* Find out how much more time we're allowed */
  441. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  442. if(timeout_ms < 0) {
  443. /* no need to continue if time already is up */
  444. failf(data, "SSL connection timeout");
  445. return CURLE_OPERATION_TIMEDOUT;
  446. }
  447. retcode = cyassl_connect_step1(conn, sockindex);
  448. if(retcode)
  449. return retcode;
  450. }
  451. while(ssl_connect_2 == connssl->connecting_state ||
  452. ssl_connect_2_reading == connssl->connecting_state ||
  453. ssl_connect_2_writing == connssl->connecting_state) {
  454. /* check allowed time left */
  455. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  456. if(timeout_ms < 0) {
  457. /* no need to continue if time already is up */
  458. failf(data, "SSL connection timeout");
  459. return CURLE_OPERATION_TIMEDOUT;
  460. }
  461. /* if ssl is expecting something, check if it's available. */
  462. if(connssl->connecting_state == ssl_connect_2_reading
  463. || connssl->connecting_state == ssl_connect_2_writing) {
  464. curl_socket_t writefd = ssl_connect_2_writing==
  465. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  466. curl_socket_t readfd = ssl_connect_2_reading==
  467. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  468. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  469. if(what < 0) {
  470. /* fatal error */
  471. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  472. return CURLE_SSL_CONNECT_ERROR;
  473. }
  474. else if(0 == what) {
  475. if(nonblocking) {
  476. *done = FALSE;
  477. return CURLE_OK;
  478. }
  479. else {
  480. /* timeout */
  481. failf(data, "SSL connection timeout");
  482. return CURLE_OPERATION_TIMEDOUT;
  483. }
  484. }
  485. /* socket is readable or writable */
  486. }
  487. /* Run transaction, and return to the caller if it failed or if
  488. * this connection is part of a multi handle and this loop would
  489. * execute again. This permits the owner of a multi handle to
  490. * abort a connection attempt before step2 has completed while
  491. * ensuring that a client using select() or epoll() will always
  492. * have a valid fdset to wait on.
  493. */
  494. retcode = cyassl_connect_step2(conn, sockindex);
  495. if(retcode || (nonblocking &&
  496. (ssl_connect_2 == connssl->connecting_state ||
  497. ssl_connect_2_reading == connssl->connecting_state ||
  498. ssl_connect_2_writing == connssl->connecting_state)))
  499. return retcode;
  500. } /* repeat step2 until all transactions are done. */
  501. if(ssl_connect_3==connssl->connecting_state) {
  502. retcode = cyassl_connect_step3(conn, sockindex);
  503. if(retcode)
  504. return retcode;
  505. }
  506. if(ssl_connect_done==connssl->connecting_state) {
  507. connssl->state = ssl_connection_complete;
  508. conn->recv[sockindex] = cyassl_recv;
  509. conn->send[sockindex] = cyassl_send;
  510. *done = TRUE;
  511. }
  512. else
  513. *done = FALSE;
  514. /* Reset our connect state machine */
  515. connssl->connecting_state = ssl_connect_1;
  516. return CURLE_OK;
  517. }
  518. CURLcode
  519. Curl_cyassl_connect_nonblocking(struct connectdata *conn,
  520. int sockindex,
  521. bool *done)
  522. {
  523. return cyassl_connect_common(conn, sockindex, TRUE, done);
  524. }
  525. CURLcode
  526. Curl_cyassl_connect(struct connectdata *conn,
  527. int sockindex)
  528. {
  529. CURLcode retcode;
  530. bool done = FALSE;
  531. retcode = cyassl_connect_common(conn, sockindex, FALSE, &done);
  532. if(retcode)
  533. return retcode;
  534. DEBUGASSERT(done);
  535. return CURLE_OK;
  536. }
  537. #endif