src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5InitCredential.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58638 7be56b2ac50d
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3220
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3220
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3220
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3220
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3220
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.jgss.krb5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    29
import sun.security.jgss.GSSCaller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.krb5.*;
25662
056656d69979 8051399: Fix for JDK-8043071 breaks dev build
weijun
parents: 23010
diff changeset
    32
import javax.security.auth.kerberos.KerberosTicket;
056656d69979 8051399: Fix for JDK-8043071 breaks dev build
weijun
parents: 23010
diff changeset
    33
import javax.security.auth.kerberos.KerberosPrincipal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.InetAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Implements the krb5 initiator credential element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Mayank Upadhyay
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Ram Marti
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class Krb5InitCredential
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    extends KerberosTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    implements Krb5CredElement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static final long serialVersionUID = 7723415700837898232L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
58510
23a06a5eeddd 8231368: Suppress warnings on non-serializable non-transient instance fields in java.security.jgss
darcy
parents: 57487
diff changeset
    56
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Krb5NameElement name;
58510
23a06a5eeddd 8231368: Suppress warnings on non-serializable non-transient instance fields in java.security.jgss
darcy
parents: 57487
diff changeset
    58
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Credentials krb5Credentials;
58611
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
    60
    public KerberosTicket proxyTicket;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private Krb5InitCredential(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               byte[] asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               KerberosPrincipal client,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    65
                               KerberosPrincipal clientAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                               KerberosPrincipal server,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    67
                               KerberosPrincipal serverAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                               byte[] sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                               int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                               boolean[] flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                               Date authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                               Date startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                               Date endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                               Date renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                               InetAddress[] clientAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                               throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        super(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
              client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
              server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
              clientAddresses);
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    88
        KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    89
                .kerberosTicketSetClientAlias(this, clientAlias);
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    90
        KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    91
                .kerberosTicketSetServerAlias(this, serverAlias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // Cache this for later use by the sun.security.krb5 package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            krb5Credentials = new Credentials(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                              client.getName(),
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    98
                                              (clientAlias != null ?
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
    99
                                                      clientAlias.getName() : null),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                              server.getName(),
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   101
                                              (serverAlias != null ?
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   102
                                                      serverAlias.getName() : null),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                                              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                                              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                                              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                                              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                                              clientAddresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private Krb5InitCredential(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                               Credentials delegatedCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                               byte[] asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                               KerberosPrincipal client,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   125
                               KerberosPrincipal clientAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                               KerberosPrincipal server,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   127
                               KerberosPrincipal serverAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                               byte[] sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                               int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                               boolean[] flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                               Date authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                               Date startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                               Date endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                               Date renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                               InetAddress[] clientAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                               throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
              client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
              server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
              clientAddresses);
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   148
        KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   149
                .kerberosTicketSetClientAlias(this, clientAlias);
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   150
        KerberosSecrets.getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   151
                .kerberosTicketSetServerAlias(this, serverAlias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        // A delegated cred does not have all fields set. So do not try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // creat new Credentials out of the delegatedCred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.krb5Credentials = delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   158
    static Krb5InitCredential getInstance(GSSCaller caller, Krb5NameElement name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                   int initLifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        KerberosTicket tgt = getTgt(caller, name, initLifetime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (tgt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                   "Failed to find any Kerberos tgt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            String fullName = tgt.getClient().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            name = Krb5NameElement.getInstance(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                       Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   173
        KerberosPrincipal clientAlias = KerberosSecrets
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   174
                .getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   175
                .kerberosTicketGetClientAlias(tgt);
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   176
        KerberosPrincipal serverAlias = KerberosSecrets
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   177
                .getJavaxSecurityAuthKerberosAccess()
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   178
                .kerberosTicketGetServerAlias(tgt);
58611
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   179
        Krb5InitCredential result = new Krb5InitCredential(name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                      tgt.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                      tgt.getClient(),
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   182
                                      clientAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                                      tgt.getServer(),
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   184
                                      serverAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                      tgt.getSessionKey().getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                      tgt.getSessionKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                      tgt.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                      tgt.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                      tgt.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                      tgt.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                      tgt.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                      tgt.getClientAddresses());
58611
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   193
        result.proxyTicket = KerberosSecrets.getJavaxSecurityAuthKerberosAccess().
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   194
            kerberosTicketGetProxy(tgt);
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   195
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    static Krb5InitCredential getInstance(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                   Credentials delegatedCred)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        EncryptionKey sessionKey = delegatedCred.getSessionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * all of the following data is optional in a KRB-CRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * messages. This check for each field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        PrincipalName cPrinc = delegatedCred.getClient();
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   210
        PrincipalName cAPrinc = delegatedCred.getClientAlias();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        PrincipalName sPrinc = delegatedCred.getServer();
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   212
        PrincipalName sAPrinc = delegatedCred.getServerAlias();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        KerberosPrincipal client = null;
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   215
        KerberosPrincipal clientAlias = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        KerberosPrincipal server = null;
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   217
        KerberosPrincipal serverAlias = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        Krb5NameElement credName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (cPrinc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            String fullName = cPrinc.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            credName = Krb5NameElement.getInstance(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                               Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            client =  new KerberosPrincipal(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   228
        if (cAPrinc != null) {
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   229
            clientAlias = new KerberosPrincipal(cAPrinc.getName());
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   230
        }
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   231
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // XXX Compare name to credName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (sPrinc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                new KerberosPrincipal(sPrinc.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                        KerberosPrincipal.KRB_NT_SRV_INST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   240
        if (sAPrinc != null) {
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   241
            serverAlias = new KerberosPrincipal(sAPrinc.getName());
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   242
        }
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   243
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return new Krb5InitCredential(credName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                      delegatedCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                      delegatedCred.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                      client,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   248
                                      clientAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                                      server,
57487
643978a35f6e 8227437: S4U2proxy cannot continue because server's TGT cannot be found
mbalao
parents: 50750
diff changeset
   250
                                      serverAlias,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                      sessionKey.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                      sessionKey.getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                      delegatedCred.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                      delegatedCred.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                                      delegatedCred.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                                      delegatedCred.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                      delegatedCred.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                                      delegatedCred.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * Returns the principal name for this credential. The name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * is in mechanism specific format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return GSSNameSpi representing principal name of this credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    public final GSSNameSpi getName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Returns the init lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @return the init lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    public int getInitLifetime() throws GSSException {
40255
0222e4232e7c 8163104: Unexpected NPE still possible on some Kerberos ticket calls
coffeys
parents: 25859
diff changeset
   279
        Date d = getEndTime();
0222e4232e7c 8163104: Unexpected NPE still possible on some Kerberos ticket calls
coffeys
parents: 25859
diff changeset
   280
        if (d == null) {
0222e4232e7c 8163104: Unexpected NPE still possible on some Kerberos ticket calls
coffeys
parents: 25859
diff changeset
   281
            return 0;
0222e4232e7c 8163104: Unexpected NPE still possible on some Kerberos ticket calls
coffeys
parents: 25859
diff changeset
   282
        }
50750
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 47216
diff changeset
   283
        long retVal = d.getTime() - System.currentTimeMillis();
45511dcfed3f 8187218: GSSCredential.getRemainingLifetime() returns negative value for TTL > 24 days.
pkoppula
parents: 47216
diff changeset
   284
        return (int)(retVal/1000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Returns the accept lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the accept lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public int getAcceptLifetime() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public boolean isInitiatorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public boolean isAcceptorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * Returns the oid representing the underlying credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * mechanism oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return the Oid for this credential mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public final Oid getMechanism() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return Krb5MechFactory.GSS_KRB5_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public final java.security.Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        return Krb5MechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns a sun.security.krb5.Credentials instance so that it maybe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * used in that package for th Kerberos protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    Credentials getKrb5Credentials() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return krb5Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * XXX Call to this.refresh() should refresh the locally cached copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * of krb5Credentials also.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Called to invalidate this credential element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } catch (javax.security.auth.DestroyFailedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                 "Could not destroy credentials - " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    // XXX call to this.destroy() should destroy the locally cached copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    // of krb5Credentials and then call super.destroy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   351
    private static KerberosTicket getTgt(GSSCaller caller, Krb5NameElement name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                 int initLifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   355
        final String clientPrincipal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * Find the TGT for the realm that the client is in. If the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * name is not available, then use the default realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            clientPrincipal = (name.getKrb5PrincipalName()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            clientPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        try {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   370
            final GSSCaller realCaller = (caller == GSSCaller.CALLER_UNKNOWN)
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   371
                                   ? GSSCaller.CALLER_INITIATE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                   : caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                new PrivilegedExceptionAction<KerberosTicket>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                public KerberosTicket run() throws Exception {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   376
                    // It's OK to use null as serverPrincipal. TGT is almost
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   377
                    // the first ticket for a principal and we use list.
58611
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   378
                    return Krb5Util.getInitialTicket(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        realCaller,
58611
53ddf218eddd 8220302: Better Kerberos ccache handling
weijun
parents: 57487
diff changeset
   380
                        clientPrincipal, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                        }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            GSSException ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    "Attempt to obtain new INITIATE credentials failed!" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    " (" + e.getMessage() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            ge.initCause(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw ge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   391
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   392
    @Override
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   393
    public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   394
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   395
            Krb5NameElement kname = (Krb5NameElement)name;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   396
            Credentials newCred = Credentials.acquireS4U2selfCreds(
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   397
                    kname.getKrb5PrincipalName(), krb5Credentials);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   398
            return new Krb5ProxyCredential(this, kname, newCred.getTicket());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   399
        } catch (IOException | KrbException ke) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   400
            GSSException ge =
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   401
                new GSSException(GSSException.FAILURE, -1,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   402
                    "Attempt to obtain S4U2self credentials failed!");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   403
            ge.initCause(ke);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   404
            throw ge;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   405
        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   406
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}