blob: 3ad64576175750ed08dbb0ca8f6b94bbdd6e4465 [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 * (C) Copyright IBM Corp. 1999 All Rights Reserved.
27 * Copyright 1997 The Open Group Research Institute. All rights reserved.
28 */
29
30package sun.security.krb5.internal;
31
32import sun.security.util.*;
33import java.io.IOException;
34import sun.security.krb5.Asn1Exception;
35import sun.security.krb5.internal.ccache.CCacheOutputStream;
36
37public class AuthorizationDataEntry implements Cloneable {
38 public int adType;
39 public byte[] adData;
40
41 private AuthorizationDataEntry() {
42 }
43
44 public AuthorizationDataEntry(
45 int new_adType,
46 byte[] new_adData
47 ) {
48 adType = new_adType;
49 adData = new_adData;
50 }
51
52 public Object clone() {
53 AuthorizationDataEntry new_authorizationDataEntry =
54 new AuthorizationDataEntry();
55 new_authorizationDataEntry.adType = adType;
56 if (adData != null) {
57 new_authorizationDataEntry.adData = new byte[adData.length];
58 System.arraycopy(adData, 0,
59 new_authorizationDataEntry.adData, 0, adData.length);
60 }
61 return new_authorizationDataEntry;
62 }
63
64 /**
65 * Constructs an instance of AuthorizationDataEntry.
66 * @param encoding a single DER-encoded value.
67 */
68 public AuthorizationDataEntry(DerValue encoding) throws Asn1Exception, IOException {
69 DerValue der;
70 if (encoding.getTag() != DerValue.tag_Sequence) {
71 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
72 }
73 der = encoding.getData().getDerValue();
74 if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
75 adType = der.getData().getBigInteger().intValue();
76 }
77 else
78 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
79 der = encoding.getData().getDerValue();
80 if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
81 adData = der.getData().getOctetString();
82 }
83 else
84 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
85 if (encoding.getData().available() > 0)
86 throw new Asn1Exception(Krb5.ASN1_BAD_ID);
87 }
88
89 /**
90 * Encodes an AuthorizationDataEntry object.
91 * @return byte array of encoded AuthorizationDataEntry object.
92 * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
93 * @exception IOException if an I/O error occurs while reading encoded data.
94 */
95 public byte[] asn1Encode() throws Asn1Exception, IOException {
96 DerOutputStream bytes = new DerOutputStream();
97 DerOutputStream temp = new DerOutputStream();
98 temp.putInteger(adType);
99 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x00), temp);
100 temp = new DerOutputStream();
101 temp.putOctetString(adData);
102 bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte)0x01), temp);
103 temp = new DerOutputStream();
104 temp.write(DerValue.tag_Sequence, bytes);
105 return temp.toByteArray();
106 }
107
108 /**
109 * Writes the entry's data fields in FCC format to an output stream.
110 *
111 * @param cos a <code>CCacheOutputStream</code>.
112 * @exception IOException if an I/O exception occurs.
113 */
114 public void writeEntry(CCacheOutputStream cos) throws IOException {
115 cos.write16(adType);
116 cos.write32(adData.length);
117 cos.write(adData, 0, adData.length);
118 }
119
120 public String toString() {
121 return ("adType=" + adType + " adData.length=" + adData.length);
122 }
123
124}