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