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.EncKrbPrivPart;
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.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: 547131 $, $Date: 2007-06-14 07:41:06 +0200 (Thu, 14 Jun 2007) $
039     */
040    public class EncKrbPrivPartEncoder implements Encoder, EncoderFactory
041    {
042        private static final int APPLICATION_CODE = 28;
043    
044    
045        public Encoder getEncoder()
046        {
047            return new EncKrbPrivPartEncoder();
048        }
049    
050    
051        public byte[] encode( Encodable privPart ) throws IOException
052        {
053            ByteArrayOutputStream baos = new ByteArrayOutputStream();
054            ASN1OutputStream aos = new ASN1OutputStream( baos );
055    
056            DERSequence privPartSequence = encodePrivatePartSequence( ( EncKrbPrivPart ) privPart );
057            aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, privPartSequence ) );
058            aos.close();
059    
060            return baos.toByteArray();
061        }
062    
063    
064        /**
065         * Encodes an {@link EncKrbPrivPart} into a {@link DERSequence}.
066         * 
067         * EncKrbPrivPart  ::= [APPLICATION 28] SEQUENCE {
068         *         user-data       [0] OCTET STRING,
069         *         timestamp       [1] KerberosTime OPTIONAL,
070         *         usec            [2] Microseconds OPTIONAL,
071         *         seq-number      [3] UInt32 OPTIONAL,
072         *         s-address       [4] HostAddress -- sender's addr --,
073         *         r-address       [5] HostAddress OPTIONAL -- recip's addr
074         * }
075         *
076         * @param message
077         * @return The {@link DERSequence};
078         */
079        private DERSequence encodePrivatePartSequence( EncKrbPrivPart message )
080        {
081            DERSequence sequence = new DERSequence();
082    
083            sequence.add( new DERTaggedObject( 0, new DEROctetString( message.getUserData() ) ) );
084    
085            if ( message.getTimestamp() != null )
086            {
087                sequence.add( new DERTaggedObject( 1, KerberosTimeEncoder.encode( message.getTimestamp() ) ) );
088            }
089    
090            if ( message.getMicroSecond() != null )
091            {
092                sequence.add( new DERTaggedObject( 2, DERInteger.valueOf( message.getMicroSecond().intValue() ) ) );
093            }
094    
095            if ( message.getSequenceNumber() != null )
096            {
097                sequence.add( new DERTaggedObject( 3, DERInteger.valueOf( message.getSequenceNumber().intValue() ) ) );
098            }
099    
100            sequence.add( new DERTaggedObject( 4, HostAddressesEncoder.encode( message.getSenderAddress() ) ) );
101    
102            if ( message.getRecipientAddress() != null )
103            {
104                sequence.add( new DERTaggedObject( 5, HostAddressesEncoder.encode( message.getRecipientAddress() ) ) );
105            }
106    
107            return sequence;
108        }
109    }