jdk/src/share/classes/sun/security/krb5/internal/KerberosTime.java
author weijun
Wed, 17 Apr 2013 10:15:33 +0800
changeset 16905 0419f45c7761
parent 14327 c0d86f6f8be8
child 25186 63e1a2ec30f5
permissions -rw-r--r--
8011124: Make KerberosTime immutable Reviewed-by: xuelei
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
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    33
import sun.security.krb5.Asn1Exception;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.krb5.Config;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.KrbException;
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    36
import sun.security.util.DerInputStream;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    37
import sun.security.util.DerOutputStream;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    38
import sun.security.util.DerValue;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    39
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    40
import java.io.IOException;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    41
import java.util.Calendar;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Date;
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    43
import java.util.TimeZone;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    46
 * Implements the ASN.1 KerberosTime type. This is an immutable class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * KerberosTime    ::= GeneralizedTime -- with no fractional seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </xmp>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The timestamps used in Kerberos are encoded as GeneralizedTimes. A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * KerberosTime value shall not include any fractional portions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * seconds.  As required by the DER, it further shall not include any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * separators, and it shall specify the UTC time zone (Z).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * http://www.ietf.org/rfc/rfc4120.txt</a>.
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    62
 *
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    63
 * The implementation also includes the microseconds info so that the
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    64
 * same class can be used as a precise timestamp in Authenticator etc.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    67
