Skip to content

Commit

Permalink
Async encryption for Assertions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lomilar committed Apr 13, 2019
1 parent 002b658 commit 416dbf2
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 9 deletions.
105 changes: 105 additions & 0 deletions cass.competency/src/main/java/org/cass/profile/EcAssertion.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ public void setSubject(EcPk pk) {
subject = EcEncryptedValue.encryptValue(pk.toPem(), id, owners, readers);
}

public void setSubjectAsync(EcPk pk, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
Array<String> owners = new Array<String>();
Array<String> readers = null;

if (reader == null)
readers = new Array<String>();
else
readers = (Array) Global.JSON.parse(Global.JSON.stringify(reader));

if (subject != null) {
if (subject.owner != null)
owners.concat(subject.owner);
if (subject.reader != null)
readers.concat(subject.reader);
}

if (owner != null)
owners = owners.concat(owner);

readers.push(pk.toPem());
EcEncryptedValue.encryptValueAsync(pk.toPem(), id, owners, readers, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue subject) {
me.subject = subject;
success.$invoke();
}
}, failure);
}

public void getSubjectAsync(final Callback1<EcPk> success, final Callback1<String> failure) {
if (subject == null) {
success.$invoke(null);
Expand Down Expand Up @@ -176,6 +206,17 @@ public void setAgent(EcPk pk) {
agent = EcEncryptedValue.encryptValue(pk.toPem(), id, subject.owner, subject.reader);
}

public void setAgentAsync(EcPk pk, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
EcEncryptedValue.encryptValueAsync(pk.toPem(), id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue agent) {
me.agent = agent;
success.$invoke();
}
}, failure);
}

