jdk/src/share/classes/sun/security/jgss/GSSUtil.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2942 37d9baeb7518
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 com.sun.security.auth.callback.TextCallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.kerberos.KerberosPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.security.auth.kerberos.KerberosTicket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.security.auth.kerberos.KerberosKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.spi.GSSNameSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jgss.spi.GSSCredentialSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.jgss.krb5.Krb5NameElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.jgss.spnego.SpNegoCredElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.security.auth.login.LoginContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.security.action.GetBooleanAction;
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
    public static final Oid NT_HOSTBASED_SERVICE2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                GSSUtil.createOid("1.2.840.113554.1.2.1.4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static final String DEFAULT_HANDLER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            "auth.login.defaultCallbackHandler";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final int CALLER_UNKNOWN         = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int CALLER_INITIATE        = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static final int CALLER_ACCEPT          = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public static final int CALLER_SSL_CLIENT      = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int CALLER_SSL_SERVER      = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public static final int CALLER_HTTP_NEGOTIATE  = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final boolean DEBUG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        DEBUG = (AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        (new GetBooleanAction("sun.security.jgss.debug"))).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    static void debug(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (DEBUG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            assert(message != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    // NOTE: this method is only for creating Oid objects with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // known to be valid <code>oidStr</code> given it ignores
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // the GSSException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static Oid createOid(String oidStr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return new Oid(oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        } catch (GSSException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            debug("Ignored invalid OID: " + oidStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return null;
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 boolean isSpNegoMech(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return (GSS_SPNEGO_MECH_OID.equals(oid));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static boolean isKerberosMech(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return (GSS_KRB5_MECH_OID.equals(oid) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                GSS_KRB5_MECH_OID2.equals(oid));
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
    public static String getMechStr(Oid oid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (isSpNegoMech(oid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return "SPNEGO";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        } else if (isKerberosMech(oid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return "Kerberos V5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return oid.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Note: The current impl only works with Sun's impl of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * GSSName and GSSCredential since it depends on package
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * private APIs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public static Subject getSubject(GSSName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                                     GSSCredential creds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        HashSet<Object> privCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        HashSet<Object> pubCredentials = new HashSet<Object>(); // empty Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        Set<GSSCredentialSpi> gssCredentials = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Set<KerberosPrincipal> krb5Principals =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                new HashSet<KerberosPrincipal>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (name instanceof GSSNameImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                GSSNameSpi ne = ((GSSNameImpl) name).getElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    (GSS_KRB5_MECH_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                String krbName = ne.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                if (ne instanceof Krb5NameElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    krbName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ((Krb5NameElement) ne).getKrb5PrincipalName().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                KerberosPrincipal krbPrinc = new KerberosPrincipal(krbName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                krb5Principals.add(krbPrinc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                debug("Skipped name " + name + " due to " + ge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (creds instanceof GSSCredentialImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            gssCredentials = ((GSSCredentialImpl) creds).getElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            privCredentials = new HashSet<Object>(gssCredentials.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            populateCredentials(privCredentials, gssCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            privCredentials = new HashSet<Object>(); // empty Set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        debug("Created Subject with the following");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        debug("principals=" + krb5Principals);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        debug("public creds=" + pubCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        debug("private creds=" + privCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return new Subject(false, krb5Principals, pubCredentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                           privCredentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Populates the set credentials with elements from gssCredentials. At
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * the same time, it converts any subclasses of KerberosTicket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * into KerberosTicket instances and any subclasses of KerberosKey into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * KerberosKey instances. (It is not desirable to expose the customer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * to sun.security.jgss.krb5.Krb5InitCredential which extends
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * KerberosTicket and sun.security.jgss.krb5.Kbr5AcceptCredential which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * extends KerberosKey.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static void populateCredentials(Set<Object> credentials,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                            Set<?> gssCredentials) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        Object cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Iterator<?> elements = gssCredentials.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        while (elements.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            cred = elements.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // Retrieve the internal cred out of SpNegoCredElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (cred instanceof SpNegoCredElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                cred = ((SpNegoCredElement) cred).getInternalCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (cred instanceof KerberosTicket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                if (!cred.getClass().getName().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    ("javax.security.auth.kerberos.KerberosTicket")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    KerberosTicket tempTkt = (KerberosTicket) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                    cred = new KerberosTicket(tempTkt.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                                              tempTkt.getClient(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                              tempTkt.getServer(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                              tempTkt.getSessionKey().getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                              tempTkt.getSessionKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                              tempTkt.getFlags(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                              tempTkt.getAuthTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                              tempTkt.getStartTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                              tempTkt.getEndTime(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                              tempTkt.getRenewTill(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                              tempTkt.getClientAddresses());
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 if (cred instanceof KerberosKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (!cred.getClass().getName().equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    ("javax.security.auth.kerberos.KerberosKey")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    KerberosKey tempKey = (KerberosKey) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    cred = new KerberosKey(tempKey.getPrincipal(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                                           tempKey.getEncoded(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                           tempKey.getKeyType(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                           tempKey.getVersionNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                credentials.add(cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                // Ignore non-KerberosTicket and non-KerberosKey elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                debug("Skipped cred element: " + cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Authenticate using the login module from the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * configuration entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param caller the caller of JAAS Login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param mech the mech to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @return the authenticated subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public static Subject login(int caller, Oid mech) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        CallbackHandler cb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (caller == GSSUtil.CALLER_HTTP_NEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            cb = new sun.net.www.protocol.http.NegotiateCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            String defaultHandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    java.security.Security.getProperty(DEFAULT_HANDLER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // get the default callback handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if ((defaultHandler != null) && (defaultHandler.length() != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                cb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                cb = new TextCallbackHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // New instance of LoginConfigImpl must be created for each login,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        // since the entry name is not passed as the first argument, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        // generated with caller and mech inside LoginConfigImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        LoginContext lc = new LoginContext("", null, cb,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                new LoginConfigImpl(caller, mech));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        lc.login();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        return lc.getSubject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Determines if the application doesn't mind if the mechanism obtains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * the required credentials from outside of the current Subject. Our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Kerberos v5 mechanism would do a JAAS login on behalf of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * application if this were the case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * The application indicates this by explicitly setting the system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * property javax.security.auth.useSubjectCredsOnly to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public static boolean useSubjectCredsOnly(int caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        // HTTP/SPNEGO doesn't use the standard JAAS framework. Instead, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        // uses the java.net.Authenticator style, therefore always return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        // false here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (caller == CALLER_HTTP_NEGOTIATE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * Don't use GetBooleanAction because the default value in the JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * (when this is unset) has to treated as true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        String propValue = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new GetPropertyAction("javax.security.auth.useSubjectCredsOnly",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                "true"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * This property has to be explicitly set to "false". Invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * values should be ignored and the default "true" assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return (!propValue.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Determines the SPNEGO interoperability mode with Microsoft;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * by default it is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * To disable it, the application indicates this by explicitly setting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * the system property sun.security.spnego.interop to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public static boolean useMSInterop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * Don't use GetBooleanAction because the default value in the JRE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * (when this is unset) has to treated as true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        String propValue = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                new GetPropertyAction("sun.security.spnego.msinterop",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                "true"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * This property has to be explicitly set to "false". Invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * values should be ignored and the default "true" assumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        return (!propValue.equalsIgnoreCase("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Searches the private credentials of current Subject with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * specified criteria and returns the matching GSSCredentialSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * object out of Sun's impl of GSSCredential. Returns null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * no Subject present or a Vector which contains 0 or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * matching GSSCredentialSpi objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public static Vector searchSubject(final GSSNameSpi name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                       final Oid mech,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                       final boolean initiate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                       final Class credCls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        debug("Search Subject for " + getMechStr(mech) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
              (initiate? " INIT" : " ACCEPT") + " cred (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
              (name == null? "<<DEF>>" : name.toString()) + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
              credCls.getName() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        final AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            Vector creds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                (new PrivilegedExceptionAction<Vector>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                    public Vector run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                        Subject accSubj = Subject.getSubject(acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                        Vector<GSSCredentialSpi> result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                        if (accSubj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                            result = new Vector<GSSCredentialSpi>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                            Iterator<GSSCredentialImpl> iterator =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                accSubj.getPrivateCredentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                (GSSCredentialImpl.class).iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                            while (iterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                GSSCredentialImpl cred = iterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                debug("...Found cred" + cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                                    GSSCredentialSpi ce =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                                        cred.getElement(mech, initiate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                                    debug("......Found element: " + ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                    if (ce.getClass().equals(credCls) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                        (name == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                         name.equals((Object) ce.getName()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                        result.add(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                                        debug("......Discard element");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                                } catch (GSSException ge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                    debug("...Discard cred (" + ge + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        } else debug("No Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return creds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            debug("Unexpected exception when searching Subject:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (DEBUG) pae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}