public class KerberosTime {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    69
    private final long kerberosTime; // milliseconds since epoch, Date.getTime()
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    70
    private final int  microSeconds; // last 3 digits of the real microsecond
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    71
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    72
    // The time when this class is loaded. Used in setNow()
11025
d7f6c062c454 7077172: KerberosTime does not take into account system clock adjustement
weijun
parents: 7183
diff changeset
    73
    private static long initMilli = System.currentTimeMillis();
d7f6c062c454 7077172: KerberosTime does not take into account system clock adjustement
weijun
parents: 7183
diff changeset
    74
    private static long initMicro = System.nanoTime() / 1000;
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    75
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    78
    // Do not make this public. It's a little confusing that micro
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    79
    // is only the last 3 digits of microsecond.
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    80
    private KerberosTime(long time, int micro) {
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    81
        kerberosTime = time;
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    82
        microSeconds = micro;
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
    83
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    85
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    86
     * Creates a KerberosTime object from milliseconds since epoch.
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    87
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    88
    public KerberosTime(long time) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    89
        this(time, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // This constructor is used in the native code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    // src/windows/native/sun/security/krb5/NativeCreds.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public KerberosTime(String time) throws Asn1Exception {
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    95
        this(toKerberosTime(time), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static long toKerberosTime(String time) throws Asn1Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // ASN.1 GeneralizedTime format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // "19700101000000Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        //  |   | | | | | |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        //  0   4 6 8 | | |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //           10 | |
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   105
        //             12 |
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   106
        //               14
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (time.length() != 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (time.charAt(14) != 'Z')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int year = Integer.parseInt(time.substring(0, 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        calendar.clear(); // so that millisecond is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        calendar.set(year,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     Integer.parseInt(time.substring(4, 6)) - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                     Integer.parseInt(time.substring(6, 8)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                     Integer.parseInt(time.substring(8, 10)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                     Integer.parseInt(time.substring(10, 12)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                     Integer.parseInt(time.substring(12, 14)));
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   121
        return calendar.getTimeInMillis();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   124
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   125
     * Creates a KerberosTime object from a Date object.
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   126
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   127
    public KerberosTime(Date time) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   128
        this(time.getTime(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   131
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   132
     * Creates a KerberosTime object for now. It uses System.nanoTime()
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   133
     * to get a more precise time than "new Date()".
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   134
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   135
    public static KerberosTime now() {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   136
        long newMilli = System.currentTimeMillis();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   137
        long newMicro = System.nanoTime() / 1000;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   138
        long microElapsed = newMicro - initMicro;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   139
        long calcMilli = initMilli + microElapsed/1000;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   140
        if (calcMilli - newMilli > 100 || newMilli - calcMilli > 100) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   141
            if (DEBUG) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   142
                System.out.println("System time adjusted");
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   143
            }
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   144
            initMilli = newMilli;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   145
            initMicro = newMicro;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   146
            return new KerberosTime(newMilli, 0);
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   147
        } else {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   148
            return new KerberosTime(calcMilli, (int)(microElapsed % 1000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns a string representation of KerberosTime object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public String toGeneralizedTimeString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        calendar.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        calendar.setTimeInMillis(kerberosTime);
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   161
        return String.format("%04d%02d%02d%02d%02d%02dZ",
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   162
                calendar.get(Calendar.YEAR),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   163
                calendar.get(Calendar.MONTH) + 1,
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   164
                calendar.get(Calendar.DAY_OF_MONTH),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   165
                calendar.get(Calendar.HOUR_OF_DAY),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   166
                calendar.get(Calendar.MINUTE),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   167
                calendar.get(Calendar.SECOND));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Encodes this object to a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return a byte array of encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        out.putGeneralizedTime(this.toDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return kerberosTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public Date toDate() {
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   187
        return new Date(kerberosTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public int getMicroSeconds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Long temp_long = new Long((kerberosTime % 1000L) * 1000L);
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   192
        return temp_long.intValue() + microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   195
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   196
     * Returns a new KerberosTime object with the original seconds
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   197
     * and the given microseconds.
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   198
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   199
    public KerberosTime withMicroSeconds(int usec) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   200
        return new KerberosTime(
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   201
                kerberosTime - kerberosTime%1000L + usec/1000L,
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   202
                usec%1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   205
    private boolean inClockSkew(int clockSkew) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   206
        return java.lang.Math.abs(kerberosTime - System.currentTimeMillis())
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   207
                <= clockSkew * 1000L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public boolean inClockSkew() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return inClockSkew(getDefaultSkew());
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 boolean greaterThanWRTClockSkew(KerberosTime time, int clockSkew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if ((kerberosTime - time.kerberosTime) > clockSkew * 1000L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return false;
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 boolean greaterThanWRTClockSkew(KerberosTime time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return greaterThanWRTClockSkew(time, getDefaultSkew());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public boolean greaterThan(KerberosTime time) {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   225
        return kerberosTime > time.kerberosTime ||
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   226
            kerberosTime == time.kerberosTime &&
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   227
                    microSeconds > time.microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        if (!(obj instanceof KerberosTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   239
        return kerberosTime == ((KerberosTime)obj).kerberosTime &&
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   240
                microSeconds == ((KerberosTime)obj).microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public int hashCode() {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   244
        int result = 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   245
        return result * 17 + microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public boolean isZero() {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   249
        return kerberosTime == 0 && microSeconds == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public int getSeconds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Long temp_long = new Long(kerberosTime / 1000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return temp_long.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * Parse (unmarshal) a kerberostime from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception Asn1Exception on error.
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   263
     * @param data the Der input stream value, which contains
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   264
     *             one or more marshaled value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return an instance of KerberosTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   270
    public static KerberosTime parse(
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   271
            DerInputStream data, byte explicitTag, boolean optional)
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   272
            throws Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F)!= explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            DerValue subDer = der.getData().getDerValue();
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   281
            Date temp = subDer.getGeneralizedTime();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   282
            return new KerberosTime(temp.getTime(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static int getDefaultSkew() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        try {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   289
            if ((tdiff = Config.getInstance().getIntValue(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   290
                    "libdefaults", "clockskew"))
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   291
                        == Integer.MIN_VALUE) {   //value is not defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                System.out.println("Exception in getting clockskew from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                   "Configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                   "using default value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return tdiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return toGeneralizedTimeString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}