2
0

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