18 static int ngx_http_ssl_verify_callback(
int ok, X509_STORE_CTX *x509_store);
19 static void ngx_ssl_info_callback(
const ngx_ssl_conn_t *ssl_conn,
int where,
21 static void ngx_ssl_handshake_handler(
ngx_event_t *ev);
23 static void ngx_ssl_write_handler(
ngx_event_t *wev);
25 static void ngx_ssl_shutdown_handler(
ngx_event_t *ev);
28 static void ngx_ssl_clear_error(
ngx_log_t *log);
34 u_char *
id,
int len,
int *copy);
41 static void *ngx_openssl_create_conf(
ngx_cycle_t *cycle);
61 ngx_openssl_create_conf,
68 &ngx_openssl_module_ctx,
95 SSL_load_error_strings();
97 OpenSSL_add_all_algorithms();
99 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
100 #ifndef SSL_OP_NO_COMPRESSION
107 STACK_OF(SSL_COMP) *ssl_comp_methods;
109 ssl_comp_methods = SSL_COMP_get_compression_methods();
110 n = sk_SSL_COMP_num(ssl_comp_methods);
113 (void) sk_SSL_COMP_pop(ssl_comp_methods);
130 "SSL_CTX_get_ex_new_index() failed");
138 "SSL_CTX_get_ex_new_index() failed");
146 "SSL_CTX_get_ex_new_index() failed");
154 "SSL_CTX_get_ex_new_index() failed");
165 ssl->
ctx = SSL_CTX_new(SSLv23_method());
167 if (ssl->
ctx == NULL) {
174 "SSL_CTX_set_ex_data() failed");
180 SSL_CTX_set_options(ssl->
ctx, SSL_OP_MICROSOFT_SESS_ID_BUG);
181 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NETSCAPE_CHALLENGE_BUG);
185 SSL_CTX_set_options(ssl->
ctx, SSL_OP_SSLREF2_REUSE_CERT_TYPE_BUG);
186 SSL_CTX_set_options(ssl->
ctx, SSL_OP_MICROSOFT_BIG_SSLV3_BUFFER);
189 SSL_CTX_set_options(ssl->
ctx, SSL_OP_MSIE_SSLV2_RSA_PADDING);
191 SSL_CTX_set_options(ssl->
ctx, SSL_OP_SSLEAY_080_CLIENT_DH_BUG);
192 SSL_CTX_set_options(ssl->
ctx, SSL_OP_TLS_D5_BUG);
193 SSL_CTX_set_options(ssl->
ctx, SSL_OP_TLS_BLOCK_PADDING_BUG);
195 SSL_CTX_set_options(ssl->
ctx, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
197 SSL_CTX_set_options(ssl->
ctx, SSL_OP_SINGLE_DH_USE);
200 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_SSLv2);
203 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_SSLv3);
206 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_TLSv1);
208 #ifdef SSL_OP_NO_TLSv1_1
210 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_TLSv1_1);
213 #ifdef SSL_OP_NO_TLSv1_2
215 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_TLSv1_2);
219 #ifdef SSL_OP_NO_COMPRESSION
220 SSL_CTX_set_options(ssl->
ctx, SSL_OP_NO_COMPRESSION);
223 #ifdef SSL_MODE_RELEASE_BUFFERS
224 SSL_CTX_set_mode(ssl->
ctx, SSL_MODE_RELEASE_BUFFERS);
227 SSL_CTX_set_read_ahead(ssl->
ctx, 1);
229 SSL_CTX_set_info_callback(ssl->
ctx, ngx_ssl_info_callback);
253 bio = BIO_new_file((
char *) cert->
data,
"r");
256 "BIO_new_file(\"%s\") failed", cert->
data);
260 x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
263 "PEM_read_bio_X509_AUX(\"%s\") failed", cert->
data);
268 if (SSL_CTX_use_certificate(ssl->
ctx, x509) == 0) {
270 "SSL_CTX_use_certificate(\"%s\") failed", cert->
data);
280 "SSL_CTX_set_ex_data() failed");
290 x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
292 n = ERR_peek_last_error();
294 if (ERR_GET_LIB(n) == ERR_LIB_PEM
295 && ERR_GET_REASON(n) == PEM_R_NO_START_LINE)
305 "PEM_read_bio_X509(\"%s\") failed", cert->
data);
310 if (SSL_CTX_add_extra_chain_cert(ssl->
ctx, x509) == 0) {
312 "SSL_CTX_add_extra_chain_cert(\"%s\") failed",
326 if (SSL_CTX_use_PrivateKey_file(ssl->
ctx, (
char *) key->
data,
331 "SSL_CTX_use_PrivateKey_file(\"%s\") failed", key->
data);
343 STACK_OF(X509_NAME) *list;
345 SSL_CTX_set_verify(ssl->
ctx, SSL_VERIFY_PEER, ngx_http_ssl_verify_callback);
347 SSL_CTX_set_verify_depth(ssl->
ctx, depth);
349 if (cert->
len == 0) {
357 if (SSL_CTX_load_verify_locations(ssl->
ctx, (
char *) cert->
data, NULL)
361 "SSL_CTX_load_verify_locations(\"%s\") failed",
366 list = SSL_load_client_CA_file((
char *) cert->
data);
370 "SSL_load_client_CA_file(\"%s\") failed", cert->
data);
381 SSL_CTX_set_client_CA_list(ssl->
ctx, list);
391 SSL_CTX_set_verify_depth(ssl->
ctx, depth);
393 if (cert->
len == 0) {
401 if (SSL_CTX_load_verify_locations(ssl->
ctx, (
char *) cert->
data, NULL)
405 "SSL_CTX_load_verify_locations(\"%s\") failed",
428 store = SSL_CTX_get_cert_store(ssl->
ctx);
432 "SSL_CTX_get_cert_store() failed");
436 lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file());
438 if (lookup == NULL) {
440 "X509_STORE_add_lookup() failed");
444 if (X509_LOOKUP_load_file(lookup, (
char *) crl->
data, X509_FILETYPE_PEM)
448 "X509_LOOKUP_load_file(\"%s\") failed", crl->
data);
452 X509_STORE_set_flags(store,
453 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL);
460 ngx_http_ssl_verify_callback(
int ok, X509_STORE_CTX *x509_store)
463 char *subject, *issuer;
466 X509_NAME *sname, *iname;
470 ssl_conn = X509_STORE_CTX_get_ex_data(x509_store,
471 SSL_get_ex_data_X509_STORE_CTX_idx());
475 cert = X509_STORE_CTX_get_current_cert(x509_store);
476 err = X509_STORE_CTX_get_error(x509_store);
477 depth = X509_STORE_CTX_get_error_depth(x509_store);
479 sname = X509_get_subject_name(cert);
480 subject = sname ? X509_NAME_oneline(sname, NULL, 0) :
"(none)";
482 iname = X509_get_issuer_name(cert);
483 issuer = iname ? X509_NAME_oneline(iname, NULL, 0) :
"(none)";
486 "verify:%d, error:%d, depth:%d, "
487 "subject:\"%s\",issuer: \"%s\"",
488 ok, err, depth, subject, issuer);
491 OPENSSL_free(subject);
495 OPENSSL_free(issuer);
504 ngx_ssl_info_callback(
const ngx_ssl_conn_t *ssl_conn,
int where,
int ret)
508 if (where & SSL_CB_HANDSHAKE_START) {
511 if (c->ssl->handshaked) {
512 c->ssl->renegotiation = 1;
524 if (key_length == 512) {
526 key = RSA_generate_key(512, RSA_F4, NULL, NULL);
548 static unsigned char dh1024_p[] = {
549 0xBB, 0xBC, 0x2D, 0xCA, 0xD8, 0x46, 0x74, 0x90, 0x7C, 0x43, 0xFC, 0xF5,
550 0x80, 0xE9, 0xCF, 0xDB, 0xD9, 0x58, 0xA3, 0xF5, 0x68, 0xB4, 0x2D, 0x4B,
551 0x08, 0xEE, 0xD4, 0xEB, 0x0F, 0xB3, 0x50, 0x4C, 0x6C, 0x03, 0x02, 0x76,
552 0xE7, 0x10, 0x80, 0x0C, 0x5C, 0xCB, 0xBA, 0xA8, 0x92, 0x26, 0x14, 0xC5,
553 0xBE, 0xEC, 0xA5, 0x65, 0xA5, 0xFD, 0xF1, 0xD2, 0x87, 0xA2, 0xBC, 0x04,
554 0x9B, 0xE6, 0x77, 0x80, 0x60, 0xE9, 0x1A, 0x92, 0xA7, 0x57, 0xE3, 0x04,
555 0x8F, 0x68, 0xB0, 0x76, 0xF7, 0xD3, 0x6C, 0xC8, 0xF2, 0x9B, 0xA5, 0xDF,
556 0x81, 0xDC, 0x2C, 0xA7, 0x25, 0xEC, 0xE6, 0x62, 0x70, 0xCC, 0x9A, 0x50,
557 0x35, 0xD8, 0xCE, 0xCE, 0xEF, 0x9E, 0xA0, 0x27, 0x4A, 0x63, 0xAB, 0x1E,
558 0x58, 0xFA, 0xFD, 0x49, 0x88, 0xD0, 0xF6, 0x5D, 0x14, 0x67, 0x57, 0xDA,
559 0x07, 0x1D, 0xF0, 0x45, 0xCF, 0xE1, 0x6B, 0x9B
562 static unsigned char dh1024_g[] = { 0x02 };
565 if (file->
len == 0) {
573 dh->p = BN_bin2bn(dh1024_p,
sizeof(dh1024_p), NULL);
574 dh->g = BN_bin2bn(dh1024_g,
sizeof(dh1024_g), NULL);
576 if (dh->p == NULL || dh->g == NULL) {
582 SSL_CTX_set_tmp_dh(ssl->
ctx, dh);
593 bio = BIO_new_file((
char *) file->
data,
"r");
596 "BIO_new_file(\"%s\") failed", file->
data);
600 dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL);
603 "PEM_read_bio_DHparams(\"%s\") failed", file->
data);
608 SSL_CTX_set_tmp_dh(ssl->
ctx, dh);
620 #if OPENSSL_VERSION_NUMBER >= 0x0090800fL
621 #ifndef OPENSSL_NO_ECDH
632 nid = OBJ_sn2nid((
const char *) name->
data);
635 "Unknown curve name \"%s\"", name->
data);
639 ecdh = EC_KEY_new_by_curve_name(nid);
642 "Unable to create curve \"%s\"", name->
data);
646 SSL_CTX_set_options(ssl->
ctx, SSL_OP_SINGLE_ECDH_USE);
648 SSL_CTX_set_tmp_ecdh(ssl->
ctx, ecdh);
704 if (SSL_set_session(c->ssl->connection, session) == 0) {
720 ngx_ssl_clear_error(c->
log);
722 n = SSL_do_handshake(c->ssl->connection);
738 char buf[129], *s, *d;
739 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
744 cipher = SSL_get_current_cipher(c->ssl->connection);
747 SSL_CIPHER_description(cipher, &buf[1], 128);
749 for (s = &buf[1], d = buf; *s; s++) {
750 if (*s ==
' ' && *d ==
' ') {
754 if (*s ==
LF || *s ==
CR) {
768 "SSL: %s, cipher: \"%s\"",
769 SSL_get_version(c->ssl->connection), &buf[1]);
771 if (SSL_session_reused(c->ssl->connection)) {
773 "SSL reused session");
778 "SSL no shared ciphers");
783 c->ssl->handshaked = 1;
791 if (c->ssl->connection->s3) {
792 c->ssl->connection->s3->flags |= SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS;
798 sslerr = SSL_get_error(c->ssl->connection, n);
802 if (sslerr == SSL_ERROR_WANT_READ) {
818 if (sslerr == SSL_ERROR_WANT_WRITE) {
834 err = (sslerr == SSL_ERROR_SYSCALL) ?
ngx_errno : 0;
836 c->ssl->no_wait_shutdown = 1;
837 c->ssl->no_send_shutdown = 1;
840 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
842 "peer closed connection in SSL handshake");
849 ngx_ssl_connection_error(c, sslerr, err,
"SSL_do_handshake() failed");
863 "SSL handshake handler: %d", ev->
write);
898 if (last == b->
end) {
944 ngx_ssl_clear_error(c->
log);
953 n = SSL_read(c->ssl->connection, buf, size);
961 c->ssl->last = ngx_ssl_handle_recv(c, n);
963 if (c->ssl->last ==
NGX_OK) {
980 switch (c->ssl->last) {
1005 if (c->ssl->renegotiation) {
1014 while (ERR_peek_error()) {
1016 "ignoring stale global SSL error");
1021 c->ssl->no_wait_shutdown = 1;
1022 c->ssl->no_send_shutdown = 1;
1029 if (c->ssl->saved_write_handler) {
1032 c->ssl->saved_write_handler = NULL;
1045 sslerr = SSL_get_error(c->ssl->connection, n);
1047 err = (sslerr == SSL_ERROR_SYSCALL) ?
ngx_errno : 0;
1051 if (sslerr == SSL_ERROR_WANT_READ) {
1056 if (sslerr == SSL_ERROR_WANT_WRITE) {
1059 "peer started SSL renegotiation");
1071 if (c->ssl->saved_write_handler == NULL) {
1079 c->ssl->no_wait_shutdown = 1;
1080 c->ssl->no_send_shutdown = 1;
1082 if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
1084 "peer shutdown SSL cleanly");
1088 ngx_ssl_connection_error(c, sslerr, err,
"SSL_read() failed");
1168 if (buf->
start == NULL) {
1170 if (buf->
start == NULL) {
1180 flush = (in == NULL) ? 1 : buf->
flush;
1184 while (in && buf->
last < buf->
end && send < limit) {
1196 if (size > buf->
end - buf->
last) {
1200 if (send + size > limit) {
1201 size = (ssize_t) (limit - send);
1205 "SSL buf copy: %d", size);
1218 if (!flush && send < limit && buf->last < buf->end) {
1252 if (in == NULL || send == limit) {
1276 ngx_ssl_clear_error(c->
log);
1280 n = SSL_write(c->ssl->connection, data, size);
1286 if (c->ssl->saved_read_handler) {
1289 c->ssl->saved_read_handler = NULL;
1302 sslerr = SSL_get_error(c->ssl->connection, n);
1304 err = (sslerr == SSL_ERROR_SYSCALL) ?
ngx_errno : 0;
1308 if (sslerr == SSL_ERROR_WANT_WRITE) {
1313 if (sslerr == SSL_ERROR_WANT_READ) {
1316 "peer started SSL renegotiation");
1329 if (c->ssl->saved_read_handler == NULL) {
1337 c->ssl->no_wait_shutdown = 1;
1338 c->ssl->no_send_shutdown = 1;
1341 ngx_ssl_connection_error(c, sslerr, err,
"SSL_write() failed");
1361 if (c->ssl->buf && c->ssl->buf->start) {
1363 c->ssl->buf->start = NULL;
1372 int n, sslerr, mode;
1376 mode = SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN;
1377 SSL_set_quiet_shutdown(c->ssl->connection, 1);
1380 mode = SSL_get_shutdown(c->ssl->connection);
1382 if (c->ssl->no_wait_shutdown) {
1383 mode |= SSL_RECEIVED_SHUTDOWN;
1386 if (c->ssl->no_send_shutdown) {
1387 mode |= SSL_SENT_SHUTDOWN;
1390 if (c->ssl->no_wait_shutdown && c->ssl->no_send_shutdown) {
1391 SSL_set_quiet_shutdown(c->ssl->connection, 1);
1395 SSL_set_shutdown(c->ssl->connection, mode);
1397 ngx_ssl_clear_error(c->
log);
1399 n = SSL_shutdown(c->ssl->connection);
1407 if (n != 1 && ERR_peek_error()) {
1408 sslerr = SSL_get_error(c->ssl->connection, n);
1411 "SSL_get_error: %d", sslerr);
1414 if (n == 1 || sslerr == 0 || sslerr == SSL_ERROR_ZERO_RETURN) {
1415 SSL_free(c->ssl->connection);
1421 if (sslerr == SSL_ERROR_WANT_READ || sslerr == SSL_ERROR_WANT_WRITE) {
1433 if (sslerr == SSL_ERROR_WANT_READ) {
1440 err = (sslerr == SSL_ERROR_SYSCALL) ?
ngx_errno : 0;
1442 ngx_ssl_connection_error(c, sslerr, err,
"SSL_shutdown() failed");
1444 SSL_free(c->ssl->connection);
1458 handler = c->ssl->handler;
1483 if (sslerr == SSL_ERROR_SYSCALL) {
1511 }
else if (sslerr == SSL_ERROR_SSL) {
1513 n = ERR_GET_REASON(ERR_peek_error());
1516 if (n == SSL_R_BAD_CHANGE_CIPHER_SPEC
1517 || n == SSL_R_BLOCK_CIPHER_PAD_IS_WRONG
1518 || n == SSL_R_DIGEST_CHECK_FAILED
1519 || n == SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST
1520 || n == SSL_R_EXCESSIVE_MESSAGE_SIZE
1521 || n == SSL_R_LENGTH_MISMATCH
1522 || n == SSL_R_NO_CIPHERS_PASSED
1523 || n == SSL_R_NO_CIPHERS_SPECIFIED
1524 || n == SSL_R_NO_COMPRESSION_SPECIFIED
1525 || n == SSL_R_NO_SHARED_CIPHER
1526 || n == SSL_R_RECORD_LENGTH_MISMATCH
1527 #ifdef SSL_R_PARSE_TLSEXT
1528 || n == SSL_R_PARSE_TLSEXT
1530 || n == SSL_R_UNEXPECTED_MESSAGE
1531 || n == SSL_R_UNEXPECTED_RECORD
1532 || n == SSL_R_UNKNOWN_ALERT_TYPE
1533 || n == SSL_R_UNKNOWN_PROTOCOL
1534 || n == SSL_R_WRONG_VERSION_NUMBER
1535 || n == SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC
1536 #ifdef SSL_R_RENEGOTIATE_EXT_TOO_LONG
1537 || n == SSL_R_RENEGOTIATE_EXT_TOO_LONG
1538 || n == SSL_R_RENEGOTIATION_ENCODING_ERR
1539 || n == SSL_R_RENEGOTIATION_MISMATCH
1541 #ifdef SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
1542 || n == SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
1544 #ifdef SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
1545 || n == SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
1548 || n == SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE
1549 || n == SSL_R_SSLV3_ALERT_BAD_RECORD_MAC
1550 || n == SSL_R_TLSV1_ALERT_DECRYPTION_FAILED
1551 || n == SSL_R_TLSV1_ALERT_RECORD_OVERFLOW
1552 || n == SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE
1553 || n == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE
1554 || n == SSL_R_SSLV3_ALERT_NO_CERTIFICATE
1555 || n == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE
1556 || n == SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE
1557 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED
1558 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED
1559 || n == SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN
1560 || n == SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER
1561 || n == SSL_R_TLSV1_ALERT_UNKNOWN_CA
1562 || n == SSL_R_TLSV1_ALERT_ACCESS_DENIED
1563 || n == SSL_R_TLSV1_ALERT_DECODE_ERROR
1564 || n == SSL_R_TLSV1_ALERT_DECRYPT_ERROR
1565 || n == SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION
1566 || n == SSL_R_TLSV1_ALERT_PROTOCOL_VERSION
1567 || n == SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY
1568 || n == SSL_R_TLSV1_ALERT_INTERNAL_ERROR
1569 || n == SSL_R_TLSV1_ALERT_USER_CANCELLED
1570 || n == SSL_R_TLSV1_ALERT_NO_RENEGOTIATION)
1596 while (ERR_peek_error()) {
1616 va_start(args, fmt);
1620 p =
ngx_cpystrn(p, (u_char *)
" (SSL:", last - p);
1624 n = ERR_peek_error_line_data(NULL, NULL, &data, &flags);
1636 ERR_error_string_n(n, (
char *) p, last - p);
1638 while (p < last && *p) {
1642 if (p < last && *data && (flags & ERR_TXT_STRING)) {
1649 (void) ERR_get_error();
1658 ssize_t builtin_session_cache,
ngx_shm_zone_t *shm_zone, time_t timeout)
1663 SSL_CTX_set_session_cache_mode(ssl->
ctx, SSL_SESS_CACHE_OFF);
1667 SSL_CTX_set_session_id_context(ssl->
ctx, sess_ctx->
data, sess_ctx->
len);
1682 SSL_CTX_set_session_cache_mode(ssl->
ctx,
1683 SSL_SESS_CACHE_SERVER
1684 |SSL_SESS_CACHE_NO_AUTO_CLEAR
1685 |SSL_SESS_CACHE_NO_INTERNAL_STORE);
1687 SSL_CTX_sess_set_cache_size(ssl->
ctx, 1);
1692 cache_mode = SSL_SESS_CACHE_SERVER;
1695 cache_mode |= SSL_SESS_CACHE_NO_INTERNAL;
1698 SSL_CTX_set_session_cache_mode(ssl->
ctx, cache_mode);
1703 SSL_CTX_sess_set_cache_size(ssl->
ctx, builtin_session_cache);
1707 SSL_CTX_set_timeout(ssl->
ctx, (
long) timeout);
1710 SSL_CTX_sess_set_new_cb(ssl->
ctx, ngx_ssl_new_session);
1711 SSL_CTX_sess_set_get_cb(ssl->
ctx, ngx_ssl_get_cached_session);
1712 SSL_CTX_sess_set_remove_cb(ssl->
ctx, ngx_ssl_remove_session);
1718 "SSL_CTX_set_ex_data() failed");
1735 shm_zone->
data = data;
1740 shm_zone->
data = data;
1747 if (cache == NULL) {
1751 shpool->
data = cache;
1752 shm_zone->
data = cache;
1755 ngx_ssl_session_rbtree_insert_value);
1759 len =
sizeof(
" in SSL session shared cache \"\"") + shm_zone->
shm.
name.
len;
1762 if (shpool->
log_ctx == NULL) {
1794 u_char *p, *id, *cached_sess;
1804 len = i2d_SSL_SESSION(sess, NULL);
1813 i2d_SSL_SESSION(sess, &p);
1817 ssl_ctx = SSL_get_SSL_CTX(ssl_conn);
1820 cache = shm_zone->
data;
1826 ngx_ssl_expire_sessions(cache, shpool, 1);
1830 if (cached_sess == NULL) {
1834 ngx_ssl_expire_sessions(cache, shpool, 0);
1838 if (cached_sess == NULL) {
1846 if (sess_id == NULL) {
1850 ngx_ssl_expire_sessions(cache, shpool, 0);
1854 if (sess_id == NULL) {
1859 #if (NGX_PTR_SIZE == 8)
1861 id = sess_id->sess_id;
1871 ngx_ssl_expire_sessions(cache, shpool, 0);
1884 ngx_memcpy(
id, sess->session_id, sess->session_id_length);
1886 hash = ngx_crc32_short(sess->session_id, sess->session_id_length);
1889 "ssl new session: %08XD:%d:%d",
1890 hash, sess->session_id_length, len);
1893 sess_id->
node.
data = (u_char) sess->session_id_length;
1896 sess_id->
session = cached_sess;
1921 "could not add new SSL session to the session cache");
1928 ngx_ssl_get_cached_session(
ngx_ssl_conn_t *ssl_conn, u_char *
id,
int len,
1931 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
1948 hash = ngx_crc32_short(
id, (
size_t) len);
1955 "ssl get session: %08XD:%d", hash, len);
1958 shm_zone = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ssl_conn),
1961 cache = shm_zone->
data;
1972 while (node != sentinel) {
1974 if (hash < node->key) {
1979 if (hash > node->
key) {
1998 sess = d2i_SSL_SESSION(NULL, &p, sess_id->
len);
2008 #if (NGX_PTR_SIZE == 4)
2018 node = (rc < 0) ? node->
left : node->
right;
2032 SSL_CTX_remove_session(ssl, sess);
2034 ngx_ssl_remove_session(ssl, sess);
2053 if (shm_zone == NULL) {
2057 cache = shm_zone->
data;
2059 id = sess->session_id;
2060 len = (size_t) sess->session_id_length;
2062 hash = ngx_crc32_short(
id, len);
2065 "ssl remove session: %08XD:%uz", hash, len);
2074 while (node != sentinel) {
2076 if (hash < node->key) {
2081 if (hash > node->
key) {
2099 #if (NGX_PTR_SIZE == 4)
2107 node = (rc < 0) ? node->
left : node->
right;
2136 if (n++ != 0 && sess_id->
expire > now) {
2143 "expire session: %08Xi", sess_id->
node.
key);
2148 #if (NGX_PTR_SIZE == 4)
2165 if (node->
key < temp->
key) {
2169 }
else if (node->
key > temp->
key) {
2179 (
size_t) node->
data, (
size_t) temp->
data)
2183 if (*p == sentinel) {
2192 node->
left = sentinel;
2193 node->
right = sentinel;
2203 SSL_CTX_free(ssl->
ctx);
2210 s->
data = (u_char *) SSL_get_version(c->ssl->connection);
2218 s->
data = (u_char *) SSL_get_cipher_name(c->ssl->connection);
2230 sess = SSL_get0_session(c->ssl->connection);
2232 len = i2d_SSL_SESSION(sess, NULL);
2241 if (s->
data == NULL) {
2247 i2d_SSL_SESSION(sess, &p);
2266 cert = SSL_get_peer_certificate(c->ssl->connection);
2271 bio = BIO_new(BIO_s_mem());
2278 if (PEM_write_bio_X509(bio, cert) == 0) {
2283 len = BIO_pending(bio);
2287 if (s->
data == NULL) {
2291 BIO_read(bio, s->
data, len);
2319 if (cert.
len == 0) {
2326 for (i = 0; i < cert.
len - 1; i++) {
2327 if (cert.
data[i] ==
LF) {
2334 if (s->
data == NULL) {
2340 for (i = 0; i < cert.
len - 1; i++) {
2341 *p++ = cert.
data[
i];
2342 if (cert.
data[i] ==
LF) {
2361 cert = SSL_get_peer_certificate(c->ssl->connection);
2366 name = X509_get_subject_name(cert);
2372 p = X509_NAME_oneline(name, NULL, 0);
2374 for (len = 0; p[len]; len++) { }
2378 if (s->
data == NULL) {
2403 cert = SSL_get_peer_certificate(c->ssl->connection);
2408 name = X509_get_issuer_name(cert);
2414 p = X509_NAME_oneline(name, NULL, 0);
2416 for (len = 0; p[len]; len++) { }
2420 if (s->
data == NULL) {
2444 cert = SSL_get_peer_certificate(c->ssl->connection);
2449 bio = BIO_new(BIO_s_mem());
2455 i2a_ASN1_INTEGER(bio, X509_get_serialNumber(cert));
2456 len = BIO_pending(bio);
2460 if (s->
data == NULL) {
2466 BIO_read(bio, s->
data, len);
2479 if (SSL_get_verify_result(c->ssl->connection) != X509_V_OK) {
2484 cert = SSL_get_peer_certificate(c->ssl->connection);
2528 return "is duplicate";
2535 engine = ENGINE_by_id((
const char *) value[1].data);
2537 if (engine == NULL) {
2539 "ENGINE_by_id(\"%V\") failed", &value[1]);
2543 if (ENGINE_set_default(engine, ENGINE_METHOD_ALL) == 0) {
2545 "ENGINE_set_default(\"%V\", ENGINE_METHOD_ALL) failed",
2548 ENGINE_free(engine);
2553 ENGINE_free(engine);