src/java.security.sasl/share/classes/com/sun/security/sasl/PlainClient.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 59024 b046ba510bbc
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
    30
import static java.nio.charset.StandardCharsets.UTF_8;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  * Implements the PLAIN SASL client mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  * (<A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * HREF="http://ftp.isi.edu/in-notes/rfc2595.txt">RFC 2595</A>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
final class PlainClient implements SaslClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private byte[] pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private String authorizationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String authenticationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static byte SEP = 0; // US-ASCII <NUL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * Creates a SASL mechanism with client credentials that it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * to participate in Plain authentication exchange with the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * @param authorizationID A possibly null string representing the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *  for which authorization is being granted; if null, same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *  authenticationID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param authenticationID A non-null string representing the principal
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    54
     * being authenticated. pw is associated with this principal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param pw A non-null byte[] containing the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    PlainClient(String authorizationID, String authenticationID, byte[] pw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (authenticationID == null || pw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                "PLAIN: authorization ID and password must be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.authorizationID = authorizationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.authenticationID = authenticationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.pw = pw;  // caller should have already cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Retrieves this mechanism's name for to initiate the PLAIN protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return  The string "PLAIN".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public String getMechanismName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        return "PLAIN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public boolean hasInitialResponse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void dispose() throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Retrieves the initial response for the SASL command, which for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * PLAIN is the concatenation of authorization ID, authentication ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * and password, with each component separated by the US-ASCII <NUL> byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param challengeData Ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return A non-null byte array containing the response to be sent to the server.
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
    94
     * @throws IllegalStateException if authentication already completed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
    96
    public byte[] evaluateChallenge(byte[] challengeData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                "PLAIN authentication already completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        completed = true;
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   102
        byte[] authz = (authorizationID != null)
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   103
            ? authorizationID.getBytes(UTF_8)
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   104
            : null;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   105
        byte[] auth = authenticationID.getBytes(UTF_8);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   107
        byte[] answer = new byte[pw.length + auth.length + 2 +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                (authz == null ? 0 : authz.length)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   110
        int pos = 0;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   111
        if (authz != null) {
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   112
            System.arraycopy(authz, 0, answer, 0, authz.length);
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   113
            pos = authz.length;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   114
        }
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   115
        answer[pos++] = SEP;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   116
        System.arraycopy(auth, 0, answer, pos, auth.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   118
        pos += auth.length;
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   119
        answer[pos++] = SEP;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
59024
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   121
        System.arraycopy(pw, 0, answer, pos, pw.length);
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   122
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   123
        clearPassword();
b046ba510bbc 8233884: Avoid looking up standard charsets in security libraries
igerasim
parents: 47216
diff changeset
   124
        return answer;
2
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
     * Determines whether this mechanism has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Plain completes after returning one response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return true if has completed; false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public boolean isComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
      * Unwraps the incoming buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
      * @throws SaslException Not applicable to this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public byte[] unwrap(byte[] incoming, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                "PLAIN supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
      * Wraps the outgoing buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
      * @throws SaslException Not applicable to this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                "PLAIN supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Retrieves the negotiated property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * This method can be called only after the authentication exchange has
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28059
diff changeset
   169
     * completed (i.e., when {@code isComplete()} returns true); otherwise, a
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 28059
diff changeset
   170
     * {@code SaslException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return value of property; only QOP is applicable to PLAIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @exception IllegalStateException if this authentication exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *     has not completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public Object getNegotiatedProperty(String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (propName.equals(Sasl.QOP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return "auth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private void clearPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            // zero out password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            for (int i = 0; i < pw.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                pw[i] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            pw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32275
diff changeset
   198
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
}