KickJava   Java API By Example, From Geeks To Geeks.

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


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.XMLX509SKI;
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  *
38  *
39  * @author $Author: raul $
40  */

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

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

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

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

185    public javax.crypto.SecretKey engineResolveSecretKey(
186            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
187     {
188       return null;
189    }
190 }
191
Popular Tags