jdk/src/share/classes/sun/security/krb5/internal/crypto/EType.java
author weijun
Fri, 04 Apr 2014 21:19:43 +0800
changeset 23716 33cc4db6209b
parent 23010 6dadb192ad81
child 24969 afa6934dd8e8
permissions -rw-r--r--
8029995: accept yes/no for boolean krb5.conf settings Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18168
diff changeset
     2
 * Copyright (c) 2000, 2013, 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: 4987
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: 4987
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: 4987
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4987
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4987
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.internal.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.krb5.Config;
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.EncryptionKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.krb5.KrbException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.krb5.KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.*;
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    41
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
//only needed if dataSize() implementation changes back to spec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
//see dataSize() below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class EType {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final boolean DEBUG = Krb5.DEBUG;
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    51
    private static boolean allowWeakCrypto;
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    52
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    53
    static {
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    54
        initStatic();
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    55
    }
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    56
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    57
    public static void initStatic() {
17443
0a7ace07ea11 8012679: Let allow_weak_crypto default to false
weijun
parents: 14342
diff changeset
    58
        boolean allowed = false;
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    59
        try {
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    60
            Config cfg = Config.getInstance();
23716
33cc4db6209b 8029995: accept yes/no for boolean krb5.conf settings
weijun
parents: 23010
diff changeset
    61
            allowed = cfg.getBooleanObject("libdefaults", "allow_weak_crypto")
33cc4db6209b 8029995: accept yes/no for boolean krb5.conf settings
weijun
parents: 23010
diff changeset
    62
                    == Boolean.TRUE;
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    63
        } catch (Exception exc) {
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    64
            if (DEBUG) {
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    65
                System.out.println ("Exception in getting allow_weak_crypto, " +
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    66
                                    "using default value " +
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    67
                                    exc.getMessage());
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    68
            }
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    69
        }
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
    70
        allowWeakCrypto = allowed;
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
    71
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static EType getInstance  (int eTypeConst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        throws KdcErrException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        EType eType = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String eTypeName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        switch (eTypeConst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        case EncryptedData.ETYPE_NULL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            eType = new NullEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            eTypeName = "sun.security.krb5.internal.crypto.NullEType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        case EncryptedData.ETYPE_DES_CBC_CRC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            eType = new DesCbcCrcEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            eTypeName = "sun.security.krb5.internal.crypto.DesCbcCrcEType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        case EncryptedData.ETYPE_DES_CBC_MD5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            eType = new DesCbcMd5EType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            eTypeName = "sun.security.krb5.internal.crypto.DesCbcMd5EType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            eType = new Des3CbcHmacSha1KdEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            eTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                "sun.security.krb5.internal.crypto.Des3CbcHmacSha1KdEType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            eType = new Aes128CtsHmacSha1EType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            eTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                "sun.security.krb5.internal.crypto.Aes128CtsHmacSha1EType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            eType = new Aes256CtsHmacSha1EType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            eTypeName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                "sun.security.krb5.internal.crypto.Aes256CtsHmacSha1EType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        case EncryptedData.ETYPE_ARCFOUR_HMAC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            eType = new ArcFourHmacEType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            eTypeName = "sun.security.krb5.internal.crypto.ArcFourHmacEType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            String msg = "encryption type = " + toString(eTypeConst)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                + " ("  + eTypeConst + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw new KdcErrException(Krb5.KDC_ERR_ETYPE_NOSUPP, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            System.out.println(">>> EType: " + eTypeName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return eType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public abstract int eType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public abstract int minimumPadSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public abstract int confounderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public abstract int checksumType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public abstract int checksumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public abstract int blockSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public abstract int keyType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public abstract int keySize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public abstract byte[] encrypt(byte[] data, byte[] key, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        throws KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public abstract byte[] encrypt(byte[] data, byte[] key, byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int usage) throws KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public abstract byte[] decrypt(byte[] cipher, byte[] key, int usage)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throws KrbApErrException, KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public abstract byte[] decrypt(byte[] cipher, byte[] key, byte[] ivec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int usage) throws KrbApErrException, KrbCryptoException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public int dataSize(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // throws Asn1Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // EncodeRef ref = new EncodeRef(data, startOfData());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // return ref.end - startOfData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // should be the above according to spec, but in fact
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // implementations include the pad bytes in the data size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return data.length - startOfData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public int padSize(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return data.length - confounderSize() - checksumSize() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            dataSize(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public int startOfChecksum() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return confounderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public int startOfData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return confounderSize() + checksumSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public int startOfPad(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return confounderSize() + checksumSize() + dataSize(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public byte[] decryptedData(byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int tempSize = dataSize(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        byte[] result = new byte[tempSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        System.arraycopy(data, startOfData(), result, 0, tempSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   187
    // Note: the first 2 entries of BUILTIN_ETYPES and BUILTIN_ETYPES_NOAES256
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   188
    // should be kept DES-related. They will be removed when allow_weak_crypto
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   189
    // is set to false.
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   190
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static final int[] BUILTIN_ETYPES = new int[] {
5974
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   192
        EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96,
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   193
        EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96,
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   194
        EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        EncryptedData.ETYPE_ARCFOUR_HMAC,
5974
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   196
        EncryptedData.ETYPE_DES_CBC_CRC,
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   197
        EncryptedData.ETYPE_DES_CBC_MD5,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private static final int[] BUILTIN_ETYPES_NOAES256 = new int[] {
5974
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   201
        EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96,
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   202
        EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        EncryptedData.ETYPE_ARCFOUR_HMAC,
5974
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   204
        EncryptedData.ETYPE_DES_CBC_CRC,
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   205
        EncryptedData.ETYPE_DES_CBC_MD5,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    // used in Config
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static int[] getBuiltInDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int allowed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            allowed = Cipher.getMaxAllowedKeyLength("AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   217
        int[] result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        if (allowed < 256) {
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   219
            result = BUILTIN_ETYPES_NOAES256;
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   220
        } else {
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   221
            result = BUILTIN_ETYPES;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
14327
c0d86f6f8be8 7184246: Simplify Config.get() of krb5
weijun
parents: 9035
diff changeset
   223
        if (!allowWeakCrypto) {
5974
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   224
            // The last 2 etypes are now weak ones
f0531b7dfebe 6844907: krb5 etype order should be from strong to weak
weijun
parents: 5506
diff changeset
   225
            return Arrays.copyOfRange(result, 0, result.length - 2);
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   226
        }
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   227
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Retrieves the default etypes from the configuration file, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * if that's not available, return the built-in list of default etypes.
18168
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   233
     * This result is always non-empty. If no etypes are found,
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   234
     * an exception is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
18168
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   236
    public static int[] getDefaults(String configName)
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   237
            throws KrbException {
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   238
        Config config = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        try {
18168
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   240
            config = Config.getInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } catch (KrbException exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                System.out.println("Exception while getting " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    configName + exc.getMessage());
4987
5b7352dd6a2f 6844909: support allow_weak_crypto in krb5.conf
weijun
parents: 2
diff changeset
   245
                System.out.println("Using default builtin etypes");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return getBuiltInDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
18168
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   249
        return config.defaultEtype(configName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * Retrieve the default etypes from the configuration file for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * those etypes for which there are corresponding keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Used in scenario we have some keys from a keytab with etypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * different from those named in configName. Then, in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * to decrypt an AS-REP, we should only ask for etypes for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * we have keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public static int[] getDefaults(String configName, EncryptionKey[] keys)
18168
f47169155ea0 8014310: JAAS/Krb5LoginModule using des encytypes failure with NPE after JDK-8012679
weijun
parents: 17443
diff changeset
   261
            throws KrbException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        int[] answer = getDefaults(configName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 5974
diff changeset
   264
        List<Integer> list = new ArrayList<>(answer.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        for (int i = 0; i < answer.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (EncryptionKey.findKey(answer[i], keys) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                list.add(answer[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        int len = list.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (len <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            StringBuffer keystr = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                keystr.append(toString(keys[i].getEType()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                keystr.append(" ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            throw new KrbException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                "Do not have keys of types listed in " + configName +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                " available; only have keys of following type: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                keystr.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            answer = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                answer[i] = list.get(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    public static boolean isSupported(int eTypeConst, int[] config) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (int i = 0; i < config.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (eTypeConst == config[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    public static boolean isSupported(int eTypeConst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        int[] enabledETypes = getBuiltInDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return isSupported(eTypeConst, enabledETypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public static String toString(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return "NULL";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return "DES CBC mode with CRC-32";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return "DES CBC mode with MD4";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return "DES CBC mode with MD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return "reserved";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return "DES3 CBC mode with MD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        case 6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            return "reserved";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        case 7:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return "DES3 CBC mode with SHA1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        case 9:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return "DSA with SHA1- Cms0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        case 10:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return "MD5 with RSA encryption - Cms0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        case 11:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            return "SHA1 with RSA encryption - Cms0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        case 12:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            return "RC2 CBC mode with Env0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case 13:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return "RSA encryption with Env0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        case 14:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            return "RSAES-0AEP-ENV-0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        case 15:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return "DES-EDE3-CBC-ENV-0ID";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        case 16:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            return "DES3 CBC mode with SHA1-KD";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        case 17:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return "AES128 CTS mode with HMAC SHA1-96";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        case 18:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return "AES256 CTS mode with HMAC SHA1-96";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        case 23:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return "RC4 with HMAC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        case 24:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return "RC4 with HMAC EXP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        return "Unknown (" + type + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}