1/*
2 * Copyright (c) 2012, 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 7152176
27 * @summary More krb5 tests
28 * @compile -XDignore.symbol.file TwoTab.java
29 * @run main/othervm TwoTab
30 */
31
32import java.io.File;
33import java.io.FileOutputStream;
34import java.nio.file.Files;
35import java.security.Security;
36import sun.security.jgss.GSSUtil;
37import sun.security.krb5.PrincipalName;
38import sun.security.krb5.internal.ktab.KeyTab;
39
40// Two services using their own keytab.
41public class TwoTab {
42
43    public static void main(String[] args) throws Exception {
44
45        KDC k = new OneKDC(null);
46
47        // Write JAAS conf, two service using different keytabs
48        System.setProperty("java.security.auth.login.config", OneKDC.JAAS_CONF);
49        File f = new File(OneKDC.JAAS_CONF);
50        try (FileOutputStream fos = new FileOutputStream(f)) {
51            fos.write((
52                "server {\n" +
53                "    com.sun.security.auth.module.Krb5LoginModule required\n" +
54                "    principal=\"" + OneKDC.SERVER + "\"\n" +
55                "    useKeyTab=true\n" +
56                "    keyTab=server.keytab\n" +
57                "    storeKey=true;\n};\n" +
58                "server2 {\n" +
59                "    com.sun.security.auth.module.Krb5LoginModule required\n" +
60                "    principal=\"" + OneKDC.BACKEND + "\"\n" +
61                "    useKeyTab=true\n" +
62                "    keyTab=backend.keytab\n" +
63                "    storeKey=true;\n};\n"
64                ).getBytes());
65        }
66        f.deleteOnExit();
67
68        k.writeKtab("server.keytab", false, "server/host.rabbit.hole@RABBIT.HOLE");
69        k.writeKtab("backend.keytab", false, "backend/host.rabbit.hole@RABBIT.HOLE");
70
71        Context c, s, s2;
72        c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
73        s = Context.fromJAAS("server");
74        s2 = Context.fromJAAS("server2");
75
76        c.startAsClient(OneKDC.SERVER, GSSUtil.GSS_KRB5_MECH_OID);
77        s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
78
79        Context.handshake(c, s);
80
81        Context.transmit("i say high --", c, s);
82        Context.transmit("   you say low", s, c);
83
84        s.dispose();
85        c.dispose();
86
87        c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
88        c.startAsClient(OneKDC.BACKEND, GSSUtil.GSS_KRB5_MECH_OID);
89        s2.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
90
91        Context.handshake(c, s2);
92
93        Context.transmit("i say high --", c, s2);
94        Context.transmit("   you say low", s2, c);
95
96        s2.dispose();
97        c.dispose();
98    }
99}
100