src/java.security.sasl/share/classes/com/sun/security/sasl/CramMD5Server.java
author serb
Thu, 12 Sep 2019 22:20:35 -0700
changeset 58323 7b3101216e61
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8225101: Crash at sun.awt.X11.XlibWrapper.XkbGetUpdatedMap when change keybord map Reviewed-by: prr, pbansal
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 14340
diff changeset
     2
 * Copyright (c) 2003, 2012, 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.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * Implements the CRAM-MD5 SASL server-side mechanism.
5820
4f5e99470724 6967036: Need to fix links with // in Javadoc comments
ohair
parents: 5506
diff changeset
    40
  * (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * CRAM-MD5 has no initial response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  * client <---- M={random, timestamp, server-fqdn} ------- server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  * client ----- {username HMAC_MD5(pw, M)} --------------> server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  * CallbackHandler must be able to handle the following callbacks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  * - NameCallback: default name is name of user for whom to get password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  * - PasswordCallback: must fill in password; if empty, no pw
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  * - AuthorizeCallback: must setAuthorized() and canonicalized authorization id
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
  *      - auth id == authzid, but needed to get canonicalized authzid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
final class CramMD5Server extends CramMD5Base implements SaslServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private String fqdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private byte[] challengeData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private String authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private CallbackHandler cbh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    61
     * Creates a CRAM-MD5 SASL server.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
14340
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    63
     * @param protocol ignored in CRAM-MD5
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    64
     * @param serverFqdn non-null, used in generating a challenge
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    65
     * @param props ignored in CRAM-MD5
e150cbaf584e 7110803: SASL service for multiple hostnames
weijun
parents: 10336
diff changeset
    66
     * @param cbh find password, authorize user
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7668
diff changeset
    68
    CramMD5Server(String protocol, String serverFqdn, Map<String, ?> props,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        CallbackHandler cbh) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (serverFqdn == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "CRAM-MD5: fully qualified server name must be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        fqdn = serverFqdn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.cbh = cbh;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Generates challenge based on response sent by client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * CRAM-MD5 has no initial response.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * First call generates challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Second call verifies client response. If authentication fails, throws
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * SaslException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param responseData A non-null byte array containing the response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *        data from the client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return A non-null byte array containing the challenge to be sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *        the client for the first call; null when 2nd call is successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @throws SaslException If authentication fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public byte[] evaluateResponse(byte[] responseData)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // See if we've been here before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                "CRAM-MD5 authentication already completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (aborted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                "CRAM-MD5 authentication previously aborted due to error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (challengeData == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                if (responseData.length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        "CRAM-MD5 does not expect any initial response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // Generate challenge {random, timestamp, fqdn}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                Random random = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                long rand = random.nextLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                long timestamp = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   120
                StringBuilder sb = new StringBuilder();
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   121
                sb.append('<');
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   122
                sb.append(rand);
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   123
                sb.append('.');
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   124
                sb.append(timestamp);
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   125
                sb.append('@');
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   126
                sb.append(fqdn);
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   127
                sb.append('>');
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 14342
diff changeset
   128
                String challengeStr = sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    "CRAMSRV01:Generated challenge: {0}", challengeStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                challengeData = challengeStr.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                return challengeData.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                // Examine response to see if correctly encrypted challengeData
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if(logger.isLoggable(Level.FINE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        "CRAMSRV02:Received response: {0}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        new String(responseData, "UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                // Extract username from response
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                int ulen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                for (int i = 0; i < responseData.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    if (responseData[i] == ' ') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        ulen = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                if (ulen == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        "CRAM-MD5: Invalid response; space missing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                String username = new String(responseData, 0, ulen, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    "CRAMSRV03:Extracted username: {0}", username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                // Get user's password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                NameCallback ncb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    new NameCallback("CRAM-MD5 authentication ID: ", username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                PasswordCallback pcb =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    new PasswordCallback("CRAM-MD5 password: ", false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                cbh.handle(new Callback[]{ncb,pcb});
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   168
                char[] pwChars = pcb.getPassword();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (pwChars == null || pwChars.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    // user has no password; OK to disclose to server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        "CRAM-MD5: username not found: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                pcb.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                String pwStr = new String(pwChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                for (int i = 0; i < pwChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    pwChars[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                pw = pwStr.getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                // Generate a keyed-MD5 digest from the user's password and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                // original challenge.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                String digest = HMAC_MD5(pw, challengeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    "CRAMSRV04:Expecting digest: {0}", digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                // clear pw when we no longer need it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                // Check whether digest is as expected
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 25859
diff changeset
   193
                byte[] expectedDigest = digest.getBytes("UTF8");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                int digestLen = responseData.length - ulen - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                if (expectedDigest.length != digestLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    throw new SaslException("Invalid response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                int j = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                for (int i = ulen + 1; i < responseData.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    if (expectedDigest[j++] != responseData[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                        throw new SaslException("Invalid response");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                // All checks out, use AuthorizeCallback to canonicalize name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                AuthorizeCallback acb = new AuthorizeCallback(username, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                cbh.handle(new Callback[]{acb});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (acb.isAuthorized()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    authzid = acb.getAuthorizedID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    // Not authorized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    throw new SaslException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                        "CRAM-MD5: user not authorized: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                logger.log(Level.FINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    "CRAMSRV05:Authorization id: {0}", authzid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                completed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } catch (UnsupportedEncodingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new SaslException("UTF8 not available on platform", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new SaslException("MD5 algorithm not available on platform", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new SaslException("CRAM-MD5 authentication failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } catch (SaslException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw e; // rethrow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            aborted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new SaslException("CRAM-MD5 authentication failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public String getAuthorizationID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return authzid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                "CRAM-MD5 authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
}