Path: blob/master/test/jdk/java/security/Signature/SignatureGetInstance.java
41149 views
/*1* Copyright (c) 2019, 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* @modules java.base/sun.security.util29* @run main/othervm SignatureGetInstance30*/31import java.security.*;32import java.security.interfaces.*;33import java.security.spec.*;34import sun.security.util.SignatureUtil;3536public class SignatureGetInstance {3738private static final String SIGALG = "RSASSA-PSS";3940public static void main(String[] args) throws Exception {41Provider testProvider = new TestProvider();42// put test provider before SunRsaSign provider43Security.insertProviderAt(testProvider, 1);44//Security.addProvider(testProvider);45KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");46KeyPair kp = kpg.generateKeyPair();4748MyPrivKey testPriv = new MyPrivKey();49MyPubKey testPub = new MyPubKey();5051testDblInit(testPriv, testPub, true, "TestProvider");52testDblInit(kp.getPrivate(), kp.getPublic(), true, "SunRsaSign");53testDblInit(testPriv, kp.getPublic(), false, null);54testDblInit(kp.getPrivate(), testPub, false, null);5556testSetAndInit(null, testPriv, true);57testSetAndInit(null, testPub, true);58testSetAndInit(null, kp.getPrivate(), true);59testSetAndInit(null, kp.getPublic(), true);6061String provName = "SunRsaSign";62testSetAndInit(provName, testPriv, false);63testSetAndInit(provName, testPub, false);64testSetAndInit(provName, kp.getPrivate(), true);65testSetAndInit(provName, kp.getPublic(), true);6667provName = "TestProvider";68testSetAndInit(provName, testPriv, true);69testSetAndInit(provName, testPub, true);70testSetAndInit(provName, kp.getPrivate(), false);71testSetAndInit(provName, kp.getPublic(), false);7273System.out.println("Test Passed");74}7576private static void checkName(Signature s, String name) {77if (name != null &&78!(name.equals(s.getProvider().getName()))) {79throw new RuntimeException("Fail: provider name mismatch");80}81}8283private static void testDblInit(PrivateKey key1, PublicKey key2,84boolean shouldPass, String expectedProvName) throws Exception {85Signature sig = Signature.getInstance(SIGALG);86SignatureUtil.initSignWithParam(sig, key1, PSSParameterSpec.DEFAULT, null);87try {88sig.initVerify(key2);89if (!shouldPass) {90throw new RuntimeException("Fail: should throw InvalidKeyException");91}92checkName(sig, expectedProvName);93} catch (InvalidKeyException ike) {94if (shouldPass) {95System.out.println("Fail: Unexpected InvalidKeyException");96throw ike;97}98}99}100101private static void testSetAndInit(String provName, Key key,102boolean shouldPass) throws Exception {103Signature sig;104if (provName == null) {105sig = Signature.getInstance(SIGALG);106} else {107sig = Signature.getInstance(SIGALG, provName);108}109AlgorithmParameterSpec params = PSSParameterSpec.DEFAULT;110boolean doSign = (key instanceof PrivateKey);111try {112if (doSign) {113SignatureUtil.initSignWithParam(sig, (PrivateKey)key, params, null);114} else {115SignatureUtil.initVerifyWithParam(sig, (PublicKey)key, params);116}117if (!shouldPass) {118throw new RuntimeException("Fail: should throw InvalidKeyException");119}120checkName(sig, provName);121// check that the earlier parameter is still there122if (sig.getParameters() == null) {123throw new RuntimeException("Fail: parameters not preserved");124}125} catch (InvalidKeyException ike) {126if (shouldPass) {127System.out.println("Fail: Unexpected InvalidKeyException");128throw ike;129}130}131}132133// Test provider which only accepts its own Key objects134// Registered to be more preferred than SunRsaSign provider135// for testing deferred provider selection136public static class TestProvider extends Provider {137TestProvider() {138super("TestProvider", "1.0", "provider for SignatureGetInstance");139put("Signature.RSASSA-PSS",140"SignatureGetInstance$MySigImpl");141}142}143144public static class MyPrivKey implements PrivateKey {145public String getAlgorithm() { return "RSASSA-PSS"; }146public String getFormat() { return "MyOwn"; }147public byte[] getEncoded() { return null; }148}149150public static class MyPubKey implements PublicKey {151public String getAlgorithm() { return "RSASSA-PSS"; }152public String getFormat() { return "MyOwn"; }153public byte[] getEncoded() { return null; }154}155156public static class MySigImpl extends SignatureSpi {157// simulate BC behavior of only using params set before init calls158AlgorithmParameterSpec initParamSpec = null;159AlgorithmParameterSpec paramSpec = null;160161public MySigImpl() {162super();163}164165@Override166protected void engineInitVerify(PublicKey publicKey)167throws InvalidKeyException {168if (!(publicKey instanceof MyPubKey)) {169throw new InvalidKeyException("Must be MyPubKey");170}171initParamSpec = paramSpec;172}173174@Override175protected void engineInitSign(PrivateKey privateKey)176throws InvalidKeyException {177if (!(privateKey instanceof MyPrivKey)) {178throw new InvalidKeyException("Must be MyPrivKey");179}180initParamSpec = paramSpec;181}182183@Override184protected void engineUpdate(byte b) throws SignatureException {185}186187@Override188protected void engineUpdate(byte[] b, int off, int len)189throws SignatureException {190}191192@Override193protected byte[] engineSign()194throws SignatureException {195return new byte[0];196}197198@Override199protected boolean engineVerify(byte[] sigBytes)200throws SignatureException {201return false;202}203204@Override205@Deprecated206protected void engineSetParameter(String param, Object value)207throws InvalidParameterException {208}209210@Override211protected void engineSetParameter(AlgorithmParameterSpec params)212throws InvalidAlgorithmParameterException {213paramSpec = params;214}215216@Override217@Deprecated218protected AlgorithmParameters engineGetParameter(String param)219throws InvalidParameterException {220return null;221}222223@Override224protected AlgorithmParameters engineGetParameters() {225if (initParamSpec != null) {226try {227AlgorithmParameters ap =228AlgorithmParameters.getInstance("RSASSA-PSS");229ap.init(initParamSpec);230return ap;231} catch (Exception e) {232throw new RuntimeException(e);233}234}235return null;236}237}238}239240241