001    package com.nimbusds.jose;
002    
003    
004    import net.jcip.annotations.Immutable;
005    
006    
007    /**
008     * JSON Web Encryption (JWE) algorithm name, represents the {@code alg} header 
009     * parameter in JWE objects. This class is immutable.
010     *
011     * <p>Includes constants for the following standard JWE algorithm names:
012     *
013     * <ul>
014     *     <li>{@link #RSA1_5}
015     *     <li>{@link #RSA_OAEP RSA-OAEP}
016     *     <li>{@link #A128KW}
017     *     <li>{@link #A256KW}
018     *     <li>{@link #DIR dir}
019     *     <li>{@link #ECDH_ES ECDH-ES}
020     *     <li>{@link #ECDH_ES_A128KW ESDH-ES+A128KW}
021     *     <li>{@link #ECDH_ES_A256KW ESDH-ES+A256KW}
022     * </ul>
023     *
024     * <p>Additional JWE algorithm names can be defined using the constructors.
025     *
026     * @author Vladimir Dzhuvinov
027     * @version $version$ (2013-05-06)
028     */
029    @Immutable
030    public final class JWEAlgorithm extends Algorithm {
031    
032    
033            /**
034             * RSAES-PKCS1-V1_5 (RFC 3447) (required).
035             */
036            public static final JWEAlgorithm RSA1_5 = new JWEAlgorithm("RSA1_5", Requirement.REQUIRED);
037    
038    
039            /**
040             * RSAES using Optimal Assymetric Encryption Padding (OAEP) (RFC 3447),
041             * with the default parameters specified by RFC 3447 in section A.2.1
042             * (recommended).
043             */
044            public static final JWEAlgorithm RSA_OAEP = new JWEAlgorithm("RSA-OAEP", Requirement.RECOMMENDED);
045    
046    
047            /**
048             * Advanced Encryption Standard (AES) Key Wrap Algorithm (RFC 3394) 
049             * using 256 bit keys (recommended).
050             */
051            public static final JWEAlgorithm A128KW = new JWEAlgorithm("A128KW", Requirement.RECOMMENDED);
052    
053    
054            /**
055             * Advanced Encryption Standard (AES) Key Wrap Algorithm (RFC 3394) 
056             * using 256 bit keys (recommended).
057             */
058            public static final JWEAlgorithm A256KW = new JWEAlgorithm("A256KW", Requirement.RECOMMENDED);
059    
060    
061            /**
062             * Direct use of a shared symmetric key as the Content Encryption Key 
063             * (CEK) for the block encryption step (rather than using the symmetric
064             * key to wrap the CEK) (recommended).
065             */
066            public static final JWEAlgorithm DIR = new JWEAlgorithm("dir", Requirement.RECOMMENDED);
067    
068    
069            /**
070             * Elliptic Curve Diffie-Hellman Ephemeral Static (RFC 6090) key 
071             * agreement using the Concat KDF, as defined in section 5.8.1 of
072             * NIST.800-56A, with the agreed-upon key being used directly as the 
073             * Content Encryption Key (CEK) (rather than being used to wrap the 
074             * CEK) (recommended).
075             */
076            public static final JWEAlgorithm ECDH_ES = new JWEAlgorithm("ECDH-ES", Requirement.RECOMMENDED);
077    
078    
079            /**
080             * Elliptic Curve Diffie-Hellman Ephemeral Static key agreement per
081             * "ECDH-ES", but where the agreed-upon key is used to wrap the Content
082             * Encryption Key (CEK) with the "A128KW" function (rather than being 
083             * used directly as the CEK) (recommended).
084             */
085            public static final JWEAlgorithm ECDH_ES_A128KW = new JWEAlgorithm("ECDH-ES+A128KW", Requirement.RECOMMENDED);
086    
087    
088            /**
089             * Elliptic Curve Diffie-Hellman Ephemeral Static key agreement per
090             * "ECDH-ES", but where the agreed-upon key is used to wrap the Content
091             * Encryption Key (CEK) with the "A256KW" function (rather than being 
092             * used directly as the CEK) (recommended).
093             */
094            public static final JWEAlgorithm ECDH_ES_A256KW = new JWEAlgorithm("ECDH-ES+A256KW", Requirement.RECOMMENDED);
095    
096    
097    
098            /**
099             * Creates a new JSON Web Encryption (JWE) algorithm.
100             *
101             * @param name The algorithm name. Must not be {@code null}.
102             * @param req  The implementation requirement, {@code null} if not 
103             *             known.
104             */
105            public JWEAlgorithm(final String name, final Requirement req) {
106    
107                    super(name, req);
108            }
109    
110    
111            /**
112             * Creates a new JSON Web Encryption (JWE) algorithm.
113             *
114             * @param name The algorithm name. Must not be {@code null}.
115             */
116            public JWEAlgorithm(final String name) {
117    
118                    super(name, null);
119            }
120    
121    
122            /**
123             * Parses a JWE algorithm from the specified string.
124             *
125             * @param s The string to parse. Must not be {@code null}.
126             *
127             * @return The JWE algorithm (matching standard algorithm constant, else
128             *         a newly created algorithm).
129             */
130            public static JWEAlgorithm parse(final String s) {
131    
132                    if (s == RSA1_5.getName()) {
133                            return RSA1_5;
134                    } else if (s == RSA_OAEP.getName()) {
135                            return RSA_OAEP;
136                    } else if (s == A128KW.getName()) {
137                            return A128KW;
138                    } else if (s == A256KW.getName()) {
139                            return A256KW;
140                    } else if (s == DIR.getName()) {
141                            return DIR;
142                    } else if (s == ECDH_ES.getName()) {
143                            return ECDH_ES;
144                    } else if (s == ECDH_ES_A128KW.getName()) {
145                            return ECDH_ES_A128KW;
146                    } else if (s == ECDH_ES_A256KW.getName()) {
147                            return ECDH_ES_A256KW;
148                    } else {
149                            return new JWEAlgorithm(s);
150                    }
151            }
152    }