Skip to content

Commit f3c8c70

Browse files
committed
[generate-cert-sh] Issue #432 Add support for wildcard host names in certificates
1 parent 2e957b2 commit f3c8c70

File tree

2 files changed

+52
-26
lines changed

2 files changed

+52
-26
lines changed

demo-keys/generate_cert.sh

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ if [ "$1" = "" ]; then
1515
fi
1616
export host=$1
1717
export mode=$2
18+
export wildcard=
19+
export fileprefix=
20+
if [ "$mode" = "wildcard" ]; then
21+
if [ "$host" = "localhost" ]; then
22+
wildcard=
23+
for i in `seq 1 32`; do {
24+
wildcard=${wildcard},DNS:$i.localhost
25+
} done;
26+
else
27+
wildcard=,DNS:*.${host};
28+
fi
29+
fileprefix=wildcard.
30+
fi
1831
which openssl
1932
if [ "$?" = "1" ]; then
2033
echo Please install openssl command
@@ -39,11 +52,11 @@ if [ ! -e demoCA.crt ]; then
3952
echo cd demo-keys
4053
echo certutil -d sql:$HOME/.pki/nssdb -A -n 'thin-hook demo CA' -i ./demoCA/demoCA.crt -t TCP,TCP,TCP
4154
fi
42-
if [ ! -e ${host}.key ]; then
43-
openssl genrsa 2048 >${host}.key
55+
if [ ! -e ${fileprefix}${host}.key ]; then
56+
openssl genrsa 2048 >${fileprefix}${host}.key
4457
fi
45-
if [ ! -e ${host}.csr ]; then
46-
cat > ${host}_csr.txt <<-EOF
58+
if [ ! -e ${fileprefix}${host}.csr ]; then
59+
cat > ${fileprefix}${host}_csr.txt <<-EOF
4760
[req]
4861
default_bits = 2048
4962
prompt = no
@@ -59,22 +72,22 @@ OU=demo
5972
CN=${host}
6073
6174
[SAN]
62-
subjectAltName=DNS:${host}
75+
subjectAltName=DNS:${host}${wildcard}
6376
EOF
64-
openssl req -config ${host}_csr.txt -new -sha256 -key ${host}.key -out ${host}.csr
65-
openssl req -text -noout -in ${host}.csr
77+
openssl req -config ${fileprefix}${host}_csr.txt -new -sha256 -key ${fileprefix}${host}.key -out ${fileprefix}${host}.csr
78+
openssl req -text -noout -in ${fileprefix}${host}.csr
6679
fi
6780
cd ..
68-
if [ ! -e demoCA/${host}.crt ]; then
69-
openssl x509 -req -CA demoCA/demoCA.crt -CAkey demoCA/demoCA.key -CAcreateserial -out demoCA/${host}.crt -in demoCA/${host}.csr -sha256 -days 3650 \
70-
-extfile demoCA/${host}_csr.txt -extensions SAN
81+
if [ ! -e demoCA/${fileprefix}${host}.crt ]; then
82+
openssl x509 -req -CA demoCA/demoCA.crt -CAkey demoCA/demoCA.key -CAcreateserial -out demoCA/${fileprefix}${host}.crt -in demoCA/${fileprefix}${host}.csr -sha256 -days 3650 \
83+
-extfile demoCA/${fileprefix}${host}_csr.txt -extensions SAN
7184
fi
7285
if [ "$mode" = "client" ]; then
73-
if [ ! -e demoCA/${host}.pfx ]; then
86+
if [ ! -e demoCA/${fileprefix}${host}.pfx ]; then
7487
echo Note: Some browsers may not accept client certificates with empty passwords
75-
openssl pkcs12 -export -inkey demoCA/${host}.key -in demoCA/${host}.crt -out demoCA/${host}.pfx
88+
openssl pkcs12 -export -inkey demoCA/${fileprefix}${host}.key -in demoCA/${fileprefix}${host}.crt -out demoCA/${fileprefix}${host}.pfx
7689
echo how to import pfx on Linux:
7790
echo cd demo-keys
78-
echo pk12util -d sql:$HOME/.pki/nssdb -i ./demoCA/${host}.pfx
91+
echo pk12util -d sql:$HOME/.pki/nssdb -i ./demoCA/${fileprefix}${host}.pfx
7992
fi
8093
fi

