generate_certs.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/bash
  2. # This script generates certificates that can be used to test SSL client
  3. # authentication.
  4. #
  5. # 1. A (end-entity) -> B -> C (self-signed root)
  6. # 2. D (end-entity) -> B -> C (self-signed root)
  7. try () {
  8. echo "$@"
  9. "$@" || exit 1
  10. }
  11. try mkdir out
  12. echo Create the serial number files and indices.
  13. serial=1000
  14. for i in B C
  15. do
  16. try /bin/sh -c "echo $serial > out/$i-serial"
  17. serial=$(expr $serial + 1)
  18. touch out/$i-index.txt
  19. touch out/$i-index.txt.attr
  20. done
  21. echo Generate the keys.
  22. for i in A B C D
  23. do
  24. try openssl genrsa -out out/$i.key 2048
  25. done
  26. echo Generate the C CSR
  27. COMMON_NAME="Root CA" \
  28. CA_DIR=out \
  29. ID=C \
  30. try openssl req \
  31. -new \
  32. -key out/C.key \
  33. -out out/C.csr \
  34. -config certs.cnf
  35. echo C signs itself.
  36. COMMON_NAME="Root CA" \
  37. CA_DIR=out \
  38. ID=C \
  39. try openssl x509 \
  40. -req -days 3650 \
  41. -in out/C.csr \
  42. -extensions ca_cert \
  43. -extfile certs.cnf \
  44. -signkey out/C.key \
  45. -out out/C.pem
  46. echo Generate the intermediates
  47. COMMON_NAME="Intermediate CA" \
  48. CA_DIR=out \
  49. ID=B \
  50. try openssl req \
  51. -new \
  52. -key out/B.key \
  53. -out out/B.csr \
  54. -config certs.cnf
  55. COMMON_NAME="Root CA" \
  56. CA_DIR=out \
  57. ID=C \
  58. try openssl ca \
  59. -batch \
  60. -extensions ca_intermediate_cert \
  61. -in out/B.csr \
  62. -out out/B.pem \
  63. -config certs.cnf
  64. echo Generate the leaf certs
  65. COMMON_NAME="Client Cert" \
  66. ID=A \
  67. try openssl req \
  68. -new \
  69. -key out/A.key \
  70. -out out/A.csr \
  71. -config certs.cnf
  72. echo B signs A
  73. COMMON_NAME="Intermediate CA" \
  74. CA_DIR=out \
  75. ID=B \
  76. try openssl ca \
  77. -batch \
  78. -extensions user_cert \
  79. -in out/A.csr \
  80. -out out/A.pem \
  81. -config certs.cnf
  82. COMMON_NAME="localhost" \
  83. ID=D \
  84. try openssl req \
  85. -new \
  86. -key out/D.key \
  87. -out out/D.csr \
  88. -config certs.cnf
  89. echo B signs D
  90. COMMON_NAME="Intermediate CA" \
  91. CA_DIR=out \
  92. ID=B \
  93. try openssl ca \
  94. -batch \
  95. -extensions server_cert \
  96. -in out/D.csr \
  97. -out out/D.pem \
  98. -config certs.cnf
  99. echo Package the client cert and private key into PKCS12 file
  100. try /bin/sh -c "cat out/A.pem out/A.key out/B.pem out/C.pem > out/A-chain.pem"
  101. try openssl pkcs12 \
  102. -in out/A-chain.pem \
  103. -out client.p12 \
  104. -export \
  105. -passout pass:electron
  106. echo Package the certs
  107. try cp out/C.pem rootCA.pem
  108. try cp out/B.pem intermediateCA.pem
  109. try cp out/D.key server.key
  110. try cp out/D.pem server.pem
  111. try rm -rf out