blob: 26566f929c9b032ff6b21065443b0476dc332aec [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4955844
27 * @summary ensure that the NONEwithRSA adapter works correctly
28 * @author Andreas Sterbenz
29 */
30
31import java.util.*;
32
33import java.security.*;
34
35import javax.crypto.*;
36
37public class NONEwithRSA {
38
39 public static void main(String[] args) throws Exception {
40// showProvider(Security.getProvider("SUN"));
41 Random random = new Random();
42 byte[] b = new byte[16];
43 random.nextBytes(b);
44
45 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
46 kpg.initialize(512);
47 KeyPair kp = kpg.generateKeyPair();
48
49 Signature sig = Signature.getInstance("NONEwithRSA");
50 sig.initSign(kp.getPrivate());
51 System.out.println("Provider: " + sig.getProvider());
52 sig.update(b);
53 byte[] sb = sig.sign();
54
55 sig.initVerify(kp.getPublic());
56 sig.update(b);
57 if (sig.verify(sb) == false) {
58 throw new Exception("verification failed");
59 }
60
61 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
62 c.init(Cipher.DECRYPT_MODE, kp.getPublic());
63 byte[] dec = c.doFinal(sb);
64 if (Arrays.equals(dec, b) == false) {
65 throw new Exception("decryption failed");
66 }
67
68 sig = Signature.getInstance("NONEwithRSA", "SunJCE");
69 sig.initSign(kp.getPrivate());
70 sig = Signature.getInstance("NONEwithRSA", Security.getProvider("SunJCE"));
71 sig.initSign(kp.getPrivate());
72
73 try {
74 Signature.getInstance("NONEwithRSA", "SUN");
75 throw new Exception("call succeeded");
76 } catch (NoSuchAlgorithmException e) {
77 e.printStackTrace();
78 }
79
80 System.out.println("OK");
81 }
82
83 private static void showProvider(Provider p) {
84 System.out.println(p);
85 for (Iterator t = p.getServices().iterator(); t.hasNext(); ) {
86 System.out.println(t.next());
87 }
88 }
89
90}