0N/A/*
3261N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.security.sasl;
0N/A
0N/Aimport javax.security.sasl.*;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/A
0N/Aimport java.util.logging.Logger;
0N/Aimport java.util.logging.Level;
0N/A
0N/A/**
0N/A * Implements the CRAM-MD5 SASL client-side mechanism.
2507N/A * (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).
0N/A * CRAM-MD5 has no initial response. It receives bytes from
0N/A * the server as a challenge, which it hashes by using MD5 and the password.
0N/A * It concatenates the authentication ID with this result and returns it
0N/A * as the response to the challenge. At that point, the exchange is complete.
0N/A *
0N/A * @author Vincent Ryan
0N/A * @author Rosanna Lee
0N/A */
0N/Afinal class CramMD5Client extends CramMD5Base implements SaslClient {
0N/A private String username;
0N/A
0N/A /**
0N/A * Creates a SASL mechanism with client credentials that it needs
0N/A * to participate in CRAM-MD5 authentication exchange with the server.
0N/A *
0N/A * @param authID A non-null string representing the principal
0N/A * being authenticated.
0N/A *
0N/A * @param pw A non-null String or byte[]
0N/A * containing the password. If it is an array, it is first cloned.
0N/A */
0N/A CramMD5Client(String authID, byte[] pw) throws SaslException {
0N/A if (authID == null || pw == null) {
0N/A throw new SaslException(
0N/A "CRAM-MD5: authentication ID and password must be specified");
0N/A }
0N/A
0N/A username = authID;
0N/A this.pw = pw; // caller should have already cloned
0N/A }
0N/A
0N/A /**
0N/A * CRAM-MD5 has no initial response.
0N/A */
0N/A public boolean hasInitialResponse() {
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Processes the challenge data.
0N/A *
0N/A * The server sends a challenge data using which the client must
0N/A * compute an MD5-digest with its password as the key.
0N/A *
0N/A * @param challengeData A non-null byte array containing the challenge
0N/A * data from the server.
0N/A * @return A non-null byte array containing the response to be sent to
0N/A * the server.
0N/A * @throws SaslException If platform does not have MD5 support
0N/A * @throw IllegalStateException if this method is invoked more than once.
0N/A */
0N/A public byte[] evaluateChallenge(byte[] challengeData)
0N/A throws SaslException {
0N/A
0N/A // See if we've been here before
0N/A if (completed) {
0N/A throw new IllegalStateException(
0N/A "CRAM-MD5 authentication already completed");
0N/A }
0N/A
0N/A if (aborted) {
0N/A throw new IllegalStateException(
0N/A "CRAM-MD5 authentication previously aborted due to error");
0N/A }
0N/A
0N/A // generate a keyed-MD5 digest from the user's password and challenge.
0N/A try {
0N/A if (logger.isLoggable(Level.FINE)) {
0N/A logger.log(Level.FINE, "CRAMCLNT01:Received challenge: {0}",
0N/A new String(challengeData, "UTF8"));
0N/A }
0N/A
0N/A String digest = HMAC_MD5(pw, challengeData);
0N/A
0N/A // clear it when we no longer need it
0N/A clearPassword();
0N/A
0N/A // response is username + " " + digest
0N/A String resp = username + " " + digest;
0N/A
0N/A logger.log(Level.FINE, "CRAMCLNT02:Sending response: {0}", resp);
0N/A
0N/A completed = true;
0N/A
0N/A return resp.getBytes("UTF8");
0N/A } catch (java.security.NoSuchAlgorithmException e) {
0N/A aborted = true;
0N/A throw new SaslException("MD5 algorithm not available on platform", e);
0N/A } catch (java.io.UnsupportedEncodingException e) {
0N/A aborted = true;
0N/A throw new SaslException("UTF8 not available on platform", e);
0N/A }
0N/A }
0N/A}