use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Capsule method checkSignatures.
private void checkSignatures(byte[] src, Collection<Binder> signatures, Binder payload, boolean allowPartiallySigned) throws EncryptionError {
signed = false;
partiallySigned = false;
clearSigners();
if (signatures == null || signatures.isEmpty())
return;
for (Binder b : payload.getBinders("signers")) {
PublicKey k = new PublicKey();
k.unpack(b.getBinary("key"));
Binder result = new Binder();
result.put("key", k);
final String id = b.getStringOrThrow("id");
result.put("id", id);
result.put("data", b.getBinder("data"));
signers.put(id, result);
}
if (signers.size() != signatures.size() && !allowPartiallySigned)
throw new BadSignatureException("signatures do not match signers");
for (Binder sig : signatures) {
AbstractKey k = (AbstractKey) signers.get(sig.getStringOrThrow("key")).get("key");
if (!k.verify(src, sig.getBinary("signature"), HashType.SHA512))
throw new BadSignatureException("signature is broken at " + sig.getStringOrThrow("key"));
// signingKeys.add(k);
}
if (signers.isEmpty()) {
partiallySigned = false;
signed = false;
} else {
signed = signers.size() == signatures.size();
partiallySigned = !signed;
}
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class Capsule method unpackPayload.
@NonNull
private Binder unpackPayload(byte[] packedCoffer, boolean allowPartiallySigned) throws EncryptionError {
signed = false;
Binder outer = Boss.unpack(packedCoffer);
Collection<Binder> signatures = outer.getBinders("signatures");
final byte[] source = outer.getBinary("content");
Binder payload = Boss.unpack(source);
if (!payload.get("type").equals("capsule"))
throw new FormatException("not capsule/unknown type");
checkSignatures(source, signatures, payload, allowPartiallySigned);
publicData = payload.getBinder("public");
return payload;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class BitrustedConnector method decryptBlock.
private Object decryptBlock(Command command) throws EncryptionError {
try {
synchronized (remoteSessionKey) {
Bytes ciphertext = command.getParam(0);
if (ciphertext == null) {
throw new IllegalStateException("missing block data");
}
Binder plain = Boss.unpack(remoteSessionKey.etaDecrypt(ciphertext.toArray()));
inputQueue.put(plain);
}
} catch (SymmetricKey.AuthenticationFailed authenticationFailed) {
throw new EncryptionError("authentication failed on bitrusted block");
} catch (InterruptedException e) {
Thread.interrupted();
} catch (Exception e) {
log.wtf("failed to process block", e);
e.printStackTrace();
}
return null;
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class BitrustedConnector method processHelloAnswer.
private void processHelloAnswer(Binder result) throws EncryptionError {
byte[] data = result.getBinaryOrThrow("data");
byte[] signature = result.getBinaryOrThrow("signature");
setRemoteKey(result.getBinaryOrThrow("public_key"));
if (!remoteKey.verify(data, signature, HashType.SHA256))
throw new EncryptionError("bad signature in hello answer");
Binder answer = Boss.unpack(myKey.decrypt(data));
if (!Arrays.equals(answer.getBinaryOrThrow("nonce"), myNonce))
throw new EncryptionError("nonce mismatch");
remoteSessionKey = new SymmetricKey(answer.getBinary("session_key"));
}
use of net.sergeych.tools.Binder in project universa by UniversaBlockchain.
the class StructureDescriptorTest method packBytes.
@Test
public void packBytes() throws Exception {
StructureDescriptor d = new StructureDescriptor();
d.addField("type", 1).addBinaryField("bb", 12);
byte[] res = d.pack(Binder.fromKeysValues("type", 17, "bb", "Hello world!".getBytes()));
assertEquals("11 48 65 6C 6C 6F 20 77 6F 72 6C 64 21", Bytes.toHex(res));
Binder r = d.unpack(res);
assertEquals(17, r.getIntOrThrow("type"));
assertEquals("Hello world!", r.getBytesOrThrow("bb").toString());
}
Aggregations