author | weijun |
Tue, 11 Aug 2009 12:20:32 +0800 | |
changeset 3483 | a16fce1820ef |
parent 73 | cf334423502b |
child 5506 | 202f599c92aa |
permissions | -rw-r--r-- |
2 | 1 |
/* |
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 |
||
31 |
package sun.security.krb5.internal; |
|
32 |
||
33 |
import sun.security.util.*; |
|
34 |
import sun.security.krb5.Asn1Exception; |
|
35 |
import java.util.Vector; |
|
36 |
import java.io.IOException; |
|
37 |
import sun.security.krb5.internal.ccache.CCacheOutputStream; |
|
38 |
||
39 |
/** |
|
40 |
* In RFC4120, the ASN.1 AuthorizationData is defined as: |
|
41 |
* |
|
42 |
* AuthorizationData ::= SEQUENCE OF SEQUENCE { |
|
43 |
* ad-type [0] Int32, |
|
44 |
* ad-data [1] OCTET STRING |
|
45 |
* } |
|
46 |
* |
|
47 |
* Here, two classes are used to implement it and they can be represented as follows: |
|
48 |
* |
|
49 |
* AuthorizationData ::= SEQUENCE OF AuthorizationDataEntry |
|
50 |
* AuthorizationDataEntry ::= SEQUENCE { |
|
51 |
* ad-type[0] Int32, |
|
52 |
* ad-data[1] OCTET STRING |
|
53 |
* } |
|
54 |
*/ |
|
55 |
public class AuthorizationData implements Cloneable { |
|
73 | 56 |
|
57 |
private AuthorizationDataEntry[] entry = null; |
|
2 | 58 |
|
73 | 59 |
private AuthorizationData() { |
60 |
} |
|
2 | 61 |
|
73 | 62 |
public AuthorizationData(AuthorizationDataEntry[] new_entries) |
63 |
throws IOException { |
|
64 |
if (new_entries != null) { |
|
65 |
entry = new AuthorizationDataEntry[new_entries.length]; |
|
66 |
for (int i = 0; i < new_entries.length; i++) { |
|
67 |
if (new_entries[i] == null) { |
|
68 |
throw new IOException("Cannot create an AuthorizationData"); |
|
69 |
} else { |
|
70 |
entry[i] = (AuthorizationDataEntry) new_entries[i].clone(); |
|
2 | 71 |
} |
73 | 72 |
} |
2 | 73 |
} |
73 | 74 |
} |
75 |
||
76 |
public AuthorizationData(AuthorizationDataEntry new_entry) { |
|
77 |
entry = new AuthorizationDataEntry[1]; |
|
78 |
entry[0] = new_entry; |
|
79 |
} |
|
80 |
||
81 |
public Object clone() { |
|
82 |
AuthorizationData new_authorizationData = |
|
83 |
new AuthorizationData(); |
|
84 |
if (entry != null) { |
|
85 |
new_authorizationData.entry = |
|
86 |
new AuthorizationDataEntry[entry.length]; |
|
87 |
for (int i = 0; i < entry.length; i++) { |
|
88 |
new_authorizationData.entry[i] = |
|
89 |
(AuthorizationDataEntry) entry[i].clone(); |
|
90 |
} |
|
91 |
} |
|
92 |
return new_authorizationData; |
|
93 |
} |
|
2 | 94 |
|
73 | 95 |
/** |
96 |
* Constructs a new <code>AuthorizationData,</code> instance. |
|
97 |
* @param der a single DER-encoded value. |
|
98 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
99 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
100 |
*/ |
|
101 |
public AuthorizationData(DerValue der) throws Asn1Exception, IOException { |
|
102 |
Vector<AuthorizationDataEntry> v = |
|
103 |
new Vector<AuthorizationDataEntry>(); |
|
104 |
if (der.getTag() != DerValue.tag_Sequence) { |
|
105 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
106 |
} |
|
107 |
while (der.getData().available() > 0) { |
|
108 |
v.addElement(new AuthorizationDataEntry(der.getData().getDerValue())); |
|
2 | 109 |
} |
73 | 110 |
if (v.size() > 0) { |
111 |
entry = new AuthorizationDataEntry[v.size()]; |
|
112 |
v.copyInto(entry); |
|
113 |
} |
|
114 |
} |
|
2 | 115 |
|
73 | 116 |
/** |
117 |
* Encodes an <code>AuthorizationData</code> object. |
|
118 |
* @return byte array of encoded <code>AuthorizationData</code> object. |
|
119 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
120 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
121 |
*/ |
|
122 |
public byte[] asn1Encode() throws Asn1Exception, IOException { |
|
123 |
DerOutputStream bytes = new DerOutputStream(); |
|
124 |
DerValue der[] = new DerValue[entry.length]; |
|
125 |
for (int i = 0; i < entry.length; i++) { |
|
126 |
der[i] = new DerValue(entry[i].asn1Encode()); |
|
2 | 127 |
} |
73 | 128 |
bytes.putSequence(der); |
129 |
return bytes.toByteArray(); |
|
130 |
} |
|
2 | 131 |
|
132 |
/** |
|
133 |
* Parse (unmarshal) an <code>AuthorizationData</code> object from a DER input stream. |
|
134 |
* This form of parsing might be used when expanding a value which is part of |
|
135 |
* a constructed sequence and uses explicitly tagged type. |
|
136 |
* |
|
137 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data. |
|
138 |
* @exception IOException if an I/O error occurs while reading encoded data. |
|
139 |
* @param data the Der input stream value, which contains one or more marshaled value. |
|
140 |
* @param explicitTag tag number. |
|
141 |
* @param optional indicates if this data field is optional |
|
142 |
* @return an instance of AuthorizationData. |
|
143 |
* |
|
144 |
*/ |
|
73 | 145 |
public static AuthorizationData parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException { |
146 |
if ((optional) && (((byte) data.peekByte() & (byte) 0x1F) != explicitTag)) { |
|
147 |
return null; |
|
148 |
} |
|
149 |
DerValue der = data.getDerValue(); |
|
150 |
if (explicitTag != (der.getTag() & (byte) 0x1F)) { |
|
151 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID); |
|
152 |
} else { |
|
153 |
DerValue subDer = der.getData().getDerValue(); |
|
154 |
return new AuthorizationData(subDer); |
|
2 | 155 |
} |
73 | 156 |
} |
2 | 157 |
|
73 | 158 |
/** |
159 |
* Writes <code>AuthorizationData</code> data fields to a output stream. |
|
160 |
* |
|
161 |
* @param cos a <code>CCacheOutputStream</code> to be written to. |
|
162 |
* @exception IOException if an I/O exception occurs. |
|
163 |
*/ |
|
164 |
public void writeAuth(CCacheOutputStream cos) throws IOException { |
|
165 |
for (int i = 0; i < entry.length; i++) { |
|
166 |
entry[i].writeEntry(cos); |
|
2 | 167 |
} |
73 | 168 |
} |
2 | 169 |
|
170 |
public String toString() { |
|
171 |
String retVal = "AuthorizationData:\n"; |
|
172 |
for (int i = 0; i < entry.length; i++) { |
|
173 |
retVal += entry[i].toString(); |
|
174 |
} |
|
175 |
return retVal; |
|
176 |
} |
|
3483
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
177 |
|
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
178 |
public int count() { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
179 |
return entry.length; |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
180 |
} |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
181 |
|
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
182 |
public AuthorizationDataEntry item(int i) { |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
183 |
return (AuthorizationDataEntry)entry[i].clone(); |
a16fce1820ef
6821190: more InquireType values for ExtendedGSSContext
weijun
parents:
73
diff
changeset
|
184 |
} |
2 | 185 |
} |