jdk/test/sun/security/krb5/auto/KDC.java
author weijun
Mon, 13 Jul 2015 17:44:34 +0800
changeset 31643 abad00f2c027
parent 31429 ce4193650b40
child 31801 1297473ab237
permissions -rw-r--r--
8058290: JAAS Krb5LoginModule has suspect ticket-renewal logic, relies on clockskew grace Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
     1
/*
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
     2
 * Copyright (c) 2008, 2015, 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
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    24
import java.lang.reflect.Constructor;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    25
import java.lang.reflect.Field;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    26
import java.lang.reflect.InvocationTargetException;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    27
import java.net.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    28
import java.io.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    29
import java.lang.reflect.Method;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    30
import java.security.SecureRandom;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    31
import java.util.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    32
import java.util.concurrent.*;
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
    33
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
    34
import sun.net.spi.nameservice.NameService;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
    35
import sun.net.spi.nameservice.NameServiceDescriptor;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    36
import sun.security.krb5.*;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    37
import sun.security.krb5.internal.*;
1575
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
    38
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
    39
import sun.security.krb5.internal.crypto.EType;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    40
import sun.security.krb5.internal.crypto.KeyUsage;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    41
import sun.security.krb5.internal.ktab.KeyTab;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    42
import sun.security.util.DerInputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    43
import sun.security.util.DerOutputStream;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    44
import sun.security.util.DerValue;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    45
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    46
/**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    47
 * A KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    48
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    49
 * Features:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    50
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    51
 * <li> Supports TCP and UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    52
 * <li> Supports AS-REQ and TGS-REQ
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    53
 * <li> Principal db and other settings hard coded in application
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    54
 * <li> Options, say, request preauth or not
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    55
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    56
 * Side effects:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    57
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    58
 * <li> The Sun-internal class <code>sun.security.krb5.Config</code> is a
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    59
 * singleton and initialized according to Kerberos settings (krb5.conf and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    60
 * java.security.krb5.* system properties). This means once it's initialized
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    61
 * it will not automatically notice any changes to these settings (or file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    62
 * changes of krb5.conf). The KDC class normally does not touch these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    63
 * settings (except for the <code>writeKtab()</code> method). However, to make
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    64
 * sure nothing ever goes wrong, if you want to make any changes to these
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    65
 * settings after calling a KDC method, call <code>Config.refresh()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    66
 * make sure your changes are reflected in the <code>Config</code> object.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    67
 * </ol>
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    68
 * System properties recognized:
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    69
 * <ul>
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    70
 * <li>test.kdc.save.ccache
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
    71
 * </ul>
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    72
 * Issues and TODOs:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    73
 * <ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    74
 * <li> Generates krb5.conf to be used on another machine, currently the kdc is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    75
 * always localhost
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    76
 * <li> More options to KDC, say, error output, say, response nonce !=
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    77
 * request nonce
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    78
 * </ol>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    79
 * Note: This program uses internal krb5 classes (including reflection to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    80
 * access private fields and methods).
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    81
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    82
 * Usages:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    83
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    84
 * 1. Init and start the KDC:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    85
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    86
 * KDC kdc = KDC.create("REALM.NAME", port, isDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    87
 * KDC kdc = KDC.create("REALM.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    88
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    89
 * Here, <code>port</code> is the UDP and TCP port number the KDC server
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    90
 * listens on. If zero, a random port is chosen, which you can use getPort()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    91
 * later to retrieve the value.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    92
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    93
 * If <code>isDaemon</code> is true, the KDC worker threads will be daemons.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    94
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    95
 * The shortcut <code>KDC.create("REALM.NAME")</code> has port=0 and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    96
 * isDaemon=false, and is commonly used in an embedded KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    97
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    98
 * 2. Adding users:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
    99
 * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   100
 * kdc.addPrincipal(String principal_name, char[] password);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   101
 * kdc.addPrincipalRandKey(String principal_name);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   102
 * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   103
 * 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
   104
 * generates a random key. To expose this key, call <code>writeKtab()</code> to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   105
 * save the keys into a keytab file.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   106
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   107
 * Note that you need to add the principal name krbtgt/REALM.NAME yourself.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   108
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   109
 * Note that you can safely add a principal at any time after the KDC is
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   110
 * started and before a user requests info on this principal.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   111
 * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   112
 * 3. Other public methods:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   113
 * <ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   114
 * <li> <code>getPort</code>: Returns the port number the KDC uses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   115
 * <li> <code>getRealm</code>: Returns the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   116
 * <li> <code>writeKtab</code>: Writes all principals' keys into a keytab file
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   117
 * <li> <code>saveConfig</code>: Saves a krb5.conf file to access this KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   118
 * <li> <code>setOption</code>: Sets various options
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   119
 * </ul>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   120
 * Read the javadoc for details. Lazy developer can use <code>OneKDC</code>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   121
 * directly.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   122
 */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   123
