summaryrefslogtreecommitdiff
path: root/localcsr
blob: a3e5daaa3e5780bd657111c2be963827738f011f (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. EOF
  64. if [ "`uname -s`" = "HP-UX" ]; then
  65. echo " RANDFILE = $RANDOMFILE" >> $CONFIG
  66. fi
  67. cat <<EOF >> $CONFIG
  68. oid_section = new_oids
  69. [ new_oids ]
  70. [ req ]
  71. default_days = 730 # how long to certify for
  72. default_keyfile = ${HOST}_privatekey.pem
  73. distinguished_name = req_distinguished_name
  74. encrypt_key = no
  75. string_mask = nombstr
  76. EOF
  77. if [ ! "$SANAMES" = "" ]; then
  78. echo "req_extensions = v3_req # Extensions to add to certificate request" >> $CONFIG
  79. fi
  80. cat <<EOF >> $CONFIG
  81. [ req_distinguished_name ]
  82. commonName = Common Name (eg, YOUR name)
  83. commonName_default = $COMMONNAME
  84. commonName_max = 64
  85. [ v3_req ]
  86. EOF
  87. if [ ! "$SANAMES" = "" ]; then
  88. echo "subjectAltName=$SANAMES" >> $CONFIG
  89. fi
  90. echo "# -------------- END custom openssl.cnf -----" >> $CONFIG
  91. echo "Running OpenSSL..."
  92. openssl req -batch -config $CONFIG -newkey rsa:2048 -out ${HOST}_csr.pem
  93. echo "Copy the following Certificate Request and paste into CAcert website to obtain a Certificate."
  94. echo "When you receive your certificate, you 'should' name it something like ${HOST}_server.pem"
  95. echo
  96. cat ${HOST}_csr.pem
  97. echo
  98. echo The Certificate request is also available in ${HOST}_csr.pem
  99. echo The Private Key is stored in ${HOST}_privatekey.pem
  100. echo
  101. rm $CONFIG
  102. #restore umask
  103. umask $LASTUMASK