jdk/test/sun/security/pkcs11/rsa/TestSignatures.java
author vinnie
Wed, 12 Aug 2015 14:38:09 +0100
changeset 32138 23830562d3d1
parent 30046 cf2c86e1819e
child 35379 1e8e336ef66b
permissions -rw-r--r--
8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
     2
 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4856966
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test signing/verifying using all the signature algorithms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ..
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12685
diff changeset
    30
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class TestSignatures extends PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private final static String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static KeyStore getKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        System.out.println("Testing " + algorithm + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Signature s = Signature.getInstance(algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (result == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new Exception("Verification 1 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (result == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            throw new Exception("Verification 2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (result == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new Exception("Verification 3 succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        testSignature("MD2withRSA", privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        testSignature("MD5withRSA", privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        testSignature("SHA1withRSA", privateKey, publicKey);
12685
8a448b5b9006 4963723: Implement SHA-224
valeriep
parents: 5506
diff changeset
    85
        testSignature("SHA224withRSA", privateKey, publicKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        testSignature("SHA256withRSA", privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (rsaKey.getModulus().bitLength() > 512) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // for SHA384 and SHA512 the data is too long for 512 bit keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            testSignature("SHA384withRSA", privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            testSignature("SHA512withRSA", privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        main(new TestSignatures());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void main(Provider p) throws Exception {
32138
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   100
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   101
        /*
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   102
         * Use Solaris SPARC 11.2 or later to avoid an intermittent failure
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   103
         * when running SunPKCS11-Solaris (8044554)
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   104
         */
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   105
        if (p.getName().equals("SunPKCS11-Solaris") &&
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   106
            System.getProperty("os.name").equals("SunOS") &&
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   107
            System.getProperty("os.arch").equals("sparcv9") &&
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   108
            System.getProperty("os.version").compareTo("5.11") <= 0 &&
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   109
            getDistro().compareTo("11.2") < 0) {
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   110
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   111
            System.out.println("SunPKCS11-Solaris provider requires " +
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   112
                "Solaris SPARC 11.2 or later, skipping");
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   113
            return;
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   114
        }
23830562d3d1 8133318: Exclude intermittent failing PKCS11 tests on Solaris SPARC 11.1 and earlier
vinnie
parents: 30046
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        provider = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        data = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        new Random().nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        KeyStore ks = getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        KeyFactory kf = KeyFactory.getInstance("RSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.out.println("* Key " + alias + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                privateKey = (PrivateKey)kf.translateKey(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                publicKey = (PublicKey)kf.translateKey(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                test(privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}