renewcerts.sh 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #!/bin/bash
  2. ###############################################################################
  3. ######################## FUNCTIONS SECTION ####################################
  4. ###############################################################################
  5. #the function that will be called when we are ready to renew the certs.
  6. function run_renewcerts(){
  7. cd certs/
  8. echo ""
  9. #move the custom cnf into our working directory
  10. cp renewcerts/cyassl.cnf cyassl.cnf
  11. # To generate these all in sha1 add the flag "-sha1" on appropriate lines
  12. # That is all lines beginning with: "openssl req"
  13. ############################################################
  14. ########## update the self-signed client-cert.pem ##########
  15. ############################################################
  16. echo "Updating client-cert.pem"
  17. echo ""
  18. #pipe the following arguments to openssl req...
  19. echo -e "US\nMontana\nBozeman\nwolfSSL\nProgramming\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key client-key.pem -nodes -out client-cert.csr
  20. openssl x509 -req -in client-cert.csr -days 1000 -extfile cyassl.cnf -extensions cyassl_opts -signkey client-key.pem -out client-cert.pem
  21. rm client-cert.csr
  22. openssl x509 -in client-cert.pem -text > tmp.pem
  23. mv tmp.pem client-cert.pem
  24. ############################################################
  25. ########## update the self-signed ca-cert.pem ##############
  26. ############################################################
  27. echo "Updating ca-cert.pem"
  28. echo ""
  29. #pipe the following arguments to openssl req...
  30. echo -e "US\nMontana\nBozeman\nSawtooth\nConsulting\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key ca-key.pem -nodes -out ca-cert.csr
  31. openssl x509 -req -in ca-cert.csr -days 1000 -extfile cyassl.cnf -extensions cyassl_opts -signkey ca-key.pem -out ca-cert.pem
  32. rm ca-cert.csr
  33. openssl x509 -in ca-cert.pem -text > tmp.pem
  34. mv tmp.pem ca-cert.pem
  35. ###########################################################
  36. ########## update and sign server-cert.ptm ################
  37. ###########################################################
  38. echo "Updating server-cert.pem"
  39. echo ""
  40. #pipe the following arguments to openssl req...
  41. echo -e "US\nMontana\nBozeman\nwolfSSL\nSupport\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key server-key.pem -nodes > server-req.pem
  42. openssl x509 -req -in server-req.pem -extfile cyassl.cnf -extensions cyassl_opts -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
  43. rm server-req.pem
  44. openssl x509 -in ca-cert.pem -text > ca_tmp.pem
  45. openssl x509 -in server-cert.pem -text > srv_tmp.pem
  46. mv srv_tmp.pem server-cert.pem
  47. cat ca_tmp.pem >> server-cert.pem
  48. rm ca_tmp.pem
  49. ############################################################
  50. ########## update and sign the server-ecc-rsa.pem ##########
  51. ############################################################
  52. echo "Updating server-ecc-rsa.pem"
  53. echo ""
  54. echo -e "US\nMontana\nBozeman\nElliptic - RSAsig\nECC-RSAsig\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key ecc-key.pem -nodes > server-ecc-req.pem
  55. openssl x509 -req -in server-ecc-req.pem -extfile cyassl.cnf -extensions cyassl_opts -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-ecc-rsa.pem
  56. rm server-ecc-req.pem
  57. openssl x509 -in server-ecc-rsa.pem -text > tmp.pem
  58. mv tmp.pem server-ecc-rsa.pem
  59. ############################################################
  60. ########## make .der files from .pem files #################
  61. ############################################################
  62. echo "Generating new ca-cert.der, client-cert.der, server-cert.der..."
  63. echo ""
  64. openssl x509 -inform PEM -in ca-cert.pem -outform DER -out ca-cert.der
  65. openssl x509 -inform PEM -in client-cert.pem -outform DER -out client-cert.der
  66. openssl x509 -inform PEM -in server-cert.pem -outform DER -out server-cert.der
  67. echo "Changing directory to cyassl root..."
  68. echo ""
  69. cd ../
  70. echo "Execute ./gencertbuf.pl..."
  71. echo ""
  72. ./gencertbuf.pl
  73. ############################################################
  74. ########## generate the new crls ###########################
  75. ############################################################
  76. echo "Change directory to cyassl/certs"
  77. echo ""
  78. cd certs
  79. echo "We are back in the certs directory"
  80. echo ""
  81. #set up the file system for updating the crls
  82. echo "setting up the file system for generating the crls..."
  83. echo ""
  84. touch crl/index.txt
  85. touch crl/crlnumber
  86. echo "01" >> crl/crlnumber
  87. touch crl/blank.index.txt
  88. mkdir crl/demoCA
  89. touch crl/demoCA/index.txt
  90. echo "Updating the crls..."
  91. echo ""
  92. cd crl
  93. echo "changed directory: cd/crl"
  94. echo ""
  95. ./gencrls.sh
  96. echo "ran ./gencrls.sh"
  97. echo ""
  98. #cleanup the file system now that we're done
  99. echo "Performing final steps, cleaning up the file system..."
  100. echo ""
  101. rm ../cyassl.cnf
  102. rm blank.index.txt
  103. rm index.*
  104. rm crlnumber*
  105. rm -r demoCA
  106. echo "Removed ../cyassl.cnf, blank.index.txt, index.*, crlnumber*, demoCA/"
  107. echo ""
  108. }
  109. #function for restoring a previous configure state
  110. function restore_config(){
  111. mv tmp.status config.status
  112. mv tmp.options.h cyassl/options.h
  113. make clean
  114. make -j 8
  115. }
  116. #function for copy and pasting ntru updates
  117. function move_ntru(){
  118. cp ntru-cert.pem certs/ntru-cert.pem
  119. cp ntru-key.raw certs/ntru-key.raw
  120. }
  121. ###############################################################################
  122. ##################### THE EXECUTABLE BODY #####################################
  123. ###############################################################################
  124. #start in root.
  125. cd ../
  126. #if HAVE_NTRU already defined && there is no argument
  127. if grep HAVE_NTRU "cyassl/options.h" && [ -z "$1" ]
  128. then
  129. #run the function to renew the certs
  130. run_renewcerts
  131. # run_renewcerts will end in the cyassl/certs/crl dir, backup to root.
  132. cd ../../
  133. echo "changed directory to cyassl root directory."
  134. echo ""
  135. ############################################################
  136. ########## update ntru if already installed ################
  137. ############################################################
  138. # We cannot assume that user has certgen and keygen enabled
  139. ./configure --with-ntru --enable-certgen --enable-keygen
  140. make check
  141. #copy/paste ntru-certs and key to certs/
  142. move_ntru
  143. #else if there was an argument given, check it for validity or print out error
  144. elif [ ! -z "$1" ]; then
  145. #valid argument then renew certs without ntru
  146. if [ "$1" == "--override-ntru" ]; then
  147. echo "overriding ntru, update all certs except ntru."
  148. run_renewcerts
  149. #valid argument print out other valid arguments
  150. elif [ "$1" == "-h" ] || [ "$1" == "-help" ]; then
  151. echo ""
  152. echo "\"no argument\" will attempt to update all certificates"
  153. echo "--override-ntru updates all certificates except ntru"
  154. echo "-h or -help display this menu"
  155. echo ""
  156. echo ""
  157. #else the argument was invalid, tell user to use -h or -help
  158. else
  159. echo ""
  160. echo "That is not a valid option."
  161. echo ""
  162. echo "use -h or -help for a list of available options."
  163. echo ""
  164. fi
  165. #else HAVE_NTRU not already defined
  166. else
  167. echo "Saving the configure state"
  168. echo ""
  169. cp config.status tmp.status
  170. cp cyassl/options.h tmp.options.h
  171. echo "Running make clean"
  172. echo ""
  173. make clean
  174. #attempt to define ntru by configuring with ntru
  175. echo "Configuring with ntru, enabling certgen and keygen"
  176. echo ""
  177. ./configure --with-ntru --enable-certgen --enable-keygen
  178. make check
  179. # check options.h a second time, if the user had
  180. # ntru installed on their system and in the default
  181. # path location, then it will now be defined, if the
  182. # user does not have ntru on their system this will fail
  183. # again and we will not update any certs until user installs
  184. # ntru in the default location
  185. # if now defined
  186. if grep HAVE_NTRU "cyassl/options.h"; then
  187. run_renewcerts
  188. #run_renewcerts leaves us in cyassl/certs/crl, backup to root
  189. cd ../../
  190. echo "changed directory to cyassl root directory."
  191. echo ""
  192. move_ntru
  193. echo "ntru-certs, and ntru-key.raw have been updated"
  194. echo ""
  195. # restore previous configure state
  196. restore_config
  197. else
  198. # restore previous configure state
  199. restore_config
  200. echo ""
  201. echo "ntru is not installed at the default location,"
  202. echo "or ntru not installed, none of the certs were updated."
  203. echo ""
  204. echo "clone the ntru repository into your \"cd ~\" directory then,"
  205. echo "\"cd NTRUEncrypt\" and run \"make\" then \"make install\""
  206. echo "once complete run this script again to update all the certs."
  207. echo ""
  208. echo "To update all certs except ntru use \"./renewcerts.sh --override-ntru\""
  209. echo ""
  210. fi #END now defined
  211. fi #END already defined