src/java.security.jgss/share/classes/sun/security/jgss/LoginConfigImpl.java
author xuelei
Mon, 25 Jun 2018 13:41:39 -0700
changeset 50768 68fa3d4026ea
parent 48579 52449da2c349
permissions -rw-r--r--
8196584: TLS 1.3 Implementation Reviewed-by: ascarpino, coffeys, dfuchs, jjiang, jnimeh, mullan, rhalade, ssahoo, valeriep, weijun, wetmore, xuelei Contributed-by: Adam Petcher <adam.petcher@oracle.com>, Amanda Jiang <amanda.jiang@oracle.com>, Anthony Scarpino <anthony.scarpino@oracle.com>, Bradford Wetmore <bradford.wetmore@oracle.com>, Jamil Nimeh <jamil.j.nimeh@oracle.com>, John Jiang <sha.jiang@oracle.com>, Rajan Halade <rajan.halade@oracle.com>, Sibabrata Sahoo <sibabrata.sahoo@oracle.com>, Valerie Peng <valerie.peng@oracle.com>, Weijun Wang <weijun.wang@oracle.com>, Xuelei Fan <xuelei.fan@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 48579
diff changeset
     2
 * Copyright (c) 2005, 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: 2942
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: 2942
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: 2942
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2942
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 java.util.HashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.auth.login.AppConfigurationEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.security.auth.login.Configuration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import org.ietf.jgss.Oid;
48579
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    32
import sun.security.action.GetPropertyAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A Configuration implementation especially designed for JGSS.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author weijun.wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class LoginConfigImpl extends Configuration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private final Configuration config;
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    43
    private final GSSCaller caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final String mechName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final sun.security.util.Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        sun.security.util.Debug.getInstance("gssloginconfig", "\t[GSS LoginConfigImpl]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
48579
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    48
    public static final boolean HTTP_USE_GLOBAL_CREDS;
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    49
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    50
    static {
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    51
        String prop = GetPropertyAction
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    52
                .privilegedGetProperty("http.use.global.creds");
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    53
        //HTTP_USE_GLOBAL_CREDS = "true".equalsIgnoreCase(prop); // default false
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    54
        HTTP_USE_GLOBAL_CREDS = !"false".equalsIgnoreCase(prop); // default true
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    55
    }
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    56
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * A new instance of LoginConfigImpl must be created for each login request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * since it's only used by a single (caller, mech) pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param caller defined in GSSUtil as CALLER_XXX final fields
32003
acb12269398a 8132130: some docs cleanup
avstepan
parents: 25859
diff changeset
    62
     * @param mech defined in GSSUtil as XXX_MECH_OID final fields
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
    64
    public LoginConfigImpl(GSSCaller caller, Oid mech) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.caller = caller;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (mech.equals(GSSUtil.GSS_KRB5_MECH_OID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            mechName = "krb5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new IllegalArgumentException(mech.toString() + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        config = java.security.AccessController.doPrivileged
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                (new java.security.PrivilegedAction <Configuration> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            public Configuration run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                return Configuration.getConfiguration();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param name Almost useless, since the (caller, mech) is already passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *             into constructor. The only use will be detecting OTHER which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *             is called in LoginContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        AppConfigurationEntry[] entries = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // This is the second call from LoginContext, which we will just ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ("OTHER".equalsIgnoreCase(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        String[] alts = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Compatibility:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // For the 4 old callers, old entry names will be used if the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // entry name is not provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if ("krb5".equals(mechName)) {
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   102
            if (caller == GSSCaller.CALLER_INITIATE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    "com.sun.security.jgss.krb5.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    "com.sun.security.jgss.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   107
            } else if (caller == GSSCaller.CALLER_ACCEPT) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    "com.sun.security.jgss.krb5.accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    "com.sun.security.jgss.accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   112
            } else if (caller instanceof HttpCaller) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    "com.sun.security.jgss.krb5.initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                };
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   116
            } else if (caller == GSSCaller.CALLER_UNKNOWN) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                throw new AssertionError("caller not defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalArgumentException(mechName + " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // No other mech at the moment, maybe --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            switch (caller) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            case GSSUtil.CALLER_INITIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            case GSSUtil.CALLER_HTTP_NEGOTIATE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    "com.sun.security.jgss." + mechName + ".initiate",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            case GSSUtil.CALLER_ACCEPT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                alts = new String[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    "com.sun.security.jgss." + mechName + ".accept",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            case GSSUtil.CALLER_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // should never use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                throw new AssertionError("caller cannot be unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                throw new AssertionError("caller not defined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (String alt: alts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            entries = config.getAppConfigurationEntry(alt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                debug.println("Trying " + alt +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        ((entries == null)?": does not exist.":": Found!"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            if (entries != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (entries == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                debug.println("Cannot read JGSS entry, use default values instead.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            entries = getDefaultConfigurationEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return entries;
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
     * Default value for a caller-mech pair when no entry is defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * the system-wide Configuration object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private AppConfigurationEntry[] getDefaultConfigurationEntry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        HashMap <String, String> options = new HashMap <String, String> (2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (mechName == null || mechName.equals("krb5")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (isServerSide(caller)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                // Assuming the keytab file can be found through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                // krb5 config file or under user home directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                options.put("useKeyTab", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                options.put("storeKey", "true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                options.put("doNotPrompt", "true");
15649
f6bd3d34f844 8001104: Unbound SASL service: the GSSAPI/krb5 mech
weijun
parents: 5506
diff changeset
   177
                options.put("principal", "*");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                options.put("isInitiator", "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else {
48579
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
   180
                if (caller instanceof HttpCaller && !HTTP_USE_GLOBAL_CREDS) {
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
   181
                    options.put("useTicketCache", "false");
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
   182
                } else {
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
   183
                    options.put("useTicketCache", "true");
52449da2c349 8186600: Improve property negotiations
weijun
parents: 47216
diff changeset
   184
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                options.put("doNotPrompt", "false");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return new AppConfigurationEntry[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                new AppConfigurationEntry(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        "com.sun.security.auth.module.Krb5LoginModule",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
2942
37d9baeb7518 6578647: Undefined requesting URL in java.net.Authenticator.getPasswordAuthentication()
weijun
parents: 2
diff changeset
   197
    private static boolean isServerSide (GSSCaller caller) {
50768
68fa3d4026ea 8196584: TLS 1.3 Implementation
xuelei
parents: 48579
diff changeset
   198
        return GSSCaller.CALLER_ACCEPT == caller;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}