Context.java 22.8 KB
Newer Older
W
weijun 已提交
1
/*
2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
W
weijun 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 *
19 20 21
 * 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.
W
weijun 已提交
22 23 24
 */

import com.sun.security.auth.module.Krb5LoginModule;
25
import java.security.Key;
W
weijun 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosKey;
import javax.security.auth.kerberos.KerberosTicket;
import javax.security.auth.login.LoginContext;
import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.GSSName;
import org.ietf.jgss.MessageProp;
import org.ietf.jgss.Oid;
42 43
import com.sun.security.jgss.ExtendedGSSContext;
import com.sun.security.jgss.InquireType;
44
import com.sun.security.jgss.AuthorizationDataEntry;
45
import com.sun.security.jgss.ExtendedGSSCredential;
46 47
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
48
import java.security.Principal;
W
weijun 已提交
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 74 75 76 77 78

/**
 * Context of a JGSS subject, encapsulating Subject and GSSContext.
 *
 * Three "constructors", which acquire the (private) credentials and fill
 * it into the Subject:
 *
 * 1. static fromJAAS(): Creates a Context using a JAAS login config entry
 * 2. static fromUserPass(): Creates a Context using a username and a password
 * 3. delegated(): A new context which uses the delegated credentials from a
 *    previously established acceptor Context
 *
 * Two context initiators, which create the GSSContext object inside:
 *
 * 1. startAsClient()
 * 2. startAsServer()
 *
 * Privileged action:
 *    doAs(): Performs an action in the name of the Subject
 *
 * Handshake process:
 *    static handShake(initiator, acceptor)
 *
 * A four-phase typical data communication which includes all four GSS
 * actions (wrap, unwrap, getMic and veryfyMiC):
 *    static transmit(message, from, to)
 */
public class Context {

    private Subject s;
W
weijun 已提交
79
    private ExtendedGSSContext x;
W
weijun 已提交
80 81 82
    private String name;
    private GSSCredential cred;     // see static method delegated().

83 84
    static boolean usingStream = false;

W
weijun 已提交
85 86 87 88 89 90 91 92 93
    private Context() {}

    /**
     * Using the delegated credentials from a previous acceptor
     * @param c
     */
    public Context delegated() throws Exception {
        Context out = new Context();
        out.s = s;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        try {
            out.cred = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() {
                @Override
                public GSSCredential run() throws Exception {
                    GSSCredential cred = x.getDelegCred();
                    if (cred == null && x.getCredDelegState() ||
                            cred != null && !x.getCredDelegState()) {
                        throw new Exception("getCredDelegState not match");
                    }
                    return cred;
                }
            });
        } catch (PrivilegedActionException pae) {
            throw pae.getException();
        }
W
weijun 已提交
109 110 111 112
        out.name = name + " as " + out.cred.getName().toString();
        return out;
    }

W
weijun 已提交
113 114 115 116 117 118 119 120 121
    /**
     * No JAAS login at all, can be used to test JGSS without JAAS
     */
    public static Context fromThinAir() throws Exception {
        Context out = new Context();
        out.s = new Subject();
        return out;
    }

W
weijun 已提交
122 123 124 125 126 127 128 129 130 131 132 133
    /**
     * Logins with a JAAS login config entry name
     */
    public static Context fromJAAS(final String name) throws Exception {
        Context out = new Context();
        out.name = name;
        LoginContext lc = new LoginContext(name);
        lc.login();
        out.s = lc.getSubject();
        return out;
    }

134 135 136
    /**
     * Logins with username/password as a new Subject
     */
W
weijun 已提交
137 138
    public static Context fromUserPass(
            String user, char[] pass, boolean storeKey) throws Exception {
139
        return fromUserPass(new Subject(), user, pass, storeKey);
W
weijun 已提交
140
    }
W
weijun 已提交
141

W
weijun 已提交
142
    /**
143 144 145
     * Logins with username/password as an existing Subject. The
     * same subject can be used multiple times to simulate multiple logins.
     * @param s existing subject
W
weijun 已提交
146
     */
