Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.11 ]​[ nginx-1.14.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #ifndef _NGX_EVENT_OPENSSL_H_INCLUDED_
0009 #define _NGX_EVENT_OPENSSL_H_INCLUDED_
0010 
0011 
0012 #include <ngx_config.h>
0013 #include <ngx_core.h>
0014 
0015 #include <openssl/ssl.h>
0016 #include <openssl/err.h>
0017 #include <openssl/bn.h>
0018 #include <openssl/conf.h>
0019 #include <openssl/crypto.h>
0020 #include <openssl/dh.h>
0021 #ifndef OPENSSL_NO_ENGINE
0022 #include <openssl/engine.h>
0023 #endif
0024 #include <openssl/evp.h>
0025 #include <openssl/hmac.h>
0026 #ifndef OPENSSL_NO_OCSP
0027 #include <openssl/ocsp.h>
0028 #endif
0029 #include <openssl/rand.h>
0030 #include <openssl/rsa.h>
0031 #include <openssl/x509.h>
0032 #include <openssl/x509v3.h>
0033 
0034 #define NGX_SSL_NAME     "OpenSSL"
0035 
0036 
0037 #if (defined LIBRESSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER == 0x20000000L)
0038 #undef OPENSSL_VERSION_NUMBER
0039 #if (LIBRESSL_VERSION_NUMBER >= 0x2080000fL)
0040 #define OPENSSL_VERSION_NUMBER  0x1010000fL
0041 #else
0042 #define OPENSSL_VERSION_NUMBER  0x1000107fL
0043 #endif
0044 #endif
0045 
0046 
0047 #if (OPENSSL_VERSION_NUMBER >= 0x10100001L)
0048 
0049 #define ngx_ssl_version()       OpenSSL_version(OPENSSL_VERSION)
0050 
0051 #else
0052 
0053 #define ngx_ssl_version()       SSLeay_version(SSLEAY_VERSION)
0054 
0055 #endif
0056 
0057 
0058 #define ngx_ssl_session_t       SSL_SESSION
0059 #define ngx_ssl_conn_t          SSL
0060 
0061 
0062 #if (OPENSSL_VERSION_NUMBER < 0x10002000L)
0063 #define SSL_is_server(s)        (s)->server
0064 #endif
0065 
0066 
0067 struct ngx_ssl_s {
0068     SSL_CTX                    *ctx;
0069     ngx_log_t                  *log;
0070     size_t                      buffer_size;
0071 };
0072 
0073 
0074 struct ngx_ssl_connection_s {
0075     ngx_ssl_conn_t             *connection;
0076     SSL_CTX                    *session_ctx;
0077 
0078     ngx_int_t                   last;
0079     ngx_buf_t                  *buf;
0080     size_t                      buffer_size;
0081 
0082     ngx_connection_handler_pt   handler;
0083 
0084     ngx_ssl_session_t          *session;
0085     ngx_connection_handler_pt   save_session;
0086 
0087     ngx_event_handler_pt        saved_read_handler;
0088     ngx_event_handler_pt        saved_write_handler;
0089 
0090     u_char                      early_buf;
0091 
0092     unsigned                    handshaked:1;
0093     unsigned                    renegotiation:1;
0094     unsigned                    buffer:1;
0095     unsigned                    no_wait_shutdown:1;
0096     unsigned                    no_send_shutdown:1;
0097     unsigned                    handshake_buffer_set:1;
0098     unsigned                    try_early_data:1;
0099     unsigned                    in_early:1;
0100     unsigned                    early_preread:1;
0101     unsigned                    write_blocked:1;
0102 };
0103 
0104 
0105 #define NGX_SSL_NO_SCACHE            -2
0106 #define NGX_SSL_NONE_SCACHE          -3
0107 #define NGX_SSL_NO_BUILTIN_SCACHE    -4
0108 #define NGX_SSL_DFLT_BUILTIN_SCACHE  -5
0109 
0110 
0111 #define NGX_SSL_MAX_SESSION_SIZE  4096
0112 
0113 typedef struct ngx_ssl_sess_id_s  ngx_ssl_sess_id_t;
0114 
0115 struct ngx_ssl_sess_id_s {
0116     ngx_rbtree_node_t           node;
0117     u_char                     *id;
0118     size_t                      len;
0119     u_char                     *session;
0120     ngx_queue_t                 queue;
0121     time_t                      expire;
0122 #if (NGX_PTR_SIZE == 8)
0123     void                       *stub;
0124     u_char                      sess_id[32];
0125 #endif
0126 };
0127 
0128 
0129 typedef struct {
0130     ngx_rbtree_t                session_rbtree;
0131     ngx_rbtree_node_t           sentinel;
0132     ngx_queue_t                 expire_queue;
0133 } ngx_ssl_session_cache_t;
0134 
0135 
0136 #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB
0137 
0138 typedef struct {
0139     size_t                      size;
0140     u_char                      name[16];
0141     u_char                      hmac_key[32];
0142     u_char                      aes_key[32];
0143 } ngx_ssl_session_ticket_key_t;
0144 
0145 #endif
0146 
0147 
0148 #define NGX_SSL_SSLv2    0x0002
0149 #define NGX_SSL_SSLv3    0x0004
0150 #define NGX_SSL_TLSv1    0x0008
0151 #define NGX_SSL_TLSv1_1  0x0010
0152 #define NGX_SSL_TLSv1_2  0x0020
0153 #define NGX_SSL_TLSv1_3  0x0040
0154 
0155 
0156 #define NGX_SSL_BUFFER   1
0157 #define NGX_SSL_CLIENT   2
0158 
0159 #define NGX_SSL_BUFSIZE  16384
0160 
0161 
0162 ngx_int_t ngx_ssl_init(ngx_log_t *log);
0163 ngx_int_t ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data);
0164 
0165 ngx_int_t ngx_ssl_certificates(ngx_conf_t *cf, ngx_ssl_t *ssl,
0166     ngx_array_t *certs, ngx_array_t *keys, ngx_array_t *passwords);
0167 ngx_int_t ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0168     ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords);
0169 ngx_int_t ngx_ssl_connection_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0170     ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords);
0171 
0172 ngx_int_t ngx_ssl_ciphers(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *ciphers,
0173     ngx_uint_t prefer_server_ciphers);
0174 ngx_int_t ngx_ssl_client_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0175     ngx_str_t *cert, ngx_int_t depth);
0176 ngx_int_t ngx_ssl_trusted_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0177     ngx_str_t *cert, ngx_int_t depth);
0178 ngx_int_t ngx_ssl_crl(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *crl);
0179 ngx_int_t ngx_ssl_stapling(ngx_conf_t *cf, ngx_ssl_t *ssl,
0180     ngx_str_t *file, ngx_str_t *responder, ngx_uint_t verify);
0181 ngx_int_t ngx_ssl_stapling_resolver(ngx_conf_t *cf, ngx_ssl_t *ssl,
0182     ngx_resolver_t *resolver, ngx_msec_t resolver_timeout);
0183 RSA *ngx_ssl_rsa512_key_callback(ngx_ssl_conn_t *ssl_conn, int is_export,
0184     int key_length);
0185 ngx_array_t *ngx_ssl_read_password_file(ngx_conf_t *cf, ngx_str_t *file);
0186 ngx_array_t *ngx_ssl_preserve_passwords(ngx_conf_t *cf,
0187     ngx_array_t *passwords);
0188 ngx_int_t ngx_ssl_dhparam(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file);
0189 ngx_int_t ngx_ssl_ecdh_curve(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *name);
0190 ngx_int_t ngx_ssl_early_data(ngx_conf_t *cf, ngx_ssl_t *ssl,
0191     ngx_uint_t enable);
0192 ngx_int_t ngx_ssl_client_session_cache(ngx_conf_t *cf, ngx_ssl_t *ssl,
0193     ngx_uint_t enable);
0194 ngx_int_t ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
0195     ngx_array_t *certificates, ssize_t builtin_session_cache,
0196     ngx_shm_zone_t *shm_zone, time_t timeout);
0197 ngx_int_t ngx_ssl_session_ticket_keys(ngx_conf_t *cf, ngx_ssl_t *ssl,
0198     ngx_array_t *paths);
0199 ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data);
0200 ngx_int_t ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c,
0201     ngx_uint_t flags);
0202 
0203 void ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess);
0204 ngx_int_t ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session);
0205 ngx_ssl_session_t *ngx_ssl_get_session(ngx_connection_t *c);
0206 ngx_ssl_session_t *ngx_ssl_get0_session(ngx_connection_t *c);
0207 #define ngx_ssl_free_session        SSL_SESSION_free
0208 #define ngx_ssl_get_connection(ssl_conn)                                      \
0209     SSL_get_ex_data(ssl_conn, ngx_ssl_connection_index)
0210 #define ngx_ssl_get_server_conf(ssl_ctx)                                      \
0211     SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_server_conf_index)
0212 
0213 #define ngx_ssl_verify_error_optional(n)                                      \
0214     (n == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT                              \
0215      || n == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN                             \
0216      || n == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY                     \
0217      || n == X509_V_ERR_CERT_UNTRUSTED                                        \
0218      || n == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE)
0219 
0220 ngx_int_t ngx_ssl_check_host(ngx_connection_t *c, ngx_str_t *name);
0221 
0222 
0223 ngx_int_t ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool,
0224     ngx_str_t *s);
0225 ngx_int_t ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool,
0226     ngx_str_t *s);
0227 ngx_int_t ngx_ssl_get_ciphers(ngx_connection_t *c, ngx_pool_t *pool,
0228     ngx_str_t *s);
0229 ngx_int_t ngx_ssl_get_curves(ngx_connection_t *c, ngx_pool_t *pool,
0230     ngx_str_t *s);
0231 ngx_int_t ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool,
0232     ngx_str_t *s);
0233 ngx_int_t ngx_ssl_get_session_reused(ngx_connection_t *c, ngx_pool_t *pool,
0234     ngx_str_t *s);
0235 ngx_int_t ngx_ssl_get_early_data(ngx_connection_t *c, ngx_pool_t *pool,
0236     ngx_str_t *s);
0237 ngx_int_t ngx_ssl_get_server_name(ngx_connection_t *c, ngx_pool_t *pool,
0238     ngx_str_t *s);
0239 ngx_int_t ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0240     ngx_str_t *s);
0241 ngx_int_t ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0242     ngx_str_t *s);
0243 ngx_int_t ngx_ssl_get_escaped_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0244     ngx_str_t *s);
0245 ngx_int_t ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool,
0246     ngx_str_t *s);
0247 ngx_int_t ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool,
0248     ngx_str_t *s);
0249 ngx_int_t ngx_ssl_get_subject_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
0250     ngx_str_t *s);
0251 ngx_int_t ngx_ssl_get_issuer_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
0252     ngx_str_t *s);
0253 ngx_int_t ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool,
0254     ngx_str_t *s);
0255 ngx_int_t ngx_ssl_get_fingerprint(ngx_connection_t *c, ngx_pool_t *pool,
0256     ngx_str_t *s);
0257 ngx_int_t ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool,
0258     ngx_str_t *s);
0259 ngx_int_t ngx_ssl_get_client_v_start(ngx_connection_t *c, ngx_pool_t *pool,
0260     ngx_str_t *s);
0261 ngx_int_t ngx_ssl_get_client_v_end(ngx_connection_t *c, ngx_pool_t *pool,
0262     ngx_str_t *s);
0263 ngx_int_t ngx_ssl_get_client_v_remain(ngx_connection_t *c, ngx_pool_t *pool,
0264     ngx_str_t *s);
0265 
0266 
0267 ngx_int_t ngx_ssl_handshake(ngx_connection_t *c);
0268 ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
0269 ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
0270 ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit);
0271 ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
0272     off_t limit);
0273 void ngx_ssl_free_buffer(ngx_connection_t *c);
0274 ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c);
0275 void ngx_cdecl ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
0276     char *fmt, ...);
0277 void ngx_ssl_cleanup_ctx(void *data);
0278 
0279 
0280 extern int  ngx_ssl_connection_index;
0281 extern int  ngx_ssl_server_conf_index;
0282 extern int  ngx_ssl_session_cache_index;
0283 extern int  ngx_ssl_session_ticket_keys_index;
0284 extern int  ngx_ssl_certificate_index;
0285 extern int  ngx_ssl_next_certificate_index;
0286 extern int  ngx_ssl_certificate_name_index;
0287 extern int  ngx_ssl_stapling_index;
0288 
0289 
0290 #endif /* _NGX_EVENT_OPENSSL_H_INCLUDED_ */