1 /*
   2  * Copyright (c) 2012, 2013, 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 8001104
  27  * @summary Unbound SASL service: the GSSAPI/krb5 mech
  28  * @compile -XDignore.symbol.file SaslUnbound.java
  29  * @run main/othervm SaslUnbound 0
  30  * @run main/othervm/fail SaslUnbound 1
  31  * @run main/othervm/fail SaslUnbound 2
  32  * @run main/othervm/fail SaslUnbound 3
  33  * @run main/othervm/fail SaslUnbound 4
  34  */
  35 import java.io.IOException;
  36 import java.util.Arrays;
  37 import java.util.HashMap;
  38 import javax.security.auth.callback.Callback;
  39 import javax.security.auth.callback.CallbackHandler;
  40 import javax.security.auth.callback.UnsupportedCallbackException;
  41 import javax.security.sasl.*;
  42 
  43 public class SaslUnbound {
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         String serverProtocol, serverName;
  48         switch (args[0].charAt(0)) {
  49             case '1':       // Using another protocol, should fail
  50                 serverProtocol = "serv";
  51                 serverName = null;
  52                 break;
  53             case '2':       // Using another protocol, should fail
  54                 serverProtocol = "otherwise";
  55                 serverName = null;
  56                 break;
  57             case '3':       // Using another protocol, should fail
  58                 serverProtocol = "otherwise";
  59                 serverName = "host." + OneKDC.REALM;
  60                 break;
  61             case '4':       // Bound to another serverName, should fail.
  62                 serverProtocol = "server";
  63                 serverName = "host2." + OneKDC.REALM;
  64                 break;
  65             default:        // Good unbound server
  66                 serverProtocol = "server";
  67                 serverName = null;
  68                 break;
  69         }
  70         new OneKDC(null).writeJAASConf();
  71         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
  72 
  73         HashMap clntprops = new HashMap();
  74         clntprops.put(Sasl.QOP, "auth-conf");
  75         SaslClient sc = Sasl.createSaslClient(
  76                 new String[]{"GSSAPI"}, null, "server",
  77                 "host." + OneKDC.REALM, clntprops, null);
  78 
  79         final HashMap srvprops = new HashMap();
  80         srvprops.put(Sasl.QOP, "auth,auth-int,auth-conf");
  81         SaslServer ss = Sasl.createSaslServer("GSSAPI", serverProtocol,
  82                 serverName, srvprops,
  83                 new CallbackHandler() {
  84                     public void handle(Callback[] callbacks)
  85                             throws IOException, UnsupportedCallbackException {
  86                         for (Callback cb : callbacks) {
  87                             if (cb instanceof RealmCallback) {
  88                                 ((RealmCallback) cb).setText(OneKDC.REALM);
  89                             } else if (cb instanceof AuthorizeCallback) {
  90                                 ((AuthorizeCallback) cb).setAuthorized(true);
  91                             }
  92                         }
  93                     }
  94                 });
  95 
  96         byte[] token = new byte[0];
  97         while (!sc.isComplete() || !ss.isComplete()) {
  98             if (!sc.isComplete()) {
  99                 token = sc.evaluateChallenge(token);
 100             }
 101             if (!ss.isComplete()) {
 102                 token = ss.evaluateResponse(token);
 103             }
 104         }
 105         System.out.println(ss.getNegotiatedProperty(Sasl.BOUND_SERVER_NAME));
 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 }