src/java.base/share/classes/sun/security/ssl/XDHKeyExchange.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 57718 a93b7b28f644
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55353
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     1
/*
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     4
 *
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    10
 *
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    15
 * accompanied this code).
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    16
 *
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    20
 *
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    23
 * questions.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    24
 */
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    25
package sun.security.ssl;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    26
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    27
import java.io.IOException;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    28
import java.math.BigInteger;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    29
import java.security.*;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    30
import java.security.interfaces.XECPublicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    31
import java.security.spec.*;
57718
a93b7b28f644 8226374: Restrict TLS signature schemes and named groups
xuelei
parents: 55353
diff changeset
    32
import sun.security.ssl.NamedGroup.NamedGroupSpec;
55353
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    33
import sun.security.util.*;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    34
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    35
/**
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    36
 * Specifics for XEC/XDH Keys/Exchanges
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    37
 */
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    38
final class XDHKeyExchange {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    39
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    40
    static final SSLKeyAgreementGenerator xdheKAGenerator
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    41
            = new XDHEKAGenerator();
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    42
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    43
    static final class XDHECredentials implements NamedGroupCredentials {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    44
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    45
        final XECPublicKey popPublicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    46
        final NamedGroup namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    47
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    48
        XDHECredentials(XECPublicKey popPublicKey, NamedGroup namedGroup) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    49
            this.popPublicKey = popPublicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    50
            this.namedGroup = namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    51
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    52
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    53
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    54
        public PublicKey getPublicKey() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    55
            return popPublicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    56
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    57
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    58
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    59
        public NamedGroup getNamedGroup() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    60
            return namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    61
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    62
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    63
        /**
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    64
         * Parse the encoded Point into the XDHECredentials using the
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    65
         * namedGroup.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    66
         */
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    67
        static XDHECredentials valueOf(NamedGroup namedGroup,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    68
                byte[] encodedPoint) throws IOException,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    69
                GeneralSecurityException {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    70
57718
a93b7b28f644 8226374: Restrict TLS signature schemes and named groups
xuelei
parents: 55353
diff changeset
    71
            if (namedGroup.spec != NamedGroupSpec.NAMED_GROUP_XDH) {
55353
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    72
                throw new RuntimeException(
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    73
                        "Credentials decoding:  Not XDH named group");
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    74
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    75
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    76
            if (encodedPoint == null || encodedPoint.length == 0) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    77
                return null;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    78
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    79
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    80
            byte[] uBytes = encodedPoint.clone();
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    81
            Utilities.reverseBytes(uBytes);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    82
            BigInteger u = new BigInteger(1, uBytes);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    83
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    84
            XECPublicKeySpec xecPublicKeySpec = new XECPublicKeySpec(
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    85
                    new NamedParameterSpec(namedGroup.name), u);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    86
            KeyFactory factory = KeyFactory.getInstance(namedGroup.algorithm);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    87
            XECPublicKey publicKey = (XECPublicKey) factory.generatePublic(
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    88
                    xecPublicKeySpec);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    89
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    90
            return new XDHECredentials(publicKey, namedGroup);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    91
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    92
    }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    93
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    94
    static final class XDHEPossession implements NamedGroupPossession {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    95
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    96
        final PrivateKey privateKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    97
        final XECPublicKey publicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    98
        final NamedGroup namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
    99
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   100
        XDHEPossession(NamedGroup namedGroup, SecureRandom random) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   101
            try {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   102
                KeyPairGenerator kpg
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   103
                        = KeyPairGenerator.getInstance(namedGroup.algorithm);
57718
a93b7b28f644 8226374: Restrict TLS signature schemes and named groups
xuelei
parents: 55353
diff changeset
   104
                kpg.initialize(namedGroup.keAlgParamSpec, random);
55353
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   105
                KeyPair kp = kpg.generateKeyPair();
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   106
                privateKey = kp.getPrivate();
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   107
                publicKey = (XECPublicKey) kp.getPublic();
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   108
            } catch (GeneralSecurityException e) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   109
                throw new RuntimeException(
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   110
                        "Could not generate XDH keypair", e);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   111
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   112
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   113
            this.namedGroup = namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   114
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   115
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   116
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   117
        public byte[] encode() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   118
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   119
            byte[] uBytes = ECUtil.trimZeroes(publicKey.getU().toByteArray());
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   120
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   121
            int expLength;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   122
            switch (namedGroup) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   123
                case X25519:
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   124
                    expLength = 32;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   125
                    break;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   126
                case X448:
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   127
                    expLength = 56;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   128
                    break;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   129
                default:
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   130
                    throw new RuntimeException("Invalid XDH group");
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   131
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   132
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   133
            if (uBytes.length > expLength) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   134
                throw new RuntimeException("Encoded XDH key too large");
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   135
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   136
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   137
            if (uBytes.length != expLength) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   138
                byte[] tmp = new byte[expLength];
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   139
                System.arraycopy(uBytes, 0, tmp,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   140
                        expLength - uBytes.length, uBytes.length);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   141
                uBytes = tmp;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   142
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   143
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   144
            Utilities.reverseBytes(uBytes);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   145
            return (uBytes);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   146
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   147
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   148
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   149
        public PublicKey getPublicKey() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   150
            return publicKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   151
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   152
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   153
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   154
        public NamedGroup getNamedGroup() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   155
            return namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   156
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   157
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   158
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   159
        public PrivateKey getPrivateKey() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   160
            return privateKey;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   161
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   162
    }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   163
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   164
    private static final class XDHEKAGenerator
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   165
            implements SSLKeyAgreementGenerator {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   166
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   167
        // Prevent instantiation of this class.
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   168
        private XDHEKAGenerator() {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   169
            // blank
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   170
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   171
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   172
        @Override
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   173
        public SSLKeyDerivation createKeyDerivation(
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   174
                HandshakeContext context) throws IOException {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   175
            XDHEPossession xdhePossession = null;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   176
            XDHECredentials xdheCredentials = null;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   177
            for (SSLPossession poss : context.handshakePossessions) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   178
                if (!(poss instanceof XDHEPossession)) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   179
                    continue;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   180
                }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   181
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   182
                NamedGroup ng = ((XDHEPossession) poss).namedGroup;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   183
                for (SSLCredentials cred : context.handshakeCredentials) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   184
                    if (!(cred instanceof XDHECredentials)) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   185
                        continue;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   186
                    }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   187
                    if (ng.equals(((XDHECredentials) cred).namedGroup)) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   188
                        xdheCredentials = (XDHECredentials) cred;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   189
                        break;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   190
                    }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   191
                }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   192
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   193
                if (xdheCredentials != null) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   194
                    xdhePossession = (XDHEPossession) poss;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   195
                    break;
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   196
                }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   197
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   198
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   199
            if (xdhePossession == null || xdheCredentials == null) {
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   200
                context.conContext.fatal(Alert.HANDSHAKE_FAILURE,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   201
                        "No sufficient XDHE key agreement "
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   202
                        + "parameters negotiated");
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   203
            }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   204
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   205
            return new KAKeyDerivation("XDH", context,
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   206
                    xdhePossession.privateKey, xdheCredentials.popPublicKey);
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   207
        }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   208
    }
946f7f2d321c 8171279: Support X25519 and X448 in TLS
wetmore
parents:
diff changeset
   209
}