W
weijun 已提交
147 148
    public static Context fromUserPass(Subject s,
            String user, char[] pass, boolean storeKey) throws Exception {
W
weijun 已提交
149 150
        Context out = new Context();
        out.name = user;
151
        out.s = s;
W
weijun 已提交
152
        Krb5LoginModule krb5 = new Krb5LoginModule();
153 154
        Map<String, String> map = new HashMap<>();
        Map<String, Object> shared = new HashMap<>();
155 156 157 158 159 160 161 162 163 164 165 166

        if (pass != null) {
            map.put("useFirstPass", "true");
            shared.put("javax.security.auth.login.name", user);
            shared.put("javax.security.auth.login.password", pass);
        } else {
            map.put("doNotPrompt", "true");
            map.put("useTicketCache", "true");
            if (user != null) {
                map.put("principal", user);
            }
        }
W
weijun 已提交
167 168 169 170 171 172 173 174 175 176
        if (storeKey) {
            map.put("storeKey", "true");
        }

        krb5.initialize(out.s, null, shared, map);
        krb5.login();
        krb5.commit();
        return out;
    }

177
    /**
178 179 180
     * Logins with username/keytab as an existing Subject. The
     * same subject can be used multiple times to simulate multiple logins.
     * @param s existing subject
181
     */
182 183 184 185 186 187 188 189 190 191
    public static Context fromUserKtab(
            String user, String ktab, boolean storeKey) throws Exception {
        return fromUserKtab(new Subject(), user, ktab, storeKey);
    }

    /**
     * Logins with username/keytab as a new subject,
     */
    public static Context fromUserKtab(Subject s,
            String user, String ktab, boolean storeKey) throws Exception {
192 193
        Context out = new Context();
        out.name = user;
194
        out.s = s;
195
        Krb5LoginModule krb5 = new Krb5LoginModule();
196
        Map<String, String> map = new HashMap<>();
197

W
weijun 已提交
198
        map.put("isInitiator", "false");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        map.put("doNotPrompt", "true");
        map.put("useTicketCache", "false");
        map.put("useKeyTab", "true");
        map.put("keyTab", ktab);
        map.put("principal", user);
        if (storeKey) {
            map.put("storeKey", "true");
        }

        krb5.initialize(out.s, null, null, map);
        krb5.login();
        krb5.commit();
        return out;
    }

W
weijun 已提交
214 215 216 217 218 219 220 221 222 223 224
    /**
     * Starts as a client
     * @param target communication peer
     * @param mech GSS mech
     * @throws java.lang.Exception
     */
    public void startAsClient(final String target, final Oid mech) throws Exception {
        doAs(new Action() {
            @Override
            public byte[] run(Context me, byte[] dummy) throws Exception {
                GSSManager m = GSSManager.getInstance();
W
weijun 已提交
225 226
                me.x = (ExtendedGSSContext)m.createContext(
                          target.indexOf('@') < 0 ?
W
weijun 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
                            m.createName(target, null) :
                            m.createName(target, GSSName.NT_HOSTBASED_SERVICE),
                        mech,
                        cred,
                        GSSContext.DEFAULT_LIFETIME);
                return null;
            }
        }, null);
    }

    /**
     * Starts as a server
     * @param mech GSS mech
     * @throws java.lang.Exception
     */
    public void startAsServer(final Oid mech) throws Exception {
243
        startAsServer(null, mech, false);
W
weijun 已提交
244 245
    }

246 247 248
    public void startAsServer(final String name, final Oid mech) throws Exception {
        startAsServer(name, mech, false);
    }
W
weijun 已提交
249 250 251 252 253 254
    /**
     * Starts as a server with the specified service name
     * @param name the service name
     * @param mech GSS mech
     * @throws java.lang.Exception
     */
255
    public void startAsServer(final String name, final Oid mech, final boolean asInitiator) throws Exception {
W
weijun 已提交
256 257 258 259
        doAs(new Action() {
            @Override
            public byte[] run(Context me, byte[] dummy) throws Exception {
                GSSManager m = GSSManager.getInstance();
260
                me.cred = m.createCredential(
W
weijun 已提交
261 262 263 264
                        name == null ? null :
                          (name.indexOf('@') < 0 ?
                            m.createName(name, null) :
                            m.createName(name, GSSName.NT_HOSTBASED_SERVICE)),
W
weijun 已提交
265 266
                        GSSCredential.INDEFINITE_LIFETIME,
                        mech,
267 268 269 270
                        asInitiator?
                                GSSCredential.INITIATE_AND_ACCEPT:
                                GSSCredential.ACCEPT_ONLY);
                me.x = (ExtendedGSSContext)m.createContext(me.cred);
W
weijun 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
                return null;
            }
        }, null);
    }

    /**
     * Accesses the internal GSSContext object. Currently it's used for --
     *
     * 1. calling requestXXX() before handshake
     * 2. accessing source name
     *
     * Note: If the application needs to do any privileged call on this
     * object, please use doAs(). Otherwise, it can be done directly. The
     * methods listed above are all non-privileged calls.
     *
     * @return the GSSContext object
     */
