Path: blob/master/test/jdk/sun/security/x509/AlgorithmId/OmitAlgIdParam.java
41153 views
/*1* Copyright (c) 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*/2223/*24* @test25* @bug 825237726* @library /test/lib27* @modules java.base/sun.security.util28* java.base/sun.security.x50929* @summary The AlgorithmIdentifier for ECDSA should omit the parameters field30*/3132import jdk.test.lib.Asserts;33import jdk.test.lib.SecurityTools;34import jdk.test.lib.process.OutputAnalyzer;35import static jdk.test.lib.security.DerUtils.*;3637import java.io.File;38import java.security.KeyStore;39import java.security.cert.X509Certificate;40import sun.security.util.*;4142public class OmitAlgIdParam {4344public static void main(String[] args) throws Exception {45keytool("-genkeypair -keyalg ec -dname CN=EC1 -alias ecsha224 "46+ "-sigalg SHA224withECDSA -keystore ks -storepass changeit");4748keytool("-genkeypair -keyalg ec -dname CN=EC2 -alias ecsha256 "49+ "-sigalg SHA256withECDSA -keystore ks -storepass changeit");5051keytool("-genkeypair -keyalg ec -dname CN=EC3 -alias ecsha384 "52+ "-sigalg SHA384withECDSA -keystore ks -storepass changeit");5354keytool("-genkeypair -keyalg ec -dname CN=EC4 -alias ecsha512 "55+ "-sigalg SHA512withECDSA -keystore ks -storepass changeit");5657KeyStore kstore = KeyStore.getInstance(58new File("ks"), "changeit".toCharArray());5960// SHA224withECDSA61checkAlgId(kstore, "ecsha224", "SHA224withECDSA",62ObjectIdentifier.of(KnownOIDs.SHA224withECDSA));6364// SHA256withECDSA65checkAlgId(kstore, "ecsha256", "SHA256withECDSA",66ObjectIdentifier.of(KnownOIDs.SHA256withECDSA));6768// SHA384withECDSA69checkAlgId(kstore, "ecsha384", "SHA384withECDSA",70ObjectIdentifier.of(KnownOIDs.SHA384withECDSA));7172// SHA512withECDSA73checkAlgId(kstore, "ecsha512", "SHA512withECDSA",74ObjectIdentifier.of(KnownOIDs.SHA512withECDSA));75}7677private static void checkAlgId(KeyStore ks, String alias, String alg,78ObjectIdentifier oid) throws Exception {79X509Certificate cert = (X509Certificate)ks.getCertificate(alias);80System.out.println("SigAlgName = " + cert.getSigAlgName());8182Asserts.assertEQ(cert.getPublicKey().getAlgorithm(), "EC");83Asserts.assertEQ(cert.getSigAlgName(), alg);8485byte[] data = cert.getEncoded();86// Parameters field in the specified AlgorithmIdentifier should be omitted87// Checking the first signature AlgorithmIdentifier in the cert88checkAlg(data, "020", oid);89shouldNotExist(data, "021");90// Checking the second signature AlgorithmIdentifier in the cert91checkAlg(data, "10", oid);92shouldNotExist(data, "11");93}9495static OutputAnalyzer keytool(String cmd) throws Exception {96return SecurityTools.keytool(cmd).shouldHaveExitValue(0);97}98}99100101