in modules/jdktools/src/main/java/org/apache/harmony/tools/keytool/KeyStoreCertPrinter.java [61:227]
static void list(KeytoolParameters param) throws KeyStoreException,
NoSuchAlgorithmException, NoSuchProviderException,
UnrecoverableKeyException, CertificateException,
FileNotFoundException, IOException {
Enumeration aliases;
KeyStore keyStore = param.getKeyStore();
String alias = param.getAlias();
if (alias != null) {
// if the alias is specified, make a single-element
// enumeration of it
aliases = Collections.enumeration(Collections.singleton(alias));
} else {// if the alias is not given,
// get all aliases
aliases = keyStore.aliases();
// print the keystore info
System.out.println("Type of keystore: " + keyStore.getType());
System.out.println("Keystore provider name: "
+ keyStore.getProvider().getName());
int keyStoreSize = keyStore.size();
System.out.println("\nThe keystore contains " + keyStoreSize
+ ((keyStoreSize == 1) ? " entry \n" : " entries \n"));
}
String mdProvider = (param.getMdProvider() != null) ? param
.getMdProvider() : param.getProvider();
while (aliases.hasMoreElements()) {
String currentAlias = (String) aliases.nextElement();
String creationDate = keyStore.getCreationDate(currentAlias)
.toString();
// true if the keystore entry is a TrustedCertificateEntry
boolean trustedEntry = false;
// true if the keystore entry is a SecretKeyEntry
boolean secretKeyEntry = false;
// get the type of the entry to print it out
String entryType = "Key entry";
if (keyStore.entryInstanceOf(currentAlias,
KeyStore.TrustedCertificateEntry.class)) {
entryType = "Trusted certificate entry";
trustedEntry = true;
} else if (keyStore.entryInstanceOf(currentAlias,
KeyStore.PrivateKeyEntry.class)) {
entryType = "Private key entry";
} else if (keyStore.entryInstanceOf(currentAlias,
KeyStore.SecretKeyEntry.class)) {
entryType = "Secret key entry";
secretKeyEntry = true;
}
// get the certificate associated with the currentAlias
X509Certificate x509cert = ((X509Certificate) keyStore
.getCertificate(currentAlias));
// if -v or -rfc options are specified
if (param.isVerbose() || param.isRfc()) {
// print detailed info about the _entry_
System.out.println("Alias name: " + currentAlias);
System.out.println("Date of creation: " + creationDate);
System.out.println("Type of the entry: " + entryType);
if (!secretKeyEntry) {
Certificate[] certChain = keyStore
.getCertificateChain(currentAlias);
if (!trustedEntry) {
System.out.println("Certificate chain length: "
+ certChain.length);
}
// if -v option was given, print the detailed info about
// the certificate
if (param.isVerbose()) {
// print out the first certificate
System.out.println("Certificate[1]:");
printX509CertDetailed(x509cert, mdProvider);
if (!trustedEntry) {
for (int i = 1; i < certChain.length; i++) {
System.out.println("Certificate[" + (i + 1)
+ "]:");
printX509CertDetailed(
(X509Certificate) certChain[i],
mdProvider);
}
}
}
// if -rfc option is given, print the certificate in Base64
// printable format
else {
// print out the first certificate
System.out.println("Certificate[1]:");
System.out.println("-----BEGIN CERTIFICATE-----");
System.out.println(Base64.encode(x509cert.getEncoded(),
"ISO-8859-1"));
System.out.println("-----END CERTIFICATE-----");
if (!trustedEntry) {
for (int i = 1; i < certChain.length; i++) {
System.out.println("Certificate[" + (i + 1)
+ "]:");
System.out
.println("-----BEGIN CERTIFICATE-----");
System.out.println(Base64.encode(certChain[i]
.getEncoded(), "ISO-8859-1"));
System.out.println("-----END CERTIFICATE-----");
}
}
}
} else {
// if the key is explicitly asked to be printed
// by setting the alias, print it out, otherwise - do
// nothing.
if (alias != null) {
// TODO: ask for password if not set, when read from
// stdin is OK.
char[] keyPass;
if ((keyPass = param.getKeyPass()) != null) {
Key key = keyStore.getKey(currentAlias, keyPass);
System.out.println("Algorithm: "
+ key.getAlgorithm() + "\nFormat: "
+ key.getFormat());
System.out.println("Key: "
+ formatBytes(key.getEncoded()));
} else {
System.out.println("If you want to print the key, "
+ "please set the entry password using "
+ "\"-keypass\" option");
}
}
}
System.out.println("\n*******************************"
+ "*******************************\n");
} else {// if neither -v nor -rfc options specified
String commaSpc = ", ";
System.out.print(currentAlias + commaSpc + creationDate
+ commaSpc + entryType);
if (!secretKeyEntry) {
System.out.print(commaSpc
+ "\nCertificate fingerprint (MD5): ");
printMD(x509cert.getEncoded(), "MD5", mdProvider);
} else {
// If the key is explicitly asked to be printed
// by setting the alias, print it out, otherwise - do
// nothing.
if (alias != null) {
char[] keyPass;
if ((keyPass = param.getKeyPass()) != null) {
Key key = keyStore.getKey(currentAlias, keyPass);
System.out.println(key.getAlgorithm() + ", "
+ key.getFormat() + ", "
+ formatBytes(key.getEncoded()));
} else {
System.out.println("If you want to print the key, "
+ "please set the entry password using "
+ "\"-keypass\" option");
}
}
}
}
}
}