jdk/test/sun/security/krb5/auto/KDC.java
author weijun
Tue, 09 Jun 2009 14:17:05 +0800
changeset 2942 37d9baeb7518
parent 1575 e0f1979051b5
child 3046 dd50d75d88e6
permissions -rw-r--r--
6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication() Reviewed-by: chegar, valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
     2
 * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     4
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     8
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    13
 * accompanied this code).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    14
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    18
 *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    21
 * have any questions.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    22
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    23
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    24
import java.lang.reflect.Constructor;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    25
import java.lang.reflect.Field;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    26
import java.lang.reflect.InvocationTargetException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    27
import java.net.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    28
import java.io.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    29
import java.lang.reflect.Method;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    30
import java.security.SecureRandom;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    31
import java.util.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    32
import java.util.concurrent.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    33
import sun.security.krb5.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    34
import sun.security.krb5.internal.*;
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
    35
import sun.security.krb5.internal.ccache.CredentialsCache;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    36
import sun.security.krb5.internal.crypto.KeyUsage;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    37
import sun.security.krb5.internal.ktab.KeyTab;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    38
import sun.security.util.DerInputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    39
import sun.security.util.DerOutputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import sun.security.util.DerValue;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    41
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    42
/**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    43
 * A KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    44
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    45
 * Features:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    46
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    47
 * <li> Supports TCP and UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    48
 * <li> Supports AS-REQ and TGS-REQ
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
 * <li> Principal db and other settings hard coded in application
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
 * <li> Options, say, request preauth or not
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    51
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    52
 * Side effects:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    53
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    54
 * <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    55
 * singleton and initialized according to Kerberos settings (krb5.conf and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
 * java.security.krb5.* system properties). This means once it's initialized
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
 * it will not automatically notice any changes to these settings (or file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
 * changes of krb5.conf). The KDC class normally does not touch these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    59
 * settings (except for the <code>writeKtab()</code> method). However, to make
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    60
 * sure nothing ever goes wrong, if you want to make any changes to these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
 * settings after calling a KDC method, call <code>Config.refresh()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
 * make sure your changes are reflected in the <code>Config</code> object.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
 * Issues and TODOs:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    66
 * <li> Generates krb5.conf to be used on another machine, currently the kdc is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    67
 * always localhost
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    68
 * <li> More options to KDC, say, error output, say, response nonce !=
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    69
 * request nonce
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    70
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    71
 * Note: This program uses internal krb5 classes (including reflection to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    72
 * access private fields and methods).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    73
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
 * Usages:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    75
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    76
 * 1. Init and start the KDC:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    77
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    78
 * KDC kdc = KDC.create("REALM.NAME", port, isDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    79
 * KDC kdc = KDC.create("REALM.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
 * Here, <code>port</code> is the UDP and TCP port number the KDC server
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
 * listens on. If zero, a random port is chosen, which you can use getPort()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    83
 * later to retrieve the value.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    84
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
 * If <code>isDaemon</code> is true, the KDC worker threads will be daemons.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
 * The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
 * isDaemon=false, and is commonly used in an embedded KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
 * 2. Adding users:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
 * kdc.addPrincipal(String principal_name, char[] password);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
 * kdc.addPrincipalRandKey(String principal_name);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    94
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    95
 * A service principal's name should look like "host/f.q.d.n". The second form
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    96
 * generates a random key. To expose this key, call <code>writeKtab()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    97
 * save the keys into a keytab file.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    98
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    99
 * Note that you need to add the principal name krbtgt/REALM.NAME yourself.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   100
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   101
 * Note that you can safely add a principal at any time after the KDC is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   102
 * started and before a user requests info on this principal.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   103
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   104
 * 3. Other public methods:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   105
 * <ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   106
 * <li> <code>getPort</code>: Returns the port number the KDC uses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   107
 * <li> <code>getRealm</code>: Returns the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   108
 * <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   109
 * <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   110
 * <li> <code>setOption</code>: Sets various options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   111
 * </ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   112
 * Read the javadoc for details. Lazy developer can use <code>OneKDC</code>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   113
 * directly.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   114
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   115
public class KDC {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   116
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   117
    // Under the hood.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   118
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   119
    // The random generator to generate random keys (including session keys)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   120
    private static SecureRandom secureRandom = new SecureRandom();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   121
    // Principal db
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   122
    private Map<String,char[]> passwords = new HashMap<String,char[]>();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   123
    // Realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   124
    private String realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   125
    // The request/response job queue
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   126
    private BlockingQueue<Job> q = new ArrayBlockingQueue<Job>(100);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   127
    // Service port number
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   128
    private int port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   129
    // Options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   130
    private Map<Option,Object> options = new HashMap<Option,Object>();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   131
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   132
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   133
     * Option names, to be expanded forever.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   134
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   135
    public static enum Option {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   136
        /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   137
         * Whether pre-authentication is required. Default Boolean.TRUE
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   138
         */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   139
        PREAUTH_REQUIRED,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   140
    };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   141
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   142
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   143
     * A standalone KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   144
     * @param args
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   145
     * @throws java.lang.Exception
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   146
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   147
    public static void main(String[] args) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   148
        if (args.length > 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   149
            if (args[0].equals("-help") || args[0].equals("--help")) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   150
                System.out.println("Usage:");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   151
                System.out.println("   java " + KDC.class + "       " +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   152
                        "Start KDC on port 8888");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   153
                return;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   154
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   155
        }
