78
78
79
79
#define SECURITY_WIN32
80
80
#define SCHANNEL_USE_BLACKLISTS 1
81
+ #define SCHANNEL_USE_BLACKLISTS 1
81
82
#include <schannel.h>
82
83
#include <schnlsp.h>
83
84
#include <security.h>
84
85
#include <versionhelpers.h>
86
+ #include <versionhelpers.h>
85
87
86
88
87
89
/* mingw doesn't define these */
@@ -467,6 +469,7 @@ _mongoc_stream_tls_secure_channel_decrypt(mongoc_stream_tls_secure_channel_t *se
467
469
size_t size = 0 ;
468
470
size_t remaining ;
469
471
bool secbuf_extra_received = false;
472
+ bool secbuf_extra_received = false;
470
473
SecBuffer inbuf [4 ];
471
474
SecBufferDesc inbuf_desc ;
472
475
SECURITY_STATUS sspi_status = SEC_E_OK ;
@@ -479,6 +482,8 @@ _mongoc_stream_tls_secure_channel_decrypt(mongoc_stream_tls_secure_channel_t *se
479
482
while (secure_channel -> encdata_offset > 0 && sspi_status == SEC_E_OK ) {
480
483
secbuf_extra_received = false;
481
484
485
+ secbuf_extra_received = false;
486
+
482
487
/* prepare data buffer for DecryptMessage call */
483
488
_mongoc_secure_channel_init_sec_buffer (& inbuf [0 ],
484
489
SECBUFFER_DATA ,
@@ -541,6 +546,8 @@ _mongoc_stream_tls_secure_channel_decrypt(mongoc_stream_tls_secure_channel_t *se
541
546
542
547
secbuf_extra_received = true;
543
548
549
+ secbuf_extra_received = true;
550
+
544
551
TRACE ("encrypted data cached: offset %d length %d" ,
545
552
(int )secure_channel -> encdata_offset ,
546
553
(int )secure_channel -> encdata_length );
@@ -574,6 +581,27 @@ _mongoc_stream_tls_secure_channel_decrypt(mongoc_stream_tls_secure_channel_t *se
574
581
sspi_status = SEC_E_OK ;
575
582
continue ;
576
583
}
584
+
585
+ if (secbuf_extra_received ) {
586
+ bool ret ;
587
+ bson_error_t error ;
588
+
589
+ secure_channel -> recv_renegotiate = true;
590
+
591
+ /* the tls handshake will pass the contents of SECBUFFER_EXTRA to the server */
592
+ secure_channel -> connecting_state = ssl_connect_2_writing ;
593
+ ret = mongoc_secure_channel_handshake_step_2 (secure_channel -> tls , secure_channel -> hostname , & error );
594
+ if (!ret ) {
595
+ TRACE ("TLS 1.3 renegotiation failed: %s" , error .message );
596
+ secure_channel -> recv_unrecoverable_err = true;
597
+ return ;
598
+ }
599
+
600
+ /* now continue decrypting data */
601
+ secure_channel -> connecting_state = ssl_connect_done ;
602
+ sspi_status = SEC_E_OK ;
603
+ continue ;
604
+ }
577
605
}
578
606
/* check if the server closed the connection */
579
607
else if (sspi_status == SEC_I_CONTEXT_EXPIRED ) {
@@ -712,6 +740,12 @@ _mongoc_stream_tls_secure_channel_readv(
712
740
continue ;
713
741
}
714
742
743
+ /* used up all read bytes for tls renegotiation, try reading again to get next message */
744
+ if (read_ret == 0 && secure_channel -> recv_renegotiate ) {
745
+ secure_channel -> recv_renegotiate = false;
746
+ continue ;
747
+ }
748
+
715
749
if (read_ret < 0 ) {
716
750
RETURN (-1 );
717
751
}
@@ -998,6 +1032,8 @@ mongoc_secure_channel_cred_new(const mongoc_ssl_opt_t *opt)
998
1032
#else
999
1033
cred -> cred = _mongoc_secure_channel_schannel_cred_new (opt , & cred -> cert , enabled_protocols );
1000
1034
cred -> cred_type = schannel_cred ;
1035
+ cred -> cred = _mongoc_secure_channel_schannel_cred_new (opt , & cred -> cert , enabled_protocols );
1036
+ cred -> cred_type = schannel_cred ;
1001
1037
#endif
1002
1038
1003
1039
return cred ;
@@ -1026,10 +1062,12 @@ mongoc_stream_tls_secure_channel_new(mongoc_stream_t *base_stream, const char *h
1026
1062
{
1027
1063
BSON_UNUSED (client );
1028
1064
return mongoc_stream_tls_secure_channel_new_with_creds (base_stream , host , opt , MONGOC_SHARED_PTR_NULL );
1065
+ return mongoc_stream_tls_secure_channel_new_with_creds (base_stream , host , opt , MONGOC_SHARED_PTR_NULL );
1029
1066
}
1030
1067
1031
1068
mongoc_stream_t *
1032
1069
mongoc_stream_tls_secure_channel_new_with_creds (mongoc_stream_t * base_stream ,
1070
+ const char * host ,
1033
1071
const char * host ,
1034
1072
const mongoc_ssl_opt_t * opt ,
1035
1073
mongoc_shared_ptr cred_ptr )
@@ -1047,6 +1085,8 @@ mongoc_stream_tls_secure_channel_new_with_creds(mongoc_stream_t *base_stream,
1047
1085
1048
1086
secure_channel -> hostname = bson_strdup (host );
1049
1087
1088
+ secure_channel -> hostname = bson_strdup (host );
1089
+
1050
1090
secure_channel -> decdata_buffer = bson_malloc (MONGOC_SCHANNEL_BUFFER_INIT_SIZE );
1051
1091
secure_channel -> decdata_length = MONGOC_SCHANNEL_BUFFER_INIT_SIZE ;
1052
1092
secure_channel -> encdata_buffer = bson_malloc (MONGOC_SCHANNEL_BUFFER_INIT_SIZE );
@@ -1073,6 +1113,8 @@ mongoc_stream_tls_secure_channel_new_with_creds(mongoc_stream_t *base_stream,
1073
1113
1074
1114
secure_channel -> tls = tls ;
1075
1115
1116
+ secure_channel -> tls = tls ;
1117
+
1076
1118
TRACE ("%s" , "SSL/TLS connection with endpoint AcquireCredentialsHandle" );
1077
1119
1078
1120
/* setup Schannel API options */
@@ -1099,6 +1141,7 @@ mongoc_stream_tls_secure_channel_new_with_creds(mongoc_stream_t *base_stream,
1099
1141
SECPKG_CRED_OUTBOUND , /* we are preparing outbound connection */
1100
1142
NULL , /* Optional logon */
1101
1143
cred -> cred , /* TLS "configuration", "auth data" */
1144
+ cred -> cred , /* TLS "configuration", "auth data" */
1102
1145
NULL , /* unused */
1103
1146
NULL , /* unused */
1104
1147
& secure_channel -> cred_handle -> cred_handle , /* credential OUT param */
0 commit comments