xref: /unit/src/nxt_openssl.c (revision 2077:624e51cfe97a)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include <nxt_conf.h>
9 #include <openssl/ssl.h>
10 #include <openssl/conf.h>
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include <openssl/x509v3.h>
14 #include <openssl/bio.h>
15 #include <openssl/evp.h>
16 
17 
18 typedef struct {
19     SSL               *session;
20     nxt_conn_t        *conn;
21 
22     int               ssl_error;
23     uint8_t           times;      /* 2 bits */
24     uint8_t           handshake;  /* 1 bit  */
25 
26     nxt_tls_conf_t    *conf;
27     nxt_buf_mem_t     buffer;
28 } nxt_openssl_conn_t;
29 
30 
31 struct nxt_tls_ticket_s {
32     u_char            name[16];
33     u_char            hmac_key[32];
34     u_char            aes_key[32];
35     uint8_t           size;
36 };
37 
38 
39 struct nxt_tls_tickets_s {
40     nxt_uint_t        count;
41     nxt_tls_ticket_t  tickets[];
42 };
43 
44 
45 typedef enum {
46     NXT_OPENSSL_HANDSHAKE = 0,
47     NXT_OPENSSL_READ,
48     NXT_OPENSSL_WRITE,
49     NXT_OPENSSL_SHUTDOWN,
50 } nxt_openssl_io_t;
51 
52 
53 static nxt_int_t nxt_openssl_library_init(nxt_task_t *task);
54 static void nxt_openssl_library_free(nxt_task_t *task);
55 #if OPENSSL_VERSION_NUMBER < 0x10100004L
56 static nxt_int_t nxt_openssl_locks_init(void);
57 static void nxt_openssl_lock(int mode, int type, const char *file, int line);
58 static unsigned long nxt_openssl_thread_id(void);
59 static void nxt_openssl_locks_free(void);
60 #endif
61 static nxt_int_t nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp,
62     nxt_tls_init_t *tls_init, nxt_bool_t last);
63 static nxt_int_t nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx,
64     nxt_tls_conf_t *conf, nxt_mp_t *mp, nxt_bool_t single);
65 #if (NXT_HAVE_OPENSSL_CONF_CMD)
66 static nxt_int_t nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx,
67     nxt_conf_value_t *value, nxt_mp_t *mp);
68 #endif
69 #if (NXT_HAVE_OPENSSL_TLSEXT)
70 static nxt_int_t nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx,
71     nxt_tls_init_t *tls_init, nxt_mp_t *mp);
72 static int nxt_tls_ticket_key_callback(SSL *s, unsigned char *name,
73     unsigned char *iv, EVP_CIPHER_CTX *ectx,HMAC_CTX *hctx, int enc);
74 #endif
75 static void nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size,
76     time_t timeout);
77 static nxt_uint_t nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert,
78     nxt_tls_conf_t *conf, nxt_mp_t *mp);
79 static nxt_int_t nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq,
80     void *data);
81 static nxt_int_t nxt_openssl_bundle_hash_insert(nxt_task_t *task,
82     nxt_lvlhsh_t *lvlhsh, nxt_tls_bundle_hash_item_t *item, nxt_mp_t * mp);
83 static nxt_int_t nxt_openssl_servername(SSL *s, int *ad, void *arg);
84 static nxt_tls_bundle_conf_t *nxt_openssl_find_ctx(nxt_tls_conf_t *conf,
85     nxt_str_t *sn);
86 static void nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf);
87 static void nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf,
88     nxt_conn_t *c);
89 static void nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data);
90 static ssize_t nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b);
91 static ssize_t nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb);
92 static ssize_t nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb,
93     void *buf, size_t size);
94 static void nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj,
95     void *data);
96 static nxt_int_t nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c,
97     int ret, nxt_err_t sys_err, nxt_openssl_io_t io);
98 static void nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj,
99     void *data);
100 static void nxt_cdecl nxt_openssl_conn_error(nxt_task_t *task,
101     nxt_err_t err, const char *fmt, ...);
102 static nxt_uint_t nxt_openssl_log_error_level(nxt_err_t err);
103 
104 
105 const nxt_tls_lib_t  nxt_openssl_lib = {
106     .library_init = nxt_openssl_library_init,
107     .library_free = nxt_openssl_library_free,
108 
109     .server_init = nxt_openssl_server_init,
110     .server_free = nxt_openssl_server_free,
111 };
112 
113 
114 static nxt_conn_io_t  nxt_openssl_conn_io = {
115     .read = nxt_conn_io_read,
116     .recvbuf = nxt_openssl_conn_io_recvbuf,
117 
118     .write = nxt_conn_io_write,
119     .sendbuf = nxt_openssl_conn_io_sendbuf,
120 
121     .shutdown = nxt_openssl_conn_io_shutdown,
122 };
123 
124 
125 static long  nxt_openssl_version;
126 static int   nxt_openssl_connection_index;
127 
128 
129 static nxt_int_t
130 nxt_openssl_library_init(nxt_task_t *task)
131 {
132     int  index;
133 
134     if (nxt_fast_path(nxt_openssl_version != 0)) {
135         return NXT_OK;
136     }
137 
138 #if OPENSSL_VERSION_NUMBER >= 0x10100003L
139 
140     OPENSSL_init_ssl(OPENSSL_INIT_LOAD_CONFIG, NULL);
141 
142 #else
143     {
144         nxt_int_t  ret;
145 
146         SSL_load_error_strings();
147 
148         OPENSSL_config(NULL);
149 
150         /*
151          * SSL_library_init(3):
152          *
153          *   SSL_library_init() always returns "1",
154          *   so it is safe to discard the return value.
155          */
156         (void) SSL_library_init();
157 
158         ret = nxt_openssl_locks_init();
159         if (nxt_slow_path(ret != NXT_OK)) {
160             return ret;
161         }
162     }
163 
164 #endif
165 
166     nxt_openssl_version = SSLeay();
167 
168     nxt_log(task, NXT_LOG_INFO, "%s, %xl",
169             SSLeay_version(SSLEAY_VERSION), nxt_openssl_version);
170 
171 #ifndef SSL_OP_NO_COMPRESSION
172     {
173         /*
174          * Disable gzip compression in OpenSSL prior to 1.0.0
175          * version, this saves about 522K per connection.
176          */
177         int                 n;
178         STACK_OF(SSL_COMP)  *ssl_comp_methods;
179 
180         ssl_comp_methods = SSL_COMP_get_compression_methods();
181 
182         for (n = sk_SSL_COMP_num(ssl_comp_methods); n != 0; n--) {
183             (void) sk_SSL_COMP_pop(ssl_comp_methods);
184         }
185     }
186 #endif
187 
188     index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
189 
190     if (index == -1) {
191         nxt_openssl_log_error(task, NXT_LOG_ALERT,
192                               "SSL_get_ex_new_index() failed");
193         return NXT_ERROR;
194     }
195 
196     nxt_openssl_connection_index = index;
197 
198     return NXT_OK;
199 }
200 
201 
202 #if OPENSSL_VERSION_NUMBER >= 0x10100003L
203 
204 static void
205 nxt_openssl_library_free(nxt_task_t *task)
206 {
207 }
208 
209 #else
210 
211 static nxt_thread_mutex_t  *nxt_openssl_locks;
212 
213 static nxt_int_t
214 nxt_openssl_locks_init(void)
215 {
216     int        i, n;
217     nxt_int_t  ret;
218 
219     n = CRYPTO_num_locks();
220 
221     nxt_openssl_locks = OPENSSL_malloc(n * sizeof(nxt_thread_mutex_t));
222     if (nxt_slow_path(nxt_openssl_locks == NULL)) {
223         return NXT_ERROR;
224     }
225 
226     for (i = 0; i < n; i++) {
227         ret = nxt_thread_mutex_create(&nxt_openssl_locks[i]);
228         if (nxt_slow_path(ret != NXT_OK)) {
229             return ret;
230         }
231     }
232 
233     CRYPTO_set_locking_callback(nxt_openssl_lock);
234 
235     CRYPTO_set_id_callback(nxt_openssl_thread_id);
236 
237     return NXT_OK;
238 }
239 
240 
241 static void
242 nxt_openssl_lock(int mode, int type, const char *file, int line)
243 {
244     nxt_thread_mutex_t  *lock;
245 
246     lock = &nxt_openssl_locks[type];
247 
248     if ((mode & CRYPTO_LOCK) != 0) {
249         (void) nxt_thread_mutex_lock(lock);
250 
251     } else {
252         (void) nxt_thread_mutex_unlock(lock);
253     }
254 }
255 
256 
257 static u_long
258 nxt_openssl_thread_id(void)
259 {
260     return (u_long) nxt_thread_handle();
261 }
262 
263 
264 static void
265 nxt_openssl_library_free(nxt_task_t *task)
266 {
267     nxt_openssl_locks_free();
268 }
269 
270 
271 static void
272 nxt_openssl_locks_free(void)
273 {
274     int  i, n;
275 
276     n = CRYPTO_num_locks();
277 
278     CRYPTO_set_locking_callback(NULL);
279 
280     for (i = 0; i < n; i++) {
281         nxt_thread_mutex_destroy(&nxt_openssl_locks[i]);
282     }
283 
284     OPENSSL_free(nxt_openssl_locks);
285 }
286 
287 #endif
288 
289 
290 static nxt_int_t
291 nxt_openssl_server_init(nxt_task_t *task, nxt_mp_t *mp,
292     nxt_tls_init_t *tls_init, nxt_bool_t last)
293 {
294     SSL_CTX                *ctx;
295     const char             *ciphers, *ca_certificate;
296     nxt_tls_conf_t         *conf;
297     STACK_OF(X509_NAME)    *list;
298     nxt_tls_bundle_conf_t  *bundle;
299 
300     ctx = SSL_CTX_new(SSLv23_server_method());
301     if (ctx == NULL) {
302         nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_CTX_new() failed");
303         return NXT_ERROR;
304     }
305 
306     conf = tls_init->conf;
307 
308     bundle = conf->bundle;
309     nxt_assert(bundle != NULL);
310 
311     bundle->ctx = ctx;
312 
313 #ifdef SSL_OP_NO_RENEGOTIATION
314     /* Renegration is not currently supported. */
315     SSL_CTX_set_options(ctx, SSL_OP_NO_RENEGOTIATION);
316 #endif
317 
318 #ifdef SSL_OP_NO_COMPRESSION
319     /*
320      * Disable gzip compression in OpenSSL 1.0.0,
321      * this saves about 522K per connection.
322      */
323     SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION);
324 #endif
325 
326 #ifdef SSL_MODE_RELEASE_BUFFERS
327 
328     if (nxt_openssl_version >= 10001078) {
329         /*
330          * Allow to release read and write buffers in OpenSSL 1.0.0,
331          * this saves about 34K per idle connection.  It is not safe
332          * before OpenSSL 1.0.1h (CVE-2010-5298).
333          */
334         SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS);
335     }
336 
337 #endif
338 
339     if (nxt_openssl_chain_file(task, ctx, conf, mp,
340                                last && bundle->next == NULL)
341         != NXT_OK)
342     {
343         goto fail;
344     }
345 /*
346     key = conf->certificate_key;
347 
348     if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) == 0) {
349         nxt_openssl_log_error(task, NXT_LOG_ALERT,
350                               "SSL_CTX_use_PrivateKey_file(\"%s\") failed",
351                               key);
352         goto fail;
353     }
354 */
355 
356     ciphers = (conf->ciphers != NULL) ? conf->ciphers : "HIGH:!aNULL:!MD5";
357 
358     if (SSL_CTX_set_cipher_list(ctx, ciphers) == 0) {
359         nxt_openssl_log_error(task, NXT_LOG_ALERT,
360                               "SSL_CTX_set_cipher_list(\"%s\") failed",
361                               ciphers);
362         goto fail;
363     }
364 
365 #if (NXT_HAVE_OPENSSL_CONF_CMD)
366     if (tls_init->conf_cmds != NULL
367         && nxt_ssl_conf_commands(task, ctx, tls_init->conf_cmds, mp) != NXT_OK)
368     {
369         goto fail;
370     }
371 #endif
372 
373     nxt_ssl_session_cache(ctx, tls_init->cache_size, tls_init->timeout);
374 
375 #if (NXT_HAVE_OPENSSL_TLSEXT)
376     if (nxt_tls_ticket_keys(task, ctx, tls_init, mp) != NXT_OK) {
377         goto fail;
378     }
379 #endif
380 
381     SSL_CTX_set_options(ctx, SSL_OP_CIPHER_SERVER_PREFERENCE);
382 
383     if (conf->ca_certificate != NULL) {
384 
385         /* TODO: verify callback */
386         SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
387 
388         /* TODO: verify depth */
389         SSL_CTX_set_verify_depth(ctx, 1);
390 
391         ca_certificate = conf->ca_certificate;
392 
393         if (SSL_CTX_load_verify_locations(ctx, ca_certificate, NULL) == 0) {
394             nxt_openssl_log_error(task, NXT_LOG_ALERT,
395                               "SSL_CTX_load_verify_locations(\"%s\") failed",
396                               ca_certificate);
397             goto fail;
398         }
399 
400         list = SSL_load_client_CA_file(ca_certificate);
401 
402         if (list == NULL) {
403             nxt_openssl_log_error(task, NXT_LOG_ALERT,
404                               "SSL_load_client_CA_file(\"%s\") failed",
405                               ca_certificate);
406             goto fail;
407         }
408 
409         /*
410          * SSL_load_client_CA_file() in OpenSSL prior to 0.9.7h and
411          * 0.9.8 versions always leaves an error in the error queue.
412          */
413         ERR_clear_error();
414 
415         SSL_CTX_set_client_CA_list(ctx, list);
416     }
417 
418     if (last) {
419         conf->conn_init = nxt_openssl_conn_init;
420 
421         if (bundle->next != NULL) {
422             SSL_CTX_set_tlsext_servername_callback(ctx, nxt_openssl_servername);
423         }
424     }
425 
426     return NXT_OK;
427 
428 fail:
429 
430     SSL_CTX_free(ctx);
431 
432 #if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \
433      && OPENSSL_VERSION_NUMBER < 0x1010101fL)
434     RAND_keep_random_devices_open(0);
435 #endif
436 
437     return NXT_ERROR;
438 }
439 
440 
441 static nxt_int_t
442 nxt_openssl_chain_file(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_conf_t *conf,
443     nxt_mp_t *mp, nxt_bool_t single)
444 {
445     BIO                    *bio;
446     X509                   *cert, *ca;
447     long                   reason;
448     EVP_PKEY               *key;
449     nxt_int_t              ret;
450     nxt_tls_bundle_conf_t  *bundle;
451 
452     ret = NXT_ERROR;
453     cert = NULL;
454 
455     bio = BIO_new(BIO_s_fd());
456     if (bio == NULL) {
457         goto end;
458     }
459 
460     bundle = conf->bundle;
461 
462     BIO_set_fd(bio, bundle->chain_file, BIO_CLOSE);
463 
464     cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL);
465     if (cert == NULL) {
466         goto end;
467     }
468 
469     if (SSL_CTX_use_certificate(ctx, cert) != 1) {
470         goto end;
471     }
472 
473     if (!single && nxt_openssl_cert_get_names(task, cert, conf, mp) != NXT_OK) {
474         goto clean;
475     }
476 
477     for ( ;; ) {
478         ca = PEM_read_bio_X509(bio, NULL, NULL, NULL);
479 
480         if (ca == NULL) {
481             reason = ERR_GET_REASON(ERR_peek_last_error());
482             if (reason != PEM_R_NO_START_LINE) {
483                 goto end;
484             }
485 
486             ERR_clear_error();
487             break;
488         }
489 
490         /*
491          * Note that ca isn't freed if it was successfully added to the chain,
492          * while the main certificate needs a X509_free() call, since
493          * its reference count is increased by SSL_CTX_use_certificate().
494          */
495 #ifdef SSL_CTX_add0_chain_cert
496         if (SSL_CTX_add0_chain_cert(ctx, ca) != 1) {
497 #else
498         if (SSL_CTX_add_extra_chain_cert(ctx, ca) != 1) {
499 #endif
500             X509_free(ca);
501             goto end;
502         }
503     }
504 
505     if (BIO_reset(bio) != 0) {
506         goto end;
507     }
508 
509     key = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL);
510     if (key == NULL) {
511         goto end;
512     }
513 
514     if (SSL_CTX_use_PrivateKey(ctx, key) == 1) {
515         ret = NXT_OK;
516     }
517 
518     EVP_PKEY_free(key);
519 
520 end:
521 
522     if (ret != NXT_OK) {
523         nxt_openssl_log_error(task, NXT_LOG_ALERT,
524                               "nxt_openssl_chain_file() failed");
525     }
526 
527 clean:
528 
529     BIO_free(bio);
530     X509_free(cert);
531 
532     return ret;
533 }
534 
535 
536 #if (NXT_HAVE_OPENSSL_CONF_CMD)
537 
538 static nxt_int_t
539 nxt_ssl_conf_commands(nxt_task_t *task, SSL_CTX *ctx, nxt_conf_value_t *conf,
540     nxt_mp_t *mp)
541 {
542     int               ret;
543     char              *zcmd, *zvalue;
544     uint32_t          index;
545     nxt_str_t         cmd, value;
546     SSL_CONF_CTX      *cctx;
547     nxt_conf_value_t  *member;
548 
549     cctx = SSL_CONF_CTX_new();
550     if (nxt_slow_path(cctx == NULL)) {
551         nxt_openssl_log_error(task, NXT_LOG_ALERT,
552                               "SSL_CONF_CTX_new() failed");
553         return NXT_ERROR;
554     }
555 
556     SSL_CONF_CTX_set_flags(cctx, SSL_CONF_FLAG_FILE
557                                  | SSL_CONF_FLAG_SERVER
558                                  | SSL_CONF_FLAG_CERTIFICATE
559                                  | SSL_CONF_FLAG_SHOW_ERRORS);
560 
561     SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
562 
563     index = 0;
564 
565     for ( ;; ) {
566         member = nxt_conf_next_object_member(conf, &cmd, &index);
567         if (nxt_slow_path(member == NULL)) {
568             break;
569         }
570 
571         nxt_conf_get_string(member, &value);
572 
573         zcmd = nxt_str_cstrz(mp, &cmd);
574         zvalue = nxt_str_cstrz(mp, &value);
575 
576         if (nxt_slow_path(zcmd == NULL || zvalue == NULL)) {
577             goto fail;
578         }
579 
580         ret = SSL_CONF_cmd(cctx, zcmd, zvalue);
581         if (ret == -2) {
582             nxt_openssl_log_error(task, NXT_LOG_ERR,
583                                   "unknown command \"%s\" in "
584                                   "\"conf_commands\" option", zcmd);
585             goto fail;
586         }
587 
588         if (ret <= 0) {
589             nxt_openssl_log_error(task, NXT_LOG_ERR,
590                                   "invalid value \"%s\" for command \"%s\" "
591                                   "in \"conf_commands\" option",
592                                   zvalue, zcmd);
593             goto fail;
594         }
595 
596         nxt_debug(task, "SSL_CONF_cmd(\"%s\", \"%s\")", zcmd, zvalue);
597     }
598 
599     if (SSL_CONF_CTX_finish(cctx) != 1) {
600         nxt_openssl_log_error(task, NXT_LOG_ALERT,
601                               "SSL_CONF_finish() failed");
602         goto fail;
603     }
604 
605     SSL_CONF_CTX_free(cctx);
606 
607     return NXT_OK;
608 
609 fail:
610 
611     SSL_CONF_CTX_free(cctx);
612 
613     return NXT_ERROR;
614 }
615 
616 #endif
617 
618 #if (NXT_HAVE_OPENSSL_TLSEXT)
619 
620 static nxt_int_t
621 nxt_tls_ticket_keys(nxt_task_t *task, SSL_CTX *ctx, nxt_tls_init_t *tls_init,
622     nxt_mp_t *mp)
623 {
624     size_t             len;
625     uint32_t           i;
626     nxt_str_t          value;
627     nxt_uint_t         count;
628     nxt_conf_value_t   *member, *tickets_conf;
629     nxt_tls_ticket_t   *ticket;
630     nxt_tls_tickets_t  *tickets;
631     u_char             buf[80];
632 
633     tickets_conf = tls_init->tickets_conf;
634 
635     if (tickets_conf == NULL) {
636         goto no_ticket;
637     }
638 
639     if (nxt_conf_type(tickets_conf) == NXT_CONF_BOOLEAN) {
640         if (nxt_conf_get_boolean(tickets_conf) == 0) {
641             goto no_ticket;
642         }
643 
644         return NXT_OK;
645     }
646 
647     count = nxt_conf_array_elements_count_or_1(tickets_conf);
648 
649     if (count == 0) {
650         goto no_ticket;
651     }
652 
653 #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB
654 
655     tickets = nxt_mp_get(mp, sizeof(nxt_tls_tickets_t)
656                              + count * sizeof(nxt_tls_ticket_t));
657     if (nxt_slow_path(tickets == NULL)) {
658         return NXT_ERROR;
659     }
660 
661     tickets->count = count;
662     tls_init->conf->tickets = tickets;
663     i = 0;
664 
665     do {
666         ticket = &tickets->tickets[i];
667 
668         i++;
669 
670         member = nxt_conf_get_array_element_or_itself(tickets_conf, count - i);
671         if (member == NULL) {
672             break;
673         }
674 
675         nxt_conf_get_string(member, &value);
676 
677         len = nxt_base64_decode(buf, value.start, value.length);
678 
679         nxt_memcpy(ticket->name, buf, 16);
680 
681         if (len == 48) {
682             nxt_memcpy(ticket->aes_key, buf + 16, 16);
683             nxt_memcpy(ticket->hmac_key, buf + 32, 16);
684             ticket->size = 16;
685 
686         } else {
687             nxt_memcpy(ticket->hmac_key, buf + 16, 32);
688             nxt_memcpy(ticket->aes_key, buf + 48, 32);
689             ticket->size = 32;
690         }
691 
692     } while (i < count);
693 
694     if (SSL_CTX_set_tlsext_ticket_key_cb(ctx, nxt_tls_ticket_key_callback)
695         == 0)
696     {
697         nxt_openssl_log_error(task, NXT_LOG_ALERT,
698                       "Unit was built with Session Tickets support, however, "
699                       "now it is linked dynamically to an OpenSSL library "
700                       "which has no tlsext support, therefore Session Tickets "
701                       "are not available");
702 
703         return NXT_ERROR;
704     }
705 
706     return NXT_OK;
707 
708 #else
709     nxt_alert(task, "Setting custom session ticket keys is not supported with "
710                     "this version of OpenSSL library");
711 
712     return NXT_ERROR;
713 
714 #endif
715 
716 no_ticket:
717 
718     SSL_CTX_set_options(ctx, SSL_OP_NO_TICKET);
719 
720     return NXT_OK;
721 }
722 
723 
724 #ifdef SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB
725 
726 static int
727 nxt_tls_ticket_key_callback(SSL *s, unsigned char *name, unsigned char *iv,
728     EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc)
729 {
730     nxt_uint_t          i;
731     nxt_conn_t          *c;
732     const EVP_MD        *digest;
733     const EVP_CIPHER    *cipher;
734     nxt_tls_ticket_t    *ticket;
735     nxt_openssl_conn_t  *tls;
736 
737     c = SSL_get_ex_data(s, nxt_openssl_connection_index);
738 
739     if (nxt_slow_path(c == NULL)) {
740         nxt_thread_log_alert("SSL_get_ex_data() failed");
741         return -1;
742     }
743 
744     tls = c->u.tls;
745     ticket = tls->conf->tickets->tickets;
746 
747     i = 0;
748 
749     if (enc == 1) {
750         /* encrypt session ticket */
751 
752         nxt_debug(c->socket.task, "TLS session ticket encrypt");
753 
754         cipher = (ticket[0].size == 16) ? EVP_aes_128_cbc() : EVP_aes_256_cbc();
755 
756         if (RAND_bytes(iv, EVP_CIPHER_iv_length(cipher)) != 1) {
757             nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT,
758                                   "RAND_bytes() failed");
759             return -1;
760         }
761 
762         nxt_memcpy(name, ticket[0].name, 16);
763 
764         if (EVP_EncryptInit_ex(ectx, cipher, NULL, ticket[0].aes_key, iv) != 1)
765         {
766             nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT,
767                                   "EVP_EncryptInit_ex() failed");
768             return -1;
769         }
770 
771     } else {
772         /* decrypt session ticket */
773 
774         do {
775             if (nxt_memcmp(name, ticket[i].name, 16) == 0) {
776                 goto found;
777             }
778 
779         } while (++i < tls->conf->tickets->count);
780 
781         nxt_debug(c->socket.task, "TLS session ticket decrypt, key not found");
782 
783         return 0;
784 
785     found:
786 
787         nxt_debug(c->socket.task,
788                   "TLS session ticket decrypt, key number: \"%d\"", i);
789 
790         enc = (i == 0) ? 1 : 2 /* renew */;
791 
792         cipher = (ticket[i].size == 16) ? EVP_aes_128_cbc() : EVP_aes_256_cbc();
793 
794         if (EVP_DecryptInit_ex(ectx, cipher, NULL, ticket[i].aes_key, iv) != 1)
795         {
796             nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT,
797                                   "EVP_DecryptInit_ex() failed");
798             return -1;
799         }
800     }
801 
802 #ifdef OPENSSL_NO_SHA256
803     digest = EVP_sha1();
804 #else
805     digest = EVP_sha256();
806 #endif
807 
808     if (HMAC_Init_ex(hctx, ticket[i].hmac_key, ticket[i].size, digest, NULL)
809         != 1)
810     {
811         nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT,
812                               "HMAC_Init_ex() failed");
813         return -1;
814     }
815 
816     return enc;
817 }
818 
819 #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */
820 
821 #endif /* NXT_HAVE_OPENSSL_TLSEXT */
822 
823 
824 static void
825 nxt_ssl_session_cache(SSL_CTX *ctx, size_t cache_size, time_t timeout)
826 {
827     if (cache_size == 0) {
828         SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF);
829         return;
830     }
831 
832     SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER);
833 
834     SSL_CTX_sess_set_cache_size(ctx, cache_size);
835 
836     SSL_CTX_set_timeout(ctx, (long) timeout);
837 }
838 
839 
840 static nxt_uint_t
841 nxt_openssl_cert_get_names(nxt_task_t *task, X509 *cert, nxt_tls_conf_t *conf,
842     nxt_mp_t *mp)
843 {
844     int                         len;
845     nxt_str_t                   domain, str;
846     X509_NAME                   *x509_name;
847     nxt_uint_t                  i, n;
848     GENERAL_NAME                *name;
849     nxt_tls_bundle_conf_t       *bundle;
850     STACK_OF(GENERAL_NAME)      *alt_names;
851     nxt_tls_bundle_hash_item_t  *item;
852 
853     bundle = conf->bundle;
854 
855     alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
856 
857     if (alt_names != NULL) {
858         n = sk_GENERAL_NAME_num(alt_names);
859 
860         for (i = 0; i != n; i++) {
861             name = sk_GENERAL_NAME_value(alt_names, i);
862 
863             if (name->type != GEN_DNS) {
864                 continue;
865             }
866 
867             str.length = ASN1_STRING_length(name->d.dNSName);
868 #if OPENSSL_VERSION_NUMBER > 0x10100000L
869             str.start = (u_char *) ASN1_STRING_get0_data(name->d.dNSName);
870 #else
871             str.start = ASN1_STRING_data(name->d.dNSName);
872 #endif
873 
874             domain.start = nxt_mp_nget(mp, str.length);
875             if (nxt_slow_path(domain.start == NULL)) {
876                 goto fail;
877             }
878 
879             domain.length = str.length;
880             nxt_memcpy_lowcase(domain.start, str.start, str.length);
881 
882             item = nxt_mp_get(mp, sizeof(nxt_tls_bundle_hash_item_t));
883             if (nxt_slow_path(item == NULL)) {
884                 goto fail;
885             }
886 
887             item->name = domain;
888             item->bundle = bundle;
889 
890             if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash,
891                                                item, mp)
892                 == NXT_ERROR)
893             {
894                 goto fail;
895             }
896         }
897 
898         sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
899 
900     } else {
901         x509_name = X509_get_subject_name(cert);
902         len = X509_NAME_get_text_by_NID(x509_name, NID_commonName,
903                                         NULL, 0);
904         if (len <= 0) {
905             nxt_log(task, NXT_LOG_WARN, "certificate \"%V\" has neither "
906                     "Subject Alternative Name nor Common Name", &bundle->name);
907             return NXT_OK;
908         }
909 
910         domain.start = nxt_mp_nget(mp, len + 1);
911         if (nxt_slow_path(domain.start == NULL)) {
912             return NXT_ERROR;
913         }
914 
915         domain.length = X509_NAME_get_text_by_NID(x509_name, NID_commonName,
916                                                   (char *) domain.start,
917                                                   len + 1);
918         nxt_memcpy_lowcase(domain.start, domain.start, domain.length);
919 
920         item = nxt_mp_get(mp, sizeof(nxt_tls_bundle_hash_item_t));
921         if (nxt_slow_path(item == NULL)) {
922             return NXT_ERROR;
923         }
924 
925         item->name = domain;
926         item->bundle = bundle;
927 
928         if (nxt_openssl_bundle_hash_insert(task, &conf->bundle_hash, item,
929                                            mp)
930             == NXT_ERROR)
931         {
932             return NXT_ERROR;
933         }
934     }
935 
936     return NXT_OK;
937 
938 fail:
939 
940     sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free);
941 
942     return NXT_ERROR;
943 }
944 
945 
946 static const nxt_lvlhsh_proto_t  nxt_openssl_bundle_hash_proto
947     nxt_aligned(64) =
948 {
949     NXT_LVLHSH_DEFAULT,
950     nxt_openssl_bundle_hash_test,
951     nxt_mp_lvlhsh_alloc,
952     nxt_mp_lvlhsh_free,
953 };
954 
955 
956 static nxt_int_t
957 nxt_openssl_bundle_hash_test(nxt_lvlhsh_query_t *lhq, void *data)
958 {
959     nxt_tls_bundle_hash_item_t  *item;
960 
961     item = data;
962 
963     return nxt_strstr_eq(&lhq->key, &item->name) ? NXT_OK : NXT_DECLINED;
964 }
965 
966 
967 static nxt_int_t
968 nxt_openssl_bundle_hash_insert(nxt_task_t *task, nxt_lvlhsh_t *lvlhsh,
969     nxt_tls_bundle_hash_item_t *item, nxt_mp_t *mp)
970 {
971     nxt_str_t                   str;
972     nxt_int_t                   ret;
973     nxt_lvlhsh_query_t          lhq;
974     nxt_tls_bundle_hash_item_t  *old;
975 
976     str = item->name;
977 
978     if (item->name.start[0] == '*') {
979         item->name.start++;
980         item->name.length--;
981 
982         if (item->name.length == 0 || item->name.start[0] != '.') {
983             nxt_log(task, NXT_LOG_WARN, "ignored invalid name \"%V\" "
984                     "in certificate \"%V\": missing \".\" "
985                     "after wildcard symbol", &str, &item->bundle->name);
986             return NXT_OK;
987         }
988     }
989 
990     lhq.pool = mp;
991     lhq.key = item->name;
992     lhq.value = item;
993     lhq.proto = &nxt_openssl_bundle_hash_proto;
994     lhq.replace = 0;
995     lhq.key_hash = nxt_murmur_hash2(item->name.start, item->name.length);
996 
997     ret = nxt_lvlhsh_insert(lvlhsh, &lhq);
998     if (nxt_fast_path(ret == NXT_OK)) {
999         nxt_debug(task, "name \"%V\" for certificate \"%V\" is inserted",
1000                   &str, &item->bundle->name);
1001         return NXT_OK;
1002     }
1003 
1004     if (nxt_fast_path(ret == NXT_DECLINED)) {
1005         old = lhq.value;
1006         if (old->bundle != item->bundle) {
1007             nxt_log(task, NXT_LOG_WARN, "ignored duplicate name \"%V\" "
1008                     "in certificate \"%V\", identical name appears in \"%V\"",
1009                     &str, &old->bundle->name, &item->bundle->name);
1010 
1011             old->bundle = item->bundle;
1012         }
1013 
1014         return NXT_OK;
1015     }
1016 
1017     return NXT_ERROR;
1018 }
1019 
1020 
1021 static nxt_int_t
1022 nxt_openssl_servername(SSL *s, int *ad, void *arg)
1023 {
1024     nxt_str_t              str;
1025     nxt_uint_t             i;
1026     nxt_conn_t             *c;
1027     const char             *servername;
1028     nxt_tls_conf_t         *conf;
1029     nxt_openssl_conn_t     *tls;
1030     nxt_tls_bundle_conf_t  *bundle;
1031 
1032     c = SSL_get_ex_data(s, nxt_openssl_connection_index);
1033 
1034     if (nxt_slow_path(c == NULL)) {
1035         nxt_thread_log_alert("SSL_get_ex_data() failed");
1036         return SSL_TLSEXT_ERR_ALERT_FATAL;
1037     }
1038 
1039     servername = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
1040 
1041     if (servername == NULL) {
1042         nxt_debug(c->socket.task, "SSL_get_servername(): NULL");
1043         goto done;
1044     }
1045 
1046     str.length = nxt_strlen(servername);
1047     if (str.length == 0) {
1048         nxt_debug(c->socket.task, "SSL_get_servername(): \"\" is empty");
1049         goto done;
1050     }
1051 
1052     if (servername[0] == '.') {
1053         nxt_debug(c->socket.task, "ignored the server name \"%s\": "
1054                                   "leading \".\"", servername);
1055         goto done;
1056     }
1057 
1058     nxt_debug(c->socket.task, "tls with servername \"%s\"", servername);
1059 
1060     str.start = nxt_mp_nget(c->mem_pool, str.length);
1061     if (nxt_slow_path(str.start == NULL)) {
1062         return SSL_TLSEXT_ERR_ALERT_FATAL;
1063     }
1064 
1065     nxt_memcpy_lowcase(str.start, (const u_char *) servername, str.length);
1066 
1067     tls = c->u.tls;
1068     conf = tls->conf;
1069 
1070     bundle = nxt_openssl_find_ctx(conf, &str);
1071 
1072     if (bundle == NULL) {
1073         for (i = 1; i < str.length; i++) {
1074             if (str.start[i] == '.') {
1075                 str.start += i;
1076                 str.length -= i;
1077 
1078                 bundle = nxt_openssl_find_ctx(conf, &str);
1079                 break;
1080             }
1081         }
1082     }
1083 
1084     if (bundle != NULL) {
1085         nxt_debug(c->socket.task, "new tls context found for \"%V\": \"%V\" "
1086                                   "(old: \"%V\")", &str, &bundle->name,
1087                                   &conf->bundle->name);
1088 
1089         if (bundle != conf->bundle) {
1090             if (SSL_set_SSL_CTX(s, bundle->ctx) == NULL) {
1091                 nxt_openssl_log_error(c->socket.task, NXT_LOG_ALERT,
1092                                       "SSL_set_SSL_CTX() failed");
1093 
1094                 return SSL_TLSEXT_ERR_ALERT_FATAL;
1095             }
1096         }
1097     }
1098 
1099 done:
1100 
1101     return SSL_TLSEXT_ERR_OK;
1102 }
1103 
1104 
1105 static nxt_tls_bundle_conf_t *
1106 nxt_openssl_find_ctx(nxt_tls_conf_t *conf, nxt_str_t *sn)
1107 {
1108     nxt_int_t                   ret;
1109     nxt_lvlhsh_query_t          lhq;
1110     nxt_tls_bundle_hash_item_t  *item;
1111 
1112     lhq.key_hash = nxt_murmur_hash2(sn->start, sn->length);
1113     lhq.key = *sn;
1114     lhq.proto = &nxt_openssl_bundle_hash_proto;
1115 
1116     ret = nxt_lvlhsh_find(&conf->bundle_hash, &lhq);
1117     if (ret != NXT_OK) {
1118         return NULL;
1119     }
1120 
1121     item = lhq.value;
1122 
1123     return item->bundle;
1124 }
1125 
1126 
1127 static void
1128 nxt_openssl_server_free(nxt_task_t *task, nxt_tls_conf_t *conf)
1129 {
1130     nxt_tls_bundle_conf_t  *bundle;
1131 
1132     bundle = conf->bundle;
1133     nxt_assert(bundle != NULL);
1134 
1135     do {
1136         SSL_CTX_free(bundle->ctx);
1137         bundle = bundle->next;
1138     } while (bundle != NULL);
1139 
1140     if (conf->tickets) {
1141         nxt_memzero(conf->tickets->tickets,
1142                     conf->tickets->count * sizeof(nxt_tls_ticket_t));
1143     }
1144 
1145 #if (OPENSSL_VERSION_NUMBER >= 0x1010100fL \
1146      && OPENSSL_VERSION_NUMBER < 0x1010101fL)
1147     RAND_keep_random_devices_open(0);
1148 #endif
1149 }
1150 
1151 
1152 static void
1153 nxt_openssl_conn_init(nxt_task_t *task, nxt_tls_conf_t *conf, nxt_conn_t *c)
1154 {
1155     int                 ret;
1156     SSL                 *s;
1157     SSL_CTX             *ctx;
1158     nxt_openssl_conn_t  *tls;
1159 
1160     nxt_log_debug(c->socket.log, "openssl conn init");
1161 
1162     tls = nxt_mp_zget(c->mem_pool, sizeof(nxt_openssl_conn_t));
1163     if (tls == NULL) {
1164         goto fail;
1165     }
1166 
1167     c->u.tls = tls;
1168     nxt_buf_mem_set_size(&tls->buffer, conf->buffer_size);
1169 
1170     ctx = conf->bundle->ctx;
1171 
1172     s = SSL_new(ctx);
1173     if (s == NULL) {
1174         nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_new() failed");
1175         goto fail;
1176     }
1177 
1178     tls->session = s;
1179     /* To pass TLS config to the nxt_openssl_servername() callback. */
1180     tls->conf = conf;
1181     tls->conn = c;
1182 
1183     ret = SSL_set_fd(s, c->socket.fd);
1184 
1185     if (ret == 0) {
1186         nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_set_fd(%d) failed",
1187                               c->socket.fd);
1188         goto fail;
1189     }
1190 
1191     SSL_set_accept_state(s);
1192 
1193     if (SSL_set_ex_data(s, nxt_openssl_connection_index, c) == 0) {
1194         nxt_openssl_log_error(task, NXT_LOG_ALERT, "SSL_set_ex_data() failed");
1195         goto fail;
1196     }
1197 
1198     c->io = &nxt_openssl_conn_io;
1199     c->sendfile = NXT_CONN_SENDFILE_OFF;
1200 
1201     nxt_openssl_conn_handshake(task, c, c->socket.data);
1202 
1203     return;
1204 
1205 fail:
1206 
1207     nxt_work_queue_add(c->read_work_queue, c->read_state->error_handler,
1208                        task, c, c->socket.data);
1209 }
1210 
1211 
1212 nxt_inline void
1213 nxt_openssl_conn_free(nxt_task_t *task, nxt_conn_t *c)
1214 {
1215     nxt_openssl_conn_t  *tls;
1216 
1217     nxt_debug(task, "openssl conn free");
1218 
1219     tls = c->u.tls;
1220 
1221     if (tls != NULL) {
1222         c->u.tls = NULL;
1223         nxt_free(tls->buffer.start);
1224         SSL_free(tls->session);
1225     }
1226 }
1227 
1228 
1229 static void
1230 nxt_openssl_conn_handshake(nxt_task_t *task, void *obj, void *data)
1231 {
1232     int                     ret;
1233     nxt_int_t               n;
1234     nxt_err_t               err;
1235     nxt_conn_t              *c;
1236     nxt_work_queue_t        *wq;
1237     nxt_work_handler_t      handler;
1238     nxt_openssl_conn_t      *tls;
1239     const nxt_conn_state_t  *state;
1240 
1241     c = obj;
1242 
1243     nxt_debug(task, "openssl conn handshake fd:%d", c->socket.fd);
1244 
1245     if (c->socket.error != 0) {
1246         return;
1247     }
1248 
1249     tls = c->u.tls;
1250 
1251     if (tls == NULL) {
1252         return;
1253     }
1254 
1255     nxt_debug(task, "openssl conn handshake: %d times", tls->times);
1256 
1257     /* "tls->times == 1" is suitable to run SSL_do_handshake() in job. */
1258 
1259     ret = SSL_do_handshake(tls->session);
1260 
1261     err = (ret <= 0) ? nxt_socket_errno : 0;
1262 
1263     nxt_thread_time_debug_update(task->thread);
1264 
1265     nxt_debug(task, "SSL_do_handshake(%d): %d err:%d", c->socket.fd, ret, err);
1266 
1267     state = (c->read_state != NULL) ? c->read_state : c->write_state;
1268 
1269     if (ret > 0) {
1270         /* ret == 1, the handshake was successfully completed. */
1271         tls->handshake = 1;
1272 
1273         if (c->read_state != NULL) {
1274             if (state->io_read_handler != NULL || c->read != NULL) {
1275                 nxt_conn_read(task->thread->engine, c);
1276                 return;
1277             }
1278 
1279         } else {
1280             if (c->write != NULL) {
1281                 nxt_conn_write(task->thread->engine, c);
1282                 return;
1283             }
1284         }
1285 
1286         handler = state->ready_handler;
1287 
1288     } else {
1289         c->socket.read_handler = nxt_openssl_conn_handshake;
1290         c->socket.write_handler = nxt_openssl_conn_handshake;
1291 
1292         n = nxt_openssl_conn_test_error(task, c, ret, err,
1293                                         NXT_OPENSSL_HANDSHAKE);
1294         switch (n) {
1295 
1296         case NXT_AGAIN:
1297             if (tls->ssl_error == SSL_ERROR_WANT_READ && tls->times < 2) {
1298                 tls->times++;
1299             }
1300 
1301             return;
1302 
1303         case 0:
1304             handler = state->close_handler;
1305             break;
1306 
1307         default:
1308         case NXT_ERROR:
1309             nxt_openssl_conn_error(task, err, "SSL_do_handshake(%d) failed",
1310                                    c->socket.fd);
1311 
1312             handler = state->error_handler;
1313             break;
1314         }
1315     }
1316 
1317     wq = (c->read_state != NULL) ? c->read_work_queue : c->write_work_queue;
1318 
1319     nxt_work_queue_add(wq, handler, task, c, data);
1320 }
1321 
1322 
1323 static ssize_t
1324 nxt_openssl_conn_io_recvbuf(nxt_conn_t *c, nxt_buf_t *b)
1325 {
1326     int                 ret;
1327     size_t              size;
1328     nxt_int_t           n;
1329     nxt_err_t           err;
1330     nxt_openssl_conn_t  *tls;
1331 
1332     tls = c->u.tls;
1333     size = b->mem.end - b->mem.free;
1334 
1335     ret = SSL_read(tls->session, b->mem.free, size);
1336 
1337     err = (ret <= 0) ? nxt_socket_errno : 0;
1338 
1339     nxt_debug(c->socket.task, "SSL_read(%d, %p, %uz): %d err:%d",
1340               c->socket.fd, b->mem.free, size, ret, err);
1341 
1342     if (ret > 0) {
1343         return ret;
1344     }
1345 
1346     n = nxt_openssl_conn_test_error(c->socket.task, c, ret, err,
1347                                     NXT_OPENSSL_READ);
1348     if (n == NXT_ERROR) {
1349         nxt_openssl_conn_error(c->socket.task, err,
1350                                "SSL_read(%d, %p, %uz) failed",
1351                                c->socket.fd, b->mem.free, size);
1352     }
1353 
1354     return n;
1355 }
1356 
1357 
1358 static ssize_t
1359 nxt_openssl_conn_io_sendbuf(nxt_task_t *task, nxt_sendbuf_t *sb)
1360 {
1361     nxt_uint_t    niov;
1362     struct iovec  iov;
1363 
1364     niov = nxt_sendbuf_mem_coalesce0(task, sb, &iov, 1);
1365 
1366     if (niov == 0 && sb->sync) {
1367         return 0;
1368     }
1369 
1370     return nxt_openssl_conn_io_send(task, sb, iov.iov_base, iov.iov_len);
1371 }
1372 
1373 
1374 static ssize_t
1375 nxt_openssl_conn_io_send(nxt_task_t *task, nxt_sendbuf_t *sb, void *buf,
1376     size_t size)
1377 {
1378     int                 ret;
1379     nxt_err_t           err;
1380     nxt_int_t           n;
1381     nxt_conn_t          *c;
1382     nxt_openssl_conn_t  *tls;
1383 
1384     tls = sb->tls;
1385 
1386     ret = SSL_write(tls->session, buf, size);
1387 
1388     err = (ret <= 0) ? nxt_socket_errno : 0;
1389 
1390     nxt_debug(task, "SSL_write(%d, %p, %uz): %d err:%d",
1391               sb->socket, buf, size, ret, err);
1392 
1393     if (ret > 0) {
1394         return ret;
1395     }
1396 
1397     c = tls->conn;
1398     c->socket.write_ready = sb->ready;
1399 
1400     n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_WRITE);
1401 
1402     sb->ready = c->socket.write_ready;
1403 
1404     if (n == NXT_ERROR) {
1405         sb->error = c->socket.error;
1406         nxt_openssl_conn_error(task, err, "SSL_write(%d, %p, %uz) failed",
1407                                sb->socket, buf, size);
1408     }
1409 
1410     return n;
1411 }
1412 
1413 
1414 static void
1415 nxt_openssl_conn_io_shutdown(nxt_task_t *task, void *obj, void *data)
1416 {
1417     int                 ret, mode;
1418     SSL                 *s;
1419     nxt_err_t           err;
1420     nxt_int_t           n;
1421     nxt_bool_t          quiet, once;
1422     nxt_conn_t          *c;
1423     nxt_openssl_conn_t  *tls;
1424     nxt_work_handler_t  handler;
1425 
1426     c = obj;
1427 
1428     nxt_debug(task, "openssl conn shutdown fd:%d", c->socket.fd);
1429 
1430     c->read_state = NULL;
1431     tls = c->u.tls;
1432 
1433     if (tls == NULL) {
1434         return;
1435     }
1436 
1437     s = tls->session;
1438 
1439     if (s == NULL || !tls->handshake) {
1440         handler = c->write_state->ready_handler;
1441         goto done;
1442     }
1443 
1444     mode = SSL_get_shutdown(s);
1445 
1446     if (c->socket.timedout || c->socket.error != 0) {
1447         quiet = 1;
1448 
1449     } else if (c->socket.closed && !(mode & SSL_RECEIVED_SHUTDOWN)) {
1450         quiet = 1;
1451 
1452     } else {
1453         quiet = 0;
1454     }
1455 
1456     SSL_set_quiet_shutdown(s, quiet);
1457 
1458     if (tls->conf->no_wait_shutdown) {
1459         mode |= SSL_RECEIVED_SHUTDOWN;
1460     }
1461 
1462     once = 1;
1463 
1464     for ( ;; ) {
1465         SSL_set_shutdown(s, mode);
1466 
1467         ret = SSL_shutdown(s);
1468 
1469         err = (ret <= 0) ? nxt_socket_errno : 0;
1470 
1471         nxt_debug(task, "SSL_shutdown(%d, %d, %b): %d err:%d",
1472                   c->socket.fd, mode, quiet, ret, err);
1473 
1474         if (ret > 0) {
1475             /* ret == 1, the shutdown was successfully completed. */
1476             handler = c->write_state->ready_handler;
1477             goto done;
1478         }
1479 
1480         if (ret == 0) {
1481             /*
1482              * If SSL_shutdown() returns 0 then it should be called
1483              * again.  The second SSL_shutdown() call should return
1484              * -1/SSL_ERROR_WANT_READ or -1/SSL_ERROR_WANT_WRITE.
1485              * OpenSSL prior to 0.9.8m version however never returns
1486              * -1 at all.  Fortunately, OpenSSL preserves internally
1487              * correct status available via SSL_get_error(-1).
1488              */
1489             if (once) {
1490                 once = 0;
1491                 mode = SSL_get_shutdown(s);
1492                 continue;
1493             }
1494 
1495             ret = -1;
1496         }
1497 
1498         /* ret == -1 */
1499 
1500         break;
1501     }
1502 
1503     c->socket.read_handler = nxt_openssl_conn_io_shutdown;
1504     c->socket.write_handler = nxt_openssl_conn_io_shutdown;
1505     c->socket.error_handler = c->write_state->error_handler;
1506 
1507     n = nxt_openssl_conn_test_error(task, c, ret, err, NXT_OPENSSL_SHUTDOWN);
1508 
1509     switch (n) {
1510 
1511     case 0:
1512         handler = c->write_state->close_handler;
1513         break;
1514 
1515     case NXT_AGAIN:
1516         c->write_timer.handler = nxt_openssl_conn_io_shutdown_timeout;
1517         nxt_timer_add(task->thread->engine, &c->write_timer, 5000);
1518         return;
1519 
1520     default:
1521     case NXT_ERROR:
1522         nxt_openssl_conn_error(task, err, "SSL_shutdown(%d) failed",
1523                                c->socket.fd);
1524         handler = c->write_state->error_handler;
1525     }
1526 
1527 done:
1528 
1529     nxt_openssl_conn_free(task, c);
1530 
1531     nxt_work_queue_add(c->write_work_queue, handler, task, c, data);
1532 }
1533 
1534 
1535 static nxt_int_t
1536 nxt_openssl_conn_test_error(nxt_task_t *task, nxt_conn_t *c, int ret,
1537     nxt_err_t sys_err, nxt_openssl_io_t io)
1538 {
1539     u_long              lib_err;
1540     nxt_openssl_conn_t  *tls;
1541 
1542     tls = c->u.tls;
1543 
1544     tls->ssl_error = SSL_get_error(tls->session, ret);
1545 
1546     nxt_debug(task, "SSL_get_error(): %d", tls->ssl_error);
1547 
1548     switch (tls->ssl_error) {
1549 
1550     case SSL_ERROR_WANT_READ:
1551         c->socket.read_ready = 0;
1552 
1553         if (io != NXT_OPENSSL_READ) {
1554             nxt_fd_event_block_write(task->thread->engine, &c->socket);
1555 
1556             if (nxt_fd_event_is_disabled(c->socket.read)) {
1557                 nxt_fd_event_enable_read(task->thread->engine, &c->socket);
1558             }
1559         }
1560 
1561         return NXT_AGAIN;
1562 
1563     case SSL_ERROR_WANT_WRITE:
1564         c->socket.write_ready = 0;
1565 
1566         if (io != NXT_OPENSSL_WRITE) {
1567             nxt_fd_event_block_read(task->thread->engine, &c->socket);
1568 
1569             if (nxt_fd_event_is_disabled(c->socket.write)) {
1570                 nxt_fd_event_enable_write(task->thread->engine, &c->socket);
1571             }
1572         }
1573 
1574         return NXT_AGAIN;
1575 
1576     case SSL_ERROR_SYSCALL:
1577         lib_err = ERR_peek_error();
1578 
1579         nxt_debug(task, "ERR_peek_error(): %l", lib_err);
1580 
1581         if (sys_err != 0 || lib_err != 0) {
1582             c->socket.error = sys_err;
1583             return NXT_ERROR;
1584         }
1585 
1586         /* A connection was just closed. */
1587         c->socket.closed = 1;
1588         return 0;
1589 
1590     case SSL_ERROR_ZERO_RETURN:
1591         /* A "close notify" alert. */
1592         return 0;
1593 
1594     default: /* SSL_ERROR_SSL, etc. */
1595         c->socket.error = 1000;  /* Nonexistent errno code. */
1596         return NXT_ERROR;
1597     }
1598 }
1599 
1600 
1601 static void
1602 nxt_openssl_conn_io_shutdown_timeout(nxt_task_t *task, void *obj, void *data)
1603 {
1604     nxt_conn_t   *c;
1605     nxt_timer_t  *timer;
1606 
1607     timer = obj;
1608 
1609     nxt_debug(task, "openssl conn shutdown timeout");
1610 
1611     c = nxt_write_timer_conn(timer);
1612 
1613     c->socket.timedout = 1;
1614     nxt_openssl_conn_io_shutdown(task, c, NULL);
1615 }
1616 
1617 
1618 static void nxt_cdecl
1619 nxt_openssl_conn_error(nxt_task_t *task, nxt_err_t err, const char *fmt, ...)
1620 {
1621     u_char      *p, *end;
1622     va_list     args;
1623     nxt_uint_t  level;
1624     u_char      msg[NXT_MAX_ERROR_STR];
1625 
1626     level = nxt_openssl_log_error_level(err);
1627 
1628     if (nxt_log_level_enough(task->log, level)) {
1629 
1630         end = msg + sizeof(msg);
1631 
1632         va_start(args, fmt);
1633         p = nxt_vsprintf(msg, end, fmt, args);
1634         va_end(args);
1635 
1636         if (err != 0) {
1637             p = nxt_sprintf(p, end, " %E", err);
1638         }
1639 
1640         p = nxt_openssl_copy_error(p, end);
1641 
1642         nxt_log(task, level, "%*s", p - msg, msg);
1643 
1644     } else {
1645         ERR_clear_error();
1646     }
1647 }
1648 
1649 
1650 static nxt_uint_t
1651 nxt_openssl_log_error_level(nxt_err_t err)
1652 {
1653     switch (ERR_GET_REASON(ERR_peek_error())) {
1654 
1655     case 0:
1656         return nxt_socket_error_level(err);
1657 
1658     case SSL_R_BAD_CHANGE_CIPHER_SPEC:                    /*  103 */
1659     case SSL_R_BLOCK_CIPHER_PAD_IS_WRONG:                 /*  129 */
1660     case SSL_R_DIGEST_CHECK_FAILED:                       /*  149 */
1661     case SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST:             /*  151 */
1662     case SSL_R_EXCESSIVE_MESSAGE_SIZE:                    /*  152 */
1663     case SSL_R_LENGTH_MISMATCH:                           /*  159 */
1664 #ifdef SSL_R_NO_CIPHERS_PASSED
1665     case SSL_R_NO_CIPHERS_PASSED:                         /*  182 */
1666 #endif
1667     case SSL_R_NO_CIPHERS_SPECIFIED:                      /*  183 */
1668     case SSL_R_NO_COMPRESSION_SPECIFIED:                  /*  187 */
1669     case SSL_R_NO_SHARED_CIPHER:                          /*  193 */
1670     case SSL_R_RECORD_LENGTH_MISMATCH:                    /*  213 */
1671 #ifdef SSL_R_PARSE_TLSEXT
1672     case SSL_R_PARSE_TLSEXT:                              /*  227 */
1673 #endif
1674     case SSL_R_UNEXPECTED_MESSAGE:                        /*  244 */
1675     case SSL_R_UNEXPECTED_RECORD:                         /*  245 */
1676     case SSL_R_UNKNOWN_ALERT_TYPE:                        /*  246 */
1677     case SSL_R_UNKNOWN_PROTOCOL:                          /*  252 */
1678     case SSL_R_WRONG_VERSION_NUMBER:                      /*  267 */
1679     case SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC:       /*  281 */
1680 #ifdef SSL_R_RENEGOTIATE_EXT_TOO_LONG
1681     case SSL_R_RENEGOTIATE_EXT_TOO_LONG:                  /*  335 */
1682     case SSL_R_RENEGOTIATION_ENCODING_ERR:                /*  336 */
1683     case SSL_R_RENEGOTIATION_MISMATCH:                    /*  337 */
1684 #endif
1685 #ifdef SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
1686     case SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED:      /*  338 */
1687 #endif
1688 #ifdef SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING
1689     case SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING:          /*  345 */
1690 #endif
1691     case 1000:/* SSL_R_SSLV3_ALERT_CLOSE_NOTIFY */
1692     case SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE:            /* 1010 */
1693     case SSL_R_SSLV3_ALERT_BAD_RECORD_MAC:                /* 1020 */
1694     case SSL_R_TLSV1_ALERT_DECRYPTION_FAILED:             /* 1021 */
1695     case SSL_R_TLSV1_ALERT_RECORD_OVERFLOW:               /* 1022 */
1696     case SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE:         /* 1030 */
1697     case SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE:             /* 1040 */
1698     case SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER:             /* 1047 */
1699         break;
1700 
1701     case SSL_R_SSLV3_ALERT_NO_CERTIFICATE:                /* 1041 */
1702     case SSL_R_SSLV3_ALERT_BAD_CERTIFICATE:               /* 1042 */
1703     case SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE:       /* 1043 */
1704     case SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED:           /* 1044 */
1705     case SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED:           /* 1045 */
1706     case SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN:           /* 1046 */
1707     case SSL_R_TLSV1_ALERT_UNKNOWN_CA:                    /* 1048 */
1708     case SSL_R_TLSV1_ALERT_ACCESS_DENIED:                 /* 1049 */
1709     case SSL_R_TLSV1_ALERT_DECODE_ERROR:                  /* 1050 */
1710     case SSL_R_TLSV1_ALERT_DECRYPT_ERROR:                 /* 1051 */
1711     case SSL_R_TLSV1_ALERT_EXPORT_RESTRICTION:            /* 1060 */
1712     case SSL_R_TLSV1_ALERT_PROTOCOL_VERSION:              /* 1070 */
1713     case SSL_R_TLSV1_ALERT_INSUFFICIENT_SECURITY:         /* 1071 */
1714     case SSL_R_TLSV1_ALERT_INTERNAL_ERROR:                /* 1080 */
1715     case SSL_R_TLSV1_ALERT_USER_CANCELLED:                /* 1090 */
1716     case SSL_R_TLSV1_ALERT_NO_RENEGOTIATION:              /* 1100 */
1717         return NXT_LOG_ERR;
1718 
1719     default:
1720         return NXT_LOG_ALERT;
1721     }
1722 
1723     return NXT_LOG_INFO;
1724 }
1725 
1726 
1727 void nxt_cdecl
1728 nxt_openssl_log_error(nxt_task_t *task, nxt_uint_t level, const char *fmt, ...)
1729 {
1730     u_char   *p, *end;
1731     va_list  args;
1732     u_char   msg[NXT_MAX_ERROR_STR];
1733 
1734     end = msg + sizeof(msg);
1735 
1736     va_start(args, fmt);
1737     p = nxt_vsprintf(msg, end, fmt, args);
1738     va_end(args);
1739 
1740     p = nxt_openssl_copy_error(p, end);
1741 
1742     nxt_log(task, level, "%*s", p - msg, msg);
1743 }
1744 
1745 
1746 u_char *
1747 nxt_openssl_copy_error(u_char *p, u_char *end)
1748 {
1749     int         flags;
1750     u_long      err;
1751     nxt_bool_t  clear;
1752     const char  *data, *delimiter;
1753 
1754     err = ERR_peek_error();
1755     if (err == 0) {
1756         return p;
1757     }
1758 
1759     /* Log the most relevant error message ... */
1760     data = ERR_reason_error_string(err);
1761 
1762     p = nxt_sprintf(p, end, " (%d: %s) (OpenSSL: ", ERR_GET_REASON(err), data);
1763 
1764     /*
1765      * ... followed by all queued cumbersome OpenSSL error messages
1766      * and drain the error queue.
1767      */
1768     delimiter = "";
1769     clear = 0;
1770 
1771     for ( ;; ) {
1772         err = ERR_get_error_line_data(NULL, NULL, &data, &flags);
1773         if (err == 0) {
1774             break;
1775         }
1776 
1777         p = nxt_sprintf(p, end, "%s", delimiter);
1778 
1779         ERR_error_string_n(err, (char *) p, end - p);
1780 
1781         while (p < end && *p != '\0') {
1782             p++;
1783         }
1784 
1785         if ((flags & ERR_TXT_STRING) != 0) {
1786             p = nxt_sprintf(p, end, ":%s", data);
1787         }
1788 
1789         clear |= ((flags & ERR_TXT_MALLOCED) != 0);
1790 
1791         delimiter = "; ";
1792     }
1793 
1794     /* Deallocate additional data. */
1795 
1796     if (clear) {
1797         ERR_clear_error();
1798     }
1799 
1800     if (p < end) {
1801         *p++ = ')';
1802     }
1803 
1804     return p;
1805 }
1806