← history for iron/libexec/iron/iron-certificate
5e2b04fairon/libexec/iron/iron-certificate111 lines⬡ raw↓ download
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
#! /bin/bash 
# Usage: $iron certificate <sign> <filename>
# Summary: create certificates 
# Help: This command groups commands used to setup config create delete a CA


APPNAME="iron"
CURRDIR=$(pwd)

RCDIR=$HOME/.$APPNAME
test -d $RCDIR || mkdir -p $RCDIR
CURRENT_TSTAMP=$(date '+%Y%m%d%H%M')

function certificate_sign()
{
	service=$1
	csrfile=$2
	client=$(basename $csrfile | sed -e 's/.csr$//')
	CLIENT_CERTPATH=$RCDIR/$service/certs
	CP=$CLIENT_CERTPATH/$client
	test -d ${CP} || mkdir -p ${CP}
	openssl x509 -req -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $csrfile -out $CP/$client.crt
}

function remote_client_write_setup()
{
	CLIENT_RCFILE="$RCDIR/client-$client"".env"
        echo "REMOTE_RCDIR=$RCDIR">${CLIENT_RCFILE}
        echo "REMOTE_USER=">>${CLIENT_RCFILE}
        echo "REMOTE_HOST=">>${CLIENT_RCFILE}
}


function remote_certificate_sign()
{
	service=$1
	remote_csr_user=$2
	remote_csr_host=$3
	remote_csrfile=$4
	csrfile=$(basename $remote_csrfile) 
	remote_certdir=$(dirname $remote_csrfile) 
	client=$(basename $csrfile | sed -e 's/.csr$//')
	CLIENT_CERTPATH=$RCDIR/$service/certs
	CP=$CLIENT_CERTPATH/$client 
	test -d ${CP} || mkdir -p ${CP}
	scp $remote_csr_user@$remote_csr_host:$remote_csrfile $CP/$client.csr
	openssl x509 -req -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $CP/$client.csr -out $CP/$client.crt
	scp $CP/$client.crt $REMOTE_USER@$REMOTE_HOST:$remote_certdir
}

function certificate_create()
{
	service=$1
	client=$2
	CLIENT_CERTPATH=$RCDIR/$service/certs
	CP=$CLIENT_CERTPATH/$client
	test -d ${CP} || mkdir -p ${CP}
	openssl x509 -req -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $CP/$client.csr -out $CP/$client.crt
	test -f $CP/$client.crt && echo "Created certificate in [$CP/$client.crt]"
}


function pkcs12_create()
{
	service=$1
	client=$2
	CLIENT_CERTPATH=$RCDIR/$service/certs
	CP=$CLIENT_CERTPATH/$client
	test -d ${CP} || mkdir -p ${CP}
        openssl pkcs12 -export -clcerts -in $CP/$client.crt -inkey $CP/$client.key -out $CP/$client.p12\
                 -name "${client}"\
                 -CAfile ${CAPATH}/ca.crt -caname root
	test -f $CP/$client.p12 && echo "Created PKCS12 (*.p12) in [$CP/$client.p12]"
}




if [ "$#" = 3 ];then
	operation=$1
	service=$2
	CA_RCFILE="$HOME/"."$APPNAME/default-ca"".env"
	source ${CA_RCFILE}
	if [ "$operation" = "sign" ]; then
		csrfile=$3
		certificate_sign $service $csrfile
	fi 

	if [ "$operation" = "create" ]; then
		client=$3
		certificate_create $service $client
		pkcs12_create $service $client
	fi 

	if [ "$operation" = "remotesign" ]; then
		client=$3
		CLIENT_RCFILE="$RCDIR/client-$client"".env"
		test -f $CLIENT_RCFILE || ( echo "Edit $CLIENT_RCFILE first" ; remote_client_write_setup  ; exit -1)
		test -f $CLIENT_RCFILE && source ${CLIENT_RCFILE}
		remote_csr_user=$REMOTE_USER
		remote_csr_host=$REMOTE_HOST
		remote_csrfile=$REMOTE_RCDIR/$service/certs/$client/$client.csr
		remote_certificate_sign $service $remote_csr_user $remote_csr_host $remote_csrfile
	fi 
else
	echo "Usage:"
	echo "\$iron certificate <sign> <service> <CSR-file>"
	echo "\$iron certificate <create> <service> <client-id>"
	echo "\$iron certificate <remotesign> <service><client-id>"
	exit -1
fi