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.EncryptedData;
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: 587682 $, $Date: 2007-10-24 01:47:43 +0300 (Wed, 24 Oct 2007) $
039 */
040 public class EncryptedDataDecoder
041 {
042 /**
043 * Decodes a byte array into an {@link EncryptedData}.
044 *
045 * @param encodedEncryptedData
046 * @return The {@link EncryptedData}.
047 * @throws IOException
048 */
049 public static EncryptedData decode( byte[] encodedEncryptedData ) throws IOException
050 {
051 ASN1InputStream ais = new ASN1InputStream( encodedEncryptedData );
052
053 DERSequence sequence = ( DERSequence ) ais.readObject();
054
055 return decode( sequence );
056 }
057
058
059 /**
060 * Decodes a {@link DERSequence} into an {@link EncryptedData}.
061 *
062 * EncryptedData ::= SEQUENCE {
063 * etype[0] INTEGER, -- EncryptionEngine
064 * kvno[1] INTEGER OPTIONAL,
065 * cipher[2] OCTET STRING -- ciphertext
066 * }
067 *
068 * @param sequence
069 * @return The {@link EncryptedData}.
070 */
071 public static EncryptedData decode( DERSequence sequence )
072 {
073 EncryptedData encryptedData = new EncryptedData();
074
075 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
076 {
077 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
078 int tag = object.getTagNo();
079 DEREncodable derObject = object.getObject();
080
081 switch ( tag )
082 {
083 case 0:
084 DERInteger etype = ( DERInteger ) derObject;
085 encryptedData.setEType( EncryptionType.getTypeByOrdinal( etype.intValue() ) );
086 break;
087
088 case 1:
089 DERInteger kvno = ( DERInteger ) derObject;
090 encryptedData.setKvno( kvno.intValue() );
091 break;
092
093 case 2:
094 DEROctetString cipher = ( DEROctetString ) derObject;
095 encryptedData.setCipher( cipher.getOctets() );
096 break;
097 }
098 }
099
100 return encryptedData;
101 }
102 }