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