use of java.security.SignedObject in project robovm by robovm.
the class SignedObjectTest method testSignedObject.
public void testSignedObject() throws Exception {
TestKeyPair tkp = null;
Properties prop;
Signature sig = Signature.getInstance("SHA1withDSA");
try {
tkp = new TestKeyPair("DSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return;
}
prop = new Properties();
prop.put("aaa", "bbb");
SignedObject so = new SignedObject(prop, tkp.getPrivate(), sig);
assertEquals("SHA1withDSA", so.getAlgorithm());
assertEquals(prop, so.getObject());
assertTrue("verify() failed", so.verify(tkp.getPublic(), sig));
assertNotNull("signature is null", so.getSignature());
}
use of java.security.SignedObject in project jdk8u_jdk by JetBrains.
the class Chain method runTest.
static boolean runTest(Test test) {
System.out.format("Test: provider = %s, signature algorithm = %s, " + "key algorithm = %s\n", test.provider, test.sigAlg, test.keyAlg);
try {
// Generate all private/public key pairs
PrivateKey[] privKeys = new PrivateKey[N];
PublicKey[] pubKeys = new PublicKey[N];
PublicKey[] anotherPubKeys = new PublicKey[N];
KeyPairGenerator kpg = KeyPairGenerator.getInstance(test.keyAlg.name);
for (int j = 0; j < N; j++) {
KeyPair kp = kpg.genKeyPair();
KeyPair anotherKp = kpg.genKeyPair();
privKeys[j] = kp.getPrivate();
pubKeys[j] = kp.getPublic();
anotherPubKeys[j] = anotherKp.getPublic();
if (Arrays.equals(pubKeys[j].getEncoded(), anotherPubKeys[j].getEncoded())) {
System.out.println("Failed: it should not get " + "the same pair of public key");
return false;
}
}
Signature signature;
if (test.provider != Provider.Default) {
signature = Signature.getInstance(test.sigAlg.name, test.provider.name);
} else {
signature = Signature.getInstance(test.sigAlg.name);
}
// Create a chain of signed objects
SignedObject[] objects = new SignedObject[N];
objects[0] = new SignedObject(str, privKeys[0], signature);
for (int j = 1; j < N; j++) {
objects[j] = new SignedObject(objects[j - 1], privKeys[j], signature);
}
// Verify the chain
int n = objects.length - 1;
SignedObject object = objects[n];
do {
if (!object.verify(pubKeys[n], signature)) {
System.out.println("Failed: verification failed, n = " + n);
return false;
}
if (object.verify(anotherPubKeys[n], signature)) {
System.out.println("Failed: verification should not " + "succeed with wrong public key, n = " + n);
return false;
}
object = (SignedObject) object.getObject();
n--;
} while (n > 0);
System.out.println("signed data: " + object.getObject());
if (!str.equals(object.getObject())) {
System.out.println("Failed: signed data is not equal to " + "original one");
return false;
}
System.out.println("Test passed");
return true;
} catch (NoSuchProviderException nspe) {
if (test.provider == Provider.SunMSCAPI && !System.getProperty("os.name").startsWith("Windows")) {
System.out.println("SunMSCAPI is available only on Windows: " + nspe);
return true;
}
System.out.println("Unexpected exception: " + nspe);
return false;
} catch (Exception e) {
System.out.println("Unexpected exception: " + e);
e.printStackTrace(System.out);
return false;
}
}
use of java.security.SignedObject in project jdk8u_jdk by JetBrains.
the class Copy method main.
public static void main(String[] args) throws Exception {
KeyPairGenerator kg = KeyPairGenerator.getInstance(DSA);
kg.initialize(KEY_SIZE);
KeyPair kp = kg.genKeyPair();
Signature signature = Signature.getInstance(DSA);
Test original = new Test();
SignedObject so = new SignedObject(original, kp.getPrivate(), signature);
System.out.println("Signature algorithm: " + so.getAlgorithm());
signature = Signature.getInstance(DSA, "SUN");
if (!so.verify(kp.getPublic(), signature)) {
throw new RuntimeException("Verification failed");
}
kg = KeyPairGenerator.getInstance(DSA);
kg.initialize(KEY_SIZE);
kp = kg.genKeyPair();
if (so.verify(kp.getPublic(), signature)) {
throw new RuntimeException("Unexpected success");
}
Object copy = so.getObject();
if (!original.equals(copy)) {
throw new RuntimeException("Signed object is not equal " + "to original one: " + copy);
}
/*
* The signed object is a copy of an original one.
* Once the copy is made, further manipulation
* of the original object shouldn't has any effect on the copy.
*/
original.set(MAGIC - 1);
copy = so.getObject();
if (original.equals(copy)) {
throw new RuntimeException("Signed object is not a copy " + "of original one: " + copy);
}
System.out.println("Test passed");
}
use of java.security.SignedObject in project jdk8u_jdk by JetBrains.
the class Correctness method main.
public static void main(String[] args) throws Exception {
String SIGALG = "SHA1withRSA";
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair kp = kpg.generateKeyPair();
SignedObject so1 = new SignedObject("Hello", kp.getPrivate(), Signature.getInstance(SIGALG));
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(so1);
out.close();
byte[] data = byteOut.toByteArray();
SignedObject so2 = (SignedObject) new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
if (!so2.getObject().equals("Hello")) {
throw new Exception("Content changed");
}
if (!so2.getAlgorithm().equals(SIGALG)) {
throw new Exception("Signature algorithm unknown");
}
if (!so2.verify(kp.getPublic(), Signature.getInstance(SIGALG))) {
throw new Exception("Not verified");
}
}
Aggregations