]> git.somenet.org - somenet/certgen.git/blob - certgen.sh
Added EXIT operation to abort generation on a given line.
[somenet/certgen.git] / certgen.sh
1 #!/bin/bash
2
3 MYPWD=$(pwd)
4 umask 0027
5
6 echo "cleanup previous run..."
7 rm -rf output/*
8 mkdir output/csr output/crt output/sgn
9
10 while read cdline ; do
11         if [[ $cdline == "" || $cdline == "#"* ]] ; then
12                 continue
13         fi
14
15         cd $MYPWD
16         read -a certdata <<< "$cdline"
17
18         if [[ ${certdata[0]} == "EXIT" ]] ; then
19                 echo "*** $cdline ***" 1>&2
20                 exit ${certdata[1]}
21         fi
22
23         echo "*** Processing: $(date -Iseconds) - ${certdata[0]} - ${certdata[1]} ***"
24         if [[ -d "output/${certdata[1]}"  ]] ; then
25                 echo "*** ERROR - THIS SEEMS TO ALREADY EXIST ***" 1>&2
26                 echo "*** ABORTED ***" 1>&2
27                 exit 1
28         fi
29
30         mkdir "output/${certdata[1]}"
31         chmod o+x "output/${certdata[1]}"
32         cd "output/${certdata[1]}"
33
34         # Handle "CA" type here.
35         if [[ ${certdata[0]} == "CA" ]] ; then
36                 mkdir -m 0700 certs crl newcerts
37                 touch index.txt
38                 export CA_PATH="./"
39                 openssl req -batch -new -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -nodes -subj "${certdata[2]}" -reqexts v3_ca_req -config "${MYPWD}/openssl.cnf" &>/dev/null
40                 continue
41         fi
42
43
44         # Handle non "CA" types here.
45         export CA_PATH="$MYPWD/ca/"
46         SUBJECT="${certdata[2]}CN=${certdata[1]}/"
47         DNS_NAMES="${certdata[1]},${certdata[3]}"
48         OLDIFS=$IFS
49         IFS=","
50         cat "${MYPWD}/openssl.cnf" > /tmp/certgen.cnf
51         COUNTER=0
52         for name in $DNS_NAMES; do
53                 if [[ "" == $name ]] ; then
54                         continue
55                 fi
56                 COUNTER=$((COUNTER+1))
57                 echo "DNS.${COUNTER} = $name" >> /tmp/certgen.cnf
58         done
59         IFS=$OLDIFS
60         unset OLDIFS
61         unset COUNTER
62         
63         openssl genrsa -out "${certdata[1]}.key" 4096 &> /dev/null
64         openssl req -new -key "${certdata[1]}.key" -out "${certdata[1]}.csr" -utf8 -batch -subj "${SUBJECT}" -config /tmp/certgen.cnf
65
66         if [[ ${certdata[0]} == "SGN" ]] ; then
67                 if [[ ! -d "${CA_PATH}"  ]] ; then
68                         echo "*** ERROR - NO CA DATA FOUND ***" 1>&2
69                         echo "*** maybe generate a CA and move it to ${CA_PATH} ***" 1>&2
70                         echo "copy template: mv output/SomeNet ${CA_PATH}" 1>&2
71                         echo "*** ABORTED ***" 1>&2
72                         exit 2
73                 fi
74
75                 openssl ca -batch -create_serial -out "${certdata[1]}.crt" -days 365 -keyfile "${MYPWD}/ca/ca.key" -extensions v3_ca \
76                         -config "${MYPWD}/openssl.cnf" -infiles "${certdata[1]}.csr"
77                 cat "${MYPWD}/ca/ca.crt" >> "${certdata[1]}.crt"
78
79         elif [[ ${certdata[0]} == "CRT" ]] ; then
80                 openssl x509 -req -signkey "${certdata[1]}.key" -in "${certdata[1]}.csr" -out "${certdata[1]}.crt" -extensions v3_req -extfile /tmp/certgen.cnf \
81                         -days 365 -sha512 &> /dev/null
82                 chmod o+r "${certdata[1]}.crt"
83
84                 echo -n "${certdata[1]} " >> "${MYPWD}/output/fpfile.txt"
85                 openssl x509 -in "${certdata[1]}.crt" -fingerprint -noout -sha512 >> "${MYPWD}/output/fpfile.txt"
86                 echo "" >> "${MYPWD}/output/fpfile.txt"
87         fi
88
89         rm /tmp/certgen.cnf
90         cd $MYPWD
91
92         if [[ ${certdata[0]} == "SGN" ]] ; then
93                 mv "output/${certdata[1]}" "output/sgn/${certdata[1]}"
94         elif [[ ${certdata[0]} == "CRT" ]] ; then
95                 mv "output/${certdata[1]}" "output/crt/${certdata[1]}"
96         else
97                 mv "output/${certdata[1]}" "output/csr/${certdata[1]}"
98         fi
99
100 done < certgen.data
101
102 echo "*** DONE ***"
103 ls -l "${MYPWD}/output/"*/ | grep -v "total"
104