@@ -117,13 +117,6 @@ void* php_mongo_io_stream_connect(mongo_con_manager *manager, mongo_server_def *
117
117
mongo_manager_log (manager , MLOG_CON , MLOG_FINE , "Connecting to %s (%s) without connection timeout (default_socket_timeout will be used)" , dsn , hash );
118
118
}
119
119
120
- /* Capture the server certificate, if SSL is enabled, so we can do further verification */
121
- if (options -> ssl && options -> ctx ) {
122
- zval capture ;
123
- ZVAL_BOOL (& capture , 1 );
124
- php_stream_context_set_option (options -> ctx , "ssl" , "capture_peer_cert" , & capture );
125
- }
126
-
127
120
zend_replace_error_handling (EH_THROW , mongo_ce_ConnectionException , & error_handler TSRMLS_CC );
128
121
stream = php_stream_xport_create (dsn , dsn_len , 0 , STREAM_XPORT_CLIENT | STREAM_XPORT_CONNECT , hash , options -> connectTimeoutMS > 0 ? & ctimeout : NULL , (php_stream_context * )options -> ctx , & errmsg , & errcode );
129
122
zend_restore_error_handling (& error_handler TSRMLS_CC );
@@ -148,6 +141,13 @@ void* php_mongo_io_stream_connect(mongo_con_manager *manager, mongo_server_def *
148
141
if (options -> ssl ) {
149
142
int crypto_enabled ;
150
143
144
+ /* Capture the server certificate so we can do further verification */
145
+ if (stream -> context ) {
146
+ zval capture ;
147
+ ZVAL_BOOL (& capture , 1 );
148
+ php_stream_context_set_option (stream -> context , "ssl" , "capture_peer_cert" , & capture );
149
+ }
150
+
151
151
zend_replace_error_handling (EH_THROW , mongo_ce_ConnectionException , & error_handler TSRMLS_CC );
152
152
153
153
/* PHP 5.6.0 until 5.6.7 screwed things a bit, see https://bugs.php.net/bug.php?id=69195 */
@@ -202,7 +202,7 @@ void* php_mongo_io_stream_connect(mongo_con_manager *manager, mongo_server_def *
202
202
203
203
#if PHP_VERSION_ID < 50600
204
204
/* This option is available since PHP 5.6.0 */
205
- if (php_stream_context_get_option (options -> ctx , "ssl" , "verify_peer_name" , & verify_peer_name ) == SUCCESS && zend_is_true (* verify_peer_name )) {
205
+ if (php_stream_context_get_option (stream -> context , "ssl" , "verify_peer_name" , & verify_peer_name ) == SUCCESS && zend_is_true (* verify_peer_name )) {
206
206
if (php_mongo_verify_hostname (server , cert TSRMLS_CC ) == FAILURE ) {
207
207
* error_message = strdup ("Cannot verify remote certificate: Hostname doesn't match" );
208
208
mongo_manager_log (manager , MLOG_CON , MLOG_WARN , "Remote certificate SubjectAltName or CN does not match '%s'" , server -> host );
@@ -214,7 +214,7 @@ void* php_mongo_io_stream_connect(mongo_con_manager *manager, mongo_server_def *
214
214
mongo_manager_log (manager , MLOG_CON , MLOG_WARN , "Not verifying peer name for %s:%d, please use 'verify_peer_name' SSL context option" , server -> host , server -> port );
215
215
}
216
216
#endif
217
- if (php_stream_context_get_option (options -> ctx , "ssl" , "verify_expiry" , & verify_expiry ) == SUCCESS && zend_is_true (* verify_expiry )) {
217
+ if (php_stream_context_get_option (stream -> context , "ssl" , "verify_expiry" , & verify_expiry ) == SUCCESS && zend_is_true (* verify_expiry )) {
218
218
time_t current = time (NULL );
219
219
time_t valid_from = php_mongo_asn1_time_to_time_t (X509_get_notBefore (cert ) TSRMLS_CC );
220
220
time_t valid_until = php_mongo_asn1_time_to_time_t (X509_get_notAfter (cert ) TSRMLS_CC );
0 commit comments