0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A * $Id: KeySelector.java,v 1.6 2005/05/10 15:47:42 mullan Exp $
0N/A */
0N/Apackage javax.xml.crypto;
0N/A
0N/Aimport java.security.Key;
0N/Aimport javax.xml.crypto.dsig.keyinfo.KeyInfo;
0N/Aimport javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
0N/A
0N/A/**
0N/A * A selector that finds and returns a key using the data contained in a
0N/A * {@link KeyInfo} object. An example of an implementation of
0N/A * this class is one that searchs a {@link java.security.KeyStore} for
0N/A * trusted keys that match information contained in a <code>KeyInfo</code>.
0N/A *
0N/A * <p>Whether or not the returned key is trusted and the mechanisms
0N/A * used to determine that is implementation-specific.
0N/A *
0N/A * @author Sean Mullan
0N/A * @author JSR 105 Expert Group
0N/A * @since 1.6
0N/A */
0N/Apublic abstract class KeySelector {
0N/A
0N/A /**
0N/A * The purpose of the key that is to be selected.
0N/A */
0N/A public static class Purpose {
0N/A
0N/A private final String name;
0N/A
0N/A private Purpose(String name) { this.name = name; }
0N/A
0N/A /**
0N/A * Returns a string representation of this purpose ("sign",
0N/A * "verify", "encrypt", or "decrypt").
0N/A *
0N/A * @return a string representation of this purpose
0N/A */
0N/A public String toString() { return name; }
0N/A
0N/A /**
0N/A * A key for signing.
0N/A */
0N/A public static final Purpose SIGN = new Purpose("sign");
0N/A /**
0N/A * A key for verifying.
0N/A */
0N/A public static final Purpose VERIFY = new Purpose("verify");
0N/A /**
0N/A * A key for encrypting.
0N/A */
0N/A public static final Purpose ENCRYPT = new Purpose("encrypt");
0N/A /**
0N/A * A key for decrypting.
0N/A */
0N/A public static final Purpose DECRYPT = new Purpose("decrypt");
0N/A }
0N/A
0N/A /**
0N/A * Default no-args constructor; intended for invocation by subclasses only.
0N/A */
0N/A protected KeySelector() {}
0N/A
0N/A /**
0N/A * Attempts to find a key that satisfies the specified constraints.
0N/A *
0N/A * @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>)
0N/A * @param purpose the key's purpose ({@link Purpose#SIGN},
0N/A * {@link Purpose#VERIFY}, {@link Purpose#ENCRYPT}, or
0N/A * {@link Purpose#DECRYPT})
0N/A * @param method the algorithm method that this key is to be used for.
0N/A * Only keys that are compatible with the algorithm and meet the
0N/A * constraints of the specified algorithm should be returned.
0N/A * @param context an <code>XMLCryptoContext</code> that may contain
0N/A * useful information for finding an appropriate key. If this key
0N/A * selector supports resolving {@link RetrievalMethod} types, the
0N/A * context's <code>baseURI</code> and <code>dereferencer</code>
0N/A * parameters (if specified) should be used by the selector to
0N/A * resolve and dereference the URI.
0N/A * @return the result of the key selector
0N/A * @throws KeySelectorException if an exceptional condition occurs while
0N/A * attempting to find a key. Note that an inability to find a key is not
0N/A * considered an exception (<code>null</code> should be
0N/A * returned in that case). However, an error condition (ex: network
0N/A * communications failure) that prevented the <code>KeySelector</code>
0N/A * from finding a potential key should be considered an exception.
0N/A * @throws ClassCastException if the data type of <code>method</code>
0N/A * is not supported by this key selector
0N/A */
0N/A public abstract KeySelectorResult select(KeyInfo keyInfo, Purpose purpose,
0N/A AlgorithmMethod method, XMLCryptoContext context)
0N/A throws KeySelectorException;
0N/A
0N/A /**
0N/A * Returns a <code>KeySelector</code> that always selects the specified
0N/A * key, regardless of the <code>KeyInfo</code> passed to it.
0N/A *
0N/A * @param key the sole key to be stored in the key selector
0N/A * @return a key selector that always selects the specified key
0N/A * @throws NullPointerException if <code>key</code> is <code>null</code>
0N/A */
0N/A public static KeySelector singletonKeySelector(Key key) {
0N/A return new SingletonKeySelector(key);
0N/A }
0N/A
0N/A private static class SingletonKeySelector extends KeySelector {
0N/A private final Key key;
0N/A
0N/A SingletonKeySelector(Key key) {
0N/A if (key == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A this.key = key;
0N/A }
0N/A
0N/A public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose,
0N/A AlgorithmMethod method, XMLCryptoContext context)
0N/A throws KeySelectorException {
0N/A
0N/A return new KeySelectorResult() {
0N/A public Key getKey() {
0N/A return key;
0N/A }
0N/A };
0N/A }
0N/A }
0N/A}