Search in sources :

Example 1 with SignedObject

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());
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Properties(java.util.Properties) SignedObject(java.security.SignedObject)

Example 2 with SignedObject

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;
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) Signature(java.security.Signature) KeyPairGenerator(java.security.KeyPairGenerator) NoSuchProviderException(java.security.NoSuchProviderException) SignedObject(java.security.SignedObject) NoSuchProviderException(java.security.NoSuchProviderException)

Example 3 with SignedObject

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");
}
Also used : KeyPair(java.security.KeyPair) Signature(java.security.Signature) SignedObject(java.security.SignedObject) KeyPairGenerator(java.security.KeyPairGenerator) SignedObject(java.security.SignedObject)

Example 4 with SignedObject

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");
    }
}
Also used : KeyPair(java.security.KeyPair) ByteArrayInputStream(java.io.ByteArrayInputStream) KeyPairGenerator(java.security.KeyPairGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) SignedObject(java.security.SignedObject) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

SignedObject (java.security.SignedObject)4 KeyPair (java.security.KeyPair)3 KeyPairGenerator (java.security.KeyPairGenerator)3 Signature (java.security.Signature)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 Properties (java.util.Properties)1 TestKeyPair (org.apache.harmony.security.tests.support.TestKeyPair)1