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.EncryptionTypeInfo2Entry; 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.DERGeneralString; 031 import org.apache.directory.shared.asn1.der.DERInteger; 032 import org.apache.directory.shared.asn1.der.DEROctetString; 033 import org.apache.directory.shared.asn1.der.DERSequence; 034 import org.apache.directory.shared.asn1.der.DERTaggedObject; 035 036 037 /** 038 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 039 * @version $Rev: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $ 040 */ 041 public class EncryptionTypeInfo2Decoder 042 { 043 /** 044 * Decodes a byte array into an array of {@link EncryptionTypeInfo2Entry}. 045 * 046 * @param encodedEntries 047 * @return The array of {@link EncryptionTypeInfo2Entry}. 048 * @throws IOException 049 */ 050 public EncryptionTypeInfo2Entry[] decode( byte[] encodedEntries ) throws IOException 051 { 052 ASN1InputStream ais = new ASN1InputStream( encodedEntries ); 053 054 DERSequence sequence = ( DERSequence ) ais.readObject(); 055 056 return decodeSequence( sequence ); 057 } 058 059 060 /** 061 * ETYPE-INFO2 ::= SEQUENCE SIZE (1..MAX) OF ETYPE-INFO2-ENTRY 062 */ 063 protected static EncryptionTypeInfo2Entry[] decodeSequence( DERSequence sequence ) 064 { 065 EncryptionTypeInfo2Entry[] entrySequence = new EncryptionTypeInfo2Entry[sequence.size()]; 066 067 int ii = 0; 068 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); ) 069 { 070 DERSequence object = (DERSequence)e.nextElement(); 071 entrySequence[ii] = decode( object ); 072 ii++; 073 } 074 075 return entrySequence; 076 } 077 078 079 /** 080 * ETYPE-INFO2-ENTRY ::= SEQUENCE { 081 * etype [0] Int32, 082 * salt [1] KerberosString OPTIONAL, 083 * s2kparams [2] OCTET STRING OPTIONAL 084 * } 085 */ 086 protected static EncryptionTypeInfo2Entry decode( DERSequence sequence ) 087 { 088 EncryptionType encryptionType = EncryptionType.NULL; 089 String salt = new String(); 090 byte[] s2kparams = new byte[0]; 091 092 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); ) 093 { 094 DERTaggedObject object = (DERTaggedObject)e.nextElement(); 095 int tag = object.getTagNo(); 096 DEREncodable derObject = object.getObject(); 097 098 switch ( tag ) 099 { 100 case 0: 101 DERInteger tag0 = ( DERInteger ) derObject; 102 encryptionType = EncryptionType.getTypeByOrdinal( tag0.intValue() ); 103 break; 104 case 1: 105 DERGeneralString tag1 = ( DERGeneralString ) derObject; 106 salt = tag1.getString(); 107 break; 108 case 2: 109 DEROctetString tag2 = ( DEROctetString ) derObject; 110 s2kparams = tag2.getOctets(); 111 break; 112 } 113 } 114 115 return new EncryptionTypeInfo2Entry( encryptionType, salt, s2kparams ); 116 } 117 }