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.krb5.*;
|
|
34 |
import sun.security.util.*;
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.math.BigInteger;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Implements the ASN.1 AP-REQ type.
|
|
40 |
*
|
|
41 |
* <xmp>
|
|
42 |
* AP-REQ ::= [APPLICATION 14] SEQUENCE {
|
|
43 |
* pvno [0] INTEGER (5),
|
|
44 |
* msg-type [1] INTEGER (14),
|
|
45 |
* ap-options [2] APOptions,
|
|
46 |
* ticket [3] Ticket,
|
|
47 |
* authenticator [4] EncryptedData -- Authenticator
|
|
48 |
* }
|
|
49 |
* </xmp>
|
|
50 |
*
|
|
51 |
* <p>
|
|
52 |
* This definition reflects the Network Working Group RFC 4120
|
|
53 |
* specification available at
|
|
54 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
|
|
55 |
* http://www.ietf.org/rfc/rfc4120.txt</a>.
|
|
56 |
*/
|
73
|
57 |
public class APReq {
|
2
|
58 |
|
73
|
59 |
public int pvno;
|
|
60 |
public int msgType;
|
|
61 |
public APOptions apOptions;
|
|
62 |
public Ticket ticket;
|
|
63 |
public EncryptedData authenticator;
|
2
|
64 |
|
73
|
65 |
public APReq(
|
|
66 |
APOptions new_apOptions,
|
|
67 |
Ticket new_ticket,
|
|
68 |
EncryptedData new_authenticator) {
|
|
69 |
pvno = Krb5.PVNO;
|
|
70 |
msgType = Krb5.KRB_AP_REQ;
|
|
71 |
apOptions = new_apOptions;
|
|
72 |
ticket = new_ticket;
|
|
73 |
authenticator = new_authenticator;
|
|
74 |
}
|
2
|
75 |
|
73
|
76 |
public APReq(byte[] data) throws Asn1Exception, IOException, KrbApErrException, RealmException {
|
2
|
77 |
init(new DerValue(data));
|
73
|
78 |
}
|
2
|
79 |
|
|
80 |
public APReq(DerValue encoding) throws Asn1Exception, IOException, KrbApErrException, RealmException {
|
73
|
81 |
init(encoding);
|
|
82 |
}
|
2
|
83 |
|
73
|
84 |
/**
|
|
85 |
* Initializes an APReq object.
|
|
86 |
* @param encoding a single DER-encoded value.
|
|
87 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
88 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
89 |
* @exception KrbApErrException if the value read from the DER-encoded data stream does not match the pre-defined value.
|
|
90 |
* @exception RealmException if an error occurs while parsing a Realm object.
|
|
91 |
*/
|
|
92 |
private void init(DerValue encoding) throws Asn1Exception,
|
|
93 |
IOException, KrbApErrException, RealmException {
|
|
94 |
DerValue der, subDer;
|
|
95 |
if (((encoding.getTag() & (byte) 0x1F) != Krb5.KRB_AP_REQ)
|
|
96 |
|| (encoding.isApplication() != true)
|
|
97 |
|| (encoding.isConstructed() != true)) {
|
2
|
98 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
73
|
99 |
}
|
|
100 |
der = encoding.getData().getDerValue();
|
|
101 |
if (der.getTag() != DerValue.tag_Sequence) {
|
|
102 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
103 |
}
|
|
104 |
subDer = der.getData().getDerValue();
|
|
105 |
if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x00) {
|
|
106 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
107 |
}
|
2
|
108 |
pvno = subDer.getData().getBigInteger().intValue();
|
73
|
109 |
if (pvno != Krb5.PVNO) {
|
|
110 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_BADVERSION);
|
|
111 |
}
|
|
112 |
subDer = der.getData().getDerValue();
|
|
113 |
if ((subDer.getTag() & (byte) 0x1F) != (byte) 0x01) {
|
|
114 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
2
|
115 |
}
|
73
|
116 |
msgType = subDer.getData().getBigInteger().intValue();
|
|
117 |
if (msgType != Krb5.KRB_AP_REQ) {
|
|
118 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_MSG_TYPE);
|
|
119 |
}
|
|
120 |
apOptions = APOptions.parse(der.getData(), (byte) 0x02, false);
|
|
121 |
ticket = Ticket.parse(der.getData(), (byte) 0x03, false);
|
|
122 |
authenticator = EncryptedData.parse(der.getData(), (byte) 0x04, false);
|
|
123 |
if (der.getData().available() > 0) {
|
|
124 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
125 |
}
|
|
126 |
}
|
2
|
127 |
|
73
|
128 |
/**
|
|
129 |
* Encodes an APReq object.
|
|
130 |
* @return byte array of encoded APReq object.
|
|
131 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
132 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
133 |
*/
|
|
134 |
public byte[] asn1Encode() throws Asn1Exception, IOException {
|
2
|
135 |
DerOutputStream bytes = new DerOutputStream();
|
73
|
136 |
DerOutputStream temp = new DerOutputStream();
|
|
137 |
temp.putInteger(BigInteger.valueOf(pvno));
|
|
138 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x00), temp);
|
|
139 |
temp = new DerOutputStream();
|
|
140 |
temp.putInteger(BigInteger.valueOf(msgType));
|
|
141 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x01), temp);
|
|
142 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x02), apOptions.asn1Encode());
|
|
143 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x03), ticket.asn1Encode());
|
|
144 |
bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT, true, (byte) 0x04), authenticator.asn1Encode());
|
|
145 |
temp = new DerOutputStream();
|
|
146 |
temp.write(DerValue.tag_Sequence, bytes);
|
|
147 |
DerOutputStream apreq = new DerOutputStream();
|
|
148 |
apreq.write(DerValue.createTag(DerValue.TAG_APPLICATION, true, (byte) 0x0E), temp);
|
|
149 |
return apreq.toByteArray();
|
|
150 |
}
|
2
|
151 |
}
|