| 1 |
#!/bin/bash -e |
| 2 |
# This is a mockup of a script to produce a snakeoil cert |
| 3 |
# The aim is to have a debconfisable ssl-certificate script |
| 4 |
|
| 5 |
# shellcheck source=/dev/null disable=SC1091 |
| 6 |
. /usr/share/debconf/confmodule |
| 7 |
db_version 2.0 |
| 8 |
db_capb backup |
| 9 |
|
| 10 |
progname=$(basename "${0}") |
| 11 |
|
| 12 |
usage() { |
| 13 |
cat <<EOF |
| 14 |
Usage: ${progname} [options] <template> <output> |
| 15 |
${progname} [options] generate-default-snakeoil |
| 16 |
|
| 17 |
Options: |
| 18 |
-h,--help This usage text |
| 19 |
-f,--force-overwrite Overwrite existing .pem and .key files |
| 20 |
-n,--no-overwrite Never overwrite existing snakeoil .pem and |
| 21 |
.key files, even if algorithms are out of date |
| 22 |
-x,--expiration-days <N> Override default expiration time |
| 23 |
EOF |
| 24 |
exit "${1}"; |
| 25 |
} |
| 26 |
|
| 27 |
ask_via_debconf() { |
| 28 |
RET="" |
| 29 |
if db_settitle make-ssl-cert/title ; then |
| 30 |
: # OK |
| 31 |
else |
| 32 |
echo "Debconf failed with error code $? $RET" >&2 |
| 33 |
echo "Maybe your debconf database is corrupt." >&2 |
| 34 |
echo "Try re-installing ssl-cert." >&2 |
| 35 |
fi |
| 36 |
|
| 37 |
RET="" |
| 38 |
while [ "x$RET" = "x" ]; do |
| 39 |
db_fset make-ssl-cert/hostname seen false |
| 40 |
db_input high make-ssl-cert/hostname || true |
| 41 |
db_go |
| 42 |
db_get make-ssl-cert/hostname |
| 43 |
done |
| 44 |
|
| 45 |
db_get make-ssl-cert/hostname |
| 46 |
HostName="$RET" |
| 47 |
db_fset make-ssl-cert/hostname seen false |
| 48 |
|
| 49 |
db_fset make-ssl-cert/altname seen false |
| 50 |
db_input high make-ssl-cert/altname || true |
| 51 |
db_go |
| 52 |
db_get make-ssl-cert/altname |
| 53 |
AddAltName="$RET" |
| 54 |
db_fset make-ssl-cert/altname seen false |
| 55 |
SubjectAltName="DNS:$HostName" |
| 56 |
[ -z "$AddAltName" ] || SubjectAltName="$SubjectAltName,$AddAltName" |
| 57 |
} |
| 58 |
|
| 59 |
make_snakeoil() { |
| 60 |
if ! HostName="$(hostname -f)" ; then |
| 61 |
HostName="$(hostname)" |
| 62 |
echo "make-ssl-cert: Could not get FQDN, using '$HostName'". |
| 63 |
echo "make-ssl-cert: You may want to fix your /etc/hosts and/or DNS setup and run" |
| 64 |
echo "make-ssl-cert: 'make-ssl-cert generate-default-snakeoil --force-overwrite'" |
| 65 |
echo "make-ssl-cert: again." |
| 66 |
fi |
| 67 |
SubjectAltName="DNS:$HostName" |
| 68 |
if [ ${#HostName} -gt 64 ] ; then |
| 69 |
# The certificate's common name cannot be longer than 64 chars. |
| 70 |
# Use the short name instead. |
| 71 |
HostName="$(hostname)" |
| 72 |
fi |
| 73 |
} |
| 74 |
|
| 75 |
create_temporary_cnf() { |
| 76 |
sed -e s#@HostName@#"$HostName"# -e s#@SubjectAltName@#"$SubjectAltName"# "${template}" > "${TMPFILE}" |
| 77 |
} |
| 78 |
|
| 79 |
create_hash_link() { |
| 80 |
local file="$1" |
| 81 |
local cryptfile filename i |
| 82 |
filename=$(basename "$file") |
| 83 |
cryptfile=$(dirname "$file")/$(openssl x509 -hash -noout -in "$file") |
| 84 |
i=0 |
| 85 |
while [ -L "${cryptfile}.$i" ] ; do |
| 86 |
if [ "$(readlink "${cryptfile}.$i")" = "$filename" ] ; then |
| 87 |
return 0 |
| 88 |
fi |
| 89 |
i=$(( i + 1 )) |
| 90 |
done |
| 91 |
ln -sf "$filename" "${cryptfile}.$i" |
| 92 |
} |
| 93 |
|
| 94 |
check_min_algo() { |
| 95 |
local file="$1" |
| 96 |
local bits |
| 97 |
if ! openssl x509 -text -in "$file" | grep -q 'Signature Algorithm:.*sha256' ; then |
| 98 |
echo "Signature algorithm of $file is not sha256. Recreating." >&2 |
| 99 |
return 1 |
| 100 |
fi |
| 101 |
bits=$(openssl x509 -text -in "$file" |grep -o "Public-Key: \\(.*\\)") |
| 102 |
# value: "Public-Key: (2048 bit)" |
| 103 |
bits="${bits##*\(}" |
| 104 |
bits="${bits% bit\)}" |
| 105 |
case "$bits" in |
| 106 |
[0-9][0-9][0-9][0-9]) |
| 107 |
;; |
| 108 |
*) |
| 109 |
echo "WARNING: Cannot determine RSA key length" >&2 |
| 110 |
return 0 |
| 111 |
;; |
| 112 |
esac |
| 113 |
if [ "$bits" -lt 2048 ] ; then |
| 114 |
echo "RSA key length of $file is $bits. Recreating with 2048 bits" >&2 |
| 115 |
return 1 |
| 116 |
fi |
| 117 |
return 0 |
| 118 |
} |
| 119 |
|
| 120 |
# Process arguments |
| 121 |
subcommand= |
| 122 |
template= |
| 123 |
opt_force_overwrite="false" |
| 124 |
opt_no_overwrite="false" |
| 125 |
opt_expiration_days="3650" |
| 126 |
|
| 127 |
# Transform long options to short ones |
| 128 |
newargs=() |
| 129 |
for arg in "${@}"; do |
| 130 |
case "${arg}" in |
| 131 |
--help) newargs+=(-h) ;; |
| 132 |
--force-overwrite) |
| 133 |
# Move to front so that we accept --force-overwrite at the end, for |
| 134 |
# compatibility with 1.0.x. |
| 135 |
newargs=("-f" "${newargs[@]}") ;; |
| 136 |
--no-overwrite) newargs+=(-n) ;; |
| 137 |
--expiration-days) newargs+=(-x) ;; |
| 138 |
--*) |
| 139 |
printf "Unrecognized option %s\n\n" "${arg}" |
| 140 |
usage 1 |
| 141 |
;; |
| 142 |
*) newargs+=("$arg") ;; |
| 143 |
esac |
| 144 |
done |
| 145 |
set -- "${newargs[@]}" |
| 146 |
|
| 147 |
# Parse short options |
| 148 |
while getopts "hfnx:" opt "${@}"; do |
| 149 |
case "${opt}" in |
| 150 |
h) usage 0 ;; |
| 151 |
f) opt_force_overwrite="true" ;; |
| 152 |
n) opt_no_overwrite="true" ;; |
| 153 |
x) opt_expiration_days="${OPTARG}" ;; |
| 154 |
*) |
| 155 |
printf "Unrecognized option %s\n\n" "-${opt}" |
| 156 |
usage 1 |
| 157 |
;; |
| 158 |
esac |
| 159 |
done |
| 160 |
shift "$((OPTIND - 1))" |
| 161 |
|
| 162 |
if $opt_force_overwrite && $opt_no_overwrite ; then |
| 163 |
usage 1 |
| 164 |
fi |
| 165 |
|
| 166 |
# Parse subcommand |
| 167 |
if [ "${1}" = "generate-default-snakeoil" ]; then |
| 168 |
subcommand="${1}" |
| 169 |
else |
| 170 |
subcommand="manual" |
| 171 |
template="${1}" |
| 172 |
fi |
| 173 |
|
| 174 |
# Takes two arguments, the base layout and the output cert. |
| 175 |
if [ "${subcommand}" = "manual" ]; then |
| 176 |
output="${2}" |
| 177 |
[ -n "${template}" ] || usage 1 |
| 178 |
[ -n "${output}" ] || usage 1 |
| 179 |
|
| 180 |
# be anal in manual mode. |
| 181 |
if [ ! -f "${template}" ]; then |
| 182 |
printf "Could not open template file: %s!\n" "${template}"; |
| 183 |
exit 1; |
| 184 |
fi |
| 185 |
if [ -f "${output}" ] && [ "${opt_force_overwrite}" != "true" ]; then |
| 186 |
printf "%s file already exists!\n" "${output}"; |
| 187 |
exit 1; |
| 188 |
fi |
| 189 |
ask_via_debconf |
| 190 |
elif [ "${subcommand}" = "generate-default-snakeoil" ]; then |
| 191 |
template="/usr/share/ssl-cert/ssleay.cnf" |
| 192 |
if [ -f "/etc/ssl/certs/ssl-cert-snakeoil.pem" ] && [ -f "/etc/ssl/private/ssl-cert-snakeoil.key" ]; then |
| 193 |
if "${opt_no_overwrite}" ; then |
| 194 |
exit 0 |
| 195 |
fi |
| 196 |
if ! "${opt_force_overwrite}" ; then |
| 197 |
if check_min_algo "/etc/ssl/certs/ssl-cert-snakeoil.pem" ; then |
| 198 |
exit 0 |
| 199 |
fi |
| 200 |
fi |
| 201 |
fi |
| 202 |
make_snakeoil |
| 203 |
else |
| 204 |
usage 1 |
| 205 |
fi |
| 206 |
|
| 207 |
# # should be a less common char |
| 208 |
# problem is that openssl virtually accepts everything and we need to |
| 209 |
# sacrifice one char. |
| 210 |
|
| 211 |
TMPFILE="$(mktemp)" || exit 1 |
| 212 |
TMPOUT="$(mktemp)" || exit 1 |
| 213 |
|
| 214 |
trap 'rm -f ${TMPFILE} ${TMPOUT}' EXIT |
| 215 |
|
| 216 |
create_temporary_cnf |
| 217 |
|
| 218 |
# create the certificate. |
| 219 |
|
| 220 |
umask 077 |
| 221 |
|
| 222 |
if [ "${subcommand}" = "manual" ]; then |
| 223 |
if ! openssl req -config "${TMPFILE}" -new -x509 -days "${opt_expiration_days}" -nodes -sha256 \ |
| 224 |
-out "${output}" -keyout "${output}" > "${TMPOUT}" 2>&1 |
| 225 |
then |
| 226 |
echo "Could not create certificate. Openssl output was:" >&2 |
| 227 |
cat "${TMPOUT}" >&2 |
| 228 |
exit 1 |
| 229 |
fi |
| 230 |
chmod 600 "${output}" |
| 231 |
create_hash_link "${output}" |
| 232 |
elif [ "${subcommand}" = "generate-default-snakeoil" ]; then |
| 233 |
if ! openssl req -config "${TMPFILE}" -new -x509 -days "${opt_expiration_days}" -nodes -sha256 \ |
| 234 |
-out /etc/ssl/certs/ssl-cert-snakeoil.pem \ |
| 235 |
-keyout /etc/ssl/private/ssl-cert-snakeoil.key > "${TMPOUT}" 2>&1 |
| 236 |
then |
| 237 |
echo "Could not create certificate. Openssl output was:" >&2 |
| 238 |
cat "${TMPOUT}" >&2 |
| 239 |
exit 1 |
| 240 |
fi |
| 241 |
chmod 644 /etc/ssl/certs/ssl-cert-snakeoil.pem |
| 242 |
chmod 640 /etc/ssl/private/ssl-cert-snakeoil.key |
| 243 |
chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key |
| 244 |
create_hash_link /etc/ssl/certs/ssl-cert-snakeoil.pem |
| 245 |
else |
| 246 |
usage 1 |
| 247 |
fi |