code_junkie

How do I list / export private keys from a keystore?

Nov 11th, 2011
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. // Load input stream into keystore
  2. keystore.load(is, password.toCharArray());
  3.  
  4. // List the aliases
  5. Enumeration enum = keystore.aliases();
  6. for (; enum.hasMoreElements(); ) {
  7. String alias = (String)enum.nextElement();
  8.  
  9. // Does alias refer to a private key?
  10. boolean b = keystore.isKeyEntry(alias);
  11.  
  12. // Does alias refer to a trusted certificate?
  13. b = keystore.isCertificateEntry(alias);
  14. }
  15.  
  16. import java.io.FileInputStream;
  17. import java.security.Key;
  18. import java.security.KeyStore;
  19. import sun.misc.BASE64Encoder;
  20.  
  21. public class DumpPrivateKey {
  22. /**
  23. * Provides the missing functionality of keytool
  24. * that Apache needs for SSLCertificateKeyFile.
  25. *
  26. * @param args <ul>
  27. * <li> [0] Keystore filename.
  28. * <li> [1] Keystore password.
  29. * <li> [2] alias
  30. * </ul>
  31. */
  32. static public void main(String[] args)
  33. throws Exception {
  34. if(args.length < 3) {
  35. throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, <key password: default same tha
  36. n keystore");
  37. }
  38. final String keystoreName = args[0];
  39. final String keystorePassword = args[1];
  40. final String alias = args[2];
  41. final String keyPassword = getKeyPassword(args,keystorePassword);
  42. KeyStore ks = KeyStore.getInstance("jks");
  43. ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray());
  44. Key key = ks.getKey(alias, keyPassword.toCharArray());
  45. String b64 = new BASE64Encoder().encode(key.getEncoded());
  46. System.out.println("-----BEGIN PRIVATE KEY-----");
  47. System.out.println(b64);
  48. System.out.println("-----END PRIVATE KEY-----");
  49. }
  50. private static String getKeyPassword(final String[] args, final String keystorePassword)
  51. {
  52. String keyPassword = keystorePassword; // default case
  53. if(args.length == 4) {
  54. keyPassword = args[3];
  55. }
  56. return keyPassword;
  57. }
  58. }
  59.  
  60. javac -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey.java
  61.  
  62. import java.io.FileInputStream;
  63. import java.security.Key;
  64. import java.security.KeyStore;
  65. //import sun.misc.BASE64Encoder;
  66. import org.apache.commons.codec.binary.Base64;
  67.  
  68. public class DumpPrivateKey {
  69. /**
  70. * Provides the missing functionality of keytool
  71. * that Apache needs for SSLCertificateKeyFile.
  72. *
  73. * @param args <ul>
  74. * <li> [0] Keystore filename.
  75. * <li> [1] Keystore password.
  76. * <li> [2] alias
  77. * </ul>
  78. */
  79. static public void main(String[] args)
  80. throws Exception {
  81. if(args.length < 3) {
  82. throw new IllegalArgumentException("expected args: Keystore filename, Keystore password, alias, <key password: default same tha
  83. n keystore");
  84. }
  85. final String keystoreName = args[0];
  86. final String keystorePassword = args[1];
  87. final String alias = args[2];
  88. final String keyPassword = getKeyPassword(args,keystorePassword);
  89. KeyStore ks = KeyStore.getInstance("jks");
  90. ks.load(new FileInputStream(keystoreName), keystorePassword.toCharArray());
  91. Key key = ks.getKey(alias, keyPassword.toCharArray());
  92. //String b64 = new BASE64Encoder().encode(key.getEncoded());
  93. String b64 = new String(Base64.encodeBase64(key.getEncoded(),true));
  94. System.out.println("-----BEGIN PRIVATE KEY-----");
  95. System.out.println(b64);
  96. System.out.println("-----END PRIVATE KEY-----");
  97. }
  98. private static String getKeyPassword(final String[] args, final String keystorePassword)
  99. {
  100. String keyPassword = keystorePassword; // default case
  101. if(args.length == 4) {
  102. keyPassword = args[3];
  103. }
  104. return keyPassword;
  105. }
  106. }
  107.  
  108. java -classpath .:commons-codec-1.4/commons-codec-1.4.jar DumpPrivateKey $HOME/.keystore changeit tomcat
  109.  
  110. keytool -importkeystore -srckeystore keystore.jks
  111. -destkeystore intermediate.p12 -deststoretype PKCS12
  112.  
  113. openssl pkcs12 -in intermediate.p12 -out extracted.pem -nodes
  114.  
  115. char[] password = ...
  116. KeyStore anchors = ...
  117. Enumeration<String> aliases = anchors.aliases();
  118. while (aliases.hasMoreElements()) {
  119. String alias = aliases.nextElement();
  120. if (!anchors.isKeyEntry(alias))
  121. continue;
  122. Key key = anchors.getKey(alias, password);
  123. if ((key instanceof PrivateKey) && "PKCS#8".equals(key.getFormat())) {
  124. /* Most PrivateKeys use this format, but check for safety. */
  125. FileOutputStream os = new FileOutputStream(alias + ".key");
  126. os.write(key.getEncoded());
  127. os.flush();
  128. os.close();
  129. }
  130. }
Add Comment
Please, Sign In to add comment