|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
22 | 22 | */
|
23 | 23 | /*
|
24 | 24 | * @test
|
25 |
| - * @bug 6844909 8012679 |
| 25 | + * @bug 6844909 8012679 8139348 |
26 | 26 | * @run main/othervm WeakCrypto
|
27 | 27 | * @run main/othervm WeakCrypto true
|
28 | 28 | * @run main/othervm WeakCrypto false
|
29 | 29 | * @summary support allow_weak_crypto in krb5.conf
|
30 | 30 | */
|
31 | 31 |
|
32 |
| -import java.io.File; |
33 | 32 | import java.lang.Exception;
|
34 | 33 | import java.nio.file.Files;
|
35 | 34 | import java.nio.file.Paths;
|
| 35 | +import java.util.Arrays; |
| 36 | +import java.util.List; |
36 | 37 |
|
| 38 | +import sun.security.krb5.EncryptionKey; |
37 | 39 | import sun.security.krb5.internal.crypto.EType;
|
38 | 40 | import sun.security.krb5.EncryptedData;
|
39 | 41 |
|
40 | 42 | public class WeakCrypto {
|
| 43 | + |
| 44 | + static List<Integer> weakOnes = Arrays.asList( |
| 45 | + EncryptedData.ETYPE_DES_CBC_CRC, |
| 46 | + EncryptedData.ETYPE_DES_CBC_MD5, |
| 47 | + EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD, |
| 48 | + EncryptedData.ETYPE_ARCFOUR_HMAC |
| 49 | + ); |
| 50 | + |
41 | 51 | public static void main(String[] args) throws Exception {
|
| 52 | + |
42 | 53 | String conf = "[libdefaults]\n" +
|
43 | 54 | (args.length > 0 ? ("allow_weak_crypto = " + args[0]) : "");
|
44 | 55 | Files.write(Paths.get("krb5.conf"), conf.getBytes());
|
45 | 56 | System.setProperty("java.security.krb5.conf", "krb5.conf");
|
46 | 57 |
|
47 |
| - boolean expected = args.length != 0 && args[0].equals("true"); |
48 |
| - int[] etypes = EType.getBuiltInDefaults(); |
| 58 | + // expected number of supported weak etypes |
| 59 | + int expected = 0; |
| 60 | + if (args.length != 0 && args[0].equals("true")) { |
| 61 | + expected = weakOnes.size(); |
| 62 | + } |
49 | 63 |
|
50 |
| - boolean found = false; |
51 |
| - for (int i=0, length = etypes.length; i<length; i++) { |
52 |
| - if (etypes[i] == EncryptedData.ETYPE_DES_CBC_CRC || |
53 |
| - etypes[i] == EncryptedData.ETYPE_DES_CBC_MD4 || |
54 |
| - etypes[i] == EncryptedData.ETYPE_DES_CBC_MD5) { |
55 |
| - found = true; |
56 |
| - } |
| 64 | + // Ensure EType.getBuiltInDefaults() has the correct etypes |
| 65 | + if (Arrays.stream(EType.getBuiltInDefaults()) |
| 66 | + .filter(weakOnes::contains) |
| 67 | + .count() != expected) { |
| 68 | + throw new Exception("getBuiltInDefaults fails"); |
57 | 69 | }
|
58 |
| - if (expected != found) { |
59 |
| - throw new Exception(); |
| 70 | + |
| 71 | + // Ensure keys generated have the correct etypes |
| 72 | + if (Arrays.stream(EncryptionKey.acquireSecretKeys( |
| 73 | + "password".toCharArray(), "salt")) |
| 74 | + .map(EncryptionKey::getEType) |
| 75 | + .filter(weakOnes::contains) |
| 76 | + .count() != expected) { |
| 77 | + throw new Exception("acquireSecretKeys fails"); |
60 | 78 | }
|
61 | 79 | }
|
62 | 80 | }
|
1 commit comments
openjdk-notifier[bot] commentedon Jun 2, 2023
Review
Issues