jdk/src/share/classes/sun/security/jgss/spnego/SpNegoMechFactory.java
author weijun
Fri, 27 Nov 2009 08:51:42 +0800
changeset 4337 2a6d13ebbbed
parent 2942 37d9baeb7518
child 4338 f36521ae16db
permissions -rw-r--r--
6901085: SPNEGO does not works with native program Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
     2
 * Copyright 2005-2009 Sun Microsystems, Inc.  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
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.spnego;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import org.ietf.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.security.jgss.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.security.jgss.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.security.jgss.krb5.Krb5MechFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.security.jgss.krb5.Krb5InitCredential;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.security.jgss.krb5.Krb5AcceptCredential;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jgss.krb5.Krb5NameElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * SpNego Mechanism plug in for JGSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * This is the properties object required by the JGSS framework.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * All mechanism specific information is defined here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public final class SpNegoMechFactory implements MechanismFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static final Provider PROVIDER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        new sun.security.jgss.SunProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static final Oid GSS_SPNEGO_MECH_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        GSSUtil.createOid("1.3.6.1.5.5.2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static Oid[] nameTypes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        new Oid[] { GSSName.NT_USER_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                        GSSName.NT_HOSTBASED_SERVICE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                        GSSName.NT_EXPORT_NAME};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // Use an instance of a GSSManager whose provider list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // does not include native provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    final GSSManagerImpl manager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    final Oid[] availableMechs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static SpNegoCredElement getCredFromSubject(GSSNameSpi name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                                                        boolean initiate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Vector<SpNegoCredElement> creds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            GSSUtil.searchSubject(name, GSS_SPNEGO_MECH_OID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                initiate, SpNegoCredElement.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        SpNegoCredElement result = ((creds == null || creds.isEmpty()) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                    null : creds.firstElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        // Force permission check before returning the cred to caller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            GSSCredentialSpi cred = result.getInternalCred();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            if (GSSUtil.isKerberosMech(cred.getMechanism())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (initiate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    Krb5InitCredential krbCred = (Krb5InitCredential) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    Krb5MechFactory.checkInitCredPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        ((Krb5NameElement) krbCred.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    Krb5AcceptCredential krbCred = (Krb5AcceptCredential) cred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    Krb5MechFactory.checkAcceptCredPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        ((Krb5NameElement) krbCred.getName(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    93
    public SpNegoMechFactory(GSSCaller caller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        manager = new GSSManagerImpl(caller, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Oid[] mechs = manager.getMechs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        availableMechs = new Oid[mechs.length-1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        for (int i = 0, j = 0; i < mechs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            // Skip SpNego mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (!mechs[i].equals(GSS_SPNEGO_MECH_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                availableMechs[j++] = mechs[i];
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public GSSNameSpi getNameElement(String nameStr, Oid nameType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // get NameElement for the default Mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return manager.getNameElement(nameStr, nameType, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public GSSNameSpi getNameElement(byte[] name, Oid nameType)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        // get NameElement for the default Mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return manager.getNameElement(name, nameType, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public GSSCredentialSpi getCredentialElement(GSSNameSpi name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
           int initLifetime, int acceptLifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
           int usage) throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        SpNegoCredElement credElement = getCredFromSubject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            (name, (usage != GSSCredential.ACCEPT_ONLY));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (credElement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            // get CredElement for the default Mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            credElement = new SpNegoCredElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                (manager.getCredentialElement(name, initLifetime,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                acceptLifetime, null, usage));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return credElement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public GSSContextSpi getMechanismContext(GSSNameSpi peer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                             GSSCredentialSpi myInitiatorCred, int lifetime)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        // get SpNego mechanism context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (myInitiatorCred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            myInitiatorCred = getCredFromSubject(null, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else if (!(myInitiatorCred instanceof SpNegoCredElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            // convert to SpNegoCredElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            SpNegoCredElement cred = new SpNegoCredElement(myInitiatorCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return new SpNegoContext(this, peer, cred, lifetime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return new SpNegoContext(this, peer, myInitiatorCred, lifetime);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public GSSContextSpi getMechanismContext(GSSCredentialSpi myAcceptorCred)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // get SpNego mechanism context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (myAcceptorCred == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            myAcceptorCred = getCredFromSubject(null, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else if (!(myAcceptorCred instanceof SpNegoCredElement)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // convert to SpNegoCredElement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            SpNegoCredElement cred = new SpNegoCredElement(myAcceptorCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return new SpNegoContext(this, cred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return new SpNegoContext(this, myAcceptorCred);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public GSSContextSpi getMechanismContext(byte[] exportedContext)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        throws GSSException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // get SpNego mechanism context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        return new SpNegoContext(this, exportedContext);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public final Oid getMechanismOid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return GSS_SPNEGO_MECH_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return PROVIDER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public Oid[] getNameTypes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // nameTypes is cloned in GSSManager.getNamesForMech
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return nameTypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}