directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From erodrig...@apache.org
Subject svn commit: r536741 - in /directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src: main/java/org/apache/directory/server/kerberos/shared/keytab/ test/java/org/apache/directory/server/kerberos/shared/keytab/
Date Thu, 10 May 2007 04:45:29 GMT
Author: erodriguez
Date: Wed May  9 21:45:28 2007
New Revision: 536741

URL: http://svn.apache.org/viewvc?view=rev&rev=536741
Log:
New component in support of Kerberos key export (DIRSERVER-898)
o  Keytab component encapsulating reading and writing of keytab files.
o  Unit test for keytab component.

Added:
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java
  (with props)
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java
  (with props)
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java
  (with props)
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java
  (with props)
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/
    directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java
  (with props)

Added: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java?view=auto&rev=536741
==============================================================================
--- directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java
(added)
+++ directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java
Wed May  9 21:45:28 2007
@@ -0,0 +1,240 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.keytab;
+
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.channels.FileChannel;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.apache.mina.common.ByteBuffer;
+
+
+/**
+ * Keytab file.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class Keytab
+{
+    /**
+     * Byte array constant for keytab file format 5.1.
+     */
+    public static final byte[] VERSION_51 = new byte[]
+        { ( byte ) 0x05, ( byte ) 0x01 };
+
+    /**
+     * Byte array constant for keytab file format 5.2.
+     */
+    public static final byte[] VERSION_52 = new byte[]
+        { ( byte ) 0x05, ( byte ) 0x02 };
+
+    private byte[] keytabVersion = VERSION_52;
+    private List<KeytabEntry> entries = new ArrayList<KeytabEntry>();
+
+
+    /**
+     * Read a keytab file.
+     *
+     * @param file
+     * @return The keytab.
+     * @throws IOException
+     */
+    public static Keytab read( File file ) throws IOException
+    {
+        ByteBuffer buffer = ByteBuffer.wrap( getBytesFromFile( file ) );
+        return readKeytab( buffer );
+    }
+
+
+    /**
+     * Returns a new instance of a keytab with the version
+     * defaulted to 5.2.
+     *
+     * @return The keytab.
+     */
+    public static Keytab getInstance()
+    {
+        return new Keytab();
+    }
+
+
+    /**
+     * Write the keytab to a {@link File}.
+     *
+     * @param file
+     * @throws IOException
+     */
+    public void write( File file ) throws IOException
+    {
+        KeytabEncoder writer = new KeytabEncoder();
+        ByteBuffer buffer = writer.write( keytabVersion, entries );
+        writeFile( buffer, file );
+    }
+
+
+    /**
+     * @param entries The entries to set.
+     */
+    public void setEntries( List<KeytabEntry> entries )
+    {
+        this.entries = entries;
+    }
+
+
+    /**
+     * @param keytabVersion The keytabVersion to set.
+     */
+    public void setKeytabVersion( byte[] keytabVersion )
+    {
+        this.keytabVersion = keytabVersion;
+    }
+
+
+    /**
+     * @return The entries.
+     */
+    public List<KeytabEntry> getEntries()
+    {
+        return Collections.unmodifiableList( entries );
+    }
+
+
+    /**
+     * @return The keytabVersion.
+     */
+    public byte[] getKeytabVersion()
+    {
+        return keytabVersion;
+    }
+
+
+    /**
+     * Read bytes into a keytab.
+     *
+     * @param bytes
+     * @return The keytab.
+     */
+    static Keytab read( byte[] bytes )
+    {
+        ByteBuffer buffer = ByteBuffer.wrap( bytes );
+        return readKeytab( buffer );
+    }
+
+
+    /**
+     * Write the keytab to a {@link ByteBuffer}.
+     * @return The buffer.
+     */
+    ByteBuffer write()
+    {
+        KeytabEncoder writer = new KeytabEncoder();
+        return writer.write( keytabVersion, entries );
+    }
+
+
+    /**
+     * Read the contents of the buffer into a keytab.
+     *
+     * @param buffer
+     * @return The keytab.
+     */
+    private static Keytab readKeytab( ByteBuffer buffer )
+    {
+        KeytabDecoder reader = new KeytabDecoder();
+        byte[] keytabVersion = reader.getKeytabVersion( buffer );
+        List<KeytabEntry> entries = reader.getKeytabEntries( buffer );
+
+        Keytab keytab = new Keytab();
+
+        keytab.setKeytabVersion( keytabVersion );
+        keytab.setEntries( entries );
+
+        return keytab;
+    }
+
+
+    /**
+     * Returns the contents of the {@link File} in a byte array.
+     *
+     * @param file
+     * @return The byte array of the file contents.
+     * @throws IOException
+     */
+    protected static byte[] getBytesFromFile( File file ) throws IOException
+    {
+        InputStream is = new FileInputStream( file );
+
+        long length = file.length();
+
+        // Check to ensure that file is not larger than Integer.MAX_VALUE.
+        if ( length > Integer.MAX_VALUE )
+        {
+            throw new IOException( "File is too large " + file.getName() );
+        }
+
+        // Create the byte array to hold the data.
+        byte[] bytes = new byte[( int ) length];
+
+        // Read in the bytes
+        int offset = 0;
+        int numRead = 0;
+        while ( offset < bytes.length && ( numRead = is.read( bytes, offset, bytes.length
- offset ) ) >= 0 )
+        {
+            offset += numRead;
+        }
+
+        // Ensure all the bytes have been read in.
+        if ( offset < bytes.length )
+        {
+            throw new IOException( "Could not completely read file " + file.getName() );
+        }
+
+        // Close the input stream and return bytes.
+        is.close();
+        return bytes;
+    }
+
+
+    /**
+     * Write the contents of the {@link ByteBuffer} to a {@link File}.
+     *
+     * @param buffer
+     * @param file
+     * @throws IOException
+     */
+    protected void writeFile( ByteBuffer buffer, File file ) throws IOException
+    {
+        // Set append false to replace existing.
+        FileChannel wChannel = new FileOutputStream( file, false ).getChannel();
+
+        // Write the bytes between the position and limit.
+        wChannel.write( buffer.buf() );
+
+        wChannel.close();
+    }
+}

