ReplayCacheTestProc.java 13.3 KB
Newer Older
W
weijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 7152176
 * @summary More krb5 tests
 * @library ../../../../java/security/testlibrary/
 * @compile -XDignore.symbol.file ReplayCacheTestProc.java
30
 * @run main/othervm/timeout=100 -Dsun.net.spi.nameservice.provider.1=ns,mock ReplayCacheTestProc
W
weijun 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
 */

import java.io.*;
import java.nio.BufferUnderflowException;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.util.*;
import sun.security.jgss.GSSUtil;
import sun.security.krb5.internal.APReq;
import sun.security.krb5.internal.rcache.AuthTime;

// This test runs multiple acceptor Procs to mimin AP-REQ replays.
public class ReplayCacheTestProc {

    private static Proc[] ps;
    private static Proc pc;
    private static List<Req> reqs = new ArrayList<>();
    private static String HOST = "localhost";

    // Where should the rcache be saved. It seems KRB5RCACHEDIR is not
    // recognized on Solaris. Maybe version too low? I see 1.6.
    private static String cwd =
            System.getProperty("os.name").startsWith("SunOS") ?
                "/var/krb5/rcache/" :
                System.getProperty("user.dir");


    private static int uid;

    public static void main0(String[] args) throws Exception {
        System.setProperty("java.security.krb5.conf", OneKDC.KRB5_CONF);
        if (args.length == 0) { // The controller
            int ns = 5;     // number of servers
            int nu = 5;     // number of users
            int nx = 50;    // number of experiments
            int np = 5;     // number of peers (services)
            int mode = 0;   // native(1), random(0), java(-1)
            boolean random = true;      // random experiments choreograph

74 75 76 77 78 79
            // Do not test interop with native GSS on some platforms
            String os = System.getProperty("os.name", "???");
            if (!os.startsWith("SunOS") && !os.startsWith("Linux")) {
                mode = -1;
            }

W
weijun 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
            try {
                Class<?> clazz = Class.forName(
                        "com.sun.security.auth.module.UnixSystem");
                uid = (int)(long)(Long)
                        clazz.getMethod("getUid").invoke(clazz.newInstance());
            } catch (Exception e) {
                uid = -1;
            }

            KDC kdc = KDC.create(OneKDC.REALM, HOST, 0, true);
            for (int i=0; i<nu; i++) {
                kdc.addPrincipal(user(i), OneKDC.PASS);
            }
            kdc.addPrincipalRandKey("krbtgt/" + OneKDC.REALM);
            for (int i=0; i<np; i++) {
                kdc.addPrincipalRandKey(peer(i));
            }

            kdc.writeKtab(OneKDC.KTAB);
            KDC.saveConfig(OneKDC.KRB5_CONF, kdc);

101 102 103 104 105 106 107 108
            if (mode != -1) {
                // A special native server to check basic sanity
                if (ns(-1).waitFor() != 0) {
                    Proc.d("Native mode sanity check failed, revert to java");
                    mode = -1;
                }
            }

W
weijun 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            pc = Proc.create("ReplayCacheTestProc").debug("C")
                    .args("client")
                    .start();
            ps = new Proc[ns];
            Ex[] result = new Ex[nx];

            if (!random) {
                // 2 experiments, 2 server, 1 peer, 1 user
                nx = 2; ns = 2; np = 1; nu = 1;

                // Creates reqs from user# to peer#
                req(0, 0);

                // Creates server#
                ps[0] = ns(0);
                ps[1] = js(1);

                // Runs ex# using req# to server# with expected result
                result[0] = round(0, 0, 0, true);
                result[1] = round(1, 0, 1, false);
            } else {
                Random r = new Random();
                for (int i=0; i<ns; i++) {
                    boolean useNative = (mode == 1) ? true
                            : (mode == -1 ? false : r.nextBoolean());
                    ps[i] = useNative?ns(i):js(i);
                }
                for (int i=0; i<nx; i++) {
                    result[i] = new Ex();
                    int old;    // which req to send
                    boolean expected;
                    if (reqs.isEmpty() || r.nextBoolean()) {
                        Proc.d("Console get new AP-REQ");
                        old = req(r.nextInt(nu), r.nextInt(np));
                        expected = true;
                    } else {
                        Proc.d("Console resue old");
                        old = r.nextInt(reqs.size());
                        expected = false;
                    }
                    int s = r.nextInt(ns);
                    Proc.d("Console send to " + s);
                    result[i] = round(i, old, s, expected);
                    Proc.d("Console sees " + result[i].actual);
                }
            }

            pc.println("END");
            for (int i=0; i<ns; i++) {
                ps[i].println("END");
            }
            System.out.println("Result\n======");
            boolean finalOut = true;
            for (int i=0; i<nx; i++) {
                boolean out = result[i].expected==result[i].actual;
                finalOut &= out;
                System.out.printf("%3d: %s (%2d): u%d h%d %s %s   %s %2d\n",
                        i,
                        result[i].expected?"----":"    ",
                        result[i].old,
                        result[i].user, result[i].peer, result[i].server,
                        result[i].actual?"Good":"Bad ",
                        out?"   ":"xxx",
                        result[i].csize);
            }
            if (!finalOut) throw new Exception();
175 176 177 178 179
        } else if (args[0].equals("N-1")) {
            // Native mode sanity check
            Proc.d("Detect start");
            Context s = Context.fromUserKtab("*", OneKDC.KTAB, true);
            s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
W
weijun 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        } else if (args[0].equals("client")) {
            while (true) {
                String title = Proc.textIn();
                Proc.d("Client see " + title);
                if (title.equals("END")) break;
                String[] cas = title.split(" ");
                Context c = Context.fromUserPass(cas[0], OneKDC.PASS, false);
                c.startAsClient(cas[1], GSSUtil.GSS_KRB5_MECH_OID);
                c.x().requestCredDeleg(true);
                byte[] token = c.take(new byte[0]);
                Proc.d("Client AP-REQ generated");
                Proc.binOut(token);
            }
        } else {
            Proc.d("Server start");
            Context s = Context.fromUserKtab("*", OneKDC.KTAB, true);
            Proc.d("Server login");
            while (true) {
                String title = Proc.textIn();
                Proc.d("Server " + args[0] + " sees " + title);
                if (title.equals("END")) break;
                s.startAsServer(GSSUtil.GSS_KRB5_MECH_OID);
                byte[] token = Proc.binIn();
                try {
                    s.take(token);
                    Proc.textOut("true");
                    Proc.d(args[0] + " Good");
                } catch (Exception e) {
                    Proc.textOut("false");
                    Proc.d(args[0] + " Bad");
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        try {
            main0(args);
        } catch (Exception e) {
            Proc.d(e);
            throw e;
        }
    }

    // returns the user name
    private static String user(int p) {
        return "USER" + p;
    }
    // returns the peer name
    private static String peer(int p) {
        return "host" + p + "/" + HOST;
    }
    // returns the dfl name for a host
    private static String dfl(int p) {
        return cwd + "host" + p + (uid == -1 ? "" : ("_"+uid));
    }
    // generates an ap-req and save into reqs, returns the index
    private static int req(int user, int peer) throws Exception {
        pc.println(user(user) + " " + peer(peer));
        Req req = new Req(user, peer, pc.readData());
        reqs.add(req);
        return reqs.size() - 1;
    }
    // carries out a round of experiment
    // i: ex#, old: which req, server: which server, expected: result?
    private static Ex round(int i, int old, int server, boolean expected)
            throws Exception {
        ps[server].println("TEST");
        ps[server].println(reqs.get(old).msg);
        String reply = ps[server].readData();
        Ex result = new Ex();
        result.i = i;
        result.expected = expected;
        result.server = ps[server].debug();
        result.actual = Boolean.valueOf(reply);
        result.user = reqs.get(old).user;
        result.peer = reqs.get(old).peer;
        result.old = old;
        result.csize = csize(result.peer);
        result.hash = hash(reqs.get(old).msg);
        if (new File(dfl(result.peer)).exists()) {
            Files.copy(Paths.get(dfl(result.peer)), Paths.get(
                String.format("%03d-USER%d-host%d-%s-%s",
                    i, result.user, result.peer, result.server,
                    result.actual)
                + "-" + result.hash),
                StandardCopyOption.COPY_ATTRIBUTES);
        }
        return result;
    }
    // create a native server
    private static Proc ns(int i) throws Exception {
        return Proc.create("ReplayCacheTestProc")
                .args("N"+i)
                .env("KRB5_CONFIG", OneKDC.KRB5_CONF)
                .env("KRB5_KTNAME", OneKDC.KTAB)
                .env("KRB5RCACHEDIR", cwd)
                .prop("sun.security.jgss.native", "true")
                .prop("javax.security.auth.useSubjectCredsOnly", "false")
                .prop("sun.security.nativegss.debug", "true")
280
                .prop("sun.net.spi.nameservice.provider.1", "ns,mock")
W
weijun 已提交
281 282 283 284 285 286 287 288 289 290
                .debug("N"+i)
                .start();
    }
    // creates a java server
    private static Proc js(int i) throws Exception {
        return Proc.create("ReplayCacheTestProc")
                .debug("S"+i)
                .args("S"+i)
                .prop("sun.security.krb5.rcache", "dfl")
                .prop("java.io.tmpdir", cwd)
291
                .prop("sun.net.spi.nameservice.provider.1", "ns,mock")
W
weijun 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
                .start();
    }
    // generates hash of authenticator inside ap-req inside initsectoken
    private static String hash(String req) throws Exception {
        byte[] data = Base64.getDecoder().decode(req);
        data = Arrays.copyOfRange(data, 17, data.length);
        byte[] hash = MessageDigest.getInstance("MD5").digest(new APReq(data).authenticator.getBytes());
        char[] h = new char[hash.length * 2];
        char[] hexConst = "0123456789ABCDEF".toCharArray();
        for (int i=0; i<hash.length; i++) {
            h[2*i] = hexConst[(hash[i]&0xff)>>4];
            h[2*i+1] = hexConst[hash[i]&0xf];
        }
        return new String(h);
    }
    // return size of dfl file, excluding the null hash ones
    private static int csize(int p) throws Exception {
        try (SeekableByteChannel chan = Files.newByteChannel(
                Paths.get(dfl(p)), StandardOpenOption.READ)) {
            chan.position(6);
            int cc = 0;
            while (true) {
                try {
                    if (AuthTime.readFrom(chan) != null) cc++;
                } catch (BufferUnderflowException e) {
                    break;
                }
            }
            return cc;
        } catch (IOException ioe) {
            return 0;
        }
    }
    // models an experiement
    private static class Ex {
        int i;              // #
        boolean expected;   // expected result
        boolean actual;     // actual output
        int old;            // which ap-req to send
        String server;      // which server to send to
        String hash;        // the hash of req
        int user;           // which initiator
        int peer;           // which acceptor
        int csize;          // size of rcache after test
    }
    // models a saved ap-req msg
    private static class Req {
        String msg;         // based64-ed req
        int user;           // which initiator
        int peer;           // which accceptor
        Req(int user, int peer, String msg) {
            this.msg = msg;
            this.user= user;
            this.peer = peer;
        }
    }
}