auto
  W   ˆl–Ü4ý('ÜZÔâ€vã,ÜÅ£“þDË+8Ù’Gš�@l+MìÕÿ?Â{‹„„-i[D<†ªoZƒ›Ù«Ã‚d _‹I|¥Žƒî4ÃVŸÃÂ 6    ‹      mRËNÃ0¼û+F=‘J$¢âDU„BpàV‰ó6ÙÐP?"? ¥ê¿ã8MÚJ½�=3;ë]7ª5Öc�Švî]2op@m�Â$/J£�'í]þí&s!ŠéT`Š'MŠñµni^¢1]–äùËØž�þaë£ñÆ²�0ñ.(E#í{?¼IÑy”$UÅ®´Më£»;b‚jÅ¦>	u�òŒ¹¨Ô’%ußb¹æk%VŒ²�àj0ZöÁjwÕ2ŠÑè7˜xKª•Üá¢Ûžm�>¾³ãÓTaÙé±8ŸãÍ,ë,qwa!x›T]v3¹P'œa/€¾g|�_çÞFqOÆb§Åfsqˆ;,ðJR®¨Ü 6ÊTA’mþº7¥ßà†ÔŠkêš<�‹3�}3       ˆÆ�þB gÚp±·$�¹üÿÅƒh¹ÄÃÂ      auto
  5   ˆl–äY>” ”¾R( 	Å‚Á6¦-ÿ�þB gÚ [u÷¥�ùÆƒh¹ÅÄÃ      auto
  5   	ˆl–ß=¿J2/”Š
�rãb¸Û*bÑ¿�þB7È³r4Ü¥Û-ùÇƒqà‹ÆÅÄ œ    	#!/bin/bash -e
# This is a mockup of a script to produce a snakeoil cert
# The aim is to have a debconfisable ssl-certificate script

# shellcheck source=/dev/null disable=SC1091
. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup

progname=$(basename "${0}")

usage() {
    cat <<EOF
Usage: ${progname} [options] <template> <output>
       ${progname} [options] generate-default-snakeoil

Options:
       -h,--help                 This usage text
       -f,--force-overwrite      Overwrite existing .pem and .key files
       -n,--no-overwrite         Never overwrite existing snakeoil .pem and
                                 .key files, even if algorithms are out of date
       -x,--expiration-days <N>  Override default expiration time
EOF
    exit "${1}";
}

ask_via_debconf() {
    RET=""
    if db_settitle make-ssl-cert/title ; then
	: # OK
    else
	echo "Debconf failed with error code $? $RET" >&2
	echo "Maybe your debconf database is corrupt." >&2
	echo "Try re-installing ssl-cert." >&2
    fi

    RET=""
    while [ "x$RET" = "x" ]; do
	db_fset make-ssl-cert/hostname seen false
	db_input high make-ssl-cert/hostname || true
	db_go
	db_get make-ssl-cert/hostname
    done

    db_get make-ssl-cert/hostname
    HostName="$RET"
    db_fset make-ssl-cert/hostname seen false

    db_fset make-ssl-cert/altname seen false
    db_input high make-ssl-cert/altname || true
    db_go
    db_get make-ssl-cert/altname
    AddAltName="$RET"
    db_fset make-ssl-cert/altname seen false
    SubjectAltName="DNS:$HostName"
    [ -z "$AddAltName" ] || SubjectAltName="$SubjectAltName,$AddAltName"
}

make_snakeoil() {
    if ! HostName="$(hostname -f)" ; then
	HostName="$(hostname)"
	echo "make-ssl-cert: Could not get FQDN, using '$HostName'".
	echo "make-ssl-cert: You may want to fix your /etc/hosts and/or DNS setup and run"
	echo "make-ssl-cert: 'make-ssl-cert generate-default-snakeoil --force-overwrite'"
	echo "make-ssl-cert: again."
    fi
    SubjectAltName="DNS:$HostName"
    if [ ${#HostName} -gt 64 ] ; then
	# The certificate's common name cannot be longer than 64 chars.
	# Use the short name instead.
	HostName="$(hostname)"
    fi
}

create_temporary_cnf() {
    sed -e s#@HostName@#"$HostName"# -e s#@SubjectAltName@#"$SubjectAltName"# "${template}" > "${TMPFILE}"
}

create_hash_link() {
    local file="$1"
    local cryptfile filename i
    filename=$(basename "$file")
    cryptfile=$(dirname "$file")/$(openssl x509 -hash -noout -in "$file")
    i=0
    while [ -L "${cryptfile}.$i" ] ; do
	if [ "$(readlink "${cryptfile}.$i")" = "$filename" ] ; then
	    return 0
	fi
	i=$(( i + 1 ))
    done
    ln -sf "$filename" "${cryptfile}.$i"
}

check_min_algo() {
    local file="$1"
    local bits
    if ! openssl x509 -text -in "$file" | grep -q 'Signature Algorithm:.*sha256' ; then
	echo "Signature algorithm of $file is not sha256. Recreating." >&2
	return 1
    fi
    bits=$(openssl x509 -text -in "$file" |grep -o "Public-Key: \\(.*\\)")
    # value: "Public-Key: (2048 bit)"
    bits="${bits##*\(}"
    bits="${bits% bit\)}"
    case "$bits" in
	[0-9][0-9][0-9][0-9])
	    ;;
	*)
	    echo "WARNING: Cannot determine RSA key length" >&2
	    return 0
	    ;;
    esac
    if [ "$bits" -lt 2048 ] ; then
	echo "RSA key length of $file is $bits. Recreating with 2048 bits" >&2
	return 1
    fi
    return 0
}

# Process arguments
subcommand=
template=
opt_force_overwrite="false"
opt_no_overwrite="false"
opt_expiration_days="3650"

