KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > storage > implementations > KeyStoreResolver


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.keys.storage.implementations;
19
20
21
22 import java.security.KeyStore JavaDoc;
23 import java.security.KeyStoreException JavaDoc;
24 import java.security.cert.X509Certificate JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverException;
29 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolverSpi;
30
31
32 /**
33  * Makes the Certificates from a JAVA {@link KeyStore} object available to the
34  * {@link com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver}.
35  *
36  * @author $Author: raul $
37  */

38 public class KeyStoreResolver extends StorageResolverSpi {
39
40    /** Field _keyStore */
41    KeyStore JavaDoc _keyStore = null;
42
43    /** Field _iterator */
44    Iterator JavaDoc _iterator = null;
45
46    /**
47     * Constructor KeyStoreResolver
48     *
49     * @param keyStore is the keystore which contains the Certificates
50     * @throws StorageResolverException
51     */

52    public KeyStoreResolver(KeyStore JavaDoc keyStore) throws StorageResolverException {
53       this._keyStore = keyStore;
54       this._iterator = new KeyStoreIterator(this._keyStore);
55    }
56
57    /** @inheritDoc */
58    public Iterator JavaDoc getIterator() {
59       return this._iterator;
60    }
61
62    /**
63     * Class KeyStoreIterator
64     *
65     * @author $Author: raul $
66     * @version $Revision: 1.7 $
67     */

68    class KeyStoreIterator implements Iterator JavaDoc {
69
70       /** Field _keyStore */
71       KeyStore JavaDoc _keyStore = null;
72
73       /** Field _aliases */
74       Enumeration JavaDoc _aliases = null;
75
76       /**
77        * Constructor KeyStoreIterator
78        *
79        * @param keyStore
80        * @throws StorageResolverException
81        */

82       public KeyStoreIterator(KeyStore JavaDoc keyStore)
83               throws StorageResolverException {
84
85          try {
86             this._keyStore = keyStore;
87             this._aliases = this._keyStore.aliases();
88          } catch (KeyStoreException JavaDoc ex) {
89             throw new StorageResolverException("generic.EmptyMessage", ex);
90          }
91       }
92
93       /** @inheritDoc */
94       public boolean hasNext() {
95          return this._aliases.hasMoreElements();
96       }
97
98       /** @inheritDoc */
99       public Object JavaDoc next() {
100
101          String JavaDoc alias = (String JavaDoc) this._aliases.nextElement();
102
103          try {
104             return this._keyStore.getCertificate(alias);
105          } catch (KeyStoreException JavaDoc ex) {
106             return null;
107          }
108       }
109
110       /**
111        * Method remove
112        *
113        */

114       public void remove() {
115          throw new UnsupportedOperationException JavaDoc(
116             "Can't remove keys from KeyStore");
117       }
118    }
119
120    /**
121     * Method main
122     *
123     * @param unused
124     * @throws Exception
125     */

126    public static void main(String JavaDoc unused[]) throws Exception JavaDoc {
127
128       KeyStore JavaDoc ks = KeyStore.getInstance(KeyStore.getDefaultType());
129
130       ks.load(
131          new java.io.FileInputStream JavaDoc(
132          "data/com/sun/org/apache/xml/internal/security/samples/input/keystore.jks"),
133             "xmlsecurity".toCharArray());
134
135       KeyStoreResolver krs = new KeyStoreResolver(ks);
136
137       for (Iterator JavaDoc i = krs.getIterator(); i.hasNext(); ) {
138          X509Certificate JavaDoc cert = (X509Certificate JavaDoc) i.next();
139          byte[] ski =
140             com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI
141                .getSKIBytesFromCert(cert);
142
143          System.out.println(com.sun.org.apache.xml.internal.security.utils.Base64.encode(ski));
144       }
145    }
146 }
147
Popular Tags