]> git.somenet.org - somenet/certgen.git/blob - certgen.sh
changed the way we write dns-alt-names.
[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
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         echo "*** Processing: $(date -Iseconds) - ${certdata[0]} - ${certdata[1]} ***"
19         if [[ -d "output/${certdata[1]}"  ]] ; then
20                 echo "*** ERROR - THIS SEEMS TO ALREADY EXIST ***" 1>&2
21                 echo "*** ABORTED ***" 1>&2
22                 exit 1
23         fi
24
25         mkdir "output/${certdata[1]}"
26         chmod o+x "output/${certdata[1]}"
27
28         SUBJECT="${certdata[2]}CN=${certdata[1]}/"
29         DNS_NAMES="${certdata[1]},${certdata[3]}"
30         OLDIFS=$IFS
31         IFS=","
32         cat openssl.cnf > /tmp/certgen.cnf
33         COUNTER=0
34         for name in $DNS_NAMES; do
35                 COUNTER=$((COUNTER+1))
36                 echo "DNS.${COUNTER} = $name" >> /tmp/certgen.cnf
37         done
38         IFS=$OLDIFS
39         unset OLDIFS
40         unset COUNTER
41         
42         cd "output/${certdata[1]}"
43         openssl genrsa -out "${certdata[1]}.key" 4096 &> /dev/null
44         openssl req -new -key "${certdata[1]}.key" -out "${certdata[1]}.csr" -utf8 -batch -subj "${SUBJECT}" -config /tmp/certgen.cnf
45
46         if [[ ${certdata[0]} == "CRT" ]] ; then
47                 openssl x509 -req -signkey "${certdata[1]}.key" -in "${certdata[1]}.csr" -out "${certdata[1]}.crt" -extensions v3_req -extfile /tmp/certgen.cnf \
48                         -days 365 -sha512 &> /dev/null
49                 chmod o+r "${certdata[1]}.crt"
50
51                 echo -n "${certdata[1]} " >> "${MYPWD}/output/fpfile.txt"
52                 openssl x509 -in "${certdata[1]}.crt" -fingerprint -noout -sha512 >> "${MYPWD}/output/fpfile.txt"
53                 echo "" >> "${MYPWD}/output/fpfile.txt"
54         fi
55
56         rm /tmp/certgen.cnf
57         cd $MYPWD
58
59         if [[ ${certdata[0]} == "CRT" ]] ; then
60                 mv "output/${certdata[1]}" "output/crt/${certdata[1]}"
61         else
62                 mv "output/${certdata[1]}" "output/csr/${certdata[1]}"
63         fi
64
65 done < certgen.data
66
67 echo "*** DONE ***"
68 ls -l output/*/ | grep -v "total"
69