Skip to content

Commit 35c3673

Browse files
committed
bin: add support for libressl
1 parent c5b4714 commit 35c3673

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# openssl-osx-ca
1+
# openssl-osx-ca (and libressl-osx-ca)
22

33
A simple script intended to be run from `cron(1)` to sync an openssl style CA
44
pem with the certificates found in the OSX Keychain(s).
55

6+
The name is now a misnomer, as the software will manage certificate bundles for
7+
both openssl and libressl installed under Homebrew.
8+
69
The keychains exported to the CA bundle by default are:
710
* System.keychain
811
* SystemRootCertificates.keychain

bin/openssl-osx-ca

100644100755
Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
usage() {
4+
echo "$(basename $0) [--skip-login-keychain] [--skip-system-keychain] [-h|--help] [\`which brew\`]"
5+
}
6+
37
skip_system_keychain=false
48
skip_login_keychain=false
59

@@ -11,9 +15,17 @@ while [ ! $# -eq 0 ]; do
1115
--skip-system-keychain)
1216
skip_system_keychain=true
1317
;;
18+
-h|--help)
19+
usage
20+
exit 1
21+
;;
1422
*brew)
1523
brew=$1
1624
;;
25+
*)
26+
echo "Unknown argument: $1" >&2
27+
exit 1
28+
;;
1729
esac
1830
shift
1931
done
@@ -24,53 +36,79 @@ fi
2436

2537
if [[ ! -x "${brew}" ]]; then
2638
echo "Homebrew not in PATH or given arguments, cannot continue"
39+
usage
2740
exit 1
2841
fi
2942

30-
openssl=$($brew list openssl | grep bin/openssl | head -n 1)
43+
exitcode=0
44+
err() {
45+
if [[ -d $1 ]]; then
46+
rm -r "${1}"
47+
fi
3148

32-
[[ "${openssl}" = "" ]] && echo "Homebrew openssl not found" && exit 1
49+
exitcode=$(($exitcode + 1))
50+
}
3351

34-
c_rehash=$($brew list openssl | grep bin/c_rehash | head -n 1)
52+
genbundle() {
53+
local sslimpl=$1
3554

36-
[[ "${c_rehash}" = "" ]] && echo "Homebrew c_rehash (openssl) not found" && exit 1
55+
local list=$($brew list $sslimpl 2>/dev/null)
56+
[[ -z $list ]] && continue
3757

38-
openssldir=$($openssl version -d | cut -d '"' -f 2)
58+
local openssl=$(echo "$list" | grep bin/openssl | head -n 1)
3959

40-
[[ "${openssldir}" = "" ]] && echo "openssl directory not found" && exit 1
60+
[[ "${openssl}" = "" ]] && echo "Homebrew $sslimpl not found" && err && return 1
4161

42-
tmpdir=$(/usr/bin/mktemp -d -t openssl_osx_ca)
62+
local openssldir=$($openssl version -d | cut -d '"' -f 2)
4363

44-
[[ "${tmpdir}" = "" ]] && echo "mktemp failed" && exit 1
64+
[[ "${openssldir}" = "" ]] && echo "$sslimpl directory not found" && err && return 1
4565

46-
certs="${tmpdir}/cert.pem"
47-
if ! $skip_system_keychain ; then
48-
security find-certificate -a -p /Library/Keychains/System.keychain > $certs
49-
fi
50-
security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> $certs
51-
if [[ -f ~/Library/Keychains/login.keychain ]] && ! $skip_login_keychain ; then
52-
security find-certificate -a -p ~/Library/Keychains/login.keychain >> $certs
53-
fi
66+
local tmpdir=$(/usr/bin/mktemp -d -t openssl_osx_ca)
5467

55-
d1=$($openssl md5 ${openssldir}/cert.pem | awk '{print $2}')
56-
d2=$($openssl md5 ${tmpdir}/cert.pem | awk '{print $2}')
68+
[[ "${tmpdir}" = "" ]] && echo "mktemp failed" && err "${tmpdir}" && return 1
5769

58-
if [[ "${d1}" = "${d2}" ]]; then
59-
logger -t "$(basename $0)" "${openssldir}/cert.pem up to date"
60-
else
61-
if ! $c_rehash -- $tmpdir > /dev/null; then
62-
logger -t "$(basename $0)" "${openssldir}/cert.pem updated failed, see cron"
70+
local certs="${tmpdir}/cert.pem"
71+
if ! $skip_system_keychain ; then
72+
security find-certificate -a -p /Library/Keychains/System.keychain > $certs
73+
fi
74+
security find-certificate -a -p /System/Library/Keychains/SystemRootCertificates.keychain >> $certs
75+
if [[ -f ~/Library/Keychains/login.keychain ]] && ! $skip_login_keychain ; then
76+
security find-certificate -a -p ~/Library/Keychains/login.keychain >> $certs
77+
fi
6378

64-
echo "rehash failed to verify, something is wrong"
65-
echo "check ${tmpdir}/cert.pem for problems"
66-
exit 1
79+
d1=$($openssl md5 ${openssldir}/cert.pem | awk '{print $2}')
80+
d2=$($openssl md5 ${tmpdir}/cert.pem | awk '{print $2}')
81+
82+
if [[ "${d1}" = "${d2}" ]]; then
83+
logger -t "$(basename $0)" "${openssldir}/cert.pem up to date"
84+
else
85+
86+
# Note: there's no c_rehash bundled with libressl, the bundle is still
87+
# produced, so carry on as normal and just produce a bundle in the standard
88+
# location.
89+
local c_rehash=$(echo "$list" | grep bin/c_rehash | head -n 1)
90+
if [[ -n $c_rehash ]]; then
91+
if ! $c_rehash -- $tmpdir > /dev/null; then
92+
logger -t "$(basename $0)" "${openssldir}/cert.pem updated failed, see cron"
93+
94+
echo "rehash failed to verify, something is wrong"
95+
echo "check ${tmpdir}/cert.pem for problems"
96+
err "${tmpdir}" && return 1
97+
fi
98+
fi
99+
100+
# XXX: I don't think this is atomic on OSX, but it's as close as we're going to
101+
# get without a lot more work.
102+
mv -f ${tmpdir}/* ${openssldir}/
103+
104+
logger -t "$(basename $0)" "${openssldir}/cert.pem updated"
67105
fi
68106

69-
# XXX: I don't think this is atomic on OSX, but it's as close as we're going to
70-
# get without a lot more work.
71-
mv -f ${tmpdir}/* ${openssldir}/
107+
rm -r "${tmpdir}"
108+
}
72109

73-
logger -t "$(basename $0)" "${openssldir}/cert.pem updated"
74-
fi
110+
for sslimpl in openssl libressl; do
111+
genbundle $sslimpl
112+
done
75113

76-
rm -r "${tmpdir}"
114+
exit $exitcode

0 commit comments

Comments
 (0)