0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A * (C) Copyright IBM Corp. 1999 All Rights Reserved.
0N/A * Copyright 1997 The Open Group Research Institute. All rights reserved.
0N/A */
0N/A
0N/Apackage sun.security.krb5.internal;
0N/A
0N/Aimport java.util.TimeZone;
0N/Aimport sun.security.util.*;
0N/Aimport sun.security.krb5.Config;
0N/Aimport sun.security.krb5.KrbException;
0N/Aimport sun.security.krb5.Asn1Exception;
0N/Aimport java.util.Date;
0N/Aimport java.util.GregorianCalendar;
0N/Aimport java.util.Calendar;
0N/Aimport java.io.IOException;
0N/A
0N/A/**
0N/A * Implements the ASN.1 KerberosTime type.
0N/A *
0N/A * <xmp>
0N/A * KerberosTime ::= GeneralizedTime -- with no fractional seconds
0N/A * </xmp>
0N/A *
0N/A * The timestamps used in Kerberos are encoded as GeneralizedTimes. A
0N/A * KerberosTime value shall not include any fractional portions of the
0N/A * seconds. As required by the DER, it further shall not include any
0N/A * separators, and it shall specify the UTC time zone (Z).
0N/A *
0N/A * <p>
0N/A * This definition reflects the Network Working Group RFC 4120
0N/A * specification available at
0N/A * <a href="http://www.ietf.org/rfc/rfc4120.txt">
0N/A * http://www.ietf.org/rfc/rfc4120.txt</a>.
2406N/A *
2406N/A * The implementation also includes the microseconds info so that the
2406N/A * same class can be used as a precise timestamp in Authenticator etc.
0N/A */
0N/A
0N/Apublic class KerberosTime implements Cloneable {
0N/A
0N/A private long kerberosTime; // milliseconds since epoch, a Date.getTime() value
2406N/A private int microSeconds; // the last three digits of the microsecond value
2406N/A
2406N/A // The time when this class is loaded. Used in setNow()
4530N/A private static long initMilli = System.currentTimeMillis();
4530N/A private static long initMicro = System.nanoTime() / 1000;
2406N/A
0N/A private static long syncTime;
0N/A private static boolean DEBUG = Krb5.DEBUG;
0N/A
0N/A public static final boolean NOW = true;
0N/A public static final boolean UNADJUSTED_NOW = false;
0N/A
0N/A public KerberosTime(long time) {
0N/A kerberosTime = time;
0N/A }
0N/A
2406N/A private KerberosTime(long time, int micro) {
2406N/A kerberosTime = time;
2406N/A microSeconds = micro;
2406N/A }
0N/A
0N/A public Object clone() {
2406N/A return new KerberosTime(kerberosTime, microSeconds);
0N/A }
0N/A
0N/A // This constructor is used in the native code
0N/A // src/windows/native/sun/security/krb5/NativeCreds.c
0N/A public KerberosTime(String time) throws Asn1Exception {
0N/A kerberosTime = toKerberosTime(time);
0N/A }
0N/A
0N/A /**
0N/A * Constructs a KerberosTime object.
0N/A * @param encoding a DER-encoded data.
0N/A * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
0N/A * @exception IOException if an I/O error occurs while reading encoded data.
0N/A */
0N/A public KerberosTime(DerValue encoding) throws Asn1Exception, IOException {
0N/A GregorianCalendar calendar = new GregorianCalendar();
0N/A Date temp = encoding.getGeneralizedTime();
0N/A kerberosTime = temp.getTime();
0N/A }
0N/A
0N/A private static long toKerberosTime(String time) throws Asn1Exception {
0N/A // this method only used by KerberosTime class.
0N/A
0N/A // ASN.1 GeneralizedTime format:
0N/A
0N/A // "19700101000000Z"
0N/A // | | | | | | |
0N/A // 0 4 6 8 | | |
0N/A // 10 | |
2406N/A // 12 |
2406N/A // 14
0N/A
0N/A if (time.length() != 15)
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
0N/A if (time.charAt(14) != 'Z')
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
0N/A int year = Integer.parseInt(time.substring(0, 4));
0N/A Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
0N/A calendar.clear(); // so that millisecond is zero
0N/A calendar.set(year,
0N/A Integer.parseInt(time.substring(4, 6)) - 1,
0N/A Integer.parseInt(time.substring(6, 8)),
0N/A Integer.parseInt(time.substring(8, 10)),
0N/A Integer.parseInt(time.substring(10, 12)),
0N/A Integer.parseInt(time.substring(12, 14)));
0N/A
0N/A //The Date constructor assumes the setting are local relative
0N/A //and converts the time to UTC before storing it. Since we
0N/A //want the internal representation to correspond to local
0N/A //and not UTC time we subtract the UTC time offset.
0N/A return (calendar.getTime().getTime());
0N/A
0N/A }
0N/A
0N/A // should be moved to sun.security.krb5.util class
0N/A public static String zeroPad(String s, int length) {
0N/A StringBuffer temp = new StringBuffer(s);
0N/A while (temp.length() < length)
0N/A temp.insert(0, '0');
0N/A return temp.toString();
0N/A }
0N/A
0N/A public KerberosTime(Date time) {
0N/A kerberosTime = time.getTime(); // (time.getTimezoneOffset() * 60000L);
0N/A }
0N/A
0N/A public KerberosTime(boolean initToNow) {
0N/A if (initToNow) {
2406N/A setNow();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of KerberosTime object.
0N/A * @return a string representation of this object.
0N/A */
0N/A public String toGeneralizedTimeString() {
0N/A Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
0N/A calendar.clear();
0N/A
0N/A calendar.setTimeInMillis(kerberosTime);
0N/A return zeroPad(Integer.toString(calendar.get(Calendar.YEAR)), 4) +
0N/A zeroPad(Integer.toString(calendar.get(Calendar.MONTH) + 1), 2) +
0N/A zeroPad(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)), 2) +
0N/A zeroPad(Integer.toString(calendar.get(Calendar.HOUR_OF_DAY)), 2) +
0N/A zeroPad(Integer.toString(calendar.get(Calendar.MINUTE)), 2) +
0N/A zeroPad(Integer.toString(calendar.get(Calendar.SECOND)), 2) + 'Z';
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Encodes this object to a byte array.
0N/A * @return a byte array of encoded data.
0N/A * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
0N/A * @exception IOException if an I/O error occurs while reading encoded data.
0N/A */
0N/A public byte[] asn1Encode() throws Asn1Exception, IOException {
0N/A DerOutputStream out = new DerOutputStream();
0N/A out.putGeneralizedTime(this.toDate());
0N/A return out.toByteArray();
0N/A }
0N/A
0N/A public long getTime() {
0N/A return kerberosTime;
0N/A }
0N/A
0N/A
0N/A public void setTime(Date time) {
0N/A kerberosTime = time.getTime(); // (time.getTimezoneOffset() * 60000L);
2406N/A microSeconds = 0;
0N/A }
0N/A
0N/A public void setTime(long time) {
0N/A kerberosTime = time;
2406N/A microSeconds = 0;
0N/A }
0N/A
0N/A public Date toDate() {
0N/A Date temp = new Date(kerberosTime);
0N/A temp.setTime(temp.getTime());
0N/A return temp;
0N/A }
0N/A
0N/A public void setNow() {
4530N/A long newMilli = System.currentTimeMillis();
4530N/A long newMicro = System.nanoTime() / 1000;
4530N/A long microElapsed = newMicro - initMicro;
4530N/A long calcMilli = initMilli + microElapsed/1000;
4530N/A if (calcMilli - newMilli > 100 || newMilli - calcMilli > 100) {
4530N/A if (DEBUG) {
4530N/A System.out.println("System time adjusted");
4530N/A }
4530N/A initMilli = newMilli;
4530N/A initMicro = newMicro;
4530N/A setTime(newMilli);
4530N/A microSeconds = 0;
4530N/A } else {
4530N/A setTime(calcMilli);
4530N/A microSeconds = (int)(microElapsed % 1000);
4530N/A }
0N/A }
0N/A
0N/A public int getMicroSeconds() {
0N/A Long temp_long = new Long((kerberosTime % 1000L) * 1000L);
2406N/A return temp_long.intValue() + microSeconds;
0N/A }
0N/A
0N/A public void setMicroSeconds(int usec) {
2406N/A microSeconds = usec % 1000;
0N/A Integer temp_int = new Integer(usec);
0N/A long temp_long = temp_int.longValue() / 1000L;
0N/A kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
0N/A }
0N/A
0N/A public void setMicroSeconds(Integer usec) {
0N/A if (usec != null) {
2406N/A microSeconds = usec.intValue() % 1000;
0N/A long temp_long = usec.longValue() / 1000L;
0N/A kerberosTime = kerberosTime - (kerberosTime % 1000L) + temp_long;
0N/A }
0N/A }
0N/A
0N/A public boolean inClockSkew(int clockSkew) {
0N/A KerberosTime now = new KerberosTime(KerberosTime.NOW);
0N/A
0N/A if (java.lang.Math.abs(kerberosTime - now.kerberosTime) >
0N/A clockSkew * 1000L)
0N/A return false;
0N/A return true;
0N/A }
0N/A
0N/A public boolean inClockSkew() {
0N/A return inClockSkew(getDefaultSkew());
0N/A }
0N/A
0N/A public boolean inClockSkew(int clockSkew, KerberosTime now) {
0N/A if (java.lang.Math.abs(kerberosTime - now.kerberosTime) >
0N/A clockSkew * 1000L)
0N/A return false;
0N/A return true;
0N/A }
0N/A
0N/A public boolean inClockSkew(KerberosTime time) {
0N/A return inClockSkew(getDefaultSkew(), time);
0N/A }
0N/A
0N/A public boolean greaterThanWRTClockSkew(KerberosTime time, int clockSkew) {
0N/A if ((kerberosTime - time.kerberosTime) > clockSkew * 1000L)
0N/A return true;
0N/A return false;
0N/A }
0N/A
0N/A public boolean greaterThanWRTClockSkew(KerberosTime time) {
0N/A return greaterThanWRTClockSkew(time, getDefaultSkew());
0N/A }
0N/A
0N/A public boolean greaterThan(KerberosTime time) {
2406N/A return kerberosTime > time.kerberosTime ||
2406N/A kerberosTime == time.kerberosTime &&
2406N/A microSeconds > time.microSeconds;
0N/A }
0N/A
0N/A public boolean equals(Object obj) {
0N/A if (this == obj) {
0N/A return true;
0N/A }
0N/A
0N/A if (!(obj instanceof KerberosTime)) {
0N/A return false;
0N/A }
0N/A
2406N/A return kerberosTime == ((KerberosTime)obj).kerberosTime &&
2406N/A microSeconds == ((KerberosTime)obj).microSeconds;
0N/A }
0N/A
0N/A public int hashCode() {
2406N/A int result = 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
2406N/A return result * 17 + microSeconds;
0N/A }
0N/A
0N/A public boolean isZero() {
2406N/A return kerberosTime == 0 && microSeconds == 0;
0N/A }
0N/A
0N/A public int getSeconds() {
0N/A Long temp_long = new Long(kerberosTime / 1000L);
0N/A return temp_long.intValue();
0N/A }
0N/A
0N/A public void setSeconds(int sec) {
0N/A Integer temp_int = new Integer(sec);
0N/A kerberosTime = temp_int.longValue() * 1000L;
0N/A }
0N/A
0N/A /**
0N/A * Parse (unmarshal) a kerberostime from a DER input stream. This form
0N/A * parsing might be used when expanding a value which is part of
0N/A * a constructed sequence and uses explicitly tagged type.
0N/A *
0N/A * @exception Asn1Exception on error.
0N/A * @param data the Der input stream value, which contains one or more marshaled value.
0N/A * @param explicitTag tag number.
0N/A * @param optional indicates if this data field is optional
0N/A * @return an instance of KerberosTime.
0N/A *
0N/A */
0N/A public static KerberosTime parse(DerInputStream data, byte explicitTag, boolean optional) throws Asn1Exception, IOException {
0N/A if ((optional) && (((byte)data.peekByte() & (byte)0x1F)!= explicitTag))
0N/A return null;
0N/A DerValue der = data.getDerValue();
0N/A if (explicitTag != (der.getTag() & (byte)0x1F)) {
0N/A throw new Asn1Exception(Krb5.ASN1_BAD_ID);
0N/A }
0N/A else {
0N/A DerValue subDer = der.getData().getDerValue();
0N/A return new KerberosTime(subDer);
0N/A }
0N/A }
0N/A
0N/A public static int getDefaultSkew() {
0N/A int tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
0N/A try {
0N/A Config c = Config.getInstance();
0N/A if ((tdiff = c.getDefaultIntValue("clockskew",
0N/A "libdefaults")) == Integer.MIN_VALUE) { //value is not defined
0N/A tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
0N/A }
0N/A } catch (KrbException e) {
0N/A if (DEBUG) {
0N/A System.out.println("Exception in getting clockskew from " +
0N/A "Configuration " +
0N/A "using default value " +
0N/A e.getMessage());
0N/A }
0N/A }
0N/A return tdiff;
0N/A }
0N/A
0N/A public String toString() {
0N/A return toGeneralizedTimeString();
0N/A }
0N/A}