public void getAgentAsync(final Callback1<EcPk> success, final Callback1<String> failure) {
if (agent == null) {
success.$invoke(null);
Expand Down Expand Up @@ -363,6 +404,16 @@ public void setAssertionDate(Long assertionDateMs) {
assertionDate = EcEncryptedValue.encryptValue(assertionDateMs.toString(), id, subject.owner, subject.reader);
}

public void setAssertionDateAsync(Long assertionDateMs, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
EcEncryptedValue.encryptValueAsync(assertionDateMs.toString(), id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue assertionDate) {
me.assertionDate = assertionDate;
success.$invoke();
}
}, failure);
}
public void getAssertionDateAsync(final Callback1<Long> success, final Callback1<String> failure) {
if (assertionDate == null) {
success.$invoke(null);
Expand Down Expand Up @@ -407,6 +458,16 @@ public void setExpirationDate(Long expirationDateMs) {
expirationDate = EcEncryptedValue.encryptValue(expirationDateMs.toString(), id, subject.owner, subject.reader);
}

public void setExpirationDateAsync(Long expirationDateMs, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
EcEncryptedValue.encryptValueAsync(expirationDateMs.toString(), id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue expirationDate) {
me.expirationDate = expirationDate;
success.$invoke();
}
}, failure);
}
public void getExpirationDateAsync(final Callback1<Long> success, final Callback1<String> failure) {
if (expirationDate == null) {
success.$invoke(null);
Expand Down Expand Up @@ -519,6 +580,16 @@ public String getDecayFunction() {
public void setDecayFunction(String decayFunctionText) {
decayFunction = EcEncryptedValue.encryptValue(decayFunctionText.toString(), id, subject.owner, subject.reader);
}
public void setDecayFunctionAsync(String decayFunctionText, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
EcEncryptedValue.encryptValueAsync(decayFunctionText, id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue decayFunction) {
me.decayFunction = decayFunction;
success.$invoke();
}
}, failure);
}

public void getDecayFunctionAsync(final Callback1<String> success, final Callback1<String> failure) {
if (decayFunction == null) {
Expand Down Expand Up @@ -564,6 +635,17 @@ public void setNegative(Boolean negativeB) {
negative = EcEncryptedValue.encryptValue(negativeB.toString(), id, subject.owner, subject.reader);
}

public void setNegativeAsync(Boolean negativeB, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
EcEncryptedValue.encryptValueAsync(negativeB.toString(), id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue negative) {
me.negative = negative;
success.$invoke();
}
}, failure);
}

public void getNegativeAsync(final Callback1<Boolean> success, final Callback1<String> failure) {
if (negative == null) {
success.$invoke(null);
Expand Down Expand Up @@ -610,6 +692,29 @@ public void setEvidence(Array<String> evidences) {
evidence = encryptedValues;
}

public void setEvidenceAsync(Array<String> evidences, final Callback0 success, Callback1<String> failure) {
final EcAssertion me = this;
final Array<EcEncryptedValue> encryptedValues = new Array<EcEncryptedValue>();
new EcAsyncHelper<String>().each(evidences, new Callback2<String, Callback0>() {
@Override
public void $invoke(String s, final Callback0 callback0) {
EcEncryptedValue.encryptValueAsync(s, id, subject.owner, subject.reader, new Callback1<EcEncryptedValue>() {
@Override
public void $invoke(EcEncryptedValue ecEncryptedValue) {
encryptedValues.push(ecEncryptedValue);
callback0.$invoke();
}
},(Callback1)callback0);
}
}, new Callback1<Array<String>>() {
@Override
public void $invoke(Array<String> strings) {
me.evidence = encryptedValues;
success.$invoke();
}
});
}

public void save(Callback1<String> success, Callback1<String> failure, EcRepository repo) {
if (competency == null || competency == "") {
String msg = "Failing to save: Competency cannot be missing";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,69 @@ public static EcEncryptedValue encryptValue(String text, String id, Array<String
return v;
}

/**
* Encrypts a text value with the owners and readers provided
*
* @param {String} text Text to encrypt
* @param {String} id ID of the value to encrypt
* @param {String[]} owners Owner keys to encrypt value with
* @param {String[]} readers Reader keys to encrypt value with
* @return {EcEncryptedValue} Encrypted value
* @memberOf EcEncryptedValue
* @method encryptValue
* @static
*/
public static void encryptValueAsync(String text, final String id, Array<String> owners, Array<String> readers, final Callback1<EcEncryptedValue> success, Callback1<String> failure) {
final EcEncryptedValue v = new EcEncryptedValue();

final String newIv = EcAes.newIv(16);
final String newSecret = EcAes.newIv(16);
v.payload = EcAesCtr.encrypt(text, newSecret, newIv);
if (owners != null) {
for (int i = 0; i < owners.$length(); i++) {
v.addOwner(EcPk.fromPem(owners.$get(i)));
}
}
if (readers != null) {
for (int i = 0; i < readers.$length(); i++) {
v.addReaderBasic(EcPk.fromPem(readers.$get(i)));
}
}

Array<String> pks = new Array<>();

if (owners != null)
if (v.owner != null)
pks = pks.concat(v.owner);
if (readers != null)
if (v.reader != null)
pks = pks.concat(v.reader);
new EcAsyncHelper<String>().each(pks, new Callback2<String, Callback0>() {
@Override
public void $invoke(String pk, final Callback0 callback0) {
EbacEncryptedSecret eSecret = new EbacEncryptedSecret();
eSecret.id = util.encode64(pkcs5.pbkdf2(id, "", 1, 8));
eSecret.iv = newIv;
eSecret.secret = newSecret;
if (v.secret == null) {
v.secret = new Array<String>();
}
EcRsaOaepAsync.encrypt(EcPk.fromPem(pk), eSecret.toEncryptableJson(), new Callback1<String>() {
@Override
public void $invoke(String s) {
v.secret.push(s);
callback0.$invoke();
}
},(Callback1)callback0);
}
}, new Callback1<Array<String>>() {
@Override
public void $invoke(Array<String> pks) {
success.$invoke(v);
}
});
}

/**
* Encrypt a value with a specific IV and secret
*
Expand Down Expand Up @@ -814,6 +877,25 @@ public boolean isAnEncrypted(String type) {
* @method addReader
*/
public void addReader(EcPk newReader) {
addReaderBasic(newReader);
EbacEncryptedSecret payloadSecret = decryptSecret();

if (payloadSecret == null) {
Global.console.error("Cannot add a Reader if you don't know the secret");
return;
}

EcArray.setAdd(secret, EcRsaOaep.encrypt(newReader, payloadSecret.toEncryptableJson()));
}

/**
* Adds a reader to the object, if the reader does not exist.
*
* @param {EcPk} newReader PK of the new reader.
* @memberOf EcEncryptedValue
* @method addReader
*/
public void addReaderBasic(EcPk newReader) {
String pem = newReader.toPem();
if (reader == null) {
reader = new Array<String>();
Expand All @@ -824,15 +906,6 @@ public void addReader(EcPk newReader) {
if (EcArray.has(owner, pem))
return;
EcArray.setAdd(reader, pem);

EbacEncryptedSecret payloadSecret = decryptSecret();

if (payloadSecret == null) {
Global.console.error("Cannot add a Reader if you don't know the secret");
return;
}

EcArray.setAdd(secret, EcRsaOaep.encrypt(newReader, payloadSecret.toEncryptableJson()));
}

/**
Expand Down

0 comments on commit 416dbf2

Please sign in to comment.