Path: blob/master/src/java.xml.crypto/share/classes/javax/xml/crypto/KeySelector.java
41159 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25* $Id: KeySelector.java,v 1.6 2005/05/10 15:47:42 mullan Exp $26*/27package javax.xml.crypto;2829import java.security.Key;30import javax.xml.crypto.dsig.keyinfo.KeyInfo;31import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;3233/**34* A selector that finds and returns a key using the data contained in a35* {@link KeyInfo} object. An example of an implementation of36* this class is one that searches a {@link java.security.KeyStore} for37* trusted keys that match information contained in a <code>KeyInfo</code>.38*39* <p>Whether or not the returned key is trusted and the mechanisms40* used to determine that is implementation-specific.41*42* @author Sean Mullan43* @author JSR 105 Expert Group44* @since 1.645*/46public abstract class KeySelector {4748/**49* The purpose of the key that is to be selected.50*/51public static class Purpose {5253private final String name;5455private Purpose(String name) { this.name = name; }5657/**58* Returns a string representation of this purpose ("sign",59* "verify", "encrypt", or "decrypt").60*61* @return a string representation of this purpose62*/63public String toString() { return name; }6465/**66* A key for signing.67*/68public static final Purpose SIGN = new Purpose("sign");69/**70* A key for verifying.71*/72public static final Purpose VERIFY = new Purpose("verify");73/**74* A key for encrypting.75*/76public static final Purpose ENCRYPT = new Purpose("encrypt");77/**78* A key for decrypting.79*/80public static final Purpose DECRYPT = new Purpose("decrypt");81}8283/**84* Default no-args constructor; intended for invocation by subclasses only.85*/86protected KeySelector() {}8788/**89* Attempts to find a key that satisfies the specified constraints.90*91* @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>)92* @param purpose the key's purpose ({@link Purpose#SIGN},93* {@link Purpose#VERIFY}, {@link Purpose#ENCRYPT}, or94* {@link Purpose#DECRYPT})95* @param method the algorithm method that this key is to be used for.96* Only keys that are compatible with the algorithm and meet the97* constraints of the specified algorithm should be returned.98* @param context an <code>XMLCryptoContext</code> that may contain99* useful information for finding an appropriate key. If this key100* selector supports resolving {@link RetrievalMethod} types, the101* context's <code>baseURI</code> and <code>dereferencer</code>102* parameters (if specified) should be used by the selector to103* resolve and dereference the URI.104* @return the result of the key selector105* @throws KeySelectorException if an exceptional condition occurs while106* attempting to find a key. Note that an inability to find a key is not107* considered an exception (<code>null</code> should be108* returned in that case). However, an error condition (ex: network109* communications failure) that prevented the <code>KeySelector</code>110* from finding a potential key should be considered an exception.111* @throws ClassCastException if the data type of <code>method</code>112* is not supported by this key selector113*/114public abstract KeySelectorResult select(KeyInfo keyInfo, Purpose purpose,115AlgorithmMethod method, XMLCryptoContext context)116throws KeySelectorException;117118/**119* Returns a <code>KeySelector</code> that always selects the specified120* key, regardless of the <code>KeyInfo</code> passed to it.121*122* @param key the sole key to be stored in the key selector123* @return a key selector that always selects the specified key124* @throws NullPointerException if <code>key</code> is <code>null</code>125*/126public static KeySelector singletonKeySelector(Key key) {127return new SingletonKeySelector(key);128}129130private static class SingletonKeySelector extends KeySelector {131private final Key key;132133SingletonKeySelector(Key key) {134if (key == null) {135throw new NullPointerException();136}137this.key = key;138}139140public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose,141AlgorithmMethod method, XMLCryptoContext context)142throws KeySelectorException {143144return new KeySelectorResult() {145public Key getKey() {146return key;147}148};149}150}151}152153154