Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.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 #define OPENSSL_VERSION_NUMBER  0x1000107fL
0040 #endif
0041 
0042 
0043 #if (OPENSSL_VERSION_NUMBER >= 0x10100001L)
0044 
0045 #define ngx_ssl_version()       OpenSSL_version(OPENSSL_VERSION)
0046 
0047 #else
0048 
0049 #define ngx_ssl_version()       SSLeay_version(SSLEAY_VERSION)
0050 
0051 #endif
0052 
0053 
0054 #define ngx_ssl_session_t       SSL_SESSION
0055 #define ngx_ssl_conn_t          SSL
0056 
0057 
0058 #if (OPENSSL_VERSION_NUMBER < 0x10002000L)
0059 #define SSL_is_server(s)        (s)->server
0060 #endif
0061 
0062 
0063 struct ngx_ssl_s {
0064     SSL_CTX                    *ctx;
0065     ngx_log_t                  *log;
0066     size_t                      buffer_size;
0067 };
0068 
0069 
0070 struct ngx_ssl_connection_s {
0071     ngx_ssl_conn_t             *connection;
0072     SSL_CTX                    *session_ctx;
0073 
0074     ngx_int_t                   last;
0075     ngx_buf_t                  *buf;
0076     size_t                      buffer_size;
0077 
0078     ngx_connection_handler_pt   handler;
0079 
0080     ngx_event_handler_pt        saved_read_handler;
0081     ngx_event_handler_pt        saved_write_handler;
0082 
0083     unsigned                    handshaked:1;
0084     unsigned                    renegotiation:1;
0085     unsigned                    buffer:1;
0086     unsigned                    no_wait_shutdown:1;
0087     unsigned                    no_send_shutdown:1;
0088     unsigned                    handshake_buffer_set:1;
0089 };
0090 
0091 
0092 #define NGX_SSL_NO_SCACHE            -2
0093 #define NGX_SSL_NONE_SCACHE          -3
0094 #define NGX_SSL_NO_BUILTIN_SCACHE    -4
0095 #define NGX_SSL_DFLT_BUILTIN_SCACHE  -5
0096 
0097 
0098 #define NGX_SSL_MAX_SESSION_SIZE  4096
0099 
0100 typedef struct ngx_ssl_sess_id_s  ngx_ssl_sess_id_t;
0101 
0102 struct ngx_ssl_sess_id_s {
0103     ngx_rbtree_node_t           node;
0104     u_char                     *id;
0105     size_t                      len;
0106     u_char                     *session;
0107     ngx_queue_t                 queue;
0108     time_t                      expire;
0109 #if (NGX_PTR_SIZE == 8)
0110     void                       *stub;
0111     u_char                      sess_id[32];
0112 #endif
0113 };
0114 
0115 
0116 typedef struct {
0117     ngx_rbtree_t                session_rbtree;
0118     ngx_rbtree_node_t           sentinel;
0119     ngx_queue_t                 expire_queue;
0120 } ngx_ssl_session_cache_t;
0121 
0122 
0123 #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB
0124 
0125 typedef struct {
0126     size_t                      size;
0127     u_char                      name[16];
0128     u_char                      hmac_key[32];
0129     u_char                      aes_key[32];
0130 } ngx_ssl_session_ticket_key_t;
0131 
0132 #endif
0133 
0134 
0135 #define NGX_SSL_SSLv2    0x0002
0136 #define NGX_SSL_SSLv3    0x0004
0137 #define NGX_SSL_TLSv1    0x0008
0138 #define NGX_SSL_TLSv1_1  0x0010
0139 #define NGX_SSL_TLSv1_2  0x0020
0140 #define NGX_SSL_TLSv1_3  0x0040
0141 
0142 
0143 #define NGX_SSL_BUFFER   1
0144 #define NGX_SSL_CLIENT   2
0145 
0146 #define NGX_SSL_BUFSIZE  16384
0147 
0148 
0149 ngx_int_t ngx_ssl_init(ngx_log_t *log);
0150 ngx_int_t ngx_ssl_create(ngx_ssl_t *ssl, ngx_uint_t protocols, void *data);
0151 ngx_int_t ngx_ssl_certificates(ngx_conf_t *cf, ngx_ssl_t *ssl,
0152     ngx_array_t *certs, ngx_array_t *keys, ngx_array_t *passwords);
0153 ngx_int_t ngx_ssl_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0154     ngx_str_t *cert, ngx_str_t *key, ngx_array_t *passwords);
0155 ngx_int_t ngx_ssl_ciphers(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *ciphers,
0156     ngx_uint_t prefer_server_ciphers);
0157 ngx_int_t ngx_ssl_client_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0158     ngx_str_t *cert, ngx_int_t depth);
0159 ngx_int_t ngx_ssl_trusted_certificate(ngx_conf_t *cf, ngx_ssl_t *ssl,
0160     ngx_str_t *cert, ngx_int_t depth);
0161 ngx_int_t ngx_ssl_crl(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *crl);
0162 ngx_int_t ngx_ssl_stapling(ngx_conf_t *cf, ngx_ssl_t *ssl,
0163     ngx_str_t *file, ngx_str_t *responder, ngx_uint_t verify);
0164 ngx_int_t ngx_ssl_stapling_resolver(ngx_conf_t *cf, ngx_ssl_t *ssl,
0165     ngx_resolver_t *resolver, ngx_msec_t resolver_timeout);
0166 RSA *ngx_ssl_rsa512_key_callback(ngx_ssl_conn_t *ssl_conn, int is_export,
0167     int key_length);
0168 ngx_array_t *ngx_ssl_read_password_file(ngx_conf_t *cf, ngx_str_t *file);
0169 ngx_int_t ngx_ssl_dhparam(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *file);
0170 ngx_int_t ngx_ssl_ecdh_curve(ngx_conf_t *cf, ngx_ssl_t *ssl, ngx_str_t *name);
0171 ngx_int_t ngx_ssl_session_cache(ngx_ssl_t *ssl, ngx_str_t *sess_ctx,
0172     ssize_t builtin_session_cache, ngx_shm_zone_t *shm_zone, time_t timeout);
0173 ngx_int_t ngx_ssl_session_ticket_keys(ngx_conf_t *cf, ngx_ssl_t *ssl,
0174     ngx_array_t *paths);
0175 ngx_int_t ngx_ssl_session_cache_init(ngx_shm_zone_t *shm_zone, void *data);
0176 ngx_int_t ngx_ssl_create_connection(ngx_ssl_t *ssl, ngx_connection_t *c,
0177     ngx_uint_t flags);
0178 
0179 void ngx_ssl_remove_cached_session(SSL_CTX *ssl, ngx_ssl_session_t *sess);
0180 ngx_int_t ngx_ssl_set_session(ngx_connection_t *c, ngx_ssl_session_t *session);
0181 #define ngx_ssl_get_session(c)      SSL_get1_session(c->ssl->connection)
0182 #define ngx_ssl_free_session        SSL_SESSION_free
0183 #define ngx_ssl_get_connection(ssl_conn)                                      \
0184     SSL_get_ex_data(ssl_conn, ngx_ssl_connection_index)
0185 #define ngx_ssl_get_server_conf(ssl_ctx)                                      \
0186     SSL_CTX_get_ex_data(ssl_ctx, ngx_ssl_server_conf_index)
0187 
0188 #define ngx_ssl_verify_error_optional(n)                                      \
0189     (n == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT                              \
0190      || n == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN                             \
0191      || n == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY                     \
0192      || n == X509_V_ERR_CERT_UNTRUSTED                                        \
0193      || n == X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE)
0194 
0195 ngx_int_t ngx_ssl_check_host(ngx_connection_t *c, ngx_str_t *name);
0196 
0197 
0198 ngx_int_t ngx_ssl_get_protocol(ngx_connection_t *c, ngx_pool_t *pool,
0199     ngx_str_t *s);
0200 ngx_int_t ngx_ssl_get_cipher_name(ngx_connection_t *c, ngx_pool_t *pool,
0201     ngx_str_t *s);
0202 ngx_int_t ngx_ssl_get_ciphers(ngx_connection_t *c, ngx_pool_t *pool,
0203     ngx_str_t *s);
0204 ngx_int_t ngx_ssl_get_curves(ngx_connection_t *c, ngx_pool_t *pool,
0205     ngx_str_t *s);
0206 ngx_int_t ngx_ssl_get_session_id(ngx_connection_t *c, ngx_pool_t *pool,
0207     ngx_str_t *s);
0208 ngx_int_t ngx_ssl_get_session_reused(ngx_connection_t *c, ngx_pool_t *pool,
0209     ngx_str_t *s);
0210 ngx_int_t ngx_ssl_get_server_name(ngx_connection_t *c, ngx_pool_t *pool,
0211     ngx_str_t *s);
0212 ngx_int_t ngx_ssl_get_raw_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0213     ngx_str_t *s);
0214 ngx_int_t ngx_ssl_get_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0215     ngx_str_t *s);
0216 ngx_int_t ngx_ssl_get_escaped_certificate(ngx_connection_t *c, ngx_pool_t *pool,
0217     ngx_str_t *s);
0218 ngx_int_t ngx_ssl_get_subject_dn(ngx_connection_t *c, ngx_pool_t *pool,
0219     ngx_str_t *s);
0220 ngx_int_t ngx_ssl_get_issuer_dn(ngx_connection_t *c, ngx_pool_t *pool,
0221     ngx_str_t *s);
0222 ngx_int_t ngx_ssl_get_subject_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
0223     ngx_str_t *s);
0224 ngx_int_t ngx_ssl_get_issuer_dn_legacy(ngx_connection_t *c, ngx_pool_t *pool,
0225     ngx_str_t *s);
0226 ngx_int_t ngx_ssl_get_serial_number(ngx_connection_t *c, ngx_pool_t *pool,
0227     ngx_str_t *s);
0228 ngx_int_t ngx_ssl_get_fingerprint(ngx_connection_t *c, ngx_pool_t *pool,
0229     ngx_str_t *s);
0230 ngx_int_t ngx_ssl_get_client_verify(ngx_connection_t *c, ngx_pool_t *pool,
0231     ngx_str_t *s);
0232 ngx_int_t ngx_ssl_get_client_v_start(ngx_connection_t *c, ngx_pool_t *pool,
0233     ngx_str_t *s);
0234 ngx_int_t ngx_ssl_get_client_v_end(ngx_connection_t *c, ngx_pool_t *pool,
0235     ngx_str_t *s);
0236 ngx_int_t ngx_ssl_get_client_v_remain(ngx_connection_t *c, ngx_pool_t *pool,
0237     ngx_str_t *s);
0238 
0239 
0240 ngx_int_t ngx_ssl_handshake(ngx_connection_t *c);
0241 ssize_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size);
0242 ssize_t ngx_ssl_write(ngx_connection_t *c, u_char *data, size_t size);
0243 ssize_t ngx_ssl_recv_chain(ngx_connection_t *c, ngx_chain_t *cl, off_t limit);
0244 ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
0245     off_t limit);
0246 void ngx_ssl_free_buffer(ngx_connection_t *c);
0247 ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c);
0248 void ngx_cdecl ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
0249     char *fmt, ...);
0250 void ngx_ssl_cleanup_ctx(void *data);
0251 
0252 
0253 extern int  ngx_ssl_connection_index;
0254 extern int  ngx_ssl_server_conf_index;
0255 extern int  ngx_ssl_session_cache_index;
0256 extern int  ngx_ssl_session_ticket_keys_index;
0257 extern int  ngx_ssl_certificate_index;
0258 extern int  ngx_ssl_next_certificate_index;
0259 extern int  ngx_ssl_certificate_name_index;
0260 extern int  ngx_ssl_stapling_index;
0261 
0262 
0263 #endif /* _NGX_EVENT_OPENSSL_H_INCLUDED_ */