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 7110803
  27  * @summary SASL service for multiple hostnames
  28  * @modules java.base/sun.net.spi.nameservice
  29  *          java.base/sun.security.util
  30  *          java.security.jgss/sun.security.krb5
  31  *          java.security.jgss/sun.security.krb5.internal
  32  *          java.security.jgss/sun.security.krb5.internal.ccache
  33  *          java.security.jgss/sun.security.krb5.internal.crypto
  34  *          java.security.jgss/sun.security.krb5.internal.ktab
  35  *          java.security.sasl
  36  * @compile -XDignore.symbol.file SaslBasic.java
  37  * @run main/othervm SaslBasic bound
  38  * @run main/othervm SaslBasic unbound
  39  */
  40 import com.sun.security.jgss.InquireType;
  41 
  42 import java.io.IOException;
  43 import java.util.Arrays;
  44 import java.util.HashMap;
  45 import java.util.Locale;
  46 import javax.security.auth.callback.Callback;
  47 import javax.security.auth.callback.CallbackHandler;
  48 import javax.security.auth.callback.UnsupportedCallbackException;
  49 import javax.security.sasl.*;
  50 
  51 // The basic krb5 test skeleton you can copy from
  52 public class SaslBasic {
  53 
  54     public static void main(String[] args) throws Exception {
  55 
  56         boolean bound = args[0].equals("bound");
  57         String name = "host." + OneKDC.REALM.toLowerCase(Locale.US);
  58 
  59         new OneKDC(null).writeJAASConf();
  60         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  61 
  62         HashMap clntprops = new HashMap();
  63         clntprops.put(Sasl.QOP, "auth-conf");
  64         SaslClient sc = Sasl.createSaslClient(
  65                 new String[]{"GSSAPI"}, null, "server",
  66                 name, clntprops, null);
  67 
  68         final HashMap srvprops = new HashMap();
  69         srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
  70         SaslServer ss = Sasl.createSaslServer("GSSAPI", "server",
  71                 bound? name: null, srvprops,
  72                 new CallbackHandler() {
  73                     public void handle(Callback[] callbacks)
  74                             throws IOException, UnsupportedCallbackException {
  75                         for (Callback cb : callbacks) {
  76                             if (cb instanceof RealmCallback) {
  77                                 ((RealmCallback) cb).setText(OneKDC.REALM);
  78                             } else if (cb instanceof AuthorizeCallback) {
  79                                 ((AuthorizeCallback) cb).setAuthorized(true);
  80                             }
  81                         }
  82                     }
  83                 });
  84 
  85         byte[] token = new byte[0];
  86         while (!sc.isComplete() || !ss.isComplete()) {
  87             if (!sc.isComplete()) {
  88                 token = sc.evaluateChallenge(token);
  89             }
  90             if (!ss.isComplete()) {
  91                 token = ss.evaluateResponse(token);
  92             }
  93         }
  94         if (!bound) {
  95             String boundName = (String)ss.getNegotiatedProperty(
  96                     Sasl.BOUND_SERVER_NAME);
  97             if (!boundName.equals(name)) {
  98                 throw new Exception("Wrong bound server name");
  99             }
 100         }
 101         Object key = ss.getNegotiatedProperty(
 102                 "com.sun.security.jgss.inquiretype.krb5_get_session_key");
 103         if (key == null) {
 104             throw new Exception("Extended negotiated property not read");
 105         }
 106         byte[] hello = "hello".getBytes();
 107         token = sc.wrap(hello, 0, hello.length);
 108         token = ss.unwrap(token, 0, token.length);
 109         if (!Arrays.equals(hello, token)) {
 110             throw new Exception("Message altered");
 111         }
 112     }
 113 }