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     */
020    package org.apache.directory.server.kerberos.shared.crypto.encryption;
021    
022    import java.util.Collection;
023    import java.util.HashMap;
024    import java.util.Map;
025    
026    
027    /**
028     * A type-safe enumeration of Kerberos encryption types.
029     * 
030     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031     * @version $Rev: 587682 $, $Date: 2007-10-24 00:47:43 +0200 (Wed, 24 Oct 2007) $
032     */
033    public enum EncryptionType
034    {
035        /**
036         * The "unknown" encryption type.
037         */
038        UNKNOWN( -1 ),
039    
040        /**
041         * The "null" encryption type.
042         */
043        NULL( 0 ),
044    
045        /**
046         * The des-cbc-crc encryption type.
047         */
048        DES_CBC_CRC( 1 ),
049    
050        /**
051         * The des-cbc-md4 encryption type.
052         */
053        DES_CBC_MD4( 2 ),
054    
055        /**
056         * The des-cbc-md5 encryption type.
057         */
058        DES_CBC_MD5( 3 ),
059    
060        /**
061         * The reserved (4) encryption type.
062         */
063        RESERVED4( 4 ),
064    
065        /**
066         * The des3-cbc-md5 encryption type.
067         */
068        DES3_CBC_MD5( 5 ),
069    
070        /**
071         * The reserved (6) encryption type.
072         */
073        RESERVED6( 6 ),
074    
075        /**
076         * The des3-cbc-sha1 encryption type.
077         */
078        DES3_CBC_SHA1( 7 ),
079    
080        /**
081         * The dsaWithSHA1-CmsOID encryption type.
082         */
083        DSAWITHSHA1_CMSOID( 9 ),
084    
085        /**
086         * The md5WithRSAEncryption-CmsOID encryption type.
087         */
088        MD5WITHRSAENCRYPTION_CMSOID( 10 ),
089    
090        /**
091         * The sha1WithRSAEncryption-CmsOID encryption type.
092         */
093        SHA1WITHRSAENCRYPTION_CMSOID( 11 ),
094    
095        /**
096         * The rc2CBC-EnvOID encryption type.
097         */
098        RC2CBC_ENVOID( 12 ),
099    
100        /**
101         * The rsaEncryption-EnvOID encryption type.
102         */
103        RSAENCRYPTION_ENVOID( 13 ),
104    
105        /**
106         * The rsaES-OAEP-ENV-OID encryption type.
107         */
108        RSAES_OAEP_ENV_OID( 14 ),
109    
110        /**
111         * The des-ede3-cbc-Env-OID encryption type.
112         */
113        DES_EDE3_CBC_ENV_OID( 15 ),
114    
115        /**
116         * The des3-cbc-sha1-kd encryption type.
117         */
118        DES3_CBC_SHA1_KD( 16 ),
119    
120        /**
121         * The aes128-cts-hmac-sha1-96 encryption type.
122         */
123        AES128_CTS_HMAC_SHA1_96( 17 ),
124    
125        /**
126         * The aes256-cts-hmac-sha1-96 encryption type.
127         */
128        AES256_CTS_HMAC_SHA1_96( 18 ),
129    
130        /**
131         * The rc4-hmac encryption type.
132         */
133        RC4_HMAC( 23 ),
134    
135        /**
136         * The rc4-hmac-exp encryption type.
137         */
138        RC4_HMAC_EXP( 24 ),
139    
140        /**
141         * The subkey-keymaterial encryption type.
142         */
143        SUBKEY_KEYMATERIAL( 65 ),
144    
145        /**
146         * The rc4-md4 encryption type.
147         */
148        RC4_MD4( -128 ),
149    
150        /**
151         * The c4-hmac-old encryption type.
152         */
153        RC4_HMAC_OLD( -133 ),
154    
155        /**
156         * The rc4-hmac-old-exp encryption type.
157         */
158        RC4_HMAC_OLD_EXP( -135 );
159    
160        /**
161         * The value/code for the encryption type.
162         */
163        private final int ordinal;
164    
165        /** A map containing all the values */
166        private static Map<String, EncryptionType> encryptionTypes = new HashMap<String, EncryptionType>();
167        
168        /** Initialization of the previous map */
169        static
170        {
171            encryptionTypes.put( "null", NULL );
172            encryptionTypes.put( "des-cbc-crc", DES_CBC_CRC ); 
173            encryptionTypes.put( "des-cbc-md4", DES_CBC_MD4 );          
174            encryptionTypes.put( "des-cbc-md5", DES_CBC_MD5 );          
175            encryptionTypes.put( "[reserved]", RESERVED4 );         
176            encryptionTypes.put( "des3-cbc-md5", DES3_CBC_MD5 );            
177            encryptionTypes.put( "[reserved]", RESERVED6 );         
178            encryptionTypes.put( "des3-cbc-sha1", DES3_CBC_SHA1 );          
179            encryptionTypes.put( "dsaWithSHA1-CmsOID", DSAWITHSHA1_CMSOID );            
180            encryptionTypes.put( "md5WithRSAEncryption-CmsOID", MD5WITHRSAENCRYPTION_CMSOID );          
181            encryptionTypes.put( "sha1WithRSAEncryption-CmsOID", SHA1WITHRSAENCRYPTION_CMSOID );            
182            encryptionTypes.put( "rc2CBC-EnvOID", RC2CBC_ENVOID );          
183            encryptionTypes.put( "rsaEncryption-EnvOID", RSAENCRYPTION_ENVOID );            
184            encryptionTypes.put( "rsaES-OAEP-ENV-OID", RSAES_OAEP_ENV_OID );        
185            encryptionTypes.put( "des-ede3-cbc-Env-OID", DES_EDE3_CBC_ENV_OID );            
186            encryptionTypes.put( "des3-cbc-sha1-kd", DES3_CBC_SHA1_KD );        
187            encryptionTypes.put( "aes128-cts-hmac-sha1-96", AES128_CTS_HMAC_SHA1_96 );          
188            encryptionTypes.put( "aes256-cts-hmac-sha1-96", AES256_CTS_HMAC_SHA1_96 );          
189            encryptionTypes.put( "rc4-hmac", RC4_HMAC );            
190            encryptionTypes.put( "rc4-hmac-exp", RC4_HMAC_EXP );            
191            encryptionTypes.put( "subkey-keymaterial", SUBKEY_KEYMATERIAL );            
192            encryptionTypes.put( "rc4-md4", RC4_MD4 );      
193            encryptionTypes.put( "rc4-hmac-old", RC4_HMAC_OLD );            
194            encryptionTypes.put( "rc4-hmac-old-exp", RC4_HMAC_OLD_EXP );            
195            encryptionTypes.put( "UNKNOWN", UNKNOWN );
196        }
197    
198        /**
199         * Private constructor prevents construction outside of this class.
200         */
201        private EncryptionType( int ordinal )
202        {
203            this.ordinal = ordinal;
204        }
205    
206        
207        /**
208         * Get all the encryption types
209         *
210         * @return A set of encryption types.
211         */
212        public static Collection<EncryptionType> getEncryptionTypes()
213        {
214            return encryptionTypes.values();
215        }
216    
217        /**
218         * Returns the encryption type when specified by its ordinal.
219         *
220         * @param type
221         * @return The encryption type.
222         */
223        public static EncryptionType getTypeByOrdinal( int type )
224        {
225            switch ( type )
226            {
227                case 0 : return NULL; 
228                case 1 : return DES_CBC_CRC; 
229                case 2 : return DES_CBC_MD4; 
230                case 3 : return DES_CBC_MD5; 
231                case 4 : return RESERVED4; 
232                case 5 : return DES3_CBC_MD5; 
233                case 6 : return RESERVED6; 
234                case 7 : return DES3_CBC_SHA1; 
235                case 9 : return DSAWITHSHA1_CMSOID; 
236                case 10 : return MD5WITHRSAENCRYPTION_CMSOID; 
237                case 11 : return SHA1WITHRSAENCRYPTION_CMSOID; 
238                case 12 : return RC2CBC_ENVOID; 
239                case 13 : return RSAENCRYPTION_ENVOID; 
240                case 14 : return RSAES_OAEP_ENV_OID; 
241                case 15 : return DES_EDE3_CBC_ENV_OID; 
242                case 16 : return DES3_CBC_SHA1_KD; 
243                case 17 : return AES128_CTS_HMAC_SHA1_96; 
244                case 18 : return AES256_CTS_HMAC_SHA1_96; 
245                case 23 : return RC4_HMAC; 
246                case 24 : return RC4_HMAC_EXP; 
247                case 65 : return SUBKEY_KEYMATERIAL; 
248                case -128 : return RC4_MD4; 
249                case -133 : return RC4_HMAC_OLD; 
250                case -135 : return RC4_HMAC_OLD_EXP; 
251                default : return UNKNOWN; 
252            }
253        }
254    
255    
256        /**
257         * Returns the number associated with this encryption type.
258         *
259         * @return The encryption type number.
260         */
261        public int getOrdinal()
262        {
263            return ordinal;
264        }
265    
266    
267        /**
268         * Returns the name associated with this encryption type.
269         *
270         * @return The name.
271         */
272        public String getName()
273        {
274            switch (this )
275            {
276                case NULL                           : return "NULL"; 
277                case DES_CBC_CRC                    : return "des-cbc-crc"; 
278                case DES_CBC_MD4                    : return "des-cbc-md4";          
279                case DES_CBC_MD5                    : return "des-cbc-md5";          
280                case RESERVED4                      : return "[reserved]";           
281                case DES3_CBC_MD5                   : return "des3-cbc-md5";         
282                case RESERVED6                      : return "[reserved]";           
283                case DES3_CBC_SHA1                  : return "des3-cbc-sha1";            
284                case DSAWITHSHA1_CMSOID             : return "dsaWithSHA1-CmsOID";           
285                case MD5WITHRSAENCRYPTION_CMSOID    : return "md5WithRSAEncryption-CmsOID";          
286                case SHA1WITHRSAENCRYPTION_CMSOID   : return "sha1WithRSAEncryption-CmsOID";         
287                case RC2CBC_ENVOID                  : return "rc2CBC-EnvOID";            
288                case RSAENCRYPTION_ENVOID           : return "rsaEncryption-EnvOID";         
289                case RSAES_OAEP_ENV_OID             : return "rsaES-OAEP-ENV-OID";       
290                case DES_EDE3_CBC_ENV_OID           : return "des-ede3-cbc-Env-OID";         
291                case DES3_CBC_SHA1_KD               : return "des3-cbc-sha1-kd";     
292                case AES128_CTS_HMAC_SHA1_96        : return "aes128-cts-hmac-sha1-96";          
293                case AES256_CTS_HMAC_SHA1_96        : return "aes256-cts-hmac-sha1-96";          
294                case RC4_HMAC                       : return "rc4-hmac";         
295                case RC4_HMAC_EXP                   : return "rc4-hmac-exp";         
296                case SUBKEY_KEYMATERIAL             : return "subkey-keymaterial";           
297                case RC4_MD4                        : return "rc4-md4";      
298                case RC4_HMAC_OLD                   : return "rc4-hmac-old";         
299                case RC4_HMAC_OLD_EXP               : return "rc4-hmac-old-exp";         
300                case UNKNOWN                        : return "UNKNOWN";
301                default                             : return "UNKNOWN";
302            }
303        }
304    
305        /**
306         * Get the EncryptionType given a String.
307         * @param type The encryption string we want to find
308         * @return The found EncryptionType, or UNKNOWN
309         */
310        public static EncryptionType getByName( String type )
311        {
312            if ( type == null )
313            {
314                return UNKNOWN;
315            }
316            
317            String lcType = type.toLowerCase();
318            
319            if ( encryptionTypes.containsKey( lcType ) )
320            {
321                return encryptionTypes.get( lcType );
322            }
323            else
324            {
325                return UNKNOWN;
326            }
327        }
328        
329        
330        /**
331         * @see Object#toString()
332         */
333        public String toString()
334        {
335            return getName() + " (" + ordinal + ")";
336        }
337    }