gnunet-gns-proxy-setup-ca.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #!/bin/sh
  2. #
  3. # This shell script will generate an X509 certificate for
  4. # your gnunet-gns-proxy and install it (for both GNUnet
  5. # and your browser).
  6. #
  7. # TODO: Implement support for more browsers
  8. # TODO: Debug and switch to the new version
  9. # TODO - The only remaining task is fixing the getopts
  10. # TODO: Error checks
  11. #
  12. # The current version partially reuses and recycles
  13. # code from build.sh by NetBSD (although not entirely
  14. # used because it needs debugging):
  15. #
  16. # Copyright (c) 2001-2011 The NetBSD Foundation, Inc.
  17. # All rights reserved.
  18. #
  19. # This code is derived from software contributed to
  20. # The NetBSD Foundation by Todd Vierling and Luke Mewburn.
  21. #
  22. # Redistribution and use in source and binary forms, with or
  23. # without modification, are permitted provided that the following
  24. # conditions are met:
  25. # 1. Redistributions of source code must retain the above
  26. # copyright notice, this list of conditions and the following
  27. # disclaimer.
  28. # 2. Redistributions in binary form must reproduce the above
  29. # copyright notice, this list of conditions and the following
  30. # disclaimer in the documentation and/or other materials
  31. # provided with the distribution.
  32. #
  33. # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
  34. # CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  35. # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  36. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. # DISCLAIMED.
  38. # IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR
  39. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  40. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  41. # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. # LIABILITY, OR TORT
  45. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  46. # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  47. # OF SUCH DAMAGE.
  48. dir=$(dirname "$0")
  49. if test -e @PKGDATADIRECTORY@/progname.sh
  50. then
  51. . @PKGDATADIRECTORY@/progname.sh
  52. else
  53. . $dir/../../contrib/build-common/sh/lib.sh/progname.sh
  54. fi
  55. if test -e @PKGDATADIRECTORY@/existence.sh
  56. then
  57. . @PKGDATADIRECTORY@/existence.sh
  58. else
  59. . $dir/../../contrib/build-common/sh/lib.sh/existence.sh
  60. fi
  61. if test -e @PKGDATADIRECTORY@/msg.sh
  62. then
  63. . @PKGDATADIRECTORY@/msg.sh
  64. else
  65. . $dir/../../contrib/build-common/sh/lib.sh/msg.sh
  66. fi
  67. if test -e @PKGDATADIRECTORY@/version_gnunet.sh
  68. then
  69. . @PKGDATADIRECTORY@/version_gnunet.sh
  70. else
  71. . $dir/../../contrib/build-common/sh/lib.sh/version_gnunet.sh
  72. fi
  73. # Whitespace normalization without depending on shell features:
  74. tab=' '
  75. tab2=' '
  76. nl='
  77. '
  78. setdefaults()
  79. {
  80. verbosity=0
  81. resfile=
  82. results=/dev/null
  83. tmpdir=${TMPDIR:-/tmp}
  84. runcmd=
  85. }
  86. usage()
  87. {
  88. if [ -n "$*" ]; then
  89. echo "${nl}${progname}: $*"
  90. fi
  91. cat <<_usage_
  92. Usage: ${progname} [-hvVto] [-c FILE]
  93. Options:
  94. ${tab}-c FILE Use the configuration file FILE.
  95. ${tab}-h${tab2}${tab2}Print this help message.
  96. ${tab}-o${tab2}${tab2}Display summary of statusmessages
  97. ${tab}-t${tab2}${tab2}Short developer test on binaries
  98. ${tab}-v${tab2}${tab2}Print the version and exit.
  99. ${tab}-V${tab2}${tab2}be verbose
  100. _usage_
  101. exit 1
  102. }
  103. generate_ca()
  104. {
  105. echo ""
  106. infomsg "Generating CA"
  107. TMPDIR=${TMPDIR:-/tmp}
  108. if test -e "$TMPDIR"; then
  109. GNSCERT=`mktemp -t cert.pem.XXXXXXXX` || exit 1
  110. GNSCAKY=`mktemp -t caky.pem.XXXXXXXX` || exit 1
  111. GNSCANO=`mktemp -t cano.pem.XXXXXXXX` || exit 1
  112. else
  113. # This warning is mostly pointless.
  114. warningmsg "You need to export the TMPDIR variable"
  115. fi
  116. # # ------------- gnutls
  117. #
  118. # if ! which certutil > /dev/null
  119. # then
  120. # warningmsg "The 'certutil' command was not found."
  121. # warningmsg "Not importing into browsers."
  122. # warningmsg "For 'certutil' install nss."
  123. # else
  124. # # Generate CA key
  125. # # pkcs#8 password-protects key
  126. # certtool --pkcs8 --generate-privkey --sec-param high --outfile ca-key.pem
  127. # # self-sign the CA to create public certificate
  128. # certtool --generate-self-signed --load-privkey ca-key.pem --template ca.cfg --outfile ca.pem
  129. # ------------- openssl
  130. GNUTLS_CA_TEMPLATE=@PKGDATADIRECTORY@/gnunet-gns-proxy-ca.template
  131. OPENSSLCFG=@PKGDATADIRECTORY@/openssl.cnf
  132. CERTTOOL=""
  133. OPENSSL=0
  134. if test -x $(existence gnunet-certtool)
  135. # if test -z "`gnutls-certtool --version`" > /dev/null
  136. then
  137. # We only support gnutls certtool for now. Treat the grep
  138. # for "gnutls" in the output with extra care, it only matches
  139. # the email address! It is probably safer to run strings(1)
  140. # over certtool for a string matching "gnutls"
  141. if test -z "`certtool --version | grep gnutls`" > /dev/null
  142. then
  143. warningmsg "'gnutls-certtool' or 'certtool' command not found. Trying openssl."
  144. # if test -z "`openssl version`" > /dev/null
  145. if test -x $(existence openssl)
  146. then
  147. OPENSSL=1
  148. else
  149. warningmsg "Install either gnutls certtool or openssl for certificate generation!"
  150. statusmsg "Cleaning up."
  151. rm -f $GNSCAKY $GNSCERT
  152. exit 1
  153. fi
  154. fi
  155. CERTTOOL="certtool"
  156. else
  157. CERTTOOL="gnutls-certtool"
  158. fi
  159. if test -n "${GNUNET_CONFIG_FILE}"; then
  160. GNUNET_CONFIG="-c ${GNUNET_CONFIG_FILE}"
  161. else
  162. GNUNET_CONFIG=""
  163. fi
  164. GNS_CA_CERT_PEM=`gnunet-config ${GNUNET_CONFIG} -s gns-proxy -o PROXY_CACERT -f ${options}`
  165. mkdir -p `dirname $GNS_CA_CERT_PEM`
  166. if test 1 -eq $OPENSSL
  167. then
  168. if test 1 -eq $verbosity; then
  169. openssl req -config $OPENSSLCFG -new -x509 -days 3650 -extensions v3_ca -keyout $GNSCAKY -out $GNSCERT -subj "/C=ZZ/L=World/O=GNU/OU=GNUnet/CN=GNS Proxy CA/emailAddress=bounce@gnunet.org" -passout pass:"GNU Name System"
  170. else
  171. openssl req -config $OPENSSLCFG -new -x509 -days 3650 -extensions v3_ca -keyout $GNSCAKY -out $GNSCERT -subj "/C=ZZ/L=World/O=GNU/OU=GNUnet/CN=GNS Proxy CA/emailAddress=bounce@gnunet.org" -passout pass:"GNU Name System" >/dev/null 2>&1
  172. fi
  173. infomsg "Removing passphrase from key"
  174. if test 1 -eq $verbosity; then
  175. openssl rsa -passin pass:"GNU Name System" -in $GNSCAKY -out $GNSCANO
  176. else
  177. openssl rsa -passin pass:"GNU Name System" -in $GNSCAKY -out $GNSCANO >/dev/null 2>&1
  178. fi
  179. cat $GNSCERT $GNSCANO > $GNS_CA_CERT_PEM
  180. else
  181. if test 1 -eq $verbosity; then
  182. $CERTTOOL --generate-privkey --outfile $GNSCAKY
  183. $CERTTOOL --template $GNUTLS_CA_TEMPLATE --generate-self-signed --load-privkey $GNSCAKY --outfile $GNSCERT
  184. else
  185. $CERTTOOL --generate-privkey --outfile $GNSCAKY >/dev/null 2>&1
  186. $CERTTOOL --template $GNUTLS_CA_TEMPLATE --generate-self-signed --load-privkey $GNSCAKY --outfile $GNSCERT >/dev/null 2>&1
  187. fi
  188. infomsg "Making private key available to gnunet-gns-proxy"
  189. cat $GNSCERT $GNSCAKY > $GNS_CA_CERT_PEM
  190. fi
  191. }
  192. importbrowsers()
  193. {
  194. # if test -z "`command -v certutil`" > /dev/null 2>&1
  195. if test -x $(existence gnutls-certutil) || test -x $(existence certutil)
  196. then
  197. statusmsg "Importing CA into browsers"
  198. # TODO: Error handling?
  199. for f in ~/.mozilla/firefox/*.*/
  200. do
  201. if [ -d $f ]; then
  202. infomsg "Importing CA into Firefox at $f"
  203. # delete old certificate (if any)
  204. certutil -D -n "GNS Proxy CA" -d "$f" >/dev/null 2>/dev/null
  205. # add new certificate
  206. certutil -A -n "GNS Proxy CA" -t CT,, -d "$f" < $GNSCERT
  207. fi
  208. done
  209. for f in ~/.mozilla/icecat/*.*/
  210. do
  211. if [ -d $f ]; then
  212. infomsg "Importing CA into Icecat at $f"
  213. # delete old certificate (if any)
  214. certutil -D -n "GNS Proxy CA" -d "$f" >/dev/null 2>/dev/null
  215. # add new certificate
  216. certutil -A -n "GNS Proxy CA" -t CT,, -d "$f" < $GNSCERT
  217. fi
  218. done
  219. # TODO: Error handling?
  220. if [ -d ~/.pki/nssdb/ ]; then
  221. statusmsg "Importing CA into Chrome at ~/.pki/nssdb/"
  222. # delete old certificate (if any)
  223. certutil -D -n "GNS Proxy CA" -d ~/.pki/nssdb/ >/dev/null 2>/dev/null
  224. # add new certificate
  225. certutil -A -n "GNS Proxy CA" -t CT,, -d ~/.pki/nssdb/ < $GNSCERT
  226. fi
  227. else
  228. warningmsg "The 'certutil' command was not found."
  229. warningmsg "Not importing into browsers."
  230. warningmsg "For 'certutil' install nss."
  231. fi
  232. }
  233. clean_up()
  234. {
  235. infomsg "Cleaning up."
  236. rm -f $GNSCAKY $GNSCANO $GNSCERT
  237. if test -e $SETUP_TMPDIR
  238. then
  239. rm -rf $SETUP_TMPDIR
  240. fi
  241. linemsg
  242. statusmsg "You can now start gnunet-gns-proxy."
  243. statusmsg "Afterwards, configure your browser "
  244. statusmsg "to use a SOCKS proxy on port 7777. "
  245. linemsg
  246. }
  247. main()
  248. {
  249. setdefaults
  250. while getopts "vhVtoc:" opt; do
  251. case $opt in
  252. v)
  253. print_version
  254. exit 0
  255. ;;
  256. h)
  257. usage
  258. ;;
  259. V)
  260. verbosity=1
  261. ;;
  262. c)
  263. options="$options -c $OPTARG"
  264. infomsg "Using configuration file $OPTARG"
  265. GNUNET_CONFIG_FILE=${OPTARG}
  266. ;;
  267. t)
  268. verbosity=1
  269. infomsg "Running short developer test"
  270. if test -x $(existence openssl); then
  271. openssl version
  272. fi
  273. if test -x $(existence certtool); then
  274. certtool --version
  275. fi
  276. if test -x $(existence gnutls-certtool); then
  277. gnutls-certtool --version
  278. fi
  279. exit 0
  280. ;;
  281. o)
  282. resfile=$(mktemp -t ${progname}.results)
  283. results="${resfile}"
  284. ;;
  285. \?)
  286. echo "Invalid option: -$OPTARG" >&2
  287. usage
  288. ;;
  289. :)
  290. echo "Option -$OPTARG requires an argument." >&2
  291. usage
  292. ;;
  293. esac
  294. done
  295. generate_ca
  296. importbrowsers
  297. if [ -s "${results}" ]; then
  298. echo "===> Summary of results:"
  299. sed -e 's/^===>//;s/^/ /' "${results}"
  300. echo "===> ."
  301. infomsg "Please remove ${results} manually."
  302. fi
  303. clean_up
  304. }
  305. main "$@"