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