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.server.kerberos.shared.crypto.encryption;
21  
22  
23  import java.security.NoSuchAlgorithmException;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.Iterator;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import javax.crypto.KeyGenerator;
31  import javax.crypto.SecretKey;
32  
33  import org.apache.directory.server.kerberos.shared.exceptions.ErrorType;
34  import org.apache.directory.server.kerberos.shared.exceptions.KerberosException;
35  import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
36  
37  
38  /**
39   * A factory class for producing random keys, suitable for use as session keys.  For a
40   * list of desired cipher types, Kerberos random-to-key functions are used to derive
41   * keys for DES-, DES3-, AES-, and RC4-based encryption types.
42   *
43   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
44   * @version $Rev$, $Date$
45   */
46  public class RandomKeyFactory
47  {
48      /** A map of default encryption types mapped to cipher names. */
49      private static final Map<EncryptionType, String> DEFAULT_CIPHERS;
50  
51      static
52      {
53          Map<EncryptionType, String> map = new HashMap<EncryptionType, String>();
54  
55          map.put( EncryptionType.DES_CBC_MD5, "DES" );
56          map.put( EncryptionType.DES3_CBC_SHA1_KD, "DESede" );
57          map.put( EncryptionType.RC4_HMAC, "RC4" );
58          map.put( EncryptionType.AES128_CTS_HMAC_SHA1_96, "AES" );
59          map.put( EncryptionType.AES256_CTS_HMAC_SHA1_96, "AES" );
60  
61          DEFAULT_CIPHERS = Collections.unmodifiableMap( map );
62      }
63  
64  
65      /**
66       * Get a map of random keys.  The default set of encryption types is used.
67       * 
68       * @return The map of random keys.
69       * @throws KerberosException 
70       */
71      public static Map<EncryptionType, EncryptionKey> getRandomKeys() throws KerberosException
72      {
73          return getRandomKeys( DEFAULT_CIPHERS.keySet() );
74      }
75  
76  
77      /**
78       * Get a map of random keys for a list of cipher types to derive keys for.
79       *
80       * @param ciphers The list of ciphers to derive keys for.
81       * @return The list of KerberosKey's.
82       * @throws KerberosException 
83       */
84      public static Map<EncryptionType, EncryptionKey> getRandomKeys( Set<EncryptionType> ciphers )
85          throws KerberosException
86      {
87          Map<EncryptionType, EncryptionKey> map = new HashMap<EncryptionType, EncryptionKey>();
88  
89          Iterator<EncryptionType> it = ciphers.iterator();
90          while ( it.hasNext() )
91          {
92              EncryptionType type = it.next();
93              map.put( type, getRandomKey( type ) );
94          }
95  
96          return map;
97      }
98  
99  
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 }