src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPrivateKeyImpl.java
author shade
Fri, 22 Feb 2019 17:54:13 +0100
changeset 53899 bae1944fc279
parent 52575 1092ba73cb2d
permissions -rw-r--r--
8219574: Minimal VM build failure after JDK-8219414 Reviewed-by: jgeorge, dholmes, cjplummer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     1
/*
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     4
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    10
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    15
 * accompanied this code).
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    16
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    20
 *
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    23
 * questions.
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    24
 */
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    25
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    26
package sun.security.ec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    27
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    28
import java.io.*;
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    29
import java.security.interfaces.XECPrivateKey;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    30
import java.util.Optional;
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    31
import java.security.*;
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    32
import java.security.spec.*;
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    33
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    34
import sun.security.pkcs.PKCS8Key;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    35
import sun.security.x509.AlgorithmId;
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    36
import sun.security.util.*;
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    37
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    38
public final class XDHPrivateKeyImpl extends PKCS8Key implements XECPrivateKey {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    39
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    40
    private static final long serialVersionUID = 1L;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    41
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    42
    private final AlgorithmParameterSpec paramSpec;
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    43
    private byte[] k;
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    44
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    45
    XDHPrivateKeyImpl(XECParameters params, byte[] k)
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    46
        throws InvalidKeyException {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    47
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    48
        this.paramSpec = new NamedParameterSpec(params.getName());
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    49
        this.k = k.clone();
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    50
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    51
        this.algid = new AlgorithmId(params.getOid());
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    52
        encodeKey();
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    53
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    54
        checkLength(params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    55
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    56
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    57
    XDHPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    58
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    59
        decode(encoded);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    60
        XECParameters params = XECParameters.get(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    61
            InvalidKeyException::new, algid);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    62
        paramSpec = new NamedParameterSpec(params.getName());
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    63
        decodeKey();
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    64
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    65
        checkLength(params);
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    66
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    67
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    68
    private void decodeKey() throws InvalidKeyException {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    69
        try {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    70
            DerInputStream derStream = new DerInputStream(key);
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    71
            k = derStream.getOctetString();
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    72
        } catch (IOException ex) {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    73
            throw new InvalidKeyException(ex);
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    74
        }
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    75
    }
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    76
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    77
    private void encodeKey() {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    78
        DerOutputStream derKey = new DerOutputStream();
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    79
        try {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    80
            derKey.putOctetString(k);
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    81
            this.key = derKey.toByteArray();
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    82
        } catch (IOException ex) {
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    83
            throw new ProviderException(ex);
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    84
        }
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    85
    }
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    86
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    87
    void checkLength(XECParameters params) throws InvalidKeyException {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    88
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    89
        if (params.getBytes() != this.k.length) {
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    90
            throw new InvalidKeyException(
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    91
                "key length must be " + params.getBytes());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    92
        }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    93
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    94
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    95
    public byte[] getK() {
52575
1092ba73cb2d 8213363: X25519 private key PKCS#8 encoding/decoding is incorrect
apetcher
parents: 50053
diff changeset
    96
        return k.clone();
50053
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    97
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    98
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
    99
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   100
    public String getAlgorithm() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   101
        return "XDH";
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   102
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   103
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   104
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   105
    public AlgorithmParameterSpec getParams() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   106
        return paramSpec;
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   107
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   108
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   109
    @Override
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   110
    public Optional<byte[]> getScalar() {
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   111
        return Optional.of(getK());
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   112
    }
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   113
}
9bc1e6487cbb 8171277: Elliptic Curves for Security in Crypto
apetcher
parents:
diff changeset
   114