#!/bin/sh

OPENSSL=/home/product/code/firmware/current/bin/openssl
CONF=/home/product/code/config/ssl.conf
KEY=/home/product/code/config/ssl_private_key.pem
CERT=/home/product/code/config/ssl_private_cert.pem
DER=/home/product/code/config/ssl_private_cert.der
CSR=/home/product/code/config/ssl_csr.pem
CERTSERIAL=`/home/product/code/firmware/current/bin/ssl_inc_serial.pl /home/product/code/config/ssl_private_cert.srl`
RND=/tmp/rnd.$$
LENGTH=`/home/product/code/firmware/current/bin/config_read 'ssl_priv_cert_expiration'| /bin/grep ssl_priv_cert_expiration| /usr/bin/cut -d '|' -f5`
KEYSIZE=`/home/product/code/firmware/current/bin/config_read 'ssl_priv_cert_key_strength'| /bin/grep ssl_priv_cert_key_strength| /usr/bin/cut -d '|' -f5 | sed -e 's/^\s*//'`

/bin/dd if=/dev/urandom of=$RND bs=256 count=1

$OPENSSL req -new -batch -x509 -days ${LENGTH:-"365"} -newkey rsa:${KEYSIZE:-"1024"} -nodes -rand $RND -config $CONF -out $CERT -keyout $KEY -set_serial $CERTSERIAL
$OPENSSL x509 -inform PEM -outform DER -in $CERT -out $DER
$OPENSSL x509 -subject -dates -fingerprint -noout -in $CERT
$OPENSSL req -new -batch -key $KEY -config $CONF -set_serial $CERTSERIAL -out $CSR
/bin/rm -f $RND

# now prepend ssl private key to generated
/bin/cat $KEY $CERT > $CERT.new
/bin/mv $CERT.new $CERT

