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