quic_record_util.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2022-2023 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 "internal/quic_record_util.h"
  10. #include "internal/quic_record_rx.h"
  11. #include "internal/quic_record_tx.h"
  12. #include "internal/quic_wire_pkt.h"
  13. #include "../ssl_local.h"
  14. #include <openssl/kdf.h>
  15. #include <openssl/core_names.h>
  16. /*
  17. * QUIC Key Derivation Utilities
  18. * =============================
  19. */
  20. int ossl_quic_hkdf_extract(OSSL_LIB_CTX *libctx,
  21. const char *propq,
  22. const EVP_MD *md,
  23. const unsigned char *salt, size_t salt_len,
  24. const unsigned char *ikm, size_t ikm_len,
  25. unsigned char *out, size_t out_len)
  26. {
  27. int ret = 0;
  28. EVP_KDF *kdf = NULL;
  29. EVP_KDF_CTX *kctx = NULL;
  30. OSSL_PARAM params[7], *p = params;
  31. int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
  32. const char *md_name;
  33. if ((md_name = EVP_MD_get0_name(md)) == NULL
  34. || (kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_HKDF, propq)) == NULL
  35. || (kctx = EVP_KDF_CTX_new(kdf)) == NULL)
  36. goto err;
  37. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
  38. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  39. (char *)md_name, 0);
  40. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  41. (unsigned char *)salt, salt_len);
  42. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  43. (unsigned char *)ikm, ikm_len);
  44. *p++ = OSSL_PARAM_construct_end();
  45. ret = EVP_KDF_derive(kctx, out, out_len, params);
  46. err:
  47. EVP_KDF_CTX_free(kctx);
  48. EVP_KDF_free(kdf);
  49. return ret;
  50. }
  51. /* Constants used for key derivation in QUIC v1. */
  52. static const unsigned char quic_client_in_label[] = {
  53. 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x69, 0x6e /* "client in" */
  54. };
  55. static const unsigned char quic_server_in_label[] = {
  56. 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x20, 0x69, 0x6e /* "server in" */
  57. };
  58. /* Salt used to derive Initial packet protection keys (RFC 9001 Section 5.2). */
  59. static const unsigned char quic_v1_initial_salt[] = {
  60. 0x38, 0x76, 0x2c, 0xf7, 0xf5, 0x59, 0x34, 0xb3, 0x4d, 0x17,
  61. 0x9a, 0xe6, 0xa4, 0xc8, 0x0c, 0xad, 0xcc, 0xbb, 0x7f, 0x0a
  62. };
  63. int ossl_quic_provide_initial_secret(OSSL_LIB_CTX *libctx,
  64. const char *propq,
  65. const QUIC_CONN_ID *dst_conn_id,
  66. int is_server,
  67. struct ossl_qrx_st *qrx,
  68. struct ossl_qtx_st *qtx)
  69. {
  70. unsigned char initial_secret[32];
  71. unsigned char client_initial_secret[32], server_initial_secret[32];
  72. unsigned char *rx_secret, *tx_secret;
  73. EVP_MD *sha256;
  74. if (qrx == NULL && qtx == NULL)
  75. return 1;
  76. /* Initial encryption always uses SHA-256. */
  77. if ((sha256 = EVP_MD_fetch(libctx, "SHA256", propq)) == NULL)
  78. return 0;
  79. if (is_server) {
  80. rx_secret = client_initial_secret;
  81. tx_secret = server_initial_secret;
  82. } else {
  83. rx_secret = server_initial_secret;
  84. tx_secret = client_initial_secret;
  85. }
  86. /* Derive initial secret from destination connection ID. */
  87. if (!ossl_quic_hkdf_extract(libctx, propq,
  88. sha256,
  89. quic_v1_initial_salt,
  90. sizeof(quic_v1_initial_salt),
  91. dst_conn_id->id,
  92. dst_conn_id->id_len,
  93. initial_secret,
  94. sizeof(initial_secret)))
  95. goto err;
  96. /* Derive "client in" secret. */
  97. if (((qtx != NULL && tx_secret == client_initial_secret)
  98. || (qrx != NULL && rx_secret == client_initial_secret))
  99. && !tls13_hkdf_expand_ex(libctx, propq,
  100. sha256,
  101. initial_secret,
  102. quic_client_in_label,
  103. sizeof(quic_client_in_label),
  104. NULL, 0,
  105. client_initial_secret,
  106. sizeof(client_initial_secret), 1))
  107. goto err;
  108. /* Derive "server in" secret. */
  109. if (((qtx != NULL && tx_secret == server_initial_secret)
  110. || (qrx != NULL && rx_secret == server_initial_secret))
  111. && !tls13_hkdf_expand_ex(libctx, propq,
  112. sha256,
  113. initial_secret,
  114. quic_server_in_label,
  115. sizeof(quic_server_in_label),
  116. NULL, 0,
  117. server_initial_secret,
  118. sizeof(server_initial_secret), 1))
  119. goto err;
  120. /* Setup RX EL. Initial encryption always uses AES-128-GCM. */
  121. if (qrx != NULL
  122. && !ossl_qrx_provide_secret(qrx, QUIC_ENC_LEVEL_INITIAL,
  123. QRL_SUITE_AES128GCM,
  124. sha256,
  125. rx_secret,
  126. sizeof(server_initial_secret)))
  127. goto err;
  128. /*
  129. * ossl_qrx_provide_secret takes ownership of our ref to SHA256, so if we
  130. * are initialising both sides, get a new ref for the following call for the
  131. * TX side.
  132. */
  133. if (qrx != NULL && qtx != NULL && !EVP_MD_up_ref(sha256)) {
  134. sha256 = NULL;
  135. goto err;
  136. }
  137. /* Setup TX cipher. */
  138. if (qtx != NULL
  139. && !ossl_qtx_provide_secret(qtx, QUIC_ENC_LEVEL_INITIAL,
  140. QRL_SUITE_AES128GCM,
  141. sha256,
  142. tx_secret,
  143. sizeof(server_initial_secret)))
  144. goto err;
  145. return 1;
  146. err:
  147. EVP_MD_free(sha256);
  148. return 0;
  149. }
  150. /*
  151. * QUIC Record Layer Ciphersuite Info
  152. * ==================================
  153. */
  154. struct suite_info {
  155. const char *cipher_name, *md_name;
  156. uint32_t secret_len, cipher_key_len, cipher_iv_len, cipher_tag_len;
  157. uint32_t hdr_prot_key_len, hdr_prot_cipher_id;
  158. uint64_t max_pkt, max_forged_pkt;
  159. };
  160. static const struct suite_info suite_aes128gcm = {
  161. "AES-128-GCM", "SHA256", 32, 16, 12, 16, 16,
  162. QUIC_HDR_PROT_CIPHER_AES_128,
  163. ((uint64_t)1) << 23, /* Limits as prescribed by RFC 9001 */
  164. ((uint64_t)1) << 52,
  165. };
  166. static const struct suite_info suite_aes256gcm = {
  167. "AES-256-GCM", "SHA384", 48, 32, 12, 16, 32,
  168. QUIC_HDR_PROT_CIPHER_AES_256,
  169. ((uint64_t)1) << 23, /* Limits as prescribed by RFC 9001 */
  170. ((uint64_t)1) << 52,
  171. };
  172. static const struct suite_info suite_chacha20poly1305 = {
  173. "ChaCha20-Poly1305", "SHA256", 32, 32, 12, 16, 32,
  174. QUIC_HDR_PROT_CIPHER_CHACHA,
  175. /* Do not use UINT64_MAX here as this represents an invalid value */
  176. UINT64_MAX - 1, /* No applicable limit for this suite (RFC 9001) */
  177. ((uint64_t)1) << 36, /* Limit as prescribed by RFC 9001 */
  178. };
  179. static const struct suite_info *get_suite(uint32_t suite_id)
  180. {
  181. switch (suite_id) {
  182. case QRL_SUITE_AES128GCM:
  183. return &suite_aes128gcm;
  184. case QRL_SUITE_AES256GCM:
  185. return &suite_aes256gcm;
  186. case QRL_SUITE_CHACHA20POLY1305:
  187. return &suite_chacha20poly1305;
  188. default:
  189. return NULL;
  190. }
  191. }
  192. const char *ossl_qrl_get_suite_cipher_name(uint32_t suite_id)
  193. {
  194. const struct suite_info *c = get_suite(suite_id);
  195. return c != NULL ? c->cipher_name : NULL;
  196. }
  197. const char *ossl_qrl_get_suite_md_name(uint32_t suite_id)
  198. {
  199. const struct suite_info *c = get_suite(suite_id);
  200. return c != NULL ? c->md_name : NULL;
  201. }
  202. uint32_t ossl_qrl_get_suite_secret_len(uint32_t suite_id)
  203. {
  204. const struct suite_info *c = get_suite(suite_id);
  205. return c != NULL ? c->secret_len : 0;
  206. }
  207. uint32_t ossl_qrl_get_suite_cipher_key_len(uint32_t suite_id)
  208. {
  209. const struct suite_info *c = get_suite(suite_id);
  210. return c != NULL ? c->cipher_key_len : 0;
  211. }
  212. uint32_t ossl_qrl_get_suite_cipher_iv_len(uint32_t suite_id)
  213. {
  214. const struct suite_info *c = get_suite(suite_id);
  215. return c != NULL ? c->cipher_iv_len : 0;
  216. }
  217. uint32_t ossl_qrl_get_suite_cipher_tag_len(uint32_t suite_id)
  218. {
  219. const struct suite_info *c = get_suite(suite_id);
  220. return c != NULL ? c->cipher_tag_len : 0;
  221. }
  222. uint32_t ossl_qrl_get_suite_hdr_prot_cipher_id(uint32_t suite_id)
  223. {
  224. const struct suite_info *c = get_suite(suite_id);
  225. return c != NULL ? c->hdr_prot_cipher_id : 0;
  226. }
  227. uint32_t ossl_qrl_get_suite_hdr_prot_key_len(uint32_t suite_id)
  228. {
  229. const struct suite_info *c = get_suite(suite_id);
  230. return c != NULL ? c->hdr_prot_key_len : 0;
  231. }
  232. uint64_t ossl_qrl_get_suite_max_pkt(uint32_t suite_id)
  233. {
  234. const struct suite_info *c = get_suite(suite_id);
  235. return c != NULL ? c->max_pkt : UINT64_MAX;
  236. }
  237. uint64_t ossl_qrl_get_suite_max_forged_pkt(uint32_t suite_id)
  238. {
  239. const struct suite_info *c = get_suite(suite_id);
  240. return c != NULL ? c->max_forged_pkt : UINT64_MAX;
  241. }