6844909: support allow_weak_crypto in krb5.conf
authorweijun
Thu, 04 Mar 2010 10:37:16 +0800
changeset 4987 5b7352dd6a2f
parent 4986 25108bab43ad
child 4988 1268f09a31e3
6844909: support allow_weak_crypto in krb5.conf Reviewed-by: valeriep
jdk/src/share/classes/sun/security/krb5/internal/crypto/EType.java
jdk/test/sun/security/krb5/etype/WeakCrypto.java
jdk/test/sun/security/krb5/etype/weakcrypto.conf
--- a/jdk/src/share/classes/sun/security/krb5/internal/crypto/EType.java	Wed Mar 03 11:29:44 2010 -0800
+++ b/jdk/src/share/classes/sun/security/krb5/internal/crypto/EType.java	Thu Mar 04 10:37:16 2010 +0800
@@ -1,5 +1,5 @@
 /*
- * Portions Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
+ * Portions Copyright 2000-2010 Sun Microsystems, Inc.  All Rights Reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -36,9 +36,9 @@
 import sun.security.krb5.EncryptedData;
 import sun.security.krb5.EncryptionKey;
 import sun.security.krb5.KrbException;
-import sun.security.krb5.Asn1Exception;
 import sun.security.krb5.KrbCryptoException;
 import javax.crypto.*;
+import java.util.Arrays;
 import java.util.List;
 import java.util.ArrayList;
 
@@ -48,6 +48,23 @@
 public abstract class EType {
 
     private static final boolean DEBUG = Krb5.DEBUG;
+    private static final boolean ALLOW_WEAK_CRYPTO;
+
+    static {
+        boolean allowed = true;
+        try {
+            Config cfg = Config.getInstance();
+            String temp = cfg.getDefault("allow_weak_crypto", "libdefaults");
+            if (temp != null && temp.equals("false")) allowed = false;
+        } catch (Exception exc) {
+            if (DEBUG) {
+                System.out.println ("Exception in getting allow_weak_crypto, " +
+                                    "using default value " +
+                                    exc.getMessage());
+            }
+        }
+        ALLOW_WEAK_CRYPTO = allowed;
+    }
 
     public static EType getInstance  (int eTypeConst)
         throws KdcErrException {
@@ -163,6 +180,10 @@
         return result;
     }
 
+    // Note: the first 2 entries of BUILTIN_ETYPES and BUILTIN_ETYPES_NOAES256
+    // should be kept DES-related. They will be removed when allow_weak_crypto
+    // is set to false.
+
     private static final int[] BUILTIN_ETYPES = new int[] {
         EncryptedData.ETYPE_DES_CBC_MD5,
         EncryptedData.ETYPE_DES_CBC_CRC,
@@ -189,10 +210,17 @@
         } catch (Exception e) {
             // should not happen
         }
+        int[] result;
         if (allowed < 256) {
-            return BUILTIN_ETYPES_NOAES256;
+            result = BUILTIN_ETYPES_NOAES256;
+        } else {
+            result = BUILTIN_ETYPES;
         }
-        return BUILTIN_ETYPES;
+        if (!ALLOW_WEAK_CRYPTO) {
+            // The first 2 etypes are now weak ones
+            return Arrays.copyOfRange(result, 2, result.length);
+        }
+        return result;
     }
 
     /**
@@ -207,9 +235,7 @@
             if (DEBUG) {
                 System.out.println("Exception while getting " +
                     configName + exc.getMessage());
-                System.out.println("Using defaults " +
-                    "des-cbc-md5, des-cbc-crc, des3-cbc-sha1," +
-                        " aes128cts, aes256cts, rc4-hmac");
+                System.out.println("Using default builtin etypes");
             }
             return getBuiltInDefaults();
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/krb5/etype/WeakCrypto.java	Thu Mar 04 10:37:16 2010 +0800
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2010 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+/*
+ * @test
+ * @bug 6844909
+ * @run main/othervm WeakCrypto
+ * @summary support allow_weak_crypto in krb5.conf
+ */
+
+import java.io.File;
+import sun.security.krb5.internal.crypto.EType;
+import sun.security.krb5.EncryptedData;
+
+public class WeakCrypto {
+    public static void main(String[] args) throws Exception {
+        System.setProperty("java.security.krb5.conf",
+                System.getProperty("test.src", ".") +
+                File.separator +
+                "weakcrypto.conf");
+        int[] etypes = EType.getBuiltInDefaults();
+
+        for (int i=0, length = etypes.length; i<length; i++) {
+            if (etypes[i] == EncryptedData.ETYPE_DES_CBC_CRC ||
+                    etypes[i] == EncryptedData.ETYPE_DES_CBC_MD4 ||
+                    etypes[i] == EncryptedData.ETYPE_DES_CBC_MD5) {
+                throw new Exception("DES should not appear");
+            }
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/krb5/etype/weakcrypto.conf	Thu Mar 04 10:37:16 2010 +0800
@@ -0,0 +1,2 @@
+[libdefaults]
+allow_weak_crypto = false