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.XMLX509SKI; 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 * 42 * @author $Author: mullan $ 43 */ 44 public class X509SKIResolver extends KeyResolverSpi { 45 46 /** {@link java.util.logging} logging facility */ 47 static java.util.logging.Logger log = 48 java.util.logging.Logger.getLogger(X509SKIResolver.class.getName()); 49 50 51 /** 52 * Method engineResolvePublicKey 53 * 54 * @param element 55 * @param BaseURI 56 * @param storage 57 * @return null if no {@link PublicKey} could be obtained 58 * @throws KeyResolverException 59 */ 60 public PublicKey engineLookupAndResolvePublicKey( 61 Element element, String BaseURI, StorageResolver storage) 62 throws KeyResolverException { 63 64 X509Certificate cert = this.engineLookupResolveX509Certificate(element, 65 BaseURI, storage); 66 67 if (cert != null) { 68 return cert.getPublicKey(); 69 } 70 71 return null; 72 } 73 74 /** 75 * Method engineResolveX509Certificate 76 * @inheritDoc 77 * @param element 78 * @param BaseURI 79 * @param storage 80 * 81 * @throws KeyResolverException 82 */ 83 public X509Certificate engineLookupResolveX509Certificate( 84 Element element, String BaseURI, StorageResolver storage) 85 throws KeyResolverException { 86 if (log.isLoggable(java.util.logging.Level.FINE)) { 87 log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?"); 88 } 89 if (!XMLUtils.elementIsInSignatureSpace(element, 90 Constants._TAG_X509DATA)) { 91 log.log(java.util.logging.Level.FINE, "I can't"); 92 return null; 93 } 94 /** Field _x509childObject[] */ 95 XMLX509SKI x509childObject[] = null; 96 97 Element x509childNodes[] = null; 98 x509childNodes = XMLUtils.selectDsNodes(element.getFirstChild(), 99 Constants._TAG_X509SKI); 100 101 if (!((x509childNodes != null) 102 && (x509childNodes.length > 0))) { 103 log.log(java.util.logging.Level.FINE, "I can't"); 104 return null; 105 } 106 try { 107 if (storage == null) { 108 Object exArgs[] = { Constants._TAG_X509SKI }; 109 KeyResolverException ex = 110 new KeyResolverException("KeyResolver.needStorageResolver", 111 exArgs); 112 113 log.log(java.util.logging.Level.INFO, "", ex); 114 115 throw ex; 116 } 117 118 x509childObject = new XMLX509SKI[x509childNodes.length]; 119 120 for (int i = 0; i < x509childNodes.length; i++) { 121 x509childObject[i] = 122 new XMLX509SKI(x509childNodes[i], BaseURI); 123 } 124 125 while (storage.hasNext()) { 126 X509Certificate cert = storage.next(); 127 XMLX509SKI certSKI = new XMLX509SKI(element.getOwnerDocument(), cert); 128 129 for (int i = 0; i < x509childObject.length; i++) { 130 if (certSKI.equals(x509childObject[i])) { 131 log.log(java.util.logging.Level.FINE, "Return PublicKey from " 132 + cert.getSubjectDN().getName()); 133 134 return cert; 135 } 136 } 137 } 138 } catch (XMLSecurityException ex) { 139 throw new KeyResolverException("empty", ex); 140 } 141 142 return null; 143 } 144 145 /** 146 * Method engineResolveSecretKey 147 * @inheritDoc 148 * @param element 149 * @param BaseURI 150 * @param storage 151 * 152 */ 153 public javax.crypto.SecretKey engineLookupAndResolveSecretKey( 154 Element element, String BaseURI, StorageResolver storage) 155 { 156 return null; 157 } 158 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations; 24 25 import java.security.PublicKey; 26 import java.security.cert.Certificate; 27 import java.security.cert.X509Certificate; 28 import java.util.Iterator; 29 30 31 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 32 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509SKI; 33 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException; 34 import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi; 35 import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; 36 import com.sun.org.apache.xml.internal.security.utils.Constants; 37 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 38 import org.w3c.dom.Element; 39 40 public class X509SKIResolver extends KeyResolverSpi { 41 42 /** {@link org.apache.commons.logging} logging facility */ 43 private static java.util.logging.Logger log = 44 java.util.logging.Logger.getLogger(X509SKIResolver.class.getName()); 45 46 47 /** 48 * Method engineResolvePublicKey 49 * 50 * @param element 51 * @param baseURI 52 * @param storage 53 * @return null if no {@link PublicKey} could be obtained 54 * @throws KeyResolverException 55 */ 56 public PublicKey engineLookupAndResolvePublicKey( 57 Element element, String baseURI, StorageResolver storage 58 ) throws KeyResolverException { 59 60 X509Certificate cert = 61 this.engineLookupResolveX509Certificate(element, baseURI, storage); 62 63 if (cert != null) { 64 return cert.getPublicKey(); 65 } 66 67 return null; 68 } 69 70 /** 71 * Method engineResolveX509Certificate 72 * @inheritDoc 73 * @param element 74 * @param baseURI 75 * @param storage 76 * 77 * @throws KeyResolverException 78 */ 79 public X509Certificate engineLookupResolveX509Certificate( 80 Element element, String baseURI, StorageResolver storage 81 ) throws KeyResolverException { 82 if (log.isLoggable(java.util.logging.Level.FINE)) { 83 log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?"); 84 } 85 if (!XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_X509DATA)) { 86 if (log.isLoggable(java.util.logging.Level.FINE)) { 87 log.log(java.util.logging.Level.FINE, "I can't"); 88 } 89 return null; 90 } 91 /** Field _x509childObject[] */ 92 XMLX509SKI x509childObject[] = null; 93 94 Element x509childNodes[] = null; 95 x509childNodes = XMLUtils.selectDsNodes(element.getFirstChild(), Constants._TAG_X509SKI); 96 97 if (!((x509childNodes != null) && (x509childNodes.length > 0))) { 98 if (log.isLoggable(java.util.logging.Level.FINE)) { 99 log.log(java.util.logging.Level.FINE, "I can't"); 100 } 101 return null; 102 } 103 try { 104 if (storage == null) { 105 Object exArgs[] = { Constants._TAG_X509SKI }; 106 KeyResolverException ex = 107 new KeyResolverException("KeyResolver.needStorageResolver", exArgs); 108 109 if (log.isLoggable(java.util.logging.Level.FINE)) { 110 log.log(java.util.logging.Level.FINE, "", ex); 111 } 112 113 throw ex; 114 } 115 116 x509childObject = new XMLX509SKI[x509childNodes.length]; 117 118 for (int i = 0; i < x509childNodes.length; i++) { 119 x509childObject[i] = new XMLX509SKI(x509childNodes[i], baseURI); 120 } 121 122 Iterator<Certificate> storageIterator = storage.getIterator(); 123 while (storageIterator.hasNext()) { 124 X509Certificate cert = (X509Certificate)storageIterator.next(); 125 XMLX509SKI certSKI = new XMLX509SKI(element.getOwnerDocument(), cert); 126 127 for (int i = 0; i < x509childObject.length; i++) { 128 if (certSKI.equals(x509childObject[i])) { 129 if (log.isLoggable(java.util.logging.Level.FINE)) { 130 log.log(java.util.logging.Level.FINE, "Return PublicKey from " + cert.getSubjectX500Principal().getName()); 131 } 132 133 return cert; 134 } 135 } 136 } 137 } catch (XMLSecurityException ex) { 138 throw new KeyResolverException("empty", ex); 139 } 140 141 return null; 142 } 143 144 /** 145 * Method engineResolveSecretKey 146 * @inheritDoc 147 * @param element 148 * @param baseURI 149 * @param storage 150 * 151 */ 152 public javax.crypto.SecretKey engineLookupAndResolveSecretKey( 153 Element element, String baseURI, StorageResolver storage 154 ) { 155 return null; 156 } 157 } |