1
10package org.jstk.crypt;
11
12import java.util.*;
13import java.security.*;
14import java.io.*;
15import javax.crypto.SecretKey;
16import javax.crypto.KeyGenerator;
17
18import org.jstk.JSTKCommand;
19import org.jstk.JSTKCommandAdapter;
20import org.jstk.JSTKException;
21import org.jstk.JSTKArgs;
22import org.jstk.JSTKOptions;
23import org.jstk.JSTKResult;
24
25public class ListKSCommand extends JSTKCommandAdapter{
26 private static HashMap defaults = new HashMap();
27 static {
28 defaults.put("kstype", "JCEKS");
29 defaults.put("keystore", "my.keystore");
30 defaults.put("storepass", "changeit");
31 }
32 public String briefDescription(){
33 return "lists keystore entries";
34 }
35 public String optionsDescription(){
36 return
37 " -keystore <keystore>: the keystore.[" +
38 defaults.get("keystore") + "]\n" +
39 " -storepass <storepass>: Password for keystore.[" +
40 defaults.get("storepass") + "]\n" +
41 " -kstype <type> : the keystore type.[" +
42 defaults.get("type") + "]\n" +
43 " -alias <alias> : alias to access the key in the keystore.[" +
44 defaults.get("alias") + "]\n" +
45 " -keypass <keypass> : Password for key in the keystore.[" +
46 defaults.get("keypass") + "]\n" +
47 " -provider <provider>: provider name for KeyStore.\n";
48
49 }
50
51 public String[] useForms(){
52 String[] forms = {
53 "[-keystore <keystore>] [-kstype (JCEKS|JKS|PKCS12)]\n" +
54 "\t[-storepass <storepass>] [-alias <alias>] [-keypass <keypass>]\n" +
55 "\t[-provider <provider>]"
56 };
57 return forms;
58 }
59
60 public String[] sampleUses(){
61 String[] uses = {
62 "",
63 "-keystore test.ks -storepass testpass",
64 "-alias test.key"
65 };
66 return uses;
67 }
68
69 private String formatEntry(KeyStore ks, String alias, String keypass) throws Exception{
70 StringBuffer sb = new StringBuffer();
71
72 sb.append("KeyStore entry \"" + alias + "\": ");
73 if (ks.isKeyEntry(alias)){
74 try {
75 sb.append("Key entry.\n");
76 Key key = ks.getKey(alias, keypass.toCharArray());
77 String keytype = (key instanceof SecretKey ? "SecretKey" :
78 (key instanceof PrivateKey ? "PrivateKey" : "PublicKey"));
79 sb.append(KeyUtil.format(key, keytype));
80 } catch (UnrecoverableKeyException e){
81 sb.append("Cannot Receover Key from KeyStore.\n");
82 }
83 } else {
84 sb.append("Certificate Entry.\n");
85 }
86 return sb.toString();
87 }
88
89 public Object execute(JSTKArgs args) throws JSTKException{
90 StringBuffer sb = new StringBuffer();
91 try {
92 args.setDefaults(defaults);
93 String keystore = args.get("keystore");
94 String storepass = args.get("storepass");
95 String type = args.get("kstype");
96 String providerName = args.get("provider");
97 String keypass = args.get("keypass");
98 if (keypass == null)
99 keypass = storepass;
00 String alias = args.get("alias");
01
02 FileInputStream fis = new FileInputStream(keystore);
03
04 KeyStore ks;
05 if (providerName != null)
06 ks = KeyStore.getInstance(type, providerName);
07 else
08 ks = KeyStore.getInstance(type);
09
10 ks.load(fis, storepass.toCharArray());
11
12 if (alias != null){
13 if (ks.containsAlias(alias)){
14 sb.append(formatEntry(ks, alias, keypass));
15 } else {
16 sb.append("No such Entry: " + alias + ".\n");
17 }
18 } else {
19 Enumeration enum = ks.aliases();
20
21 while (enum.hasMoreElements()){
22 String alias0 = (String)enum.nextElement();
23 sb.append(formatEntry(ks, alias0, keypass));
24 }
25 }
26 } catch (Exception exc){
27 throw new JSTKException("ListKSCommand.execute() failed", exc);
28 }
29 return new JSTKResult(null, true, sb.toString());
30 }
31
32 public static void main(String[] args) throws Exception {
33 JSTKOptions opts = new JSTKOptions();
34 opts.parse(args, 0);
35 GenKCommand genKCmd = new GenKCommand();
36 JSTKResult result = (JSTKResult)genKCmd.execute(opts);
37 System.out.print(result);
38 System.out.flush();
39 }
40}