Skip to content

Commit f396d4d

Browse files
authored
Merge pull request #4 from j-hartley/main
Add script to create TLS client cert and CA
2 parents e727bae + 8431da5 commit f396d4d

File tree

6 files changed

+198
-6
lines changed

6 files changed

+198
-6
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.token
2+
*.req
3+
*.key
4+
*.pem
5+
*.tar.gz

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ tasks:
55
check:
66
desc: Standard linting of shell scripts
77
cmds:
8-
- shellcheck scripts/*.sh
8+
- find scripts -type f -name '*.sh' | xargs shellcheck
99

1010
clean:
1111
desc: Clean git repo

scripts/example.sh

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
SCRIPTNAME=$(basename "$0")
7+
8+
# check that the required tools are installed
9+
type openssl 2>/dev/null || ( echo >&2 "openssl command not found, please install or add to PATH" && exit 1 )
10+
11+
case $1 in
12+
gen-ca)
13+
openssl req \
14+
-config "$SCRIPTDIR/combined.cnf" \
15+
-newkey rsa:4096 \
16+
-sha256 \
17+
-keyform PEM \
18+
-keyout "$2.key" \
19+
-nodes \
20+
-x509 \
21+
-days 3650 \
22+
-outform PEM \
23+
-out "$2.pem" \
24+
-subj "/C=GB/CN=$2-$(uuidgen)" \
25+
-extensions x509v3_CA
26+
;;
27+
gen-client)
28+
openssl genrsa \
29+
-out "$2.key" \
30+
2048
31+
openssl req \
32+
-new \
33+
-key "$2.key" \
34+
-out "$2.req" \
35+
-sha256 \
36+
-nodes \
37+
-subj "/C=GB/CN=$2"
38+
openssl x509 \
39+
-req \
40+
-in "$2.req" \
41+
-sha256 \
42+
-CA "$3.pem" \
43+
-CAkey "$3.key" \
44+
-set_serial 101 \
45+
-extensions client \
46+
-days 365 \
47+
-outform PEM \
48+
-out "$2.pem"
49+
;;
50+
51+
print)
52+
openssl x509 \
53+
-in "$2.pem" \
54+
-text
55+
;;
56+
57+
verify)
58+
openssl verify \
59+
-CAfile "$3.pem" \
60+
"$2.pem"
61+
;;
62+
63+
*) echo >&2 "Usage: $SCRIPTNAME (gen-ca NAME | gen-client NAME CANAME | print NAME | verify NAME CANAME)" && exit 1
64+
esac
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# default settings
2+
CERTPATHLEN = 1
3+
CERTUSAGE = digitalSignature,keyCertSign,cRLSign
4+
EXTCERTUSAGE = serverAuth,clientAuth
5+
CERTIP = 0.0.0.0
6+
CERTFQDN = nohost.nodomain
7+
8+
[ req ]
9+
default_bits = 4096
10+
default_md = sha256
11+
#default_keyfile = privkey.pem
12+
distinguished_name = req_distinguished_name
13+
attributes = req_attributes
14+
15+
[ req_distinguished_name ]
16+
countryName = Country Name (2 letter code)
17+
countryName_min = 2
18+
countryName_max = 2
19+
stateOrProvinceName = State or Province Name (full name)
20+
localityName = Locality Name (eg, city)
21+
0.organizationName = Organization Name (eg, company)
22+
organizationalUnitName = Organizational Unit Name (eg, section)
23+
commonName = Common Name (eg, fully qualified host name)
24+
commonName_max = 64
25+
emailAddress = Email Address
26+
emailAddress_max = 64
27+
28+
[ req_attributes ]
29+
challengePassword = A challenge password
30+
challengePassword_min = 4
31+
challengePassword_max = 20
32+
33+
# This section should be referenced when building an x509v3 CA
34+
# Certificate.
35+
# The default path length and the key usage can be overridden
36+
# modified by setting the CERTPATHLEN and CERTUSAGE environment
37+
# variables.
38+
[x509v3_CA]
39+
basicConstraints=critical,CA:true,pathlen:1
40+
keyUsage=critical,keyCertSign,digitalSignature,dataEncipherment,keyEncipherment,keyAgreement
41+
extendedKeyUsage=serverAuth,clientAuth
42+
#subjectKeyIdentifier=hash
43+
#authorityKeyIdentifier=hash
44+
45+
# This section should be referenced to add an IP Address
46+
# as an alternate subject name, needed by isakmpd
47+
# The address must be provided in the CERTIP environment variable
48+
[x509v3_IPAddr]
49+
subjectAltName=IP:$ENV::CERTIP
50+
extendedKeyUsage=$ENV::EXTCERTUSAGE
51+
52+
# This section should be referenced to add a FQDN hostname
53+
# as an alternate subject name, needed by isakmpd
54+
# The address must be provided in the CERTFQDN environment variable
55+
[x509v3_FQDN]
56+
subjectAltName=DNS:$ENV::CERTFQDN
57+
extendedKeyUsage=$ENV::EXTCERTUSAGE
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
5+
SCRIPTNAME=$(basename "$0")
6+
7+
AUTHORITY="test"
8+
ARCHIVIST_HOST="rkvst.poc.jitsuin.io"
9+
TLSARCHIVIST_HOST="auth.$ARCHIVIST_HOST"
10+
11+
# check that the required tools are installed
12+
type curl 2>/dev/null || ( echo >&2 "curl command not found, please install or add to PATH" && exit 1 )
13+
14+
usage() {
15+
cat >&2 <<EOF
16+
17+
Create and configure a TLS certificate authority and TLS client certificate for
18+
use with Jitsuin Archivist
19+
20+
Usage: $SCRIPTNAME [-a AUTHORITY] COMMON_NAME
21+
22+
-a AUTHORITY CN for certificate authority (default '$AUTHORITY')
23+
24+
Note that a uuid is appendede to the AUTHORITY when creating the CA
25+
certification Common Name as they must be unique
26+
27+
Creates a tarball (named [COMMON_NAME].tar.gz) containing the resulting
28+
client key and certificate
29+
30+
EOF
31+
exit 1
32+
}
33+
34+
while getopts ":a:" o; do
35+
case "${o}" in
36+
a) AUTHORITY="$OPTARG"
37+
;;
38+
*) usage
39+
;;
40+
esac
41+
done
42+
shift $((OPTIND-1))
43+
44+
# check args
45+
[ $# -eq 1 ] || ( echo >&2 "Must supply common name" && exit 1 )
46+
47+
COMMON_NAME="$1"
48+
shift
49+
50+
echo "Checking certficate authority..."
51+
[ -f "$AUTHORITY-ca.pem" ] || "$SCRIPTDIR"/certificateauth.sh gen-ca "$AUTHORITY-ca"
52+
53+
echo "Checking user cert..."
54+
[ -f "$COMMON_NAME-client.pem" ] || "$SCRIPTDIR"/certificateauth.sh gen-client "$COMMON_NAME-client" "$AUTHORITY-ca"
55+
56+
echo "Verifying that certs are coherent"
57+
"$SCRIPTDIR"/certificateauth.sh verify "$COMMON_NAME-client" "$AUTHORITY-ca"
58+
59+
echo "Add root cert ($AUTHORITY-ca.pem) to archivist and press any key to continue ..."
60+
read -r
61+
62+
echo "Testing client cert"
63+
64+
curl -fSs --cert "$COMMON_NAME-client.pem" --key "$COMMON_NAME-client.key" \
65+
-H "Content-Type: application/json" \
66+
https://$TLSARCHIVIST_HOST/archivist/v2/assets
67+
68+
echo
69+
echo
70+
71+
echo "Done - client key: $COMMON_NAME-client.key certificate: $COMMON_NAME-client.pem"

0 commit comments

Comments
 (0)