#!/bin/sh
#
#   rsa-keygen - generate an RSA public/private key pair for PKCIPE
#
#   Copyright 2000 Olaf Titz <olaf@bigred.inka.de>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version
#   2 of the License, or (at your option) any later version.

# $Id: rsa-keygen.in,v 1.2 2001/01/20 17:46:14 olaf Exp $

openssl=@OPENSSL@

umask 077
if [ -z "$1" ] ; then
    echo "usage: rsa-keygen [-p] name [bits]"
    echo "The name is the file name _and_ the identity name."
    echo "bits defaults to 1024."
    echo "Use -p to set a passphrase on the private key."
    exit 1
fi

pp=false
if [ "$1" = "-p" ] ; then
    pp=true
    shift
fi

tmp=$1.tmp
priv=$1.priv
pub=$1

set -e
trap "rm -f $tmp $priv $pub; trap 0; exit 1" 0 1 2 15

$openssl genrsa -out $tmp ${2:-1024}
$openssl rsa -pubout -out $pub -in $tmp
if $pp ; then
    $openssl rsa -des3 -out $priv -in $tmp
else
    mv -f $tmp $priv
fi
chmod 400 $priv
chmod 644 $pub
echo "Private key in $priv, Public key in $pub"

trap "rm -f $tmp" 0
exit 0
