@Test public void timoutOnConnection() throws Exception { StreamConnector sca = new StreamConnector(); BitrustedConnector connector = new BitrustedConnector( TestKeys.privateKey(0), sca.getInputStream(), new ByteArrayOutputStream() ); connector.setHandshakeTimeoutMillis(10); try { connector.connect(null); fail("must throw TimeoutException"); } catch (TimeoutException x) { // all ok } }
try { Socket in = ss.accept(); BitrustedConnector bc = new BitrustedConnector(TestKeys.privateKey(0), in.getInputStream(), in.getOutputStream() s.setSoTimeout(100); Farcall f = new Farcall(new BitrustedConnector( TestKeys.privateKey(1), s.getInputStream(), s.getOutputStream()
@Test public void matchAnonymousId() throws Exception { PrivateKey k1 = TestKeys.privateKey(0); byte[] id1 = k1.createAnonymousId(); byte[] id12 = k1.createAnonymousId(); PrivateKey k2 = TestKeys.privateKey(1); byte[] id2 = k2.createAnonymousId(); assertEquals(64, id1.length); assertEquals(64, id12.length); assertEquals(64, id2.length); assertFalse(Arrays.equals(id1, id12)); assertFalse(Arrays.equals(id1, id2)); assertFalse(Arrays.equals(id12, id2)); assertTrue(k1.matchAnonymousId(id1)); assertTrue(k1.matchAnonymousId(id12)); assertTrue(k2.matchAnonymousId(id2)); assertFalse(k2.matchAnonymousId(id1)); assertFalse(k2.matchAnonymousId(id12)); assertFalse(k1.matchAnonymousId(id2)); }
@Test public void unpackKey() throws Exception { AbstractKey k1 = TestKeys.privateKey(3).getPublicKey(); AbstractKey kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); k1 = SymmetricKey.fromPassword("helluva", 4096); kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); k1 = TestKeys.privateKey(2); kx = AbstractKey.fromBinder(k1.toBinder()); assertEquals(k1, kx); }
@Test public void passwordProtectedKeyTest() throws Exception { PrivateKey key = TestKeys.privateKey(3); String password = UUID.randomUUID().toString(); byte[] packed = key.pack(); byte[] packedWithPassword = key.packWithPassword(password); //try unpack password protected try { new PrivateKey(packedWithPassword); fail("exception expected"); } catch (PrivateKey.PasswordProtectedException e) { assertEquals(e.getMessage(),"key is password protected"); } //try unpack password with wrong password try { PrivateKey.unpackWithPassword(packedWithPassword,UUID.randomUUID().toString()); fail("exception expected"); } catch (PrivateKey.PasswordProtectedException e) { assertEquals(e.getMessage(),"wrong password"); } //try unpack plain key with password assertEquals(PrivateKey.unpackWithPassword(packed,UUID.randomUUID().toString()),key); assertEquals(PrivateKey.unpackWithPassword(packedWithPassword,password),key); }
@Test public void testKeys() throws Exception { // Test vectors are for key #1 PrivateKey key = (PrivateKey) TestKeys.privateKey(1); byte[] encrypted = Bytes.fromBase64(encrypted64).toArray(); byte[] decrypted = key.decrypt(encrypted); assertEquals(plainText, new String(decrypted)); PublicKey publicKey = key.getPublicKey(); byte[] encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); publicKey = new PublicKey(Do.decodeBase64(publicKey64)); encrypted2 = publicKey.encrypt(plainText); assertEquals(plainText, new String(key.decrypt(encrypted2))); }
StreamConnector scb = new StreamConnector(); BitrustedConnector ca = new BitrustedConnector( TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream() ); BitrustedConnector cb = new BitrustedConnector( TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream()
@Test public void fingerprint() throws Exception { PrivateKey k = TestKeys.privateKey(0); byte[] f1 = k.getPublicKey().fingerprint(); assertEquals(33, f1.length); byte[] f2 = k.fingerprint(); assertArrayEquals(f1, f2); assertEquals(AbstractKey.FINGERPRINT_SHA256, f1[0]); }
@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 findKey() throws Exception { KeyInfo i1 = new KeyInfo(KeyInfo.PRF.HMAC_SHA256, 1024, null, null); AbstractKey pk1 = i1.derivePassword("helluva"); KeyInfo i2 = new KeyInfo(KeyInfo.PRF.HMAC_SHA256, 1025, null, "the tag".getBytes()); AbstractKey pk2 = i2.derivePassword("helluva"); assertEquals(i2.getTag(), pk2.info().getTag()); KeyRing kr = new KeyRing(); SymmetricKey sk1 = new SymmetricKey(); SymmetricKey sk2 = new SymmetricKey(); AbstractKey privateKey = TestKeys.privateKey(0); AbstractKey publicKey1 =TestKeys.privateKey(1).getPublicKey(); AbstractKey publicKey2 = privateKey.getPublicKey(); kr.addKeys( sk1, sk2, privateKey, publicKey1, publicKey2, pk1, pk2 ); kr.addKeys(pk1, pk2); Binder b = kr.toBinder(); KeyRing kr2 = KeyRing.fromBinder(b); assertTrue(kr.keySet().contains(pk1)); assertTrue(kr.keySet().contains(pk2)); assertEquals(pk2, kr.findKey(i2).toArray()[0]); assertEquals(pk2, kr2.findKey(i2).toArray()[0]); final Collection<AbstractKey> keys = kr.findKey(i1); assertTrue(keys.contains(pk1)); assertTrue(keys.contains(pk1)); assertTrue(keys.contains(sk1)); assertTrue(keys.contains(sk2)); assertEquals(4, kr2.findKey(i1).size()); }
StreamConnector scb = new StreamConnector(); BitrustedConnector ca = new BitrustedConnector( TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream() ); BitrustedConnector cb = new BitrustedConnector( TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream()
@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 publicKeyMustHaveInfo() throws Exception { AbstractKey k = TestKeys.privateKey(3).getPublicKey(); KeyInfo h = k.info(); assertEquals(KeyInfo.Algorythm.RSAPublic, h.getAlgorythm()); assertEquals(KeyInfo.PRF.None, h.getPRF()); assertEquals(5, h.getTag().length); // Bytes.dump(h.pack()); }
@Test public void saveAndRestore() throws Exception { KeyRing kr = new KeyRing(); SymmetricKey sk1 = new SymmetricKey(); SymmetricKey sk2 = new SymmetricKey(); AbstractKey privateKey = TestKeys.privateKey(0); AbstractKey publicKey1 = TestKeys.privateKey(1).getPublicKey(); AbstractKey publicKey2 = privateKey.getPublicKey(); kr.addKeys( sk1, sk2, privateKey, publicKey1, publicKey2); Binder b = kr.toBinder(); KeyRing kr2 = KeyRing.fromBinder(b); for(AbstractKey k: kr2.keySet()) { assertTrue(kr2.contains(k)); assertTrue(kr.contains(k)); } assertEquals(kr, kr2); }
@Test public void privateKeyMustHaveInfo() throws Exception { AbstractKey prk = TestKeys.privateKey(3); AbstractKey puk = prk.getPublicKey(); KeyInfo h = prk.info(); assertEquals(KeyInfo.Algorythm.RSAPrivate, h.getAlgorythm()); assertEquals(KeyInfo.PRF.None, h.getPRF()); assertEquals(5, h.getTag().length); assertArrayEquals(puk.info().getTag(), h.getTag()); // Bytes.dump(h.pack()); }
@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); }
@Test public void signatureTest() throws Exception { PrivateKey privateKey = TestKeys.privateKey(1); PublicKey publicKey = privateKey.getPublicKey(); byte [] signature256 = Do.decodeBase64(signature256_64); final byte[] signature512 = Do.decodeBase64(signature512_64); assertTrue(publicKey.verify(plainText, signature256, HashType.SHA256)); assertFalse(publicKey.verify(plainText+"tampered", signature256, HashType.SHA256)); assertFalse(publicKey.verify(plainText, signature256, HashType.SHA512)); assertTrue(publicKey.verify(plainText, signature512, HashType.SHA512)); assertFalse(publicKey.verify(plainText, signature512, HashType.SHA256)); assertFalse(publicKey.verify(plainText+"tampered", signature512, HashType.SHA512)); }
c1.setPublicData("hello", "world", "I'm", "the coffer"); PrivateKey k1 = TestKeys.privateKey(0); PrivateKey k2 = TestKeys.privateKey(1);
StreamConnector scb = new StreamConnector(); BitrustedConnector ca = new BitrustedConnector( TestKeys.privateKey(0), sca.getInputStream(), scb.getOutputStream() ); BitrustedConnector cb = new BitrustedConnector( TestKeys.privateKey(1), scb.getInputStream(), sca.getOutputStream()
@Test public void matchTypeAndTag() throws Exception { // 2 different private keys AbstractKey k1 = TestKeys.privateKey(0); AbstractKey k2 = TestKeys.privateKey(1); assertTrue(k1.info().matchType(k2.info())); assertFalse(k1.info().matchTag(k2.info())); // public matches private, not vice versa AbstractKey k3 = k1.getPublicKey(); assertTrue(k1.info().matchType(k3.info())); assertFalse(k3.info().matchType(k1.info())); assertTrue(k1.info().matchTag(k3.info())); // public keys do not match each other! assertFalse(k3.info().matchType(k3.info())); assertFalse(k3.info().matchType(k2.getPublicKey().info())); // Check AES match algorythm and tag AbstractKey k4 = new SymmetricKey(); assertFalse(k2.matchType(k4)); assertFalse(k3.matchType(k4)); assertFalse(k4.matchType(k2)); assertFalse(k4.matchType(k3)); assertFalse(k4.matchTag(k2)); assertFalse(k4.matchTag(k3)); AbstractKey k5 = new SymmetricKey(); assertTrue(k4.matchType(k5)); assertTrue(k5.matchType(k4)); assertFalse(k4.matchTag(k5)); k4.setTag("Hello"); k5.setTag("Hello"); }