bearssl.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Michael Forney, <mforney@mforney.org>
  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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_BEARSSL
  26. #include <bearssl.h>
  27. #include "bearssl.h"
  28. #include "cipher_suite.h"
  29. #include "urldata.h"
  30. #include "sendf.h"
  31. #include "inet_pton.h"
  32. #include "vtls.h"
  33. #include "vtls_int.h"
  34. #include "connect.h"
  35. #include "select.h"
  36. #include "multiif.h"
  37. #include "curl_printf.h"
  38. /* The last #include files should be: */
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. struct x509_context {
  42. const br_x509_class *vtable;
  43. br_x509_minimal_context minimal;
  44. br_x509_decoder_context decoder;
  45. bool verifyhost;
  46. bool verifypeer;
  47. int cert_num;
  48. };
  49. struct bearssl_ssl_backend_data {
  50. br_ssl_client_context ctx;
  51. struct x509_context x509;
  52. unsigned char buf[BR_SSL_BUFSIZE_BIDI];
  53. br_x509_trust_anchor *anchors;
  54. size_t anchors_len;
  55. const char *protocols[ALPN_ENTRIES_MAX];
  56. /* SSL client context is active */
  57. bool active;
  58. /* size of pending write, yet to be flushed */
  59. size_t pending_write;
  60. BIT(sent_shutdown);
  61. };
  62. struct cafile_parser {
  63. CURLcode err;
  64. bool in_cert;
  65. br_x509_decoder_context xc;
  66. /* array of trust anchors loaded from CAfile */
  67. br_x509_trust_anchor *anchors;
  68. size_t anchors_len;
  69. /* buffer for DN data */
  70. unsigned char dn[1024];
  71. size_t dn_len;
  72. };
  73. #define CAFILE_SOURCE_PATH 1
  74. #define CAFILE_SOURCE_BLOB 2
  75. struct cafile_source {
  76. int type;
  77. const char *data;
  78. size_t len;
  79. };
  80. static void append_dn(void *ctx, const void *buf, size_t len)
  81. {
  82. struct cafile_parser *ca = ctx;
  83. if(ca->err != CURLE_OK || !ca->in_cert)
  84. return;
  85. if(sizeof(ca->dn) - ca->dn_len < len) {
  86. ca->err = CURLE_FAILED_INIT;
  87. return;
  88. }
  89. memcpy(ca->dn + ca->dn_len, buf, len);
  90. ca->dn_len += len;
  91. }
  92. static void x509_push(void *ctx, const void *buf, size_t len)
  93. {
  94. struct cafile_parser *ca = ctx;
  95. if(ca->in_cert)
  96. br_x509_decoder_push(&ca->xc, buf, len);
  97. }
  98. static CURLcode load_cafile(struct cafile_source *source,
  99. br_x509_trust_anchor **anchors,
  100. size_t *anchors_len)
  101. {
  102. struct cafile_parser ca;
  103. br_pem_decoder_context pc;
  104. br_x509_trust_anchor *ta;
  105. size_t ta_size;
  106. br_x509_trust_anchor *new_anchors;
  107. size_t new_anchors_len;
  108. br_x509_pkey *pkey;
  109. FILE *fp = 0;
  110. unsigned char buf[BUFSIZ];
  111. const unsigned char *p = NULL;
  112. const char *name;
  113. size_t n = 0, i, pushed;
  114. DEBUGASSERT(source->type == CAFILE_SOURCE_PATH
  115. || source->type == CAFILE_SOURCE_BLOB);
  116. if(source->type == CAFILE_SOURCE_PATH) {
  117. fp = fopen(source->data, "rb");
  118. if(!fp)
  119. return CURLE_SSL_CACERT_BADFILE;
  120. }
  121. if(source->type == CAFILE_SOURCE_BLOB && source->len > (size_t)INT_MAX)
  122. return CURLE_SSL_CACERT_BADFILE;
  123. ca.err = CURLE_OK;
  124. ca.in_cert = FALSE;
  125. ca.anchors = NULL;
  126. ca.anchors_len = 0;
  127. br_pem_decoder_init(&pc);
  128. br_pem_decoder_setdest(&pc, x509_push, &ca);
  129. do {
  130. if(source->type == CAFILE_SOURCE_PATH) {
  131. n = fread(buf, 1, sizeof(buf), fp);
  132. if(n == 0)
  133. break;
  134. p = buf;
  135. }
  136. else if(source->type == CAFILE_SOURCE_BLOB) {
  137. n = source->len;
  138. p = (unsigned char *) source->data;
  139. }
  140. while(n) {
  141. pushed = br_pem_decoder_push(&pc, p, n);
  142. if(ca.err)
  143. goto fail;
  144. p += pushed;
  145. n -= pushed;
  146. switch(br_pem_decoder_event(&pc)) {
  147. case 0:
  148. break;
  149. case BR_PEM_BEGIN_OBJ:
  150. name = br_pem_decoder_name(&pc);
  151. if(strcmp(name, "CERTIFICATE") && strcmp(name, "X509 CERTIFICATE"))
  152. break;
  153. br_x509_decoder_init(&ca.xc, append_dn, &ca);
  154. ca.in_cert = TRUE;
  155. ca.dn_len = 0;
  156. break;
  157. case BR_PEM_END_OBJ:
  158. if(!ca.in_cert)
  159. break;
  160. ca.in_cert = FALSE;
  161. if(br_x509_decoder_last_error(&ca.xc)) {
  162. ca.err = CURLE_SSL_CACERT_BADFILE;
  163. goto fail;
  164. }
  165. /* add trust anchor */
  166. if(ca.anchors_len == SIZE_MAX / sizeof(ca.anchors[0])) {
  167. ca.err = CURLE_OUT_OF_MEMORY;
  168. goto fail;
  169. }
  170. new_anchors_len = ca.anchors_len + 1;
  171. new_anchors = realloc(ca.anchors,
  172. new_anchors_len * sizeof(ca.anchors[0]));
  173. if(!new_anchors) {
  174. ca.err = CURLE_OUT_OF_MEMORY;
  175. goto fail;
  176. }
  177. ca.anchors = new_anchors;
  178. ca.anchors_len = new_anchors_len;
  179. ta = &ca.anchors[ca.anchors_len - 1];
  180. ta->dn.data = NULL;
  181. ta->flags = 0;
  182. if(br_x509_decoder_isCA(&ca.xc))
  183. ta->flags |= BR_X509_TA_CA;
  184. pkey = br_x509_decoder_get_pkey(&ca.xc);
  185. if(!pkey) {
  186. ca.err = CURLE_SSL_CACERT_BADFILE;
  187. goto fail;
  188. }
  189. ta->pkey = *pkey;
  190. /* calculate space needed for trust anchor data */
  191. ta_size = ca.dn_len;
  192. switch(pkey->key_type) {
  193. case BR_KEYTYPE_RSA:
  194. ta_size += pkey->key.rsa.nlen + pkey->key.rsa.elen;
  195. break;
  196. case BR_KEYTYPE_EC:
  197. ta_size += pkey->key.ec.qlen;
  198. break;
  199. default:
  200. ca.err = CURLE_FAILED_INIT;
  201. goto fail;
  202. }
  203. /* fill in trust anchor DN and public key data */
  204. ta->dn.data = malloc(ta_size);
  205. if(!ta->dn.data) {
  206. ca.err = CURLE_OUT_OF_MEMORY;
  207. goto fail;
  208. }
  209. memcpy(ta->dn.data, ca.dn, ca.dn_len);
  210. ta->dn.len = ca.dn_len;
  211. switch(pkey->key_type) {
  212. case BR_KEYTYPE_RSA:
  213. ta->pkey.key.rsa.n = ta->dn.data + ta->dn.len;
  214. memcpy(ta->pkey.key.rsa.n, pkey->key.rsa.n, pkey->key.rsa.nlen);
  215. ta->pkey.key.rsa.e = ta->pkey.key.rsa.n + ta->pkey.key.rsa.nlen;
  216. memcpy(ta->pkey.key.rsa.e, pkey->key.rsa.e, pkey->key.rsa.elen);
  217. break;
  218. case BR_KEYTYPE_EC:
  219. ta->pkey.key.ec.q = ta->dn.data + ta->dn.len;
  220. memcpy(ta->pkey.key.ec.q, pkey->key.ec.q, pkey->key.ec.qlen);
  221. break;
  222. }
  223. break;
  224. default:
  225. ca.err = CURLE_SSL_CACERT_BADFILE;
  226. goto fail;
  227. }
  228. }
  229. } while(source->type != CAFILE_SOURCE_BLOB);
  230. if(fp && ferror(fp))
  231. ca.err = CURLE_READ_ERROR;
  232. else if(ca.in_cert)
  233. ca.err = CURLE_SSL_CACERT_BADFILE;
  234. fail:
  235. if(fp)
  236. fclose(fp);
  237. if(ca.err == CURLE_OK) {
  238. *anchors = ca.anchors;
  239. *anchors_len = ca.anchors_len;
  240. }
  241. else {
  242. for(i = 0; i < ca.anchors_len; ++i)
  243. free(ca.anchors[i].dn.data);
  244. free(ca.anchors);
  245. }
  246. return ca.err;
  247. }
  248. static void x509_start_chain(const br_x509_class **ctx,
  249. const char *server_name)
  250. {
  251. struct x509_context *x509 = (struct x509_context *)ctx;
  252. if(!x509->verifypeer) {
  253. x509->cert_num = 0;
  254. return;
  255. }
  256. if(!x509->verifyhost)
  257. server_name = NULL;
  258. x509->minimal.vtable->start_chain(&x509->minimal.vtable, server_name);
  259. }
  260. static void x509_start_cert(const br_x509_class **ctx, uint32_t length)
  261. {
  262. struct x509_context *x509 = (struct x509_context *)ctx;
  263. if(!x509->verifypeer) {
  264. /* Only decode the first cert in the chain to obtain the public key */
  265. if(x509->cert_num == 0)
  266. br_x509_decoder_init(&x509->decoder, NULL, NULL);
  267. return;
  268. }
  269. x509->minimal.vtable->start_cert(&x509->minimal.vtable, length);
  270. }
  271. static void x509_append(const br_x509_class **ctx, const unsigned char *buf,
  272. size_t len)
  273. {
  274. struct x509_context *x509 = (struct x509_context *)ctx;
  275. if(!x509->verifypeer) {
  276. if(x509->cert_num == 0)
  277. br_x509_decoder_push(&x509->decoder, buf, len);
  278. return;
  279. }
  280. x509->minimal.vtable->append(&x509->minimal.vtable, buf, len);
  281. }
  282. static void x509_end_cert(const br_x509_class **ctx)
  283. {
  284. struct x509_context *x509 = (struct x509_context *)ctx;
  285. if(!x509->verifypeer) {
  286. x509->cert_num++;
  287. return;
  288. }
  289. x509->minimal.vtable->end_cert(&x509->minimal.vtable);
  290. }
  291. static unsigned x509_end_chain(const br_x509_class **ctx)
  292. {
  293. struct x509_context *x509 = (struct x509_context *)ctx;
  294. if(!x509->verifypeer) {
  295. return (unsigned)br_x509_decoder_last_error(&x509->decoder);
  296. }
  297. return x509->minimal.vtable->end_chain(&x509->minimal.vtable);
  298. }
  299. static const br_x509_pkey *x509_get_pkey(const br_x509_class *const *ctx,
  300. unsigned *usages)
  301. {
  302. struct x509_context *x509 = (struct x509_context *)ctx;
  303. if(!x509->verifypeer) {
  304. /* Nothing in the chain is verified, just return the public key of the
  305. first certificate and allow its usage for both TLS_RSA_* and
  306. TLS_ECDHE_* */
  307. if(usages)
  308. *usages = BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN;
  309. return br_x509_decoder_get_pkey(&x509->decoder);
  310. }
  311. return x509->minimal.vtable->get_pkey(&x509->minimal.vtable, usages);
  312. }
  313. static const br_x509_class x509_vtable = {
  314. sizeof(struct x509_context),
  315. x509_start_chain,
  316. x509_start_cert,
  317. x509_append,
  318. x509_end_cert,
  319. x509_end_chain,
  320. x509_get_pkey
  321. };
  322. static CURLcode
  323. bearssl_set_ssl_version_min_max(struct Curl_easy *data,
  324. br_ssl_engine_context *ssl_eng,
  325. struct ssl_primary_config *conn_config)
  326. {
  327. unsigned version_min, version_max;
  328. switch(conn_config->version) {
  329. case CURL_SSLVERSION_DEFAULT:
  330. case CURL_SSLVERSION_TLSv1:
  331. case CURL_SSLVERSION_TLSv1_0:
  332. version_min = BR_TLS10;
  333. break;
  334. case CURL_SSLVERSION_TLSv1_1:
  335. version_min = BR_TLS11;
  336. break;
  337. case CURL_SSLVERSION_TLSv1_2:
  338. version_min = BR_TLS12;
  339. break;
  340. case CURL_SSLVERSION_TLSv1_3:
  341. failf(data, "BearSSL: does not support TLS 1.3");
  342. return CURLE_SSL_CONNECT_ERROR;
  343. default:
  344. failf(data, "BearSSL: unsupported minimum TLS version value");
  345. return CURLE_SSL_CONNECT_ERROR;
  346. }
  347. switch(conn_config->version_max) {
  348. case CURL_SSLVERSION_MAX_DEFAULT:
  349. case CURL_SSLVERSION_MAX_NONE:
  350. case CURL_SSLVERSION_MAX_TLSv1_3:
  351. case CURL_SSLVERSION_MAX_TLSv1_2:
  352. version_max = BR_TLS12;
  353. break;
  354. case CURL_SSLVERSION_MAX_TLSv1_1:
  355. version_max = BR_TLS11;
  356. break;
  357. case CURL_SSLVERSION_MAX_TLSv1_0:
  358. version_max = BR_TLS10;
  359. break;
  360. default:
  361. failf(data, "BearSSL: unsupported maximum TLS version value");
  362. return CURLE_SSL_CONNECT_ERROR;
  363. }
  364. br_ssl_engine_set_versions(ssl_eng, version_min, version_max);
  365. return CURLE_OK;
  366. }
  367. static const uint16_t ciphertable[] = {
  368. /* RFC 2246 TLS 1.0 */
  369. BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */
  370. /* RFC 3268 TLS 1.0 AES */
  371. BR_TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */
  372. BR_TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */
  373. /* RFC 5246 TLS 1.2 */
  374. BR_TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */
  375. BR_TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */
  376. /* RFC 5288 TLS 1.2 AES GCM */
  377. BR_TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */
  378. BR_TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */
  379. /* RFC 4492 TLS 1.0 ECC */
  380. BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC003 */
  381. BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC004 */
  382. BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC005 */
  383. BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC008 */
  384. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */
  385. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */
  386. BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC00D */
  387. BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, /* 0xC00E */
  388. BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, /* 0xC00F */
  389. BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC012 */
  390. BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */
  391. BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */
  392. /* RFC 5289 TLS 1.2 ECC HMAC SHA256/384 */
  393. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */
  394. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */
  395. BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC025 */
  396. BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC026 */
  397. BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */
  398. BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */
  399. BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, /* 0xC029 */
  400. BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, /* 0xC02A */
  401. /* RFC 5289 TLS 1.2 GCM */
  402. BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */
  403. BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */
  404. BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02D */
  405. BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02E */
  406. BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */
  407. BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */
  408. BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, /* 0xC031 */
  409. BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, /* 0xC032 */
  410. #ifdef BR_TLS_RSA_WITH_AES_128_CCM
  411. /* RFC 6655 TLS 1.2 CCM
  412. Supported since BearSSL 0.6 */
  413. BR_TLS_RSA_WITH_AES_128_CCM, /* 0xC09C */
  414. BR_TLS_RSA_WITH_AES_256_CCM, /* 0xC09D */
  415. BR_TLS_RSA_WITH_AES_128_CCM_8, /* 0xC0A0 */
  416. BR_TLS_RSA_WITH_AES_256_CCM_8, /* 0xC0A1 */
  417. /* RFC 7251 TLS 1.2 ECC CCM
  418. Supported since BearSSL 0.6 */
  419. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, /* 0xC0AC */
  420. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, /* 0xC0AD */
  421. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, /* 0xC0AE */
  422. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, /* 0xC0AF */
  423. #endif
  424. /* RFC 7905 TLS 1.2 ChaCha20-Poly1305
  425. Supported since BearSSL 0.2 */
  426. BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */
  427. BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */
  428. };
  429. #define NUM_OF_CIPHERS (sizeof(ciphertable) / sizeof(ciphertable[0]))
  430. static CURLcode bearssl_set_selected_ciphers(struct Curl_easy *data,
  431. br_ssl_engine_context *ssl_eng,
  432. const char *ciphers)
  433. {
  434. uint16_t selected[NUM_OF_CIPHERS];
  435. size_t count = 0, i;
  436. const char *ptr, *end;
  437. for(ptr = ciphers; ptr[0] != '\0' && count < NUM_OF_CIPHERS; ptr = end) {
  438. uint16_t id = Curl_cipher_suite_walk_str(&ptr, &end);
  439. /* Check if cipher is supported */
  440. if(id) {
  441. for(i = 0; i < NUM_OF_CIPHERS && ciphertable[i] != id; i++);
  442. if(i == NUM_OF_CIPHERS)
  443. id = 0;
  444. }
  445. if(!id) {
  446. if(ptr[0] != '\0')
  447. infof(data, "BearSSL: unknown cipher in list: \"%.*s\"",
  448. (int) (end - ptr), ptr);
  449. continue;
  450. }
  451. /* No duplicates allowed */
  452. for(i = 0; i < count && selected[i] != id; i++);
  453. if(i < count) {
  454. infof(data, "BearSSL: duplicate cipher in list: \"%.*s\"",
  455. (int) (end - ptr), ptr);
  456. continue;
  457. }
  458. selected[count++] = id;
  459. }
  460. if(count == 0) {
  461. failf(data, "BearSSL: no supported cipher in list");
  462. return CURLE_SSL_CIPHER;
  463. }
  464. br_ssl_engine_set_suites(ssl_eng, selected, count);
  465. return CURLE_OK;
  466. }
  467. static CURLcode bearssl_connect_step1(struct Curl_cfilter *cf,
  468. struct Curl_easy *data)
  469. {
  470. struct ssl_connect_data *connssl = cf->ctx;
  471. struct bearssl_ssl_backend_data *backend =
  472. (struct bearssl_ssl_backend_data *)connssl->backend;
  473. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  474. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  475. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  476. const char * const ssl_cafile =
  477. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  478. (ca_info_blob ? NULL : conn_config->CAfile);
  479. const char *hostname = connssl->peer.hostname;
  480. const bool verifypeer = conn_config->verifypeer;
  481. const bool verifyhost = conn_config->verifyhost;
  482. CURLcode ret;
  483. int session_set = 0;
  484. DEBUGASSERT(backend);
  485. CURL_TRC_CF(data, cf, "connect_step1");
  486. if(verifypeer) {
  487. if(ca_info_blob) {
  488. struct cafile_source source;
  489. source.type = CAFILE_SOURCE_BLOB;
  490. source.data = ca_info_blob->data;
  491. source.len = ca_info_blob->len;
  492. CURL_TRC_CF(data, cf, "connect_step1, load ca_info_blob");
  493. ret = load_cafile(&source, &backend->anchors, &backend->anchors_len);
  494. if(ret != CURLE_OK) {
  495. failf(data, "error importing CA certificate blob");
  496. return ret;
  497. }
  498. }
  499. if(ssl_cafile) {
  500. struct cafile_source source;
  501. source.type = CAFILE_SOURCE_PATH;
  502. source.data = ssl_cafile;
  503. source.len = 0;
  504. CURL_TRC_CF(data, cf, "connect_step1, load cafile");
  505. ret = load_cafile(&source, &backend->anchors, &backend->anchors_len);
  506. if(ret != CURLE_OK) {
  507. failf(data, "error setting certificate verify locations."
  508. " CAfile: %s", ssl_cafile);
  509. return ret;
  510. }
  511. }
  512. }
  513. /* initialize SSL context */
  514. br_ssl_client_init_full(&backend->ctx, &backend->x509.minimal,
  515. backend->anchors, backend->anchors_len);
  516. ret = bearssl_set_ssl_version_min_max(data, &backend->ctx.eng, conn_config);
  517. if(ret != CURLE_OK)
  518. return ret;
  519. br_ssl_engine_set_buffer(&backend->ctx.eng, backend->buf,
  520. sizeof(backend->buf), 1);
  521. if(conn_config->cipher_list) {
  522. /* Override the ciphers as specified. For the default cipher list see the
  523. BearSSL source code of br_ssl_client_init_full() */
  524. CURL_TRC_CF(data, cf, "connect_step1, set ciphers");
  525. ret = bearssl_set_selected_ciphers(data, &backend->ctx.eng,
  526. conn_config->cipher_list);
  527. if(ret)
  528. return ret;
  529. }
  530. /* initialize X.509 context */
  531. backend->x509.vtable = &x509_vtable;
  532. backend->x509.verifypeer = verifypeer;
  533. backend->x509.verifyhost = verifyhost;
  534. br_ssl_engine_set_x509(&backend->ctx.eng, &backend->x509.vtable);
  535. if(ssl_config->primary.cache_session) {
  536. void *sdata;
  537. size_t slen;
  538. const br_ssl_session_parameters *session;
  539. CURL_TRC_CF(data, cf, "connect_step1, check session cache");
  540. Curl_ssl_sessionid_lock(data);
  541. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer, &sdata, &slen, NULL) &&
  542. slen == sizeof(*session)) {
  543. session = sdata;
  544. br_ssl_engine_set_session_parameters(&backend->ctx.eng, session);
  545. session_set = 1;
  546. infof(data, "BearSSL: reusing session ID");
  547. }
  548. Curl_ssl_sessionid_unlock(data);
  549. }
  550. if(connssl->alpn) {
  551. struct alpn_proto_buf proto;
  552. size_t i;
  553. for(i = 0; i < connssl->alpn->count; ++i) {
  554. backend->protocols[i] = connssl->alpn->entries[i];
  555. }
  556. br_ssl_engine_set_protocol_names(&backend->ctx.eng, backend->protocols,
  557. connssl->alpn->count);
  558. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  559. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  560. }
  561. if(connssl->peer.type != CURL_SSL_PEER_DNS) {
  562. if(verifyhost) {
  563. failf(data, "BearSSL: "
  564. "host verification of IP address is not supported");
  565. return CURLE_PEER_FAILED_VERIFICATION;
  566. }
  567. hostname = NULL;
  568. }
  569. else {
  570. if(!connssl->peer.sni) {
  571. failf(data, "Failed to set SNI");
  572. return CURLE_SSL_CONNECT_ERROR;
  573. }
  574. hostname = connssl->peer.sni;
  575. CURL_TRC_CF(data, cf, "connect_step1, SNI set");
  576. }
  577. /* give application a chance to interfere with SSL set up. */
  578. if(data->set.ssl.fsslctx) {
  579. Curl_set_in_callback(data, TRUE);
  580. ret = (*data->set.ssl.fsslctx)(data, &backend->ctx,
  581. data->set.ssl.fsslctxp);
  582. Curl_set_in_callback(data, FALSE);
  583. if(ret) {
  584. failf(data, "BearSSL: error signaled by ssl ctx callback");
  585. return ret;
  586. }
  587. }
  588. if(!br_ssl_client_reset(&backend->ctx, hostname, session_set))
  589. return CURLE_FAILED_INIT;
  590. backend->active = TRUE;
  591. connssl->connecting_state = ssl_connect_2;
  592. return CURLE_OK;
  593. }
  594. static CURLcode bearssl_run_until(struct Curl_cfilter *cf,
  595. struct Curl_easy *data,
  596. unsigned target)
  597. {
  598. struct ssl_connect_data *connssl = cf->ctx;
  599. struct bearssl_ssl_backend_data *backend =
  600. (struct bearssl_ssl_backend_data *)connssl->backend;
  601. unsigned state;
  602. unsigned char *buf;
  603. size_t len;
  604. ssize_t ret;
  605. CURLcode result;
  606. int err;
  607. DEBUGASSERT(backend);
  608. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  609. for(;;) {
  610. state = br_ssl_engine_current_state(&backend->ctx.eng);
  611. if(state & BR_SSL_CLOSED) {
  612. err = br_ssl_engine_last_error(&backend->ctx.eng);
  613. switch(err) {
  614. case BR_ERR_OK:
  615. /* TLS close notify */
  616. if(connssl->state != ssl_connection_complete) {
  617. failf(data, "SSL: connection closed during handshake");
  618. return CURLE_SSL_CONNECT_ERROR;
  619. }
  620. return CURLE_OK;
  621. case BR_ERR_X509_EXPIRED:
  622. failf(data, "SSL: X.509 verification: "
  623. "certificate is expired or not yet valid");
  624. return CURLE_PEER_FAILED_VERIFICATION;
  625. case BR_ERR_X509_BAD_SERVER_NAME:
  626. failf(data, "SSL: X.509 verification: "
  627. "expected server name was not found in the chain");
  628. return CURLE_PEER_FAILED_VERIFICATION;
  629. case BR_ERR_X509_NOT_TRUSTED:
  630. failf(data, "SSL: X.509 verification: "
  631. "chain could not be linked to a trust anchor");
  632. return CURLE_PEER_FAILED_VERIFICATION;
  633. default:;
  634. }
  635. failf(data, "BearSSL: connection error 0x%04x", err);
  636. /* X.509 errors are documented to have the range 32..63 */
  637. if(err >= 32 && err < 64)
  638. return CURLE_PEER_FAILED_VERIFICATION;
  639. return CURLE_SSL_CONNECT_ERROR;
  640. }
  641. if(state & target)
  642. return CURLE_OK;
  643. if(state & BR_SSL_SENDREC) {
  644. buf = br_ssl_engine_sendrec_buf(&backend->ctx.eng, &len);
  645. ret = Curl_conn_cf_send(cf->next, data, (char *)buf, len, FALSE,
  646. &result);
  647. CURL_TRC_CF(data, cf, "ssl_send(len=%zu) -> %zd, %d", len, ret, result);
  648. if(ret <= 0) {
  649. if(result == CURLE_AGAIN)
  650. connssl->io_need |= CURL_SSL_IO_NEED_SEND;
  651. return result;
  652. }
  653. br_ssl_engine_sendrec_ack(&backend->ctx.eng, ret);
  654. }
  655. else if(state & BR_SSL_RECVREC) {
  656. buf = br_ssl_engine_recvrec_buf(&backend->ctx.eng, &len);
  657. ret = Curl_conn_cf_recv(cf->next, data, (char *)buf, len, &result);
  658. CURL_TRC_CF(data, cf, "ssl_recv(len=%zu) -> %zd, %d", len, ret, result);
  659. if(ret == 0) {
  660. failf(data, "SSL: EOF without close notify");
  661. return CURLE_RECV_ERROR;
  662. }
  663. if(ret <= 0) {
  664. if(result == CURLE_AGAIN)
  665. connssl->io_need |= CURL_SSL_IO_NEED_RECV;
  666. return result;
  667. }
  668. br_ssl_engine_recvrec_ack(&backend->ctx.eng, ret);
  669. }
  670. }
  671. }
  672. static CURLcode bearssl_connect_step2(struct Curl_cfilter *cf,
  673. struct Curl_easy *data)
  674. {
  675. struct ssl_connect_data *connssl = cf->ctx;
  676. struct bearssl_ssl_backend_data *backend =
  677. (struct bearssl_ssl_backend_data *)connssl->backend;
  678. br_ssl_session_parameters session;
  679. char cipher_str[64];
  680. CURLcode ret;
  681. DEBUGASSERT(backend);
  682. CURL_TRC_CF(data, cf, "connect_step2");
  683. ret = bearssl_run_until(cf, data, BR_SSL_SENDAPP | BR_SSL_RECVAPP);
  684. if(ret == CURLE_AGAIN)
  685. return CURLE_OK;
  686. if(ret == CURLE_OK) {
  687. unsigned int tver;
  688. int subver = 0;
  689. if(br_ssl_engine_current_state(&backend->ctx.eng) == BR_SSL_CLOSED) {
  690. failf(data, "SSL: connection closed during handshake");
  691. return CURLE_SSL_CONNECT_ERROR;
  692. }
  693. connssl->connecting_state = ssl_connect_3;
  694. /* Informational message */
  695. tver = br_ssl_engine_get_version(&backend->ctx.eng);
  696. switch(tver) {
  697. case BR_TLS12:
  698. subver = 2; /* 1.2 */
  699. break;
  700. case BR_TLS11:
  701. subver = 1; /* 1.1 */
  702. break;
  703. case BR_TLS10: /* 1.0 */
  704. default: /* unknown, leave it at zero */
  705. break;
  706. }
  707. br_ssl_engine_get_session_parameters(&backend->ctx.eng, &session);
  708. Curl_cipher_suite_get_str(session.cipher_suite, cipher_str,
  709. sizeof(cipher_str), TRUE);
  710. infof(data, "BearSSL: TLS v1.%d connection using %s", subver,
  711. cipher_str);
  712. }
  713. return ret;
  714. }
  715. static void bearssl_session_free(void *sessionid, size_t idsize)
  716. {
  717. (void)idsize;
  718. free(sessionid);
  719. }
  720. static CURLcode bearssl_connect_step3(struct Curl_cfilter *cf,
  721. struct Curl_easy *data)
  722. {
  723. struct ssl_connect_data *connssl = cf->ctx;
  724. struct bearssl_ssl_backend_data *backend =
  725. (struct bearssl_ssl_backend_data *)connssl->backend;
  726. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  727. CURLcode ret;
  728. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  729. DEBUGASSERT(backend);
  730. CURL_TRC_CF(data, cf, "connect_step3");
  731. if(connssl->alpn) {
  732. const char *proto;
  733. proto = br_ssl_engine_get_selected_protocol(&backend->ctx.eng);
  734. Curl_alpn_set_negotiated(cf, data, connssl, (const unsigned char *)proto,
  735. proto ? strlen(proto) : 0);
  736. }
  737. if(ssl_config->primary.cache_session) {
  738. br_ssl_session_parameters *session;
  739. session = malloc(sizeof(*session));
  740. if(!session)
  741. return CURLE_OUT_OF_MEMORY;
  742. br_ssl_engine_get_session_parameters(&backend->ctx.eng, session);
  743. Curl_ssl_sessionid_lock(data);
  744. ret = Curl_ssl_set_sessionid(cf, data, &connssl->peer, NULL,
  745. session, sizeof(*session),
  746. bearssl_session_free);
  747. Curl_ssl_sessionid_unlock(data);
  748. if(ret)
  749. return ret;
  750. }
  751. connssl->connecting_state = ssl_connect_done;
  752. return CURLE_OK;
  753. }
  754. static ssize_t bearssl_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  755. const void *buf, size_t len, CURLcode *err)
  756. {
  757. struct ssl_connect_data *connssl = cf->ctx;
  758. struct bearssl_ssl_backend_data *backend =
  759. (struct bearssl_ssl_backend_data *)connssl->backend;
  760. unsigned char *app;
  761. size_t applen;
  762. DEBUGASSERT(backend);
  763. for(;;) {
  764. *err = bearssl_run_until(cf, data, BR_SSL_SENDAPP);
  765. if(*err)
  766. return -1;
  767. app = br_ssl_engine_sendapp_buf(&backend->ctx.eng, &applen);
  768. if(!app) {
  769. failf(data, "SSL: connection closed during write");
  770. *err = CURLE_SEND_ERROR;
  771. return -1;
  772. }
  773. if(backend->pending_write) {
  774. applen = backend->pending_write;
  775. backend->pending_write = 0;
  776. return applen;
  777. }
  778. if(applen > len)
  779. applen = len;
  780. memcpy(app, buf, applen);
  781. br_ssl_engine_sendapp_ack(&backend->ctx.eng, applen);
  782. br_ssl_engine_flush(&backend->ctx.eng, 0);
  783. backend->pending_write = applen;
  784. }
  785. }
  786. static ssize_t bearssl_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  787. char *buf, size_t len, CURLcode *err)
  788. {
  789. struct ssl_connect_data *connssl = cf->ctx;
  790. struct bearssl_ssl_backend_data *backend =
  791. (struct bearssl_ssl_backend_data *)connssl->backend;
  792. unsigned char *app;
  793. size_t applen;
  794. DEBUGASSERT(backend);
  795. *err = bearssl_run_until(cf, data, BR_SSL_RECVAPP);
  796. if(*err != CURLE_OK)
  797. return -1;
  798. app = br_ssl_engine_recvapp_buf(&backend->ctx.eng, &applen);
  799. if(!app)
  800. return 0;
  801. if(applen > len)
  802. applen = len;
  803. memcpy(buf, app, applen);
  804. br_ssl_engine_recvapp_ack(&backend->ctx.eng, applen);
  805. return applen;
  806. }
  807. static CURLcode bearssl_connect_common(struct Curl_cfilter *cf,
  808. struct Curl_easy *data,
  809. bool nonblocking,
  810. bool *done)
  811. {
  812. CURLcode ret;
  813. struct ssl_connect_data *connssl = cf->ctx;
  814. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  815. timediff_t timeout_ms;
  816. int what;
  817. CURL_TRC_CF(data, cf, "connect_common(blocking=%d)", !nonblocking);
  818. /* check if the connection has already been established */
  819. if(ssl_connection_complete == connssl->state) {
  820. CURL_TRC_CF(data, cf, "connect_common, connected");
  821. *done = TRUE;
  822. return CURLE_OK;
  823. }
  824. if(ssl_connect_1 == connssl->connecting_state) {
  825. ret = bearssl_connect_step1(cf, data);
  826. if(ret)
  827. return ret;
  828. }
  829. while(ssl_connect_2 == connssl->connecting_state) {
  830. /* check allowed time left */
  831. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  832. if(timeout_ms < 0) {
  833. /* no need to continue if time already is up */
  834. failf(data, "SSL connection timeout");
  835. return CURLE_OPERATION_TIMEDOUT;
  836. }
  837. /* if ssl is expecting something, check if it is available. */
  838. if(connssl->io_need) {
  839. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND) ?
  840. sockfd : CURL_SOCKET_BAD;
  841. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV) ?
  842. sockfd : CURL_SOCKET_BAD;
  843. CURL_TRC_CF(data, cf, "connect_common, check socket");
  844. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  845. nonblocking ? 0 : timeout_ms);
  846. CURL_TRC_CF(data, cf, "connect_common, check socket -> %d", what);
  847. if(what < 0) {
  848. /* fatal error */
  849. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  850. return CURLE_SSL_CONNECT_ERROR;
  851. }
  852. else if(0 == what) {
  853. if(nonblocking) {
  854. *done = FALSE;
  855. return CURLE_OK;
  856. }
  857. else {
  858. /* timeout */
  859. failf(data, "SSL connection timeout");
  860. return CURLE_OPERATION_TIMEDOUT;
  861. }
  862. }
  863. /* socket is readable or writable */
  864. }
  865. /* Run transaction, and return to the caller if it failed or if this
  866. * connection is done nonblocking and this loop would execute again. This
  867. * permits the owner of a multi handle to abort a connection attempt
  868. * before step2 has completed while ensuring that a client using select()
  869. * or epoll() will always have a valid fdset to wait on.
  870. */
  871. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  872. ret = bearssl_connect_step2(cf, data);
  873. if(ret || (nonblocking && (ssl_connect_2 == connssl->connecting_state)))
  874. return ret;
  875. }
  876. if(ssl_connect_3 == connssl->connecting_state) {
  877. ret = bearssl_connect_step3(cf, data);
  878. if(ret)
  879. return ret;
  880. }
  881. if(ssl_connect_done == connssl->connecting_state) {
  882. connssl->state = ssl_connection_complete;
  883. *done = TRUE;
  884. }
  885. else
  886. *done = FALSE;
  887. /* Reset our connect state machine */
  888. connssl->connecting_state = ssl_connect_1;
  889. return CURLE_OK;
  890. }
  891. static size_t bearssl_version(char *buffer, size_t size)
  892. {
  893. return msnprintf(buffer, size, "BearSSL");
  894. }
  895. static bool bearssl_data_pending(struct Curl_cfilter *cf,
  896. const struct Curl_easy *data)
  897. {
  898. struct ssl_connect_data *ctx = cf->ctx;
  899. struct bearssl_ssl_backend_data *backend;
  900. (void)data;
  901. DEBUGASSERT(ctx && ctx->backend);
  902. backend = (struct bearssl_ssl_backend_data *)ctx->backend;
  903. return br_ssl_engine_current_state(&backend->ctx.eng) & BR_SSL_RECVAPP;
  904. }
  905. static CURLcode bearssl_random(struct Curl_easy *data UNUSED_PARAM,
  906. unsigned char *entropy, size_t length)
  907. {
  908. static br_hmac_drbg_context ctx;
  909. static bool seeded = FALSE;
  910. if(!seeded) {
  911. br_prng_seeder seeder;
  912. br_hmac_drbg_init(&ctx, &br_sha256_vtable, NULL, 0);
  913. seeder = br_prng_seeder_system(NULL);
  914. if(!seeder || !seeder(&ctx.vtable))
  915. return CURLE_FAILED_INIT;
  916. seeded = TRUE;
  917. }
  918. br_hmac_drbg_generate(&ctx, entropy, length);
  919. return CURLE_OK;
  920. }
  921. static CURLcode bearssl_connect(struct Curl_cfilter *cf,
  922. struct Curl_easy *data)
  923. {
  924. CURLcode ret;
  925. bool done = FALSE;
  926. ret = bearssl_connect_common(cf, data, FALSE, &done);
  927. if(ret)
  928. return ret;
  929. DEBUGASSERT(done);
  930. return CURLE_OK;
  931. }
  932. static CURLcode bearssl_connect_nonblocking(struct Curl_cfilter *cf,
  933. struct Curl_easy *data,
  934. bool *done)
  935. {
  936. return bearssl_connect_common(cf, data, TRUE, done);
  937. }
  938. static void *bearssl_get_internals(struct ssl_connect_data *connssl,
  939. CURLINFO info UNUSED_PARAM)
  940. {
  941. struct bearssl_ssl_backend_data *backend =
  942. (struct bearssl_ssl_backend_data *)connssl->backend;
  943. DEBUGASSERT(backend);
  944. return &backend->ctx;
  945. }
  946. static CURLcode bearssl_shutdown(struct Curl_cfilter *cf,
  947. struct Curl_easy *data,
  948. bool send_shutdown, bool *done)
  949. {
  950. struct ssl_connect_data *connssl = cf->ctx;
  951. struct bearssl_ssl_backend_data *backend =
  952. (struct bearssl_ssl_backend_data *)connssl->backend;
  953. CURLcode result;
  954. DEBUGASSERT(backend);
  955. if(!backend->active || cf->shutdown) {
  956. *done = TRUE;
  957. return CURLE_OK;
  958. }
  959. *done = FALSE;
  960. if(!backend->sent_shutdown) {
  961. (void)send_shutdown; /* unknown how to suppress our close notify */
  962. br_ssl_engine_close(&backend->ctx.eng);
  963. backend->sent_shutdown = TRUE;
  964. }
  965. result = bearssl_run_until(cf, data, BR_SSL_CLOSED);
  966. if(result == CURLE_OK) {
  967. *done = TRUE;
  968. }
  969. else if(result == CURLE_AGAIN) {
  970. CURL_TRC_CF(data, cf, "shutdown EAGAIN, io_need=%x", connssl->io_need);
  971. result = CURLE_OK;
  972. }
  973. else
  974. CURL_TRC_CF(data, cf, "shutdown error: %d", result);
  975. cf->shutdown = (result || *done);
  976. return result;
  977. }
  978. static void bearssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  979. {
  980. struct ssl_connect_data *connssl = cf->ctx;
  981. struct bearssl_ssl_backend_data *backend =
  982. (struct bearssl_ssl_backend_data *)connssl->backend;
  983. size_t i;
  984. (void)data;
  985. DEBUGASSERT(backend);
  986. backend->active = FALSE;
  987. if(backend->anchors) {
  988. for(i = 0; i < backend->anchors_len; ++i)
  989. free(backend->anchors[i].dn.data);
  990. Curl_safefree(backend->anchors);
  991. }
  992. }
  993. static CURLcode bearssl_sha256sum(const unsigned char *input,
  994. size_t inputlen,
  995. unsigned char *sha256sum,
  996. size_t sha256len UNUSED_PARAM)
  997. {
  998. br_sha256_context ctx;
  999. br_sha256_init(&ctx);
  1000. br_sha256_update(&ctx, input, inputlen);
  1001. br_sha256_out(&ctx, sha256sum);
  1002. return CURLE_OK;
  1003. }
  1004. const struct Curl_ssl Curl_ssl_bearssl = {
  1005. { CURLSSLBACKEND_BEARSSL, "bearssl" }, /* info */
  1006. SSLSUPP_CAINFO_BLOB |
  1007. SSLSUPP_SSL_CTX |
  1008. SSLSUPP_HTTPS_PROXY |
  1009. SSLSUPP_CIPHER_LIST,
  1010. sizeof(struct bearssl_ssl_backend_data),
  1011. Curl_none_init, /* init */
  1012. Curl_none_cleanup, /* cleanup */
  1013. bearssl_version, /* version */
  1014. Curl_none_check_cxn, /* check_cxn */
  1015. bearssl_shutdown, /* shutdown */
  1016. bearssl_data_pending, /* data_pending */
  1017. bearssl_random, /* random */
  1018. Curl_none_cert_status_request, /* cert_status_request */
  1019. bearssl_connect, /* connect */
  1020. bearssl_connect_nonblocking, /* connect_nonblocking */
  1021. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1022. bearssl_get_internals, /* get_internals */
  1023. bearssl_close, /* close_one */
  1024. Curl_none_close_all, /* close_all */
  1025. Curl_none_set_engine, /* set_engine */
  1026. Curl_none_set_engine_default, /* set_engine_default */
  1027. Curl_none_engines_list, /* engines_list */
  1028. Curl_none_false_start, /* false_start */
  1029. bearssl_sha256sum, /* sha256sum */
  1030. NULL, /* associate_connection */
  1031. NULL, /* disassociate_connection */
  1032. bearssl_recv, /* recv decrypted data */
  1033. bearssl_send, /* send data to encrypt */
  1034. NULL, /* get_channel_binding */
  1035. };
  1036. #endif /* USE_BEARSSL */