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-01-08)
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 Master Key (CMK)
063             * for the block encryption step (rather than using the symmetric key to
064             * wrap the CMK) (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 Master Key (CMK) (rather than being used to wrap the CMK) 
074             * (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             * Master Key (CMK) with the "A128KW" function (rather than being used
083             * directly as the CMK) (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             * Master Key (CMK) with the "A256KW" function (rather than being used
092             * directly as the CMK) (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                    
135                    else if (s == RSA_OAEP.getName())
136                            return RSA_OAEP;
137                    
138                    else if (s == A128KW.getName())
139                            return A128KW;
140                    
141                    else if (s == A256KW.getName())
142                            return A256KW;
143                    
144                    else if (s == DIR.getName())
145                            return DIR;
146                    
147                    else if (s == ECDH_ES.getName())
148                            return ECDH_ES;
149                    
150                    else if (s == ECDH_ES_A128KW.getName())
151                            return ECDH_ES_A128KW;
152                    
153                    else if (s == ECDH_ES_A256KW.getName())
154                            return ECDH_ES_A256KW;
155                    
156                    else
157                            return new JWEAlgorithm(s);
158            }
159    }