1 /*
   2  * Copyright (c) 2015, 2018, 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 8194486
  34  * @summary Checks if an unbound server uses a service principal
  35  *          from sun.security.krb5.principal system property if specified
  36  * @library /test/lib
  37  * @run main jdk.test.lib.FileInstaller TestHosts TestHosts
  38  * @run main/othervm -Djdk.net.hosts.file=TestHosts UnboundSSLPrincipalProperty
  39  *                              unbound.ssl.jaas.conf server_star
  40  * @run main/othervm -Djdk.net.hosts.file=TestHosts UnboundSSLPrincipalProperty
  41  *                              unbound.ssl.jaas.conf server_multiple_principals
  42  */
  43 public class UnboundSSLPrincipalProperty {
  44 
  45     public static void main(String[] args) throws IOException,
  46             NoSuchAlgorithmException,LoginException, PrivilegedActionException,
  47             InterruptedException {
  48         UnboundSSLPrincipalProperty test = new UnboundSSLPrincipalProperty();
  49         test.start(args[0], args[1]);
  50     }
  51 
  52     public void start(String jaacConfigFile, String serverJaasConfig)
  53             throws IOException, NoSuchAlgorithmException,LoginException,
  54             PrivilegedActionException, InterruptedException {
  55 
  56         // define principals
  57         String service1host = "service1." + UnboundSSLUtils.HOST;
  58         String service3host = "service3." + UnboundSSLUtils.HOST;
  59         String service1Principal = "host/" + service1host + "@"
  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, null);
  68         principals.put(service1Principal, null);
  69         principals.put(service3Principal, null);
  70 
  71         System.setProperty("java.security.krb5.conf",
  72                 UnboundSSLUtils.KRB5_CONF_FILENAME);
  73 
  74         // start a local KDC instance
  75         KDC.startKDC(UnboundSSLUtils.HOST, UnboundSSLUtils.KRB5_CONF_FILENAME,
  76                 UnboundSSLUtils.REALM, principals,
  77                 UnboundSSLUtils.KTAB_FILENAME, KDC.KtabMode.APPEND);
  78 
  79         System.setProperty("java.security.auth.login.config",
  80                 UnboundSSLUtils.TEST_SRC + UnboundSSLUtils.FS + jaacConfigFile);
  81         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  82 
  83         // start an SSL server instance
  84         try (final SSLEchoServer server = SSLEchoServer.init(
  85                 UnboundSSLUtils.TLS_KRB5_FILTER, UnboundSSLUtils.SNI_PATTERN)) {
  86 
  87             // specify a service principal for the server
  88             System.setProperty("sun.security.krb5.principal",
  89                     service3Principal);
  90 
  91             UnboundSSLUtils.startServerWithJaas(server, serverJaasConfig);
  92 
  93             // wait for the server is ready
  94             while (!server.isReady()) {
  95                 Thread.sleep(UnboundSSLUtils.DELAY);
  96             }
  97 
  98             int port = server.getPort();
  99 
 100             // connetion failure is expected
 101             // since service3 principal was specified to use by the server
 102             System.out.println("Connect: SNI hostname = " + service1host
 103                     + ", connection failure is expected");
 104             try {
 105                 SSLClient.init(UnboundSSLUtils.HOST, port,
 106                         UnboundSSLUtils.TLS_KRB5_FILTER, service1host)
 107                             .connect();
 108                 throw new RuntimeException("Test failed: "
 109                         + "expected IOException not thrown");
 110             } catch (IOException e) {
 111                 System.out.println("Expected exception: " + e);
 112             }
 113 
 114             System.out.println("Connect: SNI hostname = " + service3host
 115                     + ", successful connection is expected");
 116             SSLClient.init(UnboundSSLUtils.HOST, port,
 117                     UnboundSSLUtils.TLS_KRB5_FILTER, service3host).connect();
 118         }
 119 
 120         System.out.println("Test passed");
 121     }
 122 }