@@ -483,6 +483,22 @@ RSA *load_rsa_privatekey(SSL_CTX *ctx, const char *file) {
483483}
484484
485485#ifndef OPENSSL_NO_TLSEXT
486+
487+ int is_servername_match (const char * servername , char * certname ) {
488+ if (strcasecmp (servername , certname ) == 0 ) {
489+ return 1 ;
490+ } else {
491+ if (strlen (certname ) > 2 && strstr (certname , "*." ) == certname ) {
492+ char * dot = strstr (servername , "." );
493+ char * after_subdomain = strcasestr (servername , & certname [1 ]);
494+ if (dot && dot == after_subdomain && strlen (after_subdomain ) == strlen (& certname [1 ])) {
495+ return 1 ;
496+ }
497+ }
498+ }
499+ return 0 ;
500+ }
501+
486502/*
487503 * Switch the context of the current SSL object to the most appropriate one
488504 * based on the SNI header
@@ -499,7 +515,7 @@ int sni_switch_ctx(SSL *ssl, int *al, void *data) {
499515 // For now, just compare servernames as case insensitive strings. Someday,
500516 // it might be nice to Do The Right Thing around star certs.
501517 for (cl = sni_ctxs ; cl != NULL ; cl = cl -> next ) {
502- if (strcasecmp (servername , cl -> servername ) == 0 ) {
518+ if (is_servername_match (servername , cl -> servername )) {
503519 SSL_set_SSL_CTX (ssl , cl -> ctx );
504520 return SSL_TLSEXT_ERR_NOACK ;
505521 }
@@ -817,6 +833,13 @@ static void shutdown_proxy(proxystate *ps, SHUTDOWN_REQUESTOR req) {
817833 close (ps -> fd_up );
818834 close (ps -> fd_down );
819835
836+ // Clear the SSL error queue - it might contain details
837+ // of errors that we haven't consumed for whatever reason.
838+ // If we don't, future calls to SSL_get_error will lead to
839+ // weird/confusing results that can throw off the handling
840+ // of normal conditions like SSL_ERROR_WANT_READ.
841+ ERR_clear_error ();
842+
820843 SSL_set_shutdown (ps -> ssl , SSL_SENT_SHUTDOWN );
821844 SSL_free (ps -> ssl );
822845
@@ -1136,7 +1159,14 @@ static void client_handshake(struct ev_loop *loop, ev_io *w, int revents) {
11361159 shutdown_proxy (ps , SHUTDOWN_SSL );
11371160 }
11381161 else {
1139- LOG ("{%s} Unexpected SSL error (in handshake): %d\n" , w -> fd == ps -> fd_up ? "client" : "backend" , err );
1162+ // Try and get more detail on the error from the SSL
1163+ // error queue. ERR_error_string requires a char buffer
1164+ // of 120 bytes.
1165+ unsigned long err_detail = ERR_get_error ();
1166+ char err_msg [120 ];
1167+ ERR_error_string (err_detail , err_msg );
1168+
1169+ LOG ("{%s} Unexpected SSL error (in handshake): %d, %s\n" , w -> fd == ps -> fd_up ? "client" : "backend" , err , err_msg );
11401170 shutdown_proxy (ps , SHUTDOWN_SSL );
11411171 }
11421172 }
0 commit comments