jdk/test/sun/security/mscapi/SignUsingNONEwithRSA.java
changeset 39508 b0c063e32440
parent 33868 9c1bde39fe18
equal deleted inserted replaced
39507:e65f112c7ee9 39508:b0c063e32440
     1 /*
     1 /*
     2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    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
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24  /*
    25  * @see SignUsingNONEwithRSA.sh
    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
    26  */
    31  */
    27 
    32 
    28 import java.security.*;
    33 import java.security.*;
       
    34 import java.security.cert.X509Certificate;
       
    35 import java.security.interfaces.RSAPrivateCrtKey;
    29 import java.util.*;
    36 import java.util.*;
       
    37 import sun.security.tools.keytool.CertAndKeyGen;
       
    38 import sun.security.x509.X500Name;
    30 
    39 
    31 public class SignUsingNONEwithRSA {
    40 public class SignUsingNONEwithRSA {
    32 
    41 
    33     private static final List<byte[]> precomputedHashes = Arrays.asList(
    42     private static final List<byte[]> precomputedHashes = Arrays.asList(
    34         // A MD5 hash
    43         // A MD5 hash
    90                 "'Signature.NONEwithRSA' algorithm: ");
    99                 "'Signature.NONEwithRSA' algorithm: ");
    91             for (Provider provider : providers) {
   100             for (Provider provider : providers) {
    92                 System.out.println("    " + provider.getName());
   101                 System.out.println("    " + provider.getName());
    93             }
   102             }
    94         }
   103         }
    95         System.out.println("-------------------------------------------------");
   104         System.out.println(
    96 
   105                 "Creating a temporary RSA keypair in the Windows-My store");
    97         KeyPair keys = getKeysFromKeyStore();
   106         KeyStore ks = KeyStore.getInstance("Windows-MY");
    98         signAllUsing("SunMSCAPI", keys.getPrivate());
   107         ks.load(null, null);
    99         System.out.println("-------------------------------------------------");
   108         CertAndKeyGen ckg = new CertAndKeyGen("RSA", "SHA1withRSA");
   100 
   109         ckg.generate(1024);
   101         verifyAllUsing("SunMSCAPI", keys.getPublic());
   110         RSAPrivateCrtKey k = (RSAPrivateCrtKey) ckg.getPrivateKey();
   102         System.out.println("-------------------------------------------------");
   111         ks.setKeyEntry("6578658", k, null, new X509Certificate[]{
   103 
   112                     ckg.getSelfCertificate(new X500Name("cn=6578658,c=US"), 1000)
   104         verifyAllUsing("SunJCE", keys.getPublic());
   113                 });
   105         System.out.println("-------------------------------------------------");
   114         ks.store(null, null);
   106 
   115 
   107         keys = generateKeys();
   116         System.out.println("---------------------------------------------");
   108         signAllUsing("SunJCE", keys.getPrivate());
   117 
   109         System.out.println("-------------------------------------------------");
   118         try {
   110 
   119             KeyPair keys = getKeysFromKeyStore();
   111         verifyAllUsing("SunMSCAPI", keys.getPublic());
   120             signAllUsing("SunMSCAPI", keys.getPrivate());
   112         System.out.println("-------------------------------------------------");
   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         }
   113 
   140 
   114     }
   141     }
   115 
   142 
   116     private static KeyPair getKeysFromKeyStore() throws Exception {
   143     private static KeyPair getKeysFromKeyStore() throws Exception {
   117         KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");
   144         KeyStore ks = KeyStore.getInstance("Windows-MY", "SunMSCAPI");