bearssl.c 39 KB

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