jdk/src/java.security.jgss/share/classes/sun/security/krb5/internal/ssl/Krb5KeyExchangeService.java
author weijun
Tue, 02 Jun 2015 22:26:36 +0800
changeset 30905 bba6fefdd660
child 31800 450f972e0488
permissions -rw-r--r--
8038089: TLS optional support for Kerberos cipher suites needs to be re-examine Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30905
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     1
/*
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     4
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    10
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    15
 * accompanied this code).
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    16
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    20
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    23
 * questions.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    24
 */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    25
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    26
package sun.security.krb5.internal.ssl;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    27
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    28
import sun.security.ssl.ClientKeyExchange;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    29
import sun.security.ssl.Debug;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    30
import sun.security.ssl.ClientKeyExchangeService;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    31
import sun.security.ssl.HandshakeOutStream;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    32
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    33
import sun.security.jgss.GSSCaller;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    34
import sun.security.jgss.krb5.Krb5Util;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    35
import sun.security.jgss.krb5.ServiceCreds;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    36
import sun.security.krb5.EncryptedData;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    37
import sun.security.krb5.EncryptionKey;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    38
import sun.security.krb5.KrbException;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    39
import sun.security.krb5.PrincipalName;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    40
import sun.security.krb5.internal.EncTicketPart;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    41
import sun.security.krb5.internal.Ticket;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    42
import sun.security.krb5.internal.crypto.KeyUsage;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    43
import sun.security.ssl.ProtocolVersion;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    44
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    45
import javax.crypto.SecretKey;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    46
import javax.crypto.spec.SecretKeySpec;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    47
import javax.security.auth.Subject;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    48
import javax.security.auth.kerberos.KerberosKey;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    49
import javax.security.auth.kerberos.KerberosPrincipal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    50
import javax.security.auth.kerberos.KerberosTicket;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    51
import javax.security.auth.kerberos.KeyTab;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    52
import javax.security.auth.kerberos.ServicePermission;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    53
import java.io.IOException;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    54
import java.io.PrintStream;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    55
import java.net.InetAddress;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    56
import java.security.AccessControlContext;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    57
import java.security.AccessController;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    58
import java.security.Principal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    59
import java.security.PrivilegedAction;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    60
import java.security.PrivilegedActionException;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    61
import java.security.PrivilegedExceptionAction;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    62
import java.security.SecureRandom;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    63
import java.util.Set;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    64
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    65
/**
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    66
 * The provider for TLS_KRB_ cipher suites.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    67
 *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    68
 * @since 1.9
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    69
 */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    70