1456
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   156
        String localhost = "localhost";
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   157
        try {
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   158
            localhost = InetAddress.getByName(localhost)
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   159
                    .getCanonicalHostName();
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   160
        } catch (UnknownHostException uhe) {
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   161
            ;   // Ignore, localhost is still "localhost"
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   162
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   163
        KDC kdc = create("RABBIT.HOLE", 8888, false);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   164
        kdc.addPrincipal("dummy", "bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   165
        kdc.addPrincipal("foo", "bar".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   166
        kdc.addPrincipalRandKey("krbtgt/" + kdc.realm);
1456
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   167
        kdc.addPrincipalRandKey("server/" + localhost);
1d3c6724de2f 6761072: new krb5 tests fail on multiple platforms
weijun
parents: 1454
diff changeset
   168
        kdc.addPrincipalRandKey("backend/" + localhost);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   169
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   170
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   171
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   172
     * Creates and starts a KDC running as a daemon on a random port.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   173
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   174
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   175
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   176
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   177
    public static KDC create(String realm) throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   178
        return create(realm, 0, true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   179
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   180
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   181
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   182
     * Creates and starts a KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   183
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   184
     * @param port the TCP and UDP port to listen to. A random port will to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   185
     *        chosen if zero.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   186
     * @param asDaemon if true, KDC threads will be daemons. Otherwise, not.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   187
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   188
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   189
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   190
    public static KDC create(String realm, int port, boolean asDaemon) throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   191
        return new KDC(realm, port, asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   192
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   193
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   194
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   195
     * Sets an option
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   196
     * @param key the option name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   197
     * @param obj the value
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   198
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   199
    public void setOption(Option key, Object value) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   200
        options.put(key, value);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   201
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   202
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   203
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   204
     * Write all principals' keys from multiple KDCsinto one keytab file.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   205
     * Note that the keys for the krbtgt principals will not be written.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   206
     * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   207
     * Attention: This method references krb5.conf settings. If you need to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   208
     * setup krb5.conf later, please call <code>Config.refresh()</code> after
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   209
     * the new setting. For example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   210
     * <pre>
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   211
     * KDC.writeKtab("/etc/kdc/ktab", kdc);  // Config is initialized,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   212
     * System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   213
     * Config.refresh();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   214
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   215
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   216
     * Inside this method there are 2 places krb5.conf is used:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   217
     * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   218
     * <li> (Fatal) Generating keys: EncryptionKey.acquireSecretKeys
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   219
     * <li> (Has workaround) Creating PrincipalName
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   220
     * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   221
     * @param tab The keytab filename to write to.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   222
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   223
     * @throws sun.security.krb5.KrbException for any realm and/or principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   224
     *         name error.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   225
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   226
    public static void writeMultiKtab(String tab, KDC... kdcs)
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   227
            throws IOException, KrbException {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   228
        KeyTab ktab = KeyTab.create(tab);
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   229
        for (KDC kdc: kdcs) {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   230
            for (String name : kdc.passwords.keySet()) {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   231
                if (name.equals("krbtgt/" + kdc.realm)) {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   232
                    continue;
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   233
                }
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   234
                ktab.addEntry(new PrincipalName(name + "@" + kdc.realm,
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   235
                        name.indexOf('/') < 0 ?
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   236
                            PrincipalName.KRB_NT_UNKNOWN :
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   237
                            PrincipalName.KRB_NT_SRV_HST),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   238
                            kdc.passwords.get(name));
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   239
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   240
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   241
        ktab.save();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   242
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   243
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   244
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   245
     * Write a ktab for this KDC.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   246
     */
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   247
    public void writeKtab(String tab) throws IOException, KrbException {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   248
        KDC.writeMultiKtab(tab, this);
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   249
    }
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   250
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   251
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   252
     * Adds a new principal to this realm with a given password.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   253
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   254
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   255
     * @param pass the password for the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   256
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   257
    public void addPrincipal(String user, char[] pass) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   258
        passwords.put(user, pass);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   259
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   260
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   261
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   262
     * Adds a new principal to this realm with a random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   263
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   264
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   265
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   266
    public void addPrincipalRandKey(String user) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   267
        passwords.put(user, randomPassword());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   268
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   269
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   270
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   271
     * Returns the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   272
     * @return the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   273
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   274
    public String getRealm() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   275
        return realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   276
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   277
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   278
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   279
     * Writes a krb5.conf for one or more KDC that includes KDC locations for
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   280
     * each realm and the default realm name. You can also add extra strings
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   281
     * into the file. The method should be called like:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   282
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   283
     *   KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   284
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   285
     * Here you can provide one or more kdc# and zero or more line# arguments.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   286
     * The line# will be put after [libdefaults] and before [realms]. Therefore
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   287
     * you can append new lines into [libdefaults] and/or create your new
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   288
     * stanzas as well. Note that a newline character will be appended to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   289
     * each line# argument.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   290
     * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   291
     * For example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   292
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   293
     * KDC.saveConfig("krb5.conf", this);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   294
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   295
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   296
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   297
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   298
     * default_realm = REALM.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   299
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   300
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   301
     *   REALM.NAME = {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   302
     *     kdc = localhost:port_number
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   303
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   304
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   305
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   306
     * Another example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   307
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   308
     * KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   309
     *         "[domain_realm]",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   310
     *         ".kdc1.com = KDC1.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   311
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   312
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   313
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   314
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   315
     * default_realm = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   316
     * forwardable = true
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   317
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   318
     * [domain_realm]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   319
     * .kdc1.com = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   320
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   321
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   322
     *   KDC1.NAME = {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   323
     *     kdc = localhost:port1
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   324
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   325
     *   KDC2.NAME = {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   326
     *     kdc = localhost:port2
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   327
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   328
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   329
     * @param file the name of the file to write into
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   330
     * @param kdc the first (and default) KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   331
     * @param more more KDCs or extra lines (in their appearing order) to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   332
     * insert into the krb5.conf file. This method reads each argument's type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   333
     * to determine what it's for. This argument can be empty.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   334
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   335
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   336
    public static void saveConfig(String file, KDC kdc, Object... more)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   337
            throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   338
        File f = new File(file);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   339
        StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   340
        sb.append("[libdefaults]\ndefault_realm = ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   341
        sb.append(kdc.realm);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   342
        sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   343
        for (Object o: more) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   344
            if (o instanceof String) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   345
                sb.append(o);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   346
                sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   347
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   348
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   349
        sb.append("\n[realms]\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   350
        sb.append(realmLineForKDC(kdc));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   351
        for (Object o: more) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   352
            if (o instanceof KDC) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   353
                sb.append(realmLineForKDC((KDC)o));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   354
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   355
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   356
        FileOutputStream fos = new FileOutputStream(f);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   357
        fos.write(sb.toString().getBytes());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   358
        fos.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   359
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   360
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   361
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   362
     * Returns the service port of the KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   363
     * @return the KDC service port
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   364
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   365
    public int getPort() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   366
        return port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   367
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   368
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   369
    // Private helper methods
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   370
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   371
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   372
     * Private constructor, cannot be called outside.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   373
     * @param realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   374
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   375
    private KDC(String realm) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   376
        this.realm = realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   377
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   378
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   379
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   380
     * A constructor that starts the KDC service also.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   381
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   382
    protected KDC(String realm, int port, boolean asDaemon)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   383
            throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   384
        this(realm);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   385
        startServer(port, asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   386
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   387
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   388
     * Generates a 32-char random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   389
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   390
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   391
    private static char[] randomPassword() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   392
        char[] pass = new char[32];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   393
        for (int i=0; i<32; i++)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   394
            pass[i] = (char)secureRandom.nextInt();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   395
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   396
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   397
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   398
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   399
     * Generates a random key for the given encryption type.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   400
     * @param eType the encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   401
     * @return the generated key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   402
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   403
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   404
    private static EncryptionKey generateRandomKey(int eType)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   405
            throws KrbException  {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   406
        // Is 32 enough for AES256? I should have generated the keys directly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   407
        // but different cryptos have different rules on what keys are valid.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   408
        char[] pass = randomPassword();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   409
        String algo;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   410
        switch (eType) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   411
            case EncryptedData.ETYPE_DES_CBC_MD5: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   412
            case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: algo = "DESede"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   413
            case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: algo = "AES128"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   414
            case EncryptedData.ETYPE_ARCFOUR_HMAC: algo = "ArcFourHMAC"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   415
            case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: algo = "AES256"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   416
            default: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   417
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   418
        return new EncryptionKey(pass, "NOTHING", algo);    // Silly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   419
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   420
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   421
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   422
     * Returns the password for a given principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   423
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   424
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   425
     * @throws sun.security.krb5.KrbException when the principal is not inside
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   426
     *         the database.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   427
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   428
    private char[] getPassword(PrincipalName p) throws KrbException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   429
        char[] pass = passwords.get(p.getNameString());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   430
        if (pass == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   431
            throw new KrbException(Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   432
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   433
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   434
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   435
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   436
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   437
     * Returns the salt string for the principal. For normal users, the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   438
     * concatenation for the realm name and the sections of the principal;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   439
     * for krgtgt/A@B and krbtgt/B@A, always return AB (so that inter-realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   440
     * principals have the same key).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   441
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   442
     * @return the salt
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   443
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   444
    private String getSalt(PrincipalName p) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   445
        String[] ns = p.getNameStrings();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   446
        if (ns[0].equals("krbtgt") && ns.length > 1) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   447
            // Shared cross-realm keys must be the same
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   448
            if (ns[1].compareTo(realm) < 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   449
                return ns[1] + realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   450
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   451
                return realm + ns[1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   452
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   453
        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   454
            String s = getRealm();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   455
            for (String n: p.getNameStrings()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   456
                s += n;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   457
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   458
            return s;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   459
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   460
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   461
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   462
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   463
     * Returns the key for a given principal of the given encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   464
     * @param p the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   465
     * @param etype the encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   466
     * @return the key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   467
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   468
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   469
    private EncryptionKey keyForUser(PrincipalName p, int etype) throws KrbException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   470
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   471
            // Do not call EncryptionKey.acquireSecretKeys(), otherwise
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   472
            // the krb5.conf config file would be loaded.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   473
            Method stringToKey = EncryptionKey.class.getDeclaredMethod("stringToKey", char[].class, String.class, byte[].class, Integer.TYPE);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   474
            stringToKey.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   475
            return new EncryptionKey((byte[]) stringToKey.invoke(null, getPassword(p), getSalt(p), null, etype), etype, null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   476
        } catch (InvocationTargetException ex) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   477
            KrbException ke = (KrbException)ex.getCause();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   478
            throw ke;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   479
        } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   480
            throw new RuntimeException(e);  // should not happen
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   481
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   482
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   483
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   484
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   485
     * Processes an incoming request and generates a response.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   486
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   487
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   488
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   489
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   490
    private byte[] processMessage(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   491
        if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   492
            return processAsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   493
        else
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   494
            return processTgsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   495
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   496
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   497
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   498
     * Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   499
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   500
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   501
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   502
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   503
    private byte[] processTgsReq(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   504
        TGSReq tgsReq = new TGSReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   505
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   506
            System.out.println(realm + "> " + tgsReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   507
                    " sends TGS-REQ for " +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   508
                    tgsReq.reqBody.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   509
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   510
            int etype = 0;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   511
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   512
            // Reflection: PAData[] pas = tgsReq.pAData;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   513
            Field f = KDCReq.class.getDeclaredField("pAData");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   514
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   515
            PAData[] pas = (PAData[])f.get(tgsReq);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   516
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   517
            Ticket tkt = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   518
            EncTicketPart etp = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   519
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   520
                throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   521
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   522
                for (PAData pa: pas) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   523
                    if (pa.getType() == Krb5.PA_TGS_REQ) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   524
                        APReq apReq = new APReq(pa.getValue());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   525
                        EncryptedData ed = apReq.authenticator;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   526
                        tkt = apReq.ticket;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   527
                        etype = tkt.encPart.getEType();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   528
                        EncryptionKey kkey = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   529
                        if (!tkt.realm.toString().equals(realm)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   530
                            if (tkt.sname.getNameString().equals("krbtgt/" + realm)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   531
                                kkey = keyForUser(new PrincipalName("krbtgt/" + tkt.realm.toString(), realm), etype);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   532
                            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   533
                        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   534
                            kkey = keyForUser(tkt.sname, etype);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   535
                        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   536
                        byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   537
                        DerInputStream derIn = new DerInputStream(bb);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   538
                        DerValue der = derIn.getDerValue();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   539
                        etp = new EncTicketPart(der.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   540
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   541
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   542
                if (tkt == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   543
                    throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   544
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   545
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   546
            EncryptionKey skey = keyForUser(body.sname, etype);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   547
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   548
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   549
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   550
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   551
            // Session key for original ticket, TGT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   552
            EncryptionKey ckey = etp.key;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   553
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   554
            // Session key for session with the service
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   555
            EncryptionKey key = generateRandomKey(etype);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   556
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   557
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   558
            KerberosTime till = body.till;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   559
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   560
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   561
            } else if (till.isZero()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   562
                till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   563
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   564
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   565
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   566
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   567
                bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   568
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   569
            if (body.kdcOptions.get(KDCOptions.FORWARDED) ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   570
                    etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   571
                bFlags[Krb5.TKT_OPTS_FORWARDED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   572
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   573
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   574
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   575
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   576
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   577
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   578
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   579
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   580
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   581
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   582
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   583
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   584
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   585
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   586
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   587
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   588
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   589
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   590
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   591
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   592
                    etp.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   593
                    etp.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   594
                    new TransitedEncoding(1, new byte[0]),  // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   595
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   596
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   597
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   598
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   599
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   600
            Ticket t = new Ticket(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   601
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   602
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   603
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   604
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   605
            EncTGSRepPart enc_part = new EncTGSRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   606
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   607
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   608
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   609
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   610
                    body.getNonce(),    // TODO: detect replay
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   611
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   612
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   613
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   614
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   615
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   616
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   617
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   618
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   619
                    body.addresses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   620
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   621
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   622
            TGSRep tgsRep = new TGSRep(null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   623
                    etp.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   624
                    etp.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   625
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   626
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   627
            System.out.println("     Return " + tgsRep.cname
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   628
                    + " ticket for " + tgsRep.ticket.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   629
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   630
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   631
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   632
                    true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   633
            return out.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   634
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   635
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   636
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   637
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   638
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   639
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   640
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   641
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   642
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   643
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   644
                        ke.returnCode(),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   645
                        body.crealm, body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   646
                        new Realm(getRealm()), body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   647
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   648
                        null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   649
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   650
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   651
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   652
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   653
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   654
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   655
     * Processes a AS_REQ and generates a AS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   656
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   657
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   658
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   659
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   660
    private byte[] processAsReq(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   661
        ASReq asReq = new ASReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   662
        int[] eTypes = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   663
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   664
            System.out.println(realm + "> " + asReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   665
                    " sends AS-REQ for " +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   666
                    asReq.reqBody.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   667
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   668
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   669
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   670
            // Reflection: int[] eType = body.eType;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   671
            Field f = KDCReqBody.class.getDeclaredField("eType");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   672
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   673
            eTypes = (int[])f.get(body);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   674
            int eType = eTypes[0];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   675
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   676
            EncryptionKey ckey = keyForUser(body.cname, eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   677
            EncryptionKey skey = keyForUser(body.sname, eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   678
            if (ckey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   679
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   680
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   681
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   682
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   683
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   684
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   685
            // Session key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   686
            EncryptionKey key = generateRandomKey(eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   687
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   688
            KerberosTime till = body.till;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   689
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   690
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   691
            } else if (till.isZero()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   692
                till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   693
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   694
            //body.from
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   695
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   696
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   697
                bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   698
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   699
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   700
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   701
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   702
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   703
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   704
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   705
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   706
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   707
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   708
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   709
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   710
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   711
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   712
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   713
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   714
            f = KDCReq.class.getDeclaredField("pAData");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   715
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   716
            PAData[] pas = (PAData[])f.get(asReq);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   717
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   718
                Object preauth = options.get(Option.PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   719
                if (preauth == null || preauth.equals(Boolean.TRUE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   720
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   721
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   722
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   723
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   724
                    Constructor<EncryptedData> ctor = EncryptedData.class.getDeclaredConstructor(DerValue.class);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   725
                    ctor.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   726
                    EncryptedData data = ctor.newInstance(new DerValue(pas[0].getValue()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   727
                    data.decrypt(ckey, KeyUsage.KU_PA_ENC_TS);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   728
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   729
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   730
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   731
                bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   732
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   733
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   734
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   735
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   736
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   737
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   738
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   739
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   740
                    new TransitedEncoding(1, new byte[0]),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   741
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   742
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   743
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   744
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   745
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   746
            Ticket t = new Ticket(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   747
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   748
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   749
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   750
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   751
            EncASRepPart enc_part = new EncASRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   752
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   753
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   754
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   755
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   756
                    body.getNonce(),    // TODO: detect replay?
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   757
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   758
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   759
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   760
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   761
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   762
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   763
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   764
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   765
                    body.addresses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   766
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   767
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_AS_REP_PART);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   768
            ASRep asRep = new ASRep(null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   769
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   770
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   771
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   772
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   773
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   774
            System.out.println("     Return " + asRep.cname
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   775
                    + " ticket for " + asRep.ticket.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   776
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   777
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   778
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   779
                    true, (byte)Krb5.KRB_AS_REP), asRep.asn1Encode());
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   780
            byte[] result = out.toByteArray();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   781
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   782
            // Added feature:
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   783
            // Write the current issuing TGT into a ccache file specified
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   784
            // by the system property below.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   785
            String ccache = System.getProperty("test.kdc.save.ccache");
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   786
            if (ccache != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   787
                asRep.encKDCRepPart = enc_part;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   788
                sun.security.krb5.internal.ccache.Credentials credentials =
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   789
                    new sun.security.krb5.internal.ccache.Credentials(asRep);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   790
                asReq.reqBody.cname.setRealm(getRealm());
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   791
                CredentialsCache cache =
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   792
                    CredentialsCache.create(asReq.reqBody.cname, ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   793
                if (cache == null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   794
                   throw new IOException("Unable to create the cache file " +
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   795
                                         ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   796
                }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   797
                cache.update(credentials);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   798
                cache.save();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   799
                new File(ccache).deleteOnExit();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   800
            }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   801
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   802
            return result;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   803
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   804
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   805
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   806
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   807
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   808
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   809
            byte[] eData = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   810
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   811
                if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   812
                        ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   813
                    PAData pa;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   814
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   815
                    ETypeInfo2 ei2 = new ETypeInfo2(eTypes[0], null, null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   816
                    DerOutputStream eid = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   817
                    eid.write(DerValue.tag_Sequence, ei2.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   818
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   819
                    pa = new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   820
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   821
                    DerOutputStream bytes = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   822
                    bytes.write(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]).asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   823
                    bytes.write(pa.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   824
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   825
                    boolean allOld = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   826
                    for (int i: eTypes) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   827
                        if (i == EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   828
                                i == EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   829
                            allOld = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   830
                            break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   831
                        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   832
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   833
                    if (allOld) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   834
                        ETypeInfo ei = new ETypeInfo(eTypes[0], null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   835
                        eid = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   836
                        eid.write(DerValue.tag_Sequence, ei.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   837
                        pa = new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   838
                        bytes.write(pa.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   839
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   840
                    DerOutputStream temp = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   841
                    temp.write(DerValue.tag_Sequence, bytes);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   842
                    eData = temp.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   843
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   844
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   845
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   846
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   847
                        ke.returnCode(),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   848
                        body.crealm, body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   849
                        new Realm(getRealm()), body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   850
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   851
                        eData);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   852
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   853
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   854
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   855
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   856
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   857
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   858
     * Generates a line for a KDC to put inside [realms] of krb5.conf
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   859
     * @param kdc the KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   860
     * @return REALM.NAME = { kdc = localhost:port }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   861
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   862
    private static String realmLineForKDC(KDC kdc) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   863
        return String.format("  %s = {\n    kdc = localhost:%d\n  }\n", kdc.realm, kdc.port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   864
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   865
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   866
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   867
     * Start the KDC service. This server listens on both UDP and TCP using
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   868
     * the same port number. It uses three threads to deal with requests.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   869
     * They can be set to daemon threads if requested.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   870
     * @param port the port number to listen to. If zero, a random available
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   871
     *  port no less than 8000 will be chosen and used.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   872
     * @param asDaemon true if the KDC threads should be daemons
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   873
     * @throws java.io.IOException for any communication error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   874
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   875
    protected void startServer(int port, boolean asDaemon) throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   876
        DatagramSocket u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   877
        ServerSocket t1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   878
        if (port > 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   879
            u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   880
            t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   881
        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   882
            while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   883
                // Try to find a port number that's both TCP and UDP free
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   884
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   885
                    port = 8000 + new java.util.Random().nextInt(10000);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   886
                    u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   887
                    u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   888
                    t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   889
                    break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   890
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   891
                    if (u1 != null) u1.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   892
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   893
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   894
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   895
        final DatagramSocket udp = u1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   896
        final ServerSocket tcp = t1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   897
        System.out.println("Start KDC on " + port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   898
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   899
        this.port = port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   900
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   901
        // The UDP consumer
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   902
        Thread thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   903
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   904
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   905
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   906
                        byte[] inbuf = new byte[8192];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   907
                        DatagramPacket p = new DatagramPacket(inbuf, inbuf.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   908
                        udp.receive(p);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   909
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   910
                        System.out.println(">>>>> UDP packet received");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   911
                        q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   912
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   913
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   914
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   915
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   916
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   917
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   918
        thread.setDaemon(asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   919
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   920
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   921
        // The TCP consumer
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   922
        thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   923
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   924
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   925
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   926
                        Socket socket = tcp.accept();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   927
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   928
                        System.out.println(">>>>> TCP connection established");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   929
                        DataInputStream in = new DataInputStream(socket.getInputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   930
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   931
                        byte[] token = new byte[in.readInt()];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   932
                        in.readFully(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   933
                        q.put(new Job(processMessage(token), socket, out));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   934
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   935
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   936
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   937
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   938
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   939
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   940
        thread.setDaemon(asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   941
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   942
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   943
        // The dispatcher
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   944
        thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   945
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   946
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   947
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   948
                        q.take().send();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   949
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   950
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   951
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   952
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   953
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   954
        thread.setDaemon(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   955
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   956
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   957
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   958
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   959
     * Helper class to encapsulate a job in a KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   960
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   961
    private static class Job {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   962
        byte[] token;           // The received request at creation time and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   963
                                // the response at send time
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   964
        Socket s;               // The TCP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   965
        DataOutputStream out;   // The OutputStream of the TCP socket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   966
        DatagramSocket s2;      // The UDP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   967
        DatagramPacket dp;      // The incoming UDP datagram packet
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   968
        boolean useTCP;         // Whether TCP or UDP is used
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   969
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   970
        // Creates a job object for TCP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   971
        Job(byte[] token, Socket s, DataOutputStream out) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   972
            useTCP = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   973
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   974
            this.s = s;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   975
            this.out = out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   976
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   977
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   978
        // Creates a job object for UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   979
        Job(byte[] token, DatagramSocket s2, DatagramPacket dp) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   980
            useTCP = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   981
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   982
            this.s2 = s2;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   983
            this.dp = dp;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   984
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   985
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   986
        // Sends the output back to the client
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   987
        void send() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   988
            try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   989
                if (useTCP) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   990
                    System.out.println(">>>>> TCP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   991
                    out.writeInt(token.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   992
                    out.write(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   993
                    s.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   994
                } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   995
                    System.out.println(">>>>> UDP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   996
                    s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   997
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   998
            } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   999
                e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1000
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1001
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1002
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1003
}