polarssl.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
  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 https://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. #include <polarssl/sha256.h>
  36. #if POLARSSL_VERSION_NUMBER < 0x01030000
  37. #error too old PolarSSL
  38. #endif
  39. #include <polarssl/error.h>
  40. #include <polarssl/entropy.h>
  41. #include <polarssl/ctr_drbg.h>
  42. #include "urldata.h"
  43. #include "sendf.h"
  44. #include "inet_pton.h"
  45. #include "polarssl.h"
  46. #include "vtls.h"
  47. #include "parsedate.h"
  48. #include "connect.h" /* for the connect timeout */
  49. #include "select.h"
  50. #include "strcase.h"
  51. #include "polarssl_threadlock.h"
  52. #include "multiif.h"
  53. #include "curl_printf.h"
  54. #include "curl_memory.h"
  55. /* The last #include file should be: */
  56. #include "memdebug.h"
  57. /* See https://tls.mbed.org/discussions/generic/
  58. howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der
  59. */
  60. #define RSA_PUB_DER_MAX_BYTES (38 + 2 * POLARSSL_MPI_MAX_SIZE)
  61. #define ECP_PUB_DER_MAX_BYTES (30 + 2 * POLARSSL_ECP_MAX_BYTES)
  62. #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
  63. RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
  64. struct ssl_backend_data {
  65. ctr_drbg_context ctr_drbg;
  66. entropy_context entropy;
  67. ssl_context ssl;
  68. int server_fd;
  69. x509_crt cacert;
  70. x509_crt clicert;
  71. x509_crl crl;
  72. rsa_context rsa;
  73. };
  74. #define BACKEND connssl->backend
  75. /* apply threading? */
  76. #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
  77. #define THREADING_SUPPORT
  78. #endif
  79. #ifndef POLARSSL_ERROR_C
  80. #define error_strerror(x,y,z)
  81. #endif /* POLARSSL_ERROR_C */
  82. #if defined(THREADING_SUPPORT)
  83. static entropy_context entropy;
  84. static int entropy_init_initialized = 0;
  85. /* start of entropy_init_mutex() */
  86. static void entropy_init_mutex(entropy_context *ctx)
  87. {
  88. /* lock 0 = entropy_init_mutex() */
  89. Curl_polarsslthreadlock_lock_function(0);
  90. if(entropy_init_initialized == 0) {
  91. entropy_init(ctx);
  92. entropy_init_initialized = 1;
  93. }
  94. Curl_polarsslthreadlock_unlock_function(0);
  95. }
  96. /* end of entropy_init_mutex() */
  97. /* start of entropy_func_mutex() */
  98. static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
  99. {
  100. int ret;
  101. /* lock 1 = entropy_func_mutex() */
  102. Curl_polarsslthreadlock_lock_function(1);
  103. ret = entropy_func(data, output, len);
  104. Curl_polarsslthreadlock_unlock_function(1);
  105. return ret;
  106. }
  107. /* end of entropy_func_mutex() */
  108. #endif /* THREADING_SUPPORT */
  109. /* Define this to enable lots of debugging for PolarSSL */
  110. #undef POLARSSL_DEBUG
  111. #ifdef POLARSSL_DEBUG
  112. static void polarssl_debug(void *context, int level, const char *line)
  113. {
  114. struct Curl_easy *data = NULL;
  115. if(!context)
  116. return;
  117. data = (struct Curl_easy *)context;
  118. infof(data, "%s", line);
  119. (void) level;
  120. }
  121. #else
  122. #endif
  123. /* ALPN for http2? */
  124. #ifdef POLARSSL_SSL_ALPN
  125. # define HAS_ALPN
  126. #endif
  127. static Curl_recv polarssl_recv;
  128. static Curl_send polarssl_send;
  129. static CURLcode polarssl_version_from_curl(int *polarver, long ssl_version)
  130. {
  131. switch(ssl_version) {
  132. case CURL_SSLVERSION_TLSv1_0:
  133. *polarver = SSL_MINOR_VERSION_1;
  134. return CURLE_OK;
  135. case CURL_SSLVERSION_TLSv1_1:
  136. *polarver = SSL_MINOR_VERSION_2;
  137. return CURLE_OK;
  138. case CURL_SSLVERSION_TLSv1_2:
  139. *polarver = SSL_MINOR_VERSION_3;
  140. return CURLE_OK;
  141. case CURL_SSLVERSION_TLSv1_3:
  142. break;
  143. }
  144. return CURLE_SSL_CONNECT_ERROR;
  145. }
  146. static CURLcode
  147. set_ssl_version_min_max(struct connectdata *conn, int sockindex)
  148. {
  149. struct Curl_easy *data = conn->data;
  150. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  151. long ssl_version = SSL_CONN_CONFIG(version);
  152. long ssl_version_max = SSL_CONN_CONFIG(version_max);
  153. int ssl_min_ver = SSL_MINOR_VERSION_1;
  154. int ssl_max_ver = SSL_MINOR_VERSION_1;
  155. CURLcode result = CURLE_OK;
  156. switch(ssl_version) {
  157. case CURL_SSLVERSION_DEFAULT:
  158. case CURL_SSLVERSION_TLSv1:
  159. ssl_version = CURL_SSLVERSION_TLSv1_0;
  160. break;
  161. }
  162. switch(ssl_version_max) {
  163. case CURL_SSLVERSION_MAX_NONE:
  164. case CURL_SSLVERSION_MAX_DEFAULT:
  165. ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
  166. break;
  167. }
  168. result = polarssl_version_from_curl(&ssl_min_ver, ssl_version);
  169. if(result) {
  170. failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
  171. return result;
  172. }
  173. result = polarssl_version_from_curl(&ssl_max_ver, ssl_version_max >> 16);
  174. if(result) {
  175. failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
  176. return result;
  177. }
  178. ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_min_ver);
  179. ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_max_ver);
  180. return result;
  181. }
  182. static CURLcode
  183. polarssl_connect_step1(struct connectdata *conn,
  184. int sockindex)
  185. {
  186. struct Curl_easy *data = conn->data;
  187. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  188. const char *capath = SSL_CONN_CONFIG(CApath);
  189. const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
  190. conn->host.name;
  191. const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
  192. int ret = -1;
  193. char errorbuf[128];
  194. errorbuf[0] = 0;
  195. /* PolarSSL only supports SSLv3 and TLSv1 */
  196. if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
  197. failf(data, "PolarSSL does not support SSLv2");
  198. return CURLE_SSL_CONNECT_ERROR;
  199. }
  200. #ifdef THREADING_SUPPORT
  201. entropy_init_mutex(&entropy);
  202. if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func_mutex, &entropy,
  203. NULL, 0)) != 0) {
  204. error_strerror(ret, errorbuf, sizeof(errorbuf));
  205. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  206. -ret, errorbuf);
  207. }
  208. #else
  209. entropy_init(&BACKEND->entropy);
  210. if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func, &BACKEND->entropy,
  211. NULL, 0)) != 0) {
  212. error_strerror(ret, errorbuf, sizeof(errorbuf));
  213. failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
  214. -ret, errorbuf);
  215. }
  216. #endif /* THREADING_SUPPORT */
  217. /* Load the trusted CA */
  218. memset(&BACKEND->cacert, 0, sizeof(x509_crt));
  219. if(SSL_CONN_CONFIG(CAfile)) {
  220. ret = x509_crt_parse_file(&BACKEND->cacert,
  221. SSL_CONN_CONFIG(CAfile));
  222. if(ret<0) {
  223. error_strerror(ret, errorbuf, sizeof(errorbuf));
  224. failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s",
  225. SSL_CONN_CONFIG(CAfile), -ret, errorbuf);
  226. if(SSL_CONN_CONFIG(verifypeer))
  227. return CURLE_SSL_CACERT_BADFILE;
  228. }
  229. }
  230. if(capath) {
  231. ret = x509_crt_parse_path(&BACKEND->cacert, capath);
  232. if(ret<0) {
  233. error_strerror(ret, errorbuf, sizeof(errorbuf));
  234. failf(data, "Error reading ca cert path %s - PolarSSL: (-0x%04X) %s",
  235. capath, -ret, errorbuf);
  236. if(SSL_CONN_CONFIG(verifypeer))
  237. return CURLE_SSL_CACERT_BADFILE;
  238. }
  239. }
  240. /* Load the client certificate */
  241. memset(&BACKEND->clicert, 0, sizeof(x509_crt));
  242. if(SSL_SET_OPTION(cert)) {
  243. ret = x509_crt_parse_file(&BACKEND->clicert,
  244. SSL_SET_OPTION(cert));
  245. if(ret) {
  246. error_strerror(ret, errorbuf, sizeof(errorbuf));
  247. failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
  248. SSL_SET_OPTION(cert), -ret, errorbuf);
  249. return CURLE_SSL_CERTPROBLEM;
  250. }
  251. }
  252. /* Load the client private key */
  253. if(SSL_SET_OPTION(key)) {
  254. pk_context pk;
  255. pk_init(&pk);
  256. ret = pk_parse_keyfile(&pk, SSL_SET_OPTION(key),
  257. SSL_SET_OPTION(key_passwd));
  258. if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA))
  259. ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
  260. if(ret == 0)
  261. rsa_copy(&BACKEND->rsa, pk_rsa(pk));
  262. else
  263. rsa_free(&BACKEND->rsa);
  264. pk_free(&pk);
  265. if(ret) {
  266. error_strerror(ret, errorbuf, sizeof(errorbuf));
  267. failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
  268. SSL_SET_OPTION(key), -ret, errorbuf);
  269. return CURLE_SSL_CERTPROBLEM;
  270. }
  271. }
  272. /* Load the CRL */
  273. memset(&BACKEND->crl, 0, sizeof(x509_crl));
  274. if(SSL_SET_OPTION(CRLfile)) {
  275. ret = x509_crl_parse_file(&BACKEND->crl,
  276. SSL_SET_OPTION(CRLfile));
  277. if(ret) {
  278. error_strerror(ret, errorbuf, sizeof(errorbuf));
  279. failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
  280. SSL_SET_OPTION(CRLfile), -ret, errorbuf);
  281. return CURLE_SSL_CRL_BADFILE;
  282. }
  283. }
  284. infof(data, "PolarSSL: Connecting to %s:%d\n", hostname, port);
  285. if(ssl_init(&BACKEND->ssl)) {
  286. failf(data, "PolarSSL: ssl_init failed");
  287. return CURLE_SSL_CONNECT_ERROR;
  288. }
  289. switch(SSL_CONN_CONFIG(version)) {
  290. case CURL_SSLVERSION_DEFAULT:
  291. case CURL_SSLVERSION_TLSv1:
  292. ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
  293. SSL_MINOR_VERSION_1);
  294. break;
  295. case CURL_SSLVERSION_SSLv3:
  296. ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
  297. SSL_MINOR_VERSION_0);
  298. ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
  299. SSL_MINOR_VERSION_0);
  300. infof(data, "PolarSSL: Forced min. SSL Version to be SSLv3\n");
  301. break;
  302. case CURL_SSLVERSION_TLSv1_0:
  303. case CURL_SSLVERSION_TLSv1_1:
  304. case CURL_SSLVERSION_TLSv1_2:
  305. case CURL_SSLVERSION_TLSv1_3:
  306. {
  307. CURLcode result = set_ssl_version_min_max(conn, sockindex);
  308. if(result != CURLE_OK)
  309. return result;
  310. break;
  311. }
  312. default:
  313. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  314. return CURLE_SSL_CONNECT_ERROR;
  315. }
  316. ssl_set_endpoint(&BACKEND->ssl, SSL_IS_CLIENT);
  317. ssl_set_authmode(&BACKEND->ssl, SSL_VERIFY_OPTIONAL);
  318. ssl_set_rng(&BACKEND->ssl, ctr_drbg_random,
  319. &BACKEND->ctr_drbg);
  320. ssl_set_bio(&BACKEND->ssl,
  321. net_recv, &conn->sock[sockindex],
  322. net_send, &conn->sock[sockindex]);
  323. ssl_set_ciphersuites(&BACKEND->ssl, ssl_list_ciphersuites());
  324. /* Check if there's a cached ID we can/should use here! */
  325. if(SSL_SET_OPTION(primary.sessionid)) {
  326. void *old_session = NULL;
  327. Curl_ssl_sessionid_lock(conn);
  328. if(!Curl_ssl_getsessionid(conn, &old_session, NULL, sockindex)) {
  329. ret = ssl_set_session(&BACKEND->ssl, old_session);
  330. if(ret) {
  331. Curl_ssl_sessionid_unlock(conn);
  332. failf(data, "ssl_set_session returned -0x%x", -ret);
  333. return CURLE_SSL_CONNECT_ERROR;
  334. }
  335. infof(data, "PolarSSL re-using session\n");
  336. }
  337. Curl_ssl_sessionid_unlock(conn);
  338. }
  339. ssl_set_ca_chain(&BACKEND->ssl,
  340. &BACKEND->cacert,
  341. &BACKEND->crl,
  342. hostname);
  343. ssl_set_own_cert_rsa(&BACKEND->ssl,
  344. &BACKEND->clicert, &BACKEND->rsa);
  345. if(ssl_set_hostname(&BACKEND->ssl, hostname)) {
  346. /* ssl_set_hostname() sets the name to use in CN/SAN checks *and* the name
  347. to set in the SNI extension. So even if curl connects to a host
  348. specified as an IP address, this function must be used. */
  349. failf(data, "couldn't set hostname in PolarSSL");
  350. return CURLE_SSL_CONNECT_ERROR;
  351. }
  352. #ifdef HAS_ALPN
  353. if(conn->bits.tls_enable_alpn) {
  354. static const char *protocols[3];
  355. int cur = 0;
  356. #ifdef USE_NGHTTP2
  357. if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
  358. protocols[cur++] = NGHTTP2_PROTO_VERSION_ID;
  359. infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
  360. }
  361. #endif
  362. protocols[cur++] = ALPN_HTTP_1_1;
  363. infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
  364. protocols[cur] = NULL;
  365. ssl_set_alpn_protocols(&BACKEND->ssl, protocols);
  366. }
  367. #endif
  368. #ifdef POLARSSL_DEBUG
  369. ssl_set_dbg(&BACKEND->ssl, polarssl_debug, data);
  370. #endif
  371. connssl->connecting_state = ssl_connect_2;
  372. return CURLE_OK;
  373. }
  374. static CURLcode
  375. polarssl_connect_step2(struct connectdata *conn,
  376. int sockindex)
  377. {
  378. int ret;
  379. struct Curl_easy *data = conn->data;
  380. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  381. char buffer[1024];
  382. const char * const pinnedpubkey = SSL_IS_PROXY() ?
  383. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
  384. data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
  385. char errorbuf[128];
  386. errorbuf[0] = 0;
  387. conn->recv[sockindex] = polarssl_recv;
  388. conn->send[sockindex] = polarssl_send;
  389. ret = ssl_handshake(&BACKEND->ssl);
  390. switch(ret) {
  391. case 0:
  392. break;
  393. case POLARSSL_ERR_NET_WANT_READ:
  394. connssl->connecting_state = ssl_connect_2_reading;
  395. return CURLE_OK;
  396. case POLARSSL_ERR_NET_WANT_WRITE:
  397. connssl->connecting_state = ssl_connect_2_writing;
  398. return CURLE_OK;
  399. default:
  400. error_strerror(ret, errorbuf, sizeof(errorbuf));
  401. failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
  402. -ret, errorbuf);
  403. return CURLE_SSL_CONNECT_ERROR;
  404. }
  405. infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
  406. ssl_get_ciphersuite(&BACKEND->ssl) );
  407. ret = ssl_get_verify_result(&BACKEND->ssl);
  408. if(ret && SSL_CONN_CONFIG(verifypeer)) {
  409. if(ret & BADCERT_EXPIRED)
  410. failf(data, "Cert verify failed: BADCERT_EXPIRED");
  411. if(ret & BADCERT_REVOKED) {
  412. failf(data, "Cert verify failed: BADCERT_REVOKED");
  413. return CURLE_PEER_FAILED_VERIFICATION;
  414. }
  415. if(ret & BADCERT_CN_MISMATCH)
  416. failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
  417. if(ret & BADCERT_NOT_TRUSTED)
  418. failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
  419. return CURLE_PEER_FAILED_VERIFICATION;
  420. }
  421. if(ssl_get_peer_cert(&(BACKEND->ssl))) {
  422. /* If the session was resumed, there will be no peer certs */
  423. memset(buffer, 0, sizeof(buffer));
  424. if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ",
  425. ssl_get_peer_cert(&(BACKEND->ssl))) != -1)
  426. infof(data, "Dumping cert info:\n%s\n", buffer);
  427. }
  428. /* adapted from mbedtls.c */
  429. if(pinnedpubkey) {
  430. int size;
  431. CURLcode result;
  432. x509_crt *p;
  433. unsigned char pubkey[PUB_DER_MAX_BYTES];
  434. const x509_crt *peercert;
  435. peercert = ssl_get_peer_cert(&BACKEND->ssl);
  436. if(!peercert || !peercert->raw.p || !peercert->raw.len) {
  437. failf(data, "Failed due to missing peer certificate");
  438. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  439. }
  440. p = calloc(1, sizeof(*p));
  441. if(!p)
  442. return CURLE_OUT_OF_MEMORY;
  443. x509_crt_init(p);
  444. /* Make a copy of our const peercert because pk_write_pubkey_der
  445. needs a non-const key, for now.
  446. https://github.com/ARMmbed/mbedtls/issues/396 */
  447. if(x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
  448. failf(data, "Failed copying peer certificate");
  449. x509_crt_free(p);
  450. free(p);
  451. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  452. }
  453. size = pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
  454. if(size <= 0) {
  455. failf(data, "Failed copying public key from peer certificate");
  456. x509_crt_free(p);
  457. free(p);
  458. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  459. }
  460. /* pk_write_pubkey_der writes data at the end of the buffer. */
  461. result = Curl_pin_peer_pubkey(data,
  462. pinnedpubkey,
  463. &pubkey[PUB_DER_MAX_BYTES - size], size);
  464. if(result) {
  465. x509_crt_free(p);
  466. free(p);
  467. return result;
  468. }
  469. x509_crt_free(p);
  470. free(p);
  471. }
  472. #ifdef HAS_ALPN
  473. if(conn->bits.tls_enable_alpn) {
  474. const char *next_protocol = ssl_get_alpn_protocol(&BACKEND->ssl);
  475. if(next_protocol != NULL) {
  476. infof(data, "ALPN, server accepted to use %s\n", next_protocol);
  477. #ifdef USE_NGHTTP2
  478. if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
  479. NGHTTP2_PROTO_VERSION_ID_LEN)) {
  480. conn->negnpn = CURL_HTTP_VERSION_2;
  481. }
  482. else
  483. #endif
  484. if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) {
  485. conn->negnpn = CURL_HTTP_VERSION_1_1;
  486. }
  487. }
  488. else
  489. infof(data, "ALPN, server did not agree to a protocol\n");
  490. Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ?
  491. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  492. }
  493. #endif
  494. connssl->connecting_state = ssl_connect_3;
  495. infof(data, "SSL connected\n");
  496. return CURLE_OK;
  497. }
  498. static CURLcode
  499. polarssl_connect_step3(struct connectdata *conn,
  500. int sockindex)
  501. {
  502. CURLcode retcode = CURLE_OK;
  503. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  504. struct Curl_easy *data = conn->data;
  505. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  506. if(SSL_SET_OPTION(primary.sessionid)) {
  507. int ret;
  508. ssl_session *our_ssl_sessionid;
  509. void *old_ssl_sessionid = NULL;
  510. our_ssl_sessionid = calloc(1, sizeof(ssl_session));
  511. if(!our_ssl_sessionid)
  512. return CURLE_OUT_OF_MEMORY;
  513. ret = ssl_get_session(&BACKEND->ssl, our_ssl_sessionid);
  514. if(ret) {
  515. failf(data, "ssl_get_session returned -0x%x", -ret);
  516. return CURLE_SSL_CONNECT_ERROR;
  517. }
  518. /* If there's already a matching session in the cache, delete it */
  519. Curl_ssl_sessionid_lock(conn);
  520. if(!Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, sockindex))
  521. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  522. retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0, sockindex);
  523. Curl_ssl_sessionid_unlock(conn);
  524. if(retcode) {
  525. free(our_ssl_sessionid);
  526. failf(data, "failed to store ssl session");
  527. return retcode;
  528. }
  529. }
  530. connssl->connecting_state = ssl_connect_done;
  531. return CURLE_OK;
  532. }
  533. static ssize_t polarssl_send(struct connectdata *conn,
  534. int sockindex,
  535. const void *mem,
  536. size_t len,
  537. CURLcode *curlcode)
  538. {
  539. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  540. int ret = -1;
  541. ret = ssl_write(&BACKEND->ssl,
  542. (unsigned char *)mem, len);
  543. if(ret < 0) {
  544. *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
  545. CURLE_AGAIN : CURLE_SEND_ERROR;
  546. ret = -1;
  547. }
  548. return ret;
  549. }
  550. static void Curl_polarssl_close(struct connectdata *conn, int sockindex)
  551. {
  552. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  553. rsa_free(&BACKEND->rsa);
  554. x509_crt_free(&BACKEND->clicert);
  555. x509_crt_free(&BACKEND->cacert);
  556. x509_crl_free(&BACKEND->crl);
  557. ssl_free(&BACKEND->ssl);
  558. }
  559. static ssize_t polarssl_recv(struct connectdata *conn,
  560. int num,
  561. char *buf,
  562. size_t buffersize,
  563. CURLcode *curlcode)
  564. {
  565. struct ssl_connect_data *connssl = &conn->ssl[num];
  566. int ret = -1;
  567. ssize_t len = -1;
  568. memset(buf, 0, buffersize);
  569. ret = ssl_read(&BACKEND->ssl, (unsigned char *)buf, buffersize);
  570. if(ret <= 0) {
  571. if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
  572. return 0;
  573. *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
  574. CURLE_AGAIN : CURLE_RECV_ERROR;
  575. return -1;
  576. }
  577. len = ret;
  578. return len;
  579. }
  580. static void Curl_polarssl_session_free(void *ptr)
  581. {
  582. ssl_session_free(ptr);
  583. free(ptr);
  584. }
  585. /* 1.3.10 was the first rebranded version. All new releases (in 1.3 branch and
  586. higher) will be mbed TLS branded.. */
  587. static size_t Curl_polarssl_version(char *buffer, size_t size)
  588. {
  589. unsigned int version = version_get_number();
  590. return msnprintf(buffer, size, "%s/%d.%d.%d",
  591. version >= 0x01030A00?"mbedTLS":"PolarSSL",
  592. version>>24, (version>>16)&0xff, (version>>8)&0xff);
  593. }
  594. static CURLcode
  595. polarssl_connect_common(struct connectdata *conn,
  596. int sockindex,
  597. bool nonblocking,
  598. bool *done)
  599. {
  600. CURLcode result;
  601. struct Curl_easy *data = conn->data;
  602. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  603. curl_socket_t sockfd = conn->sock[sockindex];
  604. long timeout_ms;
  605. int what;
  606. /* check if the connection has already been established */
  607. if(ssl_connection_complete == connssl->state) {
  608. *done = TRUE;
  609. return CURLE_OK;
  610. }
  611. if(ssl_connect_1 == connssl->connecting_state) {
  612. /* Find out how much more time we're allowed */
  613. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  614. if(timeout_ms < 0) {
  615. /* no need to continue if time already is up */
  616. failf(data, "SSL connection timeout");
  617. return CURLE_OPERATION_TIMEDOUT;
  618. }
  619. result = polarssl_connect_step1(conn, sockindex);
  620. if(result)
  621. return result;
  622. }
  623. while(ssl_connect_2 == connssl->connecting_state ||
  624. ssl_connect_2_reading == connssl->connecting_state ||
  625. ssl_connect_2_writing == connssl->connecting_state) {
  626. /* check allowed time left */
  627. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  628. if(timeout_ms < 0) {
  629. /* no need to continue if time already is up */
  630. failf(data, "SSL connection timeout");
  631. return CURLE_OPERATION_TIMEDOUT;
  632. }
  633. /* if ssl is expecting something, check if it's available. */
  634. if(connssl->connecting_state == ssl_connect_2_reading ||
  635. connssl->connecting_state == ssl_connect_2_writing) {
  636. curl_socket_t writefd = ssl_connect_2_writing ==
  637. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  638. curl_socket_t readfd = ssl_connect_2_reading ==
  639. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  640. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  641. nonblocking?0:timeout_ms);
  642. if(what < 0) {
  643. /* fatal error */
  644. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  645. return CURLE_SSL_CONNECT_ERROR;
  646. }
  647. else if(0 == what) {
  648. if(nonblocking) {
  649. *done = FALSE;
  650. return CURLE_OK;
  651. }
  652. else {
  653. /* timeout */
  654. failf(data, "SSL connection timeout");
  655. return CURLE_OPERATION_TIMEDOUT;
  656. }
  657. }
  658. /* socket is readable or writable */
  659. }
  660. /* Run transaction, and return to the caller if it failed or if
  661. * this connection is part of a multi handle and this loop would
  662. * execute again. This permits the owner of a multi handle to
  663. * abort a connection attempt before step2 has completed while
  664. * ensuring that a client using select() or epoll() will always
  665. * have a valid fdset to wait on.
  666. */
  667. result = polarssl_connect_step2(conn, sockindex);
  668. if(result || (nonblocking &&
  669. (ssl_connect_2 == connssl->connecting_state ||
  670. ssl_connect_2_reading == connssl->connecting_state ||
  671. ssl_connect_2_writing == connssl->connecting_state)))
  672. return result;
  673. } /* repeat step2 until all transactions are done. */
  674. if(ssl_connect_3 == connssl->connecting_state) {
  675. result = polarssl_connect_step3(conn, sockindex);
  676. if(result)
  677. return result;
  678. }
  679. if(ssl_connect_done == connssl->connecting_state) {
  680. connssl->state = ssl_connection_complete;
  681. conn->recv[sockindex] = polarssl_recv;
  682. conn->send[sockindex] = polarssl_send;
  683. *done = TRUE;
  684. }
  685. else
  686. *done = FALSE;
  687. /* Reset our connect state machine */
  688. connssl->connecting_state = ssl_connect_1;
  689. return CURLE_OK;
  690. }
  691. static CURLcode Curl_polarssl_connect_nonblocking(struct connectdata *conn,
  692. int sockindex, bool *done)
  693. {
  694. return polarssl_connect_common(conn, sockindex, TRUE, done);
  695. }
  696. static CURLcode Curl_polarssl_connect(struct connectdata *conn, int sockindex)
  697. {
  698. CURLcode result;
  699. bool done = FALSE;
  700. result = polarssl_connect_common(conn, sockindex, FALSE, &done);
  701. if(result)
  702. return result;
  703. DEBUGASSERT(done);
  704. return CURLE_OK;
  705. }
  706. /*
  707. * return 0 error initializing SSL
  708. * return 1 SSL initialized successfully
  709. */
  710. static int Curl_polarssl_init(void)
  711. {
  712. return Curl_polarsslthreadlock_thread_setup();
  713. }
  714. static void Curl_polarssl_cleanup(void)
  715. {
  716. (void)Curl_polarsslthreadlock_thread_cleanup();
  717. }
  718. static bool Curl_polarssl_data_pending(const struct connectdata *conn,
  719. int sockindex)
  720. {
  721. const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  722. return ssl_get_bytes_avail(&BACKEND->ssl) != 0;
  723. }
  724. static CURLcode Curl_polarssl_sha256sum(const unsigned char *input,
  725. size_t inputlen,
  726. unsigned char *sha256sum,
  727. size_t sha256len UNUSED_PARAM)
  728. {
  729. (void)sha256len;
  730. sha256(input, inputlen, sha256sum, 0);
  731. return CURLE_OK;
  732. }
  733. static void *Curl_polarssl_get_internals(struct ssl_connect_data *connssl,
  734. CURLINFO info UNUSED_PARAM)
  735. {
  736. (void)info;
  737. return &BACKEND->ssl;
  738. }
  739. const struct Curl_ssl Curl_ssl_polarssl = {
  740. { CURLSSLBACKEND_POLARSSL, "polarssl" }, /* info */
  741. SSLSUPP_CA_PATH |
  742. SSLSUPP_PINNEDPUBKEY,
  743. sizeof(struct ssl_backend_data),
  744. Curl_polarssl_init, /* init */
  745. Curl_polarssl_cleanup, /* cleanup */
  746. Curl_polarssl_version, /* version */
  747. Curl_none_check_cxn, /* check_cxn */
  748. Curl_none_shutdown, /* shutdown */
  749. Curl_polarssl_data_pending, /* data_pending */
  750. /* This might cause libcurl to use a weeker random! */
  751. Curl_none_random, /* random */
  752. Curl_none_cert_status_request, /* cert_status_request */
  753. Curl_polarssl_connect, /* connect */
  754. Curl_polarssl_connect_nonblocking, /* connect_nonblocking */
  755. Curl_polarssl_get_internals, /* get_internals */
  756. Curl_polarssl_close, /* close_one */
  757. Curl_none_close_all, /* close_all */
  758. Curl_polarssl_session_free, /* session_free */
  759. Curl_none_set_engine, /* set_engine */
  760. Curl_none_set_engine_default, /* set_engine_default */
  761. Curl_none_engines_list, /* engines_list */
  762. Curl_none_false_start, /* false_start */
  763. Curl_none_md5sum, /* md5sum */
  764. Curl_polarssl_sha256sum /* sha256sum */
  765. };
  766. #endif /* USE_POLARSSL */