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 INTEGRITY KEY_EXCHANGE LIFETIME PFS"
23 VPN_SECURITY_POLICIES_READONLY
="system performance"
25 VPN_DEFAULT_SECURITY_POLICY
="system"
27 declare -A VPN_SUPPORTED_CIPHERS
=(
29 [3DES-CBC
]="168 bit 3DES-EDE-CBC"
32 [AES256-CBC
]="256 bit AES-CBC"
33 [AES192-CBC
]="192 bit AES-CBC"
34 [AES128-CBC
]="128 bit AES-CBC"
37 [AES256-CTR
]="256 bit AES-COUNTER"
38 [AES192-CTR
]="192 bit AES-COUNTER"
39 [AES128-CTR
]="128 bit AES-COUNTER"
42 [AES256-GCM128
]="256 bit AES-GCM with 128 bit ICV"
43 [AES192-GCM128
]="192 bit AES-GCM with 128 bit ICV"
44 [AES128-GCM128
]="128 bit AES-GCM with 128 bit ICV"
45 [AES256-GCM96
]="256 bit AES-GCM with 96 bit ICV"
46 [AES192-GCM96
]="192 bit AES-GCM with 96 bit ICV"
47 [AES128-GCM96
]="128 bit AES-GCM with 96 bit ICV"
48 [AES256-GCM64
]="256 bit AES-GCM with 64 bit ICV"
49 [AES192-GCM64
]="192 bit AES-GCM with 64 bit ICV"
50 [AES128-GCM64
]="128 bit AES-GCM with 64 bit ICV"
53 [AES256-CCM128
]="256 bit AES-CCM with 128 bit ICV"
54 [AES192-CCM128
]="192 bit AES-CCM with 128 bit ICV"
55 [AES128-CCM128
]="128 bit AES-CCM with 128 bit ICV"
56 [AES256-CCM96
]="256 bit AES-CCM with 96 bit ICV"
57 [AES192-CCM96
]="192 bit AES-CCM with 96 bit ICV"
58 [AES128-CCM96
]="128 bit AES-CCM with 96 bit ICV"
59 [AES256-CCM64
]="256 bit AES-CCM with 64 bit ICV"
60 [AES192-CCM64
]="192 bit AES-CCM with 64 bit ICV"
61 [AES128-CCM64
]="128 bit AES-CCM with 64 bit ICV"
64 [CAMELLIA256-CBC
]="256 bit CAMELLIA-CBC"
65 [CAMELLIA192-CBC
]="192 bit CAMELLIA-CBC"
66 [CAMELLIA128-CBC
]="128 bit CAMELLIA-CBC"
69 [CAMELLIA256-CTR
]="256 bit CAMELLIA-COUNTER"
70 [CAMELLIA192-CTR
]="192 bit CAMELLIA-COUNTER"
71 [CAMELLIA128-CTR
]="128 bit CAMELLIA-COUNTER"
74 [CAMELLIA256-GCM128
]="256 bit CAMELLIA-GCM with 128 bit ICV"
75 [CAMELLIA192-GCM128
]="192 bit CAMELLIA-GCM with 128 bit ICV"
76 [CAMELLIA128-GCM128
]="128 bit CAMELLIA-GCM with 128 bit ICV"
77 [CAMELLIA256-GCM96
]="256 bit CAMELLIA-GCM with 96 bit ICV"
78 [CAMELLIA192-GCM96
]="192 bit CAMELLIA-GCM with 96 bit ICV"
79 [CAMELLIA128-GCM96
]="128 bit CAMELLIA-GCM with 96 bit ICV"
80 [CAMELLIA256-GCM64
]="256 bit CAMELLIA-GCM with 64 bit ICV"
81 [CAMELLIA192-GCM64
]="192 bit CAMELLIA-GCM with 64 bit ICV"
82 [CAMELLIA128-GCM64
]="128 bit CAMELLIA-GCM with 64 bit ICV"
85 [CAMELLIA256-CCM128
]="256 bit CAMELLIA-CCM with 128 bit ICV"
86 [CAMELLIA192-CCM128
]="192 bit CAMELLIA-CCM with 128 bit ICV"
87 [CAMELLIA128-CCM128
]="128 bit CAMELLIA-CCM with 128 bit ICV"
88 [CAMELLIA256-CCM96
]="256 bit CAMELLIA-CCM with 96 bit ICV"
89 [CAMELLIA192-CCM96
]="192 bit CAMELLIA-CCM with 96 bit ICV"
90 [CAMELLIA128-CCM96
]="128 bit CAMELLIA-CCM with 96 bit ICV"
91 [CAMELLIA256-CCM64
]="256 bit CAMELLIA-CCM with 64 bit ICV"
92 [CAMELLIA192-CCM64
]="192 bit CAMELLIA-CCM with 64 bit ICV"
93 [CAMELLIA128-CCM64
]="128 bit CAMELLIA-CCM with 64 bit ICV"
96 [CHACHA20-POLY1305
]="256 bit ChaCha20/Poly1305 with 128 bit ICV"
99 [NULL
]="No Encryption"
102 declare -A CIPHER_TO_STRONGSWAN
=(
107 [AES256-CBC
]="aes256"
108 [AES192-CBC
]="aes192"
109 [AES128-CBC
]="aes128"
112 [AES256-CTR
]="aes256ctr"
113 [AES192-CTR
]="aes192ctr"
114 [AES128-CTR
]="aes128ctr"
117 [AES256-GCM128
]="aes256gcm128"
118 [AES192-GCM128
]="aes192gcm128"
119 [AES128-GCM128
]="aes128gcm128"
120 [AES256-GCM96
]="aes256gcm96"
121 [AES192-GCM96
]="aes192gcm96"
122 [AES128-GCM96
]="aes128gcm96"
123 [AES256-GCM64
]="aes256gcm64"
124 [AES192-GCM64
]="aes192gcm64"
125 [AES128-GCM64
]="aes128gcm64"
128 [AES256-CCM128
]="aes256ccm128"
129 [AES192-CCM128
]="aes192ccm128"
130 [AES128-CCM128
]="aes128ccm128"
131 [AES256-CCM96
]="aes256ccm96"
132 [AES192-CCM96
]="aes192ccm96"
133 [AES128-CCM96
]="aes128ccm96"
134 [AES256-CCM64
]="aes256ccm64"
135 [AES192-CCM64
]="aes192ccm64"
136 [AES128-CCM64
]="aes128ccm64"
139 [CAMELLIA256-CBC
]="camellia256"
140 [CAMELLIA192-CBC
]="camellia192"
141 [CAMELLIA128-CBC
]="camellia128"
144 [CAMELLIA256-CTR
]="camellia256ctr"
145 [CAMELLIA192-CTR
]="camellia192ctr"
146 [CAMELLIA128-CTR
]="camellia128ctr"
149 [CAMELLIA256-GCM128
]="camellia256gcm128"
150 [CAMELLIA192-GCM128
]="camellia192gcm128"
151 [CAMELLIA128-GCM128
]="camellia128gcm128"
152 [CAMELLIA256-GCM96
]="camellia256gcm96"
153 [CAMELLIA192-GCM96
]="camellia192gcm96"
154 [CAMELLIA128-GCM96
]="camellia128gcm96"
155 [CAMELLIA256-GCM64
]="camellia256gcm64"
156 [CAMELLIA192-GCM64
]="camellia192gcm64"
157 [CAMELLIA128-GCM64
]="camellia128gcm64"
160 [CAMELLIA256-CCM128
]="camellia256ccm128"
161 [CAMELLIA192-CCM128
]="camellia192ccm128"
162 [CAMELLIA128-CCM128
]="camellia128ccm128"
163 [CAMELLIA256-CCM96
]="camellia256ccm96"
164 [CAMELLIA192-CCM96
]="camellia192ccm96"
165 [CAMELLIA128-CCM96
]="camellia128ccm96"
166 [CAMELLIA256-CCM64
]="camellia256ccm64"
167 [CAMELLIA192-CCM64
]="camellia192ccm64"
168 [CAMELLIA128-CCM64
]="camellia128ccm64"
171 [CHACHA20-POLY1305
]="chacha20poly1305"
177 declare -A VPN_SUPPORTED_INTEGRITY
=(
182 [SHA512
]="512 bit SHA2-HMAC"
183 [SHA384
]="384 bit SHA2-HMAC"
184 [SHA256
]="256 bit SHA2-HMAC"
187 [AES-XCBC
]="AES-XCBC"
188 [AES-CMAC
]="AES-CMAC"
189 [AES256-GMAC
]="256 bit AES-GMAC"
190 [AES192-GMAC
]="192 bit AES-GMAC"
191 [AES128-GMAC
]="128 bit AES-GMAC"
194 declare -A INTEGRITY_TO_STRONGSWAN
=(
206 [AES256-GMAC
]="aes256gmac"
207 [AES192-GMAC
]="aes192gmac"
208 [AES128-GMAC
]="aes128gmac"
211 declare -A VPN_SUPPORTED_GROUP_TYPES
=(
213 [MODP768
]="768 bit Modulo Prime Group"
214 [MODP1024
]="1024 bit Modulo Prime Group"
215 [MODP1536
]="1536 bit Modulo Prime Group"
216 [MODP2048
]="2048 bit Modulo Prime Group"
217 [MODP3072
]="3072 bit Modulo Prime Group"
218 [MODP4096
]="4096 bit Modulo Prime Group"
219 [MODP6144
]="6144 bit Modulo Prime Group"
220 [MODP8192
]="8192 bit Modulo Prime Group"
222 # NIST Elliptic Curve Groups
223 [ECP192
]="192 bit NIST Elliptic Curve Group"
224 [ECP224
]="224 bit NIST Elliptic Curve Group"
225 [ECP256
]="256 bit NIST Elliptic Curve Group"
226 [ECP384
]="384 bit NIST Elliptic Curve Group"
227 [ECP521
]="521 bit NIST Elliptic Curve Group"
229 # Brainpool Elliptic Curve Groups
230 [ECP224BP
]="224 bit Brainpool Elliptic Curve Group"
231 [ECP256BP
]="256 bit Brainpool Elliptic Curve Group"
232 [ECP384BP
]="384 bit Brainpool Elliptic Curve Group"
233 [ECP512BP
]="512 bit Brainpool Elliptic Curve Group"
236 [CURVE25519
]="256 bit Elliptic Curve 25519"
239 declare -A GROUP_TYPE_TO_STRONGSWAN
=(
242 [MODP1024
]="modp1024"
243 [MODP1536
]="modp1536"
244 [MODP2048
]="modp2048"
245 [MODP3072
]="modp3072"
246 [MODP4096
]="modp4096"
247 [MODP6144
]="modp6144"
248 [MODP8192
]="modp8192"
250 # NIST Elliptic Curve Groups
257 # Brainpool Elliptic Curve Groups
258 [ECP224BP
]="ecp224bp"
259 [ECP256BP
]="ecp256bp"
260 [ECP384BP
]="ecp384bp"
261 [ECP512BP
]="ecp512bp"
264 [CURVE25519
]="curve25519"
267 cli_vpn_security_policies
() {
269 local security_policy
271 if vpn_security_policy_exists
${1}; then
277 cipher|compression|integrity|lifetime|pfs|show
)
278 vpn_security_policies_
${key} ${security_policy} "$@"
281 vpn_security_policies_group_type
${security_policy} "$@"
284 vpn_security_policies_key_exchange
${security_policy} "$@"
287 error
"Unrecognized argument: ${key}"
297 vpn_security_policies_new
"$@"
300 vpn_security_policies_destroy
"$@"
303 if [ -n "${action}" ]; then
304 error
"Unrecognized argument: '${action}'"
312 # This functions checks if a policy is readonly
313 # returns true when yes and false when no
314 vpn_security_policies_check_readonly
() {
315 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
322 # This function writes all values to a via ${name} specificated vpn security policy configuration file
323 vpn_security_policies_write_config
() {
328 if ! vpn_security_policy_exists
"${name}"; then
329 log ERROR
"No such vpn security policy: ${name}"
333 if vpn_security_policies_check_readonly
"${name}"; then
334 log ERROR
"The ${name} vpn security policy cannot be changed."
338 local path
="$(vpn_security_policies_path "${name}")"
339 if [ ! -w ${path} ]; then
340 log ERROR
"${path} is not writeable"
344 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
345 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
349 if ! vpn_security_policies_reload
${name}; then
350 log WARNING
"Could not reload the IPsec connection using this security policy"
355 # reload IPsec connections using a special policy
356 vpn_security_policies_reload
() {
360 for connection
in $
(ipsec_list_connections
); do
361 local SECURITY_POLICY ENABLED
363 if ! ipsec_connection_read_config
"${connection}" "SECURITY_POLICY"; then
367 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED
; then
368 if ! ipsec_connection_to_strongswan
"${connection}"; then
369 log ERROR
"Could not generate strongswan config for ${connnection}"
374 ipsec_strongswan_load
377 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
378 vpn_security_policies_write_config_key
() {
387 if ! vpn_security_policy_exists
"${name}"; then
388 log ERROR
"No such vpn security policy: ${name}"
392 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
394 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
396 # Read the config settings
397 if ! vpn_security_policies_read_config
"${name}"; then
401 # Set the key to a new value
402 assign
"${key}" "${value}"
404 if ! vpn_security_policies_write_config
"${name}"; then
411 # Reads one or more keys out of a settings file or all if no key is provided.
412 vpn_security_policies_read_config
() {
418 if ! vpn_security_policy_exists
"${name}"; then
419 log ERROR
"No such vpn security policy: ${name}"
425 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
426 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
428 list_append args
"$@"
431 local path
="$(vpn_security_policies_path ${name})"
433 if ! settings_read
"${path}" ${args}; then
434 log ERROR
"Could not read settings for vpn security policy ${name}"
439 # Returns the path to a the configuration fora given name
440 vpn_security_policies_path
() {
445 if vpn_security_policies_check_readonly
"${name}"; then
446 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
448 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
452 # Print the content of a vpn security policy configuration file in a nice way
453 vpn_security_policies_show
() {
458 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
459 if ! vpn_security_policies_read_config
${name}; then
463 cli_print_fmt1
0 "Security Policy: ${name}"
466 # This could be done in a loop but a loop is much more complicated
467 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
468 cli_print_fmt1
1 "Ciphers:"
470 for cipher
in ${CIPHER}; do
471 cli_print_fmt1
2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
475 cli_print_fmt1
1 "Integrity:"
477 for integrity
in ${INTEGRITY}; do
478 cli_print_fmt1
2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
482 cli_print_fmt1
1 "Group Types:"
484 for group_type
in ${GROUP_TYPE}; do
485 cli_print_fmt1
2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
489 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
492 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
493 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
495 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
500 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
502 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
507 if enabled COMPRESSION
; then
508 cli_print_fmt1
1 "Compression:" "enabled"
510 cli_print_fmt1
1 "Compression:" "disabled"
515 # This function checks if a vpn security policy exists
516 # Returns True when yes and false when not
517 vpn_security_policy_exists
() {
522 local path
=$
(vpn_security_policies_path
"${name}")
524 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
528 # This function parses the parameters for the 'cipher' command
529 vpn_security_policies_cipher
(){
533 if [ $# -eq 0 ]; then
534 log ERROR
"You must pass at least one value after cipher"
539 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
543 # Remove duplicated entries to proceed the list safely
544 CIPHER
="$(list_unique ${CIPHER})"
547 local ciphers_removed
550 while [ $# -gt 0 ]; do
555 list_append ciphers_added
"${arg:1}"
558 list_append ciphers_removed
"${arg:1}"
561 list_append ciphers_set
"${arg}"
564 error
"Invalid argument: ${arg}"
571 # Check if the user is trying a mixed operation
572 if ! list_is_empty ciphers_set
&& (! list_is_empty ciphers_added ||
! list_is_empty ciphers_removed
); then
573 error
"You cannot reset the cipher list and add or remove ciphers at the same time"
577 # Set new cipher list
578 if ! list_is_empty ciphers_set
; then
579 # Check if all ciphers are valid
581 for cipher
in ${ciphers_set}; do
582 if ! vpn_security_policies_cipher_supported
${cipher}; then
583 error
"Unsupported cipher: ${cipher}"
588 CIPHER
="${ciphers_set}"
590 # Perform incremental updates
594 # Perform all removals
595 for cipher
in ${ciphers_removed}; do
596 if ! list_remove CIPHER
${cipher}; then
597 warning
"${cipher} was not on the list and could not be removed"
601 for cipher
in ${ciphers_added}; do
602 if vpn_security_policies_cipher_supported
${cipher}; then
603 if ! list_append_unique CIPHER
${cipher}; then
604 warning
"${cipher} is already on the cipher list"
607 warning
"${cipher} is unknown or unsupported and could not be added"
612 # Check if the list contain at least one valid cipher
613 if list_is_empty CIPHER
; then
614 error
"Cannot save an empty cipher list"
619 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
620 log ERROR
"The changes for the vpn security policy ${name} could not be written."
623 cli_headline
1 "Current cipher list for ${name}:"
624 for cipher
in ${CIPHER}; do
625 cli_print_fmt1
1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
629 # This function parses the parameters for the 'compression' command
630 vpn_security_policies_compression
(){
634 # Check if we get only one argument after compression <name>
635 if [ ! $# -eq 2 ]; then
636 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
640 if ! isbool value
; then
641 # We suggest only two values to avoid overburding the user.
642 log ERROR
"Invalid Argument ${value}"
646 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
649 # This function parses the parameters for the 'group-type' command
650 vpn_security_policies_group_type
(){
654 if [ $# -eq 0 ]; then
655 log ERROR
"You must pass at least one value after group-type"
660 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
664 # Remove duplicated entries to proceed the list safely
665 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
667 local group_types_added
668 local group_types_removed
669 local group_types_set
671 while [ $# -gt 0 ]; do
676 list_append group_types_added
"${arg:1}"
679 list_append group_types_removed
"${arg:1}"
682 list_append group_types_set
"${arg}"
685 error
"Invalid argument: ${arg}"
692 # Check if the user is trying a mixed operation
693 if ! list_is_empty group_types_set
&& (! list_is_empty group_types_added ||
! list_is_empty group_types_removed
); then
694 error
"You cannot reset the group type list and add or remove group types at the same time"
698 # Set new group type list
699 if ! list_is_empty group_types_set
; then
700 # Check if all group types are valid
702 for group_type
in ${group_types_set}; do
703 if ! vpn_security_policies_group_type_supported
${group_type}; then
704 error
"Unsupported group type: ${group_type}"
709 GROUP_TYPE
="${group_types_set}"
711 # Perform incremental updates
715 # Perform all removals
716 for group_type
in ${group_types_removed}; do
717 if ! list_remove GROUP_TYPE
${group_type}; then
718 warning
"${group_type} was not on the list and could not be removed"
722 for group_type
in ${group_types_added}; do
723 if vpn_security_policies_group_type_supported
${group_type}; then
724 if ! list_append_unique GROUP_TYPE
${group_type}; then
725 warning
"${group_type} is already on the group type list"
728 warning
"${group_type} is unknown or unsupported and could not be added"
733 # Check if the list contain at least one valid group_type
734 if list_is_empty GROUP_TYPE
; then
735 error
"Cannot save an empty group type list"
740 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
741 log ERROR
"The changes for the vpn security policy ${name} could not be written."
744 cli_headline
1 "Current group type list for ${name}:"
745 for group_type
in ${GROUP_TYPE}; do
746 cli_print_fmt1
1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
750 # This function parses the parameters for the 'integrity' command
751 vpn_security_policies_integrity
(){
755 if [ $# -eq 0 ]; then
756 log ERROR
"You must pass at least one value after integrity"
761 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
765 # Remove duplicated entries to proceed the list safely
766 INTEGRITY
="$(list_unique ${INTEGRITY})"
768 local integritys_added
769 local integritys_removed
772 while [ $# -gt 0 ]; do
777 list_append integritys_added
"${arg:1}"
780 list_append integritys_removed
"${arg:1}"
783 list_append integritys_set
"${arg}"
786 error
"Invalid argument: ${arg}"
793 # Check if the user is trying a mixed operation
794 if ! list_is_empty integritys_set
&& (! list_is_empty integritys_added ||
! list_is_empty integritys_removed
); then
795 error
"You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
799 # Set new integrity list
800 if ! list_is_empty integritys_set
; then
801 # Check if all integrity hashes are valid
803 for integrity
in ${integritys_set}; do
804 if ! vpn_security_policies_integrity_supported
${integrity}; then
805 error
"Unsupported integrity hash: ${integrity}"
810 INTEGRITY
="${integritys_set}"
812 # Perform incremental updates
816 # Perform all removals
817 for integrity
in ${integritys_removed}; do
818 if ! list_remove INTEGRITY
${integrity}; then
819 warning
"${integrity} was not on the list and could not be removed"
823 for integrity
in ${integritys_added}; do
824 if vpn_security_policies_integrity_supported
${integrity}; then
825 if ! list_append_unique INTEGRITY
${integrity}; then
826 warning
"${integrity} is already on the integrity list"
829 warning
"${integrity} is unknown or unsupported and could not be added"
834 # Check if the list contain at least one valid integrity
835 if list_is_empty INTEGRITY
; then
836 error
"Cannot save an empty integrity hashes list"
841 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
842 log ERROR
"The changes for the vpn security policy ${name} could not be written."
845 cli_headline
1 "Current integrity hashes list for ${name}:"
846 for integrity
in ${INTEGRITY}; do
847 cli_print_fmt1
1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}"
851 # This function parses the parameters for the 'key-exchange' command
852 vpn_security_policies_key_exchange
() {
856 # Check if we get only one argument after key-exchange <name>
857 if [ ! $# -eq 2 ]; then
858 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
862 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
863 log ERROR
"Invalid Argument ${value}"
867 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
870 # This function parses the parameters for the 'lifetime' command.
871 vpn_security_policies_lifetime
(){
877 # Check if we get only one argument after lifetime <name>
878 if [ ! $# -ge 1 ]; then
879 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"
883 if ! isinteger value
; then
884 value
=$
(parse_time
"$@")
885 if [ ! $?
-eq 0 ]; then
886 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
891 if [ ${value} -le 0 ]; then
892 log ERROR
"The passed time value must be in the sum greater zero seconds."
896 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
899 # This function parses the parameters for the 'pfs' command
900 vpn_security_policies_pfs
(){
904 # Check if we get only one argument after pfs <name>
905 if [ ! $# -eq 2 ]; then
906 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
910 if [ ! $# -eq 2 ] ||
! isbool value
; then
911 # We suggest only two values to avoid overburding the user.
912 log ERROR
"Invalid Argument ${value}"
916 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
919 # This function checks if a vpn security policy name is valid
920 # Allowed are only A-Za-z0-9
921 vpn_security_policies_check_name
() {
926 [[ ${name} =~
[^
[:alnum
:]$
] ]]
929 # Function that creates based on the paramters one ore more new vpn security policies
930 vpn_security_policies_new
() {
931 if [ $# -gt 1 ]; then
932 error
"Too many arguments"
937 if ! isset name
; then
938 error
"Please provide a name"
942 # Check for duplicates
943 if vpn_security_policy_exists
"${name}"; then
944 error
"The VPN security policy with name ${name} already exists"
948 # Check if name is valid
949 if vpn_security_policies_check_name
"${name}"; then
950 error
"'${name}' contains illegal characters"
954 # Check if we have a read-only policy with the same name
955 if vpn_security_policies_check_readonly
"${name}"; then
956 error
"The VPN security policy ${name} is read-only"
960 # Check if our source policy exists
961 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
962 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
966 log DEBUG
"Creating VPN Security Policy ${name}"
968 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
969 "$(vpn_security_policies_path ${name})"; then
970 log INFO
"VPN Security Policy ${name} successfully created"
972 log ERROR
"Could not create VPN Security Policy ${name}"
976 # Show the newly created policy
977 vpn_security_policies_show
"${name}"
980 # Function that deletes based on the passed parameters one ore more vpn security policies
981 vpn_security_policies_destroy
() {
984 if ! vpn_security_policy_exists
${name}; then
985 log ERROR
"The vpn security policy ${name} does not exist."
989 if vpn_security_policies_check_readonly
${name}; then
990 log ERROR
"The vpn security policy ${name} cannot be deleted."
994 log DEBUG
"Deleting vpn security policy ${name}"
995 settings_remove $
(vpn_security_policies_path
${name})
998 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
1002 vpn_security_policies_cipher_supported
() {
1005 list_match
${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1009 vpn_security_policies_group_type_supported
() {
1010 local group_type
=${1}
1012 list_match
${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1015 vpn_security_policies_integrity_supported
() {
1016 local integrity
=${1}
1018 list_match
${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]}
1021 vpn_security_policies_cipher_is_aead
() {
1024 # All CCM and GCM ciphers are AEAD
1025 string_match
"[CG]CM" "${cipher}"
1028 vpn_security_policies_make_ike_proposal
() {
1031 if ! vpn_security_policy_exists
${name}; then
1032 return ${EXIT_ERROR}
1035 local config_path
="$(vpn_security_policies_path ${name})"
1036 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
1038 # Get data from cache if possible
1039 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1040 fread
"${cache_path}"
1044 # No or invalid cache data found
1045 local proposal
=$
(_vpn_security_policies_make_ike_proposal
"${name}")
1047 # Write proposal to cache
1048 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1049 log WARNING
"Could not write to cache: ${cache_path}"
1055 _vpn_security_policies_make_ike_proposal
() {
1058 # Read the config settings
1059 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1060 if ! vpn_security_policies_read_config
"${name}"; then
1061 return ${EXIT_ERROR}
1067 for cipher
in ${CIPHER}; do
1069 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1071 if ! isset _cipher
; then
1072 log WARN
"Unsupported cipher: ${cipher}"
1077 for integrity
in ${INTEGRITY}; do
1078 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1080 if ! isset _integrity
; then
1081 log WARN
"Unsupported integrity: ${integrity}"
1086 for group_type
in ${GROUP_TYPE}; do
1087 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1089 if ! isset _group_type
; then
1090 log WARN
"Unsupported group-type: ${group_type}"
1094 # Put everything together
1095 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1100 # Returns as a comma-separated list
1101 list_join proposals
,
1104 vpn_security_policies_make_esp_proposal
() {
1107 if ! vpn_security_policy_exists
${name}; then
1108 return ${EXIT_ERROR}
1111 local config_path
="$(vpn_security_policies_path ${name})"
1112 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1114 # Get data from cache if possible
1115 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1116 fread
"${cache_path}"
1120 # No or invalid cache data found
1121 local proposal
=$
(_vpn_security_policies_make_esp_proposal
"${name}")
1123 # Write proposal to cache
1124 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1125 log WARNING
"Could not write to cache: ${cache_path}"
1131 _vpn_security_policies_make_esp_proposal
() {
1134 # Read the config settings
1135 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1136 if ! vpn_security_policies_read_config
"${name}"; then
1137 return ${EXIT_ERROR}
1143 for cipher
in ${CIPHER}; do
1145 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1147 if ! isset _cipher
; then
1148 log WARN
"Unsupported cipher: ${cipher}"
1152 if vpn_security_policies_cipher_is_aead
${cipher}; then
1154 for group_type
in ${GROUP_TYPE}; do
1155 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1157 if ! isset _group_type
; then
1158 log WARN
"Unsupported group-type: ${group_type}"
1162 # Put everything together
1163 list_append proposals
"${_cipher}-${_group_type}"
1167 for integrity
in ${INTEGRITY}; do
1168 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1170 if ! isset _integrity
; then
1171 log WARN
"Unsupported integrity: ${integrity}"
1176 for group_type
in ${GROUP_TYPE}; do
1177 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1179 if ! isset _group_type
; then
1180 log WARN
"Unsupported group-type: ${group_type}"
1184 # Put everything together
1185 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1191 # Returns as a comma-separated list
1192 list_join proposals
,
1195 # List all security policies
1196 vpn_security_policies_list_all
() {
1197 list_directory
"${NETWORK_SHARE_DIR}/vpn/security-policies"
1199 # Add all user-defined policies
1200 vpn_security_policies_list_user
1203 vpn_security_policies_list_user
() {
1204 list_directory
"${NETWORK_CONFIG_DIR}/vpn/security-policies"