2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 VPN_SECURITY_POLICIES_CONFIG_SETTINGS
="CIPHER COMPRESSION GROUP_TYPE \
23 INTEGRITY PSEUDO_RANDOM_FUNCTION KEY_EXCHANGE LIFETIME PFS"
24 VPN_SECURITY_POLICIES_READONLY
="system performance"
26 VPN_DEFAULT_SECURITY_POLICY
="system"
28 declare -A VPN_SUPPORTED_CIPHERS
=(
30 [3DES-CBC
]="168 bit 3DES-EDE-CBC"
33 [AES256-CBC
]="256 bit AES-CBC"
34 [AES192-CBC
]="192 bit AES-CBC"
35 [AES128-CBC
]="128 bit AES-CBC"
38 [AES256-CTR
]="256 bit AES-COUNTER"
39 [AES192-CTR
]="192 bit AES-COUNTER"
40 [AES128-CTR
]="128 bit AES-COUNTER"
43 [AES256-GCM128
]="256 bit AES-GCM with 128 bit ICV"
44 [AES192-GCM128
]="192 bit AES-GCM with 128 bit ICV"
45 [AES128-GCM128
]="128 bit AES-GCM with 128 bit ICV"
46 [AES256-GCM96
]="256 bit AES-GCM with 96 bit ICV"
47 [AES192-GCM96
]="192 bit AES-GCM with 96 bit ICV"
48 [AES128-GCM96
]="128 bit AES-GCM with 96 bit ICV"
49 [AES256-GCM64
]="256 bit AES-GCM with 64 bit ICV"
50 [AES192-GCM64
]="192 bit AES-GCM with 64 bit ICV"
51 [AES128-GCM64
]="128 bit AES-GCM with 64 bit ICV"
54 [AES256-CCM128
]="256 bit AES-CCM with 128 bit ICV"
55 [AES192-CCM128
]="192 bit AES-CCM with 128 bit ICV"
56 [AES128-CCM128
]="128 bit AES-CCM with 128 bit ICV"
57 [AES256-CCM96
]="256 bit AES-CCM with 96 bit ICV"
58 [AES192-CCM96
]="192 bit AES-CCM with 96 bit ICV"
59 [AES128-CCM96
]="128 bit AES-CCM with 96 bit ICV"
60 [AES256-CCM64
]="256 bit AES-CCM with 64 bit ICV"
61 [AES192-CCM64
]="192 bit AES-CCM with 64 bit ICV"
62 [AES128-CCM64
]="128 bit AES-CCM with 64 bit ICV"
65 [CAMELLIA256-CBC
]="256 bit CAMELLIA-CBC"
66 [CAMELLIA192-CBC
]="192 bit CAMELLIA-CBC"
67 [CAMELLIA128-CBC
]="128 bit CAMELLIA-CBC"
70 [CAMELLIA256-CTR
]="256 bit CAMELLIA-COUNTER"
71 [CAMELLIA192-CTR
]="192 bit CAMELLIA-COUNTER"
72 [CAMELLIA128-CTR
]="128 bit CAMELLIA-COUNTER"
75 [CAMELLIA256-GCM128
]="256 bit CAMELLIA-GCM with 128 bit ICV"
76 [CAMELLIA192-GCM128
]="192 bit CAMELLIA-GCM with 128 bit ICV"
77 [CAMELLIA128-GCM128
]="128 bit CAMELLIA-GCM with 128 bit ICV"
78 [CAMELLIA256-GCM96
]="256 bit CAMELLIA-GCM with 96 bit ICV"
79 [CAMELLIA192-GCM96
]="192 bit CAMELLIA-GCM with 96 bit ICV"
80 [CAMELLIA128-GCM96
]="128 bit CAMELLIA-GCM with 96 bit ICV"
81 [CAMELLIA256-GCM64
]="256 bit CAMELLIA-GCM with 64 bit ICV"
82 [CAMELLIA192-GCM64
]="192 bit CAMELLIA-GCM with 64 bit ICV"
83 [CAMELLIA128-GCM64
]="128 bit CAMELLIA-GCM with 64 bit ICV"
86 [CAMELLIA256-CCM128
]="256 bit CAMELLIA-CCM with 128 bit ICV"
87 [CAMELLIA192-CCM128
]="192 bit CAMELLIA-CCM with 128 bit ICV"
88 [CAMELLIA128-CCM128
]="128 bit CAMELLIA-CCM with 128 bit ICV"
89 [CAMELLIA256-CCM96
]="256 bit CAMELLIA-CCM with 96 bit ICV"
90 [CAMELLIA192-CCM96
]="192 bit CAMELLIA-CCM with 96 bit ICV"
91 [CAMELLIA128-CCM96
]="128 bit CAMELLIA-CCM with 96 bit ICV"
92 [CAMELLIA256-CCM64
]="256 bit CAMELLIA-CCM with 64 bit ICV"
93 [CAMELLIA192-CCM64
]="192 bit CAMELLIA-CCM with 64 bit ICV"
94 [CAMELLIA128-CCM64
]="128 bit CAMELLIA-CCM with 64 bit ICV"
97 [CHACHA20-POLY1305
]="256 bit ChaCha20/Poly1305 with 128 bit ICV"
100 [NULL
]="No Encryption"
103 declare -A CIPHER_TO_STRONGSWAN
=(
108 [AES256-CBC
]="aes256"
109 [AES192-CBC
]="aes192"
110 [AES128-CBC
]="aes128"
113 [AES256-CTR
]="aes256ctr"
114 [AES192-CTR
]="aes192ctr"
115 [AES128-CTR
]="aes128ctr"
118 [AES256-GCM128
]="aes256gcm128"
119 [AES192-GCM128
]="aes192gcm128"
120 [AES128-GCM128
]="aes128gcm128"
121 [AES256-GCM96
]="aes256gcm96"
122 [AES192-GCM96
]="aes192gcm96"
123 [AES128-GCM96
]="aes128gcm96"
124 [AES256-GCM64
]="aes256gcm64"
125 [AES192-GCM64
]="aes192gcm64"
126 [AES128-GCM64
]="aes128gcm64"
129 [AES256-CCM128
]="aes256ccm128"
130 [AES192-CCM128
]="aes192ccm128"
131 [AES128-CCM128
]="aes128ccm128"
132 [AES256-CCM96
]="aes256ccm96"
133 [AES192-CCM96
]="aes192ccm96"
134 [AES128-CCM96
]="aes128ccm96"
135 [AES256-CCM64
]="aes256ccm64"
136 [AES192-CCM64
]="aes192ccm64"
137 [AES128-CCM64
]="aes128ccm64"
140 [CAMELLIA256-CBC
]="camellia256"
141 [CAMELLIA192-CBC
]="camellia192"
142 [CAMELLIA128-CBC
]="camellia128"
145 [CAMELLIA256-CTR
]="camellia256ctr"
146 [CAMELLIA192-CTR
]="camellia192ctr"
147 [CAMELLIA128-CTR
]="camellia128ctr"
150 [CAMELLIA256-GCM128
]="camellia256gcm128"
151 [CAMELLIA192-GCM128
]="camellia192gcm128"
152 [CAMELLIA128-GCM128
]="camellia128gcm128"
153 [CAMELLIA256-GCM96
]="camellia256gcm96"
154 [CAMELLIA192-GCM96
]="camellia192gcm96"
155 [CAMELLIA128-GCM96
]="camellia128gcm96"
156 [CAMELLIA256-GCM64
]="camellia256gcm64"
157 [CAMELLIA192-GCM64
]="camellia192gcm64"
158 [CAMELLIA128-GCM64
]="camellia128gcm64"
161 [CAMELLIA256-CCM128
]="camellia256ccm128"
162 [CAMELLIA192-CCM128
]="camellia192ccm128"
163 [CAMELLIA128-CCM128
]="camellia128ccm128"
164 [CAMELLIA256-CCM96
]="camellia256ccm96"
165 [CAMELLIA192-CCM96
]="camellia192ccm96"
166 [CAMELLIA128-CCM96
]="camellia128ccm96"
167 [CAMELLIA256-CCM64
]="camellia256ccm64"
168 [CAMELLIA192-CCM64
]="camellia192ccm64"
169 [CAMELLIA128-CCM64
]="camellia128ccm64"
172 [CHACHA20-POLY1305
]="chacha20poly1305"
178 declare -A VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTION
=(
188 [AES-XCBC
]="AES-XCBC"
189 [AES-CMAC
]="AES-CMAC"
192 declare -A PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN
=(
202 [AES-XCBC
]="prfaesxcbc"
203 [AES-CMAC
]="prfaescmac"
206 declare -A VPN_SUPPORTED_INTEGRITY
=(
211 [SHA512
]="512 bit SHA2-HMAC"
212 [SHA384
]="384 bit SHA2-HMAC"
213 [SHA256
]="256 bit SHA2-HMAC"
216 [AES-XCBC
]="AES-XCBC"
217 [AES-CMAC
]="AES-CMAC"
218 [AES256-GMAC
]="256 bit AES-GMAC"
219 [AES192-GMAC
]="192 bit AES-GMAC"
220 [AES128-GMAC
]="128 bit AES-GMAC"
223 declare -A INTEGRITY_TO_STRONGSWAN
=(
235 [AES256-GMAC
]="aes256gmac"
236 [AES192-GMAC
]="aes192gmac"
237 [AES128-GMAC
]="aes128gmac"
240 declare -A VPN_SUPPORTED_GROUP_TYPES
=(
242 [MODP768
]="768 bit Modulo Prime Group"
243 [MODP1024
]="1024 bit Modulo Prime Group"
244 [MODP1536
]="1536 bit Modulo Prime Group"
245 [MODP2048
]="2048 bit Modulo Prime Group"
246 [MODP3072
]="3072 bit Modulo Prime Group"
247 [MODP4096
]="4096 bit Modulo Prime Group"
248 [MODP6144
]="6144 bit Modulo Prime Group"
249 [MODP8192
]="8192 bit Modulo Prime Group"
251 # NIST Elliptic Curve Groups
252 [ECP192
]="192 bit NIST Elliptic Curve Group"
253 [ECP224
]="224 bit NIST Elliptic Curve Group"
254 [ECP256
]="256 bit NIST Elliptic Curve Group"
255 [ECP384
]="384 bit NIST Elliptic Curve Group"
256 [ECP521
]="521 bit NIST Elliptic Curve Group"
258 # Brainpool Elliptic Curve Groups
259 [ECP224BP
]="224 bit Brainpool Elliptic Curve Group"
260 [ECP256BP
]="256 bit Brainpool Elliptic Curve Group"
261 [ECP384BP
]="384 bit Brainpool Elliptic Curve Group"
262 [ECP512BP
]="512 bit Brainpool Elliptic Curve Group"
265 [CURVE25519
]="256 bit Elliptic Curve 25519"
268 declare -A GROUP_TYPE_TO_STRONGSWAN
=(
271 [MODP1024
]="modp1024"
272 [MODP1536
]="modp1536"
273 [MODP2048
]="modp2048"
274 [MODP3072
]="modp3072"
275 [MODP4096
]="modp4096"
276 [MODP6144
]="modp6144"
277 [MODP8192
]="modp8192"
279 # NIST Elliptic Curve Groups
286 # Brainpool Elliptic Curve Groups
287 [ECP224BP
]="ecp224bp"
288 [ECP256BP
]="ecp256bp"
289 [ECP384BP
]="ecp384bp"
290 [ECP512BP
]="ecp512bp"
293 [CURVE25519
]="curve25519"
296 cli_vpn_security_policies
() {
298 local security_policy
300 if vpn_security_policy_exists
${1}; then
306 cipher|compression|integrity|lifetime|pfs|show
)
307 vpn_security_policies_
${key} ${security_policy} "$@"
310 vpn_security_policies_group_type
${security_policy} "$@"
313 vpn_security_policies_key_exchange
${security_policy} "$@"
316 error
"Unrecognized argument: ${key}"
326 vpn_security_policies_new
"$@"
329 vpn_security_policies_destroy
"$@"
332 if [ -n "${action}" ]; then
333 error
"Unrecognized argument: '${action}'"
341 # This functions checks if a policy is readonly
342 # returns true when yes and false when no
343 vpn_security_policies_check_readonly
() {
344 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
351 # This function writes all values to a via ${name} specificated vpn security policy configuration file
352 vpn_security_policies_write_config
() {
357 if ! vpn_security_policy_exists
"${name}"; then
358 log ERROR
"No such vpn security policy: ${name}"
362 if vpn_security_policies_check_readonly
"${name}"; then
363 log ERROR
"The ${name} vpn security policy cannot be changed."
367 local path
="$(vpn_security_policies_path "${name}")"
368 if [ ! -w ${path} ]; then
369 log ERROR
"${path} is not writeable"
373 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
374 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
378 if ! vpn_security_policies_reload
${name}; then
379 log WARNING
"Could not reload the IPsec connection using this security policy"
384 # reload IPsec connections using a special policy
385 vpn_security_policies_reload
() {
389 for connection
in $
(ipsec_list_connections
); do
390 local SECURITY_POLICY ENABLED
392 if ! ipsec_connection_read_config
"${connection}" "SECURITY_POLICY"; then
396 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED
; then
397 if ! ipsec_connection_to_strongswan
"${connection}"; then
398 log ERROR
"Could not generate strongswan config for ${connnection}"
403 ipsec_strongswan_load
406 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
407 vpn_security_policies_write_config_key
() {
416 if ! vpn_security_policy_exists
"${name}"; then
417 log ERROR
"No such vpn security policy: ${name}"
421 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
423 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
425 # Read the config settings
426 if ! vpn_security_policies_read_config
"${name}"; then
430 # Set the key to a new value
431 assign
"${key}" "${value}"
433 if ! vpn_security_policies_write_config
"${name}"; then
440 # Reads one or more keys out of a settings file or all if no key is provided.
441 vpn_security_policies_read_config
() {
447 if ! vpn_security_policy_exists
"${name}"; then
448 log ERROR
"No such vpn security policy: ${name}"
454 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
455 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
457 list_append args
"$@"
460 local path
="$(vpn_security_policies_path ${name})"
462 if ! settings_read
"${path}" ${args}; then
463 log ERROR
"Could not read settings for vpn security policy ${name}"
468 # Returns the path to a the configuration fora given name
469 vpn_security_policies_path
() {
474 if vpn_security_policies_check_readonly
"${name}"; then
475 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
477 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
481 # Print the content of a vpn security policy configuration file in a nice way
482 vpn_security_policies_show
() {
487 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
488 if ! vpn_security_policies_read_config
${name}; then
492 cli_print_fmt1
0 "Security Policy: ${name}"
495 # This could be done in a loop but a loop is much more complicated
496 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
497 cli_print_fmt1
1 "Ciphers:"
499 for cipher
in ${CIPHER}; do
500 cli_print_fmt1
2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
504 cli_print_fmt1
1 "Integrity:"
506 for integrity
in ${INTEGRITY}; do
507 cli_print_fmt1
2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
511 cli_print_fmt1
1 "Group Types:"
513 for group_type
in ${GROUP_TYPE}; do
514 cli_print_fmt1
2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
518 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
521 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
522 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
524 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
529 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
531 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
536 if enabled COMPRESSION
; then
537 cli_print_fmt1
1 "Compression:" "enabled"
539 cli_print_fmt1
1 "Compression:" "disabled"
544 # This function checks if a vpn security policy exists
545 # Returns True when yes and false when not
546 vpn_security_policy_exists
() {
551 local path
=$
(vpn_security_policies_path
"${name}")
553 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
557 # This function parses the parameters for the 'cipher' command
558 vpn_security_policies_cipher
(){
562 if [ $# -eq 0 ]; then
563 log ERROR
"You must pass at least one value after cipher"
568 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
572 # Remove duplicated entries to proceed the list safely
573 CIPHER
="$(list_unique ${CIPHER})"
576 local ciphers_removed
579 while [ $# -gt 0 ]; do
584 list_append ciphers_added
"${arg:1}"
587 list_append ciphers_removed
"${arg:1}"
590 list_append ciphers_set
"${arg}"
593 error
"Invalid argument: ${arg}"
600 # Check if the user is trying a mixed operation
601 if ! list_is_empty ciphers_set
&& (! list_is_empty ciphers_added ||
! list_is_empty ciphers_removed
); then
602 error
"You cannot reset the cipher list and add or remove ciphers at the same time"
606 # Set new cipher list
607 if ! list_is_empty ciphers_set
; then
608 # Check if all ciphers are valid
610 for cipher
in ${ciphers_set}; do
611 if ! vpn_security_policies_cipher_supported
${cipher}; then
612 error
"Unsupported cipher: ${cipher}"
617 CIPHER
="${ciphers_set}"
619 # Perform incremental updates
623 # Perform all removals
624 for cipher
in ${ciphers_removed}; do
625 if ! list_remove CIPHER
${cipher}; then
626 warning
"${cipher} was not on the list and could not be removed"
630 for cipher
in ${ciphers_added}; do
631 if vpn_security_policies_cipher_supported
${cipher}; then
632 if ! list_append_unique CIPHER
${cipher}; then
633 warning
"${cipher} is already on the cipher list"
636 warning
"${cipher} is unknown or unsupported and could not be added"
641 # Check if the list contain at least one valid cipher
642 if list_is_empty CIPHER
; then
643 error
"Cannot save an empty cipher list"
648 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
649 log ERROR
"The changes for the vpn security policy ${name} could not be written."
652 cli_headline
1 "Current cipher list for ${name}:"
653 for cipher
in ${CIPHER}; do
654 cli_print_fmt1
1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
658 # This function parses the parameters for the 'compression' command
659 vpn_security_policies_compression
(){
663 # Check if we get only one argument after compression <name>
664 if [ ! $# -eq 2 ]; then
665 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
669 if ! isbool value
; then
670 # We suggest only two values to avoid overburding the user.
671 log ERROR
"Invalid Argument ${value}"
675 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
678 # This function parses the parameters for the 'group-type' command
679 vpn_security_policies_group_type
(){
683 if [ $# -eq 0 ]; then
684 log ERROR
"You must pass at least one value after group-type"
689 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
693 # Remove duplicated entries to proceed the list safely
694 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
696 local group_types_added
697 local group_types_removed
698 local group_types_set
700 while [ $# -gt 0 ]; do
705 list_append group_types_added
"${arg:1}"
708 list_append group_types_removed
"${arg:1}"
711 list_append group_types_set
"${arg}"
714 error
"Invalid argument: ${arg}"
721 # Check if the user is trying a mixed operation
722 if ! list_is_empty group_types_set
&& (! list_is_empty group_types_added ||
! list_is_empty group_types_removed
); then
723 error
"You cannot reset the group type list and add or remove group types at the same time"
727 # Set new group type list
728 if ! list_is_empty group_types_set
; then
729 # Check if all group types are valid
731 for group_type
in ${group_types_set}; do
732 if ! vpn_security_policies_group_type_supported
${group_type}; then
733 error
"Unsupported group type: ${group_type}"
738 GROUP_TYPE
="${group_types_set}"
740 # Perform incremental updates
744 # Perform all removals
745 for group_type
in ${group_types_removed}; do
746 if ! list_remove GROUP_TYPE
${group_type}; then
747 warning
"${group_type} was not on the list and could not be removed"
751 for group_type
in ${group_types_added}; do
752 if vpn_security_policies_group_type_supported
${group_type}; then
753 if ! list_append_unique GROUP_TYPE
${group_type}; then
754 warning
"${group_type} is already on the group type list"
757 warning
"${group_type} is unknown or unsupported and could not be added"
762 # Check if the list contain at least one valid group_type
763 if list_is_empty GROUP_TYPE
; then
764 error
"Cannot save an empty group type list"
769 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
770 log ERROR
"The changes for the vpn security policy ${name} could not be written."
773 cli_headline
1 "Current group type list for ${name}:"
774 for group_type
in ${GROUP_TYPE}; do
775 cli_print_fmt1
1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
779 # This function parses the parameters for the 'integrity' command
780 vpn_security_policies_integrity
(){
784 if [ $# -eq 0 ]; then
785 log ERROR
"You must pass at least one value after integrity"
790 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
794 # Remove duplicated entries to proceed the list safely
795 INTEGRITY
="$(list_unique ${INTEGRITY})"
797 local integritys_added
798 local integritys_removed
801 while [ $# -gt 0 ]; do
806 list_append integritys_added
"${arg:1}"
809 list_append integritys_removed
"${arg:1}"
812 list_append integritys_set
"${arg}"
815 error
"Invalid argument: ${arg}"
822 # Check if the user is trying a mixed operation
823 if ! list_is_empty integritys_set
&& (! list_is_empty integritys_added ||
! list_is_empty integritys_removed
); then
824 error
"You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
828 # Set new integrity list
829 if ! list_is_empty integritys_set
; then
830 # Check if all integrity hashes are valid
832 for integrity
in ${integritys_set}; do
833 if ! vpn_security_policies_integrity_supported
${integrity}; then
834 error
"Unsupported integrity hash: ${integrity}"
839 INTEGRITY
="${integritys_set}"
841 # Perform incremental updates
845 # Perform all removals
846 for integrity
in ${integritys_removed}; do
847 if ! list_remove INTEGRITY
${integrity}; then
848 warning
"${integrity} was not on the list and could not be removed"
852 for integrity
in ${integritys_added}; do
853 if vpn_security_policies_integrity_supported
${integrity}; then
854 if ! list_append_unique INTEGRITY
${integrity}; then
855 warning
"${integrity} is already on the integrity list"
858 warning
"${integrity} is unknown or unsupported and could not be added"
863 # Check if the list contain at least one valid integrity
864 if list_is_empty INTEGRITY
; then
865 error
"Cannot save an empty integrity hashes list"
870 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
871 log ERROR
"The changes for the vpn security policy ${name} could not be written."
874 cli_headline
1 "Current integrity hashes list for ${name}:"
875 for integrity
in ${INTEGRITY}; do
876 cli_print_fmt1
1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}"
880 # This function parses the parameters for the 'key-exchange' command
881 vpn_security_policies_key_exchange
() {
885 # Check if we get only one argument after key-exchange <name>
886 if [ ! $# -eq 2 ]; then
887 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
891 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
892 log ERROR
"Invalid Argument ${value}"
896 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
899 # This function parses the parameters for the 'lifetime' command.
900 vpn_security_policies_lifetime
(){
906 # Check if we get only one argument after lifetime <name>
907 if [ ! $# -ge 1 ]; then
908 log ERROR
"The number of arguments do not match you must provide at least one integer value or a valid time with the format <hours>h <minutes>m <seconds>s"
912 if ! isinteger value
; then
913 value
=$
(parse_time
"$@")
914 if [ ! $?
-eq 0 ]; then
915 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
920 if [ ${value} -le 0 ]; then
921 log ERROR
"The passed time value must be in the sum greater zero seconds."
925 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
928 # This function parses the parameters for the 'pfs' command
929 vpn_security_policies_pfs
(){
933 # Check if we get only one argument after pfs <name>
934 if [ ! $# -eq 2 ]; then
935 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
939 if [ ! $# -eq 2 ] ||
! isbool value
; then
940 # We suggest only two values to avoid overburding the user.
941 log ERROR
"Invalid Argument ${value}"
945 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
948 # This function checks if a vpn security policy name is valid
949 # Allowed are only A-Za-z0-9
950 vpn_security_policies_check_name
() {
955 [[ ${name} =~
[^
[:alnum
:]$
] ]]
958 # Function that creates based on the paramters one ore more new vpn security policies
959 vpn_security_policies_new
() {
960 if [ $# -gt 1 ]; then
961 error
"Too many arguments"
966 if ! isset name
; then
967 error
"Please provide a name"
971 # Check for duplicates
972 if vpn_security_policy_exists
"${name}"; then
973 error
"The VPN security policy with name ${name} already exists"
977 # Check if name is valid
978 if vpn_security_policies_check_name
"${name}"; then
979 error
"'${name}' contains illegal characters"
983 # Check if we have a read-only policy with the same name
984 if vpn_security_policies_check_readonly
"${name}"; then
985 error
"The VPN security policy ${name} is read-only"
989 # Check if our source policy exists
990 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
991 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
995 log DEBUG
"Creating VPN Security Policy ${name}"
997 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
998 "$(vpn_security_policies_path ${name})"; then
999 log INFO
"VPN Security Policy ${name} successfully created"
1001 log ERROR
"Could not create VPN Security Policy ${name}"
1002 return ${EXIT_ERROR}
1005 # Show the newly created policy
1006 vpn_security_policies_show
"${name}"
1009 # Function that deletes based on the passed parameters one ore more vpn security policies
1010 vpn_security_policies_destroy
() {
1012 for name
in "$@"; do
1013 if ! vpn_security_policy_exists
${name}; then
1014 log ERROR
"The vpn security policy ${name} does not exist."
1018 if vpn_security_policies_check_readonly
${name}; then
1019 log ERROR
"The vpn security policy ${name} cannot be deleted."
1023 log DEBUG
"Deleting vpn security policy ${name}"
1024 settings_remove $
(vpn_security_policies_path
${name})
1027 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
1031 vpn_security_policies_cipher_supported
() {
1034 list_match
${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1038 vpn_security_policies_group_type_supported
() {
1039 local group_type
=${1}
1041 list_match
${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1044 vpn_security_policies_integrity_supported
() {
1045 local integrity
=${1}
1047 list_match
${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]}
1050 vpn_security_policies_cipher_is_aead
() {
1053 # All CCM and GCM ciphers are AEAD
1054 if string_match
"[CG]CM" "${cipher}"; then
1059 if string_match
"POLY1305" "${cipher}"; then
1063 return ${EXIT_FALSE}
1066 vpn_security_policies_make_ike_proposal
() {
1069 if ! vpn_security_policy_exists
${name}; then
1070 return ${EXIT_ERROR}
1073 local config_path
="$(vpn_security_policies_path ${name})"
1074 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
1076 # Get data from cache if possible
1077 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1078 fread
"${cache_path}"
1082 # No or invalid cache data found
1083 local proposal
=$
(_vpn_security_policies_make_ike_proposal
"${name}")
1085 # Write proposal to cache
1086 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1087 log WARNING
"Could not write to cache: ${cache_path}"
1093 _vpn_security_policies_make_ike_proposal
() {
1096 # Read the config settings
1097 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1098 if ! vpn_security_policies_read_config
"${name}"; then
1099 return ${EXIT_ERROR}
1105 for cipher
in ${CIPHER}; do
1107 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1109 if ! isset _cipher
; then
1110 log WARN
"Unsupported cipher: ${cipher}"
1114 if vpn_security_policies_cipher_is_aead
"${cipher}"; then
1116 for prf
in ${PSEUDO_RANDOM_FUNCTION}; do
1117 local _prf
="${PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN[${prf}]}"
1119 if ! isset _prf
; then
1120 log WARN
"Unsupported pseudo random function: ${prf}"
1125 for group_type
in ${GROUP_TYPE}; do
1126 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1128 if ! isset _group_type
; then
1129 log WARN
"Unsupported group-type: ${group_type}"
1133 # Put everything together
1134 list_append proposals
"${_cipher}-${_prf}-${_group_type}"
1139 for integrity
in ${INTEGRITY}; do
1140 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1142 if ! isset _integrity
; then
1143 log WARN
"Unsupported integrity: ${integrity}"
1148 for group_type
in ${GROUP_TYPE}; do
1149 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1151 if ! isset _group_type
; then
1152 log WARN
"Unsupported group-type: ${group_type}"
1156 # Put everything together
1157 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1163 # Returns as a comma-separated list
1164 list_join proposals
,
1167 vpn_security_policies_make_esp_proposal
() {
1170 if ! vpn_security_policy_exists
${name}; then
1171 return ${EXIT_ERROR}
1174 local config_path
="$(vpn_security_policies_path ${name})"
1175 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1177 # Get data from cache if possible
1178 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1179 fread
"${cache_path}"
1183 # No or invalid cache data found
1184 local proposal
=$
(_vpn_security_policies_make_esp_proposal
"${name}")
1186 # Write proposal to cache
1187 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1188 log WARNING
"Could not write to cache: ${cache_path}"
1194 _vpn_security_policies_make_esp_proposal
() {
1197 # Read the config settings
1198 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1199 if ! vpn_security_policies_read_config
"${name}"; then
1200 return ${EXIT_ERROR}
1206 for cipher
in ${CIPHER}; do
1208 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1210 if ! isset _cipher
; then
1211 log WARN
"Unsupported cipher: ${cipher}"
1215 if vpn_security_policies_cipher_is_aead
${cipher}; then
1217 for group_type
in ${GROUP_TYPE}; do
1218 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1220 if ! isset _group_type
; then
1221 log WARN
"Unsupported group-type: ${group_type}"
1225 # Put everything together
1226 list_append proposals
"${_cipher}-${_group_type}"
1230 for integrity
in ${INTEGRITY}; do
1231 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1233 if ! isset _integrity
; then
1234 log WARN
"Unsupported integrity: ${integrity}"
1239 for group_type
in ${GROUP_TYPE}; do
1240 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1242 if ! isset _group_type
; then
1243 log WARN
"Unsupported group-type: ${group_type}"
1247 # Put everything together
1248 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1254 # Returns as a comma-separated list
1255 list_join proposals
,
1258 # List all security policies
1259 vpn_security_policies_list_all
() {
1260 list_directory
"${NETWORK_SHARE_DIR}/vpn/security-policies"
1262 # Add all user-defined policies
1263 vpn_security_policies_list_user
1266 vpn_security_policies_list_user
() {
1267 list_directory
"${NETWORK_CONFIG_DIR}/vpn/security-policies"