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.api.ldap.model.constants;
21  
22  
23  /**
24   * An enum to store all the security constants used in the server
25   *
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   */
28  public enum LdapSecurityConstants
29  {
30      /** The SHA encryption method */
31      HASH_METHOD_SHA("SHA", "SHA", "sha"),
32  
33      /** The Salted SHA encryption method */
34      HASH_METHOD_SSHA("SSHA", "SHA", "ssha"),
35  
36      /** The SHA-256 encryption method */
37      HASH_METHOD_SHA256("SHA-256", "SHA-256", "sha256"),
38  
39      /** The salted SHA-256 encryption method */
40      HASH_METHOD_SSHA256("SSHA-256", "SHA-256", "ssha256"),
41  
42      /** The SHA-384 encryption method */
43      HASH_METHOD_SHA384("SHA-384", "SHA-384", "sha384"),
44  
45      /** The salted SHA-384 encryption method */
46      HASH_METHOD_SSHA384("SSHA-384", "SHA-384", "ssha384"),
47  
48      /** The SHA-512 encryption method */
49      HASH_METHOD_SHA512("SHA-512", "SHA-512", "sha512"),
50  
51      /** The salted SHA-512 encryption method */
52      HASH_METHOD_SSHA512("SSHA-512", "SHA-512", "ssha512"),
53  
54      /** The MD5 encryption method */
55      HASH_METHOD_MD5("MD5", "MD5", "md5"),
56  
57      /** The Salter MD5 encryption method */
58      HASH_METHOD_SMD5("SMD5", "MD5", "smd5"),
59  
60      /** The crypt encryption method */
61      HASH_METHOD_CRYPT("CRYPT", "CRYPT", "crypt"),
62      
63      /** The crypt (MD5) encryption method */
64      HASH_METHOD_CRYPT_MD5("CRYPT-MD5", "MD5", "crypt", "$1$"),
65      
66      /** The crypt (SHA-256) encryption method */
67      HASH_METHOD_CRYPT_SHA256("CRYPT-SHA-256", "SHA-256", "crypt", "$5$"),
68      
69      /** The crypt (SHA-512) encryption method */
70      HASH_METHOD_CRYPT_SHA512("CRYPT-SHA-512", "SHA-512", "crypt", "$6$"),
71      
72      /** The BCrypt encryption method */
73      HASH_METHOD_CRYPT_BCRYPT("CRYPT-BCRYPT", "BCRYPT", "crypt", "$2a$"),
74  
75      /** The PBKDF2-based encryption method */
76      HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
77  
78      /* These encryption types are not yet supported 
79      ** The AES encryption method *
80      ENC_METHOD_AES("aes"),
81      
82      ** The 3DES encryption method *
83      ENC_METHOD_3DES("3des"),
84      
85      ** The Blowfish encryption method *
86      ENC_METHOD_BLOWFISH("blowfish"),
87      
88      ** The RC4 encryption method *
89      ENC_METHOD_RC4("rc4");
90      */
91  
92      /** The associated name */
93      private String name;
94  
95      /** The associated algorithm */
96      private String algorithm;
97  
98      /** The associated prefix */
99      private String prefix;
100     
101     /** The optional sub-prefix */
102     private String subPrefix;
103 
104     
105     /**
106      * Creates a new instance of LdapSecurityConstants.
107      * 
108      * @param name the associated name
109      * @param algorithm the associated algorithm
110      * @param prefix the associated prefix
111      */
112     LdapSecurityConstants( String name, String algorithm, String prefix )
113     {
114         this( name, algorithm, prefix, "" );
115     }
116 
117     /**
118      * Creates a new instance of LdapSecurityConstants.
119      * 
120      * @param name the associated name
121      * @param algorithm the associated algorithm
122      * @param prefix the associated prefix
123      * @param subPrefix the optional sub-prefix
124      */
125     LdapSecurityConstants( String name, String algorithm, String prefix, String subPrefix )
126     {
127         this.name = name;
128         this.algorithm = algorithm;
129         this.prefix = prefix;
130         this.subPrefix = subPrefix;
131     }
132 
133 
134     /**
135      * @return the name associated with the constant.
136      */
137     public String getName()
138     {
139         return name;
140     }
141 
142 
143     /**
144      * @return the prefix associated with the constant.
145      */
146     public String getAlgorithm()
147     {
148         return algorithm;
149     }
150 
151 
152     /**
153      * @return the prefix associated with the constant.
154      */
155     public String getPrefix()
156     {
157         return prefix;
158     }
159 
160 
161     /**
162      * @return the optional sub-prefix associated with the constant.
163      */
164     public String getSubPrefix()
165     {
166         return subPrefix;
167     }
168 
169 
170     /**
171      * Get the associated constant from a string
172      *
173      * @param algorithm The algorithm's name
174      * @return The associated constant
175      */
176     public static LdapSecurityConstants getAlgorithm( String algorithm )
177     {
178         if ( matches( algorithm, HASH_METHOD_SHA ) )
179         {
180             return HASH_METHOD_SHA;
181         }
182 
183         if ( matches( algorithm, HASH_METHOD_SSHA ) )
184         {
185             return HASH_METHOD_SSHA;
186         }
187         if ( matches( algorithm, HASH_METHOD_MD5 ) )
188         {
189             return HASH_METHOD_MD5;
190         }
191 
192         if ( matches( algorithm, HASH_METHOD_SMD5 ) )
193         {
194             return HASH_METHOD_SMD5;
195         }
196 
197         if ( matches( algorithm, HASH_METHOD_CRYPT ) )
198         {
199             return HASH_METHOD_CRYPT;
200         }
201 
202         if ( matches( algorithm, HASH_METHOD_CRYPT_MD5 ) )
203         {
204             return HASH_METHOD_CRYPT_MD5;
205         }
206 
207         if ( matches( algorithm, HASH_METHOD_CRYPT_SHA256 ) )
208         {
209             return HASH_METHOD_CRYPT_SHA256;
210         }
211 
212         if ( matches( algorithm, HASH_METHOD_CRYPT_SHA512 ) )
213         {
214             return HASH_METHOD_CRYPT_SHA512;
215         }
216 
217         if ( matches( algorithm, HASH_METHOD_CRYPT_BCRYPT ) )
218         {
219             return HASH_METHOD_CRYPT_BCRYPT;
220         }
221 
222         if ( matches( algorithm, HASH_METHOD_SHA256 ) )
223         {
224             return HASH_METHOD_SHA256;
225         }
226 
227         if ( matches( algorithm, HASH_METHOD_SSHA256 ) )
228         {
229             return HASH_METHOD_SSHA256;
230         }
231 
232         if ( matches( algorithm, HASH_METHOD_SHA384 ) )
233         {
234             return HASH_METHOD_SHA384;
235         }
236 
237         if ( matches( algorithm, HASH_METHOD_SSHA384 ) )
238         {
239             return HASH_METHOD_SSHA384;
240         }
241 
242         if ( matches( algorithm, HASH_METHOD_SHA512 ) )
243         {
244             return HASH_METHOD_SHA512;
245         }
246 
247         if ( matches( algorithm, HASH_METHOD_SSHA512 ) )
248         {
249             return HASH_METHOD_SSHA512;
250         }
251 
252         if ( matches( algorithm, HASH_METHOD_PKCS5S2 ) )
253         {
254             return HASH_METHOD_PKCS5S2;
255         }
256 
257         /*
258         if ( ENC_METHOD_AES.name.equalsIgnoreCase( algorithm ) )
259         {
260             return ENC_METHOD_AES;
261         }
262 
263         if ( ENC_METHOD_3DES.name.equalsIgnoreCase( algorithm ) )
264         {
265             return ENC_METHOD_3DES;
266         }
267 
268         if ( ENC_METHOD_BLOWFISH.name.equalsIgnoreCase( algorithm ) )
269         {
270             return ENC_METHOD_BLOWFISH;
271         }
272 
273         if ( ENC_METHOD_RC4.name.equalsIgnoreCase( algorithm ) )
274         {
275             return ENC_METHOD_RC4;
276         }
277         */
278 
279         return null;
280     }
281 
282 
283     private static boolean matches( String algorithm, LdapSecurityConstants constant )
284     {
285         return constant.name.equalsIgnoreCase( algorithm )
286             || ( constant.prefix + constant.subPrefix ).equalsIgnoreCase( algorithm );
287     }
288 
289 }