Propchange: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/Keytab.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java?view=auto&rev=536741
==============================================================================
--- directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java
(added)
+++ directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java
Wed May  9 21:45:28 2007
@@ -0,0 +1,180 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.keytab;
+
+
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
+import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
+import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
+import org.apache.mina.common.ByteBuffer;
+
+
+/**
+ * Decode a {@link ByteBuffer} into keytab fields.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+class KeytabDecoder
+{
+    /**
+     * Read the keytab 16-bit file format version.  This
+     * keytab reader currently only supports version 5.2.
+     */
+    byte[] getKeytabVersion( ByteBuffer buffer )
+    {
+        byte[] version = new byte[2];
+        buffer.get( version );
+
+        return version;
+    }
+
+
+    /**
+     * Read keytab entries until there is no remaining data
+     * in the buffer.
+     *
+     * @param buffer
+     * @return The keytab entries.
+     */
+    List<KeytabEntry> getKeytabEntries( ByteBuffer buffer )
+    {
+        List<KeytabEntry> entries = new ArrayList<KeytabEntry>();
+
+        while ( buffer.remaining() > 0 )
+        {
+            int size = buffer.getInt();
+            byte[] entry = new byte[size];
+
+            buffer.get( entry );
+            entries.add( getKeytabEntry( ByteBuffer.wrap( entry ) ) );
+        }
+
+        return entries;
+    }
+
+
+    /**
+     * Reads off a "keytab entry," which consists of a principal name,
+     * principal type, key version number, and key material.
+     */
+    private KeytabEntry getKeytabEntry( ByteBuffer buffer )
+    {
+        String principalName = getPrincipalName( buffer );
+
+        long principalType = buffer.getUnsignedInt();
+
+        long time = buffer.getUnsignedInt();
+        KerberosTime timeStamp = new KerberosTime( time * 1000 );
+
+        byte keyVersion = buffer.get();
+
+        EncryptionKey key = getKeyBlock( buffer, keyVersion );
+
+        return new KeytabEntry( principalName, principalType, timeStamp, keyVersion, key
);
+    }
+
+
+    /**
+     * Reads off a principal name.
+     *
+     * @param buffer
+     * @return The principal name.
+     */
+    private String getPrincipalName( ByteBuffer buffer )
+    {
+        int count = buffer.getUnsignedShort();
+
+        // decrement for v1
+        String realm = getCountedString( buffer );
+
+        StringBuffer principalNameBuffer = new StringBuffer();
+
+        for ( int ii = 0; ii < count; ii++ )
+        {
+            String nameComponent = getCountedString( buffer );
+
+            principalNameBuffer.append( nameComponent );
+
+            if ( ii < count - 1 )
+            {
+                principalNameBuffer.append( "\\" );
+            }
+        }
+
+        principalNameBuffer.append( "@" + realm );
+
+        return principalNameBuffer.toString();
+    }
+
+
+    /**
+     * Read off a 16-bit encryption type and symmetric key material.
+     */
+    private EncryptionKey getKeyBlock( ByteBuffer buffer, int keyVersion )
+    {
+        int type = buffer.getUnsignedShort();
+        byte[] keyblock = getCountedBytes( buffer );
+
+        EncryptionType encryptionType = EncryptionType.getTypeByOrdinal( type );
+        EncryptionKey key = new EncryptionKey( encryptionType, keyblock, keyVersion );
+
+        return key;
+    }
+
+
+    /**
+     * Use a prefixed 16-bit length to read off a String.  Realm and name
+     * components are ASCII encoded text with no zero terminator.
+     */
+    private String getCountedString( ByteBuffer buffer )
+    {
+        int length = buffer.getUnsignedShort();
+        byte[] data = new byte[length];
+        buffer.get( data );
+
+        try
+        {
+            return new String( data, "ASCII" );
+        }
+        catch ( UnsupportedEncodingException uee )
+        {
+            // Should never happen for ASCII
+            return "";
+        }
+    }
+
+
+    /**
+     * Use a prefixed 16-bit length to read off raw bytes.
+     */
+    private byte[] getCountedBytes( ByteBuffer buffer )
+    {
+        int length = buffer.getUnsignedShort();
+        byte[] data = new byte[length];
+        buffer.get( data );
+
+        return data;
+    }
+}

