Path: blob/master/test/jdk/sun/security/mscapi/SignUsingSHA2withRSA.java
41152 views
/*1* Copyright (c) 2011, 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*/2223/**24* @test25* @bug 6753664 818057026* @summary Support SHA256 (and higher) in SunMSCAPI27* @requires os.family == "windows"28* @modules java.base/sun.security.tools.keytool29* java.base/sun.security.x50930*/3132import sun.security.tools.keytool.CertAndKeyGen;33import sun.security.x509.X500Name;3435import java.security.*;36import java.security.cert.Certificate;37import java.util.*;3839public class SignUsingSHA2withRSA {4041private static final byte[] toBeSigned = new byte[] {420x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x1043};4445private static List<byte[]> generatedSignatures = new ArrayList<>();4647public static void main(String[] args) throws Exception {48KeyStore ks = KeyStore.getInstance("Windows-MY");49ks.load(null, null);50if (ks.containsAlias("6753664")) {51ks.deleteEntry("6753664");52}5354CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");55gen.generate(2048);5657ks.setKeyEntry("6753664", gen.getPrivateKey(), null,58new Certificate[] {59gen.getSelfCertificate(new X500Name("cn=localhost,c=US"), 100)60});6162try {63run();64} finally {65ks.deleteEntry("6753664");66ks.store(null, null);67}68}6970static void run() throws Exception {7172Provider[] providers = Security.getProviders("Signature.SHA256withRSA");73if (providers == null) {74System.out.println("No JCE providers support the " +75"'Signature.SHA256withRSA' algorithm");76System.out.println("Skipping this test...");77return;7879} else {80System.out.println("The following JCE providers support the " +81"'Signature.SHA256withRSA' algorithm: ");82for (Provider provider : providers) {83System.out.println(" " + provider.getName());84}85}86System.out.println("-------------------------------------------------");8788KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");89ks.load(null, null);90System.out.println("Loaded keystore: Windows-MY");9192Enumeration<String> e = ks.aliases();93PrivateKey privateKey = null;94PublicKey publicKey = null;9596while (e.hasMoreElements()) {97String alias = e.nextElement();98if (alias.equals("6753664")) {99System.out.println("Loaded entry: " + alias);100privateKey = (PrivateKey) ks.getKey(alias, null);101publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();102}103}104if (privateKey == null || publicKey == null) {105throw new Exception("Cannot load the keys need to run this test");106}107System.out.println("-------------------------------------------------");108109generatedSignatures.add(signUsing("SHA256withRSA", privateKey));110generatedSignatures.add(signUsing("SHA384withRSA", privateKey));111generatedSignatures.add(signUsing("SHA512withRSA", privateKey));112113System.out.println("-------------------------------------------------");114115verifyUsing("SHA256withRSA", publicKey, generatedSignatures.get(0));116verifyUsing("SHA384withRSA", publicKey, generatedSignatures.get(1));117verifyUsing("SHA512withRSA", publicKey, generatedSignatures.get(2));118119System.out.println("-------------------------------------------------");120}121122private static byte[] signUsing(String signAlgorithm,123PrivateKey privateKey) throws Exception {124125// Must explicitly specify the SunMSCAPI JCE provider126// (otherwise SunJCE is chosen because it appears earlier in the list)127Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");128if (sig1 == null) {129throw new Exception("'" + signAlgorithm + "' is not supported");130}131System.out.println("Using " + signAlgorithm + " signer from the " +132sig1.getProvider().getName() + " JCE provider");133134System.out.println("Using key: " + privateKey);135sig1.initSign(privateKey);136sig1.update(toBeSigned);137byte [] sigBytes = null;138139try {140sigBytes = sig1.sign();141System.out.println("Generated RSA signature over a " +142toBeSigned.length + "-byte data (signature length: " +143sigBytes.length * 8 + " bits)");144System.out.println(String.format("0x%0" +145(sigBytes.length * 2) + "x",146new java.math.BigInteger(1, sigBytes)));147148} catch (SignatureException se) {149System.out.println("Error generating RSA signature: " + se);150}151152return sigBytes;153}154155private static void verifyUsing(String signAlgorithm, PublicKey publicKey,156byte[] signature) throws Exception {157158// Must explicitly specify the SunMSCAPI JCE provider159// (otherwise SunJCE is chosen because it appears earlier in the list)160Signature sig1 = Signature.getInstance(signAlgorithm, "SunMSCAPI");161if (sig1 == null) {162throw new Exception("'" + signAlgorithm + "' is not supported");163}164System.out.println("Using " + signAlgorithm + " verifier from the "165+ sig1.getProvider().getName() + " JCE provider");166167System.out.println("Using key: " + publicKey);168169System.out.println("\nVerifying RSA Signature over a " +170toBeSigned.length + "-byte data (signature length: " +171signature.length * 8 + " bits)");172System.out.println(String.format("0x%0" + (signature.length * 2) +173"x", new java.math.BigInteger(1, signature)));174175sig1.initVerify(publicKey);176sig1.update(toBeSigned);177178if (sig1.verify(signature)) {179System.out.println("Verify PASSED\n");180} else {181throw new Exception("Verify FAILED");182}183}184}185186187