Last active
June 21, 2023 20:45
-
-
Save 0xBigBoss/61c68ca50fddac5aaad32a5e0b1a413d to your computer and use it in GitHub Desktop.
Generate a local self-signed certificate for example.com and *.example.com.
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 | |
# Set the domain name | |
domain="example.com" | |
wildcard_domain="*.example.com" | |
# Check if the certificate and key files already exist | |
if [[ -f "${domain}.crt" && -f "${domain}.key" ]]; then | |
echo "Certificate and key files already exist. Exiting..." | |
exit 0 | |
fi | |
# Set the OpenSSL configuration file | |
openssl_conf="[req] | |
distinguished_name=req_distinguished_name | |
x509_extensions=v3_req | |
prompt=no | |
[req_distinguished_name] | |
CN=$domain | |
[v3_req] | |
keyUsage=digitalSignature, keyEncipherment | |
extendedKeyUsage=serverAuth | |
subjectKeyIdentifier=hash | |
authorityKeyIdentifier=keyid,issuer | |
subjectAltName=@alt_names | |
[alt_names] | |
DNS.1=$domain | |
DNS.2=$wildcard_domain" | |
# Export the OpenSSL configuration to a file | |
echo "$openssl_conf" >openssl.cnf | |
# Generate a private key | |
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out "${domain}.key" | |
# Generate a self-signed wildcard certificate | |
openssl req -new -x509 -sha256 -key "${domain}.key" -out "${domain}.crt" -days 365 -config openssl.cnf | |
# Clean up | |
rm openssl.cnf |
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
#!/usr/bin/env python3 | |
import http.server | |
import ssl | |
# Set the domain name | |
domain = "example.com" | |
# Set the server address and port | |
address = "localhost" | |
port = 4443 | |
# Create an HTTP server | |
httpd = http.server.HTTPServer((address, port), http.server.SimpleHTTPRequestHandler) | |
# Configure SSL | |
httpd.socket = ssl.wrap_socket(httpd.socket, | |
server_side=True, | |
certfile=f"{domain}.crt", | |
keyfile=f"{domain}.key", | |
ssl_version=ssl.PROTOCOL_TLS) | |
# Start the HTTPS server | |
print(f"Serving HTTPS on {address} port {port}...") | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment