polarssl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010, 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  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 PolarSSL-specific code for the TLS/SSL layer. No code
  24. * but sslgen.c should ever call or use these functions.
  25. *
  26. */
  27. #include "setup.h"
  28. #ifdef USE_POLARSSL
  29. #ifdef HAVE_SYS_SOCKET_H
  30. #include <sys/socket.h>
  31. #endif
  32. #include <polarssl/net.h>
  33. #include <polarssl/ssl.h>
  34. #include <polarssl/havege.h>
  35. #include <polarssl/certs.h>
  36. #include <polarssl/x509.h>
  37. #include <polarssl/version.h>
  38. #if POLARSSL_VERSION_NUMBER<0x01000000
  39. /*
  40. Earlier versions of polarssl had no WANT_READ or WANT_WRITE, only TRY_AGAIN
  41. */
  42. #define POLARSSL_ERR_NET_WANT_READ POLARSSL_ERR_NET_TRY_AGAIN
  43. #define POLARSSL_ERR_NET_WANT_WRITE POLARSSL_ERR_NET_TRY_AGAIN
  44. #endif
  45. #include "urldata.h"
  46. #include "sendf.h"
  47. #include "inet_pton.h"
  48. #include "polarssl.h"
  49. #include "sslgen.h"
  50. #include "parsedate.h"
  51. #include "connect.h" /* for the connect timeout */
  52. #include "select.h"
  53. #include "rawstr.h"
  54. #define _MPRINTF_REPLACE /* use our functions only */
  55. #include <curl/mprintf.h>
  56. #include "curl_memory.h"
  57. /* The last #include file should be: */
  58. #include "memdebug.h"
  59. /* Define this to enable lots of debugging for PolarSSL */
  60. #undef POLARSSL_DEBUG
  61. #ifdef POLARSSL_DEBUG
  62. static void polarssl_debug(void *context, int level, char *line)
  63. {
  64. struct SessionHandle *data = NULL;
  65. if(!context)
  66. return;
  67. data = (struct SessionHandle *)context;
  68. infof(data, "%s", line);
  69. }
  70. #else
  71. #endif
  72. static Curl_recv polarssl_recv;
  73. static Curl_send polarssl_send;
  74. /*
  75. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  76. * layer and do all necessary magic.
  77. */
  78. CURLcode
  79. Curl_polarssl_connect(struct connectdata *conn,
  80. int sockindex)
  81. {
  82. struct SessionHandle *data = conn->data;
  83. bool sni = TRUE; /* default is SNI enabled */
  84. int ret = -1;
  85. #ifdef ENABLE_IPV6
  86. struct in6_addr addr;
  87. #else
  88. struct in_addr addr;
  89. #endif
  90. void *old_session = NULL;
  91. size_t old_session_size = 0;
  92. char buffer[1024];
  93. if(conn->ssl[sockindex].state == ssl_connection_complete)
  94. return CURLE_OK;
  95. /* PolarSSL only supports SSLv3 and TLSv1 */
  96. if(data->set.ssl.version == CURL_SSLVERSION_SSLv2) {
  97. failf(data, "PolarSSL does not support SSLv2");
  98. return CURLE_SSL_CONNECT_ERROR;
  99. }
  100. else if(data->set.ssl.version == CURL_SSLVERSION_SSLv3)
  101. sni = FALSE; /* SSLv3 has no SNI */
  102. havege_init(&conn->ssl[sockindex].hs);
  103. /* Load the trusted CA */
  104. memset(&conn->ssl[sockindex].cacert, 0, sizeof(x509_cert));
  105. if(data->set.str[STRING_SSL_CAFILE]) {
  106. ret = x509parse_crtfile(&conn->ssl[sockindex].cacert,
  107. data->set.str[STRING_SSL_CAFILE]);
  108. if(ret) {
  109. failf(data, "Error reading ca cert file %s: -0x%04X",
  110. data->set.str[STRING_SSL_CAFILE], -ret);
  111. if(data->set.ssl.verifypeer)
  112. return CURLE_SSL_CACERT_BADFILE;
  113. }
  114. }
  115. /* Load the client certificate */
  116. memset(&conn->ssl[sockindex].clicert, 0, sizeof(x509_cert));
  117. if(data->set.str[STRING_CERT]) {
  118. ret = x509parse_crtfile(&conn->ssl[sockindex].clicert,
  119. data->set.str[STRING_CERT]);
  120. if(ret) {
  121. failf(data, "Error reading client cert file %s: -0x%04X",
  122. data->set.str[STRING_CERT], -ret);
  123. return CURLE_SSL_CERTPROBLEM;
  124. }
  125. }
  126. /* Load the client private key */
  127. if(data->set.str[STRING_KEY]) {
  128. ret = x509parse_keyfile(&conn->ssl[sockindex].rsa,
  129. data->set.str[STRING_KEY],
  130. data->set.str[STRING_KEY_PASSWD]);
  131. if(ret) {
  132. failf(data, "Error reading private key %s: -0x%04X",
  133. data->set.str[STRING_KEY], -ret);
  134. return CURLE_SSL_CERTPROBLEM;
  135. }
  136. }
  137. /* Load the CRL */
  138. memset(&conn->ssl[sockindex].crl, 0, sizeof(x509_crl));
  139. if(data->set.str[STRING_SSL_CRLFILE]) {
  140. ret = x509parse_crlfile(&conn->ssl[sockindex].crl,
  141. data->set.str[STRING_SSL_CRLFILE]);
  142. if(ret) {
  143. failf(data, "Error reading CRL file %s: -0x%04X",
  144. data->set.str[STRING_SSL_CRLFILE], -ret);
  145. return CURLE_SSL_CRL_BADFILE;
  146. }
  147. }
  148. infof(data, "PolarSSL: Connected to %s:%d\n",
  149. conn->host.name, conn->remote_port);
  150. havege_init(&conn->ssl[sockindex].hs);
  151. if(ssl_init(&conn->ssl[sockindex].ssl)) {
  152. failf(data, "PolarSSL: ssl_init failed");
  153. return CURLE_SSL_CONNECT_ERROR;
  154. }
  155. ssl_set_endpoint(&conn->ssl[sockindex].ssl, SSL_IS_CLIENT);
  156. ssl_set_authmode(&conn->ssl[sockindex].ssl, SSL_VERIFY_OPTIONAL);
  157. ssl_set_rng(&conn->ssl[sockindex].ssl, havege_rand,
  158. &conn->ssl[sockindex].hs);
  159. ssl_set_bio(&conn->ssl[sockindex].ssl,
  160. net_recv, &conn->sock[sockindex],
  161. net_send, &conn->sock[sockindex]);
  162. #if POLARSSL_VERSION_NUMBER<0x01000000
  163. ssl_set_ciphers(&conn->ssl[sockindex].ssl, ssl_default_ciphers);
  164. #else
  165. ssl_set_ciphersuites(&conn->ssl[sockindex].ssl, ssl_default_ciphersuites);
  166. #endif
  167. if(!Curl_ssl_getsessionid(conn, &old_session, &old_session_size)) {
  168. memcpy(&conn->ssl[sockindex].ssn, old_session, old_session_size);
  169. infof(data, "PolarSSL re-using session\n");
  170. }
  171. ssl_set_session(&conn->ssl[sockindex].ssl, 1, 600,
  172. &conn->ssl[sockindex].ssn);
  173. ssl_set_ca_chain(&conn->ssl[sockindex].ssl,
  174. &conn->ssl[sockindex].cacert,
  175. &conn->ssl[sockindex].crl,
  176. conn->host.name);
  177. ssl_set_own_cert(&conn->ssl[sockindex].ssl,
  178. &conn->ssl[sockindex].clicert, &conn->ssl[sockindex].rsa);
  179. if(!Curl_inet_pton(AF_INET, conn->host.name, &addr) &&
  180. #ifdef ENABLE_IPV6
  181. !Curl_inet_pton(AF_INET6, conn->host.name, &addr) &&
  182. #endif
  183. sni && ssl_set_hostname(&conn->ssl[sockindex].ssl, conn->host.name)) {
  184. infof(data, "WARNING: failed to configure "
  185. "server name indication (SNI) TLS extension\n");
  186. }
  187. infof(data, "PolarSSL: performing SSL/TLS handshake...\n");
  188. #ifdef POLARSSL_DEBUG
  189. ssl_set_dbg(&conn->ssl[sockindex].ssl, polarssl_debug, data);
  190. #endif
  191. for(;;) {
  192. if(!(ret = ssl_handshake(&conn->ssl[sockindex].ssl)))
  193. break;
  194. else if(ret != POLARSSL_ERR_NET_WANT_READ &&
  195. ret != POLARSSL_ERR_NET_WANT_WRITE) {
  196. failf(data, "ssl_handshake returned -0x%04X", -ret);
  197. return CURLE_SSL_CONNECT_ERROR;
  198. }
  199. else {
  200. /* wait for data from server... */
  201. long timeout_ms = Curl_timeleft(data, NULL, TRUE);
  202. if(timeout_ms < 0) {
  203. failf(data, "SSL connection timeout");
  204. return CURLE_OPERATION_TIMEDOUT;
  205. }
  206. switch(Curl_socket_ready(conn->sock[sockindex],
  207. CURL_SOCKET_BAD, timeout_ms)) {
  208. case 0:
  209. failf(data, "SSL handshake timeout");
  210. return CURLE_OPERATION_TIMEDOUT;
  211. break;
  212. case CURL_CSELECT_IN:
  213. continue;
  214. break;
  215. default:
  216. return CURLE_SSL_CONNECT_ERROR;
  217. break;
  218. }
  219. }
  220. }
  221. infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
  222. #if POLARSSL_VERSION_NUMBER<0x01000000
  223. ssl_get_cipher(&conn->ssl[sockindex].ssl));
  224. #else
  225. ssl_get_ciphersuite_name(&conn->ssl[sockindex].ssl));
  226. #endif
  227. ret = ssl_get_verify_result(&conn->ssl[sockindex].ssl);
  228. if(ret && data->set.ssl.verifypeer) {
  229. if(ret & BADCERT_EXPIRED)
  230. failf(data, "Cert verify failed: BADCERT_EXPIRED\n");
  231. if(ret & BADCERT_REVOKED)
  232. failf(data, "Cert verify failed: BADCERT_REVOKED");
  233. if(ret & BADCERT_CN_MISMATCH)
  234. failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
  235. if(ret & BADCERT_NOT_TRUSTED)
  236. failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
  237. return CURLE_SSL_CACERT;
  238. }
  239. if(conn->ssl[sockindex].ssl.peer_cert) {
  240. /* If the session was resumed, there will be no peer certs */
  241. memset(buffer, 0, sizeof(buffer));
  242. if(x509parse_cert_info(buffer, sizeof(buffer), (char *)"* ",
  243. conn->ssl[sockindex].ssl.peer_cert) != -1)
  244. infof(data, "Dumping cert info:\n%s\n", buffer);
  245. }
  246. conn->ssl[sockindex].state = ssl_connection_complete;
  247. conn->recv[sockindex] = polarssl_recv;
  248. conn->send[sockindex] = polarssl_send;
  249. /* Save the current session data for possible re-use */
  250. {
  251. void *new_session = malloc(sizeof(conn->ssl[sockindex].ssn));
  252. if(new_session) {
  253. memcpy(new_session, &conn->ssl[sockindex].ssn,
  254. sizeof(conn->ssl[sockindex].ssn));
  255. if(old_session)
  256. Curl_ssl_delsessionid(conn, old_session);
  257. return Curl_ssl_addsessionid(conn, new_session,
  258. sizeof(conn->ssl[sockindex].ssn));
  259. }
  260. }
  261. return CURLE_OK;
  262. }
  263. static ssize_t polarssl_send(struct connectdata *conn,
  264. int sockindex,
  265. const void *mem,
  266. size_t len,
  267. CURLcode *curlcode)
  268. {
  269. int ret = -1;
  270. ret = ssl_write(&conn->ssl[sockindex].ssl,
  271. (unsigned char *)mem, len);
  272. if(ret < 0) {
  273. *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
  274. CURLE_AGAIN : CURLE_SEND_ERROR;
  275. ret = -1;
  276. }
  277. return ret;
  278. }
  279. void Curl_polarssl_close_all(struct SessionHandle *data)
  280. {
  281. (void)data;
  282. }
  283. void Curl_polarssl_close(struct connectdata *conn, int sockindex)
  284. {
  285. rsa_free(&conn->ssl[sockindex].rsa);
  286. x509_free(&conn->ssl[sockindex].clicert);
  287. x509_free(&conn->ssl[sockindex].cacert);
  288. x509_crl_free(&conn->ssl[sockindex].crl);
  289. ssl_free(&conn->ssl[sockindex].ssl);
  290. }
  291. static ssize_t polarssl_recv(struct connectdata *conn,
  292. int num,
  293. char *buf,
  294. size_t buffersize,
  295. CURLcode *curlcode)
  296. {
  297. int ret = -1;
  298. ssize_t len = -1;
  299. memset(buf, 0, buffersize);
  300. ret = ssl_read(&conn->ssl[num].ssl, (unsigned char *)buf, buffersize);
  301. if(ret <= 0) {
  302. if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
  303. return 0;
  304. *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
  305. CURLE_AGAIN : CURLE_RECV_ERROR;
  306. return -1;
  307. }
  308. len = ret;
  309. return len;
  310. }
  311. void Curl_polarssl_session_free(void *ptr)
  312. {
  313. free(ptr);
  314. }
  315. size_t Curl_polarssl_version(char *buffer, size_t size)
  316. {
  317. return snprintf(buffer, size, "PolarSSL");
  318. }
  319. #endif