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.IOException;
024 import java.nio.ByteBuffer;
025
026 import org.apache.directory.server.kerberos.shared.messages.KdcReply;
027 import org.apache.directory.server.kerberos.shared.messages.value.PaData;
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.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: 587624 $, $Date: 2007-10-23 22:22:08 +0300 (Tue, 23 Oct 2007) $
040 */
041 public class KdcReplyEncoder
042 {
043 /**
044 * Encodes a {@link KdcReply} into a {@link ByteBuffer}.
045 *
046 * AS-REP ::= [APPLICATION 11] KDC-REP
047 * TGS-REP ::= [APPLICATION 13] KDC-REP
048 *
049 * @param app
050 * @param out
051 * @throws IOException
052 */
053 public void encode( KdcReply app, ByteBuffer out ) throws IOException
054 {
055 ASN1OutputStream aos = new ASN1OutputStream( out );
056
057 DERSequence kdcrep = encodeKdcReplySequence( app );
058 aos.writeObject( DERApplicationSpecific.valueOf( app.getMessageType().getOrdinal(), kdcrep ) );
059
060 aos.close();
061 }
062
063
064 /*
065 KDC-REP ::= SEQUENCE {
066 pvno[0] INTEGER,
067 msg-type[1] INTEGER,
068 padata[2] SEQUENCE OF PA-DATA OPTIONAL,
069 crealm[3] Realm,
070 cname[4] PrincipalName,
071 ticket[5] Ticket,
072 enc-part[6] EncryptedData
073 }*/
074 private DERSequence encodeKdcReplySequence( KdcReply app )
075 {
076 DERSequence sequence = new DERSequence();
077
078 sequence.add( new DERTaggedObject( 0, DERInteger.valueOf( app.getProtocolVersionNumber() ) ) );
079
080 sequence.add( new DERTaggedObject( 1, DERInteger.valueOf( app.getMessageType().getOrdinal() ) ) );
081
082 if ( app.getPaData() != null )
083 {
084 sequence.add( new DERTaggedObject( 2, encodePreAuthData( app.getPaData() ) ) );
085 }
086
087 sequence.add( new DERTaggedObject( 3, DERGeneralString.valueOf( app.getClientRealm().toString() ) ) );
088
089 sequence.add( new DERTaggedObject( 4, PrincipalNameEncoder.encode( app.getClientPrincipal() ) ) );
090
091 sequence.add( new DERTaggedObject( 5, TicketEncoder.encode( app.getTicket() ) ) );
092
093 sequence.add( new DERTaggedObject( 6, EncryptedDataEncoder.encodeSequence( app.getEncPart() ) ) );
094
095 return sequence;
096 }
097
098
099 /*
100 PA-DATA ::= SEQUENCE {
101 padata-type[1] INTEGER,
102 padata-value[2] OCTET STRING,
103 -- might be encoded AP-REQ
104 }*/
105 private DERSequence encodePreAuthData( PaData[] preAuthData )
106 {
107 DERSequence preAuth = new DERSequence();
108
109 for ( int ii = 0; ii < preAuthData.length; ii++ )
110 {
111 DERSequence sequence = new DERSequence();
112
113 sequence.add( new DERTaggedObject( 1, DERInteger.valueOf( preAuthData[ii].getPaDataType().getOrdinal() ) ) );
114 sequence.add( new DERTaggedObject( 2, new DEROctetString( preAuthData[ii].getPaDataValue() ) ) );
115 preAuth.add( sequence );
116 }
117
118 return preAuth;
119 }
120 }