1 /* 2 * Copyright (c) 2003, 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 4955844 27 * @summary ensure that the NONEwithRSA adapter works correctly 28 * @author Andreas Sterbenz 29 * @key randomness 30 */ 31 32 import java.util.*; 33 34 import java.security.*; 35 36 import javax.crypto.*; 37 38 public class NONEwithRSA { 39 40 public static void main(String[] args) throws Exception { 41 // showProvider(Security.getProvider("SUN")); 42 Random random = new Random(); 43 byte[] b = new byte[16]; 44 random.nextBytes(b); 45 46 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 47 kpg.initialize(512); 48 KeyPair kp = kpg.generateKeyPair(); 49 50 Signature sig = Signature.getInstance("NONEwithRSA"); 51 sig.initSign(kp.getPrivate()); 52 System.out.println("Provider: " + sig.getProvider()); 53 sig.update(b); 54 byte[] sb = sig.sign(); 55 56 sig.initVerify(kp.getPublic()); 57 sig.update(b); 58 if (sig.verify(sb) == false) { 59 throw new Exception("verification failed"); 60 } 61 62 Cipher c = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 63 c.init(Cipher.DECRYPT_MODE, kp.getPublic()); 64 byte[] dec = c.doFinal(sb); 65 if (Arrays.equals(dec, b) == false) { 66 throw new Exception("decryption failed"); 67 } 68 69 sig = Signature.getInstance("NONEwithRSA", "SunJCE"); 70 sig.initSign(kp.getPrivate()); 71 sig = Signature.getInstance("NONEwithRSA", Security.getProvider("SunJCE")); 72 sig.initSign(kp.getPrivate()); 73 74 try { 75 Signature.getInstance("NONEwithRSA", "SUN"); 76 throw new Exception("call succeeded"); 77 } catch (NoSuchAlgorithmException e) { 78 e.printStackTrace(); 79 } 80 81 System.out.println("OK"); 82 } 83 84 private static void showProvider(Provider p) { 85 System.out.println(p); 86 for (Iterator t = p.getServices().iterator(); t.hasNext(); ) { 87 System.out.println(t.next()); 88 } 89 } 90 91 }