1/*
2 * Copyright (c) 2014, 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/*
25 * @test
26 * @bug 8035986
27 * @summary KerberosKey algorithm names are not specified
28 * @modules java.security.jgss/sun.security.krb5
29 */
30
31import sun.security.krb5.EncryptedData;
32
33import javax.crypto.Cipher;
34import javax.security.auth.kerberos.KerberosKey;
35import javax.security.auth.kerberos.KerberosPrincipal;
36import java.util.Locale;
37
38public class StandardNames {
39    static KerberosPrincipal kp = new KerberosPrincipal("user@REALM");
40    static char[] pass = "secret".toCharArray();
41    static byte[] keyBytes = new byte[1];
42
43    public static void main(String[] args) throws Exception {
44        for (EncType e: EncType.values()) {
45            if (e == EncType.e18) {
46                if (Cipher.getMaxAllowedKeyLength("AES") < 256) {
47                    System.out.println("Skipping aes256-cts-hmac-sha1-96");
48                    continue;
49                }
50            }
51            checkByName(e.name, e);
52            checkByName(e.name.toUpperCase(Locale.US), e);
53            for (String n: e.oldnames) {
54                checkByName(n, e);
55                if (n != null) {
56                    checkByName(n.toLowerCase(Locale.US), e);
57                }
58            }
59            checkByEType(e.etype, e.name);
60        }
61        checkByEType(100, "unknown");
62        checkByEType(-1, "private");
63
64        try {
65            System.out.println("unsupported");
66            new KerberosKey(kp, pass, "unsupported");
67            throw new Exception("unsupported");
68        } catch (IllegalArgumentException iae) {
69            // Expected
70        }
71    }
72
73    private static void checkByName(String n, EncType e) throws Exception {
74        System.out.println("CheckByName " + n);
75        KerberosKey k = new KerberosKey(kp, pass, n);
76        if (!k.getAlgorithm().equals(e.name)) throw new Exception(n);
77        if (k.getKeyType() != e.etype) throw new Exception(n);
78        if (k.getVersionNumber() != 0) throw new Exception(n);
79    }
80
81    private static void checkByEType(int i, String n) throws Exception {
82        System.out.println("CheckByInt " + i);
83        KerberosKey k = new KerberosKey(kp, keyBytes, i, 13);
84        if (!k.getAlgorithm().equals(n)) throw new Exception("" + i);
85        if (k.getKeyType() != i) throw new Exception("" + i);
86        if (k.getVersionNumber() != 13) throw new Exception("" + i);
87    }
88}
89
90enum EncType {
91    e0("none", EncryptedData.ETYPE_NULL),
92    e1("des-cbc-crc", EncryptedData.ETYPE_DES_CBC_CRC),
93    e3("des-cbc-md5", EncryptedData.ETYPE_DES_CBC_MD5, "DES", null),
94    e16("des3-cbc-sha1-kd", EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD, "DESede"),
95    e17("aes128-cts-hmac-sha1-96", EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96, "AES128"),
96    e18("aes256-cts-hmac-sha1-96", EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96, "AES256"),
97    e23("rc4-hmac", EncryptedData.ETYPE_ARCFOUR_HMAC, "ArcFourHmac"),
98    ;
99
100    final String name;
101    final int etype;
102    final String[] oldnames;
103
104    EncType(String name, int etype, String... oldnames) {
105        this.name = name;
106        this.etype = etype;
107        this.oldnames = oldnames;
108    }
109}
110