author | martin |
Thu, 30 Oct 2014 07:31:41 -0700 | |
changeset 28059 | e576535359cc |
parent 25859 | 3317bb8137f4 |
child 29488 | 1f25b971e59a |
permissions | -rw-r--r-- |
2 | 1 |
/* |
16113 | 2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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 |
|
16913 | 42 |
* SSL stream and block cipher message. This is essentially a shared-secret |
43 |
* signature, used to provide integrity protection for SSL messages. The |
|
44 |
* MAC is actually one of several keyed hashes, as associated with the cipher |
|
45 |
* suite and protocol version. (SSL v3.0 uses one construct, TLS uses another.) |
|
2 | 46 |
* |
47 |
* @author David Brownell |
|
48 |
* @author Andreas Sterbenz |
|
49 |
*/ |
|
16913 | 50 |
final class MAC extends Authenticator { |
2 | 51 |
|
52 |
final static MAC NULL = new MAC(); |
|
53 |
||
54 |
// Value of the null MAC is fixed |
|
55 |
private static final byte nullMAC[] = new byte[0]; |
|
56 |
||
16113 | 57 |
// internal identifier for the MAC algorithm |
58 |
private final MacAlg macAlg; |
|
59 |
||
2 | 60 |
// JCE Mac object |
61 |
private final Mac mac; |
|
62 |
||
63 |
private MAC() { |
|
16113 | 64 |
macAlg = M_NULL; |
2 | 65 |
mac = null; |
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Set up, configured for the given SSL/TLS MAC type and version. |
|
70 |
*/ |
|
71 |
MAC(MacAlg macAlg, ProtocolVersion protocolVersion, SecretKey key) |
|
72 |
throws NoSuchAlgorithmException, InvalidKeyException { |
|
16913 | 73 |
super(protocolVersion); |
16113 | 74 |
this.macAlg = macAlg; |
2 | 75 |
|
76 |
String algorithm; |
|
77 |
boolean tls = (protocolVersion.v >= ProtocolVersion.TLS10.v); |
|
78 |
||
79 |
if (macAlg == M_MD5) { |
|
80 |
algorithm = tls ? "HmacMD5" : "SslMacMD5"; |
|
81 |
} else if (macAlg == M_SHA) { |
|
82 |
algorithm = tls ? "HmacSHA1" : "SslMacSHA1"; |
|
7043 | 83 |
} else if (macAlg == M_SHA256) { |
84 |
algorithm = "HmacSHA256"; // TLS 1.2+ |
|
85 |
} else if (macAlg == M_SHA384) { |
|
86 |
algorithm = "HmacSHA384"; // TLS 1.2+ |
|
2 | 87 |
} else { |
88 |
throw new RuntimeException("Unknown Mac " + macAlg); |
|
89 |
} |
|
90 |
||
91 |
mac = JsseJce.getMac(algorithm); |
|
92 |
mac.init(key); |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* Returns the length of the MAC. |
|
97 |
*/ |
|
98 |
int MAClen() { |
|
16913 | 99 |
return macAlg.size; |
2 | 100 |
} |
101 |
||
102 |
/** |
|
16113 | 103 |
* Returns the hash function block length of the MAC alorithm. |
104 |
*/ |
|
105 |
int hashBlockLen() { |
|
106 |
return macAlg.hashBlockSize; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Returns the hash function minimal padding length of the MAC alorithm. |
|
111 |
*/ |
|
112 |
int minimalPaddingLen() { |
|
113 |
return macAlg.minimalPaddingSize; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
2 | 117 |
* Computes and returns the MAC for the data in this byte array. |
118 |
* |
|
119 |
* @param type record type |
|
120 |
* @param buf compressed record on which the MAC is computed |
|
121 |
* @param offset start of compressed record data |
|
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 | 124 |
*/ |
16113 | 125 |
final byte[] compute(byte type, byte buf[], |
126 |
int offset, int len, boolean isSimulated) { |
|
16913 | 127 |
if (macAlg.size == 0) { |
128 |
return nullMAC; |
|
129 |
} |
|
130 |
||
131 |
if (!isSimulated) { |
|
132 |
byte[] additional = acquireAuthenticationBytes(type, len); |
|
133 |
mac.update(additional); |
|
134 |
} |
|
135 |
mac.update(buf, offset, len); |
|
136 |
||
137 |
return mac.doFinal(); |
|
2 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Compute and returns the MAC for the remaining data |
|
142 |
* in this ByteBuffer. |
|
143 |
* |
|
144 |
* On return, the bb position == limit, and limit will |
|
145 |
* have not changed. |
|
146 |
* |
|
147 |
* @param type record type |
|
148 |
* @param bb a ByteBuffer in which the position and limit |
|
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 | 151 |
*/ |
16113 | 152 |
final byte[] compute(byte type, ByteBuffer bb, boolean isSimulated) { |
16913 | 153 |
if (macAlg.size == 0) { |
2 | 154 |
return nullMAC; |
155 |
} |
|
156 |
||
16113 | 157 |
if (!isSimulated) { |
16913 | 158 |
byte[] additional = |
159 |
acquireAuthenticationBytes(type, bb.remaining()); |
|
160 |
mac.update(additional); |
|
16113 | 161 |
} |
16913 | 162 |
mac.update(bb); |
2 | 163 |
|
164 |
return mac.doFinal(); |
|
165 |
} |
|
166 |
||
167 |
} |
|
16913 | 168 |