← history for iron/libexec/iron/iron-ca
b99217f2iron/libexec/iron/iron-ca247 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#! /bin/bash
# Usage: iron ca <setup|create|delete|reset> [intermediate]
# Summary: manage CA
# Help: This command groups commands used to setup config create delete a CA

KEYSIZE=4096

APPNAME="iron"
CURRDIR=$(pwd)

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

function ca_create()
{
  test -d $CAPATH   || mkdir -p ./$CAPATH
  CA_DAYS=3650
  openssl genrsa -out   $CAPATH/ca.key ${KEYSIZE}
  openssl req    -batch -new -key $CAPATH/ca.key -out $CAPATH/ca.csr -config $CA_CONFIG_FILE
  openssl x509   -req   -days ${CA_DAYS} -in $CAPATH/ca.csr -signkey $CAPATH/ca.key -out $CAPATH/ca.crt -extensions v3_ca -extfile $RCDIR/CA/conf/openssl-ca-extensions.conf
}

function intermediate_create()
{
  intermediate=$1
  test -d $CAPATH/$intermediate   || mkdir -p ./$CAPATH/$intermediate
  CA_DAYS=3650
  openssl genrsa -out $CAPATH/$intermediate-ca.key ${KEYSIZE}
  openssl req    -batch -new -key $CAPATH/$intermediate-ca.key \
  -out $CAPATH/$intermediate-ca.csr -config $CA_CONFIG_FILE
  openssl x509 -req -days ${CA_DAYS} \
  -in $CAPATH/$intermediate-ca.csr \
  -signkey $CAPATH/$intermediate-ca.key \
  -out $CAPATH/$intermediate-ca.crt \
  -extensions v3_intermediate_ca \
  -extfile $RCDIR/CA/$intermediate/conf/openssl-intermediate-extensions.conf
}

function ca_home_setup_delete()
{
  echo "About to DELETE ca: are you sure? y/n"
  read confirmation
  if [ "$confirmation" = "y" ]; then
    (rm -fr  $RCDIR/CA ;  rm $RCFILE) && echo "CA DELETED"
  else
    echo "SKIPPING"
    exit -1
  fi
}

function intermediate_home_setup_delete()
{
  intermediate=$1
  echo "About to DELETE ca: are you sure? y/n"
  read confirmation
  if [ "$confirmation" = "y" ]; then
    (rm -fr  $RCDIR/CA/$intermediate ;  rm $RCFILE) && echo "CA DELETED"
  else
    echo "SKIPPING"
    exit -1
  fi
}

