001 /*
002 * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 /*
026 * $Id: KeySelector.java,v 1.6 2005/05/10 15:47:42 mullan Exp $
027 */
028 package javax.xml.crypto;
029
030 import java.security.Key;
031 import javax.xml.crypto.dsig.keyinfo.KeyInfo;
032 import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;
033
034 /**
035 * A selector that finds and returns a key using the data contained in a
036 * {@link KeyInfo} object. An example of an implementation of
037 * this class is one that searchs a {@link java.security.KeyStore} for
038 * trusted keys that match information contained in a <code>KeyInfo</code>.
039 *
040 * <p>Whether or not the returned key is trusted and the mechanisms
041 * used to determine that is implementation-specific.
042 *
043 * @author Sean Mullan
044 * @author JSR 105 Expert Group
045 * @since 1.6
046 */
047 public abstract class KeySelector {
048
049 /**
050 * The purpose of the key that is to be selected.
051 */
052 public static class Purpose {
053
054 private final String name;
055
056 private Purpose(String name) {
057 this .name = name;
058 }
059
060 /**
061 * Returns a string representation of this purpose ("sign",
062 * "verify", "encrypt", or "decrypt").
063 *
064 * @return a string representation of this purpose
065 */
066 public String toString() {
067 return name;
068 }
069
070 /**
071 * A key for signing.
072 */
073 public static final Purpose SIGN = new Purpose("sign");
074 /**
075 * A key for verifying.
076 */
077 public static final Purpose VERIFY = new Purpose("verify");
078 /**
079 * A key for encrypting.
080 */
081 public static final Purpose ENCRYPT = new Purpose("encrypt");
082 /**
083 * A key for decrypting.
084 */
085 public static final Purpose DECRYPT = new Purpose("decrypt");
086 }
087
088 /**
089 * Default no-args constructor; intended for invocation by subclasses only.
090 */
091 protected KeySelector() {
092 }
093
094 /**
095 * Attempts to find a key that satisfies the specified constraints.
096 *
097 * @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>)
098 * @param purpose the key's purpose ({@link Purpose#SIGN},
099 * {@link Purpose#VERIFY}, {@link Purpose#ENCRYPT}, or
100 * {@link Purpose#DECRYPT})
101 * @param method the algorithm method that this key is to be used for.
102 * Only keys that are compatible with the algorithm and meet the
103 * constraints of the specified algorithm should be returned.
104 * @param context an <code>XMLCryptoContext</code> that may contain
105 * useful information for finding an appropriate key. If this key
106 * selector supports resolving {@link RetrievalMethod} types, the
107 * context's <code>baseURI</code> and <code>dereferencer</code>
108 * parameters (if specified) should be used by the selector to
109 * resolve and dereference the URI.
110 * @return the result of the key selector
111 * @throws KeySelectorException if an exceptional condition occurs while
112 * attempting to find a key. Note that an inability to find a key is not
113 * considered an exception (<code>null</code> should be
114 * returned in that case). However, an error condition (ex: network
115 * communications failure) that prevented the <code>KeySelector</code>
116 * from finding a potential key should be considered an exception.
117 * @throws ClassCastException if the data type of <code>method</code>
118 * is not supported by this key selector
119 */
120 public abstract KeySelectorResult select(KeyInfo keyInfo,
121 Purpose purpose, AlgorithmMethod method,
122 XMLCryptoContext context) throws KeySelectorException;
123
124 /**
125 * Returns a <code>KeySelector</code> that always selects the specified
126 * key, regardless of the <code>KeyInfo</code> passed to it.
127 *
128 * @param key the sole key to be stored in the key selector
129 * @return a key selector that always selects the specified key
130 * @throws NullPointerException if <code>key</code> is <code>null</code>
131 */
132 public static KeySelector singletonKeySelector(Key key) {
133 return new SingletonKeySelector(key);
134 }
135
136 private static class SingletonKeySelector extends KeySelector {
137 private final Key key;
138
139 SingletonKeySelector(Key key) {
140 if (key == null) {
141 throw new NullPointerException();
142 }
143 this .key = key;
144 }
145
146 public KeySelectorResult select(KeyInfo keyInfo,
147 Purpose purpose, AlgorithmMethod method,
148 XMLCryptoContext context) throws KeySelectorException {
149
150 return new KeySelectorResult() {
151 public Key getKey() {
152 return key;
153 }
154 };
155 }
156 }
157 }
|