jdk/src/java.security.sasl/share/classes/com/sun/security/sasl/CramMD5Base.java
author rriggs
Wed, 05 Apr 2017 09:57:32 -0400
changeset 44534 a076dffbc2c1
parent 32275 17eeb583a331
permissions -rw-r--r--
8165641: Deprecate Object.finalize Reviewed-by: mchung, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32275
diff changeset
     2
 * Copyright (c) 2003, 2017, 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.SaslException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.security.sasl.Sasl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
// For HMAC_MD5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
25531
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
    35
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
  * Base class for implementing CRAM-MD5 client and server mechanisms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
  *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
  * @author Vincent Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  * @author Rosanna Lee
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
abstract class CramMD5Base {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected boolean completed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected boolean aborted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected byte[] pw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    protected CramMD5Base() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        initLogger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Retrieves this mechanism's name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @return  The string "CRAM-MD5".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public String getMechanismName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return "CRAM-MD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Determines whether this mechanism has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * CRAM-MD5 completes after processing one challenge from the server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return true if has completed; false otherwise;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public boolean isComplete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return completed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
      * Unwraps the incoming buffer. CRAM-MD5 supports no security layer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      * @throws SaslException If attempt to use this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public byte[] unwrap(byte[] incoming, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "CRAM-MD5 supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                "CRAM-MD5 authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
      * Wraps the outgoing buffer. CRAM-MD5 supports no security layer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      * @throws SaslException If attempt to use this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                "CRAM-MD5 supports neither integrity nor privacy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                "CRAM-MD5 authentication not 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Retrieves the negotiated property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * This method can be called only after the authentication exchange has
32275
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 27957
diff changeset
   106
     * completed (i.e., when {@code isComplete()} returns true); otherwise, a
17eeb583a331 8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents: 27957
diff changeset
   107
     * {@code SaslException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return value of property; only QOP is applicable to CRAM-MD5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IllegalStateException if this authentication exchange has not completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public Object getNegotiatedProperty(String propName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (completed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            if (propName.equals(Sasl.QOP)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return "auth";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                "CRAM-MD5 authentication not completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void dispose() throws SaslException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected void clearPassword() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (pw != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // zero out password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            for (int i = 0; i < pw.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                pw[i] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            pw = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
44534
a076dffbc2c1 8165641: Deprecate Object.finalize
rriggs
parents: 32275
diff changeset
   139
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected void finalize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static private final int MD5_BLOCKSIZE = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Hashes its input arguments according to HMAC-MD5 (RFC 2104)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * and returns the resulting digest in its ASCII representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * HMAC-MD5 function is described as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *       MD5(key XOR opad, MD5(key XOR ipad, text))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * where key  is an n byte key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *       ipad is the byte 0x36 repeated 64 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *       opad is the byte 0x5c repeated 64 times
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *       text is the data to be protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    final static String HMAC_MD5(byte[] key, byte[] text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        MessageDigest md5 = MessageDigest.getInstance("MD5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        /* digest the key if longer than 64 bytes */
25531
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   164
        if (key.length > MD5_BLOCKSIZE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            key = md5.digest(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        byte[] ipad = new byte[MD5_BLOCKSIZE];  /* inner padding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        byte[] opad = new byte[MD5_BLOCKSIZE];  /* outer padding */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        byte[] digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        /* store key in pads */
25531
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   174
        for (i = 0; i < key.length; i++) {
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   175
            ipad[i] = key[i];
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   176
            opad[i] = key[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /* XOR key with pads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        for (i = 0; i < MD5_BLOCKSIZE; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            ipad[i] ^= 0x36;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            opad[i] ^= 0x5c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        /* inner MD5 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        md5.update(ipad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        md5.update(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        digest = md5.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        /* outer MD5 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        md5.update(opad);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        md5.update(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        digest = md5.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Get character representation of digest
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 5506
diff changeset
   196
        StringBuilder digestString = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        for (i = 0; i < digest.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if ((digest[i] & 0x000000ff) < 0x10) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   200
                digestString.append('0').append(Integer.toHexString(digest[i] & 0x000000ff));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                digestString.append(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    Integer.toHexString(digest[i] & 0x000000ff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
25531
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   207
        Arrays.fill(ipad, (byte)0);
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   208
        Arrays.fill(opad, (byte)0);
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   209
        ipad = null;
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   210
        opad = null;
84c4a59308f0 8034272: Do not cram data into CRAM arrays
vinnie
parents: 24969
diff changeset
   211
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        return (digestString.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Sets logger field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    private static synchronized void initLogger() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (logger == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            logger = Logger.getLogger(SASL_LOGGER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Logger for debug messages
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static final String SASL_LOGGER_NAME = "javax.security.sasl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected static Logger logger;  // set in initLogger(); lazily loads logger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
}