001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose.proc;
019
020
021import java.io.IOException;
022import java.security.Key;
023import java.util.List;
024
025import com.nimbusds.jose.JWEHeader;
026import com.nimbusds.jose.KeySourceException;
027
028
029/**
030 * Interface for selecting key candidates for decrypting a JSON Web Encryption
031 * (JWE) object. Applications should utilise this interface or a similar
032 * framework to determine whether a received JWE object (or encrypted JWT) is
033 * eligible for {@link com.nimbusds.jose.JWEDecrypter decryption} and further 
034 * processing.
035 *
036 * <p>The key selection should be based on application specific criteria, such
037 * as recognised header parameters referencing the key (e.g. {@code kid},
038 * {@code x5t}) and / or the JWE object {@link SecurityContext}.
039 *
040 * <p>See JSON Web Signature (JWE), Appendix D. Notes on Key Selection for
041 * suggestions.
042 *
043 * <p>Possible key types:
044 *
045 * <ul>
046 *     <li>{@link javax.crypto.SecretKey} for AES keys.
047 *     <li>{@link java.security.interfaces.RSAPrivateKey} private RSA keys.
048 *     <li>{@link java.security.interfaces.ECPrivateKey} private EC keys.
049 * </ul>
050 *
051 * @author Vladimir Dzhuvinov
052 * @version 2016-06-21
053 */
054public interface JWEKeySelector <C extends SecurityContext> {
055
056
057        /**
058         * Selects key candidates for decrypting a JWE object.
059         *
060         * @param header  The header of the JWE object. Must not be
061         *                {@code null}.
062         * @param context Optional context of the JWE object, {@code null} if
063         *                not required.
064         *
065         * @return The key candidates in trial order, empty list if none.
066         *
067         * @throws KeySourceException If a key source exception is encountered,
068         *                            e.g. on remote JWK retrieval.
069         */
070        List<? extends Key> selectJWEKeys(final JWEHeader header, final C context)
071                throws KeySourceException;
072}