wolfssl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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 https://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 wolfSSL 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_WOLFSSL
  29. #define WOLFSSL_OPTIONS_IGNORE_SYS
  30. #include <wolfssl/version.h>
  31. #include <wolfssl/options.h>
  32. /* To determine what functions are available we rely on one or both of:
  33. - the user's options.h generated by wolfSSL
  34. - the symbols detected by curl's configure
  35. Since they are markedly different from one another, and one or the other may
  36. not be available, we do some checking below to bring things in sync. */
  37. /* HAVE_ALPN is wolfSSL's build time symbol for enabling ALPN in options.h. */
  38. #ifndef HAVE_ALPN
  39. #ifdef HAVE_WOLFSSL_USEALPN
  40. #define HAVE_ALPN
  41. #endif
  42. #endif
  43. /* WOLFSSL_ALLOW_SSLV3 is wolfSSL's build time symbol for enabling SSLv3 in
  44. options.h, but is only seen in >= 3.6.6 since that's when they started
  45. disabling SSLv3 by default. */
  46. #ifndef WOLFSSL_ALLOW_SSLV3
  47. #if (LIBWOLFSSL_VERSION_HEX < 0x03006006) || \
  48. defined(HAVE_WOLFSSLV3_CLIENT_METHOD)
  49. #define WOLFSSL_ALLOW_SSLV3
  50. #endif
  51. #endif
  52. #include <limits.h>
  53. #include "urldata.h"
  54. #include "sendf.h"
  55. #include "inet_pton.h"
  56. #include "vtls.h"
  57. #include "parsedate.h"
  58. #include "connect.h" /* for the connect timeout */
  59. #include "select.h"
  60. #include "strcase.h"
  61. #include "x509asn1.h"
  62. #include "curl_printf.h"
  63. #include "multiif.h"
  64. #include <wolfssl/openssl/ssl.h>
  65. #include <wolfssl/ssl.h>
  66. #include <wolfssl/error-ssl.h>
  67. #include "wolfssl.h"
  68. /* The last #include files should be: */
  69. #include "curl_memory.h"
  70. #include "memdebug.h"
  71. /* KEEP_PEER_CERT is a product of the presence of build time symbol
  72. OPENSSL_EXTRA without NO_CERTS, depending on the version. KEEP_PEER_CERT is
  73. in wolfSSL's settings.h, and the latter two are build time symbols in
  74. options.h. */
  75. #ifndef KEEP_PEER_CERT
  76. #if defined(HAVE_WOLFSSL_GET_PEER_CERTIFICATE) || \
  77. (defined(OPENSSL_EXTRA) && !defined(NO_CERTS))
  78. #define KEEP_PEER_CERT
  79. #endif
  80. #endif
  81. struct ssl_backend_data {
  82. SSL_CTX* ctx;
  83. SSL* handle;
  84. };
  85. #define BACKEND connssl->backend
  86. static Curl_recv wolfssl_recv;
  87. static Curl_send wolfssl_send;
  88. static int do_file_type(const char *type)
  89. {
  90. if(!type || !type[0])
  91. return SSL_FILETYPE_PEM;
  92. if(strcasecompare(type, "PEM"))
  93. return SSL_FILETYPE_PEM;
  94. if(strcasecompare(type, "DER"))
  95. return SSL_FILETYPE_ASN1;
  96. return -1;
  97. }
  98. /*
  99. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  100. * layer and do all necessary magic.
  101. */
  102. static CURLcode
  103. wolfssl_connect_step1(struct connectdata *conn,
  104. int sockindex)
  105. {
  106. char *ciphers;
  107. struct Curl_easy *data = conn->data;
  108. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  109. SSL_METHOD* req_method = NULL;
  110. curl_socket_t sockfd = conn->sock[sockindex];
  111. #ifdef HAVE_SNI
  112. bool sni = FALSE;
  113. #define use_sni(x) sni = (x)
  114. #else
  115. #define use_sni(x) Curl_nop_stmt
  116. #endif
  117. if(connssl->state == ssl_connection_complete)
  118. return CURLE_OK;
  119. if(SSL_CONN_CONFIG(version_max) != CURL_SSLVERSION_MAX_NONE) {
  120. failf(data, "wolfSSL does not support to set maximum SSL/TLS version");
  121. return CURLE_SSL_CONNECT_ERROR;
  122. }
  123. /* check to see if we've been told to use an explicit SSL/TLS version */
  124. switch(SSL_CONN_CONFIG(version)) {
  125. case CURL_SSLVERSION_DEFAULT:
  126. case CURL_SSLVERSION_TLSv1:
  127. #if LIBWOLFSSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
  128. /* minimum protocol version is set later after the CTX object is created */
  129. req_method = SSLv23_client_method();
  130. #else
  131. infof(data, "wolfSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
  132. "TLS 1.0 is used exclusively\n");
  133. req_method = TLSv1_client_method();
  134. #endif
  135. use_sni(TRUE);
  136. break;
  137. case CURL_SSLVERSION_TLSv1_0:
  138. #ifdef WOLFSSL_ALLOW_TLSV10
  139. req_method = TLSv1_client_method();
  140. use_sni(TRUE);
  141. #else
  142. failf(data, "wolfSSL does not support TLS 1.0");
  143. return CURLE_NOT_BUILT_IN;
  144. #endif
  145. break;
  146. case CURL_SSLVERSION_TLSv1_1:
  147. req_method = TLSv1_1_client_method();
  148. use_sni(TRUE);
  149. break;
  150. case CURL_SSLVERSION_TLSv1_2:
  151. req_method = TLSv1_2_client_method();
  152. use_sni(TRUE);
  153. break;
  154. case CURL_SSLVERSION_TLSv1_3:
  155. #ifdef WOLFSSL_TLS13
  156. req_method = wolfTLSv1_3_client_method();
  157. use_sni(TRUE);
  158. break;
  159. #else
  160. failf(data, "wolfSSL: TLS 1.3 is not yet supported");
  161. return CURLE_SSL_CONNECT_ERROR;
  162. #endif
  163. case CURL_SSLVERSION_SSLv3:
  164. #ifdef WOLFSSL_ALLOW_SSLV3
  165. req_method = SSLv3_client_method();
  166. use_sni(FALSE);
  167. #else
  168. failf(data, "wolfSSL does not support SSLv3");
  169. return CURLE_NOT_BUILT_IN;
  170. #endif
  171. break;
  172. case CURL_SSLVERSION_SSLv2:
  173. failf(data, "wolfSSL does not support SSLv2");
  174. return CURLE_SSL_CONNECT_ERROR;
  175. default:
  176. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  177. return CURLE_SSL_CONNECT_ERROR;
  178. }
  179. if(!req_method) {
  180. failf(data, "SSL: couldn't create a method!");
  181. return CURLE_OUT_OF_MEMORY;
  182. }
  183. if(BACKEND->ctx)
  184. SSL_CTX_free(BACKEND->ctx);
  185. BACKEND->ctx = SSL_CTX_new(req_method);
  186. if(!BACKEND->ctx) {
  187. failf(data, "SSL: couldn't create a context!");
  188. return CURLE_OUT_OF_MEMORY;
  189. }
  190. switch(SSL_CONN_CONFIG(version)) {
  191. case CURL_SSLVERSION_DEFAULT:
  192. case CURL_SSLVERSION_TLSv1:
  193. #if LIBWOLFSSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
  194. /* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is
  195. * whatever minimum version of TLS was built in and at least TLS 1.0. For
  196. * later library versions that could change (eg TLS 1.0 built in but
  197. * defaults to TLS 1.1) so we have this short circuit evaluation to find
  198. * the minimum supported TLS version.
  199. */
  200. if((wolfSSL_CTX_SetMinVersion(BACKEND->ctx, WOLFSSL_TLSV1) != 1) &&
  201. (wolfSSL_CTX_SetMinVersion(BACKEND->ctx, WOLFSSL_TLSV1_1) != 1) &&
  202. (wolfSSL_CTX_SetMinVersion(BACKEND->ctx, WOLFSSL_TLSV1_2) != 1)
  203. #ifdef WOLFSSL_TLS13
  204. && (wolfSSL_CTX_SetMinVersion(BACKEND->ctx, WOLFSSL_TLSV1_3) != 1)
  205. #endif
  206. ) {
  207. failf(data, "SSL: couldn't set the minimum protocol version");
  208. return CURLE_SSL_CONNECT_ERROR;
  209. }
  210. #endif
  211. break;
  212. }
  213. ciphers = SSL_CONN_CONFIG(cipher_list);
  214. if(ciphers) {
  215. if(!SSL_CTX_set_cipher_list(BACKEND->ctx, ciphers)) {
  216. failf(data, "failed setting cipher list: %s", ciphers);
  217. return CURLE_SSL_CIPHER;
  218. }
  219. infof(data, "Cipher selection: %s\n", ciphers);
  220. }
  221. #ifndef NO_FILESYSTEM
  222. /* load trusted cacert */
  223. if(SSL_CONN_CONFIG(CAfile)) {
  224. if(1 != SSL_CTX_load_verify_locations(BACKEND->ctx,
  225. SSL_CONN_CONFIG(CAfile),
  226. SSL_CONN_CONFIG(CApath))) {
  227. if(SSL_CONN_CONFIG(verifypeer)) {
  228. /* Fail if we insist on successfully verifying the server. */
  229. failf(data, "error setting certificate verify locations:\n"
  230. " CAfile: %s\n CApath: %s",
  231. SSL_CONN_CONFIG(CAfile)?
  232. SSL_CONN_CONFIG(CAfile): "none",
  233. SSL_CONN_CONFIG(CApath)?
  234. SSL_CONN_CONFIG(CApath) : "none");
  235. return CURLE_SSL_CACERT_BADFILE;
  236. }
  237. else {
  238. /* Just continue with a warning if no strict certificate
  239. verification is required. */
  240. infof(data, "error setting certificate verify locations,"
  241. " continuing anyway:\n");
  242. }
  243. }
  244. else {
  245. /* Everything is fine. */
  246. infof(data, "successfully set certificate verify locations:\n");
  247. }
  248. infof(data,
  249. " CAfile: %s\n"
  250. " CApath: %s\n",
  251. SSL_CONN_CONFIG(CAfile) ? SSL_CONN_CONFIG(CAfile):
  252. "none",
  253. SSL_CONN_CONFIG(CApath) ? SSL_CONN_CONFIG(CApath):
  254. "none");
  255. }
  256. /* Load the client certificate, and private key */
  257. if(SSL_SET_OPTION(cert) && SSL_SET_OPTION(key)) {
  258. int file_type = do_file_type(SSL_SET_OPTION(cert_type));
  259. if(SSL_CTX_use_certificate_file(BACKEND->ctx, SSL_SET_OPTION(cert),
  260. file_type) != 1) {
  261. failf(data, "unable to use client certificate (no key or wrong pass"
  262. " phrase?)");
  263. return CURLE_SSL_CONNECT_ERROR;
  264. }
  265. file_type = do_file_type(SSL_SET_OPTION(key_type));
  266. if(SSL_CTX_use_PrivateKey_file(BACKEND->ctx, SSL_SET_OPTION(key),
  267. file_type) != 1) {
  268. failf(data, "unable to set private key");
  269. return CURLE_SSL_CONNECT_ERROR;
  270. }
  271. }
  272. #endif /* !NO_FILESYSTEM */
  273. /* SSL always tries to verify the peer, this only says whether it should
  274. * fail to connect if the verification fails, or if it should continue
  275. * anyway. In the latter case the result of the verification is checked with
  276. * SSL_get_verify_result() below. */
  277. SSL_CTX_set_verify(BACKEND->ctx,
  278. SSL_CONN_CONFIG(verifypeer)?SSL_VERIFY_PEER:
  279. SSL_VERIFY_NONE,
  280. NULL);
  281. #ifdef HAVE_SNI
  282. if(sni) {
  283. struct in_addr addr4;
  284. #ifdef ENABLE_IPV6
  285. struct in6_addr addr6;
  286. #endif
  287. const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
  288. conn->host.name;
  289. size_t hostname_len = strlen(hostname);
  290. if((hostname_len < USHRT_MAX) &&
  291. (0 == Curl_inet_pton(AF_INET, hostname, &addr4)) &&
  292. #ifdef ENABLE_IPV6
  293. (0 == Curl_inet_pton(AF_INET6, hostname, &addr6)) &&
  294. #endif
  295. (wolfSSL_CTX_UseSNI(BACKEND->ctx, WOLFSSL_SNI_HOST_NAME, hostname,
  296. (unsigned short)hostname_len) != 1)) {
  297. infof(data, "WARNING: failed to configure server name indication (SNI) "
  298. "TLS extension\n");
  299. }
  300. }
  301. #endif
  302. /* give application a chance to interfere with SSL set up. */
  303. if(data->set.ssl.fsslctx) {
  304. CURLcode result = (*data->set.ssl.fsslctx)(data, BACKEND->ctx,
  305. data->set.ssl.fsslctxp);
  306. if(result) {
  307. failf(data, "error signaled by ssl ctx callback");
  308. return result;
  309. }
  310. }
  311. #ifdef NO_FILESYSTEM
  312. else if(SSL_CONN_CONFIG(verifypeer)) {
  313. failf(data, "SSL: Certificates can't be loaded because wolfSSL was built"
  314. " with \"no filesystem\". Either disable peer verification"
  315. " (insecure) or if you are building an application with libcurl you"
  316. " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
  317. return CURLE_SSL_CONNECT_ERROR;
  318. }
  319. #endif
  320. /* Let's make an SSL structure */
  321. if(BACKEND->handle)
  322. SSL_free(BACKEND->handle);
  323. BACKEND->handle = SSL_new(BACKEND->ctx);
  324. if(!BACKEND->handle) {
  325. failf(data, "SSL: couldn't create a context (handle)!");
  326. return CURLE_OUT_OF_MEMORY;
  327. }
  328. #ifdef HAVE_ALPN
  329. if(conn->bits.tls_enable_alpn) {
  330. char protocols[128];
  331. *protocols = '\0';
  332. /* wolfSSL's ALPN protocol name list format is a comma separated string of
  333. protocols in descending order of preference, eg: "h2,http/1.1" */
  334. #ifdef USE_NGHTTP2
  335. if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
  336. strcpy(protocols + strlen(protocols), NGHTTP2_PROTO_VERSION_ID ",");
  337. infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
  338. }
  339. #endif
  340. strcpy(protocols + strlen(protocols), ALPN_HTTP_1_1);
  341. infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
  342. if(wolfSSL_UseALPN(BACKEND->handle, protocols,
  343. (unsigned)strlen(protocols),
  344. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
  345. failf(data, "SSL: failed setting ALPN protocols");
  346. return CURLE_SSL_CONNECT_ERROR;
  347. }
  348. }
  349. #endif /* HAVE_ALPN */
  350. /* Check if there's a cached ID we can/should use here! */
  351. if(SSL_SET_OPTION(primary.sessionid)) {
  352. void *ssl_sessionid = NULL;
  353. Curl_ssl_sessionid_lock(conn);
  354. if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL, sockindex)) {
  355. /* we got a session id, use it! */
  356. if(!SSL_set_session(BACKEND->handle, ssl_sessionid)) {
  357. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  358. Curl_ssl_sessionid_unlock(conn);
  359. failf(data, "SSL: SSL_set_session failed: %s",
  360. ERR_error_string(SSL_get_error(BACKEND->handle, 0),
  361. error_buffer));
  362. return CURLE_SSL_CONNECT_ERROR;
  363. }
  364. /* Informational message */
  365. infof(data, "SSL re-using session ID\n");
  366. }
  367. Curl_ssl_sessionid_unlock(conn);
  368. }
  369. /* pass the raw socket into the SSL layer */
  370. if(!SSL_set_fd(BACKEND->handle, (int)sockfd)) {
  371. failf(data, "SSL: SSL_set_fd failed");
  372. return CURLE_SSL_CONNECT_ERROR;
  373. }
  374. connssl->connecting_state = ssl_connect_2;
  375. return CURLE_OK;
  376. }
  377. static CURLcode
  378. wolfssl_connect_step2(struct connectdata *conn,
  379. int sockindex)
  380. {
  381. int ret = -1;
  382. struct Curl_easy *data = conn->data;
  383. struct ssl_connect_data* connssl = &conn->ssl[sockindex];
  384. const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
  385. conn->host.name;
  386. const char * const dispname = SSL_IS_PROXY() ?
  387. conn->http_proxy.host.dispname : conn->host.dispname;
  388. const char * const pinnedpubkey = SSL_IS_PROXY() ?
  389. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
  390. data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
  391. conn->recv[sockindex] = wolfssl_recv;
  392. conn->send[sockindex] = wolfssl_send;
  393. /* Enable RFC2818 checks */
  394. if(SSL_CONN_CONFIG(verifyhost)) {
  395. ret = wolfSSL_check_domain_name(BACKEND->handle, hostname);
  396. if(ret == SSL_FAILURE)
  397. return CURLE_OUT_OF_MEMORY;
  398. }
  399. ret = SSL_connect(BACKEND->handle);
  400. if(ret != 1) {
  401. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  402. int detail = SSL_get_error(BACKEND->handle, ret);
  403. if(SSL_ERROR_WANT_READ == detail) {
  404. connssl->connecting_state = ssl_connect_2_reading;
  405. return CURLE_OK;
  406. }
  407. else if(SSL_ERROR_WANT_WRITE == detail) {
  408. connssl->connecting_state = ssl_connect_2_writing;
  409. return CURLE_OK;
  410. }
  411. /* There is no easy way to override only the CN matching.
  412. * This will enable the override of both mismatching SubjectAltNames
  413. * as also mismatching CN fields */
  414. else if(DOMAIN_NAME_MISMATCH == detail) {
  415. #if 1
  416. failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
  417. dispname);
  418. return CURLE_PEER_FAILED_VERIFICATION;
  419. #else
  420. /* When the wolfssl_check_domain_name() is used and you desire to
  421. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'conn->ssl_config.verifyhost
  422. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  423. * error. The only way to do this is currently to switch the
  424. * Wolfssl_check_domain_name() in and out based on the
  425. * 'conn->ssl_config.verifyhost' value. */
  426. if(SSL_CONN_CONFIG(verifyhost)) {
  427. failf(data,
  428. "\tsubject alt name(s) or common name do not match \"%s\"\n",
  429. dispname);
  430. return CURLE_PEER_FAILED_VERIFICATION;
  431. }
  432. else {
  433. infof(data,
  434. "\tsubject alt name(s) and/or common name do not match \"%s\"\n",
  435. dispname);
  436. return CURLE_OK;
  437. }
  438. #endif
  439. }
  440. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  441. else if(ASN_NO_SIGNER_E == detail) {
  442. if(SSL_CONN_CONFIG(verifypeer)) {
  443. failf(data, "\tCA signer not available for verification\n");
  444. return CURLE_SSL_CACERT_BADFILE;
  445. }
  446. else {
  447. /* Just continue with a warning if no strict certificate
  448. verification is required. */
  449. infof(data, "CA signer not available for verification, "
  450. "continuing anyway\n");
  451. }
  452. }
  453. #endif
  454. else {
  455. failf(data, "SSL_connect failed with error %d: %s", detail,
  456. ERR_error_string(detail, error_buffer));
  457. return CURLE_SSL_CONNECT_ERROR;
  458. }
  459. }
  460. if(pinnedpubkey) {
  461. #ifdef KEEP_PEER_CERT
  462. X509 *x509;
  463. const char *x509_der;
  464. int x509_der_len;
  465. curl_X509certificate x509_parsed;
  466. curl_asn1Element *pubkey;
  467. CURLcode result;
  468. x509 = SSL_get_peer_certificate(BACKEND->handle);
  469. if(!x509) {
  470. failf(data, "SSL: failed retrieving server certificate");
  471. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  472. }
  473. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  474. if(!x509_der) {
  475. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  476. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  477. }
  478. memset(&x509_parsed, 0, sizeof(x509_parsed));
  479. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  480. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  481. pubkey = &x509_parsed.subjectPublicKeyInfo;
  482. if(!pubkey->header || pubkey->end <= pubkey->header) {
  483. failf(data, "SSL: failed retrieving public key from server certificate");
  484. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  485. }
  486. result = Curl_pin_peer_pubkey(data,
  487. pinnedpubkey,
  488. (const unsigned char *)pubkey->header,
  489. (size_t)(pubkey->end - pubkey->header));
  490. if(result) {
  491. failf(data, "SSL: public key does not match pinned public key!");
  492. return result;
  493. }
  494. #else
  495. failf(data, "Library lacks pinning support built-in");
  496. return CURLE_NOT_BUILT_IN;
  497. #endif
  498. }
  499. #ifdef HAVE_ALPN
  500. if(conn->bits.tls_enable_alpn) {
  501. int rc;
  502. char *protocol = NULL;
  503. unsigned short protocol_len = 0;
  504. rc = wolfSSL_ALPN_GetProtocol(BACKEND->handle, &protocol, &protocol_len);
  505. if(rc == SSL_SUCCESS) {
  506. infof(data, "ALPN, server accepted to use %.*s\n", protocol_len,
  507. protocol);
  508. if(protocol_len == ALPN_HTTP_1_1_LENGTH &&
  509. !memcmp(protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH))
  510. conn->negnpn = CURL_HTTP_VERSION_1_1;
  511. #ifdef USE_NGHTTP2
  512. else if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
  513. protocol_len == NGHTTP2_PROTO_VERSION_ID_LEN &&
  514. !memcmp(protocol, NGHTTP2_PROTO_VERSION_ID,
  515. NGHTTP2_PROTO_VERSION_ID_LEN))
  516. conn->negnpn = CURL_HTTP_VERSION_2;
  517. #endif
  518. else
  519. infof(data, "ALPN, unrecognized protocol %.*s\n", protocol_len,
  520. protocol);
  521. Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ?
  522. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  523. }
  524. else if(rc == SSL_ALPN_NOT_FOUND)
  525. infof(data, "ALPN, server did not agree to a protocol\n");
  526. else {
  527. failf(data, "ALPN, failure getting protocol, error %d", rc);
  528. return CURLE_SSL_CONNECT_ERROR;
  529. }
  530. }
  531. #endif /* HAVE_ALPN */
  532. connssl->connecting_state = ssl_connect_3;
  533. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  534. infof(data, "SSL connection using %s / %s\n",
  535. wolfSSL_get_version(BACKEND->handle),
  536. wolfSSL_get_cipher_name(BACKEND->handle));
  537. #else
  538. infof(data, "SSL connected\n");
  539. #endif
  540. return CURLE_OK;
  541. }
  542. static CURLcode
  543. wolfssl_connect_step3(struct connectdata *conn,
  544. int sockindex)
  545. {
  546. CURLcode result = CURLE_OK;
  547. struct Curl_easy *data = conn->data;
  548. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  549. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  550. if(SSL_SET_OPTION(primary.sessionid)) {
  551. bool incache;
  552. SSL_SESSION *our_ssl_sessionid;
  553. void *old_ssl_sessionid = NULL;
  554. our_ssl_sessionid = SSL_get_session(BACKEND->handle);
  555. Curl_ssl_sessionid_lock(conn);
  556. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL,
  557. sockindex));
  558. if(incache) {
  559. if(old_ssl_sessionid != our_ssl_sessionid) {
  560. infof(data, "old SSL session ID is stale, removing\n");
  561. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  562. incache = FALSE;
  563. }
  564. }
  565. if(!incache) {
  566. result = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
  567. 0 /* unknown size */, sockindex);
  568. if(result) {
  569. Curl_ssl_sessionid_unlock(conn);
  570. failf(data, "failed to store ssl session");
  571. return result;
  572. }
  573. }
  574. Curl_ssl_sessionid_unlock(conn);
  575. }
  576. connssl->connecting_state = ssl_connect_done;
  577. return result;
  578. }
  579. static ssize_t wolfssl_send(struct connectdata *conn,
  580. int sockindex,
  581. const void *mem,
  582. size_t len,
  583. CURLcode *curlcode)
  584. {
  585. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  586. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  587. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  588. int rc = SSL_write(BACKEND->handle, mem, memlen);
  589. if(rc < 0) {
  590. int err = SSL_get_error(BACKEND->handle, rc);
  591. switch(err) {
  592. case SSL_ERROR_WANT_READ:
  593. case SSL_ERROR_WANT_WRITE:
  594. /* there's data pending, re-invoke SSL_write() */
  595. *curlcode = CURLE_AGAIN;
  596. return -1;
  597. default:
  598. failf(conn->data, "SSL write: %s, errno %d",
  599. ERR_error_string(err, error_buffer),
  600. SOCKERRNO);
  601. *curlcode = CURLE_SEND_ERROR;
  602. return -1;
  603. }
  604. }
  605. return rc;
  606. }
  607. static void Curl_wolfssl_close(struct connectdata *conn, int sockindex)
  608. {
  609. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  610. if(BACKEND->handle) {
  611. (void)SSL_shutdown(BACKEND->handle);
  612. SSL_free(BACKEND->handle);
  613. BACKEND->handle = NULL;
  614. }
  615. if(BACKEND->ctx) {
  616. SSL_CTX_free(BACKEND->ctx);
  617. BACKEND->ctx = NULL;
  618. }
  619. }
  620. static ssize_t wolfssl_recv(struct connectdata *conn,
  621. int num,
  622. char *buf,
  623. size_t buffersize,
  624. CURLcode *curlcode)
  625. {
  626. struct ssl_connect_data *connssl = &conn->ssl[num];
  627. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  628. int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  629. int nread = SSL_read(BACKEND->handle, buf, buffsize);
  630. if(nread < 0) {
  631. int err = SSL_get_error(BACKEND->handle, nread);
  632. switch(err) {
  633. case SSL_ERROR_ZERO_RETURN: /* no more data */
  634. break;
  635. case SSL_ERROR_WANT_READ:
  636. case SSL_ERROR_WANT_WRITE:
  637. /* there's data pending, re-invoke SSL_read() */
  638. *curlcode = CURLE_AGAIN;
  639. return -1;
  640. default:
  641. failf(conn->data, "SSL read: %s, errno %d",
  642. ERR_error_string(err, error_buffer),
  643. SOCKERRNO);
  644. *curlcode = CURLE_RECV_ERROR;
  645. return -1;
  646. }
  647. }
  648. return nread;
  649. }
  650. static void Curl_wolfssl_session_free(void *ptr)
  651. {
  652. (void)ptr;
  653. /* wolfSSL reuses sessions on own, no free */
  654. }
  655. static size_t Curl_wolfssl_version(char *buffer, size_t size)
  656. {
  657. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  658. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  659. #elif defined(WOLFSSL_VERSION)
  660. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  661. #endif
  662. }
  663. static int Curl_wolfssl_init(void)
  664. {
  665. return (wolfSSL_Init() == SSL_SUCCESS);
  666. }
  667. static void Curl_wolfssl_cleanup(void)
  668. {
  669. wolfSSL_Cleanup();
  670. }
  671. static bool Curl_wolfssl_data_pending(const struct connectdata* conn,
  672. int connindex)
  673. {
  674. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  675. if(BACKEND->handle) /* SSL is in use */
  676. return (0 != SSL_pending(BACKEND->handle)) ? TRUE : FALSE;
  677. else
  678. return FALSE;
  679. }
  680. /*
  681. * This function is called to shut down the SSL layer but keep the
  682. * socket open (CCC - Clear Command Channel)
  683. */
  684. static int Curl_wolfssl_shutdown(struct connectdata *conn, int sockindex)
  685. {
  686. int retval = 0;
  687. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  688. if(BACKEND->handle) {
  689. SSL_free(BACKEND->handle);
  690. BACKEND->handle = NULL;
  691. }
  692. return retval;
  693. }
  694. static CURLcode
  695. wolfssl_connect_common(struct connectdata *conn,
  696. int sockindex,
  697. bool nonblocking,
  698. bool *done)
  699. {
  700. CURLcode result;
  701. struct Curl_easy *data = conn->data;
  702. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  703. curl_socket_t sockfd = conn->sock[sockindex];
  704. time_t timeout_ms;
  705. int what;
  706. /* check if the connection has already been established */
  707. if(ssl_connection_complete == connssl->state) {
  708. *done = TRUE;
  709. return CURLE_OK;
  710. }
  711. if(ssl_connect_1 == connssl->connecting_state) {
  712. /* Find out how much more time we're allowed */
  713. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  714. if(timeout_ms < 0) {
  715. /* no need to continue if time already is up */
  716. failf(data, "SSL connection timeout");
  717. return CURLE_OPERATION_TIMEDOUT;
  718. }
  719. result = wolfssl_connect_step1(conn, sockindex);
  720. if(result)
  721. return result;
  722. }
  723. while(ssl_connect_2 == connssl->connecting_state ||
  724. ssl_connect_2_reading == connssl->connecting_state ||
  725. ssl_connect_2_writing == connssl->connecting_state) {
  726. /* check allowed time left */
  727. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  728. if(timeout_ms < 0) {
  729. /* no need to continue if time already is up */
  730. failf(data, "SSL connection timeout");
  731. return CURLE_OPERATION_TIMEDOUT;
  732. }
  733. /* if ssl is expecting something, check if it's available. */
  734. if(connssl->connecting_state == ssl_connect_2_reading
  735. || connssl->connecting_state == ssl_connect_2_writing) {
  736. curl_socket_t writefd = ssl_connect_2_writing ==
  737. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  738. curl_socket_t readfd = ssl_connect_2_reading ==
  739. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  740. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  741. nonblocking?0:timeout_ms);
  742. if(what < 0) {
  743. /* fatal error */
  744. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  745. return CURLE_SSL_CONNECT_ERROR;
  746. }
  747. else if(0 == what) {
  748. if(nonblocking) {
  749. *done = FALSE;
  750. return CURLE_OK;
  751. }
  752. else {
  753. /* timeout */
  754. failf(data, "SSL connection timeout");
  755. return CURLE_OPERATION_TIMEDOUT;
  756. }
  757. }
  758. /* socket is readable or writable */
  759. }
  760. /* Run transaction, and return to the caller if it failed or if
  761. * this connection is part of a multi handle and this loop would
  762. * execute again. This permits the owner of a multi handle to
  763. * abort a connection attempt before step2 has completed while
  764. * ensuring that a client using select() or epoll() will always
  765. * have a valid fdset to wait on.
  766. */
  767. result = wolfssl_connect_step2(conn, sockindex);
  768. if(result || (nonblocking &&
  769. (ssl_connect_2 == connssl->connecting_state ||
  770. ssl_connect_2_reading == connssl->connecting_state ||
  771. ssl_connect_2_writing == connssl->connecting_state)))
  772. return result;
  773. } /* repeat step2 until all transactions are done. */
  774. if(ssl_connect_3 == connssl->connecting_state) {
  775. result = wolfssl_connect_step3(conn, sockindex);
  776. if(result)
  777. return result;
  778. }
  779. if(ssl_connect_done == connssl->connecting_state) {
  780. connssl->state = ssl_connection_complete;
  781. conn->recv[sockindex] = wolfssl_recv;
  782. conn->send[sockindex] = wolfssl_send;
  783. *done = TRUE;
  784. }
  785. else
  786. *done = FALSE;
  787. /* Reset our connect state machine */
  788. connssl->connecting_state = ssl_connect_1;
  789. return CURLE_OK;
  790. }
  791. static CURLcode Curl_wolfssl_connect_nonblocking(struct connectdata *conn,
  792. int sockindex, bool *done)
  793. {
  794. return wolfssl_connect_common(conn, sockindex, TRUE, done);
  795. }
  796. static CURLcode Curl_wolfssl_connect(struct connectdata *conn, int sockindex)
  797. {
  798. CURLcode result;
  799. bool done = FALSE;
  800. result = wolfssl_connect_common(conn, sockindex, FALSE, &done);
  801. if(result)
  802. return result;
  803. DEBUGASSERT(done);
  804. return CURLE_OK;
  805. }
  806. static CURLcode Curl_wolfssl_random(struct Curl_easy *data,
  807. unsigned char *entropy, size_t length)
  808. {
  809. RNG rng;
  810. (void)data;
  811. if(wc_InitRng(&rng))
  812. return CURLE_FAILED_INIT;
  813. if(length > UINT_MAX)
  814. return CURLE_FAILED_INIT;
  815. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  816. return CURLE_FAILED_INIT;
  817. if(wc_FreeRng(&rng))
  818. return CURLE_FAILED_INIT;
  819. return CURLE_OK;
  820. }
  821. static CURLcode Curl_wolfssl_sha256sum(const unsigned char *tmp, /* input */
  822. size_t tmplen,
  823. unsigned char *sha256sum /* output */,
  824. size_t unused)
  825. {
  826. Sha256 SHA256pw;
  827. (void)unused;
  828. wc_InitSha256(&SHA256pw);
  829. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  830. wc_Sha256Final(&SHA256pw, sha256sum);
  831. return CURLE_OK;
  832. }
  833. static void *Curl_wolfssl_get_internals(struct ssl_connect_data *connssl,
  834. CURLINFO info UNUSED_PARAM)
  835. {
  836. (void)info;
  837. return BACKEND->handle;
  838. }
  839. const struct Curl_ssl Curl_ssl_wolfssl = {
  840. { CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
  841. #ifdef KEEP_PEER_CERT
  842. SSLSUPP_PINNEDPUBKEY |
  843. #endif
  844. SSLSUPP_SSL_CTX,
  845. sizeof(struct ssl_backend_data),
  846. Curl_wolfssl_init, /* init */
  847. Curl_wolfssl_cleanup, /* cleanup */
  848. Curl_wolfssl_version, /* version */
  849. Curl_none_check_cxn, /* check_cxn */
  850. Curl_wolfssl_shutdown, /* shutdown */
  851. Curl_wolfssl_data_pending, /* data_pending */
  852. Curl_wolfssl_random, /* random */
  853. Curl_none_cert_status_request, /* cert_status_request */
  854. Curl_wolfssl_connect, /* connect */
  855. Curl_wolfssl_connect_nonblocking, /* connect_nonblocking */
  856. Curl_wolfssl_get_internals, /* get_internals */
  857. Curl_wolfssl_close, /* close_one */
  858. Curl_none_close_all, /* close_all */
  859. Curl_wolfssl_session_free, /* session_free */
  860. Curl_none_set_engine, /* set_engine */
  861. Curl_none_set_engine_default, /* set_engine_default */
  862. Curl_none_engines_list, /* engines_list */
  863. Curl_none_false_start, /* false_start */
  864. Curl_none_md5sum, /* md5sum */
  865. Curl_wolfssl_sha256sum /* sha256sum */
  866. };
  867. #endif