2
0

bearssl.c 37 KB

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