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 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException; 29 import com.sun.org.apache.xml.internal.security.keys.content.X509Data; 30 import com.sun.org.apache.xml.internal.security.keys.content.x509.XMLX509IssuerSerial; 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.signature.XMLSignatureException; 35 import com.sun.org.apache.xml.internal.security.utils.Constants; 36 import org.w3c.dom.Element; 37 38 39 /** 40 * 41 * @author $Author: mullan $ 42 */ 43 public class X509IssuerSerialResolver extends KeyResolverSpi { 44 45 /** {@link java.util.logging} logging facility */ 46 static java.util.logging.Logger log = 47 java.util.logging.Logger.getLogger( 48 X509IssuerSerialResolver.class.getName()); 49 50 51 /** @inheritDoc */ 52 public PublicKey engineLookupAndResolvePublicKey( 53 Element element, String BaseURI, StorageResolver storage) 54 throws KeyResolverException { 55 56 X509Certificate cert = this.engineLookupResolveX509Certificate(element, 57 BaseURI, storage); 58 59 if (cert != null) { 60 return cert.getPublicKey(); 61 } 62 63 return null; 64 } 65 66 /** @inheritDoc */ 67 public X509Certificate engineLookupResolveX509Certificate( 68 Element element, String BaseURI, StorageResolver storage) 69 throws KeyResolverException { 70 if (log.isLoggable(java.util.logging.Level.FINE)) 71 log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?"); 72 73 X509Data x509data = null; 74 try { 75 x509data = new X509Data(element, BaseURI); 76 } catch (XMLSignatureException ex) { 77 log.log(java.util.logging.Level.FINE, "I can't"); 78 return null; 79 } catch (XMLSecurityException ex) { 80 log.log(java.util.logging.Level.FINE, "I can't"); 81 return null; 82 } 83 84 if (x509data == null) { 85 log.log(java.util.logging.Level.FINE, "I can't"); 86 return null; 87 } 88 89 if (!x509data.containsIssuerSerial()) { 90 return null; 91 } 92 try { 93 if (storage == null) { 94 Object exArgs[] = { Constants._TAG_X509ISSUERSERIAL }; 95 KeyResolverException ex = 96 new KeyResolverException("KeyResolver.needStorageResolver", 97 exArgs); 98 99 log.log(java.util.logging.Level.INFO, "", ex); 100 throw ex; 101 } 102 103 int noOfISS = x509data.lengthIssuerSerial(); 104 105 while (storage.hasNext()) { 106 X509Certificate cert = storage.next(); 107 XMLX509IssuerSerial certSerial = new XMLX509IssuerSerial(element.getOwnerDocument(), cert); 108 109 if (log.isLoggable(java.util.logging.Level.FINE)) { 110 log.log(java.util.logging.Level.FINE, "Found Certificate Issuer: " 111 + certSerial.getIssuerName()); 112 log.log(java.util.logging.Level.FINE, "Found Certificate Serial: " 113 + certSerial.getSerialNumber().toString()); 114 } 115 116 for (int i=0; i<noOfISS; i++) { 117 XMLX509IssuerSerial xmliss = x509data.itemIssuerSerial(i); 118 119 if (log.isLoggable(java.util.logging.Level.FINE)) { 120 log.log(java.util.logging.Level.FINE, "Found Element Issuer: " 121 + xmliss.getIssuerName()); 122 log.log(java.util.logging.Level.FINE, "Found Element Serial: " 123 + xmliss.getSerialNumber().toString()); 124 } 125 126 if (certSerial.equals(xmliss)) { 127 log.log(java.util.logging.Level.FINE, "match !!! "); 128 129 return cert; 130 } 131 log.log(java.util.logging.Level.FINE, "no match..."); 132 } 133 } 134 135 return null; 136 } catch (XMLSecurityException ex) { 137 log.log(java.util.logging.Level.FINE, "XMLSecurityException", ex); 138 139 throw new KeyResolverException("generic.EmptyMessage", ex); 140 } 141 } 142 143 /** @inheritDoc */ 144 public javax.crypto.SecretKey engineLookupAndResolveSecretKey( 145 Element element, String BaseURI, StorageResolver storage) { 146 return null; 147 } 148 }