@@ -483,6 +483,22 @@ RSA *load_rsa_privatekey(SSL_CTX *ctx, const char *file) {
483
483
}
484
484
485
485
#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
+
486
502
/*
487
503
* Switch the context of the current SSL object to the most appropriate one
488
504
* based on the SNI header
@@ -499,7 +515,7 @@ int sni_switch_ctx(SSL *ssl, int *al, void *data) {
499
515
// For now, just compare servernames as case insensitive strings. Someday,
500
516
// it might be nice to Do The Right Thing around star certs.
501
517
for (cl = sni_ctxs ; cl != NULL ; cl = cl -> next ) {
502
- if (strcasecmp (servername , cl -> servername ) == 0 ) {
518
+ if (is_servername_match (servername , cl -> servername )) {
503
519
SSL_set_SSL_CTX (ssl , cl -> ctx );
504
520
return SSL_TLSEXT_ERR_NOACK ;
505
521
}
@@ -817,6 +833,13 @@ static void shutdown_proxy(proxystate *ps, SHUTDOWN_REQUESTOR req) {
817
833
close (ps -> fd_up );
818
834
close (ps -> fd_down );
819
835
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
+
820
843
SSL_set_shutdown (ps -> ssl , SSL_SENT_SHUTDOWN );
821
844
SSL_free (ps -> ssl );
822
845
@@ -1136,7 +1159,14 @@ static void client_handshake(struct ev_loop *loop, ev_io *w, int revents) {
1136
1159
shutdown_proxy (ps , SHUTDOWN_SSL );
1137
1160
}
1138
1161
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 );
1140
1170
shutdown_proxy (ps , SHUTDOWN_SSL );
1141
1171
}
1142
1172
}
0 commit comments