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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#! /bin/bash
# Usage: $iron certificate <sign> <filename>
# Summary: create certificates
# Help: This command groups commands used to setup config create delete a certificate
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()
{
service=$1
mkdir -p $RCDIR/$service
CLIENT_RCFILE="$RCDIR/$service/client-$client"".env"
echo "REMOTE_RCDIR=$RCDIR">${CLIENT_RCFILE}
echo "REMOTE_USER=">>${CLIENT_RCFILE}
echo "REMOTE_HOST=">>${CLIENT_RCFILE}
echo "REMOTE_SSH_IDENTITY_FILE=">>${CLIENT_RCFILE}
exit 1
}
function remote_certificate_sign()
{
service=$1
remote_csr_user=$2
remote_csr_host=$3
remote_csrfile=$4
remote_ssh_identity_file=$5
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}
if [ -f $remote_ssh_identity_file ]; then
scp -i $remote_ssh_identity_file $remote_csr_user@$remote_csr_host:$remote_csrfile $CP/$client.csr
else
scp $remote_csr_user@$remote_csr_host:$remote_csrfile $CP/$client.csr
fi
openssl x509 -req -days 365 -CA $CAPATH/ca.crt -CAkey $CAPATH/ca.key -CAcreateserial -in $CP/$client.csr -out $CP/$client.crt
if [ -f $remote_ssh_identity_file ]; then
scp -i $remote_ssh_identity_file $CP/$client.crt $REMOTE_USER@$REMOTE_HOST:$remote_certdir
else
scp $CP/$client.crt $REMOTE_USER@$REMOTE_HOST:$remote_certdir
fi
}
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" = "renew" ]; then
client=$3
certificate_create $service $client
pkcs12_create $service $client
fi
if [ "$operation" = "remotesign" ]; then
client=$3
CLIENT_RCFILE="$RCDIR/$service/client-$client"".env"
test -f $CLIENT_RCFILE || echo "Edit $CLIENT_RCFILE first"
test -f $CLIENT_RCFILE || remote_client_write_setup "$service";
test -f $CLIENT_RCFILE && source ${CLIENT_RCFILE}
test -f $CLIENT_RCFILE && echo "Reading RC from: ${CLIENT_RCFILE}"
remote_ssh_identity_file=$REMOTE_SSH_IDENTITY_FILE
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 $remote_ssh_identity_file
fi
else
echo "Usage:"
echo "\$iron certificate <sign> <service> <CSR-file>"
echo "\$iron certificate <create> <service> <client-id>"
echo "\$iron certificate <renew> <service> <client-id>"
echo "\$iron certificate <remotesign> <service><client-id>"
exit -1
fi
|