src/java.security.sasl/share/classes/com/sun/security/sasl/PlainClient.java
author ssadetsky
Mon, 16 Oct 2017 08:06:56 -0700
changeset 47387 4d711a58bb3b
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8180821: [macosx]com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java: The menu item should have been GC-ed Reviewed-by: prr, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32275
diff changeset
     2
 * Copyright (c) 2000, 2017, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
  * Implements the PLAIN SASL client mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
  * (<A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
  * HREF="http://ftp.isi.edu/in-notes/rfc2595.txt">RFC 2595</A>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
final class PlainClient implements SaslClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private byte[] pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private String authorizationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String authenticationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static byte SEP = 0; // US-ASCII <NUL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates a SASL mechanism with client credentials that it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * to participate in Plain authentication exchange with the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * @param authorizationID A possibly null string representing the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     *  for which authorization is being granted; if null, same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     *  authenticationID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * @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
    52
     * being authenticated. pw is associated with this principal.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * @param pw A non-null byte[] containing the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    PlainClient(String authorizationID, String authenticationID, byte[] pw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (authenticationID == null || pw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                "PLAIN: authorization ID and password must be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.authorizationID = authorizationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this.authenticationID = authenticationID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        this.pw = pw;  // caller should have already cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Retrieves this mechanism's name for to initiate the PLAIN protocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @return  The string "PLAIN".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public String getMechanismName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return "PLAIN";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public boolean hasInitialResponse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public void dispose() throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Retrieves the initial response for the SASL command, which for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * PLAIN is the concatenation of authorization ID, authentication ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * and password, with each component separated by the US-ASCII <NUL> byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param challengeData Ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return A non-null byte array containing the response to be sent to the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @throws SaslException If cannot encode ids in UTF-8
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throw IllegalStateException if authentication already completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public byte[] evaluateChallenge(byte[] challengeData) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                "PLAIN authentication already completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        completed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            byte[] authz = (authorizationID != null)?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                authorizationID.getBytes("UTF8") :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            byte[] auth = authenticationID.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            byte[] answer = new byte[pw.length + auth.length + 2 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                (authz == null ? 0 : authz.length)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (authz != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                System.arraycopy(authz, 0, answer, 0, authz.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                pos = authz.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            answer[pos++] = SEP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            System.arraycopy(auth, 0, answer, pos, auth.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            pos += auth.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            answer[pos++] = SEP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            System.arraycopy(pw, 0, answer, pos, pw.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            return answer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new SaslException("Cannot get UTF-8 encoding of ids", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Determines whether this mechanism has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Plain completes after returning one response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return true if has completed; false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public boolean isComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
      * Unwraps the incoming buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
      * @throws SaslException Not applicable to this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public byte[] unwrap(byte[] incoming, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                "PLAIN supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
      * Wraps the outgoing buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
      * @throws SaslException Not applicable to this mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                "PLAIN supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Retrieves the negotiated property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * 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
   173
     * 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
   174
     * {@code SaslException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return value of property; only QOP is applicable to PLAIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @exception IllegalStateException if this authentication exchange
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *     has not completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public Object getNegotiatedProperty(String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (propName.equals(Sasl.QOP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                return "auth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            throw new IllegalStateException("PLAIN authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private void clearPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // zero out password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            for (int i = 0; i < pw.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                pw[i] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            pw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32275
diff changeset
   202
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}