Path: blob/master/test/jdk/sun/security/tools/keytool/GenerateAll.java
41152 views
/*1* Copyright (c) 2020, 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*/2223/*24* @test25* @bug 8242184 824206826* @summary keytool and jarsigner for all algorithms27* @library /test/lib28* @modules java.base/sun.security.util29* @run testng/timeout=300 GenerateAll30*/3132import jdk.test.lib.SecurityTools;33import jdk.test.lib.process.OutputAnalyzer;34import jdk.test.lib.security.DerUtils;35import jdk.test.lib.util.JarUtils;36import org.testng.annotations.AfterTest;37import org.testng.annotations.BeforeTest;38import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;40import static org.testng.Assert.*;4142import static sun.security.util.KnownOIDs.*;4344import sun.security.util.KnownOIDs;45import sun.security.util.ObjectIdentifier;46import sun.security.util.SignatureUtil;4748import java.io.File;49import java.io.IOException;50import java.nio.file.Files;51import java.nio.file.Path;52import java.security.KeyStore;53import java.security.PrivateKey;54import java.util.Base64;55import java.util.jar.JarEntry;56import java.util.jar.JarFile;57import java.util.stream.Collectors;5859public class GenerateAll {6061@BeforeTest62public void beforeTest() throws Exception {63// Create a CA in a separate keystore64kt("-genkeypair -alias ca -dname CN=CA -keyalg ec -ext bc -keystore ca");65kt("-export -alias ca -file ca.crt -rfc -keystore ca");6667// Import CA cert to user keystore so we can import reply later68kt("-import -alias root -file ca.crt -noprompt");6970JarUtils.createJarFile(Path.of("a.jar"), Path.of("."), Path.of("ks"));71}7273@DataProvider(name = "eddsa")74public Object[][] eddsaData() {75return new Object[][]{76{"eddsa", null, Ed25519},77{"eddsa", "eddsa", Ed25519},78{"eddsa", "ed25519", Ed25519},79{"eddsa", "ed448", null},80{"ed25519", null, Ed25519},81{"ed25519", "eddsa", Ed25519},82{"ed25519", "ed25519", Ed25519},83{"ed25519", "ed448", null},84{"ed448", null, Ed448},85{"ed448", "eddsa", Ed448},86{"ed448", "ed25519", null},87{"ed448", "ed448", Ed448},88};89}9091/**92* Test various names of EdDSA93* @param keyAlg keytool -keyalg94* @param sigAlg (optional) keytool -sigalg95* @param expected expected algorithm of generated signature96*/97@Test(dataProvider = "eddsa")98public void eddsaTest(String keyAlg, String sigAlg, KnownOIDs expected)99throws Exception {100String alias = keyAlg + "-" + sigAlg;101OutputAnalyzer oa = kt0("-genkeypair -alias " + alias102+ " -dname CN=" + alias + " -keyalg " + keyAlg103+ (sigAlg == null ? "" : (" -sigalg " + sigAlg)));104if (expected == null) {105oa.shouldNotHaveExitValue(0);106} else {107oa.shouldHaveExitValue(0);108kt("-alias " + alias + " -export -file " + alias + ".crt");109byte[] crt = Files.readAllBytes(Path.of(alias + ".crt"));110DerUtils.checkAlg(crt, "020", expected); // tbsCertificate.signature111DerUtils.checkAlg(crt, "0600", expected); // tbsCertificate.subjectPublicKeyInfo.algorithm112DerUtils.checkAlg(crt, "10", expected); // signatureAlgorithm113}114}115116@DataProvider(name = "all")117public Object[][] dataProvider() {118return new Object[][]{119{"rsa", "rsa", null, "RSA", SHA_256, SHA256withRSA},120{"dsa", "dsa", null, "DSA", SHA_256, SHA256withDSA},121{"r", "rsa", "rsassa-pss", "RSA", SHA_256, RSASSA_PSS},122{"pss", "rsassa-pss", null, "RSA", SHA_256, RSASSA_PSS},123{"ec", "ec", null, "EC", SHA_256, SHA256withECDSA},124{"ed25519", "ed25519", null, "EC", SHA_512, Ed25519},125{"ed448", "ed448", null, "EC", SHAKE256_LEN, Ed448},126};127}128129/**130* Testing all algorithms.131* @param alias alias132* @param keyAlg keytool -keyalg133* @param sigAlg (optional) keytool -sigalg134* @param ext block extension inside signed JAR135* @param expDigAlg expected digAlg in PKCS7 SignerInfo136* @param expEncAlg expected encAlg in PKCS7 SignerInfo137*/138@Test(dataProvider = "all")139public void test(String alias, String keyAlg, String sigAlg, String ext,140KnownOIDs expDigAlg, KnownOIDs expEncAlg) throws Throwable {141142char[] pass = "changeit".toCharArray();143144// If no sigAlg, derive automatically145String extra = sigAlg == null ? "" : (" -sigalg " + sigAlg);146147// gen148kt("-genkeypair -alias " + alias + " -dname CN=" + alias149+ " -keyalg " + keyAlg + extra);150kt("-export -alias " + alias + " -rfc -file " + alias + ".self");151152// req153kt("-certreq -alias " + alias + " -file " + alias + ".req" + extra);154kt("-printcertreq -file " + alias + ".req");155156// gencert157kt("-gencert -alias ca -infile " + alias158+ ".req -outfile " + alias + ".crt -rfc -keystore ca");159kt("-printcert -file " + alias + ".crt");160kt("-importcert -alias " + alias + " -file " + alias + ".crt");161162// crl163kt("-gencrl -alias " + alias + " -id 0 -rfc -file "164+ alias + ".crl" + extra);165kt("-printcrl -file " + alias + ".crl")166.shouldContain("Verified by " + alias);167168// sign169js("a.jar " + alias + extra);170171// check data172KeyStore ks = KeyStore.getInstance(new File("ks"), pass);173PrivateKey pk = (PrivateKey)ks.getKey(alias, pass);174175if (sigAlg == null) {176sigAlg = SignatureUtil.getDefaultSigAlgForKey(pk);177}178179KnownOIDs sigOID = KnownOIDs.findMatch(sigAlg);180KnownOIDs keyOID = KnownOIDs.findMatch(keyAlg);181182byte[] crt = read(alias + ".self");183DerUtils.checkAlg(crt, "020", sigOID); // tbsCertificate.signature184DerUtils.checkAlg(crt, "0600", keyOID); // tbsCertificate.subjectPublicKeyInfo.algorithm185assertEquals(186DerUtils.innerDerValue(crt, "02"), // tbsCertificate.signature187DerUtils.innerDerValue(crt, "1")); // signatureAlgorithm188189byte[] req = read(alias + ".req");190DerUtils.checkAlg(req, "10", sigOID); // signatureAlgorithm191DerUtils.checkAlg(req, "0200", keyOID); // certificationRequestInfo.subjectPKInfo.algorithm192193byte[] crl = read(alias + ".crl");194DerUtils.checkAlg(crl, "000", sigOID); // tbsCertList.signature195assertEquals(196DerUtils.innerDerValue(crl, "00"), // tbsCertList.signature197DerUtils.innerDerValue(crl, "1")); // signatureAlgorithm198199try (JarFile jf = new JarFile("a.jar")) {200JarEntry je = jf.getJarEntry(201"META-INF/" + alias.toUpperCase() + "." + ext);202byte[] p7 = jf.getInputStream(je).readAllBytes();203// SignerInfo.digestAlgorithm204DerUtils.checkAlg(p7, "104020", expDigAlg);205// SignerInfo.signatureAlgorithm206if (DerUtils.innerDerValue(p7, "10403").isContextSpecific()) {207// SignerInfo has signedAttributes at 104030208DerUtils.checkAlg(p7, "104040", expEncAlg);209} else {210DerUtils.checkAlg(p7, "104030", expEncAlg);211}212}213}214215@AfterTest216public void afterTest() throws Exception {217js("-verify a.jar -verbose -certs");218}219220static byte[] read(String f) throws IOException {221try (var v = Files.lines(Path.of(f))) {222return Base64.getDecoder().decode(v.filter(s -> !s.startsWith("-----"))223.collect(Collectors.joining("")));224}225}226227static OutputAnalyzer kt(String arg) throws Exception {228return kt0(arg).shouldHaveExitValue(0);229}230231static OutputAnalyzer kt0(String arg) throws Exception {232return SecurityTools.keytool("-keystore ks -storepass changeit " + arg);233}234235static OutputAnalyzer js(String arg) throws Exception {236return SecurityTools.jarsigner("-keystore ks -storepass changeit " + arg)237.shouldHaveExitValue(0);238}239}240241242