summaryrefslogtreecommitdiff
path: root/localcsr
blob: 9eb24e0e946ce8048b44e2d4b7ef198656bf32c6 (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. # be safe about permissions
  29. LASTUMASK=`umask`
  30. umask 077
  31. # OpenSSL for HPUX needs a random file
  32. RANDOMFILE=$HOME/.rnd
  33. # create a config file for openssl
  34. CONFIG=`mktemp -q /tmp/openssl-conf.XXXXXXXX`
  35. if [ ! $? -eq 0 ]; then
  36. echo "Could not create temporary config file. exiting"
  37. exit 1
  38. fi
  39. echo "Private Key and Certificate Signing Request Generator"
  40. echo "This script was designed to suit the request format needed by"
  41. echo "the CAcert Certificate Authority. www.CAcert.org"
  42. echo
  43. printf "Short Hostname (ie. imap big_srv www2): "
  44. read HOST
  45. printf "FQDN/CommonName (ie. www.example.com) : "
  46. read COMMONNAME
  47. echo "Type SubjectAltNames for the certificate, one per line. Enter a blank line to finish"
  48. SAN=1 # bogus value to begin the loop
  49. SANAMES="" # sanitize
  50. while [ ! "$SAN" = "" ]; do
  51. printf "SubjectAltName: DNS:"
  52. read SAN
  53. if [ "$SAN" = "" ]; then break; fi # end of input
  54. if [ "$SANAMES" = "" ]; then
  55. SANAMES="DNS:$SAN"
  56. else
  57. SANAMES="$SANAMES,DNS:$SAN"
  58. fi
  59. done
  60. # Config File Generation
  61. cat <<EOF > $CONFIG
  62. # -------------- BEGIN custom openssl.cnf -----
  63. HOME = $HOME
  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 = $HOME/${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 $HOME/${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 $HOME/${HOST}_csr.pem
  98. echo
  99. echo The Certificate request is also available in $HOME/${HOST}_csr.pem
  100. echo The Private Key is stored in $HOME/${HOST}_privatekey.pem
  101. echo
  102. rm $CONFIG
  103. #restore umask
  104. umask $LASTUMASK