blob: f37f252bbaa1d3ac8556b484ebb67795a8a3a3bd [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25/*
26 *
27 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
28 * Copyright 1997 The Open Group Research Institute. All rights reserved.
29 */
30
31package sun.security.krb5.internal;
32
33import sun.security.util.*;
34import sun.security.krb5.Asn1Exception;
35import sun.security.krb5.RealmException;
36import java.util.Vector;
37import java.io.IOException;
38import java.math.BigInteger;
39/**
40 * Implements the ASN.1 EncKrbCredPart type.
41 *
42 * <xmp>
43 * EncKrbCredPart ::= [APPLICATION 29] SEQUENCE {
44 * ticket-info [0] SEQUENCE OF KrbCredInfo,
45 * nonce [1] UInt32 OPTIONAL,
46 * timestamp [2] KerberosTime OPTIONAL,
47 * usec [3] Microseconds OPTIONAL,
48 * s-address [4] HostAddress OPTIONAL,
49 * r-address [5] HostAddress OPTIONAL
50 * }
51 * </xmp>
52 *
53 * <p>
54 * This definition reflects the Network Working Group RFC 4120
55 * specification available at
56 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
57 * http://www.ietf.org/rfc/rfc4120.txt</a>.
58 */
59public class EncKrbCredPart {
60 public KrbCredInfo[] ticketInfo = null;
61 public KerberosTime timeStamp; //optional
62
63 private Integer nonce; //optional
64 private Integer usec; //optional
65 private HostAddress sAddress; //optional
66 private HostAddresses rAddress; //optional
67
68 public EncKrbCredPart(
69 KrbCredInfo[] new_ticketInfo,
70 KerberosTime new_timeStamp,
71 Integer new_usec,
72 Integer new_nonce,
73 HostAddress new_sAddress,
74 HostAddresses new_rAddress
75 ) throws IOException {
76 if (new_ticketInfo != null) {
77 ticketInfo = new KrbCredInfo[new_ticketInfo.length];
78 for (int i = 0; i < new_ticketInfo.length; i++) {
79 if (new_ticketInfo[i] == null) {
80 throw new IOException("Cannot create a EncKrbCredPart");
81 } else {
82 ticketInfo[i] = (KrbCredInfo)new_ticketInfo[i].clone();
83 }
84 }
85 }
86 timeStamp = new_timeStamp;
87 usec = new_usec;
88 nonce = new_nonce;
89 sAddress = new_sAddress;
90 rAddress = new_rAddress;
91 }
92
93 public EncKrbCredPart(byte[] data) throws Asn1Exception,
94 IOException, RealmException {
95 init(new DerValue(data));
96 }
97
98 public EncKrbCredPart(DerValue encoding) throws Asn1Exception,
99 IOException, RealmException {
100 init(encoding);
101 }
102
103 /**
104 * Initializes an EncKrbCredPart object.
105 * @param encoding a single DER-encoded value.
106 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
107 * @exception IOException if an I/O error occurs while reading encoded data.
108 * @exception RealmException if an error occurs while parsing a Realm object.
109 */
110 private void init(DerValue encoding) throws Asn1Exception,
111 IOException, RealmException {
112 DerValue der, subDer;
113 //may not be the correct error code for a tag
114 //mismatch on an encrypted structure
115 nonce = null;
116 timeStamp = null;
117 usec= null;
118 sAddress = null;
119 rAddress = null;
120 if (((encoding.getTag() & (byte)0x1F) != (byte)0x1D)
121 || (encoding.isApplication() != true)
122 || (encoding.isConstructed() != true))
123 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
124 der = encoding.getData().getDerValue();
125 if (der.getTag() != DerValue.tag_Sequence)
126 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
127
128 subDer = der.getData().getDerValue();
129 if ((subDer.getTag() & (byte)0x1F) == (byte)0x00) {
130 DerValue derValues[] = subDer.getData().getSequence(1);
131 ticketInfo = new KrbCredInfo[derValues.length];
132 for (int i = 0; i < derValues.length; i++) {
133 ticketInfo[i] = new KrbCredInfo(derValues[i]);
134 }
135 }
136 else
137 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
138 if (der.getData().available() > 0) {
139 if (((byte)(der.getData().peekByte()) & (byte)0x1F) == (byte)0x01) {
140 subDer = der.getData().getDerValue();
141 nonce = new Integer(subDer.getData().getBigInteger().intValue());
142 }
143 }
144 if (der.getData().available() >0) {
145 timeStamp = KerberosTime.parse(der.getData(), (byte)0x02, true);
146 }
147 if (der.getData().available() >0) {
148 if (((byte)(der.getData().peekByte()) & (byte)0x1F) == (byte)0x03) {
149 subDer = der.getData().getDerValue();
150 usec = new Integer(subDer.getData().getBigInteger().intValue());
151 }
152 }
153 if (der.getData().available() >0) {
154 sAddress = HostAddress.parse(der.getData(), (byte)0x04, true);
155 }
156 if (der.getData().available() >0) {
157 rAddress = HostAddresses.parse(der.getData(), (byte)0x05, true);
158 }
159 if (der.getData().available() >0)
160 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
161 }
162
163 /**
164 * Encodes an EncKrbCredPart object.
165 * @return byte array of encoded EncKrbCredPart object.
166 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
167 * @exception IOException if an I/O error occurs while reading encoded data.
168 *
169 */
170 public byte[] asn1Encode() throws Asn1Exception, IOException{
171 DerOutputStream bytes = new DerOutputStream();
172 DerOutputStream temp = new DerOutputStream();
173 DerValue[] tickets = new DerValue[ticketInfo.length];
174 for (int i = 0; i < ticketInfo.length; i++)
175 tickets[i] = new DerValue(ticketInfo[i].asn1Encode());
176 temp.putSequence(tickets);
177 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
178
179 if (nonce != null) {
180 temp = new DerOutputStream();
181 temp.putInteger(BigInteger.valueOf(nonce.intValue()));
182 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
183 }
184 if (timeStamp != null) {
185 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x02), timeStamp.asn1Encode());
186 }
187 if (usec != null) {
188 temp = new DerOutputStream();
189 temp.putInteger(BigInteger.valueOf(usec.intValue()));
190 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x03), temp);
191 }
192 if (sAddress != null) {
193 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x04), sAddress.asn1Encode());
194 }
195 if (rAddress != null) {
196 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x05), rAddress.asn1Encode());
197 }
198 temp = new DerOutputStream();
199 temp.write(DerValue.tag_Sequence, bytes);
200 bytes = new DerOutputStream();
201 bytes.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte)0x1D), temp);
202 return bytes.toByteArray();
203 }
204}