jdk/src/java.security.jgss/share/classes/sun/security/jgss/GSSUtil.java
author weijun
Tue, 05 May 2015 21:14:12 +0800
changeset 30324 6f3109ded2b8
parent 25859 3317bb8137f4
permissions -rw-r--r--
8078439: SPNEGO auth fails if client proposes MS krb5 OID Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2011, 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: 4533
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: 4533
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: 4533
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4533
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4533
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.kerberos.KerberosPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.kerberos.KerberosTicket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.kerberos.KerberosKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jgss.spi.GSSNameSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.spi.GSSCredentialSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jgss.krb5.Krb5NameElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jgss.spnego.SpNegoCredElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.login.LoginContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.security.action.GetBooleanAction;
22973
616ad2891b71 8034943: Eliminate Kerberos dependency on com.sun.security.auth to avoid circular dependency
alanb
parents: 10336
diff changeset
    50
import sun.security.util.ConsoleCallbackHandler;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The GSSUtilImplementation that knows how to work with the internals of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * the GSS-API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class GSSUtil {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final Oid GSS_KRB5_MECH_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                GSSUtil.createOid("1.2.840.113554.1.2.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final Oid GSS_KRB5_MECH_OID2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                GSSUtil.createOid("1.3.5.1.5.2");
30324
6f3109ded2b8 8078439: SPNEGO auth fails if client proposes MS krb5 OID
weijun
parents: 25859
diff changeset
    62
    public static final Oid GSS_KRB5_MECH_OID_MS =
6f3109ded2b8 8078439: SPNEGO auth fails if client proposes MS krb5 OID
weijun
parents: 25859
diff changeset
    63
                GSSUtil.createOid("1.2.840.48018.1.2.2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public static final Oid GSS_SPNEGO_MECH_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                GSSUtil.createOid("1.3.6.1.5.5.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final Oid NT_GSS_KRB5_PRINCIPAL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                GSSUtil.createOid("1.2.840.113554.1.2.2.1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final String DEFAULT_HANDLER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            "auth.login.defaultCallbackHandler";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static final boolean DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        DEBUG = (AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                        (new GetBooleanAction("sun.security.jgss.debug"))).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    static void debug(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            assert(message != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // NOTE: this method is only for creating Oid objects with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // known to be valid <code>oidStr</code> given it ignores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // the GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static Oid createOid(String oidStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return new Oid(oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            debug("Ignored invalid OID: " + oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static boolean isSpNegoMech(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return (GSS_SPNEGO_MECH_OID.equals(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static boolean isKerberosMech(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return (GSS_KRB5_MECH_OID.equals(oid) ||
30324
6f3109ded2b8 8078439: SPNEGO auth fails if client proposes MS krb5 OID
weijun
parents: 25859
diff changeset
   106
                GSS_KRB5_MECH_OID2.equals(oid) ||
6f3109ded2b8 8078439: SPNEGO auth fails if client proposes MS krb5 OID
weijun
parents: 25859
diff changeset
   107
                GSS_KRB5_MECH_OID_MS.equals(oid));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static String getMechStr(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (isSpNegoMech(oid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return "SPNEGO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } else if (isKerberosMech(oid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return "Kerberos V5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            return oid.toString();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Note: The current impl only works with Sun's impl of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * GSSName and GSSCredential since it depends on package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * private APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static Subject getSubject(GSSName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                     GSSCredential creds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        HashSet<Object> privCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        HashSet<Object> pubCredentials = new HashSet<Object>(); // empty Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        Set<GSSCredentialSpi> gssCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Set<KerberosPrincipal> krb5Principals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                                new HashSet<KerberosPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (name instanceof GSSNameImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                GSSNameSpi ne = ((GSSNameImpl) name).getElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    (GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                String krbName = ne.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (ne instanceof Krb5NameElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    krbName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        ((Krb5NameElement) ne).getKrb5PrincipalName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                KerberosPrincipal krbPrinc = new KerberosPrincipal(krbName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                krb5Principals.add(krbPrinc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                debug("Skipped name " + name + " due to " + ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (creds instanceof GSSCredentialImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            gssCredentials = ((GSSCredentialImpl) creds).getElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            privCredentials = new HashSet<Object>(gssCredentials.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            populateCredentials(privCredentials, gssCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            privCredentials = new HashSet<Object>(); // empty Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        debug("Created Subject with the following");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        debug("principals=" + krb5Principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        debug("public creds=" + pubCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        debug("private creds=" + privCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return new Subject(false, krb5Principals, pubCredentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                           privCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Populates the set credentials with elements from gssCredentials. At
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * the same time, it converts any subclasses of KerberosTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * into KerberosTicket instances and any subclasses of KerberosKey into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * KerberosKey instances. (It is not desirable to expose the customer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * to sun.security.jgss.krb5.Krb5InitCredential which extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * KerberosTicket and sun.security.jgss.krb5.Kbr5AcceptCredential which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * extends KerberosKey.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private static void populateCredentials(Set<Object> credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                            Set<?> gssCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Object cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Iterator<?> elements = gssCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        while (elements.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            cred = elements.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Retrieve the internal cred out of SpNegoCredElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (cred instanceof SpNegoCredElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                cred = ((SpNegoCredElement) cred).getInternalCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (cred instanceof KerberosTicket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (!cred.getClass().getName().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    ("javax.security.auth.kerberos.KerberosTicket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    KerberosTicket tempTkt = (KerberosTicket) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    cred = new KerberosTicket(tempTkt.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                              tempTkt.getClient(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                                              tempTkt.getServer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                              tempTkt.getSessionKey().getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                                              tempTkt.getSessionKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                                              tempTkt.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                              tempTkt.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                                              tempTkt.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                              tempTkt.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                              tempTkt.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                              tempTkt.getClientAddresses());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                credentials.add(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            } else if (cred instanceof KerberosKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (!cred.getClass().getName().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    ("javax.security.auth.kerberos.KerberosKey")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    KerberosKey tempKey = (KerberosKey) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    cred = new KerberosKey(tempKey.getPrincipal(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                           tempKey.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                           tempKey.getKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                           tempKey.getVersionNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                credentials.add(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                // Ignore non-KerberosTicket and non-KerberosKey elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                debug("Skipped cred element: " + cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Authenticate using the login module from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * configuration entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param caller the caller of JAAS Login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param mech the mech to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @return the authenticated subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   236
    public static Subject login(GSSCaller caller, Oid mech) throws LoginException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        CallbackHandler cb = null;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   239
        if (caller instanceof HttpCaller) {
4157
558590fb3b49 6893238: Move NTLM and SPNEGO implementations into separate packages
chegar
parents: 2942
diff changeset
   240
            cb = new sun.net.www.protocol.http.spnego.NegotiateCallbackHandler(
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   241
                    ((HttpCaller)caller).info());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            String defaultHandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    java.security.Security.getProperty(DEFAULT_HANDLER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            // get the default callback handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if ((defaultHandler != null) && (defaultHandler.length() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                cb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } else {
22973
616ad2891b71 8034943: Eliminate Kerberos dependency on com.sun.security.auth to avoid circular dependency
alanb
parents: 10336
diff changeset
   249
                cb = new ConsoleCallbackHandler();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // New instance of LoginConfigImpl must be created for each login,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        // since the entry name is not passed as the first argument, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        // generated with caller and mech inside LoginConfigImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        LoginContext lc = new LoginContext("", null, cb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                new LoginConfigImpl(caller, mech));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        lc.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return lc.getSubject();
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
     * Determines if the application doesn't mind if the mechanism obtains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * the required credentials from outside of the current Subject. Our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Kerberos v5 mechanism would do a JAAS login on behalf of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * application if this were the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The application indicates this by explicitly setting the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * property javax.security.auth.useSubjectCredsOnly to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   271
    public static boolean useSubjectCredsOnly(GSSCaller caller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        // HTTP/SPNEGO doesn't use the standard JAAS framework. Instead, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // uses the java.net.Authenticator style, therefore always return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // false here.
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   276
        if (caller instanceof HttpCaller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * Don't use GetBooleanAction because the default value in the JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * (when this is unset) has to treated as true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        String propValue = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                new GetPropertyAction("javax.security.auth.useSubjectCredsOnly",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                "true"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * This property has to be explicitly set to "false". Invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         * values should be ignored and the default "true" assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return (!propValue.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Determines the SPNEGO interoperability mode with Microsoft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * by default it is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * To disable it, the application indicates this by explicitly setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * the system property sun.security.spnego.interop to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static boolean useMSInterop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
         * Don't use GetBooleanAction because the default value in the JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
         * (when this is unset) has to treated as true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        String propValue = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                new GetPropertyAction("sun.security.spnego.msinterop",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                "true"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * This property has to be explicitly set to "false". Invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * values should be ignored and the default "true" assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return (!propValue.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Searches the private credentials of current Subject with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * specified criteria and returns the matching GSSCredentialSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * object out of Sun's impl of GSSCredential. Returns null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * no Subject present or a Vector which contains 0 or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * matching GSSCredentialSpi objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   322
    public static <T extends GSSCredentialSpi> Vector<T>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   323
            searchSubject(final GSSNameSpi name,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   324
                          final Oid mech,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   325
                          final boolean initiate,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   326
                          final Class<? extends T> credCls) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        debug("Search Subject for " + getMechStr(mech) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
              (initiate? " INIT" : " ACCEPT") + " cred (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
              (name == null? "<<DEF>>" : name.toString()) + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
              credCls.getName() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        try {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   333
            Vector<T> creds =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                AccessController.doPrivileged
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   335
                (new PrivilegedExceptionAction<Vector<T>>() {
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   336
                    public Vector<T> run() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                        Subject accSubj = Subject.getSubject(acc);
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   338
                        Vector<T> result = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        if (accSubj != null) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   340
                            result = new Vector<T>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                            Iterator<GSSCredentialImpl> iterator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                                accSubj.getPrivateCredentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                (GSSCredentialImpl.class).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                            while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                GSSCredentialImpl cred = iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                debug("...Found cred" + cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                    GSSCredentialSpi ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                        cred.getElement(mech, initiate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                    debug("......Found element: " + ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                    if (ce.getClass().equals(credCls) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                        (name == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                         name.equals((Object) ce.getName()))) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5506
diff changeset
   354
                                        result.add(credCls.cast(ce));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                        debug("......Discard element");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                    debug("...Discard cred (" + ge + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        } else debug("No Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return creds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            debug("Unexpected exception when searching Subject:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            if (DEBUG) pae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
}