]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.vpn-security-policies
Always destroy zones immediately
[people/stevee/network.git] / src / functions / functions.vpn-security-policies
CommitLineData
3eae7bed
JS
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2017 IPFire Network Development Team #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22VPN_SECURITY_POLICIES_CONFIG_SETTINGS="CIPHER COMPRESSION GROUP_TYPE INTEGRITY KEY_EXCHANGE LIFETIME PFS"
878cfa3e 23VPN_SECURITY_POLICIES_READONLY="system performance"
3eae7bed 24
6fdbda80
MT
25VPN_DEFAULT_SECURITY_POLICY="system"
26
b116ad92 27declare -A VPN_SUPPORTED_CIPHERS=(
b68fed1f
MT
28 # 3DES-CBC
29 [3DES-CBC]="168 bit 3DES-EDE-CBC"
30
31 # AES-CBC
b116ad92
MT
32 [AES256-CBC]="256 bit AES-CBC"
33 [AES192-CBC]="192 bit AES-CBC"
34 [AES128-CBC]="128 bit AES-CBC"
b68fed1f
MT
35
36 # AES-CTR
37 [AES256-CTR]="256 bit AES-COUNTER"
38 [AES192-CTR]="192 bit AES-COUNTER"
39 [AES128-CTR]="128 bit AES-COUNTER"
40
41 # AES-GCM
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"
51
52 # AES-CCM
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"
62
63 # CAMELLIA-CBC
64 [CAMELLIA256-CBC]="256 bit CAMELLIA-CBC"
65 [CAMELLIA192-CBC]="192 bit CAMELLIA-CBC"
66 [CAMELLIA128-CBC]="128 bit CAMELLIA-CBC"
67
68 # CAMELLIA-CTR
69 [CAMELLIA256-CTR]="256 bit CAMELLIA-COUNTER"
70 [CAMELLIA192-CTR]="192 bit CAMELLIA-COUNTER"
71 [CAMELLIA128-CTR]="128 bit CAMELLIA-COUNTER"
72
73 # CAMELLIA-GCM
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"
83
84 # CAMELLIA-CCM
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"
d9a8af16
MT
94
95 # No Encryption
96 [NULL]="No Encryption"
b116ad92
MT
97)
98
d69af00f
MT
99declare -A CIPHER_TO_STRONGSWAN=(
100 # 3DES-CBC
101 [3DES-CBC]="3des"
102
103 # AES-CBC
104 [AES256-CBC]="aes256"
105 [AES192-CBC]="aes192"
106 [AES128-CBC]="aes128"
107
108 # AES-CTR
109 [AES256-CTR]="aes256ctr"
110 [AES192-CTR]="aes192ctr"
111 [AES128-CTR]="aes128ctr"
112
113 # AES-GCM
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"
123
124 # AES-CCM
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"
134
135 # CAMELLIA-CBC
136 [CAMELLIA256-CBC]="camellia256"
137 [CAMELLIA192-CBC]="camellia192"
138 [CAMELLIA128-CBC]="camellia128"
139
140 # CAMELLIA-CTR
141 [CAMELLIA256-CTR]="camellia256ctr"
142 [CAMELLIA192-CTR]="camellia192ctr"
143 [CAMELLIA128-CTR]="camellia128ctr"
144
145 # CAMELLIA-GCM
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"
155
156 # CAMELLIA-CCM
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"
d9a8af16
MT
166
167 # No Encryption
168 [NULL]="null"
d69af00f
MT
169)
170
3a0c376c
MT
171declare -A VPN_SUPPORTED_INTEGRITY=(
172 [MD5]="MD5-HMAC"
173
174 # SHA
175 [SHA1]="SHA1-HMAC"
3003747b 176 [SHA512]="512 bit SHA2-HMAC"
3a0c376c
MT
177 [SHA384]="384 bit SHA2-HMAC"
178 [SHA256]="256 bit SHA2-HMAC"
179
180 # AES
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"
186)
187
d69af00f
MT
188declare -A INTEGRITY_TO_STRONGSWAN=(
189 [MD5]="md5"
190
191 # SHA
192 [SHA1]="sha1"
193 [SHA512]="sha512"
194 [SHA384]="sha384"
195 [SHA256]="sha256"
196
197 # AES
198 [AES-XCBC]="aesxcbc"
199 [AES-CMAC]="aescmac"
200 [AES256-GMAC]="aes256gmac"
201 [AES192-GMAC]="aes192gmac"
202 [AES128-GMAC]="aes128gmac"
203)
204
54fc6c3f
MT
205declare -A VPN_SUPPORTED_GROUP_TYPES=(
206 # Regular Groups
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"
215
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"
222
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"
228
229 # Curve25519
230 [CURVE25519]="256 bit Elliptic Curve 25519"
231)
3eae7bed 232
d69af00f
MT
233declare -A GROUP_TYPE_TO_STRONGSWAN=(
234 # Regular Groups
235 [MODP768]="modp768"
236 [MODP1024]="modp1024"
237 [MODP1536]="modp1536"
238 [MODP2048]="modp2048"
239 [MODP3072]="modp3072"
240 [MODP4096]="modp4096"
241 [MODP6144]="modp6144"
242 [MODP8192]="modp8192"
243
244 # NIST Elliptic Curve Groups
245 [ECP192]="ecp192"
246 [ECP224]="ecp224"
247 [ECP256]="ecp256"
248 [ECP384]="ecp384"
249 [ECP521]="ecp521"
250
251 # Brainpool Elliptic Curve Groups
252 [ECP224BP]="ecp224bp"
253 [ECP256BP]="ecp256bp"
254 [ECP384BP]="ecp384bp"
255 [ECP512BP]="ecp512bp"
256
257 # Curve25519
258 [CURVE25519]="curve25519"
259)
260
2da98f56
MT
261cli_vpn_security_policies() {
262 local action
263 local security_policy
264
265 if vpn_security_policy_exists ${1}; then
266 security_policy=${1}
267 key=${2}
268 shift 2
269
270 case "${key}" in
271 cipher|compression|integrity|lifetime|pfs|show)
272 vpn_security_policies_${key} ${security_policy} $@
273 ;;
274 group-type)
275 vpn_security_policies_group_type ${security_policy} $@
276 ;;
277 key-exchange)
278 vpn_security_policies_key_exchange ${security_policy} $@
279 ;;
280 *)
281 error "Unrecognized argument: ${key}"
282 exit ${EXIT_ERROR}
283 ;;
284 esac
285 else
286 action=${1}
287 shift
288
289 case "${action}" in
290 new)
291 vpn_security_policies_new $@
292 ;;
293 destroy)
294 vpn_security_policies_destroy $@
295 ;;
296 ""|*)
297 if [ -n "${action}" ]; then
298 error "Unrecognized argument: '${action}'"
299 fi
300 exit ${EXIT_ERROR}
301 ;;
302 esac
303 fi
304}
305
6740c9c5
MT
306# This functions checks if a policy is readonly
307# returns true when yes and false when no
3eae7bed 308vpn_security_policies_check_readonly() {
3eae7bed
JS
309 if isoneof name ${VPN_SECURITY_POLICIES_READONLY}; then
310 return ${EXIT_TRUE}
311 else
312 return ${EXIT_FALSE}
313 fi
314}
315
6740c9c5 316# This function writes all values to a via ${name} specificated vpn security policy configuration file
3eae7bed 317vpn_security_policies_write_config() {
3eae7bed
JS
318 assert [ $# -ge 1 ]
319
320 local name="${1}"
321
6740c9c5 322 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
323 log ERROR "No such vpn security policy: ${name}"
324 return ${EXIT_ERROR}
325 fi
326
6740c9c5 327 if vpn_security_policies_check_readonly "${name}"; then
3eae7bed
JS
328 log ERROR "The ${name} vpn security policy cannot be changed."
329 return ${EXIT_ERROR}
330 fi
331
6740c9c5 332 local path="$(vpn_security_policies_path "${name}")"
3eae7bed
JS
333 if [ ! -w ${path} ]; then
334 log ERROR "${path} is not writeable"
335 return ${EXIT_ERROR}
336 fi
337
338 if ! settings_write "${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
339 log ERROR "Could not write configuration settings for vpn security policy ${name}"
340 return ${EXIT_ERROR}
341 fi
342
f0e91d26
JS
343 if ! vpn_security_policies_reload ${name}; then
344 log WARNING "Could not reload the IPsec connection using this security policy"
345 return ${EXIT_ERROR}
346 fi
347}
348
349# reload IPsec connections using a special policy
350vpn_security_policies_reload() {
351 local name=${1}
352
353 local connection
354 for connection in $(ipsec_list_connections); do
5601f4f5
JS
355 local SECURITY_POLICY ENABLED
356
f0e91d26
JS
357 if ! ipsec_connection_read_config "${connection}" "SECURITY_POLICY"; then
358 continue
359 fi
360
5601f4f5 361 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED; then
f0e91d26
JS
362 if ! ipsec_connection_to_strongswan "${connection}"; then
363 log ERROR "Could not generate strongswan config for ${connnection}"
364 fi
365 fi
366 done
367
368 ipsec_strongswan_load
3eae7bed
JS
369}
370
6740c9c5 371# This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
3eae7bed 372vpn_security_policies_write_config_key() {
3eae7bed 373 assert [ $# -ge 3 ]
6740c9c5 374
3eae7bed
JS
375 local name=${1}
376 local key=${2}
377 shift 2
6740c9c5 378
3eae7bed
JS
379 local value="$@"
380
6740c9c5 381 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
382 log ERROR "No such vpn security policy: ${name}"
383 return ${EXIT_ERROR}
384 fi
385
386 log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}"
387
388 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
389
390 # Read the config settings
6740c9c5 391 if ! vpn_security_policies_read_config "${name}"; then
3eae7bed
JS
392 return ${EXIT_ERROR}
393 fi
394
395 # Set the key to a new value
396 assign "${key}" "${value}"
397
6740c9c5 398 if ! vpn_security_policies_write_config "${name}"; then
3eae7bed
JS
399 return ${EXIT_ERROR}
400 fi
401
402 return ${EXIT_TRUE}
3eae7bed
JS
403}
404
6740c9c5 405# Reads one or more keys out of a settings file or all if no key is provided.
3eae7bed 406vpn_security_policies_read_config() {
3eae7bed
JS
407 assert [ $# -ge 1 ]
408
409 local name="${1}"
410 shift 1
411
6740c9c5 412 if ! vpn_security_policy_exists "${name}"; then
3eae7bed
JS
413 log ERROR "No such vpn security policy: ${name}"
414 return ${EXIT_ERROR}
415 fi
416
417
418 local args
419 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
420 list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
421 else
422 list_append args $@
423 fi
424
425 local path="$(vpn_security_policies_path ${name})"
426
427 if ! settings_read "${path}" ${args}; then
428 log ERROR "Could not read settings for vpn security policy ${name}"
429 return ${EXIT_ERROR}
430 fi
431}
432
6740c9c5 433# Returns the path to a the configuration fora given name
3eae7bed 434vpn_security_policies_path() {
3eae7bed 435 assert [ $# -eq 1 ]
6740c9c5 436
3eae7bed
JS
437 local name=${1}
438
6740c9c5 439 if vpn_security_policies_check_readonly "${name}"; then
3eae7bed
JS
440 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
441 else
442 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
443 fi
444}
445
6740c9c5 446# Print the content of a vpn security policy configuration file in a nice way
3eae7bed 447vpn_security_policies_show() {
3eae7bed 448 assert [ $# -eq 1 ]
6740c9c5 449
3eae7bed
JS
450 local name=${1}
451
452 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
3eae7bed
JS
453 if ! vpn_security_policies_read_config ${name}; then
454 return ${EXIT_ERROR}
455 fi
456
457 cli_print_fmt1 0 "Security Policy: ${name}"
458 cli_space
459
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:"
d21db419
MT
463 local cipher
464 for cipher in ${CIPHER}; do
465 cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
466 done
3eae7bed 467 cli_space
6740c9c5 468
3eae7bed 469 cli_print_fmt1 1 "Integrity:"
f3098f76
MT
470 local integrity
471 for integrity in ${INTEGRITY}; do
472 cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
473 done
3eae7bed 474 cli_space
6740c9c5 475
3eae7bed 476 cli_print_fmt1 1 "Group Types:"
84c1cc17
MT
477 local group_type
478 for group_type in ${GROUP_TYPE}; do
479 cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
480 done
3eae7bed
JS
481 cli_space
482
483 cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}"
6740c9c5
MT
484
485 # Key Lifetime
3eae7bed
JS
486 if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then
487 cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})"
488 else
489 log ERROR "The value for Key Lifetime is not a valid integer greater zero."
490 fi
6740c9c5
MT
491
492 # PFS
3eae7bed
JS
493 if enabled PFS; then
494 cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled"
495 else
496 cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled"
497 fi
498 cli_space
6740c9c5
MT
499
500 # Compression
3eae7bed
JS
501 if enabled COMPRESSION; then
502 cli_print_fmt1 1 "Compression:" "enabled"
503 else
504 cli_print_fmt1 1 "Compression:" "disabled"
505 fi
506 cli_space
507}
508
6740c9c5
MT
509# This function checks if a vpn security policy exists
510# Returns True when yes and false when not
3eae7bed 511vpn_security_policy_exists() {
3eae7bed 512 assert [ $# -eq 1 ]
6740c9c5 513
3eae7bed
JS
514 local name=${1}
515
6740c9c5
MT
516 local path=$(vpn_security_policies_path "${name}")
517
518 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
3eae7bed
JS
519}
520
521
6740c9c5 522# This function parses the parameters for the 'cipher' command
3eae7bed 523vpn_security_policies_cipher(){
3eae7bed
JS
524 local name=${1}
525 shift
526
527 if [ $# -eq 0 ]; then
528 log ERROR "You must pass at least one value after cipher"
529 return ${EXIT_ERROR}
530 fi
531
532 local CIPHER
3eae7bed
JS
533 if ! vpn_security_policies_read_config ${name} "CIPHER"; then
534 return ${EXIT_ERROR}
535 fi
536
537 # Remove duplicated entries to proceed the list safely
538 CIPHER="$(list_unique ${CIPHER})"
539
d9a6c183
MT
540 local ciphers_added
541 local ciphers_removed
542 local ciphers_set
543
3eae7bed 544 while [ $# -gt 0 ]; do
d9a6c183
MT
545 local arg="${1}"
546
547 case "${arg}" in
548 +*)
549 list_append ciphers_added "${arg:1}"
550 ;;
3eae7bed 551 -*)
d9a6c183 552 list_append ciphers_removed "${arg:1}"
3eae7bed 553 ;;
d9a6c183
MT
554 [A-Z0-9]*)
555 list_append ciphers_set "${arg}"
556 ;;
557 *)
558 error "Invalid argument: ${arg}"
559 return ${EXIT_ERROR}
3eae7bed
JS
560 ;;
561 esac
562 shift
563 done
564
d9a6c183
MT
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"
568 return ${EXIT_ERROR}
569 fi
570
571 # Set new cipher list
572 if ! list_is_empty ciphers_set; then
573 # Check if all ciphers are valid
574 local cipher
575 for cipher in ${ciphers_set}; do
576 if ! vpn_security_policies_cipher_supported ${cipher}; then
577 error "Unsupported cipher: ${cipher}"
578 return ${EXIT_ERROR}
579 fi
580 done
581
6eb85c57 582 CIPHER="${ciphers_set}"
d9a6c183
MT
583
584 # Perform incremental updates
3eae7bed 585 else
d9a6c183
MT
586 local cipher
587
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"
592 fi
593 done
594
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"
599 fi
600 else
601 warning "${cipher} is unknown or unsupported and could not be added"
602 fi
603 done
604 fi
605
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"
3eae7bed
JS
609 return ${EXIT_ERROR}
610 fi
d9a6c183
MT
611
612 # Save everything
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."
615 fi
616
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}]}"
620 done
3eae7bed
JS
621}
622
6740c9c5 623# This function parses the parameters for the 'compression' command
3eae7bed 624vpn_security_policies_compression(){
3eae7bed
JS
625 local name=${1}
626 local value=${2}
627
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."
631 return ${EXIT_ERROR}
632 fi
633
634 if ! isbool value; then
635 # We suggest only two values to avoid overburding the user.
636 log ERROR "Invalid Argument ${value}"
637 return ${EXIT_ERROR}
638 fi
639
640 vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}"
641}
642
6740c9c5 643# This function parses the parameters for the 'group-type' command
3eae7bed 644vpn_security_policies_group_type(){
3eae7bed
JS
645 local name=${1}
646 shift
647
648 if [ $# -eq 0 ]; then
649 log ERROR "You must pass at least one value after group-type"
650 return ${EXIT_ERROR}
651 fi
652
653 local GROUP_TYPE
3eae7bed
JS
654 if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then
655 return ${EXIT_ERROR}
656 fi
657
658 # Remove duplicated entries to proceed the list safely
659 GROUP_TYPE="$(list_unique ${GROUP_TYPE})"
660
661 while [ $# -gt 0 ]; do
662 case "${1}" in
663 -*)
664 value=${1#-}
665 # Check if the group type is in the list of group types and
666 # check if the list has after removing this group type at leatst one valid value
667 if list_match ${value} ${GROUP_TYPE}; then
668 list_remove GROUP_TYPE ${value}
669 else
670 # We do not break here because this error does not break the processing of the next maybe valid values.
671 log ERROR "Can not remove ${value} from the list of group types because ${value} is not in the list."
672 fi
673 ;;
674 +*)
675 value=${1#+}
676 # Check if the group type is in the list of supported group types.
54fc6c3f 677 if ! isoneof value ${!VPN_SUPPORTED_GROUP_TYPES[@]}; then
3eae7bed
JS
678 # We do not break here because the processing of other maybe valid values are indepent from this error.
679 log ERROR "${value} is not a supported group type and can thats why not added to the list of group types."
680 else
681 if list_match ${value} ${GROUP_TYPE}; then
682 log WARNING "${value} is already in the list of group-types of this policy."
683 else
684 list_append GROUP_TYPE ${value}
685 fi
686 fi
687 ;;
688 esac
689 shift
690 done
691
692 # Check if the list contain at least one valid group-type
693 if [ $(list_length ${GROUP_TYPE}) -ge 1 ]; then
694 if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then
695 log ERROR "The changes for the vpn security policy ${name} could not be written."
696 fi
697 else
698 log ERROR "After proceding all group types the list is empty and thats why no changes are written."
699 return ${EXIT_ERROR}
700 fi
701}
6740c9c5
MT
702
703# This function parses the parameters for the 'integrity' command
3eae7bed 704vpn_security_policies_integrity(){
3eae7bed
JS
705 local name=${1}
706 shift
707
708 if [ $# -eq 0 ]; then
709 log ERROR "You must pass at least one value after integrity."
710 return ${EXIT_ERROR}
711 fi
712
713 local INTEGRITY
3eae7bed
JS
714 if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then
715 return ${EXIT_ERROR}
716 fi
717
718 # Remove duplicated entries to proceed the list safely
719 INTEGRITY="$(list_unique ${INTEGRITY})"
720
721 while [ $# -gt 0 ]; do
722 case "${1}" in
723 -*)
724 value=${1#-}
725 # Check if the integrity hash is in the list of integrity hashes and
726 # check if the list has after removing this integrity hash at least one valid value
727 if list_match ${value} ${INTEGRITY}; then
728 list_remove INTEGRITY ${value}
729 else
730 # We do not break here because the processing of other maybe valid values are indepent from this error.
731 log ERROR "Can not remove ${value} from the list of integrity hashes because ${value} is not in the list."
732 fi
733 ;;
734 +*)
735 value=${1#+}
736 # Check if the Ciphers is in the list of supported integrity hashes.
3a0c376c 737 if ! isoneof value ${!VPN_SUPPORTED_INTEGRITY[@]}; then
3eae7bed
JS
738 # We do not break here because the processing of other maybe valid values are indepent from this error.
739 log ERROR "${value} is not a supported integrity hash and can thats why not added to the list of integrity hashes."
740 else
741 if list_match ${value} ${INTEGRITY}; then
742 log WARNING "${value} is already in the list of integrety hashes of this policy."
743 else
744 list_append INTEGRITY ${value}
745 fi
746 fi
747 ;;
748 esac
749 shift
750 done
751
752 # Check if the list contain at least one valid group-type
753 if [ $(list_length ${INTEGRITY}) -ge 1 ]; then
754 if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then
755 log ERROR "The changes for the vpn security policy ${name} could not be written."
756 fi
757 else
758 log ERROR "After proceding all integrity hashes the list is empty and thats why no changes are written."
759 return ${EXIT_ERROR}
760 fi
3eae7bed
JS
761}
762
6740c9c5 763# This function parses the parameters for the 'key-exchange' command
3eae7bed 764vpn_security_policies_key_exchange() {
3eae7bed
JS
765 local name=${1}
766 local value=${2}
6740c9c5 767
3eae7bed
JS
768 # Check if we get only one argument after key-exchange <name>
769 if [ ! $# -eq 2 ]; then
770 log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed."
771 return ${EXIT_ERROR}
772 fi
773
3eae7bed
JS
774 if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then
775 log ERROR "Invalid Argument ${value}"
776 return ${EXIT_ERROR}
777 fi
778
779 vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}"
780}
781
6740c9c5 782# This function parses the parameters for the 'lifetime' command.
3eae7bed 783vpn_security_policies_lifetime(){
3eae7bed
JS
784 local name=${1}
785 shift
6740c9c5 786
3eae7bed
JS
787 local value=$@
788
789 # Check if we get only one argument after lifetime <name>
790 if [ ! $# -ge 1 ]; then
791 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"
792 return ${EXIT_ERROR}
793 fi
794
795 if ! isinteger value; then
796 value=$(parse_time $@)
797 if [ ! $? -eq 0 ]; then
798 log ERROR "Parsing the passed time was not sucessful please check the passed values."
799 return ${EXIT_ERROR}
800 fi
801 fi
802
803 if [ ${value} -le 0 ]; then
804 log ERROR "The passed time value must be in the sum greater zero seconds."
805 return ${EXIT_ERROR}
806 fi
807
808 vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}"
809}
810
6740c9c5 811# This function parses the parameters for the 'pfs' command
3eae7bed 812vpn_security_policies_pfs(){
3eae7bed
JS
813 local name=${1}
814 local value=${2}
815
816 # Check if we get only one argument after pfs <name>
817 if [ ! $# -eq 2 ]; then
818 log ERROR "The number of arguments do not match. Only argument after pfs is allowed."
819 return ${EXIT_ERROR}
820 fi
821
822 if [ ! $# -eq 2 ] || ! isbool value; then
823 # We suggest only two values to avoid overburding the user.
824 log ERROR "Invalid Argument ${value}"
825 return ${EXIT_ERROR}
826 fi
827
828 vpn_security_policies_write_config_key "${name}" "PFS" "${value}"
829}
830
6740c9c5
MT
831# This function checks if a vpn security policy name is valid
832# Allowed are only A-Za-z0-9
3eae7bed 833vpn_security_policies_check_name() {
3eae7bed 834 assert [ $# -eq 1 ]
6740c9c5 835
3eae7bed 836 local name=${1}
6740c9c5 837
3eae7bed
JS
838 [[ ${name} =~ [^[:alnum:]$] ]]
839}
840
6740c9c5 841# Function that creates based on the paramters one ore more new vpn security policies
3eae7bed 842vpn_security_policies_new() {
58aa0edf
MT
843 if [ $# -gt 1 ]; then
844 error "Too many arguments"
3eae7bed
JS
845 return ${EXIT_ERROR}
846 fi
847
58aa0edf
MT
848 local name="${1}"
849 if ! isset name; then
850 error "Please provide a name"
851 return ${EXIT_ERROR}
852 fi
3eae7bed 853
58aa0edf
MT
854 # Check for duplicates
855 if vpn_security_policy_exists "${name}"; then
856 error "The VPN security policy with name ${name} already exists"
857 return ${EXIT_ERROR}
858 fi
3eae7bed 859
58aa0edf
MT
860 # Check if name is valid
861 if vpn_security_policies_check_name "${name}"; then
862 error "'${name}' contains illegal characters"
863 return ${EXIT_ERROR}
864 fi
3eae7bed 865
58aa0edf
MT
866 # Check if we have a read-only policy with the same name
867 if vpn_security_policies_check_readonly "${name}"; then
868 error "The VPN security policy ${name} is read-only"
869 return ${EXIT_ERROR}
870 fi
871
6fdbda80
MT
872 # Check if our source policy exists
873 if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then
874 error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
875 return ${EXIT_ERROR}
876 fi
877
58aa0edf
MT
878 log DEBUG "Creating VPN Security Policy ${name}"
879
88013fd8 880 if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
aab75c48 881 "$(vpn_security_policies_path ${name})"; then
58aa0edf
MT
882 log INFO "VPN Security Policy ${name} successfully created"
883 else
884 log ERROR "Could not create VPN Security Policy ${name}"
885 return ${EXIT_ERROR}
886 fi
c787fea5
MT
887
888 # Show the newly created policy
889 vpn_security_policies_show "${name}"
3eae7bed
JS
890}
891
6740c9c5 892# Function that deletes based on the passed parameters one ore more vpn security policies
3eae7bed 893vpn_security_policies_destroy() {
3eae7bed
JS
894 local name
895 for name in $@; do
896 if ! vpn_security_policy_exists ${name}; then
897 log ERROR "The vpn security policy ${name} does not exist."
898 continue
899 fi
900
901 if vpn_security_policies_check_readonly ${name}; then
902 log ERROR "The vpn security policy ${name} cannot be deleted."
903 continue
904 fi
905
906 log DEBUG "Deleting vpn security policy ${name}"
907 settings_remove $(vpn_security_policies_path ${name})
26b47a99
MT
908
909 # Delete cache
910 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
3eae7bed
JS
911 done
912}
d69af00f 913
d9a6c183
MT
914vpn_security_policies_cipher_supported() {
915 local cipher=${1}
916
917 list_match ${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
918}
919
0d645497
MT
920vpn_security_policies_cipher_is_aead() {
921 local cipher=${1}
922
923 # All CCM and GCM ciphers are AEAD
924 string_match "[CG]CM" "${cipher}"
925}
926
e3ffacf7 927vpn_security_policies_make_ike_proposal() {
d69af00f
MT
928 local name=${1}
929
e1947a76
MT
930 if ! vpn_security_policy_exists ${name}; then
931 return ${EXIT_ERROR}
932 fi
933
934 local config_path="$(vpn_security_policies_path ${name})"
e3ffacf7 935 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
e1947a76
MT
936
937 # Get data from cache if possible
938 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
939 fread "${cache_path}"
940 return ${EXIT_OK}
941 fi
942
943 # No or invalid cache data found
e3ffacf7 944 local proposal=$(_vpn_security_policies_make_ike_proposal "${name}")
e1947a76
MT
945
946 # Write proposal to cache
947 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
948 log WARNING "Could not write to cache: ${cache_path}"
949 fi
950
951 print "${proposal}"
952}
953
e3ffacf7 954_vpn_security_policies_make_ike_proposal() {
e1947a76
MT
955 local name=${1}
956
d69af00f
MT
957 # Read the config settings
958 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
959 if ! vpn_security_policies_read_config "${name}"; then
960 return ${EXIT_ERROR}
961 fi
962
963 local proposals
964
965 local cipher
966 for cipher in ${CIPHER}; do
967 # Translate cipher
968 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
969
970 if ! isset _cipher; then
971 log WARN "Unsupported cipher: ${cipher}"
972 continue
973 fi
974
975 local integrity
976 for integrity in ${INTEGRITY}; do
977 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
978
979 if ! isset _integrity; then
980 log WARN "Unsupported integrity: ${integrity}"
981 continue
982 fi
983
984 local group_type
985 for group_type in ${GROUP_TYPE}; do
986 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
987
988 if ! isset _group_type; then
989 log WARN "Unsupported group-type: ${group_type}"
990 continue
991 fi
992
993 # Put everything together
994 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
995 done
996 done
997 done
998
999 # Returns as a comma-separated list
1000 list_join proposals ,
1001}
0d645497
MT
1002
1003vpn_security_policies_make_esp_proposal() {
1004 local name=${1}
1005
e1947a76
MT
1006 if ! vpn_security_policy_exists ${name}; then
1007 return ${EXIT_ERROR}
1008 fi
1009
1010 local config_path="$(vpn_security_policies_path ${name})"
1011 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1012
1013 # Get data from cache if possible
1014 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1015 fread "${cache_path}"
1016 return ${EXIT_OK}
1017 fi
1018
1019 # No or invalid cache data found
1020 local proposal=$(_vpn_security_policies_make_esp_proposal "${name}")
1021
1022 # Write proposal to cache
1023 if ! make_parent_dir "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1024 log WARNING "Could not write to cache: ${cache_path}"
1025 fi
1026
1027 print "${proposal}"
1028}
1029
1030_vpn_security_policies_make_esp_proposal() {
1031 local name=${1}
1032
0d645497
MT
1033 # Read the config settings
1034 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1035 if ! vpn_security_policies_read_config "${name}"; then
1036 return ${EXIT_ERROR}
1037 fi
1038
1039 local proposals
1040
1041 local cipher
1042 for cipher in ${CIPHER}; do
1043 # Translate cipher
1044 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1045
1046 if ! isset _cipher; then
1047 log WARN "Unsupported cipher: ${cipher}"
1048 continue
1049 fi
1050
1051 if vpn_security_policies_cipher_is_aead ${cipher}; then
1052 local group_type
1053 for group_type in ${GROUP_TYPE}; do
1054 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1055
1056 if ! isset _group_type; then
1057 log WARN "Unsupported group-type: ${group_type}"
1058 continue
1059 fi
1060
1061 # Put everything together
1062 list_append proposals "${_cipher}-${_group_type}"
1063 done
1064 else
1065 local integrity
1066 for integrity in ${INTEGRITY}; do
1067 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1068
1069 if ! isset _integrity; then
1070 log WARN "Unsupported integrity: ${integrity}"
1071 continue
1072 fi
1073
1074 local group_type
1075 for group_type in ${GROUP_TYPE}; do
1076 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1077
1078 if ! isset _group_type; then
1079 log WARN "Unsupported group-type: ${group_type}"
1080 continue
1081 fi
1082
1083 # Put everything together
1084 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1085 done
1086 done
1087 fi
1088 done
1089
1090 # Returns as a comma-separated list
1091 list_join proposals ,
1092}
a00e7803
JS
1093
1094# List all security policies
1095vpn_security_policies_list_all() {
1096 local security_policy
1097 for security_policy in ${NETWORK_SHARE_DIR}/vpn/security-policies/*; do
1098 [ -f ${security_policy} ] || continue
1099 basename ${security_policy}
1100 done
1101
3cac4fcd
MT
1102 # Add all user-defined policies
1103 vpn_security_policies_list_user
1104}
1105
1106vpn_security_policies_list_user() {
1107 local security_policy
a00e7803
JS
1108 for security_policy in ${NETWORK_CONFIG_DIR}/vpn/security-policies/*; do
1109 [ -f ${security_policy} ] || continue
3cac4fcd 1110
a00e7803
JS
1111 basename ${security_policy}
1112 done
1113}