renewcerts.sh 13 KB

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