#! /bin/sh

rsa_params() {
	_in="$1"
	_outbase="$2"
	set -e
	openssl rsa -noout -text -in $_in | \
	    awk '/^modulus:$/,/^publicExponent:/' | \
	    grep -v '^[a-zA-Z]' | tr -d ' \n:' > ${_outbase}.n
	openssl rsa -noout -text -in $_in | \
	    awk '/^prime1:$/,/^prime2:/' | \
	    grep -v '^[a-zA-Z]' | tr -d ' \n:' > ${_outbase}.p
	openssl rsa -noout -text -in $_in | \
	    awk '/^prime2:$/,/^exponent1:/' | \
	    grep -v '^[a-zA-Z]' | tr -d ' \n:' > ${_outbase}.q
	for x in n p q ; do
		echo "" >> ${_outbase}.$x
		echo ============ ${_outbase}.$x
		cat ${_outbase}.$x
		echo ============
	done
}

set -ex

cd testdata
PW=`cat pw`

 # ..n.. - custom format keys
rm -f rsa_1 rsa_2 rsa_n rsa_1_pw rsa_n_pw

ssh-keygen -t rsa -b 2048 -C "RSA test key #1" -N '' -f rsa_1 -m PKCS8
ssh-keygen -t rsa -b 3072 -C "RSA test key #2" -N '' -f rsa_2 -m PKCS8

cp rsa_1 rsa_n
ssh-keygen -pf rsa_n -N '' -m OpenSSH

cp rsa_1 rsa_1_pw
cp rsa_1 rsa_n_pw

ssh-keygen -pf rsa_1_pw -N "$PW" -m PKCS8
ssh-keygen -pf rsa_n_pw -N "$PW" -m OpenSSH

rsa_params rsa_1 rsa_1.param
rsa_params rsa_2 rsa_2.param

ssh-keygen -s rsa_2 -I hugo -n user1,user2 \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 1 rsa_1.pub
ssh-keygen -s rsa_2 -I hugo -n user1,user2 \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 2 dsa_1.pub
ssh-keygen -s rsa_2 -I hugo -n user1,user2 \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 3 ecdsa_1.pub
ssh-keygen -s rsa_2 -I hugo -n user1,user2 \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 4 ed25519_1.pub

# Make a few RSA variant signature too.
cp rsa_1 rsa_1_sha1
cp rsa_1 rsa_1_sha512
cp rsa_1.pub rsa_1_sha1.pub
cp rsa_1.pub rsa_1_sha512.pub

ssh-keygen -s rsa_2 -I hugo -n user1,user2 -t ssh-rsa \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 1 rsa_1_sha1.pub
ssh-keygen -s rsa_2 -I hugo -n user1,user2 -t rsa-sha2-512 \
    -Oforce-command=/bin/ls -Ono-port-forwarding -Osource-address=10.0.0.0/8 \
    -V 19990101:20110101 -z 1 rsa_1_sha512.pub

ssh-keygen -s ed25519_1 -I julius -n host1,host2 -h \
    -V 19990101:20110101 -z 5 rsa_1.pub

ssh-keygen -lf rsa_1 | awk '{print $2}' > rsa_1.fp
ssh-keygen -lf rsa_2 | awk '{print $2}' > rsa_2.fp

ssh-keygen -lf rsa_1-cert.pub  | awk '{print $2}' > rsa_1-cert.fp

ssh-keygen -Bf rsa_1 | awk '{print $2}' > rsa_1.fp.bb
ssh-keygen -Bf rsa_2 | awk '{print $2}' > rsa_2.fp.bb


# Password-protected keys are converted to PKCS8 format,
# as PEM format is not supported in FIPS mode.
#ssh-keygen -pf ecdsa_1_pw  -N `cat pw` -P `cat pw` -m PKCS8
#ssh-keygen -pf dsa_1_pw  -N `cat pw` -P `cat pw` -m PKCS8

# Password protected keys in PKCS8 format for builds with OpenSSL 0.9.8
# cp rsa_1 rsa_1_pw_098
# ssh-keygen -pf rsa_1_pw_098 -N '' -P `cat pw` -m PKCS8
# cp dsa_1 dsa_1_pw_098
# ssh-keygen -pf dsa_1_pw_098 -N '' -P `cat pw` -m PKCS8
