001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.shared.kerberos.codec.types;
021
022
023import java.util.Collection;
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.directory.api.util.Strings;
028
029
030/**
031 * A type-safe enumeration of Kerberos encryption types.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public enum EncryptionType
036{
037    /**
038     * The "unknown" encryption type.
039     */
040    UNKNOWN(-1, "UNKNOWN"),
041
042    /**
043     * The "null" encryption type.
044     */
045    NULL(0, "null"),
046
047    /**
048     * The des-cbc-crc encryption type.
049     */
050    DES_CBC_CRC(1, "des-cbc-crc"),
051
052    /**
053     * The des-cbc-md4 encryption type.
054     */
055    DES_CBC_MD4(2, "des-cbc-md4"),
056
057    /**
058     * The des-cbc-md5 encryption type.
059     */
060    DES_CBC_MD5(3, "des-cbc-md5"),
061
062    /**
063     * The reserved (4) encryption type.
064     */
065    RESERVED4(4, "[reserved]"),
066
067    /**
068     * The des3-cbc-md5 encryption type.
069     */
070    DES3_CBC_MD5(5, "des3-cbc-md5"),
071
072    /**
073     * The reserved (6) encryption type.
074     */
075    RESERVED6(6, "[reserved]"),
076
077    /**
078     * The des3-cbc-sha1 encryption type.
079     */
080    DES3_CBC_SHA1(7, "des3-cbc-sha1"),
081
082    /**
083     * The dsaWithSHA1-CmsOID encryption type.
084     */
085    DSAWITHSHA1_CMSOID(9, "dsaWithSHA1-CmsOID"),
086
087    /**
088     * The md5WithRSAEncryption-CmsOID encryption type.
089     */
090    MD5WITHRSAENCRYPTION_CMSOID(10, "md5WithRSAEncryption-CmsOID"),
091
092    /**
093     * The sha1WithRSAEncryption-CmsOID encryption type.
094     */
095    SHA1WITHRSAENCRYPTION_CMSOID(11, "sha1WithRSAEncryption-CmsOID"),
096
097    /**
098     * The rc2CBC-EnvOID encryption type.
099     */
100    RC2CBC_ENVOID(12, "rc2CBC-EnvOID"),
101
102    /**
103     * The rsaEncryption-EnvOID encryption type.
104     */
105    RSAENCRYPTION_ENVOID(13, "rsaEncryption-EnvOID"),
106
107    /**
108     * The rsaES-OAEP-ENV-OID encryption type.
109     */
110    RSAES_OAEP_ENV_OID(14, "rsaES-OAEP-ENV-OID"),
111
112    /**
113     * The des-ede3-cbc-Env-OID encryption type.
114     */
115    DES_EDE3_CBC_ENV_OID(15, "des-ede3-cbc-Env-OID"),
116
117    /**
118     * The des3-cbc-sha1-kd encryption type.
119     */
120    DES3_CBC_SHA1_KD(16, "des3-cbc-sha1-kd"),
121
122    /**
123     * The aes128-cts-hmac-sha1-96 encryption type.
124     */
125    AES128_CTS_HMAC_SHA1_96(17, "aes128-cts-hmac-sha1-96"),
126
127    /**
128     * The aes256-cts-hmac-sha1-96 encryption type.
129     */
130    AES256_CTS_HMAC_SHA1_96(18, "aes256-cts-hmac-sha1-96"),
131
132    /**
133     * The aes128-cts-hmac-sha256-128 encryption type (RFC8009).
134     */
135    AES128_CTS_HMAC_SHA256_128(19, "aes128-cts-hmac-sha256-128"),
136
137    /**
138     * The aes256-cts-hmac-sha384-192 encryption type (RFC8009).
139     */
140    AES256_CTS_HMAC_SHA384_192(20, "aes256-cts-hmac-sha384-192"),
141
142    /**
143     * The rc4-hmac encryption type.
144     */
145    RC4_HMAC(23, "rc4-hmac"),
146
147    /**
148     * The rc4-hmac-exp encryption type.
149     */
150    RC4_HMAC_EXP(24, "rc4-hmac-exp"),
151
152    /**
153     * The subkey-keymaterial encryption type.
154     */
155    SUBKEY_KEYMATERIAL(65, "subkey-keymaterial"),
156
157    /**
158     * The rc4-md4 encryption type.
159     */
160    RC4_MD4(-128, "rc4-md4"),
161
162    /**
163     * The c4-hmac-old encryption type.
164     */
165    RC4_HMAC_OLD(-133, "rc4-hmac-old"),
166
167    /**
168     * The rc4-hmac-old-exp encryption type.
169     */
170    RC4_HMAC_OLD_EXP(-135, "rc4-hmac-old-exp");
171
172    /**
173     * The value/code for the encryption type.
174     */
175    private final int value;
176
177    /**
178     * The name
179     */
180    private final String name;
181
182    /** A map containing all the values */
183    private static Map<String, EncryptionType> encryptionTypesByName = new HashMap<>();
184
185    /** A map containing all the values */
186    private static Map<Integer, EncryptionType> encryptionTypesByValue = new HashMap<>();
187
188    /** Initialization of the previous map */
189    static
190    {
191        for ( EncryptionType type : EncryptionType.values() )
192        {
193            encryptionTypesByName.put( Strings.toLowerCaseAscii( type.getName() ), type );
194            encryptionTypesByValue.put( type.getValue(), type );
195        }
196    }
197
198
199    /**
200     * Private constructor prevents construction outside of this class.
201     */
202    private EncryptionType( int value, String name )
203    {
204        this.value = value;
205        this.name = name;
206    }
207
208
209    /**
210     * Get all the encryption types
211     *
212     * @return A set of encryption types.
213     */
214    public static Collection<EncryptionType> getEncryptionTypes()
215    {
216        return encryptionTypesByName.values();
217    }
218
219
220    /**
221     * Returns the encryption type when specified by its value.
222     *
223     * @param type
224     * @return The encryption type.
225     */
226    public static EncryptionType getTypeByValue( int type )
227    {
228        if ( encryptionTypesByValue.containsKey( type ) )
229        {
230            return encryptionTypesByValue.get( type );
231        }
232        else
233        {
234            return UNKNOWN;
235        }
236    }
237
238
239    /**
240     * Returns the number associated with this encryption type.
241     *
242     * @return The encryption type number.
243     */
244    public int getValue()
245    {
246        return value;
247    }
248
249
250    /**
251     * Returns the name associated with this encryption type.
252     *
253     * @return The name.
254     */
255    public String getName()
256    {
257        return name;
258    }
259
260
261    /**
262     * Get the EncryptionType given a String.
263     * @param type The encryption string we want to find
264     * @return The found EncryptionType, or UNKNOWN
265     */
266    public static EncryptionType getByName( String type )
267    {
268        if ( type == null )
269        {
270            return UNKNOWN;
271        }
272
273        String lcType = Strings.toLowerCaseAscii( type );
274
275        if ( encryptionTypesByName.containsKey( lcType ) )
276        {
277            return encryptionTypesByName.get( lcType );
278        }
279        else
280        {
281            return UNKNOWN;
282        }
283    }
284
285
286    /**
287     * @see Object#toString()
288     */
289    @Override
290    public String toString()
291    {
292        return getName() + " (" + value + ")";
293    }
294}