W
weijun 已提交
288
    public ExtendedGSSContext x() {
W
weijun 已提交
289 290 291
        return x;
    }

W
weijun 已提交
292 293 294 295 296 297 298 299
    /**
     * Accesses the internal subject.
     * @return the subject
     */
    public Subject s() {
        return s;
    }

300 301 302 303 304 305 306
    /**
     * Returns the cred inside, if there is one
     */
    public GSSCredential cred() {
        return cred;
    }

W
weijun 已提交
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    /**
     * Disposes the GSSContext within
     * @throws org.ietf.jgss.GSSException
     */
    public void dispose() throws GSSException {
        x.dispose();
    }

    /**
     * Does something using the Subject inside
     * @param action the action
     * @param in the input byte
     * @return the output byte
     * @throws java.lang.Exception
     */
    public byte[] doAs(final Action action, final byte[] in) throws Exception {
        try {
            return Subject.doAs(s, new PrivilegedExceptionAction<byte[]>() {

                @Override
                public byte[] run() throws Exception {
                    return action.run(Context.this, in);
                }
            });
        } catch (PrivilegedActionException pae) {
            throw pae.getException();
        }
    }

    /**
     * Prints status of GSSContext and Subject
     * @throws java.lang.Exception
     */
    public void status() throws Exception {
        System.out.println("STATUS OF " + name.toUpperCase());
        try {
            StringBuffer sb = new StringBuffer();
            if (x.getAnonymityState()) {
                sb.append("anon, ");
            }
            if (x.getConfState()) {
                sb.append("conf, ");
            }
            if (x.getCredDelegState()) {
                sb.append("deleg, ");
            }
            if (x.getIntegState()) {
                sb.append("integ, ");
            }
            if (x.getMutualAuthState()) {
                sb.append("mutual, ");
            }
            if (x.getReplayDetState()) {
                sb.append("rep det, ");
            }
            if (x.getSequenceDetState()) {
                sb.append("seq det, ");
            }
W
weijun 已提交
365 366 367 368 369
            if (x instanceof ExtendedGSSContext) {
                if (((ExtendedGSSContext)x).getDelegPolicyState()) {
                    sb.append("deleg policy, ");
                }
            }
W
weijun 已提交
370 371 372 373 374
            System.out.println("Context status of " + name + ": " + sb.toString());
            System.out.println(x.getSrcName() + " -> " + x.getTargName());
        } catch (Exception e) {
            ;// Don't care
        }
375 376 377 378 379 380 381
        if (s != null) {
            System.out.println("====== START SUBJECT CONTENT =====");
            for (Principal p: s.getPrincipals()) {
                System.out.println("    Principal: " + p);
            }
            for (Object o : s.getPublicCredentials()) {
                System.out.println("    " + o.getClass());
W
weijun 已提交
382
                System.out.println("        " + o);
W
weijun 已提交
383
            }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
            System.out.println("====== Private Credentials Set ======");
            for (Object o : s.getPrivateCredentials()) {
                System.out.println("    " + o.getClass());
                if (o instanceof KerberosTicket) {
                    KerberosTicket kt = (KerberosTicket) o;
                    System.out.println("        " + kt.getServer() + " for " + kt.getClient());
                } else if (o instanceof KerberosKey) {
                    KerberosKey kk = (KerberosKey) o;
                    System.out.print("        " + kk.getKeyType() + " " + kk.getVersionNumber() + " " + kk.getAlgorithm() + " ");
                    for (byte b : kk.getEncoded()) {
                        System.out.printf("%02X", b & 0xff);
                    }
                    System.out.println();
                } else if (o instanceof Map) {
                    Map map = (Map) o;
                    for (Object k : map.keySet()) {
                        System.out.println("        " + k + ": " + map.get(k));
                    }
                } else {
                    System.out.println("        " + o);
                }
            }
            System.out.println("====== END SUBJECT CONTENT =====");
W
weijun 已提交
407
        }
408 409 410 411 412 413 414 415 416
        if (x != null && x instanceof ExtendedGSSContext) {
            if (x.isEstablished()) {
                ExtendedGSSContext ex = (ExtendedGSSContext)x;
                Key k = (Key)ex.inquireSecContext(
                        InquireType.KRB5_GET_SESSION_KEY);
                if (k == null) {
                    throw new Exception("Session key cannot be null");
                }
                System.out.println("Session key is: " + k);
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                boolean[] flags = (boolean[])ex.inquireSecContext(
                        InquireType.KRB5_GET_TKT_FLAGS);
                if (flags == null) {
                    throw new Exception("Ticket flags cannot be null");
                }
                System.out.println("Ticket flags is: " + Arrays.toString(flags));
                String authTime = (String)ex.inquireSecContext(
                        InquireType.KRB5_GET_AUTHTIME);
                if (authTime == null) {
                    throw new Exception("Auth time cannot be null");
                }
                System.out.println("AuthTime is: " + authTime);
                if (!x.isInitiator()) {
                    AuthorizationDataEntry[] ad = (AuthorizationDataEntry[])ex.inquireSecContext(
                            InquireType.KRB5_GET_AUTHZ_DATA);
                    System.out.println("AuthzData is: " + Arrays.toString(ad));
                }
434 435
            }
        }
W
weijun 已提交
436 437
    }

