From 1c6e83e7f628120cd8d5244b2896277b9216719e Mon Sep 17 00:00:00 2001 From: exceptionfactory Date: Tue, 15 Aug 2023 11:35:23 -0500 Subject: [PATCH] Updated OpenSSLProvider to use Alg.Alias for X25519 --- .../java/org/conscrypt/OpenSSLProvider.java | 10 ++---- .../java/security/KeyFactoryTestX25519.java | 35 ------------------- .../java/security/KeyFactoryTestXDH.java | 16 ++++----- .../java/security/KeyPairGeneratorTest.java | 17 ++------- 4 files changed, 12 insertions(+), 66 deletions(-) delete mode 100644 common/src/test/java/org/conscrypt/java/security/KeyFactoryTestX25519.java diff --git a/common/src/main/java/org/conscrypt/OpenSSLProvider.java b/common/src/main/java/org/conscrypt/OpenSSLProvider.java index a17b7380a..bb02f94be 100644 --- a/common/src/main/java/org/conscrypt/OpenSSLProvider.java +++ b/common/src/main/java/org/conscrypt/OpenSSLProvider.java @@ -196,9 +196,9 @@ public OpenSSLProvider(String providerName) { put("Alg.Alias.KeyPairGenerator.1.2.840.10045.2.1", "EC"); put("Alg.Alias.KeyPairGenerator.1.3.133.16.840.63.0.2", "EC"); - put("KeyPairGenerator.X25519", PREFIX + "OpenSSLXDHKeyPairGenerator"); put("KeyPairGenerator.XDH", PREFIX + "OpenSSLXDHKeyPairGenerator"); put("Alg.Alias.KeyPairGenerator.1.3.101.110", "XDH"); + put("Alg.Alias.KeyPairGenerator.X25519", "XDH"); /* == KeyFactory == */ put("KeyFactory.RSA", PREFIX + "OpenSSLRSAKeyFactory"); @@ -210,9 +210,9 @@ public OpenSSLProvider(String providerName) { put("Alg.Alias.KeyFactory.1.2.840.10045.2.1", "EC"); put("Alg.Alias.KeyFactory.1.3.133.16.840.63.0.2", "EC"); - put("KeyFactory.X25519", PREFIX + "OpenSSLXDHKeyFactory"); put("KeyFactory.XDH", PREFIX + "OpenSSLXDHKeyFactory"); put("Alg.Alias.KeyFactory.1.3.101.110", "XDH"); + put("Alg.Alias.KeyFactory.X25519", "XDH"); /* == SecretKeyFactory == */ put("SecretKeyFactory.DESEDE", PREFIX + "DESEDESecretKeyFactory"); @@ -627,11 +627,7 @@ private void putXDHKeyAgreementImplClass(String className) { supportedKeyClasses, supportedKeyFormats); - putImplClassWithKeyConstraints( - "KeyAgreement.X25519", - PREFIX + className, - supportedKeyClasses, - supportedKeyFormats); + put("Alg.Alias.KeyAgreement.X25519", "XDH"); } private void putImplClassWithKeyConstraints(String typeAndAlgName, diff --git a/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestX25519.java b/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestX25519.java deleted file mode 100644 index c66b0137e..000000000 --- a/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestX25519.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.conscrypt.java.security; - -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.X509EncodedKeySpec; -import java.util.Arrays; -import java.util.List; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; -import tests.util.ServiceTester; - -@RunWith(JUnit4.class) -public class KeyFactoryTestX25519 extends KeyFactoryTestXDH { - - public KeyFactoryTestX25519() { - super("X25519"); - } -} diff --git a/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestXDH.java b/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestXDH.java index 35ce3738e..1f6b5fd7c 100644 --- a/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestXDH.java +++ b/common/src/test/java/org/conscrypt/java/security/KeyFactoryTestXDH.java @@ -31,16 +31,12 @@ public class KeyFactoryTestXDH extends AbstractKeyFactoryTest { public KeyFactoryTestXDH() { - this("XDH"); - } - - public KeyFactoryTestXDH(final String algorithmName) { - super(algorithmName, X509EncodedKeySpec.class, PKCS8EncodedKeySpec.class); + super("XDH", X509EncodedKeySpec.class, PKCS8EncodedKeySpec.class); } @Override protected void check(KeyPair keyPair) throws Exception { - new KeyAgreementHelper(algorithmName).test(keyPair); + new KeyAgreementHelper("XDH").test(keyPair); } @Override @@ -53,12 +49,12 @@ protected ServiceTester customizeTester(ServiceTester tester) { protected List getKeys() throws NoSuchAlgorithmException, InvalidKeySpecException { return Arrays.asList( new KeyPair( - DefaultKeys.getPublicKey(algorithmName), - DefaultKeys.getPrivateKey(algorithmName) + DefaultKeys.getPublicKey("XDH"), + DefaultKeys.getPrivateKey("XDH") ), new KeyPair( - new TestPublicKey(DefaultKeys.getPublicKey(algorithmName)), - new TestPrivateKey(DefaultKeys.getPrivateKey(algorithmName)) + new TestPublicKey(DefaultKeys.getPublicKey("XDH")), + new TestPrivateKey(DefaultKeys.getPrivateKey("XDH")) ) ); } diff --git a/common/src/test/java/org/conscrypt/java/security/KeyPairGeneratorTest.java b/common/src/test/java/org/conscrypt/java/security/KeyPairGeneratorTest.java index 0c7dbafbc..3ef73a2b7 100644 --- a/common/src/test/java/org/conscrypt/java/security/KeyPairGeneratorTest.java +++ b/common/src/test/java/org/conscrypt/java/security/KeyPairGeneratorTest.java @@ -89,7 +89,7 @@ public void test(Provider provider, String algorithm) throws Exception { } // KeyPairGenerator.getInstance(String) KeyPairGenerator kpg1 = KeyPairGenerator.getInstance(algorithm); - assertAlgorithmMatched(algorithm, kpg1); + assertEquals(algorithm, kpg1.getAlgorithm()); if (params != null) { kpg1.initialize(params); } @@ -97,7 +97,7 @@ public void test(Provider provider, String algorithm) throws Exception { // KeyPairGenerator.getInstance(String, Provider) KeyPairGenerator kpg2 = KeyPairGenerator.getInstance(algorithm, provider); - assertAlgorithmMatched(algorithm, kpg2); + assertEquals(algorithm, kpg2.getAlgorithm()); assertEquals(provider, kpg2.getProvider()); if (params != null) { kpg2.initialize(params); @@ -107,7 +107,7 @@ public void test(Provider provider, String algorithm) throws Exception { // KeyPairGenerator.getInstance(String, String) KeyPairGenerator kpg3 = KeyPairGenerator.getInstance(algorithm, provider.getName()); - assertAlgorithmMatched(algorithm, kpg3); + assertEquals(algorithm, kpg3.getAlgorithm()); assertEquals(provider, kpg3.getProvider()); if (params != null) { kpg3.initialize(params); @@ -361,17 +361,6 @@ private static void assertECParametersEquals(ECParameterSpec expected, ECParamet assertEquals(expected.getCofactor(), actual.getCofactor()); } - private static void assertAlgorithmMatched(final String algorithm, final KeyPairGenerator keyPairGenerator) { - final String expectedAlgorithm; - // X25519 KeyPairGenerator is an alias for XDH requiring this alternative expected algorithm - if ("X25519".equals(algorithm)) { - expectedAlgorithm = "XDH"; - } else { - expectedAlgorithm = algorithm; - } - assertEquals(expectedAlgorithm, keyPairGenerator.getAlgorithm()); - } - /** * DH parameters pre-generated so that the test doesn't take too long. * These parameters were generated with: