renewcerts.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_2048\nProgramming-2048\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_1024\nProgramming-1024\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 server-revoked-key.pem #########
  89. ###########################################################
  90. echo "Updating server-revoked-cert.pem"
  91. echo ""
  92. #pipe the following arguments to openssl req...
  93. echo -e "US\nMontana\nBozeman\nwolfSSL_revoked\nSupport_revoked\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key server-revoked-key.pem -nodes > server-revoked-req.pem
  94. openssl x509 -req -in server-revoked-req.pem -extfile wolfssl.cnf -extensions wolfssl_opts -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 02 > server-revoked-cert.pem
  95. rm server-revoked-req.pem
  96. openssl x509 -in ca-cert.pem -text > ca_tmp.pem
  97. openssl x509 -in server-revoked-cert.pem -text > srv_tmp.pem
  98. mv srv_tmp.pem server-revoked-cert.pem
  99. cat ca_tmp.pem >> server-revoked-cert.pem
  100. rm ca_tmp.pem
  101. ############################################################
  102. ########## update and sign the server-ecc-rsa.pem ##########
  103. ############################################################
  104. echo "Updating server-ecc-rsa.pem"
  105. echo ""
  106. 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
  107. 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
  108. rm server-ecc-req.pem
  109. openssl x509 -in server-ecc-rsa.pem -text > tmp.pem
  110. mv tmp.pem server-ecc-rsa.pem
  111. ############################################################
  112. ####### update the self-signed client-ecc-cert.pem #########
  113. ############################################################
  114. echo "Updating client-ecc-cert.pem"
  115. echo ""
  116. #pipe the following arguments to openssl req...
  117. echo -e "US\nOregon\nSalem\nClient ECC\nFast\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key ecc-client-key.pem -nodes -out client-ecc-cert.csr
  118. 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
  119. rm client-ecc-cert.csr
  120. openssl x509 -in client-ecc-cert.pem -text > tmp.pem
  121. mv tmp.pem client-ecc-cert.pem
  122. ############################################################
  123. ########## update the self-signed server-ecc.pem ###########
  124. ############################################################
  125. echo "Updating server-ecc.pem"
  126. echo ""
  127. #pipe the following arguments to openssl req...
  128. echo -e "US\nWashington\nSeattle\nEliptic\nECC\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key ecc-key.pem -nodes -out server-ecc.csr
  129. openssl x509 -req -in server-ecc.csr -days 1000 -extfile wolfssl.cnf -extensions wolfssl_opts -signkey ecc-key.pem -out server-ecc.pem
  130. rm server-ecc.csr
  131. openssl x509 -in server-ecc.pem -text > tmp.pem
  132. mv tmp.pem server-ecc.pem
  133. ############################################################
  134. ###### update the self-signed server-ecc-comp.pem ##########
  135. ############################################################
  136. echo "Updating server-ecc-comp.pem"
  137. echo ""
  138. #pipe the following arguments to openssl req...
  139. echo -e "US\nMontana\nBozeman\nElliptic - comp\nServer ECC-comp\nwww.wolfssl.com\ninfo@wolfssl.com\n.\n.\n" | openssl req -new -key ecc-key-comp.pem -nodes -out server-ecc-comp.csr
  140. 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
  141. rm server-ecc-comp.csr
  142. openssl x509 -in server-ecc-comp.pem -text > tmp.pem
  143. mv tmp.pem server-ecc-comp.pem
  144. ############################################################
  145. ########## make .der files from .pem files #################
  146. ############################################################
  147. openssl x509 -inform PEM -in \1024/client-cert.pem -outform DER -out \1024/client-cert.der
  148. echo "Generating new ca-cert.der, client-cert.der, server-cert.der..."
  149. echo ""
  150. openssl x509 -inform PEM -in ca-cert.pem -outform DER -out ca-cert.der
  151. openssl x509 -inform PEM -in client-cert.pem -outform DER -out client-cert.der
  152. openssl x509 -inform PEM -in server-cert.pem -outform DER -out server-cert.der
  153. openssl x509 -inform PEM -in client-ecc-cert.pem -outform DER -out client-ecc-cert.der
  154. echo "Changing directory to wolfssl root..."
  155. echo ""
  156. cd ../
  157. echo "Execute ./gencertbuf.pl..."
  158. echo ""
  159. ./gencertbuf.pl
  160. ############################################################
  161. ########## generate the new crls ###########################
  162. ############################################################
  163. echo "Change directory to wolfssl/certs"
  164. echo ""
  165. cd certs
  166. echo "We are back in the certs directory"
  167. echo ""
  168. echo "Updating the crls..."
  169. echo ""
  170. cd crl
  171. echo "changed directory: cd/crl"
  172. echo ""
  173. ./gencrls.sh
  174. echo "ran ./gencrls.sh"
  175. echo ""
  176. #cleanup the file system now that we're done
  177. echo "Performing final steps, cleaning up the file system..."
  178. echo ""
  179. rm ../wolfssl.cnf
  180. }
  181. #function for restoring a previous configure state
  182. function restore_config(){
  183. mv tmp.status config.status
  184. mv tmp.options.h wolfssl/options.h
  185. make clean
  186. make -j 8
  187. }
  188. #function for copy and pasting ntru updates
  189. function move_ntru(){
  190. cp ntru-cert.pem certs/ntru-cert.pem
  191. cp ntru-key.raw certs/ntru-key.raw
  192. }
  193. ###############################################################################
  194. ##################### THE EXECUTABLE BODY #####################################
  195. ###############################################################################
  196. #start in root.
  197. cd ../
  198. #if HAVE_NTRU already defined && there is no argument
  199. if grep HAVE_NTRU "wolfssl/options.h" && [ -z "$1" ]
  200. then
  201. #run the function to renew the certs
  202. run_renewcerts
  203. # run_renewcerts will end in the wolfssl/certs/crl dir, backup to root.
  204. cd ../../
  205. echo "changed directory to wolfssl root directory."
  206. echo ""
  207. ############################################################
  208. ########## update ntru if already installed ################
  209. ############################################################
  210. # We cannot assume that user has certgen and keygen enabled
  211. ./configure --with-ntru --enable-certgen --enable-keygen
  212. make check
  213. #copy/paste ntru-certs and key to certs/
  214. move_ntru
  215. #else if there was an argument given, check it for validity or print out error
  216. elif [ ! -z "$1" ]; then
  217. #valid argument then renew certs without ntru
  218. if [ "$1" == "--override-ntru" ]; then
  219. echo "overriding ntru, update all certs except ntru."
  220. run_renewcerts
  221. #valid argument print out other valid arguments
  222. elif [ "$1" == "-h" ] || [ "$1" == "-help" ]; then
  223. echo ""
  224. echo "\"no argument\" will attempt to update all certificates"
  225. echo "--override-ntru updates all certificates except ntru"
  226. echo "-h or -help display this menu"
  227. echo ""
  228. echo ""
  229. #else the argument was invalid, tell user to use -h or -help
  230. else
  231. echo ""
  232. echo "That is not a valid option."
  233. echo ""
  234. echo "use -h or -help for a list of available options."
  235. echo ""
  236. fi
  237. #else HAVE_NTRU not already defined
  238. else
  239. echo "Saving the configure state"
  240. echo ""
  241. cp config.status tmp.status
  242. cp wolfssl/options.h tmp.options.h
  243. echo "Running make clean"
  244. echo ""
  245. make clean
  246. #attempt to define ntru by configuring with ntru
  247. echo "Configuring with ntru, enabling certgen and keygen"
  248. echo ""
  249. ./configure --with-ntru --enable-certgen --enable-keygen
  250. make check
  251. # check options.h a second time, if the user had
  252. # ntru installed on their system and in the default
  253. # path location, then it will now be defined, if the
  254. # user does not have ntru on their system this will fail
  255. # again and we will not update any certs until user installs
  256. # ntru in the default location
  257. # if now defined
  258. if grep HAVE_NTRU "wolfssl/options.h"; then
  259. run_renewcerts
  260. #run_renewcerts leaves us in wolfssl/certs/crl, backup to root
  261. cd ../../
  262. echo "changed directory to wolfssl root directory."
  263. echo ""
  264. move_ntru
  265. echo "ntru-certs, and ntru-key.raw have been updated"
  266. echo ""
  267. # restore previous configure state
  268. restore_config
  269. else
  270. # restore previous configure state
  271. restore_config
  272. echo ""
  273. echo "ntru is not installed at the default location,"
  274. echo "or ntru not installed, none of the certs were updated."
  275. echo ""
  276. echo "clone the ntru repository into your \"cd ~\" directory then,"
  277. echo "\"cd NTRUEncrypt\" and run \"make\" then \"make install\""
  278. echo "once complete run this script again to update all the certs."
  279. echo ""
  280. echo "To update all certs except ntru use \"./renewcerts.sh --override-ntru\""
  281. echo ""
  282. fi #END now defined
  283. fi #END already defined