-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
easytls
executable file
·9892 lines (8570 loc) · 269 KB
/
easytls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/sh
# Copyright - negotiable
#
# VERBATUM_COPYRIGHT_HEADER_INCLUDE_NEGOTIABLE
# Easy-TLS -- A Shell-based Easy-RSA extension utility to help manage
# * OpenVPN specific TLS keys
# * Easy-RSA based x509 security credentials
# * Verified 'inline' combined OpenVPN node packages
#
# Copyright (C) 2020 Richard Bonhomme (Friday 13th of March 2020)
# https://github.com/TinCanTech/easy-tls
# All Rights reserved.
#
# This code is released under version 2 of the GNU GPL
# See LICENSE of this project for full licensing details.
#
# Acknowledgement:
# This utility is "written in the style of" and "borrows heavily from" Easy-RSA
#
# Easy-TLS is inspired by syzzer
# See: https://github.com/OpenVPN/openvpn/blob/master/doc/tls-crypt-v2.txt
#
# VERBATUM_COPYRIGHT_HEADER_INCLUDE_NEGOTIABLE
#
# Easy-TLS requirements:
# + Easy-RSA Version 3.0.8
# + OpenVPN Version 2.5
# Set the Easy-TLS version
easytls_version () {
unset -v EASYTLS_VERBOSE
config_use || :
print "Easy-TLS ${EASYTLS_VERSION} (${EASYTLS_STATUS})"
} # => easytls_version ()
############################################################################
#
# EASYTLS HELP Section
#
# Help/usage output to stdout
usage() {
print "
Easy-TLS usage and overview
USAGE: easytls [options] COMMAND [command-options]
A list of commands is shown below. To get detailed usage and help for a
command, use:
./easytls help COMMAND
For a listing of options that can be supplied before the command, use:
./easytls help options
For a list of abbreviated command names, use:
./easytls help abb
For a list of configurable options, use:
./easytls help config
Here is the list of commands available with a short syntax reminder.
Use the 'help' command above to get full usage details.
build :Inter-active menu to build TLS keys
inline :Inter-active menu to build Inline files
remove :Inter-active menu to remove TLS keys and Inline files
script :Inter-active menu to configure Server scripts
selfsign :Inter-active menu to build and inline self-signed certificates
init | init-tls <hash_algorithm> no-ca
cf | config
|
sss | self-sign-server <server_filename_base> (No-CA mode only)
ssc | self-sign-client <client_filename_base> (No-CA mode only)
|
bta | build-tls-auth
btc | build-tls-crypt
bc2s | build-tls-crypt-v2-server <server_common_name>
bc2c | build-tls-crypt-v2-client
| <server_common_name> <client_common_name> <HW-ADDR> <IP-ADDR>
|
ita | inline-tls-auth <filename_base> <key_direction> [ cmd-opts ]
itc | inline-tls-crypt <filename_base> [ cmd-opts ]
ic2 | inline-tls-crypt-v2 <filename_base> [ cmd-opts ]
|
bc2gc | build-tls-crypt-v2-group-client
| <server_common_name> <client_group_name> <HW-ADDR> <IP-ADDR>
ic2gc | inline-tls-crypt-v2-group-client
| <client_common_name> <client_group_name> [ cmd-opts ]
|
s | status [ cmd-opts ]
rk | remove-tlskey <client_filename_base>
rgk | remove-group-tlskey <client_filename_base> <Group-Name>
ri | remove-inline <filename_base>
rgi | remove-group-inline <Client-Name> <Group-Name>
is | inline-show <filename_base>
| inline-index-rebuild
ix | inline-expire <filename_base>
cx | cert-expire <filename_base> | <ca>
d | disable <filename_base> (Or display the current disabled list)
e | enable <filename_base> (Or display the current disabled list)
| disabled-list-rehash
sid | save-id
ver | version
| upgrade
Easy-TLS also has a useful Howto and wiki with expanded help and examples:
* https://github.com/TinCanTech/easy-tls/blob/master/easytls-howto-ii.md
* https://github.com/TinCanTech/easy-tls/wiki"
# collect/show dir status:
err_source="Not defined: vars autodetect failed and no value provided"
work_dir="${EASYRSA:-$err_source}"
pki_dir="${EASYRSA_PKI:-$err_source}"
tls_dir="${EASYTLS_PKI:-$err_source}"
print "
DIRECTORY STATUS (commands would take effect on these locations)
EASYTLS: ${work_dir}
PKI: ${pki_dir}
TLS: ${tls_dir}"
} # => usage()
# Detailed command help
# When called with no args, calls usage(), otherwise shows help for a command
cmd_help() {
text=""
opts=""
case "${1}" in
init|init-tls) text="
init-tls <hash_algorithm> no-ca
Removes & re-initializes the TLS-key directory.
* Easy-RSA is also required to be initialised.
<hash_algorithm> - Specify a hash algorithm.
Default SHA256 - Alternative SHA1
nc|noca|no-ca - Use Easy-TLS without an Easy-RSA CA Certificate Authority.
* Use 'no-ca' to build self-signed certificates and fingerprints.
If you have upgraded an old copy of Easy-TLS to the latest version
then it may be possible to use 'easytls upgrade' or 'easytls rehash'
to establish a functioning easytls data directory." ;;
build|inline|remove|script|selfsign) text="
Inter-active menus to build, inline, remove
and configure various Easy-TLS files" ;;
bta|build-tls-auth) text="
build-tls-auth
Create an OpenVPN TLS auth PSK (tls-auth.key)" ;;
btc|build-tls-crypt) text="
build-tls-crypt
Create an OpenVPN TLS crypt PSK (tls-crypt.key)" ;;
btc2s|btv2s|btcv2s|build-tls-crypt-v2-server) text="
build-tls-crypt-v2-server <server_filename_base>
Create an OpenVPN TLS crypt V2 Server key" ;;
btc2c|btv2c|btcv2c|build-tls-crypt-v2-client)
EASYTLS_github="https://github.com/TinCanTech/easy-tls/wiki"
EASYTLS_url="15-=-Multiple-TLS-Crypt-V2-Keys-per-X509-Certificate"
text="
build-tls-crypt-v2-client
<server_filename_base> <client_filename_base> <HW-ADDR> <HW-ADDR>
Create an OpenVPN TLS crypt V2 Client key
The metadata can also be used to specify Hardware Addresses which this
key is allowed to be used from. Use easytls-cryptv2-client-connect.sh
to verify the HW-ADDR.
Note: ALL scripts are required to successfully verify Hardware.
Each X509 Client can have multiple TLS-Crypt-V2 keys by using option:
-k|--subkey-name=<NAME>
This allows an unlimited number of keys, see the following wiki article
for further details:
${EASYTLS_github}/${EASYTLS_url}" ;;
bc2gs|btc2gs|btv2gs|btcv2gs|build-tls-crypt-v2-group-server)
text="
build-tls-crypt-v2-group-server <Server_Group_Name>
The Server Group name is simply a name for the key file." ;;
bc2gc|btc2gc|btv2gc|btcv2gc|build-tls-crypt-v2-group-client)
text="
build-tls-crypt-v2-group-client <Server_Group_Name> <Client_Group_Name>
The Client Group name is simply a name for the key file." ;;
rt|rtk|remove-tlskey) text="
remove-tlskey <client_filename_base>
Remove a TLS-Crypt-V2 Client key and update the tlskey-index." ;;
rgt|rgk|rgtk|remove-group-tlskey) text="
remove-group-tlskey <Group-Key-Name>
Remove a TLS-Crypt-V2 GROUP Client key and update the tlskey-index." ;;
sid|save-id) text="
save-id [ no options ]
Save the CA-Identity to easytls/easytls-ca-identity.txt
CA-Identity is the Hex ONLY value of the CA finger print.
This can then by used by easytls-cryptv2-verify.sh as a time-saver
by not needing to load OpenSSL to generate the CA fingerprint.
Combining this with 'easytls-cryptv2-verify.sh --verify-via-index',
OpenSSL binary never needs to be loaded by easytls-cryptv2-verify.sh
See EasyTLS Howto for an example." ;;
s|status) text="
status [ cmd-opts ]
Compare Easy-RSA certificate(s) to Easy-TLS inline(s) status"
opts="
val|valid - List only Easy-RSA valid certificates
rev|revoked - List only Easy-RSA revoked certificates
inl|inline - List only Easy-TLS inline files
inv|invalid - List only Easy-TLS invalid inline files
ren|renewed - List only Easy-RSA renewed certificates
tls|tlskeys - List only Easy-TLS known TLS keys" ;;
ita|inline-tls-auth) text="
inline-tls-auth <filename_base> <key_direction> [ cmd-opts ]
Create a complete OpenVPN node package from Easy-RSA and Easy-TLS files
for VPN node <filename_base> using the Easy-TLS TLS auth file
<key_direction> '0' or '1' (key-direction is mandatory)"
opts="
no-key - do not require an x509 key (default: key is required)
add-dh - inline Diffie-Hellman parameters file (Server only)
Default file is easyrsa3/pki/dh.pem
To specify an alternative file use the --dh option" ;;
itc|inline-tls-crypt) text="
inline-tls-crypt <filename_base> [ cmd-opts ]
Create a complete OpenVPN node package from Easy-RSA and Easy-TLS files
for VPN node <filename_base> using the Easy-TLS TLS crypt file"
opts="
no-key - do not require an x509 key (default: key is required)
add-dh - inline Diffie-Hellman parameters file (Server only)
Default file is easyrsa3/pki/dh.pem
To specify an alternative file use the --dh option" ;;
itc2|itv2|itcv2|inline-tls-crypt-v2) text="
inline-tls-crypt-v2 <filename_base> [ cmd-opts ]
Create a complete OpenVPN node package from Easy-RSA and Easy-TLS files
for VPN node <filename_base> using the Easy-TLS TLS crypt v2 file"
opts="
no-key - do not require an x509 key (default: key is required)
add-dh - inline Diffie-Hellman parameters file (Server only)
Default file is easyrsa3/pki/dh.pem
To specify an alternative file use the --dh option
no-md - Do not add public metadata details to inline file
add-hw - Include hardware address in metadata details" ;;
ic2gc|itv2gc|itcv2gc|inline-tls-crypt-v2-group-client) text="
inline-tls-crypt-v2-group-client
<client_common_name> <client_group_name> [ cmd-opts ]
Create a complete OpenVPN node package from Easy-RSA and Easy-TLS files
for VPN node <client_common_name> using the Easy-TLS GROUP key file"
opts="
no-key - do not require an x509 key (default: key is required)
add-dh - inline Diffie-Hellman parameters file (Server only)
Default file is easyrsa3/pki/dh.pem
To specify an alternative file use the --dh option
no-md - Do not add public metadata details to inline file
add-hw - Include hardware address in metadata details" ;;
ri|ril|remove-inline) text="
remove-inline <filename_base>
Delete <filename_base>.inline and update the inline-index
When an Easy-RSA certificate is revoked then the inline file is invalid" ;;
rgi|rgil|remove-group-inline) text="
remove-group-inline <Client-Name> <Group-Name>
Delete <Client-Name>-<Group-Name>.inline and update the inline-index" ;;
is|inline-show) text="
inline-show <filename_base>
Copy <filename_base>.inline to stdout" ;;
inline-index-rebuild) text="
inline-index-rebuild
Rebuild easytls-index.txt
If you need to do this then you may have found a bug, please
raise an issue https://github.com/TinCanTech/easy-tls/issues" ;;
ix|inline-expire) text="
inline-expire <filename_base>
Display inline expiry Date for <filename_base>"
opts="
If no <filename_base> is given then list all inline expiry Dates" ;;
cx|cert-expire) text="
cert-expire <filename_base> | <ca>
Display certificate expiry Date for <filename_base>"
opts="
If <filename_base> is 'ca' then show ca.crt expiry
If no <filename_base> is given then list all certificate expiry Dates" ;;
d|disable) text="
d|disable <filename_base>
Add serial number for <filename_base> to the disabled list
for immediate use by the easytls-cryptv2-verify.sh script.
This also supports --sub-key-name for clients with multiple keys.
If no <filename_base> is given then show the disabled-list." ;;
e|enable) text="
e|enable <filename_base>
Remove serial number for <filename_base> from the disabled list
for immediate use by the easytls-cryptv2-verify.sh script.
This also supports --sub-key-name for clients with multiple keys.
If no <filename_base> is given then show the disabled-list." ;;
rehash) text="
rehash
Rehash the master-hash if the current hash is corrupted.
These test commands also exist:
gmh|generate-master-hash - Generate your current Master Hash.
vmh|verify-master-hash - Verify your current Master Hash.
smh|save-master-hash - Save your current Master Hash." ;;
sss|self-sign-server) text="
self-sign-server
Build a self signed server certificate and key.
Also see 'help options' for: -r|--ss-peer-fingerprint=<CommonName>" ;;
ssc|self-sign-client) text="
self-sign-client
Build a self signed client certificate and key.
Also see 'help options' for: -r|--ss-peer-fingerprint=<CommonName>" ;;
upgrade) text="
upgrade
To upgrade from an older version of Easy-TLS which did not create
the required folders and files. Very limited usage." ;;
options)
opt_usage ;;
abb)
opt_abbreviations ;;
cf|cfg|config)
opt_config ;;
ver|version) text="
Show version information." ;;
"") usage ;;
*) text="
Unknown command: '${1}' (try without commands for a list of commands)" ;;
esac
# display the help text
[ -z "${text}" ] || print "${text}"
[ -z "${opts}" ] || print "
cmd-opts is an optional set of command options from this list:
${opts}"
easytls_verbose
} # => cmd_help()
# Options usage
opt_usage() {
print "
Easy-TLS Global Option Flags
The following options may be provided before the command. Options specified
at runtime override env-vars and any 'vars' file in use. Unless noted,
non-empty values to options are mandatory.
General options:
-V Version
--batch Set automatic (no-prompts when possible) mode.
-v|--verbose Verbose output.
-s|--silent Silence all message output except prompts.
-p|--pki-dir=<DIR> Declare the EasyRSA PKI directory.
--vars='FILE' Define a specific 'vars' file to use for Easy-RSA config.
--dh='FILE' Define an alternate Diffie-Hellman parameters file.
-n|--no-auto-check For performance you can disable auto-check.
-y|--why-disable-file-hash
To temporarily disable file hash verification.
-g|--custom-group=<GROUP-NAME>
<CUSTOM-GROUP> is an optional single word which will be used
in .inline files and TLS-Crypt-V2 client key metadata to identify
the group to which this TLS-Crypt-V2 client key belongs.
Once set and used, the <CUSTOM-GROUP> must not be changed.
The <CUSTOM-GROUP> is also matched in easytls-cryptv2-verify.sh
by using the command line switch --c|custom-group=XYZ
-k|--subkey|--sub-key-name=<SUBKEY-NAME>
This allows for one single X509 client certificate to have multiple
TLS-Crypt-V2 client keys associated with it. For example, the same
X509 certificate can be used from different locations with unique
TLS-Crypt-V2 client keys.
-a|--ss-age=<AGE> Self-signed certificate age. (Default: 3650 days)
-w|--ss-password Prompt for a password to encrypt a self-signed key.
-c|--ss-eccurve=<CURVE_NAME>
Specify an alternate Elliptic Curve for a self-signed key.
(Default: secp384r1)
-r|--ss-peer-fingerprint=<CommonName>
When inlining a self-signed client certificate, specify the commonName
of the server certificate to share fingerprints of each node.
The fingerprint of the server will be added to the client inline file
and the fingerprint of the client will be added to the server inline file.
-i|--inline When building a TLS Crypt V2 key, also build the Inline file.
(Fix: This only accepts default options for inline-tls-crypt-v2)
-t|--tmp-dir=<DIR> Temp directory where server-scripts write data.
Default: *nix /tmp/easytls
Windows C:/Windows/Temp/easytls
-b|--base-dir=<DIR> Path to OpenVPN base directory. (Windows Only)
Default: C:/Progra~1/OpenVPN
-o|--ovpnbin-dir=<DIR> Path to OpenVPN bin directory. (Windows Only)
Default: C:/Progra~1/OpenVPN/bin
-e|--ersabin-dir=<DIR> Path to Easy-RSA3 bin directory. (Windows Only)
Default: C:/Progra~1/Openvpn/easy-rsa/bin
"
} # => opt_usage()
# Option abbreviations
opt_abbreviations () {
print "
Easy-TLS abbreviations:
config - cf | cfg
init-tls - init
build-tls-auth - bta
build-tls-crypt - btc
build-tls-crypt-v2-server - bc2s | btc2s | btv2s | btcv2s
build-tls-crypt-v2-client - bc2c | btc2c | btv2c | btcv2c
build-tls-crypt-v2-group-server - bc2gs | btc2gs | btcv2gs
build-tls-crypt-v2-group-client - bc2gc | btc2gc | btcv2gc
remove-tlskey - rt | rk | rtk
save-id - sid
status - s
Easy-RSA valid certificates - val | valid
Easy-RSA revoked certificates - rev | revoked
Easy-TLS inline files - inl | inline
Easy-TLS invalid inline files - inv | invalid
Easy-RSA renewed certificates - ren | renewed
Easy-TLS known TLS keys - tls | tlskeys
inline-tls-auth - ita
inline-tls-crypt - itc
inline-tls-crypt-v2 - ic2 | itc2 | itv2 | itcv2
inline-tls-crypt-v2-group-client - ic2gc | itc2gc | itcv2gc
remove-inline - ri | ril
remove-group-inline - rgi | rgil
inline-show - is
inline-expire - ix
cert-expire - cx
disable - d
enable - e
rehash - No abbreviation
self-sign-server - sss
self-sign-client - ssc
"
} # => opt_abbreviations ()
# Option config
opt_config () {
print "
Easy-TLS configurable options (Abbreviation|full option):
cg|custom.group NAME
Save the Custom Group NAME - NAME must be a single contiguous word.
This group can be changed.
Using easytls-cryptv2-verify.sh in your openvpn server, you can
configure multiple group names to match. eg. 'Home Office Remote'
td|tmp-dir
Set server scripts temporary directory
ac|auto.check on|off
Always run auto-check
Toggle auto.check on|off.
co|custom.openvpn '/full/path/to/openvpn(.exe)'
Save your custom openvpn binary location.
EG: /usr/local/bin/openvpn
Quotes are not required.
EG: 'C:/Program Files/OpenVPN/mybin/openvpn.exe'
Quotes are required for spaces.
Back-slash is not support, use Forward-slash '/' ONLY.
im|inline.metadata on|off
Add metadata to inline file
Toggle inline-file metadata on|off.
ih|inline.hardware on|off
Include metadata hardware-addresses in metadata for inline file
Toggle inline-file hardware-address metadata on|off.
addition (No short form)
Add arbitrary item to config
This only adds the <label = > item to the config.
The item must then be configured.
This allows for future additions to config without the need to hack.
deletion (No short form)
delete an item from config
Should the need arise, items can now be deleted also.
"
} # => opt_config ()
############################################################################
#
# EASYTLS TO EASYRSA3 COMPATIBILITY Section
#
# Wrapper around 'printf' - clobber 'print' since it's not POSIX anyway
print () {
[ -z "${EASYTLS_SILENT}" ] || return 0
"${EASYTLS_PRINTF}" "%s\n" "${*}"
} # => print ()
# Exit fatally with a message to stderr
# present even with EASYTLS_BATCH as these are fatal problems
die () {
unset -v EASYTLS_SILENT
print
error_msg
print "Error: ${1}" 1>&2
print
[ -n "${help_note}" ] && print "Note: ${help_note}" && print
easytls_version
exit "${2:-1}"
} # => die ()
# Specific error messages from sub-functions which do not use die()
error_msg () {
if [ -n "${1}" ]; then
error_log="${error_log:-ERROR:}
${1}"
else
print "Error log: ${error_log}"
print
fi
} # => error_msg ()
# Missing files
missing_file () {
die "Missing file: ${1}"
} # => missing_file ()
# Fatal errors prior to deps
fatal_opt () {
if [ -z "${1}" ]; then
# If fatal_msg is not set then ok
[ -n "${fatal_msg}" ] || return 0
else
# Add the message to fatal_msg and ok
fatal_msg="${fatal_msg}
${1}"
return 0
fi
# fatal_msg is set
die "${fatal_msg}"
} # => fatal_opt ()
# remove temp files and do terminal cleanups
cleanup () {
"${EASYTLS_RM}" -f "${EASYTLS_SSL_CONF}" \
"${EASYTLS_TEMP_LIST}" "${EASYTLS_TEMP_RECORD}"
if [ -d "${EASYTLS_TEMP_LOCK}" ]; then
"${EASYTLS_RM}" -r "${EASYTLS_TEMP_LOCK}"
easytls_verbose "Lock released"
fi
if [ -n "${EASYTLS_FOR_WINDOWS}" ]; then
# shellcheck disable=SC3040
set -o echo
else
[ -t 1 ] && stty echo
fi
[ -n "${EASYTLS_SILENT}" ] || echo "" # just to get a clean line
} # => cleanup ()
# non-fatal warning output
warn () {
[ -z "${EASYTLS_SILENT}" ] || return 0
print "
WARNING:
${1}
" 1>&2
} # => warn ()
# informational notices to stdout
notice () {
[ -z "${EASYTLS_QUIET}" ] || return 0
[ -z "${EASYTLS_SILENT}" ] || return 0
print "
${1}"
} # => notice ()
# intent confirmation helper func
# returns without prompting in EASYTLS_BATCH
confirm () {
[ -z "${require_batch}" ] || return 0
[ -z "${EASYTLS_BATCH}" ] || return 0
prompt="${1}"
value="${2}"
msg="${3}"
input=""
print "
${msg}
Type the word '${value}' to continue, or any other input to abort."
"${EASYTLS_PRINTF}" '\n%s' " ${prompt}"
read -r input
if [ "${input}" = "${value}" ]; then
"${EASYTLS_PRINTF}" "\n"
return 0
fi
notice "Aborting without confirmation."
exit 9
} # => confirm ()
# Check for defined EASYRSA_PKI
vars_source_check () {
[ -n "${EASYRSA_PKI}" ] || die "EASYRSA_PKI env-var undefined"
} # => vars_source_check ()
# Basic sanity-check of PKI init and complain if missing
verify_pki_init () {
# check that the pki dir exists
vars_source_check
[ -d "${EASYRSA_PKI}" ] || {
error_msg "Easy-RSA has not been initialised."
error_msg "Easy-TLS requires an Easy-RSA CA"
error_msg "Otherwise, use Easy-TLS in No-CA mode"
error_msg "$(cmd_help init-tls||:)"
return 1
# die "verify_pki_init - vars_source_check"
}
# verify expected dirs present:
for i in private reqs; do
[ -d "${EASYRSA_PKI}/${i}" ] || {
error_msg "Easy-RSA has not been initialised."
error_msg "Easy-TLS requires an Easy-RSA CA"
error_msg "Otherwise, use Easy-TLS in No-CA mode"
error_msg "$(cmd_help init-tls||:)"
return 1
# die "verify_pki_init - private reqs"
}
done
} # => verify_pki_init ()
# Verify core CA files present
verify_ca_init () {
help_note="Run without commands for usage and command help."
# First check the PKI has been initialized
verify_pki_init
# Verify expected files are present. Allow files to be regular files
# (or symlinks), but also pipes, for flexibility with ca.key
for i in serial index.txt index.txt.attr ca.crt private/ca.key; do
if [ ! -f "${EASYRSA_PKI}/${i}" ] && [ ! -p "${EASYRSA_PKI}/${i}" ]; then
#[ "$1" = "test" ] && return 1
die "
Easy-TLS requires that you have built your EASY-RSA CA.
Easy-RSA error:
Missing expected CA file: ${i} (perhaps you need to run build-ca?)
${help_note}"
fi
done
# When operating in 'test' mode, return success.
# test callers don't care about CA-specific dir structure
#[ "$1" = "test" ] && return 0
# verify expected CA-specific dirs:
for i in issued certs_by_serial
do
[ -d "${EASYRSA_PKI}/${i}" ] || die "
Easy-TLS requires that you have built your EASY-RSA CA.
Easy-RSA error:
Missing expected CA dir: ${i} (perhaps you need to run build-ca?)
${help_note}"
done
unset -v help_note
} # => verify_ca_init ()
############################################################################
#
# EASYTLS MANAGEMENT Section
#
# Verbose messages
easytls_verbose () {
[ -n "${EASYTLS_VERBOSE}" ] || return 0
[ -z "${EASYTLS_SILENT}" ] || return 0
[ -z "${flash_config}" ] || return 0
[ -z "${EASYTLS_verboff}" ] || return 0
verbose_message="${*}"
[ -n "${verbose_message}" ] || { print ""; return 0; }
"${EASYTLS_PRINTF}" '%s\n' "* ${verbose_message}"
} # => easytls_verbose ()
# Configurable options
easytls_config () {
# Verify config-file and hash has already been done by config_use()
case "${1}" in
addition)
cfg_opt="${2}"
[ -n "${cfg_opt}" ] || { error_msg "Required: option"; return 1; }
config_addition && return 0
error_msg "Failed to add: ${cfg_opt}"
return 1
;;
deletion)
cfg_opt="${2}"
[ -n "${cfg_opt}" ] || { error_msg "Required: option"; return 1; }
config_deletion && return 0
error_msg "Failed to delete: ${cfg_opt}"
return 1
;;
nc|no.ca) cfg_opt="no.ca"; cfg_val="${2}" ;;
ha|hash.algorithm) cfg_opt="hash.algorithm"; cfg_val="${2}" ;;
td|tmp.dir) cfg_opt="tmp.dir"; cfg_val="${2}" ;;
ac|auto.check) cfg_opt="auto.check"; cfg_val="${2}" ;;
cg|custom.group) cfg_opt="custom.group"; cfg_val="${2}" ;;
id|ca.id) cfg_opt="ca.id"; cfg_val="${2}" ;;
co|custom.openvpn) cfg_opt="custom.openvpn"; cfg_val="${2}" ;;
im|inline.metadata) cfg_opt="inline.metadata"; cfg_val="${2}" ;;
ih|inline.hardware) cfg_opt="inline.hardware"; cfg_val="${2}" ;;
status) cfg_opt="status"; cfg_val="${2}" ;;
test.bool) cfg_opt="test.bool"; cfg_val="${2}" ;;
'')
easytls_verbose
"${EASYTLS_PRINTF}" "%s\n" "easytls = ${EASYTLS_VERSION}"
"${EASYTLS_GREP}" -v 'status' "${EASYTLS_CONFIG_FILE}"
easytls_verbose
skip_master_hash=1
return 0
;;
*)
error_msg "Unknown option: ${1}"
return 1
esac
case "${cfg_opt}" in
test.bool)
case "${cfg_val}" in
0|1) : ;; # ok
*)
help_note="Supported options: 0 | 1"
die "Unsupported value for ${cfg_opt}: ${cfg_val}"
esac
;;
no.ca)
help_note="See help for init-tls"
die "To change No-CA mode you must create a new Easy-TLS PKI"
;;
tmp.dir)
# Test for dir
[ -d "${cfg_val}" ] || missing_file "${cfg_val}"
;;
auto.check|inline.metadata|inline.hardware)
case "${cfg_val}" in
on|off) ;; # ok
*)
help_note="Supported options: on | off"
die "Unsupported value for ${cfg_opt}: ${cfg_val}"
esac
;;
custom.group)
# Cannot be empty
[ -n "${cfg_val}" ] || die "custom.group cannot be empty"
# spaces ?
test_val="${cfg_val%% *}"
[ "${cfg_val}" = "${test_val}" ] || {
help_note="custom.group does not support spaces."
die "Unsupported value for ${cfg_opt}: ${cfg_val}"
}
confirm "Change your Custom-Group: " "yes" \
"NOTICE:
* If you change your custom group here then you will need to use
ENABLE_MULTI_CUSTOM_G=1 in your easytls-cryptv2-verify.vars file.
And also insert all the custom groups into LOCAL_CUSTOM_G variable."
;;
custom.openvpn)
if [ -n "${cfg_val}" ]; then
# Test for file
[ -f "${cfg_val}" ] || missing_file "${cfg_val}"
fi
;;
ca.id)
if [ -n "${EASYTLS_MASTER_ID}" ]; then
help_note="To change your CA-ID you must create a new Easy-RSA CA"
die "The CA-ID cannot be changed"
fi
if [ -z "${save_id_authorized}" ]; then
die "Please use 'easytls save-id' to configure your CA-ID"
fi
;;
hash.algorithm)
help_note="See help for init-tls"
die "To change your HASH you must create a new Easy-TLS PKI"
;;
status) [ -n "${flash_config}" ] || return 0 ;;
*) die "Unknown option: ${cfg_opt}"
esac
# Write config
config_update || die "Error updating config."
#config_save_hash || die "Error hashing config."
config_use || die "config_use (updated) - Error: ${?}"
} # => easytls_config ()
# Update config
config_update () {
# remove old_record
old_record="^${cfg_opt} = .*\$"
if universal_update del "${EASYTLS_CONFIG_FILE}" "${old_record}"; then
: # ok
else
return 1
fi
# add new_record
new_record="${cfg_opt} = ${cfg_val}"
if universal_update add "${EASYTLS_CONFIG_FILE}" "${new_record}"; then
: # ok
else
return 1
fi
easytls_verbose
easytls_verbose "config-file updated: ${cfg_opt} = ${cfg_val}"
easytls_verbose
update_master_hash=1
} # => config_update ()
# Use config
config_use () {
# Verify once for config and then again later for other commands
# Don't load config if it does not exist
[ -f "${EASYTLS_CONFIG_FILE}" ] || return 0
# Cannot use IFS because Windows/sh/read needs to recognise CRLF as well
# It is simplest to rely on Windows/sh/read default which does CRLF or NL
# Any writes to config and Windows/sed converts all CRLF to NL anyway
unset -v config_error
while read -r cfg_opt cfg_equ cfg_val; do
# May as well check it for shellcheck
if [ "${cfg_equ}" != "=" ]; then
error_msg "Config error: Require '='"
config_error=2
fi
# These settings do not change command line
case "${cfg_opt}" in
test.bool) : ;;
auto.check)
case "${cfg_val}" in
off) set_var AUTO_CHECK_DISABLED 1 ;;
on) : ;;
*)
error_msg "Config error: Invalid value: ${cfg_val}"
config_error=3
esac
;;
inline.metadata)
case "${cfg_val}" in
off) set_var no_metadata 1 ;;
on) : ;;
*)
error_msg "Config error: Invalid value: ${cfg_val}"
config_error=4
esac
;;
inline.hardware)
case "${cfg_val}" in
off) : ;;
on) set_var add_hardware 1 ;;
*)
error_msg "Config error: Invalid value: ${cfg_val}"
config_error=5
esac
;;
custom.group)
[ -z "${cfg_val}" ] || set_var TLSKEY_CUSTOM_GRP "${cfg_val}"
set_var TLSKEY_CUSTOM_GRP "EASYTLS"
;;
custom.openvpn)
[ -z "${cfg_val}" ] || set_var EASYTLS_OPENVPN "${cfg_val}"
set_var EASYTLS_OPENVPN "openvpn"
;;
tmp.dir)
[ -z "${cfg_val}" ] || set_var EASYTLS_tmp_dir "${cfg_val}"
;;
no.ca)
# This is set explicitly here to protect config
[ -z "${cfg_val}" ] || export EASYTLS_NO_CA="${cfg_val}"
;;
hash.algorithm)
# This is set explicitly here to protect config
[ -z "${cfg_val}" ] || export EASYTLS_HASH_ALGO="${cfg_val}"
;;
ca.id)
# This is set explicitly here to protect config
export EASYTLS_MASTER_ID="${cfg_val}"
;;
status)
EASYTLS_STATUS="${cfg_val}"
unset -v flash_config
;;
*) error_msg "Config error: Ignored option: ${cfg_opt}"
esac
done < "${EASYTLS_CONFIG_FILE}"
[ -z "${config_error}" ] || return "${config_error}"
# Set default hash.algorithm
EASYTLS_HASH_ALGO="${EASYTLS_HASH_ALGO:-SHA256}"
# Set fixed hash
Zero_x8=00000000
Zero_x16="${Zero_x8}${Zero_x8}"
case "${EASYTLS_HASH_ALGO}" in
SHA1)
fixed_hash=1111111111111111111111111111111111111111
fixed_length=40
;;
SHA256)
fixed_hash=2222222222222222222222222222222222222222222222222222222222222222
fixed_length=64
;;
*)
error_msg "config_use - Unknown algorithm: ${EASYTLS_HASH_ALGO}"
return 1
esac
# Group mode is not tied to a CA or Certificates
# Group server TLS-CV2 Key is free to roam anywhere
# Group client TLS-CV2 Key must follow the the server key which built it
group_mode_fixed_ca_id="${Zero_x16}${Zero_x16}${Zero_x16}${Zero_x16}"
# generate forbidden_hash (empty input)
if easytls_ssl_generate_empty_hash; then
forbidden_hash="${empty_hash}"
unset -v empty_hash
else
error_msg "config_use - easytls_ssl_generate_empty_hash"
return 1
fi
# Fixed TLS key serial
fixed_tls_auth_serial=7A01
fixed_tls_cryptv1_serial=7C01
# maintenance
config_version || { error_msg "config_use - config_version"; return 1; }
easytls_verbose "config_use OK"
} # => config_use ()
# verify config file hash
config_verify_hash () {
# DISABLED_DELETE
die "DISABLE: config_verify_hash"
[ -z "${config_verify_hash_block}" ] || \
die "config verify hash must only run once"
request_fixed_hash=1
generate_and_match_valid_hash \
"${EASYTLS_CONFIG_FILE}" "${EASYTLS_CONFIG_HASH}" || {
error_msg "config_verify_hash - generate_and_match_valid_hash"
return 1
}
unset -v request_fixed_hash
easytls_verbose "config_verify_hash OK"
config_verify_hash_block=1
} # => config_save_hash ()
# Hash config-file
config_save_hash () {
# DISABLED_DELETE
die "DISABLE: config_save_hash"
[ -z "${config_save_hash_block}" ] || \
die "config save hash must only run once"
request_fixed_hash=1
generate_and_save_file_hash \
"${EASYTLS_CONFIG_FILE}" "${EASYTLS_CONFIG_HASH}" || {
error_msg "config_save_hash - generate_and_save_file_hash"
return 1
}
unset -v request_fixed_hash
easytls_verbose "config-file hash save OK"
update_master_hash=1
config_save_hash_block=1
} # => config_save_hash ()
# Add a new option to config
config_addition () {
# Only allow alpha caracters and '.'