author | lana |
Thu, 22 Oct 2015 08:47:39 -0700 | |
changeset 33027 | f40267448832 |
parent 32646 | db7c5592a47f |
permissions | -rw-r--r-- |
27182 | 1 |
/* |
32472 | 2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
27182 | 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 |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
32472
diff
changeset
|
26 |
* @bug 8029849 8132082 8133535 |
27182 | 27 |
* @summary Make sure signing via encrypt and verifying via decrypt are not |
28 |
* supported by OracleUcrypto provider. |
|
29 |
* @author Anthony Scarpino |
|
30046 | 30 |
* @key randomness |
27182 | 31 |
*/ |
32 |
||
33 |
import java.util.Random; |
|
32472 | 34 |
import java.security.*; |
35 |
import java.security.interfaces.*; |
|
36 |
import java.security.spec.RSAPrivateKeySpec; |
|
27182 | 37 |
import javax.crypto.Cipher; |
38 |
||
39 |
public 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 |
|
32472 | 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) { |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
32472
diff
changeset
|
92 |
if (!e.getMessage().contains("RSAPublicKey required for encryption")) { |
32472 | 93 |
System.out.println("Wrong exception thrown."); |
94 |
throw e; |
|
95 |
} |
|
27182 | 96 |
} |
97 |
} |
|
32472 | 98 |
|
27182 | 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) { |
|
32646
db7c5592a47f
8133535: Better exception messaging in Ucrypto code
coffeys
parents:
32472
diff
changeset
|
105 |
if (!e.getMessage().contains("RSAPrivateKey required for decryption")) { |
27182 | 106 |
System.out.println("Wrong exception thrown."); |
107 |
throw e; |
|
108 |
} |
|
109 |
} |
|
110 |
||
111 |
System.out.println("Pass"); |
|
112 |
} |
|
113 |
} |