2
0

CA.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/sh
  2. #
  3. # CA - wrapper around ca to make it easier to use ... basically ca requires
  4. # some setup stuff to be done before you can use it and this makes
  5. # things easier between now and when Eric is convinced to fix it :-)
  6. #
  7. # CA -newca ... will setup the right stuff
  8. # CA -newreq ... will generate a certificate request
  9. # CA -sign ... will sign the generated request and output
  10. #
  11. # At the end of that grab newreq.pem and newcert.pem (one has the key
  12. # and the other the certificate) and cat them together and that is what
  13. # you want/need ... I'll make even this a little cleaner later.
  14. #
  15. #
  16. # 12-Jan-96 tjh Added more things ... including CA -signcert which
  17. # converts a certificate to a request and then signs it.
  18. # 10-Jan-96 eay Fixed a few more bugs and added the SSLEAY_CONFIG
  19. # environment variable so this can be driven from
  20. # a script.
  21. # 25-Jul-96 eay Cleaned up filenames some more.
  22. # 11-Jun-96 eay Fixed a few filename missmatches.
  23. # 03-May-96 eay Modified to use 'ssleay cmd' instead of 'cmd'.
  24. # 18-Apr-96 tjh Original hacking
  25. #
  26. # Tim Hudson
  27. # tjh@cryptsoft.com
  28. #
  29. # default openssl.cnf file has setup as per the following
  30. # demoCA ... where everything is stored
  31. cp_pem() {
  32. infile=$1
  33. outfile=$2
  34. bound=$3
  35. flag=0
  36. exec <$infile;
  37. while read line; do
  38. if [ $flag -eq 1 ]; then
  39. echo $line|grep "^-----END.*$bound" 2>/dev/null 1>/dev/null
  40. if [ $? -eq 0 ] ; then
  41. echo $line >>$outfile
  42. break
  43. else
  44. echo $line >>$outfile
  45. fi
  46. fi
  47. echo $line|grep "^-----BEGIN.*$bound" 2>/dev/null 1>/dev/null
  48. if [ $? -eq 0 ]; then
  49. echo $line >$outfile
  50. flag=1
  51. fi
  52. done
  53. }
  54. usage() {
  55. echo "usage: $0 -newcert|-newreq|-newreq-nodes|-newca|-sign|-verify" >&2
  56. }
  57. if [ -z "$OPENSSL" ]; then OPENSSL=openssl; fi
  58. if [ -z "$DAYS" ] ; then DAYS="-days 365" ; fi # 1 year
  59. CADAYS="-days 1095" # 3 years
  60. REQ="$OPENSSL req $SSLEAY_CONFIG"
  61. CA="$OPENSSL ca $SSLEAY_CONFIG"
  62. VERIFY="$OPENSSL verify"
  63. X509="$OPENSSL x509"
  64. PKCS12="openssl pkcs12"
  65. if [ -z "$CATOP" ] ; then CATOP=./demoCA ; fi
  66. CAKEY=./cakey.pem
  67. CAREQ=./careq.pem
  68. CACERT=./cacert.pem
  69. RET=0
  70. while [ "$1" != "" ] ; do
  71. case $1 in
  72. -\?|-h|-help)
  73. usage
  74. exit 0
  75. ;;
  76. -newcert)
  77. # create a certificate
  78. $REQ -new -x509 -keyout newkey.pem -out newcert.pem $DAYS
  79. RET=$?
  80. echo "Certificate is in newcert.pem, private key is in newkey.pem"
  81. ;;
  82. -newreq)
  83. # create a certificate request
  84. $REQ -new -keyout newkey.pem -out newreq.pem $DAYS
  85. RET=$?
  86. echo "Request is in newreq.pem, private key is in newkey.pem"
  87. ;;
  88. -newreq-nodes)
  89. # create a certificate request
  90. $REQ -new -nodes -keyout newreq.pem -out newreq.pem $DAYS
  91. RET=$?
  92. echo "Request (and private key) is in newreq.pem"
  93. ;;
  94. -newca)
  95. # if explicitly asked for or it doesn't exist then setup the directory
  96. # structure that Eric likes to manage things
  97. NEW="1"
  98. if [ "$NEW" -o ! -f ${CATOP}/serial ]; then
  99. # create the directory hierarchy
  100. mkdir -p ${CATOP}
  101. mkdir -p ${CATOP}/certs
  102. mkdir -p ${CATOP}/crl
  103. mkdir -p ${CATOP}/newcerts
  104. mkdir -p ${CATOP}/private
  105. touch ${CATOP}/index.txt
  106. fi
  107. if [ ! -f ${CATOP}/private/$CAKEY ]; then
  108. echo "CA certificate filename (or enter to create)"
  109. read FILE
  110. # ask user for existing CA certificate
  111. if [ "$FILE" ]; then
  112. cp_pem $FILE ${CATOP}/private/$CAKEY PRIVATE
  113. cp_pem $FILE ${CATOP}/$CACERT CERTIFICATE
  114. RET=$?
  115. if [ ! -f "${CATOP}/serial" ]; then
  116. $X509 -in ${CATOP}/$CACERT -noout -next_serial \
  117. -out ${CATOP}/serial
  118. fi
  119. else
  120. echo "Making CA certificate ..."
  121. $REQ -new -keyout ${CATOP}/private/$CAKEY \
  122. -out ${CATOP}/$CAREQ
  123. $CA -create_serial -out ${CATOP}/$CACERT $CADAYS -batch \
  124. -keyfile ${CATOP}/private/$CAKEY -selfsign \
  125. -extensions v3_ca \
  126. -infiles ${CATOP}/$CAREQ
  127. RET=$?
  128. fi
  129. fi
  130. ;;
  131. -xsign)
  132. $CA -policy policy_anything -infiles newreq.pem
  133. RET=$?
  134. ;;
  135. -pkcs12)
  136. if [ -z "$2" ] ; then
  137. CNAME="My Certificate"
  138. else
  139. CNAME="$2"
  140. fi
  141. $PKCS12 -in newcert.pem -inkey newreq.pem -certfile ${CATOP}/$CACERT \
  142. -out newcert.p12 -export -name "$CNAME"
  143. RET=$?
  144. exit $RET
  145. ;;
  146. -sign|-signreq)
  147. $CA -policy policy_anything -out newcert.pem -infiles newreq.pem
  148. RET=$?
  149. cat newcert.pem
  150. echo "Signed certificate is in newcert.pem"
  151. ;;
  152. -signCA)
  153. $CA -policy policy_anything -out newcert.pem -extensions v3_ca -infiles newreq.pem
  154. RET=$?
  155. echo "Signed CA certificate is in newcert.pem"
  156. ;;
  157. -signcert)
  158. echo "Cert passphrase will be requested twice - bug?"
  159. $X509 -x509toreq -in newreq.pem -signkey newreq.pem -out tmp.pem
  160. $CA -policy policy_anything -out newcert.pem -infiles tmp.pem
  161. RET=$?
  162. cat newcert.pem
  163. echo "Signed certificate is in newcert.pem"
  164. ;;
  165. -verify)
  166. shift
  167. if [ -z "$1" ]; then
  168. $VERIFY -CAfile $CATOP/$CACERT newcert.pem
  169. RET=$?
  170. else
  171. for j
  172. do
  173. $VERIFY -CAfile $CATOP/$CACERT $j
  174. if [ $? != 0 ]; then
  175. RET=$?
  176. fi
  177. done
  178. fi
  179. exit $RET
  180. ;;
  181. *)
  182. echo "Unknown arg $i" >&2
  183. usage
  184. exit 1
  185. ;;
  186. esac
  187. shift
  188. done
  189. exit $RET