|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
package com.sun.org.apache.xml.internal.security.keys.keyresolver; |
|
|
|
import java.lang.reflect.InvocationTargetException; |
|
import java.security.PublicKey; |
|
import java.security.cert.X509Certificate; |
|
import java.util.ArrayList; |
|
import java.util.Iterator; |
|
import java.util.List; |
|
import java.util.concurrent.CopyOnWriteArrayList; |
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
import org.w3c.dom.Element; |
|
import org.w3c.dom.Node; |
|
|
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.DEREncodedKeyValueResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.DSAKeyValueResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.ECKeyValueResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.KeyInfoReferenceResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.RSAKeyValueResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.RetrievalMethodResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.X509CertificateResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.X509DigestResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.X509IssuerSerialResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.X509SKIResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations.X509SubjectNameResolver; |
|
import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver; |
|
import com.sun.org.apache.xml.internal.security.utils.JavaUtils; |
|
|
|
|
|
|
|
|
|
*/ |
|
public class KeyResolver { |
|
|
|
private static final com.sun.org.slf4j.internal.Logger LOG = |
|
com.sun.org.slf4j.internal.LoggerFactory.getLogger(KeyResolver.class); |
|
|
|
private static List<KeyResolverSpi> resolverList = new CopyOnWriteArrayList<>(); |
|
|
|
private static final AtomicBoolean defaultResolversAdded = new AtomicBoolean(); |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static int length() { |
|
return resolverList.size(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static final X509Certificate getX509Certificate( |
|
Element element, String baseURI, StorageResolver storage, boolean secureValidation |
|
) throws KeyResolverException { |
|
for (KeyResolverSpi resolver : resolverList) { |
|
if (resolver == null) { |
|
Object[] exArgs = { |
|
element != null |
|
&& element.getNodeType() == Node.ELEMENT_NODE |
|
? element.getTagName() : "null" |
|
}; |
|
|
|
throw new KeyResolverException("utils.resolver.noClass", exArgs); |
|
} |
|
LOG.debug("check resolvability by class {}", resolver.getClass()); |
|
|
|
X509Certificate cert = resolver.engineLookupResolveX509Certificate(element, baseURI, storage, secureValidation); |
|
if (cert != null) { |
|
return cert; |
|
} |
|
} |
|
|
|
Object[] exArgs = { |
|
element != null && element.getNodeType() == Node.ELEMENT_NODE |
|
? element.getTagName() : "null" |
|
}; |
|
|
|
throw new KeyResolverException("utils.resolver.noClass", exArgs); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static final PublicKey getPublicKey( |
|
Element element, String baseURI, StorageResolver storage, boolean secureValidation |
|
) throws KeyResolverException { |
|
for (KeyResolverSpi resolver : resolverList) { |
|
if (resolver == null) { |
|
Object[] exArgs = { |
|
element != null |
|
&& element.getNodeType() == Node.ELEMENT_NODE |
|
? element.getTagName() : "null" |
|
}; |
|
|
|
throw new KeyResolverException("utils.resolver.noClass", exArgs); |
|
} |
|
LOG.debug("check resolvability by class {}", resolver.getClass()); |
|
|
|
PublicKey cert = resolver.engineLookupAndResolvePublicKey(element, baseURI, storage, secureValidation); |
|
if (cert != null) { |
|
return cert; |
|
} |
|
} |
|
|
|
Object[] exArgs = { |
|
element != null && element.getNodeType() == Node.ELEMENT_NODE |
|
? element.getTagName() : "null" |
|
}; |
|
|
|
throw new KeyResolverException("utils.resolver.noClass", exArgs); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static void register(String className) throws |
|
ClassNotFoundException, IllegalAccessException, |
|
InstantiationException, InvocationTargetException { |
|
JavaUtils.checkRegisterPermission(); |
|
KeyResolverSpi keyResolverSpi = |
|
(KeyResolverSpi) JavaUtils.newInstanceWithEmptyConstructor(ClassLoaderUtils.loadClass(className, KeyResolver.class)); |
|
register(keyResolverSpi, false); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static void registerAtStart(String className) { |
|
JavaUtils.checkRegisterPermission(); |
|
KeyResolverSpi keyResolverSpi = null; |
|
Exception ex = null; |
|
try { |
|
keyResolverSpi = (KeyResolverSpi) JavaUtils.newInstanceWithEmptyConstructor( |
|
ClassLoaderUtils.loadClass(className, KeyResolver.class)); |
|
register(keyResolverSpi, true); |
|
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException | InvocationTargetException e) { |
|
ex = e; |
|
} |
|
|
|
if (ex != null) { |
|
throw (IllegalArgumentException) new |
|
IllegalArgumentException("Invalid KeyResolver class name").initCause(ex); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static void register( |
|
KeyResolverSpi keyResolverSpi, |
|
boolean start |
|
) { |
|
JavaUtils.checkRegisterPermission(); |
|
if (start) { |
|
resolverList.add(0, keyResolverSpi); |
|
} else { |
|
resolverList.add(keyResolverSpi); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static void registerClassNames(List<String> classNames) |
|
throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { |
|
JavaUtils.checkRegisterPermission(); |
|
List<KeyResolverSpi> keyResolverList = new ArrayList<>(classNames.size()); |
|
for (String className : classNames) { |
|
KeyResolverSpi keyResolverSpi = (KeyResolverSpi)JavaUtils |
|
.newInstanceWithEmptyConstructor(ClassLoaderUtils.loadClass(className, KeyResolver.class)); |
|
keyResolverList.add(keyResolverSpi); |
|
} |
|
resolverList.addAll(keyResolverList); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public static void registerDefaultResolvers() { |
|
|
|
if (defaultResolversAdded.compareAndSet(false, true)) { |
|
List<KeyResolverSpi> keyResolverList = new ArrayList<>(); |
|
keyResolverList.add(new RSAKeyValueResolver()); |
|
keyResolverList.add(new DSAKeyValueResolver()); |
|
keyResolverList.add(new X509CertificateResolver()); |
|
keyResolverList.add(new X509SKIResolver()); |
|
keyResolverList.add(new RetrievalMethodResolver()); |
|
keyResolverList.add(new X509SubjectNameResolver()); |
|
keyResolverList.add(new X509IssuerSerialResolver()); |
|
keyResolverList.add(new DEREncodedKeyValueResolver()); |
|
keyResolverList.add(new KeyInfoReferenceResolver()); |
|
keyResolverList.add(new X509DigestResolver()); |
|
keyResolverList.add(new ECKeyValueResolver()); |
|
|
|
resolverList.addAll(keyResolverList); |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
static class ResolverIterator implements Iterator<KeyResolverSpi> { |
|
private List<KeyResolverSpi> res; |
|
private Iterator<KeyResolverSpi> it; |
|
|
|
public ResolverIterator(List<KeyResolverSpi> list) { |
|
res = list; |
|
it = res.iterator(); |
|
} |
|
|
|
public boolean hasNext() { |
|
return it.hasNext(); |
|
} |
|
|
|
public KeyResolverSpi next() { |
|
KeyResolverSpi resolver = it.next(); |
|
if (resolver == null) { |
|
throw new RuntimeException("utils.resolver.noClass"); |
|
} |
|
|
|
return resolver; |
|
} |
|
|
|
public void remove() { |
|
throw new UnsupportedOperationException("Can't remove resolvers using the iterator"); |
|
} |
|
} |
|
|
|
public static Iterator<KeyResolverSpi> iterator() { |
|
return new ResolverIterator(resolverList); |
|
} |
|
} |