J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * reserved comment block |
| 3 | * DO NOT REMOVE OR ALTER! |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright 1999-2004 The Apache Software Foundation. |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * |
| 20 | */ |
| 21 | package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations; |
| 22 | |
| 23 | |
| 24 | |
| 25 | import java.security.PublicKey; |
| 26 | import java.security.cert.X509Certificate; |
| 27 | |
| 28 | |
| 29 | import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; |
| 30 | import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SubjectName; |
| 31 | import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException; |
| 32 | import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi; |
| 33 | import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; |
| 34 | import com.sun.org.apache.xml.internal.security.utils.Constants; |
| 35 | import com.sun.org.apache.xml.internal.security.utils.XMLUtils; |
| 36 | import org.w3c.dom.Element; |
| 37 | |
| 38 | |
| 39 | /** |
| 40 | * |
| 41 | * @author $Author: raul $ |
| 42 | */ |
| 43 | public class X509SubjectNameResolver extends KeyResolverSpi { |
| 44 | |
| 45 | /** {@link java.util.logging} logging facility */ |
| 46 | static java.util.logging.Logger log = |
| 47 | java.util.logging.Logger.getLogger( |
| 48 | X509SubjectNameResolver.class.getName()); |
| 49 | |
| 50 | /** Field _x509childNodes */ |
| 51 | private Element[] _x509childNodes = null; |
| 52 | |
| 53 | /** Field _x509childObject[] */ |
| 54 | private XMLX509SubjectName _x509childObject[] = null; |
| 55 | |
| 56 | /** |
| 57 | * Method engineCanResolve |
| 58 | * @inheritDoc |
| 59 | * @param element |
| 60 | * @param BaseURI |
| 61 | * @param storage |
| 62 | * |
| 63 | */ |
| 64 | public boolean engineCanResolve(Element element, String BaseURI, |
| 65 | StorageResolver storage) { |
| 66 | if (true) |
| 67 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?"); |
| 68 | |
| 69 | |
| 70 | if (!XMLUtils.elementIsInSignatureSpace(element, |
| 71 | Constants._TAG_X509DATA) ) { |
| 72 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't"); |
| 73 | |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | this._x509childNodes = XMLUtils.selectDsNodes(element, |
| 80 | Constants._TAG_X509SUBJECTNAME); |
| 81 | |
| 82 | if ((this._x509childNodes != null) |
| 83 | && (this._x509childNodes.length > 0)) { |
| 84 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Yes Sir, I can"); |
| 85 | |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "I can't"); |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Method engineResolvePublicKey |
| 97 | * |
| 98 | * @param element |
| 99 | * @param BaseURI |
| 100 | * @param storage |
| 101 | * @return null if no {@link PublicKey} could be obtained |
| 102 | * @throws KeyResolverException |
| 103 | */ |
| 104 | public PublicKey engineResolvePublicKey( |
| 105 | Element element, String BaseURI, StorageResolver storage) |
| 106 | throws KeyResolverException { |
| 107 | |
| 108 | X509Certificate cert = this.engineResolveX509Certificate(element, |
| 109 | BaseURI, storage); |
| 110 | |
| 111 | if (cert != null) { |
| 112 | return cert.getPublicKey(); |
| 113 | } |
| 114 | |
| 115 | return null; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Method engineResolveX509Certificate |
| 120 | * @inheritDoc |
| 121 | * @param element |
| 122 | * @param BaseURI |
| 123 | * @param storage |
| 124 | * |
| 125 | * @throws KeyResolverException |
| 126 | */ |
| 127 | public X509Certificate engineResolveX509Certificate( |
| 128 | Element element, String BaseURI, StorageResolver storage) |
| 129 | throws KeyResolverException { |
| 130 | |
| 131 | try { |
| 132 | if (this._x509childNodes == null) { |
| 133 | boolean weCanResolve = this.engineCanResolve(element, BaseURI, |
| 134 | storage); |
| 135 | |
| 136 | if (!weCanResolve || (this._x509childNodes == null)) { |
| 137 | return null; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (storage == null) { |
| 142 | Object exArgs[] = { Constants._TAG_X509SUBJECTNAME }; |
| 143 | KeyResolverException ex = |
| 144 | new KeyResolverException("KeyResolver.needStorageResolver", |
| 145 | exArgs); |
| 146 | |
| 147 | if (log.isLoggable(java.util.logging.Level.INFO)) log.log(java.util.logging.Level.INFO, "", ex); |
| 148 | |
| 149 | throw ex; |
| 150 | } |
| 151 | |
| 152 | this._x509childObject = |
| 153 | new XMLX509SubjectName[this._x509childNodes.length]; |
| 154 | |
| 155 | for (int i = 0; i < this._x509childNodes.length; i++) { |
| 156 | this._x509childObject[i] = |
| 157 | new XMLX509SubjectName(this._x509childNodes[i], |
| 158 | BaseURI); |
| 159 | } |
| 160 | |
| 161 | while (storage.hasNext()) { |
| 162 | X509Certificate cert = storage.next(); |
| 163 | XMLX509SubjectName certSN = |
| 164 | new XMLX509SubjectName(element.getOwnerDocument(), cert); |
| 165 | |
| 166 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Certificate SN: " + certSN.getSubjectName()); |
| 167 | |
| 168 | for (int i = 0; i < this._x509childObject.length; i++) { |
| 169 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Found Element SN: " |
| 170 | + this._x509childObject[i].getSubjectName()); |
| 171 | |
| 172 | if (certSN.equals(this._x509childObject[i])) { |
| 173 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "match !!! "); |
| 174 | |
| 175 | return cert; |
| 176 | } |
| 177 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "no match..."); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return null; |
| 182 | } catch (XMLSecurityException ex) { |
| 183 | if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex); |
| 184 | |
| 185 | throw new KeyResolverException("generic.EmptyMessage", ex); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Method engineResolveSecretKey |
| 191 | * @inheritDoc |
| 192 | * @param element |
| 193 | * @param BaseURI |
| 194 | * @param storage |
| 195 | * |
| 196 | */ |
| 197 | public javax.crypto.SecretKey engineResolveSecretKey( |
| 198 | Element element, String BaseURI, StorageResolver storage) |
| 199 | { |
| 200 | return null; |
| 201 | } |
| 202 | } |