Created
May 2, 2025 08:32
-
-
Save wilfriedwoivre/cf22e29a531c94f933c626f4081ac1d0 to your computer and use it in GitHub Desktop.
openssl self signed certificate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Script to create a self-signed certificate for test.org | |
| # Set variables | |
| DOMAIN="test.org" | |
| DAYS_VALID=365 | |
| OUTPUT_DIR="./certs" | |
| # Create output directory if it doesn't exist | |
| mkdir -p $OUTPUT_DIR | |
| # Generate a private key (2048-bit RSA) | |
| openssl genrsa -out $OUTPUT_DIR/$DOMAIN.key 2048 | |
| # Create a certificate signing request (CSR) | |
| openssl req -new -key $OUTPUT_DIR/$DOMAIN.key -out $OUTPUT_DIR/$DOMAIN.csr -subj "/C=US/ST=State/L=City/O=Organization/OU=Department/CN=$DOMAIN" | |
| # Create a self-signed certificate | |
| openssl x509 -req -days $DAYS_VALID -in $OUTPUT_DIR/$DOMAIN.csr -signkey $OUTPUT_DIR/$DOMAIN.key -out $OUTPUT_DIR/$DOMAIN.crt | |
| # Create a PEM file that includes both the certificate and private key | |
| cat $OUTPUT_DIR/$DOMAIN.crt $OUTPUT_DIR/$DOMAIN.key > $OUTPUT_DIR/$DOMAIN.pem | |
| # Add Subject Alternative Name (SAN) for www subdomain | |
| cat > $OUTPUT_DIR/san.cnf << EOF | |
| [req] | |
| distinguished_name = req_distinguished_name | |
| req_extensions = v3_req | |
| prompt = no | |
| [req_distinguished_name] | |
| CN = $DOMAIN | |
| [v3_req] | |
| subjectAltName = @alt_names | |
| [alt_names] | |
| DNS.1 = $DOMAIN | |
| DNS.2 = www.$DOMAIN | |
| EOF | |
| # Create certificate with SAN | |
| openssl x509 -req -days $DAYS_VALID -in $OUTPUT_DIR/$DOMAIN.csr -signkey $OUTPUT_DIR/$DOMAIN.key -out $OUTPUT_DIR/${DOMAIN}_with_san.crt -extfile $OUTPUT_DIR/san.cnf -extensions v3_req | |
| echo "Self-signed certificate for $DOMAIN created successfully in $OUTPUT_DIR" | |
| echo "Files created:" | |
| echo " - $DOMAIN.key (Private Key)" | |
| echo " - $DOMAIN.csr (Certificate Signing Request)" | |
| echo " - $DOMAIN.crt (Certificate)" | |
| echo " - $DOMAIN.pem (Combined Certificate and Key)" | |
| echo " - ${DOMAIN}_with_san.crt (Certificate with SAN for www subdomain)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment