gen.sh 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # This file generates the keys and certificates used for testing mosquitto.
  2. # None of the keys are encrypted, so do not just use this script to generate
  3. # files for your own use.
  4. rm -f *.crt *.key *.csr
  5. for a in root signing; do
  6. rm -rf ${a}CA/
  7. mkdir -p ${a}CA/newcerts
  8. touch ${a}CA/index.txt
  9. echo 01 > ${a}CA/serial
  10. echo 01 > ${a}CA/crlnumber
  11. done
  12. rm -rf certs
  13. BASESUBJ="/C=GB/ST=Derbyshire/L=Derby/O=Mosquitto Project/OU=Testing"
  14. SBASESUBJ="/C=GB/ST=Nottinghamshire/L=Nottingham/O=Server/OU=Production"
  15. BBASESUBJ="/C=GB/ST=Nottinghamshire/L=Nottingham/O=Server/OU=Bridge"
  16. # The root CA
  17. openssl genrsa -out test-root-ca.key 2048
  18. openssl req -new -x509 -days 3650 -key test-root-ca.key -out test-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Root CA/"
  19. # Another root CA that doesn't sign anything
  20. openssl genrsa -out test-bad-root-ca.key 2048
  21. openssl req -new -x509 -days 3650 -key test-bad-root-ca.key -out test-bad-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Bad Root CA/"
  22. # This is a root CA that has the exact same details as the real root CA, but is a different key and certificate. Effectively a "fake" CA.
  23. openssl genrsa -out test-fake-root-ca.key 2048
  24. openssl req -new -x509 -days 3650 -key test-fake-root-ca.key -out test-fake-root-ca.crt -config openssl.cnf -subj "${BASESUBJ}/CN=Root CA/"
  25. # An intermediate CA, signed by the root CA, used to sign server/client csrs.
  26. openssl genrsa -out test-signing-ca.key 2048
  27. openssl req -out test-signing-ca.csr -key test-signing-ca.key -new -config openssl.cnf -subj "${BASESUBJ}/CN=Signing CA/"
  28. openssl ca -batch -config openssl.cnf -name CA_root -extensions v3_ca -out test-signing-ca.crt -infiles test-signing-ca.csr
  29. rm -f test-signing-ca.csr
  30. # An alternative intermediate CA, signed by the root CA, not used to sign anything.
  31. openssl genrsa -out test-alt-ca.key 2048
  32. openssl req -out test-alt-ca.csr -key test-alt-ca.key -new -config openssl.cnf -subj "${BASESUBJ}/CN=Alternative Signing CA/"
  33. openssl ca -batch -config openssl.cnf -name CA_root -extensions v3_ca -out test-alt-ca.crt -infiles test-alt-ca.csr
  34. rm -f test-alt-ca.csr
  35. # Valid server key and certificate.
  36. openssl genrsa -out server.key 2048
  37. openssl req -new -key server.key -out server.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=localhost/"
  38. openssl ca -batch -config openssl.cnf -name CA_signing -out server.crt -infiles server.csr
  39. rm -f server.csr
  40. # Expired server certificate
  41. openssl genrsa -out server-expired.key 2048
  42. openssl req -new -key server-expired.key -out server-expired.csr -config openssl.cnf -subj "${SBASESUBJ}-expired/CN=localhost/"
  43. openssl ca -batch -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out server-expired.crt -infiles server-expired.csr
  44. rm -f server-expired.csr
  45. # Valid client key and certificate.
  46. openssl genrsa -out client.key 2048
  47. openssl req -new -key client.key -out client.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client/"
  48. openssl ca -batch -config openssl.cnf -name CA_signing -out client.crt -infiles client.csr
  49. rm -f client.csr
  50. # Expired client certificate
  51. openssl genrsa -out client-expired.key 2048
  52. openssl req -new -key client-expired.key -out client-expired.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client expired/"
  53. openssl ca -batch -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out client-expired.crt -infiles client-expired.csr
  54. rm -f client-expired.csr
  55. # Empty CRL file
  56. openssl ca -batch -config openssl.cnf -name CA_signing -gencrl -out crl-empty.pem
  57. # Revoked client certificate
  58. openssl genrsa -out client-revoked.key 2048
  59. openssl req -new -key client-revoked.key -out client-revoked.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client revoked/"
  60. openssl ca -batch -config openssl.cnf -name CA_signing -out client-revoked.crt -infiles client-revoked.csr
  61. openssl ca -batch -config openssl.cnf -name CA_signing -revoke client-revoked.crt
  62. openssl ca -batch -config openssl.cnf -name CA_signing -gencrl -out crl.pem
  63. rm -f client-revoked.csr
  64. # Valid client key and certificate, encrypted (use "password" as password)
  65. openssl genrsa -des3 -out client-encrypted.key -passout pass:password 2048
  66. openssl req -new -key client-encrypted.key -out client-encrypted.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client encrypted/" -passin pass:password
  67. openssl ca -batch -config openssl.cnf -name CA_signing -out client-encrypted.crt -infiles client-encrypted.csr
  68. rm -f client-encrypted.csr
  69. cat test-signing-ca.crt test-root-ca.crt > all-ca.crt
  70. #mkdir certs
  71. #cp test-signing-ca.crt certs/test-signing-ca.pem
  72. #cp test-root-ca.crt certs/test-root.ca.pem
  73. #openssl rehash certs