src/event/quic/ngx_event_quic_protection.h - nginx source code

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Nginx, Inc.
  3. */


  4. #ifndef _NGX_EVENT_QUIC_PROTECTION_H_INCLUDED_
  5. #define _NGX_EVENT_QUIC_PROTECTION_H_INCLUDED_


  6. #include <ngx_config.h>
  7. #include <ngx_core.h>

  8. #include <ngx_event_quic_transport.h>


  9. #define NGX_QUIC_ENCRYPTION_LAST  ((ssl_encryption_application) + 1)

  10. /* RFC 5116, 5.1/5.3 and RFC 8439, 2.3/2.5 for all supported ciphers */
  11. #define NGX_QUIC_IV_LEN               12
  12. #define NGX_QUIC_TAG_LEN              16

  13. /* largest hash used in TLS is SHA-384 */
  14. #define NGX_QUIC_MAX_MD_SIZE          48


  15. #ifdef OPENSSL_IS_BORINGSSL
  16. #define ngx_quic_cipher_t             EVP_AEAD
  17. #define ngx_quic_crypto_ctx_t         EVP_AEAD_CTX
  18. #else
  19. #define ngx_quic_cipher_t             EVP_CIPHER
  20. #define ngx_quic_crypto_ctx_t         EVP_CIPHER_CTX
  21. #endif


  22. typedef struct {
  23.     size_t                    len;
  24.     u_char                    data[NGX_QUIC_MAX_MD_SIZE];
  25. } ngx_quic_md_t;


  26. typedef struct {
  27.     size_t                    len;
  28.     u_char                    data[NGX_QUIC_IV_LEN];
  29. } ngx_quic_iv_t;


  30. typedef struct {
  31.     ngx_quic_md_t             secret;
  32.     ngx_quic_iv_t             iv;
  33.     ngx_quic_md_t             hp;
  34.     ngx_quic_crypto_ctx_t    *ctx;
  35.     EVP_CIPHER_CTX           *hp_ctx;
  36. } ngx_quic_secret_t;


  37. typedef struct {
  38.     ngx_quic_secret_t         client;
  39.     ngx_quic_secret_t         server;
  40. } ngx_quic_secrets_t;


  41. struct ngx_quic_keys_s {
  42.     ngx_quic_secrets_t        secrets[NGX_QUIC_ENCRYPTION_LAST];
  43.     ngx_quic_secrets_t        next_key;
  44.     ngx_uint_t                cipher;
  45. };


  46. typedef struct {
  47.     const ngx_quic_cipher_t  *c;
  48.     const EVP_CIPHER         *hp;
  49.     const EVP_MD             *d;
  50. } ngx_quic_ciphers_t;


  51. typedef struct {
  52.     size_t                    out_len;
  53.     u_char                   *out;

  54.     size_t                    prk_len;
  55.     const uint8_t            *prk;

  56.     size_t                    label_len;
  57.     const u_char             *label;
  58. } ngx_quic_hkdf_t;

  59. #define ngx_quic_hkdf_set(seq, _label, _out, _prk)                            \
  60.     (seq)->out_len = (_out)->len; (seq)->out = (_out)->data;                  \
  61.     (seq)->prk_len = (_prk)->len, (seq)->prk = (_prk)->data,                  \
  62.     (seq)->label_len = (sizeof(_label) - 1); (seq)->label = (u_char *)(_label);


  63. ngx_int_t ngx_quic_keys_set_initial_secret(ngx_quic_keys_t *keys,
  64.     ngx_str_t *secret, ngx_log_t *log);
  65. ngx_int_t ngx_quic_keys_set_encryption_secret(ngx_log_t *log,
  66.     ngx_uint_t is_write, ngx_quic_keys_t *keys,
  67.     enum ssl_encryption_level_t level, const SSL_CIPHER *cipher,
  68.     const uint8_t *secret, size_t secret_len);
  69. ngx_uint_t ngx_quic_keys_available(ngx_quic_keys_t *keys,
  70.     enum ssl_encryption_level_t level, ngx_uint_t is_write);
  71. void ngx_quic_keys_discard(ngx_quic_keys_t *keys,
  72.     enum ssl_encryption_level_t level);
  73. void ngx_quic_keys_switch(ngx_connection_t *c, ngx_quic_keys_t *keys);
  74. void ngx_quic_keys_update(ngx_event_t *ev);
  75. void ngx_quic_keys_cleanup(ngx_quic_keys_t *keys);
  76. ngx_int_t ngx_quic_encrypt(ngx_quic_header_t *pkt, ngx_str_t *res);
  77. ngx_int_t ngx_quic_decrypt(ngx_quic_header_t *pkt, uint64_t *largest_pn);
  78. void ngx_quic_compute_nonce(u_char *nonce, size_t len, uint64_t pn);
  79. ngx_int_t ngx_quic_ciphers(ngx_uint_t id, ngx_quic_ciphers_t *ciphers);
  80. ngx_int_t ngx_quic_crypto_init(const ngx_quic_cipher_t *cipher,
  81.     ngx_quic_secret_t *s, ngx_quic_md_t *key, ngx_int_t enc, ngx_log_t *log);
  82. ngx_int_t ngx_quic_crypto_seal(ngx_quic_secret_t *s, ngx_str_t *out,
  83.     const u_char *nonce, ngx_str_t *in, ngx_str_t *ad, ngx_log_t *log);
  84. void ngx_quic_crypto_cleanup(ngx_quic_secret_t *s);
  85. ngx_int_t ngx_quic_hkdf_expand(ngx_quic_hkdf_t *hkdf, const EVP_MD *digest,
  86.     ngx_log_t *log);


  87. #endif /* _NGX_EVENT_QUIC_PROTECTION_H_INCLUDED_ */