1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 package com.sun.org.apache.xml.internal.security.keys.storage.implementations; 22 23 import java.security.KeyStore; 24 import java.security.KeyStoreException; 25 import java.security.cert.X509Certificate; 26 import java.util.Enumeration; 27 import java.util.Iterator; 28 29 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverException; 30 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverSpi; 31 32 33 /** 34 * Makes the Certificates from a JAVA {@link KeyStore} object available to the 35 * {@link com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver}. 36 * 37 * @author $Author: mullan $ 38 */ 39 public class KeyStoreResolver extends StorageResolverSpi { 40 41 /** Field _keyStore */ 42 KeyStore _keyStore = null; 43 44 /** Field _iterator */ 45 Iterator<X509Certificate> _iterator = null; 46 47 /** 48 * Constructor KeyStoreResolver 49 * 50 * @param keyStore is the keystore which contains the Certificates 51 * @throws StorageResolverException 52 */ 53 public KeyStoreResolver(KeyStore keyStore) throws StorageResolverException { 54 this._keyStore = keyStore; 55 this._iterator = new KeyStoreIterator(this._keyStore); 56 } 57 58 /** @inheritDoc */ 59 public Iterator<X509Certificate> getIterator() { 60 return this._iterator; 61 } 62 63 /** 64 * Class KeyStoreIterator 65 * 66 * @author $Author: mullan $ 67 * @version $Revision: 1.5 $ 68 */ 69 static class KeyStoreIterator implements Iterator<X509Certificate> { 70 71 /** Field _keyStore */ 72 KeyStore _keyStore = null; 73 74 /** Field _aliases */ 75 Enumeration<String> _aliases = null; 76 77 /** 78 * Constructor KeyStoreIterator 79 * 80 * @param keyStore 81 * @throws StorageResolverException 82 */ 83 public KeyStoreIterator(KeyStore keyStore) 84 throws StorageResolverException { 85 86 try { 87 this._keyStore = keyStore; 88 this._aliases = this._keyStore.aliases(); 89 } catch (KeyStoreException ex) { 90 throw new StorageResolverException("generic.EmptyMessage", ex); 91 } 92 } 93 94 /** @inheritDoc */ 95 public boolean hasNext() { 96 return this._aliases.hasMoreElements(); 97 } 98 99 /** @inheritDoc */ 100 @SuppressWarnings("unchecked") 101 public X509Certificate next() { 102 103 String alias = this._aliases.nextElement(); 104 105 try { 106 return (X509Certificate)this._keyStore.getCertificate(alias); 107 } catch (KeyStoreException ex) { 108 return null; 109 } 110 } 111 112 /** 113 * Method remove 114 * 115 */ 116 public void remove() { 117 throw new UnsupportedOperationException( 118 "Can't remove keys from KeyStore"); 119 } 120 } 121 122 /** 123 * Method main 124 * 125 * @param unused 126 * @throws Exception 127 */ 128 public static void main(String unused[]) throws Exception { 129 130 KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType()); 131 132 ks.load( 133 new java.io.FileInputStream( 134 "data/com/sun/org/apache/xml/internal/security/samples/input/keystore.jks"), 135 "xmlsecurity".toCharArray()); 136 137 KeyStoreResolver krs = new KeyStoreResolver(ks); 138 139 for (Iterator<X509Certificate> i = krs.getIterator(); i.hasNext(); ) { 140 X509Certificate cert = i.next(); 141 byte[] ski = 142 com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI 143 .getSKIBytesFromCert(cert); 144 145 System.out.println(com.sun.org.apache.xml.internal.security.utils.Base64.encode(ski)); 146 } 147 } 148 }