cyassl.c 18 KB

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