quic.h 10 KB

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