Search in sources :

Example 46 with Binder

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;
    }
}
Also used : Binder(net.sergeych.tools.Binder)

Example 47 with Binder

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;
}
Also used : Binder(net.sergeych.tools.Binder) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Example 48 with Binder

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;
}
Also used : Bytes(net.sergeych.utils.Bytes) Binder(net.sergeych.tools.Binder) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 49 with Binder

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"));
}
Also used : Binder(net.sergeych.tools.Binder)

Example 50 with Binder

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());
}
Also used : Binder(net.sergeych.tools.Binder) Test(org.junit.Test)

Aggregations

Binder (net.sergeych.tools.Binder)136 Test (org.junit.Test)67 PrivateKey (com.icodici.crypto.PrivateKey)21 Contract (com.icodici.universa.contract.Contract)14 PublicKey (com.icodici.crypto.PublicKey)13 IOException (java.io.IOException)11 KeyRecord (com.icodici.universa.contract.KeyRecord)10 SimpleRole (com.icodici.universa.contract.roles.SimpleRole)10 Yaml (org.yaml.snakeyaml.Yaml)9 Bytes (net.sergeych.utils.Bytes)8 Decimal (com.icodici.universa.Decimal)7 ListRole (com.icodici.universa.contract.roles.ListRole)6 NonNull (org.checkerframework.checker.nullness.qual.NonNull)5 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 XStream (com.thoughtworks.xstream.XStream)4 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)4 List (java.util.List)4 RoleLink (com.icodici.universa.contract.roles.RoleLink)3 ItemResult (com.icodici.universa.node.ItemResult)3