test/jdk/sun/security/krb5/auto/KDC.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57487 643978a35f6e
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     1
/*
53273
bbc79e0ec9ee 8216265: [testbug] Introduce Platform.sharedLibraryPathVariableName() and adapt all tests.
goetz
parents: 50750
diff changeset
     2
 * Copyright (c) 2008, 2019, Oracle and/or its affiliates. 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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4532
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4532
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4532
diff changeset
    21
 * questions.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    22
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    23
53273
bbc79e0ec9ee 8216265: [testbug] Introduce Platform.sharedLibraryPathVariableName() and adapt all tests.
goetz
parents: 50750
diff changeset
    24
import jdk.test.lib.Platform;
bbc79e0ec9ee 8216265: [testbug] Introduce Platform.sharedLibraryPathVariableName() and adapt all tests.
goetz
parents: 50750
diff changeset
    25
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    26
import java.lang.reflect.Constructor;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    27
import java.lang.reflect.Field;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    29
import java.net.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    30
import java.io.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    31
import java.lang.reflect.Method;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    32
import java.nio.file.Files;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    33
import java.nio.file.Paths;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    34
import java.util.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    35
import java.util.concurrent.*;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    36
import java.util.stream.Collectors;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    37
import java.util.stream.Stream;
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
    38
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    39
import sun.security.krb5.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import sun.security.krb5.internal.*;
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
    41
import sun.security.krb5.internal.ccache.CredentialsCache;
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
    42
import sun.security.krb5.internal.crypto.EType;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    43
import sun.security.krb5.internal.crypto.KeyUsage;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    44
import sun.security.krb5.internal.ktab.KeyTab;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    45
import sun.security.util.DerInputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    46
import sun.security.util.DerOutputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    47
import sun.security.util.DerValue;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    48
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
/**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
 * A KDC server.
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    51
 *
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    52
 * Note: By setting the system property native.kdc.path to a native
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    53
 * krb5 installation, this class starts a native KDC with the
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    54
 * given realm and host. It can also add new principals and save keytabs.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
    55
 * Other features might not be available.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
 * Features:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    59
 * <li> Supports TCP and UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    60
 * <li> Supports AS-REQ and TGS-REQ
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
 * <li> Principal db and other settings hard coded in application
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
 * <li> Options, say, request preauth or not
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
 * Side effects:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    66
 * <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    67
 * singleton and initialized according to Kerberos settings (krb5.conf and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    68
 * java.security.krb5.* system properties). This means once it's initialized
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    69
 * it will not automatically notice any changes to these settings (or file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    70
 * changes of krb5.conf). The KDC class normally does not touch these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    71
 * settings (except for the <code>writeKtab()</code> method). However, to make
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    72
 * sure nothing ever goes wrong, if you want to make any changes to these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    73
 * settings after calling a KDC method, call <code>Config.refresh()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
 * make sure your changes are reflected in the <code>Config</code> object.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    75
 * </ol>
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    76
 * System properties recognized:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    77
 * <ul>
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    78
 * <li>test.kdc.save.ccache
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    79
 * </ul>
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
 * Issues and TODOs:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
 * <li> Generates krb5.conf to be used on another machine, currently the kdc is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    83
 * always localhost
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    84
 * <li> More options to KDC, say, error output, say, response nonce !=
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
 * request nonce
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
 * Note: This program uses internal krb5 classes (including reflection to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
 * access private fields and methods).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
 * Usages:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
 * 1. Init and start the KDC:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    94
 * KDC kdc = KDC.create("REALM.NAME", port, isDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    95
 * KDC kdc = KDC.create("REALM.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    96
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    97
 * Here, <code>port</code> is the UDP and TCP port number the KDC server
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    98
 * listens on. If zero, a random port is chosen, which you can use getPort()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    99
 * later to retrieve the value.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   100
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   101
 * If <code>isDaemon</code> is true, the KDC worker threads will be daemons.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   102
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   103
 * The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   104
 * isDaemon=false, and is commonly used in an embedded KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   105
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   106
 * 2. Adding users:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   107
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   108
 * kdc.addPrincipal(String principal_name, char[] password);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   109
 * kdc.addPrincipalRandKey(String principal_name);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   110
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   111
 * 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
   112
 * generates a random key. To expose this key, call <code>writeKtab()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   113
 * save the keys into a keytab file.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   114
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   115
 * Note that you need to add the principal name krbtgt/REALM.NAME yourself.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   116
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   117
 * 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
   118
 * started and before a user requests info on this principal.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   119
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   120
 * 3. Other public methods:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   121
 * <ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   122
 * <li> <code>getPort</code>: Returns the port number the KDC uses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   123
 * <li> <code>getRealm</code>: Returns the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   124
 * <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   125
 * <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   126
 * <li> <code>setOption</code>: Sets various options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   127
 * </ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   128
 * Read the javadoc for details. Lazy developer can use <code>OneKDC</code>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   129
 * directly.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   130
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   131
public class KDC {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   132
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
   133
    public static final int DEFAULT_LIFETIME = 39600;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
   134
    public static final int DEFAULT_RENEWTIME = 86400;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
   135
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   136
    public static final String NOT_EXISTING_HOST = "not.existing.host";
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   137
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   138
    // What etypes the KDC supports. Comma-separated strings. Null for all.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   139
    // Please note native KDCs might use different names.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   140
    private static final String SUPPORTED_ETYPES
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   141
            = System.getProperty("kdc.supported.enctypes");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   142
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   143
    // The native KDC
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   144
    private final NativeKdc nativeKdc;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   145
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   146
    // The native KDC process
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   147
    private Process kdcProc = null;
36967
d041d2e80712 8134577: Eliminate or standardize a replacement for sun.net.spi.nameservice.NameServiceDescriptor
msheppar
parents: 34438
diff changeset
   148
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   149
    // Under the hood.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   150
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   151
    // Principal db. principal -> pass. A case-insensitive TreeMap is used
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   152
    // so that even if the client provides a name with different case, the KDC
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   153
    // can still locate the principal and give back correct salt.
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   154
    private TreeMap<String,char[]> passwords = new TreeMap<>
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   155
            (String.CASE_INSENSITIVE_ORDER);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   156
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   157
    // Non default salts. Precisely, there should be different salts for
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   158
    // different etypes, pretend they are the same at the moment.
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   159
    private TreeMap<String,String> salts = new TreeMap<>
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   160
            (String.CASE_INSENSITIVE_ORDER);
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   161
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   162
    // Non default s2kparams for newer etypes. Precisely, there should be
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   163
    // different s2kparams for different etypes, pretend they are the same
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   164
    // at the moment.
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   165
    private TreeMap<String,byte[]> s2kparamses = new TreeMap<>
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   166
            (String.CASE_INSENSITIVE_ORDER);
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   167
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   168
    // Alias for referrals.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   169
    private TreeMap<String,KDC> aliasReferrals = new TreeMap<>
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   170
            (String.CASE_INSENSITIVE_ORDER);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   171
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   172
    // Alias for local resolution.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   173
    private TreeMap<String,PrincipalName> alias2Principals = new TreeMap<>
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   174
            (String.CASE_INSENSITIVE_ORDER);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   175
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   176
    // Realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   177
    private String realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   178
    // KDC
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   179
    private String kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   180
    // Service port number
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   181
    private int port;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   182
    // The request/response job queue
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   183
    private BlockingQueue<Job> q = new ArrayBlockingQueue<>(100);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   184
    // Options
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   185
    private Map<Option,Object> options = new HashMap<>();
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   186
    // Realm-specific krb5.conf settings
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   187
    private List<String> conf = new ArrayList<>();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   188
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   189
    private Thread thread1, thread2, thread3;
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   190
    private volatile boolean udpConsumerReady = false;
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   191
    private volatile boolean tcpConsumerReady = false;
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   192
    private volatile boolean dispatcherReady = false;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   193
    DatagramSocket u1 = null;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   194
    ServerSocket t1 = null;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   195
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
   196
    public static enum KtabMode { APPEND, EXISTING };
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
   197
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   198
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   199
     * Option names, to be expanded forever.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   200
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   201
    public static enum Option {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   202
        /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   203
         * Whether pre-authentication is required. Default Boolean.TRUE
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   204
         */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   205
        PREAUTH_REQUIRED,
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   206
        /**
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   207
         * Only issue TGT in RC4
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   208
         */
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   209
        ONLY_RC4_TGT,
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   210
        /**
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   211
         * Use RC4 as the first in preauth
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   212
         */
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   213
        RC4_FIRST_PREAUTH,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   214
        /**
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   215
         * Use only one preauth, so that some keys are not easy to generate
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   216
         */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   217
        ONLY_ONE_PREAUTH,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   218
        /**
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   219
         * Set all name-type to a value in response
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   220
         */
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   221
        RESP_NT,
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   222
        /**
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   223
         * Multiple ETYPE-INFO-ENTRY with same etype but different salt
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   224
         */
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   225
        DUP_ETYPE,
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   226
        /**
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   227
         * What backend server can be delegated to
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   228
         */
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   229
        OK_AS_DELEGATE,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   230
        /**
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   231
         * Allow S4U2self, List<String> of middle servers.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   232
         * If not set, means KDC does not understand S4U2self at all, therefore
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   233
         * would ignore any PA-FOR-USER request and send a ticket using the
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   234
         * cname of teh requestor. If set, it returns FORWARDABLE tickets to
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   235
         * a server with its name in the list
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   236
         */
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   237
        ALLOW_S4U2SELF,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   238
        /**
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   239
         * Allow S4U2proxy, Map<String,List<String>> of middle servers to
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   240
         * backends. If not set or a backend not in a server's list,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   241
         * Krb5.KDC_ERR_POLICY will be send for S4U2proxy request.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   242
         */
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   243
        ALLOW_S4U2PROXY,
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   244
        /**
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   245
         * Sensitive accounts can never be delegated.
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   246
         */
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   247
        SENSITIVE_ACCOUNTS,
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   248
        /**
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   249
         * If true, will check if TGS-REQ contains a non-null addresses field.
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   250
         */
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   251
        CHECK_ADDRESSES,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   252
    };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   253
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   254
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   255
     * A standalone KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   256
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   257
    public static void main(String[] args) throws Exception {
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
   258
        int port = args.length > 0 ? Integer.parseInt(args[0]) : 0;
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
   259
        KDC kdc = create("RABBIT.HOLE", "kdc.rabbit.hole", port, false);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   260
        kdc.addPrincipal("dummy", "bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   261
        kdc.addPrincipal("foo", "bar".toCharArray());
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   262
        kdc.addPrincipalRandKey("krbtgt/RABBIT.HOLE");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   263
        kdc.addPrincipalRandKey("server/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   264
        kdc.addPrincipalRandKey("backend/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   265
        KDC.saveConfig("krb5.conf", kdc, "forwardable = true");
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   266
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   267
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   268
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   269
     * Creates and starts a KDC running as a daemon on a random port.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   270
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   271
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   272
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   273
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   274
    public static KDC create(String realm) throws IOException {
39484
3cf8b2fa2b9e 8135114: sun/security/krb5/auto tests failed on machine with TR locale
akosarev
parents: 39049
diff changeset
   275
        return create(realm, "kdc." + realm.toLowerCase(Locale.US), 0, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   276
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   277
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   278
    public static KDC existing(String realm, String kdc, int port) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   279
        KDC k = new KDC(realm, kdc);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   280
        k.port = port;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   281
        return k;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   282
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   283
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   284
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   285
     * Creates and starts a KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   286
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   287
     * @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
   288
     *        chosen if zero.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   289
     * @param asDaemon if true, KDC threads will be daemons. Otherwise, not.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   290
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   291
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   292
     */
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   293
    public static KDC create(String realm, String kdc, int port,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   294
                             boolean asDaemon) throws IOException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   295
        return new KDC(realm, kdc, port, asDaemon);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   296
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   297
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   298
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   299
     * Sets an option
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   300
     * @param key the option name
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   301
     * @param value the value
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   302
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   303
    public void setOption(Option key, Object value) {
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   304
        if (value == null) {
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   305
            options.remove(key);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   306
        } else {
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   307
            options.put(key, value);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   308
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   309
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   310
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   311
    /**
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   312
     * Writes or appends keys into a keytab.
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   313
     * <p>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   314
     * Attention: This is the most basic one of a series of methods below on
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   315
     * keytab creation or modification. All these methods reference krb5.conf
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   316
     * settings. If you need to modify krb5.conf or switch to another krb5.conf
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   317
     * later, please call <code>Config.refresh()</code> again. For example:
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   318
     * <pre>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   319
     * kdc.writeKtab("/etc/kdc/ktab", true);  // Config is initialized,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   320
     * System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf");
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   321
     * Config.refresh();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   322
     * </pre>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   323
     * Inside this method there are 2 places krb5.conf is used:
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   324
     * <ol>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   325
     * <li> (Fatal) Generating keys: EncryptionKey.acquireSecretKeys
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   326
     * <li> (Has workaround) Creating PrincipalName
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   327
     * </ol>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   328
     * @param tab the keytab file name
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   329
     * @param append true if append, otherwise, overwrite.
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   330
     * @param names the names to write into, write all if names is empty
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   331
     */
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   332
    public void writeKtab(String tab, boolean append, String... names)
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   333
            throws IOException, KrbException {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   334
        KeyTab ktab = null;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   335
        if (nativeKdc == null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   336
            ktab = append ? KeyTab.getInstance(tab) : KeyTab.create(tab);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   337
        }
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   338
        Iterable<String> entries =
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   339
                (names.length != 0) ? Arrays.asList(names): passwords.keySet();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   340
        for (String name : entries) {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   341
            if (name.indexOf('@') < 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   342
                name = name + "@" + realm;
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   343
            }
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   344
            if (nativeKdc == null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   345
                char[] pass = passwords.get(name);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   346
                int kvno = 0;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   347
                if (Character.isDigit(pass[pass.length - 1])) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   348
                    kvno = pass[pass.length - 1] - '0';
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   349
                }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   350
                PrincipalName pn = new PrincipalName(name,
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   351
                        name.indexOf('/') < 0 ?
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   352
                                PrincipalName.KRB_NT_UNKNOWN :
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   353
                                PrincipalName.KRB_NT_SRV_HST);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   354
                ktab.addEntry(pn,
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   355
                        getSalt(pn),
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   356
                        pass,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   357
                        kvno,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   358
                        true);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   359
            } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   360
                nativeKdc.ktadd(name, tab);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   361
            }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   362
        }
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   363
        if (nativeKdc == null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   364
            ktab.save();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   365
        }
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   366
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   367
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   368
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   369
     * Writes all principals' keys from multiple KDCs into one keytab file.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   370
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   371
     * @throws sun.security.krb5.KrbException for any realm and/or principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   372
     *         name error.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   373
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   374
    public static void writeMultiKtab(String tab, KDC... kdcs)
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   375
            throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   376
        KeyTab.create(tab).save();      // Empty the old keytab
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   377
        appendMultiKtab(tab, kdcs);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   378
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   379
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   380
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   381
     * Appends all principals' keys from multiple KDCs to one keytab file.
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   382
     */
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   383
    public static void appendMultiKtab(String tab, KDC... kdcs)
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   384
            throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   385
        for (KDC kdc: kdcs) {
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   386
            kdc.writeKtab(tab, true);
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   387
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   388
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   389
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   390
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   391
     * Write a ktab for this KDC.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   392
     */
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   393
    public void writeKtab(String tab) throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   394
        writeKtab(tab, false);
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   395
    }
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   396
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   397
    /**
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   398
     * Appends keys in this KDC to a ktab.
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   399
     */
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   400
    public void appendKtab(String tab) throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   401
        writeKtab(tab, true);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   402
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   403
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   404
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   405
     * Adds a new principal to this realm with a given password.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   406
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   407
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   408
     * @param pass the password for the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   409
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   410
    public void addPrincipal(String user, char[] pass) {
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   411
        addPrincipal(user, pass, null, null);
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   412
    }
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   413
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   414
    /**
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   415
     * Adds a new principal to this realm with a given password.
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   416
     * @param user the principal's name. For a service principal, use the
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   417
     *        form of host/f.q.d.n
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   418
     * @param pass the password for the principal
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   419
     * @param salt the salt, or null if a default value will be used
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   420
     * @param s2kparams the s2kparams, or null if a default value will be used
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   421
     */
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   422
    public void addPrincipal(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   423
            String user, char[] pass, String salt, byte[] s2kparams) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   424
        if (user.indexOf('@') < 0) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   425
            user = user + "@" + realm;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   426
        }
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   427
        if (nativeKdc != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   428
            if (!user.equals("krbtgt/" + realm)) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   429
                nativeKdc.addPrincipal(user, new String(pass));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   430
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   431
            passwords.put(user, new char[0]);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   432
        } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   433
            passwords.put(user, pass);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   434
            if (salt != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   435
                salts.put(user, salt);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   436
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   437
            if (s2kparams != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   438
                s2kparamses.put(user, s2kparams);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   439
            }
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   440
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   441
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   442
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   443
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   444
     * Adds a new principal to this realm with a random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   445
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   446
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   447
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   448
    public void addPrincipalRandKey(String user) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   449
        addPrincipal(user, randomPassword());
1454
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
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   453
     * Returns the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   454
     * @return the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   455
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   456
    public String getRealm() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   457
        return realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   458
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   459
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   460
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   461
     * Returns the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   462
     * @return the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   463
     */
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   464
    public String getKDC() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   465
        return kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   466
    }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   467
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   468
    /**
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   469
     * Add realm-specific krb5.conf setting
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   470
     */
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   471
    public void addConf(String s) {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   472
        conf.add(s);
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   473
    }
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   474
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   475
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   476
     * Writes a krb5.conf for one or more KDC that includes KDC locations for
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   477
     * each realm and the default realm name. You can also add extra strings
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   478
     * into the file. The method should be called like:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   479
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   480
     *   KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   481
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   482
     * Here you can provide one or more kdc# and zero or more line# arguments.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   483
     * The line# will be put after [libdefaults] and before [realms]. Therefore
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   484
     * you can append new lines into [libdefaults] and/or create your new
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   485
     * stanzas as well. Note that a newline character will be appended to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   486
     * each line# argument.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   487
     * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   488
     * For example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   489
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   490
     * KDC.saveConfig("krb5.conf", this);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   491
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   492
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   493
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   494
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   495
     * default_realm = REALM.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   496
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   497
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   498
     *   REALM.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   499
     *     kdc = host:port_number
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   500
     *     # realm-specific settings
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   501
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   502
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   503
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   504
     * Another example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   505
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   506
     * KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   507
     *         "[domain_realm]",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   508
     *         ".kdc1.com = KDC1.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   509
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   510
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   511
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   512
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   513
     * default_realm = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   514
     * forwardable = true
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   515
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   516
     * [domain_realm]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   517
     * .kdc1.com = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   518
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   519
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   520
     *   KDC1.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   521
     *     kdc = host:port1
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   522
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   523
     *   KDC2.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   524
     *     kdc = host:port2
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   525
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   526
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   527
     * @param file the name of the file to write into
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   528
     * @param kdc the first (and default) KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   529
     * @param more more KDCs or extra lines (in their appearing order) to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   530
     * insert into the krb5.conf file. This method reads each argument's type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   531
     * to determine what it's for. This argument can be empty.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   532
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   533
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   534
    public static void saveConfig(String file, KDC kdc, Object... more)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   535
            throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   536
        StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   537
        sb.append("[libdefaults]\ndefault_realm = ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   538
        sb.append(kdc.realm);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   539
        sb.append("\n");
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   540
        for (Object o : more) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   541
            if (o instanceof String) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   542
                sb.append(o);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   543
                sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   544
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   545
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   546
        sb.append("\n[realms]\n");
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   547
        sb.append(kdc.realmLine());
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   548
        for (Object o : more) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   549
            if (o instanceof KDC) {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   550
                sb.append(((KDC) o).realmLine());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   551
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   552
        }
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   553
        Files.write(Paths.get(file), sb.toString().getBytes());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   554
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   555
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   556
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   557
     * Returns the service port of the KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   558
     * @return the KDC service port
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   559
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   560
    public int getPort() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   561
        return port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   562
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   563
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   564
    /**
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   565
     * Register an alias name to be referred to a different KDC for
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   566
     * resolution, according to RFC 6806.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   567
     * @param alias Alias name (i.e. user@REALM.COM).
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   568
     * @param referredKDC KDC to which the alias is referred for resolution.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   569
     */
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   570
    public void registerAlias(String alias, KDC referredKDC) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   571
        aliasReferrals.remove(alias);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   572
        aliasReferrals.put(alias, referredKDC);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   573
    }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   574
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   575
    /**
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   576
     * Register an alias to be resolved to a Principal Name locally,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   577
     * according to RFC 6806.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   578
     * @param alias Alias name (i.e. user@REALM.COM).
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   579
     * @param user Principal Name to which the alias is resolved.
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   580
     */
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   581
    public void registerAlias(String alias, String user)
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   582
            throws RealmException {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   583
        alias2Principals.remove(alias);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   584
        alias2Principals.put(alias, new PrincipalName(user));
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   585
    }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   586
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   587
    // Private helper methods
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   588
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   589
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   590
     * Private constructor, cannot be called outside.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   591
     * @param realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   592
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   593
    private KDC(String realm, String kdc) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   594
        this.realm = realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   595
        this.kdc = kdc;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   596
        this.nativeKdc = null;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   597
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   598
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   599
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   600
     * A constructor that starts the KDC service also.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   601
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   602
    protected KDC(String realm, String kdc, int port, boolean asDaemon)
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   603
            throws IOException {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   604
        this.realm = realm;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   605
        this.kdc = kdc;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   606
        this.nativeKdc = NativeKdc.get(this);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   607
        startServer(port, asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   608
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   609
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   610
     * Generates a 32-char random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   611
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   612
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   613
    private static char[] randomPassword() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   614
        char[] pass = new char[32];
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   615
        Random r = new Random();
5622
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   616
        for (int i=0; i<31; i++)
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   617
            pass[i] = (char)('a' + r.nextInt(26));
5622
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   618
        // The last char cannot be a number, otherwise, keyForUser()
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   619
        // believes it's a sign of kvno
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   620
        pass[31] = 'Z';
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   621
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   622
    }
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
     * Generates a random key for the given encryption type.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   626
     * @param eType the encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   627
     * @return the generated key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   628
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   629
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   630
    private static EncryptionKey generateRandomKey(int eType)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   631
            throws KrbException  {
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   632
        return genKey0(randomPassword(), "NOTHING", null, eType, null);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   633
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   634
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   635
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   636
     * Returns the password for a given principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   637
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   638
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   639
     * @throws sun.security.krb5.KrbException when the principal is not inside
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   640
     *         the database.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   641
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   642
    private char[] getPassword(PrincipalName p, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   643
            throws KrbException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   644
        String pn = p.toString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   645
        if (p.getRealmString() == null) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   646
            pn = pn + "@" + getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   647
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   648
        char[] pass = passwords.get(pn);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   649
        if (pass == null) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   650
            throw new KrbException(server?
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   651
                Krb5.KDC_ERR_S_PRINCIPAL_UNKNOWN:
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   652
                Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN, pn.toString());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   653
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   654
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   655
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   656
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   657
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   658
     * Returns the salt string for the principal.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   659
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   660
     * @return the salt
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   661
     */
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   662
    protected String getSalt(PrincipalName p) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   663
        String pn = p.toString();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   664
        if (p.getRealmString() == null) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   665
            pn = pn + "@" + getRealm();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   666
        }
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   667
        if (salts.containsKey(pn)) {
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   668
            return salts.get(pn);
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   669
        }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   670
        if (passwords.containsKey(pn)) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   671
            try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   672
                // Find the principal name with correct case.
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   673
                p = new PrincipalName(passwords.ceilingEntry(pn).getKey());
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   674
            } catch (RealmException re) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   675
                // Won't happen
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   676
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   677
        }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   678
        String s = p.getRealmString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   679
        if (s == null) s = getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   680
        for (String n: p.getNameStrings()) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   681
            s += n;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   682
        }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   683
        return s;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   684
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   685
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   686
    /**
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   687
     * Returns the s2kparams for the principal given the etype.
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   688
     * @param p principal
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   689
     * @param etype encryption type
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   690
     * @return the s2kparams, might be null
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   691
     */
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   692
    protected byte[] getParams(PrincipalName p, int etype) {
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   693
        switch (etype) {
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   694
            case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96:
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   695
            case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96:
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   696
            case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA256_128:
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   697
            case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA384_192:
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   698
                String pn = p.toString();
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   699
                if (p.getRealmString() == null) {
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   700
                    pn = pn + "@" + getRealm();
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   701
                }
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   702
                if (s2kparamses.containsKey(pn)) {
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   703
                    return s2kparamses.get(pn);
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   704
                }
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   705
                if (etype < EncryptedData.ETYPE_AES128_CTS_HMAC_SHA256_128) {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   706
                    return new byte[]{0, 0, 0x10, 0};
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   707
                } else {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   708
                    return new byte[]{0, 0, (byte) 0x80, 0};
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   709
                }
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   710
            default:
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   711
                return null;
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   712
        }
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   713
    }
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   714
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
   715
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   716
     * Returns the key for a given principal of the given encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   717
     * @param p the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   718
     * @param etype the encryption type
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   719
     * @param server looking for a server principal?
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   720
     * @return the key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   721
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   722
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   723
    private EncryptionKey keyForUser(PrincipalName p, int etype, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   724
            throws KrbException {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   725
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   726
            // Do not call EncryptionKey.acquireSecretKeys(), otherwise
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   727
            // the krb5.conf config file would be loaded.
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   728
            Integer kvno = null;
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4531
diff changeset
   729
            // For service whose password ending with a number, use it as kvno.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4531
diff changeset
   730
            // Kvno must be postive.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4531
diff changeset
   731
            if (p.toString().indexOf('/') > 0) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   732
                char[] pass = getPassword(p, server);
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   733
                if (Character.isDigit(pass[pass.length-1])) {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   734
                    kvno = pass[pass.length-1] - '0';
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   735
                }
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   736
            }
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   737
            return genKey0(getPassword(p, server), getSalt(p),
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   738
                    getParams(p, etype), etype, kvno);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   739
        } catch (KrbException ke) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   740
            throw ke;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   741
        } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   742
            throw new RuntimeException(e);  // should not happen
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   743
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   744
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   745
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   746
    /**
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   747
     * Returns a KerberosTime.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   748
     *
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   749
     * @param offset offset from NOW in seconds
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   750
     */
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   751
    private static KerberosTime timeAfter(int offset) {
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   752
        return new KerberosTime(new Date().getTime() + offset * 1000L);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   753
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   754
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   755
    /**
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   756
     * Generates key from password.
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   757
     */
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   758
    private static EncryptionKey genKey0(
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   759
            char[] pass, String salt, byte[] s2kparams,
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   760
            int etype, Integer kvno) throws KrbException {
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   761
        return new EncryptionKey(EncryptionKeyDotStringToKey(
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   762
                pass, salt, s2kparams, etype),
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   763
                etype, kvno);
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   764
    }
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   765
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
   766
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   767
     * Processes an incoming request and generates a response.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   768
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   769
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   770
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   771
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
   772
    protected byte[] processMessage(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   773
        if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   774
            return processAsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   775
        else
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   776
            return processTgsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   777
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   778
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   779
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   780
     * Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   781
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   782
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   783
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   784
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
   785
    protected byte[] processTgsReq(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   786
        TGSReq tgsReq = new TGSReq(in);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   787
        PrincipalName service = tgsReq.reqBody.sname;
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   788
        if (options.containsKey(KDC.Option.RESP_NT)) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   789
            service = new PrincipalName((int)options.get(KDC.Option.RESP_NT),
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   790
                    service.getNameStrings(), service.getRealm());
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   791
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   792
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   793
            System.out.println(realm + "> " + tgsReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   794
                    " sends TGS-REQ for " +
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   795
                    service + ", " + tgsReq.reqBody.kdcOptions);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   796
            KDCReqBody body = tgsReq.reqBody;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   797
            int[] eTypes = filterSupported(KDCReqBodyDotEType(body));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   798
            if (eTypes.length == 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   799
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   800
            }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   801
            int e2 = eTypes[0];     // etype for outgoing session key
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   802
            int e3 = eTypes[0];     // etype for outgoing ticket
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   803
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   804
            PAData[] pas = KDCReqDotPAData(tgsReq);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   805
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   806
            Ticket tkt = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   807
            EncTicketPart etp = null;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   808
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   809
            PrincipalName cname = null;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   810
            boolean allowForwardable = true;
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   811
            boolean isReferral = false;
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   812
            if (body.kdcOptions.get(KDCOptions.CANONICALIZE)) {
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   813
                System.out.println(realm + "> verifying referral for " +
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   814
                        body.sname.getNameString());
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   815
                KDC referral = aliasReferrals.get(body.sname.getNameString());
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   816
                if (referral != null) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   817
                    service = new PrincipalName(
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   818
                            PrincipalName.TGS_DEFAULT_SRV_NAME +
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   819
                            PrincipalName.NAME_COMPONENT_SEPARATOR_STR +
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   820
                            referral.getRealm(), PrincipalName.KRB_NT_SRV_INST,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   821
                            this.getRealm());
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   822
                    System.out.println(realm + "> referral to " +
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   823
                            referral.getRealm());
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   824
                    isReferral = true;
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   825
                }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   826
            }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
   827
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   828
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   829
                throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   830
            } else {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   831
                PrincipalName forUserCName = null;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   832
                for (PAData pa: pas) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   833
                    if (pa.getType() == Krb5.PA_TGS_REQ) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   834
                        APReq apReq = new APReq(pa.getValue());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   835
                        EncryptedData ed = apReq.authenticator;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   836
                        tkt = apReq.ticket;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   837
                        int te = tkt.encPart.getEType();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   838
                        EncryptionKey kkey = keyForUser(tkt.sname, te, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   839
                        byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   840
                        DerInputStream derIn = new DerInputStream(bb);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   841
                        DerValue der = derIn.getDerValue();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   842
                        etp = new EncTicketPart(der.toByteArray());
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   843
                        // Finally, cname will be overwritten by PA-FOR-USER
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   844
                        // if it exists.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   845
                        cname = etp.cname;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   846
                        System.out.println(realm + "> presenting a ticket of "
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   847
                                + etp.cname + " to " + tkt.sname);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   848
                    } else if (pa.getType() == Krb5.PA_FOR_USER) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   849
                        if (options.containsKey(Option.ALLOW_S4U2SELF)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   850
                            PAForUserEnc p4u = new PAForUserEnc(
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   851
                                    new DerValue(pa.getValue()), null);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   852
                            forUserCName = p4u.name;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   853
                            System.out.println(realm + "> See PA_FOR_USER "
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   854
                                    + " in the name of " + p4u.name);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   855
                        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   856
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   857
                }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   858
                if (forUserCName != null) {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   859
                    List<String> names = (List<String>)
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   860
                            options.get(Option.ALLOW_S4U2SELF);
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   861
                    if (!names.contains(cname.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   862
                        // Mimic the normal KDC behavior. When a server is not
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   863
                        // allowed to send S4U2self, do not send an error.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   864
                        // Instead, send a ticket which is useless later.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   865
                        allowForwardable = false;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   866
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   867
                    cname = forUserCName;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   868
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   869
                if (tkt == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   870
                    throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   871
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   872
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   873
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   874
            // Session key for original ticket, TGT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   875
            EncryptionKey ckey = etp.key;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   876
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   877
            // Session key for session with the service
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   878
            EncryptionKey key = generateRandomKey(e2);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   879
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   880
            // Check time, TODO
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   881
            KerberosTime from = body.from;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   882
            KerberosTime till = body.till;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   883
            if (from == null || from.isZero()) {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   884
                from = timeAfter(0);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   885
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   886
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   887
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   888
            } else if (till.isZero()) {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   889
                till = timeAfter(DEFAULT_LIFETIME);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   890
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   891
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   892
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   893
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   894
                    && allowForwardable) {
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   895
                List<String> sensitives = (List<String>)
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   896
                        options.get(Option.SENSITIVE_ACCOUNTS);
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   897
                if (sensitives != null && sensitives.contains(cname.toString())) {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   898
                    // Cannot make FORWARDABLE
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   899
                } else {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   900
                    bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   901
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   902
            }
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 31817
diff changeset
   903
            // We do not request for addresses for FORWARDED tickets
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   904
            if (options.containsKey(Option.CHECK_ADDRESSES)
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   905
                    && body.kdcOptions.get(KDCOptions.FORWARDED)
32013
e7ad0380f7be 8132111: Do not request for addresses for forwarded TGT
weijun
parents: 31817
diff changeset
   906
                    && body.addresses != null) {
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   907
                throw new KrbException(Krb5.KDC_ERR_BADOPTION);
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   908
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   909
            if (body.kdcOptions.get(KDCOptions.FORWARDED) ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   910
                    etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   911
                bFlags[Krb5.TKT_OPTS_FORWARDED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   912
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   913
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   914
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   915
                //renew = timeAfter(3600 * 24 * 7);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   916
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   917
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   918
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   919
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   920
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   921
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   922
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   923
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   924
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   925
            }
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   926
            if (body.kdcOptions.get(KDCOptions.CNAME_IN_ADDL_TKT) &&
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
   927
                    !isReferral) {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   928
                if (!options.containsKey(Option.ALLOW_S4U2PROXY)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   929
                    // Don't understand CNAME_IN_ADDL_TKT
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   930
                    throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   931
                } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   932
                    Map<String,List<String>> map = (Map<String,List<String>>)
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   933
                            options.get(Option.ALLOW_S4U2PROXY);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   934
                    Ticket second = KDCReqBodyDotFirstAdditionalTicket(body);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   935
                    EncryptionKey key2 = keyForUser(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   936
                            second.sname, second.encPart.getEType(), true);
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   937
                    byte[] bb = second.encPart.decrypt(key2, KeyUsage.KU_TICKET);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   938
                    DerInputStream derIn = new DerInputStream(bb);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   939
                    DerValue der = derIn.getDerValue();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   940
                    EncTicketPart tktEncPart = new EncTicketPart(der.toByteArray());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   941
                    if (!tktEncPart.flags.get(Krb5.TKT_OPTS_FORWARDABLE)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   942
                        //throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   943
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   944
                    PrincipalName client = tktEncPart.cname;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   945
                    System.out.println(realm + "> and an additional ticket of "
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   946
                            + client + " to " + second.sname);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   947
                    if (map.containsKey(cname.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   948
                        if (map.get(cname.toString()).contains(service.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   949
                            System.out.println(realm + "> S4U2proxy OK");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   950
                        } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   951
                            throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   952
                        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   953
                    } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   954
                        throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   955
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   956
                    cname = client;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   957
                }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   958
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   959
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   960
            String okAsDelegate = (String)options.get(Option.OK_AS_DELEGATE);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   961
            if (okAsDelegate != null && (
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   962
                    okAsDelegate.isEmpty() ||
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   963
                    okAsDelegate.contains(service.getNameString()))) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   964
                bFlags[Krb5.TKT_OPTS_DELEGATE] = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   965
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   966
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   967
47005
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   968
            KerberosTime renewTill = etp.renewTill;
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   969
            if (renewTill != null && body.kdcOptions.get(KDCOptions.RENEW)) {
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   970
                // till should never pass renewTill
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   971
                if (till.greaterThan(renewTill)) {
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   972
                    till = renewTill;
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   973
                }
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   974
                if (System.getProperty("test.set.null.renew") != null) {
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   975
                    // Testing 8186576, see NullRenewUntil.java.
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   976
                    renewTill = null;
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   977
                }
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   978
            }
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   979
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   980
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   981
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   982
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   983
                    key,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   984
                    cname,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   985
                    new TransitedEncoding(1, new byte[0]),  // TODO
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
   986
                    timeAfter(0),
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
   987
                    from,
47005
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
   988
                    till, renewTill,
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   989
                    body.addresses != null ? body.addresses
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   990
                            : etp.caddr,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   991
                    null);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   992
            EncryptionKey skey = keyForUser(service, e3, true);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   993
            if (skey == null) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   994
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   995
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   996
            Ticket t = new Ticket(
45276
633535faf95d 8178794: krb5 client should ignore sname in incoming tickets
weijun
parents: 39484
diff changeset
   997
                    System.getProperty("test.kdc.diff.sname") != null ?
633535faf95d 8178794: krb5 client should ignore sname in incoming tickets
weijun
parents: 39484
diff changeset
   998
                        new PrincipalName("xx" + service.toString()) :
633535faf95d 8178794: krb5 client should ignore sname in incoming tickets
weijun
parents: 39484
diff changeset
   999
                        service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1000
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1001
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1002
            EncTGSRepPart enc_part = new EncTGSRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1003
                    key,
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1004
                    new LastReq(new LastReqEntry[] {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1005
                        new LastReqEntry(0, timeAfter(-10))
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1006
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1007
                    body.getNonce(),    // TODO: detect replay
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1008
                    timeAfter(3600 * 24),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1009
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1010
                    tFlags,
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1011
                    timeAfter(0),
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1012
                    from,
47005
dbfaf076da58 8186576: KerberosTicket does not properly handle renewable tickets at the end of their lifetime
weijun
parents: 45276
diff changeset
  1013
                    till, renewTill,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1014
                    service,
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1015
                    body.addresses,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1016
                    null
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1017
                    );
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1018
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(),
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1019
                    KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1020
            TGSRep tgsRep = new TGSRep(null,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1021
                    cname,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1022
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1023
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1024
            System.out.println("     Return " + tgsRep.cname
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1025
                    + " ticket for " + tgsRep.ticket.sname + ", flags "
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1026
                    + tFlags);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1027
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1028
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1029
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1030
                    true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1031
            return out.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1032
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1033
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1034
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1035
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1036
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1037
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1038
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1039
                kerr = new KRBError(null, null, null,
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1040
                        timeAfter(0),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1041
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1042
                        ke.returnCode(),
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1043
                        body.cname,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1044
                        service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1045
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1046
                        null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1047
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1048
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1049
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1050
    }
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
     * Processes a AS_REQ and generates a AS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1054
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1055
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1056
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1057
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
  1058
    protected byte[] processAsReq(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1059
        ASReq asReq = new ASReq(in);
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1060
        byte[] asReqbytes = asReq.asn1Encode();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1061
        int[] eTypes = null;
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
  1062
        List<PAData> outPAs = new ArrayList<>();
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1063
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1064
        PrincipalName service = asReq.reqBody.sname;
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1065
        if (options.containsKey(KDC.Option.RESP_NT)) {
33282
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32013
diff changeset
  1066
            service = new PrincipalName((int)options.get(KDC.Option.RESP_NT),
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32013
diff changeset
  1067
                    service.getNameStrings(),
00f3c40fd3af 8048030: Expectations should be consistent
weijun
parents: 32013
diff changeset
  1068
                    Realm.getDefault());
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1069
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1070
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1071
            System.out.println(realm + "> " + asReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1072
                    " sends AS-REQ for " +
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1073
                    service + ", " + asReq.reqBody.kdcOptions);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1074
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1075
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1076
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1077
            eTypes = filterSupported(KDCReqBodyDotEType(body));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1078
            if (eTypes.length == 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1079
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1080
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1081
            int eType = eTypes[0];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1082
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 55598
diff changeset
  1083
            if (body.kdcOptions.get(KDCOptions.CANONICALIZE)) {
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1084
                PrincipalName principal = alias2Principals.get(
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1085
                        body.cname.getNameString());
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1086
                if (principal != null) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1087
                    body.cname = principal;
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1088
                } else {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1089
                    KDC referral = aliasReferrals.get(body.cname.getNameString());
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1090
                    if (referral != null) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1091
                        body.cname = new PrincipalName(
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1092
                                PrincipalName.TGS_DEFAULT_SRV_NAME,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1093
                                PrincipalName.KRB_NT_SRV_INST,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1094
                                referral.getRealm());
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1095
                        throw new KrbException(Krb5.KRB_ERR_WRONG_REALM);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1096
                    }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1097
                }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1098
            }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1099
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
  1100
            EncryptionKey ckey = keyForUser(body.cname, eType, false);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1101
            EncryptionKey skey = keyForUser(service, eType, true);
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1102
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1103
            if (options.containsKey(KDC.Option.ONLY_RC4_TGT)) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1104
                int tgtEType = EncryptedData.ETYPE_ARCFOUR_HMAC;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1105
                boolean found = false;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1106
                for (int i=0; i<eTypes.length; i++) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1107
                    if (eTypes[i] == tgtEType) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1108
                        found = true;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1109
                        break;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1110
                    }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1111
                }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1112
                if (!found) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1113
                    throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1114
                }
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1115
                skey = keyForUser(service, tgtEType, true);
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1116
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1117
            if (ckey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1118
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1119
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1120
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1121
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1122
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1123
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1124
            // Session key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1125
            EncryptionKey key = generateRandomKey(eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1126
            // Check time, TODO
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1127
            KerberosTime from = body.from;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1128
            KerberosTime till = body.till;
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
  1129
            KerberosTime rtime = body.rtime;
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1130
            if (from == null || from.isZero()) {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1131
                from = timeAfter(0);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1132
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1133
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1134
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1135
            } else if (till.isZero()) {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1136
                till = timeAfter(DEFAULT_LIFETIME);
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1137
            } else if (till.greaterThan(timeAfter(24 * 3600))
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1138
                     && System.getProperty("test.kdc.force.till") == null) {
31817
1cd8bae1ef0c 8131051: KDC might issue a renewable ticket even if not requested
weijun
parents: 31801
diff changeset
  1139
                // If till is more than 1 day later, make it renewable
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1140
                till = timeAfter(DEFAULT_LIFETIME);
31817
1cd8bae1ef0c 8131051: KDC might issue a renewable ticket even if not requested
weijun
parents: 31801
diff changeset
  1141
                body.kdcOptions.set(KDCOptions.RENEWABLE, true);
1cd8bae1ef0c 8131051: KDC might issue a renewable ticket even if not requested
weijun
parents: 31801
diff changeset
  1142
                if (rtime == null) rtime = till;
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
  1143
            }
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
  1144
            if (rtime == null && body.kdcOptions.get(KDCOptions.RENEWABLE)) {
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1145
                rtime = timeAfter(DEFAULT_RENEWTIME);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1146
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1147
            //body.from
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1148
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1149
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1150
                List<String> sensitives = (List<String>)
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1151
                        options.get(Option.SENSITIVE_ACCOUNTS);
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1152
                if (sensitives != null
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1153
                        && sensitives.contains(body.cname.toString())) {
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1154
                    // Cannot make FORWARDABLE
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1155
                } else {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1156
                    bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1157
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1158
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1159
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1160
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1161
                //renew = timeAfter(3600 * 24 * 7);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1162
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1163
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1164
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1165
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1166
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1167
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1168
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1169
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1170
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1171
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1172
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1173
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1174
            // Creating PA-DATA
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1175
            DerValue[] pas2 = null, pas = null;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1176
            if (options.containsKey(KDC.Option.DUP_ETYPE)) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1177
                int n = (Integer)options.get(KDC.Option.DUP_ETYPE);
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1178
                switch (n) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1179
                    case 1:     // customer's case in 7067974
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1180
                        pas2 = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1181
                            new DerValue(new ETypeInfo2(1, null, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1182
                            new DerValue(new ETypeInfo2(1, "", null).asn1Encode()),
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1183
                            new DerValue(new ETypeInfo2(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1184
                                    1, realm, new byte[]{1}).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1185
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1186
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1187
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1188
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
  1189
                            new DerValue(new ETypeInfo(1, realm).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1190
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1191
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1192
                    case 2:     // we still reject non-null s2kparams and prefer E2 over E
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1193
                        pas2 = new DerValue[] {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1194
                            new DerValue(new ETypeInfo2(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1195
                                    1, realm, new byte[]{1}).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1196
                            new DerValue(new ETypeInfo2(1, null, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1197
                            new DerValue(new ETypeInfo2(1, "", null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1198
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1199
                        pas = new DerValue[] {
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
  1200
                            new DerValue(new ETypeInfo(1, realm).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1201
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1202
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1203
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1204
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1205
                    case 3:     // but only E is wrong
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1206
                        pas = new DerValue[] {
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
  1207
                            new DerValue(new ETypeInfo(1, realm).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1208
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1209
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1210
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1211
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1212
                    case 4:     // we also ignore rc4-hmac
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1213
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1214
                            new DerValue(new ETypeInfo(23, "ANYTHING").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1215
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1216
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1217
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1218
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1219
                    case 5:     // "" should be wrong, but we accept it now
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1220
                                // See s.s.k.internal.PAData$SaltAndParams
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1221
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1222
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1223
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1224
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1225
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1226
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1227
            } else {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1228
                int[] epas = eTypes;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1229
                if (options.containsKey(KDC.Option.RC4_FIRST_PREAUTH)) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1230
                    for (int i=1; i<epas.length; i++) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1231
                        if (epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1232
                            epas[i] = epas[0];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1233
                            epas[0] = EncryptedData.ETYPE_ARCFOUR_HMAC;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1234
                            break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1235
                        }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1236
                    };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1237
                } else if (options.containsKey(KDC.Option.ONLY_ONE_PREAUTH)) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1238
                    epas = new int[] { eTypes[0] };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1239
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1240
                pas2 = new DerValue[epas.length];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1241
                for (int i=0; i<epas.length; i++) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1242
                    pas2[i] = new DerValue(new ETypeInfo2(
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1243
                            epas[i],
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1244
                            epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ?
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1245
                                null : getSalt(body.cname),
47226
4f029f064481 8186831: Kerberos ignores PA-DATA with a non-null s2kparams
weijun
parents: 47216
diff changeset
  1246
                            getParams(body.cname, epas[i])).asn1Encode());
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1247
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1248
                boolean allOld = true;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1249
                for (int i: eTypes) {
48651
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
  1250
                    if (i >= EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 &&
67abfee27e69 8014628: Support AES Encryption with HMAC-SHA2 for Kerberos 5
weijun
parents: 47227
diff changeset
  1251
                            i != EncryptedData.ETYPE_ARCFOUR_HMAC) {
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1252
                        allOld = false;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1253
                        break;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1254
                    }
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1255
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1256
                if (allOld) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1257
                    pas = new DerValue[epas.length];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1258
                    for (int i=0; i<epas.length; i++) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1259
                        pas[i] = new DerValue(new ETypeInfo(
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1260
                                epas[i],
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1261
                                epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ?
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1262
                                    null : getSalt(body.cname)
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1263
                                ).asn1Encode());
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1264
                    }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1265
                }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1266
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1267
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1268
            DerOutputStream eid;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1269
            if (pas2 != null) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1270
                eid = new DerOutputStream();
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1271
                eid.putSequence(pas2);
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1272
                outPAs.add(new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray()));
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1273
            }
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1274
            if (pas != null) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1275
                eid = new DerOutputStream();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1276
                eid.putSequence(pas);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1277
                outPAs.add(new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray()));
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1278
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1279
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1280
            PAData[] inPAs = KDCReqDotPAData(asReq);
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1281
            List<PAData> enc_outPAs = new ArrayList<>();
55598
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1282
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1283
            byte[] paEncTimestamp = null;
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1284
            if (inPAs != null) {
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1285
                for (PAData inPA : inPAs) {
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1286
                    if (inPA.getType() == Krb5.PA_ENC_TIMESTAMP) {
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1287
                        paEncTimestamp = inPA.getValue();
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1288
                    }
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1289
                }
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1290
            }
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1291
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1292
            if (paEncTimestamp == null) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1293
                Object preauth = options.get(Option.PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1294
                if (preauth == null || preauth.equals(Boolean.TRUE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1295
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1296
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1297
            } else {
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1298
                EncryptionKey pakey = null;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1299
                try {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1300
                    EncryptedData data = newEncryptedData(
55598
091ea1a285dc 8227061: KDC.java test behaves incorrectly when AS-REQ contains a PAData not PA-ENC-TS-ENC
weijun
parents: 55258
diff changeset
  1301
                            new DerValue(paEncTimestamp));
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1302
                    pakey = keyForUser(body.cname, data.getEType(), false);
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1303
                    data.decrypt(pakey, KeyUsage.KU_PA_ENC_TS);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1304
                } catch (Exception e) {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1305
                    KrbException ke = new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1306
                    ke.initCause(e);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1307
                    throw ke;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1308
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1309
                bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true;
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1310
                for (PAData pa : inPAs) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1311
                    if (pa.getType() == Krb5.PA_REQ_ENC_PA_REP) {
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1312
                        Checksum ckSum = new Checksum(
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1313
                                Checksum.CKSUMTYPE_HMAC_SHA1_96_AES128,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1314
                                asReqbytes, ckey, KeyUsage.KU_AS_REQ);
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1315
                        enc_outPAs.add(new PAData(Krb5.PA_REQ_ENC_PA_REP,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1316
                                ckSum.asn1Encode()));
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1317
                        bFlags[Krb5.TKT_OPTS_ENC_PA_REP] = true;
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1318
                        break;
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1319
                    }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1320
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1321
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1322
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1323
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1324
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1325
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1326
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1327
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1328
                    new TransitedEncoding(1, new byte[0]),
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1329
                    timeAfter(0),
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1330
                    from,
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
  1331
                    till, rtime,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1332
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1333
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1334
            Ticket t = new Ticket(
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1335
                    service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1336
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1337
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1338
            EncASRepPart enc_part = new EncASRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1339
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1340
                    new LastReq(new LastReqEntry[]{
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1341
                        new LastReqEntry(0, timeAfter(-10))
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1342
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1343
                    body.getNonce(),    // TODO: detect replay?
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1344
                    timeAfter(3600 * 24),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1345
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1346
                    tFlags,
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1347
                    timeAfter(0),
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1348
                    from,
27946
9f99b93cbbb2 8044500: Add kinit options and krb5.conf flags that allow users to obtain renewable tickets and specify ticket lifetimes
weijun
parents: 24631
diff changeset
  1349
                    till, rtime,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1350
                    service,
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1351
                    body.addresses,
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1352
                    enc_outPAs.toArray(new PAData[enc_outPAs.size()])
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1353
                    );
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1354
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(),
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1355
                    KeyUsage.KU_ENC_AS_REP_PART);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1356
            ASRep asRep = new ASRep(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1357
                    outPAs.toArray(new PAData[outPAs.size()]),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1358
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1359
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1360
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1361
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1362
            System.out.println("     Return " + asRep.cname
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1363
                    + " ticket for " + asRep.ticket.sname + ", flags "
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1364
                    + tFlags);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1365
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1366
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1367
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1368
                    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
  1369
            byte[] result = out.toByteArray();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1370
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1371
            // Added feature:
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1372
            // 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
  1373
            // by the system property below.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1374
            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
  1375
            if (ccache != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1376
                asRep.encKDCRepPart = enc_part;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1377
                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
  1378
                    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
  1379
                CredentialsCache cache =
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1380
                    CredentialsCache.create(asReq.reqBody.cname, ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1381
                if (cache == null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1382
                   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
  1383
                                         ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1384
                }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1385
                cache.update(credentials);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1386
                cache.save();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1387
            }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1388
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1389
            return result;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1390
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1391
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1392
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1393
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1394
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1395
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1396
            byte[] eData = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1397
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1398
                if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1399
                        ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) {
55258
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1400
                    outPAs.add(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]));
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1401
                }
d65d3c37232c 8215032: Support Kerberos cross-realm referrals (RFC 6806)
mbalao
parents: 53273
diff changeset
  1402
                if (outPAs.size() > 0) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1403
                    DerOutputStream bytes = new DerOutputStream();
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1404
                    for (PAData p: outPAs) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1405
                        bytes.write(p.asn1Encode());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1406
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1407
                    DerOutputStream temp = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1408
                    temp.write(DerValue.tag_Sequence, bytes);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1409
                    eData = temp.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1410
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1411
                kerr = new KRBError(null, null, null,
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 48668
diff changeset
  1412
                        timeAfter(0),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1413
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1414
                        ke.returnCode(),
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1415
                        body.cname,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1416
                        service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1417
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1418
                        eData);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1419
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1420
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1421
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1422
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1423
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1424
    private int[] filterSupported(int[] input) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1425
        int count = 0;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1426
        for (int i = 0; i < input.length; i++) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1427
            if (!EType.isSupported(input[i])) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1428
                continue;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1429
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1430
            if (SUPPORTED_ETYPES != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1431
                boolean supported = false;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1432
                for (String se : SUPPORTED_ETYPES.split(",")) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1433
                    if (Config.getType(se) == input[i]) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1434
                        supported = true;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1435
                        break;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1436
                    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1437
                }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1438
                if (!supported) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1439
                    continue;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1440
                }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1441
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1442
            if (count != i) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1443
                input[count] = input[i];
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1444
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1445
            count++;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1446
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1447
        if (count != input.length) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1448
            input = Arrays.copyOf(input, count);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1449
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1450
        return input;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1451
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1452
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1453
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1454
     * Generates a line for a KDC to put inside [realms] of krb5.conf
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1455
     * @return REALM.NAME = { kdc = host:port etc }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1456
     */
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1457
    private String realmLine() {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1458
        StringBuilder sb = new StringBuilder();
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1459
        sb.append(realm).append(" = {\n    kdc = ")
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1460
                .append(kdc).append(':').append(port).append('\n');
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1461
        for (String s: conf) {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1462
            sb.append("    ").append(s).append('\n');
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1463
        }
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1464
        return sb.append("}\n").toString();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1465
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1466
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1467
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1468
     * Start the KDC service. This server listens on both UDP and TCP using
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1469
     * the same port number. It uses three threads to deal with requests.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1470
     * They can be set to daemon threads if requested.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1471
     * @param port the port number to listen to. If zero, a random available
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1472
     *  port no less than 8000 will be chosen and used.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1473
     * @param asDaemon true if the KDC threads should be daemons
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1474
     * @throws java.io.IOException for any communication error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1475
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1476
    protected void startServer(int port, boolean asDaemon) throws IOException {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1477
        if (nativeKdc != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1478
            startNativeServer(port, asDaemon);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1479
        } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1480
            startJavaServer(port, asDaemon);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1481
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1482
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1483
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1484
    private void startNativeServer(int port, boolean asDaemon) throws IOException {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1485
        nativeKdc.prepare();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1486
        nativeKdc.init();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1487
        kdcProc = nativeKdc.kdc();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1488
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1489
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1490
    private void startJavaServer(int port, boolean asDaemon) throws IOException {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1491
        if (port > 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1492
            u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1493
            t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1494
        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1495
            while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1496
                // Try to find a port number that's both TCP and UDP free
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1497
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1498
                    port = 8000 + new java.util.Random().nextInt(10000);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1499
                    u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1500
                    u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1501
                    t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1502
                    break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1503
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1504
                    if (u1 != null) u1.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1505
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1506
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1507
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1508
        final DatagramSocket udp = u1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1509
        final ServerSocket tcp = t1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1510
        System.out.println("Start KDC on " + port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1511
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1512
        this.port = port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1513
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1514
        // The UDP consumer
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1515
        thread1 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1516
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1517
                udpConsumerReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1518
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1519
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1520
                        byte[] inbuf = new byte[8192];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1521
                        DatagramPacket p = new DatagramPacket(inbuf, inbuf.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1522
                        udp.receive(p);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1523
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1524
                        System.out.println(">>>>> UDP packet received");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1525
                        q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1526
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1527
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1528
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1529
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1530
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1531
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1532
        thread1.setDaemon(asDaemon);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1533
        thread1.start();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1534
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1535
        // The TCP consumer
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1536
        thread2 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1537
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1538
                tcpConsumerReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1539
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1540
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1541
                        Socket socket = tcp.accept();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1542
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1543
                        System.out.println(">>>>> TCP connection established");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1544
                        DataInputStream in = new DataInputStream(socket.getInputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1545
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
34438
9ecd0f296185 8138638: Security tests using jdk/test/sun/security/krb5/auto/KDC.java fail intermittently with OutOfMemoryError
weijun
parents: 33282
diff changeset
  1546
                        int len = in.readInt();
9ecd0f296185 8138638: Security tests using jdk/test/sun/security/krb5/auto/KDC.java fail intermittently with OutOfMemoryError
weijun
parents: 33282
diff changeset
  1547
                        if (len > 65535) {
9ecd0f296185 8138638: Security tests using jdk/test/sun/security/krb5/auto/KDC.java fail intermittently with OutOfMemoryError
weijun
parents: 33282
diff changeset
  1548
                            throw new Exception("Huge request not supported");
9ecd0f296185 8138638: Security tests using jdk/test/sun/security/krb5/auto/KDC.java fail intermittently with OutOfMemoryError
weijun
parents: 33282
diff changeset
  1549
                        }
9ecd0f296185 8138638: Security tests using jdk/test/sun/security/krb5/auto/KDC.java fail intermittently with OutOfMemoryError
weijun
parents: 33282
diff changeset
  1550
                        byte[] token = new byte[len];
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1551
                        in.readFully(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1552
                        q.put(new Job(processMessage(token), socket, out));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1553
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1554
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1555
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1556
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1557
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1558
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1559
        thread2.setDaemon(asDaemon);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1560
        thread2.start();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1561
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1562
        // The dispatcher
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1563
        thread3 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1564
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1565
                dispatcherReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1566
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1567
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1568
                        q.take().send();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1569
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1570
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1571
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1572
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1573
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1574
        thread3.setDaemon(true);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1575
        thread3.start();
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1576
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1577
        // wait for the KDC is ready
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1578
        try {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1579
            while (!isReady()) {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1580
                Thread.sleep(100);
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1581
            }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1582
        } catch(InterruptedException e) {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1583
            throw new IOException(e);
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1584
        }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1585
    }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1586
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1587
    public void kinit(String user, String ccache) throws Exception {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1588
        if (user.indexOf('@') < 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1589
            user = user + "@" + realm;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1590
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1591
        if (nativeKdc != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1592
            nativeKdc.kinit(user, ccache);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1593
        } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1594
            Context.fromUserPass(user, passwords.get(user), false)
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1595
                    .ccache(ccache);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1596
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1597
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1598
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1599
    boolean isReady() {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1600
        return udpConsumerReady && tcpConsumerReady && dispatcherReady;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1601
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1602
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1603
    public void terminate() {
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1604
        if (nativeKdc != null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1605
            System.out.println("Killing kdc...");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1606
            kdcProc.destroyForcibly();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1607
            System.out.println("Done");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1608
        } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1609
            try {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1610
                thread1.stop();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1611
                thread2.stop();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1612
                thread3.stop();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1613
                u1.close();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1614
                t1.close();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1615
            } catch (Exception e) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1616
                // OK
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1617
            }
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1618
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1619
    }
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1620
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1621
    public static KDC startKDC(final String host, final String krbConfFileName,
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1622
            final String realm, final Map<String, String> principals,
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1623
            final String ktab, final KtabMode mode) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1624
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1625
        KDC kdc;
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1626
        try {
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1627
            kdc = KDC.create(realm, host, 0, true);
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1628
            kdc.setOption(KDC.Option.PREAUTH_REQUIRED, Boolean.FALSE);
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1629
            if (krbConfFileName != null) {
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1630
                KDC.saveConfig(krbConfFileName, kdc);
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1631
            }
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1632
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1633
            // Add principals
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1634
            if (principals != null) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1635
                principals.forEach((name, password) -> {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1636
                    if (password == null || password.isEmpty()) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1637
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1638
                                "KDC:add a principal '%s' with a random " +
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1639
                                        "password", name));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1640
                        kdc.addPrincipalRandKey(name);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1641
                    } else {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1642
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1643
                                "KDC:add a principal '%s' with '%s' password",
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1644
                                name, password));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1645
                        kdc.addPrincipal(name, password.toCharArray());
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1646
                    }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1647
                });
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1648
            }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1649
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1650
            // Create or append keys to existing keytab file
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1651
            if (ktab != null) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1652
                File ktabFile = new File(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1653
                switch(mode) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1654
                    case APPEND:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1655
                        if (ktabFile.exists()) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1656
                            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1657
                                    "KDC:append keys to an exising keytab "
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1658
                                    + "file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1659
                            kdc.appendKtab(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1660
                        } else {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1661
                            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1662
                                    "KDC:create a new keytab file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1663
                            kdc.writeKtab(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1664
                        }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1665
                        break;
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1666
                    case EXISTING:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1667
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1668
                                "KDC:use an existing keytab file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1669
                        break;
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1670
                    default:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1671
                        throw new RuntimeException(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1672
                                "KDC:unsupported keytab mode: %s", mode));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1673
                }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1674
            }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1675
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1676
            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1677
                    "KDC: started on %s:%s with '%s' realm",
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1678
                    host, kdc.getPort(), realm));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1679
        } catch (Exception e) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1680
            throw new RuntimeException("KDC: unexpected exception", e);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1681
        }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1682
31801
1297473ab237 8075297: Tests for RFEs 4515853 and 4745056
asmotrak
parents: 31643
diff changeset
  1683
        return kdc;
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1684
    }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1685
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1686
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1687
     * Helper class to encapsulate a job in a KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1688
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1689
    private static class Job {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1690
        byte[] token;           // The received request at creation time and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1691
                                // the response at send time
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1692
        Socket s;               // The TCP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1693
        DataOutputStream out;   // The OutputStream of the TCP socket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1694
        DatagramSocket s2;      // The UDP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1695
        DatagramPacket dp;      // The incoming UDP datagram packet
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1696
        boolean useTCP;         // Whether TCP or UDP is used
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1697
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1698
        // Creates a job object for TCP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1699
        Job(byte[] token, Socket s, DataOutputStream out) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1700
            useTCP = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1701
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1702
            this.s = s;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1703
            this.out = out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1704
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1705
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1706
        // Creates a job object for UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1707
        Job(byte[] token, DatagramSocket s2, DatagramPacket dp) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1708
            useTCP = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1709
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1710
            this.s2 = s2;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1711
            this.dp = dp;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1712
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1713
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1714
        // Sends the output back to the client
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1715
        void send() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1716
            try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1717
                if (useTCP) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1718
                    System.out.println(">>>>> TCP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1719
                    out.writeInt(token.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1720
                    out.write(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1721
                    s.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1722
                } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1723
                    System.out.println(">>>>> UDP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1724
                    s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1725
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1726
            } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1727
                e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1728
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1729
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1730
    }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1731
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1732
    /**
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1733
     * A native KDC using the binaries in nativePath. Attention:
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1734
     * this is using binaries, not an existing KDC instance.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1735
     * An implementation of this takes care of configuration,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1736
     * principal db managing and KDC startup.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1737
     */
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1738
    static abstract class NativeKdc {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1739
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1740
        protected Map<String,String> env;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1741
        protected String nativePath;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1742
        protected String base;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1743
        protected String realm;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1744
        protected int port;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1745
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1746
        NativeKdc(String nativePath, KDC kdc) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1747
            if (kdc.port == 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1748
                kdc.port = 8000 + new java.util.Random().nextInt(10000);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1749
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1750
            this.nativePath = nativePath;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1751
            this.realm = kdc.realm;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1752
            this.port = kdc.port;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1753
            this.base = Paths.get("" + port).toAbsolutePath().toString();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1754
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1755
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1756
        // Add a new principal
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1757
        abstract void addPrincipal(String user, String pass);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1758
        // Add a keytab entry
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1759
        abstract void ktadd(String user, String ktab);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1760
        // Initialize KDC
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1761
        abstract void init();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1762
        // Start kdc
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1763
        abstract Process kdc();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1764
        // Configuration
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1765
        abstract void prepare();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1766
        // Fill ccache
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1767
        abstract void kinit(String user, String ccache);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1768
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1769
        static NativeKdc get(KDC kdc) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1770
            String prop = System.getProperty("native.kdc.path");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1771
            if (prop == null) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1772
                return null;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1773
            } else if (Files.exists(Paths.get(prop, "sbin/krb5kdc"))) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1774
                return new MIT(true, prop, kdc);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1775
            } else if (Files.exists(Paths.get(prop, "kdc/krb5kdc"))) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1776
                return new MIT(false, prop, kdc);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1777
            } else if (Files.exists(Paths.get(prop, "libexec/kdc"))) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1778
                return new Heimdal(prop, kdc);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1779
            } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1780
                throw new IllegalArgumentException("Strange " + prop);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1781
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1782
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1783
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1784
        Process run(boolean wait, String... cmd) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1785
            try {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1786
                System.out.println("Running " + cmd2str(env, cmd));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1787
                ProcessBuilder pb = new ProcessBuilder();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1788
                pb.inheritIO();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1789
                pb.environment().putAll(env);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1790
                Process p = pb.command(cmd).start();
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1791
                if (wait) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1792
                    if (p.waitFor() < 0) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1793
                        throw new RuntimeException("exit code is not null");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1794
                    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1795
                    return null;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1796
                } else {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1797
                    return p;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1798
                }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1799
            } catch (Exception e) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1800
                throw new RuntimeException(e);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1801
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1802
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1803
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1804
        private String cmd2str(Map<String,String> env, String... cmd) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1805
            return env.entrySet().stream().map(e -> e.getKey()+"="+e.getValue())
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1806
                    .collect(Collectors.joining(" ")) + " " +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1807
                    Stream.of(cmd).collect(Collectors.joining(" "));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1808
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1809
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1810
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1811
    // Heimdal KDC. Build your own and run "make install" to nativePath.
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1812
    static class Heimdal extends NativeKdc {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1813
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1814
        Heimdal(String nativePath, KDC kdc) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1815
            super(nativePath, kdc);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1816
            this.env = Map.of(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1817
                    "KRB5_CONFIG", base + "/krb5.conf",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1818
                    "KRB5_TRACE", "/dev/stderr",
53273
bbc79e0ec9ee 8216265: [testbug] Introduce Platform.sharedLibraryPathVariableName() and adapt all tests.
goetz
parents: 50750
diff changeset
  1819
                    Platform.sharedLibraryPathVariableName(), nativePath + "/lib");
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1820
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1821
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1822
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1823
        public void addPrincipal(String user, String pass) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1824
            run(true, nativePath + "/bin/kadmin", "-l", "-r", realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1825
                    "add", "-p", pass, "--use-defaults", user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1826
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1827
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1828
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1829
        public void ktadd(String user, String ktab) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1830
            run(true, nativePath + "/bin/kadmin", "-l", "-r", realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1831
                    "ext_keytab", "-k", ktab, user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1832
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1833
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1834
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1835
        public void init() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1836
            run(true, nativePath + "/bin/kadmin",  "-l",  "-r", realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1837
                    "init", "--realm-max-ticket-life=1day",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1838
                    "--realm-max-renewable-life=1month", realm);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1839
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1840
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1841
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1842
        public Process kdc() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1843
            return run(false, nativePath + "/libexec/kdc",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1844
                    "--addresses=127.0.0.1", "-P", "" + port);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1845
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1846
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1847
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1848
        public void prepare() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1849
            try {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1850
                Files.createDirectory(Paths.get(base));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1851
                Files.write(Paths.get(base + "/krb5.conf"), Arrays.asList(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1852
                        "[libdefaults]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1853
                        "default_realm = " + realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1854
                        "default_keytab_name = FILE:" + base + "/krb5.keytab",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1855
                        "forwardable = true",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1856
                        "dns_lookup_kdc = no",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1857
                        "dns_lookup_realm = no",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1858
                        "dns_canonicalize_hostname = false",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1859
                        "\n[realms]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1860
                        realm + " = {",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1861
                        "  kdc = localhost:" + port,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1862
                        "}",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1863
                        "\n[kdc]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1864
                        "db-dir = " + base,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1865
                        "database = {",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1866
                        "    label = {",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1867
                        "        dbname = " + base + "/current-db",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1868
                        "        realm = " + realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1869
                        "        mkey_file = " + base + "/mkey.file",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1870
                        "        acl_file = " + base + "/heimdal.acl",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1871
                        "        log_file = " + base + "/current.log",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1872
                        "    }",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1873
                        "}",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1874
                        SUPPORTED_ETYPES == null ? ""
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1875
                                : ("\n[kadmin]\ndefault_keys = "
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1876
                                + (SUPPORTED_ETYPES + ",")
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1877
                                        .replaceAll(",", ":pw-salt ")),
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1878
                        "\n[logging]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1879
                        "kdc = 0-/FILE:" + base + "/messages.log",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1880
                        "krb5 = 0-/FILE:" + base + "/messages.log",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1881
                        "default = 0-/FILE:" + base + "/messages.log"
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1882
                ));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1883
            } catch (IOException e) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1884
                throw new UncheckedIOException(e);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1885
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1886
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1887
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1888
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1889
        void kinit(String user, String ccache) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1890
            String tmpName = base + "/" + user + "." +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1891
                    System.identityHashCode(this) + ".keytab";
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1892
            ktadd(user, tmpName);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1893
            run(true, nativePath + "/bin/kinit",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1894
                    "-f", "-t", tmpName, "-c", ccache, user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1895
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1896
    }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1897
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1898
    // MIT krb5 KDC. Make your own exploded (install == false), or
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1899
    // "make install" into nativePath (install == true).
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1900
    static class MIT extends NativeKdc {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1901
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1902
        private boolean install; // "make install" or "make"
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1903
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1904
        MIT(boolean install, String nativePath, KDC kdc) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1905
            super(nativePath, kdc);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1906
            this.install = install;
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1907
            this.env = Map.of(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1908
                    "KRB5_KDC_PROFILE", base + "/kdc.conf",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1909
                    "KRB5_CONFIG", base + "/krb5.conf",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1910
                    "KRB5_TRACE", "/dev/stderr",
53273
bbc79e0ec9ee 8216265: [testbug] Introduce Platform.sharedLibraryPathVariableName() and adapt all tests.
goetz
parents: 50750
diff changeset
  1911
                    Platform.sharedLibraryPathVariableName(), nativePath + "/lib");
47227
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1912
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1913
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1914
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1915
        public void addPrincipal(String user, String pass) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1916
            run(true, nativePath +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1917
                    (install ? "/sbin/" : "/kadmin/cli/") + "kadmin.local",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1918
                    "-q", "addprinc -pw " + pass + " " + user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1919
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1920
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1921
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1922
        public void ktadd(String user, String ktab) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1923
            run(true, nativePath +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1924
                    (install ? "/sbin/" : "/kadmin/cli/") + "kadmin.local",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1925
                    "-q", "ktadd -k " + ktab + " -norandkey " + user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1926
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1927
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1928
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1929
        public void init() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1930
            run(true, nativePath +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1931
                    (install ? "/sbin/" : "/kadmin/dbutil/") + "kdb5_util",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1932
                    "create", "-s", "-W", "-P", "olala");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1933
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1934
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1935
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1936
        public Process kdc() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1937
            return run(false, nativePath +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1938
                    (install ? "/sbin/" : "/kdc/") + "krb5kdc",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1939
                    "-n");
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1940
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1941
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1942
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1943
        public void prepare() {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1944
            try {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1945
                Files.createDirectory(Paths.get(base));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1946
                Files.write(Paths.get(base + "/kdc.conf"), Arrays.asList(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1947
                        "[kdcdefaults]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1948
                        "\n[realms]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1949
                        realm + "= {",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1950
                        "  kdc_listen = " + this.port,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1951
                        "  kdc_tcp_listen = " + this.port,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1952
                        "  database_name = " + base + "/principal",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1953
                        "  key_stash_file = " + base + "/.k5.ATHENA.MIT.EDU",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1954
                        SUPPORTED_ETYPES == null ? ""
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1955
                                : ("  supported_enctypes = "
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1956
                                + (SUPPORTED_ETYPES + ",")
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1957
                                        .replaceAll(",", ":normal ")),
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1958
                        "}"
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1959
                ));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1960
                Files.write(Paths.get(base + "/krb5.conf"), Arrays.asList(
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1961
                        "[libdefaults]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1962
                        "default_realm = " + realm,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1963
                        "default_keytab_name = FILE:" + base + "/krb5.keytab",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1964
                        "forwardable = true",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1965
                        "dns_lookup_kdc = no",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1966
                        "dns_lookup_realm = no",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1967
                        "dns_canonicalize_hostname = false",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1968
                        "\n[realms]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1969
                        realm + " = {",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1970
                        "  kdc = localhost:" + port,
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1971
                        "}",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1972
                        "\n[logging]",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1973
                        "kdc = FILE:" + base + "/krb5kdc.log"
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1974
                ));
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1975
            } catch (IOException e) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1976
                throw new UncheckedIOException(e);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1977
            }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1978
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1979
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1980
        @Override
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1981
        void kinit(String user, String ccache) {
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1982
            String tmpName = base + "/" + user + "." +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1983
                    System.identityHashCode(this) + ".keytab";
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1984
            ktadd(user, tmpName);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1985
            run(true, nativePath +
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1986
                    (install ? "/bin/" : "/clients/kinit/") + "kinit",
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1987
                    "-f", "-t", tmpName, "-c", ccache, user);
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1988
        }
8052fa06e1b7 8186884: Test native KDC, Java krb5 lib, and native krb5 lib in one test
weijun
parents: 47226
diff changeset
  1989
    }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1990
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1991
    // Calling private methods thru reflections
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1992
    private static final Field getPADataField;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1993
    private static final Field getEType;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1994
    private static final Constructor<EncryptedData> ctorEncryptedData;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1995
    private static final Method stringToKey;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1996
    private static final Field getAddlTkt;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1997
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1998
    static {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1999
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2000
            ctorEncryptedData = EncryptedData.class.getDeclaredConstructor(DerValue.class);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2001
            ctorEncryptedData.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2002
            getPADataField = KDCReq.class.getDeclaredField("pAData");
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2003
            getPADataField.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2004
            getEType = KDCReqBody.class.getDeclaredField("eType");
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2005
            getEType.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2006
            stringToKey = EncryptionKey.class.getDeclaredMethod(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2007
                    "stringToKey",
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2008
                    char[].class, String.class, byte[].class, Integer.TYPE);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2009
            stringToKey.setAccessible(true);
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2010
            getAddlTkt = KDCReqBody.class.getDeclaredField("additionalTickets");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2011
            getAddlTkt.setAccessible(true);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2012
        } catch (NoSuchFieldException nsfe) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2013
            throw new AssertionError(nsfe);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2014
        } catch (NoSuchMethodException nsme) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2015
            throw new AssertionError(nsme);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2016
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2017
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2018
    private EncryptedData newEncryptedData(DerValue der) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2019
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2020
            return ctorEncryptedData.newInstance(der);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2021
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2022
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2023
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2024
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2025
    private static PAData[] KDCReqDotPAData(KDCReq req) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2026
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2027
            return (PAData[])getPADataField.get(req);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2028
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2029
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2030
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2031
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2032
    private static int[] KDCReqBodyDotEType(KDCReqBody body) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2033
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2034
            return (int[]) getEType.get(body);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2035
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2036
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2037
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2038
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2039
    private static byte[] EncryptionKeyDotStringToKey(char[] password, String salt,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2040
            byte[] s2kparams, int keyType) throws KrbCryptoException {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2041
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2042
            return (byte[])stringToKey.invoke(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2043
                    null, password, salt, s2kparams, keyType);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2044
        } catch (InvocationTargetException ex) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2045
            throw (KrbCryptoException)ex.getCause();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2046
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2047
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2048
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  2049
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2050
    private static Ticket KDCReqBodyDotFirstAdditionalTicket(KDCReqBody body) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2051
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2052
            return ((Ticket[])getAddlTkt.get(body))[0];
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2053
        } catch (Exception e) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2054
            throw new AssertionError(e);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2055
        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  2056
    }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  2057
}