438 439 440
    public byte[] wrap(byte[] t, final boolean privacy)
            throws Exception {
        return doAs(new Action() {
W
weijun 已提交
441
            @Override
442 443 444
            public byte[] run(Context me, byte[] input) throws Exception {
                System.out.printf("wrap %s privacy from %s: ", privacy?"with":"without", me.name);
                MessageProp p1 = new MessageProp(0, privacy);
445 446 447
                byte[] out;
                if (usingStream) {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
448
                    me.x.wrap(new ByteArrayInputStream(input), os, p1);
449 450
                    out = os.toByteArray();
                } else {
451
                    out = me.x.wrap(input, 0, input.length, p1);
452
                }
W
weijun 已提交
453 454 455
                System.out.println(printProp(p1));
                return out;
            }
456 457
        }, t);
    }
W
weijun 已提交
458

459 460 461
    public byte[] unwrap(byte[] t, final boolean privacy)
            throws Exception {
        return doAs(new Action() {
W
weijun 已提交
462 463
            @Override
            public byte[] run(Context me, byte[] input) throws Exception {
464 465
                System.out.printf("unwrap %s privacy from %s: ", privacy?"with":"without", me.name);
                MessageProp p1 = new MessageProp(0, privacy);
466 467 468 469 470 471 472 473
                byte[] bytes;
                if (usingStream) {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    me.x.unwrap(new ByteArrayInputStream(input), os, p1);
                    bytes = os.toByteArray();
                } else {
                    bytes = me.x.unwrap(input, 0, input.length, p1);
                }
W
weijun 已提交
474
                System.out.println(printProp(p1));
475 476 477 478 479 480 481 482 483 484 485
                return bytes;
            }
        }, t);
    }

    public byte[] getMic(byte[] t) throws Exception {
        return doAs(new Action() {
            @Override
            public byte[] run(Context me, byte[] input) throws Exception {
                MessageProp p1 = new MessageProp(0, true);
                byte[] bytes;
W
weijun 已提交
486
                p1 = new MessageProp(0, true);
487
                System.out.printf("getMic from %s: ", me.name);
488 489
                if (usingStream) {
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
490
                    me.x.getMIC(new ByteArrayInputStream(input), os, p1);
491 492
                    bytes = os.toByteArray();
                } else {
493
                    bytes = me.x.getMIC(input, 0, input.length, p1);
494
                }
W
weijun 已提交
495 496 497 498
                System.out.println(printProp(p1));
                return bytes;
            }
        }, t);
499
    }
500

501 502
    public void verifyMic(byte[] t, final byte[] msg) throws Exception {
        doAs(new Action() {
W
weijun 已提交
503 504 505
            @Override
            public byte[] run(Context me, byte[] input) throws Exception {
                MessageProp p1 = new MessageProp(0, true);
506
                System.out.printf("verifyMic from %s: ", me.name);
507 508
                if (usingStream) {
                    me.x.verifyMIC(new ByteArrayInputStream(input),
509
                            new ByteArrayInputStream(msg), p1);
510 511
                } else {
                    me.x.verifyMIC(input, 0, input.length,
512
                            msg, 0, msg.length,
513 514
                            p1);
                }
W
weijun 已提交
515 516 517 518 519 520
                System.out.println(printProp(p1));
                return null;
            }
        }, t);
    }

