1 /*
   2  * Copyright (c) 2015, 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 import java.io.IOException;
  25 import java.security.NoSuchAlgorithmException;
  26 import java.security.PrivilegedActionException;
  27 import java.util.HashMap;
  28 import java.util.Map;
  29 import javax.security.auth.login.LoginException;
  30 
  31 /*
  32  * @test
  33  * @bug 8025123
  34  * @summary Checks if an unbound server pick up a correct key from keytab
  35  * @run main/othervm/policy=unbound.ssl.policy UnboundSSLMultipleKeys
  36  *                              unbound.ssl.jaas.conf server_star
  37  * @run main/othervm/policy=unbound.ssl.policy UnboundSSLMultipleKeys
  38  *                              unbound.ssl.jaas.conf server_multiple_principals
  39  */
  40 public class UnboundSSLMultipleKeys {
  41 
  42     public static void main(String[] args)
  43             throws IOException, NoSuchAlgorithmException, LoginException,
  44             PrivilegedActionException, InterruptedException {
  45         UnboundSSLMultipleKeys test = new UnboundSSLMultipleKeys();
  46         test.start(args[0], args[1]);
  47     }
  48 
  49     private void start(String jaacConfigFile, String serverJaasConfig)
  50             throws IOException, NoSuchAlgorithmException, LoginException,
  51             PrivilegedActionException, InterruptedException {
  52 
  53         // define service principals
  54         String service1host = "service1." + UnboundSSLUtils.HOST;
  55         String service2host = "service2." + UnboundSSLUtils.HOST;
  56         String service3host = "service3." + UnboundSSLUtils.HOST;
  57         String service1Principal = "host/" + service1host + "@"
  58                 + UnboundSSLUtils.REALM;
  59         String service2Principal = "host/" + service2host + "@"
  60                 + UnboundSSLUtils.REALM;
  61         String service3Principal = "host/" + service3host + "@"
  62                 + UnboundSSLUtils.REALM;
  63 
  64         Map<String, String> principals = new HashMap<>();
  65         principals.put(UnboundSSLUtils.USER_PRINCIPAL,
  66                 UnboundSSLUtils.USER_PASSWORD);
  67         principals.put(UnboundSSLUtils.KRBTGT_PRINCIPAL, "pass");
  68         principals.put(service1Principal, "pass0");
  69         principals.put(service1Principal, "pass1");
  70         principals.put(service1Principal, "pass2");
  71         principals.put(service2Principal, "pass");
  72         principals.put(service3Principal, "pass");
  73 
  74         System.setProperty("java.security.krb5.conf",
  75                 UnboundSSLUtils.KRB5_CONF_FILENAME);
  76 
  77         /*
  78          * Start a local KDC instance
  79          *
  80          * Keytab file contains 3 keys (with different KVNO) for service1
  81          * principal, but password for only one key is the same with the record
  82          * for service1 principal in KDC.
  83          */
  84         UnboundSSLUtils.startKDC(UnboundSSLUtils.REALM, principals,
  85                 UnboundSSLUtils.KTAB_FILENAME, UnboundSSLUtils.KtabMode.APPEND);
  86 
  87         System.setProperty("java.security.auth.login.config",
  88                 UnboundSSLUtils.TEST_SRC + UnboundSSLUtils.FS + jaacConfigFile);
  89         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  90 
  91         // start an SSL server instance
  92         try (SSLEchoServer server = SSLEchoServer.init(
  93                 UnboundSSLUtils.TLS_KRB5_FILTER, UnboundSSLUtils.SNI_PATTERN)) {
  94 
  95             UnboundSSLUtils.startServerWithJaas(server, serverJaasConfig);
  96 
  97             //  wait for the server is ready
  98             while (!server.isReady()) {
  99                 Thread.sleep(UnboundSSLUtils.DELAY);
 100             }
 101 
 102             // run a client
 103             System.out.println("Successful connection is expected");
 104             SSLClient.init(UnboundSSLUtils.HOST, server.getPort(),
 105                     UnboundSSLUtils.TLS_KRB5_FILTER, service1host).connect();
 106         }
 107 
 108         System.out.println("Test passed");
 109     }
 110 
 111 }