]> git.ipfire.org Git - people/ms/network.git/blob - src/functions/functions.vpn-security-policies
ipsec: Generate IKE proposals with PRFs
[people/ms/network.git] / src / functions / functions.vpn-security-policies
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
22 VPN_SECURITY_POLICIES_CONFIG_SETTINGS="CIPHER COMPRESSION GROUP_TYPE \
23 INTEGRITY PSEUDO_RANDOM_FUNCTION KEY_EXCHANGE LIFETIME PFS"
24 VPN_SECURITY_POLICIES_READONLY="system performance"
25
26 VPN_DEFAULT_SECURITY_POLICY="system"
27
28 declare -A VPN_SUPPORTED_CIPHERS=(
29 # 3DES-CBC
30 [3DES-CBC]="168 bit 3DES-EDE-CBC"
31
32 # AES-CBC
33 [AES256-CBC]="256 bit AES-CBC"
34 [AES192-CBC]="192 bit AES-CBC"
35 [AES128-CBC]="128 bit AES-CBC"
36
37 # AES-CTR
38 [AES256-CTR]="256 bit AES-COUNTER"
39 [AES192-CTR]="192 bit AES-COUNTER"
40 [AES128-CTR]="128 bit AES-COUNTER"
41
42 # AES-GCM
43 [AES256-GCM128]="256 bit AES-GCM with 128 bit ICV"
44 [AES192-GCM128]="192 bit AES-GCM with 128 bit ICV"
45 [AES128-GCM128]="128 bit AES-GCM with 128 bit ICV"
46 [AES256-GCM96]="256 bit AES-GCM with 96 bit ICV"
47 [AES192-GCM96]="192 bit AES-GCM with 96 bit ICV"
48 [AES128-GCM96]="128 bit AES-GCM with 96 bit ICV"
49 [AES256-GCM64]="256 bit AES-GCM with 64 bit ICV"
50 [AES192-GCM64]="192 bit AES-GCM with 64 bit ICV"
51 [AES128-GCM64]="128 bit AES-GCM with 64 bit ICV"
52
53 # AES-CCM
54 [AES256-CCM128]="256 bit AES-CCM with 128 bit ICV"
55 [AES192-CCM128]="192 bit AES-CCM with 128 bit ICV"
56 [AES128-CCM128]="128 bit AES-CCM with 128 bit ICV"
57 [AES256-CCM96]="256 bit AES-CCM with 96 bit ICV"
58 [AES192-CCM96]="192 bit AES-CCM with 96 bit ICV"
59 [AES128-CCM96]="128 bit AES-CCM with 96 bit ICV"
60 [AES256-CCM64]="256 bit AES-CCM with 64 bit ICV"
61 [AES192-CCM64]="192 bit AES-CCM with 64 bit ICV"
62 [AES128-CCM64]="128 bit AES-CCM with 64 bit ICV"
63
64 # CAMELLIA-CBC
65 [CAMELLIA256-CBC]="256 bit CAMELLIA-CBC"
66 [CAMELLIA192-CBC]="192 bit CAMELLIA-CBC"
67 [CAMELLIA128-CBC]="128 bit CAMELLIA-CBC"
68
69 # CAMELLIA-CTR
70 [CAMELLIA256-CTR]="256 bit CAMELLIA-COUNTER"
71 [CAMELLIA192-CTR]="192 bit CAMELLIA-COUNTER"
72 [CAMELLIA128-CTR]="128 bit CAMELLIA-COUNTER"
73
74 # CAMELLIA-GCM
75 [CAMELLIA256-GCM128]="256 bit CAMELLIA-GCM with 128 bit ICV"
76 [CAMELLIA192-GCM128]="192 bit CAMELLIA-GCM with 128 bit ICV"
77 [CAMELLIA128-GCM128]="128 bit CAMELLIA-GCM with 128 bit ICV"
78 [CAMELLIA256-GCM96]="256 bit CAMELLIA-GCM with 96 bit ICV"
79 [CAMELLIA192-GCM96]="192 bit CAMELLIA-GCM with 96 bit ICV"
80 [CAMELLIA128-GCM96]="128 bit CAMELLIA-GCM with 96 bit ICV"
81 [CAMELLIA256-GCM64]="256 bit CAMELLIA-GCM with 64 bit ICV"
82 [CAMELLIA192-GCM64]="192 bit CAMELLIA-GCM with 64 bit ICV"
83 [CAMELLIA128-GCM64]="128 bit CAMELLIA-GCM with 64 bit ICV"
84
85 # CAMELLIA-CCM
86 [CAMELLIA256-CCM128]="256 bit CAMELLIA-CCM with 128 bit ICV"
87 [CAMELLIA192-CCM128]="192 bit CAMELLIA-CCM with 128 bit ICV"
88 [CAMELLIA128-CCM128]="128 bit CAMELLIA-CCM with 128 bit ICV"
89 [CAMELLIA256-CCM96]="256 bit CAMELLIA-CCM with 96 bit ICV"
90 [CAMELLIA192-CCM96]="192 bit CAMELLIA-CCM with 96 bit ICV"
91 [CAMELLIA128-CCM96]="128 bit CAMELLIA-CCM with 96 bit ICV"
92 [CAMELLIA256-CCM64]="256 bit CAMELLIA-CCM with 64 bit ICV"
93 [CAMELLIA192-CCM64]="192 bit CAMELLIA-CCM with 64 bit ICV"
94 [CAMELLIA128-CCM64]="128 bit CAMELLIA-CCM with 64 bit ICV"
95
96 # DJB
97 [CHACHA20-POLY1305]="256 bit ChaCha20/Poly1305 with 128 bit ICV"
98
99 # No Encryption
100 [NULL]="No Encryption"
101 )
102
103 declare -A CIPHER_TO_STRONGSWAN=(
104 # 3DES-CBC
105 [3DES-CBC]="3des"
106
107 # AES-CBC
108 [AES256-CBC]="aes256"
109 [AES192-CBC]="aes192"
110 [AES128-CBC]="aes128"
111
112 # AES-CTR
113 [AES256-CTR]="aes256ctr"
114 [AES192-CTR]="aes192ctr"
115 [AES128-CTR]="aes128ctr"
116
117 # AES-GCM
118 [AES256-GCM128]="aes256gcm128"
119 [AES192-GCM128]="aes192gcm128"
120 [AES128-GCM128]="aes128gcm128"
121 [AES256-GCM96]="aes256gcm96"
122 [AES192-GCM96]="aes192gcm96"
123 [AES128-GCM96]="aes128gcm96"
124 [AES256-GCM64]="aes256gcm64"
125 [AES192-GCM64]="aes192gcm64"
126 [AES128-GCM64]="aes128gcm64"
127
128 # AES-CCM
129 [AES256-CCM128]="aes256ccm128"
130 [AES192-CCM128]="aes192ccm128"
131 [AES128-CCM128]="aes128ccm128"
132 [AES256-CCM96]="aes256ccm96"
133 [AES192-CCM96]="aes192ccm96"
134 [AES128-CCM96]="aes128ccm96"
135 [AES256-CCM64]="aes256ccm64"
136 [AES192-CCM64]="aes192ccm64"
137 [AES128-CCM64]="aes128ccm64"
138
139 # CAMELLIA-CBC
140 [CAMELLIA256-CBC]="camellia256"
141 [CAMELLIA192-CBC]="camellia192"
142 [CAMELLIA128-CBC]="camellia128"
143
144 # CAMELLIA-CTR
145 [CAMELLIA256-CTR]="camellia256ctr"
146 [CAMELLIA192-CTR]="camellia192ctr"
147 [CAMELLIA128-CTR]="camellia128ctr"
148
149 # CAMELLIA-GCM
150 [CAMELLIA256-GCM128]="camellia256gcm128"
151 [CAMELLIA192-GCM128]="camellia192gcm128"
152 [CAMELLIA128-GCM128]="camellia128gcm128"
153 [CAMELLIA256-GCM96]="camellia256gcm96"
154 [CAMELLIA192-GCM96]="camellia192gcm96"
155 [CAMELLIA128-GCM96]="camellia128gcm96"
156 [CAMELLIA256-GCM64]="camellia256gcm64"
157 [CAMELLIA192-GCM64]="camellia192gcm64"
158 [CAMELLIA128-GCM64]="camellia128gcm64"
159
160 # CAMELLIA-CCM
161 [CAMELLIA256-CCM128]="camellia256ccm128"
162 [CAMELLIA192-CCM128]="camellia192ccm128"
163 [CAMELLIA128-CCM128]="camellia128ccm128"
164 [CAMELLIA256-CCM96]="camellia256ccm96"
165 [CAMELLIA192-CCM96]="camellia192ccm96"
166 [CAMELLIA128-CCM96]="camellia128ccm96"
167 [CAMELLIA256-CCM64]="camellia256ccm64"
168 [CAMELLIA192-CCM64]="camellia192ccm64"
169 [CAMELLIA128-CCM64]="camellia128ccm64"
170
171 # DJB
172 [CHACHA20-POLY1305]="chacha20poly1305"
173
174 # No Encryption
175 [NULL]="null"
176 )
177
178 declare -A VPN_SUPPORTED_PSEUDO_RANDOM_FUNCTION=(
179 [MD5]="MD5"
180
181 # SHA
182 [SHA1]="SHA1"
183 [SHA256]="SHA256"
184 [SHA384]="SHA384"
185 [SHA512]="SHA512"
186
187 # AES
188 [AES-XCBC]="AES-XCBC"
189 [AES-CMAC]="AES-CMAC"
190 )
191
192 declare -A PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN=(
193 [MD5]="prfmd5"
194
195 # SHA
196 [SHA1]="prfsha1"
197 [SHA256]="prfsha256"
198 [SHA384]="prfsha384"
199 [SHA512]="prfsha512"
200
201 # AES
202 [AES-XCBC]="prfaesxcbc"
203 [AES-CMAC]="prfaescmac"
204 )
205
206 declare -A VPN_SUPPORTED_INTEGRITY=(
207 [MD5]="MD5-HMAC"
208
209 # SHA
210 [SHA1]="SHA1-HMAC"
211 [SHA512]="512 bit SHA2-HMAC"
212 [SHA384]="384 bit SHA2-HMAC"
213 [SHA256]="256 bit SHA2-HMAC"
214
215 # AES
216 [AES-XCBC]="AES-XCBC"
217 [AES-CMAC]="AES-CMAC"
218 [AES256-GMAC]="256 bit AES-GMAC"
219 [AES192-GMAC]="192 bit AES-GMAC"
220 [AES128-GMAC]="128 bit AES-GMAC"
221 )
222
223 declare -A INTEGRITY_TO_STRONGSWAN=(
224 [MD5]="md5"
225
226 # SHA
227 [SHA1]="sha1"
228 [SHA512]="sha512"
229 [SHA384]="sha384"
230 [SHA256]="sha256"
231
232 # AES
233 [AES-XCBC]="aesxcbc"
234 [AES-CMAC]="aescmac"
235 [AES256-GMAC]="aes256gmac"
236 [AES192-GMAC]="aes192gmac"
237 [AES128-GMAC]="aes128gmac"
238 )
239
240 declare -A VPN_SUPPORTED_GROUP_TYPES=(
241 # Regular Groups
242 [MODP768]="768 bit Modulo Prime Group"
243 [MODP1024]="1024 bit Modulo Prime Group"
244 [MODP1536]="1536 bit Modulo Prime Group"
245 [MODP2048]="2048 bit Modulo Prime Group"
246 [MODP3072]="3072 bit Modulo Prime Group"
247 [MODP4096]="4096 bit Modulo Prime Group"
248 [MODP6144]="6144 bit Modulo Prime Group"
249 [MODP8192]="8192 bit Modulo Prime Group"
250
251 # NIST Elliptic Curve Groups
252 [ECP192]="192 bit NIST Elliptic Curve Group"
253 [ECP224]="224 bit NIST Elliptic Curve Group"
254 [ECP256]="256 bit NIST Elliptic Curve Group"
255 [ECP384]="384 bit NIST Elliptic Curve Group"
256 [ECP521]="521 bit NIST Elliptic Curve Group"
257
258 # Brainpool Elliptic Curve Groups
259 [ECP224BP]="224 bit Brainpool Elliptic Curve Group"
260 [ECP256BP]="256 bit Brainpool Elliptic Curve Group"
261 [ECP384BP]="384 bit Brainpool Elliptic Curve Group"
262 [ECP512BP]="512 bit Brainpool Elliptic Curve Group"
263
264 # Curve25519
265 [CURVE25519]="256 bit Elliptic Curve 25519"
266 )
267
268 declare -A GROUP_TYPE_TO_STRONGSWAN=(
269 # Regular Groups
270 [MODP768]="modp768"
271 [MODP1024]="modp1024"
272 [MODP1536]="modp1536"
273 [MODP2048]="modp2048"
274 [MODP3072]="modp3072"
275 [MODP4096]="modp4096"
276 [MODP6144]="modp6144"
277 [MODP8192]="modp8192"
278
279 # NIST Elliptic Curve Groups
280 [ECP192]="ecp192"
281 [ECP224]="ecp224"
282 [ECP256]="ecp256"
283 [ECP384]="ecp384"
284 [ECP521]="ecp521"
285
286 # Brainpool Elliptic Curve Groups
287 [ECP224BP]="ecp224bp"
288 [ECP256BP]="ecp256bp"
289 [ECP384BP]="ecp384bp"
290 [ECP512BP]="ecp512bp"
291
292 # Curve25519
293 [CURVE25519]="curve25519"
294 )
295
296 cli_vpn_security_policies() {
297 local action
298 local security_policy
299
300 if vpn_security_policy_exists ${1}; then
301 security_policy=${1}
302 key=${2}
303 shift 2
304
305 case "${key}" in
306 cipher|compression|integrity|lifetime|pfs|show)
307 vpn_security_policies_${key} ${security_policy} "$@"
308 ;;
309 group-type)
310 vpn_security_policies_group_type ${security_policy} "$@"
311 ;;
312 key-exchange)
313 vpn_security_policies_key_exchange ${security_policy} "$@"
314 ;;
315 *)
316 error "Unrecognized argument: ${key}"
317 exit ${EXIT_ERROR}
318 ;;
319 esac
320 else
321 action=${1}
322 shift
323
324 case "${action}" in
325 new)
326 vpn_security_policies_new "$@"
327 ;;
328 destroy)
329 vpn_security_policies_destroy "$@"
330 ;;
331 ""|*)
332 if [ -n "${action}" ]; then
333 error "Unrecognized argument: '${action}'"
334 fi
335 exit ${EXIT_ERROR}
336 ;;
337 esac
338 fi
339 }
340
341 # This functions checks if a policy is readonly
342 # returns true when yes and false when no
343 vpn_security_policies_check_readonly() {
344 if isoneof name ${VPN_SECURITY_POLICIES_READONLY}; then
345 return ${EXIT_TRUE}
346 else
347 return ${EXIT_FALSE}
348 fi
349 }
350
351 # This function writes all values to a via ${name} specificated vpn security policy configuration file
352 vpn_security_policies_write_config() {
353 assert [ $# -ge 1 ]
354
355 local name="${1}"
356
357 if ! vpn_security_policy_exists "${name}"; then
358 log ERROR "No such vpn security policy: ${name}"
359 return ${EXIT_ERROR}
360 fi
361
362 if vpn_security_policies_check_readonly "${name}"; then
363 log ERROR "The ${name} vpn security policy cannot be changed."
364 return ${EXIT_ERROR}
365 fi
366
367 local path="$(vpn_security_policies_path "${name}")"
368 if [ ! -w ${path} ]; then
369 log ERROR "${path} is not writeable"
370 return ${EXIT_ERROR}
371 fi
372
373 if ! settings_write "${path}" ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}; then
374 log ERROR "Could not write configuration settings for vpn security policy ${name}"
375 return ${EXIT_ERROR}
376 fi
377
378 if ! vpn_security_policies_reload ${name}; then
379 log WARNING "Could not reload the IPsec connection using this security policy"
380 return ${EXIT_ERROR}
381 fi
382 }
383
384 # reload IPsec connections using a special policy
385 vpn_security_policies_reload() {
386 local name=${1}
387
388 local connection
389 for connection in $(ipsec_list_connections); do
390 local SECURITY_POLICY ENABLED
391
392 if ! ipsec_connection_read_config "${connection}" "SECURITY_POLICY"; then
393 continue
394 fi
395
396 if [[ "${SECURITY_POLICY}" = "${name}" ]] && enabled ENABLED; then
397 if ! ipsec_connection_to_strongswan "${connection}"; then
398 log ERROR "Could not generate strongswan config for ${connnection}"
399 fi
400 fi
401 done
402
403 ipsec_strongswan_load
404 }
405
406 # This funtion writes the value for one key to a via ${name} specificated vpn security policy configuration file
407 vpn_security_policies_write_config_key() {
408 assert [ $# -ge 3 ]
409
410 local name=${1}
411 local key=${2}
412 shift 2
413
414 local value="$@"
415
416 if ! vpn_security_policy_exists "${name}"; then
417 log ERROR "No such vpn security policy: ${name}"
418 return ${EXIT_ERROR}
419 fi
420
421 log DEBUG "Set '${key}' to new value '${value}' in vpn security policy ${name}"
422
423 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
424
425 # Read the config settings
426 if ! vpn_security_policies_read_config "${name}"; then
427 return ${EXIT_ERROR}
428 fi
429
430 # Set the key to a new value
431 assign "${key}" "${value}"
432
433 if ! vpn_security_policies_write_config "${name}"; then
434 return ${EXIT_ERROR}
435 fi
436
437 return ${EXIT_TRUE}
438 }
439
440 # Reads one or more keys out of a settings file or all if no key is provided.
441 vpn_security_policies_read_config() {
442 assert [ $# -ge 1 ]
443
444 local name="${1}"
445 shift 1
446
447 if ! vpn_security_policy_exists "${name}"; then
448 log ERROR "No such vpn security policy: ${name}"
449 return ${EXIT_ERROR}
450 fi
451
452
453 local args
454 if [ $# -eq 0 ] && [ -n "${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}" ]; then
455 list_append args ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
456 else
457 list_append args "$@"
458 fi
459
460 local path="$(vpn_security_policies_path ${name})"
461
462 if ! settings_read "${path}" ${args}; then
463 log ERROR "Could not read settings for vpn security policy ${name}"
464 return ${EXIT_ERROR}
465 fi
466 }
467
468 # Returns the path to a the configuration fora given name
469 vpn_security_policies_path() {
470 assert [ $# -eq 1 ]
471
472 local name=${1}
473
474 if vpn_security_policies_check_readonly "${name}"; then
475 echo "${NETWORK_SHARE_DIR}/vpn/security-policies/${name}"
476 else
477 echo "${NETWORK_CONFIG_DIR}/vpn/security-policies/${name}"
478 fi
479 }
480
481 # Print the content of a vpn security policy configuration file in a nice way
482 vpn_security_policies_show() {
483 assert [ $# -eq 1 ]
484
485 local name=${1}
486
487 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
488 if ! vpn_security_policies_read_config ${name}; then
489 return ${EXIT_ERROR}
490 fi
491
492 cli_print_fmt1 0 "Security Policy: ${name}"
493 cli_space
494
495 # This could be done in a loop but a loop is much more complicated
496 # because we print 'Group Types' but the variable is named 'GROUP_TYPES'
497 cli_print_fmt1 1 "Ciphers:"
498 local cipher
499 for cipher in ${CIPHER}; do
500 cli_print_fmt1 2 "${VPN_SUPPORTED_CIPHERS[${cipher}]-${cipher}}"
501 done
502 cli_space
503
504 cli_print_fmt1 1 "Integrity:"
505 local integrity
506 for integrity in ${INTEGRITY}; do
507 cli_print_fmt1 2 "${VPN_SUPPORTED_INTEGRITY[${integrity}]-${integrity}}"
508 done
509 cli_space
510
511 cli_print_fmt1 1 "Group Types:"
512 local group_type
513 for group_type in ${GROUP_TYPE}; do
514 cli_print_fmt1 2 "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]-${group_type}}"
515 done
516 cli_space
517
518 cli_print_fmt1 1 "Key Exchange:" "${KEY_EXCHANGE}"
519
520 # Key Lifetime
521 if isinteger LIFETIME && [ ${LIFETIME} -gt 0 ]; then
522 cli_print_fmt1 1 "Key Lifetime:" "$(format_time ${LIFETIME})"
523 else
524 log ERROR "The value for Key Lifetime is not a valid integer greater zero."
525 fi
526
527 # PFS
528 if enabled PFS; then
529 cli_print_fmt1 1 "Perfect Forward Secrecy:" "enabled"
530 else
531 cli_print_fmt1 1 "Perfect Forward Secrecy:" "disabled"
532 fi
533 cli_space
534
535 # Compression
536 if enabled COMPRESSION; then
537 cli_print_fmt1 1 "Compression:" "enabled"
538 else
539 cli_print_fmt1 1 "Compression:" "disabled"
540 fi
541 cli_space
542 }
543
544 # This function checks if a vpn security policy exists
545 # Returns True when yes and false when not
546 vpn_security_policy_exists() {
547 assert [ $# -eq 1 ]
548
549 local name=${1}
550
551 local path=$(vpn_security_policies_path "${name}")
552
553 [ -f ${path} ] && return ${EXIT_TRUE} || return ${EXIT_FALSE}
554 }
555
556
557 # This function parses the parameters for the 'cipher' command
558 vpn_security_policies_cipher(){
559 local name=${1}
560 shift
561
562 if [ $# -eq 0 ]; then
563 log ERROR "You must pass at least one value after cipher"
564 return ${EXIT_ERROR}
565 fi
566
567 local CIPHER
568 if ! vpn_security_policies_read_config ${name} "CIPHER"; then
569 return ${EXIT_ERROR}
570 fi
571
572 # Remove duplicated entries to proceed the list safely
573 CIPHER="$(list_unique ${CIPHER})"
574
575 local ciphers_added
576 local ciphers_removed
577 local ciphers_set
578
579 while [ $# -gt 0 ]; do
580 local arg="${1}"
581
582 case "${arg}" in
583 +*)
584 list_append ciphers_added "${arg:1}"
585 ;;
586 -*)
587 list_append ciphers_removed "${arg:1}"
588 ;;
589 [A-Z0-9]*)
590 list_append ciphers_set "${arg}"
591 ;;
592 *)
593 error "Invalid argument: ${arg}"
594 return ${EXIT_ERROR}
595 ;;
596 esac
597 shift
598 done
599
600 # Check if the user is trying a mixed operation
601 if ! list_is_empty ciphers_set && (! list_is_empty ciphers_added || ! list_is_empty ciphers_removed); then
602 error "You cannot reset the cipher list and add or remove ciphers at the same time"
603 return ${EXIT_ERROR}
604 fi
605
606 # Set new cipher list
607 if ! list_is_empty ciphers_set; then
608 # Check if all ciphers are valid
609 local cipher
610 for cipher in ${ciphers_set}; do
611 if ! vpn_security_policies_cipher_supported ${cipher}; then
612 error "Unsupported cipher: ${cipher}"
613 return ${EXIT_ERROR}
614 fi
615 done
616
617 CIPHER="${ciphers_set}"
618
619 # Perform incremental updates
620 else
621 local cipher
622
623 # Perform all removals
624 for cipher in ${ciphers_removed}; do
625 if ! list_remove CIPHER ${cipher}; then
626 warning "${cipher} was not on the list and could not be removed"
627 fi
628 done
629
630 for cipher in ${ciphers_added}; do
631 if vpn_security_policies_cipher_supported ${cipher}; then
632 if ! list_append_unique CIPHER ${cipher}; then
633 warning "${cipher} is already on the cipher list"
634 fi
635 else
636 warning "${cipher} is unknown or unsupported and could not be added"
637 fi
638 done
639 fi
640
641 # Check if the list contain at least one valid cipher
642 if list_is_empty CIPHER; then
643 error "Cannot save an empty cipher list"
644 return ${EXIT_ERROR}
645 fi
646
647 # Save everything
648 if ! vpn_security_policies_write_config_key ${name} "CIPHER" ${CIPHER}; then
649 log ERROR "The changes for the vpn security policy ${name} could not be written."
650 fi
651
652 cli_headline 1 "Current cipher list for ${name}:"
653 for cipher in ${CIPHER}; do
654 cli_print_fmt1 1 "${cipher}" "${VPN_SUPPORTED_CIPHERS[${cipher}]}"
655 done
656 }
657
658 # This function parses the parameters for the 'compression' command
659 vpn_security_policies_compression(){
660 local name=${1}
661 local value=${2}
662
663 # Check if we get only one argument after compression <name>
664 if [ ! $# -eq 2 ]; then
665 log ERROR "The number of arguments do not match. Only one argument after compression is allowed."
666 return ${EXIT_ERROR}
667 fi
668
669 if ! isbool value; then
670 # We suggest only two values to avoid overburding the user.
671 log ERROR "Invalid Argument ${value}"
672 return ${EXIT_ERROR}
673 fi
674
675 vpn_security_policies_write_config_key "${name}" "COMPRESSION" "${value}"
676 }
677
678 # This function parses the parameters for the 'group-type' command
679 vpn_security_policies_group_type(){
680 local name=${1}
681 shift
682
683 if [ $# -eq 0 ]; then
684 log ERROR "You must pass at least one value after group-type"
685 return ${EXIT_ERROR}
686 fi
687
688 local GROUP_TYPE
689 if ! vpn_security_policies_read_config ${name} "GROUP_TYPE"; then
690 return ${EXIT_ERROR}
691 fi
692
693 # Remove duplicated entries to proceed the list safely
694 GROUP_TYPE="$(list_unique ${GROUP_TYPE})"
695
696 local group_types_added
697 local group_types_removed
698 local group_types_set
699
700 while [ $# -gt 0 ]; do
701 local arg="${1}"
702
703 case "${arg}" in
704 +*)
705 list_append group_types_added "${arg:1}"
706 ;;
707 -*)
708 list_append group_types_removed "${arg:1}"
709 ;;
710 [A-Z0-9]*)
711 list_append group_types_set "${arg}"
712 ;;
713 *)
714 error "Invalid argument: ${arg}"
715 return ${EXIT_ERROR}
716 ;;
717 esac
718 shift
719 done
720
721 # Check if the user is trying a mixed operation
722 if ! list_is_empty group_types_set && (! list_is_empty group_types_added || ! list_is_empty group_types_removed); then
723 error "You cannot reset the group type list and add or remove group types at the same time"
724 return ${EXIT_ERROR}
725 fi
726
727 # Set new group type list
728 if ! list_is_empty group_types_set; then
729 # Check if all group types are valid
730 local group_type
731 for group_type in ${group_types_set}; do
732 if ! vpn_security_policies_group_type_supported ${group_type}; then
733 error "Unsupported group type: ${group_type}"
734 return ${EXIT_ERROR}
735 fi
736 done
737
738 GROUP_TYPE="${group_types_set}"
739
740 # Perform incremental updates
741 else
742 local group_type
743
744 # Perform all removals
745 for group_type in ${group_types_removed}; do
746 if ! list_remove GROUP_TYPE ${group_type}; then
747 warning "${group_type} was not on the list and could not be removed"
748 fi
749 done
750
751 for group_type in ${group_types_added}; do
752 if vpn_security_policies_group_type_supported ${group_type}; then
753 if ! list_append_unique GROUP_TYPE ${group_type}; then
754 warning "${group_type} is already on the group type list"
755 fi
756 else
757 warning "${group_type} is unknown or unsupported and could not be added"
758 fi
759 done
760 fi
761
762 # Check if the list contain at least one valid group_type
763 if list_is_empty GROUP_TYPE; then
764 error "Cannot save an empty group type list"
765 return ${EXIT_ERROR}
766 fi
767
768 # Save everything
769 if ! vpn_security_policies_write_config_key ${name} "GROUP_TYPE" ${GROUP_TYPE}; then
770 log ERROR "The changes for the vpn security policy ${name} could not be written."
771 fi
772
773 cli_headline 1 "Current group type list for ${name}:"
774 for group_type in ${GROUP_TYPE}; do
775 cli_print_fmt1 1 "${group_type}" "${VPN_SUPPORTED_GROUP_TYPES[${group_type}]}"
776 done
777 }
778
779 # This function parses the parameters for the 'integrity' command
780 vpn_security_policies_integrity(){
781 local name=${1}
782 shift
783
784 if [ $# -eq 0 ]; then
785 log ERROR "You must pass at least one value after integrity"
786 return ${EXIT_ERROR}
787 fi
788
789 local INTEGRITY
790 if ! vpn_security_policies_read_config ${name} "INTEGRITY"; then
791 return ${EXIT_ERROR}
792 fi
793
794 # Remove duplicated entries to proceed the list safely
795 INTEGRITY="$(list_unique ${INTEGRITY})"
796
797 local integritys_added
798 local integritys_removed
799 local integritys_set
800
801 while [ $# -gt 0 ]; do
802 local arg="${1}"
803
804 case "${arg}" in
805 +*)
806 list_append integritys_added "${arg:1}"
807 ;;
808 -*)
809 list_append integritys_removed "${arg:1}"
810 ;;
811 [A-Z0-9]*)
812 list_append integritys_set "${arg}"
813 ;;
814 *)
815 error "Invalid argument: ${arg}"
816 return ${EXIT_ERROR}
817 ;;
818 esac
819 shift
820 done
821
822 # Check if the user is trying a mixed operation
823 if ! list_is_empty integritys_set && (! list_is_empty integritys_added || ! list_is_empty integritys_removed); then
824 error "You cannot reset the integrity hashes list and add or remove integrity hashes at the same time"
825 return ${EXIT_ERROR}
826 fi
827
828 # Set new integrity list
829 if ! list_is_empty integritys_set; then
830 # Check if all integrity hashes are valid
831 local integrity
832 for integrity in ${integritys_set}; do
833 if ! vpn_security_policies_integrity_supported ${integrity}; then
834 error "Unsupported integrity hash: ${integrity}"
835 return ${EXIT_ERROR}
836 fi
837 done
838
839 INTEGRITY="${integritys_set}"
840
841 # Perform incremental updates
842 else
843 local integrity
844
845 # Perform all removals
846 for integrity in ${integritys_removed}; do
847 if ! list_remove INTEGRITY ${integrity}; then
848 warning "${integrity} was not on the list and could not be removed"
849 fi
850 done
851
852 for integrity in ${integritys_added}; do
853 if vpn_security_policies_integrity_supported ${integrity}; then
854 if ! list_append_unique INTEGRITY ${integrity}; then
855 warning "${integrity} is already on the integrity list"
856 fi
857 else
858 warning "${integrity} is unknown or unsupported and could not be added"
859 fi
860 done
861 fi
862
863 # Check if the list contain at least one valid integrity
864 if list_is_empty INTEGRITY; then
865 error "Cannot save an empty integrity hashes list"
866 return ${EXIT_ERROR}
867 fi
868
869 # Save everything
870 if ! vpn_security_policies_write_config_key ${name} "INTEGRITY" ${INTEGRITY}; then
871 log ERROR "The changes for the vpn security policy ${name} could not be written."
872 fi
873
874 cli_headline 1 "Current integrity hashes list for ${name}:"
875 for integrity in ${INTEGRITY}; do
876 cli_print_fmt1 1 "${integrity}" "${VPN_SUPPORTED_INTEGRITY[${integrity}]}"
877 done
878 }
879
880 # This function parses the parameters for the 'key-exchange' command
881 vpn_security_policies_key_exchange() {
882 local name=${1}
883 local value=${2}
884
885 # Check if we get only one argument after key-exchange <name>
886 if [ ! $# -eq 2 ]; then
887 log ERROR "The number of arguments do not match. Only argument after key-exchange is allowed."
888 return ${EXIT_ERROR}
889 fi
890
891 if ! isoneof value "ikev1" "ikev2" "IKEV1" "IKEV2"; then
892 log ERROR "Invalid Argument ${value}"
893 return ${EXIT_ERROR}
894 fi
895
896 vpn_security_policies_write_config_key "${name}" "KEY_EXCHANGE" "${value,,}"
897 }
898
899 # This function parses the parameters for the 'lifetime' command.
900 vpn_security_policies_lifetime(){
901 local name=${1}
902 shift
903
904 local value=$@
905
906 # Check if we get only one argument after lifetime <name>
907 if [ ! $# -ge 1 ]; then
908 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"
909 return ${EXIT_ERROR}
910 fi
911
912 if ! isinteger value; then
913 value=$(parse_time "$@")
914 if [ ! $? -eq 0 ]; then
915 log ERROR "Parsing the passed time was not sucessful please check the passed values."
916 return ${EXIT_ERROR}
917 fi
918 fi
919
920 if [ ${value} -le 0 ]; then
921 log ERROR "The passed time value must be in the sum greater zero seconds."
922 return ${EXIT_ERROR}
923 fi
924
925 vpn_security_policies_write_config_key "${name}" "LIFETIME" "${value}"
926 }
927
928 # This function parses the parameters for the 'pfs' command
929 vpn_security_policies_pfs(){
930 local name=${1}
931 local value=${2}
932
933 # Check if we get only one argument after pfs <name>
934 if [ ! $# -eq 2 ]; then
935 log ERROR "The number of arguments do not match. Only argument after pfs is allowed."
936 return ${EXIT_ERROR}
937 fi
938
939 if [ ! $# -eq 2 ] || ! isbool value; then
940 # We suggest only two values to avoid overburding the user.
941 log ERROR "Invalid Argument ${value}"
942 return ${EXIT_ERROR}
943 fi
944
945 vpn_security_policies_write_config_key "${name}" "PFS" "${value}"
946 }
947
948 # This function checks if a vpn security policy name is valid
949 # Allowed are only A-Za-z0-9
950 vpn_security_policies_check_name() {
951 assert [ $# -eq 1 ]
952
953 local name=${1}
954
955 [[ ${name} =~ [^[:alnum:]$] ]]
956 }
957
958 # Function that creates based on the paramters one ore more new vpn security policies
959 vpn_security_policies_new() {
960 if [ $# -gt 1 ]; then
961 error "Too many arguments"
962 return ${EXIT_ERROR}
963 fi
964
965 local name="${1}"
966 if ! isset name; then
967 error "Please provide a name"
968 return ${EXIT_ERROR}
969 fi
970
971 # Check for duplicates
972 if vpn_security_policy_exists "${name}"; then
973 error "The VPN security policy with name ${name} already exists"
974 return ${EXIT_ERROR}
975 fi
976
977 # Check if name is valid
978 if vpn_security_policies_check_name "${name}"; then
979 error "'${name}' contains illegal characters"
980 return ${EXIT_ERROR}
981 fi
982
983 # Check if we have a read-only policy with the same name
984 if vpn_security_policies_check_readonly "${name}"; then
985 error "The VPN security policy ${name} is read-only"
986 return ${EXIT_ERROR}
987 fi
988
989 # Check if our source policy exists
990 if ! vpn_security_policy_exists "${VPN_DEFAULT_SECURITY_POLICY}"; then
991 error "Default VPN Security Policy '${VPN_DEFAULT_SECURITY_POLICY}' does not exist"
992 return ${EXIT_ERROR}
993 fi
994
995 log DEBUG "Creating VPN Security Policy ${name}"
996
997 if copy "$(vpn_security_policies_path "${VPN_DEFAULT_SECURITY_POLICY}")" \
998 "$(vpn_security_policies_path ${name})"; then
999 log INFO "VPN Security Policy ${name} successfully created"
1000 else
1001 log ERROR "Could not create VPN Security Policy ${name}"
1002 return ${EXIT_ERROR}
1003 fi
1004
1005 # Show the newly created policy
1006 vpn_security_policies_show "${name}"
1007 }
1008
1009 # Function that deletes based on the passed parameters one ore more vpn security policies
1010 vpn_security_policies_destroy() {
1011 local name
1012 for name in "$@"; do
1013 if ! vpn_security_policy_exists ${name}; then
1014 log ERROR "The vpn security policy ${name} does not exist."
1015 continue
1016 fi
1017
1018 if vpn_security_policies_check_readonly ${name}; then
1019 log ERROR "The vpn security policy ${name} cannot be deleted."
1020 continue
1021 fi
1022
1023 log DEBUG "Deleting vpn security policy ${name}"
1024 settings_remove $(vpn_security_policies_path ${name})
1025
1026 # Delete cache
1027 rm -rf "${NETWORK_CACHE_DIR}/vpn/security-policies/${name}"
1028 done
1029 }
1030
1031 vpn_security_policies_cipher_supported() {
1032 local cipher=${1}
1033
1034 list_match ${cipher} ${!VPN_SUPPORTED_CIPHERS[@]}
1035 }
1036
1037
1038 vpn_security_policies_group_type_supported() {
1039 local group_type=${1}
1040
1041 list_match ${group_type} ${!VPN_SUPPORTED_GROUP_TYPES[@]}
1042 }
1043
1044 vpn_security_policies_integrity_supported() {
1045 local integrity=${1}
1046
1047 list_match ${integrity} ${!VPN_SUPPORTED_INTEGRITY[@]}
1048 }
1049
1050 vpn_security_policies_cipher_is_aead() {
1051 local cipher=${1}
1052
1053 # All CCM and GCM ciphers are AEAD
1054 if string_match "[CG]CM" "${cipher}"; then
1055 return ${EXIT_TRUE}
1056 fi
1057
1058 # Poly1305 is AEAD
1059 if string_match "POLY1305" "${cipher}"; then
1060 return ${EXIT_TRUE}
1061 fi
1062
1063 return ${EXIT_FALSE}
1064 }
1065
1066 vpn_security_policies_make_ike_proposal() {
1067 local name=${1}
1068
1069 if ! vpn_security_policy_exists ${name}; then
1070 return ${EXIT_ERROR}
1071 fi
1072
1073 local config_path="$(vpn_security_policies_path ${name})"
1074 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/ike-proposal"
1075
1076 # Get data from cache if possible
1077 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1078 fread "${cache_path}"
1079 return ${EXIT_OK}
1080 fi
1081
1082 # No or invalid cache data found
1083 local proposal=$(_vpn_security_policies_make_ike_proposal "${name}")
1084
1085 # Write proposal to cache
1086 if ! make_parent_directory "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1087 log WARNING "Could not write to cache: ${cache_path}"
1088 fi
1089
1090 print "${proposal}"
1091 }
1092
1093 _vpn_security_policies_make_ike_proposal() {
1094 local name=${1}
1095
1096 # Read the config settings
1097 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1098 if ! vpn_security_policies_read_config "${name}"; then
1099 return ${EXIT_ERROR}
1100 fi
1101
1102 local proposals
1103
1104 local cipher
1105 for cipher in ${CIPHER}; do
1106 # Translate cipher
1107 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1108
1109 if ! isset _cipher; then
1110 log WARN "Unsupported cipher: ${cipher}"
1111 continue
1112 fi
1113
1114 if vpn_security_policies_cipher_is_aead "${cipher}"; then
1115 local prf
1116 for prf in ${PSEUDO_RANDOM_FUNCTION}; do
1117 local _prf="${PSEUDO_RANDOM_FUNCTION_TO_STRONGSWAN[${prf}]}"
1118
1119 if ! isset _prf; then
1120 log WARN "Unsupported pseudo random function: ${prf}"
1121 continue
1122 fi
1123
1124 local group_type
1125 for group_type in ${GROUP_TYPE}; do
1126 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1127
1128 if ! isset _group_type; then
1129 log WARN "Unsupported group-type: ${group_type}"
1130 continue
1131 fi
1132
1133 # Put everything together
1134 list_append proposals "${_cipher}-${_prf}-${_group_type}"
1135 done
1136 done
1137 else
1138 local integrity
1139 for integrity in ${INTEGRITY}; do
1140 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1141
1142 if ! isset _integrity; then
1143 log WARN "Unsupported integrity: ${integrity}"
1144 continue
1145 fi
1146
1147 local group_type
1148 for group_type in ${GROUP_TYPE}; do
1149 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1150
1151 if ! isset _group_type; then
1152 log WARN "Unsupported group-type: ${group_type}"
1153 continue
1154 fi
1155
1156 # Put everything together
1157 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1158 done
1159 done
1160 fi
1161 done
1162
1163 # Returns as a comma-separated list
1164 list_join proposals ,
1165 }
1166
1167 vpn_security_policies_make_esp_proposal() {
1168 local name=${1}
1169
1170 if ! vpn_security_policy_exists ${name}; then
1171 return ${EXIT_ERROR}
1172 fi
1173
1174 local config_path="$(vpn_security_policies_path ${name})"
1175 local cache_path="${NETWORK_CACHE_DIR}/vpn/security-policies/${name}/esp-proposal"
1176
1177 # Get data from cache if possible
1178 if file_exists "${cache_path}" && ! file_is_newer_than "${config_path}" "${cache_path}"; then
1179 fread "${cache_path}"
1180 return ${EXIT_OK}
1181 fi
1182
1183 # No or invalid cache data found
1184 local proposal=$(_vpn_security_policies_make_esp_proposal "${name}")
1185
1186 # Write proposal to cache
1187 if ! make_parent_directory "${cache_path}" || ! fwrite "${cache_path}" "${proposal}"; then
1188 log WARNING "Could not write to cache: ${cache_path}"
1189 fi
1190
1191 print "${proposal}"
1192 }
1193
1194 _vpn_security_policies_make_esp_proposal() {
1195 local name=${1}
1196
1197 # Read the config settings
1198 local ${VPN_SECURITY_POLICIES_CONFIG_SETTINGS}
1199 if ! vpn_security_policies_read_config "${name}"; then
1200 return ${EXIT_ERROR}
1201 fi
1202
1203 local proposals
1204
1205 local cipher
1206 for cipher in ${CIPHER}; do
1207 # Translate cipher
1208 local _cipher=${CIPHER_TO_STRONGSWAN[${cipher}]}
1209
1210 if ! isset _cipher; then
1211 log WARN "Unsupported cipher: ${cipher}"
1212 continue
1213 fi
1214
1215 if vpn_security_policies_cipher_is_aead ${cipher}; then
1216 local group_type
1217 for group_type in ${GROUP_TYPE}; do
1218 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1219
1220 if ! isset _group_type; then
1221 log WARN "Unsupported group-type: ${group_type}"
1222 continue
1223 fi
1224
1225 # Put everything together
1226 list_append proposals "${_cipher}-${_group_type}"
1227 done
1228 else
1229 local integrity
1230 for integrity in ${INTEGRITY}; do
1231 local _integrity=${INTEGRITY_TO_STRONGSWAN[${integrity}]}
1232
1233 if ! isset _integrity; then
1234 log WARN "Unsupported integrity: ${integrity}"
1235 continue
1236 fi
1237
1238 local group_type
1239 for group_type in ${GROUP_TYPE}; do
1240 local _group_type=${GROUP_TYPE_TO_STRONGSWAN[${group_type}]}
1241
1242 if ! isset _group_type; then
1243 log WARN "Unsupported group-type: ${group_type}"
1244 continue
1245 fi
1246
1247 # Put everything together
1248 list_append proposals "${_cipher}-${_integrity}-${_group_type}"
1249 done
1250 done
1251 fi
1252 done
1253
1254 # Returns as a comma-separated list
1255 list_join proposals ,
1256 }
1257
1258 # List all security policies
1259 vpn_security_policies_list_all() {
1260 list_directory "${NETWORK_SHARE_DIR}/vpn/security-policies"
1261
1262 # Add all user-defined policies
1263 vpn_security_policies_list_user
1264 }
1265
1266 vpn_security_policies_list_user() {
1267 list_directory "${NETWORK_CONFIG_DIR}/vpn/security-policies"
1268 }