2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
5506
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
*
|
|
28 |
* (C) Copyright IBM Corp. 1999 All Rights Reserved.
|
|
29 |
* Copyright 1997 The Open Group Research Institute. All rights reserved.
|
|
30 |
*/
|
|
31 |
|
|
32 |
package sun.security.krb5;
|
|
33 |
|
|
34 |
import sun.security.krb5.internal.*;
|
|
35 |
import sun.security.krb5.internal.crypto.KeyUsage;
|
|
36 |
import sun.security.util.*;
|
|
37 |
import java.io.IOException;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* This class encapsulates a KRB-AP-REP sent from the service to the
|
|
41 |
* client.
|
|
42 |
*/
|
|
43 |
public class KrbApRep {
|
|
44 |
private byte[] obuf;
|
|
45 |
private byte[] ibuf;
|
|
46 |
private EncAPRepPart encPart; // although in plain text
|
|
47 |
private APRep apRepMessg;
|
|
48 |
|
|
49 |
/**
|
|
50 |
* Constructs a KRB-AP-REP to send to a client.
|
|
51 |
* @throws KrbException
|
|
52 |
* @throws IOException
|
|
53 |
*/
|
|
54 |
// Used in AcceptSecContextToken
|
|
55 |
public KrbApRep(KrbApReq incomingReq,
|
|
56 |
boolean useSeqNumber,
|
|
57 |
boolean useSubKey) throws KrbException, IOException {
|
|
58 |
|
|
59 |
EncryptionKey subKey =
|
|
60 |
(useSubKey?
|
|
61 |
new EncryptionKey(incomingReq.getCreds().getSessionKey()):null);
|
|
62 |
SeqNumber seqNum = new LocalSeqNumber();
|
|
63 |
|
|
64 |
init(incomingReq, subKey, seqNum);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Constructs a KRB-AP-REQ from the bytes received from a service.
|
|
69 |
* @throws KrbException
|
|
70 |
* @throws IOException
|
|
71 |
*/
|
|
72 |
// Used in AcceptSecContextToken
|
|
73 |
public KrbApRep(byte[] message, Credentials tgtCreds,
|
|
74 |
KrbApReq outgoingReq) throws KrbException, IOException {
|
|
75 |
this(message, tgtCreds);
|
|
76 |
authenticate(outgoingReq);
|
|
77 |
}
|
|
78 |
|
|
79 |
private void init(KrbApReq apReq,
|
|
80 |
EncryptionKey subKey,
|
|
81 |
SeqNumber seqNumber)
|
|
82 |
throws KrbException, IOException {
|
|
83 |
createMessage(
|
|
84 |
apReq.getCreds().key,
|
|
85 |
apReq.getCtime(),
|
|
86 |
apReq.cusec(),
|
|
87 |
subKey,
|
|
88 |
seqNumber);
|
|
89 |
obuf = apRepMessg.asn1Encode();
|
|
90 |
}
|
|
91 |
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Constructs a KrbApRep object.
|
|
95 |
* @param msg a byte array of reply message.
|
|
96 |
* @param tgs_creds client's credential.
|
|
97 |
* @exception KrbException
|
|
98 |
* @exception IOException
|
|
99 |
*/
|
|
100 |
private KrbApRep(byte[] msg, Credentials tgs_creds)
|
|
101 |
throws KrbException, IOException {
|
|
102 |
this(new DerValue(msg), tgs_creds);
|
|
103 |
}
|
|
104 |
|
|
105 |
/**
|
|
106 |
* Constructs a KrbApRep object.
|
|
107 |
* @param msg a byte array of reply message.
|
|
108 |
* @param tgs_creds client's credential.
|
|
109 |
* @exception KrbException
|
|
110 |
* @exception IOException
|
|
111 |
*/
|
|
112 |
private KrbApRep(DerValue encoding, Credentials tgs_creds)
|
|
113 |
throws KrbException, IOException {
|
|
114 |
APRep rep = null;
|
|
115 |
try {
|
|
116 |
rep = new APRep(encoding);
|
|
117 |
} catch (Asn1Exception e) {
|
|
118 |
rep = null;
|
|
119 |
KRBError err = new KRBError(encoding);
|
|
120 |
String errStr = err.getErrorString();
|
|
121 |
String eText;
|
|
122 |
if (errStr.charAt(errStr.length() - 1) == 0)
|
|
123 |
eText = errStr.substring(0, errStr.length() - 1);
|
|
124 |
else
|
|
125 |
eText = errStr;
|
|
126 |
KrbException ke = new KrbException(err.getErrorCode(), eText);
|
|
127 |
ke.initCause(e);
|
|
128 |
throw ke;
|
|
129 |
}
|
|
130 |
|
|
131 |
byte[] temp = rep.encPart.decrypt(tgs_creds.key,
|
|
132 |
KeyUsage.KU_ENC_AP_REP_PART);
|
|
133 |
byte[] enc_ap_rep_part = rep.encPart.reset(temp, true);
|
|
134 |
|
|
135 |
encoding = new DerValue(enc_ap_rep_part);
|
|
136 |
encPart = new EncAPRepPart(encoding);
|
|
137 |
}
|
|
138 |
|
|
139 |
private void authenticate(KrbApReq apReq)
|
|
140 |
throws KrbException, IOException {
|
|
141 |
if (encPart.ctime.getSeconds() != apReq.getCtime().getSeconds() ||
|
|
142 |
encPart.cusec != apReq.getCtime().getMicroSeconds())
|
|
143 |
throw new KrbApErrException(Krb5.KRB_AP_ERR_MUT_FAIL);
|
|
144 |
}
|
|
145 |
|
|
146 |
|
|
147 |
/**
|
|
148 |
* Returns the optional subkey stored in
|
|
149 |
* this message. Returns null if none is stored.
|
|
150 |
*/
|
|
151 |
public EncryptionKey getSubKey() {
|
|
152 |
// XXX Can encPart be null
|
|
153 |
return encPart.getSubKey();
|
|
154 |
|
|
155 |
}
|
|
156 |
|
|
157 |
/**
|
|
158 |
* Returns the optional sequence number stored in the
|
|
159 |
* this message. Returns null if none is stored.
|
|
160 |
*/
|
|
161 |
public Integer getSeqNumber() {
|
|
162 |
// XXX Can encPart be null
|
|
163 |
return encPart.getSeqNumber();
|
|
164 |
}
|
|
165 |
|
|
166 |
/**
|
|
167 |
* Returns the ASN.1 encoding that should be sent to the peer.
|
|
168 |
*/
|
|
169 |
public byte[] getMessage() {
|
|
170 |
return obuf;
|
|
171 |
}
|
|
172 |
|
|
173 |
private void createMessage(
|
|
174 |
EncryptionKey key,
|
|
175 |
KerberosTime ctime,
|
|
176 |
int cusec,
|
|
177 |
EncryptionKey subKey,
|
|
178 |
SeqNumber seqNumber)
|
|
179 |
throws Asn1Exception, IOException,
|
|
180 |
KdcErrException, KrbCryptoException {
|
|
181 |
|
|
182 |
Integer seqno = null;
|
|
183 |
|
|
184 |
if (seqNumber != null)
|
|
185 |
seqno = new Integer(seqNumber.current());
|
|
186 |
|
|
187 |
encPart = new EncAPRepPart(ctime,
|
|
188 |
cusec,
|
|
189 |
subKey,
|
|
190 |
seqno);
|
|
191 |
|
|
192 |
byte[] encPartEncoding = encPart.asn1Encode();
|
|
193 |
|
|
194 |
EncryptedData encEncPart = new EncryptedData(key, encPartEncoding,
|
|
195 |
KeyUsage.KU_ENC_AP_REP_PART);
|
|
196 |
|
|
197 |
apRepMessg = new APRep(encEncPart);
|
|
198 |
}
|
|
199 |
|
|
200 |
}
|