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"
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 declare -A VPN_SUPPORTED_INTEGRITY
=(
101 [SHA512
]="256 bit SHA2-HMAC"
102 [SHA384
]="384 bit SHA2-HMAC"
103 [SHA256
]="256 bit SHA2-HMAC"
106 [AES-XCBC
]="AES-XCBC"
107 [AES-CMAC
]="AES-CMAC"
108 [AES256-GMAC
]="256 bit AES-GMAC"
109 [AES192-GMAC
]="192 bit AES-GMAC"
110 [AES128-GMAC
]="128 bit AES-GMAC"
113 VPN_SUPPORTED_GROUP_TYPES
="MODP8192 MODP4096"
115 # This functions checks if a policy is readonly
116 # returns true when yes and false when no
117 vpn_security_policies_check_readonly
() {
118 if isoneof name
${VPN_SECURITY_POLICIES_READONLY}; then
125 # This function writes all values to a via ${name} specificated vpn security policy configuration file
126 vpn_security_policies_write_config
() {
131 if ! vpn_security_policy_exists
"${name}"; then
132 log ERROR
"No such vpn security policy: ${name}"
136 if vpn_security_policies_check_readonly
"${name}"; then
137 log ERROR
"The ${name} vpn security policy cannot be changed."
141 local path
="$(vpn_security_policies_path "${name}")"
142 if [ ! -w ${path} ]; then
143 log ERROR
"${path} is not writeable"
147 if ! settings_write
"${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
148 log ERROR
"Could not write configuration settings for vpn security policy ${name}"
152 # TODO everytime we successfully write a config we should call some trigger to take the changes into effect
155 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
156 vpn_security_policies_write_config_key
() {
165 if ! vpn_security_policy_exists
"${name}"; then
166 log ERROR
"No such vpn security policy: ${name}"
170 log DEBUG
"Set '${key}' to new value '${value}' in vpn security policy ${name}"
172 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
174 # Read the config settings
175 if ! vpn_security_policies_read_config
"${name}"; then
179 # Set the key to a new value
180 assign
"${key}" "${value}"
182 if ! vpn_security_policies_write_config
"${name}"; then
189 # Reads one or more keys out of a settings file or all if no key is provided.
190 vpn_security_policies_read_config
() {
196 if ! vpn_security_policy_exists
"${name}"; then
197 log ERROR
"No such vpn security policy: ${name}"
203 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
204 list_append args
${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
209 local path
="$(vpn_security_policies_path ${name})"
211 if ! settings_read
"${path}" ${args}; then
212 log ERROR
"Could not read settings for vpn security policy ${name}"
217 # Returns the path to a the configuration fora given name
218 vpn_security_policies_path
() {
223 if vpn_security_policies_check_readonly
"${name}"; then
224 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
226 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
230 # Print the content of a vpn security policy configuration file in a nice way
231 vpn_security_policies_show
() {
236 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
237 if ! vpn_security_policies_read_config
${name}; then
241 cli_print_fmt1
0 "Security Policy: ${name}"
244 # This could be done in a loop but a loop is much more complicated
245 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
246 cli_print_fmt1
1 "Ciphers:"
248 for cipher
in ${CIPHER}; do
249 cli_print_fmt1
2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
253 cli_print_fmt1
1 "Integrity:"
255 for integrity
in ${INTEGRITY}; do
256 cli_print_fmt1
2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
260 cli_print_fmt1
1 "Group Types:"
261 cli_print_fmt1
2 "${GROUP_TYPE}"
264 cli_print_fmt1
1 "Key Exchange:" "${KEY_EXCHANGE}"
267 if isinteger LIFETIME
&& [ ${LIFETIME} -gt 0 ]; then
268 cli_print_fmt1
1 "Key Lifetime:" "$(format_time ${LIFETIME})"
270 log ERROR
"The value for Key Lifetime is not a valid integer greater zero."
275 cli_print_fmt1
1 "Perfect Forward Secrecy:" "enabled"
277 cli_print_fmt1
1 "Perfect Forward Secrecy:" "disabled"
282 if enabled COMPRESSION
; then
283 cli_print_fmt1
1 "Compression:" "enabled"
285 cli_print_fmt1
1 "Compression:" "disabled"
290 # This function checks if a vpn security policy exists
291 # Returns True when yes and false when not
292 vpn_security_policy_exists
() {
297 local path
=$
(vpn_security_policies_path
"${name}")
299 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
303 # This function parses the parameters for the 'cipher' command
304 vpn_security_policies_cipher
(){
308 if [ $# -eq 0 ]; then
309 log ERROR
"You must pass at least one value after cipher"
314 if ! vpn_security_policies_read_config
${name} "CIPHER"; then
318 # Remove duplicated entries to proceed the list safely
319 CIPHER
="$(list_unique ${CIPHER})"
321 while [ $# -gt 0 ]; do
325 # Check if the cipher is in the list of ciphers and
326 # check if the list has after removing this cipher at least one valid value
327 if list_match
${value} ${CIPHER}; then
328 list_remove CIPHER
${value}
330 # We do not break here because this error does not break the processing of the next maybe valid values.
331 log ERROR
"Can not remove ${value} from the list of Ciphers because ${value} is not in the list."
336 # Check if the Ciphers is in the list of supported ciphers.
337 if ! isoneof value
${!VPN_SUPPORTED_CIPHERS[@]}; then
338 # We do not break here because this error does not break the processing of the next maybe valid values.
339 log ERROR
"${value} is not a supported cipher and can thats why not added to the list of ciphers."
341 if list_match
${value} ${CIPHER}; then
342 log WARNING
"${value} is already in the list of ciphers of this policy."
344 list_append CIPHER
${value}
352 # Check if the list contain at least one valid cipher
353 if [ $
(list_length
${CIPHER}) -ge 1 ]; then
354 if ! vpn_security_policies_write_config_key
${name} "CIPHER" ${CIPHER}; then
355 log ERROR
"The changes for the vpn security policy ${name} could not be written."
358 log ERROR
"After proceding all ciphers the list is empty and thats why no changes are written."
363 # This function parses the parameters for the 'compression' command
364 vpn_security_policies_compression
(){
368 # Check if we get only one argument after compression <name>
369 if [ ! $# -eq 2 ]; then
370 log ERROR
"The number of arguments do not match. Only one argument after compression is allowed."
374 if ! isbool value
; then
375 # We suggest only two values to avoid overburding the user.
376 log ERROR
"Invalid Argument ${value}"
380 vpn_security_policies_write_config_key
"${name}" "COMPRESSION" "${value}"
383 # This function parses the parameters for the 'group-type' command
384 vpn_security_policies_group_type
(){
388 if [ $# -eq 0 ]; then
389 log ERROR
"You must pass at least one value after group-type"
394 if ! vpn_security_policies_read_config
${name} "GROUP_TYPE"; then
398 # Remove duplicated entries to proceed the list safely
399 GROUP_TYPE
="$(list_unique ${GROUP_TYPE})"
401 while [ $# -gt 0 ]; do
405 # Check if the group type is in the list of group types and
406 # check if the list has after removing this group type at leatst one valid value
407 if list_match
${value} ${GROUP_TYPE}; then
408 list_remove GROUP_TYPE
${value}
410 # We do not break here because this error does not break the processing of the next maybe valid values.
411 log ERROR
"Can not remove ${value} from the list of group types because ${value} is not in the list."
416 # Check if the group type is in the list of supported group types.
417 if ! isoneof value
${VPN_SUPPORTED_GROUP_TYPES}; then
418 # We do not break here because the processing of other maybe valid values are indepent from this error.
419 log ERROR
"${value} is not a supported group type and can thats why not added to the list of group types."
421 if list_match
${value} ${GROUP_TYPE}; then
422 log WARNING
"${value} is already in the list of group-types of this policy."
424 list_append GROUP_TYPE
${value}
432 # Check if the list contain at least one valid group-type
433 if [ $
(list_length
${GROUP_TYPE}) -ge 1 ]; then
434 if ! vpn_security_policies_write_config_key
${name} "GROUP_TYPE" ${GROUP_TYPE}; then
435 log ERROR
"The changes for the vpn security policy ${name} could not be written."
438 log ERROR
"After proceding all group types the list is empty and thats why no changes are written."
443 # This function parses the parameters for the 'integrity' command
444 vpn_security_policies_integrity
(){
448 if [ $# -eq 0 ]; then
449 log ERROR
"You must pass at least one value after integrity."
454 if ! vpn_security_policies_read_config
${name} "INTEGRITY"; then
458 # Remove duplicated entries to proceed the list safely
459 INTEGRITY
="$(list_unique ${INTEGRITY})"
461 while [ $# -gt 0 ]; do
465 # Check if the integrity hash is in the list of integrity hashes and
466 # check if the list has after removing this integrity hash at least one valid value
467 if list_match
${value} ${INTEGRITY}; then
468 list_remove INTEGRITY
${value}
470 # We do not break here because the processing of other maybe valid values are indepent from this error.
471 log ERROR
"Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
476 # Check if the Ciphers is in the list of supported integrity hashes.
477 if ! isoneof value
${!VPN_SUPPORTED_INTEGRITY[@]}; then
478 # We do not break here because the processing of other maybe valid values are indepent from this error.
479 log ERROR
"${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
481 if list_match
${value} ${INTEGRITY}; then
482 log WARNING
"${value} is already in the list of integrety hashes of this policy."
484 list_append INTEGRITY
${value}
492 # Check if the list contain at least one valid group-type
493 if [ $
(list_length
${INTEGRITY}) -ge 1 ]; then
494 if ! vpn_security_policies_write_config_key
${name} "INTEGRITY" ${INTEGRITY}; then
495 log ERROR
"The changes for the vpn security policy ${name} could not be written."
498 log ERROR
"After proceding all integrity hashes the list is empty and thats why no changes are written."
503 # This function parses the parameters for the 'key-exchange' command
504 vpn_security_policies_key_exchange
() {
508 # Check if we get only one argument after key-exchange <name>
509 if [ ! $# -eq 2 ]; then
510 log ERROR
"The number of arguments do not match. Only argument after key-exchange is allowed."
514 if ! isoneof value
"ikev1" "ikev2" "IKEV1" "IKEV2"; then
515 log ERROR
"Invalid Argument ${value}"
519 vpn_security_policies_write_config_key
"${name}" "KEY_EXCHANGE" "${value,,}"
522 # This function parses the parameters for the 'lifetime' command.
523 vpn_security_policies_lifetime
(){
529 # Check if we get only one argument after lifetime <name>
530 if [ ! $# -ge 1 ]; then
531 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"
535 if ! isinteger value
; then
536 value
=$
(parse_time $@
)
537 if [ ! $?
-eq 0 ]; then
538 log ERROR
"Parsing the passed time was not sucessful please check the passed values."
543 if [ ${value} -le 0 ]; then
544 log ERROR
"The passed time value must be in the sum greater zero seconds."
548 vpn_security_policies_write_config_key
"${name}" "LIFETIME" "${value}"
551 # This function parses the parameters for the 'pfs' command
552 vpn_security_policies_pfs
(){
556 # Check if we get only one argument after pfs <name>
557 if [ ! $# -eq 2 ]; then
558 log ERROR
"The number of arguments do not match. Only argument after pfs is allowed."
562 if [ ! $# -eq 2 ] ||
! isbool value
; then
563 # We suggest only two values to avoid overburding the user.
564 log ERROR
"Invalid Argument ${value}"
568 vpn_security_policies_write_config_key
"${name}" "PFS" "${value}"
571 # This function checks if a vpn security policy name is valid
572 # Allowed are only A-Za-z0-9
573 vpn_security_policies_check_name
() {
578 [[ ${name} =~
[^
[:alnum
:]$
] ]]
581 # Function that creates based on the paramters one ore more new vpn security policies
582 vpn_security_policies_new
() {
583 if [ $# -gt 1 ]; then
584 error
"Too many arguments"
589 if ! isset name
; then
590 error
"Please provide a name"
594 # Check for duplicates
595 if vpn_security_policy_exists
"${name}"; then
596 error
"The VPN security policy with name ${name} already exists"
600 # Check if name is valid
601 if vpn_security_policies_check_name
"${name}"; then
602 error
"'${name}' contains illegal characters"
606 # Check if we have a read-only policy with the same name
607 if vpn_security_policies_check_readonly
"${name}"; then
608 error
"The VPN security policy ${name} is read-only"
612 # Check if our source policy exists
613 if ! vpn_security_policy_exists
"${VPN_DEFAULT_SECURITY_POLICY}"; then
614 error
"Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
618 log DEBUG
"Creating VPN Security Policy ${name}"
620 if copy
"$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")"
621 "$(vpn_security_policies_path ${name})"; then
622 log INFO
"VPN Security Policy ${name} successfully created"
624 log ERROR
"Could not create VPN Security Policy ${name}"
628 # Show the newly created policy
629 vpn_security_policies_show
"${name}"
632 # Function that deletes based on the passed parameters one ore more vpn security policies
633 vpn_security_policies_destroy
() {
636 if ! vpn_security_policy_exists
${name}; then
637 log ERROR
"The vpn security policy ${name} does not exist."
641 if vpn_security_policies_check_readonly
${name}; then
642 log ERROR
"The vpn security policy ${name} cannot be deleted."
646 log DEBUG
"Deleting vpn security policy ${name}"
647 settings_remove $
(vpn_security_policies_path
${name})