src/java.security.jgss/share/classes/sun/security/krb5/internal/crypto/CksumType.java
author weijun
Wed, 03 Jul 2019 11:43:01 +0800
changeset 55570 1e95931e7d8f
parent 47216 71c04702a3d5
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
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14327
diff changeset
     2
 * Copyright (c) 2000, 2012, 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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
package sun.security.krb5.internal.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
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.Checksum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.krb5.EncryptedData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public abstract class CksumType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static boolean DEBUG = Krb5.DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static CksumType getInstance(int cksumTypeConst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        CksumType cksumType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        String cksumTypeName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        switch (cksumTypeConst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        case Checksum.CKSUMTYPE_CRC32:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            cksumType = new Crc32CksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            cksumTypeName = "sun.security.krb5.internal.crypto.Crc32CksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        case Checksum.CKSUMTYPE_DES_MAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            cksumType = new DesMacCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            cksumTypeName = "sun.security.krb5.internal.crypto.DesMacCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        case Checksum.CKSUMTYPE_DES_MAC_K:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            cksumType = new DesMacKCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                "sun.security.krb5.internal.crypto.DesMacKCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        case Checksum.CKSUMTYPE_RSA_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            cksumType = new RsaMd5CksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            cksumTypeName = "sun.security.krb5.internal.crypto.RsaMd5CksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        case Checksum.CKSUMTYPE_RSA_MD5_DES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            cksumType = new RsaMd5DesCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                "sun.security.krb5.internal.crypto.RsaMd5DesCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        case Checksum.CKSUMTYPE_HMAC_SHA1_DES3_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            cksumType = new HmacSha1Des3KdCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                "sun.security.krb5.internal.crypto.HmacSha1Des3KdCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            cksumType = new HmacSha1Aes128CksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                "sun.security.krb5.internal.crypto.HmacSha1Aes128CksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        case Checksum.CKSUMTYPE_HMAC_SHA1_96_AES256:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            cksumType = new HmacSha1Aes256CksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                "sun.security.krb5.internal.crypto.HmacSha1Aes256CksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        case Checksum.CKSUMTYPE_HMAC_MD5_ARCFOUR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            cksumType = new HmacMd5ArcFourCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                "sun.security.krb5.internal.crypto.HmacMd5ArcFourCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // currently we don't support MD4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        case Checksum.CKSUMTYPE_RSA_MD4_DES_K:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // cksumType = new RsaMd4DesKCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            // cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            //          "sun.security.krb5.internal.crypto.RsaMd4DesKCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        case Checksum.CKSUMTYPE_RSA_MD4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            // cksumType = new RsaMd4CksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // linux box support rsamd4, how to solve conflict?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            //          "sun.security.krb5.internal.crypto.RsaMd4CksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        case Checksum.CKSUMTYPE_RSA_MD4_DES:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // cksumType = new RsaMd4DesCksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // cksumTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            //          "sun.security.krb5.internal.crypto.RsaMd4DesCksumType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new KdcErrException(Krb5.KDC_ERR_SUMTYPE_NOSUPP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            System.out.println(">>> CksumType: " + cksumTypeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return cksumType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns default checksum type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static CksumType getInstance() throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // this method provided for Kerberos applications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            Config c = Config.getInstance();
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 5506
diff changeset
   129
            if ((cksumType = (Config.getType(c.get("libdefaults",
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 5506
diff changeset
   130
                    "ap_req_checksum_type")))) == - 1) {
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 5506
diff changeset
   131
                if ((cksumType = Config.getType(c.get("libdefaults",
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 5506
diff changeset
   132
                        "checksum_type"))) == -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    cksumType = Checksum.CKSUMTYPE_RSA_MD5; // default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return getInstance(cksumType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public abstract int confounderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public abstract int cksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public abstract boolean isSafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public abstract int cksumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public abstract int keyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public abstract int keySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public abstract byte[] calculateChecksum(byte[] data, int size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        throws KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public abstract byte[] calculateKeyedChecksum(byte[] data, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        byte[] key, int usage) throws KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
55570
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 47216
diff changeset
   159
    public boolean verifyChecksum(byte[] data, byte[] checksum)
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 47216
diff changeset
   160
            throws KrbCryptoException {
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 47216
diff changeset
   161
        throw new UnsupportedOperationException("Not supported");
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 47216
diff changeset
   162
    }
1e95931e7d8f 8226719: Kerberos login to Windows 2000 failed with "Inappropriate type of checksum in message"
weijun
parents: 47216
diff changeset
   163
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public abstract boolean verifyKeyedChecksum(byte[] data, int size,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        byte[] key, byte[] checksum, int usage) throws KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public static boolean isChecksumEqual(byte[] cksum1, byte[] cksum2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (cksum1 == cksum2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if ((cksum1 == null && cksum2 != null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            (cksum1 != null && cksum2 == null))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (cksum1.length != cksum2.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (int i = 0; i < cksum1.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (cksum1[i] != cksum2[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}