@@ -557,12 +557,6 @@ mongoc_stream_tls_openssl_handshake (mongoc_stream_t *stream,
557
557
558
558
BIO_get_ssl (openssl -> bio , & ssl );
559
559
560
- /* Added in OpenSSL 0.9.8f, as a build time option */
561
- #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
562
- /* Set the SNI hostname we are expecting certificate for */
563
- SSL_set_tlsext_host_name (ssl , host );
564
- #endif
565
-
566
560
if (BIO_do_handshake (openssl -> bio ) == 1 ) {
567
561
if (_mongoc_openssl_check_cert (
568
562
ssl , host , tls -> ssl_opts .allow_invalid_hostname )) {
@@ -602,6 +596,25 @@ mongoc_stream_tls_openssl_handshake (mongoc_stream_t *stream,
602
596
RETURN (false);
603
597
}
604
598
599
+ /* Callback to get the client provided SNI, if any
600
+ * It is only called in SSL "server mode" (e.g. when using the Mock Server),
601
+ * and we don't actually use the hostname for anything, just debug print it
602
+ */
603
+ static int
604
+ _mongoc_stream_tls_openssl_sni (SSL * ssl , int * ad , void * arg )
605
+ {
606
+ const char * hostname ;
607
+
608
+ if (ssl == NULL ) {
609
+ MONGOC_DEBUG ("No SNI hostname provided" );
610
+ return SSL_TLSEXT_ERR_NOACK ;
611
+ }
612
+
613
+ hostname = SSL_get_servername (ssl , TLSEXT_NAMETYPE_host_name );
614
+ MONGOC_DEBUG ("Got SNI: '%s'" , hostname );
615
+
616
+ return SSL_TLSEXT_ERR_OK ;
617
+ }
605
618
606
619
/*
607
620
*--------------------------------------------------------------------------
@@ -667,6 +680,13 @@ mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream,
667
680
}
668
681
#endif
669
682
683
+ if (!client ) {
684
+ /* Only usd by the Mock Server.
685
+ * Set a callback to get the SNI, if provided */
686
+ SSL_CTX_set_tlsext_servername_callback (ssl_ctx ,
687
+ _mongoc_stream_tls_openssl_sni );
688
+ }
689
+
670
690
if (opt -> weak_cert_validation ) {
671
691
SSL_CTX_set_verify (ssl_ctx , SSL_VERIFY_NONE , NULL );
672
692
} else {
@@ -686,6 +706,16 @@ mongoc_stream_tls_openssl_new (mongoc_stream_t *base_stream,
686
706
RETURN (NULL );
687
707
}
688
708
709
+ /* Added in OpenSSL 0.9.8f, as a build time option */
710
+ #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
711
+ if (client ) {
712
+ SSL * ssl ;
713
+ /* Set the SNI hostname we are expecting certificate for */
714
+ BIO_get_ssl (bio_ssl , & ssl );
715
+ SSL_set_tlsext_host_name (ssl , host );
716
+ #endif
717
+ }
718
+
689
719
690
720
BIO_push (bio_ssl , bio_mongoc_shim );
691
721
0 commit comments