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.components.EncTicketPart;
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.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: 546367 $, $Date: 2007-06-12 06:30:24 +0300 (Tue, 12 Jun 2007) $
039 */
040 public class EncTicketPartEncoder implements Encoder, EncoderFactory
041 {
042 /**
043 * Application code constant for the {@link EncTicketPart} (3).
044 */
045 private static final int APPLICATION_CODE = 3;
046
047
048 public byte[] encode( Encodable ticketPart ) throws IOException
049 {
050 ByteArrayOutputStream baos = new ByteArrayOutputStream();
051 ASN1OutputStream aos = new ASN1OutputStream( baos );
052
053 DERSequence ticketSequence = encodeInitialSequence( ( EncTicketPart ) ticketPart );
054 aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, ticketSequence ) );
055 aos.close();
056
057 return baos.toByteArray();
058 }
059
060
061 public Encoder getEncoder()
062 {
063 return new EncTicketPartEncoder();
064 }
065
066
067 /**
068 * Encodes an {@link EncTicketPart} into a {@link DERSequence}.
069 *
070 * -- Encrypted part of ticket
071 * EncTicketPart ::= [APPLICATION 3] SEQUENCE {
072 * flags[0] TicketFlags,
073 * key[1] EncryptionKey,
074 * crealm[2] Realm,
075 * cname[3] PrincipalName,
076 * transited[4] TransitedEncoding,
077 * authtime[5] KerberosTime,
078 * starttime[6] KerberosTime OPTIONAL,
079 * endtime[7] KerberosTime,
080 * renew-till[8] KerberosTime OPTIONAL,
081 * caddr[9] HostAddresses OPTIONAL,
082 * authorization-data[10] AuthorizationData OPTIONAL
083 * }
084 *
085 * @param ticketPart
086 * @return The {@link DERSequence}.
087 */
088 public DERSequence encodeInitialSequence( EncTicketPart ticketPart )
089 {
090 DERSequence sequence = new DERSequence();
091
092 sequence.add( new DERTaggedObject( 0, new DERBitString( ticketPart.getFlags().getBytes() ) ) );
093 sequence.add( new DERTaggedObject( 1, EncryptionKeyEncoder.encodeSequence( ticketPart.getSessionKey() ) ) );
094 sequence.add( new DERTaggedObject( 2, DERGeneralString.valueOf( ticketPart.getClientRealm().toString() ) ) );
095 sequence.add( new DERTaggedObject( 3, PrincipalNameEncoder.encode( ticketPart.getClientPrincipal() ) ) );
096 sequence.add( new DERTaggedObject( 4, TransitedEncodingEncoder.encode( ticketPart.getTransitedEncoding() ) ) );
097 sequence.add( new DERTaggedObject( 5, KerberosTimeEncoder.encode( ticketPart.getAuthTime() ) ) );
098
099 // OPTIONAL
100 if ( ticketPart.getStartTime() != null )
101 {
102 sequence.add( new DERTaggedObject( 6, KerberosTimeEncoder.encode( ticketPart.getStartTime() ) ) );
103 }
104
105 sequence.add( new DERTaggedObject( 7, KerberosTimeEncoder.encode( ticketPart.getEndTime() ) ) );
106
107 // OPTIONAL
108 if ( ticketPart.getRenewTill() != null )
109 {
110 sequence.add( new DERTaggedObject( 8, KerberosTimeEncoder.encode( ticketPart.getRenewTill() ) ) );
111 }
112
113 // OPTIONAL
114 if ( ticketPart.getClientAddresses() != null )
115 {
116 sequence
117 .add( new DERTaggedObject( 9, HostAddressesEncoder.encodeSequence( ticketPart.getClientAddresses() ) ) );
118 }
119
120 // OPTIONAL
121 if ( ticketPart.getAuthorizationData() != null )
122 {
123 sequence
124 .add( new DERTaggedObject( 10, AuthorizationDataEncoder.encode( ticketPart.getAuthorizationData() ) ) );
125 }
126
127 return sequence;
128 }
129 }