ssl_quic.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * Copyright 2019 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 "ssl_local.h"
  10. #include "internal/cryptlib.h"
  11. #include "internal/refcount.h"
  12. int SSL_set_quic_transport_params(SSL *ssl, const uint8_t *params,
  13. size_t params_len)
  14. {
  15. uint8_t *tmp;
  16. if (params == NULL || params_len == 0) {
  17. tmp = NULL;
  18. params_len = 0;
  19. } else {
  20. tmp = OPENSSL_memdup(params, params_len);
  21. if (tmp == NULL)
  22. return 0;
  23. }
  24. OPENSSL_free(ssl->ext.quic_transport_params);
  25. ssl->ext.quic_transport_params = tmp;
  26. ssl->ext.quic_transport_params_len = params_len;
  27. return 1;
  28. }
  29. void SSL_get_peer_quic_transport_params(const SSL *ssl,
  30. const uint8_t **out_params,
  31. size_t *out_params_len)
  32. {
  33. if (ssl->ext.peer_quic_transport_params_len) {
  34. *out_params = ssl->ext.peer_quic_transport_params;
  35. *out_params_len = ssl->ext.peer_quic_transport_params_len;
  36. } else {
  37. *out_params = ssl->ext.peer_quic_transport_params_draft;
  38. *out_params_len = ssl->ext.peer_quic_transport_params_draft_len;
  39. }
  40. }
  41. /* Returns the negotiated version, or -1 on error */
  42. int SSL_get_peer_quic_transport_version(const SSL *ssl)
  43. {
  44. if (ssl->ext.peer_quic_transport_params_len != 0
  45. && ssl->ext.peer_quic_transport_params_draft_len != 0)
  46. return -1;
  47. if (ssl->ext.peer_quic_transport_params_len != 0)
  48. return TLSEXT_TYPE_quic_transport_parameters;
  49. if (ssl->ext.peer_quic_transport_params_draft_len != 0)
  50. return TLSEXT_TYPE_quic_transport_parameters_draft;
  51. return -1;
  52. }
  53. void SSL_set_quic_use_legacy_codepoint(SSL *ssl, int use_legacy)
  54. {
  55. if (use_legacy)
  56. ssl->quic_transport_version = TLSEXT_TYPE_quic_transport_parameters_draft;
  57. else
  58. ssl->quic_transport_version = TLSEXT_TYPE_quic_transport_parameters;
  59. }
  60. void SSL_set_quic_transport_version(SSL *ssl, int version)
  61. {
  62. ssl->quic_transport_version = version;
  63. }
  64. int SSL_get_quic_transport_version(const SSL *ssl)
  65. {
  66. return ssl->quic_transport_version;
  67. }
  68. size_t SSL_quic_max_handshake_flight_len(const SSL *ssl, OSSL_ENCRYPTION_LEVEL level)
  69. {
  70. /*
  71. * Limits flights to 16K by default when there are no large
  72. * (certificate-carrying) messages.
  73. */
  74. static const size_t DEFAULT_FLIGHT_LIMIT = 16384;
  75. switch (level) {
  76. case ssl_encryption_initial:
  77. return DEFAULT_FLIGHT_LIMIT;
  78. case ssl_encryption_early_data:
  79. /* QUIC does not send EndOfEarlyData. */
  80. return 0;
  81. case ssl_encryption_handshake:
  82. if (ssl->server) {
  83. /*
  84. * Servers may receive Certificate message if configured to request
  85. * client certificates.
  86. */
  87. if ((ssl->verify_mode & SSL_VERIFY_PEER)
  88. && ssl->max_cert_list > DEFAULT_FLIGHT_LIMIT)
  89. return ssl->max_cert_list;
  90. } else {
  91. /*
  92. * Clients may receive both Certificate message and a CertificateRequest
  93. * message.
  94. */
  95. if (2*ssl->max_cert_list > DEFAULT_FLIGHT_LIMIT)
  96. return 2 * ssl->max_cert_list;
  97. }
  98. return DEFAULT_FLIGHT_LIMIT;
  99. case ssl_encryption_application:
  100. return DEFAULT_FLIGHT_LIMIT;
  101. }
  102. return 0;
  103. }
  104. OSSL_ENCRYPTION_LEVEL SSL_quic_read_level(const SSL *ssl)
  105. {
  106. return ssl->quic_read_level;
  107. }
  108. OSSL_ENCRYPTION_LEVEL SSL_quic_write_level(const SSL *ssl)
  109. {
  110. return ssl->quic_write_level;
  111. }
  112. int SSL_provide_quic_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL level,
  113. const uint8_t *data, size_t len)
  114. {
  115. size_t l, offset;
  116. if (!SSL_IS_QUIC(ssl)) {
  117. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  118. return 0;
  119. }
  120. /* Level can be different than the current read, but not less */
  121. if (level < ssl->quic_read_level
  122. || (ssl->quic_input_data_tail != NULL && level < ssl->quic_input_data_tail->level)
  123. || level < ssl->quic_latest_level_received) {
  124. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
  125. return 0;
  126. }
  127. if (len == 0)
  128. return 1;
  129. if (ssl->quic_buf == NULL) {
  130. BUF_MEM *buf;
  131. if ((buf = BUF_MEM_new()) == NULL) {
  132. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, ERR_R_INTERNAL_ERROR);
  133. return 0;
  134. }
  135. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  136. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, ERR_R_INTERNAL_ERROR);
  137. BUF_MEM_free(buf);
  138. return 0;
  139. }
  140. ssl->quic_buf = buf;
  141. /* We preallocated storage, but there's still no *data*. */
  142. ssl->quic_buf->length = 0;
  143. buf = NULL;
  144. }
  145. /* A TLS message must not cross an encryption level boundary */
  146. if (ssl->quic_buf->length != ssl->quic_next_record_start
  147. && level != ssl->quic_latest_level_received) {
  148. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA,
  149. SSL_R_WRONG_ENCRYPTION_LEVEL_RECEIVED);
  150. return 0;
  151. }
  152. ssl->quic_latest_level_received = level;
  153. offset = ssl->quic_buf->length;
  154. if (!BUF_MEM_grow(ssl->quic_buf, offset + len)) {
  155. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, ERR_R_INTERNAL_ERROR);
  156. return 0;
  157. }
  158. memcpy(ssl->quic_buf->data + offset, data, len);
  159. /* Split on handshake message boundaries */
  160. while (ssl->quic_buf->length > ssl->quic_next_record_start
  161. + SSL3_HM_HEADER_LENGTH) {
  162. QUIC_DATA *qd;
  163. const uint8_t *p;
  164. /* TLS Handshake message header has 1-byte type and 3-byte length */
  165. p = (const uint8_t *)ssl->quic_buf->data
  166. + ssl->quic_next_record_start + 1;
  167. n2l3(p, l);
  168. l += SSL3_HM_HEADER_LENGTH;
  169. /* Don't allocate a QUIC_DATA if we don't have a full record */
  170. if (l > ssl->quic_buf->length - ssl->quic_next_record_start)
  171. break;
  172. qd = OPENSSL_zalloc(sizeof(*qd));
  173. if (qd == NULL) {
  174. SSLerr(SSL_F_SSL_PROVIDE_QUIC_DATA, ERR_R_INTERNAL_ERROR);
  175. return 0;
  176. }
  177. qd->next = NULL;
  178. qd->length = l;
  179. qd->start = ssl->quic_next_record_start;
  180. qd->level = level;
  181. if (ssl->quic_input_data_tail != NULL)
  182. ssl->quic_input_data_tail->next = qd;
  183. else
  184. ssl->quic_input_data_head = qd;
  185. ssl->quic_input_data_tail = qd;
  186. ssl->quic_next_record_start += l;
  187. }
  188. return 1;
  189. }
  190. int SSL_CTX_set_quic_method(SSL_CTX *ctx, const SSL_QUIC_METHOD *quic_method)
  191. {
  192. if (ctx->method->version != TLS_ANY_VERSION)
  193. return 0;
  194. ctx->quic_method = quic_method;
  195. ctx->options &= ~SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  196. return 1;
  197. }
  198. int SSL_set_quic_method(SSL *ssl, const SSL_QUIC_METHOD *quic_method)
  199. {
  200. if (ssl->method->version != TLS_ANY_VERSION)
  201. return 0;
  202. ssl->quic_method = quic_method;
  203. ssl->options &= ~SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  204. return 1;
  205. }
  206. int quic_set_encryption_secrets(SSL *ssl, OSSL_ENCRYPTION_LEVEL level)
  207. {
  208. uint8_t *c2s_secret = NULL;
  209. uint8_t *s2c_secret = NULL;
  210. size_t len;
  211. const EVP_MD *md;
  212. if (!SSL_IS_QUIC(ssl))
  213. return 1;
  214. /* secrets from the POV of the client */
  215. switch (level) {
  216. case ssl_encryption_early_data:
  217. c2s_secret = ssl->client_early_traffic_secret;
  218. break;
  219. case ssl_encryption_handshake:
  220. c2s_secret = ssl->client_hand_traffic_secret;
  221. s2c_secret = ssl->server_hand_traffic_secret;
  222. break;
  223. case ssl_encryption_application:
  224. c2s_secret = ssl->client_app_traffic_secret;
  225. s2c_secret = ssl->server_app_traffic_secret;
  226. break;
  227. default:
  228. return 1;
  229. }
  230. if (level == ssl_encryption_early_data) {
  231. const SSL_CIPHER *c = SSL_SESSION_get0_cipher(ssl->session);
  232. if (ssl->early_data_state == SSL_EARLY_DATA_CONNECTING
  233. && ssl->max_early_data > 0
  234. && ssl->session->ext.max_early_data == 0) {
  235. if (!ossl_assert(ssl->psksession != NULL
  236. && ssl->max_early_data
  237. == ssl->psksession->ext.max_early_data)) {
  238. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR,
  239. SSL_F_QUIC_SET_ENCRYPTION_SECRETS,
  240. ERR_R_INTERNAL_ERROR);
  241. return 0;
  242. }
  243. c = SSL_SESSION_get0_cipher(ssl->psksession);
  244. }
  245. if (c == NULL) {
  246. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR,
  247. SSL_F_QUIC_SET_ENCRYPTION_SECRETS, ERR_R_INTERNAL_ERROR);
  248. return 0;
  249. }
  250. md = ssl_md(c->algorithm2);
  251. } else {
  252. md = ssl_handshake_md(ssl);
  253. if (md == NULL) {
  254. /* May not have selected cipher, yet */
  255. const SSL_CIPHER *c = NULL;
  256. /*
  257. * It probably doesn't make sense to use an (external) PSK session,
  258. * but in theory some kinds of external session caches could be
  259. * implemented using it, so allow psksession to be used as well as
  260. * the regular session.
  261. */
  262. if (ssl->session != NULL)
  263. c = SSL_SESSION_get0_cipher(ssl->session);
  264. else if (ssl->psksession != NULL)
  265. c = SSL_SESSION_get0_cipher(ssl->psksession);
  266. if (c != NULL)
  267. md = SSL_CIPHER_get_handshake_digest(c);
  268. }
  269. }
  270. if ((len = EVP_MD_size(md)) <= 0) {
  271. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_SET_ENCRYPTION_SECRETS,
  272. ERR_R_INTERNAL_ERROR);
  273. return 0;
  274. }
  275. if (ssl->server) {
  276. if (!ssl->quic_method->set_encryption_secrets(ssl, level, c2s_secret,
  277. s2c_secret, len)) {
  278. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_SET_ENCRYPTION_SECRETS,
  279. ERR_R_INTERNAL_ERROR);
  280. return 0;
  281. }
  282. } else {
  283. if (!ssl->quic_method->set_encryption_secrets(ssl, level, s2c_secret,
  284. c2s_secret, len)) {
  285. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_QUIC_SET_ENCRYPTION_SECRETS,
  286. ERR_R_INTERNAL_ERROR);
  287. return 0;
  288. }
  289. }
  290. return 1;
  291. }
  292. int SSL_process_quic_post_handshake(SSL *ssl)
  293. {
  294. int ret;
  295. if (SSL_in_init(ssl) || !SSL_IS_QUIC(ssl)) {
  296. SSLerr(SSL_F_SSL_PROCESS_QUIC_POST_HANDSHAKE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  297. return 0;
  298. }
  299. /* if there is no data, return success as BoringSSL */
  300. while (ssl->quic_input_data_head != NULL) {
  301. /*
  302. * This is always safe (we are sure to be at a record boundary) because
  303. * SSL_read()/SSL_write() are never used for QUIC connections -- the
  304. * application data is handled at the QUIC layer instead.
  305. */
  306. ossl_statem_set_in_init(ssl, 1);
  307. ret = ssl->handshake_func(ssl);
  308. ossl_statem_set_in_init(ssl, 0);
  309. if (ret <= 0)
  310. return 0;
  311. }
  312. return 1;
  313. }
  314. int SSL_is_quic(SSL* ssl)
  315. {
  316. return SSL_IS_QUIC(ssl);
  317. }
  318. void SSL_set_quic_early_data_enabled(SSL *ssl, int enabled)
  319. {
  320. if (!SSL_is_quic(ssl) || !SSL_in_before(ssl))
  321. return;
  322. if (!enabled) {
  323. ssl->early_data_state = SSL_EARLY_DATA_NONE;
  324. return;
  325. }
  326. if (ssl->server) {
  327. ssl->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  328. return;
  329. }
  330. if ((ssl->session == NULL || ssl->session->ext.max_early_data == 0)
  331. && ssl->psk_use_session_cb == NULL)
  332. return;
  333. ssl->early_data_state = SSL_EARLY_DATA_CONNECTING;
  334. }