1
+ #! /bin/bash
2
+
3
+ # Set variables
4
+ PRIMARY_DOMAIN=" ccn-coverage-vis"
5
+ CERT_DIR=" /certs" # Use absolute path
6
+ DAYS_VALID=365
7
+
8
+ # Add all domains that might be used to access your service
9
+ DOMAINS=(
10
+ " $PRIMARY_DOMAIN "
11
+ " localhost"
12
+ " 127.0.0.1"
13
+ )
14
+
15
+ # Create directory for certificates if it doesn't exist
16
+ mkdir -p $CERT_DIR
17
+
18
+ echo " Generating self-signed certificates..."
19
+
20
+ # Generate private key
21
+ openssl genrsa -out $CERT_DIR /private-key.pem 2048
22
+
23
+ # Create config file for SAN support
24
+ cat > $CERT_DIR /openssl.cnf << EOF
25
+ [req]
26
+ distinguished_name = req_distinguished_name
27
+ req_extensions = v3_req
28
+ prompt = no
29
+
30
+ [req_distinguished_name]
31
+ CN = $PRIMARY_DOMAIN
32
+
33
+ [v3_req]
34
+ basicConstraints = CA:FALSE
35
+ keyUsage = nonRepudiation, digitalSignature, keyEncipherment
36
+ subjectAltName = @alt_names
37
+
38
+ [alt_names]
39
+ EOF
40
+
41
+ # Add all domains to the config file
42
+ for i in " ${! DOMAINS[@]} " ; do
43
+ echo " DNS.$(( i+ 1 )) = ${DOMAINS[$i]} " >> $CERT_DIR /openssl.cnf
44
+ done
45
+
46
+ # Generate a CSR with the config
47
+ openssl req -new -key $CERT_DIR /private-key.pem -out $CERT_DIR /csr.pem -config $CERT_DIR /openssl.cnf
48
+
49
+ # Generate the self-signed certificate
50
+ openssl x509 -req -days $DAYS_VALID -in $CERT_DIR /csr.pem -signkey $CERT_DIR /private-key.pem -out $CERT_DIR /certificate.pem -extensions v3_req -extfile $CERT_DIR /openssl.cnf
51
+
52
+ # Create a full chain file
53
+ cat $CERT_DIR /certificate.pem > $CERT_DIR /fullchain.pem
54
+
55
+ # Set proper permissions (readable by all)
56
+ chmod 644 $CERT_DIR /private-key.pem
57
+ chmod 644 $CERT_DIR /certificate.pem
58
+ chmod 644 $CERT_DIR /fullchain.pem
59
+
60
+ # Try to generate PKCS12 file but don't fail if it doesn't work
61
+ openssl pkcs12 -export -out $CERT_DIR /certificate.pfx -inkey $CERT_DIR /private-key.pem -in $CERT_DIR /certificate.pem -passout pass: || echo " PKCS12 export failed, but continuing"
62
+
63
+ # Verify file creation and permissions
64
+ echo " Certificates generated successfully in $CERT_DIR directory!"
65
+ echo " Files generated with permissions:"
66
+ ls -la $CERT_DIR /
67
+
68
+ # Verify certificate content
69
+ echo " Verifying certificate:"
70
+ openssl x509 -in $CERT_DIR /certificate.pem -text -noout | head -n 15
71
+
72
+ # Verify private key
73
+ echo " Verifying private key:"
74
+ openssl rsa -in $CERT_DIR /private-key.pem -check -noout || echo " Private key verification failed"
0 commit comments