-
-
Save shaunfink/c49ff85c1b94018d22c67a161fe85022 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This Works is placed under the terms of the Copyright Less License, | |
# see file COPYRIGHT.CLL. USE AT OWN RISK, ABSOLUTELY NO WARRANTY. | |
# | |
# COPYRIGHT.CLL can be found at http://permalink.de/tino/cll | |
# (CLL is CC0 as long as not covered by any Copyright) | |
OOPS() { echo "OOPS: $*" >&2; exit 23; } | |
[ -z "`pidof openssl`" ] || OOPS "openssl running, consider: killall openssl" | |
PID= | |
kick() { [ -n "$PID" ] && kill "$PID" && sleep .2; PID=; } | |
trap 'kick' 0 | |
serve() | |
{ | |
kick | |
PID= | |
openssl s_server -key "$KEY" -cert "$CRT" "$@" -www & | |
PID=$! | |
sleep .5 # give it time to startup | |
} | |
check() | |
{ | |
while read -r line | |
do | |
case "$line" in | |
'Verify return code: 0 (ok)') return 0;; | |
'Verify return code: '*) return 1;; | |
# *) echo "::: $line :::";; | |
esac | |
done < <(echo | openssl s_client -verify 8 -CApath /etc/ssl/certs/) | |
OOPS "Something failed, verification output not found!" | |
return 2 | |
} | |
ARG="${1%.}" | |
KEY="$ARG.key" | |
CRT="$ARG.crt" | |
BND="$ARG.bundle" | |
for a in "$KEY" "$CRT" "$BND" | |
do | |
[ -s "$a" ] || OOPS "missing $a" | |
done | |
serve | |
check && echo "!!! =========> CA-Bundle is not needed! <========" | |
echo | |
serve -CAfile "$BND" | |
check | |
ret=$? | |
kick | |
echo | |
case $ret in | |
0) echo "EVERYTHING OK" | |
echo "SSLCertificateKeyFile $KEY" | |
echo "SSLCertificateFile $CRT" | |
echo "SSLCACertificateFile $BND" | |
;; | |
*) echo "!!! =========> something is wrong, verification failed! <======== ($ret)";; | |
esac | |
exit $ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites of this script:
-- DIR/certificate.crt which contains the certificate
-- DIR/certificate.key which contains the secret key for your webservice (without passphrase)
-- DIR/certificate.bundle which contains the CA-Bundle. On how to prepare the bundle, see below.
How to create the certificate.bundle file?
In the WWW the trust chain usually looks like this:
Now, the evaluation takes place from bottom to top, this means, first, your certificate is read, then the unknown intermediate certificate is needed, then perhaps the cross-signing-certificate and then /etc/ssl/certs is consulted to find the proper trusted certificate.
The ca-bundle must be made up in excactly the right processing order, this means, the first needed certificate (the intermediate certificate which signs your certificate) comes first in the bundle. Then the cross-signing-cert is needed.
Usually your CA (the authority who signed your certificate) will provide such a proper ca-bundle-file already. If not, you need to pick all the needed intermediate certificates and cat them together into a single file (on Unix). On Windows you can just open a text editor (like notepad.exe) and paste the certificates into the file, the first needed on top and following the others.
There is another thing. The files need to be in PEM format. Some CAs issue DER (a binary) format. PEM is easy to spot: It is ASCII readable. For mor on how to convert something into PEM, see How to convert .crt to .pem and follow the yellow brick road.
Example:
-- intermediate2.crt the intermediate cert which signed your certificate.crt
-- intermediate1.crt another intermediate cert, which singed intermediate2.crt
-- crossigned.crt which is a cross signing certificate from another CA, which signed intermediate1.crt
-- crossintermediate.crt which is another intermediate from the other CA which signed crossigned.crt (you probably will never ever see such a thing)
Then the proper cat would look like this:
And how can you find out which files are needed or not and in which sequence?
Well, experiment, until the check tells you everything is OK. It is like a computer puzzle game to solve the riddle. Every. Single. Time. Even for pros. But you will get better each time you need to do this. So you are definitively not alone with all that pain. It's SSL, ya' know? SSL is probably one of the worst designs I ever saw in over 30 years of professional system administration. Ever wondered why crypto has not become mainstream in the last 30 years? That's why. 'nuff said.