Propchange: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabDecoder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java?view=auto&rev=536741
==============================================================================
--- directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java
(added)
+++ directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java
Wed May  9 21:45:28 2007
@@ -0,0 +1,168 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.keytab;
+
+
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
+import org.apache.mina.common.ByteBuffer;
+
+
+/**
+ * Encode keytab fields into a {@link ByteBuffer}.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+class KeytabEncoder
+{
+    /**
+     * Write the keytab version and entries into a {@link ByteBuffer}.
+     *
+     * @param keytabVersion
+     * @param entries
+     * @return The ByteBuffer.
+     */
+    ByteBuffer write( byte[] keytabVersion, List<KeytabEntry> entries )
+    {
+        ByteBuffer buffer = ByteBuffer.allocate( 512 );
+        putKeytabVersion( buffer, keytabVersion );
+        putKeytabEntries( buffer, entries );
+        buffer.flip();
+
+        return buffer;
+    }
+
+
+    /**
+     * Encode the 16-bit file format version.  This
+     * keytab reader currently only support verision 5.2.
+     */
+    private void putKeytabVersion( ByteBuffer buffer, byte[] version )
+    {
+        buffer.put( version );
+    }
+
+
+    /**
+     * Encode the keytab entries.
+     *
+     * @param buffer
+     * @param entries
+     */
+    private void putKeytabEntries( ByteBuffer buffer, List<KeytabEntry> entries )
+    {
+        Iterator<KeytabEntry> iterator = entries.iterator();
+
+        while ( iterator.hasNext() )
+        {
+            ByteBuffer entryBuffer = putKeytabEntry( iterator.next() );
+            int size = entryBuffer.position();
+
+            entryBuffer.flip();
+
+            buffer.putInt( size );
+            buffer.put( entryBuffer );
+        }
+    }
+
+
+    /**
+     * Encode a "keytab entry," which consists of a principal name,
+     * principal type, key version number, and key material.
+     */
+    private ByteBuffer putKeytabEntry( KeytabEntry entry )
+    {
+        ByteBuffer buffer = ByteBuffer.allocate( 100 );
+
+        putPrincipalName( buffer, entry.getPrincipalName() );
+
+        buffer.putInt( ( int ) entry.getPrincipalType() );
+
+        buffer.putInt( ( int ) ( entry.getTimeStamp().getTime() / 1000 ) );
+
+        buffer.put( entry.getKeyVersion() );
+
+        putKeyBlock( buffer, entry.getKey() );
+
+        return buffer;
+    }
+
+
+    /**
+     * Encode a principal name.
+     *
+     * @param buffer
+     * @param principalName
+     */
+    private void putPrincipalName( ByteBuffer buffer, String principalName )
+    {
+        String[] split = principalName.split( "@" );
+        String nameComponent = split[0];
+        String realm = split[1];
+
+        String[] nameComponents = nameComponent.split( "/" );
+
+        // increment for v1
+        buffer.putShort( ( short ) nameComponents.length );
+
+        putCountedString( buffer, realm );
+        // write components
+
+        for ( int ii = 0; ii < nameComponents.length; ii++ )
+        {
+            putCountedString( buffer, nameComponents[ii] );
+        }
+    }
+
+
+    /**
+     * Encode a 16-bit encryption type and symmetric key material.
+     */
+    private void putKeyBlock( ByteBuffer buffer, EncryptionKey key )
+    {
+        buffer.putShort( ( short ) key.getKeyType().getOrdinal() );
+        putCountedBytes( buffer, key.getKeyValue() );
+    }
+
+
+    /**
+     * Use a prefixed 16-bit length to encode a String.  Realm and name
+     * components are ASCII encoded text with no zero terminator.
+     */
+    private void putCountedString( ByteBuffer buffer, String string )
+    {
+        byte[] data = string.getBytes();
+        buffer.putShort( ( short ) data.length );
+        buffer.put( data );
+    }
+
+
+    /**
+     * Use a prefixed 16-bit length to encode raw bytes.
+     */
+    private void putCountedBytes( ByteBuffer buffer, byte[] data )
+    {
+        buffer.putShort( ( short ) data.length );
+        buffer.put( data );
+    }
+}

