10Sigor@sysoev.ru 20Sigor@sysoev.ru /* 30Sigor@sysoev.ru * Copyright (C) Igor Sysoev 40Sigor@sysoev.ru * Copyright (C) NGINX, Inc. 50Sigor@sysoev.ru */ 60Sigor@sysoev.ru 70Sigor@sysoev.ru #include <nxt_main.h> 80Sigor@sysoev.ru #include <openssl/ssl.h> 90Sigor@sysoev.ru #include <openssl/conf.h> 100Sigor@sysoev.ru #include <openssl/err.h> 110Sigor@sysoev.ru 120Sigor@sysoev.ru 130Sigor@sysoev.ru typedef struct { 140Sigor@sysoev.ru SSL *session; 15771Sigor@sysoev.ru nxt_conn_t *conn; 160Sigor@sysoev.ru 170Sigor@sysoev.ru int ssl_error; 180Sigor@sysoev.ru uint8_t times; /* 2 bits */ 19771Sigor@sysoev.ru uint8_t handshake; /* 1 bit */ 200Sigor@sysoev.ru 210Sigor@sysoev.ru nxt_buf_mem_t buffer; 220Sigor@sysoev.ru } nxt_openssl_conn_t; 230Sigor@sysoev.ru 240Sigor@sysoev.ru 25771Sigor@sysoev.ru typedef enum { 26771Sigor@sysoev.ru NXT_OPENSSL_HANDSHAKE = 0, 27771Sigor@sysoev.ru NXT_OPENSSL_READ, 28771Sigor@sysoev.ru NXT_OPENSSL_WRITE, 29771Sigor@sysoev.ru NXT_OPENSSL_SHUTDOWN, 30771Sigor@sysoev.ru } nxt_openssl_io_t; 31771Sigor@sysoev.ru 320Sigor@sysoev.ru 33771Sigor@sysoev.ru static nxt_int_t nxt_openssl_library_init(nxt_task_t *task); 34771Sigor@sysoev.ru static void nxt_openssl_library_free(nxt_task_t *task); 35771Sigor@sysoev.ru #if OPENSSL_VERSION_NUMBER < 0x10100004L 36771Sigor@sysoev.ru static nxt_int_t nxt_openssl_locks_init(void); 37771Sigor@sysoev.ru static void nxt_openssl_lock(int mode, int type, const char *file, int line); 38771Sigor@sysoev.ru static unsigned long nxt_openssl_thread_id(void); 39771Sigor@sysoev.ru static void nxt_openssl_locks_free(void); 40771Sigor@sysoev.ru #endif 41771Sigor@sysoev.ru static nxt_int_t nxt_openssl_server_init(nxt_task_t *task, 42771Sigor@sysoev.ru nxt_tls_conf_t *conf); 43*774Svbart@nginx.com static nxt_uint_t nxt_openssl_chain_file(SSL_CTX *ctx, nxt_fd_t fd); 44771Sigor@sysoev.ru static void nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf); 45771Sigor@sysoev.ru static void nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, 4662Sigor@sysoev.ru nxt_conn_t *c); 471Sigor@sysoev.ru static void nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data); 48771Sigor@sysoev.ru static ssize_t nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b); 49771Sigor@sysoev.ru static ssize_t nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb); 50771Sigor@sysoev.ru static ssize_t nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, 51771Sigor@sysoev.ru void *buf, size_t size); 521Sigor@sysoev.ru static void nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, 530Sigor@sysoev.ru void *data); 54771Sigor@sysoev.ru static nxt_int_t nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, 55771Sigor@sysoev.ru int ret, nxt_err_t sys_err, nxt_openssl_io_t io); 56771Sigor@sysoev.ru static void nxt_cdecl nxt_openssl_conn_error(nxt_task_t *task, 57771Sigor@sysoev.ru nxt_err_t err, const char *fmt, ...); 58771Sigor@sysoev.ru static nxt_uint_t nxt_openssl_log_error_level(nxt_err_t err); 590Sigor@sysoev.ru 600Sigor@sysoev.ru 61771Sigor@sysoev.ru const nxt_tls_lib_t nxt_openssl_lib = { 62771Sigor@sysoev.ru .library_init = nxt_openssl_library_init, 63771Sigor@sysoev.ru .library_free = nxt_openssl_library_free, 64771Sigor@sysoev.ru 65771Sigor@sysoev.ru .server_init = nxt_openssl_server_init, 66771Sigor@sysoev.ru .server_free = nxt_openssl_server_free, 670Sigor@sysoev.ru }; 680Sigor@sysoev.ru 690Sigor@sysoev.ru 7062Sigor@sysoev.ru static nxt_conn_io_t nxt_openssl_conn_io = { 71771Sigor@sysoev.ru .read = nxt_conn_io_read, 72771Sigor@sysoev.ru .recvbuf = nxt_openssl_conn_io_recvbuf, 730Sigor@sysoev.ru 74771Sigor@sysoev.ru .write = nxt_conn_io_write, 75771Sigor@sysoev.ru .sendbuf = nxt_openssl_conn_io_sendbuf, 760Sigor@sysoev.ru 77771Sigor@sysoev.ru .shutdown = nxt_openssl_conn_io_shutdown, 780Sigor@sysoev.ru }; 790Sigor@sysoev.ru 800Sigor@sysoev.ru 810Sigor@sysoev.ru static long nxt_openssl_version; 820Sigor@sysoev.ru static int nxt_openssl_connection_index; 830Sigor@sysoev.ru 840Sigor@sysoev.ru 850Sigor@sysoev.ru static nxt_int_t 86771Sigor@sysoev.ru nxt_openssl_library_init(nxt_task_t *task) 870Sigor@sysoev.ru { 880Sigor@sysoev.ru int index; 890Sigor@sysoev.ru 900Sigor@sysoev.ru if (nxt_fast_path(nxt_openssl_version != 0)) { 910Sigor@sysoev.ru return NXT_OK; 920Sigor@sysoev.ru } 930Sigor@sysoev.ru 94771Sigor@sysoev.ru #if OPENSSL_VERSION_NUMBER >= 0x10100003L 95771Sigor@sysoev.ru 96771Sigor@sysoev.ru OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL); 970Sigor@sysoev.ru 98771Sigor@sysoev.ru #else 99771Sigor@sysoev.ru { 100771Sigor@sysoev.ru nxt_int_t ret; 101771Sigor@sysoev.ru 102771Sigor@sysoev.ru SSL_load_error_strings(); 103771Sigor@sysoev.ru 104771Sigor@sysoev.ru OPENSSL_config(NULL); 1050Sigor@sysoev.ru 106771Sigor@sysoev.ru /* 107771Sigor@sysoev.ru * SSL_library_init(3): 108771Sigor@sysoev.ru * 109771Sigor@sysoev.ru * SSL_library_init() always returns "1", 110771Sigor@sysoev.ru * so it is safe to discard the return value. 111771Sigor@sysoev.ru */ 112771Sigor@sysoev.ru (void) SSL_library_init(); 113771Sigor@sysoev.ru 114771Sigor@sysoev.ru ret = nxt_openssl_locks_init(); 115771Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 116771Sigor@sysoev.ru return ret; 117771Sigor@sysoev.ru } 118771Sigor@sysoev.ru } 119771Sigor@sysoev.ru 120771Sigor@sysoev.ru #endif 1210Sigor@sysoev.ru 1220Sigor@sysoev.ru nxt_openssl_version = SSLeay(); 1230Sigor@sysoev.ru 124771Sigor@sysoev.ru nxt_log(task, NXT_LOG_INFO, "%s, %xl", 125771Sigor@sysoev.ru SSLeay_version(SSLEAY_VERSION), nxt_openssl_version); 1260Sigor@sysoev.ru 1270Sigor@sysoev.ru #ifndef SSL_OP_NO_COMPRESSION 1280Sigor@sysoev.ru { 1290Sigor@sysoev.ru /* 1300Sigor@sysoev.ru * Disable gzip compression in OpenSSL prior to 1.0.0 1310Sigor@sysoev.ru * version, this saves about 522K per connection. 1320Sigor@sysoev.ru */ 1330Sigor@sysoev.ru int n; 1340Sigor@sysoev.ru STACK_OF(SSL_COMP) *ssl_comp_methods; 1350Sigor@sysoev.ru 1360Sigor@sysoev.ru ssl_comp_methods = SSL_COMP_get_compression_methods(); 1370Sigor@sysoev.ru 1380Sigor@sysoev.ru for (n = sk_SSL_COMP_num(ssl_comp_methods); n != 0; n--) { 1390Sigor@sysoev.ru (void) sk_SSL_COMP_pop(ssl_comp_methods); 1400Sigor@sysoev.ru } 1410Sigor@sysoev.ru } 1420Sigor@sysoev.ru #endif 1430Sigor@sysoev.ru 1440Sigor@sysoev.ru index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); 1450Sigor@sysoev.ru 1460Sigor@sysoev.ru if (index == -1) { 147771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 1480Sigor@sysoev.ru "SSL_get_ex_new_index() failed"); 1490Sigor@sysoev.ru return NXT_ERROR; 1500Sigor@sysoev.ru } 1510Sigor@sysoev.ru 1520Sigor@sysoev.ru nxt_openssl_connection_index = index; 1530Sigor@sysoev.ru 1540Sigor@sysoev.ru return NXT_OK; 1550Sigor@sysoev.ru } 1560Sigor@sysoev.ru 1570Sigor@sysoev.ru 158771Sigor@sysoev.ru #if OPENSSL_VERSION_NUMBER >= 0x10100003L 159771Sigor@sysoev.ru 160771Sigor@sysoev.ru static void 161771Sigor@sysoev.ru nxt_openssl_library_free(nxt_task_t *task) 162771Sigor@sysoev.ru { 163771Sigor@sysoev.ru } 164771Sigor@sysoev.ru 165771Sigor@sysoev.ru #else 166771Sigor@sysoev.ru 167771Sigor@sysoev.ru static nxt_thread_mutex_t *nxt_openssl_locks; 168771Sigor@sysoev.ru 1690Sigor@sysoev.ru static nxt_int_t 170771Sigor@sysoev.ru nxt_openssl_locks_init(void) 171771Sigor@sysoev.ru { 172771Sigor@sysoev.ru int i, n; 173771Sigor@sysoev.ru nxt_int_t ret; 174771Sigor@sysoev.ru 175771Sigor@sysoev.ru n = CRYPTO_num_locks(); 176771Sigor@sysoev.ru 177771Sigor@sysoev.ru nxt_openssl_locks = OPENSSL_malloc(n * sizeof(nxt_thread_mutex_t)); 178771Sigor@sysoev.ru if (nxt_slow_path(nxt_openssl_locks == NULL)) { 179771Sigor@sysoev.ru return NXT_ERROR; 180771Sigor@sysoev.ru } 181771Sigor@sysoev.ru 182771Sigor@sysoev.ru for (i = 0; i < n; i++) { 183771Sigor@sysoev.ru ret = nxt_thread_mutex_create(&nxt_openssl_locks[i]); 184771Sigor@sysoev.ru if (nxt_slow_path(ret != NXT_OK)) { 185771Sigor@sysoev.ru return ret; 186771Sigor@sysoev.ru } 187771Sigor@sysoev.ru } 188771Sigor@sysoev.ru 189771Sigor@sysoev.ru CRYPTO_set_locking_callback(nxt_openssl_lock); 190771Sigor@sysoev.ru 191771Sigor@sysoev.ru CRYPTO_set_id_callback(nxt_openssl_thread_id); 192771Sigor@sysoev.ru 193771Sigor@sysoev.ru return NXT_OK; 194771Sigor@sysoev.ru } 195771Sigor@sysoev.ru 196771Sigor@sysoev.ru 197771Sigor@sysoev.ru static void 198771Sigor@sysoev.ru nxt_openssl_lock(int mode, int type, const char *file, int line) 199771Sigor@sysoev.ru { 200771Sigor@sysoev.ru nxt_thread_mutex_t *lock; 201771Sigor@sysoev.ru 202771Sigor@sysoev.ru lock = &nxt_openssl_locks[type]; 203771Sigor@sysoev.ru 204771Sigor@sysoev.ru if ((mode & CRYPTO_LOCK) != 0) { 205771Sigor@sysoev.ru (void) nxt_thread_mutex_lock(lock); 206771Sigor@sysoev.ru 207771Sigor@sysoev.ru } else { 208771Sigor@sysoev.ru (void) nxt_thread_mutex_unlock(lock); 209771Sigor@sysoev.ru } 210771Sigor@sysoev.ru } 211771Sigor@sysoev.ru 212771Sigor@sysoev.ru 213771Sigor@sysoev.ru static u_long 214771Sigor@sysoev.ru nxt_openssl_thread_id(void) 215771Sigor@sysoev.ru { 216771Sigor@sysoev.ru return (u_long) nxt_thread_handle(); 217771Sigor@sysoev.ru } 218771Sigor@sysoev.ru 219771Sigor@sysoev.ru 220771Sigor@sysoev.ru static void 221771Sigor@sysoev.ru nxt_openssl_library_free(nxt_task_t *task) 222771Sigor@sysoev.ru { 223771Sigor@sysoev.ru nxt_openssl_locks_free(); 224771Sigor@sysoev.ru } 225771Sigor@sysoev.ru 226771Sigor@sysoev.ru 227771Sigor@sysoev.ru static void 228771Sigor@sysoev.ru nxt_openssl_locks_free(void) 229771Sigor@sysoev.ru { 230771Sigor@sysoev.ru int i, n; 231771Sigor@sysoev.ru 232771Sigor@sysoev.ru n = CRYPTO_num_locks(); 233771Sigor@sysoev.ru 234771Sigor@sysoev.ru CRYPTO_set_locking_callback(NULL); 235771Sigor@sysoev.ru 236771Sigor@sysoev.ru for (i = 0; i < n; i++) { 237771Sigor@sysoev.ru nxt_thread_mutex_destroy(&nxt_openssl_locks[i]); 238771Sigor@sysoev.ru } 239771Sigor@sysoev.ru 240771Sigor@sysoev.ru OPENSSL_free(nxt_openssl_locks); 241771Sigor@sysoev.ru } 242771Sigor@sysoev.ru 243771Sigor@sysoev.ru #endif 244771Sigor@sysoev.ru 245771Sigor@sysoev.ru 246771Sigor@sysoev.ru static nxt_int_t 247771Sigor@sysoev.ru nxt_openssl_server_init(nxt_task_t *task, nxt_tls_conf_t *conf) 2480Sigor@sysoev.ru { 2490Sigor@sysoev.ru SSL_CTX *ctx; 250*774Svbart@nginx.com nxt_fd_t fd; 251*774Svbart@nginx.com const char *ciphers, *ca_certificate; 2520Sigor@sysoev.ru STACK_OF(X509_NAME) *list; 2530Sigor@sysoev.ru 2540Sigor@sysoev.ru ctx = SSL_CTX_new(SSLv23_server_method()); 2550Sigor@sysoev.ru if (ctx == NULL) { 256771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_CTX_new() failed"); 2570Sigor@sysoev.ru return NXT_ERROR; 2580Sigor@sysoev.ru } 2590Sigor@sysoev.ru 2600Sigor@sysoev.ru conf->ctx = ctx; 2610Sigor@sysoev.ru conf->conn_init = nxt_openssl_conn_init; 2620Sigor@sysoev.ru 263771Sigor@sysoev.ru #ifdef SSL_OP_NO_RENEGOTIATION 264771Sigor@sysoev.ru /* Renegration is not currently supported. */ 265771Sigor@sysoev.ru SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION); 266771Sigor@sysoev.ru #endif 267771Sigor@sysoev.ru 2680Sigor@sysoev.ru #ifdef SSL_OP_NO_COMPRESSION 2690Sigor@sysoev.ru /* 2700Sigor@sysoev.ru * Disable gzip compression in OpenSSL 1.0.0, 2710Sigor@sysoev.ru * this saves about 522K per connection. 2720Sigor@sysoev.ru */ 2730Sigor@sysoev.ru SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION); 2740Sigor@sysoev.ru #endif 2750Sigor@sysoev.ru 2760Sigor@sysoev.ru #ifdef SSL_MODE_RELEASE_BUFFERS 2770Sigor@sysoev.ru 2780Sigor@sysoev.ru if (nxt_openssl_version >= 10001078) { 2790Sigor@sysoev.ru /* 2800Sigor@sysoev.ru * Allow to release read and write buffers in OpenSSL 1.0.0, 2810Sigor@sysoev.ru * this saves about 34K per idle connection. It is not safe 2820Sigor@sysoev.ru * before OpenSSL 1.0.1h (CVE-2010-5298). 2830Sigor@sysoev.ru */ 2840Sigor@sysoev.ru SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); 2850Sigor@sysoev.ru } 2860Sigor@sysoev.ru 2870Sigor@sysoev.ru #endif 2880Sigor@sysoev.ru 289*774Svbart@nginx.com fd = conf->chain_file; 2900Sigor@sysoev.ru 291*774Svbart@nginx.com if (nxt_openssl_chain_file(ctx, fd) != NXT_OK) { 292771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 293*774Svbart@nginx.com "nxt_openssl_chain_file() failed"); 2940Sigor@sysoev.ru goto fail; 2950Sigor@sysoev.ru } 296*774Svbart@nginx.com /* 2970Sigor@sysoev.ru key = conf->certificate_key; 2980Sigor@sysoev.ru 2990Sigor@sysoev.ru if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) == 0) { 300771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 3010Sigor@sysoev.ru "SSL_CTX_use_PrivateKey_file(\"%s\") failed", 3020Sigor@sysoev.ru key); 3030Sigor@sysoev.ru goto fail; 3040Sigor@sysoev.ru } 305*774Svbart@nginx.com */ 3060Sigor@sysoev.ru ciphers = (conf->ciphers != NULL) ? conf->ciphers : "HIGH:!aNULL:!MD5"; 3070Sigor@sysoev.ru 3080Sigor@sysoev.ru if (SSL_CTX_set_cipher_list(ctx, ciphers) == 0) { 309771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 3100Sigor@sysoev.ru "SSL_CTX_set_cipher_list(\"%s\") failed", 3110Sigor@sysoev.ru ciphers); 3120Sigor@sysoev.ru goto fail; 3130Sigor@sysoev.ru } 3140Sigor@sysoev.ru 3150Sigor@sysoev.ru SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); 3160Sigor@sysoev.ru 3170Sigor@sysoev.ru if (conf->ca_certificate != NULL) { 3180Sigor@sysoev.ru 3190Sigor@sysoev.ru /* TODO: verify callback */ 3200Sigor@sysoev.ru SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); 3210Sigor@sysoev.ru 3220Sigor@sysoev.ru /* TODO: verify depth */ 3230Sigor@sysoev.ru SSL_CTX_set_verify_depth(ctx, 1); 3240Sigor@sysoev.ru 3250Sigor@sysoev.ru ca_certificate = conf->ca_certificate; 3260Sigor@sysoev.ru 3270Sigor@sysoev.ru if (SSL_CTX_load_verify_locations(ctx, ca_certificate, NULL) == 0) { 328771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 3290Sigor@sysoev.ru "SSL_CTX_load_verify_locations(\"%s\") failed", 3300Sigor@sysoev.ru ca_certificate); 3310Sigor@sysoev.ru goto fail; 3320Sigor@sysoev.ru } 3330Sigor@sysoev.ru 3340Sigor@sysoev.ru list = SSL_load_client_CA_file(ca_certificate); 3350Sigor@sysoev.ru 3360Sigor@sysoev.ru if (list == NULL) { 337771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, 3380Sigor@sysoev.ru "SSL_load_client_CA_file(\"%s\") failed", 3390Sigor@sysoev.ru ca_certificate); 3400Sigor@sysoev.ru goto fail; 3410Sigor@sysoev.ru } 3420Sigor@sysoev.ru 3430Sigor@sysoev.ru /* 3440Sigor@sysoev.ru * SSL_load_client_CA_file() in OpenSSL prior to 0.9.7h and 3450Sigor@sysoev.ru * 0.9.8 versions always leaves an error in the error queue. 3460Sigor@sysoev.ru */ 3470Sigor@sysoev.ru ERR_clear_error(); 3480Sigor@sysoev.ru 3490Sigor@sysoev.ru SSL_CTX_set_client_CA_list(ctx, list); 3500Sigor@sysoev.ru } 3510Sigor@sysoev.ru 3520Sigor@sysoev.ru return NXT_OK; 3530Sigor@sysoev.ru 3540Sigor@sysoev.ru fail: 3550Sigor@sysoev.ru 3560Sigor@sysoev.ru SSL_CTX_free(ctx); 3570Sigor@sysoev.ru 3580Sigor@sysoev.ru return NXT_ERROR; 3590Sigor@sysoev.ru } 3600Sigor@sysoev.ru 3610Sigor@sysoev.ru 362*774Svbart@nginx.com static nxt_uint_t 363*774Svbart@nginx.com nxt_openssl_chain_file(SSL_CTX *ctx, nxt_fd_t fd) 364*774Svbart@nginx.com { 365*774Svbart@nginx.com BIO *bio; 366*774Svbart@nginx.com X509 *cert, *ca; 367*774Svbart@nginx.com long reason; 368*774Svbart@nginx.com EVP_PKEY *key; 369*774Svbart@nginx.com nxt_uint_t ret; 370*774Svbart@nginx.com 371*774Svbart@nginx.com bio = BIO_new(BIO_s_fd()); 372*774Svbart@nginx.com if (bio == NULL) { 373*774Svbart@nginx.com return NXT_ERROR; 374*774Svbart@nginx.com } 375*774Svbart@nginx.com 376*774Svbart@nginx.com BIO_set_fd(bio, fd, BIO_CLOSE); 377*774Svbart@nginx.com 378*774Svbart@nginx.com ret = NXT_ERROR; 379*774Svbart@nginx.com 380*774Svbart@nginx.com cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL); 381*774Svbart@nginx.com if (cert == NULL) { 382*774Svbart@nginx.com goto end; 383*774Svbart@nginx.com } 384*774Svbart@nginx.com 385*774Svbart@nginx.com if (SSL_CTX_use_certificate(ctx, cert) != 1) { 386*774Svbart@nginx.com goto end; 387*774Svbart@nginx.com } 388*774Svbart@nginx.com 389*774Svbart@nginx.com for ( ;; ) { 390*774Svbart@nginx.com ca = PEM_read_bio_X509(bio, NULL, NULL, NULL); 391*774Svbart@nginx.com 392*774Svbart@nginx.com if (ca == NULL) { 393*774Svbart@nginx.com reason = ERR_GET_REASON(ERR_peek_last_error()); 394*774Svbart@nginx.com if (reason != PEM_R_NO_START_LINE) { 395*774Svbart@nginx.com goto end; 396*774Svbart@nginx.com } 397*774Svbart@nginx.com 398*774Svbart@nginx.com ERR_clear_error(); 399*774Svbart@nginx.com break; 400*774Svbart@nginx.com } 401*774Svbart@nginx.com 402*774Svbart@nginx.com /* 403*774Svbart@nginx.com * Note that ca isn't freed if it was successfully added to the chain, 404*774Svbart@nginx.com * while the main certificate needs a X509_free() call, since 405*774Svbart@nginx.com * its reference count is increased by SSL_CTX_use_certificate(). 406*774Svbart@nginx.com */ 407*774Svbart@nginx.com #if OPENSSL_VERSION_NUMBER > 0x10002000L 408*774Svbart@nginx.com if (SSL_CTX_add0_chain_cert(ctx, ca) != 1) { 409*774Svbart@nginx.com #else 410*774Svbart@nginx.com if (SSL_CTX_add_extra_chain_cert(ctx, ca) != 1) { 411*774Svbart@nginx.com #endif 412*774Svbart@nginx.com X509_free(ca); 413*774Svbart@nginx.com goto end; 414*774Svbart@nginx.com } 415*774Svbart@nginx.com } 416*774Svbart@nginx.com 417*774Svbart@nginx.com if (BIO_reset(bio) != 0) { 418*774Svbart@nginx.com goto end; 419*774Svbart@nginx.com } 420*774Svbart@nginx.com 421*774Svbart@nginx.com key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL); 422*774Svbart@nginx.com if (key == NULL) { 423*774Svbart@nginx.com goto end; 424*774Svbart@nginx.com } 425*774Svbart@nginx.com 426*774Svbart@nginx.com if (SSL_CTX_use_PrivateKey(ctx, key) == 1) { 427*774Svbart@nginx.com ret = NXT_OK; 428*774Svbart@nginx.com } 429*774Svbart@nginx.com 430*774Svbart@nginx.com EVP_PKEY_free(key); 431*774Svbart@nginx.com 432*774Svbart@nginx.com end: 433*774Svbart@nginx.com 434*774Svbart@nginx.com X509_free(cert); 435*774Svbart@nginx.com BIO_free(bio); 436*774Svbart@nginx.com 437*774Svbart@nginx.com return ret; 438*774Svbart@nginx.com } 439*774Svbart@nginx.com 440*774Svbart@nginx.com 4410Sigor@sysoev.ru static void 442771Sigor@sysoev.ru nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf) 4430Sigor@sysoev.ru { 444771Sigor@sysoev.ru SSL_CTX_free(conf->ctx); 445771Sigor@sysoev.ru } 446771Sigor@sysoev.ru 447771Sigor@sysoev.ru 448771Sigor@sysoev.ru static void 449771Sigor@sysoev.ru nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c) 450771Sigor@sysoev.ru { 451771Sigor@sysoev.ru int ret; 452771Sigor@sysoev.ru SSL *s; 453771Sigor@sysoev.ru SSL_CTX *ctx; 454771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 4550Sigor@sysoev.ru 4560Sigor@sysoev.ru nxt_log_debug(c->socket.log, "openssl conn init"); 4570Sigor@sysoev.ru 458771Sigor@sysoev.ru tls = nxt_mp_zget(c->mem_pool, sizeof(nxt_openssl_conn_t)); 459771Sigor@sysoev.ru if (tls == NULL) { 4600Sigor@sysoev.ru goto fail; 4610Sigor@sysoev.ru } 4620Sigor@sysoev.ru 463771Sigor@sysoev.ru c->u.tls = tls; 464771Sigor@sysoev.ru nxt_buf_mem_set_size(&tls->buffer, conf->buffer_size); 4650Sigor@sysoev.ru 4660Sigor@sysoev.ru ctx = conf->ctx; 4670Sigor@sysoev.ru 4680Sigor@sysoev.ru s = SSL_new(ctx); 4690Sigor@sysoev.ru if (s == NULL) { 470771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_new() failed"); 4710Sigor@sysoev.ru goto fail; 4720Sigor@sysoev.ru } 4730Sigor@sysoev.ru 474771Sigor@sysoev.ru tls->session = s; 475771Sigor@sysoev.ru tls->conn = c; 4760Sigor@sysoev.ru 4770Sigor@sysoev.ru ret = SSL_set_fd(s, c->socket.fd); 4780Sigor@sysoev.ru 4790Sigor@sysoev.ru if (ret == 0) { 480771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_set_fd(%d) failed", 481771Sigor@sysoev.ru c->socket.fd); 4820Sigor@sysoev.ru goto fail; 4830Sigor@sysoev.ru } 4840Sigor@sysoev.ru 4850Sigor@sysoev.ru SSL_set_accept_state(s); 4860Sigor@sysoev.ru 4870Sigor@sysoev.ru if (SSL_set_ex_data(s, nxt_openssl_connection_index, c) == 0) { 488771Sigor@sysoev.ru nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_set_ex_data() failed"); 4890Sigor@sysoev.ru goto fail; 4900Sigor@sysoev.ru } 4910Sigor@sysoev.ru 49262Sigor@sysoev.ru c->io = &nxt_openssl_conn_io; 4930Sigor@sysoev.ru c->sendfile = NXT_CONN_SENDFILE_OFF; 4940Sigor@sysoev.ru 4951Sigor@sysoev.ru nxt_openssl_conn_handshake(task, c, c->socket.data); 4960Sigor@sysoev.ru return; 4970Sigor@sysoev.ru 4980Sigor@sysoev.ru fail: 4990Sigor@sysoev.ru 50013Sigor@sysoev.ru nxt_work_queue_add(c->read_work_queue, c->read_state->error_handler, 50113Sigor@sysoev.ru task, c, c->socket.data); 5020Sigor@sysoev.ru } 5030Sigor@sysoev.ru 5040Sigor@sysoev.ru 505771Sigor@sysoev.ru nxt_inline void 506771Sigor@sysoev.ru nxt_openssl_conn_free(nxt_task_t *task, nxt_openssl_conn_t *tls) 5070Sigor@sysoev.ru { 508771Sigor@sysoev.ru nxt_debug(task, "openssl conn free"); 5090Sigor@sysoev.ru 510771Sigor@sysoev.ru nxt_free(tls->buffer.start); 5110Sigor@sysoev.ru 512771Sigor@sysoev.ru SSL_free(tls->session); 5130Sigor@sysoev.ru } 5140Sigor@sysoev.ru 5150Sigor@sysoev.ru 5160Sigor@sysoev.ru static void 5171Sigor@sysoev.ru nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data) 5180Sigor@sysoev.ru { 519771Sigor@sysoev.ru int ret; 520771Sigor@sysoev.ru nxt_int_t n; 521771Sigor@sysoev.ru nxt_err_t err; 522771Sigor@sysoev.ru nxt_conn_t *c; 523771Sigor@sysoev.ru nxt_work_queue_t *wq; 524771Sigor@sysoev.ru nxt_work_handler_t handler; 525771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 526771Sigor@sysoev.ru const nxt_conn_state_t *state; 5270Sigor@sysoev.ru 5280Sigor@sysoev.ru c = obj; 529771Sigor@sysoev.ru tls = c->u.tls; 5300Sigor@sysoev.ru 531771Sigor@sysoev.ru nxt_debug(task, "openssl conn handshake: %d", tls->times); 5320Sigor@sysoev.ru 533771Sigor@sysoev.ru /* "tls->times == 1" is suitable to run SSL_do_handshake() in job. */ 5340Sigor@sysoev.ru 535771Sigor@sysoev.ru ret = SSL_do_handshake(tls->session); 5360Sigor@sysoev.ru 5370Sigor@sysoev.ru err = (ret <= 0) ? nxt_socket_errno : 0; 5380Sigor@sysoev.ru 5391Sigor@sysoev.ru nxt_thread_time_debug_update(task->thread); 5400Sigor@sysoev.ru 5411Sigor@sysoev.ru nxt_debug(task, "SSL_do_handshake(%d): %d err:%d", c->socket.fd, ret, err); 5420Sigor@sysoev.ru 543771Sigor@sysoev.ru state = (c->read_state != NULL) ? c->read_state : c->write_state; 544771Sigor@sysoev.ru 5450Sigor@sysoev.ru if (ret > 0) { 5460Sigor@sysoev.ru /* ret == 1, the handshake was successfully completed. */ 547771Sigor@sysoev.ru tls->handshake = 1; 5480Sigor@sysoev.ru 549771Sigor@sysoev.ru if (c->read_state != NULL) { 550771Sigor@sysoev.ru if (state->io_read_handler != NULL || c->read != NULL) { 551771Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 5520Sigor@sysoev.ru return; 5530Sigor@sysoev.ru } 5540Sigor@sysoev.ru 555771Sigor@sysoev.ru } else { 556771Sigor@sysoev.ru if (c->write != NULL) { 557771Sigor@sysoev.ru nxt_conn_write(task->thread->engine, c); 558771Sigor@sysoev.ru return; 559771Sigor@sysoev.ru } 560771Sigor@sysoev.ru } 561771Sigor@sysoev.ru 562771Sigor@sysoev.ru handler = state->ready_handler; 563771Sigor@sysoev.ru 564771Sigor@sysoev.ru } else { 565771Sigor@sysoev.ru c->socket.read_handler = nxt_openssl_conn_handshake; 566771Sigor@sysoev.ru c->socket.write_handler = nxt_openssl_conn_handshake; 567771Sigor@sysoev.ru 568771Sigor@sysoev.ru n = nxt_openssl_conn_test_error(task, c, ret, err, 569771Sigor@sysoev.ru NXT_OPENSSL_HANDSHAKE); 570771Sigor@sysoev.ru switch (n) { 5710Sigor@sysoev.ru 572771Sigor@sysoev.ru case NXT_AGAIN: 573771Sigor@sysoev.ru if (tls->ssl_error == SSL_ERROR_WANT_READ && tls->times < 2) { 574771Sigor@sysoev.ru tls->times++; 575771Sigor@sysoev.ru } 576771Sigor@sysoev.ru 577771Sigor@sysoev.ru return; 578771Sigor@sysoev.ru 579771Sigor@sysoev.ru case 0: 580771Sigor@sysoev.ru handler = state->close_handler; 581771Sigor@sysoev.ru break; 582771Sigor@sysoev.ru 583771Sigor@sysoev.ru default: 584771Sigor@sysoev.ru case NXT_ERROR: 585771Sigor@sysoev.ru c->socket.error = err; 586771Sigor@sysoev.ru nxt_openssl_conn_error(task, err, "SSL_do_handshake(%d) failed", 587771Sigor@sysoev.ru c->socket.fd); 588771Sigor@sysoev.ru 589771Sigor@sysoev.ru handler = state->error_handler; 590771Sigor@sysoev.ru break; 5910Sigor@sysoev.ru } 5920Sigor@sysoev.ru } 5930Sigor@sysoev.ru 594771Sigor@sysoev.ru wq = (c->read_state != NULL) ? c->read_work_queue : c->write_work_queue; 595771Sigor@sysoev.ru 596771Sigor@sysoev.ru nxt_work_queue_add(wq, handler, task, c, data); 5970Sigor@sysoev.ru } 5980Sigor@sysoev.ru 5990Sigor@sysoev.ru 6000Sigor@sysoev.ru static ssize_t 601771Sigor@sysoev.ru nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b) 6020Sigor@sysoev.ru { 603771Sigor@sysoev.ru int ret; 604771Sigor@sysoev.ru size_t size; 605771Sigor@sysoev.ru nxt_int_t n; 606771Sigor@sysoev.ru nxt_err_t err; 607771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 608771Sigor@sysoev.ru 609771Sigor@sysoev.ru tls = c->u.tls; 610771Sigor@sysoev.ru size = b->mem.end - b->mem.free; 611771Sigor@sysoev.ru 612771Sigor@sysoev.ru ret = SSL_read(tls->session, b->mem.free, size); 613771Sigor@sysoev.ru 614771Sigor@sysoev.ru err = (ret <= 0) ? nxt_socket_errno : 0; 615771Sigor@sysoev.ru 616771Sigor@sysoev.ru nxt_debug(c->socket.task, "SSL_read(%d, %p, %uz): %d err:%d", 617771Sigor@sysoev.ru c->socket.fd, b->mem.free, size, ret, err); 6180Sigor@sysoev.ru 619771Sigor@sysoev.ru if (ret > 0) { 620771Sigor@sysoev.ru if ((size_t) ret < size) { 621771Sigor@sysoev.ru c->socket.read_ready = 0; 622771Sigor@sysoev.ru } 623771Sigor@sysoev.ru 624771Sigor@sysoev.ru return ret; 625771Sigor@sysoev.ru } 6260Sigor@sysoev.ru 627771Sigor@sysoev.ru n = nxt_openssl_conn_test_error(c->socket.task, c, ret, err, 628771Sigor@sysoev.ru NXT_OPENSSL_READ); 629771Sigor@sysoev.ru if (n == NXT_ERROR) { 630771Sigor@sysoev.ru c->socket.error = err; 631771Sigor@sysoev.ru nxt_openssl_conn_error(c->socket.task, err, 632771Sigor@sysoev.ru "SSL_read(%d, %p, %uz) failed", 633771Sigor@sysoev.ru c->socket.fd, b->mem.free, size); 634771Sigor@sysoev.ru } 6350Sigor@sysoev.ru 636771Sigor@sysoev.ru return n; 6370Sigor@sysoev.ru } 6380Sigor@sysoev.ru 6390Sigor@sysoev.ru 6400Sigor@sysoev.ru static ssize_t 641771Sigor@sysoev.ru nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb) 642771Sigor@sysoev.ru { 643771Sigor@sysoev.ru nxt_uint_t niov; 644771Sigor@sysoev.ru struct iovec iov; 645771Sigor@sysoev.ru 646771Sigor@sysoev.ru niov = nxt_sendbuf_mem_coalesce0(task, sb, &iov, 1); 647771Sigor@sysoev.ru 648771Sigor@sysoev.ru if (niov == 0 && sb->sync) { 649771Sigor@sysoev.ru return 0; 650771Sigor@sysoev.ru } 651771Sigor@sysoev.ru 652771Sigor@sysoev.ru return nxt_openssl_conn_io_send(task, sb, iov.iov_base, iov.iov_len); 653771Sigor@sysoev.ru } 654771Sigor@sysoev.ru 655771Sigor@sysoev.ru 656771Sigor@sysoev.ru static ssize_t 657771Sigor@sysoev.ru nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf, 658771Sigor@sysoev.ru size_t size) 6590Sigor@sysoev.ru { 6600Sigor@sysoev.ru int ret; 6610Sigor@sysoev.ru nxt_err_t err; 6620Sigor@sysoev.ru nxt_int_t n; 663771Sigor@sysoev.ru nxt_conn_t *c; 664771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 6650Sigor@sysoev.ru 666771Sigor@sysoev.ru tls = sb->tls; 6670Sigor@sysoev.ru 668771Sigor@sysoev.ru ret = SSL_write(tls->session, buf, size); 6690Sigor@sysoev.ru 6700Sigor@sysoev.ru if (ret <= 0) { 6710Sigor@sysoev.ru err = nxt_socket_errno; 672771Sigor@sysoev.ru sb->error = err; 6730Sigor@sysoev.ru 6740Sigor@sysoev.ru } else { 6750Sigor@sysoev.ru err = 0; 6760Sigor@sysoev.ru } 6770Sigor@sysoev.ru 678771Sigor@sysoev.ru nxt_debug(task, "SSL_write(%d, %p, %uz): %d err:%d", 679771Sigor@sysoev.ru sb->socket, buf, size, ret, err); 6800Sigor@sysoev.ru 6810Sigor@sysoev.ru if (ret > 0) { 6820Sigor@sysoev.ru return ret; 6830Sigor@sysoev.ru } 6840Sigor@sysoev.ru 685771Sigor@sysoev.ru c = tls->conn; 686771Sigor@sysoev.ru c->socket.write_ready = sb->ready; 687771Sigor@sysoev.ru c->socket.error = sb->error; 688771Sigor@sysoev.ru 689771Sigor@sysoev.ru n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_WRITE); 690771Sigor@sysoev.ru 691771Sigor@sysoev.ru sb->ready = c->socket.write_ready; 692771Sigor@sysoev.ru sb->error = c->socket.error; 6930Sigor@sysoev.ru 6940Sigor@sysoev.ru if (n == NXT_ERROR) { 695771Sigor@sysoev.ru sb->error = err; 696771Sigor@sysoev.ru nxt_openssl_conn_error(task, err, "SSL_write(%d, %p, %uz) failed", 697771Sigor@sysoev.ru sb->socket, buf, size); 6980Sigor@sysoev.ru } 6990Sigor@sysoev.ru 7000Sigor@sysoev.ru return n; 7010Sigor@sysoev.ru } 7020Sigor@sysoev.ru 7030Sigor@sysoev.ru 7040Sigor@sysoev.ru static void 7051Sigor@sysoev.ru nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data) 7060Sigor@sysoev.ru { 7070Sigor@sysoev.ru int ret, mode; 7080Sigor@sysoev.ru SSL *s; 7090Sigor@sysoev.ru nxt_err_t err; 7100Sigor@sysoev.ru nxt_int_t n; 7110Sigor@sysoev.ru nxt_bool_t quiet, once; 71262Sigor@sysoev.ru nxt_conn_t *c; 713771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 7140Sigor@sysoev.ru nxt_work_handler_t handler; 7150Sigor@sysoev.ru 7160Sigor@sysoev.ru c = obj; 7170Sigor@sysoev.ru 7181Sigor@sysoev.ru nxt_debug(task, "openssl conn shutdown"); 7190Sigor@sysoev.ru 720771Sigor@sysoev.ru c->read_state = NULL; 721771Sigor@sysoev.ru tls = c->u.tls; 722771Sigor@sysoev.ru s = tls->session; 7230Sigor@sysoev.ru 724771Sigor@sysoev.ru if (s == NULL || !tls->handshake) { 725771Sigor@sysoev.ru handler = c->write_state->ready_handler; 7260Sigor@sysoev.ru goto done; 7270Sigor@sysoev.ru } 7280Sigor@sysoev.ru 7290Sigor@sysoev.ru mode = SSL_get_shutdown(s); 7300Sigor@sysoev.ru 7310Sigor@sysoev.ru if (c->socket.timedout || c->socket.error != 0) { 7320Sigor@sysoev.ru quiet = 1; 7330Sigor@sysoev.ru 7340Sigor@sysoev.ru } else if (c->socket.closed && !(mode & SSL_RECEIVED_SHUTDOWN)) { 7350Sigor@sysoev.ru quiet = 1; 7360Sigor@sysoev.ru 7370Sigor@sysoev.ru } else { 7380Sigor@sysoev.ru quiet = 0; 7390Sigor@sysoev.ru } 7400Sigor@sysoev.ru 7410Sigor@sysoev.ru SSL_set_quiet_shutdown(s, quiet); 7420Sigor@sysoev.ru 7430Sigor@sysoev.ru once = 1; 7440Sigor@sysoev.ru 7450Sigor@sysoev.ru for ( ;; ) { 7460Sigor@sysoev.ru SSL_set_shutdown(s, mode); 7470Sigor@sysoev.ru 7480Sigor@sysoev.ru ret = SSL_shutdown(s); 7490Sigor@sysoev.ru 7500Sigor@sysoev.ru err = (ret <= 0) ? nxt_socket_errno : 0; 7510Sigor@sysoev.ru 7521Sigor@sysoev.ru nxt_debug(task, "SSL_shutdown(%d, %d, %b): %d err:%d", 7531Sigor@sysoev.ru c->socket.fd, mode, quiet, ret, err); 7540Sigor@sysoev.ru 7550Sigor@sysoev.ru if (ret > 0) { 7560Sigor@sysoev.ru /* ret == 1, the shutdown was successfully completed. */ 757771Sigor@sysoev.ru handler = c->write_state->ready_handler; 7580Sigor@sysoev.ru goto done; 7590Sigor@sysoev.ru } 7600Sigor@sysoev.ru 7610Sigor@sysoev.ru if (ret == 0) { 7620Sigor@sysoev.ru /* 7630Sigor@sysoev.ru * If SSL_shutdown() returns 0 then it should be called 764771Sigor@sysoev.ru * again. The second SSL_shutdown() call should return 7650Sigor@sysoev.ru * -1/SSL_ERROR_WANT_READ or -1/SSL_ERROR_WANT_WRITE. 7660Sigor@sysoev.ru * OpenSSL prior to 0.9.8m version however never returns 767771Sigor@sysoev.ru * -1 at all. Fortunately, OpenSSL preserves internally 7680Sigor@sysoev.ru * correct status available via SSL_get_error(-1). 7690Sigor@sysoev.ru */ 7700Sigor@sysoev.ru if (once) { 771771Sigor@sysoev.ru once = 0; 7720Sigor@sysoev.ru mode = SSL_get_shutdown(s); 7730Sigor@sysoev.ru continue; 7740Sigor@sysoev.ru } 7750Sigor@sysoev.ru 7760Sigor@sysoev.ru ret = -1; 7770Sigor@sysoev.ru } 7780Sigor@sysoev.ru 7790Sigor@sysoev.ru /* ret == -1 */ 7800Sigor@sysoev.ru 7810Sigor@sysoev.ru break; 7820Sigor@sysoev.ru } 7830Sigor@sysoev.ru 784771Sigor@sysoev.ru c->socket.read_handler = nxt_openssl_conn_io_shutdown; 785771Sigor@sysoev.ru c->socket.write_handler = nxt_openssl_conn_io_shutdown; 786771Sigor@sysoev.ru c->socket.error_handler = c->write_state->error_handler; 787771Sigor@sysoev.ru 788771Sigor@sysoev.ru n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_SHUTDOWN); 789771Sigor@sysoev.ru 790771Sigor@sysoev.ru switch (n) { 7910Sigor@sysoev.ru 792771Sigor@sysoev.ru case 0: 793771Sigor@sysoev.ru handler = c->write_state->close_handler; 794771Sigor@sysoev.ru break; 795771Sigor@sysoev.ru 796771Sigor@sysoev.ru case NXT_AGAIN: 797771Sigor@sysoev.ru nxt_timer_add(task->thread->engine, &c->read_timer, 5000); 7980Sigor@sysoev.ru return; 799771Sigor@sysoev.ru 800771Sigor@sysoev.ru default: 801771Sigor@sysoev.ru case NXT_ERROR: 802771Sigor@sysoev.ru c->socket.error = err; 803771Sigor@sysoev.ru nxt_openssl_conn_error(task, err, "SSL_shutdown(%d) failed", 804771Sigor@sysoev.ru c->socket.fd); 805771Sigor@sysoev.ru handler = c->write_state->error_handler; 8060Sigor@sysoev.ru } 8070Sigor@sysoev.ru 808771Sigor@sysoev.ru done: 8090Sigor@sysoev.ru 810771Sigor@sysoev.ru nxt_openssl_conn_free(task, tls); 8110Sigor@sysoev.ru 81213Sigor@sysoev.ru nxt_work_queue_add(c->write_work_queue, handler, task, c, data); 8130Sigor@sysoev.ru } 8140Sigor@sysoev.ru 8150Sigor@sysoev.ru 8160Sigor@sysoev.ru static nxt_int_t 81762Sigor@sysoev.ru nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret, 818771Sigor@sysoev.ru nxt_err_t sys_err, nxt_openssl_io_t io) 8190Sigor@sysoev.ru { 8200Sigor@sysoev.ru u_long lib_err; 821771Sigor@sysoev.ru nxt_openssl_conn_t *tls; 8220Sigor@sysoev.ru 823771Sigor@sysoev.ru tls = c->u.tls; 8240Sigor@sysoev.ru 825771Sigor@sysoev.ru tls->ssl_error = SSL_get_error(tls->session, ret); 8260Sigor@sysoev.ru 827771Sigor@sysoev.ru nxt_debug(task, "SSL_get_error(): %d", tls->ssl_error); 8280Sigor@sysoev.ru 829771Sigor@sysoev.ru switch (tls->ssl_error) { 8300Sigor@sysoev.ru 8310Sigor@sysoev.ru case SSL_ERROR_WANT_READ: 832771Sigor@sysoev.ru 833771Sigor@sysoev.ru if (io != NXT_OPENSSL_READ) { 834771Sigor@sysoev.ru nxt_fd_event_block_write(task->thread->engine, &c->socket); 8350Sigor@sysoev.ru 836771Sigor@sysoev.ru c->socket.read_ready = 0; 8370Sigor@sysoev.ru 838771Sigor@sysoev.ru if (nxt_fd_event_is_disabled(c->socket.read)) { 839771Sigor@sysoev.ru nxt_fd_event_enable_read(task->thread->engine, &c->socket); 840771Sigor@sysoev.ru } 8410Sigor@sysoev.ru } 8420Sigor@sysoev.ru 8430Sigor@sysoev.ru return NXT_AGAIN; 8440Sigor@sysoev.ru 8450Sigor@sysoev.ru case SSL_ERROR_WANT_WRITE: 846771Sigor@sysoev.ru 847771Sigor@sysoev.ru if (io != NXT_OPENSSL_WRITE) { 848771Sigor@sysoev.ru nxt_fd_event_block_read(task->thread->engine, &c->socket); 8490Sigor@sysoev.ru 850771Sigor@sysoev.ru c->socket.write_ready = 0; 8510Sigor@sysoev.ru 852771Sigor@sysoev.ru if (nxt_fd_event_is_disabled(c->socket.write)) { 853771Sigor@sysoev.ru nxt_fd_event_enable_write(task->thread->engine, &c->socket); 854771Sigor@sysoev.ru } 8550Sigor@sysoev.ru } 8560Sigor@sysoev.ru 8570Sigor@sysoev.ru return NXT_AGAIN; 8580Sigor@sysoev.ru 8590Sigor@sysoev.ru case SSL_ERROR_SYSCALL: 8600Sigor@sysoev.ru lib_err = ERR_peek_error(); 8610Sigor@sysoev.ru 8621Sigor@sysoev.ru nxt_debug(task, "ERR_peek_error(): %l", lib_err); 8630Sigor@sysoev.ru 8640Sigor@sysoev.ru if (sys_err != 0 || lib_err != 0) { 8650Sigor@sysoev.ru return NXT_ERROR; 8660Sigor@sysoev.ru } 8670Sigor@sysoev.ru 8680Sigor@sysoev.ru /* A connection was just closed. */ 8690Sigor@sysoev.ru c->socket.closed = 1; 870771Sigor@sysoev.ru return 0; 8710Sigor@sysoev.ru 8720Sigor@sysoev.ru case SSL_ERROR_ZERO_RETURN: 8730Sigor@sysoev.ru /* A "close notify" alert. */ 8740Sigor@sysoev.ru return 0; 8750Sigor@sysoev.ru 8760Sigor@sysoev.ru default: /* SSL_ERROR_SSL, etc. */ 8770Sigor@sysoev.ru c->socket.error = 1000; /* Nonexistent errno code. */ 8780Sigor@sysoev.ru return NXT_ERROR; 8790Sigor@sysoev.ru } 8800Sigor@sysoev.ru } 8810Sigor@sysoev.ru 8820Sigor@sysoev.ru 8830Sigor@sysoev.ru static void nxt_cdecl 884771Sigor@sysoev.ru nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...) 8850Sigor@sysoev.ru { 8860Sigor@sysoev.ru u_char *p, *end; 8870Sigor@sysoev.ru va_list args; 8880Sigor@sysoev.ru nxt_uint_t level; 8890Sigor@sysoev.ru u_char msg[NXT_MAX_ERROR_STR]; 8900Sigor@sysoev.ru 891771Sigor@sysoev.ru level = nxt_openssl_log_error_level(err); 8920Sigor@sysoev.ru 893771Sigor@sysoev.ru if (nxt_log_level_enough(task->log, level)) { 8940Sigor@sysoev.ru 8950Sigor@sysoev.ru end = msg + sizeof(msg); 8960Sigor@sysoev.ru 8970Sigor@sysoev.ru va_start(args, fmt); 8980Sigor@sysoev.ru p = nxt_vsprintf(msg, end, fmt, args); 8990Sigor@sysoev.ru va_end(args); 9000Sigor@sysoev.ru 9010Sigor@sysoev.ru if (err != 0) { 9020Sigor@sysoev.ru p = nxt_sprintf(p, end, " %E", err); 9030Sigor@sysoev.ru } 9040Sigor@sysoev.ru 9050Sigor@sysoev.ru p = nxt_openssl_copy_error(p, end); 9060Sigor@sysoev.ru 907771Sigor@sysoev.ru nxt_log(task, level, "%*s", p - msg, msg); 9080Sigor@sysoev.ru 9090Sigor@sysoev.ru } else { 9100Sigor@sysoev.ru ERR_clear_error(); 9110Sigor@sysoev.ru } 9120Sigor@sysoev.ru } 9130Sigor@sysoev.ru 9140Sigor@sysoev.ru 9150Sigor@sysoev.ru static nxt_uint_t 916771Sigor@sysoev.ru nxt_openssl_log_error_level(nxt_err_t err) 9170Sigor@sysoev.ru { 9180Sigor@sysoev.ru switch (ERR_GET_REASON(ERR_peek_error())) { 9190Sigor@sysoev.ru 9200Sigor@sysoev.ru case 0: 92113Sigor@sysoev.ru return nxt_socket_error_level(err); 9220Sigor@sysoev.ru 9230Sigor@sysoev.ru case SSL_R_BAD_CHANGE_CIPHER_SPEC: /* 103 */ 9240Sigor@sysoev.ru case SSL_R_BLOCK_CIPHER_PAD_IS_WRONG: /* 129 */ 9250Sigor@sysoev.ru case SSL_R_DIGEST_CHECK_FAILED: /* 149 */ 9260Sigor@sysoev.ru case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST: /* 151 */ 9270Sigor@sysoev.ru case SSL_R_EXCESSIVE_MESSAGE_SIZE: /* 152 */ 9280Sigor@sysoev.ru case SSL_R_LENGTH_MISMATCH: /* 159 */ 929771Sigor@sysoev.ru #ifdef SSL_R_NO_CIPHERS_PASSED 9300Sigor@sysoev.ru case SSL_R_NO_CIPHERS_PASSED: /* 182 */ 931771Sigor@sysoev.ru #endif 9320Sigor@sysoev.ru case SSL_R_NO_CIPHERS_SPECIFIED: /* 183 */ 9330Sigor@sysoev.ru case SSL_R_NO_COMPRESSION_SPECIFIED: /* 187 */ 9340Sigor@sysoev.ru case SSL_R_NO_SHARED_CIPHER: /* 193 */ 9350Sigor@sysoev.ru case SSL_R_RECORD_LENGTH_MISMATCH: /* 213 */ 9360Sigor@sysoev.ru #ifdef SSL_R_PARSE_TLSEXT 9370Sigor@sysoev.ru case SSL_R_PARSE_TLSEXT: /* 227 */ 9380Sigor@sysoev.ru #endif 9390Sigor@sysoev.ru case SSL_R_UNEXPECTED_MESSAGE: /* 244 */ 9400Sigor@sysoev.ru case SSL_R_UNEXPECTED_RECORD: /* 245 */ 9410Sigor@sysoev.ru case SSL_R_UNKNOWN_ALERT_TYPE: /* 246 */ 9420Sigor@sysoev.ru case SSL_R_UNKNOWN_PROTOCOL: /* 252 */ 9430Sigor@sysoev.ru case SSL_R_WRONG_VERSION_NUMBER: /* 267 */ 9440Sigor@sysoev.ru case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC: /* 281 */ 9450Sigor@sysoev.ru #ifdef SSL_R_RENEGOTIATE_EXT_TOO_LONG 9460Sigor@sysoev.ru case SSL_R_RENEGOTIATE_EXT_TOO_LONG: /* 335 */ 9470Sigor@sysoev.ru case SSL_R_RENEGOTIATION_ENCODING_ERR: /* 336 */ 9480Sigor@sysoev.ru case SSL_R_RENEGOTIATION_MISMATCH: /* 337 */ 9490Sigor@sysoev.ru #endif 9500Sigor@sysoev.ru #ifdef SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED 9510Sigor@sysoev.ru case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED: /* 338 */ 9520Sigor@sysoev.ru #endif 9530Sigor@sysoev.ru #ifdef SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING 9540Sigor@sysoev.ru case SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING: /* 345 */ 9550Sigor@sysoev.ru #endif 9560Sigor@sysoev.ru case 1000:/* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */ 9570Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE: /* 1010 */ 9580Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC: /* 1020 */ 9590Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED: /* 1021 */ 9600Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW: /* 1022 */ 9610Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE: /* 1030 */ 9620Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE: /* 1040 */ 9630Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER: /* 1047 */ 9640Sigor@sysoev.ru break; 9650Sigor@sysoev.ru 9660Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_NO_CERTIFICATE: /* 1041 */ 9670Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE: /* 1042 */ 9680Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE: /* 1043 */ 9690Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED: /* 1044 */ 9700Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED: /* 1045 */ 9710Sigor@sysoev.ru case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN: /* 1046 */ 9720Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_UNKNOWN_CA: /* 1048 */ 9730Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_ACCESS_DENIED: /* 1049 */ 9740Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_DECODE_ERROR: /* 1050 */ 9750Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_DECRYPT_ERROR: /* 1051 */ 9760Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION: /* 1060 */ 9770Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION: /* 1070 */ 9780Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY: /* 1071 */ 9790Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_INTERNAL_ERROR: /* 1080 */ 9800Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_USER_CANCELLED: /* 1090 */ 9810Sigor@sysoev.ru case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION: /* 1100 */ 9820Sigor@sysoev.ru return NXT_LOG_ERR; 9830Sigor@sysoev.ru 9840Sigor@sysoev.ru default: 985564Svbart@nginx.com return NXT_LOG_ALERT; 9860Sigor@sysoev.ru } 9870Sigor@sysoev.ru 9880Sigor@sysoev.ru return NXT_LOG_INFO; 9890Sigor@sysoev.ru } 9900Sigor@sysoev.ru 9910Sigor@sysoev.ru 992771Sigor@sysoev.ru void nxt_cdecl 993771Sigor@sysoev.ru nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, ...) 9940Sigor@sysoev.ru { 9950Sigor@sysoev.ru u_char *p, *end; 9960Sigor@sysoev.ru va_list args; 9970Sigor@sysoev.ru u_char msg[NXT_MAX_ERROR_STR]; 9980Sigor@sysoev.ru 9990Sigor@sysoev.ru end = msg + sizeof(msg); 10000Sigor@sysoev.ru 10010Sigor@sysoev.ru va_start(args, fmt); 10020Sigor@sysoev.ru p = nxt_vsprintf(msg, end, fmt, args); 10030Sigor@sysoev.ru va_end(args); 10040Sigor@sysoev.ru 10050Sigor@sysoev.ru p = nxt_openssl_copy_error(p, end); 10060Sigor@sysoev.ru 1007771Sigor@sysoev.ru nxt_log(task, level, "%*s", p - msg, msg); 10080Sigor@sysoev.ru } 10090Sigor@sysoev.ru 10100Sigor@sysoev.ru 1011771Sigor@sysoev.ru u_char * 10120Sigor@sysoev.ru nxt_openssl_copy_error(u_char *p, u_char *end) 10130Sigor@sysoev.ru { 10140Sigor@sysoev.ru int flags; 10150Sigor@sysoev.ru u_long err; 10160Sigor@sysoev.ru nxt_bool_t clear; 10170Sigor@sysoev.ru const char *data, *delimiter; 10180Sigor@sysoev.ru 10190Sigor@sysoev.ru err = ERR_peek_error(); 10200Sigor@sysoev.ru if (err == 0) { 10210Sigor@sysoev.ru return p; 10220Sigor@sysoev.ru } 10230Sigor@sysoev.ru 10240Sigor@sysoev.ru /* Log the most relevant error message ... */ 10250Sigor@sysoev.ru data = ERR_reason_error_string(err); 10260Sigor@sysoev.ru 10270Sigor@sysoev.ru p = nxt_sprintf(p, end, " (%d: %s) (OpenSSL: ", ERR_GET_REASON(err), data); 10280Sigor@sysoev.ru 10290Sigor@sysoev.ru /* 1030771Sigor@sysoev.ru * ... followed by all queued cumbersome OpenSSL error messages 1031771Sigor@sysoev.ru * and drain the error queue. 10320Sigor@sysoev.ru */ 10330Sigor@sysoev.ru delimiter = ""; 10340Sigor@sysoev.ru clear = 0; 10350Sigor@sysoev.ru 10360Sigor@sysoev.ru for ( ;; ) { 10370Sigor@sysoev.ru err = ERR_get_error_line_data(NULL, NULL, &data, &flags); 10380Sigor@sysoev.ru if (err == 0) { 10390Sigor@sysoev.ru break; 10400Sigor@sysoev.ru } 10410Sigor@sysoev.ru 10420Sigor@sysoev.ru p = nxt_sprintf(p, end, "%s", delimiter); 10430Sigor@sysoev.ru 10440Sigor@sysoev.ru ERR_error_string_n(err, (char *) p, end - p); 10450Sigor@sysoev.ru 10460Sigor@sysoev.ru while (p < end && *p != '\0') { 10470Sigor@sysoev.ru p++; 10480Sigor@sysoev.ru } 10490Sigor@sysoev.ru 10500Sigor@sysoev.ru if ((flags & ERR_TXT_STRING) != 0) { 10510Sigor@sysoev.ru p = nxt_sprintf(p, end, ":%s", data); 10520Sigor@sysoev.ru } 10530Sigor@sysoev.ru 10540Sigor@sysoev.ru clear |= ((flags & ERR_TXT_MALLOCED) != 0); 10550Sigor@sysoev.ru 10560Sigor@sysoev.ru delimiter = "; "; 10570Sigor@sysoev.ru } 10580Sigor@sysoev.ru 10590Sigor@sysoev.ru /* Deallocate additional data. */ 10600Sigor@sysoev.ru 10610Sigor@sysoev.ru if (clear) { 10620Sigor@sysoev.ru ERR_clear_error(); 10630Sigor@sysoev.ru } 10640Sigor@sysoev.ru 10650Sigor@sysoev.ru if (p < end) { 10660Sigor@sysoev.ru *p++ = ')'; 10670Sigor@sysoev.ru } 10680Sigor@sysoev.ru 10690Sigor@sysoev.ru return p; 10700Sigor@sysoev.ru } 1071