Skip to content

Commit c336b47

Browse files
committed
Add certification creation scripts
1 parent 4dd598f commit c336b47

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

test/certificates/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.crt
2+
*.key
3+
*.pem
4+
*.csr
5+
castuff

test/certificates/createca.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
set -e
2+
CA_KEY=ca.key
3+
CA_CRT=ca.crt
4+
SUBJ="/C=CH/ST=Bern/L=Bern/O=Example Company/CN=CA Domain1"
5+
6+
if [ ! -f $CA_KEY ]; then
7+
openssl genrsa -out $CA_KEY 4096
8+
fi
9+
10+
if [ ! -f $CA_CRT ]; then
11+
openssl req -new -x509 -key ${CA_KEY} -out ${CA_CRT} -subj "$SUBJ" -days 3000
12+
fi
13+

test/certificates/createcerts.sh

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
set -e
2+
3+
if [ $# -ne 3 ]
4+
then
5+
cat <<EOF
6+
Usage: $0 DNS_NAME IP (server_cert|usr_cert)
7+
8+
Example:
9+
$0 searchhead.example.local 192.0.2.2 server_cert
10+
EOF
11+
exit 1
12+
fi
13+
14+
HOSTNAME=$1
15+
IP=$2
16+
CRT_TYPE=$3
17+
18+
KEYOUT=$1.key
19+
CSR=$1.csr
20+
CERT=$1.crt
21+
COMBINED=$1.pem
22+
23+
# -config <(cat openssl.conf) \
24+
# <(printf "[SAN]\nsubjectAltName='DNS.1:${HOSTNAME},IP.1:${IP}'")
25+
26+
27+
openssl req \
28+
-newkey rsa:4096 \
29+
-nodes \
30+
-keyout ${KEYOUT} \
31+
-out ${CSR} \
32+
-config <(
33+
cat <<-EOF
34+
[req]
35+
prompt = no
36+
default_md = sha256
37+
req_extensions = req_ext
38+
distinguished_name = dn
39+
40+
[ dn ]
41+
C=CH
42+
O=Lab
43+
OU=IT
44+
CN=${HOSTNAME}
45+
46+
[ req_ext ]
47+
subjectAltName = @alt_names
48+
49+
[ alt_names ]
50+
DNS.1=${HOSTNAME}
51+
IP.1=${IP}
52+
EOF
53+
)
54+
55+
dir=./castuff
56+
mkdir -p $dir/newcerts
57+
mkdir -p $dir/crl
58+
mkdir -p $dir/certs
59+
[ ! -f $dir/serial ] && echo 1000 > $dir/serial
60+
[ ! -f $dir/index.txt ] && touch $dir/index.txt
61+
# SIGN Request
62+
openssl ca \
63+
-in ${CSR} \
64+
-out ${CERT} \
65+
-extensions ${CRT_TYPE} \
66+
-config <(
67+
cat <<-EOF
68+
[ ca ]
69+
default_ca = CA_default
70+
[ CA_default ]
71+
copy_extensions = copy
72+
dir = .
73+
certs = $dir/certs
74+
crl_dir = $dir/crl
75+
new_certs_dir = $dir/newcerts
76+
database = $dir/index.txt
77+
serial = $dir/serial
78+
RANDFILE = $dir/private/.rand
79+
80+
private_key = ./ca.key
81+
certificate = ./ca.crt
82+
83+
crlnumber = $dir/crlnumber
84+
crl = $dir/crl/ca.crl.pem
85+
crl_extensions = crl_ext
86+
default_crl_days = 30
87+
88+
# SHA-1 is deprecated, so use SHA-2 instead.
89+
default_md = sha256
90+
91+
name_opt = ca_default
92+
cert_opt = ca_default
93+
default_days = 375
94+
preserve = no
95+
policy = policy_loose
96+
unique_subject = no
97+
98+
[ policy_loose ]
99+
countryName = optional
100+
stateOrProvinceName = optional
101+
localityName = optional
102+
organizationName = optional
103+
organizationalUnitName = optional
104+
commonName = supplied
105+
emailAddress = optional
106+
107+
[ usr_cert ]
108+
basicConstraints = CA:FALSE
109+
nsCertType = client
110+
nsComment = "Client Certificate"
111+
subjectKeyIdentifier = hash
112+
authorityKeyIdentifier = keyid,issuer
113+
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
114+
extendedKeyUsage = clientAuth, emailProtection
115+
116+
[ server_cert ]
117+
basicConstraints = CA:FALSE
118+
nsCertType = server
119+
nsComment = "OpenSSL Generated Server Certificate"
120+
subjectKeyIdentifier = hash
121+
authorityKeyIdentifier = keyid,issuer:always
122+
keyUsage = critical, digitalSignature, keyEncipherment
123+
extendedKeyUsage = serverAuth
124+
125+
EOF
126+
)
127+
128+
cat ${CERT} ${KEYOUT} > ${COMBINED}
129+
#rm ${CERT} ${CSR} ${KEYOUT}

0 commit comments

Comments
 (0)