1 /* 2 * Copyright (c) 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 import java.io.Serializable; 25 import java.security.KeyPair; 26 import java.security.KeyPairGenerator; 27 import java.security.Signature; 28 import java.security.SignedObject; 29 30 /* 31 * @test 32 * @bug 8050374 33 * @summary Checks if a signed object is a copy of an original object 34 */ 35 public class Copy { 36 37 private static final String DSA = "DSA"; 38 private static final int KEY_SIZE = 512; 39 private static final int MAGIC = 123; 40 41 public static void main(String args[]) throws Exception { 42 KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA); 43 kg.initialize(KEY_SIZE); 44 KeyPair kp = kg.genKeyPair(); 45 46 Signature signature = Signature.getInstance(DSA); 47 Test original = new Test(); 48 SignedObject so = new SignedObject(original, kp.getPrivate(), 49 signature); 50 System.out.println("Signature algorithm: " + so.getAlgorithm()); 51 52 signature = Signature.getInstance(DSA, "SUN"); 53 if (!so.verify(kp.getPublic(), signature)) { 54 throw new RuntimeException("Verification failed"); 55 } 56 57 kg = KeyPairGenerator.getInstance(DSA); 58 kg.initialize(KEY_SIZE); 59 kp = kg.genKeyPair(); 60 61 if (so.verify(kp.getPublic(), signature)) { 62 throw new RuntimeException("Unexpected success"); 63 } 64 65 Object copy = so.getObject(); 66 if (!original.equals(copy)) { 67 throw new RuntimeException("Signed object is not equal " 68 + "to original one: " + copy); 69 } 70 71 /* 72 * The signed object is a copy of an original one. 73 * Once the copy is made, further manipulation 74 * of the original object shouldn't has ane effect on the copy. 75 */ 76 original.set(MAGIC - 1); 77 copy = so.getObject(); 78 if (original.equals(copy)) { 79 throw new RuntimeException("Signed object is not a copy " 80 + "of original one: " + copy); 81 } 82 83 System.out.println("Test passed"); 84 } 85 86 private static class Test implements Serializable { 87 private int number = MAGIC; 88 89 public int get() { 90 return number; 91 } 92 93 public void set(int magic) { 94 this.number = magic; 95 } 96 97 @Override 98 public int hashCode() { 99 return number; 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (obj == null) { 105 return false; 106 } 107 108 if (!(obj instanceof Test)) { 109 return false; 110 } 111 112 Test other = (Test) obj; 113 return number == other.number; 114 } 115 116 @Override 117 public String toString() { 118 return "" + number; 119 } 120 } 121 } 122 123