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