Propchange: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEncoder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java?view=auto&rev=536741
==============================================================================
--- directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java
(added)
+++ directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java
Wed May  9 21:45:28 2007
@@ -0,0 +1,109 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.keytab;
+
+
+import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
+import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
+
+
+/**
+ * An entry within a keytab file.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class KeytabEntry
+{
+    private String principalName;
+
+    private long principalType;
+
+    private KerberosTime timeStamp;
+
+    private byte keyVersion;
+
+    private EncryptionKey key;
+
+
+    /**
+     * Creates a new instance of Entry.
+     *
+     * @param principalName
+     * @param principalType
+     * @param timeStamp
+     * @param keyVersion
+     * @param key
+     */
+    public KeytabEntry( String principalName, long principalType, KerberosTime timeStamp,
byte keyVersion,
+        EncryptionKey key )
+    {
+        this.principalName = principalName;
+        this.principalType = principalType;
+        this.timeStamp = timeStamp;
+        this.keyVersion = keyVersion;
+        this.key = key;
+    }
+
+
+    /**
+     * @return The key.
+     */
+    public EncryptionKey getKey()
+    {
+        return key;
+    }
+
+
+    /**
+     * @return The keyVersion.
+     */
+    public byte getKeyVersion()
+    {
+        return keyVersion;
+    }
+
+
+    /**
+     * @return The principalName.
+     */
+    public String getPrincipalName()
+    {
+        return principalName;
+    }
+
+
+    /**
+     * @return The principalType.
+     */
+    public long getPrincipalType()
+    {
+        return principalType;
+    }
+
+
+    /**
+     * @return The timeStamp.
+     */
+    public KerberosTime getTimeStamp()
+    {
+        return timeStamp;
+    }
+}

