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