wolfssl.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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. static Curl_recv wolfssl_recv;
  86. static Curl_send wolfssl_send;
  87. static int do_file_type(const char *type)
  88. {
  89. if(!type || !type[0])
  90. return SSL_FILETYPE_PEM;
  91. if(strcasecompare(type, "PEM"))
  92. return SSL_FILETYPE_PEM;
  93. if(strcasecompare(type, "DER"))
  94. return SSL_FILETYPE_ASN1;
  95. return -1;
  96. }
  97. /*
  98. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  99. * layer and do all necessary magic.
  100. */
  101. static CURLcode
  102. wolfssl_connect_step1(struct connectdata *conn,
  103. int sockindex)
  104. {
  105. char *ciphers;
  106. struct Curl_easy *data = conn->data;
  107. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  108. struct ssl_backend_data *backend = connssl->backend;
  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. struct ssl_backend_data *backend = connssl->backend;
  385. const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
  386. conn->host.name;
  387. const char * const dispname = SSL_IS_PROXY() ?
  388. conn->http_proxy.host.dispname : conn->host.dispname;
  389. const char * const pinnedpubkey = SSL_IS_PROXY() ?
  390. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
  391. data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
  392. conn->recv[sockindex] = wolfssl_recv;
  393. conn->send[sockindex] = wolfssl_send;
  394. /* Enable RFC2818 checks */
  395. if(SSL_CONN_CONFIG(verifyhost)) {
  396. ret = wolfSSL_check_domain_name(backend->handle, hostname);
  397. if(ret == SSL_FAILURE)
  398. return CURLE_OUT_OF_MEMORY;
  399. }
  400. ret = SSL_connect(backend->handle);
  401. if(ret != 1) {
  402. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  403. int detail = SSL_get_error(backend->handle, ret);
  404. if(SSL_ERROR_WANT_READ == detail) {
  405. connssl->connecting_state = ssl_connect_2_reading;
  406. return CURLE_OK;
  407. }
  408. else if(SSL_ERROR_WANT_WRITE == detail) {
  409. connssl->connecting_state = ssl_connect_2_writing;
  410. return CURLE_OK;
  411. }
  412. /* There is no easy way to override only the CN matching.
  413. * This will enable the override of both mismatching SubjectAltNames
  414. * as also mismatching CN fields */
  415. else if(DOMAIN_NAME_MISMATCH == detail) {
  416. #if 1
  417. failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
  418. dispname);
  419. return CURLE_PEER_FAILED_VERIFICATION;
  420. #else
  421. /* When the wolfssl_check_domain_name() is used and you desire to
  422. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'conn->ssl_config.verifyhost
  423. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  424. * error. The only way to do this is currently to switch the
  425. * Wolfssl_check_domain_name() in and out based on the
  426. * 'conn->ssl_config.verifyhost' value. */
  427. if(SSL_CONN_CONFIG(verifyhost)) {
  428. failf(data,
  429. "\tsubject alt name(s) or common name do not match \"%s\"\n",
  430. dispname);
  431. return CURLE_PEER_FAILED_VERIFICATION;
  432. }
  433. else {
  434. infof(data,
  435. "\tsubject alt name(s) and/or common name do not match \"%s\"\n",
  436. dispname);
  437. return CURLE_OK;
  438. }
  439. #endif
  440. }
  441. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  442. else if(ASN_NO_SIGNER_E == detail) {
  443. if(SSL_CONN_CONFIG(verifypeer)) {
  444. failf(data, "\tCA signer not available for verification\n");
  445. return CURLE_SSL_CACERT_BADFILE;
  446. }
  447. else {
  448. /* Just continue with a warning if no strict certificate
  449. verification is required. */
  450. infof(data, "CA signer not available for verification, "
  451. "continuing anyway\n");
  452. }
  453. }
  454. #endif
  455. else {
  456. failf(data, "SSL_connect failed with error %d: %s", detail,
  457. ERR_error_string(detail, error_buffer));
  458. return CURLE_SSL_CONNECT_ERROR;
  459. }
  460. }
  461. if(pinnedpubkey) {
  462. #ifdef KEEP_PEER_CERT
  463. X509 *x509;
  464. const char *x509_der;
  465. int x509_der_len;
  466. curl_X509certificate x509_parsed;
  467. curl_asn1Element *pubkey;
  468. CURLcode result;
  469. x509 = SSL_get_peer_certificate(backend->handle);
  470. if(!x509) {
  471. failf(data, "SSL: failed retrieving server certificate");
  472. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  473. }
  474. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  475. if(!x509_der) {
  476. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  477. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  478. }
  479. memset(&x509_parsed, 0, sizeof(x509_parsed));
  480. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  481. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  482. pubkey = &x509_parsed.subjectPublicKeyInfo;
  483. if(!pubkey->header || pubkey->end <= pubkey->header) {
  484. failf(data, "SSL: failed retrieving public key from server certificate");
  485. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  486. }
  487. result = Curl_pin_peer_pubkey(data,
  488. pinnedpubkey,
  489. (const unsigned char *)pubkey->header,
  490. (size_t)(pubkey->end - pubkey->header));
  491. if(result) {
  492. failf(data, "SSL: public key does not match pinned public key!");
  493. return result;
  494. }
  495. #else
  496. failf(data, "Library lacks pinning support built-in");
  497. return CURLE_NOT_BUILT_IN;
  498. #endif
  499. }
  500. #ifdef HAVE_ALPN
  501. if(conn->bits.tls_enable_alpn) {
  502. int rc;
  503. char *protocol = NULL;
  504. unsigned short protocol_len = 0;
  505. rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
  506. if(rc == SSL_SUCCESS) {
  507. infof(data, "ALPN, server accepted to use %.*s\n", protocol_len,
  508. protocol);
  509. if(protocol_len == ALPN_HTTP_1_1_LENGTH &&
  510. !memcmp(protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH))
  511. conn->negnpn = CURL_HTTP_VERSION_1_1;
  512. #ifdef USE_NGHTTP2
  513. else if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
  514. protocol_len == NGHTTP2_PROTO_VERSION_ID_LEN &&
  515. !memcmp(protocol, NGHTTP2_PROTO_VERSION_ID,
  516. NGHTTP2_PROTO_VERSION_ID_LEN))
  517. conn->negnpn = CURL_HTTP_VERSION_2;
  518. #endif
  519. else
  520. infof(data, "ALPN, unrecognized protocol %.*s\n", protocol_len,
  521. protocol);
  522. Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ?
  523. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  524. }
  525. else if(rc == SSL_ALPN_NOT_FOUND)
  526. infof(data, "ALPN, server did not agree to a protocol\n");
  527. else {
  528. failf(data, "ALPN, failure getting protocol, error %d", rc);
  529. return CURLE_SSL_CONNECT_ERROR;
  530. }
  531. }
  532. #endif /* HAVE_ALPN */
  533. connssl->connecting_state = ssl_connect_3;
  534. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  535. infof(data, "SSL connection using %s / %s\n",
  536. wolfSSL_get_version(backend->handle),
  537. wolfSSL_get_cipher_name(backend->handle));
  538. #else
  539. infof(data, "SSL connected\n");
  540. #endif
  541. return CURLE_OK;
  542. }
  543. static CURLcode
  544. wolfssl_connect_step3(struct connectdata *conn,
  545. int sockindex)
  546. {
  547. CURLcode result = CURLE_OK;
  548. struct Curl_easy *data = conn->data;
  549. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  550. struct ssl_backend_data *backend = connssl->backend;
  551. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  552. if(SSL_SET_OPTION(primary.sessionid)) {
  553. bool incache;
  554. SSL_SESSION *our_ssl_sessionid;
  555. void *old_ssl_sessionid = NULL;
  556. our_ssl_sessionid = SSL_get_session(backend->handle);
  557. Curl_ssl_sessionid_lock(conn);
  558. incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL,
  559. sockindex));
  560. if(incache) {
  561. if(old_ssl_sessionid != our_ssl_sessionid) {
  562. infof(data, "old SSL session ID is stale, removing\n");
  563. Curl_ssl_delsessionid(conn, old_ssl_sessionid);
  564. incache = FALSE;
  565. }
  566. }
  567. if(!incache) {
  568. result = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
  569. 0 /* unknown size */, sockindex);
  570. if(result) {
  571. Curl_ssl_sessionid_unlock(conn);
  572. failf(data, "failed to store ssl session");
  573. return result;
  574. }
  575. }
  576. Curl_ssl_sessionid_unlock(conn);
  577. }
  578. connssl->connecting_state = ssl_connect_done;
  579. return result;
  580. }
  581. static ssize_t wolfssl_send(struct connectdata *conn,
  582. int sockindex,
  583. const void *mem,
  584. size_t len,
  585. CURLcode *curlcode)
  586. {
  587. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  588. struct ssl_backend_data *backend = connssl->backend;
  589. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  590. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  591. int rc = SSL_write(backend->handle, mem, memlen);
  592. if(rc < 0) {
  593. int err = SSL_get_error(backend->handle, rc);
  594. switch(err) {
  595. case SSL_ERROR_WANT_READ:
  596. case SSL_ERROR_WANT_WRITE:
  597. /* there's data pending, re-invoke SSL_write() */
  598. *curlcode = CURLE_AGAIN;
  599. return -1;
  600. default:
  601. failf(conn->data, "SSL write: %s, errno %d",
  602. ERR_error_string(err, error_buffer),
  603. SOCKERRNO);
  604. *curlcode = CURLE_SEND_ERROR;
  605. return -1;
  606. }
  607. }
  608. return rc;
  609. }
  610. static void Curl_wolfssl_close(struct connectdata *conn, int sockindex)
  611. {
  612. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  613. struct ssl_backend_data *backend = connssl->backend;
  614. if(backend->handle) {
  615. (void)SSL_shutdown(backend->handle);
  616. SSL_free(backend->handle);
  617. backend->handle = NULL;
  618. }
  619. if(backend->ctx) {
  620. SSL_CTX_free(backend->ctx);
  621. backend->ctx = NULL;
  622. }
  623. }
  624. static ssize_t wolfssl_recv(struct connectdata *conn,
  625. int num,
  626. char *buf,
  627. size_t buffersize,
  628. CURLcode *curlcode)
  629. {
  630. struct ssl_connect_data *connssl = &conn->ssl[num];
  631. struct ssl_backend_data *backend = connssl->backend;
  632. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  633. int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  634. int nread = SSL_read(backend->handle, buf, buffsize);
  635. if(nread < 0) {
  636. int err = SSL_get_error(backend->handle, nread);
  637. switch(err) {
  638. case SSL_ERROR_ZERO_RETURN: /* no more data */
  639. break;
  640. case SSL_ERROR_WANT_READ:
  641. case SSL_ERROR_WANT_WRITE:
  642. /* there's data pending, re-invoke SSL_read() */
  643. *curlcode = CURLE_AGAIN;
  644. return -1;
  645. default:
  646. failf(conn->data, "SSL read: %s, errno %d",
  647. ERR_error_string(err, error_buffer),
  648. SOCKERRNO);
  649. *curlcode = CURLE_RECV_ERROR;
  650. return -1;
  651. }
  652. }
  653. return nread;
  654. }
  655. static void Curl_wolfssl_session_free(void *ptr)
  656. {
  657. (void)ptr;
  658. /* wolfSSL reuses sessions on own, no free */
  659. }
  660. static size_t Curl_wolfssl_version(char *buffer, size_t size)
  661. {
  662. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  663. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  664. #elif defined(WOLFSSL_VERSION)
  665. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  666. #endif
  667. }
  668. static int Curl_wolfssl_init(void)
  669. {
  670. return (wolfSSL_Init() == SSL_SUCCESS);
  671. }
  672. static void Curl_wolfssl_cleanup(void)
  673. {
  674. wolfSSL_Cleanup();
  675. }
  676. static bool Curl_wolfssl_data_pending(const struct connectdata *conn,
  677. int connindex)
  678. {
  679. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  680. struct ssl_backend_data *backend = connssl->backend;
  681. if(backend->handle) /* SSL is in use */
  682. return (0 != SSL_pending(backend->handle)) ? TRUE : FALSE;
  683. else
  684. return FALSE;
  685. }
  686. /*
  687. * This function is called to shut down the SSL layer but keep the
  688. * socket open (CCC - Clear Command Channel)
  689. */
  690. static int Curl_wolfssl_shutdown(struct connectdata *conn, int sockindex)
  691. {
  692. int retval = 0;
  693. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  694. struct ssl_backend_data *backend = connssl->backend;
  695. if(backend->handle) {
  696. SSL_free(backend->handle);
  697. backend->handle = NULL;
  698. }
  699. return retval;
  700. }
  701. static CURLcode
  702. wolfssl_connect_common(struct connectdata *conn,
  703. int sockindex,
  704. bool nonblocking,
  705. bool *done)
  706. {
  707. CURLcode result;
  708. struct Curl_easy *data = conn->data;
  709. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  710. curl_socket_t sockfd = conn->sock[sockindex];
  711. time_t timeout_ms;
  712. int what;
  713. /* check if the connection has already been established */
  714. if(ssl_connection_complete == connssl->state) {
  715. *done = TRUE;
  716. return CURLE_OK;
  717. }
  718. if(ssl_connect_1 == connssl->connecting_state) {
  719. /* Find out how much more time we're allowed */
  720. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  721. if(timeout_ms < 0) {
  722. /* no need to continue if time already is up */
  723. failf(data, "SSL connection timeout");
  724. return CURLE_OPERATION_TIMEDOUT;
  725. }
  726. result = wolfssl_connect_step1(conn, sockindex);
  727. if(result)
  728. return result;
  729. }
  730. while(ssl_connect_2 == connssl->connecting_state ||
  731. ssl_connect_2_reading == connssl->connecting_state ||
  732. ssl_connect_2_writing == connssl->connecting_state) {
  733. /* check allowed time left */
  734. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  735. if(timeout_ms < 0) {
  736. /* no need to continue if time already is up */
  737. failf(data, "SSL connection timeout");
  738. return CURLE_OPERATION_TIMEDOUT;
  739. }
  740. /* if ssl is expecting something, check if it's available. */
  741. if(connssl->connecting_state == ssl_connect_2_reading
  742. || connssl->connecting_state == ssl_connect_2_writing) {
  743. curl_socket_t writefd = ssl_connect_2_writing ==
  744. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  745. curl_socket_t readfd = ssl_connect_2_reading ==
  746. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  747. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  748. nonblocking?0:timeout_ms);
  749. if(what < 0) {
  750. /* fatal error */
  751. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  752. return CURLE_SSL_CONNECT_ERROR;
  753. }
  754. else if(0 == what) {
  755. if(nonblocking) {
  756. *done = FALSE;
  757. return CURLE_OK;
  758. }
  759. else {
  760. /* timeout */
  761. failf(data, "SSL connection timeout");
  762. return CURLE_OPERATION_TIMEDOUT;
  763. }
  764. }
  765. /* socket is readable or writable */
  766. }
  767. /* Run transaction, and return to the caller if it failed or if
  768. * this connection is part of a multi handle and this loop would
  769. * execute again. This permits the owner of a multi handle to
  770. * abort a connection attempt before step2 has completed while
  771. * ensuring that a client using select() or epoll() will always
  772. * have a valid fdset to wait on.
  773. */
  774. result = wolfssl_connect_step2(conn, sockindex);
  775. if(result || (nonblocking &&
  776. (ssl_connect_2 == connssl->connecting_state ||
  777. ssl_connect_2_reading == connssl->connecting_state ||
  778. ssl_connect_2_writing == connssl->connecting_state)))
  779. return result;
  780. } /* repeat step2 until all transactions are done. */
  781. if(ssl_connect_3 == connssl->connecting_state) {
  782. result = wolfssl_connect_step3(conn, sockindex);
  783. if(result)
  784. return result;
  785. }
  786. if(ssl_connect_done == connssl->connecting_state) {
  787. connssl->state = ssl_connection_complete;
  788. conn->recv[sockindex] = wolfssl_recv;
  789. conn->send[sockindex] = wolfssl_send;
  790. *done = TRUE;
  791. }
  792. else
  793. *done = FALSE;
  794. /* Reset our connect state machine */
  795. connssl->connecting_state = ssl_connect_1;
  796. return CURLE_OK;
  797. }
  798. static CURLcode Curl_wolfssl_connect_nonblocking(struct connectdata *conn,
  799. int sockindex, bool *done)
  800. {
  801. return wolfssl_connect_common(conn, sockindex, TRUE, done);
  802. }
  803. static CURLcode Curl_wolfssl_connect(struct connectdata *conn, int sockindex)
  804. {
  805. CURLcode result;
  806. bool done = FALSE;
  807. result = wolfssl_connect_common(conn, sockindex, FALSE, &done);
  808. if(result)
  809. return result;
  810. DEBUGASSERT(done);
  811. return CURLE_OK;
  812. }
  813. static CURLcode Curl_wolfssl_random(struct Curl_easy *data,
  814. unsigned char *entropy, size_t length)
  815. {
  816. WC_RNG rng;
  817. (void)data;
  818. if(wc_InitRng(&rng))
  819. return CURLE_FAILED_INIT;
  820. if(length > UINT_MAX)
  821. return CURLE_FAILED_INIT;
  822. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  823. return CURLE_FAILED_INIT;
  824. if(wc_FreeRng(&rng))
  825. return CURLE_FAILED_INIT;
  826. return CURLE_OK;
  827. }
  828. static CURLcode Curl_wolfssl_sha256sum(const unsigned char *tmp, /* input */
  829. size_t tmplen,
  830. unsigned char *sha256sum /* output */,
  831. size_t unused)
  832. {
  833. wc_Sha256 SHA256pw;
  834. (void)unused;
  835. wc_InitSha256(&SHA256pw);
  836. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  837. wc_Sha256Final(&SHA256pw, sha256sum);
  838. return CURLE_OK;
  839. }
  840. static void *Curl_wolfssl_get_internals(struct ssl_connect_data *connssl,
  841. CURLINFO info UNUSED_PARAM)
  842. {
  843. struct ssl_backend_data *backend = connssl->backend;
  844. (void)info;
  845. return backend->handle;
  846. }
  847. const struct Curl_ssl Curl_ssl_wolfssl = {
  848. { CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
  849. #ifdef KEEP_PEER_CERT
  850. SSLSUPP_PINNEDPUBKEY |
  851. #endif
  852. SSLSUPP_SSL_CTX,
  853. sizeof(struct ssl_backend_data),
  854. Curl_wolfssl_init, /* init */
  855. Curl_wolfssl_cleanup, /* cleanup */
  856. Curl_wolfssl_version, /* version */
  857. Curl_none_check_cxn, /* check_cxn */
  858. Curl_wolfssl_shutdown, /* shutdown */
  859. Curl_wolfssl_data_pending, /* data_pending */
  860. Curl_wolfssl_random, /* random */
  861. Curl_none_cert_status_request, /* cert_status_request */
  862. Curl_wolfssl_connect, /* connect */
  863. Curl_wolfssl_connect_nonblocking, /* connect_nonblocking */
  864. Curl_wolfssl_get_internals, /* get_internals */
  865. Curl_wolfssl_close, /* close_one */
  866. Curl_none_close_all, /* close_all */
  867. Curl_wolfssl_session_free, /* session_free */
  868. Curl_none_set_engine, /* set_engine */
  869. Curl_none_set_engine_default, /* set_engine_default */
  870. Curl_none_engines_list, /* engines_list */
  871. Curl_none_false_start, /* false_start */
  872. Curl_none_md5sum, /* md5sum */
  873. Curl_wolfssl_sha256sum /* sha256sum */
  874. };
  875. #endif