Path: blob/master/test/jdk/javax/crypto/KeyGenerator/CompareKeys.java
41149 views
/*1* Copyright (c) 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.crypto.SecretKey;24import javax.crypto.spec.SecretKeySpec;25import java.security.Key;26import java.security.KeyFactory;27import java.security.KeyPair;28import java.security.KeyPairGenerator;29import java.security.Provider;30import java.security.Security;31import java.security.spec.PKCS8EncodedKeySpec;32import java.security.spec.X509EncodedKeySpec;33import java.util.Arrays;34import java.util.LinkedList;35import java.util.List;36import javax.crypto.KeyGenerator;3738/*39* @test40* @bug 818512741* @summary Test key comparison for the Keys generated through KeyGenerator42* @run main CompareKeys43*/44public class CompareKeys {4546public static void main(String[] args) throws Exception {4748for (KeygenAlgo alg : getSupportedAlgo("KeyGenerator")) {49System.out.printf("Verifying provider %s and algorithm %s%n",50alg.provider().getName(), alg.algoName());51SecretKey k = genSecretKey(alg.algoName(), alg.provider());52checkKeyEquality(k, copy(alg.algoName(), k));53}5455for (KeygenAlgo alg : getSupportedAlgo("KeyPairGenerator")) {56System.out.printf("Verifying provider %s and algorithm %s%n",57alg.provider().getName(), alg.algoName());58KeyPair kp = genKeyPair(alg.algoName(), alg.provider());59checkKeyPairEquality(kp, copy(alg.algoName(), kp));60}61System.out.println("Done!");62}6364@SuppressWarnings("preview")65private record KeygenAlgo(String algoName, Provider provider) {6667}6869private static void checkKeyPairEquality(KeyPair origkp, KeyPair copykp)70throws Exception {7172checkKeyEquality(origkp.getPrivate(), copykp.getPrivate());73checkKeyEquality(origkp.getPublic(), copykp.getPublic());74}7576/**77* Compare original Key with another copy.78*/79private static void checkKeyEquality(Key origKey, Key copyKey) {8081if ((origKey.equals(copyKey)82&& origKey.hashCode() == copyKey.hashCode()83&& Arrays.equals(origKey.getEncoded(), copyKey.getEncoded())84&& origKey.getFormat().equals(copyKey.getFormat()))) {85System.out.printf("%s equality check Passed%n",86origKey.getClass().getName());87} else {88System.out.println("Result- equals: "89+ origKey.equals(copyKey));90System.out.println("Result- hashCode: "91+ (origKey.hashCode() == copyKey.hashCode()));92System.out.println("Result- encoded check: " + Arrays.equals(93origKey.getEncoded(), copyKey.getEncoded()));94System.out.println("Result- format check: "95+ origKey.getFormat().equals(copyKey.getFormat()));96throw new RuntimeException("Key inequality found");97}98}99100private static Key copy(String algo, Key key) throws Exception {101102return new SecretKeySpec(key.getEncoded(), algo);103}104105private static KeyPair copy(String algo, KeyPair kp) throws Exception {106107KeyFactory kf = KeyFactory.getInstance(algo);108return new KeyPair(109kf.generatePublic(110new X509EncodedKeySpec(kp.getPublic().getEncoded())),111kf.generatePrivate(112new PKCS8EncodedKeySpec(kp.getPrivate().getEncoded())));113}114115private static List<KeygenAlgo> getSupportedAlgo(String type)116throws Exception {117118List<KeygenAlgo> kgs = new LinkedList<>();119for (Provider p : Security.getProviders()) {120for (Provider.Service s : p.getServices()) {121// Ignore the algorithms from the list which require122// pre-initialization to make the Test generic across algorithms.123// SunMSCAPI provider is ignored too because of incompatibilty124// for serialization and with PKCS8EncodedKeySpec for certain125// algorithms like RSA.126if (s.getType().contains(type)127&& !((s.getAlgorithm().startsWith("SunTls"))128|| s.getProvider().getName().equals("SunMSCAPI"))) {129kgs.add(new KeygenAlgo(s.getAlgorithm(), s.getProvider()));130}131}132}133return kgs;134}135136public static SecretKey genSecretKey(String algoName, Provider provider)137throws Exception {138139return KeyGenerator.getInstance(algoName, provider).generateKey();140}141142public static KeyPair genKeyPair(String algoName, Provider provider)143throws Exception {144145return KeyPairGenerator.getInstance(algoName, provider)146.generateKeyPair();147}148}149150151