plugins/generate-cert-sh/generate_cert.sh

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ if [ "$1" = "" ]; then
1515
fi
1616
export host=$1
1717
export mode=$2
18+
export wildcard=
19+
export fileprefix=
20+
if [ "$mode" = "wildcard" ]; then
21+
if [ "$host" = "localhost" ]; then
22+
wildcard=
23+
for i in `seq 1 32`; do {
24+
wildcard=${wildcard},DNS:$i.localhost
25+
} done;
26+
else
27+
wildcard=,DNS:*.${host};
28+
fi
29+
fileprefix=wildcard.
30+
fi
1831
which openssl
1932
if [ "$?" = "1" ]; then
2033
echo Please install openssl command
@@ -39,11 +52,11 @@ if [ ! -e /* @echo demoCA */.crt ]; then
3952
echo cd /* @echo keys */
4053
echo certutil -d sql:$HOME/.pki/nssdb -A -n '/* @echo CN */' -i .//* @echo demoCA *///* @echo demoCA */.crt -t TCP,TCP,TCP
4154
fi
42-
if [ ! -e ${host}.key ]; then
43-
openssl genrsa 2048 >${host}.key
55+
if [ ! -e ${fileprefix}${host}.key ]; then
56+
openssl genrsa 2048 >${fileprefix}${host}.key
4457
fi
45-
if [ ! -e ${host}.csr ]; then
46-
cat > ${host}_csr.txt <<-EOF
58+
if [ ! -e ${fileprefix}${host}.csr ]; then
59+
cat > ${fileprefix}${host}_csr.txt <<-EOF
4760
[req]
4861
default_bits = 2048
4962
prompt = no
@@ -59,22 +72,22 @@ OU=/* @echo OU */
5972
CN=${host}
6073
6174
[SAN]
62-
subjectAltName=DNS:${host}
75+
subjectAltName=DNS:${host}${wildcard}
6376
EOF
64-
openssl req -config ${host}_csr.txt -new -sha256 -key ${host}.key -out ${host}.csr
65-
openssl req -text -noout -in ${host}.csr
77+
openssl req -config ${fileprefix}${host}_csr.txt -new -sha256 -key ${fileprefix}${host}.key -out ${fileprefix}${host}.csr
78+
openssl req -text -noout -in ${fileprefix}${host}.csr
6679
fi
6780
cd ..
68-
if [ ! -e demoCA/${host}.crt ]; then
69-
openssl x509 -req -CA /* @echo demoCA *///* @echo demoCA */.crt -CAkey /* @echo demoCA *///* @echo demoCA */.key -CAcreateserial -out /* @echo demoCA *//${host}.crt -in /* @echo demoCA *//${host}.csr -sha256 -days 3650 \
70-
-extfile /* @echo demoCA *//${host}_csr.txt -extensions SAN
81+
if [ ! -e demoCA/${fileprefix}${host}.crt ]; then
82+
openssl x509 -req -CA /* @echo demoCA *///* @echo demoCA */.crt -CAkey /* @echo demoCA *///* @echo demoCA */.key -CAcreateserial -out /* @echo demoCA *//${fileprefix}${host}.crt -in /* @echo demoCA *//${fileprefix}${host}.csr -sha256 -days 3650 \
83+
-extfile /* @echo demoCA *//${fileprefix}${host}_csr.txt -extensions SAN
7184
fi
7285
if [ "$mode" = "client" ]; then
73-
if [ ! -e /* @echo demoCA *//${host}.pfx ]; then
86+
if [ ! -e /* @echo demoCA *//${fileprefix}${host}.pfx ]; then
7487
echo Note: Some browsers may not accept client certificates with empty passwords
75-
openssl pkcs12 -export -inkey /* @echo demoCA *//${host}.key -in /* @echo demoCA *//${host}.crt -out /* @echo demoCA *//${host}.pfx
88+
openssl pkcs12 -export -inkey /* @echo demoCA *//${fileprefix}${host}.key -in /* @echo demoCA *//${fileprefix}${host}.crt -out /* @echo demoCA *//${fileprefix}${host}.pfx
7689
echo how to import pfx on Linux:
7790
echo cd /* @echo keys */
78-
echo pk12util -d sql:$HOME/.pki/nssdb -i .//* @echo demoCA *//${host}.pfx
91+
echo pk12util -d sql:$HOME/.pki/nssdb -i .//* @echo demoCA *//${fileprefix}${host}.pfx
7992
fi
8093
fi

0 commit comments

Comments
 (0)