quic_tls.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright 2022-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/ssl.h>
  10. #include "internal/recordmethod.h"
  11. #include "internal/quic_tls.h"
  12. #include "../ssl_local.h"
  13. #include "internal/quic_record_util.h"
  14. #include "internal/quic_error.h"
  15. #include "internal/quic_types.h"
  16. #include "internal/ssl_unwrap.h"
  17. #define QUIC_TLS_FATAL(rl, ad, err) \
  18. do { \
  19. if ((rl) != NULL) (rl)->alert = (ad); \
  20. ERR_raise(ERR_LIB_SSL, (err)); \
  21. if ((rl) != NULL) (rl)->qtls->inerror = 1; \
  22. } while(0)
  23. struct quic_tls_st {
  24. QUIC_TLS_ARGS args;
  25. /*
  26. * Transport parameters which client should send. Buffer lifetime must
  27. * exceed the lifetime of the QUIC_TLS object.
  28. */
  29. const unsigned char *local_transport_params;
  30. size_t local_transport_params_len;
  31. ERR_STATE *error_state;
  32. /*
  33. * QUIC error code (usually in the TLS Alert-mapped CRYPTO_ERR range). Valid
  34. * only if inerror is 1.
  35. */
  36. uint64_t error_code;
  37. /*
  38. * Error message with static storage duration. Valid only if inerror is 1.
  39. * Should be suitable for encapsulation in a CONNECTION_CLOSE frame.
  40. */
  41. const char *error_msg;
  42. /* Whether our SSL object for TLS has been configured for use in QUIC */
  43. unsigned int configured : 1;
  44. /* Set if we have hit any error state */
  45. unsigned int inerror : 1;
  46. /* Set if the handshake has completed */
  47. unsigned int complete : 1;
  48. /* Set if we have consumed the local transport parameters yet. */
  49. unsigned int local_transport_params_consumed : 1;
  50. };
  51. struct ossl_record_layer_st {
  52. QUIC_TLS *qtls;
  53. /* Protection level */
  54. int level;
  55. /* Only used for retry flags */
  56. BIO *dummybio;
  57. /* Number of bytes written so far if we are part way through a write */
  58. size_t written;
  59. /* If we are part way through a write, a copy of the template */
  60. OSSL_RECORD_TEMPLATE template;
  61. /*
  62. * If we hit an error, what alert code should be used
  63. */
  64. int alert;
  65. /* Amount of crypto stream data we read in the last call to quic_read_record */
  66. size_t recread;
  67. /* Amount of crypto stream data read but not yet released */
  68. size_t recunreleased;
  69. /* Callbacks */
  70. OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
  71. void *cbarg;
  72. };
  73. static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
  74. static int quic_free(OSSL_RECORD_LAYER *r);
  75. static int
  76. quic_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
  77. int role, int direction, int level, uint16_t epoch,
  78. unsigned char *secret, size_t secretlen,
  79. unsigned char *key, size_t keylen, unsigned char *iv,
  80. size_t ivlen, unsigned char *mackey, size_t mackeylen,
  81. const EVP_CIPHER *ciph, size_t taglen,
  82. int mactype,
  83. const EVP_MD *md, COMP_METHOD *comp,
  84. const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
  85. BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
  86. const OSSL_PARAM *settings, const OSSL_PARAM *options,
  87. const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
  88. OSSL_RECORD_LAYER **retrl)
  89. {
  90. OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
  91. int qdir;
  92. uint32_t suite_id = 0;
  93. if (rl == NULL) {
  94. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  95. return 0;
  96. }
  97. rl->qtls = (QUIC_TLS *)rlarg;
  98. rl->level = level;
  99. if (!quic_set1_bio(rl, transport)) {
  100. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  101. goto err;
  102. }
  103. rl->cbarg = cbarg;
  104. *retrl = rl;
  105. if (fns != NULL) {
  106. for (; fns->function_id != 0; fns++) {
  107. switch (fns->function_id) {
  108. break;
  109. case OSSL_FUNC_RLAYER_MSG_CALLBACK:
  110. rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
  111. break;
  112. default:
  113. /* Just ignore anything we don't understand */
  114. break;
  115. }
  116. }
  117. }
  118. if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
  119. return 1;
  120. if (direction == OSSL_RECORD_DIRECTION_READ)
  121. qdir = 0;
  122. else
  123. qdir = 1;
  124. if (rl->qtls->args.ossl_quic) {
  125. #ifndef OPENSSL_NO_QUIC
  126. /*
  127. * We only look up the suite_id/MD for internal callers. Not used in the
  128. * public API. We assume that a 3rd party QUIC stack will want to
  129. * figure this out by itself (e.g. so that they could add new
  130. * ciphersuites at a different pace to us)
  131. */
  132. if (EVP_CIPHER_is_a(ciph, "AES-128-GCM")) {
  133. suite_id = QRL_SUITE_AES128GCM;
  134. } else if (EVP_CIPHER_is_a(ciph, "AES-256-GCM")) {
  135. suite_id = QRL_SUITE_AES256GCM;
  136. } else if (EVP_CIPHER_is_a(ciph, "CHACHA20-POLY1305")) {
  137. suite_id = QRL_SUITE_CHACHA20POLY1305;
  138. } else {
  139. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
  140. goto err;
  141. }
  142. /* We pass a ref to the md in a successful yield_secret_cb call */
  143. /* TODO(QUIC FUTURE): This cast is horrible. We should try and remove it */
  144. if (!EVP_MD_up_ref((EVP_MD *)kdfdigest)) {
  145. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  146. goto err;
  147. }
  148. #else
  149. if (!ossl_assert("Should not happen" == NULL))
  150. goto err;
  151. #endif
  152. } else {
  153. kdfdigest = NULL;
  154. }
  155. if (!rl->qtls->args.yield_secret_cb(level, qdir, suite_id,
  156. (EVP_MD *)kdfdigest, secret, secretlen,
  157. rl->qtls->args.yield_secret_cb_arg)) {
  158. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  159. EVP_MD_free((EVP_MD *)kdfdigest);
  160. goto err;
  161. }
  162. return 1;
  163. err:
  164. *retrl = NULL;
  165. quic_free(rl);
  166. return 0;
  167. }
  168. static int quic_free(OSSL_RECORD_LAYER *rl)
  169. {
  170. if (rl == NULL)
  171. return 1;
  172. BIO_free(rl->dummybio);
  173. OPENSSL_free(rl);
  174. return 1;
  175. }
  176. static int quic_unprocessed_read_pending(OSSL_RECORD_LAYER *rl)
  177. {
  178. /*
  179. * Read ahead isn't really a thing for QUIC so we never have unprocessed
  180. * data pending
  181. */
  182. return 0;
  183. }
  184. static int quic_processed_read_pending(OSSL_RECORD_LAYER *rl)
  185. {
  186. /*
  187. * This is currently only ever used by:
  188. * - SSL_has_pending()
  189. * - to check whether we have more records that we want to supply to the
  190. * upper layers
  191. *
  192. * We only ever supply 1 record at a time to the upper layers, and
  193. * SSL_has_pending() will go via the QUIC method not the TLS method so that
  194. * use case doesn't apply here.
  195. * Therefore we can ignore this for now and always return 0. We might
  196. * eventually want to change this to check in the receive buffers to see if
  197. * we have any more data pending.
  198. */
  199. return 0;
  200. }
  201. static size_t quic_get_max_records(OSSL_RECORD_LAYER *rl, uint8_t type,
  202. size_t len,
  203. size_t maxfrag, size_t *preffrag)
  204. {
  205. return 1;
  206. }
  207. static int quic_write_records(OSSL_RECORD_LAYER *rl,
  208. OSSL_RECORD_TEMPLATE *template,
  209. size_t numtempl)
  210. {
  211. size_t consumed;
  212. unsigned char alert;
  213. if (!ossl_assert(numtempl == 1)) {
  214. /* How could this be? quic_get_max_records() always returns 1 */
  215. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  216. return OSSL_RECORD_RETURN_FATAL;
  217. }
  218. BIO_clear_retry_flags(rl->dummybio);
  219. if (rl->msg_callback != NULL) {
  220. unsigned char dummyrec[SSL3_RT_HEADER_LENGTH];
  221. /*
  222. * For the purposes of the callback we "pretend" to be normal TLS,
  223. * and manufacture a dummy record header
  224. */
  225. dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
  226. ? template->type
  227. : SSL3_RT_APPLICATION_DATA;
  228. dummyrec[1] = (unsigned char)((template->version >> 8) & 0xff);
  229. dummyrec[2] = (unsigned char)(template->version & 0xff);
  230. /*
  231. * We assume that buflen is always <= UINT16_MAX. Since this is
  232. * generated by libssl itself we actually expect it to never
  233. * exceed SSL3_RT_MAX_PLAIN_LENGTH - so it should be a safe
  234. * assumption
  235. */
  236. dummyrec[3] = (unsigned char)((template->buflen >> 8) & 0xff);
  237. dummyrec[4] = (unsigned char)(template->buflen & 0xff);
  238. rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec,
  239. SSL3_RT_HEADER_LENGTH, rl->cbarg);
  240. if (rl->level != OSSL_RECORD_PROTECTION_LEVEL_NONE) {
  241. rl->msg_callback(1, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE,
  242. &template->type, 1, rl->cbarg);
  243. }
  244. }
  245. switch (template->type) {
  246. case SSL3_RT_ALERT:
  247. if (template->buflen != 2) {
  248. /*
  249. * We assume that libssl always sends both bytes of an alert to
  250. * us in one go, and never fragments it. If we ever get more
  251. * or less bytes than exactly 2 then this is very unexpected.
  252. */
  253. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_VALUE);
  254. return OSSL_RECORD_RETURN_FATAL;
  255. }
  256. /*
  257. * Byte 0 is the alert level (we ignore it) and byte 1 is the alert
  258. * description that we are actually interested in.
  259. */
  260. alert = template->buf[1];
  261. if (!rl->qtls->args.alert_cb(rl->qtls->args.alert_cb_arg, alert)) {
  262. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  263. return OSSL_RECORD_RETURN_FATAL;
  264. }
  265. break;
  266. case SSL3_RT_HANDSHAKE:
  267. /*
  268. * We expect this to only fail on some fatal error (e.g. malloc
  269. * failure)
  270. */
  271. if (!rl->qtls->args.crypto_send_cb(template->buf + rl->written,
  272. template->buflen - rl->written,
  273. &consumed,
  274. rl->qtls->args.crypto_send_cb_arg)) {
  275. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  276. return OSSL_RECORD_RETURN_FATAL;
  277. }
  278. /*
  279. * We might have written less than we wanted to if we have filled the
  280. * send stream buffer.
  281. */
  282. if (consumed + rl->written != template->buflen) {
  283. if (!ossl_assert(consumed + rl->written < template->buflen)) {
  284. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  285. return OSSL_RECORD_RETURN_FATAL;
  286. }
  287. /*
  288. * We've not written everything we wanted to. Take a copy of the
  289. * template, remember how much we wrote so far and signal a retry.
  290. * The buffer supplied in the template is guaranteed to be the same
  291. * on a retry for handshake data
  292. */
  293. rl->written += consumed;
  294. rl->template = *template;
  295. BIO_set_retry_write(rl->dummybio);
  296. return OSSL_RECORD_RETURN_RETRY;
  297. }
  298. rl->written = 0;
  299. break;
  300. default:
  301. /* Anything else is unexpected and an error */
  302. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  303. return OSSL_RECORD_RETURN_FATAL;
  304. }
  305. return OSSL_RECORD_RETURN_SUCCESS;
  306. }
  307. static int quic_retry_write_records(OSSL_RECORD_LAYER *rl)
  308. {
  309. return quic_write_records(rl, &rl->template, 1);
  310. }
  311. static int quic_read_record(OSSL_RECORD_LAYER *rl, void **rechandle,
  312. int *rversion, uint8_t *type, const unsigned char **data,
  313. size_t *datalen, uint16_t *epoch,
  314. unsigned char *seq_num)
  315. {
  316. if (rl->recread != 0 || rl->recunreleased != 0)
  317. return OSSL_RECORD_RETURN_FATAL;
  318. BIO_clear_retry_flags(rl->dummybio);
  319. if (!rl->qtls->args.crypto_recv_rcd_cb(data, datalen,
  320. rl->qtls->args.crypto_recv_rcd_cb_arg)) {
  321. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  322. return OSSL_RECORD_RETURN_FATAL;
  323. }
  324. if (*datalen == 0) {
  325. BIO_set_retry_read(rl->dummybio);
  326. return OSSL_RECORD_RETURN_RETRY;
  327. }
  328. *rechandle = rl;
  329. *rversion = TLS1_3_VERSION;
  330. *type = SSL3_RT_HANDSHAKE;
  331. rl->recread = rl->recunreleased = *datalen;
  332. /* epoch/seq_num are not relevant for TLS */
  333. if (rl->msg_callback != NULL) {
  334. unsigned char dummyrec[SSL3_RT_HEADER_LENGTH];
  335. /*
  336. * For the purposes of the callback we "pretend" to be normal TLS,
  337. * and manufacture a dummy record header
  338. */
  339. dummyrec[0] = (rl->level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
  340. ? SSL3_RT_HANDSHAKE
  341. : SSL3_RT_APPLICATION_DATA;
  342. dummyrec[1] = (unsigned char)((TLS1_2_VERSION >> 8) & 0xff);
  343. dummyrec[2] = (unsigned char)(TLS1_2_VERSION & 0xff);
  344. /*
  345. * *datalen will always fit into 2 bytes because our original buffer
  346. * size is less than that.
  347. */
  348. dummyrec[3] = (unsigned char)((*datalen >> 8) & 0xff);
  349. dummyrec[4] = (unsigned char)(*datalen & 0xff);
  350. rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_HEADER, dummyrec,
  351. SSL3_RT_HEADER_LENGTH, rl->cbarg);
  352. rl->msg_callback(0, TLS1_3_VERSION, SSL3_RT_INNER_CONTENT_TYPE, type, 1,
  353. rl->cbarg);
  354. }
  355. return OSSL_RECORD_RETURN_SUCCESS;
  356. }
  357. static int quic_release_record(OSSL_RECORD_LAYER *rl, void *rechandle,
  358. size_t length)
  359. {
  360. if (!ossl_assert(rl->recread > 0)
  361. || !ossl_assert(rl->recunreleased <= rl->recread)
  362. || !ossl_assert(rl == rechandle)
  363. || !ossl_assert(length <= rl->recunreleased)) {
  364. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  365. return OSSL_RECORD_RETURN_FATAL;
  366. }
  367. if (rl->recunreleased == length) {
  368. if (!rl->qtls->args.crypto_release_rcd_cb(rl->recread,
  369. rl->qtls->args.crypto_release_rcd_cb_arg)) {
  370. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  371. return OSSL_RECORD_RETURN_FATAL;
  372. }
  373. rl->recread = 0;
  374. }
  375. rl->recunreleased -= length;
  376. return OSSL_RECORD_RETURN_SUCCESS;
  377. }
  378. static int quic_get_alert_code(OSSL_RECORD_LAYER *rl)
  379. {
  380. return rl->alert;
  381. }
  382. static int quic_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
  383. {
  384. /* We only support TLSv1.3, so its bad if we negotiate anything else */
  385. if (!ossl_assert(version == TLS1_3_VERSION)) {
  386. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  387. return 0;
  388. }
  389. return 1;
  390. }
  391. static void quic_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
  392. {
  393. /* We don't care */
  394. }
  395. static void quic_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
  396. {
  397. /* We don't care */
  398. }
  399. static void quic_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
  400. {
  401. /* We don't care */
  402. }
  403. static void quic_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
  404. const char **longstr)
  405. {
  406. /*
  407. * According to the docs, valid read state strings are: "RH"/"read header",
  408. * "RB"/"read body", and "unknown"/"unknown". We don't read records in quite
  409. * that way, so we report every "normal" state as "read header". In the
  410. * event of error then we report "unknown".
  411. */
  412. if (rl->qtls->inerror) {
  413. if (shortstr != NULL)
  414. *shortstr = "unknown";
  415. if (longstr != NULL)
  416. *longstr = "unknown";
  417. } else {
  418. if (shortstr != NULL)
  419. *shortstr = "RH";
  420. if (longstr != NULL)
  421. *longstr = "read header";
  422. }
  423. }
  424. static int quic_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
  425. {
  426. /*
  427. * We don't support any options yet - but we might do at some point so
  428. * this could be useful.
  429. */
  430. return 1;
  431. }
  432. static const COMP_METHOD *quic_get_compression(OSSL_RECORD_LAYER *rl)
  433. {
  434. /* We only support TLSv1.3 which doesn't have compression */
  435. return NULL;
  436. }
  437. static void quic_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
  438. {
  439. /* This really doesn't make any sense for QUIC. Ignore it */
  440. }
  441. static int quic_alloc_buffers(OSSL_RECORD_LAYER *rl)
  442. {
  443. /*
  444. * This is a hint only. We don't support it (yet), so just ignore the
  445. * request
  446. */
  447. return 1;
  448. }
  449. static int quic_free_buffers(OSSL_RECORD_LAYER *rl)
  450. {
  451. /*
  452. * This is a hint only. We don't support it (yet), so just ignore the
  453. * request
  454. */
  455. return 1;
  456. }
  457. static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
  458. {
  459. if (bio != NULL && !BIO_up_ref(bio))
  460. return 0;
  461. BIO_free(rl->dummybio);
  462. rl->dummybio = bio;
  463. return 1;
  464. }
  465. /*
  466. * Never called functions
  467. *
  468. * Due to the way we are configured and used we never expect any of the next set
  469. * of functions to be called. Therefore we set them to always fail.
  470. */
  471. static size_t quic_app_data_pending(OSSL_RECORD_LAYER *rl)
  472. {
  473. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  474. return (size_t)ossl_assert(0);
  475. }
  476. static size_t quic_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
  477. {
  478. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  479. return (size_t)ossl_assert(0);
  480. }
  481. static int quic_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
  482. {
  483. QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  484. return ossl_assert(0);
  485. }
  486. /* End of never called functions */
  487. static const OSSL_RECORD_METHOD quic_tls_record_method = {
  488. quic_new_record_layer,
  489. quic_free,
  490. quic_unprocessed_read_pending,
  491. quic_processed_read_pending,
  492. quic_app_data_pending, /* Never called */
  493. quic_get_max_records,
  494. quic_write_records,
  495. quic_retry_write_records,
  496. quic_read_record,
  497. quic_release_record,
  498. quic_get_alert_code,
  499. quic_set1_bio,
  500. quic_set_protocol_version,
  501. quic_set_plain_alerts,
  502. quic_set_first_handshake,
  503. quic_set_max_pipelines,
  504. NULL, /* set_in_init: Optional - we don't need it */
  505. quic_get_state,
  506. quic_set_options,
  507. quic_get_compression,
  508. quic_set_max_frag_len,
  509. quic_get_max_record_overhead, /* Never called */
  510. quic_increment_sequence_ctr, /* Never called */
  511. quic_alloc_buffers,
  512. quic_free_buffers
  513. };
  514. static int add_transport_params_cb(SSL *s, unsigned int ext_type,
  515. unsigned int context,
  516. const unsigned char **out, size_t *outlen,
  517. X509 *x, size_t chainidx, int *al,
  518. void *add_arg)
  519. {
  520. QUIC_TLS *qtls = add_arg;
  521. *out = qtls->local_transport_params;
  522. *outlen = qtls->local_transport_params_len;
  523. qtls->local_transport_params_consumed = 1;
  524. return 1;
  525. }
  526. static void free_transport_params_cb(SSL *s, unsigned int ext_type,
  527. unsigned int context,
  528. const unsigned char *out,
  529. void *add_arg)
  530. {
  531. }
  532. static int parse_transport_params_cb(SSL *s, unsigned int ext_type,
  533. unsigned int context,
  534. const unsigned char *in,
  535. size_t inlen, X509 *x,
  536. size_t chainidx,
  537. int *al, void *parse_arg)
  538. {
  539. QUIC_TLS *qtls = parse_arg;
  540. return qtls->args.got_transport_params_cb(in, inlen,
  541. qtls->args.got_transport_params_cb_arg);
  542. }
  543. QUIC_TLS *ossl_quic_tls_new(const QUIC_TLS_ARGS *args)
  544. {
  545. QUIC_TLS *qtls;
  546. if (args->crypto_send_cb == NULL
  547. || args->crypto_recv_rcd_cb == NULL
  548. || args->crypto_release_rcd_cb == NULL) {
  549. ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
  550. return NULL;
  551. }
  552. qtls = OPENSSL_zalloc(sizeof(*qtls));
  553. if (qtls == NULL)
  554. return NULL;
  555. if (args->ossl_quic && (qtls->error_state = OSSL_ERR_STATE_new()) == NULL) {
  556. OPENSSL_free(qtls);
  557. return NULL;
  558. }
  559. qtls->args = *args;
  560. return qtls;
  561. }
  562. void ossl_quic_tls_free(QUIC_TLS *qtls)
  563. {
  564. if (qtls == NULL)
  565. return;
  566. OSSL_ERR_STATE_free(qtls->error_state);
  567. OPENSSL_free(qtls);
  568. }
  569. static int raise_error(QUIC_TLS *qtls, uint64_t error_code,
  570. const char *error_msg,
  571. const char *src_file,
  572. int src_line,
  573. const char *src_func)
  574. {
  575. /*
  576. * When QTLS fails, add a "cover letter" error with information, potentially
  577. * with any underlying libssl errors underneath it (but our cover error may
  578. * be the only error in some cases). Then capture this into an ERR_STATE so
  579. * we can report it later if need be when the QUIC_CHANNEL asks for it.
  580. * For external QUIC TLS we just raise the error.
  581. */
  582. ERR_new();
  583. ERR_set_debug(src_file, src_line, src_func);
  584. ERR_set_error(ERR_LIB_SSL, SSL_R_QUIC_HANDSHAKE_LAYER_ERROR,
  585. "handshake layer error, error code %llu (0x%llx) (\"%s\")",
  586. error_code, error_code, error_msg);
  587. if (qtls->args.ossl_quic) {
  588. OSSL_ERR_STATE_save_to_mark(qtls->error_state);
  589. /*
  590. * We record the error information reported via the QUIC protocol
  591. * separately.
  592. */
  593. qtls->error_code = error_code;
  594. qtls->error_msg = error_msg;
  595. qtls->inerror = 1;
  596. ERR_pop_to_mark();
  597. }
  598. return 0;
  599. }
  600. #define RAISE_ERROR(qtls, error_code, error_msg) \
  601. raise_error((qtls), (error_code), (error_msg), \
  602. OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC)
  603. #ifndef OPENSSL_NO_QUIC
  604. # define RAISE_INTERNAL_ERROR(qtls) \
  605. RAISE_ERROR((qtls), OSSL_QUIC_ERR_INTERNAL_ERROR, "internal error")
  606. #else
  607. # define RAISE_INTERNAL_ERROR(qtls) \
  608. RAISE_ERROR((qtls), 0x01, "internal error")
  609. #endif
  610. int ossl_quic_tls_configure(QUIC_TLS *qtls)
  611. {
  612. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
  613. BIO *nullbio;
  614. if (sc == NULL || !SSL_set_min_proto_version(qtls->args.s, TLS1_3_VERSION))
  615. return RAISE_INTERNAL_ERROR(qtls);
  616. nullbio = BIO_new(BIO_s_null());
  617. if (nullbio == NULL)
  618. return RAISE_INTERNAL_ERROR(qtls);
  619. /*
  620. * Our custom record layer doesn't use the BIO - but libssl generally
  621. * expects one to be present.
  622. */
  623. SSL_set_bio(qtls->args.s, nullbio, nullbio);
  624. SSL_clear_options(qtls->args.s, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  625. ossl_ssl_set_custom_record_layer(sc, &quic_tls_record_method, qtls);
  626. if (!ossl_tls_add_custom_ext_intern(NULL, &sc->cert->custext,
  627. qtls->args.is_server ? ENDPOINT_SERVER
  628. : ENDPOINT_CLIENT,
  629. TLSEXT_TYPE_quic_transport_parameters,
  630. SSL_EXT_TLS1_3_ONLY
  631. | SSL_EXT_CLIENT_HELLO
  632. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
  633. add_transport_params_cb,
  634. free_transport_params_cb, qtls,
  635. parse_transport_params_cb, qtls))
  636. return 0;
  637. sc->s3.flags |= TLS1_FLAGS_QUIC;
  638. return 1;
  639. }
  640. #ifndef OPENSSL_NO_QUIC
  641. int ossl_quic_tls_tick(QUIC_TLS *qtls)
  642. {
  643. int ret, err;
  644. const unsigned char *alpn;
  645. unsigned int alpnlen;
  646. if (qtls->inerror)
  647. return 0;
  648. /*
  649. * SSL_get_error does not truly know what the cause of an SSL_read failure
  650. * is and to some extent guesses based on contextual information. In
  651. * particular, if there is _any_ ERR on the error stack, SSL_ERROR_SSL or
  652. * SSL_ERROR_SYSCALL will be returned no matter what and there is no
  653. * possibility of SSL_ERROR_WANT_READ/WRITE being returned, even if that was
  654. * the actual cause of the SSL_read() failure.
  655. *
  656. * This means that ordinarily, the below code might not work right if the
  657. * application has any ERR on the error stack. In order to make this code
  658. * perform correctly regardless of prior ERR state, we use a variant of
  659. * SSL_get_error() which ignores the error stack. However, some ERRs are
  660. * raised by SSL_read() and actually indicate that something has gone wrong
  661. * during the call to SSL_read(). We therefore adopt a strategy of marking
  662. * the ERR stack and seeing if any errors get appended during the call to
  663. * SSL_read(). If they are, we assume SSL_read() has raised an error and
  664. * that we should use normal SSL_get_error() handling.
  665. *
  666. * NOTE: Ensure all escape paths from this function call
  667. * ERR_clear_to_mark(). The RAISE macros handle this in failure cases.
  668. */
  669. ERR_set_mark();
  670. if (!qtls->configured) {
  671. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
  672. SSL_CTX *sctx;
  673. if (sc == NULL)
  674. return RAISE_INTERNAL_ERROR(qtls);
  675. sctx = SSL_CONNECTION_GET_CTX(sc);
  676. /*
  677. * No matter how the user has configured us, there are certain
  678. * requirements for QUIC-TLS that we enforce
  679. */
  680. /* ALPN is a requirement for QUIC and must be set */
  681. if (qtls->args.is_server) {
  682. if (sctx->ext.alpn_select_cb == NULL)
  683. return RAISE_INTERNAL_ERROR(qtls);
  684. } else {
  685. if (sc->ext.alpn == NULL || sc->ext.alpn_len == 0)
  686. return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
  687. "ALPN must be configured when using QUIC");
  688. }
  689. if (!ossl_quic_tls_configure(qtls))
  690. return RAISE_INTERNAL_ERROR(qtls);
  691. sc->s3.flags |= TLS1_FLAGS_QUIC_INTERNAL;
  692. if (qtls->args.is_server)
  693. SSL_set_accept_state(qtls->args.s);
  694. else
  695. SSL_set_connect_state(qtls->args.s);
  696. qtls->configured = 1;
  697. }
  698. if (qtls->complete)
  699. /*
  700. * There should never be app data to read, but calling SSL_read() will
  701. * ensure any post-handshake messages are processed.
  702. */
  703. ret = SSL_read(qtls->args.s, NULL, 0);
  704. else
  705. ret = SSL_do_handshake(qtls->args.s);
  706. if (ret <= 0) {
  707. err = ossl_ssl_get_error(qtls->args.s, ret,
  708. /*check_err=*/ERR_count_to_mark() > 0);
  709. switch (err) {
  710. case SSL_ERROR_WANT_READ:
  711. case SSL_ERROR_WANT_WRITE:
  712. case SSL_ERROR_WANT_CLIENT_HELLO_CB:
  713. case SSL_ERROR_WANT_X509_LOOKUP:
  714. case SSL_ERROR_WANT_RETRY_VERIFY:
  715. ERR_pop_to_mark();
  716. return 1;
  717. default:
  718. return RAISE_INTERNAL_ERROR(qtls);
  719. }
  720. }
  721. if (!qtls->complete) {
  722. /* Validate that we have ALPN */
  723. SSL_get0_alpn_selected(qtls->args.s, &alpn, &alpnlen);
  724. if (alpn == NULL || alpnlen == 0)
  725. return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
  726. "no application protocol negotiated");
  727. qtls->complete = 1;
  728. ERR_pop_to_mark();
  729. return qtls->args.handshake_complete_cb(qtls->args.handshake_complete_cb_arg);
  730. }
  731. ERR_pop_to_mark();
  732. return 1;
  733. }
  734. #endif
  735. void ossl_quic_tls_clear(QUIC_TLS *qtls)
  736. {
  737. if (qtls == NULL)
  738. return;
  739. qtls->local_transport_params_consumed = 0;
  740. }
  741. int ossl_quic_tls_set_transport_params(QUIC_TLS *qtls,
  742. const unsigned char *transport_params,
  743. size_t transport_params_len)
  744. {
  745. if (qtls->local_transport_params_consumed)
  746. return 0;
  747. qtls->local_transport_params = transport_params;
  748. qtls->local_transport_params_len = transport_params_len;
  749. return 1;
  750. }
  751. int ossl_quic_tls_get_error(QUIC_TLS *qtls,
  752. uint64_t *error_code,
  753. const char **error_msg,
  754. ERR_STATE **error_state)
  755. {
  756. if (qtls->inerror) {
  757. *error_code = qtls->error_code;
  758. *error_msg = qtls->error_msg;
  759. *error_state = qtls->error_state;
  760. }
  761. return qtls->inerror;
  762. }
  763. /*
  764. * Returns true if the last handshake record message we processed was a
  765. * CertificateRequest
  766. */
  767. int ossl_quic_tls_is_cert_request(QUIC_TLS *qtls)
  768. {
  769. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
  770. if (sc == NULL)
  771. return 0;
  772. return sc->s3.tmp.message_type == SSL3_MT_CERTIFICATE_REQUEST;
  773. }
  774. /*
  775. * Returns true if the last session associated with the connection has an
  776. * invalid max_early_data value for QUIC.
  777. */
  778. int ossl_quic_tls_has_bad_max_early_data(QUIC_TLS *qtls)
  779. {
  780. uint32_t max_early_data = SSL_get0_session(qtls->args.s)->ext.max_early_data;
  781. /*
  782. * If max_early_data was present we always ensure a non-zero value is
  783. * stored in the session for QUIC. Therefore if max_early_data == 0 here
  784. * we can be confident that it was not present in the NewSessionTicket
  785. */
  786. return max_early_data != 0xffffffff && max_early_data != 0;
  787. }
  788. int ossl_quic_tls_set_early_data_enabled(QUIC_TLS *qtls, int enabled)
  789. {
  790. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
  791. if (sc == NULL || !SSL_IS_QUIC_HANDSHAKE(sc) || !SSL_in_before(qtls->args.s))
  792. return 0;
  793. if (!enabled) {
  794. sc->max_early_data = 0;
  795. sc->early_data_state = SSL_EARLY_DATA_NONE;
  796. return 1;
  797. }
  798. if (sc->server) {
  799. sc->max_early_data = 0xffffffff;
  800. sc->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  801. return 1;
  802. }
  803. if ((sc->session == NULL || sc->session->ext.max_early_data != 0xffffffff)
  804. && sc->psk_use_session_cb == NULL)
  805. return 0;
  806. sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
  807. return 1;
  808. }