KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > keys > keyresolver > implementations > X509SubjectNameResolver


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

17 package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations;
18
19
20
21 import java.security.PublicKey JavaDoc;
22 import java.security.cert.X509Certificate JavaDoc;
23
24
25 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
26 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SubjectName;
27 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException;
28 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
29 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
30 import com.sun.org.apache.xml.internal.security.utils.Constants;
31 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
32 import org.w3c.dom.Element JavaDoc;
33
34
35 /**
36  *
37  * @author $Author: raul $
38  */

39 public class X509SubjectNameResolver extends KeyResolverSpi {
40
41    /** {@link java.util.logging} logging facility */
42     static java.util.logging.Logger JavaDoc log =
43         java.util.logging.Logger.getLogger(
44                     X509SubjectNameResolver.class.getName());
45
46    /** Field _x509childNodes */
47    private Element JavaDoc[] _x509childNodes = null;
48
49    /** Field _x509childObject[] */
50    private XMLX509SubjectName _x509childObject[] = null;
51
52    /**
53     * Method engineCanResolve
54     * @inheritDoc
55     * @param element
56     * @param BaseURI
57     * @param storage
58     *
59     */

60    public boolean engineCanResolve(Element JavaDoc element, String JavaDoc BaseURI,
61                                    StorageResolver storage) {
62       if (true)
63         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
64
65       
66        if (!XMLUtils.elementIsInSignatureSpace(element,
67                  Constants._TAG_X509DATA) ) {
68          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
69
70          return false;
71       }
72
73
74          
75          this._x509childNodes = XMLUtils.selectDsNodes(element,
76                  Constants._TAG_X509SUBJECTNAME);
77
78          if ((this._x509childNodes != null)
79                  && (this._x509childNodes.length > 0)) {
80             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Yes Sir, I can");
81
82             return true;
83          }
84      
85
86       if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
87
88       return false;
89    }
90
91    /**
92     * Method engineResolvePublicKey
93     *
94     * @param element
95     * @param BaseURI
96     * @param storage
97     * @return null if no {@link PublicKey} could be obtained
98     * @throws KeyResolverException
99     */

100    public PublicKey JavaDoc engineResolvePublicKey(
101            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
102               throws KeyResolverException {
103
104       X509Certificate JavaDoc cert = this.engineResolveX509Certificate(element,
105                                 BaseURI, storage);
106
107       if (cert != null) {
108          return cert.getPublicKey();
109       }
110
111       return null;
112    }
113
114    /**
115     * Method engineResolveX509Certificate
116     * @inheritDoc
117     * @param element
118     * @param BaseURI
119     * @param storage
120     *
121     * @throws KeyResolverException
122     */

123    public X509Certificate JavaDoc engineResolveX509Certificate(
124            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
125               throws KeyResolverException {
126
127       try {
128          if (this._x509childNodes == null) {
129             boolean weCanResolve = this.engineCanResolve(element, BaseURI,
130                                       storage);
131
132             if (!weCanResolve || (this._x509childNodes == null)) {
133                return null;
134             }
135          }
136
137          if (storage == null) {
138             Object JavaDoc exArgs[] = { Constants._TAG_X509SUBJECTNAME };
139             KeyResolverException ex =
140                new KeyResolverException("KeyResolver.needStorageResolver",
141                                         exArgs);
142
143             if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "", ex);
144
145             throw ex;
146          }
147
148          this._x509childObject =
149             new XMLX509SubjectName[this._x509childNodes.length];
150
151          for (int i = 0; i < this._x509childNodes.length; i++) {
152             this._x509childObject[i] =
153                new XMLX509SubjectName(this._x509childNodes[i],
154                                       BaseURI);
155          }
156
157          while (storage.hasNext()) {
158             X509Certificate JavaDoc cert = storage.next();
159             XMLX509SubjectName certSN =
160                new XMLX509SubjectName(element.getOwnerDocument(), cert);
161
162             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Certificate SN: " + certSN.getSubjectName());
163
164             for (int i = 0; i < this._x509childObject.length; i++) {
165                if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Element SN: "
166                          + this._x509childObject[i].getSubjectName());
167
168                if (certSN.equals(this._x509childObject[i])) {
169                   if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "match !!! ");
170
171                   return cert;
172                }
173                if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "no match...");
174             }
175          }
176
177          return null;
178       } catch (XMLSecurityException ex) {
179          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
180
181          throw new KeyResolverException("generic.EmptyMessage", ex);
182       }
183    }
184
185    /**
186     * Method engineResolveSecretKey
187     * @inheritDoc
188     * @param element
189     * @param BaseURI
190     * @param storage
191     *
192     */

193    public javax.crypto.SecretKey engineResolveSecretKey(
194            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
195    {
196       return null;
197    }
198 }
199
Popular Tags