View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.shared.kerberos.codec.types;
21  
22  
23  import java.util.Collection;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.directory.api.util.Strings;
28  
29  
30  /**
31   * A type-safe enumeration of Kerberos encryption types.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public enum EncryptionType
36  {
37      /**
38       * The "unknown" encryption type.
39       */
40      UNKNOWN(-1, "UNKNOWN"),
41  
42      /**
43       * The "null" encryption type.
44       */
45      NULL(0, "null"),
46  
47      /**
48       * The des-cbc-crc encryption type.
49       */
50      DES_CBC_CRC(1, "des-cbc-crc"),
51  
52      /**
53       * The des-cbc-md4 encryption type.
54       */
55      DES_CBC_MD4(2, "des-cbc-md4"),
56  
57      /**
58       * The des-cbc-md5 encryption type.
59       */
60      DES_CBC_MD5(3, "des-cbc-md5"),
61  
62      /**
63       * The reserved (4) encryption type.
64       */
65      RESERVED4(4, "[reserved]"),
66  
67      /**
68       * The des3-cbc-md5 encryption type.
69       */
70      DES3_CBC_MD5(5, "des3-cbc-md5"),
71  
72      /**
73       * The reserved (6) encryption type.
74       */
75      RESERVED6(6, "[reserved]"),
76  
77      /**
78       * The des3-cbc-sha1 encryption type.
79       */
80      DES3_CBC_SHA1(7, "des3-cbc-sha1"),
81  
82      /**
83       * The dsaWithSHA1-CmsOID encryption type.
84       */
85      DSAWITHSHA1_CMSOID(9, "dsaWithSHA1-CmsOID"),
86  
87      /**
88       * The md5WithRSAEncryption-CmsOID encryption type.
89       */
90      MD5WITHRSAENCRYPTION_CMSOID(10, "md5WithRSAEncryption-CmsOID"),
91  
92      /**
93       * The sha1WithRSAEncryption-CmsOID encryption type.
94       */
95      SHA1WITHRSAENCRYPTION_CMSOID(11, "sha1WithRSAEncryption-CmsOID"),
96  
97      /**
98       * The rc2CBC-EnvOID encryption type.
99       */
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 }