Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/SignUsingSHA2withRSA.java
38840 views
/*1* Copyright (c) 2011, 2015, 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* @see SignUsingSHA2withRSA.sh25*/2627import java.security.*;28import java.util.*;2930public class SignUsingSHA2withRSA {3132private static final byte[] toBeSigned = new byte[] {330x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x1034};3536private static List<byte[]> generatedSignatures = new ArrayList<>();3738public static void main(String[] args) throws Exception {3940Provider[] providers = Security.getProviders("Signature.SHA256withRSA");41if (providers == null) {42System.out.println("No JCE providers support the " +43"'Signature.SHA256withRSA' algorithm");44System.out.println("Skipping this test...");45return;4647} else {48System.out.println("The following JCE providers support the " +49"'Signature.SHA256withRSA' algorithm: ");50for (Provider provider : providers) {51System.out.println(" " + provider.getName());52}53}54System.out.println("-------------------------------------------------");5556KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");57ks.load(null, null);58System.out.println("Loaded keystore: Windows-MY");5960Enumeration<String> e = ks.aliases();61PrivateKey privateKey = null;62PublicKey publicKey = null;6364while (e.hasMoreElements()) {65String alias = e.nextElement();66if (alias.equals("6753664")) {67System.out.println("Loaded entry: " + alias);68privateKey = (PrivateKey) ks.getKey(alias, null);69publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();70}71}72if (privateKey == null || publicKey == null) {73throw new Exception("Cannot load the keys need to run this test");74}75System.out.println("-------------------------------------------------");7677generatedSignatures.add(signUsing("SHA256withRSA", privateKey));78generatedSignatures.add(signUsing("SHA384withRSA", privateKey));79generatedSignatures.add(signUsing("SHA512withRSA", privateKey));8081System.out.println("-------------------------------------------------");8283verifyUsing("SHA256withRSA", publicKey, generatedSignatures.get(0));84verifyUsing("SHA384withRSA", publicKey, generatedSignatures.get(1));85verifyUsing("SHA512withRSA", publicKey, generatedSignatures.get(2));8687System.out.println("-------------------------------------------------");88}8990private static byte[] signUsing(String signAlgorithm,91PrivateKey privateKey) throws Exception {9293// Must explicitly specify the SunMSCAPI JCE provider94// (otherwise SunJCE is chosen because it appears earlier in the list)95Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");96if (sig1 == null) {97throw new Exception("'" + signAlgorithm + "' is not supported");98}99System.out.println("Using " + signAlgorithm + " signer from the " +100sig1.getProvider().getName() + " JCE provider");101102System.out.println("Using key: " + privateKey);103sig1.initSign(privateKey);104sig1.update(toBeSigned);105byte [] sigBytes = null;106107try {108sigBytes = sig1.sign();109System.out.println("Generated RSA signature over a " +110toBeSigned.length + "-byte data (signature length: " +111sigBytes.length * 8 + " bits)");112System.out.println(String.format("0x%0" +113(sigBytes.length * 2) + "x",114new java.math.BigInteger(1, sigBytes)));115116} catch (SignatureException se) {117System.out.println("Error generating RSA signature: " + se);118}119120return sigBytes;121}122123private static void verifyUsing(String signAlgorithm, PublicKey publicKey,124byte[] signature) throws Exception {125126// Must explicitly specify the SunMSCAPI JCE provider127// (otherwise SunJCE is chosen because it appears earlier in the list)128Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");129if (sig1 == null) {130throw new Exception("'" + signAlgorithm + "' is not supported");131}132System.out.println("Using " + signAlgorithm + " verifier from the "133+ sig1.getProvider().getName() + " JCE provider");134135System.out.println("Using key: " + publicKey);136137System.out.println("\nVerifying RSA Signature over a " +138toBeSigned.length + "-byte data (signature length: " +139signature.length * 8 + " bits)");140System.out.println(String.format("0x%0" + (signature.length * 2) +141"x", new java.math.BigInteger(1, signature)));142143sig1.initVerify(publicKey);144sig1.update(toBeSigned);145146if (sig1.verify(signature)) {147System.out.println("Verify PASSED\n");148} else {149throw new Exception("Verify FAILED");150}151}152}153154155