certs.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/sh
  2. #
  3. # Creates the CA, server and client certs to be used by tls_test.go
  4. # http://www.rabbitmq.com/ssl.html
  5. #
  6. # Copy stdout into the const section of tls_test.go or use for RabbitMQ
  7. #
  8. root=$PWD/certs
  9. if [ -f $root/ca/serial ]; then
  10. echo >&2 "Previous installation found"
  11. echo >&2 "Remove $root/ca and rerun to overwrite"
  12. exit 1
  13. fi
  14. mkdir -p $root/ca/private
  15. mkdir -p $root/ca/certs
  16. mkdir -p $root/server
  17. mkdir -p $root/client
  18. cd $root/ca
  19. chmod 700 private
  20. touch index.txt
  21. echo 'unique_subject = no' > index.txt.attr
  22. echo '01' > serial
  23. echo >openssl.cnf '
  24. [ ca ]
  25. default_ca = testca
  26. [ testca ]
  27. dir = .
  28. certificate = $dir/cacert.pem
  29. database = $dir/index.txt
  30. new_certs_dir = $dir/certs
  31. private_key = $dir/private/cakey.pem
  32. serial = $dir/serial
  33. default_crl_days = 7
  34. default_days = 3650
  35. default_md = sha1
  36. policy = testca_policy
  37. x509_extensions = certificate_extensions
  38. [ testca_policy ]
  39. commonName = supplied
  40. stateOrProvinceName = optional
  41. countryName = optional
  42. emailAddress = optional
  43. organizationName = optional
  44. organizationalUnitName = optional
  45. [ certificate_extensions ]
  46. basicConstraints = CA:false
  47. [ req ]
  48. default_bits = 2048
  49. default_keyfile = ./private/cakey.pem
  50. default_md = sha1
  51. prompt = yes
  52. distinguished_name = root_ca_distinguished_name
  53. x509_extensions = root_ca_extensions
  54. [ root_ca_distinguished_name ]
  55. commonName = hostname
  56. [ root_ca_extensions ]
  57. basicConstraints = CA:true
  58. keyUsage = keyCertSign, cRLSign
  59. [ client_ca_extensions ]
  60. basicConstraints = CA:false
  61. keyUsage = digitalSignature
  62. extendedKeyUsage = 1.3.6.1.5.5.7.3.2
  63. [ server_ca_extensions ]
  64. basicConstraints = CA:false
  65. keyUsage = keyEncipherment
  66. extendedKeyUsage = 1.3.6.1.5.5.7.3.1
  67. subjectAltName = @alt_names
  68. [ alt_names ]
  69. IP.1 = 127.0.0.1
  70. '
  71. openssl req \
  72. -x509 \
  73. -nodes \
  74. -config openssl.cnf \
  75. -newkey rsa:2048 \
  76. -days 3650 \
  77. -subj "/CN=MyTestCA/" \
  78. -out cacert.pem \
  79. -outform PEM
  80. openssl x509 \
  81. -in cacert.pem \
  82. -out cacert.cer \
  83. -outform DER
  84. openssl genrsa -out $root/server/key.pem 2048
  85. openssl genrsa -out $root/client/key.pem 2048
  86. openssl req \
  87. -new \
  88. -nodes \
  89. -config openssl.cnf \
  90. -subj "/CN=127.0.0.1/O=server/" \
  91. -key $root/server/key.pem \
  92. -out $root/server/req.pem \
  93. -outform PEM
  94. openssl req \
  95. -new \
  96. -nodes \
  97. -config openssl.cnf \
  98. -subj "/CN=127.0.0.1/O=client/" \
  99. -key $root/client/key.pem \
  100. -out $root/client/req.pem \
  101. -outform PEM
  102. openssl ca \
  103. -config openssl.cnf \
  104. -in $root/server/req.pem \
  105. -out $root/server/cert.pem \
  106. -notext \
  107. -batch \
  108. -extensions server_ca_extensions
  109. openssl ca \
  110. -config openssl.cnf \
  111. -in $root/client/req.pem \
  112. -out $root/client/cert.pem \
  113. -notext \
  114. -batch \
  115. -extensions client_ca_extensions
  116. cat <<-END
  117. const caCert = \`
  118. `cat $root/ca/cacert.pem`
  119. \`
  120. const serverCert = \`
  121. `cat $root/server/cert.pem`
  122. \`
  123. const serverKey = \`
  124. `cat $root/server/key.pem`
  125. \`
  126. const clientCert = \`
  127. `cat $root/client/cert.pem`
  128. \`
  129. const clientKey = \`
  130. `cat $root/client/key.pem`
  131. \`
  132. END