Showing the changes to java/pkg/javax/xml/crypto/KeySelector.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,152 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | +/* | |
| 26 | + * $Id: KeySelector.java,v 1.6 2005/05/10 15:47:42 mullan Exp $ | |
| 27 | + */ | |
| 28 | +package javax.xml.crypto; | |
| 29 | + | |
| 30 | +import java.security.Key; | |
| 31 | +import javax.xml.crypto.dsig.keyinfo.KeyInfo; | |
| 32 | +import javax.xml.crypto.dsig.keyinfo.RetrievalMethod; | |
| 33 | + | |
| 34 | +/** | |
| 35 | + * A selector that finds and returns a key using the data contained in a | |
| 36 | + * {@link KeyInfo} object. An example of an implementation of | |
| 37 | + * this class is one that searchs a {@link java.security.KeyStore} for | |
| 38 | + * trusted keys that match information contained in a <code>KeyInfo</code>. | |
| 39 | + * | |
| 40 | + * <p>Whether or not the returned key is trusted and the mechanisms | |
| 41 | + * used to determine that is implementation-specific. | |
| 42 | + * | |
| 43 | + * @author Sean Mullan | |
| 44 | + * @author JSR 105 Expert Group | |
| 45 | + * @since 1.6 | |
| 46 | + */ | |
| 47 | +public abstract class KeySelector { | |
| 48 | + | |
| 49 | + /** | |
| 50 | + * The purpose of the key that is to be selected. | |
| 51 | + */ | |
| 52 | + public static class Purpose { | |
| 53 | + | |
| 54 | + private final String name; | |
| 55 | + | |
| 56 | + private Purpose(String name) { this.name = name; } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Returns a string representation of this purpose ("sign", | |
| 60 | + * "verify", "encrypt", or "decrypt"). | |
| 61 | + * | |
| 62 | + * @return a string representation of this purpose | |
| 63 | + */ | |
| 64 | + public String toString() { return name; } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * A key for signing. | |
| 68 | + */ | |
| 69 | + public static final Purpose SIGN = new Purpose("sign"); | |
| 70 | + /** | |
| 71 | + * A key for verifying. | |
| 72 | + */ | |
| 73 | + public static final Purpose VERIFY = new Purpose("verify"); | |
| 74 | + /** | |
| 75 | + * A key for encrypting. | |
| 76 | + */ | |
| 77 | + public static final Purpose ENCRYPT = new Purpose("encrypt"); | |
| 78 | + /** | |
| 79 | + * A key for decrypting. | |
| 80 | + */ | |
| 81 | + public static final Purpose DECRYPT = new Purpose("decrypt"); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Default no-args constructor; intended for invocation by subclasses only. | |
| 86 | + */ | |
| 87 | + protected KeySelector() {} | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Attempts to find a key that satisfies the specified constraints. | |
| 91 | + * | |
| 92 | + * @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>) | |
| 93 | + * @param purpose the key's purpose ({@link Purpose#SIGN}, | |
| 94 | + * {@link Purpose#VERIFY}, {@link Purpose#ENCRYPT}, or | |
| 95 | + * {@link Purpose#DECRYPT}) | |
| 96 | + * @param method the algorithm method that this key is to be used for. | |
| 97 | + * Only keys that are compatible with the algorithm and meet the | |
| 98 | + * constraints of the specified algorithm should be returned. | |
| 99 | + * @param context an <code>XMLCryptoContext</code> that may contain | |
| 100 | + * useful information for finding an appropriate key. If this key | |
| 101 | + * selector supports resolving {@link RetrievalMethod} types, the | |
| 102 | + * context's <code>baseURI</code> and <code>dereferencer</code> | |
| 103 | + * parameters (if specified) should be used by the selector to | |
| 104 | + * resolve and dereference the URI. | |
| 105 | + * @return the result of the key selector | |
| 106 | + * @throws KeySelectorException if an exceptional condition occurs while | |
| 107 | + * attempting to find a key. Note that an inability to find a key is not | |
| 108 | + * considered an exception (<code>null</code> should be | |
| 109 | + * returned in that case). However, an error condition (ex: network | |
| 110 | + * communications failure) that prevented the <code>KeySelector</code> | |
| 111 | + * from finding a potential key should be considered an exception. | |
| 112 | + * @throws ClassCastException if the data type of <code>method</code> | |
| 113 | + * is not supported by this key selector | |
| 114 | + */ | |
| 115 | + public abstract KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, | |
| 116 | + AlgorithmMethod method, XMLCryptoContext context) | |
| 117 | + throws KeySelectorException; | |
| 118 | + | |
| 119 | + /** | |
| 120 | + * Returns a <code>KeySelector</code> that always selects the specified | |
| 121 | + * key, regardless of the <code>KeyInfo</code> passed to it. | |
| 122 | + * | |
| 123 | + * @param key the sole key to be stored in the key selector | |
| 124 | + * @return a key selector that always selects the specified key | |
| 125 | + * @throws NullPointerException if <code>key</code> is <code>null</code> | |
| 126 | + */ | |
| 127 | + public static KeySelector singletonKeySelector(Key key) { | |
| 128 | + return new SingletonKeySelector(key); | |
| 129 | + } | |
| 130 | + | |
| 131 | + private static class SingletonKeySelector extends KeySelector { | |
| 132 | + private final Key key; | |
| 133 | + | |
| 134 | + SingletonKeySelector(Key key) { | |
| 135 | + if (key == null) { | |
| 136 | + throw new NullPointerException(); | |
| 137 | + } | |
| 138 | + this.key = key; | |
| 139 | + } | |
| 140 | + | |
| 141 | + public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, | |
| 142 | + AlgorithmMethod method, XMLCryptoContext context) | |
| 143 | + throws KeySelectorException { | |
| 144 | + | |
| 145 | + return new KeySelectorResult() { | |
| 146 | + public Key getKey() { | |
| 147 | + return key; | |
| 148 | + } | |
| 149 | + }; | |
| 150 | + } | |
| 151 | + } | |
| 152 | +} | |