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.components.Ticket; 027 import org.apache.directory.shared.asn1.der.ASN1OutputStream; 028 import org.apache.directory.shared.asn1.der.DERApplicationSpecific; 029 import org.apache.directory.shared.asn1.der.DERGeneralString; 030 import org.apache.directory.shared.asn1.der.DERInteger; 031 import org.apache.directory.shared.asn1.der.DERSequence; 032 import org.apache.directory.shared.asn1.der.DERTaggedObject; 033 034 035 /** 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 * @version $Rev: 589780 $, $Date: 2007-10-29 19:14:59 +0100 (Mon, 29 Oct 2007) $ 038 */ 039 public class TicketEncoder 040 { 041 /** 042 * Encodes a {@link Ticket} into a its ASN.1 DER encoding. 043 * 044 * @param ticket 045 * @return The byte[] containing the ASN.1 DER encoding of the {@link Ticket}. 046 * @throws IOException 047 */ 048 public static byte[] encodeTicket( Ticket ticket ) throws IOException 049 { 050 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 051 ASN1OutputStream aos = new ASN1OutputStream( baos ); 052 053 aos.writeObject( encode( ticket ) ); 054 aos.close(); 055 056 return baos.toByteArray(); 057 } 058 059 060 /** 061 * Ticket ::= [APPLICATION 1] SEQUENCE { 062 * tkt-vno[0] INTEGER, 063 * realm[1] Realm, 064 * sname[2] PrincipalName, 065 * enc-part[3] EncryptedData 066 * } 067 */ 068 protected static DERApplicationSpecific encode( Ticket ticket ) 069 { 070 DERSequence vector = new DERSequence(); 071 072 vector.add( new DERTaggedObject( 0, DERInteger.valueOf( ticket.getTktVno() ) ) ); 073 vector.add( new DERTaggedObject( 1, DERGeneralString.valueOf( ticket.getRealm() ) ) ); 074 vector.add( new DERTaggedObject( 2, PrincipalNameEncoder.encode( ticket.getSName() ) ) ); 075 vector.add( new DERTaggedObject( 3, EncryptedDataEncoder.encodeSequence( ticket.getEncPart() ) ) ); 076 077 DERApplicationSpecific ticketSequence = null; 078 079 try 080 { 081 ticketSequence = DERApplicationSpecific.valueOf( 1, vector ); 082 } 083 catch ( Exception e ) 084 { 085 e.printStackTrace(); 086 } 087 088 return ticketSequence; 089 } 090 091 092 protected static DERSequence encodeSequence( Ticket[] tickets ) 093 { 094 DERSequence outerVector = new DERSequence(); 095 096 for ( int ii = 0; ii < tickets.length; ii++ ) 097 { 098 DERSequence vector = new DERSequence(); 099 vector.add( encode( tickets[ii] ) ); 100 outerVector.add( vector ); 101 } 102 103 return outerVector; 104 } 105 }