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 PBKDF2-based encryption method */
073    HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
074
075    /* These encryption types are not yet supported 
076    ** The AES encryption method *
077    ENC_METHOD_AES("aes"),
078    
079    ** The 3DES encryption method *
080    ENC_METHOD_3DES("3des"),
081    
082    ** The Blowfish encryption method *
083    ENC_METHOD_BLOWFISH("blowfish"),
084    
085    ** The RC4 encryption method *
086    ENC_METHOD_RC4("rc4");
087    */
088
089    /** The associated name */
090    private String name;
091
092    /** The associated algorithm */
093    private String algorithm;
094
095    /** The associated prefix */
096    private String prefix;
097    
098    /** The optional sub-prefix */
099    private String subPrefix;
100
101    
102    /**
103     * Creates a new instance of LdapSecurityConstants.
104     * 
105     * @param name the associated name
106     * @param algorithm the associated algorithm
107     * @param prefix the associated prefix
108     */
109    private LdapSecurityConstants( String name, String algorithm, String prefix )
110    {
111        this( name, algorithm, prefix, "" );
112    }
113
114    /**
115     * Creates a new instance of LdapSecurityConstants.
116     * 
117     * @param name the associated name
118     * @param algorithm the associated algorithm
119     * @param prefix the associated prefix
120     * @param subPrefix the optional sub-prefix
121     */
122    private LdapSecurityConstants( String name, String algorithm, String prefix, String subPrefix )
123    {
124        this.name = name;
125        this.algorithm = algorithm;
126        this.prefix = prefix;
127        this.subPrefix = subPrefix;
128    }
129
130
131    /**
132     * @return the name associated with the constant.
133     */
134    public String getName()
135    {
136        return name;
137    }
138
139
140    /**
141     * @return the prefix associated with the constant.
142     */
143    public String getAlgorithm()
144    {
145        return algorithm;
146    }
147
148
149    /**
150     * @return the prefix associated with the constant.
151     */
152    public String getPrefix()
153    {
154        return prefix;
155    }
156
157
158    /**
159     * @return the optional sub-prefix associated with the constant.
160     */
161    public String getSubPrefix()
162    {
163        return subPrefix;
164    }
165
166
167    /**
168     * Get the associated constant from a string
169     *
170     * @param algorithm The algorithm's name
171     * @return The associated constant
172     */
173    public static LdapSecurityConstants getAlgorithm( String algorithm )
174    {
175        if ( matches( algorithm, HASH_METHOD_SHA ) )
176        {
177            return HASH_METHOD_SHA;
178        }
179
180        if ( matches( algorithm, HASH_METHOD_SSHA ) )
181        {
182            return HASH_METHOD_SSHA;
183        }
184        if ( matches( algorithm, HASH_METHOD_MD5 ) )
185        {
186            return HASH_METHOD_MD5;
187        }
188
189        if ( matches( algorithm, HASH_METHOD_SMD5 ) )
190        {
191            return HASH_METHOD_SMD5;
192        }
193
194        if ( matches( algorithm, HASH_METHOD_CRYPT ) )
195        {
196            return HASH_METHOD_CRYPT;
197        }
198
199        if ( matches( algorithm, HASH_METHOD_CRYPT_MD5 ) )
200        {
201            return HASH_METHOD_CRYPT_MD5;
202        }
203
204        if ( matches( algorithm, HASH_METHOD_CRYPT_SHA256 ) )
205        {
206            return HASH_METHOD_CRYPT_SHA256;
207        }
208
209        if ( matches( algorithm, HASH_METHOD_CRYPT_SHA512 ) )
210        {
211            return HASH_METHOD_CRYPT_SHA512;
212        }
213
214        if ( matches( algorithm, HASH_METHOD_SHA256 ) )
215        {
216            return HASH_METHOD_SHA256;
217        }
218
219        if ( matches( algorithm, HASH_METHOD_SSHA256 ) )
220        {
221            return HASH_METHOD_SSHA256;
222        }
223
224        if ( matches( algorithm, HASH_METHOD_SHA384 ) )
225        {
226            return HASH_METHOD_SHA384;
227        }
228
229        if ( matches( algorithm, HASH_METHOD_SSHA384 ) )
230        {
231            return HASH_METHOD_SSHA384;
232        }
233
234        if ( matches( algorithm, HASH_METHOD_SHA512 ) )
235        {
236            return HASH_METHOD_SHA512;
237        }
238
239        if ( matches( algorithm, HASH_METHOD_SSHA512 ) )
240        {
241            return HASH_METHOD_SSHA512;
242        }
243
244        if ( matches( algorithm, HASH_METHOD_PKCS5S2 ) )
245        {
246            return HASH_METHOD_PKCS5S2;
247        }
248
249        /*
250        if ( ENC_METHOD_AES.name.equalsIgnoreCase( algorithm ) )
251        {
252            return ENC_METHOD_AES;
253        }
254
255        if ( ENC_METHOD_3DES.name.equalsIgnoreCase( algorithm ) )
256        {
257            return ENC_METHOD_3DES;
258        }
259
260        if ( ENC_METHOD_BLOWFISH.name.equalsIgnoreCase( algorithm ) )
261        {
262            return ENC_METHOD_BLOWFISH;
263        }
264
265        if ( ENC_METHOD_RC4.name.equalsIgnoreCase( algorithm ) )
266        {
267            return ENC_METHOD_RC4;
268        }
269        */
270
271        return null;
272    }
273
274
275    private static boolean matches( String algorithm, LdapSecurityConstants constant )
276    {
277        return constant.name.equalsIgnoreCase( algorithm )
278            || ( constant.prefix + constant.subPrefix ).equalsIgnoreCase( algorithm );
279    }
280
281}