@@ -224,6 +224,39 @@ pub fn build_openvpn_connection(
224224 }
225225 }
226226
227+ // TLS hardening options
228+ if let Some ( ref key) = config. tls_auth_key {
229+ vpn_data. push ( ( "tls-auth" . into ( ) , key. clone ( ) ) ) ;
230+ if let Some ( dir) = config. tls_auth_direction {
231+ vpn_data. push ( ( "ta-dir" . into ( ) , dir. to_string ( ) ) ) ;
232+ }
233+ }
234+ if let Some ( ref key) = config. tls_crypt {
235+ vpn_data. push ( ( "tls-crypt" . into ( ) , key. clone ( ) ) ) ;
236+ }
237+ if let Some ( ref key) = config. tls_crypt_v2 {
238+ vpn_data. push ( ( "tls-crypt-v2" . into ( ) , key. clone ( ) ) ) ;
239+ }
240+ if let Some ( ref ver) = config. tls_version_min {
241+ vpn_data. push ( ( "tls-version-min" . into ( ) , ver. clone ( ) ) ) ;
242+ }
243+ if let Some ( ref ver) = config. tls_version_max {
244+ vpn_data. push ( ( "tls-version-max" . into ( ) , ver. clone ( ) ) ) ;
245+ }
246+ if let Some ( ref cipher) = config. tls_cipher {
247+ vpn_data. push ( ( "tls-cipher" . into ( ) , cipher. clone ( ) ) ) ;
248+ }
249+ if let Some ( ref cert_type) = config. remote_cert_tls {
250+ vpn_data. push ( ( "remote-cert-tls" . into ( ) , cert_type. clone ( ) ) ) ;
251+ }
252+ if let Some ( ( ref name, ref name_type) ) = config. verify_x509_name {
253+ vpn_data. push ( ( "verify-x509-name" . into ( ) , name. clone ( ) ) ) ;
254+ vpn_data. push ( ( "verify-x509-type" . into ( ) , name_type. clone ( ) ) ) ;
255+ }
256+ if let Some ( ref path) = config. crl_verify {
257+ vpn_data. push ( ( "crl-verify" . into ( ) , path. clone ( ) ) ) ;
258+ }
259+
227260 if let Some ( ref proxy) = config. proxy {
228261 match proxy {
229262 OpenVpnProxy :: Http {
@@ -995,6 +1028,19 @@ mod tests {
9951028 ) ;
9961029 }
9971030
1031+ fn get_vpn_data_value (
1032+ settings : & HashMap < & str , HashMap < & str , Value > > ,
1033+ key : & str ,
1034+ ) -> Option < String > {
1035+ let vpn = settings. get ( "vpn" ) ?;
1036+ let data = vpn. get ( "data" ) ?;
1037+ if let Value :: Dict ( dict) = data {
1038+ let val: String = dict. get :: < Value , String > ( & Value :: from ( key) ) . ok ( ) ??;
1039+ return Some ( val) ;
1040+ }
1041+ None
1042+ }
1043+
9981044 #[ test]
9991045 fn openvpn_vpn_secrets_has_dict_signature ( ) {
10001046 let config = create_openvpn_config ( )
@@ -1011,4 +1057,141 @@ mod tests {
10111057 "vpn.secrets must be a{{ss}} for NetworkManager"
10121058 ) ;
10131059 }
1060+
1061+ #[ test]
1062+ fn openvpn_tls_auth_key_and_direction ( ) {
1063+ let config = create_openvpn_config ( ) . with_tls_auth ( "/etc/openvpn/ta.key" , Some ( 1 ) ) ;
1064+ let opts = create_test_options ( ) ;
1065+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1066+ assert_eq ! (
1067+ get_vpn_data_value( & settings, "tls-auth" ) . as_deref( ) ,
1068+ Some ( "/etc/openvpn/ta.key" )
1069+ ) ;
1070+ assert_eq ! (
1071+ get_vpn_data_value( & settings, "ta-dir" ) . as_deref( ) ,
1072+ Some ( "1" )
1073+ ) ;
1074+ }
1075+
1076+ #[ test]
1077+ fn openvpn_tls_auth_key_without_direction ( ) {
1078+ let config = create_openvpn_config ( ) . with_tls_auth ( "/etc/openvpn/ta.key" , None ) ;
1079+ let opts = create_test_options ( ) ;
1080+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1081+ assert_eq ! (
1082+ get_vpn_data_value( & settings, "tls-auth" ) . as_deref( ) ,
1083+ Some ( "/etc/openvpn/ta.key" )
1084+ ) ;
1085+ assert ! ( get_vpn_data_value( & settings, "ta-dir" ) . is_none( ) ) ;
1086+ }
1087+
1088+ #[ test]
1089+ fn openvpn_tls_crypt ( ) {
1090+ let config = create_openvpn_config ( ) . with_tls_crypt ( "/etc/openvpn/tls-crypt.key" ) ;
1091+ let opts = create_test_options ( ) ;
1092+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1093+ assert_eq ! (
1094+ get_vpn_data_value( & settings, "tls-crypt" ) . as_deref( ) ,
1095+ Some ( "/etc/openvpn/tls-crypt.key" )
1096+ ) ;
1097+ }
1098+
1099+ #[ test]
1100+ fn openvpn_tls_crypt_v2 ( ) {
1101+ let config = create_openvpn_config ( ) . with_tls_crypt_v2 ( "/etc/openvpn/tls-crypt-v2.key" ) ;
1102+ let opts = create_test_options ( ) ;
1103+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1104+ assert_eq ! (
1105+ get_vpn_data_value( & settings, "tls-crypt-v2" ) . as_deref( ) ,
1106+ Some ( "/etc/openvpn/tls-crypt-v2.key" )
1107+ ) ;
1108+ }
1109+
1110+ #[ test]
1111+ fn openvpn_tls_version_min ( ) {
1112+ let config = create_openvpn_config ( ) . with_tls_version_min ( "1.2" ) ;
1113+ let opts = create_test_options ( ) ;
1114+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1115+ assert_eq ! (
1116+ get_vpn_data_value( & settings, "tls-version-min" ) . as_deref( ) ,
1117+ Some ( "1.2" )
1118+ ) ;
1119+ }
1120+
1121+ #[ test]
1122+ fn openvpn_tls_version_max ( ) {
1123+ let config = create_openvpn_config ( ) . with_tls_version_max ( "1.3" ) ;
1124+ let opts = create_test_options ( ) ;
1125+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1126+ assert_eq ! (
1127+ get_vpn_data_value( & settings, "tls-version-max" ) . as_deref( ) ,
1128+ Some ( "1.3" )
1129+ ) ;
1130+ }
1131+
1132+ #[ test]
1133+ fn openvpn_tls_cipher ( ) {
1134+ let config =
1135+ create_openvpn_config ( ) . with_tls_cipher ( "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384" ) ;
1136+ let opts = create_test_options ( ) ;
1137+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1138+ assert_eq ! (
1139+ get_vpn_data_value( & settings, "tls-cipher" ) . as_deref( ) ,
1140+ Some ( "TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384" )
1141+ ) ;
1142+ }
1143+
1144+ #[ test]
1145+ fn openvpn_remote_cert_tls ( ) {
1146+ let config = create_openvpn_config ( ) . with_remote_cert_tls ( "server" ) ;
1147+ let opts = create_test_options ( ) ;
1148+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1149+ assert_eq ! (
1150+ get_vpn_data_value( & settings, "remote-cert-tls" ) . as_deref( ) ,
1151+ Some ( "server" )
1152+ ) ;
1153+ }
1154+
1155+ #[ test]
1156+ fn openvpn_verify_x509_name ( ) {
1157+ let config = create_openvpn_config ( ) . with_verify_x509_name ( "vpn.example.com" , "name" ) ;
1158+ let opts = create_test_options ( ) ;
1159+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1160+ assert_eq ! (
1161+ get_vpn_data_value( & settings, "verify-x509-name" ) . as_deref( ) ,
1162+ Some ( "vpn.example.com" )
1163+ ) ;
1164+ assert_eq ! (
1165+ get_vpn_data_value( & settings, "verify-x509-type" ) . as_deref( ) ,
1166+ Some ( "name" )
1167+ ) ;
1168+ }
1169+
1170+ #[ test]
1171+ fn openvpn_crl_verify ( ) {
1172+ let config = create_openvpn_config ( ) . with_crl_verify ( "/etc/openvpn/crl.pem" ) ;
1173+ let opts = create_test_options ( ) ;
1174+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1175+ assert_eq ! (
1176+ get_vpn_data_value( & settings, "crl-verify" ) . as_deref( ) ,
1177+ Some ( "/etc/openvpn/crl.pem" )
1178+ ) ;
1179+ }
1180+
1181+ #[ test]
1182+ fn openvpn_tls_options_absent_by_default ( ) {
1183+ let config = create_openvpn_config ( ) ;
1184+ let opts = create_test_options ( ) ;
1185+ let settings = build_openvpn_connection ( & config, & opts) . unwrap ( ) ;
1186+ assert ! ( get_vpn_data_value( & settings, "tls-auth" ) . is_none( ) ) ;
1187+ assert ! ( get_vpn_data_value( & settings, "ta-dir" ) . is_none( ) ) ;
1188+ assert ! ( get_vpn_data_value( & settings, "tls-crypt" ) . is_none( ) ) ;
1189+ assert ! ( get_vpn_data_value( & settings, "tls-crypt-v2" ) . is_none( ) ) ;
1190+ assert ! ( get_vpn_data_value( & settings, "tls-version-min" ) . is_none( ) ) ;
1191+ assert ! ( get_vpn_data_value( & settings, "tls-version-max" ) . is_none( ) ) ;
1192+ assert ! ( get_vpn_data_value( & settings, "tls-cipher" ) . is_none( ) ) ;
1193+ assert ! ( get_vpn_data_value( & settings, "remote-cert-tls" ) . is_none( ) ) ;
1194+ assert ! ( get_vpn_data_value( & settings, "verify-x509-name" ) . is_none( ) ) ;
1195+ assert ! ( get_vpn_data_value( & settings, "crl-verify" ) . is_none( ) ) ;
1196+ }
10141197}
0 commit comments