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 */
020 package org.apache.directory.server.kerberos.shared.crypto.encryption;
021
022
023 import java.security.NoSuchAlgorithmException;
024 import java.util.Collections;
025 import java.util.HashMap;
026 import java.util.Iterator;
027 import java.util.Map;
028 import java.util.Set;
029
030 import javax.crypto.KeyGenerator;
031 import javax.crypto.SecretKey;
032
033 import org.apache.directory.server.kerberos.shared.exceptions.ErrorType;
034 import org.apache.directory.server.kerberos.shared.exceptions.KerberosException;
035 import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
036
037
038 /**
039 * A factory class for producing random keys, suitable for use as session keys. For a
040 * list of desired cipher types, Kerberos random-to-key functions are used to derive
041 * keys for DES-, DES3-, AES-, and RC4-based encryption types.
042 *
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 * @version $Rev$, $Date$
045 */
046 public class RandomKeyFactory
047 {
048 /** A map of default encryption types mapped to cipher names. */
049 private static final Map<EncryptionType, String> DEFAULT_CIPHERS;
050
051 static
052 {
053 Map<EncryptionType, String> map = new HashMap<EncryptionType, String>();
054
055 map.put( EncryptionType.DES_CBC_MD5, "DES" );
056 map.put( EncryptionType.DES3_CBC_SHA1_KD, "DESede" );
057 map.put( EncryptionType.RC4_HMAC, "RC4" );
058 map.put( EncryptionType.AES128_CTS_HMAC_SHA1_96, "AES" );
059 map.put( EncryptionType.AES256_CTS_HMAC_SHA1_96, "AES" );
060
061 DEFAULT_CIPHERS = Collections.unmodifiableMap( map );
062 }
063
064
065 /**
066 * Get a map of random keys. The default set of encryption types is used.
067 *
068 * @return The map of random keys.
069 * @throws KerberosException
070 */
071 public static Map<EncryptionType, EncryptionKey> getRandomKeys() throws KerberosException
072 {
073 return getRandomKeys( DEFAULT_CIPHERS.keySet() );
074 }
075
076
077 /**
078 * Get a map of random keys for a list of cipher types to derive keys for.
079 *
080 * @param ciphers The list of ciphers to derive keys for.
081 * @return The list of KerberosKey's.
082 * @throws KerberosException
083 */
084 public static Map<EncryptionType, EncryptionKey> getRandomKeys( Set<EncryptionType> ciphers )
085 throws KerberosException
086 {
087 Map<EncryptionType, EncryptionKey> map = new HashMap<EncryptionType, EncryptionKey>();
088
089 Iterator<EncryptionType> it = ciphers.iterator();
090 while ( it.hasNext() )
091 {
092 EncryptionType type = it.next();
093 map.put( type, getRandomKey( type ) );
094 }
095
096 return map;
097 }
098
099
100 /**
101 * Get a new random key for a given {@link EncryptionType}.
102 *
103 * @param encryptionType
104 *
105 * @return The new random key.
106 * @throws KerberosException
107 */
108 public static EncryptionKey getRandomKey( EncryptionType encryptionType ) throws KerberosException
109 {
110 String algorithm = DEFAULT_CIPHERS.get( encryptionType );
111
112 if ( algorithm == null )
113 {
114 throw new KerberosException( ErrorType.KDC_ERR_ETYPE_NOSUPP, encryptionType.getName()
115 + " is not a supported encryption type." );
116 }
117
118 try
119 {
120 KeyGenerator keyGenerator = KeyGenerator.getInstance( algorithm );
121
122 if ( encryptionType.equals( EncryptionType.AES128_CTS_HMAC_SHA1_96 ) )
123 {
124 keyGenerator.init( 128 );
125 }
126
127 if ( encryptionType.equals( EncryptionType.AES256_CTS_HMAC_SHA1_96 ) )
128 {
129 keyGenerator.init( 256 );
130 }
131
132 SecretKey key = keyGenerator.generateKey();
133
134 byte[] keyBytes = key.getEncoded();
135
136 return new EncryptionKey( encryptionType, keyBytes );
137 }
138 catch ( NoSuchAlgorithmException nsae )
139 {
140 throw new KerberosException( ErrorType.KDC_ERR_ETYPE_NOSUPP, nsae );
141 }
142 }
143 }