#!/bin/sh
# Generate OpenSSH host key pair from same keys as NETCONF
set -e
umask 0077

NAME="$1"
DIR="$2"
PUBLIC="$3"
PRIVATE="$4"
TMP="$(mktemp)"

{
	echo '-----BEGIN PRIVATE KEY-----'
	printf '%s\n' "$PRIVATE" | fold -w 64
	echo '-----END PRIVATE KEY-----'
} > "$DIR/$NAME"

{
	echo "-----BEGIN RSA PUBLIC KEY-----"
	printf '%s\n' "$PUBLIC" | fold -w 64
	echo "-----END RSA PUBLIC KEY-----"
} > "$TMP"

ssh-keygen -i -f "$TMP" -m PKCS8 > "$DIR/$NAME.pub"
rm "$TMP"

chmod 0600 "$DIR/$NAME.pub"
chmod 0600 "$DIR/$NAME"
chown sshd:sshd "$DIR/$NAME.pub"
chown sshd:sshd "$DIR/$NAME"
