jdk/test/sun/security/krb5/auto/KDC.java
author weijun
Fri, 27 Nov 2009 08:51:28 +0800
changeset 4336 4c792c19266e
parent 4168 1a8d21bb898c
child 4531 3a9206343ab2
permissions -rw-r--r--
6853328: Support OK-AS-DELEGATE flag Reviewed-by: 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.*;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
    33
import sun.net.spi.nameservice.NameService;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
    34
import sun.net.spi.nameservice.NameServiceDescriptor;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    35
import sun.security.krb5.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    36
import sun.security.krb5.internal.*;
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
    37
import sun.security.krb5.internal.ccache.CredentialsCache;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    38
import sun.security.krb5.internal.crypto.KeyUsage;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    39
import sun.security.krb5.internal.ktab.KeyTab;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import sun.security.util.DerInputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    41
import sun.security.util.DerOutputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    42
import sun.security.util.DerValue;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    43
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    44
/**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    45
 * A KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    46
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    47
 * Features:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    48
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
 * <li> Supports TCP and UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
 * <li> Supports AS-REQ and TGS-REQ
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    51
 * <li> Principal db and other settings hard coded in application
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    52
 * <li> Options, say, request preauth or not
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    53
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    54
 * Side effects:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    55
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
 * <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
 * singleton and initialized according to Kerberos settings (krb5.conf and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
 * java.security.krb5.* system properties). This means once it's initialized
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    59
 * it will not automatically notice any changes to these settings (or file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    60
 * changes of krb5.conf). The KDC class normally does not touch these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
 * settings (except for the <code>writeKtab()</code> method). However, to make
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
 * sure nothing ever goes wrong, if you want to make any changes to these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
 * settings after calling a KDC method, call <code>Config.refresh()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
 * make sure your changes are reflected in the <code>Config</code> object.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
 * </ol>
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    66
 * System properties recognized:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    67
 * <ul>
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    68
 * <li>test.kdc.save.ccache
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    69
 * </ul>
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    70
 * Support policies:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    71
 * <ul>
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    72
 * <li>ok-as-delegate
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    73
 * </ul>
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
 * Issues and TODOs:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    75
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    76
 * <li> Generates krb5.conf to be used on another machine, currently the kdc is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    77
 * always localhost
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    78
 * <li> More options to KDC, say, error output, say, response nonce !=
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    79
 * request nonce
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
 * Note: This program uses internal krb5 classes (including reflection to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
 * access private fields and methods).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    83
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    84
 * Usages:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
 * 1. Init and start the KDC:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
 * KDC kdc = KDC.create("REALM.NAME", port, isDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
 * KDC kdc = KDC.create("REALM.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
 * Here, <code>port</code> is the UDP and TCP port number the KDC server
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
 * listens on. If zero, a random port is chosen, which you can use getPort()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
 * later to retrieve the value.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    94
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    95
 * If <code>isDaemon</code> is true, the KDC worker threads will be daemons.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    96
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    97
 * The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    98
 * isDaemon=false, and is commonly used in an embedded KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    99
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   100
 * 2. Adding users:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   101
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   102
 * kdc.addPrincipal(String principal_name, char[] password);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   103
 * kdc.addPrincipalRandKey(String principal_name);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   104
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   105
 * 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
   106
 * generates a random key. To expose this key, call <code>writeKtab()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   107
 * save the keys into a keytab file.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   108
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   109
 * Note that you need to add the principal name krbtgt/REALM.NAME yourself.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   110
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   111
 * 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
   112
 * started and before a user requests info on this principal.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   113
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   114
 * 3. Other public methods:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   115
 * <ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   116
 * <li> <code>getPort</code>: Returns the port number the KDC uses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   117
 * <li> <code>getRealm</code>: Returns the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   118
 * <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   119
 * <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   120
 * <li> <code>setOption</code>: Sets various options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   121
 * </ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   122
 * Read the javadoc for details. Lazy developer can use <code>OneKDC</code>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   123
 * directly.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   124
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   125
public class KDC {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   126
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   127
    // Under the hood.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   128
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   129
    // The random generator to generate random keys (including session keys)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   130
    private static SecureRandom secureRandom = new SecureRandom();
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   131
    // Principal db. principal -> pass
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   132
    private Map<String,char[]> passwords = new HashMap<String,char[]>();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   133
    // Realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   134
    private String realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   135
    // KDC
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   136
    private String kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   137
    // Service port number
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   138
    private int port;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   139
    // The request/response job queue
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   140
    private BlockingQueue<Job> q = new ArrayBlockingQueue<Job>(100);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   141
    // Options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   142
    private Map<Option,Object> options = new HashMap<Option,Object>();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   143
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   144
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   145
     * Option names, to be expanded forever.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   146
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   147
    public static enum Option {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   148
        /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   149
         * Whether pre-authentication is required. Default Boolean.TRUE
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   150
         */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   151
        PREAUTH_REQUIRED,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   152
    };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   153
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   154
    static {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   155
        System.setProperty("sun.net.spi.nameservice.provider.1", "ns,mock");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   156
    }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   157
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   158
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   159
     * A standalone KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   160
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   161
    public static void main(String[] args) throws Exception {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   162
        KDC kdc = create("RABBIT.HOLE", "kdc.rabbit.hole", 0, false);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   163
        kdc.addPrincipal("dummy", "bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   164
        kdc.addPrincipal("foo", "bar".toCharArray());
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   165
        kdc.addPrincipalRandKey("krbtgt/RABBIT.HOLE");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   166
        kdc.addPrincipalRandKey("server/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   167
        kdc.addPrincipalRandKey("backend/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   168
        KDC.saveConfig("krb5.conf", kdc, "forwardable = true");
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 {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   178
        return create(realm, "kdc." + realm.toLowerCase(), 0, true);
1454
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
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   190
    public static KDC create(String realm, String kdc, int port, boolean asDaemon) throws IOException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   191
        return new KDC(realm, kdc, port, asDaemon);
1454
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()) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   231
                ktab.addEntry(new PrincipalName(name,
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   232
                        name.indexOf('/') < 0 ?
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   233
                            PrincipalName.KRB_NT_UNKNOWN :
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   234
                            PrincipalName.KRB_NT_SRV_HST),
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   235
                            kdc.passwords.get(name));
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   236
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   237
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   238
        ktab.save();
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
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   242
     * Write a ktab for this KDC.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   243
     */
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   244
    public void writeKtab(String tab) throws IOException, KrbException {
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   245
        KDC.writeMultiKtab(tab, this);
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
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   248
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   249
     * Adds a new principal to this realm with a given password.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   250
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   251
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   252
     * @param pass the password for the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   253
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   254
    public void addPrincipal(String user, char[] pass) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   255
        if (user.indexOf('@') < 0) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   256
            user = user + "@" + realm;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   257
        }
1454
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) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   267
        addPrincipal(user, randomPassword());
1454
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
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   279
     * Returns the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   280
     * @return the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   281
     */
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   282
    public String getKDC() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   283
        return kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   284
    }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   285
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   286
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   287
     * Writes a krb5.conf for one or more KDC that includes KDC locations for
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   288
     * each realm and the default realm name. You can also add extra strings
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   289
     * into the file. The method should be called like:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   290
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   291
     *   KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   292
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   293
     * Here you can provide one or more kdc# and zero or more line# arguments.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   294
     * The line# will be put after [libdefaults] and before [realms]. Therefore
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   295
     * you can append new lines into [libdefaults] and/or create your new
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   296
     * stanzas as well. Note that a newline character will be appended to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   297
     * each line# argument.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   298
     * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   299
     * For example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   300
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   301
     * KDC.saveConfig("krb5.conf", this);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   302
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   303
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   304
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   305
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   306
     * default_realm = REALM.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   307
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   308
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   309
     *   REALM.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   310
     *     kdc = host:port_number
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   311
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   312
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   313
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   314
     * Another example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   315
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   316
     * KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   317
     *         "[domain_realm]",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   318
     *         ".kdc1.com = KDC1.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   319
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   320
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   321
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   322
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   323
     * default_realm = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   324
     * forwardable = true
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   325
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   326
     * [domain_realm]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   327
     * .kdc1.com = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   328
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   329
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   330
     *   KDC1.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   331
     *     kdc = host:port1
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   332
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   333
     *   KDC2.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   334
     *     kdc = host:port2
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   335
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   336
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   337
     * @param file the name of the file to write into
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   338
     * @param kdc the first (and default) KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   339
     * @param more more KDCs or extra lines (in their appearing order) to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   340
     * insert into the krb5.conf file. This method reads each argument's type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   341
     * to determine what it's for. This argument can be empty.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   342
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   343
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   344
    public static void saveConfig(String file, KDC kdc, Object... more)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   345
            throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   346
        File f = new File(file);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   347
        StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   348
        sb.append("[libdefaults]\ndefault_realm = ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   349
        sb.append(kdc.realm);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   350
        sb.append("\n");
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 String) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   353
                sb.append(o);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   354
                sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   355
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   356
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   357
        sb.append("\n[realms]\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   358
        sb.append(realmLineForKDC(kdc));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   359
        for (Object o: more) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   360
            if (o instanceof KDC) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   361
                sb.append(realmLineForKDC((KDC)o));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   362
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   363
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   364
        FileOutputStream fos = new FileOutputStream(f);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   365
        fos.write(sb.toString().getBytes());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   366
        fos.close();
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
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   370
     * Returns the service port of the KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   371
     * @return the KDC service port
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   372
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   373
    public int getPort() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   374
        return port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   375
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   376
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   377
    // Private helper methods
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
     * Private constructor, cannot be called outside.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   381
     * @param realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   382
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   383
    private KDC(String realm, String kdc) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   384
        this.realm = realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   385
        this.kdc = kdc;
1454
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
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   389
     * A constructor that starts the KDC service also.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   390
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   391
    protected KDC(String realm, String kdc, int port, boolean asDaemon)
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   392
            throws IOException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   393
        this(realm, kdc);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   394
        startServer(port, asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   395
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   396
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   397
     * Generates a 32-char random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   398
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   399
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   400
    private static char[] randomPassword() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   401
        char[] pass = new char[32];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   402
        for (int i=0; i<32; i++)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   403
            pass[i] = (char)secureRandom.nextInt();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   404
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   405
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   406
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   407
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   408
     * Generates a random key for the given encryption type.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   409
     * @param eType the encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   410
     * @return the generated key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   411
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   412
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   413
    private static EncryptionKey generateRandomKey(int eType)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   414
            throws KrbException  {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   415
        // Is 32 enough for AES256? I should have generated the keys directly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   416
        // but different cryptos have different rules on what keys are valid.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   417
        char[] pass = randomPassword();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   418
        String algo;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   419
        switch (eType) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   420
            case EncryptedData.ETYPE_DES_CBC_MD5: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   421
            case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: algo = "DESede"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   422
            case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: algo = "AES128"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   423
            case EncryptedData.ETYPE_ARCFOUR_HMAC: algo = "ArcFourHMAC"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   424
            case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: algo = "AES256"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   425
            default: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   426
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   427
        return new EncryptionKey(pass, "NOTHING", algo);    // Silly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   428
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   429
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   430
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   431
     * Returns the password for a given principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   432
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   433
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   434
     * @throws sun.security.krb5.KrbException when the principal is not inside
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   435
     *         the database.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   436
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   437
    private char[] getPassword(PrincipalName p, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   438
            throws KrbException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   439
        String pn = p.toString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   440
        if (p.getRealmString() == null) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   441
            pn = pn + "@" + getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   442
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   443
        char[] pass = passwords.get(pn);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   444
        if (pass == null) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   445
            throw new KrbException(server?
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   446
                Krb5.KDC_ERR_S_PRINCIPAL_UNKNOWN:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   447
                Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   448
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   449
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   450
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   451
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   452
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   453
     * Returns the salt string for the principal.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   454
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   455
     * @return the salt
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   456
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   457
    private String getSalt(PrincipalName p) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   458
        String[] ns = p.getNameStrings();
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   459
        String s = p.getRealmString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   460
        if (s == null) s = getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   461
        for (String n: p.getNameStrings()) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   462
            s += n;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   463
        }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   464
        return s;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   465
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   466
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   467
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   468
     * Returns the key for a given principal of the given encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   469
     * @param p the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   470
     * @param etype the encryption type
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   471
     * @param server looking for a server principal?
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   472
     * @return the key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   473
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   474
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   475
    private EncryptionKey keyForUser(PrincipalName p, int etype, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   476
            throws KrbException {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   477
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   478
            // Do not call EncryptionKey.acquireSecretKeys(), otherwise
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   479
            // the krb5.conf config file would be loaded.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   480
            Method stringToKey = EncryptionKey.class.getDeclaredMethod("stringToKey", char[].class, String.class, byte[].class, Integer.TYPE);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   481
            stringToKey.setAccessible(true);
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   482
            Integer kvno = null;
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   483
            // For service whose password ending with a number, use it as kvno
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   484
            if (p.toString().indexOf('/') >= 0) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   485
                char[] pass = getPassword(p, server);
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   486
                if (Character.isDigit(pass[pass.length-1])) {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   487
                    kvno = pass[pass.length-1] - '0';
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   488
                }
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   489
            }
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   490
            return new EncryptionKey((byte[]) stringToKey.invoke(
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   491
                    null, getPassword(p, server), getSalt(p), null, etype),
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   492
                    etype, kvno);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   493
        } catch (InvocationTargetException ex) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   494
            KrbException ke = (KrbException)ex.getCause();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   495
            throw ke;
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   496
        } catch (KrbException ke) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   497
            throw ke;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   498
        } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   499
            throw new RuntimeException(e);  // should not happen
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   500
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   501
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   502
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   503
    private Map<String,String> policies = new HashMap<String,String>();
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   504
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   505
    public void setPolicy(String rule, String value) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   506
        if (value == null) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   507
            policies.remove(rule);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   508
        } else {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   509
            policies.put(rule, value);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   510
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   511
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   512
    /**
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   513
     * If the provided client/server pair matches a rule
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   514
     *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   515
     * A system property named test.kdc.policy.RULE will be consulted.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   516
     * If it's unset, returns false. If its value is "", any pair is
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   517
     * matched. Otherwise, it should contains the server name matched.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   518
     *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   519
     * TODO: client name is not used currently.
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   520
     *
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   521
     * @param c client name
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   522
     * @param s server name
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   523
     * @param rule rule name
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   524
     * @return if a match is found
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   525
     */
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   526
    private boolean configMatch(String c, String s, String rule) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   527
        String policy = policies.get(rule);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   528
        boolean result = false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   529
        if (policy == null) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   530
            result = false;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   531
        } else if (policy.length() == 0) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   532
            result = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   533
        } else {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   534
            String[] names = policy.split("\\s+");
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   535
            for (String name: names) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   536
                if (name.equals(s)) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   537
                    result = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   538
                    break;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   539
                }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   540
            }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   541
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   542
        if (result) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   543
            System.out.printf(">>>> Policy match result (%s vs %s on %s) %b\n",
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   544
                    c, s, rule, result);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   545
        }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   546
        return result;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   547
    }
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   548
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   549
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   550
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   551
     * Processes an incoming request and generates a response.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   552
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   553
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   554
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   555
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   556
    private byte[] processMessage(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   557
        if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   558
            return processAsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   559
        else
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   560
            return processTgsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   561
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   562
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   563
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   564
     * Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   565
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   566
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   567
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   568
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   569
    private byte[] processTgsReq(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   570
        TGSReq tgsReq = new TGSReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   571
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   572
            System.out.println(realm + "> " + tgsReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   573
                    " sends TGS-REQ for " +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   574
                    tgsReq.reqBody.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   575
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   576
            int etype = 0;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   577
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   578
            // Reflection: PAData[] pas = tgsReq.pAData;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   579
            Field f = KDCReq.class.getDeclaredField("pAData");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   580
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   581
            PAData[] pas = (PAData[])f.get(tgsReq);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   582
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   583
            Ticket tkt = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   584
            EncTicketPart etp = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   585
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   586
                throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   587
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   588
                for (PAData pa: pas) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   589
                    if (pa.getType() == Krb5.PA_TGS_REQ) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   590
                        APReq apReq = new APReq(pa.getValue());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   591
                        EncryptedData ed = apReq.authenticator;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   592
                        tkt = apReq.ticket;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   593
                        etype = tkt.encPart.getEType();
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   594
                        tkt.sname.setRealm(tkt.realm);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   595
                        EncryptionKey kkey = keyForUser(tkt.sname, etype, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   596
                        byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   597
                        DerInputStream derIn = new DerInputStream(bb);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   598
                        DerValue der = derIn.getDerValue();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   599
                        etp = new EncTicketPart(der.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   600
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   601
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   602
                if (tkt == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   603
                    throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   604
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   605
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   606
            EncryptionKey skey = keyForUser(body.sname, etype, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   607
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   608
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   609
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   610
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   611
            // Session key for original ticket, TGT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   612
            EncryptionKey ckey = etp.key;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   613
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   614
            // Session key for session with the service
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   615
            EncryptionKey key = generateRandomKey(etype);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   616
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   617
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   618
            KerberosTime till = body.till;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   619
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   620
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   621
            } else if (till.isZero()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   622
                till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   623
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   624
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   625
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   626
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   627
                bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   628
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   629
            if (body.kdcOptions.get(KDCOptions.FORWARDED) ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   630
                    etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   631
                bFlags[Krb5.TKT_OPTS_FORWARDED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   632
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   633
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   634
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   635
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   636
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   637
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   638
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   639
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   640
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   641
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   642
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   643
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   644
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   645
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   646
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   647
            if (configMatch("", body.sname.getNameString(), "ok-as-delegate")) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   648
                bFlags[Krb5.TKT_OPTS_DELEGATE] = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   649
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   650
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   651
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   652
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   653
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   654
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   655
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   656
                    etp.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   657
                    etp.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   658
                    new TransitedEncoding(1, new byte[0]),  // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   659
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   660
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   661
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   662
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   663
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   664
            Ticket t = new Ticket(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   665
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   666
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   667
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   668
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   669
            EncTGSRepPart enc_part = new EncTGSRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   670
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   671
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   672
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   673
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   674
                    body.getNonce(),    // TODO: detect replay
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   675
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   676
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   677
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   678
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   679
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   680
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   681
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   682
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   683
                    body.addresses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   684
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   685
            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
   686
            TGSRep tgsRep = new TGSRep(null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   687
                    etp.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   688
                    etp.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   689
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   690
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   691
            System.out.println("     Return " + tgsRep.cname
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   692
                    + " ticket for " + tgsRep.ticket.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   693
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   694
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   695
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   696
                    true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   697
            return out.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   698
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   699
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   700
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   701
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   702
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   703
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   704
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   705
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   706
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   707
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   708
                        ke.returnCode(),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   709
                        body.crealm, body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   710
                        new Realm(getRealm()), body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   711
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   712
                        null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   713
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   714
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   715
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   716
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   717
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   718
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   719
     * Processes a AS_REQ and generates a AS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   720
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   721
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   722
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   723
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   724
    private byte[] processAsReq(byte[] in) throws Exception {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   725
        ASReq asReq = new ASReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   726
        int[] eTypes = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   727
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   728
            System.out.println(realm + "> " + asReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   729
                    " sends AS-REQ for " +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   730
                    asReq.reqBody.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   731
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   732
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   733
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   734
            // Reflection: int[] eType = body.eType;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   735
            Field f = KDCReqBody.class.getDeclaredField("eType");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   736
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   737
            eTypes = (int[])f.get(body);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   738
            int eType = eTypes[0];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   739
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   740
            EncryptionKey ckey = keyForUser(body.cname, eType, false);
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   741
            EncryptionKey skey = keyForUser(body.sname, eType, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   742
            if (ckey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   743
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   744
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   745
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   746
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   747
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   748
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   749
            // Session key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   750
            EncryptionKey key = generateRandomKey(eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   751
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   752
            KerberosTime till = body.till;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   753
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   754
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   755
            } else if (till.isZero()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   756
                till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   757
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   758
            //body.from
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   759
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   760
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   761
                bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   762
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   763
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   764
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   765
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   766
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   767
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   768
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   769
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   770
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   771
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   772
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   773
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   774
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   775
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   776
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   777
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   778
            f = KDCReq.class.getDeclaredField("pAData");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   779
            f.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   780
            PAData[] pas = (PAData[])f.get(asReq);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   781
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   782
                Object preauth = options.get(Option.PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   783
                if (preauth == null || preauth.equals(Boolean.TRUE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   784
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   785
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   786
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   787
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   788
                    Constructor<EncryptedData> ctor = EncryptedData.class.getDeclaredConstructor(DerValue.class);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   789
                    ctor.setAccessible(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   790
                    EncryptedData data = ctor.newInstance(new DerValue(pas[0].getValue()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   791
                    data.decrypt(ckey, KeyUsage.KU_PA_ENC_TS);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   792
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   793
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   794
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   795
                bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   796
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   797
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   798
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   799
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   800
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   801
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   802
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   803
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   804
                    new TransitedEncoding(1, new byte[0]),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   805
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   806
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   807
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   808
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   809
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   810
            Ticket t = new Ticket(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   811
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   812
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   813
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   814
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   815
            EncASRepPart enc_part = new EncASRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   816
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   817
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   818
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   819
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   820
                    body.getNonce(),    // TODO: detect replay?
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   821
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   822
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   823
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   824
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   825
                    body.from,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   826
                    till, body.rtime,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   827
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   828
                    body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   829
                    body.addresses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   830
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   831
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_AS_REP_PART);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   832
            ASRep asRep = new ASRep(null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   833
                    body.crealm,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   834
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   835
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   836
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   837
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   838
            System.out.println("     Return " + asRep.cname
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   839
                    + " ticket for " + asRep.ticket.sname);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   840
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   841
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   842
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   843
                    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
   844
            byte[] result = out.toByteArray();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   845
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   846
            // Added feature:
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   847
            // 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
   848
            // by the system property below.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   849
            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
   850
            if (ccache != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   851
                asRep.encKDCRepPart = enc_part;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   852
                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
   853
                    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
   854
                asReq.reqBody.cname.setRealm(getRealm());
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   855
                CredentialsCache cache =
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   856
                    CredentialsCache.create(asReq.reqBody.cname, ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   857
                if (cache == null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   858
                   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
   859
                                         ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   860
                }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   861
                cache.update(credentials);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   862
                cache.save();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   863
                new File(ccache).deleteOnExit();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   864
            }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   865
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
   866
            return result;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   867
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   868
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   869
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   870
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   871
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   872
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   873
            byte[] eData = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   874
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   875
                if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   876
                        ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   877
                    PAData pa;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   878
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   879
                    ETypeInfo2 ei2 = new ETypeInfo2(eTypes[0], null, null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   880
                    DerOutputStream eid = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   881
                    eid.write(DerValue.tag_Sequence, ei2.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   882
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   883
                    pa = new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   884
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   885
                    DerOutputStream bytes = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   886
                    bytes.write(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]).asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   887
                    bytes.write(pa.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   888
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   889
                    boolean allOld = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   890
                    for (int i: eTypes) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   891
                        if (i == EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   892
                                i == EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   893
                            allOld = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   894
                            break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   895
                        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   896
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   897
                    if (allOld) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   898
                        ETypeInfo ei = new ETypeInfo(eTypes[0], null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   899
                        eid = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   900
                        eid.write(DerValue.tag_Sequence, ei.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   901
                        pa = new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   902
                        bytes.write(pa.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   903
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   904
                    DerOutputStream temp = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   905
                    temp.write(DerValue.tag_Sequence, bytes);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   906
                    eData = temp.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   907
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   908
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   909
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   910
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   911
                        ke.returnCode(),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   912
                        body.crealm, body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   913
                        new Realm(getRealm()), body.sname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   914
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   915
                        eData);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   916
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   917
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   918
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   919
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   920
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   921
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   922
     * Generates a line for a KDC to put inside [realms] of krb5.conf
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   923
     * @param kdc the KDC
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   924
     * @return REALM.NAME = { kdc = host:port }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   925
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   926
    private static String realmLineForKDC(KDC kdc) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   927
        return String.format("  %s = {\n    kdc = %s:%d\n  }\n",
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   928
                kdc.realm,
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   929
                kdc.kdc,
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   930
                kdc.port);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   931
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   932
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   933
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   934
     * Start the KDC service. This server listens on both UDP and TCP using
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   935
     * the same port number. It uses three threads to deal with requests.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   936
     * They can be set to daemon threads if requested.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   937
     * @param port the port number to listen to. If zero, a random available
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   938
     *  port no less than 8000 will be chosen and used.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   939
     * @param asDaemon true if the KDC threads should be daemons
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   940
     * @throws java.io.IOException for any communication error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   941
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   942
    protected void startServer(int port, boolean asDaemon) throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   943
        DatagramSocket u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   944
        ServerSocket t1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   945
        if (port > 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   946
            u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   947
            t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   948
        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   949
            while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   950
                // Try to find a port number that's both TCP and UDP free
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   951
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   952
                    port = 8000 + new java.util.Random().nextInt(10000);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   953
                    u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   954
                    u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   955
                    t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   956
                    break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   957
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   958
                    if (u1 != null) u1.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   959
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   960
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   961
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   962
        final DatagramSocket udp = u1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   963
        final ServerSocket tcp = t1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   964
        System.out.println("Start KDC on " + port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   965
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   966
        this.port = port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   967
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   968
        // The UDP consumer
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   969
        Thread thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   970
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   971
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   972
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   973
                        byte[] inbuf = new byte[8192];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   974
                        DatagramPacket p = new DatagramPacket(inbuf, inbuf.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   975
                        udp.receive(p);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   976
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   977
                        System.out.println(">>>>> UDP packet received");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   978
                        q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   979
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   980
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   981
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   982
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   983
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   984
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   985
        thread.setDaemon(asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   986
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   987
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   988
        // The TCP consumer
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   989
        thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   990
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   991
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   992
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   993
                        Socket socket = tcp.accept();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   994
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   995
                        System.out.println(">>>>> TCP connection established");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   996
                        DataInputStream in = new DataInputStream(socket.getInputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   997
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   998
                        byte[] token = new byte[in.readInt()];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   999
                        in.readFully(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1000
                        q.put(new Job(processMessage(token), socket, out));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1001
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1002
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1003
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1004
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1005
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1006
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1007
        thread.setDaemon(asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1008
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1009
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1010
        // The dispatcher
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1011
        thread = new Thread() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1012
            public void run() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1013
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1014
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1015
                        q.take().send();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1016
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1017
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1018
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1019
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1020
        };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1021
        thread.setDaemon(true);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1022
        thread.start();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1023
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1024
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1025
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1026
     * Helper class to encapsulate a job in a KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1027
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1028
    private static class Job {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1029
        byte[] token;           // The received request at creation time and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1030
                                // the response at send time
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1031
        Socket s;               // The TCP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1032
        DataOutputStream out;   // The OutputStream of the TCP socket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1033
        DatagramSocket s2;      // The UDP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1034
        DatagramPacket dp;      // The incoming UDP datagram packet
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1035
        boolean useTCP;         // Whether TCP or UDP is used
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1036
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1037
        // Creates a job object for TCP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1038
        Job(byte[] token, Socket s, DataOutputStream out) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1039
            useTCP = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1040
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1041
            this.s = s;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1042
            this.out = out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1043
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1044
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1045
        // Creates a job object for UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1046
        Job(byte[] token, DatagramSocket s2, DatagramPacket dp) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1047
            useTCP = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1048
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1049
            this.s2 = s2;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1050
            this.dp = dp;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1051
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1052
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1053
        // Sends the output back to the client
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1054
        void send() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1055
            try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1056
                if (useTCP) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1057
                    System.out.println(">>>>> TCP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1058
                    out.writeInt(token.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1059
                    out.write(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1060
                    s.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1061
                } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1062
                    System.out.println(">>>>> UDP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1063
                    s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1064
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1065
            } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1066
                e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1067
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1068
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1069
    }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1070
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1071
    public static class KDCNameService implements NameServiceDescriptor {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1072
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1073
        public NameService createNameService() throws Exception {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1074
            NameService ns = new NameService() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1075
                @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1076
                public InetAddress[] lookupAllHostAddr(String host)
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1077
                        throws UnknownHostException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1078
                    // Everything is localhost
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1079
                    return new InetAddress[]{
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1080
                        InetAddress.getByAddress(host, new byte[]{127,0,0,1})
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1081
                    };
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1082
                }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1083
                @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1084
                public String getHostByAddr(byte[] addr)
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1085
                        throws UnknownHostException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1086
                    // No reverse lookup, PrincipalName use original string
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1087
                    throw new UnknownHostException();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1088
                }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1089
            };
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1090
            return ns;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1091
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1092
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1093
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1094
        public String getProviderName() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1095
            return "mock";
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1096
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1097
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1098
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1099
        public String getType() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1100
            return "ns";
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1101
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1102
    }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1103
}