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 */
020 package org.apache.directory.server.kerberos.shared.io.decoder;
021
022
023 import java.io.IOException;
024 import java.util.Enumeration;
025
026 import org.apache.directory.server.kerberos.shared.crypto.encryption.EncryptionType;
027 import org.apache.directory.server.kerberos.shared.messages.value.EncryptionKey;
028 import org.apache.directory.shared.asn1.der.ASN1InputStream;
029 import org.apache.directory.shared.asn1.der.DEREncodable;
030 import org.apache.directory.shared.asn1.der.DERInteger;
031 import org.apache.directory.shared.asn1.der.DEROctetString;
032 import org.apache.directory.shared.asn1.der.DERSequence;
033 import org.apache.directory.shared.asn1.der.DERTaggedObject;
034
035
036 /**
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 * @version $Rev: 540371 $, $Date: 2007-05-22 03:00:43 +0300 (Tue, 22 May 2007) $
039 */
040 public class EncryptionKeyDecoder
041 {
042 /**
043 * Decodes a byte array into an {@link EncryptionKey}.
044 *
045 * @param encodedEncryptionKey
046 * @return The {@link EncryptionKey}.
047 * @throws IOException
048 */
049 public static EncryptionKey decode( byte[] encodedEncryptionKey ) throws IOException
050 {
051 ASN1InputStream ais = new ASN1InputStream( encodedEncryptionKey );
052
053 DERSequence sequence = ( DERSequence ) ais.readObject();
054
055 return decode( sequence );
056 }
057
058
059 /**
060 * EncryptionKey ::= SEQUENCE {
061 * keytype[0] INTEGER,
062 * keyvalue[1] OCTET STRING
063 * }
064 */
065 protected static EncryptionKey decode( DERSequence sequence )
066 {
067 EncryptionType type = EncryptionType.NULL;
068 byte[] data = null;
069
070 for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
071 {
072 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
073 int tag = object.getTagNo();
074 DEREncodable derObject = object.getObject();
075
076 switch ( tag )
077 {
078 case 0:
079 DERInteger tag0 = ( DERInteger ) derObject;
080 type = EncryptionType.getTypeByOrdinal( tag0.intValue() );
081 break;
082 case 1:
083 DEROctetString tag1 = ( DEROctetString ) derObject;
084 data = tag1.getOctets();
085 break;
086 }
087 }
088
089 return new EncryptionKey( type, data );
090 }
091 }