Debian Codegraph @

images/squid/generate-certs.sh

raw symbols SCIP
1 #!/bin/sh
2
3 # Generates a CA and a certificate for use with squid
4 # After running the script, the CA will be in ${destDir}/rootCA.crt, clients
5 # will need it, but the squid container has no need for it
6 # The only file squid needs is ${destDir}/squid.pem
7 #
8 # The certificate is only valid for 127.0.0.1, localhost and 192.168.122.1
9 # If certificates for other hostnames/IPs are needed, modify the [alt_names]
10 # section
11 #
12 # The certificate used by squid can be overriden when starting the container:
13 # podman run -v ./pki/:/etc/pki/squid:z ...
14
15 set -euo pipefail
16
17 destDir=${1:-pki}
18 baseName=${2:-squid}
19
20 mkdir -p $destDir
21
22 cat >${destDir}/rootCA.cnf <<EOF
23 # default section for "req" command options
24 [req]
25 distinguished_name = rootca_dn
26 prompt = no
27
28 [rootca_dn]
29 # Minimum of 4 bytes are needed for common name
30 commonName = squid CA
31
32 # ISO2 country code only
33 countryName = FR
34
35 # City is required
36 localityName = Paris
37
38 [v3_ca]
39 subjectKeyIdentifier=hash
40 authorityKeyIdentifier=keyid:always,issuer
41 basicConstraints = critical, CA:TRUE, pathlen:3
42 keyUsage = critical, cRLSign, keyCertSign
43 nsCertType = sslCA, emailCA
44 EOF
45
46 # can most likely be done without config file, see EXAMPLES in man openssl-req:
47 # openssl req -new -subj "/C=GB/CN=foo" \
48 # -addext "subjectAltName = DNS:foo.co.uk" \
49 # -addext "certificatePolicies = 1.2.3.4" \
50 # -newkey rsa:2048 -keyout key.pem -out req.pem
51 openssl req -x509 -newkey rsa:4096 -nodes -keyout ${destDir}/rootCA.key -sha256 -days 2048 -out ${destDir}/rootCA.crt -config ${destDir}/rootCA.cnf -extensions v3_ca
52
53 cat >${destDir}/${baseName}.cnf <<EOF
54 # default section for "req" command options
55 [req]
56 distinguished_name = cert_dn
57 prompt = no
58
59 [cert_dn]
60 commonName = squid
61
62 # ISO2 country code only
63 countryName = FR
64
65 # City is required
66 localityName = Paris
67
68 [v3_req]
69 basicConstraints = CA:FALSE
70 keyUsage = nonRepudiation, digitalSignature, keyEncipherment
71 subjectAltName = @alt_names
72
73 [alt_names]
74 DNS.0 = localhost
75 IP.0 = 127.0.0.1
76 # can be useful in nested virtualization setups
77 IP.1 = 192.168.122.1
78 EOF
79
80 openssl req -newkey rsa:4096 -nodes -keyout ${destDir}/${baseName}.key -out ${destDir}/${baseName}.csr -config ${destDir}/${baseName}.cnf -extensions v3_req #-addext "subjectAltName = IP:${commonName}"
81 openssl x509 -req -in ${destDir}/${baseName}.csr -CA ${destDir}/rootCA.crt -CAkey ${destDir}/rootCA.key -CAcreateserial -out ${destDir}/${baseName}.crt -days 1000 -sha256 -extensions v3_req -extfile ${destDir}/${baseName}.cnf
82
83 cat ${destDir}/${baseName}.crt ${destDir}/${baseName}.key >${destDir}/${baseName}.pem
84
85 echo "Successfully generated root CA and certificate"
86 echo ""
87 echo "Add 'https_port 3129 tls-cert=${destDir}/${baseName}.pem' to your squid configuration file"
88 echo ""
89 echo "The public CA certificate is ${destDir}/rootCA.crt"
90 echo "This can be used with curl with: curl --proxy-cacert ${destDir}/rootCA.crt -L https://gandi.net"