]> WPIA git - gigi.git/blob - config/generateTruststoreNRE.sh
fix: allow dev-certificates to be regenerated (with different serials)
[gigi.git] / config / generateTruststoreNRE.sh
1 #!/bin/bash
2 # this script imports the root certs into a Java key store
3 # additionally it can generate the certs for gigi, if none are provided and the CA-keys are available in the config folder for the Simple Signer
4 # This script is generally only intended for development purposes.
5
6 rm -f cacerts.jks
7
8 function import(){
9   name=$1
10   keytool -importcert -keystore ../config/cacerts.jks -file "$1.crt" -alias own -storepass "changeit" -alias "$(basename $name)" $2
11 }
12
13 function importP(){
14  keytool -importkeystore -srckeystore "$1" -noprompt -destkeystore keystore.pkcs12 -srcstoretype pkcs12 -deststoretype pkcs12 -deststorepass changeit -srcstorepass changeit
15 }
16
17 import ca/root -noprompt
18 import ca/assured
19 import ca/unassured
20 import ca/orga
21 import ca/orgaSign
22 import ca/codesign
23
24 for i in ca/*_*_*; do
25   import ${i%.crt}
26 done
27
28 # Generate Gigi certificates manually
29 cabasename=assured
30 caname=${cabasename}_$(date +%Y)_1
31 ca=../signer/ca/$caname/ca
32 if [[ -f "$ca.key" ]] && ! [[ -f keystore.pkcs12 ]]; then
33     if [[ -f serial_base ]]; then
34         serial_base=$(< serial_base)
35     else
36         serial_base=100000
37     fi
38     serial_base=$((serial_base + 1))
39     printf "%d\n" "$serial_base" >| serial_base
40     # when the domain is provided externally as environment variable, use it and do not prompt for it.
41     [[ -z $DOMAIN ]] && read -rp "I need to generate gigi-certificates. I need your base domain: " DOMAIN
42     # Assuming we have access to the CA-keys we generate two certificates and present them to gigi
43     # One to be used for all 4 https domains and one as email certificate.
44
45     # Generate two keys and certs requests. The CN of the SSL-server cert doesn't really matter, as we use subject alt names anyways.
46     openssl req -newkey rsa:2048 -keyout www.key -out www.csr -nodes -subj "/CN=gigi server certificate"
47     openssl req -newkey rsa:2048 -keyout mail.key -out mail.csr -nodes -subj "/CN=gigi system"
48
49     # Sign the two requests with the keys in the config of the simple signer. Use the serial_base with extensions 1 and 2. These serials are long enough to probably not collide with the "simple signer"
50     openssl x509 -req -in www.csr -out www.crt -CA $ca.crt -CAkey $ca.key -set_serial ${serial_base}1 -extfile <(printf "[ext]\nsubjectAltName=DNS:www.$DOMAIN,DNS:secure.$DOMAIN,DNS:static.$DOMAIN,DNS:api.$DOMAIN\nbasicConstraints=CA:FALSE\nextendedKeyUsage=serverAuth\nkeyUsage=digitalSignature,keyEncipherment\n") -extensions ext
51     openssl x509 -req -in mail.csr -out mail.crt -CA $ca.crt -CAkey $ca.key -set_serial ${serial_base}2 -extfile <(printf "[ext]\nsubjectAltName=email:support@$DOMAIN\nbasicConstraints=CA:FALSE\nextendedKeyUsage=emailProtection\nkeyUsage=digitalSignature,keyEncipherment\n") -extensions ext
52
53     # Store the webserver cert in 4 different pkcs12-keystores to have different "key aliases" and import them all into the "keystore.pkcs12" using the "importP"-method
54     for t in www api secure static; do
55         # concatenate private key and certificate chain together
56         # and filter out comments from .crt files with "openssl x509"
57         # before feeding them into "openssl pkcs12"
58         cat www.key www.crt ca/$caname.crt ca/$cabasename.crt ca/root.crt |\
59             (openssl pkey; for i in {1..4}; do openssl x509; done) |\
60             openssl pkcs12 -export -out $t.pkcs12 -name "$t" -passout pass:changeit
61         importP "$t.pkcs12"
62     done
63     # and finally add the mail certificate
64     cat mail.key mail.crt ca/$caname.crt ca/$cabasename.crt ca/root.crt |\
65         (openssl pkey; for i in {1..4}; do openssl x509; done) |\
66         openssl pkcs12 -export -out mail.pkcs12 -name "mail" -passout pass:changeit
67     importP "mail.pkcs12"
68 fi
69 keytool -list -keystore ../config/cacerts.jks -storepass "changeit"