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
023import org.apache.directory.api.util.Strings;
024
025
026/**
027 * An enum to store all the security constants used in the server
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public enum LdapSecurityConstants
032{
033    /** The SHA encryption method */
034    HASH_METHOD_SHA("SHA", "SHA", "sha"),
035
036    /** The Salted SHA encryption method */
037    HASH_METHOD_SSHA("SSHA", "SHA", "ssha"),
038
039    /** The SHA-256 encryption method */
040    HASH_METHOD_SHA256("SHA-256", "SHA-256", "sha256"),
041
042    /** The salted SHA-256 encryption method */
043    HASH_METHOD_SSHA256("SSHA-256", "SHA-256", "ssha256"),
044
045    /** The SHA-384 encryption method */
046    HASH_METHOD_SHA384("SHA-384", "SHA-384", "sha384"),
047
048    /** The salted SHA-384 encryption method */
049    HASH_METHOD_SSHA384("SSHA-384", "SHA-384", "ssha384"),
050
051    /** The SHA-512 encryption method */
052    HASH_METHOD_SHA512("SHA-512", "SHA-512", "sha512"),
053
054    /** The salted SHA-512 encryption method */
055    HASH_METHOD_SSHA512("SSHA-512", "SHA-512", "ssha512"),
056
057    /** The MD5 encryption method */
058    HASH_METHOD_MD5("MD5", "MD5", "md5"),
059
060    /** The Salter MD5 encryption method */
061    HASH_METHOD_SMD5("SMD5", "MD5", "smd5"),
062
063    /** The crypt encryption method */
064    HASH_METHOD_CRYPT("CRYPT", "CRYPT", "crypt"),
065
066    /** The PBKDF2-based encryption method */
067    HASH_METHOD_PKCS5S2("PKCS5S2", "PBKDF2WithHmacSHA1", "PKCS5S2");
068
069    /* These encryption types are not yet supported 
070    ** The AES encryption method *
071    ENC_METHOD_AES("aes"),
072    
073    ** The 3DES encryption method *
074    ENC_METHOD_3DES("3des"),
075    
076    ** The Blowfish encryption method *
077    ENC_METHOD_BLOWFISH("blowfish"),
078    
079    ** The RC4 encryption method *
080    ENC_METHOD_RC4("rc4");
081    */
082
083    /** The associated name */
084    private String name;
085
086    /** The associated algorithm */
087    private String algorithm;
088
089    /** The associated prefix */
090    private String prefix;
091
092
093    /**
094     * Creates a new instance of LdapSecurityConstants.
095     * 
096     * @param name the associated name
097     * @param algorithm the associated algorithm
098     * @param prefix the associated prefix
099     */
100    private LdapSecurityConstants( String name, String algorithm, String prefix )
101    {
102        this.name = name;
103        this.algorithm = algorithm;
104        this.prefix = prefix;
105    }
106
107
108    /**
109     * @return the name associated with the constant.
110     */
111    public String getName()
112    {
113        return name;
114    }
115
116
117    /**
118     * @return the prefix associated with the constant.
119     */
120    public String getAlgorithm()
121    {
122        return algorithm;
123    }
124
125
126    /**
127     * @return the prefix associated with the constant.
128     */
129    public String getPrefix()
130    {
131        return prefix;
132    }
133
134
135    /**
136     * Get the associated constant from a string
137     *
138     * @param name The algorithm's name
139     * @return The associated constant
140     */
141    public static LdapSecurityConstants getAlgorithm( String name )
142    {
143        String algorithm = "";
144
145        if ( name != null )
146        {
147            algorithm = Strings.toLowerCase( name );
148        }
149
150        if ( HASH_METHOD_SHA.getName().equalsIgnoreCase( algorithm )
151            || HASH_METHOD_SHA.getPrefix().equalsIgnoreCase( algorithm ) )
152        {
153            return HASH_METHOD_SHA;
154        }
155
156        if ( HASH_METHOD_SSHA.getName().equalsIgnoreCase( algorithm )
157            || HASH_METHOD_SSHA.getPrefix().equalsIgnoreCase( algorithm ) )
158        {
159            return HASH_METHOD_SSHA;
160        }
161
162        if ( HASH_METHOD_MD5.getName().equalsIgnoreCase( algorithm )
163            || HASH_METHOD_MD5.getPrefix().equalsIgnoreCase( algorithm ))
164        {
165            return HASH_METHOD_MD5;
166        }
167
168        if ( HASH_METHOD_SMD5.getName().equalsIgnoreCase( algorithm )
169            || HASH_METHOD_SMD5.getPrefix().equalsIgnoreCase( algorithm ))
170        {
171            return HASH_METHOD_SMD5;
172        }
173
174        if ( HASH_METHOD_CRYPT.getName().equalsIgnoreCase( algorithm )
175            || HASH_METHOD_CRYPT.getPrefix().equalsIgnoreCase( algorithm ))
176        {
177            return HASH_METHOD_CRYPT;
178        }
179
180        if ( ( HASH_METHOD_SHA256.getName().equalsIgnoreCase( algorithm ) )
181            || ( HASH_METHOD_SHA256.getPrefix().equalsIgnoreCase( algorithm ) )
182            || ( "sha-256".equalsIgnoreCase( algorithm ) ) ) // "sha-256" used for backwards compatibility
183        {
184            return HASH_METHOD_SHA256;
185        }
186
187        if ( ( HASH_METHOD_SSHA256.getName().equalsIgnoreCase( algorithm ) )
188            || ( HASH_METHOD_SSHA256.getPrefix().equalsIgnoreCase( algorithm ) )
189            || ( "ssha-256".equalsIgnoreCase( algorithm ) ) ) // "ssha-256" used for backwards compatibility
190        {
191            return HASH_METHOD_SSHA256;
192        }
193
194        if ( ( HASH_METHOD_SHA384.getName().equalsIgnoreCase( algorithm ) )
195            || ( HASH_METHOD_SHA384.getPrefix().equalsIgnoreCase( algorithm ) )
196            || ( "sha-384".equalsIgnoreCase( algorithm ) ) ) // "sha-384" used for backwards compatibility
197        {
198            return HASH_METHOD_SHA384;
199        }
200
201        if ( ( HASH_METHOD_SSHA384.getName().equalsIgnoreCase( algorithm ) )
202            ||  ( HASH_METHOD_SSHA384.getPrefix().equalsIgnoreCase( algorithm ) )
203            || ( "ssha-384".equalsIgnoreCase( algorithm ) ) ) // "ssha-384" used for backwards compatibility
204        {
205            return HASH_METHOD_SSHA384;
206        }
207
208        if ( ( HASH_METHOD_SHA512.getName().equalsIgnoreCase( algorithm ) )
209            ||  ( HASH_METHOD_SHA512.getPrefix().equalsIgnoreCase( algorithm ) )
210            || ( "sha-512".equalsIgnoreCase( algorithm ) ) ) // "sha-512" used for backwards compatibility
211        {
212            return HASH_METHOD_SHA512;
213        }
214
215        if ( ( HASH_METHOD_SSHA512.getName().equalsIgnoreCase( algorithm ) )
216            ||  ( HASH_METHOD_SSHA512.getPrefix().equalsIgnoreCase( algorithm ) )
217            || ( "ssha-512".equalsIgnoreCase( algorithm ) ) ) // "ssha-512" used for backwards compatibility
218        {
219            return HASH_METHOD_SSHA512;
220        }
221
222        if ( HASH_METHOD_PKCS5S2.getName().equalsIgnoreCase( algorithm )
223            || HASH_METHOD_PKCS5S2.getPrefix().equalsIgnoreCase( algorithm ) )
224        {
225            return HASH_METHOD_PKCS5S2;
226        }
227
228        /*
229        if ( ENC_METHOD_AES.getName().equalsIgnoreCase( algorithm ) )
230        {
231            return ENC_METHOD_AES;
232        }
233
234        if ( ENC_METHOD_3DES.getName().equalsIgnoreCase( algorithm ) )
235        {
236            return ENC_METHOD_3DES;
237        }
238
239        if ( ENC_METHOD_BLOWFISH.getName().equalsIgnoreCase( algorithm ) )
240        {
241            return ENC_METHOD_BLOWFISH;
242        }
243
244        if ( ENC_METHOD_RC4.getName().equalsIgnoreCase( algorithm ) )
245        {
246            return ENC_METHOD_RC4;
247        }
248        */
249
250        return null;
251    }
252}