0N/A/*
0N/A * reserved comment block
0N/A * DO NOT REMOVE OR ALTER!
0N/A */
0N/A/*
0N/A * Copyright 1999-2004 The Apache Software Foundation.
0N/A *
0N/A * Licensed under the Apache License, Version 2.0 (the "License");
0N/A * you may not use this file except in compliance with the License.
0N/A * You may obtain a copy of the License at
0N/A *
0N/A * http://www.apache.org/licenses/LICENSE-2.0
0N/A *
0N/A * Unless required by applicable law or agreed to in writing, software
0N/A * distributed under the License is distributed on an "AS IS" BASIS,
0N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0N/A * See the License for the specific language governing permissions and
0N/A * limitations under the License.
0N/A *
0N/A */
0N/Apackage com.sun.org.apache.xml.internal.security.keys.keyresolver.implementations;
0N/A
0N/Aimport java.security.Key;
0N/Aimport java.security.PublicKey;
0N/Aimport java.security.cert.X509Certificate;
0N/A
0N/Aimport javax.crypto.SecretKey;
0N/A
0N/Aimport com.sun.org.apache.xml.internal.security.encryption.EncryptedKey;
0N/Aimport com.sun.org.apache.xml.internal.security.encryption.XMLCipher;
0N/Aimport com.sun.org.apache.xml.internal.security.keys.keyresolver.KeyResolverSpi;
0N/Aimport com.sun.org.apache.xml.internal.security.keys.storage.StorageResolver;
0N/Aimport com.sun.org.apache.xml.internal.security.utils.EncryptionConstants;
0N/Aimport com.sun.org.apache.xml.internal.security.utils.XMLUtils;
0N/Aimport org.w3c.dom.Element;
0N/A
0N/A
0N/A/**
0N/A * The <code>EncryptedKeyResolver</code> is not a generic resolver. It can
0N/A * only be for specific instantiations, as the key being unwrapped will
0N/A * always be of a particular type and will always have been wrapped by
0N/A * another key which needs to be recursively resolved.
0N/A *
0N/A * The <code>EncryptedKeyResolver</code> can therefore only be instantiated
0N/A * with an algorithm. It can also be instantiated with a key (the KEK) or
0N/A * will search the static KeyResolvers to find the appropriate key.
0N/A *
0N/A * @author Berin Lautenbach
0N/A */
0N/A
0N/Apublic class EncryptedKeyResolver extends KeyResolverSpi {
0N/A
0N/A /** {@link java.util.logging} logging facility */
0N/A static java.util.logging.Logger log =
0N/A java.util.logging.Logger.getLogger(
0N/A RSAKeyValueResolver.class.getName());
0N/A
0N/A
0N/A Key _kek;
0N/A String _algorithm;
0N/A
0N/A /**
0N/A * Constructor for use when a KEK needs to be derived from a KeyInfo
0N/A * list
0N/A * @param algorithm
0N/A */
0N/A public EncryptedKeyResolver(String algorithm) {
0N/A _kek = null;
0N/A _algorithm=algorithm;
0N/A }
0N/A
0N/A /**
0N/A * Constructor used for when a KEK has been set
0N/A * @param algorithm
0N/A * @param kek
0N/A */
0N/A
0N/A public EncryptedKeyResolver(String algorithm, Key kek) {
0N/A _algorithm = algorithm;
0N/A _kek = kek;
0N/A
0N/A }
0N/A
0N/A /** @inheritDoc */
661N/A public PublicKey engineLookupAndResolvePublicKey(
0N/A Element element, String BaseURI, StorageResolver storage) {
0N/A
0N/A return null;
0N/A }
0N/A
0N/A /** @inheritDoc */
661N/A public X509Certificate engineLookupResolveX509Certificate(
0N/A Element element, String BaseURI, StorageResolver storage) {
0N/A return null;
0N/A }
0N/A
0N/A /** @inheritDoc */
661N/A public javax.crypto.SecretKey engineLookupAndResolveSecretKey(
0N/A Element element, String BaseURI, StorageResolver storage) {
661N/A SecretKey key=null;
661N/A if (log.isLoggable(java.util.logging.Level.FINE))
661N/A log.log(java.util.logging.Level.FINE, "EncryptedKeyResolver - Can I resolve " + element.getTagName());
661N/A
661N/A if (element == null) {
661N/A return null;
661N/A }
661N/A
661N/A boolean isEncryptedKey = XMLUtils.elementIsInEncryptionSpace(element,
661N/A EncryptionConstants._TAG_ENCRYPTEDKEY);
661N/A
661N/A if (isEncryptedKey) {
661N/A log.log(java.util.logging.Level.FINE, "Passed an Encrypted Key");
661N/A try {
661N/A XMLCipher cipher = XMLCipher.getInstance();
661N/A cipher.init(XMLCipher.UNWRAP_MODE, _kek);
661N/A EncryptedKey ek = cipher.loadEncryptedKey(element);
661N/A key = (SecretKey) cipher.decryptKey(ek, _algorithm);
661N/A }
661N/A catch (Exception e) {}
661N/A }
661N/A
661N/A return key;
0N/A }
0N/A}