1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* * Copyright 2006 Sun Microsystems, Inc. All rights reserved. */ /* * $Id: KeySelector.java,v 1.6 2005/05/10 15:47:42 mullan Exp $ */ package javax.xml.crypto; import java.security.Key; import javax.xml.crypto.dsig.keyinfo.KeyInfo; import javax.xml.crypto.dsig.keyinfo.RetrievalMethod; /** * A selector that finds and returns a key using the data contained in a * {@link KeyInfo} object. An example of an implementation of * this class is one that searchs a {@link java.security.KeyStore} for * trusted keys that match information contained in a <code>KeyInfo</code>. * * <p>Whether or not the returned key is trusted and the mechanisms * used to determine that is implementation-specific. * * @author Sean Mullan * @author JSR 105 Expert Group * @since 1.6 */ public abstract class KeySelector { /** * The purpose of the key that is to be selected. */ public static class Purpose { private final String name; private Purpose(String name) { this.name = name; } /** * Returns a string representation of this purpose ("sign", * "verify", "encrypt", or "decrypt"). * * @return a string representation of this purpose */ public String toString() { return name; } /** * A key for signing. */ public static final Purpose SIGN = new Purpose("sign"); /** * A key for verifying. */ public static final Purpose VERIFY = new Purpose("verify"); /** * A key for encrypting. */ public static final Purpose ENCRYPT = new Purpose("encrypt"); /** * A key for decrypting. */ public static final Purpose DECRYPT = new Purpose("decrypt"); } /** * Default no-args constructor; intended for invocation by subclasses only. */ protected KeySelector() {} /** * Attempts to find a key that satisfies the specified constraints. * * @param keyInfo a <code>KeyInfo</code> (may be <code>null</code>) * @param purpose the key's purpose ({@link Purpose#SIGN}, * {@link Purpose#VERIFY}, {@link Purpose#ENCRYPT}, or * {@link Purpose#DECRYPT}) * @param method the algorithm method that this key is to be used for. * Only keys that are compatible with the algorithm and meet the * constraints of the specified algorithm should be returned. * @param context an <code>XMLCryptoContext</code> that may contain * useful information for finding an appropriate key. If this key * selector supports resolving {@link RetrievalMethod} types, the * context's <code>baseURI</code> and <code>dereferencer</code> * parameters (if specified) should be used by the selector to * resolve and dereference the URI. * @return the result of the key selector * @throws KeySelectorException if an exceptional condition occurs while * attempting to find a key. Note that an inability to find a key is not * considered an exception (<code>null</code> should be * returned in that case). However, an error condition (ex: network * communications failure) that prevented the <code>KeySelector</code> * from finding a potential key should be considered an exception. * @throws ClassCastException if the data type of <code>method</code> * is not supported by this key selector */ public abstract KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException; /** * Returns a <code>KeySelector</code> that always selects the specified * key, regardless of the <code>KeyInfo</code> passed to it. * * @param key the sole key to be stored in the key selector * @return a key selector that always selects the specified key * @throws NullPointerException if <code>key</code> is <code>null</code> */ public static KeySelector singletonKeySelector(Key key) { return new SingletonKeySelector(key); } private static class SingletonKeySelector extends KeySelector { private final Key key; SingletonKeySelector(Key key) { if (key == null) { throw new NullPointerException(); } this.key = key; } public KeySelectorResult select(KeyInfo keyInfo, Purpose purpose, AlgorithmMethod method, XMLCryptoContext context) throws KeySelectorException { return new KeySelectorResult() { public Key getKey() { return key; } }; } } }