Search in sources :

Example 11 with SealedObject

use of javax.crypto.SealedObject in project alfresco-repository by Alfresco.

the class ReEncryptor method reEncryptProperties.

protected void reEncryptProperties(final List<NodePropertyEntity> properties, final String lockToken) {
    final Iterator<NodePropertyEntity> it = properties.iterator();
    // TODO use BatchProcessWorkerAdaptor?
    BatchProcessor.BatchProcessWorker<NodePropertyEntity> worker = new BatchProcessor.BatchProcessWorker<NodePropertyEntity>() {

        public String getIdentifier(NodePropertyEntity entity) {
            return String.valueOf(entity.getNodeId());
        }

        public void beforeProcess() throws Throwable {
            refreshLock(lockToken, chunkSize * 100L);
        }

        public void afterProcess() throws Throwable {
        }

        public void process(final NodePropertyEntity entity) throws Throwable {
            NodePropertyValue nodePropValue = entity.getValue();
            // TODO check that we have the correct type i.e. can be cast to Serializable
            Serializable value = nodePropValue.getSerializableValue();
            if (value instanceof SealedObject) {
                SealedObject sealed = (SealedObject) value;
                NodePropertyKey propertyKey = entity.getKey();
                QName propertyQName = qnameDAO.getQName(propertyKey.getQnameId()).getSecond();
                // decrypt...
                Serializable decrypted = metadataEncryptor.decrypt(propertyQName, sealed);
                // ...and then re-encrypt. The new key will be used.
                Serializable resealed = metadataEncryptor.encrypt(propertyQName, decrypted);
                // TODO update resealed using batch update?
                // does the node DAO do batch updating?
                nodeDAO.setNodeProperties(entity.getNodeId(), Collections.singletonMap(propertyQName, resealed));
            } else {
                NodePropertyKey nodeKey = entity.getKey();
                QName propertyQName = qnameDAO.getQName(nodeKey.getQnameId()).getSecond();
                logger.warn("Encountered an encrypted property that is not a SealedObject, for node id " + entity.getNodeId() + ", property " + propertyQName);
            }
        }
    };
    BatchProcessWorkProvider<NodePropertyEntity> provider = new BatchProcessWorkProvider<NodePropertyEntity>() {

        @Override
        public int getTotalEstimatedWorkSize() {
            return properties.size();
        }

        @Override
        public Collection<NodePropertyEntity> getNextWork() {
            List<NodePropertyEntity> sublist = new ArrayList<NodePropertyEntity>(chunkSize);
            synchronized (it) {
                int count = 0;
                while (it.hasNext() && count < chunkSize) {
                    sublist.add(it.next());
                    count++;
                }
            }
            return sublist;
        }
    };
    new BatchProcessor<NodePropertyEntity>("Reencryptor", transactionHelper, provider, numThreads, chunkSize, applicationContext, logger, 100).process(worker, splitTxns);
}
Also used : Serializable(java.io.Serializable) QName(org.alfresco.service.namespace.QName) NodePropertyEntity(org.alfresco.repo.domain.node.NodePropertyEntity) ArrayList(java.util.ArrayList) SealedObject(javax.crypto.SealedObject) BatchProcessWorkProvider(org.alfresco.repo.batch.BatchProcessWorkProvider) BatchProcessor(org.alfresco.repo.batch.BatchProcessor) NodePropertyValue(org.alfresco.repo.domain.node.NodePropertyValue) NodePropertyKey(org.alfresco.repo.domain.node.NodePropertyKey)

Example 12 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testReadObject.

/**
     * readObject(ObjectInputStream s) method testing. Tests if the
     * serialization/deserialization works correctly: object is serialized,
     * deserialized, the content od deserialized object equals to the content of
     * initial object.
     */