public class Krb5KeyExchangeService implements ClientKeyExchangeService {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    71
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    72
    public static final Debug debug = Debug.getInstance("ssl");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    73
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    74
    @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    75
    public String[] supported() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    76
        return new String[] { "KRB5", "KRB5_EXPORT" };
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    77
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    78
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    79
    @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    80
    public Object getServiceCreds(AccessControlContext acc) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    81
        try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    82
            ServiceCreds serviceCreds = AccessController.doPrivileged(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    83
                    (PrivilegedExceptionAction<ServiceCreds>)
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    84
                            () -> Krb5Util.getServiceCreds(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    85
                                    GSSCaller.CALLER_SSL_SERVER, null, acc));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    86
            if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    87
                System.out.println("Using Kerberos creds");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    88
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    89
            String serverPrincipal = serviceCreds.getName();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    90
            if (serverPrincipal != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    91
                // When service is bound, we check ASAP. Otherwise,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    92
                // will check after client request is received
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    93
                // in in Kerberos ClientKeyExchange
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    94
                SecurityManager sm = System.getSecurityManager();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    95
                try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    96
                    if (sm != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    97
                        // Eliminate dependency on ServicePermission
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    98
                        sm.checkPermission(new ServicePermission(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
    99
                                serverPrincipal, "accept"), acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   100
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   101
                } catch (SecurityException se) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   102
                    if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   103
                        System.out.println("Permission to access Kerberos"
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   104
                                + " secret key denied");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   105
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   106
                    return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   107
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   108
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   109
            return serviceCreds;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   110
        } catch (PrivilegedActionException e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   111
            // Likely exception here is LoginException
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   112
            if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   113
                System.out.println("Attempt to obtain Kerberos key failed: "
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   114
                        + e.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   115
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   116
            return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   117
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   118
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   119
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   120
    @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   121
    public String getServiceHostName(Principal principal) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   122
        if (principal == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   123
            return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   124
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   125
        String hostName = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   126
        try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   127
            PrincipalName princName =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   128
                    new PrincipalName(principal.getName(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   129
                            PrincipalName.KRB_NT_SRV_HST);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   130
            String[] nameParts = princName.getNameStrings();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   131
            if (nameParts.length >= 2) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   132
                hostName = nameParts[1];
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   133
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   134
        } catch (Exception e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   135
            // ignore
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   136
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   137
        return hostName;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   138
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   139
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   140
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   141
    @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   142
    public boolean isRelated(boolean isClient,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   143
            AccessControlContext acc, Principal p) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   144
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   145
        if (p == null) return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   146
        try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   147
            Subject subject = AccessController.doPrivileged(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   148
                    (PrivilegedExceptionAction<Subject>)
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   149
                            () -> Krb5Util.getSubject(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   150
                                    isClient ? GSSCaller.CALLER_SSL_CLIENT
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   151
                                            : GSSCaller.CALLER_SSL_SERVER,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   152
                                    acc));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   153
            if (subject == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   154
                if (debug != null && Debug.isOn("session")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   155
                    System.out.println("Kerberos credentials are" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   156
                            " not present in the current Subject;" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   157
                            " check if " +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   158
                            " javax.security.auth.useSubjectAsCreds" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   159
                            " system property has been set to false");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   160
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   161
                return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   162
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   163
            Set<Principal> principals =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   164
                    subject.getPrincipals(Principal.class);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   165
            if (principals.contains(p)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   166
                // bound to this principal
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   167
                return true;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   168
            } else {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   169
                if (isClient) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   170
                    return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   171
                } else {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   172
                    for (KeyTab pc : subject.getPrivateCredentials(KeyTab.class)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   173
                        if (!pc.isBound()) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   174
                            return true;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   175
                        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   176
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   177
                    return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   178
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   179
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   180
        } catch (PrivilegedActionException pae) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   181
            if (debug != null && Debug.isOn("session")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   182
                System.out.println("Attempt to obtain" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   183
                        " subject failed! " + pae);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   184
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   185
            return false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   186
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   187
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   188
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   189
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   190
    public ClientKeyExchange createClientExchange(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   191
            String serverName, AccessControlContext acc,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   192
            ProtocolVersion protocolVerson, SecureRandom rand) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   193
        return new ExchangerImpl(serverName, acc, protocolVerson, rand);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   194
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   195
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   196
    public ClientKeyExchange createServerExchange(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   197
            ProtocolVersion protocolVersion, ProtocolVersion clientVersion,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   198
            SecureRandom rand, byte[] encodedTicket, byte[] encrypted,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   199
            AccessControlContext acc, Object serviceCreds) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   200
        return new ExchangerImpl(protocolVersion, clientVersion, rand,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   201
                encodedTicket, encrypted, acc, serviceCreds);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   202
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   203
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   204
    static class ExchangerImpl extends ClientKeyExchange {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   205
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   206
        final private KerberosPreMasterSecret preMaster;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   207
        final private byte[] encodedTicket;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   208
        final private KerberosPrincipal peerPrincipal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   209
        final private KerberosPrincipal localPrincipal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   210
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   211
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   212
        public int messageLength() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   213
            return encodedTicket.length + preMaster.getEncrypted().length + 6;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   214
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   215
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   216
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   217
        public void send(HandshakeOutStream s) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   218
            s.putBytes16(encodedTicket);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   219
            s.putBytes16(null);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   220
            s.putBytes16(preMaster.getEncrypted());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   221
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   222
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   223
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   224
        public void print(PrintStream s) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   225
            s.println("*** ClientKeyExchange, Kerberos");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   226
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   227
            if (debug != null && Debug.isOn("verbose")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   228
                Debug.println(s, "Kerberos service ticket", encodedTicket);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   229
                Debug.println(s, "Random Secret", preMaster.getUnencrypted());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   230
                Debug.println(s, "Encrypted random Secret", preMaster.getEncrypted());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   231
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   232
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   233
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   234
        ExchangerImpl(String serverName, AccessControlContext acc,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   235
                ProtocolVersion protocolVersion, SecureRandom rand) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   236
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   237
            // Get service ticket
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   238
            KerberosTicket ticket = getServiceTicket(serverName, acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   239
            encodedTicket = ticket.getEncoded();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   240
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   241
            // Record the Kerberos principals
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   242
            peerPrincipal = ticket.getServer();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   243
            localPrincipal = ticket.getClient();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   244
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   245
            // Optional authenticator, encrypted using session key,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   246
            // currently ignored
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   247
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   248
            // Generate premaster secret and encrypt it using session key
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   249
            EncryptionKey sessionKey = new EncryptionKey(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   250
                    ticket.getSessionKeyType(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   251
                    ticket.getSessionKey().getEncoded());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   252
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   253
            preMaster = new KerberosPreMasterSecret(protocolVersion,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   254
                    rand, sessionKey);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   255
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   256
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   257
        ExchangerImpl(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   258
                ProtocolVersion protocolVersion, ProtocolVersion clientVersion, SecureRandom rand,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   259
                byte[] encodedTicket, byte[] encrypted,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   260
                AccessControlContext acc, Object serviceCreds) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   261
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   262
            // Read ticket
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   263
            this.encodedTicket = encodedTicket;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   264
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   265
            if (debug != null && Debug.isOn("verbose")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   266
                Debug.println(System.out,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   267
                        "encoded Kerberos service ticket", encodedTicket);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   268
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   269
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   270
            EncryptionKey sessionKey = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   271
            KerberosPrincipal tmpPeer = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   272
            KerberosPrincipal tmpLocal = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   273
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   274
            try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   275
                Ticket t = new Ticket(encodedTicket);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   276
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   277
                EncryptedData encPart = t.encPart;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   278
                PrincipalName ticketSname = t.sname;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   279
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   280
                final ServiceCreds creds = (ServiceCreds)serviceCreds;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   281
                final KerberosPrincipal princ =
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   282
                        new KerberosPrincipal(ticketSname.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   283
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   284
                // For bound service, permission already checked at setup
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   285
                if (creds.getName() == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   286
                    SecurityManager sm = System.getSecurityManager();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   287
                    try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   288
                        if (sm != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   289
                            // Eliminate dependency on ServicePermission
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   290
                            sm.checkPermission(new ServicePermission(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   291
                                    ticketSname.toString(), "accept"), acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   292
                        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   293
                    } catch (SecurityException se) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   294
                        serviceCreds = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   295
                        // Do not destroy keys. Will affect Subject
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   296
                        if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   297
                            System.out.println("Permission to access Kerberos"
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   298
                                    + " secret key denied");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   299
                            se.printStackTrace(System.out);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   300
                        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   301
                        throw new IOException("Kerberos service not allowedy");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   302
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   303
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   304
                KerberosKey[] serverKeys = AccessController.doPrivileged(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   305
                        new PrivilegedAction<KerberosKey[]>() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   306
                            @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   307
                            public KerberosKey[] run() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   308
                                return creds.getKKeys(princ);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   309
                            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   310
                        });
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   311
                if (serverKeys.length == 0) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   312
                    throw new IOException("Found no key for " + princ +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   313
                            (creds.getName() == null ? "" :
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   314
                                    (", this keytab is for " + creds.getName() + " only")));
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   315
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   316
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   317
                /*
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   318
                 * permission to access and use the secret key of the Kerberized
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   319
                 * "host" service is done in ServerHandshaker.getKerberosKeys()
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   320
                 * to ensure server has the permission to use the secret key
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   321
                 * before promising the client
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   322
                 */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   323
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   324
                // See if we have the right key to decrypt the ticket to get
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   325
                // the session key.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   326
                int encPartKeyType = encPart.getEType();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   327
                Integer encPartKeyVersion = encPart.getKeyVersionNumber();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   328
                KerberosKey dkey = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   329
                try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   330
                    dkey = findKey(encPartKeyType, encPartKeyVersion, serverKeys);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   331
                } catch (KrbException ke) { // a kvno mismatch
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   332
                    throw new IOException(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   333
                            "Cannot find key matching version number", ke);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   334
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   335
                if (dkey == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   336
                    // %%% Should print string repr of etype
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   337
                    throw new IOException("Cannot find key of appropriate type" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   338
                            " to decrypt ticket - need etype " + encPartKeyType);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   339
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   340
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   341
                EncryptionKey secretKey = new EncryptionKey(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   342
                        encPartKeyType,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   343
                        dkey.getEncoded());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   344
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   345
                // Decrypt encPart using server's secret key
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   346
                byte[] bytes = encPart.decrypt(secretKey, KeyUsage.KU_TICKET);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   347
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   348
                // Reset data stream after decryption, remove redundant bytes
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   349
                byte[] temp = encPart.reset(bytes);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   350
                EncTicketPart encTicketPart = new EncTicketPart(temp);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   351
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   352
                // Record the Kerberos Principals
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   353
                tmpPeer = new KerberosPrincipal(encTicketPart.cname.getName());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   354
                tmpLocal = new KerberosPrincipal(ticketSname.getName());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   355
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   356
                sessionKey = encTicketPart.key;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   357
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   358
                if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   359
                    System.out.println("server principal: " + ticketSname);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   360
                    System.out.println("cname: " + encTicketPart.cname.toString());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   361
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   362
            } catch (IOException e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   363
                throw e;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   364
            } catch (Exception e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   365
                if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   366
                    System.out.println("KerberosWrapper error getting session key,"
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   367
                            + " generating random secret (" + e.getMessage() + ")");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   368
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   369
                sessionKey = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   370
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   371
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   372
            //input.getBytes16();   // XXX Read and ignore authenticator
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   373
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   374
            if (sessionKey != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   375
                preMaster = new KerberosPreMasterSecret(protocolVersion,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   376
                        clientVersion, rand, encrypted, sessionKey);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   377
            } else {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   378
                // Generate bogus premaster secret
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   379
                preMaster = new KerberosPreMasterSecret(clientVersion, rand);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   380
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   381
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   382
            peerPrincipal = tmpPeer;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   383
            localPrincipal = tmpLocal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   384
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   385
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   386
        // Similar to sun.security.jgss.krb5.Krb5InitCredenetial/Krb5Context
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   387
        private static KerberosTicket getServiceTicket(String serverName,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   388
                final AccessControlContext acc) throws IOException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   389
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   390
            if ("localhost".equals(serverName) ||
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   391
                    "localhost.localdomain".equals(serverName)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   392
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   393
                if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   394
                    System.out.println("Get the local hostname");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   395
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   396
                String localHost = java.security.AccessController.doPrivileged(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   397
                        new java.security.PrivilegedAction<String>() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   398
                            public String run() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   399
                                try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   400
                                    return InetAddress.getLocalHost().getHostName();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   401
                                } catch (java.net.UnknownHostException e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   402
                                    if (debug != null && Debug.isOn("handshake")) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   403
                                        System.out.println("Warning,"
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   404
                                                + " cannot get the local hostname: "
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   405
                                                + e.getMessage());
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   406
                                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   407
                                    return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   408
                                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   409
                            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   410
                        });
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   411
                if (localHost != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   412
                    serverName = localHost;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   413
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   414
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   415
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   416
            // Resolve serverName (possibly in IP addr form) to Kerberos principal
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   417
            // name for service with hostname
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   418
            String serviceName = "host/" + serverName;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   419
            PrincipalName principal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   420
            try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   421
                principal = new PrincipalName(serviceName,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   422
                        PrincipalName.KRB_NT_SRV_HST);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   423
            } catch (SecurityException se) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   424
                throw se;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   425
            } catch (Exception e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   426
                IOException ioe = new IOException("Invalid service principal" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   427
                        " name: " + serviceName);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   428
                ioe.initCause(e);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   429
                throw ioe;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   430
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   431
            String realm = principal.getRealmAsString();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   432
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   433
            final String serverPrincipal = principal.toString();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   434
            final String tgsPrincipal = "krbtgt/" + realm + "@" + realm;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   435
            final String clientPrincipal = null;  // use default
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   436
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   437
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   438
            // check permission to obtain a service ticket to initiate a
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   439
            // context with the "host" service
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   440
            SecurityManager sm = System.getSecurityManager();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   441
            if (sm != null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   442
                sm.checkPermission(new ServicePermission(serverPrincipal,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   443
                        "initiate"), acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   444
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   445
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   446
            try {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   447
                KerberosTicket ticket = AccessController.doPrivileged(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   448
                        new PrivilegedExceptionAction<KerberosTicket>() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   449
                            public KerberosTicket run() throws Exception {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   450
                                return Krb5Util.getTicketFromSubjectAndTgs(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   451
                                        GSSCaller.CALLER_SSL_CLIENT,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   452
                                        clientPrincipal, serverPrincipal,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   453
                                        tgsPrincipal, acc);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   454
                            }});
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   455
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   456
                if (ticket == null) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   457
                    throw new IOException("Failed to find any kerberos service" +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   458
                            " ticket for " + serverPrincipal);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   459
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   460
                return ticket;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   461
            } catch (PrivilegedActionException e) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   462
                IOException ioe = new IOException(
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   463
                        "Attempt to obtain kerberos service ticket for " +
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   464
                                serverPrincipal + " failed!");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   465
                ioe.initCause(e);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   466
                throw ioe;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   467
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   468
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   469
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   470
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   471
        public SecretKey clientKeyExchange() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   472
            byte[] secretBytes = preMaster.getUnencrypted();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   473
            return new SecretKeySpec(secretBytes, "TlsPremasterSecret");
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   474
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   475
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   476
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   477
        public Principal getPeerPrincipal() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   478
            return peerPrincipal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   479
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   480
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   481
        @Override
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   482
        public Principal getLocalPrincipal() {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   483
            return localPrincipal;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   484
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   485
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   486
        /**
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   487
         * Determines if a kvno matches another kvno. Used in the method
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   488
         * findKey(etype, version, keys). Always returns true if either input
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   489
         * is null or zero, in case any side does not have kvno info available.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   490
         *
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   491
         * Note: zero is included because N/A is not a legal value for kvno
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   492
         * in javax.security.auth.kerberos.KerberosKey. Therefore, the info
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   493
         * that the kvno is N/A might be lost when converting between
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   494
         * EncryptionKey and KerberosKey.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   495
         */
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   496
        private static boolean versionMatches(Integer v1, int v2) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   497
            if (v1 == null || v1 == 0 || v2 == 0) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   498
                return true;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   499
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   500
            return v1.equals(v2);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   501
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   502
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   503
        private static KerberosKey findKey(int etype, Integer version,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   504
                KerberosKey[] keys) throws KrbException {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   505
            int ktype;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   506
            boolean etypeFound = false;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   507
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   508
            // When no matched kvno is found, returns tke key of the same
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   509
            // etype with the highest kvno
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   510
            int kvno_found = 0;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   511
            KerberosKey key_found = null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   512
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   513
            for (int i = 0; i < keys.length; i++) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   514
                ktype = keys[i].getKeyType();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   515
                if (etype == ktype) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   516
                    int kv = keys[i].getVersionNumber();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   517
                    etypeFound = true;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   518
                    if (versionMatches(version, kv)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   519
                        return keys[i];
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   520
                    } else if (kv > kvno_found) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   521
                        key_found = keys[i];
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   522
                        kvno_found = kv;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   523
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   524
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   525
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   526
            // Key not found.
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   527
            // %%% kludge to allow DES keys to be used for diff etypes
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   528
            if ((etype == EncryptedData.ETYPE_DES_CBC_CRC ||
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   529
                    etype == EncryptedData.ETYPE_DES_CBC_MD5)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   530
                for (int i = 0; i < keys.length; i++) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   531
                    ktype = keys[i].getKeyType();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   532
                    if (ktype == EncryptedData.ETYPE_DES_CBC_CRC ||
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   533
                            ktype == EncryptedData.ETYPE_DES_CBC_MD5) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   534
                        int kv = keys[i].getVersionNumber();
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   535
                        etypeFound = true;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   536
                        if (versionMatches(version, kv)) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   537
                            return new KerberosKey(keys[i].getPrincipal(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   538
                                    keys[i].getEncoded(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   539
                                    etype,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   540
                                    kv);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   541
                        } else if (kv > kvno_found) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   542
                            key_found = new KerberosKey(keys[i].getPrincipal(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   543
                                    keys[i].getEncoded(),
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   544
                                    etype,
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   545
                                    kv);
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   546
                            kvno_found = kv;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   547
                        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   548
                    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   549
                }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   550
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   551
            if (etypeFound) {
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   552
                return key_found;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   553
            }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   554
            return null;
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   555
        }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   556
    }
bba6fefdd660 8038089: TLS optional support for Kerberos cipher suites needs to be re-examine
weijun
parents:
diff changeset
   557
}