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.encoder; 021 022 023 import java.io.ByteArrayOutputStream; 024 import java.io.IOException; 025 026 import org.apache.directory.server.kerberos.shared.messages.Encodable; 027 import org.apache.directory.server.kerberos.shared.messages.KdcReply; 028 import org.apache.directory.shared.asn1.der.ASN1OutputStream; 029 import org.apache.directory.shared.asn1.der.DERApplicationSpecific; 030 import org.apache.directory.shared.asn1.der.DERBitString; 031 import org.apache.directory.shared.asn1.der.DERGeneralString; 032 import org.apache.directory.shared.asn1.der.DERInteger; 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-22 02:00:43 +0200 (Tue, 22 May 2007) $ 040 */ 041 public abstract class EncKdcRepPartEncoder implements Encoder 042 { 043 private int applicationCode; 044 045 046 protected EncKdcRepPartEncoder(int applicationCode) 047 { 048 this.applicationCode = applicationCode; 049 } 050 051 052 public byte[] encode( Encodable app ) throws IOException 053 { 054 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 055 ASN1OutputStream aos = new ASN1OutputStream( baos ); 056 057 DERSequence initialSequence = encodeInitialSequence( ( KdcReply ) app ); 058 aos.writeObject( DERApplicationSpecific.valueOf( applicationCode, initialSequence ) ); 059 060 return baos.toByteArray(); 061 } 062 063 064 /** 065 * EncKDCRepPart ::= SEQUENCE { 066 * key[0] EncryptionKey, 067 * last-req[1] LastReq, 068 * 069 * nonce[2] INTEGER, 070 * key-expiration[3] KerberosTime OPTIONAL, 071 * flags[4] TicketFlags, 072 * authtime[5] KerberosTime, 073 * starttime[6] KerberosTime OPTIONAL, 074 * endtime[7] KerberosTime, 075 * renew-till[8] KerberosTime OPTIONAL, 076 * srealm[9] Realm, 077 * sname[10] PrincipalName, 078 * caddr[11] HostAddresses OPTIONAL 079 * } 080 */ 081 protected DERSequence encodeInitialSequence( KdcReply reply ) 082 { 083 DERSequence sequence = new DERSequence(); 084 085 sequence.add( new DERTaggedObject( 0, EncryptionKeyEncoder.encodeSequence( reply.getKey() ) ) ); 086 sequence.add( new DERTaggedObject( 1, LastRequestEncoder.encode( reply.getLastRequest() ) ) ); 087 sequence.add( new DERTaggedObject( 2, DERInteger.valueOf( reply.getNonce() ) ) ); 088 089 // OPTIONAL 090 if ( reply.getKeyExpiration() != null ) 091 { 092 sequence.add( new DERTaggedObject( 3, KerberosTimeEncoder.encode( reply.getKeyExpiration() ) ) ); 093 } 094 095 sequence.add( new DERTaggedObject( 4, new DERBitString( reply.getFlags().getBytes() ) ) ); 096 sequence.add( new DERTaggedObject( 5, KerberosTimeEncoder.encode( reply.getAuthTime() ) ) ); 097 098 // OPTIONAL 099 if ( reply.getStartTime() != null ) 100 { 101 sequence.add( new DERTaggedObject( 6, KerberosTimeEncoder.encode( reply.getStartTime() ) ) ); 102 } 103 104 sequence.add( new DERTaggedObject( 7, KerberosTimeEncoder.encode( reply.getEndTime() ) ) ); 105 106 // OPTIONAL 107 if ( reply.getRenewTill() != null ) 108 { 109 sequence.add( new DERTaggedObject( 8, KerberosTimeEncoder.encode( reply.getRenewTill() ) ) ); 110 } 111 112 sequence.add( new DERTaggedObject( 9, DERGeneralString.valueOf( reply.getServerRealm().toString() ) ) ); 113 sequence.add( new DERTaggedObject( 10, PrincipalNameEncoder.encode( reply.getServerPrincipal() ) ) ); 114 115 // OPTIONAL 116 if ( reply.getClientAddresses() != null ) 117 { 118 sequence.add( new DERTaggedObject( 11, HostAddressesEncoder.encodeSequence( reply.getClientAddresses() ) ) ); 119 } 120 121 return sequence; 122 } 123 }