1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 6844909 8012679
  26  * @modules java.security.jgss/sun.security.krb5
  27  *          java.security.jgss/sun.security.krb5.internal.crypto
  28  * @run main/othervm WeakCrypto
  29  * @run main/othervm WeakCrypto true
  30  * @run main/othervm WeakCrypto false
  31  * @summary support allow_weak_crypto in krb5.conf
  32  */
  33 
  34 import java.io.File;
  35 import java.lang.Exception;
  36 import java.nio.file.Files;
  37 import java.nio.file.Paths;
  38 
  39 import sun.security.krb5.internal.crypto.EType;
  40 import sun.security.krb5.EncryptedData;
  41 
  42 public class WeakCrypto {
  43     public static void main(String[] args) throws Exception {
  44         String conf = "[libdefaults]\n" +
  45                 (args.length > 0 ? ("allow_weak_crypto = " + args[0]) : "");
  46         Files.write(Paths.get("krb5.conf"), conf.getBytes());
  47         System.setProperty("java.security.krb5.conf", "krb5.conf");
  48 
  49         boolean expected = args.length != 0 && args[0].equals("true");
  50         int[] etypes = EType.getBuiltInDefaults();
  51 
  52         boolean found = false;
  53         for (int i=0, length = etypes.length; i<length; i++) {
  54             if (etypes[i] == EncryptedData.ETYPE_DES_CBC_CRC ||
  55                     etypes[i] == EncryptedData.ETYPE_DES_CBC_MD4 ||
  56                     etypes[i] == EncryptedData.ETYPE_DES_CBC_MD5) {
  57                 found = true;
  58             }
  59         }
  60         if (expected != found) {
  61             throw new Exception();
  62         }
  63     }
  64 }