bearssl.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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 "vtls_scache.h"
  35. #include "connect.h"
  36. #include "select.h"
  37. #include "multiif.h"
  38. #include "curl_printf.h"
  39. /* The last #include files should be: */
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. struct x509_context {
  43. const br_x509_class *vtable;
  44. br_x509_minimal_context minimal;
  45. br_x509_decoder_context decoder;
  46. bool verifyhost;
  47. bool verifypeer;
  48. int cert_num;
  49. };
  50. struct bearssl_ssl_backend_data {
  51. br_ssl_client_context ctx;
  52. struct x509_context x509;
  53. unsigned char buf[BR_SSL_BUFSIZE_BIDI];
  54. br_x509_trust_anchor *anchors;
  55. size_t anchors_len;
  56. const char *protocols[ALPN_ENTRIES_MAX];
  57. /* SSL client context is active */
  58. bool active;
  59. /* size of pending write, yet to be flushed */
  60. size_t pending_write;
  61. BIT(sent_shutdown);
  62. };
  63. struct cafile_parser {
  64. CURLcode err;
  65. bool in_cert;
  66. br_x509_decoder_context xc;
  67. /* array of trust anchors loaded from CAfile */
  68. br_x509_trust_anchor *anchors;
  69. size_t anchors_len;
  70. /* buffer for DN data */
  71. unsigned char dn[1024];
  72. size_t dn_len;
  73. };
  74. #define CAFILE_SOURCE_PATH 1
  75. #define CAFILE_SOURCE_BLOB 2
  76. struct cafile_source {
  77. int type;
  78. const char *data;
  79. size_t len;
  80. };
  81. static void append_dn(void *ctx, const void *buf, size_t len)
  82. {
  83. struct cafile_parser *ca = ctx;
  84. if(ca->err != CURLE_OK || !ca->in_cert)
  85. return;
  86. if(sizeof(ca->dn) - ca->dn_len < len) {
  87. ca->err = CURLE_FAILED_INIT;
  88. return;
  89. }
  90. memcpy(ca->dn + ca->dn_len, buf, len);
  91. ca->dn_len += len;
  92. }
  93. static void x509_push(void *ctx, const void *buf, size_t len)
  94. {
  95. struct cafile_parser *ca = ctx;
  96. if(ca->in_cert)
  97. br_x509_decoder_push(&ca->xc, buf, len);
  98. }
  99. static CURLcode load_cafile(struct cafile_source *source,
  100. br_x509_trust_anchor **anchors,
  101. size_t *anchors_len)
  102. {
  103. struct cafile_parser ca;
  104. br_pem_decoder_context pc;
  105. br_x509_trust_anchor *ta;
  106. size_t ta_size;
  107. br_x509_trust_anchor *new_anchors;
  108. size_t new_anchors_len;
  109. br_x509_pkey *pkey;
  110. FILE *fp = 0;
  111. unsigned char buf[BUFSIZ];
  112. const unsigned char *p = NULL;
  113. const char *name;
  114. size_t n = 0, i, pushed;
  115. DEBUGASSERT(source->type == CAFILE_SOURCE_PATH
  116. || source->type == CAFILE_SOURCE_BLOB);
  117. if(source->type == CAFILE_SOURCE_PATH) {
  118. fp = fopen(source->data, "rb");
  119. if(!fp)
  120. return CURLE_SSL_CACERT_BADFILE;
  121. }
  122. if(source->type == CAFILE_SOURCE_BLOB && source->len > (size_t)INT_MAX)
  123. return CURLE_SSL_CACERT_BADFILE;
  124. ca.err = CURLE_OK;
  125. ca.in_cert = FALSE;
  126. ca.anchors = NULL;
  127. ca.anchors_len = 0;
  128. br_pem_decoder_init(&pc);
  129. br_pem_decoder_setdest(&pc, x509_push, &ca);
  130. do {
  131. if(source->type == CAFILE_SOURCE_PATH) {
  132. n = fread(buf, 1, sizeof(buf), fp);
  133. if(n == 0)
  134. break;
  135. p = buf;
  136. }
  137. else if(source->type == CAFILE_SOURCE_BLOB) {
  138. n = source->len;
  139. p = (unsigned char *) source->data;
  140. }
  141. while(n) {
  142. pushed = br_pem_decoder_push(&pc, p, n);
  143. if(ca.err)
  144. goto fail;
  145. p += pushed;
  146. n -= pushed;
  147. switch(br_pem_decoder_event(&pc)) {
  148. case 0:
  149. break;
  150. case BR_PEM_BEGIN_OBJ:
  151. name = br_pem_decoder_name(&pc);
  152. if(strcmp(name, "CERTIFICATE") && strcmp(name, "X509 CERTIFICATE"))
  153. break;
  154. br_x509_decoder_init(&ca.xc, append_dn, &ca);
  155. ca.in_cert = TRUE;
  156. ca.dn_len = 0;
  157. break;
  158. case BR_PEM_END_OBJ:
  159. if(!ca.in_cert)
  160. break;
  161. ca.in_cert = FALSE;
  162. if(br_x509_decoder_last_error(&ca.xc)) {
  163. ca.err = CURLE_SSL_CACERT_BADFILE;
  164. goto fail;
  165. }
  166. /* add trust anchor */
  167. if(ca.anchors_len == SIZE_MAX / sizeof(ca.anchors[0])) {
  168. ca.err = CURLE_OUT_OF_MEMORY;
  169. goto fail;
  170. }
  171. new_anchors_len = ca.anchors_len + 1;
  172. new_anchors = realloc(ca.anchors,
  173. new_anchors_len * sizeof(ca.anchors[0]));
  174. if(!new_anchors) {
  175. ca.err = CURLE_OUT_OF_MEMORY;
  176. goto fail;
  177. }
  178. ca.anchors = new_anchors;
  179. ca.anchors_len = new_anchors_len;
  180. ta = &ca.anchors[ca.anchors_len - 1];
  181. ta->dn.data = NULL;
  182. ta->flags = 0;
  183. if(br_x509_decoder_isCA(&ca.xc))
  184. ta->flags |= BR_X509_TA_CA;
  185. pkey = br_x509_decoder_get_pkey(&ca.xc);
  186. if(!pkey) {
  187. ca.err = CURLE_SSL_CACERT_BADFILE;
  188. goto fail;
  189. }
  190. ta->pkey = *pkey;
  191. /* calculate space needed for trust anchor data */
  192. ta_size = ca.dn_len;
  193. switch(pkey->key_type) {
  194. case BR_KEYTYPE_RSA:
  195. ta_size += pkey->key.rsa.nlen + pkey->key.rsa.elen;
  196. break;
  197. case BR_KEYTYPE_EC:
  198. ta_size += pkey->key.ec.qlen;
  199. break;
  200. default:
  201. ca.err = CURLE_FAILED_INIT;
  202. goto fail;
  203. }
  204. /* fill in trust anchor DN and public key data */
  205. ta->dn.data = malloc(ta_size);
  206. if(!ta->dn.data) {
  207. ca.err = CURLE_OUT_OF_MEMORY;
  208. goto fail;
  209. }
  210. memcpy(ta->dn.data, ca.dn, ca.dn_len);
  211. ta->dn.len = ca.dn_len;
  212. switch(pkey->key_type) {
  213. case BR_KEYTYPE_RSA:
  214. ta->pkey.key.rsa.n = ta->dn.data + ta->dn.len;
  215. memcpy(ta->pkey.key.rsa.n, pkey->key.rsa.n, pkey->key.rsa.nlen);
  216. ta->pkey.key.rsa.e = ta->pkey.key.rsa.n + ta->pkey.key.rsa.nlen;
  217. memcpy(ta->pkey.key.rsa.e, pkey->key.rsa.e, pkey->key.rsa.elen);
  218. break;
  219. case BR_KEYTYPE_EC:
  220. ta->pkey.key.ec.q = ta->dn.data + ta->dn.len;
  221. memcpy(ta->pkey.key.ec.q, pkey->key.ec.q, pkey->key.ec.qlen);
  222. break;
  223. }
  224. break;
  225. default:
  226. ca.err = CURLE_SSL_CACERT_BADFILE;
  227. goto fail;
  228. }
  229. }
  230. } while(source->type != CAFILE_SOURCE_BLOB);
  231. if(fp && ferror(fp))
  232. ca.err = CURLE_READ_ERROR;
  233. else if(ca.in_cert)
  234. ca.err = CURLE_SSL_CACERT_BADFILE;
  235. fail:
  236. if(fp)
  237. fclose(fp);
  238. if(ca.err == CURLE_OK) {
  239. *anchors = ca.anchors;
  240. *anchors_len = ca.anchors_len;
  241. }
  242. else {
  243. for(i = 0; i < ca.anchors_len; ++i)
  244. free(ca.anchors[i].dn.data);
  245. free(ca.anchors);
  246. }
  247. return ca.err;
  248. }
  249. static void x509_start_chain(const br_x509_class **ctx,
  250. const char *server_name)
  251. {
  252. struct x509_context *x509 = (struct x509_context *)ctx;
  253. if(!x509->verifypeer) {
  254. x509->cert_num = 0;
  255. return;
  256. }
  257. if(!x509->verifyhost)
  258. server_name = NULL;
  259. x509->minimal.vtable->start_chain(&x509->minimal.vtable, server_name);
  260. }
  261. static void x509_start_cert(const br_x509_class **ctx, uint32_t length)
  262. {
  263. struct x509_context *x509 = (struct x509_context *)ctx;
  264. if(!x509->verifypeer) {
  265. /* Only decode the first cert in the chain to obtain the public key */
  266. if(x509->cert_num == 0)
  267. br_x509_decoder_init(&x509->decoder, NULL, NULL);
  268. return;
  269. }
  270. x509->minimal.vtable->start_cert(&x509->minimal.vtable, length);
  271. }
  272. static void x509_append(const br_x509_class **ctx, const unsigned char *buf,
  273. size_t len)
  274. {
  275. struct x509_context *x509 = (struct x509_context *)ctx;
  276. if(!x509->verifypeer) {
  277. if(x509->cert_num == 0)
  278. br_x509_decoder_push(&x509->decoder, buf, len);
  279. return;
  280. }
  281. x509->minimal.vtable->append(&x509->minimal.vtable, buf, len);
  282. }
  283. static void x509_end_cert(const br_x509_class **ctx)
  284. {
  285. struct x509_context *x509 = (struct x509_context *)ctx;
  286. if(!x509->verifypeer) {
  287. x509->cert_num++;
  288. return;
  289. }
  290. x509->minimal.vtable->end_cert(&x509->minimal.vtable);
  291. }
  292. static unsigned x509_end_chain(const br_x509_class **ctx)
  293. {
  294. struct x509_context *x509 = (struct x509_context *)ctx;
  295. if(!x509->verifypeer) {
  296. return (unsigned)br_x509_decoder_last_error(&x509->decoder);
  297. }
  298. return x509->minimal.vtable->end_chain(&x509->minimal.vtable);
  299. }
  300. static const br_x509_pkey *x509_get_pkey(const br_x509_class *const *ctx,
  301. unsigned *usages)
  302. {
  303. struct x509_context *x509 = (struct x509_context *)ctx;
  304. if(!x509->verifypeer) {
  305. /* Nothing in the chain is verified, just return the public key of the
  306. first certificate and allow its usage for both TLS_RSA_* and
  307. TLS_ECDHE_* */
  308. if(usages)
  309. *usages = BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN;
  310. return br_x509_decoder_get_pkey(&x509->decoder);
  311. }
  312. return x509->minimal.vtable->get_pkey(&x509->minimal.vtable, usages);
  313. }
  314. static const br_x509_class x509_vtable = {
  315. sizeof(struct x509_context),
  316. x509_start_chain,
  317. x509_start_cert,
  318. x509_append,
  319. x509_end_cert,
  320. x509_end_chain,
  321. x509_get_pkey
  322. };
  323. static CURLcode
  324. bearssl_set_ssl_version_min_max(struct Curl_easy *data,
  325. br_ssl_engine_context *ssl_eng,
  326. struct ssl_primary_config *conn_config)
  327. {
  328. unsigned version_min, version_max;
  329. switch(conn_config->version) {
  330. case CURL_SSLVERSION_DEFAULT:
  331. case CURL_SSLVERSION_TLSv1:
  332. case CURL_SSLVERSION_TLSv1_0:
  333. version_min = BR_TLS10;
  334. break;
  335. case CURL_SSLVERSION_TLSv1_1:
  336. version_min = BR_TLS11;
  337. break;
  338. case CURL_SSLVERSION_TLSv1_2:
  339. version_min = BR_TLS12;
  340. break;
  341. case CURL_SSLVERSION_TLSv1_3:
  342. failf(data, "BearSSL: does not support TLS 1.3");
  343. return CURLE_SSL_CONNECT_ERROR;
  344. default:
  345. failf(data, "BearSSL: unsupported minimum TLS version value");
  346. return CURLE_SSL_CONNECT_ERROR;
  347. }
  348. switch(conn_config->version_max) {
  349. case CURL_SSLVERSION_MAX_DEFAULT:
  350. case CURL_SSLVERSION_MAX_NONE:
  351. case CURL_SSLVERSION_MAX_TLSv1_3:
  352. case CURL_SSLVERSION_MAX_TLSv1_2:
  353. version_max = BR_TLS12;
  354. break;
  355. case CURL_SSLVERSION_MAX_TLSv1_1:
  356. version_max = BR_TLS11;
  357. break;
  358. case CURL_SSLVERSION_MAX_TLSv1_0:
  359. version_max = BR_TLS10;
  360. break;
  361. default:
  362. failf(data, "BearSSL: unsupported maximum TLS version value");
  363. return CURLE_SSL_CONNECT_ERROR;
  364. }
  365. br_ssl_engine_set_versions(ssl_eng, version_min, version_max);
  366. return CURLE_OK;
  367. }
  368. static const uint16_t ciphertable[] = {
  369. /* RFC 2246 TLS 1.0 */
  370. BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */
  371. /* RFC 3268 TLS 1.0 AES */
  372. BR_TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */
  373. BR_TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */
  374. /* RFC 5246 TLS 1.2 */
  375. BR_TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */
  376. BR_TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */
  377. /* RFC 5288 TLS 1.2 AES GCM */
  378. BR_TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */
  379. BR_TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */
  380. /* RFC 4492 TLS 1.0 ECC */
  381. BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC003 */
  382. BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC004 */
  383. BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC005 */
  384. BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC008 */
  385. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */
  386. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */
  387. BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC00D */
  388. BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, /* 0xC00E */
  389. BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, /* 0xC00F */
  390. BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC012 */
  391. BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */
  392. BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */
  393. /* RFC 5289 TLS 1.2 ECC HMAC SHA256/384 */
  394. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */
  395. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */
  396. BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC025 */
  397. BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC026 */
  398. BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */
  399. BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */
  400. BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, /* 0xC029 */
  401. BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, /* 0xC02A */
  402. /* RFC 5289 TLS 1.2 GCM */
  403. BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */
  404. BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */
  405. BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02D */
  406. BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02E */
  407. BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */
  408. BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */
  409. BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, /* 0xC031 */
  410. BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, /* 0xC032 */
  411. #ifdef BR_TLS_RSA_WITH_AES_128_CCM
  412. /* RFC 6655 TLS 1.2 CCM
  413. Supported since BearSSL 0.6 */
  414. BR_TLS_RSA_WITH_AES_128_CCM, /* 0xC09C */
  415. BR_TLS_RSA_WITH_AES_256_CCM, /* 0xC09D */
  416. BR_TLS_RSA_WITH_AES_128_CCM_8, /* 0xC0A0 */
  417. BR_TLS_RSA_WITH_AES_256_CCM_8, /* 0xC0A1 */
  418. /* RFC 7251 TLS 1.2 ECC CCM
  419. Supported since BearSSL 0.6 */
  420. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM, /* 0xC0AC */
  421. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM, /* 0xC0AD */
  422. BR_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, /* 0xC0AE */
  423. BR_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, /* 0xC0AF */
  424. #endif
  425. /* RFC 7905 TLS 1.2 ChaCha20-Poly1305
  426. Supported since BearSSL 0.2 */
  427. BR_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */
  428. BR_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */
  429. };
  430. #define NUM_OF_CIPHERS (sizeof(ciphertable) / sizeof(ciphertable[0]))
  431. static CURLcode bearssl_set_selected_ciphers(struct Curl_easy *data,
  432. br_ssl_engine_context *ssl_eng,
  433. const char *ciphers)
  434. {
  435. uint16_t selected[NUM_OF_CIPHERS];
  436. size_t count = 0, i;
  437. const char *ptr, *end;
  438. for(ptr = ciphers; ptr[0] != '\0' && count < NUM_OF_CIPHERS; ptr = end) {
  439. uint16_t id = Curl_cipher_suite_walk_str(&ptr, &end);
  440. /* Check if cipher is supported */
  441. if(id) {
  442. for(i = 0; i < NUM_OF_CIPHERS && ciphertable[i] != id; i++);
  443. if(i == NUM_OF_CIPHERS)
  444. id = 0;
  445. }
  446. if(!id) {
  447. if(ptr[0] != '\0')
  448. infof(data, "BearSSL: unknown cipher in list: \"%.*s\"",
  449. (int) (end - ptr), ptr);
  450. continue;
  451. }
  452. /* No duplicates allowed */
  453. for(i = 0; i < count && selected[i] != id; i++);
  454. if(i < count) {
  455. infof(data, "BearSSL: duplicate cipher in list: \"%.*s\"",
  456. (int) (end - ptr), ptr);
  457. continue;
  458. }
  459. selected[count++] = id;
  460. }
  461. if(count == 0) {
  462. failf(data, "BearSSL: no supported cipher in list");
  463. return CURLE_SSL_CIPHER;
  464. }
  465. br_ssl_engine_set_suites(ssl_eng, selected, count);
  466. return CURLE_OK;
  467. }
  468. static CURLcode bearssl_connect_step1(struct Curl_cfilter *cf,
  469. struct Curl_easy *data)
  470. {
  471. struct ssl_connect_data *connssl = cf->ctx;
  472. struct bearssl_ssl_backend_data *backend =
  473. (struct bearssl_ssl_backend_data *)connssl->backend;
  474. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  475. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  476. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  477. const char * const ssl_cafile =
  478. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  479. (ca_info_blob ? NULL : conn_config->CAfile);
  480. const char *hostname = connssl->peer.hostname;
  481. const bool verifypeer = conn_config->verifypeer;
  482. const bool verifyhost = conn_config->verifyhost;
  483. CURLcode ret;
  484. int session_set = 0;
  485. DEBUGASSERT(backend);
  486. CURL_TRC_CF(data, cf, "connect_step1");
  487. if(verifypeer) {
  488. if(ca_info_blob) {
  489. struct cafile_source source;
  490. source.type = CAFILE_SOURCE_BLOB;
  491. source.data = ca_info_blob->data;
  492. source.len = ca_info_blob->len;
  493. CURL_TRC_CF(data, cf, "connect_step1, load ca_info_blob");
  494. ret = load_cafile(&source, &backend->anchors, &backend->anchors_len);
  495. if(ret != CURLE_OK) {
  496. failf(data, "error importing CA certificate blob");
  497. return ret;
  498. }
  499. }
  500. if(ssl_cafile) {
  501. struct cafile_source source;
  502. source.type = CAFILE_SOURCE_PATH;
  503. source.data = ssl_cafile;
  504. source.len = 0;
  505. CURL_TRC_CF(data, cf, "connect_step1, load cafile");
  506. ret = load_cafile(&source, &backend->anchors, &backend->anchors_len);
  507. if(ret != CURLE_OK) {
  508. failf(data, "error setting certificate verify locations."
  509. " CAfile: %s", ssl_cafile);
  510. return ret;
  511. }
  512. }
  513. }
  514. /* initialize SSL context */
  515. br_ssl_client_init_full(&backend->ctx, &backend->x509.minimal,
  516. backend->anchors, backend->anchors_len);
  517. ret = bearssl_set_ssl_version_min_max(data, &backend->ctx.eng, conn_config);
  518. if(ret != CURLE_OK)
  519. return ret;
  520. br_ssl_engine_set_buffer(&backend->ctx.eng, backend->buf,
  521. sizeof(backend->buf), 1);
  522. if(conn_config->cipher_list) {
  523. /* Override the ciphers as specified. For the default cipher list see the
  524. BearSSL source code of br_ssl_client_init_full() */
  525. CURL_TRC_CF(data, cf, "connect_step1, set ciphers");
  526. ret = bearssl_set_selected_ciphers(data, &backend->ctx.eng,
  527. conn_config->cipher_list);
  528. if(ret)
  529. return ret;
  530. }
  531. /* initialize X.509 context */
  532. backend->x509.vtable = &x509_vtable;
  533. backend->x509.verifypeer = verifypeer;
  534. backend->x509.verifyhost = verifyhost;
  535. br_ssl_engine_set_x509(&backend->ctx.eng, &backend->x509.vtable);
  536. if(ssl_config->primary.cache_session) {
  537. struct Curl_ssl_session *sc_session = NULL;
  538. const br_ssl_session_parameters *session;
  539. ret = Curl_ssl_scache_take(cf, data, connssl->peer.scache_key,
  540. &sc_session);
  541. if(!ret && sc_session && sc_session->sdata && sc_session->sdata_len) {
  542. session = (br_ssl_session_parameters *)(void *)sc_session->sdata;
  543. br_ssl_engine_set_session_parameters(&backend->ctx.eng, session);
  544. session_set = 1;
  545. infof(data, "BearSSL: reusing session ID");
  546. /* single use of sessions */
  547. Curl_ssl_scache_return(cf, data, connssl->peer.scache_key, sc_session);
  548. }
  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 CURLcode bearssl_connect_step3(struct Curl_cfilter *cf,
  716. struct Curl_easy *data)
  717. {
  718. struct ssl_connect_data *connssl = cf->ctx;
  719. struct bearssl_ssl_backend_data *backend =
  720. (struct bearssl_ssl_backend_data *)connssl->backend;
  721. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  722. CURLcode ret;
  723. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  724. DEBUGASSERT(backend);
  725. CURL_TRC_CF(data, cf, "connect_step3");
  726. if(connssl->alpn) {
  727. const char *proto;
  728. proto = br_ssl_engine_get_selected_protocol(&backend->ctx.eng);
  729. Curl_alpn_set_negotiated(cf, data, connssl, (const unsigned char *)proto,
  730. proto ? strlen(proto) : 0);
  731. }
  732. if(ssl_config->primary.cache_session) {
  733. struct Curl_ssl_session *sc_session;
  734. br_ssl_session_parameters *session;
  735. session = malloc(sizeof(*session));
  736. if(!session)
  737. return CURLE_OUT_OF_MEMORY;
  738. br_ssl_engine_get_session_parameters(&backend->ctx.eng, session);
  739. ret = Curl_ssl_session_create((unsigned char *)session, sizeof(*session),
  740. (int)session->version,
  741. connssl->negotiated.alpn,
  742. 0, -1, 0, &sc_session);
  743. if(!ret) {
  744. ret = Curl_ssl_scache_put(cf, data, connssl->peer.scache_key,
  745. sc_session);
  746. /* took ownership of `sc_session` */
  747. }
  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. NULL, /* init */
  1012. NULL, /* cleanup */
  1013. bearssl_version, /* version */
  1014. bearssl_shutdown, /* shutdown */
  1015. bearssl_data_pending, /* data_pending */
  1016. bearssl_random, /* random */
  1017. NULL, /* cert_status_request */
  1018. bearssl_connect, /* connect */
  1019. bearssl_connect_nonblocking, /* connect_nonblocking */
  1020. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1021. bearssl_get_internals, /* get_internals */
  1022. bearssl_close, /* close_one */
  1023. NULL, /* close_all */
  1024. NULL, /* set_engine */
  1025. NULL, /* set_engine_default */
  1026. NULL, /* engines_list */
  1027. NULL, /* false_start */
  1028. bearssl_sha256sum, /* sha256sum */
  1029. bearssl_recv, /* recv decrypted data */
  1030. bearssl_send, /* send data to encrypt */
  1031. NULL, /* get_channel_binding */
  1032. };
  1033. #endif /* USE_BEARSSL */