Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHPrivateKeyImpl.java
41161 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.ec;2627import java.io.*;28import java.security.interfaces.XECPrivateKey;29import java.util.Optional;30import java.security.*;31import java.security.spec.*;3233import sun.security.pkcs.PKCS8Key;34import sun.security.x509.AlgorithmId;35import sun.security.util.*;3637public final class XDHPrivateKeyImpl extends PKCS8Key implements XECPrivateKey {3839private static final long serialVersionUID = 1L;4041private final AlgorithmParameterSpec paramSpec;42private byte[] k;4344XDHPrivateKeyImpl(XECParameters params, byte[] k)45throws InvalidKeyException {4647this.paramSpec = new NamedParameterSpec(params.getName());48this.k = k.clone();4950this.algid = new AlgorithmId(params.getOid());5152DerValue val = new DerValue(DerValue.tag_OctetString, k);53try {54this.key = val.toByteArray();55} catch (IOException ex) {56throw new AssertionError("Should not happen", ex);57} finally {58val.clear();59}60checkLength(params);61}6263XDHPrivateKeyImpl(byte[] encoded) throws InvalidKeyException {64super(encoded);65XECParameters params = XECParameters.get(66InvalidKeyException::new, algid);67paramSpec = new NamedParameterSpec(params.getName());68try {69DerInputStream derStream = new DerInputStream(key);70k = derStream.getOctetString();71} catch (IOException ex) {72throw new InvalidKeyException(ex);73}74checkLength(params);75}7677void checkLength(XECParameters params) throws InvalidKeyException {7879if (params.getBytes() != this.k.length) {80throw new InvalidKeyException(81"key length must be " + params.getBytes());82}83}8485public byte[] getK() {86return k.clone();87}8889@Override90public String getAlgorithm() {91return "XDH";92}9394@Override95public AlgorithmParameterSpec getParams() {96return paramSpec;97}9899@Override100public Optional<byte[]> getScalar() {101return Optional.of(getK());102}103}104105106107