Skip to content

Commit 748fdbe

Browse files
authored
Merge pull request #6 from haensl/feature/5
Add option to specify ca key password. Add option to specify ca certi…
2 parents bb7d357 + 2c6509c commit 748fdbe

File tree

5 files changed

+91
-13
lines changed

5 files changed

+91
-13
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.0
2+
* [#5: Add option to specify ca key password.](https://github.com/haensl/openssl-certgen/issues/3)
3+
* Add option to specify ca certificate subject.
4+
15
## 1.1.2
26
* [#3: Fix directory and symlink issue on OSX.](https://github.com/haensl/openssl-certgen/issues/3)
37
* Add option to overwrite existing version.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ For additional information please consider consulting the man page.
2525

2626
### Synopsis
2727
```bash
28-
openssl-generate-certificates -h hostname [-i ip] [-p prefix] [-v]
28+
openssl-generate-certificates -h hostname [-i ip] [-j subject] [-p prefix] [-s password] [-v]
2929
```
3030

3131
Generated certificate files and keys are written to the current working directory.
@@ -40,10 +40,18 @@ Specify hostname or IP of server to generate certificates for.
4040

4141
Specify additional ips to bind to `hostname`. *Default: 127.0.0.1*
4242

43+
`-j subject, --subject subject`
44+
45+
Specify the subject string to use when generating the CA certificate.
46+
4347
`-p prefix, --prefix prefix`
4448

4549
Specify a prefix for output filenames.
4650

51+
`-s password, --secret password`
52+
53+
Specify the password with which to encrypt the CA key-signing key.
54+
4755
`-v, --verbose`
4856

4957
Print verbose output.

man/openssl-generate-certificates.1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ openssl-generate-certificates - Generate self-signed certificates for client/ser
77
.SH SYNOPSIS
88
openssl-generate-certificates [-v]
99
[-i \fI\,ip\fR]
10+
[-j \fI\,subject\fR]
1011
[-p \fI\,prefix\fR]
12+
[-s \fI\,password\fR]
1113
-h \fI\,hostname\fR
1214

1315
.SH DESCRIPTION
@@ -27,6 +29,8 @@ The client's private key.
2729
The client certificate.
2830
.RE
2931

32+
\fI\,openssl-generate-certificates\fR was created to improve automated generation of client/server key pairs and comes equiped with a set of options to automate certificate generation.
33+
3034
For convenience and future reference \fI\,openssl-generate-certificates\fR also saves the client and server extension files containing respective certificate configuration.
3135

3236
All generated assets are saved to the current working directory.
@@ -51,11 +55,21 @@ Specify the hostname or IP address of the server.
5155
Specify additional ip(s) to associate with \fI\,hostname\fR. Default: 127.0.0.1
5256
.RE
5357

58+
-j \fI\,subject\fR, --subject \fI\,subject\fR
59+
.RS
60+
Specify the subject string to use when generating the ca certificate.
61+
.RE
62+
5463
-p \fI\,prefix\fR, --prefix \fI\,prefix\fR
5564
.RS
5665
Specify a prefix for output filenames. All generated assets will be prefixed with the given string.
5766
.RE
5867

68+
-s \fI\,password\fR, --secret \fI\,password\fR
69+
.RS
70+
Specify the password with which to encrypt the key-signing key.
71+
.RE
72+
5973
-v, --verbose
6074
.RS
6175
Print verbose output.
152 Bytes
Binary file not shown.

openssl-generate-certificates

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
#!/usr/bin/env bash
22

3+
set -e
34
readonly ARGS="$@"
45

56
usage() {
67
local file=`basename "$0"`
78
cat <<-EOF
8-
./${file} -h|--host hostname/ip [-i ip] [-p|--prefix certificate-prefix] [-v|--verbose]
9+
./${file} -h hostname [-i ip] [-j subject] [-p prefix] [-s password] [-v|--verbose]
910
1011
Options:
11-
-h hostname, --host hostname specify hostname or ip of server
12-
-i ip, --ip ip specify an ip to bind to hostname
13-
-p prefix, --prefix prefix specify a prefix for output files
14-
-v, --verbose print verbose output
12+
-h hostname, specify hostname or ip of server
13+
--host hostname
14+
15+
-i ip, specify an ip to bind to hostname
16+
--ip ip
17+
18+
-j subject, specify the certificate subject string
19+
--subject subject
20+
21+
-p prefix, specify a prefix for output files
22+
--prefix prefix
23+
24+
-s password, specify the password to use for the ca certificate
25+
--secret password
26+
27+
-v, print verbose output
28+
--verbose
1529
EOF
1630
}
1731

@@ -21,9 +35,11 @@ parseArgs() {
2135
for arg
2236
do
2337
case "$arg" in
38+
--subject) args="${args}-j " ;;
2439
--host) args="${args}-h " ;;
2540
--ip) args="${args}-i " ;;
2641
--prefix) args="${args}-p " ;;
42+
--secret) args="${args}-s " ;;
2743
--verbose) args="${args}-v " ;;
2844
*) args="${args} ${arg} " ;;
2945
esac
@@ -36,7 +52,9 @@ parseArgs() {
3652
local prefix
3753
local ips=()
3854
local verbose=1
39-
while getopts "p:h:i:" OPTION;
55+
local secret
56+
local subject
57+
while getopts "c:h:i:j:p:s:v" OPTION;
4058
do
4159
case $OPTION in
4260
h)
@@ -45,9 +63,15 @@ parseArgs() {
4563
i)
4664
ips+=("$OPTARG")
4765
;;
66+
j)
67+
subject="$OPTARG"
68+
;;
4869
p)
4970
prefix="$OPTARG-"
5071
;;
72+
s)
73+
secret="$OPTARG"
74+
;;
5175
v)
5276
verbose=0
5377
;;
@@ -72,6 +96,8 @@ parseArgs() {
7296
readonly IPS=${ips[@]}
7397
readonly PREFIX=${prefix}
7498
readonly VERBOSE=${verbose}
99+
readonly PASSWORD=${secret}
100+
readonly SUBJECT=${subject}
75101

76102
generateCertificates
77103
}
@@ -81,13 +107,29 @@ generateCertificates() {
81107
echo "host: ${HOST}"
82108
echo "ips: ${IPS[@]}"
83109
echo "prefix: ${PREFIX}"
110+
echo "password: ${PASSWORD}"
111+
echo "subject: ${SUBJECT}"
84112

85113
echo 'Generating CA key'
86114
fi
87-
openssl genrsa -aes256 -out ${PREFIX}ca-key.pem 4096
115+
116+
local ca_genrsa_opts="-aes256 -out ${PREFIX}ca-key.pem"
117+
if [ -n ${PASSWORD} ]; then
118+
ca_genrsa_opts="-passout pass:${PASSWORD} ${ca_genrsa_opts}"
119+
fi
120+
echo "${ca_genrsa_opts}"
121+
openssl genrsa ${ca_genrsa_opts} 4096
88122

89123
[ ${VERBOSE} -eq 0 ] && echo 'Generating CA certificate'
90-
openssl req -new -x509 -days 365 -key ${PREFIX}ca-key.pem -sha256 -out ${PREFIX}ca-cert.pem
124+
local ca_req_opts="-new -x509 -days 365 -key ${PREFIX}ca-key.pem -sha256 -out ${PREFIX}ca-cert.pem"
125+
if [ -n ${PASSWORD} ]; then
126+
ca_req_opts="-passin pass:${PASSWORD} ${ca_req_opts}"
127+
fi
128+
129+
if [ -n ${SUBJECT} ]; then
130+
ca_req_opts="-subj ${SUBJECT} ${ca_req_opts}"
131+
fi
132+
openssl req ${ca_req_opts}
91133

92134
[ ${VERBOSE} -eq 0 ] && echo 'Generating server key'
93135
openssl genrsa -out ${PREFIX}server-key.pem 4096
@@ -104,8 +146,13 @@ generateCertificates() {
104146
echo 'extendedKeyUsage = serverAuth' >> ${PREFIX}server-extfile.cnf
105147

106148
[ ${VERBOSE} -eq 0 ] && echo 'Signing server certificate'
107-
openssl x509 -req -days 365 -sha256 -in ${PREFIX}server.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
108-
-CAcreateserial -out ${PREFIX}server-cert.pem -extfile ${PREFIX}server-extfile.cnf
149+
if [ -z ${PASSWORD} ]; then
150+
openssl x509 -req -days 365 -sha256 -in ${PREFIX}server.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
151+
-CAcreateserial -out ${PREFIX}server-cert.pem -extfile ${PREFIX}server-extfile.cnf
152+
else
153+
openssl x509 -req -days 365 -sha256 -in ${PREFIX}server.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
154+
-CAcreateserial -out ${PREFIX}server-cert.pem -extfile ${PREFIX}server-extfile.cnf -passin pass:${PASSWORD}
155+
fi
109156

110157
[ ${VERBOSE} -eq 0 ] && echo 'Generating client key'
111158
openssl genrsa -out ${PREFIX}client-key.pem 4096
@@ -117,8 +164,13 @@ generateCertificates() {
117164
echo 'extendedKeyUsage = clientAuth' >> ${PREFIX}client-extfile.cnf
118165

119166
[ ${VERBOSE} -eq 0 ] && echo 'Signing client certificate'
120-
openssl x509 -req -days 365 -sha256 -in ${PREFIX}client.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
121-
-CAcreateserial -out ${PREFIX}client-cert.pem -extfile ${PREFIX}client-extfile.cnf
167+
if [ -z ${PASSWORD} ]; then
168+
openssl x509 -req -days 365 -sha256 -in ${PREFIX}client.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
169+
-CAcreateserial -out ${PREFIX}client-cert.pem -extfile ${PREFIX}client-extfile.cnf
170+
else
171+
openssl x509 -req -days 365 -sha256 -in ${PREFIX}client.csr -CA ${PREFIX}ca-cert.pem -CAkey ${PREFIX}ca-key.pem \
172+
-CAcreateserial -out ${PREFIX}client-cert.pem -extfile ${PREFIX}client-extfile.cnf -passin pass:${PASSWORD}
173+
fi
122174

123175
[ ${VERBOSE} -eq 0 ] && echo 'Removing signing requests'
124176
rm ${PREFIX}server.csr ${PREFIX}client.csr

0 commit comments

Comments
 (0)