KickJava   Java API By Example, From Geeks To Geeks.

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


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.keyresolver.implementations;
19
20
21
22 import java.security.PublicKey JavaDoc;
23 import java.security.cert.X509Certificate JavaDoc;
24
25
26 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
27 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509Certificate;
28 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException;
29 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
30 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
31 import com.sun.org.apache.xml.internal.security.utils.Constants;
32 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
33 import org.w3c.dom.Element JavaDoc;
34
35
36 /**
37  * Resolves Certificates which are directly contained inside a
38  * <CODE>ds:X509Certificate</CODE> Element.
39  *
40  * @author $Author: raul $
41  */

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

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

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

122    public X509Certificate JavaDoc engineResolveX509Certificate(
123            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
124               throws KeyResolverException {
125
126       try {
127          if ((this._x509CertKeyElements == null)
128                  || (this._x509CertKeyElements.length == 0)) {
129             boolean weCanResolve = this.engineCanResolve(element, BaseURI,
130                                       storage);
131
132             if (!weCanResolve || (this._x509CertKeyElements == null)
133                     || (this._x509CertKeyElements.length == 0)) {
134                return null;
135             }
136          }
137
138          this._x509certObject =
139             new XMLX509Certificate[this._x509CertKeyElements.length];
140
141          // populate Object array
142
for (int i = 0; i < this._x509CertKeyElements.length; i++) {
143             this._x509certObject[i] =
144                new XMLX509Certificate(this._x509CertKeyElements[i]
145                   , BaseURI);
146          }
147
148          for (int i = 0; i < this._x509certObject.length; i++) {
149             X509Certificate JavaDoc cert = this._x509certObject[i].getX509Certificate();
150
151             if (cert != null) {
152                return cert;
153             }
154          }
155
156          return null;
157       } catch (XMLSecurityException ex) {
158          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
159
160          throw new KeyResolverException("generic.EmptyMessage", ex);
161       }
162    }
163
164    /**
165     * Method engineResolveSecretKey
166     * @inheritDoc
167     * @param element
168     * @param BaseURI
169     * @param storage
170     *
171     */

172    public javax.crypto.SecretKey engineResolveSecretKey(
173            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
174    {
175       return null;
176    }
177 }
178
Popular Tags