@@ -10,6 +10,9 @@ use crate::error::{Error, ErrorKind};
1010#[ cfg( feature = "postgresql-native" ) ]
1111use tokio_postgres:: config:: { ChannelBinding , SslMode } ;
1212
13+ #[ cfg( feature = "postgresql-native" ) ]
14+ use native_tls:: Protocol ;
15+
1316#[ derive( Clone ) ]
1417pub ( crate ) struct Hidden < T > ( pub ( crate ) T ) ;
1518
@@ -31,6 +34,10 @@ pub struct SslParams {
3134 pub ( crate ) identity_file : Option < String > ,
3235 pub ( crate ) identity_password : Hidden < Option < String > > ,
3336 pub ( crate ) ssl_accept_mode : SslAcceptMode ,
37+ #[ cfg( feature = "postgresql-native" ) ]
38+ pub ( crate ) tls_min_version : Option < native_tls:: Protocol > ,
39+ #[ cfg( feature = "postgresql-native" ) ]
40+ pub ( crate ) tls_max_version : Option < native_tls:: Protocol > ,
3441}
3542
3643#[ derive( Debug , Clone , Copy ) ]
@@ -290,6 +297,17 @@ impl PostgresNativeUrl {
290297 self . flavour = flavour;
291298 }
292299
300+ #[ cfg( feature = "postgresql-native" ) ]
301+ fn parse_tls_protocol ( v : & str ) -> Result < Protocol , Error > {
302+ match v. to_lowercase ( ) . as_str ( ) {
303+ "tlsv1" | "tls1" | "tls1.0" => Ok ( Protocol :: Tlsv10 ) ,
304+ "tlsv1.1" | "tls1.1" => Ok ( Protocol :: Tlsv11 ) ,
305+ "tlsv1.2" | "tls1.2" => Ok ( Protocol :: Tlsv12 ) ,
306+ _ => Err ( Error :: builder ( ErrorKind :: InvalidConnectionArguments )
307+ . build ( ) ) ,
308+ }
309+ }
310+
293311 fn parse_query_params ( url : & Url ) -> Result < PostgresUrlQueryParams , Error > {
294312 #[ cfg( feature = "postgresql-native" ) ]
295313 let mut ssl_mode = SslMode :: Prefer ;
@@ -313,6 +331,10 @@ impl PostgresNativeUrl {
313331 let mut max_idle_connection_lifetime = Some ( Duration :: from_secs ( 300 ) ) ;
314332 let mut options = None ;
315333 let mut single_use_connections = false ;
334+ #[ cfg( feature = "postgresql-native" ) ]
335+ let mut tls_min_version: Option < Protocol > = None ;
336+ #[ cfg( feature = "postgresql-native" ) ]
337+ let mut tls_max_version: Option < Protocol > = None ;
316338
317339 for ( k, v) in url. query_pairs ( ) {
318340 match k. as_ref ( ) {
@@ -451,6 +473,14 @@ impl PostgresNativeUrl {
451473 "options" => {
452474 options = Some ( v. to_string ( ) ) ;
453475 }
476+ #[ cfg( feature = "postgresql-native" ) ]
477+ "tls_min_version" => {
478+ tls_min_version = Some ( Self :: parse_tls_protocol ( & v) ?) ;
479+ }
480+ #[ cfg( feature = "postgresql-native" ) ]
481+ "tls_max_version" => {
482+ tls_max_version = Some ( Self :: parse_tls_protocol ( & v) ?) ;
483+ }
454484 "single_use_connections" => {
455485 single_use_connections = v
456486 . parse ( )
@@ -468,6 +498,10 @@ impl PostgresNativeUrl {
468498 identity_file,
469499 ssl_accept_mode,
470500 identity_password : Hidden ( identity_password) ,
501+ #[ cfg( feature = "postgresql-native" ) ]
502+ tls_min_version,
503+ #[ cfg( feature = "postgresql-native" ) ]
504+ tls_max_version,
471505 } ,
472506 connection_limit,
473507 schema,
0 commit comments