2
0

bearssl.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 *session;
  537. CURL_TRC_CF(data, cf, "connect_step1, check session cache");
  538. Curl_ssl_sessionid_lock(data);
  539. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer, &session, NULL)) {
  540. br_ssl_engine_set_session_parameters(&backend->ctx.eng, session);
  541. session_set = 1;
  542. infof(data, "BearSSL: reusing session ID");
  543. }
  544. Curl_ssl_sessionid_unlock(data);
  545. }
  546. if(connssl->alpn) {
  547. struct alpn_proto_buf proto;
  548. size_t i;
  549. for(i = 0; i < connssl->alpn->count; ++i) {
  550. backend->protocols[i] = connssl->alpn->entries[i];
  551. }
  552. br_ssl_engine_set_protocol_names(&backend->ctx.eng, backend->protocols,
  553. connssl->alpn->count);
  554. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  555. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  556. }
  557. if(connssl->peer.type != CURL_SSL_PEER_DNS) {
  558. if(verifyhost) {
  559. failf(data, "BearSSL: "
  560. "host verification of IP address is not supported");
  561. return CURLE_PEER_FAILED_VERIFICATION;
  562. }
  563. hostname = NULL;
  564. }
  565. else {
  566. if(!connssl->peer.sni) {
  567. failf(data, "Failed to set SNI");
  568. return CURLE_SSL_CONNECT_ERROR;
  569. }
  570. hostname = connssl->peer.sni;
  571. CURL_TRC_CF(data, cf, "connect_step1, SNI set");
  572. }
  573. /* give application a chance to interfere with SSL set up. */
  574. if(data->set.ssl.fsslctx) {
  575. Curl_set_in_callback(data, true);
  576. ret = (*data->set.ssl.fsslctx)(data, &backend->ctx,
  577. data->set.ssl.fsslctxp);
  578. Curl_set_in_callback(data, false);
  579. if(ret) {
  580. failf(data, "BearSSL: error signaled by ssl ctx callback");
  581. return ret;
  582. }
  583. }
  584. if(!br_ssl_client_reset(&backend->ctx, hostname, session_set))
  585. return CURLE_FAILED_INIT;
  586. backend->active = TRUE;
  587. connssl->connecting_state = ssl_connect_2;
  588. return CURLE_OK;
  589. }
  590. static CURLcode bearssl_run_until(struct Curl_cfilter *cf,
  591. struct Curl_easy *data,
  592. unsigned target)
  593. {
  594. struct ssl_connect_data *connssl = cf->ctx;
  595. struct bearssl_ssl_backend_data *backend =
  596. (struct bearssl_ssl_backend_data *)connssl->backend;
  597. unsigned state;
  598. unsigned char *buf;
  599. size_t len;
  600. ssize_t ret;
  601. CURLcode result;
  602. int err;
  603. DEBUGASSERT(backend);
  604. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  605. for(;;) {
  606. state = br_ssl_engine_current_state(&backend->ctx.eng);
  607. if(state & BR_SSL_CLOSED) {
  608. err = br_ssl_engine_last_error(&backend->ctx.eng);
  609. switch(err) {
  610. case BR_ERR_OK:
  611. /* TLS close notify */
  612. if(connssl->state != ssl_connection_complete) {
  613. failf(data, "SSL: connection closed during handshake");
  614. return CURLE_SSL_CONNECT_ERROR;
  615. }
  616. return CURLE_OK;
  617. case BR_ERR_X509_EXPIRED:
  618. failf(data, "SSL: X.509 verification: "
  619. "certificate is expired or not yet valid");
  620. return CURLE_PEER_FAILED_VERIFICATION;
  621. case BR_ERR_X509_BAD_SERVER_NAME:
  622. failf(data, "SSL: X.509 verification: "
  623. "expected server name was not found in the chain");
  624. return CURLE_PEER_FAILED_VERIFICATION;
  625. case BR_ERR_X509_NOT_TRUSTED:
  626. failf(data, "SSL: X.509 verification: "
  627. "chain could not be linked to a trust anchor");
  628. return CURLE_PEER_FAILED_VERIFICATION;
  629. default:;
  630. }
  631. failf(data, "BearSSL: connection error 0x%04x", err);
  632. /* X.509 errors are documented to have the range 32..63 */
  633. if(err >= 32 && err < 64)
  634. return CURLE_PEER_FAILED_VERIFICATION;
  635. return CURLE_SSL_CONNECT_ERROR;
  636. }
  637. if(state & target)
  638. return CURLE_OK;
  639. if(state & BR_SSL_SENDREC) {
  640. buf = br_ssl_engine_sendrec_buf(&backend->ctx.eng, &len);
  641. ret = Curl_conn_cf_send(cf->next, data, (char *)buf, len, FALSE,
  642. &result);
  643. CURL_TRC_CF(data, cf, "ssl_send(len=%zu) -> %zd, %d", len, ret, result);
  644. if(ret <= 0) {
  645. if(result == CURLE_AGAIN)
  646. connssl->io_need |= CURL_SSL_IO_NEED_SEND;
  647. return result;
  648. }
  649. br_ssl_engine_sendrec_ack(&backend->ctx.eng, ret);
  650. }
  651. else if(state & BR_SSL_RECVREC) {
  652. buf = br_ssl_engine_recvrec_buf(&backend->ctx.eng, &len);
  653. ret = Curl_conn_cf_recv(cf->next, data, (char *)buf, len, &result);
  654. CURL_TRC_CF(data, cf, "ssl_recv(len=%zu) -> %zd, %d", len, ret, result);
  655. if(ret == 0) {
  656. failf(data, "SSL: EOF without close notify");
  657. return CURLE_RECV_ERROR;
  658. }
  659. if(ret <= 0) {
  660. if(result == CURLE_AGAIN)
  661. connssl->io_need |= CURL_SSL_IO_NEED_RECV;
  662. return result;
  663. }
  664. br_ssl_engine_recvrec_ack(&backend->ctx.eng, ret);
  665. }
  666. }
  667. }
  668. static CURLcode bearssl_connect_step2(struct Curl_cfilter *cf,
  669. struct Curl_easy *data)
  670. {
  671. struct ssl_connect_data *connssl = cf->ctx;
  672. struct bearssl_ssl_backend_data *backend =
  673. (struct bearssl_ssl_backend_data *)connssl->backend;
  674. br_ssl_session_parameters session;
  675. char cipher_str[64];
  676. char ver_str[16];
  677. CURLcode ret;
  678. DEBUGASSERT(backend);
  679. CURL_TRC_CF(data, cf, "connect_step2");
  680. ret = bearssl_run_until(cf, data, BR_SSL_SENDAPP | BR_SSL_RECVAPP);
  681. if(ret == CURLE_AGAIN)
  682. return CURLE_OK;
  683. if(ret == CURLE_OK) {
  684. unsigned int tver;
  685. if(br_ssl_engine_current_state(&backend->ctx.eng) == BR_SSL_CLOSED) {
  686. failf(data, "SSL: connection closed during handshake");
  687. return CURLE_SSL_CONNECT_ERROR;
  688. }
  689. connssl->connecting_state = ssl_connect_3;
  690. /* Informational message */
  691. tver = br_ssl_engine_get_version(&backend->ctx.eng);
  692. if(tver == BR_TLS12)
  693. strcpy(ver_str, "TLSv1.2");
  694. else if(tver == BR_TLS11)
  695. strcpy(ver_str, "TLSv1.1");
  696. else if(tver == BR_TLS10)
  697. strcpy(ver_str, "TLSv1.0");
  698. else {
  699. msnprintf(ver_str, sizeof(ver_str), "TLS 0x%04x", tver);
  700. }
  701. br_ssl_engine_get_session_parameters(&backend->ctx.eng, &session);
  702. Curl_cipher_suite_get_str(session.cipher_suite, cipher_str,
  703. sizeof(cipher_str), true);
  704. infof(data, "BearSSL: %s connection using %s", ver_str, cipher_str);
  705. }
  706. return ret;
  707. }
  708. static void bearssl_session_free(void *sessionid, size_t idsize)
  709. {
  710. (void)idsize;
  711. free(sessionid);
  712. }
  713. static CURLcode bearssl_connect_step3(struct Curl_cfilter *cf,
  714. struct Curl_easy *data)
  715. {
  716. struct ssl_connect_data *connssl = cf->ctx;
  717. struct bearssl_ssl_backend_data *backend =
  718. (struct bearssl_ssl_backend_data *)connssl->backend;
  719. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  720. CURLcode ret;
  721. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  722. DEBUGASSERT(backend);
  723. CURL_TRC_CF(data, cf, "connect_step3");
  724. if(connssl->alpn) {
  725. const char *proto;
  726. proto = br_ssl_engine_get_selected_protocol(&backend->ctx.eng);
  727. Curl_alpn_set_negotiated(cf, data, (const unsigned char *)proto,
  728. proto ? strlen(proto) : 0);
  729. }
  730. if(ssl_config->primary.cache_session) {
  731. br_ssl_session_parameters *session;
  732. session = malloc(sizeof(*session));
  733. if(!session)
  734. return CURLE_OUT_OF_MEMORY;
  735. br_ssl_engine_get_session_parameters(&backend->ctx.eng, session);
  736. Curl_ssl_sessionid_lock(data);
  737. ret = Curl_ssl_set_sessionid(cf, data, &connssl->peer, session, 0,
  738. bearssl_session_free);
  739. Curl_ssl_sessionid_unlock(data);
  740. if(ret)
  741. return ret;
  742. }
  743. connssl->connecting_state = ssl_connect_done;
  744. return CURLE_OK;
  745. }
  746. static ssize_t bearssl_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  747. const void *buf, size_t len, CURLcode *err)
  748. {
  749. struct ssl_connect_data *connssl = cf->ctx;
  750. struct bearssl_ssl_backend_data *backend =
  751. (struct bearssl_ssl_backend_data *)connssl->backend;
  752. unsigned char *app;
  753. size_t applen;
  754. DEBUGASSERT(backend);
  755. for(;;) {
  756. *err = bearssl_run_until(cf, data, BR_SSL_SENDAPP);
  757. if(*err)
  758. return -1;
  759. app = br_ssl_engine_sendapp_buf(&backend->ctx.eng, &applen);
  760. if(!app) {
  761. failf(data, "SSL: connection closed during write");
  762. *err = CURLE_SEND_ERROR;
  763. return -1;
  764. }
  765. if(backend->pending_write) {
  766. applen = backend->pending_write;
  767. backend->pending_write = 0;
  768. return applen;
  769. }
  770. if(applen > len)
  771. applen = len;
  772. memcpy(app, buf, applen);
  773. br_ssl_engine_sendapp_ack(&backend->ctx.eng, applen);
  774. br_ssl_engine_flush(&backend->ctx.eng, 0);
  775. backend->pending_write = applen;
  776. }
  777. }
  778. static ssize_t bearssl_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  779. char *buf, size_t len, CURLcode *err)
  780. {
  781. struct ssl_connect_data *connssl = cf->ctx;
  782. struct bearssl_ssl_backend_data *backend =
  783. (struct bearssl_ssl_backend_data *)connssl->backend;
  784. unsigned char *app;
  785. size_t applen;
  786. DEBUGASSERT(backend);
  787. *err = bearssl_run_until(cf, data, BR_SSL_RECVAPP);
  788. if(*err != CURLE_OK)
  789. return -1;
  790. app = br_ssl_engine_recvapp_buf(&backend->ctx.eng, &applen);
  791. if(!app)
  792. return 0;
  793. if(applen > len)
  794. applen = len;
  795. memcpy(buf, app, applen);
  796. br_ssl_engine_recvapp_ack(&backend->ctx.eng, applen);
  797. return applen;
  798. }
  799. static CURLcode bearssl_connect_common(struct Curl_cfilter *cf,
  800. struct Curl_easy *data,
  801. bool nonblocking,
  802. bool *done)
  803. {
  804. CURLcode ret;
  805. struct ssl_connect_data *connssl = cf->ctx;
  806. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  807. timediff_t timeout_ms;
  808. int what;
  809. CURL_TRC_CF(data, cf, "connect_common(blocking=%d)", !nonblocking);
  810. /* check if the connection has already been established */
  811. if(ssl_connection_complete == connssl->state) {
  812. CURL_TRC_CF(data, cf, "connect_common, connected");
  813. *done = TRUE;
  814. return CURLE_OK;
  815. }
  816. if(ssl_connect_1 == connssl->connecting_state) {
  817. ret = bearssl_connect_step1(cf, data);
  818. if(ret)
  819. return ret;
  820. }
  821. while(ssl_connect_2 == connssl->connecting_state) {
  822. /* check allowed time left */
  823. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  824. if(timeout_ms < 0) {
  825. /* no need to continue if time already is up */
  826. failf(data, "SSL connection timeout");
  827. return CURLE_OPERATION_TIMEDOUT;
  828. }
  829. /* if ssl is expecting something, check if it is available. */
  830. if(connssl->io_need) {
  831. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND) ?
  832. sockfd : CURL_SOCKET_BAD;
  833. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV) ?
  834. sockfd : CURL_SOCKET_BAD;
  835. CURL_TRC_CF(data, cf, "connect_common, check socket");
  836. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  837. nonblocking ? 0 : timeout_ms);
  838. CURL_TRC_CF(data, cf, "connect_common, check socket -> %d", what);
  839. if(what < 0) {
  840. /* fatal error */
  841. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  842. return CURLE_SSL_CONNECT_ERROR;
  843. }
  844. else if(0 == what) {
  845. if(nonblocking) {
  846. *done = FALSE;
  847. return CURLE_OK;
  848. }
  849. else {
  850. /* timeout */
  851. failf(data, "SSL connection timeout");
  852. return CURLE_OPERATION_TIMEDOUT;
  853. }
  854. }
  855. /* socket is readable or writable */
  856. }
  857. /* Run transaction, and return to the caller if it failed or if this
  858. * connection is done nonblocking and this loop would execute again. This
  859. * permits the owner of a multi handle to abort a connection attempt
  860. * before step2 has completed while ensuring that a client using select()
  861. * or epoll() will always have a valid fdset to wait on.
  862. */
  863. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  864. ret = bearssl_connect_step2(cf, data);
  865. if(ret || (nonblocking && (ssl_connect_2 == connssl->connecting_state)))
  866. return ret;
  867. }
  868. if(ssl_connect_3 == connssl->connecting_state) {
  869. ret = bearssl_connect_step3(cf, data);
  870. if(ret)
  871. return ret;
  872. }
  873. if(ssl_connect_done == connssl->connecting_state) {
  874. connssl->state = ssl_connection_complete;
  875. *done = TRUE;
  876. }
  877. else
  878. *done = FALSE;
  879. /* Reset our connect state machine */
  880. connssl->connecting_state = ssl_connect_1;
  881. return CURLE_OK;
  882. }
  883. static size_t bearssl_version(char *buffer, size_t size)
  884. {
  885. return msnprintf(buffer, size, "BearSSL");
  886. }
  887. static bool bearssl_data_pending(struct Curl_cfilter *cf,
  888. const struct Curl_easy *data)
  889. {
  890. struct ssl_connect_data *ctx = cf->ctx;
  891. struct bearssl_ssl_backend_data *backend;
  892. (void)data;
  893. DEBUGASSERT(ctx && ctx->backend);
  894. backend = (struct bearssl_ssl_backend_data *)ctx->backend;
  895. return br_ssl_engine_current_state(&backend->ctx.eng) & BR_SSL_RECVAPP;
  896. }
  897. static CURLcode bearssl_random(struct Curl_easy *data UNUSED_PARAM,
  898. unsigned char *entropy, size_t length)
  899. {
  900. static br_hmac_drbg_context ctx;
  901. static bool seeded = FALSE;
  902. if(!seeded) {
  903. br_prng_seeder seeder;
  904. br_hmac_drbg_init(&ctx, &br_sha256_vtable, NULL, 0);
  905. seeder = br_prng_seeder_system(NULL);
  906. if(!seeder || !seeder(&ctx.vtable))
  907. return CURLE_FAILED_INIT;
  908. seeded = TRUE;
  909. }
  910. br_hmac_drbg_generate(&ctx, entropy, length);
  911. return CURLE_OK;
  912. }
  913. static CURLcode bearssl_connect(struct Curl_cfilter *cf,
  914. struct Curl_easy *data)
  915. {
  916. CURLcode ret;
  917. bool done = FALSE;
  918. ret = bearssl_connect_common(cf, data, FALSE, &done);
  919. if(ret)
  920. return ret;
  921. DEBUGASSERT(done);
  922. return CURLE_OK;
  923. }
  924. static CURLcode bearssl_connect_nonblocking(struct Curl_cfilter *cf,
  925. struct Curl_easy *data,
  926. bool *done)
  927. {
  928. return bearssl_connect_common(cf, data, TRUE, done);
  929. }
  930. static void *bearssl_get_internals(struct ssl_connect_data *connssl,
  931. CURLINFO info UNUSED_PARAM)
  932. {
  933. struct bearssl_ssl_backend_data *backend =
  934. (struct bearssl_ssl_backend_data *)connssl->backend;
  935. DEBUGASSERT(backend);
  936. return &backend->ctx;
  937. }
  938. static CURLcode bearssl_shutdown(struct Curl_cfilter *cf,
  939. struct Curl_easy *data,
  940. bool send_shutdown, bool *done)
  941. {
  942. struct ssl_connect_data *connssl = cf->ctx;
  943. struct bearssl_ssl_backend_data *backend =
  944. (struct bearssl_ssl_backend_data *)connssl->backend;
  945. CURLcode result;
  946. DEBUGASSERT(backend);
  947. if(!backend->active || cf->shutdown) {
  948. *done = TRUE;
  949. return CURLE_OK;
  950. }
  951. *done = FALSE;
  952. if(!backend->sent_shutdown) {
  953. (void)send_shutdown; /* unknown how to suppress our close notify */
  954. br_ssl_engine_close(&backend->ctx.eng);
  955. backend->sent_shutdown = TRUE;
  956. }
  957. result = bearssl_run_until(cf, data, BR_SSL_CLOSED);
  958. if(result == CURLE_OK) {
  959. *done = TRUE;
  960. }
  961. else if(result == CURLE_AGAIN) {
  962. CURL_TRC_CF(data, cf, "shutdown EAGAIN, io_need=%x", connssl->io_need);
  963. result = CURLE_OK;
  964. }
  965. else
  966. CURL_TRC_CF(data, cf, "shutdown error: %d", result);
  967. cf->shutdown = (result || *done);
  968. return result;
  969. }
  970. static void bearssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  971. {
  972. struct ssl_connect_data *connssl = cf->ctx;
  973. struct bearssl_ssl_backend_data *backend =
  974. (struct bearssl_ssl_backend_data *)connssl->backend;
  975. size_t i;
  976. (void)data;
  977. DEBUGASSERT(backend);
  978. backend->active = FALSE;
  979. if(backend->anchors) {
  980. for(i = 0; i < backend->anchors_len; ++i)
  981. free(backend->anchors[i].dn.data);
  982. Curl_safefree(backend->anchors);
  983. }
  984. }
  985. static CURLcode bearssl_sha256sum(const unsigned char *input,
  986. size_t inputlen,
  987. unsigned char *sha256sum,
  988. size_t sha256len UNUSED_PARAM)
  989. {
  990. br_sha256_context ctx;
  991. br_sha256_init(&ctx);
  992. br_sha256_update(&ctx, input, inputlen);
  993. br_sha256_out(&ctx, sha256sum);
  994. return CURLE_OK;
  995. }
  996. const struct Curl_ssl Curl_ssl_bearssl = {
  997. { CURLSSLBACKEND_BEARSSL, "bearssl" }, /* info */
  998. SSLSUPP_CAINFO_BLOB |
  999. SSLSUPP_SSL_CTX |
  1000. SSLSUPP_HTTPS_PROXY |
  1001. SSLSUPP_CIPHER_LIST,
  1002. sizeof(struct bearssl_ssl_backend_data),
  1003. Curl_none_init, /* init */
  1004. Curl_none_cleanup, /* cleanup */
  1005. bearssl_version, /* version */
  1006. Curl_none_check_cxn, /* check_cxn */
  1007. bearssl_shutdown, /* shutdown */
  1008. bearssl_data_pending, /* data_pending */
  1009. bearssl_random, /* random */
  1010. Curl_none_cert_status_request, /* cert_status_request */
  1011. bearssl_connect, /* connect */
  1012. bearssl_connect_nonblocking, /* connect_nonblocking */
  1013. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1014. bearssl_get_internals, /* get_internals */
  1015. bearssl_close, /* close_one */
  1016. Curl_none_close_all, /* close_all */
  1017. Curl_none_set_engine, /* set_engine */
  1018. Curl_none_set_engine_default, /* set_engine_default */
  1019. Curl_none_engines_list, /* engines_list */
  1020. Curl_none_false_start, /* false_start */
  1021. bearssl_sha256sum, /* sha256sum */
  1022. NULL, /* associate_connection */
  1023. NULL, /* disassociate_connection */
  1024. bearssl_recv, /* recv decrypted data */
  1025. bearssl_send, /* send data to encrypt */
  1026. NULL, /* get_channel_binding */
  1027. };
  1028. #endif /* USE_BEARSSL */