Path: blob/master/test/jdk/sun/security/mscapi/SignUsingNONEwithRSA.java
41149 views
/*1* Copyright (c) 2011, 2016, 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 657865826* @modules java.base/sun.security.x50927* java.base/sun.security.tools.keytool28* @requires os.family == "windows"29* @summary Sign using the NONEwithRSA signature algorithm from SunMSCAPI30*/3132import java.security.*;33import java.security.cert.X509Certificate;34import java.security.interfaces.RSAPrivateCrtKey;35import java.util.*;36import sun.security.tools.keytool.CertAndKeyGen;37import sun.security.x509.X500Name;3839public class SignUsingNONEwithRSA {4041private static final List<byte[]> precomputedHashes = Arrays.asList(42// A MD5 hash43new byte[] {440x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,450x11, 0x12, 0x13, 0x14, 0x15, 0x1646},47// A SHA-1 hash48new byte[] {490x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,500x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x2051},52// A concatenation of SHA-1 and MD5 hashes (used during SSL handshake)53new byte[] {540x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,550x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,560x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,570x31, 0x32, 0x33, 0x34, 0x35, 0x3658},59// A SHA-256 hash60new byte[] {610x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,620x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,630x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,640x31, 0x3265},66// A SHA-384 hash67new byte[] {680x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,690x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,700x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,710x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,720x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x4873},74// A SHA-512 hash75new byte[] {760x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,770x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,780x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,790x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,800x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50,810x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60,820x61, 0x62, 0x63, 0x6483});8485private static List<byte[]> generatedSignatures = new ArrayList<>();8687public static void main(String[] args) throws Exception {8889Provider[] providers = Security.getProviders("Signature.NONEwithRSA");90if (providers == null) {91System.out.println("No JCE providers support the " +92"'Signature.NONEwithRSA' algorithm");93System.out.println("Skipping this test...");94return;9596} else {97System.out.println("The following JCE providers support the " +98"'Signature.NONEwithRSA' algorithm: ");99for (Provider provider : providers) {100System.out.println(" " + provider.getName());101}102}103System.out.println(104"Creating a temporary RSA keypair in the Windows-My store");105KeyStore ks = KeyStore.getInstance("Windows-MY");106ks.load(null, null);107CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");108ckg.generate(1024);109RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();110ks.setKeyEntry("6578658", k, null, new X509Certificate[]{111ckg.getSelfCertificate(new X500Name("cn=6578658,c=US"), 1000)112});113ks.store(null, null);114115System.out.println("---------------------------------------------");116117try {118KeyPair keys = getKeysFromKeyStore();119signAllUsing("SunMSCAPI", keys.getPrivate());120System.out.println("---------------------------------------------");121122verifyAllUsing("SunMSCAPI", keys.getPublic());123System.out.println("---------------------------------------------");124125verifyAllUsing("SunJCE", keys.getPublic());126System.out.println("---------------------------------------------");127128keys = generateKeys();129signAllUsing("SunJCE", keys.getPrivate());130System.out.println("---------------------------------------------");131132verifyAllUsing("SunMSCAPI", keys.getPublic());133System.out.println("---------------------------------------------");134} finally {135System.out.println(136"Deleting temporary RSA keypair from Windows-My store");137ks.deleteEntry("6578658");138}139140}141142private static KeyPair getKeysFromKeyStore() throws Exception {143KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");144ks.load(null, null);145System.out.println("Loaded keystore: Windows-MY");146147Enumeration<String> e = ks.aliases();148PrivateKey privateKey = null;149PublicKey publicKey = null;150151while (e.hasMoreElements()) {152String alias = e.nextElement();153if (alias.equals("6578658")) {154System.out.println("Loaded entry: " + alias);155privateKey = (PrivateKey) ks.getKey(alias, null);156publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();157}158}159if (privateKey == null || publicKey == null) {160throw new Exception("Cannot load the keys need to run this test");161}162163return new KeyPair(publicKey, privateKey);164}165166167private static KeyPair generateKeys() throws Exception {168KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");169keyGen.initialize(1024, null);170KeyPair pair = keyGen.generateKeyPair();171PrivateKey privateKey = pair.getPrivate();172PublicKey publicKey = pair.getPublic();173174if (privateKey == null || publicKey == null) {175throw new Exception("Cannot load the keys need to run this test");176}177178return new KeyPair(publicKey, privateKey);179}180181private static void signAllUsing(String providerName, PrivateKey privateKey)182throws Exception {183Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);184if (sig1 == null) {185throw new Exception("'NONEwithRSA' is not supported");186}187if (sig1.getProvider() != null) {188System.out.println("Using NONEwithRSA signer from the " +189sig1.getProvider().getName() + " JCE provider");190} else {191System.out.println(192"Using NONEwithRSA signer from the internal JCE provider");193}194195System.out.println("Using key: " + privateKey);196generatedSignatures.clear();197for (byte[] hash : precomputedHashes) {198sig1.initSign(privateKey);199sig1.update(hash);200201try {202203byte [] sigBytes = sig1.sign();204System.out.println("\nGenerated RSA signature over a " +205hash.length + "-byte hash (signature length: " +206sigBytes.length * 8 + " bits)");207System.out.println(String.format("0x%0" +208(sigBytes.length * 2) + "x",209new java.math.BigInteger(1, sigBytes)));210generatedSignatures.add(sigBytes);211212} catch (SignatureException se) {213System.out.println("Error generating RSA signature: " + se);214}215}216}217218private static void verifyAllUsing(String providerName, PublicKey publicKey)219throws Exception {220Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);221if (sig1.getProvider() != null) {222System.out.println("\nUsing NONEwithRSA verifier from the " +223sig1.getProvider().getName() + " JCE provider");224} else {225System.out.println(226"\nUsing NONEwithRSA verifier from the internal JCE provider");227}228229System.out.println("Using key: " + publicKey);230231int i = 0;232for (byte[] hash : precomputedHashes) {233234byte[] sigBytes = generatedSignatures.get(i++);235System.out.println("\nVerifying RSA Signature over a " +236hash.length + "-byte hash (signature length: " +237sigBytes.length * 8 + " bits)");238System.out.println(String.format("0x%0" +239(sigBytes.length * 2) + "x",240new java.math.BigInteger(1, sigBytes)));241242sig1.initVerify(publicKey);243sig1.update(hash);244if (sig1.verify(sigBytes)) {245System.out.println("Verify PASSED");246} else {247throw new Exception("Verify FAILED");248}249}250}251}252253254