gen.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 1024
  18. openssl req -new -x509 -days 30000 -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 1024
  21. openssl req -new -x509 -days 30000 -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 1024
  24. openssl req -new -x509 -days 30000 -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 1024
  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 -config openssl.cnf -name CA_root -extensions v3_ca -out test-signing-ca.crt -infiles test-signing-ca.csr
  29. # An alternative intermediate CA, signed by the root CA, not used to sign anything.
  30. openssl genrsa -out test-alt-ca.key 1024
  31. openssl req -out test-alt-ca.csr -key test-alt-ca.key -new -config openssl.cnf -subj "${BASESUBJ}/CN=Alternative Signing CA/"
  32. openssl ca -config openssl.cnf -name CA_root -extensions v3_ca -out test-alt-ca.crt -infiles test-alt-ca.csr
  33. # Valid server key and certificate.
  34. openssl genrsa -out server.key 1024
  35. openssl req -new -key server.key -out server.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=localhost/"
  36. openssl ca -config openssl.cnf -name CA_signing -out server.crt -infiles server.csr
  37. # Expired server certificate, based on the above server key.
  38. openssl req -new -days 1 -key server.key -out server-expired.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=localhost/"
  39. openssl ca -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out server-expired.crt -infiles server-expired.csr
  40. # Valid client key and certificate.
  41. openssl genrsa -out client.key 1024
  42. openssl req -new -key client.key -out client.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client/"
  43. openssl ca -config openssl.cnf -name CA_signing -out client.crt -infiles client.csr
  44. # Expired client certificate, based on the above client key.
  45. openssl req -new -days 1 -key client.key -out client-expired.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client expired/"
  46. openssl ca -config openssl.cnf -name CA_signing -days 1 -startdate 120820000000Z -enddate 120821000000Z -out client-expired.crt -infiles client-expired.csr
  47. # Revoked client certificate, based on a new client key.
  48. openssl genrsa -out client-revoked.key 1024
  49. openssl req -new -days 1 -key client-revoked.key -out client-revoked.csr -config openssl.cnf -subj "${SBASESUBJ}/CN=test client revoked/"
  50. openssl ca -config openssl.cnf -name CA_signing -out client-revoked.crt -infiles client-revoked.csr
  51. openssl ca -config openssl.cnf -name CA_signing -revoke client-revoked.crt
  52. openssl ca -config openssl.cnf -name CA_signing -gencrl -out crl.pem
  53. cat test-signing-ca.crt test-root-ca.crt > all-ca.crt
  54. cat client.crt client.key all-ca.crt > client.pem
  55. #mkdir certs
  56. #cp test-signing-ca.crt certs/test-signing-ca.pem
  57. #cp test-root-ca.crt certs/test-root.ca.pem
  58. c_rehash certs