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.decoder;
021
022
023 import java.io.IOException;
024 import java.util.Enumeration;
025
026 import org.apache.directory.server.kerberos.shared.messages.components.Ticket;
027 import org.apache.directory.shared.asn1.der.ASN1InputStream;
028 import org.apache.directory.shared.asn1.der.DERApplicationSpecific;
029 import org.apache.directory.shared.asn1.der.DEREncodable;
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.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: 589780 $, $Date: 2007-10-29 20:14:59 +0200 (Mon, 29 Oct 2007) $
039 */
040 public class TicketDecoder
041 {
042 /**
043 * Decodes a byte array into an {@link Ticket}.
044 *
045 * @param encodedTicket
046 * @return The {@link Ticket}.
047 * @throws IOException
048 */
049 public static Ticket decode( byte[] encodedTicket ) throws IOException
050 {
051 ASN1InputStream ais = new ASN1InputStream( encodedTicket );
052
053 DERApplicationSpecific app = ( DERApplicationSpecific ) ais.readObject();
054
055 return decode( app );
056 }
057
058
059 /**
060 * Decodes a {@link DERSequence} into an array of {@link Ticket}s.
061 *
062 * @param sequence
063 * @return The array of {@link Ticket}s.
064 * @throws IOException
065 */
066 public static Ticket[] decodeSequence( DERSequence sequence ) throws IOException
067 {
068 Ticket[] tickets = new Ticket[sequence.size()];
069
070 int ii = 0;
071 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
072 {
073 DERApplicationSpecific object = ( DERApplicationSpecific ) e.nextElement();
074 tickets[ii] = decode( object );
075 }
076
077 return tickets;
078 }
079
080
081 /**
082 * Ticket ::= [APPLICATION 1] SEQUENCE {
083 * tkt-vno[0] INTEGER,
084 * realm[1] Realm,
085 * sname[2] PrincipalName,
086 * enc-part[3] EncryptedData
087 * }
088 */
089 protected static Ticket decode( DERApplicationSpecific app ) throws IOException
090 {
091 DERSequence sequence = ( DERSequence ) app.getObject();
092
093 Ticket ticket = new Ticket();
094
095 for ( Enumeration<DEREncodable> e = sequence.getObjects(); e.hasMoreElements(); )
096 {
097 DERTaggedObject object = ( DERTaggedObject ) e.nextElement();
098 int tag = object.getTagNo();
099 DEREncodable derObject = object.getObject();
100
101 switch ( tag )
102 {
103 case 0:
104 DERInteger tag0 = ( DERInteger ) derObject;
105 ticket.setTktVno( tag0.intValue() );
106 break;
107
108 case 1:
109 DERGeneralString tag1 = ( DERGeneralString ) derObject;
110 ticket.setRealm( tag1.getString() );
111 break;
112
113 case 2:
114 DERSequence tag2 = ( DERSequence ) derObject;
115 ticket.setSName( PrincipalNameDecoder.decode( tag2 ) );
116 break;
117
118 case 3:
119 DERSequence tag3 = ( DERSequence ) derObject;
120 ticket.setEncPart( EncryptedDataDecoder.decode( tag3 ) );
121 break;
122 }
123 }
124
125 return ticket;
126 }
127 }