function intermediate_home_setup_write()
{
  intermediate=$1
  test -d $RCDIR/CA/$intermediate && ( echo "CA exist, please delete before" && exit -1)
  test -d $RCDIR/CA/$intermediate || mkdir -p $RCDIR/CA/$intermediate
  test -d $RCDIR/CA/$intermediate/conf || mkdir -p $RCDIR/CA/$intermediate/conf
  echo "CAPATH=$RCDIR/CA/$intermediate">${RCFILE}
  echo "CA_CONFIG_FILE=$RCDIR/CA/$intermediate/conf/openssl-intermediate.conf">>${RCFILE}
	cat<<__EOF__ >$RCDIR/CA/$intermediate/conf/openssl-intermediate-extensions.conf
[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign
__EOF__
  
  
	cat<<__EOF__ >$RCDIR/CA/$intermediate/conf/openssl-intermediate.conf
#RANDFILE               = $ENV::HOME/.rnd

[ req ]
default_bits           = ${KEYSIZE}
default_keyfile        = keyfile.pem
distinguished_name     = req_distinguished_name
attributes             = req_attributes
prompt                 = no
#output_password        = abadpass
default_days		=3650
#x509_extensions     = v3_ca


[ req_distinguished_name ]
C                      = SE
ST                     = Sweden
L                      = Gotenburg
O                      = kevwe.se
OU                     = kevwe.se
#CN                     = kevwe.se
CN                     = ${intermediate}.kevwe.se
emailAddress           = cto@kevwe.se

[ req_attributes ]
challengePassword      = blablabla

[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

__EOF__
}

function ca_home_setup_write()
{
  test -d $RCDIR/CA && ( echo "CA exist, please delete before" && exit -1)
  test -d $RCDIR/CA || mkdir -p $RCDIR/CA
  test -d $RCDIR/CA/conf || mkdir -p $RCDIR/CA/conf
  echo "CAPATH=$RCDIR/CA">${RCFILE}
  echo "CA_CONFIG_FILE=$RCDIR/CA/conf/openssl-ca.conf">>${RCFILE}
	cat<<__EOF__ >$RCDIR/CA/conf/openssl-ca-extensions.conf
[ v3_ca ]
# Extensions for a typical CA
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

__EOF__
  
  
	cat<<__EOF__ >$RCDIR/CA/conf/openssl-ca.conf
#RANDFILE               = $ENV::HOME/.rnd

[ req ]
default_bits           = ${KEYSIZE}
default_keyfile        = keyfile.pem
distinguished_name     = req_distinguished_name
attributes             = req_attributes
prompt                 = no
#output_password        = abadpass
default_days		=3650
#x509_extensions     = v3_ca


[ req_distinguished_name ]
C                      = SE
ST                     = Sweden
L                      = Gotenburg
O                      = kevwe.se
OU                     = kevwe.se
CN                     = kevwe.se
emailAddress           = cto@kevwe.se

[ req_attributes ]
challengePassword      = blablabla

[ v3_intermediate_ca ]
# Extensions for a typical intermediate CA
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer
basicConstraints = critical, CA:true, pathlen:0
keyUsage = critical, digitalSignature, cRLSign, keyCertSign

[ usr_cert ]
# Extensions for client certificates
basicConstraints = CA:FALSE
nsCertType = client, email
nsComment = "OpenSSL Generated Client Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
extendedKeyUsage = clientAuth, emailProtection

[ server_cert ]
# Extensions for server certificates
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "OpenSSL Generated Server Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth

[ crl_ext ]
# Extension for CRLs
authorityKeyIdentifier=keyid:always

[ ocsp ]
# Extension for OCSP signing certificates
basicConstraints = CA:FALSE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer
keyUsage = critical, digitalSignature
extendedKeyUsage = critical, OCSPSigning

__EOF__
}

if [ "$#" = 2 ];then
  operation=$1
  intermediate=$2
  RCFILE="$HOME/"."$APPNAME/$intermediate-ca"".env"
  if [ "$operation" = "create" ]; then
    test -f ${RCFILE} || ( intermediate_home_setup_write $intermediate; echo "Edit values in ${RCFILE}"; exit -1)
    source ${RCFILE}
    intermediate_create $intermediate
    exit 0
  fi
  if [ "$operation" = "setup" ]; then
    test -f ${RCFILE} || ( intermediate_home_setup_write $intermediate ; echo "Edit values in ${RCFILE}"; exit 0)
    exit 0
  fi
  if [ "$operation" = "delete" ]; then
    intermediate_home_setup_delete $intermediate
    exit 0
  fi
fi

if [ "$#" = 1 ];then
  operation=$1
  RCFILE="$HOME/"."$APPNAME/default-ca"".env"
  if [ "$operation" = "create" ]; then
    test -f ${RCFILE} || ( ca_home_setup_write ; echo "Edit values in ${RCFILE}"; exit -1)
    source ${RCFILE}
    ca_create
    exit 0
  fi
  if [ "$operation" = "setup" ]; then
    test -f ${RCFILE} || ( ca_home_setup_write ; echo "Edit values in ${RCFILE}"; exit 0)
    #source ${RCFILE}
    exit 0
  fi
  if [ "$operation" = "delete" ]; then
    ca_home_setup_delete
    exit 0
  fi
fi

echo "Usage: iron ca <create|setup|delete> [intermediate]"
exit -1