2
|
1 |
/*
|
|
2 |
* Copyright 1996-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
package sun.security.ssl;
|
|
28 |
|
|
29 |
import java.security.InvalidKeyException;
|
|
30 |
import java.security.NoSuchAlgorithmException;
|
|
31 |
|
|
32 |
import java.nio.ByteBuffer;
|
|
33 |
|
|
34 |
import javax.crypto.Mac;
|
|
35 |
import javax.crypto.SecretKey;
|
|
36 |
|
|
37 |
import sun.security.ssl.CipherSuite.MacAlg;
|
|
38 |
import static sun.security.ssl.CipherSuite.*;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* This class computes the "Message Authentication Code" (MAC) for each
|
|
42 |
* SSL message. This is essentially a shared-secret signature, used to
|
|
43 |
* provide integrity protection for SSL messages. The MAC is actually
|
|
44 |
* one of several keyed hashes, as associated with the cipher suite and
|
|
45 |
* protocol version. (SSL v3.0 uses one construct, TLS uses another.)
|
|
46 |
*
|
|
47 |
* <P>NOTE: MAC computation is the only place in the SSL protocol that the
|
|
48 |
* sequence number is used. It's also reset to zero with each change of
|
|
49 |
* a cipher spec, so this is the only place this state is needed.
|
|
50 |
*
|
|
51 |
* @author David Brownell
|
|
52 |
* @author Andreas Sterbenz
|
|
53 |
*/
|
|
54 |
final class MAC {
|
|
55 |
|
|
56 |
final static MAC NULL = new MAC();
|
|
57 |
|
|
58 |
// Value of the null MAC is fixed
|
|
59 |
private static final byte nullMAC[] = new byte[0];
|
|
60 |
|
|
61 |
// internal identifier for the MAC algorithm
|
|
62 |
private final MacAlg macAlg;
|
|
63 |
|
|
64 |
// stuff defined by the kind of MAC algorithm
|
|
65 |
private final int macSize;
|
|
66 |
|
|
67 |
// JCE Mac object
|
|
68 |
private final Mac mac;
|
|
69 |
|
|
70 |
// byte array containing the additional information we MAC in each record
|
|
71 |
// (see below)
|
|
72 |
private final byte[] block;
|
|
73 |
|
|
74 |
// sequence number + record type + + record length
|
|
75 |
private static final int BLOCK_SIZE_SSL = 8 + 1 + 2;
|
|
76 |
|
|
77 |
// sequence number + record type + protocol version + record length
|
|
78 |
private static final int BLOCK_SIZE_TLS = 8 + 1 + 2 + 2;
|
|
79 |
|
|
80 |
// offset of record type in block
|
|
81 |
private static final int BLOCK_OFFSET_TYPE = 8;
|
|
82 |
|
|
83 |
// offset of protocol version number in block (TLS only)
|
|
84 |
private static final int BLOCK_OFFSET_VERSION = 8 + 1;
|
|
85 |
|
|
86 |
private MAC() {
|
|
87 |
macSize = 0;
|
|
88 |
macAlg = M_NULL;
|
|
89 |
mac = null;
|
|
90 |
block = null;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Set up, configured for the given SSL/TLS MAC type and version.
|
|
95 |
*/
|
|
96 |
MAC(MacAlg macAlg, ProtocolVersion protocolVersion, SecretKey key)
|
|
97 |
throws NoSuchAlgorithmException, InvalidKeyException {
|
|
98 |
this.macAlg = macAlg;
|
|
99 |
this.macSize = macAlg.size;
|
|
100 |
|
|
101 |
String algorithm;
|
|
102 |
boolean tls = (protocolVersion.v >= ProtocolVersion.TLS10.v);
|
|
103 |
|
|
104 |
if (macAlg == M_MD5) {
|
|
105 |
algorithm = tls ? "HmacMD5" : "SslMacMD5";
|
|
106 |
} else if (macAlg == M_SHA) {
|
|
107 |
algorithm = tls ? "HmacSHA1" : "SslMacSHA1";
|
|
108 |
} else {
|
|
109 |
throw new RuntimeException("Unknown Mac " + macAlg);
|
|
110 |
}
|
|
111 |
|
|
112 |
mac = JsseJce.getMac(algorithm);
|
|
113 |
mac.init(key);
|
|
114 |
|
|
115 |
if (tls) {
|
|
116 |
block = new byte[BLOCK_SIZE_TLS];
|
|
117 |
block[BLOCK_OFFSET_VERSION] = protocolVersion.major;
|
|
118 |
block[BLOCK_OFFSET_VERSION+1] = protocolVersion.minor;
|
|
119 |
} else {
|
|
120 |
block = new byte[BLOCK_SIZE_SSL];
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
/**
|
|
125 |
* Returns the length of the MAC.
|
|
126 |
*/
|
|
127 |
int MAClen() {
|
|
128 |
return macSize;
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Computes and returns the MAC for the data in this byte array.
|
|
133 |
*
|
|
134 |
* @param type record type
|
|
135 |
* @param buf compressed record on which the MAC is computed
|
|
136 |
* @param offset start of compressed record data
|
|
137 |
* @param len the size of the compressed record
|
|
138 |
*/
|
|
139 |
final byte[] compute(byte type, byte buf[], int offset, int len) {
|
|
140 |
return compute(type, null, buf, offset, len);
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* Compute and returns the MAC for the remaining data
|
|
145 |
* in this ByteBuffer.
|
|
146 |
*
|
|
147 |
* On return, the bb position == limit, and limit will
|
|
148 |
* have not changed.
|
|
149 |
*
|
|
150 |
* @param type record type
|
|
151 |
* @param bb a ByteBuffer in which the position and limit
|
|
152 |
* demarcate the data to be MAC'd.
|
|
153 |
*/
|
|
154 |
final byte[] compute(byte type, ByteBuffer bb) {
|
|
155 |
return compute(type, bb, null, 0, bb.remaining());
|
|
156 |
}
|
|
157 |
|
|
158 |
// increment the sequence number in the block array
|
|
159 |
// it is a 64-bit number stored in big-endian format
|
|
160 |
private void incrementSequenceNumber() {
|
|
161 |
int k = 7;
|
|
162 |
while ((k >= 0) && (++block[k] == 0)) {
|
|
163 |
k--;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
/*
|
|
168 |
* Compute based on either buffer type, either bb.position/limit
|
|
169 |
* or buf/offset/len.
|
|
170 |
*/
|
|
171 |
private byte[] compute(byte type, ByteBuffer bb, byte[] buf, int offset, int len) {
|
|
172 |
|
|
173 |
if (macSize == 0) {
|
|
174 |
return nullMAC;
|
|
175 |
}
|
|
176 |
|
|
177 |
block[BLOCK_OFFSET_TYPE] = type;
|
|
178 |
block[block.length - 2] = (byte)(len >> 8);
|
|
179 |
block[block.length - 1] = (byte)(len );
|
|
180 |
|
|
181 |
mac.update(block);
|
|
182 |
incrementSequenceNumber();
|
|
183 |
|
|
184 |
// content
|
|
185 |
if (bb != null) {
|
|
186 |
mac.update(bb);
|
|
187 |
} else {
|
|
188 |
mac.update(buf, offset, len);
|
|
189 |
}
|
|
190 |
|
|
191 |
return mac.doFinal();
|
|
192 |
}
|
|
193 |
|
|
194 |
}
|