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 [NULL
]="No Encryption"
99 declare -A CIPHER_TO_STRONGSWAN
=(
104 [AES256-CBC
]="aes256"
105 [AES192-CBC
]="aes192"
106 [AES128-CBC
]="aes128"
109 [AES256-CTR
]="aes256ctr"
110 [AES192-CTR
]="aes192ctr"
111 [AES128-CTR
]="aes128ctr"
114 [AES256-GCM128
]="aes256gcm128"
115 [AES192-GCM128
]="aes192gcm128"
116 [AES128-GCM128
]="aes128gcm128"
117 [AES256-GCM96
]="aes256gcm96"
118 [AES192-GCM96
]="aes192gcm96"
119 [AES128-GCM96
]="aes128gcm96"
120 [AES256-GCM64
]="aes256gcm64"
121 [AES192-GCM64
]="aes192gcm64"
122 [AES128-GCM64
]="aes128gcm64"
125 [AES256-CCM128
]="aes256ccm128"
126 [AES192-CCM128
]="aes192ccm128"
127 [AES128-CCM128
]="aes128ccm128"
128 [AES256-CCM96
]="aes256ccm96"
129 [AES192-CCM96
]="aes192ccm96"
130 [AES128-CCM96
]="aes128ccm96"
131 [AES256-CCM64
]="aes256ccm64"
132 [AES192-CCM64
]="aes192ccm64"
133 [AES128-CCM64
]="aes128ccm64"
136 [CAMELLIA256-CBC
]="camellia256"
137 [CAMELLIA192-CBC
]="camellia192"
138 [CAMELLIA128-CBC
]="camellia128"
141 [CAMELLIA256-CTR
]="camellia256ctr"
142 [CAMELLIA192-CTR
]="camellia192ctr"
143 [CAMELLIA128-CTR
]="camellia128ctr"
146 [CAMELLIA256-GCM128
]="camellia256gcm128"
147 [CAMELLIA192-GCM128
]="camellia192gcm128"
148 [CAMELLIA128-GCM128
]="camellia128gcm128"
149 [CAMELLIA256-GCM96
]="camellia256gcm96"
150 [CAMELLIA192-GCM96
]="camellia192gcm96"
151 [CAMELLIA128-GCM96
]="camellia128gcm96"
152 [CAMELLIA256-GCM64
]="camellia256gcm64"
153 [CAMELLIA192-GCM64
]="camellia192gcm64"
154 [CAMELLIA128-GCM64
]="camellia128gcm64"
157 [CAMELLIA256-CCM128
]="camellia256ccm128"
158 [CAMELLIA192-CCM128
]="camellia192ccm128"
159 [CAMELLIA128-CCM128
]="camellia128ccm128"
160 [CAMELLIA256-CCM96
]="camellia256ccm96"
161 [CAMELLIA192-CCM96
]="camellia192ccm96"
162 [CAMELLIA128-CCM96
]="camellia128ccm96"
163 [CAMELLIA256-CCM64
]="camellia256ccm64"
164 [CAMELLIA192-CCM64
]="camellia192ccm64"
165 [CAMELLIA128-CCM64
]="camellia128ccm64"
171 declare -A VPN_SUPPORTED_INTEGRITY
=(
176 [SHA512
]="512 bit SHA2-HMAC"
177 [SHA384
]="384 bit SHA2-HMAC"
178 [SHA256
]="256 bit SHA2-HMAC"
181 [AES-XCBC
]="AES-XCBC"
182 [AES-CMAC
]="AES-CMAC"
183 [AES256-GMAC
]="256 bit AES-GMAC"
184 [AES192-GMAC
]="192 bit AES-GMAC"
185 [AES128-GMAC
]="128 bit AES-GMAC"
188 declare -A INTEGRITY_TO_STRONGSWAN
=(
200 [AES256-GMAC
]="aes256gmac"
201 [AES192-GMAC
]="aes192gmac"
202 [AES128-GMAC
]="aes128gmac"
205 declare -A VPN_SUPPORTED_GROUP_TYPES
=(
207 [MODP768
]="768 bit Modulo Prime Group"
208 [MODP1024
]="1024 bit Modulo Prime Group"
209 [MODP1536
]="1536 bit Modulo Prime Group"
210 [MODP2048
]="2048 bit Modulo Prime Group"
211 [MODP3072
]="3072 bit Modulo Prime Group"
212 [MODP4096
]="4096 bit Modulo Prime Group"
213 [MODP6144
]="6144 bit Modulo Prime Group"
214 [MODP8192
]="8192 bit Modulo Prime Group"
216 # NIST Elliptic Curve Groups
217 [ECP192
]="192 bit NIST Elliptic Curve Group"
218 [ECP224
]="224 bit NIST Elliptic Curve Group"
219 [ECP256
]="256 bit NIST Elliptic Curve Group"
220 [ECP384
]="384 bit NIST Elliptic Curve Group"
221 [ECP521
]="521 bit NIST Elliptic Curve Group"
223 # Brainpool Elliptic Curve Groups
224 [ECP224BP
]="224 bit Brainpool Elliptic Curve Group"
225 [ECP256BP
]="256 bit Brainpool Elliptic Curve Group"
226 [ECP384BP
]="384 bit Brainpool Elliptic Curve Group"
227 [ECP512BP
]="512 bit Brainpool Elliptic Curve Group"
230 [CURVE25519
]="256 bit Elliptic Curve 25519"
233 declare -A GROUP_TYPE_TO_STRONGSWAN
=(
236 [MODP1024
]="modp1024"
237 [MODP1536
]="modp1536"
238 [MODP2048
]="modp2048"
239 [MODP3072
]="modp3072"
240 [MODP4096
]="modp4096"
241 [MODP6144
]="modp6144"
242 [MODP8192
]="modp8192"
244 # NIST Elliptic Curve Groups
251 # Brainpool Elliptic Curve Groups
252 [ECP224BP
]="ecp224bp"
253 [ECP256BP
]="ecp256bp"
254 [ECP384BP
]="ecp384bp"
255 [ECP512BP
]="ecp512bp"
258 [CURVE25519
]="curve25519"
261 cli_vpn_security_policies
() {
263 local security_policy
265 if vpn_security_policy_exists
${1}; then
271 cipher|compression|integrity|lifetime|pfs|show
)
272 vpn_security_policies_
${key} ${security_policy} "$@"
275 vpn_security_policies_group_type
${security_policy} "$@"
278 vpn_security_policies_key_exchange
${security_policy} "$@"
281 error
"Unrecognized argument: ${key}"
291 vpn_security_policies_new
"$@"
294 vpn_security_policies_destroy
"$@"
297 if [ -n "${action}" ]; then
298 error
"Unrecognized argument: '${action}'"
306 # This functions checks if a policy is readonly
307 # returns true when yes and false when no
308 vpn_security_policies_check_readonly
() {
309 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
316 # This function writes all values to a via ${name} specificated vpn security policy configuration file
317 vpn_security_policies_write_config
() {
322 if ! vpn_security_policy_exists
"${name}"; then
323 log ERROR
"No such vpn security policy: ${name}"
327 if vpn_security_policies_check_readonly
"${name}"; then
328 log ERROR
"The ${name} vpn security policy cannot be changed."
332 local path
="$(vpn_security_policies_path "${name}")"
333 if [ ! -w ${path} ]; then
334 log ERROR
"${path} is not writeable"
338 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
339 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
343 if ! vpn_security_policies_reload
${name}; then
344 log WARNING
"Could not reload the IPsec connection using this security policy"
349 # reload IPsec connections using a special policy
350 vpn_security_policies_reload
() {
354 for connection
in $
(ipsec_list_connections
); do
355 local SECURITY_POLICY ENABLED
357 if ! ipsec_connection_read_config
"${connection}" "SECURITY_POLICY"; then
361 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED
; then
362 if ! ipsec_connection_to_strongswan
"${connection}"; then
363 log ERROR
"Could not generate strongswan config for ${connnection}"
368 ipsec_strongswan_load
371 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
372 vpn_security_policies_write_config_key
() {
381 if ! vpn_security_policy_exists
"${name}"; then
382 log ERROR
"No such vpn security policy: ${name}"
386 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
388 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
390 # Read the config settings
391 if ! vpn_security_policies_read_config
"${name}"; then
395 # Set the key to a new value
396 assign
"${key}" "${value}"
398 if ! vpn_security_policies_write_config
"${name}"; then
405 # Reads one or more keys out of a settings file or all if no key is provided.
406 vpn_security_policies_read_config
() {
412 if ! vpn_security_policy_exists
"${name}"; then
413 log ERROR
"No such vpn security policy: ${name}"
419 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
420 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
422 list_append args
"$@"
425 local path
="$(vpn_security_policies_path ${name})"
427 if ! settings_read
"${path}" ${args}; then
428 log ERROR
"Could not read settings for vpn security policy ${name}"
433 # Returns the path to a the configuration fora given name
434 vpn_security_policies_path
() {
439 if vpn_security_policies_check_readonly
"${name}"; then
440 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
442 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
446 # Print the content of a vpn security policy configuration file in a nice way
447 vpn_security_policies_show
() {
452 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
453 if ! vpn_security_policies_read_config
${name}; then
457 cli_print_fmt1
0 "Security Policy: ${name}"
460 # This could be done in a loop but a loop is much more complicated
461 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
462 cli_print_fmt1
1 "Ciphers:"
464 for cipher
in ${CIPHER}; do
465 cli_print_fmt1
2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
469 cli_print_fmt1
1 "Integrity:"
471 for integrity
in ${INTEGRITY}; do
472 cli_print_fmt1
2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
476 cli_print_fmt1
1 "Group Types:"
478 for group_type
in ${GROUP_TYPE}; do
479 cli_print_fmt1
2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
483 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
486 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
487 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
489 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
494 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
496 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
501 if enabled COMPRESSION
; then
502 cli_print_fmt1
1 "Compression:" "enabled"
504 cli_print_fmt1
1 "Compression:" "disabled"
509 # This function checks if a vpn security policy exists
510 # Returns True when yes and false when not
511 vpn_security_policy_exists
() {
516 local path
=$
(vpn_security_policies_path
"${name}")
518 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
522 # This function parses the parameters for the 'cipher' command
523 vpn_security_policies_cipher
(){
527 if [ $# -eq 0 ]; then
528 log ERROR
"You must pass at least one value after cipher"
533 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
537 # Remove duplicated entries to proceed the list safely
538 CIPHER
="$(list_unique ${CIPHER})"
541 local ciphers_removed
544 while [ $# -gt 0 ]; do
549 list_append ciphers_added
"${arg:1}"
552 list_append ciphers_removed
"${arg:1}"
555 list_append ciphers_set
"${arg}"
558 error
"Invalid argument: ${arg}"
565 # Check if the user is trying a mixed operation
566 if ! list_is_empty ciphers_set
&& (! list_is_empty ciphers_added ||
! list_is_empty ciphers_removed
); then
567 error
"You cannot reset the cipher list and add or remove ciphers at the same time"
571 # Set new cipher list
572 if ! list_is_empty ciphers_set
; then
573 # Check if all ciphers are valid
575 for cipher
in ${ciphers_set}; do
576 if ! vpn_security_policies_cipher_supported
${cipher}; then
577 error
"Unsupported cipher: ${cipher}"
582 CIPHER
="${ciphers_set}"
584 # Perform incremental updates
588 # Perform all removals
589 for cipher
in ${ciphers_removed}; do
590 if ! list_remove CIPHER
${cipher}; then
591 warning
"${cipher} was not on the list and could not be removed"
595 for cipher
in ${ciphers_added}; do
596 if vpn_security_policies_cipher_supported
${cipher}; then
597 if ! list_append_unique CIPHER
${cipher}; then
598 warning
"${cipher} is already on the cipher list"
601 warning
"${cipher} is unknown or unsupported and could not be added"
606 # Check if the list contain at least one valid cipher
607 if list_is_empty CIPHER
; then
608 error
"Cannot save an empty cipher list"
613 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
614 log ERROR
"The changes for the vpn security policy ${name} could not be written."
617 cli_headline
1 "Current cipher list for ${name}:"
618 for cipher
in ${CIPHER}; do
619 cli_print_fmt1
1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
623 # This function parses the parameters for the 'compression' command
624 vpn_security_policies_compression
(){
628 # Check if we get only one argument after compression <name>
629 if [ ! $# -eq 2 ]; then
630 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
634 if ! isbool value
; then
635 # We suggest only two values to avoid overburding the user.
636 log ERROR
"Invalid Argument ${value}"
640 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
643 # This function parses the parameters for the 'group-type' command
644 vpn_security_policies_group_type
(){
648 if [ $# -eq 0 ]; then
649 log ERROR
"You must pass at least one value after group-type"
654 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
658 # Remove duplicated entries to proceed the list safely
659 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
661 local group_types_added
662 local group_types_removed
663 local group_types_set
665 while [ $# -gt 0 ]; do
670 list_append group_types_added
"${arg:1}"
673 list_append group_types_removed
"${arg:1}"
676 list_append group_types_set
"${arg}"
679 error
"Invalid argument: ${arg}"
686 # Check if the user is trying a mixed operation
687 if ! list_is_empty group_types_set
&& (! list_is_empty group_types_added ||
! list_is_empty group_types_removed
); then
688 error
"You cannot reset the group type list and add or remove group types at the same time"
692 # Set new group type list
693 if ! list_is_empty group_types_set
; then
694 # Check if all group types are valid
696 for group_type
in ${group_types_set}; do
697 if ! vpn_security_policies_group_type_supported
${group_type}; then
698 error
"Unsupported group type: ${group_type}"
703 GROUP_TYPE
="${group_types_set}"
705 # Perform incremental updates
709 # Perform all removals
710 for group_type
in ${group_types_removed}; do
711 if ! list_remove GROUP_TYPE
${group_type}; then
712 warning
"${group_type} was not on the list and could not be removed"
716 for group_type
in ${group_types_added}; do
717 if vpn_security_policies_group_type_supported
${group_type}; then
718 if ! list_append_unique GROUP_TYPE
${group_type}; then
719 warning
"${group_type} is already on the group type list"
722 warning
"${group_type} is unknown or unsupported and could not be added"
727 # Check if the list contain at least one valid group_type
728 if list_is_empty GROUP_TYPE
; then
729 error
"Cannot save an empty group type list"
734 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
735 log ERROR
"The changes for the vpn security policy ${name} could not be written."
738 cli_headline
1 "Current group type list for ${name}:"
739 for group_type
in ${GROUP_TYPE}; do
740 cli_print_fmt1
1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
744 # This function parses the parameters for the 'integrity' command
745 vpn_security_policies_integrity
(){
749 if [ $# -eq 0 ]; then
750 log ERROR
"You must pass at least one value after integrity"
755 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
759 # Remove duplicated entries to proceed the list safely
760 INTEGRITY
="$(list_unique ${INTEGRITY})"
762 local integritys_added
763 local integritys_removed
766 while [ $# -gt 0 ]; do
771 list_append integritys_added
"${arg:1}"
774 list_append integritys_removed
"${arg:1}"
777 list_append integritys_set
"${arg}"
780 error
"Invalid argument: ${arg}"
787 # Check if the user is trying a mixed operation
788 if ! list_is_empty integritys_set
&& (! list_is_empty integritys_added ||
! list_is_empty integritys_removed
); then
789 error
"You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
793 # Set new integrity list
794 if ! list_is_empty integritys_set
; then
795 # Check if all integrity hashes are valid
797 for integrity
in ${integritys_set}; do
798 if ! vpn_security_policies_integrity_supported
${integrity}; then
799 error
"Unsupported integrity hash: ${integrity}"
804 INTEGRITY
="${integritys_set}"
806 # Perform incremental updates
810 # Perform all removals
811 for integrity
in ${integritys_removed}; do
812 if ! list_remove INTEGRITY
${integrity}; then
813 warning
"${integrity} was not on the list and could not be removed"
817 for integrity
in ${integritys_added}; do
818 if vpn_security_policies_integrity_supported
${integrity}; then
819 if ! list_append_unique INTEGRITY
${integrity}; then
820 warning
"${integrity} is already on the integrity list"
823 warning
"${integrity} is unknown or unsupported and could not be added"
828 # Check if the list contain at least one valid integrity
829 if list_is_empty INTEGRITY
; then
830 error
"Cannot save an empty integrity hashes list"
835 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
836 log ERROR
"The changes for the vpn security policy ${name} could not be written."
839 cli_headline
1 "Current integrity hashes list for ${name}:"
840 for integrity
in ${INTEGRITY}; do
841 cli_print_fmt1
1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}"
845 # This function parses the parameters for the 'key-exchange' command
846 vpn_security_policies_key_exchange
() {
850 # Check if we get only one argument after key-exchange <name>
851 if [ ! $# -eq 2 ]; then
852 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
856 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
857 log ERROR
"Invalid Argument ${value}"
861 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
864 # This function parses the parameters for the 'lifetime' command.
865 vpn_security_policies_lifetime
(){
871 # Check if we get only one argument after lifetime <name>
872 if [ ! $# -ge 1 ]; then
873 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"
877 if ! isinteger value
; then
878 value
=$
(parse_time
"$@")
879 if [ ! $?
-eq 0 ]; then
880 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
885 if [ ${value} -le 0 ]; then
886 log ERROR
"The passed time value must be in the sum greater zero seconds."
890 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
893 # This function parses the parameters for the 'pfs' command
894 vpn_security_policies_pfs
(){
898 # Check if we get only one argument after pfs <name>
899 if [ ! $# -eq 2 ]; then
900 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
904 if [ ! $# -eq 2 ] ||
! isbool value
; then
905 # We suggest only two values to avoid overburding the user.
906 log ERROR
"Invalid Argument ${value}"
910 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
913 # This function checks if a vpn security policy name is valid
914 # Allowed are only A-Za-z0-9
915 vpn_security_policies_check_name
() {
920 [[ ${name} =~
[^
[:alnum
:]$
] ]]
923 # Function that creates based on the paramters one ore more new vpn security policies
924 vpn_security_policies_new
() {
925 if [ $# -gt 1 ]; then
926 error
"Too many arguments"
931 if ! isset name
; then
932 error
"Please provide a name"
936 # Check for duplicates
937 if vpn_security_policy_exists
"${name}"; then
938 error
"The VPN security policy with name ${name} already exists"
942 # Check if name is valid
943 if vpn_security_policies_check_name
"${name}"; then
944 error
"'${name}' contains illegal characters"
948 # Check if we have a read-only policy with the same name
949 if vpn_security_policies_check_readonly
"${name}"; then
950 error
"The VPN security policy ${name} is read-only"
954 # Check if our source policy exists
955 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
956 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
960 log DEBUG
"Creating VPN Security Policy ${name}"
962 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
963 "$(vpn_security_policies_path ${name})"; then
964 log INFO
"VPN Security Policy ${name} successfully created"
966 log ERROR
"Could not create VPN Security Policy ${name}"
970 # Show the newly created policy
971 vpn_security_policies_show
"${name}"
974 # Function that deletes based on the passed parameters one ore more vpn security policies
975 vpn_security_policies_destroy
() {
978 if ! vpn_security_policy_exists
${name}; then
979 log ERROR
"The vpn security policy ${name} does not exist."
983 if vpn_security_policies_check_readonly
${name}; then
984 log ERROR
"The vpn security policy ${name} cannot be deleted."
988 log DEBUG
"Deleting vpn security policy ${name}"
989 settings_remove $
(vpn_security_policies_path
${name})
992 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
996 vpn_security_policies_cipher_supported
() {
999 list_match
${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1003 vpn_security_policies_group_type_supported
() {
1004 local group_type
=${1}
1006 list_match
${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1009 vpn_security_policies_integrity_supported
() {
1010 local integrity
=${1}
1012 list_match
${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]}
1015 vpn_security_policies_cipher_is_aead
() {
1018 # All CCM and GCM ciphers are AEAD
1019 string_match
"[CG]CM" "${cipher}"
1022 vpn_security_policies_make_ike_proposal
() {
1025 if ! vpn_security_policy_exists
${name}; then
1026 return ${EXIT_ERROR}
1029 local config_path
="$(vpn_security_policies_path ${name})"
1030 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
1032 # Get data from cache if possible
1033 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1034 fread
"${cache_path}"
1038 # No or invalid cache data found
1039 local proposal
=$
(_vpn_security_policies_make_ike_proposal
"${name}")
1041 # Write proposal to cache
1042 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1043 log WARNING
"Could not write to cache: ${cache_path}"
1049 _vpn_security_policies_make_ike_proposal
() {
1052 # Read the config settings
1053 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1054 if ! vpn_security_policies_read_config
"${name}"; then
1055 return ${EXIT_ERROR}
1061 for cipher
in ${CIPHER}; do
1063 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1065 if ! isset _cipher
; then
1066 log WARN
"Unsupported cipher: ${cipher}"
1071 for integrity
in ${INTEGRITY}; do
1072 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1074 if ! isset _integrity
; then
1075 log WARN
"Unsupported integrity: ${integrity}"
1080 for group_type
in ${GROUP_TYPE}; do
1081 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1083 if ! isset _group_type
; then
1084 log WARN
"Unsupported group-type: ${group_type}"
1088 # Put everything together
1089 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1094 # Returns as a comma-separated list
1095 list_join proposals
,
1098 vpn_security_policies_make_esp_proposal
() {
1101 if ! vpn_security_policy_exists
${name}; then
1102 return ${EXIT_ERROR}
1105 local config_path
="$(vpn_security_policies_path ${name})"
1106 local cache_path
="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1108 # Get data from cache if possible
1109 if file_exists
"${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1110 fread
"${cache_path}"
1114 # No or invalid cache data found
1115 local proposal
=$
(_vpn_security_policies_make_esp_proposal
"${name}")
1117 # Write proposal to cache
1118 if ! make_parent_directory
"${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1119 log WARNING
"Could not write to cache: ${cache_path}"
1125 _vpn_security_policies_make_esp_proposal
() {
1128 # Read the config settings
1129 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1130 if ! vpn_security_policies_read_config
"${name}"; then
1131 return ${EXIT_ERROR}
1137 for cipher
in ${CIPHER}; do
1139 local _cipher
=${CIPHER_TO_STRONGSWAN[${cipher}]}
1141 if ! isset _cipher
; then
1142 log WARN
"Unsupported cipher: ${cipher}"
1146 if vpn_security_policies_cipher_is_aead
${cipher}; then
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}-${_group_type}"
1161 for integrity
in ${INTEGRITY}; do
1162 local _integrity
=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1164 if ! isset _integrity
; then
1165 log WARN
"Unsupported integrity: ${integrity}"
1170 for group_type
in ${GROUP_TYPE}; do
1171 local _group_type
=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1173 if ! isset _group_type
; then
1174 log WARN
"Unsupported group-type: ${group_type}"
1178 # Put everything together
1179 list_append proposals
"${_cipher}-${_integrity}-${_group_type}"
1185 # Returns as a comma-separated list
1186 list_join proposals
,
1189 # List all security policies
1190 vpn_security_policies_list_all
() {
1191 list_directory
"${NETWORK_SHARE_DIR}/vpn/security-policies"
1193 # Add all user-defined policies
1194 vpn_security_policies_list_user
1197 vpn_security_policies_list_user
() {
1198 list_directory
"${NETWORK_CONFIG_DIR}/vpn/security-policies"