KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
26 import com.sun.org.apache.xml.internal.security.keys.content.X509Data;
27 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial;
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.signature.XMLSignatureException;
32 import com.sun.org.apache.xml.internal.security.utils.Constants;
33 import org.w3c.dom.Element JavaDoc;
34
35
36 /**
37  *
38  * @author $Author: raul $
39  */

40 public class X509IssuerSerialResolver extends KeyResolverSpi {
41
42    /** {@link java.util.logging} logging facility */
43     static java.util.logging.Logger JavaDoc log =
44         java.util.logging.Logger.getLogger(
45                     X509IssuerSerialResolver.class.getName());
46
47     /** @inheritDoc */
48    public boolean engineCanResolve(Element JavaDoc element, String JavaDoc BaseURI,
49                                    StorageResolver storage) {
50       if (true)
51         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
52
53       X509Data x509data = null;
54       try {
55          x509data = new X509Data(element, BaseURI);
56       } catch (XMLSignatureException ex) {
57          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
58
59          return false;
60       } catch (XMLSecurityException ex) {
61          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
62
63          return false;
64       }
65
66       if (x509data == null) {
67          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
68          return false;
69       }
70
71       if (x509data.containsIssuerSerial()) {
72             return true;
73       }
74
75       if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't");
76       return false;
77    }
78
79    /** @inheritDoc */
80    public PublicKey JavaDoc engineResolvePublicKey(
81            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
82               throws KeyResolverException {
83
84       X509Certificate JavaDoc cert = this.engineResolveX509Certificate(element,
85                                 BaseURI, storage);
86
87       if (cert != null) {
88          return cert.getPublicKey();
89       }
90
91       return null;
92    }
93
94    /** @inheritDoc */
95    public X509Certificate JavaDoc engineResolveX509Certificate(
96            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage)
97               throws KeyResolverException {
98
99       try {
100          if (storage == null) {
101             Object JavaDoc exArgs[] = { Constants._TAG_X509ISSUERSERIAL };
102             KeyResolverException ex =
103                new KeyResolverException("KeyResolver.needStorageResolver",
104                                         exArgs);
105
106             if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "", ex);
107             throw ex;
108          }
109
110          X509Data x509data = new X509Data(element, BaseURI);
111          int noOfISS = x509data.lengthIssuerSerial();
112
113          while (storage.hasNext()) {
114             X509Certificate JavaDoc cert = storage.next();
115             XMLX509IssuerSerial certSerial = new XMLX509IssuerSerial(element.getOwnerDocument(), cert);
116
117             if (true) {
118                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Certificate Issuer: "
119                       + certSerial.getIssuerName());
120                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Certificate Serial: "
121                       + certSerial.getSerialNumber().toString());
122             }
123
124             for (int i=0; i<noOfISS; i++) {
125                XMLX509IssuerSerial xmliss = x509data.itemIssuerSerial(i);
126
127                if (true) {
128                     if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Element Issuer: "
129                          + xmliss.getIssuerName());
130                     if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Element Serial: "
131                          + xmliss.getSerialNumber().toString());
132                }
133
134                if (certSerial.equals(xmliss)) {
135                   if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "match !!! ");
136
137                   return cert;
138                }
139                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "no match...");
140             }
141          }
142
143          return null;
144       } catch (XMLSecurityException ex) {
145          if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex);
146
147          throw new KeyResolverException("generic.EmptyMessage", ex);
148       }
149    }
150
151    /** @inheritDoc */
152    public javax.crypto.SecretKey engineResolveSecretKey(
153            Element JavaDoc element, String JavaDoc BaseURI, StorageResolver storage) {
154       return null;
155    }
156 }
157
Popular Tags