quic.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* quic.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* wolfSSL QUIC API */
  22. #ifndef WOLFSSL_QUIC_H
  23. #define WOLFSSL_QUIC_H
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #ifdef WOLFSSL_QUIC
  28. #include <stdint.h>
  29. /* QUIC operates on three encryption levels which determine
  30. * which keys/algos are used for de-/encryption. These are
  31. * kept separately for incoming and outgoing data and.
  32. * Due to the nature of UDP, more than one might be in use
  33. * at the same time due to resends or out-of-order arrivals.
  34. */
  35. typedef enum wolfssl_encryption_level_t {
  36. wolfssl_encryption_initial = 0,
  37. wolfssl_encryption_early_data,
  38. wolfssl_encryption_handshake,
  39. wolfssl_encryption_application
  40. } WOLFSSL_ENCRYPTION_LEVEL;
  41. /* All QUIC related callbacks to the application.
  42. */
  43. typedef struct wolfssl_quic_method_t WOLFSSL_QUIC_METHOD;
  44. struct wolfssl_quic_method_t {
  45. /**
  46. * Provide secrets to the QUIC stack when they become available in the SSL
  47. * instance during handshake processing. read/write secrets have the same
  48. * length. A call may only provide one, passing NULL as the other.
  49. */
  50. int (*set_encryption_secrets)(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  51. const uint8_t* read_secret,
  52. const uint8_t* write_secret,
  53. size_t secret_len);
  54. /**
  55. * Provide handshake packets to the QUIC stack to send to the peer. The
  56. * QUIC stack will wrap these and take care of re-transmissions.
  57. */
  58. int (*add_handshake_data)(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  59. const uint8_t* data, size_t len);
  60. /**
  61. * Flush any buffered packets during handshake.
  62. */
  63. int (*flush_flight)(WOLFSSL* ssl);
  64. /**
  65. * Send a TLS alert that happened during handshake. In QUIC, such alerts
  66. * lead to connection shutdown.
  67. */
  68. int (*send_alert)(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  69. uint8_t alert);
  70. };
  71. /**
  72. * Mark the given SSL context for QUIC protocol handling. Meaning all
  73. * SSL instances derived from it will inherit this. Provides all callbacks
  74. * to the QUIC application the SSL stack needs.
  75. */
  76. WOLFSSL_API
  77. int wolfSSL_CTX_set_quic_method(WOLFSSL_CTX* ctx,
  78. const WOLFSSL_QUIC_METHOD* quic_method);
  79. /**
  80. * Mark exactly this SSL instance for QUIC protocol handling.
  81. * Provides all callbacks to the QUIC application the SSL stack needs.
  82. */
  83. WOLFSSL_API
  84. int wolfSSL_set_quic_method(WOLFSSL* ssl,
  85. const WOLFSSL_QUIC_METHOD* quic_method);
  86. /**
  87. * Check if QUIC handling has been installed on the given SSL instance.
  88. */
  89. WOLFSSL_API int wolfSSL_is_quic(WOLFSSL* ssl);
  90. /**
  91. * Return the current encryption level of the SSL instance for READs.
  92. */
  93. WOLFSSL_API
  94. WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_read_level(const WOLFSSL* ssl);
  95. /**
  96. * Return the current encryption level of the SSL instance for WRITEs.
  97. */
  98. WOLFSSL_API
  99. WOLFSSL_ENCRYPTION_LEVEL wolfSSL_quic_write_level(const WOLFSSL* ssl);
  100. /**
  101. * Configure the QUIC transport version to use. On `use_legacy` != 0,
  102. * selects TLSX_KEY_QUIC_TP_PARAMS_DRAFT, otherwise TLSX_KEY_QUIC_TP_PARAMS.
  103. * This method is part of the BoringSSL API and replicated here for app
  104. * portability (as in quictls/openssl).
  105. */
  106. WOLFSSL_API
  107. void wolfSSL_set_quic_use_legacy_codepoint(WOLFSSL* ssl, int use_legacy);
  108. /**
  109. * Set the TLS extension for the transport parameter version to announce
  110. * to the peer. Known values are TLSX_KEY_QUIC_TP_PARAMS (V1) and
  111. * TLSX_KEY_QUIC_TP_PARAMS_DRAFT.
  112. * Setting it to 0 will announce both V1 and draft versions to a server.
  113. * Servers will, on 0, select the latest version seen from the client.
  114. * Default is 0.
  115. */
  116. WOLFSSL_API
  117. void wolfSSL_set_quic_transport_version(WOLFSSL* ssl, int version);
  118. /**
  119. * Get the configured transport version.
  120. */
  121. WOLFSSL_API int wolfSSL_get_quic_transport_version(const WOLFSSL* ssl);
  122. /**
  123. * Set the raw QUIC transport parameter that will be sent in the TLS extension
  124. * to the peer, using the configured transport version(s).
  125. */
  126. WOLFSSL_API int wolfSSL_set_quic_transport_params(WOLFSSL* ssl,
  127. const uint8_t* params,
  128. size_t params_len);
  129. /**
  130. * Get the raw QUIC transport parameter as retrieved via TLS Extension
  131. * from the peer. If the peer announced several versions,
  132. * return the latest one.
  133. * If the extension has not arrived yet, initializes out parameter to
  134. * NULL, resp. 0.
  135. */
  136. WOLFSSL_API
  137. void wolfSSL_get_peer_quic_transport_params(const WOLFSSL* ssl,
  138. const uint8_t* *out_params,
  139. size_t* out_params_len);
  140. /**
  141. * Get the QUIC version negotiated with the peer during the handshake.
  142. */
  143. WOLFSSL_API int wolfSSL_get_peer_quic_transport_version(const WOLFSSL* ssl);
  144. #ifdef WOLFSSL_EARLY_DATA
  145. WOLFSSL_API void wolfSSL_set_quic_early_data_enabled(WOLFSSL* ssl, int enabled);
  146. #endif
  147. /**
  148. * Advisory amount of the maximum data a QUIC protocol handler should have
  149. * in flight. This varies during handshake processing, for example certificate
  150. * exchange will increase the limit.
  151. */
  152. WOLFSSL_API
  153. size_t wolfSSL_quic_max_handshake_flight_len(const WOLFSSL* ssl,
  154. WOLFSSL_ENCRYPTION_LEVEL level);
  155. /**
  156. * The QUIC protocol handler provides peer TLS records to the SSL instance
  157. * during handshake to progress it. The SSL instance will use the registered
  158. * callbacks to send packets to the peer.
  159. * Encryption level is provided to indicate how to decrypt the data. Data may
  160. * be added for levels not yet reached by the SSL instance. However, data
  161. * may only be added in ever increasing levels and levels may only increase
  162. * at TLS record boundaries. Any violation will make this function fail.
  163. */
  164. WOLFSSL_API
  165. int wolfSSL_provide_quic_data(WOLFSSL* ssl, WOLFSSL_ENCRYPTION_LEVEL level,
  166. const uint8_t* data, size_t len);
  167. WOLFSSL_API
  168. int wolfSSL_quic_do_handshake(WOLFSSL* ssl);
  169. /**
  170. * Process any CRYPTO data added post-handshake.
  171. */
  172. WOLFSSL_API int wolfSSL_process_quic_post_handshake(WOLFSSL* ssl);
  173. /**
  174. * Process any pending input and flush all output. Can be invoked
  175. * during and/or after handshake processing.
  176. */
  177. WOLFSSL_API int wolfSSL_quic_read_write(WOLFSSL* ssl);
  178. /**
  179. * Get the AEAD cipher that is currently selected in the SSL instance.
  180. * Will return NULL if none has been selected so far. This is used by the
  181. * QUIC stack to encrypt/decrypt packets after the handshake.
  182. */
  183. WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_quic_get_aead(WOLFSSL* ssl);
  184. /**
  185. * Use to classify the AEAD cipher for key reuse limits.
  186. */
  187. WOLFSSL_API int wolfSSL_quic_aead_is_gcm(const WOLFSSL_EVP_CIPHER* aead_cipher);
  188. WOLFSSL_API int wolfSSL_quic_aead_is_ccm(const WOLFSSL_EVP_CIPHER* aead_cipher);
  189. WOLFSSL_API
  190. int wolfSSL_quic_aead_is_chacha20(const WOLFSSL_EVP_CIPHER* aead_cipher);
  191. /**
  192. * Get the 'tag' length used by the AEAD cipher. Encryption buffer lengths
  193. * are plaintext length plus this tag length.
  194. */
  195. WOLFSSL_API
  196. size_t wolfSSL_quic_get_aead_tag_len(const WOLFSSL_EVP_CIPHER* aead_cipher);
  197. /**
  198. * The message digest currently selected in the SSL instance.
  199. */
  200. WOLFSSL_API const WOLFSSL_EVP_MD* wolfSSL_quic_get_md(WOLFSSL* ssl);
  201. /**
  202. * The QUIC header protection cipher matching the AEAD cipher currently
  203. * selected in the SSL instance.
  204. */
  205. WOLFSSL_API const WOLFSSL_EVP_CIPHER* wolfSSL_quic_get_hp(WOLFSSL* ssl);
  206. /**
  207. * Create and initialize a cipher context for use in en- or decryption.
  208. */
  209. WOLFSSL_API WOLFSSL_EVP_CIPHER_CTX*
  210. wolfSSL_quic_crypt_new(const WOLFSSL_EVP_CIPHER* cipher,
  211. const uint8_t* key, const uint8_t* iv, int encrypt);
  212. /**
  213. * Use a previously created cipher context to encrypt the given plain text.
  214. */
  215. WOLFSSL_API
  216. int wolfSSL_quic_aead_encrypt(uint8_t* dest, WOLFSSL_EVP_CIPHER_CTX* aead_ctx,
  217. const uint8_t* plain, size_t plainlen,
  218. const uint8_t* iv, const uint8_t* aad,
  219. size_t aadlen);
  220. /**
  221. * Use a previously created cipher context to decrypt the given encoded text.
  222. */
  223. WOLFSSL_API
  224. int wolfSSL_quic_aead_decrypt(uint8_t* dest, WOLFSSL_EVP_CIPHER_CTX* ctx,
  225. const uint8_t* enc, size_t enclen,
  226. const uint8_t* iv, const uint8_t* aad,
  227. size_t aadlen);
  228. /**
  229. * Extract a pseudo-random key, using the given message digest, a secret
  230. * and a salt. The key size is the size of the digest.
  231. */
  232. WOLFSSL_API
  233. int wolfSSL_quic_hkdf_extract(uint8_t* dest, const WOLFSSL_EVP_MD* md,
  234. const uint8_t* secret, size_t secretlen,
  235. const uint8_t* salt, size_t saltlen);
  236. /**
  237. * Expand a pseudo-random key (secret) into a new key, using the mesasge
  238. * digest and the info bytes.
  239. */
  240. WOLFSSL_API
  241. int wolfSSL_quic_hkdf_expand(uint8_t* dest, size_t destlen,
  242. const WOLFSSL_EVP_MD* md,
  243. const uint8_t* secret, size_t secretlen,
  244. const uint8_t* info, size_t infolen);
  245. /**
  246. * Extract and extpand secret, salt and info into a new key.
  247. */
  248. WOLFSSL_API
  249. int wolfSSL_quic_hkdf(uint8_t* dest, size_t destlen,
  250. const WOLFSSL_EVP_MD* md,
  251. const uint8_t* secret, size_t secretlen,
  252. const uint8_t* salt, size_t saltlen,
  253. const uint8_t* info, size_t infolen);
  254. #endif /* WOLFSSL_QUIC */
  255. #ifdef __cplusplus
  256. } /* extern "C" */
  257. #endif
  258. #endif /* WOLFSSL_QUIC_H */