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
="CIPHERS COMPRESSION GROUP_TYPES \
23 INTEGRITIES PSEUDO_RANDOM_FUNCTIONS 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_FUNCTIONS
=(
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_INTEGRITIES
=(
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 [CURVE448
]="224 bit Elliptic Curve 448"
271 declare -A GROUP_TYPE_TO_STRONGSWAN
=(
274 [MODP1024
]="modp1024"
275 [MODP1536
]="modp1536"
276 [MODP2048
]="modp2048"
277 [MODP3072
]="modp3072"
278 [MODP4096
]="modp4096"
279 [MODP6144
]="modp6144"
280 [MODP8192
]="modp8192"
282 # NIST Elliptic Curve Groups
289 # Brainpool Elliptic Curve Groups
290 [ECP224BP
]="ecp224bp"
291 [ECP256BP
]="ecp256bp"
292 [ECP384BP
]="ecp384bp"
293 [ECP512BP
]="ecp512bp"
296 [CURVE25519
]="curve25519"
297 [CURVE448
]="curve448"
300 cli_vpn_security_policies
() {
302 local security_policy
304 if vpn_security_policy_exists
${1}; then
310 ciphers|compression|integrities|lifetime|pfs|show
)
311 vpn_security_policies_
${key} ${security_policy} "$@"
313 pseudo-random-functions
)
314 vpn_security_policies_pseudo_random_functions
"${security_policy}" "$@"
317 vpn_security_policies_group_types
${security_policy} "$@"
320 vpn_security_policies_key_exchange
${security_policy} "$@"
323 error
"Unrecognized argument: ${key}"
333 vpn_security_policies_new
"$@"
336 vpn_security_policies_destroy
"$@"
339 if [ -n "${action}" ]; then
340 error
"Unrecognized argument: '${action}'"
348 # This functions checks if a policy is readonly
349 # returns true when yes and false when no
350 vpn_security_policies_check_readonly
() {
351 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
358 # This function writes all values to a via ${name} specificated vpn security policy configuration file
359 vpn_security_policies_write_config
() {
364 if ! vpn_security_policy_exists
"${name}"; then
365 log ERROR
"No such vpn security policy: ${name}"
369 if vpn_security_policies_check_readonly
"${name}"; then
370 log ERROR
"The ${name} vpn security policy cannot be changed."
374 local path
="$(vpn_security_policies_path "${name}")"
375 if [ ! -w ${path} ]; then
376 log ERROR
"${path} is not writeable"
380 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
381 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
385 if ! vpn_security_policies_reload
${name}; then
386 log WARNING
"Could not reload the IPsec connection using this security policy"
391 # reload IPsec connections using a special policy
392 vpn_security_policies_reload
() {
396 for connection
in $
(ipsec_list_connections
); do
397 local SECURITY_POLICY ENABLED
399 if ! ipsec_connection_read_config
"${connection}" "SECURITY_POLICY"; then
403 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED
; then
404 if ! ipsec_connection_to_strongswan
"${connection}"; then
405 log ERROR
"Could not generate strongswan config for ${connnection}"
410 ipsec_strongswan_load
413 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
414 vpn_security_policies_write_config_key
() {
423 if ! vpn_security_policy_exists
"${name}"; then
424 log ERROR
"No such vpn security policy: ${name}"
428 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
430 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
432 # Read the config settings
433 if ! vpn_security_policies_read_config
"${name}"; then
437 # Set the key to a new value
438 assign
"${key}" "${value}"
440 if ! vpn_security_policies_write_config
"${name}"; then
447 # Reads one or more keys out of a settings file or all if no key is provided.
448 vpn_security_policies_read_config
() {
454 if ! vpn_security_policy_exists
"${name}"; then
455 log ERROR
"No such vpn security policy: ${name}"
461 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
462 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
464 list_append args
"$@"
467 local path
="$(vpn_security_policies_path ${name})"
469 if ! settings_read
"${path}" ${args}; then
470 log ERROR
"Could not read settings for vpn security policy ${name}"
475 # Returns the path to a the configuration fora given name
476 vpn_security_policies_path
() {
481 if vpn_security_policies_check_readonly
"${name}"; then
482 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
484 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
488 # Print the content of a vpn security policy configuration file in a nice way
489 vpn_security_policies_show
() {
494 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
495 if ! vpn_security_policies_read_config
${name}; then
499 cli_print_fmt1
0 "Security Policy: ${name}"
502 # This could be done in a loop but a loop is much more complicated
503 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
504 cli_print_fmt1
1 "Ciphers:"
506 for cipher
in ${CIPHERS}; do
507 cli_print_fmt1
2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
511 cli_print_fmt1
1 "Integrity:"
513 for integrity
in ${INTEGRITIES}; do
514 cli_print_fmt1
2 "${VPN_SUPPORTED_INTEGRITIES[${integrity}]-${integrity}}"
518 cli_print_fmt1
1 "Pseudo Random Functions:"
520 for prf
in ${PSEUDO_RANDOM_FUNCTIONS}; do
521 cli_print_fmt1
2 "${VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTIONS[${prf}]-${prf}}"
525 cli_print_fmt1
1 "Group Types:"
527 for group_type
in ${GROUP_TYPES}; do
528 cli_print_fmt1
2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
532 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
535 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
536 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
538 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
543 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
545 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
550 if enabled COMPRESSION
; then
551 cli_print_fmt1
1 "Compression:" "enabled"
553 cli_print_fmt1
1 "Compression:" "disabled"
558 # This function checks if a vpn security policy exists
559 # Returns True when yes and false when not
560 vpn_security_policy_exists
() {
565 local path
=$
(vpn_security_policies_path
"${name}")
567 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
571 # This function parses the parameters for the 'cipher' command
572 vpn_security_policies_ciphers
() {
576 if [ $# -eq 0 ]; then
577 log ERROR
"You must pass at least one value after cipher"
582 if ! vpn_security_policies_read_config
${name} "CIPHERS"; then
586 # Remove duplicated entries to proceed the list safely
587 CIPHERS
="$(list_unique ${CIPHERS})"
590 local ciphers_removed
593 while [ $# -gt 0 ]; do
598 list_append ciphers_added
"${arg:1}"
601 list_append ciphers_removed
"${arg:1}"
604 list_append ciphers_set
"${arg}"
607 error
"Invalid argument: ${arg}"
614 # Check if the user is trying a mixed operation
615 if ! list_is_empty ciphers_set
&& (! list_is_empty ciphers_added ||
! list_is_empty ciphers_removed
); then
616 error
"You cannot reset the cipher list and add or remove ciphers at the same time"
620 # Set new cipher list
621 if ! list_is_empty ciphers_set
; then
622 # Check if all ciphers are valid
624 for cipher
in ${ciphers_set}; do
625 if ! vpn_security_policies_cipher_supported
${cipher}; then
626 error
"Unsupported cipher: ${cipher}"
631 CIPHERS
="${ciphers_set}"
633 # Perform incremental updates
637 # Perform all removals
638 for cipher
in ${ciphers_removed}; do
639 if ! list_remove CIPHERS
${cipher}; then
640 warning
"${cipher} was not on the list and could not be removed"
644 for cipher
in ${ciphers_added}; do
645 if vpn_security_policies_cipher_supported
${cipher}; then
646 if ! list_append_unique CIPHERS
${cipher}; then
647 warning
"${cipher} is already on the cipher list"
650 warning
"${cipher} is unknown or unsupported and could not be added"
655 # Check if the list contain at least one valid cipher
656 if list_is_empty CIPHERS
; then
657 error
"Cannot save an empty cipher list"
662 if ! vpn_security_policies_write_config_key
${name} "CIPHERS" ${CIPHERS}; then
663 log ERROR
"The changes for the vpn security policy ${name} could not be written."
666 cli_headline
1 "Current cipher list for ${name}:"
667 for cipher
in ${CIPHERS}; do
668 cli_print_fmt1
1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
672 # This function parses the parameters for the 'compression' command
673 vpn_security_policies_compression
(){
677 # Check if we get only one argument after compression <name>
678 if [ ! $# -eq 2 ]; then
679 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
683 if ! isbool value
; then
684 # We suggest only two values to avoid overburding the user.
685 log ERROR
"Invalid Argument ${value}"
689 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
692 # This function parses the parameters for the 'group-type' command
693 vpn_security_policies_group_types
() {
697 if [ $# -eq 0 ]; then
698 log ERROR
"You must pass at least one value after group-type"
703 if ! vpn_security_policies_read_config
${name} "GROUP_TYPES"; then
707 # Remove duplicated entries to proceed the list safely
708 GROUP_TYPES
="$(list_unique ${GROUP_TYPES})"
710 local group_types_added
711 local group_types_removed
712 local group_types_set
714 while [ $# -gt 0 ]; do
719 list_append group_types_added
"${arg:1}"
722 list_append group_types_removed
"${arg:1}"
725 list_append group_types_set
"${arg}"
728 error
"Invalid argument: ${arg}"
735 # Check if the user is trying a mixed operation
736 if ! list_is_empty group_types_set
&& (! list_is_empty group_types_added ||
! list_is_empty group_types_removed
); then
737 error
"You cannot reset the group type list and add or remove group types at the same time"
741 # Set new group type list
742 if ! list_is_empty group_types_set
; then
743 # Check if all group types are valid
745 for group_type
in ${group_types_set}; do
746 if ! vpn_security_policies_group_type_supported
${group_type}; then
747 error
"Unsupported group type: ${group_type}"
752 GROUP_TYPES
="${group_types_set}"
754 # Perform incremental updates
758 # Perform all removals
759 for group_type
in ${group_types_removed}; do
760 if ! list_remove GROUP_TYPES
${group_type}; then
761 warning
"${group_type} was not on the list and could not be removed"
765 for group_type
in ${group_types_added}; do
766 if vpn_security_policies_group_type_supported
${group_type}; then
767 if ! list_append_unique GROUP_TYPES
${group_type}; then
768 warning
"${group_type} is already on the group type list"
771 warning
"${group_type} is unknown or unsupported and could not be added"
776 # Check if the list contain at least one valid group_type
777 if list_is_empty GROUP_TYPES
; then
778 error
"Cannot save an empty group type list"
783 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPES" ${GROUP_TYPES}; then
784 log ERROR
"The changes for the vpn security policy ${name} could not be written."
787 cli_headline
1 "Current group type list for ${name}:"
788 for group_type
in ${GROUP_TYPES}; do
789 cli_print_fmt1
1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
793 # This function parses the parameters for the 'integrity' command
794 vpn_security_policies_integrities
() {
798 if [ $# -eq 0 ]; then
799 log ERROR
"You must pass at least one value"
804 if ! vpn_security_policies_read_config
${name} "INTEGRITIES"; then
808 # Remove duplicated entries to proceed the list safely
809 INTEGRITIES
="$(list_unique ${INTEGRITIES})"
811 local integritys_added
812 local integritys_removed
815 while [ $# -gt 0 ]; do
820 list_append integritys_added
"${arg:1}"
823 list_append integritys_removed
"${arg:1}"
826 list_append integritys_set
"${arg}"
829 error
"Invalid argument: ${arg}"
836 # Check if the user is trying a mixed operation
837 if ! list_is_empty integritys_set
&& (! list_is_empty integritys_added ||
! list_is_empty integritys_removed
); then
838 error
"You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
842 # Set new integrity list
843 if ! list_is_empty integritys_set
; then
844 # Check if all integrity hashes are valid
846 for integrity
in ${integritys_set}; do
847 if ! vpn_security_policies_integrity_supported
${integrity}; then
848 error
"Unsupported integrity hash: ${integrity}"
853 INTEGRITIES
="${integritys_set}"
855 # Perform incremental updates
859 # Perform all removals
860 for integrity
in ${integritys_removed}; do
861 if ! list_remove INTEGRITIES
${integrity}; then
862 warning
"${integrity} was not on the list and could not be removed"
866 for integrity
in ${integritys_added}; do
867 if vpn_security_policies_integrity_supported
${integrity}; then
868 if ! list_append_unique INTEGRITIES
${integrity}; then
869 warning
"${integrity} is already on the integrity list"
872 warning
"${integrity} is unknown or unsupported and could not be added"
877 # Check if the list contain at least one valid integrity
878 if list_is_empty INTEGRITIES
; then
879 error
"Cannot save an empty integrity hashes list"
884 if ! vpn_security_policies_write_config_key
${name} "INTEGRITIES" ${INTEGRITIES}; then
885 log ERROR
"The changes for the vpn security policy ${name} could not be written."
888 cli_headline
1 "Current integrity hashes list for ${name}:"
889 for integrity
in ${INTEGRITIES}; do
890 cli_print_fmt1
1 "${integrity}" "${VPN_SUPPORTED_INTEGRITIES[${integrity}]}"
894 # This function parses the parameters for the 'pseudo-random-functions' command
895 vpn_security_policies_pseudo_random_functions
() {
899 if [ $# -eq 0 ]; then
900 log ERROR
"You must pass at least one value"
904 local PSEUDO_RANDOM_FUNCTIONS
905 if ! vpn_security_policies_read_config
${name} "PSEUDO_RANDOM_FUNCTIONS"; then
909 # Remove duplicated entries to proceed the list safely
910 PSEUDO_RANDOM_FUNCTIONS
="$(list_unique ${PSEUDO_RANDOM_FUNCTIONS})"
916 while [ $# -gt 0 ]; do
921 list_append prfs_added
"${arg:1}"
924 list_append prfs_removed
"${arg:1}"
927 list_append prfs_set
"${arg}"
930 error
"Invalid argument: ${arg}"
937 # Check if the user is trying a mixed operation
938 if ! list_is_empty prfs_set
&& (! list_is_empty prfs_added ||
! list_is_empty prfs_removed
); then
939 error
"You cannot reset the pseudo random function list and add or remove functions at the same time"
943 # Set new psudo random function list
944 if ! list_is_empty prfs_set
; then
945 # Check if all PRFs are valid
947 for prf
in ${prfs_set}; do
948 if ! vpn_security_policies_pseudo_random_function_supported
"${prf}"; then
949 error
"Unsupported pseudo random function: ${prf}"
954 PSEUDO_RANDOM_FUNCTIONS
="${prfs_set}"
956 # Perform incremental updates
960 # Perform all removals
961 for prf
in ${prfs_removed}; do
962 if ! list_remove PSEUDO_RANDOM_FUNCTIONS
"${prf}"; then
963 warning
"${prf} was not on the list and could not be removed"
967 for prf
in ${prfs_added}; do
968 if vpn_security_policies_pseudo_random_function_supported
"${prf}"; then
969 if ! list_append_unique PSEUDO_RANDOM_FUNCTIONS
"${prf}"; then
970 warning
"${prf} is already on the list"
973 warning
"${prf} is unknown or unsupported and could not be added"
978 # Check if the list contain at least one valid value
979 if list_is_empty PSEUDO_RANDOM_FUNCTIONS
; then
980 error
"Cannot save an empty list of pseudo random functions"
985 if ! vpn_security_policies_write_config_key
"${name}" "PSEUDO_RANDOM_FUNCTIONS" "${PSEUDO_RANDOM_FUNCTIONS}"; then
986 log ERROR
"The changes for the VPN security policy ${name} could not be written"
989 cli_headline
1 "Current pseudo random function list for ${name}:"
990 for prf
in ${PSEUDO_RANDOM_FUNCTIONS}; do
991 cli_print_fmt1
1 "${prf}" "${VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTIONS[${prf}]}"
995 # This function parses the parameters for the 'key-exchange' command
996 vpn_security_policies_key_exchange
() {
1000 # Check if we get only one argument after key-exchange <name>
1001 if [ ! $# -eq 2 ]; then
1002 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
1003 return ${EXIT_ERROR}
1006 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
1007 log ERROR
"Invalid Argument ${value}"
1008 return ${EXIT_ERROR}
1011 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
1014 # This function parses the parameters for the 'lifetime' command.
1015 vpn_security_policies_lifetime
(){
1021 # Check if we get only one argument after lifetime <name>
1022 if [ ! $# -ge 1 ]; then
1023 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"
1024 return ${EXIT_ERROR}
1027 if ! isinteger value
; then
1028 value
=$
(parse_time
"$@")
1029 if [ ! $?
-eq 0 ]; then
1030 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
1031 return ${EXIT_ERROR}
1035 if [ ${value} -le 0 ]; then
1036 log ERROR
"The passed time value must be in the sum greater zero seconds."
1037 return ${EXIT_ERROR}
1040 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
1043 # This function parses the parameters for the 'pfs' command
1044 vpn_security_policies_pfs
(){
1048 # Check if we get only one argument after pfs <name>
1049 if [ ! $# -eq 2 ]; then
1050 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
1051 return ${EXIT_ERROR}
1054 if [ ! $# -eq 2 ] ||
! isbool value
; then
1055 # We suggest only two values to avoid overburding the user.
1056 log ERROR
"Invalid Argument ${value}"
1057 return ${EXIT_ERROR}
1060 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
1063 # This function checks if a vpn security policy name is valid
1064 # Allowed are only A-Za-z0-9
1065 vpn_security_policies_check_name
() {
1070 [[ ${name} =~
[^
[:alnum
:]$
] ]]
1073 # Function that creates based on the paramters one ore more new vpn security policies
1074 vpn_security_policies_new
() {
1075 if [ $# -gt 1 ]; then
1076 error
"Too many arguments"
1077 return ${EXIT_ERROR}
1081 if ! isset name
; then
1082 error
"Please provide a name"
1083 return ${EXIT_ERROR}
1086 # Check for duplicates
1087 if vpn_security_policy_exists
"${name}"; then
1088 error
"The VPN security policy with name ${name} already exists"
1089 return ${EXIT_ERROR}
1092 # Check if name is valid
1093 if vpn_security_policies_check_name
"${name}"; then
1094 error
"'${name}' contains illegal characters"
1095 return ${EXIT_ERROR}
1098 # Check if we have a read-only policy with the same name
1099 if vpn_security_policies_check_readonly
"${name}"; then
1100 error
"The VPN security policy ${name} is read-only"
1101 return ${EXIT_ERROR}
1104 # Check if our source policy exists
1105 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
1106 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
1107 return ${EXIT_ERROR}
1110 log DEBUG
"Creating VPN Security Policy ${name}"
1112 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
1113 "$(vpn_security_policies_path ${name})"; then
1114 log INFO
"VPN Security Policy ${name} successfully created"
1116 log ERROR
"Could not create VPN Security Policy ${name}"
1117 return ${EXIT_ERROR}
1120 # Show the newly created policy
1121 vpn_security_policies_show
"${name}"
1124 # Function that deletes based on the passed parameters one ore more vpn security policies
1125 vpn_security_policies_destroy
() {
1127 for name
in "$@"; do
1128 if ! vpn_security_policy_exists
${name}; then
1129 log ERROR
"The vpn security policy ${name} does not exist."
1133 if vpn_security_policies_check_readonly
${name}; then
1134 log ERROR
"The vpn security policy ${name} cannot be deleted."
1138 log DEBUG
"Deleting vpn security policy ${name}"
1139 settings_remove $
(vpn_security_policies_path
${name})
1142 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
1146 vpn_security_policies_cipher_supported
() {
1149 list_match
${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1153 vpn_security_policies_group_type_supported
() {
1154 local group_type
=${1}
1156 list_match
${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1159 vpn_security_policies_integrity_supported
() {
1160 local integrity
=${1}
1162 list_match
${integrity} ${!VPN_SUPPORTED_INTEGRITIES[@]}
1165 vpn_security_policies_pseudo_random_function_supported
() {
1168 list_match
"${prf}" ${!VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTIONS[@]}
1171 vpn_security_policies_cipher_is_aead
() {
1174 # All CCM and GCM ciphers are AEAD
1175 if string_match
"[CG]CM" "${cipher}"; then
1180 if string_match
"POLY1305" "${cipher}"; then
1184 return ${EXIT_FALSE}
1187 vpn_security_policies_make_ike_proposal
() {
1190 if ! vpn_security_policy_exists
${name}; then
1191 return ${EXIT_ERROR}
1194 local config_path
="$(vpn_security_policies_path ${name})"
1195 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
1197 # Get data from cache if possible
1198 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1199 fread
"${cache_path}"
1203 # No or invalid cache data found
1204 local proposal
=$
(_vpn_security_policies_make_ike_proposal
"${name}")
1206 # Write proposal to cache
1207 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1208 log WARNING
"Could not write to cache: ${cache_path}"
1214 _vpn_security_policies_make_ike_proposal
() {
1217 # Read the config settings
1218 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1219 if ! vpn_security_policies_read_config
"${name}"; then
1220 return ${EXIT_ERROR}
1226 for cipher
in ${CIPHERS}; do
1228 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1230 if ! isset _cipher
; then
1231 log WARN
"Unsupported cipher: ${cipher}"
1235 if vpn_security_policies_cipher_is_aead
"${cipher}"; then
1237 for prf
in ${PSEUDO_RANDOM_FUNCTIONS}; do
1238 local _prf
="${PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN[${prf}]}"
1240 if ! isset _prf
; then
1241 log WARN
"Unsupported pseudo random function: ${prf}"
1246 for group_type
in ${GROUP_TYPES}; do
1247 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1249 if ! isset _group_type
; then
1250 log WARN
"Unsupported group-type: ${group_type}"
1254 # Put everything together
1255 list_append proposals
"${_cipher}-${_prf}-${_group_type}"
1260 for integrity
in ${INTEGRITIES}; do
1261 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1263 if ! isset _integrity
; then
1264 log WARN
"Unsupported integrity: ${integrity}"
1269 for group_type
in ${GROUP_TYPES}; do
1270 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1272 if ! isset _group_type
; then
1273 log WARN
"Unsupported group-type: ${group_type}"
1277 # Put everything together
1278 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1284 # Returns as a comma-separated list
1285 list_join proposals
,
1288 vpn_security_policies_make_esp_proposal
() {
1291 if ! vpn_security_policy_exists
${name}; then
1292 return ${EXIT_ERROR}
1295 local config_path
="$(vpn_security_policies_path ${name})"
1296 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1298 # Get data from cache if possible
1299 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1300 fread
"${cache_path}"
1304 # No or invalid cache data found
1305 local proposal
=$
(_vpn_security_policies_make_esp_proposal
"${name}")
1307 # Write proposal to cache
1308 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1309 log WARNING
"Could not write to cache: ${cache_path}"
1315 _vpn_security_policies_make_esp_proposal
() {
1318 # Read the config settings
1319 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1320 if ! vpn_security_policies_read_config
"${name}"; then
1321 return ${EXIT_ERROR}
1327 for cipher
in ${CIPHERS}; do
1329 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1331 if ! isset _cipher
; then
1332 log WARN
"Unsupported cipher: ${cipher}"
1336 if vpn_security_policies_cipher_is_aead
${cipher}; then
1338 for group_type
in ${GROUP_TYPES}; do
1339 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1341 if ! isset _group_type
; then
1342 log WARN
"Unsupported group-type: ${group_type}"
1346 # Put everything together
1347 list_append proposals
"${_cipher}-${_group_type}"
1351 for integrity
in ${INTEGRITIES}; do
1352 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1354 if ! isset _integrity
; then
1355 log WARN
"Unsupported integrity: ${integrity}"
1360 for group_type
in ${GROUP_TYPES}; do
1361 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1363 if ! isset _group_type
; then
1364 log WARN
"Unsupported group-type: ${group_type}"
1368 # Put everything together
1369 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1375 # Returns as a comma-separated list
1376 list_join proposals
,
1379 # List all security policies
1380 vpn_security_policies_list_all
() {
1381 list_directory
"${NETWORK_SHARE_DIR}/vpn/security-policies"
1383 # Add all user-defined policies
1384 vpn_security_policies_list_user
1387 vpn_security_policies_list_user
() {
1388 list_directory
"${NETWORK_CONFIG_DIR}/vpn/security-policies"