jdk/test/com/oracle/security/ucrypto/CipherSignNotSupported.java
author igerasim
Fri, 04 Sep 2015 15:28:01 +0300
changeset 32472 37dabe787932
parent 30046 cf2c86e1819e
child 32646 db7c5592a47f
permissions -rw-r--r--
8132082: Let OracleUcrypto accept RSAPrivateKey Reviewed-by: xuelei, valeriep, coffeys Contributed-by: valerie.peng@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     1
/*
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     4
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     8
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    13
 * accompanied this code).
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    14
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    18
 *
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    21
 * questions.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    22
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    23
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    24
/**
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    25
 * @test
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    26
 * @bug 8029849 8132082
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    27
 * @summary Make sure signing via encrypt and verifying via decrypt are not
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    28
 * supported by OracleUcrypto provider.
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    29
 * @author Anthony Scarpino
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 27182
diff changeset
    30
 * @key randomness
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    31
 */
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    32
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    33
import java.util.Random;
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    34
import java.security.*;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    35
import java.security.interfaces.*;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    36
import java.security.spec.RSAPrivateKeySpec;
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    37
import javax.crypto.Cipher;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    38
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    39
public class CipherSignNotSupported extends UcryptoTest {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    40
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    42
        main(new CipherSignNotSupported(), null);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    43
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    44
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    45
    public void doTest(Provider p) throws Exception {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    46
        Cipher c = null;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    47
        Random random = new Random();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    48
        byte[] pt = new byte[117];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    49
        byte[] ct = new byte[200];
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    50
        random.nextBytes(pt);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    51
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    52
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    53
            c = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    54
        } catch (NoSuchAlgorithmException e) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    55
            if (System.getProperty("os.version").compareTo("5.10") == 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    56
                System.out.println("RSA not supported in S10");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    57
                return;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    58
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    59
            throw e;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    60
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    61
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    62
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    63
        kpg.initialize(1024);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    64
        KeyPair kp = kpg.generateKeyPair();
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    65
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    66
        // Encryption
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    67
        c.init(Cipher.ENCRYPT_MODE, kp.getPublic());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    68
        ct = c.doFinal(pt);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    69
        // Decryption
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    70
        PrivateKey[] privKeys = new PrivateKey[2];
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    71
        privKeys[0] = kp.getPrivate();
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    72
        if (privKeys[0] instanceof RSAPrivateCrtKey) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    73
            RSAPrivateCrtKey k = (RSAPrivateCrtKey) privKeys[0];
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    74
            KeyFactory kf = KeyFactory.getInstance("RSA");
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    75
            privKeys[1] = kf.generatePrivate
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    76
                (new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent()));
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    77
        } else {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    78
            privKeys = new PrivateKey[] {privKeys[0]};
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    79
        }
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    80
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    81
        for (PrivateKey pk : privKeys) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    82
            System.out.println("Testing " + pk);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    83
            c.init(Cipher.DECRYPT_MODE, pk);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    84
            c.doFinal(ct);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    85
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    86
            // Sign
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    87
            try {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    88
                c.init(Cipher.ENCRYPT_MODE, pk);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    89
                ct = c.doFinal(pt);
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    90
                throw new RuntimeException("Encrypt operation should have failed.");
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    91
            } catch (InvalidKeyException e) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    92
                if (e.getMessage().compareTo("RSAPublicKey required for " +
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    93
                        "encryption") != 0) {
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    94
                    System.out.println("Wrong exception thrown.");
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    95
                    throw e;
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    96
                }
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    97
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
    98
        }
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
    99
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   100
        // Verify
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   101
        try {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   102
            c.init(Cipher.DECRYPT_MODE, kp.getPublic());
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   103
            c.doFinal(ct);
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   104
            throw new RuntimeException("Decrypt operation should have failed.");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   105
        } catch (InvalidKeyException e) {
32472
37dabe787932 8132082: Let OracleUcrypto accept RSAPrivateKey
igerasim
parents: 30046
diff changeset
   106
            if (e.getMessage().compareTo("RSAPrivateKey required for " +
27182
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   107
                    "decryption") != 0) {
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   108
                System.out.println("Wrong exception thrown.");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   109
                throw e;
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   110
            }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   111
        }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   112
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   113
        System.out.println("Pass");
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   114
    }
4525d13b8af1 8046002: Move Ucrypto to the open jdk repo
valeriep
parents:
diff changeset
   115
}