keytool-importkeypair 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #! /bin/bash
  2. #
  3. # This file is part of keytool-importkeypair.
  4. #
  5. # keytool-importkeypair is free software: you can redistribute it
  6. # and/or modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation, either version 3 of
  8. # the License, or (at your option) any later version.
  9. #
  10. # keytool-importkeypair is distributed in the hope that it will be
  11. # useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  12. # of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with keytool-importkeypair. If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. #
  19. DEFAULT_KEYSTORE=$HOME/.keystore
  20. keystore=$DEFAULT_KEYSTORE
  21. pk8=""
  22. cert=""
  23. alias=""
  24. passphrase=""
  25. tmpdir=""
  26. scriptname=`basename $0`
  27. usage() {
  28. cat << EOF
  29. usage: ${scriptname} [-k keystore] [-p storepass]
  30. -pk8 pk8 -cert cert -alias key_alias
  31. This script is used to import a key/certificate pair
  32. into a Java keystore.
  33. If a keystore is not specified then the key pair is imported into
  34. ~/.keystore in the user's home directory.
  35. The passphrase can also be read from stdin.
  36. EOF
  37. }
  38. cleanup() {
  39. if [ ! -z "${tmpdir}" -a -d ${tmpdir} ]; then
  40. rm -fr ${tmpdir}
  41. fi
  42. }
  43. while [ $# -gt 0 ]; do
  44. case $1
  45. in
  46. -p | --passphrase | -passphrase)
  47. passphrase=$2
  48. shift 2
  49. ;;
  50. -h | --help)
  51. usage
  52. exit 0
  53. ;;
  54. -k | -keystore | --keystore)
  55. keystore=$2
  56. shift 2
  57. ;;
  58. -pk8 | --pk8 | -key | --key)
  59. pk8=$2
  60. shift 2
  61. ;;
  62. -cert | --cert | -pem | --pem)
  63. cert=$2
  64. shift 2
  65. ;;
  66. -a | -alias | --alias)
  67. alias=$2
  68. shift 2
  69. ;;
  70. *)
  71. echo "${scriptname}: Unknown option $1, exiting" 1>&2
  72. usage
  73. exit 1
  74. ;;
  75. esac
  76. done
  77. if [ -z "${pk8}" -o -z "${cert}" -o -z "${alias}" ]; then
  78. echo "${scriptname}: Missing option, exiting..." 1>&2
  79. usage
  80. exit 1
  81. fi
  82. for f in "${pk8}" "${cert}"; do
  83. if [ ! -f "$f" ]; then
  84. echo "${scriptname}: Can't find file $f, exiting..." 1>&2
  85. exit 1
  86. fi
  87. done
  88. if [ ! -f "${keystore}" ]; then
  89. storedir=`dirname "${keystore}"`
  90. if [ ! -d "${storedir}" -o ! -w "${storedir}" ]; then
  91. echo "${scriptname}: Can't access ${storedir}, exiting..." 1>&2
  92. exit 1
  93. fi
  94. fi
  95. # Create temp directory ofr key and pkcs12 bundle
  96. tmpdir=`mktemp -q -d "/tmp/${scriptname}.XXXX"`
  97. if [ $? -ne 0 ]; then
  98. echo "${scriptname}: Can't create temp directory, exiting..." 1>&2
  99. exit 1
  100. fi
  101. key="${tmpdir}/key"
  102. p12="${tmpdir}/p12"
  103. if [ -z "${passphrase}" ]; then
  104. # Request a passphrase
  105. read -p "Enter a passphrase: " -s passphrase
  106. echo ""
  107. fi
  108. # Convert PK8 to PEM KEY
  109. openssl pkcs8 -inform DER -nocrypt -in "${pk8}" -out "${key}"
  110. # Bundle CERT and KEY
  111. openssl pkcs12 -export -in "${cert}" -inkey "${key}" -out "${p12}" -password pass:"${passphrase}" -name "${alias}"
  112. # Print cert
  113. echo -n "Importing \"${alias}\" with "
  114. openssl x509 -noout -fingerprint -in "${cert}"
  115. # Import P12 in Keystore
  116. keytool -importkeystore -deststorepass "${passphrase}" -destkeystore "${keystore}" -srckeystore "${p12}" -srcstoretype PKCS12 -srcstorepass "${passphrase}"
  117. # Cleanup
  118. cleanup