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
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
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 java.util.TimeZone;
|
|
34 |
import sun.security.util.*;
|
|
35 |
import sun.security.krb5.Config;
|
|
36 |
import sun.security.krb5.KrbException;
|
|
37 |
import sun.security.krb5.Asn1Exception;
|
|
38 |
import java.util.Date;
|
|
39 |
import java.util.GregorianCalendar;
|
|
40 |
import java.util.Calendar;
|
|
41 |
import java.io.IOException;
|
|
42 |
|
|
43 |
/**
|
|
44 |
* Implements the ASN.1 KerberosTime type.
|
|
45 |
*
|
|
46 |
* <xmp>
|
|
47 |
* KerberosTime ::= GeneralizedTime -- with no fractional seconds
|
|
48 |
* </xmp>
|
|
49 |
*
|
|
50 |
* The timestamps used in Kerberos are encoded as GeneralizedTimes. A
|
|
51 |
* KerberosTime value shall not include any fractional portions of the
|
|
52 |
* seconds. As required by the DER, it further shall not include any
|
|
53 |
* separators, and it shall specify the UTC time zone (Z).
|
|
54 |
*
|
|
55 |
* <p>
|
|
56 |
* This definition reflects the Network Working Group RFC 4120
|
|
57 |
* specification available at
|
|
58 |
* <a href="http://www.ietf.org/rfc/rfc4120.txt">
|
|
59 |
* http://www.ietf.org/rfc/rfc4120.txt</a>.
|
5615
|
60 |
*
|
|
61 |
* The implementation also includes the microseconds info so that the
|
|
62 |
* same class can be used as a precise timestamp in Authenticator etc.
|
2
|
63 |
*/
|
|
64 |
|
|
65 |
public class KerberosTime implements Cloneable {
|
|
66 |
|
|
67 |
private long kerberosTime; // milliseconds since epoch, a Date.getTime() value
|
5615
|
68 |
private int microSeconds; // the last three digits of the microsecond value
|
|
69 |
|
|
70 |
// The time when this class is loaded. Used in setNow()
|
|
71 |
private static final long initMilli = System.currentTimeMillis();
|
|
72 |
private static final long initMicro = System.nanoTime() / 1000;
|
|
73 |
|
2
|
74 |
private static long syncTime;
|
|
75 |
private static boolean DEBUG = Krb5.DEBUG;
|
|
76 |
|
|
77 |
public static final boolean NOW = true;
|
|
78 |
public static final boolean UNADJUSTED_NOW = false;
|
|
79 |
|
|
80 |
public KerberosTime(long time) {
|
|
81 |
kerberosTime = time;
|
|
82 |
}
|
|
83 |
|
5615
|
84 |
private KerberosTime(long time, int micro) {
|
|
85 |
kerberosTime = time;
|
|
86 |
microSeconds = micro;
|
|
87 |
}
|
2
|
88 |
|
|
89 |
public Object clone() {
|
5615
|
90 |
return new KerberosTime(kerberosTime, microSeconds);
|
2
|
91 |
}
|
|
92 |
|
|
93 |
// This constructor is used in the native code
|
|
94 |
// src/windows/native/sun/security/krb5/NativeCreds.c
|
|
95 |
public KerberosTime(String time) throws Asn1Exception {
|
|
96 |
kerberosTime = toKerberosTime(time);
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Constructs a KerberosTime object.
|
|
101 |
* @param encoding a DER-encoded data.
|
|
102 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
103 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
104 |
*/
|
|
105 |
public KerberosTime(DerValue encoding) throws Asn1Exception, IOException {
|
|
106 |
GregorianCalendar calendar = new GregorianCalendar();
|
|
107 |
Date temp = encoding.getGeneralizedTime();
|
|
108 |
kerberosTime = temp.getTime();
|
|
109 |
}
|
|
110 |
|
|
111 |
private static long toKerberosTime(String time) throws Asn1Exception {
|
|
112 |
// this method only used by KerberosTime class.
|
|
113 |
|
|
114 |
// ASN.1 GeneralizedTime format:
|
|
115 |
|
|
116 |
// "19700101000000Z"
|
|
117 |
// | | | | | | |
|
|
118 |
// 0 4 6 8 | | |
|
|
119 |
// 10 | |
|
5615
|
120 |
// 12 |
|
|
121 |
// 14
|
2
|
122 |
|
|
123 |
if (time.length() != 15)
|
|
124 |
throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
|
|
125 |
if (time.charAt(14) != 'Z')
|
|
126 |
throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
|
|
127 |
int year = Integer.parseInt(time.substring(0, 4));
|
|
128 |
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
|
129 |
calendar.clear(); // so that millisecond is zero
|
|
130 |
calendar.set(year,
|
|
131 |
Integer.parseInt(time.substring(4, 6)) - 1,
|
|
132 |
Integer.parseInt(time.substring(6, 8)),
|
|
133 |
Integer.parseInt(time.substring(8, 10)),
|
|
134 |
Integer.parseInt(time.substring(10, 12)),
|
|
135 |
Integer.parseInt(time.substring(12, 14)));
|
|
136 |
|
|
137 |
//The Date constructor assumes the setting are local relative
|
|
138 |
//and converts the time to UTC before storing it. Since we
|
|
139 |
//want the internal representation to correspond to local
|
|
140 |
//and not UTC time we subtract the UTC time offset.
|
|
141 |
return (calendar.getTime().getTime());
|
|
142 |
|
|
143 |
}
|
|
144 |
|
|
145 |
// should be moved to sun.security.krb5.util class
|
|
146 |
public static String zeroPad(String s, int length) {
|
|
147 |
StringBuffer temp = new StringBuffer(s);
|
|
148 |
while (temp.length() < length)
|
|
149 |
temp.insert(0, '0');
|
|
150 |
return temp.toString();
|
|
151 |
}
|
|
152 |
|
|
153 |
public KerberosTime(Date time) {
|
|
154 |
kerberosTime = time.getTime(); // (time.getTimezoneOffset() * 60000L);
|
|
155 |
}
|
|
156 |
|
|
157 |
public KerberosTime(boolean initToNow) {
|
|
158 |
if (initToNow) {
|
5615
|
159 |
setNow();
|
2
|
160 |
}
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns a string representation of KerberosTime object.
|
|
165 |
* @return a string representation of this object.
|
|
166 |
*/
|
|
167 |
public String toGeneralizedTimeString() {
|
|
168 |
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
|
|
169 |
calendar.clear();
|
|
170 |
|
|
171 |
calendar.setTimeInMillis(kerberosTime);
|
|
172 |
return zeroPad(Integer.toString(calendar.get(Calendar.YEAR)), 4) +
|
|
173 |
zeroPad(Integer.toString(calendar.get(Calendar.MONTH) + 1), 2) +
|
|
174 |
zeroPad(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)), 2) +
|
|
175 |
zeroPad(Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)), 2) +
|
|
176 |
zeroPad(Integer.toString(calendar.get(Calendar.MINUTE)), 2) +
|
|
177 |
zeroPad(Integer.toString(calendar.get(Calendar.SECOND)), 2) + 'Z';
|
|
178 |
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Encodes this object to a byte array.
|
|
183 |
* @return a byte array of encoded data.
|
|
184 |
* @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
|
|
185 |
* @exception IOException if an I/O error occurs while reading encoded data.
|
|
186 |
*/
|
|
187 |
public byte[] asn1Encode() throws Asn1Exception, IOException {
|
|
188 |
DerOutputStream out = new DerOutputStream();
|
|
189 |
out.putGeneralizedTime(this.toDate());
|
|
190 |
return out.toByteArray();
|
|
191 |
}
|
|
192 |
|
|
193 |
public long getTime() {
|
|
194 |
return kerberosTime;
|
|
195 |
}
|
|
196 |
|
|
197 |
|
|
198 |
public void setTime(Date time) {
|
|
199 |
kerberosTime = time.getTime(); // (time.getTimezoneOffset() * 60000L);
|
5615
|
200 |
microSeconds = 0;
|
2
|
201 |
}
|
|
202 |
|
|
203 |
public void setTime(long time) {
|
|
204 |
kerberosTime = time;
|
5615
|
205 |
microSeconds = 0;
|
2
|
206 |
}
|
|
207 |
|
|
208 |
public Date toDate() {
|
|
209 |
Date temp = new Date(kerberosTime);
|
|
210 |
temp.setTime(temp.getTime());
|
|
211 |
return temp;
|
|
212 |
}
|
|
213 |
|
|
214 |
public void setNow() {
|
5615
|
215 |
long microElapsed = System.nanoTime() / 1000 - initMicro;
|
|
216 |
setTime(initMilli + microElapsed/1000);
|
|
217 |
microSeconds = (int)(microElapsed % 1000);
|
2
|
218 |
}
|
|
219 |
|
|
220 |
public int getMicroSeconds() {
|
|
221 |
Long temp_long = new Long((kerberosTime % 1000L) * 1000L);
|
5615
|
222 |
return temp_long.intValue() + microSeconds;
|
2
|
223 |
}
|
|
224 |
|
|
225 |
public void setMicroSeconds(int usec) {
|
5615
|
226 |
microSeconds = usec % 1000;
|
2
|
227 |
Integer temp_int = new Integer(usec);
|
|
228 |
long temp_long = temp_int.longValue() / 1000L;
|
|
229 |
kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
|
|
230 |
}
|
|
231 |
|
|
232 |
public void setMicroSeconds(Integer usec) {
|
|
233 |
if (usec != null) {
|
5615
|
234 |
microSeconds = usec.intValue() % 1000;
|
2
|
235 |
long temp_long = usec.longValue() / 1000L;
|
|
236 |
kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
|
|
237 |
}
|
|
238 |
}
|
|
239 |
|
|
240 |
public boolean inClockSkew(int clockSkew) {
|
|
241 |
KerberosTime now = new KerberosTime(KerberosTime.NOW);
|
|
242 |
|
|
243 |
if (java.lang.Math.abs(kerberosTime - now.kerberosTime) >
|
|
244 |
clockSkew * 1000L)
|
|
245 |
return false;
|
|
246 |
return true;
|
|
247 |
}
|
|
248 |
|
|
249 |
public boolean inClockSkew() {
|
|
250 |
return inClockSkew(getDefaultSkew());
|
|
251 |
}
|
|
252 |
|
|
253 |
public boolean inClockSkew(int clockSkew, KerberosTime now) {
|
|
254 |
if (java.lang.Math.abs(kerberosTime - now.kerberosTime) >
|
|
255 |
clockSkew * 1000L)
|
|
256 |
return false;
|
|
257 |
return true;
|
|
258 |
}
|
|
259 |
|
|
260 |
public boolean inClockSkew(KerberosTime time) {
|
|
261 |
return inClockSkew(getDefaultSkew(), time);
|
|
262 |
}
|
|
263 |
|
|
264 |
public boolean greaterThanWRTClockSkew(KerberosTime time, int clockSkew) {
|
|
265 |
if ((kerberosTime - time.kerberosTime) > clockSkew * 1000L)
|
|
266 |
return true;
|
|
267 |
return false;
|
|
268 |
}
|
|
269 |
|
|
270 |
public boolean greaterThanWRTClockSkew(KerberosTime time) {
|
|
271 |
return greaterThanWRTClockSkew(time, getDefaultSkew());
|
|
272 |
}
|
|
273 |
|
|
274 |
public boolean greaterThan(KerberosTime time) {
|
5615
|
275 |
return kerberosTime > time.kerberosTime ||
|
|
276 |
kerberosTime == time.kerberosTime &&
|
|
277 |
microSeconds > time.microSeconds;
|
2
|
278 |
}
|
|
279 |
|
|
280 |
public boolean equals(Object obj) {
|
|
281 |
if (this == obj) {
|
|
282 |
return true;
|
|
283 |
}
|
|
284 |
|
|
285 |
if (!(obj instanceof KerberosTime)) {
|
|
286 |
return false;
|
|
287 |
}
|
|
288 |
|
5615
|
289 |
return kerberosTime == ((KerberosTime)obj).kerberosTime &&
|
|
290 |
microSeconds == ((KerberosTime)obj).microSeconds;
|
2
|
291 |
}
|
|
292 |
|
|
293 |
public int hashCode() {
|
5615
|
294 |
int result = 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
|
|
295 |
return result * 17 + microSeconds;
|
2
|
296 |
}
|
|
297 |
|
|
298 |
public boolean isZero() {
|
5615
|
299 |
return kerberosTime == 0 && microSeconds == 0;
|
2
|
300 |
}
|
|
301 |
|
|
302 |
public int getSeconds() {
|
|
303 |
Long temp_long = new Long(kerberosTime / 1000L);
|
|
304 |
return temp_long.intValue();
|
|
305 |
}
|
|
306 |
|
|
307 |
public void setSeconds(int sec) {
|
|
308 |
Integer temp_int = new Integer(sec);
|
|
309 |
kerberosTime = temp_int.longValue() * 1000L;
|
|
310 |
}
|
|
311 |
|
|
312 |
/**
|
|
313 |
* Parse (unmarshal) a kerberostime from a DER input stream. This form
|
|
314 |
* parsing might be used when expanding a value which is part of
|
|
315 |
* a constructed sequence and uses explicitly tagged type.
|
|
316 |
*
|
|
317 |
* @exception Asn1Exception on error.
|
|
318 |
* @param data the Der input stream value, which contains one or more marshaled value.
|
|
319 |
* @param explicitTag tag number.
|
|
320 |
* @param optional indicates if this data field is optional
|
|
321 |
* @return an instance of KerberosTime.
|
|
322 |
*
|
|
323 |
*/
|
|
324 |
public static KerberosTime parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {
|
|
325 |
if ((optional) && (((byte)data.peekByte() & (byte)0x1F)!= explicitTag))
|
|
326 |
return null;
|
|
327 |
DerValue der = data.getDerValue();
|
|
328 |
if (explicitTag != (der.getTag() & (byte)0x1F)) {
|
|
329 |
throw new Asn1Exception(Krb5.ASN1_BAD_ID);
|
|
330 |
}
|
|
331 |
else {
|
|
332 |
DerValue subDer = der.getData().getDerValue();
|
|
333 |
return new KerberosTime(subDer);
|
|
334 |
}
|
|
335 |
}
|
|
336 |
|
|
337 |
public static int getDefaultSkew() {
|
|
338 |
int tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
|
|
339 |
try {
|
|
340 |
Config c = Config.getInstance();
|
|
341 |
if ((tdiff = c.getDefaultIntValue("clockskew",
|
|
342 |
"libdefaults")) == Integer.MIN_VALUE) { //value is not defined
|
|
343 |
tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
|
|
344 |
}
|
|
345 |
} catch (KrbException e) {
|
|
346 |
if (DEBUG) {
|
|
347 |
System.out.println("Exception in getting clockskew from " +
|
|
348 |
"Configuration " +
|
|
349 |
"using default value " +
|
|
350 |
e.getMessage());
|
|
351 |
}
|
|
352 |
}
|
|
353 |
return tdiff;
|
|
354 |
}
|
|
355 |
|
|
356 |
public String toString() {
|
|
357 |
return toGeneralizedTimeString();
|
|
358 |
}
|
|
359 |
}
|