jdk/src/share/classes/sun/security/ssl/MAC.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * SSL message.  This is essentially a shared-secret signature, used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * provide integrity protection for SSL messages.  The MAC is actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * one of several keyed hashes, as associated with the cipher suite and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * protocol version.  (SSL v3.0 uses one construct, TLS uses another.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <P>NOTE: MAC computation is the only place in the SSL protocol that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * sequence number is used.  It's also reset to zero with each change of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * a cipher spec, so this is the only place this state is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
final class MAC {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    final static MAC NULL = new MAC();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // Value of the null MAC is fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final byte nullMAC[] = new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // internal identifier for the MAC algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private final MacAlg        macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // stuff defined by the kind of MAC algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final int           macSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // JCE Mac object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final Mac mac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // byte array containing the additional information we MAC in each record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // (see below)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private final byte[] block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // sequence number + record type + + record length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final int BLOCK_SIZE_SSL = 8 + 1 + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // sequence number + record type + protocol version + record length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int BLOCK_SIZE_TLS = 8 + 1 + 2 + 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // offset of record type in block
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static final int BLOCK_OFFSET_TYPE    = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // offset of protocol version number in block (TLS only)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private static final int BLOCK_OFFSET_VERSION = 8 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private MAC() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        macSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        macAlg = M_NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        mac = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        block = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Set up, configured for the given SSL/TLS MAC type and version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    MAC(MacAlg macAlg, ProtocolVersion protocolVersion, SecretKey key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throws NoSuchAlgorithmException, InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.macAlg = macAlg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.macSize = macAlg.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        boolean tls = (protocolVersion.v >= ProtocolVersion.TLS10.v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (macAlg == M_MD5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            algorithm = tls ? "HmacMD5" : "SslMacMD5";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } else if (macAlg == M_SHA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            algorithm = tls ? "HmacSHA1" : "SslMacSHA1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            throw new RuntimeException("Unknown Mac " + macAlg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        mac = JsseJce.getMac(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        mac.init(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (tls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            block = new byte[BLOCK_SIZE_TLS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            block[BLOCK_OFFSET_VERSION]   = protocolVersion.major;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            block[BLOCK_OFFSET_VERSION+1] = protocolVersion.minor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            block = new byte[BLOCK_SIZE_SSL];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
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
     * Returns the length of the MAC.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int MAClen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return macSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Computes and returns the MAC for the data in this byte array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param buf compressed record on which the MAC is computed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param offset start of compressed record data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param len the size of the compressed record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    final byte[] compute(byte type, byte buf[], int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return compute(type, null, buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Compute and returns the MAC for the remaining data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * in this ByteBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * On return, the bb position == limit, and limit will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * have not changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param type record type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param bb a ByteBuffer in which the position and limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *          demarcate the data to be MAC'd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    final byte[] compute(byte type, ByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return compute(type, bb, null, 0, bb.remaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // increment the sequence number in the block array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // it is a 64-bit number stored in big-endian format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private void incrementSequenceNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        int k = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        while ((k >= 0) && (++block[k] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            k--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Compute based on either buffer type, either bb.position/limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * or buf/offset/len.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private byte[] compute(byte type, ByteBuffer bb, byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (macSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return nullMAC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        block[BLOCK_OFFSET_TYPE] = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        block[block.length - 2]  = (byte)(len >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        block[block.length - 1]  = (byte)(len     );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        mac.update(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        incrementSequenceNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // content
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (bb != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            mac.update(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            mac.update(buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return mac.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
}