Propchange: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/main/java/org/apache/directory/server/kerberos/shared/keytab/KeytabEntry.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java?view=auto&rev=536741
==============================================================================
--- directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java
(added)
+++ directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java
Wed May  9 21:45:28 2007
@@ -0,0 +1,180 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.server.kerberos.shared.keytab;
+
+
+import java.security.InvalidKeyException;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.TimeZone;
+
+import javax.crypto.spec.DESKeySpec;
+
+import junit.framework.TestCase;
+
+import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
+import org.apache.directory.server.kerberos.shared.crypto.encryption.KerberosKeyFactory;
+import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
+import org.apache.directory.server.kerberos.shared.messages.value.KerberosTime;
+import org.apache.mina.common.ByteBuffer;
+
+
+/**
+ * Tests 'keytab' formatted files.
+ * 
+ * All values are in network byte order.  All text is ASCII.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class KeytabTest extends TestCase
+{
+    private static final byte[] keytab1 = new byte[]
+        { ( byte ) 0x05, ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte
) 0x3C, ( byte ) 0x00,
+            ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x0B, ( byte ) 0x45, ( byte ) 0x58, (
byte ) 0x41, ( byte ) 0x4D,
+            ( byte ) 0x50, ( byte ) 0x4C, ( byte ) 0x45, ( byte ) 0x2E, ( byte ) 0x43, (
byte ) 0x4F, ( byte ) 0x4D,
+            ( byte ) 0x00, ( byte ) 0x04, ( byte ) 0x6C, ( byte ) 0x64, ( byte ) 0x61, (
byte ) 0x70, ( byte ) 0x00,
+            ( byte ) 0x10, ( byte ) 0x77, ( byte ) 0x77, ( byte ) 0x77, ( byte ) 0x2E, (
byte ) 0x76, ( byte ) 0x65,
+            ( byte ) 0x72, ( byte ) 0x69, ( byte ) 0x73, ( byte ) 0x69, ( byte ) 0x67, (
byte ) 0x6E, ( byte ) 0x2E,
+            ( byte ) 0x63, ( byte ) 0x6F, ( byte ) 0x6D, ( byte ) 0x00, ( byte ) 0x00, (
byte ) 0x00, ( byte ) 0x01,
+            ( byte ) 0x45, ( byte ) 0xD9, ( byte ) 0x60, ( byte ) 0xBE, ( byte ) 0x01, (
byte ) 0x00, ( byte ) 0x03,
+            ( byte ) 0x00, ( byte ) 0x08, ( byte ) 0xD5, ( byte ) 0xE6, ( byte ) 0xC4, (
byte ) 0xD0, ( byte ) 0xFE,
+            ( byte ) 0x25, ( byte ) 0x07, ( byte ) 0x0D };
+
+    private static final byte[] keytab2 = new byte[]
+        { ( byte ) 0x05, ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x00, ( byte ) 0x00, ( byte
) 0x3C, ( byte ) 0x00,
+            ( byte ) 0x02, ( byte ) 0x00, ( byte ) 0x0B, ( byte ) 0x45, ( byte ) 0x58, (
byte ) 0x41, ( byte ) 0x4D,
+            ( byte ) 0x50, ( byte ) 0x4C, ( byte ) 0x45, ( byte ) 0x2E, ( byte ) 0x43, (
byte ) 0x4F, ( byte ) 0x4D,
+            ( byte ) 0x00, ( byte ) 0x04, ( byte ) 0x48, ( byte ) 0x54, ( byte ) 0x54, (
byte ) 0x50, ( byte ) 0x00,
+            ( byte ) 0x10, ( byte ) 0x77, ( byte ) 0x77, ( byte ) 0x77, ( byte ) 0x2E, (
byte ) 0x76, ( byte ) 0x65,
+            ( byte ) 0x72, ( byte ) 0x69, ( byte ) 0x73, ( byte ) 0x69, ( byte ) 0x67, (
byte ) 0x6E, ( byte ) 0x2E,
+            ( byte ) 0x63, ( byte ) 0x6F, ( byte ) 0x6D, ( byte ) 0x00, ( byte ) 0x00, (
byte ) 0x00, ( byte ) 0x01,
+            ( byte ) 0x45, ( byte ) 0xD7, ( byte ) 0x96, ( byte ) 0x79, ( byte ) 0x04, (
byte ) 0x00, ( byte ) 0x03,
+            ( byte ) 0x00, ( byte ) 0x08, ( byte ) 0x13, ( byte ) 0xD9, ( byte ) 0x19, (
byte ) 0x98, ( byte ) 0x23,
+            ( byte ) 0x8F, ( byte ) 0x9E, ( byte ) 0x31 };
+
+    private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
+
+    private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'"
);
+
+    static
+    {
+        dateFormat.setTimeZone( UTC_TIME_ZONE );
+    }
+
+
+    /**
+     * Read the first keytab test bytes and check for the presence of a valid DES key.
+     *
+     * @throws Exception
+     */
+    public void testReadKeytab1() throws Exception
+    {
+        Keytab keytab = Keytab.read( keytab1 );
+
+        assertTrue( "Keytab version", Arrays.equals( Keytab.VERSION_52, keytab.getKeytabVersion()
) );
+        assertEquals( "Entries size", 1, keytab.getEntries().size() );
+
+        KeytabEntry entry = keytab.getEntries().get( 0 );
+        EncryptionKey key = entry.getKey();
+
+        try
+        {
+            assertTrue( DESKeySpec.isParityAdjusted( key.getKeyValue(), 0 ) );
+        }
+        catch ( InvalidKeyException ike )
+        {
+            fail( "Key is invalid." );
+        }
+    }
+
+
+    /**
+     * Read the second keytab test bytes and check for the presence of a valid DES key.
+     *
+     * @throws Exception
+     */
+    public void testReadKeytab2() throws Exception
+    {
+        Keytab keytab = Keytab.read( keytab2 );
+
+        assertTrue( "Keytab version", Arrays.equals( Keytab.VERSION_52, keytab.getKeytabVersion()
) );
+        assertEquals( "Entries size", 1, keytab.getEntries().size() );
+
+        KeytabEntry entry = keytab.getEntries().get( 0 );
+        EncryptionKey key = entry.getKey();
+
+        try
+        {
+            assertTrue( DESKeySpec.isParityAdjusted( key.getKeyValue(), 0 ) );
+        }
+        catch ( InvalidKeyException ike )
+        {
+            fail( "Key is invalid." );
+        }
+    }
+
+
+    /**
+     * Test the writing of a keytab file.
+     *
+     * @throws Exception
+     */
+    public void testWriteKeytab() throws Exception
+    {
+        List<KeytabEntry> entries = new ArrayList<KeytabEntry>();
+
+        entries.add( getEntry1() );
+        entries.add( getEntry1() );
+
+        Keytab writer = Keytab.getInstance();
+        writer.setEntries( entries );
+        ByteBuffer buffer = writer.write();
+        assertEquals( "Expected file size.", 130, buffer.limit() );
+    }
+
+
+    private KeytabEntry getEntry1() throws ParseException
+    {
+        String principalName = "HTTP/www.verisign.com@EXAMPLE.COM";
+        long principalType = 1;
+
+        String zuluTime = "20070217235745Z";
+        Date date = null;
+        synchronized ( dateFormat )
+        {
+            date = dateFormat.parse( zuluTime );
+        }
+
+        KerberosTime timeStamp = new KerberosTime( date );
+
+        byte keyVersion = 1;
+        String passPhrase = "secret";
+        Map<EncryptionType, EncryptionKey> keys = KerberosKeyFactory.getKerberosKeys(
principalName, passPhrase );
+        EncryptionKey key = keys.get( EncryptionType.DES_CBC_MD5 );
+
+        return new KeytabEntry( principalName, principalType, timeStamp, keyVersion, key
);
+    }
+}

Propchange: directory/apacheds/branches/kerberos-encryption-types/kerberos-shared/src/test/java/org/apache/directory/server/kerberos/shared/keytab/KeytabTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message