Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/mscapi/SignUsingNONEwithRSA.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 SignUsingNONEwithRSA.sh25*/2627import java.security.*;28import java.util.*;2930public class SignUsingNONEwithRSA {3132private static final List<byte[]> precomputedHashes = Arrays.asList(33// A MD5 hash34new byte[] {350x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,360x11, 0x12, 0x13, 0x14, 0x15, 0x1637},38// A SHA-1 hash39new byte[] {400x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,410x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x2042},43// A concatenation of SHA-1 and MD5 hashes (used during SSL handshake)44new byte[] {450x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,460x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,470x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,480x31, 0x32, 0x33, 0x34, 0x35, 0x3649},50// A SHA-256 hash51new byte[] {520x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,530x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,540x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,550x31, 0x3256},57// A SHA-384 hash58new byte[] {590x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,600x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,610x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,620x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,630x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x4864},65// A SHA-512 hash66new byte[] {670x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,680x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,690x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,700x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,710x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50,720x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60,730x61, 0x62, 0x63, 0x6474});7576private static List<byte[]> generatedSignatures = new ArrayList<>();7778public static void main(String[] args) throws Exception {7980Provider[] providers = Security.getProviders("Signature.NONEwithRSA");81if (providers == null) {82System.out.println("No JCE providers support the " +83"'Signature.NONEwithRSA' algorithm");84System.out.println("Skipping this test...");85return;8687} else {88System.out.println("The following JCE providers support the " +89"'Signature.NONEwithRSA' algorithm: ");90for (Provider provider : providers) {91System.out.println(" " + provider.getName());92}93}94System.out.println("-------------------------------------------------");9596KeyPair keys = getKeysFromKeyStore();97signAllUsing("SunMSCAPI", keys.getPrivate());98System.out.println("-------------------------------------------------");99100verifyAllUsing("SunMSCAPI", keys.getPublic());101System.out.println("-------------------------------------------------");102103verifyAllUsing("SunJCE", keys.getPublic());104System.out.println("-------------------------------------------------");105106keys = generateKeys();107signAllUsing("SunJCE", keys.getPrivate());108System.out.println("-------------------------------------------------");109110verifyAllUsing("SunMSCAPI", keys.getPublic());111System.out.println("-------------------------------------------------");112113}114115private static KeyPair getKeysFromKeyStore() throws Exception {116KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");117ks.load(null, null);118System.out.println("Loaded keystore: Windows-MY");119120Enumeration<String> e = ks.aliases();121PrivateKey privateKey = null;122PublicKey publicKey = null;123124while (e.hasMoreElements()) {125String alias = e.nextElement();126if (alias.equals("6578658")) {127System.out.println("Loaded entry: " + alias);128privateKey = (PrivateKey) ks.getKey(alias, null);129publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();130}131}132if (privateKey == null || publicKey == null) {133throw new Exception("Cannot load the keys need to run this test");134}135136return new KeyPair(publicKey, privateKey);137}138139140private static KeyPair generateKeys() throws Exception {141KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");142keyGen.initialize(1024, null);143KeyPair pair = keyGen.generateKeyPair();144PrivateKey privateKey = pair.getPrivate();145PublicKey publicKey = pair.getPublic();146147if (privateKey == null || publicKey == null) {148throw new Exception("Cannot load the keys need to run this test");149}150151return new KeyPair(publicKey, privateKey);152}153154private static void signAllUsing(String providerName, PrivateKey privateKey)155throws Exception {156Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);157if (sig1 == null) {158throw new Exception("'NONEwithRSA' is not supported");159}160if (sig1.getProvider() != null) {161System.out.println("Using NONEwithRSA signer from the " +162sig1.getProvider().getName() + " JCE provider");163} else {164System.out.println(165"Using NONEwithRSA signer from the internal JCE provider");166}167168System.out.println("Using key: " + privateKey);169generatedSignatures.clear();170for (byte[] hash : precomputedHashes) {171sig1.initSign(privateKey);172sig1.update(hash);173174try {175176byte [] sigBytes = sig1.sign();177System.out.println("\nGenerated RSA signature over a " +178hash.length + "-byte hash (signature length: " +179sigBytes.length * 8 + " bits)");180System.out.println(String.format("0x%0" +181(sigBytes.length * 2) + "x",182new java.math.BigInteger(1, sigBytes)));183generatedSignatures.add(sigBytes);184185} catch (SignatureException se) {186System.out.println("Error generating RSA signature: " + se);187}188}189}190191private static void verifyAllUsing(String providerName, PublicKey publicKey)192throws Exception {193Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);194if (sig1.getProvider() != null) {195System.out.println("\nUsing NONEwithRSA verifier from the " +196sig1.getProvider().getName() + " JCE provider");197} else {198System.out.println(199"\nUsing NONEwithRSA verifier from the internal JCE provider");200}201202System.out.println("Using key: " + publicKey);203204int i = 0;205for (byte[] hash : precomputedHashes) {206207byte[] sigBytes = generatedSignatures.get(i++);208System.out.println("\nVerifying RSA Signature over a " +209hash.length + "-byte hash (signature length: " +210sigBytes.length * 8 + " bits)");211System.out.println(String.format("0x%0" +212(sigBytes.length * 2) + "x",213new java.math.BigInteger(1, sigBytes)));214215sig1.initVerify(publicKey);216sig1.update(hash);217if (sig1.verify(sigBytes)) {218System.out.println("Verify PASSED");219} else {220throw new Exception("Verify FAILED");221}222}223}224}225226227