gen_p12_key_par.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/bin/sh
  2. CertValidDays=3650
  3. x=`basename $0`
  4. if test $# -ne 4; then
  5. echo "Generate a certificate to a PKCS12 key store."
  6. echo "You must supply a target key store without the extension (extension will be added as .p12) and an alias for generated certificate."
  7. echo "usage: ${x} <target keystore> <cert alias> <sigalg> <distinguished name>"
  8. echo " WHERE"
  9. echo " target keystore = name of the target keystore file without .p12 extension"
  10. echo " cert alias = alias name for the digital certificate"
  11. echo " sigalg = signing algorithm for the digital certificate ... SHA1, MD5 etc"
  12. echo " distinguished name = a string in the format:"
  13. echo " CN=<cName>, OU=<orgUnit>, O=<org>, L=<city>, S=<state>, C=<countryCode>"
  14. echo ""
  15. echo " eg. $0 as2_certs openas2a SHA1 \"CN=OpenAS2A Testing, OU=QA, O=OpenAS2A, L=New York, S=New York, C=US\""
  16. echo "OUTPUT: as2_certs.p12 - keystore containing both public and private key"
  17. echo " openas2a.cer - certificate file the public key."
  18. exit 1
  19. fi
  20. tgtStore=$1
  21. certAlias=$2
  22. sigAlg="$3withRSA"
  23. dName=$4
  24. if [ -z $JAVA_HOME ]; then
  25. OS=$(uname -s)
  26. if [[ "${OS}" == *Darwin* ]]; then
  27. # Mac OS X platform
  28. JAVA_HOME=$(/usr/libexec/java_home)
  29. elif [[ "${OS}" == *Linux* ]]; then
  30. # Linux platform
  31. JAVA_HOME=$(dirname $(dirname $(readlink -f $(which java))))
  32. elif [[ "${OS}" == *MINGW* ]]; then
  33. # Windows NT platform
  34. echo "Windows not supported by this script"
  35. fi
  36. fi
  37. if [ -z $JAVA_HOME ]; then
  38. echo "ERROR: Cannot find JAVA_HOME"
  39. exit
  40. fi
  41. echo "Generate a certificate to a PKCS12 key store."
  42. echo "Generating certificate: using alias $certAlias to ${tgtStore}.p12"
  43. read -p "Do you wish to execute this request? [Y/N]" Response
  44. if [ $Response != "Y" -a $Response != "y" ] ; then
  45. exit 1
  46. fi
  47. read -p "Enter password for keystore:" ksPwd
  48. $JAVA_HOME/bin/keytool -genkeypair -alias $certAlias -validity $CertValidDays -keyalg RSA -sigalg $sigAlg -keystore ${tgtStore}.p12 -storepass $ksPwd -storetype pkcs12 -dname "$dName"
  49. $JAVA_HOME/bin/keytool -selfcert -alias $certAlias -validity $CertValidDays -sigalg $sigAlg -keystore ${tgtStore}.p12 -storepass $ksPwd -storetype pkcs12
  50. $JAVA_HOME/bin/keytool -export -rfc -file $certAlias.cer -alias $certAlias -keystore ${tgtStore}.p12 -storepass $ksPwd -storetype pkcs12