Path: blob/master/test/jdk/sun/security/mscapi/InteropWithSunRsaSign.java
41149 views
/*1* Copyright (c) 2018, 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*/222324/**25* @test26* @bug 820544527* @summary Interop test between SunMSCAPI and SunRsaSign on RSASSA-PSS28* @requires os.family == "windows"29*/3031import java.security.KeyPair;32import java.security.KeyPairGenerator;33import java.security.PrivateKey;34import java.security.PublicKey;35import java.security.SecureRandom;36import java.security.Signature;37import java.security.spec.MGF1ParameterSpec;38import java.security.spec.PSSParameterSpec;39import java.util.Random;4041public class InteropWithSunRsaSign {4243private static final SecureRandom NOT_SECURE_RANDOM = new SecureRandom() {44Random r = new Random();45@Override46public void nextBytes(byte[] bytes) {47r.nextBytes(bytes);48}49};5051private static boolean allResult = true;52private static byte[] msg = "hello".getBytes();5354public static void main(String[] args) throws Exception {5556matrix(new PSSParameterSpec(57"SHA-1",58"MGF1",59MGF1ParameterSpec.SHA1,6020,61PSSParameterSpec.TRAILER_FIELD_BC));6263matrix(new PSSParameterSpec(64"SHA-256",65"MGF1",66MGF1ParameterSpec.SHA256,6732,68PSSParameterSpec.TRAILER_FIELD_BC));6970matrix(new PSSParameterSpec(71"SHA-384",72"MGF1",73MGF1ParameterSpec.SHA384,7448,75PSSParameterSpec.TRAILER_FIELD_BC));7677matrix(new PSSParameterSpec(78"SHA-512",79"MGF1",80MGF1ParameterSpec.SHA512,8164,82PSSParameterSpec.TRAILER_FIELD_BC));8384// non-typical salt length85matrix(new PSSParameterSpec(86"SHA-1",87"MGF1",88MGF1ParameterSpec.SHA1,8917,90PSSParameterSpec.TRAILER_FIELD_BC));9192if (!allResult) {93throw new Exception("Failed");94}95}9697static void matrix(PSSParameterSpec pss) throws Exception {9899System.out.printf("\n%10s%20s%20s%20s %s\n", pss.getDigestAlgorithm(),100"KeyPairGenerator", "signer", "verifier", "result");101System.out.printf("%10s%20s%20s%20s %s\n",102"-------", "----------------", "------", "--------", "------");103104// KeyPairGenerator chooses SPI when getInstance() is called.105String[] provsForKPG = {"SunRsaSign", "SunMSCAPI"};106107// "-" means no preferred provider. In this case, SPI is chosen108// when initSign/initVerify is called. Worth testing.109String[] provsForSignature = {"SunRsaSign", "SunMSCAPI", "-"};110111int pos = 0;112for (String pg : provsForKPG) {113for (String ps : provsForSignature) {114for (String pv : provsForSignature) {115System.out.printf("%10d%20s%20s%20s ", ++pos, pg, ps, pv);116try {117boolean result = test(pg, ps, pv, pss);118System.out.println(result);119if (!result) {120allResult = false;121}122} catch (Exception e) {123if (pg.equals("-") || pg.equals(ps)) {124// When Signature provider is automatically125// chosen or the same with KeyPairGenerator,126// this is an error.127allResult = false;128System.out.println("X " + e.getMessage());129} else {130// Known restriction: SunRsaSign and SunMSCAPI can't131// use each other's private key for signing.132System.out.println(e.getMessage());133}134}135}136}137}138}139140static boolean test(String pg, String ps, String pv, PSSParameterSpec pss)141throws Exception {142143KeyPairGenerator kpg = pg.length() == 1144? KeyPairGenerator.getInstance("RSA")145:KeyPairGenerator.getInstance("RSA", pg);146kpg.initialize(147pss.getDigestAlgorithm().equals("SHA-512") ? 2048: 1024,148NOT_SECURE_RANDOM);149KeyPair kp = kpg.generateKeyPair();150PrivateKey pr = kp.getPrivate();151PublicKey pu = kp.getPublic();152153Signature s = ps.length() == 1154? Signature.getInstance("RSASSA-PSS")155: Signature.getInstance("RSASSA-PSS", ps);156s.initSign(pr);157s.setParameter(pss);158s.update(msg);159byte[] sig = s.sign();160161Signature s2 = pv.length() == 1162? Signature.getInstance("RSASSA-PSS")163: Signature.getInstance("RSASSA-PSS", pv);164s2.initVerify(pu);165s2.setParameter(pss);166s2.update(msg);167168return s2.verify(sig);169}170}171172173