public void testReadObject() throws Exception {
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, new NullCipher());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(so);
    ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
    SealedObject so_des = (SealedObject) ois.readObject();
    assertEquals("The secret content of deserialized object " + "should be equal to the secret content of initial object", secret, so_des.getObject(new NullCipher()));
    assertEquals("The value returned by getAlgorithm() method of " + "deserialized object should be equal to the value returned " + "by getAlgorithm() method of initial object", so.getAlgorithm(), so_des.getAlgorithm());
}
Also used : NullCipher(javax.crypto.NullCipher) ByteArrayInputStream(java.io.ByteArrayInputStream) SealedObject(javax.crypto.SealedObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 13 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testDeserialization.

// http://code.google.com/p/android/issues/detail?id=4834
public void testDeserialization() throws Exception {
    // (Boilerplate so we can create SealedObject instances.)
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance("DES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    // Incorrect use of readUnshared meant you couldn't have two SealedObjects
    // with the same algorithm or parameters algorithm...
    ArrayList<SealedObject> sealedObjects = new ArrayList<SealedObject>();
    for (int i = 0; i < 10; ++i) {
        sealedObjects.add(new SealedObject("hello", cipher));
    }
    String serializedForm = SerializationTester.serializeHex(sealedObjects);
    // ...so this would throw "java.io.InvalidObjectException: Unshared read of back reference".
    SerializationTester.deserializeHex(serializedForm);
}
Also used : ArrayList(java.util.ArrayList) SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 14 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testGetObject2.

/**
     * getObject(Cipher c) method testing. Tests if the proper exception is
     * thrown in the case of incorrect input parameters and if the object sealed
     * with encryption algorithm and specified parameters can be retrieved by
     * specifying the initialized Cipher object.
     */
public void testGetObject2() throws Exception {
    try {
        new SealedObject("secret string", new NullCipher()).getObject((Cipher) null);
        fail("NullPointerException should be thrown in the case of " + "null cipher.");
    } catch (NullPointerException e) {
    }
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    Key key = kg.generateKey();
    IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
    Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ips);
    String secret = "secret string";
    SealedObject so = new SealedObject(secret, cipher);
    cipher.init(Cipher.DECRYPT_MODE, key, ips);
    assertEquals("The returned object does not equals to the " + "original object.", secret, so.getObject(cipher));
    try {
        so.getObject((Cipher) null);
        fail("NullPointerException expected");
    } catch (NullPointerException e) {
    //expected
    }
}
Also used : NullCipher(javax.crypto.NullCipher) SealedObject(javax.crypto.SealedObject) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 15 with SealedObject

use of javax.crypto.SealedObject in project robovm by robovm.

the class SealedObjectTest method testGetAlgorithm.

/**
     * getAlgorithm() method testing. Tests if the returned value equals to the
     * corresponding value of Cipher object.
     */
public void testGetAlgorithm() throws Exception {
    String secret = "secret string";
    String algorithm = "DES";
    KeyGenerator kg = KeyGenerator.getInstance(algorithm);
    Key key = kg.generateKey();
    Cipher cipher = Cipher.getInstance(algorithm);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    SealedObject so = new SealedObject(secret, cipher);
    assertEquals("The algorithm name should be the same as used " + "in cipher.", algorithm, so.getAlgorithm());
}
Also used : SealedObject(javax.crypto.SealedObject) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Aggregations

SealedObject (javax.crypto.SealedObject)23 Cipher (javax.crypto.Cipher)11 Serializable (java.io.Serializable)8 NullCipher (javax.crypto.NullCipher)8 KeyGenerator (javax.crypto.KeyGenerator)7 Key (java.security.Key)6 QName (org.alfresco.service.namespace.QName)6 InvalidKeyException (java.security.InvalidKeyException)5 HashMap (java.util.HashMap)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 PropertyDefinition (org.alfresco.service.cmr.dictionary.PropertyDefinition)4 ObjectInputStream (java.io.ObjectInputStream)3 ObjectOutputStream (java.io.ObjectOutputStream)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)3 SecretKey (javax.crypto.SecretKey)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 AlgorithmParameters (java.security.AlgorithmParameters)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2