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