Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/Signature/SignatureGetInstance.java
38812 views
/*1* Copyright (c) 2019, 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 821603926* @summary Ensure the BC provider-reselection workaround in Signature class27* functions correctly28* @run main/othervm SignatureGetInstance29*/30import java.security.*;31import java.security.interfaces.*;32import java.security.spec.*;33import sun.security.util.SignatureUtil;3435public class SignatureGetInstance {3637private static final String SIGALG = "RSASSA-PSS";3839public static void main(String[] args) throws Exception {40Provider testProvider = new TestProvider();41// put test provider before SunRsaSign provider42Security.insertProviderAt(testProvider, 1);43//Security.addProvider(testProvider);44KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");45KeyPair kp = kpg.generateKeyPair();4647MyPrivKey testPriv = new MyPrivKey();48MyPubKey testPub = new MyPubKey();4950testDblInit(testPriv, testPub, true, "TestProvider");51testDblInit(kp.getPrivate(), kp.getPublic(), true, "SunRsaSign");52testDblInit(testPriv, kp.getPublic(), false, null);53testDblInit(kp.getPrivate(), testPub, false, null);5455testSetAndInit(null, testPriv, true);56testSetAndInit(null, testPub, true);57testSetAndInit(null, kp.getPrivate(), true);58testSetAndInit(null, kp.getPublic(), true);5960String provName = "SunRsaSign";61testSetAndInit(provName, testPriv, false);62testSetAndInit(provName, testPub, false);63testSetAndInit(provName, kp.getPrivate(), true);64testSetAndInit(provName, kp.getPublic(), true);6566provName = "TestProvider";67testSetAndInit(provName, testPriv, true);68testSetAndInit(provName, testPub, true);69testSetAndInit(provName, kp.getPrivate(), false);70testSetAndInit(provName, kp.getPublic(), false);7172System.out.println("Test Passed");73}7475private static void checkName(Signature s, String name) {76if (name != null &&77!(name.equals(s.getProvider().getName()))) {78throw new RuntimeException("Fail: provider name mismatch");79}80}8182private static void testDblInit(PrivateKey key1, PublicKey key2,83boolean shouldPass, String expectedProvName) throws Exception {84Signature sig = Signature.getInstance(SIGALG);85SignatureUtil.initSignWithParam(sig, key1, PSSParameterSpec.DEFAULT, null);86try {87sig.initVerify(key2);88if (!shouldPass) {89throw new RuntimeException("Fail: should throw InvalidKeyException");90}91checkName(sig, expectedProvName);92} catch (InvalidKeyException ike) {93if (shouldPass) {94System.out.println("Fail: Unexpected InvalidKeyException");95throw ike;96}97}98}99100private static void testSetAndInit(String provName, Key key,101boolean shouldPass) throws Exception {102Signature sig;103if (provName == null) {104sig = Signature.getInstance(SIGALG);105} else {106sig = Signature.getInstance(SIGALG, provName);107}108AlgorithmParameterSpec params = PSSParameterSpec.DEFAULT;109boolean doSign = (key instanceof PrivateKey);110try {111if (doSign) {112SignatureUtil.initSignWithParam(sig, (PrivateKey)key, params, null);113} else {114SignatureUtil.initVerifyWithParam(sig, (PublicKey)key, params);115}116if (!shouldPass) {117throw new RuntimeException("Fail: should throw InvalidKeyException");118}119checkName(sig, provName);120// check that the earlier parameter is still there121if (sig.getParameters() == null) {122throw new RuntimeException("Fail: parameters not preserved");123}124} catch (InvalidKeyException ike) {125if (shouldPass) {126System.out.println("Fail: Unexpected InvalidKeyException");127throw ike;128}129}130}131132// Test provider which only accepts its own Key objects133// Registered to be more preferred than SunRsaSign provider134// for testing deferred provider selection135public static class TestProvider extends Provider {136TestProvider() {137super("TestProvider", 1.0d, "provider for SignatureGetInstance");138put("Signature.RSASSA-PSS",139"SignatureGetInstance$MySigImpl");140}141}142143public static class MyPrivKey implements PrivateKey {144public String getAlgorithm() { return "RSASSA-PSS"; }145public String getFormat() { return "MyOwn"; }146public byte[] getEncoded() { return null; }147}148149public static class MyPubKey implements PublicKey {150public String getAlgorithm() { return "RSASSA-PSS"; }151public String getFormat() { return "MyOwn"; }152public byte[] getEncoded() { return null; }153}154155public static class MySigImpl extends SignatureSpi {156// simulate BC behavior of only using params set before init calls157AlgorithmParameterSpec initParamSpec = null;158AlgorithmParameterSpec paramSpec = null;159160public MySigImpl() {161super();162}163164@Override165protected void engineInitVerify(PublicKey publicKey)166throws InvalidKeyException {167if (!(publicKey instanceof MyPubKey)) {168throw new InvalidKeyException("Must be MyPubKey");169}170initParamSpec = paramSpec;171}172173@Override174protected void engineInitSign(PrivateKey privateKey)175throws InvalidKeyException {176if (!(privateKey instanceof MyPrivKey)) {177throw new InvalidKeyException("Must be MyPrivKey");178}179initParamSpec = paramSpec;180}181182@Override183protected void engineUpdate(byte b) throws SignatureException {184}185186@Override187protected void engineUpdate(byte[] b, int off, int len)188throws SignatureException {189}190191@Override192protected byte[] engineSign()193throws SignatureException {194return new byte[0];195}196197@Override198protected boolean engineVerify(byte[] sigBytes)199throws SignatureException {200return false;201}202203@Override204@Deprecated205protected void engineSetParameter(String param, Object value)206throws InvalidParameterException {207}208209@Override210protected void engineSetParameter(AlgorithmParameterSpec params)211throws InvalidAlgorithmParameterException {212paramSpec = params;213}214215@Override216@Deprecated217protected AlgorithmParameters engineGetParameter(String param)218throws InvalidParameterException {219return null;220}221222@Override223protected AlgorithmParameters engineGetParameters() {224if (initParamSpec != null) {225try {226AlgorithmParameters ap =227AlgorithmParameters.getInstance("RSASSA-PSS");228ap.init(initParamSpec);229return ap;230} catch (Exception e) {231throw new RuntimeException(e);232}233}234return null;235}236}237}238239240