jdk/src/share/classes/com/sun/security/sasl/CramMD5Client.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5820 4f5e99470724
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5820
diff changeset
     2
 * Copyright (c) 1999, 2010, 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 java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  * Implements the CRAM-MD5 SASL client-side mechanism.
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    36
  * (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  * CRAM-MD5 has no initial response. It receives bytes from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  * the server as a challenge, which it hashes by using MD5 and the password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * It concatenates the authentication ID with this result and returns it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * as the response to the challenge. At that point, the exchange is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
final class CramMD5Client extends CramMD5Base implements SaslClient {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Creates a SASL mechanism with client credentials that it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * to participate in CRAM-MD5 authentication exchange with the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param authID A  non-null string representing the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * being authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param pw A non-null String or byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * containing the password. If it is an array, it is first cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    CramMD5Client(String authID, byte[] pw) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (authID == null || pw == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                "CRAM-MD5: authentication 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
        username = authID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.pw = pw;  // caller should have already cloned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * CRAM-MD5 has no initial response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public boolean hasInitialResponse() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Processes the challenge data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * The server sends a challenge data using which the client must
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * compute an MD5-digest with its password as the key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param challengeData A non-null byte array containing the challenge
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *        data from the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return A non-null byte array containing the response to be sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *        the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws SaslException If platform does not have MD5 support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @throw IllegalStateException if this method is invoked more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public byte[] evaluateChallenge(byte[] challengeData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // See if we've been here before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                "CRAM-MD5 authentication already completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                "CRAM-MD5 authentication previously aborted due to error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // generate a keyed-MD5 digest from the user's password and challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                logger.log(Level.FINE, "CRAMCLNT01:Received challenge: {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    new String(challengeData, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            String digest = HMAC_MD5(pw, challengeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // clear it when we no longer need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            // response is username + " " + digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            String resp = username + " " + digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            logger.log(Level.FINE, "CRAMCLNT02:Sending response: {0}", resp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            completed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return resp.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } catch (java.security.NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throw new SaslException("MD5 algorithm not available on platform", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (java.io.UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throw new SaslException("UTF8 not available on platform", 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
}