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);
}
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());
}
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);
}
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
}
}
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());
}
Aggregations