public void addKeys(AbstractKey... keys) { addKeys(Do.collection(keys)); }
public void addSigners(Collection<AbstractKey> signers) { for (AbstractKey k : signers) addSigner(k, null); }
public void addSigners(AbstractKey... keys) { addSigners(Do.collection(keys)); }
@Test public void testSignedExtra() throws Exception { Capsule c1 = new Capsule(); c1.setPublicData("hello", "world", "I'm", "the coffer"); c1.addSigners(k1); String kid = c1.addSigner(k2, new Binder("extra", "data", "is", "OK") ); Capsule c2 = new Capsule(); c2.setPublicData("hello", "world", "I'm", "the coffer"); byte[] packed = c1.pack(); Capsule c4 = new Capsule(packed, null); assertEquals(c1, c4); assertTrue(c4.isSigned()); assertFalse(c4.isPartiallySigned()); Map<String,Binder> signers = c4.getSigners(); assertEquals(2, signers.size()); ArrayList<AbstractKey> kk = new ArrayList<>(); Binder extra = c4.getSignerData(k2.getPublicKey()); assertEquals(kid, c4.getSignerId(k2.getPublicKey())); assertEquals(k2.getPublicKey(), c4.getSignerKey(kid)); extra = c4.getSignerData(kid); assertEquals("data", extra.getStringOrThrow("extra"));
@Test public void testSigned() throws Exception { Capsule c1 = new Capsule(); c1.setPublicData("hello", "world", "I'm", "the coffer"); PrivateKey k1 = TestKeys.privateKey(0); PrivateKey k2 = TestKeys.privateKey(1); c1.addSigners(k1, k2); Capsule c2 = new Capsule(); c2.setPublicData("hello", "world", "I'm", "the coffer"); byte[] packed = c1.pack(); Capsule c4 = new Capsule(packed, null); assertEquals(c1, c4); assertTrue(c4.isSigned()); assertFalse(c4.isPartiallySigned()); Collection<AbstractKey> signers = c4.getSigningKeys(); assertEquals(2, signers.size()); assertTrue(signers.contains(k1.getPublicKey())); assertTrue(signers.contains(k2.getPublicKey())); packed[0x456]--; exception.expect(Capsule.BadSignatureException.class); c4 = new Capsule(packed, null); }
@Test public void decryptWithPassword() throws Exception { Capsule c1 = new Capsule(); c1.setPrivateData("Very", "secret materials"); String password = "icodici forever"; c1.addKeys(new KeyInfo(KeyInfo.PRF.HMAC_SHA256, 1000, null, null).derivePassword(password)); c1.addKeys(new KeyInfo(KeyInfo.PRF.HMAC_SHA256, 1000, null, null).derivePassword(password+"12")); byte[] packed = c1.pack(); Capsule c2 = new Capsule(password, packed); assertEquals(c1, c2); exception.expect(Capsule.DecryptionFailedException.class); new Capsule(password + "bad", packed); }
@Test public void testNotSigned() throws Exception { Capsule c1 = new Capsule(); c1.setPublicData("hello", "world", "I'm", "the coffer"); Capsule c2 = new Capsule(); c2.setPublicData("hello", "world", "I'm", "the coffer"); assertEquals(c1, c2); Capsule c4 = new Capsule(c1.pack(), null); assertFalse(c1.isSigned()); assertEquals(c1, c4); assertNotSame(c1, c4); }
public void setPublicData(Object... keysAndValues) { setPublicData(new Binder(keysAndValues)); }
public void setPrivateData(Object... keysAndValues) { setPrivateData(new Binder(keysAndValues)); }
String getSignerId(AbstractKey key) { return getSigner(key).getStringOrThrow("id"); }
@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; }
signed = false; partiallySigned = false; clearSigners();
@Test public void testPartiallySigned() throws Exception { Capsule c1 = new Capsule(); c1.setPublicData("hello", "world", "I'm", "the coffer"); PrivateKey k1 = TestKeys.privateKey(0); PrivateKey k2 = TestKeys.privateKey(1); c1.addSigners(k1, k2); // Let's remove one signature byte[] packed = c1.pack(); Binder b = Boss.unpack(packed); ArrayList<Binder> ss = b.getBinders("signatures"); ss.remove(0); b.put("signatures", ss); packed = Boss.pack(b); // Now it is only partially signed Capsule c3 = new Capsule(packed, null, true, false); assertFalse(c3.isSigned()); assertTrue(c3.isPartiallySigned()); }
@Test public void testEncrypted() throws Exception { Capsule c1 = new Capsule(); c1.setPrivateData("hello", "world", "I'm", "the coffer"); PrivateKey k1 = TestKeys.privateKey(0); PrivateKey k2 = TestKeys.privateKey(1); SymmetricKey k3 = new SymmetricKey(); SymmetricKey k4 = new SymmetricKey(); c1.addKeys(k1.getPublicKey()); c1.addKeys(k3); byte[] packed = c1.pack(); Capsule c2 = new Capsule(packed, new KRing(k3)); assertEquals(c1, c2); c2 = new Capsule(packed, k1.asKeySource()); assertEquals(c1, c2); exception.expect(Capsule.DecryptionFailedException.class); c2 = new Capsule(packed, new KRing(k2)); assertEquals(c1, c2); }
/** * Retreive extra information of the signer. * * @param key * * @return any extra data associated to the key. Empty {@link Binder} instance if there is no * associated information * * @throws IllegalArgumentException * if key is not found */ public Binder getSignerData(AbstractKey key) { return getSigner(key).getBinder("data"); }