jdk/src/java.security.jgss/share/classes/sun/security/jgss/krb5/Krb5InitCredential.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 25662 jdk/src/share/classes/sun/security/jgss/krb5/Krb5InitCredential.java@056656d69979
child 40255 0222e4232e7c
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 14413
diff changeset
     2
 * Copyright (c) 2000, 2012, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Krb5NameElement name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private Credentials krb5Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Krb5InitCredential(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                               byte[] asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                               KerberosPrincipal client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                               KerberosPrincipal server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               byte[] sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                               int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                               boolean[] flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                               Date authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                               Date startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                               Date endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                               Date renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                               InetAddress[] clientAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                               throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
              client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
              server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
              clientAddresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // Cache this for later use by the sun.security.krb5 package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            krb5Credentials = new Credentials(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                                              client.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                              server.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                                              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                              clientAddresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (KrbException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                   e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private Krb5InitCredential(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                               Credentials delegatedCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                               byte[] asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                               KerberosPrincipal client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                               KerberosPrincipal server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                               byte[] sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                               int keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                               boolean[] flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                               Date authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                               Date startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                               Date endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                               Date renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                               InetAddress[] clientAddresses)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                               throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        super(asn1Encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
              client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
              server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
              sessionKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
              keyType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
              flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
              authTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
              startTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
              endTime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
              renewTill,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
              clientAddresses);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // A delegated cred does not have all fields set. So do not try to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // creat new Credentials out of the delegatedCred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.krb5Credentials = delegatedCred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   141
    static Krb5InitCredential getInstance(GSSCaller caller, Krb5NameElement name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                   int initLifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        KerberosTicket tgt = getTgt(caller, name, initLifetime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (tgt == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            throw new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                   "Failed to find any Kerberos tgt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            String fullName = tgt.getClient().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            name = Krb5NameElement.getInstance(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                       Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        return new Krb5InitCredential(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                      tgt.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                      tgt.getClient(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                      tgt.getServer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                      tgt.getSessionKey().getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                      tgt.getSessionKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                                      tgt.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                      tgt.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                      tgt.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                      tgt.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                      tgt.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                      tgt.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static Krb5InitCredential getInstance(Krb5NameElement name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                   Credentials delegatedCred)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        EncryptionKey sessionKey = delegatedCred.getSessionKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * all of the following data is optional in a KRB-CRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * messages. This check for each field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        PrincipalName cPrinc = delegatedCred.getClient();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        PrincipalName sPrinc = delegatedCred.getServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        KerberosPrincipal client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        KerberosPrincipal server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Krb5NameElement credName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (cPrinc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            String fullName = cPrinc.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            credName = Krb5NameElement.getInstance(fullName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                               Krb5MechFactory.NT_GSS_KRB5_PRINCIPAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            client =  new KerberosPrincipal(fullName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // XXX Compare name to credName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (sPrinc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            server =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                new KerberosPrincipal(sPrinc.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                        KerberosPrincipal.KRB_NT_SRV_INST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return new Krb5InitCredential(credName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                      delegatedCred,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                      delegatedCred.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                      client,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                      server,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                      sessionKey.getBytes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                      sessionKey.getEType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                      delegatedCred.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                      delegatedCred.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                      delegatedCred.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                      delegatedCred.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                      delegatedCred.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                      delegatedCred.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns the principal name for this credential. The name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * is in mechanism specific format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return GSSNameSpi representing principal name of this credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public final GSSNameSpi getName() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Returns the init lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @return the init lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public int getInitLifetime() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        int retVal = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        retVal = (int)(getEndTime().getTime()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                       - (new Date().getTime()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
3220
5516f07e05d0 6857802: GSS getRemainingInitLifetime method returns milliseconds not seconds
weijun
parents: 2942
diff changeset
   241
        return retVal/1000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns the accept lifetime remaining.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @return the accept lifetime remaining in seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public int getAcceptLifetime() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public boolean isInitiatorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public boolean isAcceptorCredential() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns the oid representing the underlying credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * mechanism oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @return the Oid for this credential mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception GSSException may be thrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public final Oid getMechanism() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return Krb5MechFactory.GSS_KRB5_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public final java.security.Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return Krb5MechFactory.PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns a sun.security.krb5.Credentials instance so that it maybe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * used in that package for th Kerberos protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    Credentials getKrb5Credentials() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return krb5Credentials;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * XXX Call to this.refresh() should refresh the locally cached copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * of krb5Credentials also.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Called to invalidate this credential element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public void dispose() throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        } catch (javax.security.auth.DestroyFailedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            GSSException gssException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                new GSSException(GSSException.FAILURE, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                 "Could not destroy credentials - " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            gssException.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    // XXX call to this.destroy() should destroy the locally cached copy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    // of krb5Credentials and then call super.destroy().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   308
    private static KerberosTicket getTgt(GSSCaller caller, Krb5NameElement name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                                                 int initLifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   312
        final String clientPrincipal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * Find the TGT for the realm that the client is in. If the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * name is not available, then use the default realm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            clientPrincipal = (name.getKrb5PrincipalName()).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            clientPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        try {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   327
            final GSSCaller realCaller = (caller == GSSCaller.CALLER_UNKNOWN)
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   328
                                   ? GSSCaller.CALLER_INITIATE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                   : caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                new PrivilegedExceptionAction<KerberosTicket>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                public KerberosTicket run() throws Exception {
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   333
                    // It's OK to use null as serverPrincipal. TGT is almost
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   334
                    // the first ticket for a principal and we use list.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    return Krb5Util.getTicket(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                        realCaller,
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   337
                        clientPrincipal, null, acc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            GSSException ge =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                new GSSException(GSSException.NO_CRED, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    "Attempt to obtain new INITIATE credentials failed!" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    " (" + e.getMessage() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            ge.initCause(e.getException());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw ge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
14413
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   348
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   349
    @Override
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   350
    public GSSCredentialSpi impersonate(GSSNameSpi name) throws GSSException {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   351
        try {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   352
            Krb5NameElement kname = (Krb5NameElement)name;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   353
            Credentials newCred = Credentials.acquireS4U2selfCreds(
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   354
                    kname.getKrb5PrincipalName(), krb5Credentials);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   355
            return new Krb5ProxyCredential(this, kname, newCred.getTicket());
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   356
        } catch (IOException | KrbException ke) {
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   357
            GSSException ge =
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   358
                new GSSException(GSSException.FAILURE, -1,
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   359
                    "Attempt to obtain S4U2self credentials failed!");
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   360
            ge.initCause(ke);
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   361
            throw ge;
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   362
        }
e954df027393 6355584: Introduce constrained Kerberos delegation
weijun
parents: 5506
diff changeset
   363
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}