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.shared.kerberos.codec;
021
022
023import java.nio.ByteBuffer;
024
025import org.apache.directory.api.asn1.DecoderException;
026import org.apache.directory.api.asn1.ber.Asn1Container;
027import org.apache.directory.api.asn1.ber.Asn1Decoder;
028import org.apache.directory.shared.kerberos.codec.encryptionKey.EncryptionKeyContainer;
029import org.apache.directory.shared.kerberos.codec.principalName.PrincipalNameContainer;
030import org.apache.directory.shared.kerberos.components.EncryptionKey;
031import org.apache.directory.shared.kerberos.components.PrincipalName;
032import org.apache.directory.shared.kerberos.exceptions.ErrorType;
033import org.apache.directory.shared.kerberos.exceptions.KerberosException;
034
035
036/**
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class KerberosDecoder
040{
041    /**
042     * Decode an EncryptionKey structure
043     * 
044     * @param data The byte array containing the data structure to decode
045     * @return An instance of EncryptionKey
046     * @throws KerberosException If the decoding fails
047     */
048    public static EncryptionKey decodeEncryptionKey( byte[] data ) throws KerberosException
049    {
050        ByteBuffer stream = ByteBuffer.allocate( data.length );
051        stream.put( data );
052        stream.flip();
053        
054        // Allocate a EncryptionKey Container
055        Asn1Container encryptionKeyContainer = new EncryptionKeyContainer();
056
057        // Decode the EncryptionKey PDU
058        try
059        {
060            Asn1Decoder.decode( stream, encryptionKeyContainer );
061        }
062        catch ( DecoderException de )
063        {
064            throw new KerberosException( ErrorType.KRB_AP_ERR_BAD_INTEGRITY, de );
065        }
066
067        // get the decoded EncryptionKey
068        return ( ( EncryptionKeyContainer ) encryptionKeyContainer ).getEncryptionKey();
069    }
070    
071    
072    /**
073     * Decode an PrincipalName structure
074     * 
075     * @param data The byte array containing the data structure to decode
076     * @return An instance of PrincipalName
077     * @throws KerberosException If the decoding fails
078     */
079    public static PrincipalName decodePrincipalName( byte[] data ) throws KerberosException
080    {
081        ByteBuffer stream = ByteBuffer.allocate( data.length );
082        stream.put( data );
083        stream.flip();
084        
085        // Allocate a PrincipalName Container
086        Asn1Container principalNameContainer = new PrincipalNameContainer();
087
088        // Decode the PrincipalName PDU
089        try
090        {
091            Asn1Decoder.decode( stream, principalNameContainer );
092        }
093        catch ( DecoderException de )
094        {
095            throw new KerberosException( ErrorType.KRB_AP_ERR_BAD_INTEGRITY, de );
096        }
097
098        // get the decoded PrincipalName
099        return ( ( PrincipalNameContainer ) principalNameContainer ).getPrincipalName();
100    }
101}