56542
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle 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 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.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.security.ssl;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.security.GeneralSecurityException;
|
|
31 |
import java.security.spec.AlgorithmParameterSpec;
|
|
32 |
import javax.crypto.SecretKey;
|
|
33 |
import javax.net.ssl.SSLHandshakeException;
|
|
34 |
|
|
35 |
final class SSLBasicKeyDerivation implements SSLKeyDerivation {
|
|
36 |
private final String hashAlg;
|
|
37 |
private final SecretKey secret;
|
|
38 |
private final byte[] hkdfInfo;
|
|
39 |
|
|
40 |
SSLBasicKeyDerivation(SecretKey secret, String hashAlg,
|
|
41 |
byte[] label, byte[] context, int length) {
|
|
42 |
this.hashAlg = hashAlg.replace("-", "");
|
|
43 |
this.secret = secret;
|
|
44 |
this.hkdfInfo = createHkdfInfo(label, context, length);
|
|
45 |
}
|
|
46 |
|
|
47 |
@Override
|
|
48 |
public SecretKey deriveKey(String algorithm,
|
|
49 |
AlgorithmParameterSpec keySpec) throws IOException {
|
|
50 |
try {
|
|
51 |
HKDF hkdf = new HKDF(hashAlg);
|
|
52 |
return hkdf.expand(secret, hkdfInfo,
|
|
53 |
((SecretSizeSpec)keySpec).length, algorithm);
|
|
54 |
} catch (GeneralSecurityException gse) {
|
|
55 |
throw (SSLHandshakeException) new SSLHandshakeException(
|
|
56 |
"Could not generate secret").initCause(gse);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
|
60 |
private static byte[] createHkdfInfo(
|
|
61 |
byte[] label, byte[] context, int length) {
|
|
62 |
byte[] info = new byte[4 + label.length + context.length];
|
|
63 |
ByteBuffer m = ByteBuffer.wrap(info);
|
|
64 |
try {
|
|
65 |
Record.putInt16(m, length);
|
|
66 |
Record.putBytes8(m, label);
|
|
67 |
Record.putBytes8(m, context);
|
|
68 |
} catch (IOException ioe) {
|
|
69 |
// unlikely
|
|
70 |
}
|
|
71 |
return info;
|
|
72 |
}
|
|
73 |
|
|
74 |
static class SecretSizeSpec implements AlgorithmParameterSpec {
|
|
75 |
final int length;
|
|
76 |
|
|
77 |
SecretSizeSpec(int length) {
|
|
78 |
this.length = length;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|