1 2 18 package com.sun.org.apache.xml.internal.security.keys.storage.implementations; 19 20 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.security.cert.CertificateException ; 27 import java.security.cert.CertificateExpiredException ; 28 import java.security.cert.CertificateFactory ; 29 import java.security.cert.CertificateNotYetValidException ; 30 import java.security.cert.X509Certificate ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 35 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverException; 36 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverSpi; 37 import com.sun.org.apache.xml.internal.security.utils.Base64; 38 39 40 46 public class CertsInFilesystemDirectoryResolver extends StorageResolverSpi { 47 48 49 static java.util.logging.Logger log = 50 java.util.logging.Logger.getLogger( 51 CertsInFilesystemDirectoryResolver.class.getName()); 52 53 54 String _merlinsCertificatesDir = null; 55 56 57 private List _certs = new ArrayList (); 58 59 60 Iterator _iterator = null; 61 62 68 public CertsInFilesystemDirectoryResolver(String directoryName) 69 throws StorageResolverException { 70 71 this._merlinsCertificatesDir = directoryName; 72 73 this.readCertsFromHarddrive(); 74 75 this._iterator = new FilesystemIterator(this._certs); 76 } 77 78 83 private void readCertsFromHarddrive() throws StorageResolverException { 84 85 File certDir = new File (this._merlinsCertificatesDir); 86 ArrayList al = new ArrayList (); 87 String [] names = certDir.list(); 88 89 for (int i = 0; i < names.length; i++) { 90 String currentFileName = names[i]; 91 92 if (currentFileName.endsWith(".crt")) { 93 al.add(names[i]); 94 } 95 } 96 97 CertificateFactory cf = null; 98 99 try { 100 cf = CertificateFactory.getInstance("X.509"); 101 } catch (CertificateException ex) { 102 throw new StorageResolverException("empty", ex); 103 } 104 105 if (cf == null) { 106 throw new StorageResolverException("empty"); 107 } 108 109 for (int i = 0; i < al.size(); i++) { 110 String filename = certDir.getAbsolutePath() + File.separator 111 + (String ) al.get(i); 112 File file = new File (filename); 113 boolean added = false; 114 String dn = null; 115 116 try { 117 FileInputStream fis = new FileInputStream (file); 118 X509Certificate cert = 119 (X509Certificate ) cf.generateCertificate(fis); 120 121 fis.close(); 122 123 cert.checkValidity(); 125 this._certs.add(cert); 126 127 dn = cert.getSubjectDN().getName(); 128 added = true; 129 } catch (FileNotFoundException ex) { 130 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex); 131 } catch (IOException ex) { 132 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex); 133 } catch (CertificateNotYetValidException ex) { 134 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex); 135 } catch (CertificateExpiredException ex) { 136 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex); 137 } catch (CertificateException ex) { 138 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Could not add certificate from file " + filename, ex); 139 } 140 141 if (added) { 142 if (true) 143 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Added certificate: " + dn); 144 } 145 } 146 } 147 148 149 public Iterator getIterator() { 150 return this._iterator; 151 } 152 153 159 class FilesystemIterator implements Iterator { 160 161 162 List _certs = null; 163 164 165 int _i; 166 167 172 public FilesystemIterator(List certs) { 173 this._certs = certs; 174 this._i = 0; 175 } 176 177 178 public boolean hasNext() { 179 return (this._i < this._certs.size()); 180 } 181 182 183 public Object next() { 184 return this._certs.get(this._i++); 185 } 186 187 191 public void remove() { 192 throw new UnsupportedOperationException ( 193 "Can't remove keys from KeyStore"); 194 } 195 } 196 197 203 public static void main(String unused[]) throws Exception { 204 205 CertsInFilesystemDirectoryResolver krs = 206 new CertsInFilesystemDirectoryResolver( 207 "data/ie/baltimore/merlin-examples/merlin-xmldsig-eighteen/certs"); 208 209 for (Iterator i = krs.getIterator(); i.hasNext(); ) { 210 X509Certificate cert = (X509Certificate ) i.next(); 211 byte[] ski = 212 com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI 213 .getSKIBytesFromCert(cert); 214 215 System.out.println(); 216 System.out.println("Base64(SKI())= \"" 217 + Base64.encode(ski) + "\""); 218 System.out.println("cert.getSerialNumber()= \"" 219 + cert.getSerialNumber().toString() + "\""); 220 System.out.println("cert.getSubjectDN().getName()= \"" 221 + cert.getSubjectDN().getName() + "\""); 222 System.out.println("cert.getIssuerDN().getName()= \"" 223 + cert.getIssuerDN().getName() + "\""); 224 } 225 } 226 } 227 | Popular Tags |