makecert 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. #
  3. # Creates a trust collection certificate (ca.crt)
  4. # and self-signed server certificate (server.crt) and private key (server.pem)
  5. # and client certificate (client.crt) and key file (client.pem) for mutual TLS.
  6. # Replace "example.com" with the host name you'd like for your certificate.
  7. #
  8. # https://github.com/grpc/grpc-java/tree/master/examples
  9. #
  10. set -euo pipefail
  11. SIZE=2048
  12. # CA
  13. openssl genrsa -out ca.key $SIZE
  14. openssl req -new -x509 -days 365 -key ca.key -out ca.crt -subj "/CN=some-ca"
  15. # Other CA
  16. openssl genrsa -out other-ca.key $SIZE
  17. openssl req -new -x509 -days 365 -key other-ca.key -out other-ca.crt -subj "/CN=some-other-ca"
  18. # Server certs (localhost)
  19. openssl genrsa -out server-localhost.key $SIZE
  20. openssl req -new -key server-localhost.key -out server-localhost.csr -subj "/CN=localhost"
  21. openssl x509 -req -days 365 -in server-localhost.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server-localhost.crt
  22. openssl x509 -req -days 365 -in server-localhost.csr -CA other-ca.crt -CAkey other-ca.key -set_serial 01 -out server-localhost-other-ca.crt
  23. # Server certs (example.com)
  24. openssl genrsa -out server-example.com.key $SIZE
  25. openssl req -new -key server-example.com.key -out server-example.com.csr -subj "/CN=example.com"
  26. openssl x509 -req -days 365 -in server-example.com.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out server-example.com.crt
  27. # Client certs (localhost)
  28. openssl genrsa -out client.key $SIZE
  29. openssl req -new -key client.key -out client.csr -subj "/CN=localhost"
  30. openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out client.crt
  31. openssl x509 -req -days 365 -in client.csr -CA other-ca.crt -CAkey other-ca.key -set_serial 01 -out client-other-ca.crt
  32. # netty only supports PKCS8 keys. openssl is used to convert from PKCS1 to PKCS8
  33. # http://netty.io/wiki/sslcontextbuilder-and-private-key.html
  34. openssl pkcs8 -topk8 -nocrypt -in client.key -out client.pem
  35. openssl pkcs8 -topk8 -nocrypt -in server-example.com.key -out server.pem
  36. # Server cert with explicit EC parameters (not supported)
  37. openssl ecparam -name prime256v1 -genkey -param_enc explicit -out server-explicit-ec.key
  38. openssl req -new -x509 -days 365 -key server-explicit-ec.key -out server-explicit-ec.crt -subj "/CN=example.com"