2004-07-16 14:33:35 +08:00
|
|
|
|
2004-07-16 00:35:51 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2004-07-16 14:33:35 +08:00
|
|
|
#include <ngx_event.h>
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_ssl_init(ngx_log_t *log)
|
|
|
|
{
|
|
|
|
SSL_library_init();
|
|
|
|
SSL_load_error_strings();
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
ngx_int_t ngx_ssl_create_session(ngx_ssl_ctx_t *ssl_ctx, ngx_connection_t *c,
|
|
|
|
ngx_uint_t flags)
|
2004-07-16 00:35:51 +08:00
|
|
|
{
|
|
|
|
ngx_ssl_t *ssl;
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (!(ssl = ngx_pcalloc(c->pool, sizeof(ngx_ssl_t)))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-07-16 00:35:51 +08:00
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (flags & NGX_SSL_BUFFER) {
|
|
|
|
if (!(ssl->buf = ngx_create_temp_buf(c->pool, NGX_SSL_BUFSIZE))) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->ssl = SSL_new(ssl_ctx);
|
|
|
|
|
|
|
|
if (ssl->ssl == NULL) {
|
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_new() failed");
|
2004-07-16 00:35:51 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (SSL_set_fd(ssl->ssl, c->fd) == 0) {
|
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_set_fd() failed");
|
2004-07-16 00:35:51 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
SSL_set_accept_state(ssl->ssl);
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
c->ssl = ssl;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_ssl_recv(ngx_connection_t *c, u_char *buf, size_t size)
|
|
|
|
{
|
2004-07-17 01:11:43 +08:00
|
|
|
int n, sslerr;
|
|
|
|
ngx_err_t err;
|
2004-07-16 00:35:51 +08:00
|
|
|
char *handshake;
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
n = SSL_read(c->ssl->ssl, buf, size);
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_read: %d", n);
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
sslerr = SSL_get_error(c->ssl->ssl, n);
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", sslerr);
|
2004-07-16 00:35:51 +08:00
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
err = (sslerr == SSL_ERROR_SYSCALL) ? ngx_errno : 0;
|
2004-07-16 00:35:51 +08:00
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (sslerr == SSL_ERROR_WANT_READ) {
|
2004-07-16 00:35:51 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
2004-07-16 14:33:35 +08:00
|
|
|
|
2004-07-16 00:35:51 +08:00
|
|
|
#if 0
|
2004-07-17 01:11:43 +08:00
|
|
|
if (sslerr == SSL_ERROR_WANT_WRITE) {
|
2004-07-16 00:35:51 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (!SSL_is_init_finished(c->ssl->ssl)) {
|
2004-07-16 00:35:51 +08:00
|
|
|
handshake = "in SSL handshake";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
handshake = "";
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
if (sslerr == SSL_ERROR_ZERO_RETURN || ERR_peek_error() == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, err,
|
2004-07-16 00:35:51 +08:00
|
|
|
"client closed connection%s", handshake);
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
SSL_set_shutdown(c->ssl->ssl, SSL_RECEIVED_SHUTDOWN);
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, err,
|
|
|
|
"SSL_read() failed%s", handshake);
|
2004-07-16 00:35:51 +08:00
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
SSL_set_shutdown(c->ssl->ssl, SSL_RECEIVED_SHUTDOWN);
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_chain_t *ngx_ssl_send_chain(ngx_connection_t *c, ngx_chain_t *in,
|
|
|
|
off_t limit)
|
|
|
|
{
|
2004-07-17 01:11:43 +08:00
|
|
|
int n;
|
|
|
|
ssize_t send, size;
|
|
|
|
ngx_buf_t *buf;
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
send = 0;
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
buf = c->ssl->buf;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
if (buf) {
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
for ( /* void */ ; in && buf->last < buf->end; in = in->next) {
|
|
|
|
if (ngx_buf_special(in->buf)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = in->buf->last - in->buf->pos;
|
|
|
|
|
|
|
|
if (size > buf->end - buf->last) {
|
|
|
|
size = buf->end - buf->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"SSL buf copy: %d", size);
|
|
|
|
|
|
|
|
ngx_memcpy(buf->last, in->buf->pos, size);
|
|
|
|
|
|
|
|
buf->last += size;
|
|
|
|
in->buf->pos += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = buf->last - buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"SSL to write: %d", size);
|
|
|
|
|
|
|
|
n = SSL_write(c->ssl->ssl, buf->pos, size);
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"SSL_write: %d", n);
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
buf->pos += n;
|
|
|
|
send += n;
|
|
|
|
|
|
|
|
if (n < size) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send < limit) {
|
|
|
|
if (buf->pos == buf->last) {
|
|
|
|
buf->pos = buf->start;
|
|
|
|
buf->last = buf->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = SSL_get_error(c->ssl->ssl, n);
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"SSL_get_error: %d", n);
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_WRITE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (n == SSL_ERROR_WANT_READ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, "SSL_write() failed");
|
|
|
|
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
c->write->ready = 0;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->pos == buf->last) {
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
c->write->ready = 0;
|
|
|
|
return NGX_CHAIN_AGAIN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-07-16 14:33:35 +08:00
|
|
|
for (/* void */; in; in = in->next) {
|
|
|
|
if (ngx_buf_special(in->buf)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = in->buf->last - in->buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"SSL to write: %d", size);
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
n = SSL_write(c->ssl->ssl, in->buf->pos, size);
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_write: %d", n);
|
|
|
|
|
|
|
|
if (n > 0) {
|
|
|
|
in->buf->pos += n;
|
|
|
|
send += n;
|
|
|
|
|
|
|
|
if (n == size) {
|
|
|
|
if (send < limit) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
c->write->ready = 0;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
n = SSL_get_error(c->ssl->ssl, n);
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", n);
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_WRITE) {
|
|
|
|
c->write->ready = 0;
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (n == SSL_ERROR_WANT_READ) {
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_write() failed");
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t ngx_ssl_shutdown(ngx_connection_t *c)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
ngx_uint_t again;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (c->read->timedout || c->write->timedout) {
|
|
|
|
SSL_set_shutdown(c->ssl, SSL_RECEIVED_SHUTDOWN);
|
|
|
|
SSL_set_shutdown(c->ssl, SSL_RECEIVED_SHUTDOWN|SSL_SENT_SHUTDOWN);
|
|
|
|
}
|
|
|
|
#endif
|
2004-07-16 00:35:51 +08:00
|
|
|
|
2004-07-16 14:33:35 +08:00
|
|
|
#if 0
|
2004-07-17 01:11:43 +08:00
|
|
|
SSL_set_shutdown(c->ssl->ssl, SSL_RECEIVED_SHUTDOWN);
|
2004-07-16 14:33:35 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
again = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2004-07-17 01:11:43 +08:00
|
|
|
n = SSL_shutdown(c->ssl->ssl);
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_shutdown: %d", n);
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
again = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 1) {
|
2004-07-17 01:11:43 +08:00
|
|
|
SSL_free(c->ssl->ssl);
|
2004-07-16 14:33:35 +08:00
|
|
|
c->ssl = NULL;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!again) {
|
2004-07-17 01:11:43 +08:00
|
|
|
n = SSL_get_error(c->ssl->ssl, n);
|
2004-07-16 14:33:35 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "SSL_get_error: %d", n);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (again || n == SSL_ERROR_WANT_READ) {
|
|
|
|
|
|
|
|
ngx_add_timer(c->read, 10000);
|
|
|
|
|
|
|
|
if (ngx_handle_read_event(c->read, 0) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == SSL_ERROR_WANT_WRITE) {
|
|
|
|
|
|
|
|
if (ngx_handle_write_event(c->write, 0) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
ngx_ssl_error(NGX_LOG_ALERT, c->log, 0, "SSL_shutdown() failed");
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
void ngx_ssl_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
|
|
|
|
char *fmt, ...)
|
2004-07-16 00:35:51 +08:00
|
|
|
{
|
2004-07-17 01:11:43 +08:00
|
|
|
int len;
|
|
|
|
char errstr[NGX_MAX_CONF_ERRSTR];
|
|
|
|
va_list args;
|
2004-07-16 00:35:51 +08:00
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
len = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
errstr[len++] = ' ';
|
|
|
|
errstr[len++] = '(';
|
|
|
|
errstr[len++] = 'S';
|
|
|
|
errstr[len++] = 'S';
|
|
|
|
errstr[len++] = 'L';
|
|
|
|
errstr[len++] = ':';
|
|
|
|
errstr[len++] = ' ';
|
|
|
|
|
|
|
|
ERR_error_string_n(ERR_get_error(), errstr + len, sizeof(errstr) - len - 1);
|
|
|
|
|
2004-07-17 01:11:43 +08:00
|
|
|
ngx_log_error(level, log, err, "%s)", errstr);
|
2004-07-16 00:35:51 +08:00
|
|
|
}
|