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.server.kerberos.shared.crypto.encryption; 021 022 023import java.security.NoSuchAlgorithmException; 024import java.util.Collections; 025import java.util.EnumMap; 026import java.util.HashMap; 027import java.util.Map; 028 029import javax.crypto.KeyGenerator; 030import javax.crypto.SecretKey; 031 032import org.apache.directory.server.i18n.I18n; 033import org.apache.directory.shared.kerberos.codec.types.EncryptionType; 034import org.apache.directory.shared.kerberos.components.EncryptionKey; 035import org.apache.directory.shared.kerberos.exceptions.ErrorType; 036import org.apache.directory.shared.kerberos.exceptions.KerberosException; 037 038 039/** 040 * A factory class for producing random keys, suitable for use as session keys. For a 041 * list of desired cipher types, Kerberos random-to-key functions are used to derive 042 * keys for DES-, DES3-, AES-, and RC4-based encryption types. 043 * 044 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 045 */ 046public 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 EnumMap<EncryptionType, String> map = new EnumMap<>( EncryptionType.class ); 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 Map<EncryptionType, EncryptionKey> map = new HashMap<>(); 074 075 for ( EncryptionType encryptionType : DEFAULT_CIPHERS.keySet() ) 076 { 077 map.put( encryptionType, getRandomKey( encryptionType ) ); 078 } 079 080 return map; 081 } 082 083 084 /** 085 * Get a new random key for a given {@link EncryptionType}. 086 * 087 * @param encryptionType 088 * 089 * @return The new random key. 090 * @throws KerberosException 091 */ 092 public static EncryptionKey getRandomKey( EncryptionType encryptionType ) throws KerberosException 093 { 094 String algorithm = DEFAULT_CIPHERS.get( encryptionType ); 095 096 if ( algorithm == null ) 097 { 098 throw new KerberosException( ErrorType.KDC_ERR_ETYPE_NOSUPP, I18n.err( I18n.ERR_616, 099 encryptionType.getName() ) ); 100 } 101 102 try 103 { 104 KeyGenerator keyGenerator = KeyGenerator.getInstance( algorithm ); 105 106 if ( encryptionType.equals( EncryptionType.AES128_CTS_HMAC_SHA1_96 ) ) 107 { 108 keyGenerator.init( 128 ); 109 } 110 111 if ( encryptionType.equals( EncryptionType.AES256_CTS_HMAC_SHA1_96 ) ) 112 { 113 keyGenerator.init( 256 ); 114 } 115 116 SecretKey key = keyGenerator.generateKey(); 117 118 byte[] keyBytes = key.getEncoded(); 119 120 return new EncryptionKey( encryptionType, keyBytes ); 121 } 122 catch ( NoSuchAlgorithmException nsae ) 123 { 124 throw new KerberosException( ErrorType.KDC_ERR_ETYPE_NOSUPP, nsae ); 125 } 126 } 127}