src/event/ngx_event_openssl.h - nginx source code

Data types defined

Macros defined

Source code


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


  5. #ifndef _NGX_EVENT_OPENSSL_H_INCLUDED_
  6. #define _NGX_EVENT_OPENSSL_H_INCLUDED_


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

  9. #define OPENSSL_SUPPRESS_DEPRECATED

  10. #include <openssl/ssl.h>
  11. #include <openssl/err.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/conf.h>
  14. #include <openssl/crypto.h>
  15. #include <openssl/dh.h>
  16. #ifndef OPENSSL_NO_ENGINE
  17. #include <openssl/engine.h>
  18. #endif
  19. #include <openssl/evp.h>
  20. #if (NGX_QUIC)
  21. #ifdef OPENSSL_IS_BORINGSSL
  22. #include <openssl/hkdf.h>
  23. #include <openssl/chacha.h>
  24. #else
  25. #include <openssl/kdf.h>
  26. #endif
  27. #endif
  28. #include <openssl/hmac.h>
  29. #ifndef OPENSSL_NO_OCSP
  30. #include <openssl/ocsp.h>
  31. #endif
  32. #include <openssl/rand.h>
  33. #include <openssl/x509.h>
  34. #include <openssl/x509v3.h>

  35. #define NGX_SSL_NAME     "OpenSSL"


  36. #if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L)
  37. #undef OPENSSL_VERSION_NUMBER
  38. #if (LIBRESSL_VERSION_NUMBER >= 0x3050000fL)
  39. #define OPENSSL_VERSION_NUMBER  0x1010000fL
  40. #else
  41. #define OPENSSL_VERSION_NUMBER  0x1000107fL
  42. #endif
  43. #endif


  44. #if (OPENSSL_VERSION_NUMBER >= 0x10100001L)

  45. #define ngx_ssl_version()       OpenSSL_version(OPENSSL_VERSION)

  46. #else

  47. #define ngx_ssl_version()       SSLeay_version(SSLEAY_VERSION)

  48. #endif


  49. #define ngx_ssl_session_t       SSL_SESSION
  50. #define ngx_ssl_conn_t          SSL


  51. #if (OPENSSL_VERSION_NUMBER < 0x10002000L)
  52. #define SSL_is_server(s)        (s)->server
  53. #endif


  54. #if (OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined SSL_get_peer_certificate)
  55. #define SSL_get_peer_certificate(s)  SSL_get1_peer_certificate(s)
  56. #endif


  57. #if (OPENSSL_VERSION_NUMBER < 0x30000000L && !defined ERR_peek_error_data)
  58. #define ERR_peek_error_data(d, f)    ERR_peek_error_line_data(NULL, NULL, d, f)
  59. #endif


  60. typedef struct ngx_ssl_ocsp_s  ngx_ssl_ocsp_t;


  61. struct ngx_ssl_s {
  62.     SSL_CTX                    *ctx;
  63.     ngx_log_t                  *log;
  64.     size_t                      buffer_size;

  65.     ngx_array_t                 certs;

  66.     ngx_rbtree_t                staple_rbtree;
  67.     ngx_rbtree_node_t           staple_sentinel;
  68. };


  69. struct ngx_ssl_connection_s {
  70.     ngx_ssl_conn_t             *connection;
  71.     SSL_CTX                    *session_ctx;

  72.     ngx_int_t                   last;
  73.     ngx_buf_t                  *buf;
  74.     size_t                      buffer_size;

  75.     ngx_connection_handler_pt   handler;

  76.     ngx_ssl_session_t          *session;
  77.     ngx_connection_handler_pt   save_session;

  78.     ngx_event_handler_pt        saved_read_handler;
  79.     ngx_event_handler_pt        saved_write_handler;

  80.     ngx_ssl_ocsp_t             *ocsp;

  81.     u_char                      early_buf;

  82.     unsigned                    handshaked:1;
  83.     unsigned                    handshake_rejected:1;
  84.     unsigned                    renegotiation:1;
  85.     unsigned                    buffer:1;
  86.     unsigned                    sendfile:1;
  87.     unsigned                    no_wait_shutdown:1;
  88.     unsigned                    no_send_shutdown:1;
  89.     unsigned                    shutdown_without_free:1;
  90.     unsigned                    handshake_buffer_set:1;
  91.     unsigned                    session_timeout_set:1;
  92.     unsigned                    try_early_data:1;
  93.     unsigned                    in_early:1;
  94.     unsigned                    in_ocsp:1;
  95.     unsigned                    early_preread:1;
  96.     unsigned                    write_blocked:1;
  97. };


  98. #define NGX_SSL_NO_SCACHE            -2
  99. #define NGX_SSL_NONE_SCACHE          -3
  100. #define NGX_SSL_NO_BUILTIN_SCACHE    -4
  101. #define NGX_SSL_DFLT_BUILTIN_SCACHE  -5


  102. #define NGX_SSL_MAX_SESSION_SIZE  4096

  103. typedef struct ngx_ssl_sess_id_s  ngx_ssl_sess_id_t;

  104. struct ngx_ssl_sess_id_s {
  105.     ngx_rbtree_node_t           node;
  106.     size_t                      len;
  107.     ngx_queue_t                 queue;
  108.     time_t                      expire;
  109.     u_char                      id[32];
  110. #if (NGX_PTR_SIZE == 8)
  111.     u_char                     *session;
  112. #else
  113.     u_char                      session[1];
  114. #endif
  115. };


  116. typedef struct {
  117.     u_char                      name[16];
  118.     u_char                      hmac_key[32];
  119.     u_char                      aes_key[32];
  120.     time_t                      expire;
  121.     unsigned                    size:8;
  122.     unsigned                    shared:1;
  123. } ngx_ssl_ticket_key_t;


  124. typedef struct {
  125.     ngx_rbtree_t                session_rbtree;
  126.     ngx_rbtree_node_t           sentinel;
  127.     ngx_queue_t                 expire_queue;
  128.     ngx_ssl_ticket_key_t        ticket_keys[3];
  129.     time_t                      fail_time;
  130. } ngx_ssl_session_cache_t;


  131. #define NGX_SSL_SSLv2    0x0002
  132. #define NGX_SSL_SSLv3    0x0004
  133. #define NGX_SSL_TLSv1    0x0008
  134. #define NGX_SSL_TLSv1_1  0x0010
  135. #define NGX_SSL_TLSv1_2  0x0020
  136. #define NGX_SSL_TLSv1_3  0x0040


  137. #if (defined SSL_OP_NO_TLSv1_2 || defined SSL_OP_NO_TLSv1_3)
  138. #define NGX_SSL_DEFAULT_PROTOCOLS  (NGX_SSL_TLSv1_2|NGX_SSL_TLSv1_3)
  139. #else
  140. #define NGX_SSL_DEFAULT_PROTOCOLS  (NGX_SSL_TLSv1|NGX_SSL_TLSv1_1)
  141. #endif


  142. #define NGX_SSL_BUFFER   1
  143. #define NGX_SSL_CLIENT   2

  144. #define NGX_SSL_BUFSIZE  16384


  145. #define NGX_SSL_CACHE_CERT  0
  146. #define NGX_SSL_CACHE_PKEY  1
  147. #define NGX_SSL_CACHE_CRL   2
  148. #define NGX_SSL_CACHE_CA    3


  149. ngx_int_t ngx_ssl_init(ngx_log_t *log);
  150. ngx_int_t ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data);

  151. ngx_int_t ngx_ssl_certificates(ngx_conf_t *cf, ngx_ssl_t *ssl,
  152.     ngx_array_t *certs, ngx_array_t *keys, ngx_array_t *passwords);
  153. ngx_int_t ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
  154.     ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords);
  155. ngx_int_t ngx_ssl_connection_certificate(ngx_connection_t *c, ngx_pool_t *pool,
  156.     ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords);

  157. ngx_int_t ngx_ssl_ciphers(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *ciphers,
  158.     ngx_uint_t prefer_server_ciphers);
  159. ngx_int_t ngx_ssl_client_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
  160.     ngx_str_t *cert, ngx_int_t depth);
  161. ngx_int_t ngx_ssl_trusted_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
  162.     ngx_str_t *cert, ngx_int_t depth);
  163. ngx_int_t ngx_ssl_crl(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *crl);
  164. ngx_int_t ngx_ssl_stapling(ngx_conf_t *cf, ngx_ssl_t *ssl,
  165.     ngx_str_t *file, ngx_str_t *responder, ngx_uint_t verify);
  166. ngx_int_t ngx_ssl_stapling_resolver(ngx_conf_t *cf, ngx_ssl_t *ssl,
  167.     ngx_resolver_t *resolver, ngx_msec_t resolver_timeout);
  168. ngx_int_t ngx_ssl_ocsp(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *responder,
  169.     ngx_uint_t depth, ngx_shm_zone_t *shm_zone);
  170. ngx_int_t ngx_ssl_ocsp_resolver(ngx_conf_t *cf, ngx_ssl_t *ssl,
  171.     ngx_resolver_t *resolver, ngx_msec_t resolver_timeout);

  172. ngx_int_t ngx_ssl_ocsp_validate(ngx_connection_t *c);
  173. ngx_int_t ngx_ssl_ocsp_get_status(ngx_connection_t *c, const char **s);
  174. void ngx_ssl_ocsp_cleanup(ngx_connection_t *c);
  175. ngx_int_t ngx_ssl_ocsp_cache_init(ngx_shm_zone_t *shm_zone, void *data);

  176. void *ngx_ssl_cache_fetch(ngx_conf_t *cf, ngx_uint_t index, char **err,
  177.     ngx_str_t *path, void *data);
  178. void *ngx_ssl_cache_connection_fetch(ngx_pool_t *pool, ngx_uint_t index,
  179.     char **err, ngx_str_t *path, void *data);

  180. ngx_array_t *ngx_ssl_read_password_file(ngx_conf_t *cf, ngx_str_t *file);
  181. ngx_array_t *ngx_ssl_preserve_passwords(ngx_conf_t *cf,
  182.     ngx_array_t *passwords);
  183. ngx_int_t ngx_ssl_dhparam(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file);
  184. ngx_int_t ngx_ssl_ecdh_curve(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *name);
  185. ngx_int_t ngx_ssl_early_data(ngx_conf_t *cf, ngx_ssl_t *ssl,
  186.     ngx_uint_t enable);
  187. ngx_int_t ngx_ssl_conf_commands(ngx_conf_t *cf, ngx_ssl_t *ssl,
  188.     ngx_array_t *commands);

  189. ngx_int_t ngx_ssl_client_session_cache(ngx_conf_t *cf, ngx_ssl_t *ssl,
  190.     ngx_uint_t enable);
  191. ngx_int_t ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
  192.     ngx_array_t *certificates, ssize_t builtin_session_cache,
  193.     ngx_shm_zone_t *shm_zone, time_t timeout);
  194. ngx_int_t ngx_ssl_session_ticket_keys(ngx_conf_t *cf, ngx_ssl_t *ssl,
  195.     ngx_array_t *paths);
  196. ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data);

  197. ngx_int_t ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c,
  198.     ngx_uint_t flags);

  199. void ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess);
  200. ngx_int_t ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session);
  201. ngx_ssl_session_t *ngx_ssl_get_session(ngx_connection_t *c);
  202. ngx_ssl_session_t *ngx_ssl_get0_session(ngx_connection_t *c);
  203. #define ngx_ssl_free_session        SSL_SESSION_free
  204. #define ngx_ssl_get_connection(ssl_conn)                                      \
  205.     SSL_get_ex_data(ssl_conn, ngx_ssl_connection_index)
  206. #define ngx_ssl_get_server_conf(ssl_ctx)                                      \
  207.     SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_server_conf_index)

  208. #define ngx_ssl_verify_error_optional(n)                                      \
  209.     (n == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT                              \
  210.      || n == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN                             \
  211.      || n == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY                     \
  212.      || n == X509_V_ERR_CERT_UNTRUSTED                                        \
  213.      || n == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE)

  214. ngx_int_t ngx_ssl_check_host(ngx_connection_t *c, ngx_str_t *name);


  215. ngx_int_t ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool,
  216.     ngx_str_t *s);
  217. ngx_int_t ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool,
  218.     ngx_str_t *s);
  219. ngx_int_t ngx_ssl_get_ciphers(ngx_connection_t *c, ngx_pool_t *pool,
  220.     ngx_str_t *s);
  221. ngx_int_t ngx_ssl_get_curve(ngx_connection_t *c, ngx_pool_t *pool,
  222.     ngx_str_t *s);
  223. ngx_int_t ngx_ssl_get_curves(ngx_connection_t *c, ngx_pool_t *pool,
  224.     ngx_str_t *s);
  225. ngx_int_t ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool,
  226.     ngx_str_t *s);
  227. ngx_int_t ngx_ssl_get_session_reused(ngx_connection_t *c, ngx_pool_t *pool,
  228.     ngx_str_t *s);
  229. ngx_int_t ngx_ssl_get_early_data(ngx_connection_t *c, ngx_pool_t *pool,
  230.     ngx_str_t *s);
  231. ngx_int_t ngx_ssl_get_server_name(ngx_connection_t *c, ngx_pool_t *pool,
  232.     ngx_str_t *s);
  233. ngx_int_t ngx_ssl_get_alpn_protocol(ngx_connection_t *c, ngx_pool_t *pool,
  234.     ngx_str_t *s);
  235. ngx_int_t ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool,
  236.     ngx_str_t *s);
  237. ngx_int_t ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool,
  238.     ngx_str_t *s);
  239. ngx_int_t ngx_ssl_get_escaped_certificate(ngx_connection_t *c, ngx_pool_t *pool,
  240.     ngx_str_t *s);
  241. ngx_int_t ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool,
  242.     ngx_str_t *s);
  243. ngx_int_t ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool,
  244.     ngx_str_t *s);
  245. ngx_int_t ngx_ssl_get_subject_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
  246.     ngx_str_t *s);
  247. ngx_int_t ngx_ssl_get_issuer_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
  248.     ngx_str_t *s);
  249. ngx_int_t ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool,
  250.     ngx_str_t *s);
  251. ngx_int_t ngx_ssl_get_fingerprint(ngx_connection_t *c, ngx_pool_t *pool,
  252.     ngx_str_t *s);
  253. ngx_int_t ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool,
  254.     ngx_str_t *s);
  255. ngx_int_t ngx_ssl_get_client_v_start(ngx_connection_t *c, ngx_pool_t *pool,
  256.     ngx_str_t *s);
  257. ngx_int_t ngx_ssl_get_client_v_end(ngx_connection_t *c, ngx_pool_t *pool,
  258.     ngx_str_t *s);
  259. ngx_int_t ngx_ssl_get_client_v_remain(ngx_connection_t *c, ngx_pool_t *pool,
  260.     ngx_str_t *s);


  261. ngx_int_t ngx_ssl_handshake(ngx_connection_t *c);
  262. #if (NGX_DEBUG)
  263. void ngx_ssl_handshake_log(ngx_connection_t *c);
  264. #endif
  265. ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
  266. ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
  267. ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit);
  268. ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
  269.     off_t limit);
  270. void ngx_ssl_free_buffer(ngx_connection_t *c);
  271. ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c);
  272. void ngx_cdecl ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
  273.     char *fmt, ...);
  274. void ngx_ssl_cleanup_ctx(void *data);


  275. extern int  ngx_ssl_connection_index;
  276. extern int  ngx_ssl_server_conf_index;
  277. extern int  ngx_ssl_session_cache_index;
  278. extern int  ngx_ssl_ticket_keys_index;
  279. extern int  ngx_ssl_ocsp_index;
  280. extern int  ngx_ssl_index;
  281. extern int  ngx_ssl_certificate_name_index;


  282. #endif /* _NGX_EVENT_OPENSSL_H_INCLUDED_ */