blob: 4b44f1c50d15434bf71c3bf98cf349f4921422bd [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 */
21package com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations;
22
23import java.security.Key;
24import java.security.PublicKey;
25import java.security.cert.X509Certificate;
26
27import javax.crypto.SecretKey;
28
29import com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
30import com.sun.org.apache.xml.internal.security.encryption.XMLCipher;
31import com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
32import com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
33import com.sun.org.apache.xml.internal.security.utils.EncryptionConstants;
34import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
35import org.w3c.dom.Element;
36
37
38/**
39 * The <code>EncryptedKeyResolver</code> is not a generic resolver. It can
40 * only be for specific instantiations, as the key being unwrapped will
41 * always be of a particular type and will always have been wrapped by
42 * another key which needs to be recursively resolved.
43 *
44 * The <code>EncryptedKeyResolver</code> can therefore only be instantiated
45 * with an algorithm. It can also be instantiated with a key (the KEK) or
46 * will search the static KeyResolvers to find the appropriate key.
47 *
48 * @author Berin Lautenbach
49 */
50
51public class EncryptedKeyResolver extends KeyResolverSpi {
52
53 /** {@link java.util.logging} logging facility */
54 static java.util.logging.Logger log =
55 java.util.logging.Logger.getLogger(
56 RSAKeyValueResolver.class.getName());
57
58
59 Key _key;
60 Key _kek;
61 String _algorithm;
62
63 /**
64 * Constructor for use when a KEK needs to be derived from a KeyInfo
65 * list
66 * @param algorithm
67 */
68 public EncryptedKeyResolver(String algorithm) {
69 _key = null;
70 _kek = null;
71 _algorithm=algorithm;
72 }
73
74 /**
75 * Constructor used for when a KEK has been set
76 * @param algorithm
77 * @param kek
78 */
79
80 public EncryptedKeyResolver(String algorithm, Key kek) {
81 _key = null;
82 _algorithm = algorithm;
83 _kek = kek;
84
85 }
86
87 /**
88 * Method engineCanResolve
89 *
90 * @param element
91 * @param BaseURI
92 * @param storage
93 * @return true if can resolve the key in the element
94 *
95 */
96
97 public boolean engineCanResolve(Element element, String BaseURI,
98 StorageResolver storage) {
99 if (true)
100 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "EncryptedKeyResolver - Can I resolve " + element.getTagName());
101
102 if (element == null) {
103 return false;
104 }
105
106 boolean isEncryptedKey = XMLUtils.elementIsInEncryptionSpace(element,
107 EncryptionConstants._TAG_ENCRYPTEDKEY);
108
109 if (isEncryptedKey) {
110 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Passed an Encrypted Key");
111 try {
112 XMLCipher cipher = XMLCipher.getInstance();
113 cipher.init(XMLCipher.UNWRAP_MODE, _kek);
114 EncryptedKey ek = cipher.loadEncryptedKey(element);
115 _key = cipher.decryptKey(ek, _algorithm);
116 }
117 catch (Exception e) {}
118 }
119
120 return (_key != null);
121 }
122
123 /** @inheritDoc */
124 public PublicKey engineResolvePublicKey(
125 Element element, String BaseURI, StorageResolver storage) {
126
127 return null;
128 }
129
130 /** @inheritDoc */
131 public X509Certificate engineResolveX509Certificate(
132 Element element, String BaseURI, StorageResolver storage) {
133 return null;
134 }
135
136 /** @inheritDoc */
137 public javax.crypto.SecretKey engineResolveSecretKey(
138 Element element, String BaseURI, StorageResolver storage) {
139 return (SecretKey) _key;
140 }
141}