1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations;
6
7import java.security.Key;
8import java.security.KeyStore;
9import java.security.PrivateKey;
10import java.security.PublicKey;
11import java.security.cert.X509Certificate;
12import javax.crypto.SecretKey;
13import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverException;
14import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
15import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
16import com.sun.org.apache.xml.internal.security.utils.Constants;
17import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
18import org.w3c.dom.Element;
19
20/**
21 * Resolves a SecretKey within a KeyStore based on the KeyName.
22 * The KeyName is the key entry alias within the KeyStore.
23 */
24public class SecretKeyResolver extends KeyResolverSpi
25{
26    /** {@link org.apache.commons.logging} logging facility */
27    private static java.util.logging.Logger log =
28        java.util.logging.Logger.getLogger(SecretKeyResolver.class.getName());
29
30    private KeyStore keyStore;
31    private char[] password;
32
33    /**
34     * Constructor.
35     */
36    public SecretKeyResolver(KeyStore keyStore, char[] password) {
37        this.keyStore = keyStore;
38        this.password = password;
39    }
40
41    /**
42     * This method returns whether the KeyResolverSpi is able to perform the requested action.
43     *
44     * @param element
45     * @param baseURI
46     * @param storage
47     * @return whether the KeyResolverSpi is able to perform the requested action.
48     */
49    public boolean engineCanResolve(Element element, String baseURI, StorageResolver storage) {
50        return XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME);
51    }
52
53    /**
54     * Method engineLookupAndResolvePublicKey
55     *
56     * @param element
57     * @param baseURI
58     * @param storage
59     * @return null if no {@link PublicKey} could be obtained
60     * @throws KeyResolverException
61     */
62    public PublicKey engineLookupAndResolvePublicKey(
63        Element element, String baseURI, StorageResolver storage
64    ) throws KeyResolverException {
65        return null;
66    }
67
68    /**
69     * Method engineResolveX509Certificate
70     * @inheritDoc
71     * @param element
72     * @param baseURI
73     * @param storage
74     * @throws KeyResolverException
75     */
76    public X509Certificate engineLookupResolveX509Certificate(
77        Element element, String baseURI, StorageResolver storage
78    ) throws KeyResolverException {
79        return null;
80    }
81
82    /**
83     * Method engineResolveSecretKey
84     *
85     * @param element
86     * @param baseURI
87     * @param storage
88     * @return resolved SecretKey key or null if no {@link SecretKey} could be obtained
89     *
90     * @throws KeyResolverException
91     */
92    public SecretKey engineResolveSecretKey(
93        Element element, String baseURI, StorageResolver storage
94    ) throws KeyResolverException {
95        if (log.isLoggable(java.util.logging.Level.FINE)) {
96            log.log(java.util.logging.Level.FINE, "Can I resolve " + element.getTagName() + "?");
97        }
98
99        if (XMLUtils.elementIsInSignatureSpace(element, Constants._TAG_KEYNAME)) {
100            String keyName = element.getFirstChild().getNodeValue();
101            try {
102                Key key = keyStore.getKey(keyName, password);
103                if (key instanceof SecretKey) {
104                    return (SecretKey) key;
105                }
106            } catch (Exception e) {
107                log.log(java.util.logging.Level.FINE, "Cannot recover the key", e);
108            }
109        }
110
111        log.log(java.util.logging.Level.FINE, "I can't");
112        return null;
113    }
114
115    /**
116     * Method engineResolvePrivateKey
117     * @inheritDoc
118     * @param element
119     * @param baseURI
120     * @param storage
121     * @return resolved PrivateKey key or null if no {@link PrivateKey} could be obtained
122     * @throws KeyResolverException
123     */
124    public PrivateKey engineLookupAndResolvePrivateKey(
125        Element element, String baseURI, StorageResolver storage
126    ) throws KeyResolverException {
127        return null;
128    }
129}
130