-
Notifications
You must be signed in to change notification settings - Fork 8
/
tls-cert-manage.sh
executable file
·2497 lines (2256 loc) · 87.6 KB
/
tls-cert-manage.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
#
# TODO: Add:
# CNF_REQ_EXT_PROMPT = yes to email
#
# NAME
# tls-cert-manage.sh - Manage Root and Intermediate Certificate Authorities
#
# SYNOPSIS
# tls-cert-manage.sh create <CERT-NAME> <CERT-TYPE> <CA-NAME>
# tls-cert-manage.sh verify <CERT-NAME> <CERT-TYPE>
# tls-cert-manage.sh renew <CERT-NAME> <CA-NAME>
# tls-cert-manage.sh revoke <CERT-NAME> <CA-NAME> [REASON]
#
# DESCRIPTION
# A front-end tool to OpenSSL that enables creation, renewal,
# revocation, and verification of PKI certificates.
#
# CERT-NAME
# Specifies the certificate name. Often Common Name such as
# DNS, IP or email are used as this certificate name.
# Must be a valid filename specification (file-system dependent).
#
# CERT-TYPE
# Mandatory option which specifies a type of certificate:
#
# server - TLS server: Web server, MTA, VPN, IMAP, POP3, 802.1ar
# client - TLS client:
# ocsp - OCSP
# email - Encryption part of SMTP body
# identity - Signing CA for Microsoft SmartCard identity
# encryption - Microsoft Encrypted File System (msEFS)
# codesign - Signed executable code
# timestamping - ANSI X9.95 TimeStamping (RFC3161)
# proxy - Used for proxying
#
# This argument is used only with 'create' AND 'verify' command line options
#
# CA-NAME
# Specifies the simple name of CA in which to sign this
# certificate against with. It may be the Root CA name
# or the Intermediate CA name. The CA name is
# the same CA-NAME used when creating the parent CA.
#
# This argument is not used with 'verify' command line option
#
# REASON
# Specifies the reason for this revocation of the certificate
# The value must be one of the following:
# - unspecified
# - keyCompromise
# - CACompromise
# - affiliationChanged
# - superseded
# - cessationOfOperation
# - certificateHold
# - removeFromCRL (from RFC5280)
#
# This argument is used only with 'revoke' command line option
#
# -a, --algorithm
# Selects the encryption algorithm.
# Valid algorithms are: rsa, ecdsa, poly1305 OR ed25519
# These value are case-sensitive.
# If no algorithm specified, then RSA is used by default.
#
# -b, --base-dir
# The top-level directory of SSL, typically /etc/ssl
# Useful for testing this command in non-root shell
# or maintaining SSL certs elsewhere (other than /etc/ssl).
#
# -c, --cipher
# Specify the cipher method of the PEM key file in
# which to password-protect the key with.
# Default is a plaintext key file with no password protection.
#
# -f, --force-delete
# Forces deletion of its specified certificate files
# as pointed to by CA-NAME argument. Only files
# pointed to by CA-NAMe will get deleted: key, certificate,
# and CRL. Run a risk of unsynchronized CA database there.
#
# -g, --group
# Use this Unix group name for all files created or updated.
# Default is ssl-cert group.
#
# -h, --help
#
# -k, --key-size
# Specifies the number of bits in the key. The choice of key
# size depends on the algorithm (-a) used.
# The key size does not need to be specified if using a default
# algorithm. The default key size is 4096 bits.
#
# Key size for ed25519 algorithm gets ignored here.
# Valid poly1305 key sizes are:
# Valid rsa key sizes are: 4096, 2048, 1024 or 512.
# Valid ecdsa key sizes are: 521, 384, 256, 224 or 192.
#
# -m, --message-digest
# blake2b512 blake2s256 gost md4
# md5 rmd160 sha1 sha224
# sha256 sha3-224 sha3-256 sha3-384
# sha3-512 sha384 sha512 sha512-224
# sha512-256 shake128 shake256 sm3
#
# -n, --nested-ca
# First chaining of first-level CAs are placed in subdirectory inside
# its Root CA directory, and subsequent chaining of second-level CA
# get nesting also in subdirectory inside its respective Intermediate
# CA directory. Very few organizations use this.
#
# -T, --traditional
# Indicates the standard OpenSSL directory layout.
# Default is to use the new centralized directory layout.
#
# -v, --verbose
# Sets the debugging level.
#
# Files Created:
# $SSL_CA_CERTS_DIR/PARENT_ID
#
# NOTES:
#
# Enforces 'ssl-cert' group; and requires all admins to have 'ssl-cert'
# group when using this command
# DO NOT be giving 'ssl-cert' group to server daemons' supplemental
# group ID (or worse, as its group ID);
# for that, you copy the certs over to app-specific directory and use
# THAT app's file permissions.
# This command does not deal with distribution of certificates, just
# creation/renewal/revokation of therein.
# 'ssl-cert' group means 'working with SSL/TLS certificates,
# not just reading certs'.
#
# Inspired by: https://jamielinux.com/docs/openssl-certificate-authority/create-the-root-pair.html
#
X509_PREFIX=""
X509_SUFFIX=""
CNF_CA_EXT_EXTRA=""
CNF_CA_EXT_EXTRA_A=()
execute() {
if [[ "$VERBOSITY" -ge 1 || -n "$DRY_RUN" ]]; then
echo "COMMAND: $@"
fi
if [ -z "$DRY_RUN" ]; then
$@
return $?
fi
}
function cmd_show_syntax_usage {
cat << SYNTAX_EOF | more
Usage:
$0 create [options] <cert-name> <cert-type> <ca-name>
$0 renew [options] <cert-name> <ca-name>
$0 revoke [options] <cert-name> <ca-name> <reason>
$0 verify [options] <cert-name> <cert-type>
$0 help
cert-name: A simple filename for this certificate
cert-type: This certificate type
ca-name: Simple name of CA in which to sign certificate against with.
Alphanumeric, dash, and underscore characters supported.
options:
[ --help|-h ] [ --verbosity|-v ] [ --force-delete|-f ]
[ --base-dir|-b <ssl-directory-path> ]
[ --algorithm|-a <rsa|ed25519|ecdsa|poly1305> ]
[ --message-digest|-m <sha512|sha384|sha256|sha224|sha3-256|
sha3-224|sha3-512|sha1|md5> ]
[ --keysize|-k <4096|2048|1024|521|512|384|256|224> ]
[ --cipher < > ]
[ --group|-g <group-name> ] # (default: $DEFAULT_GROUP_NAME)
[ --traditional|-T ]
[ --dry-run|-d ]
<ca-type>: server, client, email, ocsp, timestamping, security, codesign
Default settings:
Top-level SSL directory: $DEFAULT_SSL_DIR
Cipher: $DEFAULT_PEER_SIGNATURE
Digest: $DEFAULT_MESSAGE_DIGEST
Keysize: $DEFAULT_KEYSIZE_BITS
SYNTAX_EOF
exit 1
}
# Create a top-level or intermediate certificate authority (CA)
#
# Complete with all directories and file protections
#
# LFS/FSSTD: Single directory for all CA (or a directory for each CA depth?)
# Default values (tweakable)
# DEFAULT_CIPHER="des-ede3-cbc"
DEFAULT_CIPHER=
DEFAULT_CMD_MODE="verify"
DEFAULT_GROUP_NAME="ssl-cert"
DEFAULT_KEYSIZE_BITS=4096
DEFAULT_MESSAGE_DIGEST="sha256"
# shellcheck disable=SC2230
DEFAULT_OPENSSL=$(which openssl)
DEFAULT_PEER_SIGNATURE="rsa"
DEFAULT_SERIAL_ID_HEX=1000
DEFAULT_SSL_DIR="/etc/ssl"
DEFAULT_VERBOSITY=0
DEFAULT_USER_NAME=${USER} # tells most IDE syntax checker to say that $USER is defined
# No DEFAULT_ROOT_CA_NAME
# DEFAULT_INT_CA_NAME="network"
# DEFAULT_SIGNING_CA_NAME="component"
DEFAULT_FILETYPE_KEY="key" # sometimes .private
DEFAULT_FILETYPE_CSR="csr" # sometimes .req, .request (PKCS#10)
DEFAULT_FILETYPE_CERT="crt" # sometimes .pem, .cert, .cer (PKCS#7)
DEFAULT_FILETYPE_CRL="crl" # sometimes .revoke
DEFAULT_OFSTD_LAYOUT="centralized"
DEFAULT_OFSTD_DIR_TREE_TYPE="flat"
DEF_DOMAIN="example.invalid"
# if the X509 is blank, use 'none'
DEFAULT_CA_X509_COUNTRY="US"
DEFAULT_CA_X509_STATE="none"
DEFAULT_CA_X509_LOCALITY="none"
DEFAULT_CA_X509_COMMON="ACME Internal Root CA A1"
DEFAULT_CA_X509_ORG="ACME Networks"
DEFAULT_CA_X509_OU="Trust Division"
DEFAULT_CA_X509_EMAIL="ca.example@${DEF_DOMAIN}"
# Do not use HTTPS in X509_CRL (Catch-22)
DEFAULT_CA_X509_CRL="http://${DEF_DOMAIN}/ca/example-crl.$DEFAULT_FILETYPE_CERT"
# DEFAULT_CA_X509_URL_BASE="http://${DEF_DOMAIN}/ca"
# DEFAULT_CA_X509_URL_OCSP="http://ocsp.${DEF_DOMAIN}:9080"
# if the X509 is blank, use 'none'
# DEFAULT_INTCA_X509_COUNTRY="US"
# DEFAULT_INTCA_X509_STATE="none"
# DEFAULT_INTCA_X509_LOCALITY="none"
# DEFAULT_INTCA_X509_COMMON="ACME Internal Intermediate CA B2"
# DEFAULT_INTCA_X509_ORG="ACME Networks"
# DEFAULT_INTCA_X509_OU="Semi-Trust Department"
# DEFAULT_INTCA_X509_EMAIL="ca.subroot@${DEF_DOMAIN}"
# DEFAULT_INTCA_X509_CRL="http://${DEF_DOMAIN}/subroot-ca.crl"
# DEFAULT_INTCA_X509_URL_BASE="http://${DEF_DOMAIN}/ca/subroot"
# DEFAULT_INTCA_X509_URL_OCSP="http://ocsp.${DEF_DOMAIN}:9080"
function input_data {
ID_PROMPT="$1"
ID_DEFAULT_VALUE="$2"
echo -n "$ID_PROMPT (default: '$ID_DEFAULT_VALUE'): "
read -r ID_INPUT_DATA
if [[ -z "$ID_INPUT_DATA" ]]; then
if [ -z "$ID_DEFAULT_VALUE" ]; then
ID_INPUT_DATA='none'
else
ID_INPUT_DATA="$ID_DEFAULT_VALUE"
fi
fi
unset ID_PROMPT
unset ID_DEFAULT_VALUE
}
function directory_file_layout {
# directory_file_layout arguments:
# $1 - Single CA directory Layout (traditional or centralized)
# $2 - Multiple CA layout (flat or nested)
# $3 - Starting OpenSSL directory
# $4 - current issuing authority simplified filename
# [ $5 - parent issuing authority simplified filename ]
DFL_OFSTD_LAYOUT="$1" # traditional | centralized
# DFL_OFSTD_DIR_TREE_TYPE="$2" # hierarchy | flat
DFL_SSL_DIR="$3"
DFL_CERT_NAME="$4"
DFL_PARENT_IA_NAME="$5"
PRIVATE_DNAME="private"
CERTS_DNAME="certs"
CRL_DNAME="crl"
DB_DNAME="db"
NEWCERTS_DNAME="newcerts"
# SSL_CERTS_DIR="${DFL_SSL_DIR}/${CERTS_DNAME}" # /etc/ssl/certs, always
if [[ "$DFL_OFSTD_LAYOUT" == "traditional" ]]; then
# Template filename
DIRNAME_PREFIX="ca-"
DIRNAME_SUFFIX=""
FILENAME_PREFIX="ca."
FILENAME_SUFFIX=""
CERT_FILENAME_PREFIX=""
CERT_FILENAME_SUFFIX=""
X509_PREFIX="ca_"
X509_SUFFIX=""
CERT_X509_PREFIX=""
CERT_X509_SUFFIX="_ca"
PEM_FILETYPE_SUFFIX=".pem"
SSL_SUBDIR_DNAME=""
SSL_CA_DIR="${DFL_SSL_DIR}"
elif [[ "$DFL_OFSTD_LAYOUT" == "centralized" ]]; then
# Template filename
DIRNAME_PREFIX=""
DIRNAME_SUFFIX="-ca"
FILENAME_PREFIX=""
FILENAME_SUFFIX="-ca"
CERT_FILENAME_PREFIX=""
CERT_FILENAME_SUFFIX=""
# shellcheck disable=SC2034
CERT_X509_PREFIX=""
# shellcheck disable=SC2034
CERT_X509_SUFFIX="_ca"
# shellcheck disable=SC2034
X509_PREFIX=""
# shellcheck disable=SC2034
X509_SUFFIX="_ca"
PEM_FILETYPE_SUFFIX=""
SSL_SUBDIR_DNAME="ca"
SSL_CA_DIR="${DFL_SSL_DIR}/${SSL_SUBDIR_DNAME}"
else
echo "Invalid parameter 1 (must be 'traditional' or 'centralized')"
exit 2
fi
if [[ "$VERBOSITY" -gt 1 ]]; then
echo "DFL_PARENT_IA_NAME: $DFL_PARENT_IA_NAME"
echo "DFL_CERT_NAME : $DFL_CERT_NAME"
fi
if [[ "$OFSTD_DIR_TREE_TYPE" == "flat" ]]; then
# Inspired by readthedocs
echo ""
CERT_FNAME="${CERT_FILENAME_PREFIX}${CERT_NAME}${CERT_FILENAME_SUFFIX}"
# CERT_SNAME="${CERT_X509_PREFIX}${CERT_NAME}${CERT_X509_SUFFIX}"
CERT_PATH_FNAME="${DIRNAME_PREFIX}${CERT_NAME}${DIRNAME_SUFFIX}"
PARENT_IA_FNAME="${FILENAME_PREFIX}${PARENT_IA_NAME}${FILENAME_SUFFIX}"
# PARENT_IA_SNAME="${X509_PREFIX}${PARENT_IA_NAME}${X509_SUFFIX}"
PARENT_IA_PATH_FNAME="${DIRNAME_PREFIX}${PARENT_IA_NAME}${DIRNAME_SUFFIX}"
CRT_CERTS_DIR="$SSL_DIR/$CERTS_DNAME"
PARENT_IA_DIR="$SSL_CA_DIR/$PARENT_IA_PATH_FNAME"
elif [[ "$OFSTD_DIR_TREE_TYPE" == "hierarchy" ]]; then
# Inspired by traditional OpenSSL
CERT_FNAME="${CERT_FILENAME_PREFIX}${CERT_NAME}${CERT_FILENAME_SUFFIX}"
# CERT_SNAME="${X509_PREFIX}${CERT_NAME}${X509_SUFFIX}"
CERT_PATH_FNAME="${DIRNAME_PREFIX}${CERT_NAME}${DIRNAME_SUFFIX}"
PARENT_IA_FNAME="${FILENAME_PREFIX}${PARENT_IA_NAME}${FILENAME_SUFFIX}"
# PARENT_IA_SNAME="${X509_PREFIX}${PARENT_IA_NAME}${X509_SUFFIX}"
PARENT_IA_PATH_FNAME="${DIRNAME_PREFIX}${PARENT_IA_NAME}${DIRNAME_SUFFIX}"
CRT_CERTS_DIR="$SSL_CA_DIR/$CERT_PATH_FNAME/$CERT_PATH_FNAME"
PARENT_IA_DIR="$SSL_CA_DIR/$PARENT_IA_PATH_FNAME"
else
echo "Invalid parameter 2 (must be 'flat' or 'nested')"
exit 1
fi
if [[ "$VERBOSITY" -gt 1 ]]; then
echo "DFL_OFSTD_LAYOUT: $DFL_OFSTD_LAYOUT"
echo "OFSTD_DIR_TREE_TYPE: $OFSTD_DIR_TREE_TYPE"
echo "SSL_CA_DIR: $SSL_CA_DIR"
echo "CERT_FNAME: $CERT_FNAME"
echo "PARENT_IA_FNAME: $PARENT_IA_FNAME"
echo "CRT_CERTS_DIR: $CRT_CERTS_DIR"
echo "PARENT_IA_DIR: $PARENT_IA_DIR"
fi
CERT_DB_DNAME="$DB_DNAME"
# Define full dirspec paths for all the things associated with this issuing authority (IA)
if [[ "$DFL_OFSTD_LAYOUT" == "traditional" ]]; then
CERT_KEY_DIR="$PARENT_IA_DIR/$PRIVATE_DNAME"
CERT_CERTS_DIR="$PARENT_IA_DIR/$CERTS_DNAME"
CERT_CSR_DIR="$PARENT_IA_DIR"
CERT_CRL_DIR="$PARENT_IA_DIR/$CRL_DNAME"
CERT_CHAIN_DIR="$PARENT_IA_DIR"
CERT_EXT_DIR="$PARENT_IA_DIR"
CERT_NEWCERTS_ARCHIVE_DIR="$PARENT_IA_DIR/$NEWCERTS_DNAME"
PARENT_IA_KEY_DIR="$PARENT_IA_DIR/$PRIVATE_DNAME"
PARENT_IA_CERTS_DIR="$PARENT_IA_DIR/$CERTS_DNAME"
PARENT_IA_EXT_DIR="$PARENT_IA_DIR"
PARENT_IA_DB_DIR="$PARENT_IA_DIR"
PARENT_IA_NEWCERTS_ARCHIVE_DIR="$PARENT_IA_DIR/$NEWCERTS_DNAME"
PARENT_IA_INDEX_DB_DIR="$PARENT_IA_DB_DIR"
PARENT_IA_SERIAL_DB_DIR="$PARENT_IA_DB_DIR"
PARENT_IA_CRL_DB_DIR="$PARENT_IA_DB_DIR"
CERT_OPENSSL_CNF_REQ_FILE="$CERT_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}__${CERT_FNAME}__req.cnf"
CERT_OPENSSL_CNF_REQ_EXTFILE="$CERT_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}__${CERT_FNAME}__reqext.cnf"
PARENT_IA_OPENSSL_CNF_CA_FILE="$PARENT_IA_EXT_DIR/${PARENT_IA_FNAME}.cnf"
PARENT_IA_OPENSSL_CNF_CA_EXTFILE="$PARENT_IA_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}-${CERT_FNAME}__ext.cnf"
else
CERT_EXT_DIR="$DFL_SSL_DIR/etc"
CERT_CRL_DIR="$DFL_SSL_DIR/$CRL_DNAME"
CERT_CERTS_DIR="$SSL_DIR/$CERTS_DNAME"
CERT_CSR_DIR="$SSL_CA_DIR"
CERT_CHAIN_DIR="$SSL_CA_DIR"
CERT_EXT_DIR="$DFL_SSL_DIR/etc"
CERT_CRL_DIR="$CERT_CERTS_DIR"
# CERT_CERTS_DIR="$CERT_CERTS_DIR"
CERT_CSR_DIR="$CERT_CERTS_DIR"
PARENT_IA_EXT_DIR="$CERT_EXT_DIR"
PARENT_IA_CERTS_DIR="$SSL_CA_DIR"
PARENT_IA_KEY_DIR="$PARENT_IA_DIR/$PRIVATE_DNAME"
PARENT_IA_DB_DIR="$PARENT_IA_DIR/$CERT_DB_DNAME"
CERT_NEWCERTS_ARCHIVE_DIR="$PARENT_IA_DIR" # where those 000x.pem files go
CERT_KEY_DIR="$SSL_DIR/$PRIVATE_DNAME"
PARENT_IA_NEWCERTS_ARCHIVE_DIR="$PARENT_IA_DIR" # where those 000x.pem files go
PARENT_IA_INDEX_DB_DIR="$PARENT_IA_DIR/$CERT_DB_DNAME"
PARENT_IA_SERIAL_DB_DIR="$PARENT_IA_DIR/$CERT_DB_DNAME"
PARENT_IA_CRL_DB_DIR="$PARENT_IA_DIR/$CERT_DB_DNAME"
CERT_OPENSSL_CNF_REQ_FILE="$CERT_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}__${CERT_FNAME}__req.cnf"
CERT_OPENSSL_CNF_REQ_EXTFILE="$CERT_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}__${CERT_FNAME}__reqext.cnf"
PARENT_IA_OPENSSL_CNF_CA_FILE="$PARENT_IA_EXT_DIR/${PARENT_IA_FNAME}.cnf"
PARENT_IA_OPENSSL_CNF_CA_EXTFILE="$PARENT_IA_EXT_DIR/${PARENT_IA_FNAME}__${CERT_NODE_TYPE}__${CERT_FNAME}__ext.cnf"
fi
if [[ "$VERBOSITY" -gt 1 ]]; then
echo "CERT_KEY_DIR: $CERT_KEY_DIR"
echo "CERT_CERTS_DIR: $CERT_CERTS_DIR"
echo "CERT_CSR_DIR: $CERT_CSR_DIR"
echo "CERT_CRL_DIR: $CERT_CRL_DIR"
echo "CERT_CHAIN_DIR: $CERT_CHAIN_DIR"
echo "CERT_EXT_DIR: $CERT_EXT_DIR"
echo "PARENT_IA_KEY_DIR: $PARENT_IA_KEY_DIR"
echo "PARENT_IA_CERTS_DIR: $PARENT_IA_CERTS_DIR"
echo "PARENT_IA_DB_DIR: $PARENT_IA_DB_DIR"
echo "CERT_NEWCERTS_ARCHIVE_DIR: $CERT_NEWCERTS_ARCHIVE_DIR"
echo "PARENT_IA_NEWCERTS_ARCHIVE_DIR: $PARENT_IA_NEWCERTS_ARCHIVE_DIR"
echo "CERT_OPENSSL_CNF_REQ_FILE: $CERT_OPENSSL_CNF_REQ_FILE"
echo "CERT_OPENSSL_CNF_REQ_EXTFILE: $CERT_OPENSSL_CNF_REQ_EXTFILE"
echo "PARENT_IA_OPENSSL_CNF_CA_FILE: $PARENT_IA_OPENSSL_CNF_CA_FILE"
echo "PARENT_IA_OPENSSL_CNF_CA_EXTFILE: $PARENT_IA_OPENSSL_CNF_CA_EXTFILE"
fi
if [[ "$DFL_OFSTD_LAYOUT" == "traditional" ]]; then
#PARENT_IA_FNAME_PREFIX="$FILENAME_PREFIX$PARENT_IA_NAME$FILENAME_SUFFIX"
PARENT_IA_FNAME_PREFIX="cakey" # It's in another directory
# IA_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$FILENAME_SUFFIX"
KEY_FNAME_PREFIX="cakey"
# Traditional CSR is user-defined, we automate it here
CSR_FNAME_PREFIX="ca-csr"
CERT_FNAME_PREFIX="cacert"
# CERT_SERIAL_FNAME="serial"
# CERT_INDEX_FNAME="index.txt"
# CERT_CRLNUMBER_FNAME="crlnumber"
PARENT_IA_SERIAL_FNAME="serial"
PARENT_IA_INDEX_FNAME="index.txt"
PARENT_IA_CRLNUMBER_FNAME="crlnumber"
CRL_FNAME_PREFIX="crl"
CHAIN_FILENAME_MID=".chain"
CHAIN_FILETYPE_SUFFIX=".pem"
# CHAIN_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CHAIN_FILENAME_MID$CHAIN_FILENAME_SUFFIX"
CHAIN_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CERT_FILENAME_SUFFIX$CHAIN_FILENAME_MID"
CERT_KEY_FNAME="${KEY_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
PARENT_IA_KEY_FNAME="${PARENT_IA_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
else
CERT_FFNAME_KEY="$CERT_FILENAME_SUFFIX.$DEFAULT_FILETYPE_KEY" # '-ca.key'
CERT_FFNAME_CSR="$CERT_FILENAME_SUFFIX.$DEFAULT_FILETYPE_CSR" # '-ca.csr'
CERT_FFNAME_CERT="$CERT_FILENAME_SUFFIX.$DEFAULT_FILETYPE_CERT" # '-ca.crt'
CERT_FFNAME_CRL="$CERT_FILENAME_SUFFIX.$DEFAULT_FILETYPE_CRL" # '-ca.crl'
PARENT_IA_FFNAME_KEY="$FILENAME_SUFFIX.$DEFAULT_FILETYPE_KEY" # '-ca.key'
# PARENT_IA_FFNAME_CSR="$FILENAME_SUFFIX.$DEFAULT_FILETYPE_CSR" # '-ca.csr'
PARENT_IA_FFNAME_CERT="$FILENAME_SUFFIX.$DEFAULT_FILETYPE_CERT" # '-ca.crt'
PARENT_IA_FFNAME_CRL="$FILENAME_SUFFIX.$DEFAULT_FILETYPE_CRL" # '-ca.crl'
PARENT_IA_FNAME_PREFIX="$FILENAME_PREFIX$PARENT_IA_NAME$PARENT_IA_FFNAME_KEY"
KEY_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CERT_FFNAME_KEY"
CSR_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CERT_FFNAME_CSR"
CRL_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CERT_FFNAME_CRL"
CERT_FNAME_PREFIX="$FILENAME_PREFIX$CERT_NAME$CERT_FFNAME_CERT"
PARENT_CERT_FNAME_PREFIX="$FILENAME_PREFIX$PARENT_IA_NAME$PARENT_IA_FFNAME_CERT"
CERT_KEY_FNAME="${KEY_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
# CERT_SERIAL_FNAME="$FILENAME_PREFIX$CERT_NAME$FFNAME_CERT.srl"
# CERT_INDEX_FNAME="$FILENAME_PREFIX$CERT_NAME$CERT_FILENAME_SUFFIX.$DB_DNAME"
# CERT_CRLNUMBER_FNAME="$FILENAME_PREFIX$CERT_NAME$FFNAME_CRL.srl"
CHAIN_FILENAME_MID="-chain"
CHAIN_FILETYPE_SUFFIX=".pem"
CHAIN_FNAME_PREFIX="$CERT_FILENAME_PREFIX$CERT_NAME$CERT_FILENAME_SUFFIX$CHAIN_FILENAME_MID"
PARENT_IA_KEY_FNAME="${PARENT_IA_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
PARENT_IA_SERIAL_FNAME="$FILENAME_PREFIX$PARENT_IA_NAME$PARENT_IA_FFNAME_CRL.srl"
PARENT_IA_INDEX_FNAME="$FILENAME_PREFIX$PARENT_IA_NAME$FILENAME_SUFFIX.$DB_DNAME"
PARENT_IA_CRLNUMBER_FNAME="$FILENAME_PREFIX$PARENT_IA_NAME$CERT_FFNAME_CRL.srl"
fi
CERT_CSR_FNAME="${CSR_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
CERT_CERT_FNAME="${CERT_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
PARENT_IA_CERT_FNAME="${PARENT_CERT_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
CERT_CRL_FNAME="${CRL_FNAME_PREFIX}${PEM_FILETYPE_SUFFIX}"
CERT_CHAIN_FNAME="${CHAIN_FNAME_PREFIX}${CHAIN_FILETYPE_SUFFIX}"
if [[ "$VERBOSITY" -gt 1 ]]; then
echo "PARENT_IA_KEY_FNAME: $PARENT_IA_KEY_FNAME"
echo "CERT_KEY_FNAME: $CERT_KEY_FNAME"
echo "CERT_CSR_FNAME: $CERT_CSR_FNAME"
echo "CERT_CERT_FNAME: $CERT_CERT_FNAME"
echo "PARENT_IA_CERT_FNAME: $PARENT_IA_CERT_FNAME"
echo "CERT_CRL_FNAME: $CERT_CRL_FNAME"
echo "CERT_CHAIN_FNAME: $CERT_CHAIN_FNAME"
fi
CERT_KEY_PEM="$CERT_KEY_DIR/$CERT_KEY_FNAME"
CERT_CSR_PEM="$CERT_CSR_DIR/$CERT_CSR_FNAME"
CERT_CERT_PEM="$CERT_CERTS_DIR/$CERT_CERT_FNAME"
CERT_CRL_PEM="$CERT_CRL_DIR/$CERT_CRL_FNAME"
CERT_CHAIN_PEM="$CERT_CHAIN_DIR/$CERT_CHAIN_FNAME"
PARENT_IA_CERT_PEM="$PARENT_IA_CERTS_DIR/$PARENT_IA_CERT_FNAME"
PARENT_IA_KEY_PEM="$PARENT_IA_KEY_DIR/$PARENT_IA_KEY_FNAME"
PARENT_IA_INDEX_DB="$PARENT_IA_INDEX_DB_DIR/$PARENT_IA_INDEX_FNAME"
PARENT_IA_SERIAL_DB="$PARENT_IA_SERIAL_DB_DIR/$PARENT_IA_SERIAL_FNAME"
PARENT_IA_CRL_DB="$PARENT_IA_CRL_DB_DIR/$PARENT_IA_CRLNUMBER_FNAME"
if [[ "$VERBOSITY" -gt 1 ]]; then
echo "PARENT_IA_KEY_PEM: $PARENT_IA_KEY_PEM"
echo "CERT_KEY_PEM: $CERT_KEY_PEM"
echo "CERT_CSR_PEM: $CERT_CSR_PEM"
echo "CERT_CERT_PEM: $CERT_CERT_PEM"
echo "PARENT_IA_CERT_PEM: $PARENT_IA_CERT_PEM"
echo "CERT_CRL_PEM: $CERT_CRL_PEM"
echo "CERT_CHAIN_PEM: $CERT_CHAIN_PEM"
echo "PARENT_IA_INDEX_DB: $PARENT_IA_INDEX_DB"
echo "PARENT_IA_SERIAL_DB: $PARENT_IA_SERIAL_DB"
echo "PARENT_IA_CRL_DB: $PARENT_IA_CRL_DB"
fi
unset DFL_OFSTD_LAYOUT
unset DFL_OFSTD_DIR_TREE_TYPE
}
function change_owner_perm {
CHOP_USER="$1"
CHOP_GROUP="$2"
CHOP_PERM="$3"
CHOP_FILESPEC="$4"
chown "$CHOP_USER:$CHOP_GROUP" "$CHOP_FILESPEC"
RETSTS=$?
if [[ ${RETSTS} -ne 0 ]]; then
echo "Error $RETSTS setting $CHOP_USER:$CHOP_GROUP owner to $CHOP_FILESPEC; aborting..."
exit ${RETSTS}
fi
chmod "$CHOP_PERM" "$CHOP_FILESPEC"
RETSTS=$?
if [[ ${RETSTS} -ne 0 ]]; then
echo "Error $RETSTS setting $CHOP_USER:$CHOP_GROUP owner to $CHOP_FILESPEC; aborting..."
exit ${RETSTS}
fi
unset CHOP_USER
unset CHOP_GROUP
unset CHOP_PERM
unset CHOP_FILESPEC
}
function touch_cert_file {
TOUCH_THIS_FILE="$1"
if [[ -d ${TOUCH_THIS_FILE} ]]; then
# it does wonder to the file system by touching a directory (EXT4 corruption)
echo "File $TOUCH_THIS_FILE is already directory "
echo "(and untouchable); aborting..."
exit 1
fi
[[ ${VERBOSITY} -gt 0 ]] && echo "touch $1"
touch "$1"
change_owner_perm "$SSL_USER_NAME" "$SSL_GROUP_NAME" 0640 "$1"
}
function delete_file {
DELETE_FILE="${1:-/tmp/nope}" # emergency undefined $1 protection
if [[ -f "$DELETE_FILE" ]]; then
[[ ${VERBOSITY} -gt 0 ]] && echo "rm $DELETE_FILE"
rm "$DELETE_FILE"
fi
}
function delete_cert_config {
delete_file "$CERT_OPENSSL_CNF_REQ_FILE" # loses any user-customization(s)
delete_file "$CERT_OPENSSL_CNF_REQ_EXTFILE" # loses any user-customization(s)
# DO NOT DELETE PARENT_IA_OPENSSL_CNF_CA_FILE, that's the CA's own main config file
delete_file "$PARENT_IA_OPENSSL_CNF_CA_EXTFILE" # loses any user-customization(s)
}
function delete_cert_dirfiles {
delete_file "$CERT_KEY_PEM"
delete_file "$CERT_CSR_PEM"
delete_file "$CERT_CERT_PEM"
delete_file "$CERT_CRL_PEM"
delete_file "$CERT_CHAIN_PEM"
if [[ ${FORCE_DELETE_CONFIG} -eq 1 ]]; then
delete_cert_config
fi
}
function write_line_or_no
{
WLON_KEY_NAME=$1
WLON_VALUE_NAME=$2
WLON_INTERNODE_CONFIG_FILESPEC=$3
if [[ -n "$WLON_VALUE_NAME" ]]; then
echo "$WLON_KEY_NAME = $WLON_VALUE_NAME" >> "$WLON_INTERNODE_CONFIG_FILESPEC"
fi
unset WLON_KEY_NAME
unset WLON_VALUE_NAME
unset WLON_INTERNODE_CONFIG_FILESPEC
}
# Usage: create_cert_config
# <section_name> \
# <internode_filespec>
function write_generic_cert_req_config_file
{
CGCRCF_SECTION_NAME=$1
CGCRCF_CNFFILE=$2
CGCRCF_CURRENT_TIMESTAMP=$(date)
echo "Creating $CGCRCF_CNFFILE file ..."
cat << CERT_REQ_EOF | tee "$CGCRCF_CNFFILE" >/dev/null
# File: $CGCRCF_CNFFILE
# Created on: $CGCRCF_CURRENT_TIMESTAMP
# Generated by: $0/write_generic_cert_req_config_file()
#
# Description:
# Create generic Certificate Request main configuration file.
#
# This file is used by the 'openssl req' command as main\
# configuration file to create a end-user certificate.
#
# Since we cannot know the DN in advance, the user
# must be prompted for DN information.
#
# An OpenSSL extension configuration file that contains
# key-value pair that characterize the relationship between
# the parent CA and itself.
#
# Usage:
# openssl req -config $CGCRCF_CNFFILE \\
# -reqexts $CGCRCF_SECTION_NAME \\
# ...
#
# Section Name breakdown:
# req: section name '[ req ]'; used by 'openssl req'
# x509_extensions: Key name (to a key-value statement)
# ${PARENT_IA_NAME}: Parent CA's config file
# ${CERT_NAME}: this node
#
[ default ]
base_url = ${X509_URL_BASE} # CA base URL
aia_url = ${X509_URL_BASE}/${IA_CERT_FNAME} # CA certificate URL
crl_url = ${X509_CRL} # CRL distribution point
ocsp_url = ${X509_URL_OCSP}
[ req ]
distinguished_name = ${NODE_TYPE}_dn # DN template
req_extensions = $CGCRCF_SECTION_NAME # Desired extensions
default_bits = $KEYSIZE_BITS # RSA key size
encrypt_key = yes # Protect private key
default_md = $MESSAGE_DIGEST # MD to use
utf8 = yes # Input is UTF-8
string_mask = utf8only # Emit UTF-8 strings
# if you flip this 'prompt' value, you must intensively change *_dn
prompt = no # Prompt for DN
# insert [ v3_ca ] here to compensate for macOS bastardizing OpenSSH
[ ${NODE_TYPE}_dn ]
countryName = $X509_COUNTRY
# countryName = Country Name (2-letter code)
# countryName_default = $X509_COUNTRY
# countryName_min = 2
# countryName_max = 2
stateOrProvinceName = $X509_STATE
# stateOrProvinceName = State or Province Name (full name)
# stateOrProvinceName_default = $X509_STATE
localityName = $X509_LOCALITY
# localityName = Locality Name (eg, city)
# localityName_default = $X509_LOCALITY
0.organizationName = $X509_ORG
# 0.organizationName = Organization Name (eg, company)
# 0.organizationName_default = $X509_ORG
organizationalUnitName = $X509_OU
# organizationalUnitName = Organizational Unit Name (eg, section)
# organizationalUnitName_default = $X509_OU
commonName = $X509_COMMON
# commonName = Common Name (e.g. server FQDN or YOUR name)
# commonName_default = $X509_COMMON
# commonName_max = 64
emailAddress = $X509_EMAIL
# emailAddress = Email Address
# emailAddress_default = $X509_EMAIL
# emailAddress_max = 64
CERT_REQ_EOF
# so far, 'server' appears to be the only SAN
if [ "$NODE_TYPE" == 'server' ]; then
#
# Break apart domain name into domain labels
X509_COMMON_LIST="$(cat "$X509_COMMON" | sed 's/\./ /g')"
# Adjust for 0-based indices
X509_COMMON_COUNT="$(cat "$X509_COMMON_LIST" | wc -w)"
if [ "$X509_COMMON_COUNT" -lt 2 ]; then
echo
echo "WARNING: Your data-entry of commonName did not appear "
echo "to be a fully-qualified domain name."
echo "I could not count beyond 1 dots."
echo "Ignoring..."
echo
fi
((X509_COMMON_COUNT--))
if [ $X509_COMMON_COUNT -ge 0 ]; then
# Reverse the domain labels
X509_COMMON_REVLIST="$(echo "$X509_COMMON_LIST" | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }')"
idx=0
for domain_label in $X509_COMMON_REVLIST; do
echo "${idx}.domainComponent = \"${domain_label}\"" >> "$CGCRCF_CNFFILE"
((idx++))
done
fi
fi
if [ 0 -ne 0 ]; then
cat << CERT_REQ_EOF | tee -a "$CGCRCF_CNFFILE" >/dev/null
# domainComponent is not used by 'identity.conf'
0.domainComponent = Top-level Domain Name (i.e., com, net, org)
0.domainComponent_default =
1.domainComponent = The Domain Name (i.e., example, test, acme)
1.domainComponent_default = example
2.domainComponent = Sub-Domain Name (i.e., www, ocsp, or 'blank')
2.domainComponent_default =
CERT_REQ_EOF
fi # [ 0 -ne 0 ]
cat << CERT_REQ_EOF | tee -a "$CGCRCF_CNFFILE" >/dev/null
[ $CGCRCF_SECTION_NAME ]
CERT_REQ_EOF
write_line_or_no "keyUsage" "$CNF_REQ_EXT_KU" "$CGCRCF_CNFFILE"
write_line_or_no "basicConstraints" "$CNF_REQ_EXT_BC" "$CGCRCF_CNFFILE"
write_line_or_no "subjectKeyIdentifier" "$CNF_REQ_EXT_SKI" "$CGCRCF_CNFFILE"
write_line_or_no "authorityKeyIdentifier" "$CNF_REQ_EXT_AKI" "$CGCRCF_CNFFILE"
write_line_or_no "extendedKeyUsage" "$CNF_REQ_EXT_EKU" "$CGCRCF_CNFFILE"
write_line_or_no "subjectAltName" "$CNF_REQ_EXT_SAN" "$CGCRCF_CNFFILE"
write_line_or_no "authorityInfoAccess" "$CNF_REQ_EXT_AIA" "$CGCRCF_CNFFILE"
write_line_or_no "crlDistributionPoints" "$CNF_REQ_EXT_CRL" "$CGCRCF_CNFFILE"
if [ -n "$CNF_CA_EXT_EXTRA" ]; then
echo "$CNF_CA_EXT_EXTRA" >> "$CGCRCF_CNFFILE"
fi
for idx in $(seq 0 ${#CNF_CA_EXT_EXTRA_A[@]} | xargs); do
echo "${CNF_CA_EXT_EXTRA_A[$idx]}" >> "$CGCRCF_CNFFILE"
done
cat << RCF_CNF_EOF | tee -a "$CGCRCF_CNFFILE" > /dev/null
[ crl_ext ]
authorityKeyIdentifier = keyid:always
authorityInfoAccess = @issuer_info
[ ocsp_info ]
caIssuers;URI.0 = \$aia_url
OCSP;URI.0 = \$ocsp_url
[ issuer_info ]
caIssuers;URI.0 = \$aia_url
[ crl_info ]
URI.0 = \$crl_url
RCF_CNF_EOF
unset CIC_SECTION_NAME
unset CIC_CONFIG_FILESPEC
unset CIC_TIMESTAMP
}
# Creates an extension file that details the
# relatioship between parent CA and its child CA
#
function write_generic_ca_extension_config_file
{
CCEC_SECTION_NAME="$1"
CCEC_EXTFILE="$2"
CCEC_CURRENT_TIMESTAMP=$(date)
echo "**************Creating $CCEC_EXTFILE file ..."
cat << CA_EXT_EOF | tee "$CCEC_EXTFILE" >/dev/null
# File: $CCEC_EXTFILE
# Created on: $CCEC_CURRENT_TIMESTAMP
# Generated by: $0/write_generic_ca_extension_config_file()
#
# Description:
# Generic CA certificate extension configuration file.
#
# This file makes it possible to extend functionality of
# the parent Signing CA to supporting different types of certificates
#
# This file is used by the 'openssl ca' command.
#
# An OpenSSL extension configuration file that contains
# key-value pair that characterize the relationship between
# the parent CA and itself.
#
# Usage:
# openssl ca -config $PARENT_IA_OPENSSL_CNF_CA_FILE \\
# -extfile $CCEC_EXTFILE \\
# -extension $CCEC_SECTION_NAME \\
# ...
#
# Section Name breakdown:
# ca: section name '[ ca ]'; used by 'openssl ca'
# x509_extensions: Key name (to a key-value statement)
# ${PARENT_IA_NAME}: Parent CA's config file
# ${CERT_NAME}: this node
#
base_url = ${X509_URL_BASE} # CA base URL
aia_url = ${X509_URL_BASE}/${IA_CERT_FNAME} # CA certificate URL
crl_url = ${X509_CRL} # CRL distribution point
ocsp_url = ${X509_URL_OCSP}
# Section name could be a simple ${NODE_TYPE} name
# But having CA_TYPE-CA_NAME makes it possible to support
# different class of the same CA-TYPE by using CA_NAMe as a unique label
[ $CCEC_SECTION_NAME ]
CA_EXT_EOF
write_line_or_no "keyUsage" "$CNF_CA_EXT_KU" "$CCEC_EXTFILE"
write_line_or_no "basicConstraints" "$CNF_CA_EXT_BC" "$CCEC_EXTFILE"
write_line_or_no "subjectKeyIdentifier" "$CNF_CA_EXT_SKI" "$CCEC_EXTFILE"
write_line_or_no "authorityKeyIdentifier" "$CNF_CA_EXT_AKI" "$CCEC_EXTFILE"
write_line_or_no "extendedKeyUsage" "$CNF_CA_EXT_EKU" "$CCEC_EXTFILE"
write_line_or_no "subjectAltName" "$CNF_CA_EXT_SAN" "$CCEC_EXTFILE"
write_line_or_no "authorityInfoAccess" "$CNF_CA_EXT_AIA" "$CCEC_EXTFILE"
write_line_or_no "crlDistributionPoints" "$CNF_CA_EXT_CRL" "$CCEC_EXTFILE"
cat << EXTFILE_EOF | tee -a "$CCEC_EXTFILE" >/dev/null
#
[ ocsp_info ]
caIssuers;URI.0 = \$aia_url
OCSP;URI.0 = \$ocsp_url
[ issuer_info ]
caIssuers;URI.0 = \$aia_url
[ crl_info ]
URI.0 = \$crl_url
[openssl_init]
ssl_conf = ssl_sect
[ssl_sect]
system_default = system_default_sect
[system_default_sect]
MinProtocol = TLSv1.2
CipherString = DEFAULT@SECLEVEL=2
RSA.Certificate = server-rsa.pem
ECDSA.Certificate = server-ecdsa.pem
EXTFILE_EOF
unset CCEC_SECTION_NAME
unset CCEC_EXTFILE
unset CCEC_TIMESTAMP
}
#########################################################
# Create the public key for a CA node #
#########################################################
function cert_create_public_key
{
# pre-privacy
touch_cert_file "$CERT_KEY_PEM"
change_owner_perm "$SSL_USER_NAME" "$SSL_GROUP_NAME" 0640 "$CERT_KEY_PEM"
# shellcheck disable=SC2086
execute ${OPENSSL_GENPKEY} \
${OPENSSL_ALGORITHM} \
-text \
-outform PEM \
-out "${CERT_KEY_PEM}"
RETSTS=$?
if [[ ${RETSTS} -ne 0 ]]; then
echo "Error $RETSTS in 'openssl genpkey'; aborting..."
exit ${RETSTS}
fi
if [[ -z "$DRY_RUN" ]]; then
if [[ ! -f "$CERT_KEY_PEM" ]]; then
echo "Failed to create private key for $CERT_NODE_TYPE ($CERT_KEY_PEM)"
exit 126 # ENOKEY
fi
change_owner_perm "$SSL_USER_NAME" "$SSL_GROUP_NAME" 0640 "$CERT_KEY_PEM"
fi
if [[ ${VERBOSITY} -ne 0 ]]; then
# View the private key in readable format
execute ${OPENSSL_ASN1PARSE} -in "$CERT_KEY_PEM"
execute ${OPENSSL_PKEY} \
-in "$CERT_KEY_PEM" \
-noout \
-text
fi
}
#########################################################
# Create the CA node's signing request certificate #
# Usage: cert_create_csr <section_name> \
# <internode_filespec> \
# <parent_node_name>
#########################################################
function cert_create_csr
{
CCC_SECTION_NAME=$1
CCC_CERT_CONFIG_FILESPEC=$2
# CCC_CERT_CONFIG_EXT_FILESPEC=$3
# Do not use -keyout, we have cert_create_public_key() function for that
# shellcheck disable=SC2086
execute ${OPENSSL} req -config "${CCC_CERT_CONFIG_FILESPEC}" \
-reqexts "$CCC_SECTION_NAME" \
-new \
-key "$CERT_KEY_PEM" \
"$MESSAGE_DIGEST_CMDOPT" \
-out "$CERT_CSR_PEM"
RETSTS=$?
if [[ ${RETSTS} -ne 0 ]]; then
echo "Error $RETSTS in 'openssl req'; aborting..."
exit ${RETSTS}
fi
if [[ -z "$DRY_RUN" ]]; then
if [[ ! -f "$CERT_CSR_PEM" ]]; then
echo "Failed to create signing request for $CERT_NODE_TYPE ($CERT_CSR_PEM)"
exit 2 # ENOENT
fi
change_owner_perm "$SSL_USER_NAME" "$SSL_GROUP_NAME" 0640 "$CERT_CSR_PEM"
fi
if [[ ${VERBOSITY} -ne 0 ]]; then
# View the CSR in readable format
execute ${OPENSSL_ASN1PARSE} -in "$CERT_CSR_PEM"
execute ${OPENSSL_REQ} -in "$CERT_CSR_PEM" -noout -text
fi
unset CCC_SECTION_NAME
unset CCC_INTERNODE_CONFIG_FILESPEC
unset CCC_PARENT_CONFIG_FILESPEC
}
###############################################
# Parent CA accept CA node's CSR by trusting #
###############################################
function cert_create_certificate {
echo "Creating $CERT_NAME ($NODE_TYPE) certificate ..."
# CCC_CERT_OPENSSL_CNF_EXTENSION="$1"
CCC_CERT_OPENSSL_CNF_EXTFILE="$2"
CCC_CERT_SECTION_NAME="$3"
# shellcheck disable=SC2086
execute ${OPENSSL_CA} \
-batch \