Path: blob/master/src/java.security.sasl/share/classes/com/sun/security/sasl/CramMD5Client.java
41161 views
/*1* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.security.sasl;2627import javax.security.sasl.*;28import java.security.NoSuchAlgorithmException;2930import java.util.logging.Logger;31import java.util.logging.Level;3233import static java.nio.charset.StandardCharsets.UTF_8;3435/**36* Implements the CRAM-MD5 SASL client-side mechanism.37* (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).38* CRAM-MD5 has no initial response. It receives bytes from39* the server as a challenge, which it hashes by using MD5 and the password.40* It concatenates the authentication ID with this result and returns it41* as the response to the challenge. At that point, the exchange is complete.42*43* @author Vincent Ryan44* @author Rosanna Lee45*/46final class CramMD5Client extends CramMD5Base implements SaslClient {47private String username;4849/**50* Creates a SASL mechanism with client credentials that it needs51* to participate in CRAM-MD5 authentication exchange with the server.52*53* @param authID A non-null string representing the principal54* being authenticated.55*56* @param pw A non-null String or byte[]57* containing the password. If it is an array, it is first cloned.58*/59CramMD5Client(String authID, byte[] pw) throws SaslException {60if (authID == null || pw == null) {61throw new SaslException(62"CRAM-MD5: authentication ID and password must be specified");63}6465username = authID;66this.pw = pw; // caller should have already cloned67}6869/**70* CRAM-MD5 has no initial response.71*/72public boolean hasInitialResponse() {73return false;74}7576/**77* Processes the challenge data.78*79* The server sends a challenge data using which the client must80* compute an MD5-digest with its password as the key.81*82* @param challengeData A non-null byte array containing the challenge83* data from the server.84* @return A non-null byte array containing the response to be sent to85* the server.86* @throws SaslException if platform does not have MD5 support87* @throws IllegalStateException if this method is invoked more than once.88*/89public byte[] evaluateChallenge(byte[] challengeData)90throws SaslException {9192// See if we've been here before93if (completed) {94throw new IllegalStateException(95"CRAM-MD5 authentication already completed");96}9798if (aborted) {99throw new IllegalStateException(100"CRAM-MD5 authentication previously aborted due to error");101}102103// generate a keyed-MD5 digest from the user's password and challenge.104try {105if (logger.isLoggable(Level.FINE)) {106logger.log(Level.FINE, "CRAMCLNT01:Received challenge: {0}",107new String(challengeData, UTF_8));108}109110String digest = HMAC_MD5(pw, challengeData);111112// clear it when we no longer need it113clearPassword();114115// response is username + " " + digest116String resp = username + " " + digest;117118logger.log(Level.FINE, "CRAMCLNT02:Sending response: {0}", resp);119120completed = true;121122return resp.getBytes(UTF_8);123} catch (java.security.NoSuchAlgorithmException e) {124aborted = true;125throw new SaslException("MD5 algorithm not available on platform", e);126}127}128}129130131