public class KDC {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   124
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
   125
    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
   126
    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
   127
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   128
    // Under the hood.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   129
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   130
    // The random generator to generate random keys (including session keys)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   131
    private static SecureRandom secureRandom = new SecureRandom();
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   132
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   133
    // Principal db. principal -> pass. A case-insensitive TreeMap is used
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   134
    // 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
   135
    // 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
   136
    private TreeMap<String,char[]> passwords = new TreeMap<>
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   137
            (String.CASE_INSENSITIVE_ORDER);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   138
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   139
    // Realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   140
    private String realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   141
    // KDC
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   142
    private String kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   143
    // Service port number
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   144
    private int port;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   145
    // The request/response job queue
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   146
    private BlockingQueue<Job> q = new ArrayBlockingQueue<>(100);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   147
    // Options
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   148
    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
   149
    // Realm-specific krb5.conf settings
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   150
    private List<String> conf = new ArrayList<>();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   151
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   152
    private Thread thread1, thread2, thread3;
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   153
    private volatile boolean udpConsumerReady = false;
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   154
    private volatile boolean tcpConsumerReady = false;
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
   155
    private volatile boolean dispatcherReady = false;
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   156
    DatagramSocket u1 = null;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   157
    ServerSocket t1 = null;
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
   158
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
   159
    public static enum KtabMode { APPEND, EXISTING };
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
   160
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   161
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   162
     * Option names, to be expanded forever.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   163
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   164
    public static enum Option {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   165
        /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   166
         * Whether pre-authentication is required. Default Boolean.TRUE
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   167
         */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   168
        PREAUTH_REQUIRED,
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   169
        /**
5802
ea99d72d3c19 6959292: regression: cannot login if session key and preauth does not use the same etype
weijun
parents: 5774
diff changeset
   170
         * 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
   171
         */
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   172
        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
   173
        /**
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   174
         * 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
   175
         */
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   176
        RC4_FIRST_PREAUTH,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   177
        /**
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   178
         * 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
   179
         */
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   180
        ONLY_ONE_PREAUTH,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   181
        /**
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   182
         * 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
   183
         */
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   184
        RESP_NT,
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   185
        /**
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   186
         * 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
   187
         */
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   188
        DUP_ETYPE,
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   189
        /**
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   190
         * What backend server can be delegated to
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   191
         */
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   192
        OK_AS_DELEGATE,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   193
        /**
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   194
         * Allow S4U2self, List<String> of middle servers.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   195
         * If not set, means KDC does not understand S4U2self at all, therefore
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   196
         * would ignore any PA-FOR-USER request and send a ticket using the
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   197
         * cname of teh requestor. If set, it returns FORWARDABLE tickets to
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   198
         * a server with its name in the list
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   199
         */
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   200
        ALLOW_S4U2SELF,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   201
        /**
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   202
         * Allow S4U2proxy, Map<String,List<String>> of middle servers to
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   203
         * backends. If not set or a backend not in a server's list,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   204
         * Krb5.KDC_ERR_POLICY will be send for S4U2proxy request.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   205
         */
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   206
        ALLOW_S4U2PROXY,
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   207
        /**
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   208
         * Sensitive accounts can never be delegated.
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   209
         */
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   210
        SENSITIVE_ACCOUNTS,
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   211
        /**
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   212
         * If true, will check if TGS-REQ contains a non-null addresses field.
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   213
         */
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   214
        CHECK_ADDRESSES,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   215
    };
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   216
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   217
    static {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   218
        System.setProperty("sun.net.spi.nameservice.provider.1", "ns,mock");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   219
    }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   220
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   221
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   222
     * A standalone KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   223
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   224
    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
   225
        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
   226
        KDC kdc = create("RABBIT.HOLE", "kdc.rabbit.hole", port, false);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   227
        kdc.addPrincipal("dummy", "bogus".toCharArray());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   228
        kdc.addPrincipal("foo", "bar".toCharArray());
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   229
        kdc.addPrincipalRandKey("krbtgt/RABBIT.HOLE");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   230
        kdc.addPrincipalRandKey("server/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   231
        kdc.addPrincipalRandKey("backend/host.rabbit.hole");
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   232
        KDC.saveConfig("krb5.conf", kdc, "forwardable = true");
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   233
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   234
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   235
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   236
     * Creates and starts a KDC running as a daemon on a random port.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   237
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   238
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   239
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   240
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   241
    public static KDC create(String realm) throws IOException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   242
        return create(realm, "kdc." + realm.toLowerCase(), 0, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   243
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   244
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   245
    public static KDC existing(String realm, String kdc, int port) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   246
        KDC k = new KDC(realm, kdc);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   247
        k.port = port;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   248
        return k;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   249
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   250
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   251
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   252
     * Creates and starts a KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   253
     * @param realm the realm name
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   254
     * @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
   255
     *        chosen if zero.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   256
     * @param asDaemon if true, KDC threads will be daemons. Otherwise, not.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   257
     * @return the running KDC instance
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   258
     * @throws java.io.IOException for any socket creation error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   259
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   260
    public static KDC create(String realm, String kdc, int port, boolean asDaemon) throws IOException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   261
        return new KDC(realm, kdc, port, asDaemon);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   262
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   263
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   264
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   265
     * Sets an option
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   266
     * @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
   267
     * @param value the value
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   268
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   269
    public void setOption(Option key, Object value) {
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   270
        if (value == null) {
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   271
            options.remove(key);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   272
        } else {
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   273
            options.put(key, value);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   274
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   275
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   276
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   277
    /**
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   278
     * Writes or appends keys into a keytab.
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   279
     * <p>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   280
     * Attention: This is the most basic one of a series of methods below on
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   281
     * keytab creation or modification. All these methods reference krb5.conf
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   282
     * settings. If you need to modify krb5.conf or switch to another krb5.conf
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   283
     * later, please call <code>Config.refresh()</code> again. For example:
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   284
     * <pre>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   285
     * kdc.writeKtab("/etc/kdc/ktab", true);  // Config is initialized,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   286
     * System.setProperty("java.security.krb5.conf", "/home/mykrb5.conf");
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   287
     * Config.refresh();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   288
     * </pre>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   289
     * Inside this method there are 2 places krb5.conf is used:
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   290
     * <ol>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   291
     * <li> (Fatal) Generating keys: EncryptionKey.acquireSecretKeys
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   292
     * <li> (Has workaround) Creating PrincipalName
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   293
     * </ol>
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   294
     * @param tab the keytab file name
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   295
     * @param append true if append, otherwise, overwrite.
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   296
     * @param names the names to write into, write all if names is empty
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   297
     */
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   298
    public void writeKtab(String tab, boolean append, String... names)
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   299
            throws IOException, KrbException {
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   300
        KeyTab ktab = append ? KeyTab.getInstance(tab) : KeyTab.create(tab);
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   301
        Iterable<String> entries =
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   302
                (names.length != 0) ? Arrays.asList(names): passwords.keySet();
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   303
        for (String name : entries) {
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   304
            char[] pass = passwords.get(name);
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   305
            int kvno = 0;
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   306
            if (Character.isDigit(pass[pass.length-1])) {
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   307
                kvno = pass[pass.length-1] - '0';
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   308
            }
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   309
            PrincipalName pn = new PrincipalName(name,
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   310
                        name.indexOf('/') < 0 ?
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   311
                            PrincipalName.KRB_NT_UNKNOWN :
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   312
                            PrincipalName.KRB_NT_SRV_HST);
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   313
            ktab.addEntry(pn,
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   314
                        getSalt(pn),
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   315
                        pass,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   316
                        kvno,
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   317
                        true);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   318
        }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   319
        ktab.save();
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   320
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   321
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   322
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   323
     * Writes all principals' keys from multiple KDCs into one keytab file.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   324
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   325
     * @throws sun.security.krb5.KrbException for any realm and/or principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   326
     *         name error.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   327
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   328
    public static void writeMultiKtab(String tab, KDC... kdcs)
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   329
            throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   330
        KeyTab.create(tab).save();      // Empty the old keytab
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   331
        appendMultiKtab(tab, kdcs);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   332
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   333
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   334
    /**
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   335
     * Appends all principals' keys from multiple KDCs to one keytab file.
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   336
     */
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   337
    public static void appendMultiKtab(String tab, KDC... kdcs)
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   338
            throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   339
        for (KDC kdc: kdcs) {
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   340
            kdc.writeKtab(tab, true);
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   341
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   342
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   343
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   344
    /**
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   345
     * Write a ktab for this KDC.
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   346
     */
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   347
    public void writeKtab(String tab) throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   348
        writeKtab(tab, false);
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   349
    }
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   350
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 1575
diff changeset
   351
    /**
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   352
     * Appends keys in this KDC to a ktab.
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   353
     */
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   354
    public void appendKtab(String tab) throws IOException, KrbException {
12199
3de38eedde69 7152176: More krb5 tests
weijun
parents: 11107
diff changeset
   355
        writeKtab(tab, true);
9499
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   356
    }
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   357
f3115698a012 6894072: always refresh keytab
weijun
parents: 9275
diff changeset
   358
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   359
     * Adds a new principal to this realm with a given password.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   360
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   361
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   362
     * @param pass the password for the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   363
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   364
    public void addPrincipal(String user, char[] pass) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   365
        if (user.indexOf('@') < 0) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   366
            user = user + "@" + realm;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   367
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   368
        passwords.put(user, pass);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   369
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   370
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   371
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   372
     * Adds a new principal to this realm with a random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   373
     * @param user the principal's name. For a service principal, use the
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   374
     *        form of host/f.q.d.n
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   375
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   376
    public void addPrincipalRandKey(String user) {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   377
        addPrincipal(user, randomPassword());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   378
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   379
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   380
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   381
     * Returns the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   382
     * @return the name of this realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   383
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   384
    public String getRealm() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   385
        return realm;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   386
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   387
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   388
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   389
     * Returns the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   390
     * @return the name of kdc
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   391
     */
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   392
    public String getKDC() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   393
        return kdc;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   394
    }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   395
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   396
    /**
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   397
     * 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
   398
     */
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   399
    public void addConf(String s) {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   400
        conf.add(s);
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   401
    }
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   402
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   403
    /**
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   404
     * Writes a krb5.conf for one or more KDC that includes KDC locations for
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   405
     * each realm and the default realm name. You can also add extra strings
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   406
     * into the file. The method should be called like:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   407
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   408
     *   KDC.saveConfig("krb5.conf", kdc1, kdc2, ..., line1, line2, ...);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   409
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   410
     * Here you can provide one or more kdc# and zero or more line# arguments.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   411
     * The line# will be put after [libdefaults] and before [realms]. Therefore
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   412
     * you can append new lines into [libdefaults] and/or create your new
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   413
     * stanzas as well. Note that a newline character will be appended to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   414
     * each line# argument.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   415
     * <p>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   416
     * For example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   417
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   418
     * KDC.saveConfig("krb5.conf", this);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   419
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   420
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   421
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   422
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   423
     * default_realm = REALM.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   424
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   425
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   426
     *   REALM.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   427
     *     kdc = host:port_number
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   428
     *     # realm-specific settings
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   429
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   430
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   431
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   432
     * Another example:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   433
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   434
     * KDC.saveConfig("krb5.conf", kdc1, kdc2, "forwardable = true", "",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   435
     *         "[domain_realm]",
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   436
     *         ".kdc1.com = KDC1.NAME");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   437
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   438
     * generates:
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   439
     * <pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   440
     * [libdefaults]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   441
     * default_realm = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   442
     * forwardable = true
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   443
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   444
     * [domain_realm]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   445
     * .kdc1.com = KDC1.NAME
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   446
     *
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   447
     * [realms]
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   448
     *   KDC1.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   449
     *     kdc = host:port1
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   450
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   451
     *   KDC2.NAME = {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   452
     *     kdc = host:port2
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   453
     *   }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   454
     * </pre>
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   455
     * @param file the name of the file to write into
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   456
     * @param kdc the first (and default) KDC
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   457
     * @param more more KDCs or extra lines (in their appearing order) to
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   458
     * insert into the krb5.conf file. This method reads each argument's type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   459
     * to determine what it's for. This argument can be empty.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   460
     * @throws java.io.IOException for any file output error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   461
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   462
    public static void saveConfig(String file, KDC kdc, Object... more)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   463
            throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   464
        File f = new File(file);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   465
        StringBuffer sb = new StringBuffer();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   466
        sb.append("[libdefaults]\ndefault_realm = ");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   467
        sb.append(kdc.realm);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   468
        sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   469
        for (Object o: more) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   470
            if (o instanceof String) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   471
                sb.append(o);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   472
                sb.append("\n");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   473
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   474
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   475
        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
   476
        sb.append(kdc.realmLine());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   477
        for (Object o: more) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   478
            if (o instanceof KDC) {
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
   479
                sb.append(((KDC)o).realmLine());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   480
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   481
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   482
        FileOutputStream fos = new FileOutputStream(f);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   483
        fos.write(sb.toString().getBytes());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   484
        fos.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   485
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   486
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   487
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   488
     * Returns the service port of the KDC server.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   489
     * @return the KDC service port
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   490
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   491
    public int getPort() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   492
        return port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   493
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   494
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   495
    // Private helper methods
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   496
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   497
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   498
     * Private constructor, cannot be called outside.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   499
     * @param realm
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   500
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   501
    private KDC(String realm, String kdc) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   502
        this.realm = realm;
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   503
        this.kdc = kdc;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   504
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   505
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   506
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   507
     * A constructor that starts the KDC service also.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   508
     */
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   509
    protected KDC(String realm, String kdc, int port, boolean asDaemon)
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   510
            throws IOException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   511
        this(realm, kdc);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   512
        startServer(port, asDaemon);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   513
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   514
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   515
     * Generates a 32-char random password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   516
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   517
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   518
    private static char[] randomPassword() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   519
        char[] pass = new char[32];
5622
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   520
        for (int i=0; i<31; i++)
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   521
            pass[i] = (char)secureRandom.nextInt();
5622
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   522
        // The last char cannot be a number, otherwise, keyForUser()
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   523
        // believes it's a sign of kvno
2a600d13659a 6948287: KDC test strange knvo
weijun
parents: 5617
diff changeset
   524
        pass[31] = 'Z';
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   525
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   526
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   527
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   528
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   529
     * Generates a random key for the given encryption type.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   530
     * @param eType the encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   531
     * @return the generated key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   532
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   533
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   534
    private static EncryptionKey generateRandomKey(int eType)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   535
            throws KrbException  {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   536
        // Is 32 enough for AES256? I should have generated the keys directly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   537
        // but different cryptos have different rules on what keys are valid.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   538
        char[] pass = randomPassword();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   539
        String algo;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   540
        switch (eType) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   541
            case EncryptedData.ETYPE_DES_CBC_MD5: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   542
            case EncryptedData.ETYPE_DES3_CBC_HMAC_SHA1_KD: algo = "DESede"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   543
            case EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96: algo = "AES128"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   544
            case EncryptedData.ETYPE_ARCFOUR_HMAC: algo = "ArcFourHMAC"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   545
            case EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96: algo = "AES256"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   546
            default: algo = "DES"; break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   547
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   548
        return new EncryptionKey(pass, "NOTHING", algo);    // Silly
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   549
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   550
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   551
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   552
     * Returns the password for a given principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   553
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   554
     * @return the password
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   555
     * @throws sun.security.krb5.KrbException when the principal is not inside
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   556
     *         the database.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   557
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   558
    private char[] getPassword(PrincipalName p, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   559
            throws KrbException {
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   560
        String pn = p.toString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   561
        if (p.getRealmString() == null) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   562
            pn = pn + "@" + getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   563
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   564
        char[] pass = passwords.get(pn);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   565
        if (pass == null) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   566
            throw new KrbException(server?
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   567
                Krb5.KDC_ERR_S_PRINCIPAL_UNKNOWN:
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   568
                Krb5.KDC_ERR_C_PRINCIPAL_UNKNOWN, pn.toString());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   569
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   570
        return pass;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   571
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   572
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   573
    /**
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   574
     * Returns the salt string for the principal.
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   575
     * @param p principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   576
     * @return the salt
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   577
     */
15006
10d6aacdd67f 8005447: default principal should act as anyone
weijun
parents: 14413
diff changeset
   578
    protected String getSalt(PrincipalName p) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   579
        String pn = p.toString();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   580
        if (p.getRealmString() == null) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   581
            pn = pn + "@" + getRealm();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   582
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   583
        if (passwords.containsKey(pn)) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   584
            try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   585
                // Find the principal name with correct case.
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   586
                p = new PrincipalName(passwords.ceilingEntry(pn).getKey());
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   587
            } catch (RealmException re) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   588
                // Won't happen
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   589
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   590
        }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   591
        String s = p.getRealmString();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   592
        if (s == null) s = getRealm();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   593
        for (String n: p.getNameStrings()) {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   594
            s += n;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   595
        }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
   596
        return s;
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
     * Returns the key for a given principal of the given encryption type
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   601
     * @param p the principal
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   602
     * @param etype the encryption type
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   603
     * @param server looking for a server principal?
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   604
     * @return the key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   605
     * @throws sun.security.krb5.KrbException for unknown/unsupported etype
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   606
     */
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   607
    private EncryptionKey keyForUser(PrincipalName p, int etype, boolean server)
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   608
            throws KrbException {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   609
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   610
            // Do not call EncryptionKey.acquireSecretKeys(), otherwise
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   611
            // the krb5.conf config file would be loaded.
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   612
            Integer kvno = null;
4532
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4531
diff changeset
   613
            // 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
   614
            // Kvno must be postive.
f39917c8cf46 6907425: JCK Kerberos tests fail since b77
weijun
parents: 4531
diff changeset
   615
            if (p.toString().indexOf('/') > 0) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   616
                char[] pass = getPassword(p, server);
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   617
                if (Character.isDigit(pass[pass.length-1])) {
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   618
                    kvno = pass[pass.length-1] - '0';
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   619
                }
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   620
            }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   621
            return new EncryptionKey(EncryptionKeyDotStringToKey(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   622
                    getPassword(p, server), getSalt(p), null, etype),
4168
1a8d21bb898c 6893158: AP_REQ check should use key version number
weijun
parents: 3046
diff changeset
   623
                    etype, kvno);
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   624
        } catch (KrbException ke) {
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   625
            throw ke;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   626
        } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   627
            throw new RuntimeException(e);  // should not happen
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   628
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   629
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   630
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   631
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   632
     * Processes an incoming request and generates a response.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   633
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   634
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   635
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   636
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
   637
    protected byte[] processMessage(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   638
        if ((in[0] & 0x1f) == Krb5.KRB_AS_REQ)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   639
            return processAsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   640
        else
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   641
            return processTgsReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   642
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   643
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   644
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   645
     * Processes a TGS_REQ and generates a TGS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   646
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   647
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   648
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   649
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
   650
    protected byte[] processTgsReq(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   651
        TGSReq tgsReq = new TGSReq(in);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   652
        PrincipalName service = tgsReq.reqBody.sname;
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   653
        if (options.containsKey(KDC.Option.RESP_NT)) {
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   654
            service = new PrincipalName((int)options.get(KDC.Option.RESP_NT),
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   655
                    service.getNameStrings(), service.getRealm());
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   656
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   657
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   658
            System.out.println(realm + "> " + tgsReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   659
                    " sends TGS-REQ for " +
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   660
                    service + ", " + tgsReq.reqBody.kdcOptions);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   661
            KDCReqBody body = tgsReq.reqBody;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   662
            int[] eTypes = KDCReqBodyDotEType(body);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   663
            int e2 = eTypes[0];     // etype for outgoing session key
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   664
            int e3 = eTypes[0];     // etype for outgoing ticket
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   665
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   666
            PAData[] pas = KDCReqDotPAData(tgsReq);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   667
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   668
            Ticket tkt = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   669
            EncTicketPart etp = null;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   670
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   671
            PrincipalName cname = null;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   672
            boolean allowForwardable = true;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   673
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   674
            if (pas == null || pas.length == 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   675
                throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   676
            } else {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   677
                PrincipalName forUserCName = null;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   678
                for (PAData pa: pas) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   679
                    if (pa.getType() == Krb5.PA_TGS_REQ) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   680
                        APReq apReq = new APReq(pa.getValue());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   681
                        EncryptedData ed = apReq.authenticator;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   682
                        tkt = apReq.ticket;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   683
                        int te = tkt.encPart.getEType();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   684
                        EncryptionKey kkey = keyForUser(tkt.sname, te, true);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   685
                        byte[] bb = tkt.encPart.decrypt(kkey, KeyUsage.KU_TICKET);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   686
                        DerInputStream derIn = new DerInputStream(bb);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   687
                        DerValue der = derIn.getDerValue();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   688
                        etp = new EncTicketPart(der.toByteArray());
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   689
                        // Finally, cname will be overwritten by PA-FOR-USER
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   690
                        // if it exists.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   691
                        cname = etp.cname;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   692
                        System.out.println(realm + "> presenting a ticket of "
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   693
                                + etp.cname + " to " + tkt.sname);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   694
                    } else if (pa.getType() == Krb5.PA_FOR_USER) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   695
                        if (options.containsKey(Option.ALLOW_S4U2SELF)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   696
                            PAForUserEnc p4u = new PAForUserEnc(
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   697
                                    new DerValue(pa.getValue()), null);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   698
                            forUserCName = p4u.name;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   699
                            System.out.println(realm + "> presenting a PA_FOR_USER "
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   700
                                    + " in the name of " + p4u.name);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   701
                        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   702
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   703
                }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   704
                if (forUserCName != null) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   705
                    List<String> names = (List<String>)options.get(Option.ALLOW_S4U2SELF);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   706
                    if (!names.contains(cname.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   707
                        // Mimic the normal KDC behavior. When a server is not
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   708
                        // allowed to send S4U2self, do not send an error.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   709
                        // Instead, send a ticket which is useless later.
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   710
                        allowForwardable = false;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   711
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   712
                    cname = forUserCName;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   713
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   714
                if (tkt == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   715
                    throw new KrbException(Krb5.KDC_ERR_PADATA_TYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   716
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   717
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   718
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   719
            // Session key for original ticket, TGT
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   720
            EncryptionKey ckey = etp.key;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   721
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   722
            // Session key for session with the service
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   723
            EncryptionKey key = generateRandomKey(e2);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   724
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   725
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   726
            KerberosTime till = body.till;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   727
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   728
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   729
            } else if (till.isZero()) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   730
                till = new KerberosTime(new Date().getTime() + 1000 * 3600 * 11);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   731
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   732
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   733
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   734
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   735
                    && allowForwardable) {
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   736
                List<String> sensitives = (List<String>)
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   737
                        options.get(Option.SENSITIVE_ACCOUNTS);
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   738
                if (sensitives != null && sensitives.contains(cname.toString())) {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   739
                    // Cannot make FORWARDABLE
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   740
                } else {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   741
                    bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   742
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   743
            }
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   744
            if (options.containsKey(Option.CHECK_ADDRESSES)
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   745
                    && body.kdcOptions.get(KDCOptions.FORWARDED)
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   746
                    && body.addresses == null) {
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   747
                throw new KrbException(Krb5.KDC_ERR_BADOPTION);
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   748
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   749
            if (body.kdcOptions.get(KDCOptions.FORWARDED) ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   750
                    etp.flags.get(Krb5.TKT_OPTS_FORWARDED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   751
                bFlags[Krb5.TKT_OPTS_FORWARDED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   752
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   753
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   754
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   755
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   756
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   757
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   758
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   759
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   760
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   761
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   762
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   763
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   764
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   765
            }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   766
            if (body.kdcOptions.get(KDCOptions.CNAME_IN_ADDL_TKT)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   767
                if (!options.containsKey(Option.ALLOW_S4U2PROXY)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   768
                    // Don't understand CNAME_IN_ADDL_TKT
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   769
                    throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   770
                } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   771
                    Map<String,List<String>> map = (Map<String,List<String>>)
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   772
                            options.get(Option.ALLOW_S4U2PROXY);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   773
                    Ticket second = KDCReqBodyDotFirstAdditionalTicket(body);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   774
                    EncryptionKey key2 = keyForUser(second.sname, second.encPart.getEType(), true);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   775
                    byte[] bb = second.encPart.decrypt(key2, KeyUsage.KU_TICKET);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   776
                    DerInputStream derIn = new DerInputStream(bb);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   777
                    DerValue der = derIn.getDerValue();
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   778
                    EncTicketPart tktEncPart = new EncTicketPart(der.toByteArray());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   779
                    if (!tktEncPart.flags.get(Krb5.TKT_OPTS_FORWARDABLE)) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   780
                        //throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   781
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   782
                    PrincipalName client = tktEncPart.cname;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   783
                    System.out.println(realm + "> and an additional ticket of "
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   784
                            + client + " to " + second.sname);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   785
                    if (map.containsKey(cname.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   786
                        if (map.get(cname.toString()).contains(service.toString())) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   787
                            System.out.println(realm + "> S4U2proxy OK");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   788
                        } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   789
                            throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   790
                        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   791
                    } else {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   792
                        throw new KrbException(Krb5.KDC_ERR_BADOPTION);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   793
                    }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   794
                    cname = client;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   795
                }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   796
            }
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   797
12867
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   798
            String okAsDelegate = (String)options.get(Option.OK_AS_DELEGATE);
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   799
            if (okAsDelegate != null && (
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   800
                    okAsDelegate.isEmpty() ||
5492127ab0a8 7172701: KDC tests cleanup
weijun
parents: 12199
diff changeset
   801
                    okAsDelegate.contains(service.getNameString()))) {
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   802
                bFlags[Krb5.TKT_OPTS_DELEGATE] = true;
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   803
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   804
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   805
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   806
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   807
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   808
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   809
                    key,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   810
                    cname,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   811
                    new TransitedEncoding(1, new byte[0]),  // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   812
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   813
                    body.from,
31643
abad00f2c027 8058290: JAAS Krb5LoginModule has suspect ticket-renewal logic, relies on clockskew grace
weijun
parents: 31429
diff changeset
   814
                    till, etp.renewTill,
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   815
                    body.addresses != null ? body.addresses
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   816
                            : etp.caddr,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   817
                    null);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   818
            EncryptionKey skey = keyForUser(service, e3, true);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   819
            if (skey == null) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   820
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   821
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   822
            Ticket t = new Ticket(
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   823
                    service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   824
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   825
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   826
            EncTGSRepPart enc_part = new EncTGSRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   827
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   828
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   829
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   830
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   831
                    body.getNonce(),    // TODO: detect replay
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   832
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   833
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   834
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   835
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   836
                    body.from,
31643
abad00f2c027 8058290: JAAS Krb5LoginModule has suspect ticket-renewal logic, relies on clockskew grace
weijun
parents: 31429
diff changeset
   837
                    till, etp.renewTill,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   838
                    service,
30959
14e1b420cdd6 8031111: fix krb5 caddr
weijun
parents: 30038
diff changeset
   839
                    body.addresses
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   840
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   841
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_TGS_REP_PART_SESSKEY);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   842
            TGSRep tgsRep = new TGSRep(null,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
   843
                    cname,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   844
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   845
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   846
            System.out.println("     Return " + tgsRep.cname
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   847
                    + " ticket for " + tgsRep.ticket.sname + ", flags "
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   848
                    + tFlags);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   849
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   850
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   851
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   852
                    true, (byte)Krb5.KRB_TGS_REP), tgsRep.asn1Encode());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   853
            return out.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   854
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   855
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   856
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   857
            KDCReqBody body = tgsReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   858
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   859
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   860
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   861
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   862
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   863
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   864
                        ke.returnCode(),
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   865
                        body.cname,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
   866
                        service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   867
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   868
                        null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   869
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   870
            return kerr.asn1Encode();
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
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   875
     * Processes a AS_REQ and generates a AS_REP (or KRB_ERROR)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   876
     * @param in the request
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   877
     * @return the response
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   878
     * @throws java.lang.Exception for various errors
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   879
     */
21961
50019af27ca3 8028351: JWS doesn't get authenticated when using kerberos auth proxy
weijun
parents: 18536
diff changeset
   880
    protected byte[] processAsReq(byte[] in) throws Exception {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   881
        ASReq asReq = new ASReq(in);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   882
        int[] eTypes = null;
7977
f47f211cd627 7008713: diamond conversion of kerberos5 and security tools
smarks
parents: 7183
diff changeset
   883
        List<PAData> outPAs = new ArrayList<>();
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   884
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   885
        PrincipalName service = asReq.reqBody.sname;
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   886
        if (options.containsKey(KDC.Option.RESP_NT)) {
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   887
            service = new PrincipalName(service.getNameStrings(),
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   888
                    (int)options.get(KDC.Option.RESP_NT));
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   889
        }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   890
        try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   891
            System.out.println(realm + "> " + asReq.reqBody.cname +
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   892
                    " sends AS-REQ for " +
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   893
                    service + ", " + asReq.reqBody.kdcOptions);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   894
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   895
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   896
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   897
            eTypes = KDCReqBodyDotEType(body);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   898
            int eType = eTypes[0];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   899
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
   900
            // Maybe server does not support aes256, but a kinit does
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
   901
            if (!EType.isSupported(eType)) {
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
   902
                if (eTypes.length < 2) {
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
   903
                    throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
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
   904
                }
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
   905
                eType = eTypes[1];
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
   906
            }
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
   907
4336
4c792c19266e 6853328: Support OK-AS-DELEGATE flag
weijun
parents: 4168
diff changeset
   908
            EncryptionKey ckey = keyForUser(body.cname, eType, false);
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   909
            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
   910
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   911
            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
   912
                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
   913
                boolean found = false;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   914
                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
   915
                    if (eTypes[i] == tgtEType) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   916
                        found = true;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   917
                        break;
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   918
                    }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   919
                }
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   920
                if (!found) {
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
   921
                    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
   922
                }
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
   923
                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
   924
            }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   925
            if (ckey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   926
                throw new KrbException(Krb5.KDC_ERR_ETYPE_NOSUPP);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   927
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   928
            if (skey == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   929
                throw new KrbException(Krb5.KDC_ERR_SUMTYPE_NOSUPP); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   930
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   931
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   932
            // Session key
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   933
            EncryptionKey key = generateRandomKey(eType);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   934
            // Check time, TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   935
            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
   936
            KerberosTime rtime = body.rtime;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   937
            if (till == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   938
                throw new KrbException(Krb5.KDC_ERR_NEVER_VALID); // TODO
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   939
            } else if (till.isZero()) {
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
   940
                till = new KerberosTime(
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
   941
                        new Date().getTime() + 1000 * DEFAULT_LIFETIME);
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
   942
            }
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
   943
            if (rtime == null && body.kdcOptions.get(KDCOptions.RENEWABLE)) {
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
   944
                rtime = new KerberosTime(
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
   945
                        new Date().getTime() + 1000 * DEFAULT_RENEWTIME);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   946
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   947
            //body.from
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   948
            boolean[] bFlags = new boolean[Krb5.TKT_OPTS_MAX+1];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   949
            if (body.kdcOptions.get(KDCOptions.FORWARDABLE)) {
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   950
                List<String> sensitives = (List<String>)
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   951
                        options.get(Option.SENSITIVE_ACCOUNTS);
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   952
                if (sensitives != null && sensitives.contains(body.cname.toString())) {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   953
                    // Cannot make FORWARDABLE
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   954
                } else {
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   955
                    bFlags[Krb5.TKT_OPTS_FORWARDABLE] = true;
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
   956
                }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   957
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   958
            if (body.kdcOptions.get(KDCOptions.RENEWABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   959
                bFlags[Krb5.TKT_OPTS_RENEWABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   960
                //renew = new KerberosTime(new Date().getTime() + 1000 * 3600 * 24 * 7);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   961
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   962
            if (body.kdcOptions.get(KDCOptions.PROXIABLE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   963
                bFlags[Krb5.TKT_OPTS_PROXIABLE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   964
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   965
            if (body.kdcOptions.get(KDCOptions.POSTDATED)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   966
                bFlags[Krb5.TKT_OPTS_POSTDATED] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   967
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   968
            if (body.kdcOptions.get(KDCOptions.ALLOW_POSTDATE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   969
                bFlags[Krb5.TKT_OPTS_MAY_POSTDATE] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   970
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   971
            bFlags[Krb5.TKT_OPTS_INITIAL] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
   972
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
   973
            // Creating PA-DATA
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   974
            DerValue[] pas2 = null, pas = null;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   975
            if (options.containsKey(KDC.Option.DUP_ETYPE)) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   976
                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
   977
                switch (n) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   978
                    case 1:     // customer's case in 7067974
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   979
                        pas2 = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   980
                            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
   981
                            new DerValue(new ETypeInfo2(1, "", null).asn1Encode()),
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
   982
                            new DerValue(new ETypeInfo2(1, realm, new byte[]{1}).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   983
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   984
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   985
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   986
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
   987
                            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
   988
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   989
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   990
                    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
   991
                        pas2 = new DerValue[] {
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
   992
                            new DerValue(new ETypeInfo2(1, realm, new byte[]{1}).asn1Encode()),
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   993
                            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
   994
                            new DerValue(new ETypeInfo2(1, "", null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   995
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   996
                        pas = new DerValue[] {
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
   997
                            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
   998
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
   999
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1000
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1001
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1002
                    case 3:     // but only E is wrong
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1003
                        pas = new DerValue[] {
16504
1e8ff2df7152 8009875: Provide a default udp_preference_limit for krb5.conf
weijun
parents: 15006
diff changeset
  1004
                            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
  1005
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1006
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1007
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1008
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1009
                    case 4:     // we also ignore rc4-hmac
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1010
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1011
                            new DerValue(new ETypeInfo(23, "ANYTHING").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1012
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1013
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1014
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1015
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1016
                    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
  1017
                                // See s.s.k.internal.PAData$SaltAndParams
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1018
                        pas = new DerValue[] {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1019
                            new DerValue(new ETypeInfo(1, "").asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1020
                            new DerValue(new ETypeInfo(1, null).asn1Encode()),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1021
                        };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1022
                        break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1023
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1024
            } else {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1025
                int[] epas = eTypes;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1026
                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
  1027
                    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
  1028
                        if (epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1029
                            epas[i] = epas[0];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1030
                            epas[0] = EncryptedData.ETYPE_ARCFOUR_HMAC;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1031
                            break;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1032
                        }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1033
                    };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1034
                } 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
  1035
                    epas = new int[] { eTypes[0] };
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1036
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1037
                pas2 = new DerValue[epas.length];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1038
                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
  1039
                    pas2[i] = new DerValue(new ETypeInfo2(
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1040
                            epas[i],
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1041
                            epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ?
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1042
                                null : getSalt(body.cname),
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1043
                            null).asn1Encode());
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1044
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1045
                boolean allOld = true;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1046
                for (int i: eTypes) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1047
                    if (i == EncryptedData.ETYPE_AES128_CTS_HMAC_SHA1_96 ||
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1048
                            i == EncryptedData.ETYPE_AES256_CTS_HMAC_SHA1_96) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1049
                        allOld = false;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1050
                        break;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1051
                    }
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1052
                }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1053
                if (allOld) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1054
                    pas = new DerValue[epas.length];
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1055
                    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
  1056
                        pas[i] = new DerValue(new ETypeInfo(
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1057
                                epas[i],
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1058
                                epas[i] == EncryptedData.ETYPE_ARCFOUR_HMAC ?
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1059
                                    null : getSalt(body.cname)
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1060
                                ).asn1Encode());
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1061
                    }
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1062
                }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1063
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1064
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1065
            DerOutputStream eid;
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1066
            if (pas2 != null) {
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1067
                eid = new DerOutputStream();
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1068
                eid.putSequence(pas2);
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1069
                outPAs.add(new PAData(Krb5.PA_ETYPE_INFO2, eid.toByteArray()));
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1070
            }
10432
ef33e56c55a9 7067974: multiple ETYPE-INFO-ENTRY with same etype and different salt
weijun
parents: 10141
diff changeset
  1071
            if (pas != null) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1072
                eid = new DerOutputStream();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1073
                eid.putSequence(pas);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1074
                outPAs.add(new PAData(Krb5.PA_ETYPE_INFO, eid.toByteArray()));
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1075
            }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1076
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1077
            PAData[] inPAs = KDCReqDotPAData(asReq);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1078
            if (inPAs == null || inPAs.length == 0) {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1079
                Object preauth = options.get(Option.PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1080
                if (preauth == null || preauth.equals(Boolean.TRUE)) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1081
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_REQUIRED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1082
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1083
            } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1084
                try {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1085
                    EncryptedData data = newEncryptedData(new DerValue(inPAs[0].getValue()));
5774
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1086
                    EncryptionKey pakey = keyForUser(body.cname, data.getEType(), false);
4b9857e483c1 6951366: kerberos login failure on win2008 with AD set to win2000 compat mode
weijun
parents: 5627
diff changeset
  1087
                    data.decrypt(pakey, KeyUsage.KU_PA_ENC_TS);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1088
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1089
                    throw new KrbException(Krb5.KDC_ERR_PREAUTH_FAILED);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1090
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1091
                bFlags[Krb5.TKT_OPTS_PRE_AUTHENT] = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1092
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1093
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1094
            TicketFlags tFlags = new TicketFlags(bFlags);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1095
            EncTicketPart enc = new EncTicketPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1096
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1097
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1098
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1099
                    new TransitedEncoding(1, new byte[0]),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1100
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1101
                    body.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
  1102
                    till, rtime,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1103
                    body.addresses,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1104
                    null);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1105
            Ticket t = new Ticket(
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1106
                    service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1107
                    new EncryptedData(skey, enc.asn1Encode(), KeyUsage.KU_TICKET)
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1108
            );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1109
            EncASRepPart enc_part = new EncASRepPart(
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1110
                    key,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1111
                    new LastReq(new LastReqEntry[]{
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1112
                        new LastReqEntry(0, new KerberosTime(new Date().getTime() - 10000))
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1113
                    }),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1114
                    body.getNonce(),    // TODO: detect replay?
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1115
                    new KerberosTime(new Date().getTime() + 1000 * 3600 * 24),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1116
                    // Next 5 and last MUST be same with ticket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1117
                    tFlags,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1118
                    new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1119
                    body.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
  1120
                    till, rtime,
10141
664ba64d2472 7061379: [Kerberos] Cross-realm authentication fails, due to nameType problem
weijun
parents: 9499
diff changeset
  1121
                    service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1122
                    body.addresses
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1123
                    );
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1124
            EncryptedData edata = new EncryptedData(ckey, enc_part.asn1Encode(), KeyUsage.KU_ENC_AS_REP_PART);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1125
            ASRep asRep = new ASRep(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1126
                    outPAs.toArray(new PAData[outPAs.size()]),
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1127
                    body.cname,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1128
                    t,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1129
                    edata);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1130
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1131
            System.out.println("     Return " + asRep.cname
28670
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1132
                    + " ticket for " + asRep.ticket.sname + ", flags "
bb9afe681988 8022582: Relax response flags checking in sun.security.krb5.KrbKdcRep.check.
weijun
parents: 27946
diff changeset
  1133
                    + tFlags);
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1134
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1135
            DerOutputStream out = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1136
            out.write(DerValue.createTag(DerValue.TAG_APPLICATION,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1137
                    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
  1138
            byte[] result = out.toByteArray();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1139
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1140
            // Added feature:
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1141
            // 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
  1142
            // by the system property below.
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1143
            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
  1144
            if (ccache != null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1145
                asRep.encKDCRepPart = enc_part;
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1146
                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
  1147
                    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
  1148
                CredentialsCache cache =
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1149
                    CredentialsCache.create(asReq.reqBody.cname, ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1150
                if (cache == null) {
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1151
                   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
  1152
                                         ccache);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1153
                }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1154
                cache.update(credentials);
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1155
                cache.save();
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1156
            }
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1157
e0f1979051b5 6765491: Krb5LoginModule a little too restrictive, and the doc is not clear.
weijun
parents: 1456
diff changeset
  1158
            return result;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1159
        } catch (KrbException ke) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1160
            ke.printStackTrace(System.out);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1161
            KRBError kerr = ke.getError();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1162
            KDCReqBody body = asReq.reqBody;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1163
            System.out.println("     Error " + ke.returnCode()
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1164
                    + " " +ke.returnCodeMessage());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1165
            byte[] eData = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1166
            if (kerr == null) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1167
                if (ke.returnCode() == Krb5.KDC_ERR_PREAUTH_REQUIRED ||
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1168
                        ke.returnCode() == Krb5.KDC_ERR_PREAUTH_FAILED) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1169
                    DerOutputStream bytes = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1170
                    bytes.write(new PAData(Krb5.PA_ENC_TIMESTAMP, new byte[0]).asn1Encode());
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1171
                    for (PAData p: outPAs) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1172
                        bytes.write(p.asn1Encode());
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1173
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1174
                    DerOutputStream temp = new DerOutputStream();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1175
                    temp.write(DerValue.tag_Sequence, bytes);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1176
                    eData = temp.toByteArray();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1177
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1178
                kerr = new KRBError(null, null, null,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1179
                        new KerberosTime(new Date()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1180
                        0,
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1181
                        ke.returnCode(),
13247
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1182
                        body.cname,
74902cfeb9c6 6966259: Make PrincipalName and Realm immutable
weijun
parents: 12867
diff changeset
  1183
                        service,
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1184
                        KrbException.errorMessage(ke.returnCode()),
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1185
                        eData);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1186
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1187
            return kerr.asn1Encode();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1188
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1189
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1190
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1191
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1192
     * 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
  1193
     * @return REALM.NAME = { kdc = host:port etc }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1194
     */
24631
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1195
    private String realmLine() {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1196
        StringBuilder sb = new StringBuilder();
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1197
        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
  1198
                .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
  1199
        for (String s: conf) {
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1200
            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
  1201
        }
dafd257bc7f3 8036779: sun.security.krb5.KdcComm interprets kdc_timeout as msec instead of sec
weijun
parents: 23010
diff changeset
  1202
        return sb.append("}\n").toString();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1203
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1204
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1205
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1206
     * Start the KDC service. This server listens on both UDP and TCP using
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1207
     * the same port number. It uses three threads to deal with requests.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1208
     * They can be set to daemon threads if requested.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1209
     * @param port the port number to listen to. If zero, a random available
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1210
     *  port no less than 8000 will be chosen and used.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1211
     * @param asDaemon true if the KDC threads should be daemons
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1212
     * @throws java.io.IOException for any communication error
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1213
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1214
    protected void startServer(int port, boolean asDaemon) throws IOException {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1215
        if (port > 0) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1216
            u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1217
            t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1218
        } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1219
            while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1220
                // Try to find a port number that's both TCP and UDP free
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1221
                try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1222
                    port = 8000 + new java.util.Random().nextInt(10000);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1223
                    u1 = null;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1224
                    u1 = new DatagramSocket(port, InetAddress.getByName("127.0.0.1"));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1225
                    t1 = new ServerSocket(port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1226
                    break;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1227
                } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1228
                    if (u1 != null) u1.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1229
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1230
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1231
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1232
        final DatagramSocket udp = u1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1233
        final ServerSocket tcp = t1;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1234
        System.out.println("Start KDC on " + port);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1235
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1236
        this.port = port;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1237
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1238
        // The UDP consumer
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1239
        thread1 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1240
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1241
                udpConsumerReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1242
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1243
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1244
                        byte[] inbuf = new byte[8192];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1245
                        DatagramPacket p = new DatagramPacket(inbuf, inbuf.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1246
                        udp.receive(p);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1247
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1248
                        System.out.println(">>>>> UDP packet received");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1249
                        q.put(new Job(processMessage(Arrays.copyOf(inbuf, p.getLength())), udp, p));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1250
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1251
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1252
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1253
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1254
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1255
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1256
        thread1.setDaemon(asDaemon);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1257
        thread1.start();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1258
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1259
        // The TCP consumer
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1260
        thread2 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1261
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1262
                tcpConsumerReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1263
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1264
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1265
                        Socket socket = tcp.accept();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1266
                        System.out.println("-----------------------------------------------");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1267
                        System.out.println(">>>>> TCP connection established");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1268
                        DataInputStream in = new DataInputStream(socket.getInputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1269
                        DataOutputStream out = new DataOutputStream(socket.getOutputStream());
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1270
                        byte[] token = new byte[in.readInt()];
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1271
                        in.readFully(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1272
                        q.put(new Job(processMessage(token), socket, out));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1273
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1274
                        e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1275
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1276
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1277
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1278
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1279
        thread2.setDaemon(asDaemon);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1280
        thread2.start();
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1281
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1282
        // The dispatcher
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1283
        thread3 = new Thread() {
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1284
            public void run() {
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1285
                dispatcherReady = true;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1286
                while (true) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1287
                    try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1288
                        q.take().send();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1289
                    } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1290
                    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1291
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1292
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1293
        };
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1294
        thread3.setDaemon(true);
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1295
        thread3.start();
30038
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1296
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1297
        // wait for the KDC is ready
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1298
        try {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1299
            while (!isReady()) {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1300
                Thread.sleep(100);
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1301
            }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1302
        } catch(InterruptedException e) {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1303
            throw new IOException(e);
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1304
        }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1305
    }
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1306
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1307
    boolean isReady() {
632939157af2 8075007: Additional tests for krb5-related cipher suites with unbound server
asmotrak
parents: 28670
diff changeset
  1308
        return udpConsumerReady && tcpConsumerReady && dispatcherReady;
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1309
    }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1310
4531
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1311
    public void terminate() {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1312
        try {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1313
            thread1.stop();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1314
            thread2.stop();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1315
            thread3.stop();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1316
            u1.close();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1317
            t1.close();
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1318
        } catch (Exception e) {
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1319
            // OK
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1320
        }
3a9206343ab2 6843127: krb5 should not try to access unavailable kdc too often
weijun
parents: 4336
diff changeset
  1321
    }
31429
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1322
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1323
    public static void startKDC(final String host, final String krbConfFileName,
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1324
            final String realm, final Map<String, String> principals,
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1325
            final String ktab, final KtabMode mode) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1326
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1327
        try {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1328
            KDC kdc = KDC.create(realm, host, 0, true);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1329
            kdc.setOption(KDC.Option.PREAUTH_REQUIRED, Boolean.FALSE);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1330
            KDC.saveConfig(krbConfFileName, kdc);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1331
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1332
            // Add principals
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1333
            if (principals != null) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1334
                principals.forEach((name, password) -> {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1335
                    if (password == null || password.isEmpty()) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1336
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1337
                                "KDC:add a principal '%s' with a random " +
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1338
                                        "password", name));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1339
                        kdc.addPrincipalRandKey(name);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1340
                    } else {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1341
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1342
                                "KDC:add a principal '%s' with '%s' password",
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1343
                                name, password));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1344
                        kdc.addPrincipal(name, password.toCharArray());
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1345
                    }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1346
                });
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1347
            }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1348
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1349
            // Create or append keys to existing keytab file
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1350
            if (ktab != null) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1351
                File ktabFile = new File(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1352
                switch(mode) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1353
                    case APPEND:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1354
                        if (ktabFile.exists()) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1355
                            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1356
                                    "KDC:append keys to an exising keytab "
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1357
                                    + "file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1358
                            kdc.appendKtab(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1359
                        } else {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1360
                            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1361
                                    "KDC:create a new keytab file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1362
                            kdc.writeKtab(ktab);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1363
                        }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1364
                        break;
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1365
                    case EXISTING:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1366
                        System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1367
                                "KDC:use an existing keytab file %s", ktab));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1368
                        break;
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1369
                    default:
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1370
                        throw new RuntimeException(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1371
                                "KDC:unsupported keytab mode: %s", mode));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1372
                }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1373
            }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1374
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1375
            System.out.println(String.format(
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1376
                    "KDC: started on %s:%s with '%s' realm",
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1377
                    host, kdc.getPort(), realm));
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1378
        } catch (Exception e) {
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1379
            throw new RuntimeException("KDC: unexpected exception", e);
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1380
        }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1381
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1382
    }
ce4193650b40 8075301: Tests for sun.security.krb5.principal system property
weijun
parents: 30959
diff changeset
  1383
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1384
    /**
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1385
     * Helper class to encapsulate a job in a KDC.
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1386
     */
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1387
    private static class Job {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1388
        byte[] token;           // The received request at creation time and
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1389
                                // the response at send time
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1390
        Socket s;               // The TCP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1391
        DataOutputStream out;   // The OutputStream of the TCP socket
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1392
        DatagramSocket s2;      // The UDP socket from where the request comes
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1393
        DatagramPacket dp;      // The incoming UDP datagram packet
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1394
        boolean useTCP;         // Whether TCP or UDP is used
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1395
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1396
        // Creates a job object for TCP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1397
        Job(byte[] token, Socket s, DataOutputStream out) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1398
            useTCP = true;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1399
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1400
            this.s = s;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1401
            this.out = out;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1402
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1403
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1404
        // Creates a job object for UDP
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1405
        Job(byte[] token, DatagramSocket s2, DatagramPacket dp) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1406
            useTCP = false;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1407
            this.token = token;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1408
            this.s2 = s2;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1409
            this.dp = dp;
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1410
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1411
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1412
        // Sends the output back to the client
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1413
        void send() {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1414
            try {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1415
                if (useTCP) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1416
                    System.out.println(">>>>> TCP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1417
                    out.writeInt(token.length);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1418
                    out.write(token);
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1419
                    s.close();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1420
                } else {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1421
                    System.out.println(">>>>> UDP request honored");
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1422
                    s2.send(new DatagramPacket(token, token.length, dp.getAddress(), dp.getPort()));
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1423
                }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1424
            } catch (Exception e) {
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1425
                e.printStackTrace();
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1426
            }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1427
        }
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1428
    }
3046
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1429
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1430
    public static class KDCNameService implements NameServiceDescriptor {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1431
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1432
        public NameService createNameService() throws Exception {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1433
            NameService ns = new NameService() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1434
                @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1435
                public InetAddress[] lookupAllHostAddr(String host)
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1436
                        throws UnknownHostException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1437
                    // Everything is localhost
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1438
                    return new InetAddress[]{
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1439
                        InetAddress.getByAddress(host, new byte[]{127,0,0,1})
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1440
                    };
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1441
                }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1442
                @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1443
                public String getHostByAddr(byte[] addr)
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1444
                        throws UnknownHostException {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1445
                    // No reverse lookup, PrincipalName use original string
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1446
                    throw new UnknownHostException();
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1447
                }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1448
            };
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1449
            return ns;
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1450
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1451
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1452
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1453
        public String getProviderName() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1454
            return "mock";
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1455
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1456
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1457
        @Override
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1458
        public String getType() {
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1459
            return "ns";
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1460
        }
dd50d75d88e6 6849275: enhance krb5 reg tests
weijun
parents: 2942
diff changeset
  1461
    }
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1462
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1463
    // Calling private methods thru reflections
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1464
    private static final Field getPADataField;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1465
    private static final Field getEType;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1466
    private static final Constructor<EncryptedData> ctorEncryptedData;
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1467
    private static final Method stringToKey;
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1468
    private static final Field getAddlTkt;
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1469
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1470
    static {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1471
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1472
            ctorEncryptedData = EncryptedData.class.getDeclaredConstructor(DerValue.class);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1473
            ctorEncryptedData.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1474
            getPADataField = KDCReq.class.getDeclaredField("pAData");
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1475
            getPADataField.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1476
            getEType = KDCReqBody.class.getDeclaredField("eType");
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1477
            getEType.setAccessible(true);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1478
            stringToKey = EncryptionKey.class.getDeclaredMethod(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1479
                    "stringToKey",
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1480
                    char[].class, String.class, byte[].class, Integer.TYPE);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1481
            stringToKey.setAccessible(true);
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1482
            getAddlTkt = KDCReqBody.class.getDeclaredField("additionalTickets");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1483
            getAddlTkt.setAccessible(true);
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1484
        } catch (NoSuchFieldException nsfe) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1485
            throw new AssertionError(nsfe);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1486
        } catch (NoSuchMethodException nsme) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1487
            throw new AssertionError(nsme);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1488
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1489
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1490
    private EncryptedData newEncryptedData(DerValue der) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1491
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1492
            return ctorEncryptedData.newInstance(der);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1493
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1494
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1495
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1496
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1497
    private static PAData[] KDCReqDotPAData(KDCReq req) {
7183
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1498
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1499
            return (PAData[])getPADataField.get(req);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1500
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1501
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1502
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1503
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1504
    private static int[] KDCReqBodyDotEType(KDCReqBody body) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1505
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1506
            return (int[]) getEType.get(body);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1507
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1508
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1509
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1510
    }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1511
    private static byte[] EncryptionKeyDotStringToKey(char[] password, String salt,
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1512
            byte[] s2kparams, int keyType) throws KrbCryptoException {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1513
        try {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1514
            return (byte[])stringToKey.invoke(
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1515
                    null, password, salt, s2kparams, keyType);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1516
        } catch (InvocationTargetException ex) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1517
            throw (KrbCryptoException)ex.getCause();
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1518
        } catch (Exception e) {
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1519
            throw new AssertionError(e);
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1520
        }
d8ccc1c73358 6960894: Better AS-REQ creation and processing
weijun
parents: 7037
diff changeset
  1521
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1522
    private static Ticket KDCReqBodyDotFirstAdditionalTicket(KDCReqBody body) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1523
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1524
            return ((Ticket[])getAddlTkt.get(body))[0];
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1525
        } catch (Exception e) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1526
            throw new AssertionError(e);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1527
        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 13247
diff changeset
  1528
    }
1454
d9b6f1de641f 6706974: Add krb5 test infrastructure
weijun
parents:
diff changeset
  1529
}