jdk/src/java.base/share/classes/sun/security/ssl/MAC.java
author martin
Thu, 30 Oct 2014 07:31:41 -0700
changeset 28059 e576535359cc
parent 25859 3317bb8137f4
child 29488 1f25b971e59a
permissions -rw-r--r--
8067377: My hobby: caning, then then canning, the the can-can Summary: Fix ALL the stutters! Reviewed-by: rriggs, mchung, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
     2
 * Copyright (c) 1996, 2013, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.security.ssl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.crypto.Mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.ssl.CipherSuite.MacAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import static sun.security.ssl.CipherSuite.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class computes the "Message Authentication Code" (MAC) for each
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    42
 * SSL stream and block cipher message.  This is essentially a shared-secret
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    43
 * signature, used to provide integrity protection for SSL messages.  The
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    44
 * MAC is actually one of several keyed hashes, as associated with the cipher
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    45
 * suite and protocol version. (SSL v3.0 uses one construct, TLS uses another.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    50
final class MAC extends Authenticator {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    final static MAC NULL = new MAC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // Value of the null MAC is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static final byte nullMAC[] = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
    57
    // internal identifier for the MAC algorithm
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
    58
    private final MacAlg        macAlg;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
    59
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // JCE Mac object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final Mac mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private MAC() {
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
    64
        macAlg = M_NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        mac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Set up, configured for the given SSL/TLS MAC type and version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    MAC(MacAlg macAlg, ProtocolVersion protocolVersion, SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throws NoSuchAlgorithmException, InvalidKeyException {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    73
        super(protocolVersion);
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
    74
        this.macAlg = macAlg;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean tls = (protocolVersion.v >= ProtocolVersion.TLS10.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (macAlg == M_MD5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            algorithm = tls ? "HmacMD5" : "SslMacMD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        } else if (macAlg == M_SHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            algorithm = tls ? "HmacSHA1" : "SslMacSHA1";
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    83
        } else if (macAlg == M_SHA256) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    84
            algorithm = "HmacSHA256";    // TLS 1.2+
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    85
        } else if (macAlg == M_SHA384) {
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 7039
diff changeset
    86
            algorithm = "HmacSHA384";    // TLS 1.2+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("Unknown Mac " + macAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        mac = JsseJce.getMac(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        mac.init(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Returns the length of the MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    int MAClen() {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
    99
        return macAlg.size;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   103
     * Returns the hash function block length of the MAC alorithm.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   104
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   105
    int hashBlockLen() {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   106
        return macAlg.hashBlockSize;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   107
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   108
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   109
    /**
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   110
     * Returns the hash function minimal padding length of the MAC alorithm.
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   111
     */
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   112
    int minimalPaddingLen() {
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   113
        return macAlg.minimalPaddingSize;
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   114
    }
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   115
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   116
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Computes and returns the MAC for the data in this byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param buf compressed record on which the MAC is computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param offset start of compressed record data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param len the size of the compressed record
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   123
     * @param isSimulated if true, simulate the MAC computation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   125
    final byte[] compute(byte type, byte buf[],
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   126
            int offset, int len, boolean isSimulated) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   127
        if (macAlg.size == 0) {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   128
            return nullMAC;
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   129
        }
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   130
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   131
        if (!isSimulated) {
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   132
            byte[] additional = acquireAuthenticationBytes(type, len);
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   133
            mac.update(additional);
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   134
        }
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   135
        mac.update(buf, offset, len);
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   136
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   137
        return mac.doFinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Compute and returns the MAC for the remaining data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * in this ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * On return, the bb position == limit, and limit will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * have not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param bb a ByteBuffer in which the position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          demarcate the data to be MAC'd.
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   150
     * @param isSimulated if true, simulate the MAC computation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   152
    final byte[] compute(byte type, ByteBuffer bb, boolean isSimulated) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   153
        if (macAlg.size == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return nullMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   157
        if (!isSimulated) {
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   158
            byte[] additional =
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   159
                    acquireAuthenticationBytes(type, bb.remaining());
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   160
            mac.update(additional);
16113
946ec9b22004 8006777: Improve TLS handling of invalid messages
xuelei
parents: 14675
diff changeset
   161
        }
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   162
        mac.update(bb);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return mac.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
}
16913
a6f4d1626ad9 8011680: Re-integrate AEAD implementation of JSSE
xuelei
parents: 16126
diff changeset
   168