bearssl.c 39 KB

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