Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Creates self-signed CA certificates and certificates for local domains.
  4. #
  5. # Prompts for a local domain name (e.g. my-app.localhost) and creates all
  6. # necessary certificates.
  7. #
  8. # Next steps:
  9. # Copy the certificates (e.g. my-app.localhost.crt and my-app.localhost.key) to
  10. # your service (Nginx, Apache, ...) and configure it.
  11. # Import the CA certificates in your browsers settings (e.g. my-app.localhost.rootCA.crt).
  12.  
  13. # Your country code
  14. COUNTRY=DE
  15. # Your state
  16. STATE=Berlin
  17. # Your organization. This will appear in the list of trusted CAs in your browser.
  18. ORGANIZATION=DCD
  19.  
  20. # Check if openssl is installed
  21. if [ ! -x "$(command -v openssl)" ]; then
  22. echo 'Error: openssl is not installed.' >&2
  23. exit 1
  24. fi
  25.  
  26. read -p "Please enter the local domain name: " DOMAIN
  27.  
  28. # Check if the root CA file is already created
  29. CANAME="rootCA"
  30. if [ ! -f "$CANAME.crt" ]; then
  31. echo "CA file \"$CANAME.crt\" does not exist. Create root key and certificate..."
  32. openssl genrsa -out $CANAME.key 4096 # or with pw protection: openssl genrsa -des3 -out $CANAME.key 4096
  33. openssl req -x509 -new -nodes -subj "/C=$COUNTRY/ST=$STATE/O=$ORGANIZATION/CN=$ORGANIZATION" -key $CANAME.key -sha256 -days 1024 -out $CANAME.crt
  34. fi
  35.  
  36. # Create Certificates
  37. echo "Create file $DOMAIN.key..."
  38. openssl genrsa -out $DOMAIN.key 2048
  39.  
  40. echo "Create file $DOMAIN.csr..."
  41. openssl req -new -sha256 -key $DOMAIN.key -subj "/C=$COUNTRY/ST=$STATE/O=$ORGANIZATION/CN=$DOMAIN" -out $DOMAIN.csr
  42.  
  43. echo "Create and sign file $DOMAIN.crt..."
  44. # Create config file
  45. cat >$DOMAIN.v3.ext<<EOF
  46. authorityKeyIdentifier=keyid,issuer
  47. basicConstraints=CA:FALSE
  48. keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  49. subjectAltName = @alt_names
  50. [alt_names]
  51. DNS.1 = $DOMAIN
  52. EOF
  53.  
  54. # Create and sign certificate (valid for 500 days)
  55. openssl x509 -req -in $DOMAIN.csr -CA $CANAME.crt -CAkey $CANAME.key -CAcreateserial -out $DOMAIN.crt -days 1024 -sha256 -extfile $DOMAIN.v3.ext
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement