File genkey.sh of Package netqmail
#!/bin/sh
# (C) 2011 Peter Conrad <conrad@quisquis.de>
#
# This file is licensed under the terms of the
# GNU General Public License Version 2. A copy of these terms should be
# enclosed as "gpl-2.0.txt" in the package containing this file.
if [ "$1" = "-h" ]; then
echo "Usage: $0 [-f | -r | -h] <subject>"
echo " -r re-use existing key"
echo " -f overwrite existing key"
echo " -h show this help"
echo "<subject> Certificate subject in the form"
echo " /C=country/ST=state/L=location/O=organization/CN=common-name"
echo "Creates SSL key, certificate request + self-signed certificate for the"
echo "given subject as ssl.key, ssl.crq and ssl.crt."
exit 0
fi
keyargs="-new -keyout ssl.key -newkey rsa:1024"
if [ -r ssl.key ]; then
if [ "$1" = "-r" ]; then
keyargs="-key ssl.key"
shift
elif [ "$1" != "-f" ]; then
echo "Key exists. Will not overwrite. Use -f to force me."
exit 1
fi
else
if [ "$1" = "-r" ]; then
echo "No key found. Aborting."
exit 1
fi
shift
fi
openssl req -new -out ssl.crq $keyargs -nodes -batch -subj "$1"
openssl req -x509 -in ssl.crq -out ssl.crt -key ssl.key -nodes -days 365