-
Notifications
You must be signed in to change notification settings - Fork 2
/
lint.sh
executable file
·2170 lines (1893 loc) · 51.1 KB
/
lint.sh
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/bash
#
# [0x19e Networks]
# Copyright (c) 2019 Robert W. Baumgartner
#
# PROJECT : pki-lint x509 certificate linter
# AUTHOR : Robert W. Baumgartner <rwb@0x19e.net>
# LICENSE : MIT License
#
## DESCRIPTION
#
# A simple Bash wrapper for a collection of x509 certificate
# and Public-key Infrastructure (PKI) checks.
#
# The script enables quick and easy identification of potential
# issues with generated x509 certificates.
#
## USAGE
#
# To initialize Git sub-modules and compile all certificate lints,
# run the 'build.sh' script found in the root directory:
# `./build.sh`
#
# Afterwards, you can run `./lint.sh --help` for usage information.
#
## LICENSE
#
# MIT License
#
# Copyright (c) 2019 Robert W. Baumgartner
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Script variables
CERTTOOL_MIN_VER="3.0.0"
RUBY_MIN_VERSION="2.2"
OPENSSL_MIN_VERSION_NUM="1.1.0"
OPENSSL_MIN_VERSION_EXT="g"
# define script error messages
errorMessages=([0]="Certificate passed all checks."
[1]="Certificate linting produced one or more warnings; manual validation required."
[2]="Certificate linting found one or more critical issues.")
# define supported security levels
# level 0: 112 bits (RSA >= 2048 bits ; ECC >= 224 bits)
# level 1: 128 bits (RSA >= 3072 bits ; ECC >= 256 bits)
# level 2: 192 bits (RSA >= 7680 bits ; ECC >= 384 bits)
# level 3: 256 bits (RSA >= 15360 bits ; ECC >= 512 bits)
securityLevels=([0]="minimum" [1]="medium" [2]="high" [3]="extreme")
# define table of EKU purpose arguments for various tools
certPurposes=( [0]="client" [1]="server" [2]="mailsign" [3]="mailencrypt" [4]="ocsp" [5]="anyCA" )
opensslk_opts=( [0]="sslclient" [1]="sslserver" [2]="smimesign" [3]="smimeencrypt" [4]="" [5]="" )
vfychain_opts=( [0]="0" [1]="1" [2]="4" [3]="5" [4]="10" [5]="11" )
certutil_opts=( [0]="C" [1]="V" [2]="S" [3]="R" [4]="O" [5]="A" )
golangku_opts=( [0]="2" [1]="1" [2]="4" [3]="4" [4]="9" [5]="" )
gnutlsku_opts=( [0]="1.3.6.1.5.5.7.3.2"
[1]="1.3.6.1.5.5.7.3.1"
[2]="1.3.6.1.5.5.7.3.4"
[3]="1.3.6.1.5.5.7.3.4"
[4]="1.3.6.1.5.5.7.3.9"
[5]="")
# Check for commands which are required to continue executing.
# If one of these is missing the script must exit immediately.
hash grep 2>/dev/null || { echo >&2 "You need to install grep. Aborting."; exit 1; }
GOLANG_INSTALLED=0
if hash go 2>/dev/null; then
GOLANG_INSTALLED=1
fi
# Create an array to track missing packages
declare -a pkg_missing=();
function add_missing_pkg()
{
if [ -z "$1" ]; then
usage "Package name cannot be null."
fi
if ! echo "${pkg_missing[@]}" | grep -q -w "$1"; then
pkg_name="$1"
pkg_missing=("${pkg_missing[@]}" "${pkg_name}")
fi
}
# Check for required comamnds in PATH
hash openssl 2>/dev/null || { add_missing_pkg "openssl"; }
hash git 2>/dev/null || { add_missing_pkg "git"; }
hash certtool 2>/dev/null || { add_missing_pkg "gnutls-bin"; }
hash jq 2>/dev/null || { add_missing_pkg "jq"; }
hash vfychain 2>/dev/null || { add_missing_pkg "libnss3-tools"; }
# Ruby can be required here; otherwise it will only affect AWS linting
#hash ruby 2>/dev/null || { add_missing_pkg "ruby"; }
if [ ${#pkg_missing[@]} -gt 0 ]; then
echo >&2 "ERROR: You need to install the following packages: ${pkg_missing[*]}"
exit 1
fi
# Define additional variables
CERT=""
CA_CERT="false"
X509_MODE=""
CA_CHAIN=""
EV_POLICY=""
EV_HOST=""
PRINT_MODE=""
OPT_PURPOSE=""
OPT_LEVEL=""
OPT_ERROR_LEVEL=0
OPENSSL_SECLVL=2
RUBY_VERSION=""
EV_DETECTED="false"
NO_EV_CHECK="false"
ERROR_LEVEL=0
DEBUG_LEVEL=0
SECURITY_LEVEL=0
NSS_VERIFY_CHAIN="false"
CRL_CHECK_SKIP="false"
OPENSSL_ARGS="-verbose -x509_strict -policy_print -policy_check"
# check for required env. variables and set if missing
if [ -z "${VERBOSITY}" ]; then
VERBOSITY=0
fi
if [ -z "${SILENT}" ]; then
SILENT="false"
fi
if [ -z "${NO_COLOR}" ]; then
NO_COLOR="true"
fi
if [ -z "${BOLD_TAGGED}" ]; then
BOLD_TAGGED="true"
fi
# usage: version_gt( current_version, required_version )
function version_gt() { test "$(printf '%s\n' "$@" | sort -bt. -k1,1 -k2,2n -k3,3n -k4,4n -k5,5n | head -n 1)" != "$1"; }
# get the root directory this script is running from
# if the script is called from a symlink, the link is
# resolved to the absolute path.
function get_root_dir()
{
source="${BASH_SOURCE[0]}"
# resolve $source until the file is no longer a symlink
while [ -h "${source}" ]; do
dir=$( cd -P "$( dirname "${source}" )" && pwd )
source=$(readlink "${source}")
# if $source was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ ${source} != /* ]] && source="${dir}/${source}"
done
dir="$( cd -P "$( dirname "${source}" )" && pwd )"
echo "${dir}"
return
}
function check_ruby_version()
{
if hash ruby 2>/dev/null; then
RUBY_VERSION=$(ruby --version | head -n1 | grep -Po '(?<=\s)([1-9][0-9]{0,8}|0)(\.([1-9][0-9]{0,8}|0)){1,3}')
if version_gt "$RUBY_VERSION" "$RUBY_MIN_VERSION"; then
return 0
fi
fi
return 1
}
function is_number()
{
if [ -z "${1}" ]; then
return 1
fi
re='^[0-9]+$'
if [[ ${1} =~ $re ]]; then
return 0
fi
return 1
}
function print_newline()
{
if [ "${SILENT}" != "true" ]; then
echo
fi
}
# Print text with optional color
# red=31
# green=32
# yellow=33
# blue=34
# magenta=35
# cyan=36
function print_ex()
{
if [ "${SILENT}" == "true" ]; then
return
fi
if [ -z "$1" ]; then
echo
return
fi
st=0
fg=39
bg=49
str="${1}"
if [ "${NO_COLOR}" == "false" ]; then
str="${str//\\/\\\\}"
if [ -n "${2}" ]; then
if ! echo "${2}" | grep -qPo '^[0-9\;]+$'; then
exit_script 1 "Invalid argument passed to function: '${2}' is not a valid number."
fi
st="${2}"
fi
if [ -n "${3}" ]; then
if ! is_number "${3}"; then
exit_script 1 "Invalid argument passed to function: '${3}' is not a valid number."
fi
fg="${3}"
fi
if [ -n "${4}" ]; then
if ! is_number "${4}"; then
exit_script 1 "Invalid argument passed to function: '${4}' is not a valid number."
fi
bg="${4}"
fi
echo -e "\e[0m\e[${st};${bg};${fg}m${str}\e[0m"
else
echo "${1}"
fi
}
function print_ex_tagged()
{
if [ "${SILENT}" == "true" ]; then
return
fi
if [ -z "$1" ]; then
echo
return
fi
st=0
fg=39
bg=49
hdr="${1}"
str="${2}"
if [ -z "${hdr}" ]; then
hdr="INFO"
fi
if [ "${NO_COLOR}" == "false" ]; then
str="${str//\\/\\\\}"
if [ -n "${3}" ]; then
if ! echo "${3}" | grep -qPo '^[0-9\;]+$'; then
exit_script 1 "Invalid argument passed to function: '${3}' is not a valid number."
fi
st="${3}"
fi
if [ -n "${4}" ]; then
if ! is_number "${4}"; then
exit_script 1 "Invalid argument passed to function: '${4}' is not a valid number."
fi
fg="${4}"
fi
if [ -n "${5}" ]; then
if ! is_number "${5}"; then
exit_script 1 "Invalid argument passed to function: '${5}' is not a valid number."
fi
bg="${5}"
fi
echo -e "\e[0m\e[1;${bg};${fg}m${1}:\e[0m\e[${st};${bg};${fg}m ${str}\e[0m"
else
echo "${1}: ${2}"
fi
}
function print_normal()
{
fg=39
bg=49
str="${1}"
if [ -n "${2}" ]; then
fg="${2}"
fi
if [ -n "${3}" ]; then
bg="${3}"
fi
print_ex "${str}" 0 "${fg}" "${bg}"
}
function print_bold_ul()
{
fg=39
bg=49
str="${1}"
if [ -n "${2}" ]; then
fg="${2}"
fi
if [ -n "${3}" ]; then
bg="${3}"
fi
print_ex "${str}" "1;4" "${fg}" "${bg}"
}
function print_bold()
{
fg=39
bg=49
str="${1}"
if [ -n "${2}" ]; then
fg="${2}"
fi
if [ -n "${3}" ]; then
bg="${3}"
fi
print_ex "${str}" 1 "${fg}" "${bg}"
}
function print_ul()
{
fg=39
bg=49
str="${1}"
if [ -n "${2}" ]; then
fg="${2}"
fi
if [ -n "${3}" ]; then
bg="${3}"
fi
print_ex "${str}" 4 "${fg}" "${bg}"
}
function print_tagged()
{
fg=39
bg=49
tag="${1}"
str="${2}"
if [ -n "${3}" ]; then
fg="${3}"
fi
if [ -n "${4}" ]; then
bg="${4}"
fi
if [ "${BOLD_TAGGED}" == "true" ]; then
print_ex_tagged "${tag}" "${str}" 0 "${fg}" "${bg}"
else
print_ex "${tag}: ${str}" 0 "${fg}" "${bg}"
fi
}
function print_info()
{
print_tagged "INFO" "${1}"
}
function print_error()
{
print_tagged "ERROR" "${1}" 31
}
function print_pass()
{
print_tagged "PASS" "${1}" 32
}
function print_notice()
{
print_tagged "NOTICE" "${1}" #35
#print_tagged "NOTICE" "${1}" 32 # Green
#print_tagged "NOTICE" "${1}" 33 # Yellow
#print_tagged "NOTICE" "${1}" 34 # Blue
#print_tagged "NOTICE" "${1}" 35 # Magenta
#print_tagged "NOTICE" "${1}" 36 # Cyan
#print_tagged "NOTICE" "${1}" 90 # Grey
}
function print_warn()
{
print_tagged "WARNING" "${1}" 33
}
function print_debug()
{
if [ $VERBOSITY -gt 1 ]; then
print_tagged "DEBUG" "${1}" 36 >&2
fi
}
function print_data()
{
print_cyan "${1}"
}
function print_header()
{
print_bold_ul "${1}"
#print_ul "${1}"
#print_bold "${1}" 34
}
function print_green()
{
print_ex "${1}" 0 32
}
function print_red()
{
print_ex "${1}" 0 31
}
function print_blue()
{
print_ex "${1}" 0 34
}
function print_magenta()
{
print_ex "${1}" 0 35
}
function print_cyan()
{
print_ex "${1}" 0 36
}
function print_yellow()
{
print_ex "${1}" 0 33
}
function exit_script()
{
# Default exit code is 1
local exit_code=1
re='^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$'
if echo "$1" | grep -q -E "$re"; then
exit_code=$1
shift
fi
re='[[:alnum:]]'
if echo "$*" | grep -iq -E "$re"; then
if [ "$exit_code" -eq 0 ]; then
print_normal "INFO: $*" >&2
else
print_red "ERROR: $*" 1>&2
fi
fi
# Print 'aborting' string if exit code is not 0
[ "$exit_code" -ne 0 ] && echo >&2 "Aborting script..."
exit "$exit_code"
}
function usage()
{
# Prints out usage and exit.
sed -e "s/^ //" -e "s|SCRIPT_NAME|$(basename "$0")|" << "EOF"
USAGE
Performs various linting tests against the specified X.509 certificate.
SYNTAX
SCRIPT_NAME [OPTIONS] ARGUMENTS
ARGUMENTS
certificate The certificate (in PEM format) to lint.
OPTIONS
-r, --root Certificate is a root CA.
-i, --intermediate Certificate is an Intermediate CA.
-s, --subscriber Certificate is for an end-entity.
-c, --chain <file> Specifies a CA chain file to use.
-o, --policy <oid> Specifies an OID of a policy to test.
-n, --hostname <name> Specifies the hostname for validation.
-u, --usage <purpose> Specifies the certificate purpose to test for.
Supported options are:
- 0=client
- 1=server
- 2=mailsign
- 3=mailencrypt
- 4=ocsp
- 5=anyCA
-l, --level <level> Specify the required certificate security level.
Supported options are:
- 0=minimum (>= 112 bits) (default)
- 1=medium (>= 128 bits)
- 2=high (>= 192 bits)
- 3=extreme (>= 256 bits)
-e, --error-level <int> Specify the maximum allowed error level.
--no-ev-check Do not validate Extended Validation (EV) certificates.
-p, --print Print the input certificate.
-b, --colors Print colorful output.
-q, --quiet Do not print results to console.
-v, --verbose Make the script more verbose.
-h, --help Prints this usage.
EOF
exit_script "$@"
}
function test_arg()
{
# Used to validate user input
local arg="$1"
local argv="$2"
if [ -z "$argv" ]; then
if echo "$arg" | grep -q -E '^-'; then
usage "Null argument supplied for option $arg"
fi
fi
if echo "$argv" | grep -q -E '^-'; then
usage "Argument for option $arg cannot start with '-'"
fi
}
function test_number_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
if ! is_number "$argv"; then
usage "Value is not a valid number: '$argv'."
fi
}
function test_file_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
if ! [ -e "$argv" ]; then
usage "File does not exist: '$argv'."
fi
if [ ! -s "$argv" ]; then
usage "File is empty: '$argv'."
fi
}
function test_oid_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
if ! echo "$argv" | grep -qPo '^([1-9][0-9]{0,8}|0)(\.([1-9][0-9]{0,8}|0)){5,16}$'; then
usage "Argument is not a valid object identifier: '$argv'"
fi
}
function test_host_arg()
{
local arg="$1"
local argv="$2"
test_arg "$arg" "$argv"
if [ -z "$argv" ]; then
argv="$arg"
fi
host_regex='^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'
if ! echo "$argv" | grep -qPo "${host_regex}"; then
usage "Invalid hostname: '${argv}'"
fi
}
function test_chain()
{
if [ -n "${CA_CHAIN}" ]; then
usage "Cannot specify multiple chain files."
fi
}
function test_ev_host()
{
if [ -n "${EV_HOST}" ]; then
usage "Cannot specify multiple hostnames."
fi
}
function test_cert()
{
if [ -n "${CERT}" ]; then
usage "Cannot specify multiple certificates."
fi
}
function test_mode()
{
if [ -n "${X509_MODE}" ]; then
usage "Cannot specify conflicting options."
fi
}
function test_ev_policy()
{
if [ -n "${EV_POLICY}" ]; then
usage "Cannot specify multiple EV policies."
fi
}
declare -a zlint_names=();
function add_zlint_lint()
{
if [ -z "$1" ]; then
exit_script 1 "zlint lint name cannot be null."
fi
if echo "${zlint_names[@]}" | grep -q -w "$1"; then
exit_script 1 "zlint lint name processed twice."
fi
lint_name="$1"
zlint_names=("${zlint_names[@]}" "${lint_name}")
}
function is_pem_format()
{
local file="$1"
if [ -z "${file}" ] || [ ! -e "${file}" ]; then
exit_script 1 "No file passed to function."
fi
if ! openssl x509 -inform PEM -in "${file}" -text -noout > /dev/null 2>&1; then
return 1
fi
return 0
}
function convert2pem()
{
local file="$1"
if [ -z "${file}" ] || [ ! -e "${file}" ]; then
exit_script 1 "Invalid file argument passed to function."
fi
# check if file is already in PEM format
if is_pem_format "${file}"; then
echo "${file}"
return 0
fi
if ! openssl crl -inform DER -in "${file}" -outform PEM -out "${file}"; then
exit_script 1 "Failed to convert file from DER->PEM encoding: '${file}'"
fi
echo "${file}"
return 0
}
function get_aia_issuer_http_from_pem()
{
local pem_file="$1"
local aia_url
if [ -z "${pem_file}" ] || [ ! -e "${pem_file}" ]; then
exit_script 1 "Invalid file path passed to function."
fi
if aia_url=$(openssl x509 -noout -text -in "${pem_file}" | grep -A 1 'Authority Information Access' | grep -Po '(?<=CA\sIssuers\s\-\sURI\:)(http)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?\.(cer|crt)$'); then
if [ -n "${aia_url}" ]; then
echo "${aia_url}"
return 0
fi
fi
return 1
}
function get_crl_http_from_pem()
{
local pem_file="$1"
local crl_url
if [ -z "${pem_file}" ] || [ ! -e "${pem_file}" ]; then
exit_script 1 "Invalid file path passed to function."
fi
if crl_url=$(openssl x509 -noout -text -in "${pem_file}" | grep -A 3 'X509v3 CRL Distribution Points' | grep -Po '(?<=URI\:)(http)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$'); then
if [ -n "${crl_url}" ]; then
echo "${crl_url}"
return 0
fi
fi
return 1
}
function get_pem_common_name()
{
local pem_file="$1"
local crt_common_name
if [ -z "${pem_file}" ] || [ ! -e "${pem_file}" ]; then
exit_script 1 "Invalid file path passed to function."
fi
if crt_common_name=$(openssl x509 -noout -subject -nameopt multiline -in "${pem_file}" | grep commonName | sed -n 's/ *commonName *= //p'); then
if [ -n "${crt_common_name}" ]; then
echo "${crt_common_name}"
return 0
fi
fi
return 1
}
function get_pem_subject_name()
{
local pem_file="$1"
local subject_name
if [ -z "${pem_file}" ] || [ ! -e "${pem_file}" ]; then
exit_script 1 "Invalid file path passed to function."
fi
if subject_name=$(openssl x509 -noout -subject -nameopt multiline -in "${pem_file}" | tail -n1 | awk -F= '{ print $2 }' | sed -e 's/^\s//g'); then
if [ -n "${subject_name}" ]; then
echo "${subject_name}"
return 0
fi
fi
return 1
}
function get_purpose()
{
if [ -z "$1" ]; then
usage "Purpose cannot be null."
fi
temp=$1
re='^[0-9]+$'
if [[ $temp =~ $re ]] ; then
temp="${certPurposes[temp]}"
if [ -z "${temp}" ]; then
return 1
#usage "'$1' is not mapped to a known purpose."
fi
fi
if ! echo "${certPurposes[@]}" | grep -q -w "$temp"; then
return 1
#usage "'$temp' is not a valid purpose."
fi
echo "${temp}"
return 0
}
function get_level()
{
if [ -z "$1" ]; then
exit_script 1 "Security level cannot be null."
fi
temp=$1
re='^[0-9]+$'
if [[ $temp =~ $re ]] ; then
temp="${securityLevels[temp]}"
if [ -z "${temp}" ]; then
usage "'$1' is not mapped to a known security level."
fi
fi
if ! echo "${securityLevels[@]}" | grep -q -w "$temp"; then
usage "'$temp' is not a valid security level."
fi
echo "${temp}"
}
function get_seclvl()
{
if [ -z "$1" ]; then
exit_script 1 "Security level cannot be null."
fi
value="$1"
temp=0
for i in "${!securityLevels[@]}"; do
if [[ "${securityLevels[$i]}" = "${value}" ]]; then
temp=$i
break;
fi
done
echo "${temp}"
}
function get_openssl_seclvl()
{
if [ -z "$1" ]; then
exit_script 1 "Security level cannot be null."
fi
value="$1"
temp=0
for i in "${!securityLevels[@]}"; do
if [[ "${securityLevels[$i]}" = "${value}" ]]; then
temp=$i
break;
fi
done
echo "$((temp+2))"
}
function get_errlvl()
{
input="${1}"
if [ -z "${1}" ]; then
return 0
fi
ret=0
case "${input}" in
warn|WARN|W|Warn|Warning|1)
ret=1
;;
error|fatal|ERROR|FATAL|E|F|B|Error|Fatal|2)
ret=2
;;
esac
return ${ret}
}
function get_print_func()
{
input="${1}"
if [ -z "${1}" ]; then
echo "print_normal"
fi
case "${input}" in
pass|0)
echo "print_pass"
;;
info|I|Info)
echo "print_info"
;;
warn|W|Warning|1)
echo "print_warn"
;;
error|fatal|E|F|B|Error|2)
echo "print_error"
;;
notice|N)
echo "print_notice"
;;
*)
echo "print_normal"
;;
esac
return 0
}
function get_print_func_raw()
{
input="${1}"
if [ -z "${1}" ]; then
echo "print_normal"
fi
case "${input}" in
pass|0)
echo "print_green"
;;
info|I|Info)
echo "print_normal"
;;
warn|W|Warning|1)
echo "print_yellow"
;;
error|fatal|E|F|B|Error|2)
echo "print_red"
;;
notice|N)
echo "print_green"
;;
*)
echo "print_normal"
;;
esac
return 0
}
function check_min_bits()
{
local algo="$1"
local bits="$2"
local algoName=""
local minBits=0
test_number_arg "minimumBits" "$bits"
case "${algo}" in
rsaEncryption|RSA)
algoName="RSA"
minBits=${RSA_MIN_BITS}
;;
id-ecPublicKey|ECC)
algoName="ECC"
minBits=${ECC_MIN_BITS}
;;
*)
usage "Invalid algorithm identifier passed to function."
;;
esac
if [[ $bits -lt $minBits ]]; then
print_error "An ${algoName} key of at least ${minBits} bits is required (certificate: ${bits} bits)."
return 1
fi
print_pass "${algoName} certificate key length of ${bits} bits (${minBits} bits required)."
return 0
}
# process arguments
[ $# -gt 0 ] || usage
while [ $# -gt 0 ]; do
case "$1" in
-r|--root)
test_mode
CA_CERT="true"