View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.server.kerberos.shared.io.encoder;
21  
22  
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  
26  import org.apache.directory.server.kerberos.shared.messages.Encodable;
27  import org.apache.directory.server.kerberos.shared.messages.components.EncKrbPrivPart;
28  import org.apache.directory.shared.asn1.der.ASN1OutputStream;
29  import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
30  import org.apache.directory.shared.asn1.der.DERInteger;
31  import org.apache.directory.shared.asn1.der.DEROctetString;
32  import org.apache.directory.shared.asn1.der.DERSequence;
33  import org.apache.directory.shared.asn1.der.DERTaggedObject;
34  
35  
36  /**
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   * @version $Rev: 547131 $, $Date: 2007-06-14 07:41:06 +0200 (Do, 14 Jun 2007) $
39   */
40  public class EncKrbPrivPartEncoder implements Encoder, EncoderFactory
41  {
42      private static final int APPLICATION_CODE = 28;
43  
44  
45      public Encoder getEncoder()
46      {
47          return new EncKrbPrivPartEncoder();
48      }
49  
50  
51      public byte[] encode( Encodable privPart ) throws IOException
52      {
53          ByteArrayOutputStream baos = new ByteArrayOutputStream();
54          ASN1OutputStream aos = new ASN1OutputStream( baos );
55  
56          DERSequence privPartSequence = encodePrivatePartSequence( ( EncKrbPrivPart ) privPart );
57          aos.writeObject( DERApplicationSpecific.valueOf( APPLICATION_CODE, privPartSequence ) );
58          aos.close();
59  
60          return baos.toByteArray();
61      }
62  
63  
64      /**
65       * Encodes an {@link EncKrbPrivPart} into a {@link DERSequence}.
66       * 
67       * EncKrbPrivPart  ::= [APPLICATION 28] SEQUENCE {
68       *         user-data       [0] OCTET STRING,
69       *         timestamp       [1] KerberosTime OPTIONAL,
70       *         usec            [2] Microseconds OPTIONAL,
71       *         seq-number      [3] UInt32 OPTIONAL,
72       *         s-address       [4] HostAddress -- sender's addr --,
73       *         r-address       [5] HostAddress OPTIONAL -- recip's addr
74       * }
75       *
76       * @param message
77       * @return The {@link DERSequence};
78       */
79      private DERSequence encodePrivatePartSequence( EncKrbPrivPart message )
80      {
81          DERSequence sequence = new DERSequence();
82  
83          sequence.add( new DERTaggedObject( 0, new DEROctetString( message.getUserData() ) ) );
84  
85          if ( message.getTimestamp() != null )
86          {
87              sequence.add( new DERTaggedObject( 1, KerberosTimeEncoder.encode( message.getTimestamp() ) ) );
88          }
89  
90          if ( message.getMicroSecond() != null )
91          {
92              sequence.add( new DERTaggedObject( 2, DERInteger.valueOf( message.getMicroSecond().intValue() ) ) );
93          }
94  
95          if ( message.getSequenceNumber() != null )
96          {
97              sequence.add( new DERTaggedObject( 3, DERInteger.valueOf( message.getSequenceNumber().intValue() ) ) );
98          }
99  
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 }