Path: blob/master/src/jdk.crypto.ec/share/classes/sun/security/ec/XDHKeyFactory.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.security.KeyFactorySpi;28import java.security.Key;29import java.security.PublicKey;30import java.security.PrivateKey;31import java.security.InvalidKeyException;32import java.security.ProviderException;33import java.security.interfaces.XECKey;34import java.security.interfaces.XECPrivateKey;35import java.security.interfaces.XECPublicKey;36import java.security.spec.AlgorithmParameterSpec;37import java.security.spec.NamedParameterSpec;38import java.security.spec.KeySpec;39import java.security.spec.InvalidKeySpecException;40import java.security.spec.PKCS8EncodedKeySpec;41import java.security.spec.X509EncodedKeySpec;42import java.security.spec.XECPublicKeySpec;43import java.security.spec.XECPrivateKeySpec;44import java.util.Arrays;45import java.util.function.Function;4647public class XDHKeyFactory extends KeyFactorySpi {4849private XECParameters lockedParams = null;5051XDHKeyFactory() {52// do nothing53}5455protected XDHKeyFactory(AlgorithmParameterSpec paramSpec) {56lockedParams = XECParameters.get(ProviderException::new, paramSpec);57}5859@Override60protected Key engineTranslateKey(Key key) throws InvalidKeyException {6162if (key == null) {63throw new InvalidKeyException("Key must not be null");64}6566if (key instanceof XECKey) {67XECKey xecKey = (XECKey) key;68XECParameters params = XECParameters.get(InvalidKeyException::new,69xecKey.getParams());70checkLockedParams(InvalidKeyException::new, params);7172if (xecKey instanceof XECPublicKey) {73XECPublicKey publicKey = (XECPublicKey) xecKey;74return new XDHPublicKeyImpl(params, publicKey.getU());75} else if (xecKey instanceof XECPrivateKey) {76XECPrivateKey privateKey = (XECPrivateKey) xecKey;77byte[] scalar = privateKey.getScalar().orElseThrow(78() -> new InvalidKeyException("No private key data"));79return new XDHPrivateKeyImpl(params, scalar);80} else {81throw new InvalidKeyException("Unsupported XECKey subclass");82}83} else if (key instanceof PublicKey &&84key.getFormat().equals("X.509")) {85XDHPublicKeyImpl result = new XDHPublicKeyImpl(key.getEncoded());86checkLockedParams(InvalidKeyException::new, result.getParams());87return result;88} else if (key instanceof PrivateKey &&89key.getFormat().equals("PKCS#8")) {90byte[] encoded = key.getEncoded();91try {92XDHPrivateKeyImpl result = new XDHPrivateKeyImpl(encoded);93checkLockedParams(InvalidKeyException::new, result.getParams());94return result;95} finally {96Arrays.fill(encoded, (byte)0);97}98} else {99throw new InvalidKeyException("Unsupported key type or format");100}101}102103private104<T extends Throwable>105void checkLockedParams(Function<String, T> exception,106AlgorithmParameterSpec spec) throws T {107108XECParameters params = XECParameters.get(exception, spec);109checkLockedParams(exception, params);110}111112private113<T extends Throwable>114void checkLockedParams(Function<String, T> exception,115XECParameters params) throws T {116117if (lockedParams != null && lockedParams != params) {118throw exception.apply("Parameters must be " +119lockedParams.getName());120}121}122123@Override124protected PublicKey engineGeneratePublic(KeySpec keySpec)125throws InvalidKeySpecException {126127try {128return generatePublicImpl(keySpec);129} catch (InvalidKeyException ex) {130throw new InvalidKeySpecException(ex);131}132}133134@Override135protected PrivateKey engineGeneratePrivate(KeySpec keySpec)136throws InvalidKeySpecException {137138try {139return generatePrivateImpl(keySpec);140} catch (InvalidKeyException ex) {141throw new InvalidKeySpecException(ex);142}143}144145146private PublicKey generatePublicImpl(KeySpec keySpec)147throws InvalidKeyException, InvalidKeySpecException {148149if (keySpec instanceof X509EncodedKeySpec) {150X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;151XDHPublicKeyImpl result =152new XDHPublicKeyImpl(x509Spec.getEncoded());153checkLockedParams(InvalidKeySpecException::new,154result.getParams());155return result;156} else if (keySpec instanceof XECPublicKeySpec) {157XECPublicKeySpec publicKeySpec = (XECPublicKeySpec) keySpec;158XECParameters params = XECParameters.get(159InvalidKeySpecException::new, publicKeySpec.getParams());160checkLockedParams(InvalidKeySpecException::new, params);161return new XDHPublicKeyImpl(params, publicKeySpec.getU());162} else {163throw new InvalidKeySpecException(164"Only X509EncodedKeySpec and XECPublicKeySpec are supported");165}166}167168private PrivateKey generatePrivateImpl(KeySpec keySpec)169throws InvalidKeyException, InvalidKeySpecException {170171if (keySpec instanceof PKCS8EncodedKeySpec) {172PKCS8EncodedKeySpec pkcsSpec = (PKCS8EncodedKeySpec) keySpec;173byte[] encoded = pkcsSpec.getEncoded();174try {175XDHPrivateKeyImpl result = new XDHPrivateKeyImpl(encoded);176checkLockedParams(InvalidKeySpecException::new,177result.getParams());178return result;179} finally {180Arrays.fill(encoded, (byte) 0);181}182} else if (keySpec instanceof XECPrivateKeySpec) {183XECPrivateKeySpec privateKeySpec = (XECPrivateKeySpec) keySpec;184XECParameters params = XECParameters.get(185InvalidKeySpecException::new, privateKeySpec.getParams());186checkLockedParams(InvalidKeySpecException::new, params);187byte[] scalar = privateKeySpec.getScalar();188try {189return new XDHPrivateKeyImpl(params, scalar);190} finally {191Arrays.fill(scalar, (byte)0);192}193} else {194throw new InvalidKeySpecException(195"Only PKCS8EncodedKeySpec and XECPrivateKeySpec supported");196}197}198199protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)200throws InvalidKeySpecException {201202if (key instanceof XECPublicKey) {203checkLockedParams(InvalidKeySpecException::new,204((XECPublicKey) key).getParams());205206if (keySpec.isAssignableFrom(X509EncodedKeySpec.class)) {207if (!key.getFormat().equals("X.509")) {208throw new InvalidKeySpecException("Format is not X.509");209}210return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));211} else if (keySpec.isAssignableFrom(XECPublicKeySpec.class)) {212XECPublicKey xecKey = (XECPublicKey) key;213return keySpec.cast(214new XECPublicKeySpec(xecKey.getParams(), xecKey.getU()));215} else {216throw new InvalidKeySpecException(217"KeySpec must be X509EncodedKeySpec or XECPublicKeySpec");218}219} else if (key instanceof XECPrivateKey) {220checkLockedParams(InvalidKeySpecException::new,221((XECPrivateKey) key).getParams());222223if (keySpec.isAssignableFrom(PKCS8EncodedKeySpec.class)) {224if (!key.getFormat().equals("PKCS#8")) {225throw new InvalidKeySpecException("Format is not PKCS#8");226}227byte[] encoded = key.getEncoded();228try {229return keySpec.cast(new PKCS8EncodedKeySpec(encoded));230} finally {231Arrays.fill(encoded, (byte)0);232}233} else if (keySpec.isAssignableFrom(XECPrivateKeySpec.class)) {234XECPrivateKey xecKey = (XECPrivateKey) key;235byte[] scalar = xecKey.getScalar().orElseThrow(236() -> new InvalidKeySpecException("No private key value")237);238try {239return keySpec.cast(240new XECPrivateKeySpec(xecKey.getParams(), scalar));241} finally {242Arrays.fill(scalar, (byte)0);243}244} else {245throw new InvalidKeySpecException246("KeySpec must be PKCS8EncodedKeySpec or XECPrivateKeySpec");247}248} else {249throw new InvalidKeySpecException("Unsupported key type");250}251}252253static class X25519 extends XDHKeyFactory {254255public X25519() {256super(NamedParameterSpec.X25519);257}258}259260static class X448 extends XDHKeyFactory {261262public X448() {263super(NamedParameterSpec.X448);264}265}266}267268269