1 /* 2 * CertificateImportTest.java 3 * 4 * Created on April 8, 2002, 10:45 AM 5 * 6 * $Header: /cvsroot/ebxmlrr/jaxr/test/com/sun/xml/registry/ebxml/CertificateImportTest.java,v 1.5 2002/11/09 01:44:09 jasilva Exp $ 7 * 8 */ 9 10 package com.sun.xml.registry.ebxml; 11 12 import java.util.*; 13 14 import java.security.*; 15 import java.security.cert.*; 16 import java.io.*; 17 18 /*** 19 * 20 * @author <a href="mailto:Farrukh.Najmi@Sun.COM">Farrukh S. Najmi</a> 21 */ 22 public class CertificateImportTest { 23 24 /*** Creates new CertificateImportTest */ 25 public CertificateImportTest() { 26 27 } 28 29 void displayError(String msg) { 30 System.err.println(msg); 31 } 32 33 void displayError(Exception e) { 34 e.printStackTrace(); 35 } 36 37 void displayError(String msg, Exception e) { 38 System.err.println(msg); 39 e.printStackTrace(); 40 } 41 42 private char[] stringToCharArray(String str) { 43 char[] arr = null; 44 if (str != null) { 45 arr = str.toCharArray(); 46 } 47 48 return arr; 49 } 50 51 public boolean validateCertificate(String certFileName, String storePassword, String alias, String keyPassword) { 52 boolean valid = false; 53 54 try { 55 KeyStore store = KeyStore.getInstance("pkcs12"); 56 store.load(new FileInputStream(certFileName), stringToCharArray(storePassword)); 57 58 for (Enumeration aliases = store.aliases() ; aliases.hasMoreElements() ;) { 59 System.out.println("Found alias: '" + aliases.nextElement() + "'"); 60 } 61 62 if (store.containsAlias(alias)) { 63 try { 64 java.security.cert.Certificate cert = store.getCertificate(alias); 65 66 if (cert instanceof X509Certificate) { 67 Key key = store.getKey(alias, stringToCharArray(keyPassword)); 68 if (key instanceof PrivateKey){ 69 valid = true; 70 } 71 else { 72 displayError("Key must be a private key. Instead found: " + key.getClass().getName()); 73 } 74 } 75 else { 76 displayError("Certificate must be an X509Certificate. Instead found: " + cert.getClass().getName()); 77 } 78 } 79 catch (KeyStoreException e) { 80 displayError("Internal error getting private key.", e); 81 } 82 catch (NoSuchAlgorithmException e) { 83 displayError("Internal error private key", e); 84 } 85 catch (UnrecoverableKeyException e) { 86 displayError("Error getting private key. Check if password is correct", e); 87 } 88 } 89 else { 90 displayError("Alias " + alias + " is not valid for this certificate. Specify correct alias."); 91 } 92 } 93 catch (KeyStoreException e) { 94 displayError(e); 95 } 96 catch (NoSuchAlgorithmException e) { 97 displayError(e); 98 } 99 catch (CertificateException e) { 100 displayError(e); 101 } 102 catch (FileNotFoundException e) { 103 displayError(e); 104 } 105 catch (IOException e) { 106 displayError(e); 107 } 108 109 return valid; 110 } 111 112 static void printUsage() { 113 System.err.println("CerytificateImportTest <certFilePath> <storePassword> <alias> <keyPassword>"); 114 System.err.println("\tSpecify the word null when there is no password"); 115 } 116 117 /*** 118 * @param args the command line arguments 119 */ 120 public static void main(String args[]) { 121 try { 122 if (args.length < 3) { 123 printUsage(); 124 System.exit(-1); 125 } 126 127 String certFile = args[0]; 128 String storePassword = args[1]; 129 String alias = args[2]; 130 String keyPassword = args[3]; 131 132 CertificateImportTest test = new CertificateImportTest(); 133 134 if (storePassword.equals("null")) { 135 storePassword = null; 136 } 137 138 if (keyPassword.equals("null")) { 139 keyPassword = null; 140 } 141 142 test.validateCertificate(certFile, storePassword, alias, keyPassword); 143 } 144 catch (Exception e) { 145 e.printStackTrace(); 146 System.exit(-1); 147 } 148 } 149 150 }

This page was automatically generated by Maven