1/*
2 * Copyright (c) 2014, 2015, 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 8029849 8132082 8133535
27 * @summary Make sure signing via encrypt and verifying via decrypt are not
28 * supported by OracleUcrypto provider.
29 * @author Anthony Scarpino
30 * @key randomness
31 */
32
33import java.util.Random;
34import java.security.*;
35import java.security.interfaces.*;
36import java.security.spec.RSAPrivateKeySpec;
37import javax.crypto.Cipher;
38
39public class CipherSignNotSupported extends UcryptoTest {
40
41    public static void main(String[] args) throws Exception {
42        main(new CipherSignNotSupported(), null);
43    }
44
45    public void doTest(Provider p) throws Exception {
46        Cipher c = null;
47        Random random = new Random();
48        byte[] pt = new byte[117];
49        byte[] ct = new byte[200];
50        random.nextBytes(pt);
51
52        try {
53            c = Cipher.getInstance("RSA/ECB/PKCS1Padding", p);
54        } catch (NoSuchAlgorithmException e) {
55            if (System.getProperty("os.version").compareTo("5.10") == 0) {
56                System.out.println("RSA not supported in S10");
57                return;
58            }
59            throw e;
60        }
61
62        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
63        kpg.initialize(1024);
64        KeyPair kp = kpg.generateKeyPair();
65
66        // Encryption
67        c.init(Cipher.ENCRYPT_MODE, kp.getPublic());
68        ct = c.doFinal(pt);
69        // Decryption
70        PrivateKey[] privKeys = new PrivateKey[2];
71        privKeys[0] = kp.getPrivate();
72        if (privKeys[0] instanceof RSAPrivateCrtKey) {
73            RSAPrivateCrtKey k = (RSAPrivateCrtKey) privKeys[0];
74            KeyFactory kf = KeyFactory.getInstance("RSA");
75            privKeys[1] = kf.generatePrivate
76                (new RSAPrivateKeySpec(k.getModulus(), k.getPrivateExponent()));
77        } else {
78            privKeys = new PrivateKey[] {privKeys[0]};
79        }
80
81        for (PrivateKey pk : privKeys) {
82            System.out.println("Testing " + pk);
83            c.init(Cipher.DECRYPT_MODE, pk);
84            c.doFinal(ct);
85
86            // Sign
87            try {
88                c.init(Cipher.ENCRYPT_MODE, pk);
89                ct = c.doFinal(pt);
90                throw new RuntimeException("Encrypt operation should have failed.");
91            } catch (InvalidKeyException e) {
92                if (!e.getMessage().contains("RSAPublicKey required for encryption")) {
93                    System.out.println("Wrong exception thrown.");
94                    throw e;
95                }
96            }
97        }
98
99        // Verify
100        try {
101            c.init(Cipher.DECRYPT_MODE, kp.getPublic());
102            c.doFinal(ct);
103            throw new RuntimeException("Decrypt operation should have failed.");
104        } catch (InvalidKeyException e) {
105            if (!e.getMessage().contains("RSAPrivateKey required for decryption")) {
106                System.out.println("Wrong exception thrown.");
107                throw e;
108            }
109        }
110
111        System.out.println("Pass");
112    }
113}
114