jdk/test/sun/security/krb5/auto/SaslGSS.java
changeset 17209 6f556e154816
child 18793 4d9455e24050
equal deleted inserted replaced
17208:cf76dafb155d 17209:6f556e154816
       
     1 /*
       
     2  * Copyright (c) 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 8012082
       
    27  * @summary SASL: auth-conf negotiated, but unencrypted data is accepted,
       
    28   *         reset to unencrypt
       
    29  * @compile -XDignore.symbol.file SaslGSS.java
       
    30  * @run main/othervm SaslGSS
       
    31  */
       
    32 
       
    33 import javax.security.auth.callback.Callback;
       
    34 import javax.security.auth.callback.CallbackHandler;
       
    35 import javax.security.auth.callback.UnsupportedCallbackException;
       
    36 import javax.security.sasl.AuthorizeCallback;
       
    37 import javax.security.sasl.RealmCallback;
       
    38 import javax.security.sasl.Sasl;
       
    39 import javax.security.sasl.SaslServer;
       
    40 import java.io.IOException;
       
    41 import java.util.HashMap;
       
    42 import java.util.Locale;
       
    43 import org.ietf.jgss.*;
       
    44 import sun.security.jgss.GSSUtil;
       
    45 
       
    46 public class SaslGSS {
       
    47 
       
    48     public static void main(String[] args) throws Exception {
       
    49 
       
    50         String name = "host." + OneKDC.REALM.toLowerCase(Locale.US);
       
    51 
       
    52         new OneKDC(null).writeJAASConf();
       
    53         System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
       
    54 
       
    55         // Client in JGSS so that it can control wrap privacy mode
       
    56         GSSManager m = GSSManager.getInstance();
       
    57         GSSContext sc = m.createContext(
       
    58                         m.createName(OneKDC.SERVER, GSSUtil.NT_GSS_KRB5_PRINCIPAL),
       
    59                         GSSUtil.GSS_KRB5_MECH_OID,
       
    60                         null,
       
    61                         GSSContext.DEFAULT_LIFETIME);
       
    62         sc.requestMutualAuth(false);
       
    63 
       
    64         // Server in SASL
       
    65         final HashMap props = new HashMap();
       
    66         props.put(Sasl.QOP, "auth-conf");
       
    67         SaslServer ss = Sasl.createSaslServer("GSSAPI", "server",
       
    68                 name, props,
       
    69                 new CallbackHandler() {
       
    70                     public void handle(Callback[] callbacks)
       
    71                             throws IOException, UnsupportedCallbackException {
       
    72                         for (Callback cb : callbacks) {
       
    73                             if (cb instanceof RealmCallback) {
       
    74                                 ((RealmCallback) cb).setText(OneKDC.REALM);
       
    75                             } else if (cb instanceof AuthorizeCallback) {
       
    76                                 ((AuthorizeCallback) cb).setAuthorized(true);
       
    77                             }
       
    78                         }
       
    79                     }
       
    80                 });
       
    81 
       
    82         // Handshake
       
    83         byte[] token = new byte[0];
       
    84         token = sc.initSecContext(token, 0, token.length);
       
    85         token = ss.evaluateResponse(token);
       
    86         token = sc.unwrap(token, 0, token.length, new MessageProp(0, false));
       
    87         token[0] = (byte)(((token[0] & 4) != 0) ? 4 : 2);
       
    88         token = sc.wrap(token, 0, token.length, new MessageProp(0, false));
       
    89         ss.evaluateResponse(token);
       
    90 
       
    91         // Talk
       
    92         // 1. Client sends a auth-int message
       
    93         byte[] hello = "hello".getBytes();
       
    94         MessageProp qop = new MessageProp(0, false);
       
    95         token = sc.wrap(hello, 0, hello.length, qop);
       
    96         // 2. Server accepts it anyway
       
    97         ss.unwrap(token, 0, token.length);
       
    98         // 3. Server sends a message
       
    99         token = ss.wrap(hello, 0, hello.length);
       
   100         // 4. Client accepts, should be auth-conf
       
   101         sc.unwrap(token, 0, token.length, qop);
       
   102         if (!qop.getPrivacy()) {
       
   103             throw new Exception();
       
   104         }
       
   105     }
       
   106 }