test/jdk/sun/security/mscapi/SignUsingNONEwithRSA.java
changeset 47216 71c04702a3d5
parent 39508 b0c063e32440
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2011, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24  /*
       
    25  * @test
       
    26  * @bug 6578658
       
    27  * @modules java.base/sun.security.x509
       
    28  *          java.base/sun.security.tools.keytool
       
    29  * @requires os.family == "windows"
       
    30  * @summary Sign using the NONEwithRSA signature algorithm from SunMSCAPI
       
    31  */
       
    32 
       
    33 import java.security.*;
       
    34 import java.security.cert.X509Certificate;
       
    35 import java.security.interfaces.RSAPrivateCrtKey;
       
    36 import java.util.*;
       
    37 import sun.security.tools.keytool.CertAndKeyGen;
       
    38 import sun.security.x509.X500Name;
       
    39 
       
    40 public class SignUsingNONEwithRSA {
       
    41 
       
    42     private static final List<byte[]> precomputedHashes = Arrays.asList(
       
    43         // A MD5 hash
       
    44         new byte[] {
       
    45             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    46             0x11, 0x12, 0x13, 0x14, 0x15, 0x16
       
    47         },
       
    48         // A SHA-1 hash
       
    49         new byte[] {
       
    50             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    51             0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20
       
    52         },
       
    53         // A concatenation of SHA-1 and MD5 hashes (used during SSL handshake)
       
    54         new byte[] {
       
    55             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    56             0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
       
    57             0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
       
    58             0x31, 0x32, 0x33, 0x34, 0x35, 0x36
       
    59         },
       
    60         // A SHA-256 hash
       
    61         new byte[] {
       
    62             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    63             0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
       
    64             0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
       
    65             0x31, 0x32
       
    66         },
       
    67         // A SHA-384 hash
       
    68         new byte[] {
       
    69             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    70             0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
       
    71             0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
       
    72             0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,
       
    73             0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48
       
    74         },
       
    75         // A SHA-512 hash
       
    76         new byte[] {
       
    77             0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
       
    78             0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20,
       
    79             0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30,
       
    80             0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40,
       
    81             0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50,
       
    82             0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60,
       
    83             0x61, 0x62, 0x63, 0x64
       
    84         });
       
    85 
       
    86     private static List<byte[]> generatedSignatures = new ArrayList<>();
       
    87 
       
    88     public static void main(String[] args) throws Exception {
       
    89 
       
    90         Provider[] providers = Security.getProviders("Signature.NONEwithRSA");
       
    91         if (providers == null) {
       
    92             System.out.println("No JCE providers support the " +
       
    93                 "'Signature.NONEwithRSA' algorithm");
       
    94             System.out.println("Skipping this test...");
       
    95             return;
       
    96 
       
    97         } else {
       
    98             System.out.println("The following JCE providers support the " +
       
    99                 "'Signature.NONEwithRSA' algorithm: ");
       
   100             for (Provider provider : providers) {
       
   101                 System.out.println("    " + provider.getName());
       
   102             }
       
   103         }
       
   104         System.out.println(
       
   105                 "Creating a temporary RSA keypair in the Windows-My store");
       
   106         KeyStore ks = KeyStore.getInstance("Windows-MY");
       
   107         ks.load(null, null);
       
   108         CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
       
   109         ckg.generate(1024);
       
   110         RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();
       
   111         ks.setKeyEntry("6578658", k, null, new X509Certificate[]{
       
   112                     ckg.getSelfCertificate(new X500Name("cn=6578658,c=US"), 1000)
       
   113                 });
       
   114         ks.store(null, null);
       
   115 
       
   116         System.out.println("---------------------------------------------");
       
   117 
       
   118         try {
       
   119             KeyPair keys = getKeysFromKeyStore();
       
   120             signAllUsing("SunMSCAPI", keys.getPrivate());
       
   121             System.out.println("---------------------------------------------");
       
   122 
       
   123             verifyAllUsing("SunMSCAPI", keys.getPublic());
       
   124             System.out.println("---------------------------------------------");
       
   125 
       
   126             verifyAllUsing("SunJCE", keys.getPublic());
       
   127             System.out.println("---------------------------------------------");
       
   128 
       
   129             keys = generateKeys();
       
   130             signAllUsing("SunJCE", keys.getPrivate());
       
   131             System.out.println("---------------------------------------------");
       
   132 
       
   133             verifyAllUsing("SunMSCAPI", keys.getPublic());
       
   134             System.out.println("---------------------------------------------");
       
   135         } finally {
       
   136             System.out.println(
       
   137                     "Deleting temporary RSA keypair from Windows-My store");
       
   138             ks.deleteEntry("6578658");
       
   139         }
       
   140 
       
   141     }
       
   142 
       
   143     private static KeyPair getKeysFromKeyStore() throws Exception {
       
   144         KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
       
   145         ks.load(null, null);
       
   146         System.out.println("Loaded keystore: Windows-MY");
       
   147 
       
   148         Enumeration<String> e = ks.aliases();
       
   149         PrivateKey privateKey = null;
       
   150         PublicKey publicKey = null;
       
   151 
       
   152         while (e.hasMoreElements()) {
       
   153             String alias = e.nextElement();
       
   154             if (alias.equals("6578658")) {
       
   155                 System.out.println("Loaded entry: " + alias);
       
   156                 privateKey = (PrivateKey) ks.getKey(alias, null);
       
   157                 publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
       
   158             }
       
   159         }
       
   160         if (privateKey == null || publicKey == null) {
       
   161             throw new Exception("Cannot load the keys need to run this test");
       
   162         }
       
   163 
       
   164         return new KeyPair(publicKey, privateKey);
       
   165     }
       
   166 
       
   167 
       
   168     private static KeyPair generateKeys() throws Exception {
       
   169         KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
       
   170         keyGen.initialize(1024, null);
       
   171         KeyPair pair = keyGen.generateKeyPair();
       
   172         PrivateKey privateKey = pair.getPrivate();
       
   173         PublicKey publicKey = pair.getPublic();
       
   174 
       
   175         if (privateKey == null || publicKey == null) {
       
   176             throw new Exception("Cannot load the keys need to run this test");
       
   177         }
       
   178 
       
   179         return new KeyPair(publicKey, privateKey);
       
   180     }
       
   181 
       
   182     private static void signAllUsing(String providerName, PrivateKey privateKey)
       
   183             throws Exception {
       
   184         Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);
       
   185         if (sig1 == null) {
       
   186             throw new Exception("'NONEwithRSA' is not supported");
       
   187         }
       
   188         if (sig1.getProvider() != null) {
       
   189             System.out.println("Using NONEwithRSA signer from the " +
       
   190                 sig1.getProvider().getName() + " JCE provider");
       
   191         } else {
       
   192             System.out.println(
       
   193                 "Using NONEwithRSA signer from the internal JCE provider");
       
   194         }
       
   195 
       
   196         System.out.println("Using key: " + privateKey);
       
   197         generatedSignatures.clear();
       
   198         for (byte[] hash : precomputedHashes) {
       
   199             sig1.initSign(privateKey);
       
   200             sig1.update(hash);
       
   201 
       
   202             try {
       
   203 
       
   204                 byte [] sigBytes = sig1.sign();
       
   205                 System.out.println("\nGenerated RSA signature over a " +
       
   206                     hash.length + "-byte hash (signature length: " +
       
   207                     sigBytes.length * 8 + " bits)");
       
   208                 System.out.println(String.format("0x%0" +
       
   209                     (sigBytes.length * 2) + "x",
       
   210                     new java.math.BigInteger(1, sigBytes)));
       
   211                 generatedSignatures.add(sigBytes);
       
   212 
       
   213             } catch (SignatureException se) {
       
   214                 System.out.println("Error generating RSA signature: " + se);
       
   215             }
       
   216         }
       
   217     }
       
   218 
       
   219     private static void verifyAllUsing(String providerName, PublicKey publicKey)
       
   220             throws Exception {
       
   221         Signature sig1 = Signature.getInstance("NONEwithRSA", providerName);
       
   222         if (sig1.getProvider() != null) {
       
   223             System.out.println("\nUsing NONEwithRSA verifier from the " +
       
   224                 sig1.getProvider().getName() + " JCE provider");
       
   225         } else {
       
   226             System.out.println(
       
   227                 "\nUsing NONEwithRSA verifier from the internal JCE provider");
       
   228         }
       
   229 
       
   230         System.out.println("Using key: " + publicKey);
       
   231 
       
   232         int i = 0;
       
   233         for (byte[] hash : precomputedHashes) {
       
   234 
       
   235             byte[] sigBytes = generatedSignatures.get(i++);
       
   236             System.out.println("\nVerifying RSA Signature over a " +
       
   237                 hash.length + "-byte hash (signature length: " +
       
   238                 sigBytes.length * 8 + " bits)");
       
   239             System.out.println(String.format("0x%0" +
       
   240                 (sigBytes.length * 2) + "x",
       
   241                 new java.math.BigInteger(1, sigBytes)));
       
   242 
       
   243             sig1.initVerify(publicKey);
       
   244             sig1.update(hash);
       
   245             if (sig1.verify(sigBytes)) {
       
   246                 System.out.println("Verify PASSED");
       
   247             } else {
       
   248                 throw new Exception("Verify FAILED");
       
   249             }
       
   250         }
       
   251     }
       
   252 }