src/java.security.jgss/share/classes/sun/security/krb5/internal/KerberosTime.java
author weijun
Wed, 25 Sep 2019 17:54:21 +0800
changeset 58331 e4ce29f6094e
parent 47216 71c04702a3d5
permissions -rw-r--r--
8228659: Record which Java methods are called by native codes in JGSS and JAAS Reviewed-by: mullan
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
58331
e4ce29f6094e 8228659: Record which Java methods are called by native codes in JGSS and JAAS
weijun
parents: 47216
diff changeset
    91
    // Warning: called by NativeCreds.c and nativeccache.c
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public KerberosTime(String time) throws Asn1Exception {
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
    93
        this(toKerberosTime(time), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private static long toKerberosTime(String time) throws Asn1Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // ASN.1 GeneralizedTime format:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // "19700101000000Z"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        //  |   | | | | | |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        //  0   4 6 8 | | |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        //           10 | |
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   103
        //             12 |
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   104
        //               14
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (time.length() != 15)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (time.charAt(14) != 'Z')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new Asn1Exception(Krb5.ASN1_BAD_TIMEFORMAT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        int year = Integer.parseInt(time.substring(0, 4));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        calendar.clear(); // so that millisecond is zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        calendar.set(year,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                     Integer.parseInt(time.substring(4, 6)) - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                     Integer.parseInt(time.substring(6, 8)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                     Integer.parseInt(time.substring(8, 10)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                     Integer.parseInt(time.substring(10, 12)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                     Integer.parseInt(time.substring(12, 14)));
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   119
        return calendar.getTimeInMillis();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   120
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   122
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   123
     * Creates a KerberosTime object from a Date object.
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   124
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   125
    public KerberosTime(Date time) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   126
        this(time.getTime(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   129
    /**
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
   130
     * 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
   131
     */
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
    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
   133
        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
   134
                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
   135
    }
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
    /**
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   138
     * Creates a KerberosTime object for now. It uses System.nanoTime()
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   139
     * to get a more precise time than "new Date()".
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   140
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   141
    public static KerberosTime now() {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   142
        long newMilli = System.currentTimeMillis();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   143
        long newMicro = System.nanoTime() / 1000;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   144
        long microElapsed = newMicro - initMicro;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   145
        long calcMilli = initMilli + microElapsed/1000;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   146
        if (calcMilli - newMilli > 100 || newMilli - calcMilli > 100) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   147
            if (DEBUG) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   148
                System.out.println("System time adjusted");
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   149
            }
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   150
            initMilli = newMilli;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   151
            initMicro = newMicro;
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   152
            return new KerberosTime(newMilli, 0);
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   153
        } else {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   154
            return new KerberosTime(calcMilli, (int)(microElapsed % 1000));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
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
     * Returns a string representation of KerberosTime object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @return a string representation of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public String toGeneralizedTimeString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        calendar.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        calendar.setTimeInMillis(kerberosTime);
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   167
        return String.format("%04d%02d%02d%02d%02d%02dZ",
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   168
                calendar.get(Calendar.YEAR),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   169
                calendar.get(Calendar.MONTH) + 1,
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   170
                calendar.get(Calendar.DAY_OF_MONTH),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   171
                calendar.get(Calendar.HOUR_OF_DAY),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   172
                calendar.get(Calendar.MINUTE),
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   173
                calendar.get(Calendar.SECOND));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Encodes this object to a byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return a byte array of encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception Asn1Exception if an error occurs while decoding an ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        out.putGeneralizedTime(this.toDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public long getTime() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return kerberosTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public Date toDate() {
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   193
        return new Date(kerberosTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public int getMicroSeconds() {
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 16905
diff changeset
   197
        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
   198
        return temp_int + microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   201
    /**
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   202
     * Returns a new KerberosTime object with the original seconds
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   203
     * and the given microseconds.
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   204
     */
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   205
    public KerberosTime withMicroSeconds(int usec) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   206
        return new KerberosTime(
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   207
                kerberosTime - kerberosTime%1000L + usec/1000L,
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   208
                usec%1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   211
    private boolean inClockSkew(int clockSkew) {
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   212
        return java.lang.Math.abs(kerberosTime - System.currentTimeMillis())
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   213
                <= clockSkew * 1000L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public boolean inClockSkew() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return inClockSkew(getDefaultSkew());
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, int clockSkew) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if ((kerberosTime - time.kerberosTime) > clockSkew * 1000L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public boolean greaterThanWRTClockSkew(KerberosTime time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return greaterThanWRTClockSkew(time, getDefaultSkew());
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 greaterThan(KerberosTime time) {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   231
        return kerberosTime > time.kerberosTime ||
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   232
            kerberosTime == time.kerberosTime &&
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   233
                    microSeconds > time.microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (!(obj instanceof KerberosTime)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   245
        return kerberosTime == ((KerberosTime)obj).kerberosTime &&
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   246
                microSeconds == ((KerberosTime)obj).microSeconds;
2
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 int hashCode() {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   250
        int result = 37 * 17 + (int)(kerberosTime ^ (kerberosTime >>> 32));
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   251
        return result * 17 + microSeconds;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public boolean isZero() {
5615
3d77087a5a6c 6882687: KerberosTime too imprecise
weijun
parents: 2
diff changeset
   255
        return kerberosTime == 0 && microSeconds == 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public int getSeconds() {
25186
63e1a2ec30f5 8048267: Replace uses of 'new Long()' with appropriate alternative across core classes
prappo
parents: 16905
diff changeset
   259
        return (int) (kerberosTime / 1000L);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Parse (unmarshal) a kerberostime from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception Asn1Exception on error.
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   268
     * @param data the Der input stream value, which contains
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   269
     *             one or more marshaled value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @return an instance of KerberosTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   275
    public static KerberosTime parse(
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   276
            DerInputStream data, byte explicitTag, boolean optional)
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   277
            throws Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if ((optional) && (((byte)data.peekByte() & (byte)0x1F)!= explicitTag))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            DerValue subDer = der.getData().getDerValue();
16905
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   286
            Date temp = subDer.getGeneralizedTime();
0419f45c7761 8011124: Make KerberosTime immutable
weijun
parents: 14327
diff changeset
   287
            return new KerberosTime(temp.getTime(), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public static int getDefaultSkew() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        try {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   294
            if ((tdiff = Config.getInstance().getIntValue(
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   295
                    "libdefaults", "clockskew"))
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 11025
diff changeset
   296
                        == Integer.MIN_VALUE) {   //value is not defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                tdiff = Krb5.DEFAULT_ALLOWABLE_CLOCKSKEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                System.out.println("Exception in getting clockskew from " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                   "Configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                   "using default value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return tdiff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        return toGeneralizedTimeString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
}