create_cert.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #! /bin/sh
  2. CLIENT=$1
  3. if [ -z "${CLIENT}" ] ; then
  4. echo "Usage: $0 FQDN"
  5. exit 1
  6. fi
  7. set -e
  8. CACERT=ca/ca.crt
  9. CAKEY=private/ca.key
  10. CLIENTKEY=clients/${CLIENT}/${CLIENT}.key
  11. CLIENTPUB=clients/${CLIENT}/${CLIENT}.pub
  12. CLIENTCSR=clients/${CLIENT}/${CLIENT}.csr
  13. CLIENTCRT=certs/${CLIENT}.crt
  14. PASSFILE=
  15. if [ -e ca.pass ] ; then
  16. PASSFILE="-passin file:ca.pass"
  17. fi
  18. mkdir -p clients/${CLIENT}
  19. mkdir -p certs
  20. cat x509v3.cnf > x509v3_ca.cnf
  21. cat >> x509v3_ca.cnf <<!
  22. [ x509v3_FQDN ]
  23. keyUsage = \$ENV::CERTUSAGE
  24. subjectAltName = DNS:${CLIENT}
  25. extendedKeyUsage = \$ENV::EXTCERTUSAGE
  26. nsCertType = \$ENV::NSCERTTYPE
  27. !
  28. sed 's/^commonName.*/commonName='${CLIENT}'/' openssl.cnf > "sign-${CLIENT}.cnf"
  29. # Create the clients key if not already available
  30. if [ \! -f "${CLIENTKEY}" ] ; then
  31. openssl genrsa -out "${CLIENTKEY}" 2048
  32. fi
  33. if [ \! -f "${CLIENTPUB}" ] ; then
  34. openssl rsa -in "${CLIENTKEY}" -pubout -out "${CLIENTPUB}"
  35. fi
  36. # Create a signing requrest
  37. openssl req -new -key "${CLIENTKEY}" \
  38. -config "sign-${CLIENT}.cnf" -out "${CLIENTCSR}"
  39. # And sign the certificate
  40. openssl x509 -req -sha256 -days 3650 \
  41. -in "${CLIENTCSR}" \
  42. -CA "${CACERT}" -CAkey "${CAKEY}" \
  43. -CAcreateserial \
  44. -extfile x509v3_ca.cnf -extensions x509v3_FQDN \
  45. -out "${CLIENTCRT}" \
  46. ${PASSFILE}
  47. openssl verify -CAfile "${CACERT}" "${CLIENTCRT}"
  48. rm -f x509v3_ca.cnf "sign-${CLIENT}.cnf"