src/java.base/share/classes/sun/security/ssl/SSLBasicKeyDerivation.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     1
/*
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     4
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    10
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    15
 * accompanied this code).
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    16
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    20
 *
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    23
 * questions.
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    24
 */
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    25
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    26
package sun.security.ssl;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    27
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    28
import java.io.IOException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    29
import java.nio.ByteBuffer;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    30
import java.security.GeneralSecurityException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    31
import java.security.spec.AlgorithmParameterSpec;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    32
import javax.crypto.SecretKey;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    33
import javax.net.ssl.SSLHandshakeException;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    34
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    35
final class SSLBasicKeyDerivation implements SSLKeyDerivation {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    36
    private final String hashAlg;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    37
    private final SecretKey secret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    38
    private final byte[] hkdfInfo;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    39
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    40
    SSLBasicKeyDerivation(SecretKey secret, String hashAlg,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    41
            byte[] label, byte[] context, int length) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    42
        this.hashAlg = hashAlg.replace("-", "");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    43
        this.secret = secret;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    44
        this.hkdfInfo = createHkdfInfo(label, context, length);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    45
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    46
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    47
    @Override
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    48
    public SecretKey deriveKey(String algorithm,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    49
            AlgorithmParameterSpec keySpec) throws IOException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    50
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    51
            HKDF hkdf = new HKDF(hashAlg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    52
            return hkdf.expand(secret, hkdfInfo,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    53
                    ((SecretSizeSpec)keySpec).length, algorithm);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    54
        } catch (GeneralSecurityException gse) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    55
            throw (SSLHandshakeException) new SSLHandshakeException(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    56
                "Could not generate secret").initCause(gse);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    57
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    58
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    59
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    60
    private static byte[] createHkdfInfo(
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    61
            byte[] label, byte[] context, int length) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    62
        byte[] info = new byte[4 + label.length + context.length];
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    63
        ByteBuffer m = ByteBuffer.wrap(info);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    64
        try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    65
            Record.putInt16(m, length);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    66
            Record.putBytes8(m, label);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    67
            Record.putBytes8(m, context);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    68
        } catch (IOException ioe) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    69
            // unlikely
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    70
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    71
        return info;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    72
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    73
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    74
    static class SecretSizeSpec implements AlgorithmParameterSpec {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    75
        final int length;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    76
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    77
        SecretSizeSpec(int length) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    78
            this.length = length;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    79
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    80
    }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents:
diff changeset
    81
}