summaryrefslogtreecommitdiff
path: root/localcsr
blob: cffbedd8ae9afe31b1bad360190e56803ad9c1fa (plain)
  1. #!/bin/sh
  2. # csr.sh: Certificate Signing Request Generator
  3. # Copyright(c) 2005 Evaldo Gardenali <evaldo@gardenali.biz>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. # 1. Redistributions of source code must retain the above copyright
  10. # notice, this list of conditions and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. # notice, this list of conditions and the following disclaimer in the
  13. # documentation and/or other materials provided with the distribution.
  14. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "ASIS"
  15. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. # POSSIBILITY OF SUCH DAMAGE.
  25. #
  26. # ChangeLog:
  27. # Mon May 23 00:14:37 BRT 2005 - evaldo - Initial Release
  28. # Wed May 3 12:09:24 UTC 2006 - jonas - Drop $HOME, use current workdir
  29. # be safe about permissions
  30. LASTUMASK=`umask`
  31. umask 077
  32. # OpenSSL for HPUX needs a random file
  33. RANDOMFILE=$HOME/.rnd
  34. # create a config file for openssl
  35. CONFIG=`mktemp -q /tmp/openssl-conf.XXXXXXXX`
  36. if [ ! $? -eq 0 ]; then
  37. echo "Could not create temporary config file. exiting"
  38. exit 1
  39. fi
  40. echo "Private Key and Certificate Signing Request Generator"
  41. echo "This script was designed to suit the request format needed by"
  42. echo "the CAcert Certificate Authority. www.CAcert.org"
  43. echo
  44. printf "Short Hostname (ie. imap big_srv www2): "
  45. read HOST
  46. printf "FQDN/CommonName (ie. www.example.com) : "
  47. read COMMONNAME
  48. echo "Type SubjectAltNames for the certificate, one per line. Enter a blank line to finish"
  49. SAN=1 # bogus value to begin the loop
  50. SANAMES="" # sanitize
  51. while [ ! "$SAN" = "" ]; do
  52. printf "SubjectAltName: DNS:"
  53. read SAN
  54. if [ "$SAN" = "" ]; then break; fi # end of input
  55. if [ "$SANAMES" = "" ]; then
  56. SANAMES="DNS:$SAN"
  57. else
  58. SANAMES="$SANAMES,DNS:$SAN"
  59. fi
  60. done
  61. # Config File Generation
  62. cat <<EOF > $CONFIG
  63. # -------------- BEGIN custom openssl.cnf -----
  64. EOF
  65. if [ "`uname -s`" = "HP-UX" ]; then
  66. echo " RANDFILE = $RANDOMFILE" >> $CONFIG
  67. fi
  68. cat <<EOF >> $CONFIG
  69. oid_section = new_oids
  70. [ new_oids ]
  71. [ req ]
  72. default_days = 730 # how long to certify for
  73. default_keyfile = ${HOST}_privatekey.pem
  74. distinguished_name = req_distinguished_name
  75. encrypt_key = no
  76. string_mask = nombstr
  77. EOF
  78. if [ ! "$SANAMES" = "" ]; then
  79. echo "req_extensions = v3_req # Extensions to add to certificate request" >> $CONFIG
  80. fi
  81. cat <<EOF >> $CONFIG
  82. [ req_distinguished_name ]
  83. commonName = Common Name (eg, YOUR name)
  84. commonName_default = $COMMONNAME
  85. commonName_max = 64
  86. [ v3_req ]
  87. EOF
  88. if [ ! "$SANAMES" = "" ]; then
  89. echo "subjectAltName=$SANAMES" >> $CONFIG
  90. fi
  91. echo "# -------------- END custom openssl.cnf -----" >> $CONFIG
  92. echo "Running OpenSSL..."
  93. openssl req -batch -config $CONFIG -newkey rsa:2048 -out ${HOST}_csr.pem
  94. echo "Copy the following Certificate Request and paste into CAcert website to obtain a Certificate."
  95. echo "When you receive your certificate, you 'should' name it something like ${HOST}_server.pem"
  96. echo
  97. cat ${HOST}_csr.pem
  98. echo
  99. echo The Certificate request is also available in ${HOST}_csr.pem
  100. echo The Private Key is stored in ${HOST}_privatekey.pem
  101. echo
  102. rm $CONFIG
  103. #restore umask
  104. umask $LASTUMASK