ssl-certificates.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. CADIR=./demoCA
  3. DAYS=$[3*365]
  4. NAME=$1
  5. if [ -d /usr/share/ssl/misc ]; then
  6. # SuSE / RHEL4
  7. if [ -x /usr/share/ssl/misc/CA.pl ]; then
  8. CASCRIPT=/usr/share/ssl/misc/CA.pl
  9. elif [ -x /usr/share/ssl/misc/CA.sh ]; then
  10. CASCRIPT=/usr/share/ssl/misc/CA.sh
  11. elif [ -x /usr/share/ssl/misc/CA ]; then
  12. CASCRIPT=/usr/share/ssl/misc/CA
  13. fi
  14. if [ -f /usr/share/ssl/misc/openssl.cnf ]; then
  15. # RHEL4
  16. CADIR=`grep -w ^dir -m 1 /usr/share/ssl/misc/openssl.cnf | awk {'print $3'}`
  17. elif [ -f /etc/ssl/openssl.cnf ]; then
  18. # SuSE
  19. CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
  20. fi
  21. elif [ -d /usr/lib/ssl/misc ]; then
  22. # Debian / Ubuntu
  23. if [ -x /usr/lib/ssl/misc/CA.pl ]; then
  24. CASCRIPT=/usr/lib/ssl/misc/CA.pl
  25. elif [ -x /usr/lib/ssl/misc/CA.sh ]; then
  26. CASCRIPT=/usr/lib/ssl/misc/CA.sh
  27. elif [ -x /usr/lib/ssl/misc/CA ]; then
  28. CASCRIPT=/usr/lib/ssl/misc/CA
  29. fi
  30. if [ -f /usr/lib/ssl/misc/openssl.cnf ]; then
  31. # --
  32. CADIR=`grep -w ^dir -m 1 /usr/lib/ssl/misc/openssl.cnf | awk {'print $3'}`
  33. elif [ -f /etc/ssl/openssl.cnf ]; then
  34. # Debian / Ubuntu
  35. CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
  36. fi
  37. elif [ -d /etc/pki/tls/misc ]; then
  38. # Fedora Core, RHEL5, RHEL6
  39. if [ -x /etc/pki/tls/misc/CA.pl ]; then
  40. CASCRIPT=/etc/pki/tls/misc/CA.pl
  41. elif [ -x /etc/pki/tls/misc/CA.sh ]; then
  42. CASCRIPT=/etc/pki/tls/misc/CA.sh
  43. elif [ -x /etc/pki/tls/misc/CA ]; then
  44. CASCRIPT=/etc/pki/tls/misc/CA
  45. fi
  46. if [ -f /etc/pki/tls/openssl.cnf ]; then
  47. CADIR=`grep -w ^dir -m 1 /etc/pki/tls/openssl.cnf | awk {'print $3'}`
  48. elif [ -f /etc/ssl/openssl.cnf ]; then
  49. # --
  50. CADIR=`grep -w ^dir -m 1 /etc/ssl/openssl.cnf | awk {'print $3'}`
  51. fi
  52. elif [ -d /var/lib/ssl/misc ]; then
  53. # ALTLinux
  54. if [ -x /var/lib/ssl/misc/CA ]; then
  55. CASCRIPT=/var/lib/ssl/misc/CA
  56. fi
  57. if [ -f /etc/openssl/openssl.cnf ]; then
  58. # ALTLinux
  59. CADIR=`grep -w ^dir -m 1 /etc/openssl/openssl.cnf | awk {'print $3'}`
  60. fi
  61. fi
  62. if [ -z "$CASCRIPT" ]; then
  63. echo "OpenSSL CA script not found. Type script location below, or press enter to exit."
  64. read CASCRIPT
  65. if [ -z "$CASCRIPT" ]; then
  66. exit 0
  67. fi
  68. if [ ! -x "$CASCRIPT" ]; then
  69. echo "Script '$CASCRIPT' does not exist, or is not executable."
  70. exit 1
  71. fi
  72. fi
  73. if [ -z "$NAME" ]; then
  74. while [ -z "$NAME" -o -f "$NAME.pem" ]; do
  75. echo -n "Enter the name of the service: "
  76. read NAME
  77. if [ -z "$NAME" ]; then
  78. echo "No name given."
  79. elif [ -f "$NAME.pem" ]; then
  80. echo "$NAME.pem already exists."
  81. fi
  82. done
  83. fi
  84. set -e
  85. if [ ! -d "$CADIR" -o ! -f "$CADIR/serial" ]; then
  86. echo "No Certificate Authority Root found in current directory."
  87. echo "Press enter to create, or ctrl-c to exit."
  88. read dummy
  89. if [ -d "$CADIR" ]; then
  90. mv $CADIR ${CADIR}-backup.kopano
  91. fi
  92. $CASCRIPT -newca
  93. fi
  94. echo
  95. echo "Now creating service certificate"
  96. echo
  97. # create new local service certificate
  98. openssl req -new -keyout newkey.pem -out newreq.pem -days $DAYS
  99. echo
  100. echo "Signing certificate"
  101. echo
  102. openssl ca -days $DAYS -policy policy_anything -out newcert.pem -infiles newreq.pem
  103. cat newkey.pem newcert.pem > $NAME.pem
  104. chmod 600 $NAME.pem
  105. rm newkey.pem newcert.pem newreq.pem
  106. echo
  107. echo -n "Create public key from this certificate? [y] "
  108. read public
  109. PUBCMD="openssl rsa -in $NAME.pem -out $NAME-public.pem -outform PEM -pubout"
  110. if [ -z "$public" -o "$public" = "y" ]; then
  111. set -- -e
  112. $PUBCMD
  113. if [ $? -ne 0 ]; then
  114. echo
  115. echo "No public key created. Use the following command to create it:"
  116. echo $PUBCMD
  117. fi
  118. else
  119. echo "No public key created. Use the following command to create it:"
  120. echo $PUBCMD
  121. fi