test/jdk/sun/security/rsa/TestSignatures.java
author wetmore
Fri, 11 May 2018 15:53:12 -0700
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
parent 47216 71c04702a3d5
permissions -rw-r--r--
Initial TLSv1.3 Implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    26
 * @bug 4853305 4963723 8146293
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test signing/verifying using all the signature algorithms
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    28
 * @library /test/lib
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    29
 * @build jdk.test.lib.SigTestUtil
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    30
 * @run main TestSignatures
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author Andreas Sterbenz
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12685
diff changeset
    32
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.interfaces.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    41
import jdk.test.lib.SigTestUtil;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    42
import static jdk.test.lib.SigTestUtil.SignatureType;
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    43
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class TestSignatures {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private final static String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final char[] password = "test12".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static KeyStore getKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        KeyStore ks = KeyStore.getInstance("JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        ks.load(in, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    62
    private static void testSignature(String mdAlg, PrivateKey privateKey,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    63
            PublicKey publicKey) throws NoSuchAlgorithmException,
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    64
            InvalidKeyException, SignatureException {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    65
        System.out.println("Testing against " + mdAlg + "...");
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    66
        String sigAlg = SigTestUtil.generateSigAlg(SignatureType.RSA, mdAlg);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    67
        Signature s = Signature.getInstance(sigAlg, provider);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        s.initSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        byte[] sig = s.sign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        s.initVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        boolean result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (result == false) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    76
            throw new RuntimeException("Verification 1 failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        s.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (result == false) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    81
            throw new RuntimeException("Verification 2 failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        result = s.verify(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (result == true) {
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    85
            throw new RuntimeException("Verification 3 succeeded");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    89
    private static void test(PrivateKey privateKey, PublicKey publicKey)
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    90
            throws Exception {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    91
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    92
        int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength();
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    93
        System.out.println("modulus size = " + testSize);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    94
        // work around a corner case where the key size is one bit short
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    95
        if ((testSize & 0x07) != 0) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    96
            testSize += (8 - (testSize & 0x07));
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    97
            System.out.println("adjusted modulus size = " + testSize);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
56542
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
    99
        Iterable<String> sign_alg_pkcs15 =
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   100
            SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   101
        sign_alg_pkcs15.forEach(testAlg -> {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   102
            try {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   103
                testSignature(testAlg, privateKey, publicKey);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   104
            } catch (NoSuchAlgorithmException | InvalidKeyException |
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   105
                     SignatureException ex) {
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   106
                throw new RuntimeException(ex);
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   107
            }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   108
        }
56aaa6cb3693 Initial TLSv1.3 Implementation
wetmore
parents: 47216
diff changeset
   109
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        provider = Security.getProvider("SunRsaSign");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        data = new byte[2048];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        new Random().nextBytes(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        KeyStore ks = getKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        KeyFactory kf = KeyFactory.getInstance("RSA", provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            String alias = (String)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            if (ks.isKeyEntry(alias)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println("* Key " + alias + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                privateKey = (PrivateKey)kf.translateKey(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                publicKey = (PublicKey)kf.translateKey(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                test(privateKey, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        System.out.println("All tests passed (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}