jdk/src/share/classes/com/sun/security/sasl/CramMD5Server.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5820 4f5e99470724
child 10336 0bb1999251f8
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) 2003, 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 javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.UnsupportedEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  * Implements the CRAM-MD5 SASL server-side mechanism.
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    41
  * (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * CRAM-MD5 has no initial response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * client <---- M={random, timestamp, server-fqdn} ------- server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  * client ----- {username HMAC_MD5(pw, M)} --------------> server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * CallbackHandler must be able to handle the following callbacks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * - NameCallback: default name is name of user for whom to get password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * - PasswordCallback: must fill in password; if empty, no pw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  * - AuthorizeCallback: must setAuthorized() and canonicalized authorization id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  *      - auth id == authzid, but needed to get canonicalized authzid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
final class CramMD5Server extends CramMD5Base implements SaslServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private String fqdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private byte[] challengeData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private String authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private CallbackHandler cbh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * Creates a SASL mechanism with client credentials that it needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * to participate in CRAM-MD5 authentication exchange with the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * @param authID A  non-null string representing the principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * being authenticated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param pw A non-null String or byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * containing the password. If it is an array, it is first cloned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    CramMD5Server(String protocol, String serverFqdn, Map props,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        CallbackHandler cbh) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (serverFqdn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "CRAM-MD5: fully qualified server name must be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        fqdn = serverFqdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        this.cbh = cbh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Generates challenge based on response sent by client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * CRAM-MD5 has no initial response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * First call generates challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Second call verifies client response. If authentication fails, throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * SaslException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param responseData A non-null byte array containing the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *        data from the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @return A non-null byte array containing the challenge to be sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *        the client for the first call; null when 2nd call is successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @throws SaslException If authentication fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public byte[] evaluateResponse(byte[] responseData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // See if we've been here before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                "CRAM-MD5 authentication already completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                "CRAM-MD5 authentication previously aborted due to error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (challengeData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                if (responseData.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                        "CRAM-MD5 does not expect any initial response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                // Generate challenge {random, timestamp, fqdn}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                long rand = random.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                long timestamp = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                StringBuffer buf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                buf.append('<');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                buf.append(rand);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                buf.append('.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                buf.append(timestamp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                buf.append('@');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                buf.append(fqdn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                buf.append('>');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                String challengeStr = buf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    "CRAMSRV01:Generated challenge: {0}", challengeStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                challengeData = challengeStr.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return challengeData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                // Examine response to see if correctly encrypted challengeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if(logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        "CRAMSRV02:Received response: {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        new String(responseData, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                // Extract username from response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                int ulen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                for (int i = 0; i < responseData.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    if (responseData[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        ulen = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (ulen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        "CRAM-MD5: Invalid response; space missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                String username = new String(responseData, 0, ulen, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    "CRAMSRV03:Extracted username: {0}", username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                // Get user's password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                NameCallback ncb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    new NameCallback("CRAM-MD5 authentication ID: ", username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                PasswordCallback pcb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    new PasswordCallback("CRAM-MD5 password: ", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                cbh.handle(new Callback[]{ncb,pcb});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                char pwChars[] = pcb.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (pwChars == null || pwChars.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    // user has no password; OK to disclose to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        "CRAM-MD5: username not found: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                pcb.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                String pwStr = new String(pwChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                for (int i = 0; i < pwChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    pwChars[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                pw = pwStr.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                // Generate a keyed-MD5 digest from the user's password and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                // original challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                String digest = HMAC_MD5(pw, challengeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    "CRAMSRV04:Expecting digest: {0}", digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // clear pw when we no longer need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                // Check whether digest is as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                byte [] expectedDigest = digest.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                int digestLen = responseData.length - ulen - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (expectedDigest.length != digestLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new SaslException("Invalid response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                for (int i = ulen + 1; i < responseData.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (expectedDigest[j++] != responseData[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        throw new SaslException("Invalid response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                // All checks out, use AuthorizeCallback to canonicalize name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                AuthorizeCallback acb = new AuthorizeCallback(username, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                cbh.handle(new Callback[]{acb});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (acb.isAuthorized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    authzid = acb.getAuthorizedID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    // Not authorized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                        "CRAM-MD5: user not authorized: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    "CRAMSRV05:Authorization id: {0}", authzid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                completed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new SaslException("UTF8 not available on platform", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new SaslException("MD5 algorithm not available on platform", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            throw new SaslException("CRAM-MD5 authentication failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        } catch (SaslException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw e; // rethrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new SaslException("CRAM-MD5 authentication failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public String getAuthorizationID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                "CRAM-MD5 authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
}