@@ -214,6 +214,7 @@ pub struct SslOpts {
214
214
#[ cfg( any( feature = "native-tls-tls" , feature = "rustls-tls" ) ) ]
215
215
client_identity : Option < ClientIdentity > ,
216
216
root_certs : Vec < PathOrBuf < ' static > > ,
217
+ disable_built_in_roots : bool ,
217
218
skip_domain_validation : bool ,
218
219
accept_invalid_certs : bool ,
219
220
tls_hostname_override : Option < Cow < ' static , str > > ,
@@ -236,15 +237,61 @@ impl SslOpts {
236
237
self
237
238
}
238
239
239
- /// The way to not validate the server's domain
240
- /// name against its certificate (defaults to `false`).
240
+ /// If `true`, use only the root certificates configured via [`SslOpts::with_root_certs`],
241
+ /// not any system or built-in certs. By default system built-in certs _will be_ used.
242
+ ///
243
+ /// # Connection URL
244
+ ///
245
+ /// Use `built_in_roots` URL parameter to set this value:
246
+ ///
247
+ /// ```
248
+ /// # use mysql_async::*;
249
+ /// # use std::time::Duration;
250
+ /// # fn main() -> Result<()> {
251
+ /// let opts = Opts::from_url("mysql://localhost/db?require_ssl=true&built_in_roots=false")?;
252
+ /// assert_eq!(opts.ssl_opts().unwrap().disable_built_in_roots(), true);
253
+ /// # Ok(()) }
254
+ /// ```
255
+ pub fn with_disable_built_in_roots ( mut self , disable_built_in_roots : bool ) -> Self {
256
+ self . disable_built_in_roots = disable_built_in_roots;
257
+ self
258
+ }
259
+
260
+ /// The way to not validate the server's domain name against its certificate.
261
+ /// By default domain name _will be_ validated.
262
+ ///
263
+ /// # Connection URL
264
+ ///
265
+ /// Use `verify_identity` URL parameter to set this value:
266
+ ///
267
+ /// ```
268
+ /// # use mysql_async::*;
269
+ /// # use std::time::Duration;
270
+ /// # fn main() -> Result<()> {
271
+ /// let opts = Opts::from_url("mysql://localhost/db?require_ssl=true&verify_identity=false")?;
272
+ /// assert_eq!(opts.ssl_opts().unwrap().skip_domain_validation(), true);
273
+ /// # Ok(()) }
274
+ /// ```
241
275
pub fn with_danger_skip_domain_validation ( mut self , value : bool ) -> Self {
242
276
self . skip_domain_validation = value;
243
277
self
244
278
}
245
279
246
- /// If `true` then client will accept invalid certificate (expired, not trusted, ..)
247
- /// (defaults to `false`).
280
+ /// If `true` then client will accept invalid certificate (expired, not trusted, ..).
281
+ /// Invalid certificates _won't get_ accepted by default.
282
+ ///
283
+ /// # Connection URL
284
+ ///
285
+ /// Use `verify_ca` URL parameter to set this value:
286
+ ///
287
+ /// ```
288
+ /// # use mysql_async::*;
289
+ /// # use std::time::Duration;
290
+ /// # fn main() -> Result<()> {
291
+ /// let opts = Opts::from_url("mysql://localhost/db?require_ssl=true&verify_ca=false")?;
292
+ /// assert_eq!(opts.ssl_opts().unwrap().accept_invalid_certs(), true);
293
+ /// # Ok(()) }
294
+ /// ```
248
295
pub fn with_danger_accept_invalid_certs ( mut self , value : bool ) -> Self {
249
296
self . accept_invalid_certs = value;
250
297
self
@@ -271,6 +318,10 @@ impl SslOpts {
271
318
& self . root_certs
272
319
}
273
320
321
+ pub fn disable_built_in_roots ( & self ) -> bool {
322
+ self . disable_built_in_roots
323
+ }
324
+
274
325
pub fn skip_domain_validation ( & self ) -> bool {
275
326
self . skip_domain_validation
276
327
}
@@ -1584,6 +1635,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1584
1635
1585
1636
let mut skip_domain_validation = false ;
1586
1637
let mut accept_invalid_certs = false ;
1638
+ let mut disable_built_in_roots = false ;
1587
1639
1588
1640
for ( key, value) in query_pairs {
1589
1641
if key == "pool_min" {
@@ -1684,10 +1736,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1684
1736
}
1685
1737
} else if key == "max_allowed_packet" {
1686
1738
match usize:: from_str ( & value) {
1687
- Ok ( value) => {
1688
- opts. max_allowed_packet =
1689
- Some ( std:: cmp:: max ( 1024 , std:: cmp:: min ( 1073741824 , value) ) )
1690
- }
1739
+ Ok ( value) => opts. max_allowed_packet = Some ( value. clamp ( 1024 , 1073741824 ) ) ,
1691
1740
_ => {
1692
1741
return Err ( UrlError :: InvalidParamValue {
1693
1742
param : "max_allowed_packet" . into ( ) ,
@@ -1839,6 +1888,18 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1839
1888
} ) ;
1840
1889
}
1841
1890
}
1891
+ } else if key == "built_in_roots" {
1892
+ match bool:: from_str ( & value) {
1893
+ Ok ( x) => {
1894
+ disable_built_in_roots = !x;
1895
+ }
1896
+ _ => {
1897
+ return Err ( UrlError :: InvalidParamValue {
1898
+ param : "built_in_roots" . into ( ) ,
1899
+ value,
1900
+ } ) ;
1901
+ }
1902
+ }
1842
1903
} else {
1843
1904
return Err ( UrlError :: UnknownParameter { param : key } ) ;
1844
1905
}
@@ -1856,6 +1917,7 @@ fn mysqlopts_from_url(url: &Url) -> std::result::Result<MysqlOpts, UrlError> {
1856
1917
if let Some ( ref mut ssl_opts) = opts. ssl_opts . as_mut ( ) {
1857
1918
ssl_opts. accept_invalid_certs = accept_invalid_certs;
1858
1919
ssl_opts. skip_domain_validation = skip_domain_validation;
1920
+ ssl_opts. disable_built_in_roots = disable_built_in_roots;
1859
1921
}
1860
1922
1861
1923
Ok ( opts)
@@ -1973,14 +2035,15 @@ mod test {
1973
2035
) ;
1974
2036
1975
2037
const URL4 : & str =
1976
- "mysql://localhost/foo?require_ssl=true&verify_ca=false&verify_identity=false" ;
2038
+ "mysql://localhost/foo?require_ssl=true&verify_ca=false&verify_identity=false&built_in_roots=false " ;
1977
2039
let opts = Opts :: from_url ( URL4 ) . unwrap ( ) ;
1978
2040
assert_eq ! (
1979
2041
opts. ssl_opts( ) ,
1980
2042
Some (
1981
2043
& SslOpts :: default ( )
1982
2044
. with_danger_accept_invalid_certs( true )
1983
2045
. with_danger_skip_domain_validation( true )
2046
+ . with_disable_built_in_roots( true )
1984
2047
)
1985
2048
) ;
1986
2049
0 commit comments