src/java.security.sasl/share/classes/com/sun/security/sasl/ClientFactoryImpl.java
author henryjen
Mon, 14 Oct 2019 21:01:25 +0000
changeset 58638 7be56b2ac50d
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1999, 2006, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 com.sun.security.sasl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.security.sasl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.security.sasl.util.PolicyUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.security.auth.callback.Callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.security.auth.callback.NameCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.security.auth.callback.PasswordCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.security.auth.callback.UnsupportedCallbackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * Client factory for EXTERNAL, CRAM-MD5, PLAIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * Requires the following callbacks to be satisfied by callback handler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * when using CRAM-MD5 or PLAIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * - NameCallback (to get username)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * - PasswordCallback (to get password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
final public class ClientFactoryImpl implements SaslClientFactory {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    50
    private static final String[] myMechs = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        "EXTERNAL",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        "CRAM-MD5",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        "PLAIN",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
    56
    private static final int[] mechPolicies = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        // %%% RL: Policies should actually depend on the external channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        PolicyUtils.NOPLAINTEXT|PolicyUtils.NOACTIVE|PolicyUtils.NODICTIONARY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        PolicyUtils.NOPLAINTEXT|PolicyUtils.NOANONYMOUS,    // CRAM-MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        PolicyUtils.NOANONYMOUS,                            // PLAIN
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int EXTERNAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int CRAMMD5 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final int PLAIN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public ClientFactoryImpl() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public SaslClient createSaslClient(String[] mechs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        String authorizationId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        String protocol,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        String serverName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Map<String,?> props,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        CallbackHandler cbh) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            for (int i = 0; i < mechs.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (mechs[i].equals(myMechs[EXTERNAL])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    && PolicyUtils.checkPolicy(mechPolicies[EXTERNAL], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    return new ExternalClient(authorizationId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                } else if (mechs[i].equals(myMechs[CRAMMD5])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    && PolicyUtils.checkPolicy(mechPolicies[CRAMMD5], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    Object[] uinfo = getUserInfo("CRAM-MD5", authorizationId, cbh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    // Callee responsible for clearing bytepw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    return new CramMD5Client((String) uinfo[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        (byte []) uinfo[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                } else if (mechs[i].equals(myMechs[PLAIN])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    && PolicyUtils.checkPolicy(mechPolicies[PLAIN], props)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    Object[] uinfo = getUserInfo("PLAIN", authorizationId, cbh);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    // Callee responsible for clearing bytepw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    return new PlainClient(authorizationId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        (String) uinfo[0], (byte []) uinfo[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return null;
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 String[] getMechanismNames(Map<String,?> props) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return PolicyUtils.filterMechs(myMechs, mechPolicies, props);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Gets the authentication id and password. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * password is converted to bytes using UTF-8 and stored in bytepw.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The authentication id is stored in authId.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param prefix The non-null prefix to use for the prompt (e.g., mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *  name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @param authorizationId The possibly null authorization id. This is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * as a default for the NameCallback. If null, it is not used in prompt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param cbh The non-null callback handler to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @return an {authid, passwd} pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private Object[] getUserInfo(String prefix, String authorizationId,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        CallbackHandler cbh) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (cbh == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                "Callback handler to get username/password required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            String userPrompt = prefix + " authentication id: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            String passwdPrompt = prefix + " password: ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            NameCallback ncb = authorizationId == null?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                new NameCallback(userPrompt) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                new NameCallback(userPrompt, authorizationId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            PasswordCallback pcb = new PasswordCallback(passwdPrompt, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            cbh.handle(new Callback[]{ncb,pcb});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            char[] pw = pcb.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            byte[] bytepw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            String authId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                bytepw = new String(pw).getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                pcb.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                bytepw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            authId = ncb.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return new Object[]{authId, bytepw};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            throw new SaslException("Cannot get password", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new SaslException("Cannot get userid/password", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}