@@ -1968,6 +1968,29 @@ static bool php_phongo_apply_wc_options_to_uri(mongoc_uri_t* uri, bson_t* option
1968
1968
} /* }}} */
1969
1969
1970
1970
#ifdef MONGOC_ENABLE_SSL
1971
+
1972
+ static void php_phongo_mongoc_ssl_opts_from_uri (mongoc_ssl_opt_t * ssl_opt , mongoc_uri_t * uri , bool * any_ssl_option_set )
1973
+ {
1974
+ const char * pem_file = mongoc_uri_get_option_as_utf8 (uri , MONGOC_URI_SSLCLIENTCERTIFICATEKEYFILE , NULL );
1975
+ const char * pem_pwd = mongoc_uri_get_option_as_utf8 (uri , MONGOC_URI_SSLCLIENTCERTIFICATEKEYPASSWORD , NULL );
1976
+ const char * ca_file = mongoc_uri_get_option_as_utf8 (uri , MONGOC_URI_SSLCERTIFICATEAUTHORITYFILE , NULL );
1977
+
1978
+ ssl_opt -> pem_file = pem_file ? estrdup (pem_file ) : NULL ;
1979
+ ssl_opt -> pem_pwd = pem_pwd ? estrdup (pem_pwd ) : NULL ;
1980
+ ssl_opt -> ca_file = ca_file ? estrdup (ca_file ) : NULL ;
1981
+ ssl_opt -> weak_cert_validation = mongoc_uri_get_option_as_bool (uri , MONGOC_URI_SSLALLOWINVALIDCERTIFICATES , false);
1982
+ ssl_opt -> allow_invalid_hostname = mongoc_uri_get_option_as_bool (uri , MONGOC_URI_SSLALLOWINVALIDHOSTNAMES , false);
1983
+
1984
+ /* Boolean options default to false, so we cannot consider them for
1985
+ * any_ssl_option_set. This isn't actually a problem as libmongoc will
1986
+ * already have assigned them when creating the client, enabling SSL, and
1987
+ * assigning SSL options. Therefore, we only need to check for non-defaults
1988
+ * (i.e. non-NULL strings, true booleans). */
1989
+ if (pem_file || pem_pwd || ca_file || ssl_opt -> weak_cert_validation || ssl_opt -> allow_invalid_hostname ) {
1990
+ * any_ssl_option_set = true;
1991
+ }
1992
+ }
1993
+
1971
1994
static inline char * php_phongo_fetch_ssl_opt_string (zval * zoptions , const char * key , int key_len )
1972
1995
{
1973
1996
int plen ;
@@ -1981,9 +2004,10 @@ static inline char* php_phongo_fetch_ssl_opt_string(zval* zoptions, const char*
1981
2004
return value ;
1982
2005
}
1983
2006
1984
- static mongoc_ssl_opt_t * php_phongo_make_ssl_opt (zval * zoptions TSRMLS_DC )
2007
+ static mongoc_ssl_opt_t * php_phongo_make_ssl_opt (mongoc_uri_t * uri , zval * zoptions TSRMLS_DC )
1985
2008
{
1986
2009
mongoc_ssl_opt_t * ssl_opt ;
2010
+ bool any_ssl_option_set = false;
1987
2011
1988
2012
if (!zoptions ) {
1989
2013
return NULL ;
@@ -2010,44 +2034,76 @@ static mongoc_ssl_opt_t* php_phongo_make_ssl_opt(zval* zoptions TSRMLS_DC)
2010
2034
2011
2035
ssl_opt = ecalloc (1 , sizeof (mongoc_ssl_opt_t ));
2012
2036
2037
+ /* If SSL options are set in the URL, we need to read them and set them on
2038
+ * the options struct so we can merge potential options from passed in
2039
+ * driverOptions (zoptions) */
2040
+ if (mongoc_uri_get_ssl (uri )) {
2041
+ php_phongo_mongoc_ssl_opts_from_uri (ssl_opt , uri , & any_ssl_option_set );
2042
+ }
2043
+
2013
2044
/* Check canonical option names first and fall back to SSL context options
2014
2045
* for backwards compatibility. */
2015
2046
if (php_array_existsc (zoptions , "allow_invalid_hostname" )) {
2016
2047
ssl_opt -> allow_invalid_hostname = php_array_fetchc_bool (zoptions , "allow_invalid_hostname" );
2048
+ any_ssl_option_set = true;
2017
2049
}
2018
2050
2019
2051
if (php_array_existsc (zoptions , "weak_cert_validation" )) {
2020
2052
ssl_opt -> weak_cert_validation = php_array_fetchc_bool (zoptions , "weak_cert_validation" );
2053
+ any_ssl_option_set = true;
2021
2054
} else if (php_array_existsc (zoptions , "allow_self_signed" )) {
2022
2055
ssl_opt -> weak_cert_validation = php_array_fetchc_bool (zoptions , "allow_self_signed" );
2056
+ any_ssl_option_set = true;
2023
2057
}
2024
2058
2059
+ #define PHONGO_SSL_OPTION_SWAP_STRING (o , n ) \
2060
+ if ((o)) { \
2061
+ efree((char*) (o)); \
2062
+ } \
2063
+ (o) = php_phongo_fetch_ssl_opt_string(zoptions, ZEND_STRL((n)));
2064
+
2025
2065
if (php_array_existsc (zoptions , "pem_file" )) {
2026
- ssl_opt -> pem_file = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("pem_file" ));
2066
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> pem_file , "pem_file" );
2067
+ any_ssl_option_set = true;
2027
2068
} else if (php_array_existsc (zoptions , "local_cert" )) {
2028
- ssl_opt -> pem_file = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("local_cert" ));
2069
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> pem_file , "local_cert" );
2070
+ any_ssl_option_set = true;
2029
2071
}
2030
2072
2031
2073
if (php_array_existsc (zoptions , "pem_pwd" )) {
2032
- ssl_opt -> pem_pwd = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("pem_pwd" ));
2074
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> pem_pwd , "pem_pwd" );
2075
+ any_ssl_option_set = true;
2033
2076
} else if (php_array_existsc (zoptions , "passphrase" )) {
2034
- ssl_opt -> pem_pwd = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("passphrase" ));
2077
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> pem_pwd , "passphrase" );
2078
+ any_ssl_option_set = true;
2035
2079
}
2036
2080
2037
2081
if (php_array_existsc (zoptions , "ca_file" )) {
2038
- ssl_opt -> ca_file = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("ca_file" ));
2082
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> ca_file , "ca_file" );
2083
+ any_ssl_option_set = true;
2039
2084
} else if (php_array_existsc (zoptions , "cafile" )) {
2040
- ssl_opt -> ca_file = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("cafile" ));
2085
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> ca_file , "cafile" );
2086
+ any_ssl_option_set = true;
2041
2087
}
2042
2088
2043
2089
if (php_array_existsc (zoptions , "ca_dir" )) {
2044
- ssl_opt -> ca_dir = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("ca_dir" ));
2090
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> ca_dir , "ca_dir" );
2091
+ any_ssl_option_set = true;
2045
2092
} else if (php_array_existsc (zoptions , "capath" )) {
2046
- ssl_opt -> ca_dir = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("capath" ));
2093
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> ca_dir , "capath" );
2094
+ any_ssl_option_set = true;
2047
2095
}
2048
2096
2049
2097
if (php_array_existsc (zoptions , "crl_file" )) {
2050
- ssl_opt -> crl_file = php_phongo_fetch_ssl_opt_string (zoptions , ZEND_STRL ("crl_file" ));
2098
+ PHONGO_SSL_OPTION_SWAP_STRING (ssl_opt -> crl_file , "crl_file" );
2099
+ any_ssl_option_set = true;
2100
+ }
2101
+
2102
+ #undef PHONGO_SSL_OPTION_SWAP_STRING
2103
+
2104
+ if (!any_ssl_option_set ) {
2105
+ efree (ssl_opt );
2106
+ return NULL ;
2051
2107
}
2052
2108
2053
2109
return ssl_opt ;
@@ -2465,7 +2521,7 @@ void phongo_manager_init(php_phongo_manager_t* manager, const char* uri_string,
2465
2521
}
2466
2522
2467
2523
#ifdef MONGOC_ENABLE_SSL
2468
- ssl_opt = php_phongo_make_ssl_opt (driverOptions TSRMLS_CC );
2524
+ ssl_opt = php_phongo_make_ssl_opt (uri , driverOptions TSRMLS_CC );
2469
2525
2470
2526
/* An exception may be thrown during SSL option creation */
2471
2527
if (EG (exception )) {
0 commit comments