+ "/ou=users,dc=example,dc=com" );
env.put( "java.naming.security.principal", "uid=admin,ou=system" );
env.put( "java.naming.security.credentials", "secret" );
env.put( "java.naming.security.authentication", "simple" );
env.put( "java.naming.ldap.attributes.binary", "krb5key" );
DirContext ctx = new InitialDirContext( env );
Attributes attrs = getPersonAttributes( "Quist", "Thomas Quist", "tquist", "randomKey", "tquist@EXAMPLE.COM" );
ctx.createSubcontext( "uid=tquist", attrs );
attrs = getPersonAttributes( "Fryer", "John Fryer", "jfryer", "randomKey", "jfryer@EXAMPLE.COM" );
ctx.createSubcontext( "uid=jfryer", attrs );
String[] attrIDs =
{ "uid", "userPassword", "krb5Key" };
Attributes tquistAttrs = ctx.getAttributes( "uid=tquist", attrIDs );
Attributes jfryerAttrs = ctx.getAttributes( "uid=jfryer", attrIDs );
String uid = null;
byte[] userPassword = null;
if ( tquistAttrs.get( "uid" ) != null )
{
uid = ( String ) tquistAttrs.get( "uid" ).get();
}
assertEquals( "tquist", uid );
if ( tquistAttrs.get( "userPassword" ) != null )
{
userPassword = ( byte[] ) tquistAttrs.get( "userPassword" ).get();
}
// Bytes for "randomKey."
byte[] testPasswordBytes =
{ ( byte ) 0x72, ( byte ) 0x61, ( byte ) 0x6E, ( byte ) 0x64, ( byte ) 0x6F, ( byte ) 0x6D, ( byte ) 0x4B,
( byte ) 0x65, ( byte ) 0x79 };
assertTrue( Arrays.equals( testPasswordBytes, userPassword ) );
if ( jfryerAttrs.get( "uid" ) != null )
{
uid = ( String ) jfryerAttrs.get( "uid" ).get();
}
assertEquals( "jfryer", uid );
if ( jfryerAttrs.get( "userPassword" ) != null )
{
userPassword = ( byte[] ) jfryerAttrs.get( "userPassword" ).get();
}
assertTrue( Arrays.equals( testPasswordBytes, userPassword ) );
byte[] testKeyBytes =
{ ( byte ) 0xF4, ( byte ) 0xA7, ( byte ) 0x13, ( byte ) 0x64, ( byte ) 0x8A, ( byte ) 0x61, ( byte ) 0xCE,
( byte ) 0x5B };
Attribute krb5key = tquistAttrs.get( "krb5key" );
Map<EncryptionType, EncryptionKey> map = reconstituteKeyMap( krb5key );
EncryptionKey encryptionKey = map.get( EncryptionType.DES_CBC_MD5 );
byte[] tquistKey = encryptionKey.getKeyValue();
assertEquals( EncryptionType.DES_CBC_MD5, encryptionKey.getKeyType() );
krb5key = jfryerAttrs.get( "krb5key" );
map = reconstituteKeyMap( krb5key );
encryptionKey = map.get( EncryptionType.DES_CBC_MD5 );
byte[] jfryerKey = encryptionKey.getKeyValue();
assertEquals( EncryptionType.DES_CBC_MD5, encryptionKey.getKeyType() );
assertEquals( "Key length", 8, tquistKey.length );
assertEquals( "Key length", 8, jfryerKey.length );
assertFalse( Arrays.equals( testKeyBytes, tquistKey ) );
assertFalse( Arrays.equals( testKeyBytes, jfryerKey ) );
assertFalse( Arrays.equals( jfryerKey, tquistKey ) );
byte[] tquistDerivedKey =
{ ( byte ) 0xFD, ( byte ) 0x7F, ( byte ) 0x6B, ( byte ) 0x83, ( byte ) 0xA4, ( byte ) 0x76, ( byte ) 0xC1,
( byte ) 0xEA };
byte[] jfryerDerivedKey =
{ ( byte ) 0xA4, ( byte ) 0x10, ( byte ) 0x3B, ( byte ) 0x49, ( byte ) 0xCE, ( byte ) 0x0B, ( byte ) 0xB5,
( byte ) 0x07 };
assertFalse( Arrays.equals( tquistDerivedKey, tquistKey ) );
assertFalse( Arrays.equals( jfryerDerivedKey, jfryerKey ) );
assertTrue( DESKeySpec.isParityAdjusted( tquistKey, 0 ) );
assertTrue( DESKeySpec.isParityAdjusted( jfryerKey, 0 ) );
ctx.close();
}