src/java.security.jgss/share/classes/sun/security/krb5/Checksum.java
author weijun
Wed, 03 Jul 2019 11:43:01 +0800
changeset 55570 1e95931e7d8f
parent 55258 d65d3c37232c
child 58679 9c3209ff7550
permissions -rw-r--r--
8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message" Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 48651
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * 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
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class encapsulates the concept of a Kerberos checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class Checksum {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private int cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private byte[] checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // ----------------------------------------------+-------------+-----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    //                      Checksum type            |sumtype      |checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    //                                               |value        | size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // ----------------------------------------------+-------------+-----------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int CKSUMTYPE_NULL          = 0;               // 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int CKSUMTYPE_CRC32         = 1;               // 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int CKSUMTYPE_RSA_MD4       = 2;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static final int CKSUMTYPE_RSA_MD4_DES   = 3;               // 24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final int CKSUMTYPE_DES_MAC       = 4;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final int CKSUMTYPE_DES_MAC_K     = 5;               // 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int CKSUMTYPE_RSA_MD4_DES_K = 6;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int CKSUMTYPE_RSA_MD5       = 7;               // 16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int CKSUMTYPE_RSA_MD5_DES   = 8;               // 24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     // draft-ietf-krb-wg-crypto-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final int CKSUMTYPE_HMAC_SHA1_DES3_KD = 12;          // 20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // draft-raeburn-krb-rijndael-krb-07.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public static final int CKSUMTYPE_HMAC_SHA1_96_AES128 = 15;        // 96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final int CKSUMTYPE_HMAC_SHA1_96_AES256 = 16;        // 96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
    69
    // rfc8009
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
    70
    public static final int CKSUMTYPE_HMAC_SHA256_128_AES128 = 19;        // 96
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
    71
    public static final int CKSUMTYPE_HMAC_SHA384_192_AES256 = 20;        // 96
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47216
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // draft-brezak-win2k-krb-rc4-hmac-04.txt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final int CKSUMTYPE_HMAC_MD5_ARCFOUR = -138;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static int CKSUMTYPE_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static int SAFECKSUMTYPE_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    81
        initStatic();
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    82
    }
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    83
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    84
    public static void initStatic() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        String temp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Config cfg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            cfg = Config.getInstance();
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    89
            temp = cfg.get("libdefaults", "default_checksum");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            if (temp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
    92
                    CKSUMTYPE_DEFAULT = Config.getType(temp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                     * If the default checksum is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                     * specified in the configuration we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                     * set it to RSA_MD5. We follow the MIT and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                     * SEAM implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                System.out.println("Exception in getting default checksum "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                   "value from the configuration " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                   "Setting default checksum to be RSA-MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                exc.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            CKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
   114
            temp = cfg.get("libdefaults", "safe_checksum_type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            if (temp != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 10336
diff changeset
   117
                    SAFECKSUMTYPE_DEFAULT = Config.getType(temp);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                System.out.println("Exception in getting safe default " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                   "checksum value " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                   "from the configuration Setting  " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                   "safe default checksum to be RSA-MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                exc.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            SAFECKSUMTYPE_DEFAULT = CKSUMTYPE_RSA_MD5_DES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Constructs a new Checksum using the raw data and type.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   135
     * @param data the byte array of checksum.
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   136
     * @param new_cksumType the type of checksum.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
         // used in InitialToken
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Checksum(byte[] data, int new_cksumType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        checksum = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Constructs a new Checksum by calculating the checksum over the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * using specified checksum type.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   148
     * @param new_cksumType the type of checksum.
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   149
     * @param data the data that needs to be performed a checksum calculation on.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public Checksum(int new_cksumType, byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (!cksumEngine.isSafe()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            checksum = cksumEngine.calculateChecksum(data, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new KdcErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Constructs a new Checksum by calculating the keyed checksum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * over the data using specified checksum type.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   166
     * @param new_cksumType the type of checksum.
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   167
     * @param data the data that needs to be performed a checksum calculation on.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         // KrbSafe, KrbTgsReq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Checksum(int new_cksumType, byte[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        EncryptionKey key, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        cksumType = new_cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (!cksumEngine.isSafe())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        checksum =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            cksumEngine.calculateKeyedChecksum(data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                data.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                key.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Verifies the keyed checksum over the data passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public boolean verifyKeyedChecksum(byte[] data, EncryptionKey key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                        int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        throws KdcErrException, KrbApErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        CksumType cksumEngine = CksumType.getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!cksumEngine.isSafe())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new KrbApErrException(Krb5.KRB_AP_ERR_INAPP_CKSUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return cksumEngine.verifyKeyedChecksum(data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                               data.length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                                               key.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                               checksum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            usage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
55570
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   200
    // ===============  ATTENTION! Use with care  ==================
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   201
    // According to https://tools.ietf.org/html/rfc3961#section-6.1,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   202
    // An unkeyed checksum should only be used "in limited circumstances
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   203
    // where the lack of a key does not provide a window for an attack,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   204
    // preferably as part of an encrypted message".
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   205
    public boolean verifyAnyChecksum(byte[] data, EncryptionKey key,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   206
            int usage)
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   207
            throws KdcErrException, KrbCryptoException {
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   208
        CksumType cksumEngine = CksumType.getInstance(cksumType);
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   209
        if (!cksumEngine.isSafe()) {
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   210
            return cksumEngine.verifyChecksum(data, checksum);
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   211
        } else {
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   212
            return cksumEngine.verifyKeyedChecksum(data,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   213
                    data.length,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   214
                    key.getBytes(),
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   215
                    checksum,
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   216
                    usage);
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   217
        }
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   218
    }
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 55258
diff changeset
   219
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public Checksum(byte[] data) throws KdcErrException, KrbCryptoException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        this(Checksum.CKSUMTYPE_DEFAULT, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    boolean isEqual(Checksum cksum) throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (cksumType != cksum.cksumType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        CksumType cksumEngine = CksumType.getInstance(cksumType);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   230
        return CksumType.isChecksumEqual(checksum, cksum.checksum);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Constructs an instance of Checksum from an ASN.1 encoded representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param encoding a single DER-encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @exception Asn1Exception if an error occurs while decoding an ASN1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @exception IOException if an I/O error occurs while reading encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 48651
diff changeset
   241
    public Checksum(DerValue encoding) throws Asn1Exception, IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        DerValue der;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (encoding.getTag() != DerValue.tag_Sequence) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if ((der.getTag() & (byte)0x1F) == (byte)0x00) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            cksumType = der.getData().getBigInteger().intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        der = encoding.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if ((der.getTag() & (byte)0x1F) == (byte)0x01) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            checksum = der.getData().getOctetString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (encoding.getData().available() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Encodes a Checksum object.
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   265
     * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Checksum    ::= SEQUENCE {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *         cksumtype   [0] Int32,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *         checksum    [1] OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * }
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
   270
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * This definition reflects the Network Working Group RFC 4120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * specification available at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <a href="http://www.ietf.org/rfc/rfc4120.txt">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * http://www.ietf.org/rfc/rfc4120.txt</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return byte array of enocded Checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public byte[] asn1Encode() throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        DerOutputStream temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        temp.putInteger(BigInteger.valueOf(cksumType));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                                       true, (byte)0x00), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        temp.putOctetString(checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        bytes.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                       true, (byte)0x01), temp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        temp = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        temp.write(DerValue.tag_Sequence, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return temp.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * Parse (unmarshal) a checksum object from a DER input stream.  This form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * parsing might be used when expanding a value which is part of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * a constructed sequence and uses explicitly tagged type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception Asn1Exception if an error occurs while decoding an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * ASN1 encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception IOException if an I/O error occurs while reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @param data the Der input stream value, which contains one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * marshaled value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * @param explicitTag tag number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param optional indicates if this data field is optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @return an instance of Checksum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static Checksum parse(DerInputStream data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                 byte explicitTag, boolean optional)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        throws Asn1Exception, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if ((optional) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            (((byte)data.peekByte() & (byte)0x1F) != explicitTag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        DerValue der = data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (explicitTag != (der.getTag() & (byte)0x1F))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw new Asn1Exception(Krb5.ASN1_BAD_ID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            DerValue subDer = der.getData().getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return new Checksum(subDer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Returns the raw bytes of the checksum, not in ASN.1 encoded form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public final byte[] getBytes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        return checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public final int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    @Override public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        if (!(obj instanceof Checksum)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            return isEqual((Checksum)obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        } catch (KdcErrException kee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    @Override public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        result = 37 * result + cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        if (checksum != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            result = 37 * result + Arrays.hashCode(checksum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}