create_self_signed_cert.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. # create CA
  3. openssl req -x509 \
  4. -sha256 -days 356 \
  5. -nodes \
  6. -newkey rsa:2048 \
  7. -subj "/CN=MyOwnCA/C=US/L=San Fransisco" \
  8. -keyout rootCA.key -out rootCA.crt
  9. # create server private key
  10. openssl genrsa -out server.key 2048
  11. # create certificate signing request (CSR)
  12. cat > csr.conf <<EOF
  13. [ req ]
  14. default_bits = 2048
  15. prompt = no
  16. default_md = sha256
  17. req_extensions = req_ext
  18. distinguished_name = dn
  19. [ dn ]
  20. C = US
  21. ST = California
  22. L = San Fransisco
  23. O = Someone
  24. OU = Someone
  25. CN = localhost
  26. [ req_ext ]
  27. subjectAltName = @alt_names
  28. [ alt_names ]
  29. DNS.1 = localhost
  30. EOF
  31. openssl req -new -key server.key -out server.csr -config csr.conf
  32. # create server cert
  33. cat > cert.conf <<EOF
  34. authorityKeyIdentifier=keyid,issuer
  35. basicConstraints=CA:FALSE
  36. keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
  37. subjectAltName = @alt_names
  38. [alt_names]
  39. DNS.1 = localhost
  40. EOF
  41. openssl x509 -req \
  42. -in server.csr \
  43. -CA rootCA.crt -CAkey rootCA.key \
  44. -out server.crt \
  45. -days 365 \
  46. -sha256 -extfile cert.conf
  47. # create pkcs12
  48. openssl pkcs12 -export -out identity.pfx -inkey server.key -in server.crt -certfile rootCA.crt -passout pass:1234
  49. # clean up
  50. rm server.csr csr.conf cert.conf