521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
    /**
     * Transmits a message from one Context to another. The sender wraps the
     * message and sends it to the receiver. The receiver unwraps it, creates
     * a MIC of the clear text and sends it back to the sender. The sender
     * verifies the MIC against the message sent earlier.
     * @param message the message
     * @param s1 the sender
     * @param s2 the receiver
     * @throws java.lang.Exception If anything goes wrong
     */
    static public void transmit(final String message, final Context s1,
            final Context s2) throws Exception {
        final byte[] messageBytes = message.getBytes();
        System.out.printf("-------------------- TRANSMIT from %s to %s------------------------\n",
                s1.name, s2.name);
        byte[] wrapped = s1.wrap(messageBytes, true);
        byte[] unwrapped = s2.unwrap(wrapped, true);
        if (!Arrays.equals(messageBytes, unwrapped)) {
            throw new Exception("wrap/unwrap mismatch");
        }
        byte[] mic = s2.getMic(unwrapped);
        s1.verifyMic(mic, messageBytes);
    }

W
weijun 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558
    /**
     * Returns a string description of a MessageProp object
     * @param prop the object
     * @return the description
     */
    static public String printProp(MessageProp prop) {
        StringBuffer sb = new StringBuffer();
        sb.append("MessagePop: ");
        sb.append("QOP="+ prop.getQOP() + ", ");
        sb.append(prop.getPrivacy()?"privacy, ":"");
        sb.append(prop.isDuplicateToken()?"dup, ":"");
        sb.append(prop.isGapToken()?"gap, ":"");
        sb.append(prop.isOldToken()?"old, ":"");
        sb.append(prop.isUnseqToken()?"unseq, ":"");
559 560 561
        if (prop.getMinorStatus() != 0) {
            sb.append(prop.getMinorString()+ "(" + prop.getMinorStatus()+")");
        }
W
weijun 已提交
562 563 564
        return sb.toString();
    }

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    public Context impersonate(final String someone) throws Exception {
        try {
            GSSCredential creds = Subject.doAs(s, new PrivilegedExceptionAction<GSSCredential>() {
                @Override
                public GSSCredential run() throws Exception {
                    GSSManager m = GSSManager.getInstance();
                    GSSName other = m.createName(someone, GSSName.NT_USER_NAME);
                    if (Context.this.cred == null) {
                        Context.this.cred = m.createCredential(GSSCredential.INITIATE_ONLY);
                    }
                    return ((ExtendedGSSCredential)Context.this.cred).impersonate(other);
                }
            });
            Context out = new Context();
            out.s = s;
            out.cred = creds;
            out.name = name + " as " + out.cred.getName().toString();
            return out;
        } catch (PrivilegedActionException pae) {
            throw pae.getException();
        }
    }

W
weijun 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600
    public byte[] take(final byte[] in) throws Exception {
        return doAs(new Action() {
            @Override
            public byte[] run(Context me, byte[] input) throws Exception {
                if (me.x.isEstablished()) {
                    System.out.println(name + " side established");
                    if (input != null) {
                        throw new Exception("Context established but " +
                                "still receive token at " + name);
                    }
                    return null;
                } else {
                    if (me.x.isInitiator()) {
601
                        System.out.println(name + " call initSecContext");
W
weijun 已提交
602 603
                        return me.x.initSecContext(input, 0, input.length);
                    } else {
604
                        System.out.println(name + " call acceptSecContext");
W
weijun 已提交
605 606 607 608 609 610 611
                        return me.x.acceptSecContext(input, 0, input.length);
                    }
                }
            }
        }, in);
    }

W
weijun 已提交
612 613 614 615 616 617 618 619
    /**
     * Handshake (security context establishment process) between two Contexts
     * @param c the initiator
     * @param s the acceptor
     * @throws java.lang.Exception
     */
    static public void handshake(final Context c, final Context s) throws Exception {
        byte[] t = new byte[0];
W
weijun 已提交
620 621 622 623
        while (true) {
            if (t != null || !c.x.isEstablished()) t = c.take(t);
            if (t != null || !s.x.isEstablished()) t = s.take(t);
            if (c.x.isEstablished() && s.x.isEstablished()) break;
W
weijun 已提交
624 625 626
        }
    }
}