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.api.ldap.model.constants;
021
022
023/**
024 * An enum to store all the security constants used in the server
025 *
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 */
028public enum LdapSecurityConstants
029{
030    /** The SHA encryption method */
031    HASH_METHOD_SHA("SHA", "SHA", "sha"),
032
033    /** The Salted SHA encryption method */
034    HASH_METHOD_SSHA("SSHA", "SHA", "ssha"),
035
036    /** The SHA-256 encryption method */
037    HASH_METHOD_SHA256("SHA-256", "SHA-256", "sha256"),
038
039    /** The salted SHA-256 encryption method */
040    HASH_METHOD_SSHA256("SSHA-256", "SHA-256", "ssha256"),
041
042    /** The SHA-384 encryption method */
043    HASH_METHOD_SHA384("SHA-384", "SHA-384", "sha384"),
044
045    /** The salted SHA-384 encryption method */
046    HASH_METHOD_SSHA384("SSHA-384", "SHA-384", "ssha384"),
047
048    /** The SHA-512 encryption method */
049    HASH_METHOD_SHA512("SHA-512", "SHA-512", "sha512"),
050
051    /** The salted SHA-512 encryption method */
052    HASH_METHOD_SSHA512("SSHA-512", "SHA-512", "ssha512"),
053
054    /** The MD5 encryption method */
055    HASH_METHOD_MD5("MD5", "MD5", "md5"),
056
057    /** The Salter MD5 encryption method */
058    HASH_METHOD_SMD5("SMD5", "MD5", "smd5"),
059
060    /** The crypt encryption method */
061    HASH_METHOD_CRYPT("CRYPT", "CRYPT", "crypt"),
062    
063    /** The crypt (MD5) encryption method */
064    HASH_METHOD_CRYPT_MD5("CRYPT-MD5", "MD5", "crypt", "$1$"),
065    
066    /** The crypt (SHA-256) encryption method */
067    HASH_METHOD_CRYPT_SHA256("CRYPT-SHA-256", "SHA-256", "crypt", "$5$"),
068    
069    /** The crypt (SHA-512) encryption method */
070    HASH_METHOD_CRYPT_SHA512("CRYPT-SHA-512", "SHA-512", "crypt", "$6$"),
071    
072    /** The BCrypt encryption method */
073    HASH_METHOD_CRYPT_BCRYPT("CRYPT-BCRYPT", "BCRYPT", "crypt", "$2a$"),
074
075    /** The PBKDF2-based encryption method */
076    HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
077
078    /* These encryption types are not yet supported 
079    ** The AES encryption method *
080    ENC_METHOD_AES("aes"),
081    
082    ** The 3DES encryption method *
083    ENC_METHOD_3DES("3des"),
084    
085    ** The Blowfish encryption method *
086    ENC_METHOD_BLOWFISH("blowfish"),
087    
088    ** The RC4 encryption method *
089    ENC_METHOD_RC4("rc4");
090    */
091
092    /** The associated name */
093    private String name;
094
095    /** The associated algorithm */
096    private String algorithm;
097
098    /** The associated prefix */
099    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}