-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeca
executable file
·140 lines (130 loc) · 5.15 KB
/
makeca
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
#!/usr/bin/env bash
#
# Copyright 1996 - 2012 Luc Suryo <[email protected]>.
# Copyright 2012 - 2020 (c) Badassops LLC / Luc Suryo
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSEcw
# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#*
#* File : makeca
#*
#* Description : script to make a CA X500-SSL certificate
#*
#* Author : Luc Suryo <[email protected]>
#*
#* Version : 0.6
#*
#* Date : May 23, 2020
#*
#* History :
#* Date: Author: Info:
#* March 1, 1996 LIS First Release
#* July 20, 2010 LIS New license
#* Aug 10, 2012 LIS Set Copyright to Badassops LLC
#* Nov 23, 2015 LIS Adjusted license and variable for Poynt
#* and made script with the standard setup
#* May 21, 2020 LIS fixed several bug, add color
#* may 23, 2020 LIS change to use libraries
#*
_program="${0##*/}"
_author='Luc Suryo'
_copyright="Copyright 2012 - $(date "+%Y") (c) Badassops LLC"
_license='License BSD, http://www.freebsd.org/copyright/freebsd-license.html'
_version='0.6'
_email='[email protected]'
_summary='script to make a CA X500-SSL certificate'
_cancelled="OK : Process has been cancelled on your request."
_info="$_program $_version\n$_copyright\n$_license\n\nWritten by $_author <$_email>\n$_summary\n"
_text_before_start="This script will create your own CA certificate, this should not be used in live production!"
source lib/variables.var
source lib/color.func
source lib/cleanup.func
source lib/check_is_root.func
source lib/check_running.func
source lib/make_env.func
source lib/make_ca_config.func
source lib/make_key.func
source lib/check_ca.func
source lib/decrypt_key.func
source lib/create_it.func
source lib/make_pass_file.func
source lib/main.func
# working variables
# hardcode ca name unless overwritten in env
_my_cert_name="ca"
_my_cert_dir="./ca"
_my_ca_cnf="$_my_cert_dir/ca.cnf"
_my_domain=
function inthandler() {
echo $_echo_flag "\n$_color_bold_red\t!!! Something when wrong or Control-C was press, deleting all certs file for $_my_cert_name !!!$_reset_color"
clean_up_upon_fail
clean_up
echo $_echo_flag "$_cancelled"
exit 0
}
# Set interrupt handler
trap inthandler 1 2 3 4 5 9 10 12 15 23 24 25
function help() {
trap 1 2 3 4 5 9 10 12 15 23 24 25
echo $_echo_flag "$_info"
echo $_echo_flag "Usage : $_program [-h] [-d domain name]"
echo $_echo_flag "Note the -d option is required if no env file exist"
clean_up
exit 0
}
function get_given_options() {
local _option_domain=0
local _has_error=0
case "$1" in
'-d'|'--domain'|'-domain') _my_domain=$2 ; shift ; let let _option_domain++ ;;
'-h'|'--help'|'-help') help ; exit $_state_ok ;;
'-v'|'--version'|'-version') echo $_echo_flag "$_version" ; exit $_state_ok ;;
esac
(( $_option_domain > 1 )) && echo $_echo_flag "\n\t: -d can only be given once." && help
[[ -z $_my_domain ]] && echo $_echo_flag "\n\t: domain is not given, this is required, use the vn file or the -d flage." && help
_subject_alt_name="DNS:$_my_cert_name, DNS:$_my_cert_name.$_my_domain"
_my_cert_crt="$_my_cert_name".crt
_my_cert_key="$_my_cert_name".key
_my_cert_csr="$_my_cert_name".csr
_my_cert_pass="$_my_cert_name".pass
_cert_files="$_my_cert_crt $_my_cert_key $_my_cert_csr $_my_cert_pass ${_my_cert_key}.crypt"
return 0
}
function create_csr() {
echo $_echo_flag "\t$_color_bold_cyan Generating X.509 certificate request for the CA [${_my_cert_csr}]$_reset_color"
$_openssl req -nodes -config $_my_ca_cnf -new -key "$_my_cert_dir"/"$_my_cert_key" -out "$_my_cert_dir"/"$_my_cert_csr"
(( $? != 0 )) && return 1
return 0
}
function create_cert() {
echo $_echo_flag "\t*** Generating X.509 certificate for CA signed by itself [$_my_cert_crt] ***"
$_openssl ca -selfsign \
-config $_my_ca_cnf \
-in "$_my_cert_dir"/$_my_cert_csr \
-out "$_my_cert_dir"/$_my_cert_crt \
-extensions root-ca_ext
(( $? != 0 )) && return 1
return 0
}
main "$@"