Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/security/sasl/CramMD5Client.java
38924 views
/*1* Copyright (c) 1999, 2010, 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;3233/**34* Implements the CRAM-MD5 SASL client-side mechanism.35* (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).36* CRAM-MD5 has no initial response. It receives bytes from37* the server as a challenge, which it hashes by using MD5 and the password.38* It concatenates the authentication ID with this result and returns it39* as the response to the challenge. At that point, the exchange is complete.40*41* @author Vincent Ryan42* @author Rosanna Lee43*/44final class CramMD5Client extends CramMD5Base implements SaslClient {45private String username;4647/**48* Creates a SASL mechanism with client credentials that it needs49* to participate in CRAM-MD5 authentication exchange with the server.50*51* @param authID A non-null string representing the principal52* being authenticated.53*54* @param pw A non-null String or byte[]55* containing the password. If it is an array, it is first cloned.56*/57CramMD5Client(String authID, byte[] pw) throws SaslException {58if (authID == null || pw == null) {59throw new SaslException(60"CRAM-MD5: authentication ID and password must be specified");61}6263username = authID;64this.pw = pw; // caller should have already cloned65}6667/**68* CRAM-MD5 has no initial response.69*/70public boolean hasInitialResponse() {71return false;72}7374/**75* Processes the challenge data.76*77* The server sends a challenge data using which the client must78* compute an MD5-digest with its password as the key.79*80* @param challengeData A non-null byte array containing the challenge81* data from the server.82* @return A non-null byte array containing the response to be sent to83* the server.84* @throws SaslException If platform does not have MD5 support85* @throw IllegalStateException if this method is invoked more than once.86*/87public byte[] evaluateChallenge(byte[] challengeData)88throws SaslException {8990// See if we've been here before91if (completed) {92throw new IllegalStateException(93"CRAM-MD5 authentication already completed");94}9596if (aborted) {97throw new IllegalStateException(98"CRAM-MD5 authentication previously aborted due to error");99}100101// generate a keyed-MD5 digest from the user's password and challenge.102try {103if (logger.isLoggable(Level.FINE)) {104logger.log(Level.FINE, "CRAMCLNT01:Received challenge: {0}",105new String(challengeData, "UTF8"));106}107108String digest = HMAC_MD5(pw, challengeData);109110// clear it when we no longer need it111clearPassword();112113// response is username + " " + digest114String resp = username + " " + digest;115116logger.log(Level.FINE, "CRAMCLNT02:Sending response: {0}", resp);117118completed = true;119120return resp.getBytes("UTF8");121} catch (java.security.NoSuchAlgorithmException e) {122aborted = true;123throw new SaslException("MD5 algorithm not available on platform", e);124} catch (java.io.UnsupportedEncodingException e) {125aborted = true;126throw new SaslException("UTF8 not available on platform", e);127}128}129}130131132