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.decoder;
21
22
23 import java.io.IOException;
24 import java.util.Enumeration;
25
26 import org.apache.directory.server.kerberos.shared.messages.application.ApplicationReply;
27 import org.apache.directory.shared.asn1.der.ASN1InputStream;
28 import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
29 import org.apache.directory.shared.asn1.der.DEREncodable;
30 import org.apache.directory.shared.asn1.der.DERSequence;
31 import org.apache.directory.shared.asn1.der.DERTaggedObject;
32
33
34 /**
35 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
36 * @version $Rev: 540371 $, $Date: 2007-05-21 17:00:43 -0700 (Mon, 21 May 2007) $
37 */
38 public class ApplicationReplyDecoder
39 {
40 /**
41 * Decodes a byte array into an {@link ApplicationReply}.
42 *
43 * @param encodedAuthHeader
44 * @return The {@link ApplicationReply}.
45 * @throws IOException
46 */
47 public ApplicationReply decode( byte[] encodedAuthHeader ) throws IOException
48 {
49 ASN1InputStream ais = new ASN1InputStream( encodedAuthHeader );
50
51 DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
52
53 DERSequence apreq = ( DERSequence ) app.getObject();
54
55 return decodeApplicationRequestSequence( apreq );
56 }
57
58
59 /*
60 AP-REP ::= [APPLICATION 15] SEQUENCE {
61 pvno[0] INTEGER,
62 msg-type[1] INTEGER,
63 enc-part[2] EncryptedData
64 }
65 */
66 private ApplicationReply decodeApplicationRequestSequence( DERSequence sequence )
67 {
68 ApplicationReply authHeader = null;
69
70 for ( Enumeration e = sequence.getObjects(); e.hasMoreElements(); )
71 {
72 DERTaggedObject object = ( ( DERTaggedObject ) e.nextElement() );
73 int tag = object.getTagNo();
74 DEREncodable derObject = object.getObject();
75
76 switch ( tag )
77 {
78 case 0:
79 //DERInteger tag0 = ( DERInteger ) derObject;
80 //authHeader.setProtocolVersionNumber( tag0.intValue() );
81 break;
82 case 1:
83 //DERInteger tag1 = ( DERInteger ) derObject;
84 //authHeader.setMessageType( MessageType.getTypeByOrdinal( tag1.intValue() ) );
85 break;
86 case 2:
87 DERSequence tag2 = ( DERSequence ) derObject;
88 authHeader = new ApplicationReply( EncryptedDataDecoder.decode( tag2 ) );
89 break;
90 }
91 }
92
93 return authHeader;
94 }
95 }