Path: blob/master/test/jdk/java/security/SignedObject/Chain.java
41152 views
/*1* Copyright (c) 2015, 2020, 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 java.security.*;24import java.security.spec.*;25import java.util.*;26import jdk.test.lib.SigTestUtil;27import static jdk.test.lib.SigTestUtil.SignatureType;2829/*30* @test31* @bug 8050374 8181048 814629332* @summary Verify a chain of signed objects33* @library /test/lib34* @build jdk.test.lib.SigTestUtil35* @run main Chain36*/37public class Chain {3839static enum KeyAlg {40RSA("RSA"),41DSA("DSA"),42EC("EC");4344final String name;4546KeyAlg(String alg) {47this.name = alg;48}49}5051static enum Provider {52Default("default"),53SunRsaSign("SunRsaSign"),54Sun("SUN"),55SunEC("SunEC"),56SunJSSE("SunJSSE"),57SunMSCAPI("SunMSCAPI");5859final String name;6061Provider(String name) {62this.name = name;63}64}6566static enum SigAlg {67MD2withRSA("MD2withRSA"),68MD5withRSA("md5withRSA"),6970SHA1withDSA("SHA1withDSA"),71SHA224withDSA("SHA224withDSA"),72SHA256withDSA("SHA256withDSA"),73SHA384withDSA("SHA384withDSA"),74SHA512withDSA("SHA512withDSA"),7576SHA3_224withDSA("SHA3-224withDSA"),77SHA3_256withDSA("SHA3-256withDSA"),78SHA3_384withDSA("SHA3-384withDSA"),79SHA3_512withDSA("SHA3-512withDSA"),8081SHA1withRSA("Sha1withrSA"),82SHA224withRSA("SHA224withRSA"),83SHA256withRSA("SHA256withRSA"),84SHA384withRSA("SHA384withRSA"),85SHA512withRSA("SHA512withRSA"),86SHA512_224withRSA("SHA512/224withRSA"),87SHA512_256withRSA("SHA512/256withRSA"),88SHA3_224withRSA("SHA3-224withRSA"),89SHA3_256withRSA("SHA3-256withRSA"),90SHA3_384withRSA("SHA3-384withRSA"),91SHA3_512withRSA("SHA3-512withRSA"),9293SHA1withECDSA("SHA1withECDSA"),94SHA224withECDSA("SHA224withECDSA"),95SHA256withECDSA("SHA256withECDSA"),96SHA384withECDSA("SHA384withECDSA"),97SHA512withECDSA("SHA512withECDSA"),98SHA3_224withECDSA("SHA3-224withECDSA"),99SHA3_256withECDSA("SHA3-256withECDSA"),100SHA3_384withECDSA("SHA3-384withECDSA"),101SHA3_512withECDSA("SHA3-512withECDSA"),102103MD5andSHA1withRSA("MD5andSHA1withRSA"),104105RSASSA_PSS("RSASSA-PSS");106107final String name;108109SigAlg(String name) {110this.name = name;111}112}113114static class Test {115final Provider provider;116final KeyAlg keyAlg;117final SigAlg sigAlg;118final int keySize;119final AlgorithmParameterSpec sigParams;120121Test(SigAlg sigAlg, KeyAlg keyAlg, Provider provider) {122this(sigAlg, keyAlg, provider, -1, null);123}124125Test(SigAlg sigAlg, KeyAlg keyAlg, Provider provider, int keySize) {126this(sigAlg, keyAlg, provider, keySize, null);127}128129Test(SigAlg sigAlg, KeyAlg keyAlg, Provider provider, int keySize,130AlgorithmParameterSpec sigParams) {131this.provider = provider;132this.keyAlg = keyAlg;133this.sigAlg = sigAlg;134this.keySize = keySize;135this.sigParams = sigParams;136}137138private static String formatParams(AlgorithmParameterSpec aps) {139if (aps == null) return "null";140if (aps instanceof PSSParameterSpec) {141PSSParameterSpec p = (PSSParameterSpec) aps;142return String.format("PSSParameterSpec (%s, %s, %s, %s)",143p.getDigestAlgorithm(), formatParams(p.getMGFParameters()),144p.getSaltLength(), p.getTrailerField());145} else if (aps instanceof MGF1ParameterSpec) {146return "MGF1" +147((MGF1ParameterSpec)aps).getDigestAlgorithm();148} else {149return aps.toString();150}151}152153public String toString() {154return String.format("Test: provider = %s, signature alg = %s, "155+ " w/ %s, key alg = %s", provider, sigAlg,156formatParams(sigParams), keyAlg);157}158}159160private static final Test[] tests = {161new Test(SigAlg.SHA1withDSA, KeyAlg.DSA, Provider.Default, 1024),162new Test(SigAlg.MD2withRSA, KeyAlg.RSA, Provider.Default),163new Test(SigAlg.MD5withRSA, KeyAlg.RSA, Provider.Default),164new Test(SigAlg.SHA3_224withRSA, KeyAlg.RSA, Provider.Default),165new Test(SigAlg.SHA3_256withRSA, KeyAlg.RSA, Provider.Default),166new Test(SigAlg.SHA3_384withRSA, KeyAlg.RSA, Provider.Default),167new Test(SigAlg.SHA3_512withRSA, KeyAlg.RSA, Provider.Default),168new Test(SigAlg.SHA1withDSA, KeyAlg.DSA, Provider.Sun, 1024),169new Test(SigAlg.SHA224withDSA, KeyAlg.DSA, Provider.Sun, 2048),170new Test(SigAlg.SHA256withDSA, KeyAlg.DSA, Provider.Sun, 2048),171};172173private static final String str = "to-be-signed";174private static final int N = 3;175176public static void main(String argv[]) {177boolean result = Arrays.stream(tests).allMatch((test) -> runTest(test));178result &= runTestPSS(2048);179if (result) {180System.out.println("All tests passed");181} else {182throw new RuntimeException("Some tests failed");183}184}185186private static boolean runTestPSS(int keysize) {187boolean result = true;188SigAlg pss = SigAlg.RSASSA_PSS;189Iterator<String> mdAlgs = SigTestUtil.getDigestAlgorithms190(SignatureType.RSASSA_PSS, keysize).iterator();191while (mdAlgs.hasNext()) {192result &= runTest(new Test(pss, KeyAlg.RSA, Provider.SunRsaSign,193keysize, SigTestUtil.generateDefaultParameter194(SignatureType.RSASSA_PSS, mdAlgs.next())));195}196return result;197}198199static boolean runTest(Test test) {200System.out.println(test);201try {202// Generate all private/public key pairs203PrivateKey[] privKeys = new PrivateKey[N];204PublicKey[] pubKeys = new PublicKey[N];205PublicKey[] anotherPubKeys = new PublicKey[N];206Signature signature;207KeyPairGenerator kpg;208if (test.provider != Provider.Default) {209signature = Signature.getInstance(test.sigAlg.name,210test.provider.name);211// try using the same provider first, if not, fallback212// to the first available impl213try {214kpg = KeyPairGenerator.getInstance(215test.keyAlg.name, test.provider.name);216} catch (NoSuchAlgorithmException nsae) {217kpg = KeyPairGenerator.getInstance(218test.keyAlg.name);219}220} else {221signature = Signature.getInstance(test.sigAlg.name);222kpg = KeyPairGenerator.getInstance(test.keyAlg.name);223}224if (test.sigParams != null) {225signature.setParameter(test.sigParams);226}227228for (int j=0; j < N; j++) {229if (test.keySize != -1) {230kpg.initialize(test.keySize);231}232KeyPair kp = kpg.genKeyPair();233KeyPair anotherKp = kpg.genKeyPair();234privKeys[j] = kp.getPrivate();235pubKeys[j] = kp.getPublic();236anotherPubKeys[j] = anotherKp.getPublic();237238if (Arrays.equals(pubKeys[j].getEncoded(),239anotherPubKeys[j].getEncoded())) {240System.out.println("Failed: it should not get "241+ "the same pair of public key");242return false;243}244}245246// Create a chain of signed objects247SignedObject[] objects = new SignedObject[N];248objects[0] = new SignedObject(str, privKeys[0], signature);249for (int j = 1; j < N; j++) {250objects[j] = new SignedObject(objects[j - 1], privKeys[j],251signature);252}253254// Verify the chain255int n = objects.length - 1;256SignedObject object = objects[n];257do {258if (!object.verify(pubKeys[n], signature)) {259System.out.println("Failed: verification failed, n = " + n);260return false;261}262if (object.verify(anotherPubKeys[n], signature)) {263System.out.println("Failed: verification should not "264+ "succeed with wrong public key, n = " + n);265return false;266}267268object = (SignedObject) object.getObject();269n--;270} while (n > 0);271272System.out.println("signed data: " + object.getObject());273if (!str.equals(object.getObject())) {274System.out.println("Failed: signed data is not equal to "275+ "original one");276return false;277}278279System.out.println("Test passed");280return true;281} catch (NoSuchProviderException nspe) {282if (test.provider == Provider.SunMSCAPI283&& !System.getProperty("os.name").startsWith("Windows")) {284System.out.println("SunMSCAPI is available only on Windows: "285+ nspe);286return true;287}288System.out.println("Unexpected exception: " + nspe);289return false;290} catch (Exception e) {291System.out.println("Unexpected exception: " + e);292e.printStackTrace(System.out);293return false;294}295}296}297298299300