mkcert.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/sh
  2. # Accept one parameter as output file names
  3. # Generates a self-signed certificate.
  4. # Edit EOF section before running
  5. if [ $# -ne 1 ]
  6. then
  7. echo -e "\nUse with a single name paramater for output files will be generated into current folder.\nAnd don't forget to edit [ req_dn ] section in script.\n"
  8. echo -e "Example:\n\n./mkcert.sh foo\n\n"
  9. exit 1
  10. fi
  11. OPENSSL=openssl
  12. DIR=`pwd`
  13. CONFIG=$1-ssl.config
  14. CERTDIR=$SSLDIR
  15. KEYDIR=$SSLDIR
  16. CERTFILE=$DIR/$1.cert
  17. KEYFILE=$DIR/$1.key
  18. REQFILE=$DIR/$1.req
  19. KEYBITS=1024
  20. DAYS=3650
  21. if [ ! -f $CONFIG ]; then
  22. cat > $CONFIG << EOF
  23. [ req ]
  24. default_bits = 1024
  25. encrypt_key = yes
  26. distinguished_name = req_dn
  27. x509_extensions = cert_type
  28. prompt = no
  29. [ req_dn ]
  30. # country (2 letter code)
  31. C=XY
  32. # State or Province Name (full name)
  33. ST=SomeState
  34. # Locality Name (eg. city)
  35. L=SomeCity
  36. # Organization (eg. company)
  37. O=SomeCompany
  38. # Organizational Unit Name (eg. section)
  39. OU=PisiLinux
  40. # Common Name (*.example.com is also possible)
  41. CN=mail.example.com
  42. # E-mail contact
  43. #emailAddress=admin@example.com
  44. [ cert_type ]
  45. nsCertType = server
  46. EOF
  47. fi
  48. if [ -f $CERTFILE ]; then
  49. echo "$CERTFILE already exists, won't overwrite"
  50. exit 1
  51. fi
  52. if [ -f $KEYFILE ]; then
  53. echo "$KEYFILE already exists, won't overwrite"
  54. exit 1
  55. fi
  56. #Generate key. Use -des3 for password protected key.
  57. $OPENSSL genrsa -out $KEYFILE $KEYBITS
  58. chmod 0600 $KEYFILE
  59. #Unmask password protected key
  60. #mv $KEYFILE $KEYFILE.orig
  61. #$OPENSSL rsa -in $KEYFILE.orig -out $KEYFILE
  62. #Generate request file
  63. $OPENSSL req -new -key $KEYFILE -out $REQFILE -config $CONFIG
  64. #Generate self signed certificate
  65. $OPENSSL x509 -req -days $DAYS -in $REQFILE -signkey $KEYFILE -out $CERTFILE
  66. #Verify
  67. $OPENSSL x509 -subject -fingerprint -noout -in $CERTFILE || exit 2