public void testEncryptionDecryption() { EncryptParams encryptParams = new EncryptParams( net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, 0); Blob privateKeyBlob = new Blob(net.named_data.jndn.util.Common.base64Decode(PRIVATE_KEY), false); Blob publicKeyBlob = new Blob(net.named_data.jndn.util.Common.base64Decode(PUBLIC_KEY), false); DecryptKey decryptKey = new DecryptKey(privateKeyBlob); EncryptKey encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(decryptKey .getKeyBits()); Blob encodedPublic = publicKeyBlob; Blob derivedPublicKey = encryptKey.getKeyBits(); Assert.AssertTrue(encodedPublic.equals(derivedPublicKey)); Blob plainBlob = new Blob(PLAINTEXT, false); Blob encryptBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams); Blob receivedBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(decryptKey.getKeyBits(), encryptBlob, encryptParams); Assert.AssertTrue(plainBlob.equals(receivedBlob)); Blob cipherBlob = new Blob(CIPHERTEXT_OAEP, false); Blob decryptedBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams); Assert.AssertTrue(plainBlob.equals(decryptedBlob)); // Now test RsaPkcs. encryptParams = new EncryptParams(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaPkcs, 0); encryptBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.encrypt(encryptKey.getKeyBits(), plainBlob, encryptParams); receivedBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(decryptKey.getKeyBits(), encryptBlob, encryptParams); Assert.AssertTrue(plainBlob.equals(receivedBlob)); cipherBlob = new Blob(CIPHERTEXT_PKCS, false); decryptedBlob = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(decryptKey.getKeyBits(), cipherBlob, encryptParams); Assert.AssertTrue(plainBlob.equals(decryptedBlob)); }
public void testFullName() { Data data = new Data(); try { data.wireDecode(codedData); } catch (EncodingException ex) { Assert.Fail("Can't decode codedData"); } // Check the full name format. Assert.AssertEquals(data.getName().size() + 1, data.getFullName().size()); Assert.AssertEquals(data.getName(), data.getFullName().getPrefix(-1)); Assert.AssertEquals(32, data.getFullName().get(-1).getValue().size()); // Check the independent digest calculation. Blob newDigest = new Blob(net.named_data.jndn.util.Common.digestSha256(codedData)); Assert.AssertTrue(newDigest.equals(data.getFullName().get(-1).getValue())); // Check the expected URI. Assert.AssertEquals( "/ndn/abc/sha256digest=" + "96556d685dcb1af04be4ae57f0e7223457d4055ea9b3d07c0d337bef4a8b3ee9", data.getFullName().toUri()); // Changing the Data packet should change the full name. Name saveFullName = new Name(data.getFullName()); data.setContent(new Blob()); Assert.AssertFalse(data.getFullName().get(-1).equals(saveFullName.get(-1))); }
public void testGenericSignature() { // Test correct encoding. GenericSignature signature = new GenericSignature(); signature.setSignatureInfoEncoding(new Blob(experimentalSignatureInfo, false), -1); Blob signatureValue = new Blob(toBuffer(new int[] { 1, 2, 3, 4 }), false); signature.setSignature(signatureValue); freshData.setSignature(signature); Blob encoding = freshData.wireEncode(); Data decodedData = new Data(); try { decodedData.wireDecode(encoding); } catch (EncodingException ex) { Assert.Fail("Error decoding Data with GenericSignature: " + ex); } GenericSignature decodedSignature = (GenericSignature) decodedData .getSignature(); Assert.AssertEquals(experimentalSignatureType, decodedSignature.getTypeCode()); Assert.AssertTrue(new Blob(experimentalSignatureInfo, false) .equals(decodedSignature.getSignatureInfoEncoding())); Assert.AssertTrue(signatureValue.equals(decodedSignature.getSignature())); // Test bad encoding. signature = new GenericSignature(); signature.setSignatureInfoEncoding(new Blob( experimentalSignatureInfoNoSignatureType, false), -1); signature.setSignature(signatureValue); freshData.setSignature(signature); bool gotError = true; try { freshData.wireEncode(); gotError = false; } catch (Exception ex_0) { } if (!gotError) Assert.Fail("Expected encoding error for experimentalSignatureInfoNoSignatureType"); signature = new GenericSignature(); signature.setSignatureInfoEncoding(new Blob( experimentalSignatureInfoBadTlv, false), -1); signature.setSignature(signatureValue); freshData.setSignature(signature); gotError = true; try { freshData.wireEncode(); gotError = false; } catch (Exception ex_1) { } if (!gotError) Assert.Fail("Expected encoding error for experimentalSignatureInfoBadTlv"); }
public void testRegisterPrefixResponse() { Name prefixName = new Name("/test"); int[] interestCallbackCount_0 = new int[] { 0 }; int[] failedCallbackCount_1 = new int[] { 0 }; faceIn.registerPrefix(prefixName, new TestFaceCallRegisterMethods.Anonymous_C3 (this, interestCallbackCount_0), new TestFaceCallRegisterMethods.Anonymous_C2 (failedCallbackCount_1)); // Give the "server" time to register the interest. double timeout = 1000; double startTime = getNowMilliseconds(); while (getNowMilliseconds() - startTime < timeout) { try { faceIn.processEvents(); } catch (IOException ex) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex); break; } catch (EncodingException ex_2) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex_2); break; } try { // We need to sleep for a few milliseconds so we don't use 100% of the CPU. ILOG.J2CsMapping.Threading.ThreadWrapper.sleep(10); } catch (ThreadInterruptedException ex_3) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex_3); break; } } // Now express an interest on this new face, and see if onInterest is called. // Add the timestamp so it is unique and we don't get a cached response. int[] dataCallbackCount_4 = new int[] { 0 }; int[] timeoutCallbackCount_5 = new int[] { 0 }; Data[] receivedData_6 = new Data[1]; Name interestName = prefixName.append("hello" + getNowMilliseconds()); faceOut.expressInterest(interestName, new TestFaceCallRegisterMethods.Anonymous_C1 (receivedData_6, dataCallbackCount_4), new TestFaceCallRegisterMethods.Anonymous_C0 (timeoutCallbackCount_5)); // Process events for the in and out faces. timeout = 10000; startTime = getNowMilliseconds(); while (getNowMilliseconds() - startTime < timeout) { try { faceIn.processEvents(); faceOut.processEvents(); } catch (IOException ex_7) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex_7); break; } catch (EncodingException ex_8) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex_8); break; } bool done = true; if (interestCallbackCount_0[0] == 0 && failedCallbackCount_1[0] == 0) // Still processing faceIn. done = false; if (dataCallbackCount_4[0] == 0 && timeoutCallbackCount_5[0] == 0) // Still processing face_out. done = false; if (done) break; try { // We need to sleep for a few milliseconds so we don't use 100% of the CPU. ILOG.J2CsMapping.Threading.ThreadWrapper.sleep(10); } catch (ThreadInterruptedException ex_9) { logger.log(ILOG.J2CsMapping.Util.Logging.Level.SEVERE, null, ex_9); break; } } AssertEquals("Failed to register prefix at all", 0, failedCallbackCount_1[0]); AssertEquals("Expected 1 onInterest callback", 1, interestCallbackCount_0[0]); AssertEquals("Expected 1 onData callback", 1, dataCallbackCount_4[0]); // Check the message content. Blob expectedBlob = new Blob("SUCCESS"); AssertTrue( "Data received on the face does not match the expected format", expectedBlob.equals(receivedData_6[0].getContent())); }
public void testContentAsymmetricEncryptLarge() { /* foreach */ foreach (TestEncryptor.AsymmetricEncryptInput input in encryptorRsaTestInputs) { Blob largeContent = new Blob(toBuffer(new int[] { 0x73, 0x5a, 0xbd, 0x47, 0x0c, 0xfe, 0xf8, 0x7d, 0x2e, 0x17, 0xaa, 0x11, 0x6f, 0x23, 0xc5, 0x10, 0x23, 0x36, 0x88, 0xc4, 0x2a, 0x0f, 0x9a, 0x72, 0x54, 0x31, 0xa8, 0xb3, 0x51, 0x18, 0x9f, 0x0e, 0x1b, 0x93, 0x62, 0xd9, 0xc4, 0xf5, 0xf4, 0x3d, 0x61, 0x9a, 0xca, 0x05, 0x65, 0x6b, 0xc6, 0x41, 0xf9, 0xd5, 0x1c, 0x67, 0xc1, 0xd0, 0xd5, 0x6f, 0x7b, 0x70, 0xb8, 0x8f, 0xdb, 0x19, 0x68, 0x7c, 0xe0, 0x2d, 0x04, 0x49, 0xa9, 0xa2, 0x77, 0x4e, 0xfc, 0x60, 0x0d, 0x7c, 0x1b, 0x93, 0x6c, 0xd2, 0x61, 0xc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 0x77, 0x07, 0x1d, 0x6d, 0xb5, 0x65, 0x17, 0xdf, 0x76, 0x6f, 0xb5, 0x43, 0xde, 0x71, 0xac, 0xf1, 0x22, 0xbf, 0xb2, 0xe5, 0xd9 }), false); Data data = new Data(); RsaKeyParams rsaParams = new RsaKeyParams(1024); Name keyName = new Name("test"); DecryptKey decryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(rsaParams); EncryptKey encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(decryptKey .getKeyBits()); Blob eKey = encryptKey.getKeyBits(); Blob dKey = decryptKey.getKeyBits(); EncryptParams encryptParams = new EncryptParams(input.type()); net.named_data.jndn.encrypt.algo.Encryptor.encryptData(data, largeContent, keyName, eKey, encryptParams); Assert.AssertEquals(input.testName(), new Name("/FOR").append(keyName), data.getName()); Blob largeDataContent = data.getContent(); // largeDataContent is a sequence of the two EncryptedContent. EncryptedContent encryptedNonce = new EncryptedContent(); encryptedNonce.wireDecode(largeDataContent); Assert.AssertEquals(input.testName(), keyName, encryptedNonce .getKeyLocator().getKeyName()); Assert.AssertEquals(input.testName(), 0, encryptedNonce.getInitialVector() .size()); Assert.AssertEquals(input.testName(), input.type(), encryptedNonce.getAlgorithmType()); // Use the size of encryptedNonce to find the start of encryptedPayload. ByteBuffer payloadContent = largeDataContent.buf().duplicate(); payloadContent.position(encryptedNonce.wireEncode().size()); EncryptedContent encryptedPayload = new EncryptedContent(); encryptedPayload.wireDecode(payloadContent); Name nonceKeyName = new Name(keyName); nonceKeyName.append("nonce"); Assert.AssertEquals(input.testName(), nonceKeyName, encryptedPayload .getKeyLocator().getKeyName()); Assert.AssertEquals(input.testName(), 16, encryptedPayload .getInitialVector().size()); Assert.AssertEquals(input.testName(), net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc, encryptedPayload.getAlgorithmType()); Assert.AssertTrue(input.testName(), encryptedNonce.wireEncode().size() + encryptedPayload.wireEncode().size() == largeDataContent .size()); Blob blobNonce = encryptedNonce.getPayload(); Blob nonce = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(dKey, blobNonce, encryptParams); encryptParams.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.AesCbc); encryptParams.setInitialVector(encryptedPayload.getInitialVector()); Blob bufferPayload = encryptedPayload.getPayload(); Blob largePayload = net.named_data.jndn.encrypt.algo.AesAlgorithm.decrypt(nonce, bufferPayload, encryptParams); Assert.AssertTrue(input.testName(), largeContent.equals(largePayload)); } }
public void testContentAsymmetricEncryptSmall() { /* foreach */ foreach (TestEncryptor.AsymmetricEncryptInput input in encryptorRsaTestInputs) { Blob rawContent = new Blob(toBuffer(new int[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x73 }), false); Data data = new Data(); RsaKeyParams rsaParams = new RsaKeyParams(1024); Name keyName = new Name("test"); DecryptKey decryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(rsaParams); EncryptKey encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(decryptKey .getKeyBits()); Blob eKey = encryptKey.getKeyBits(); Blob dKey = decryptKey.getKeyBits(); EncryptParams encryptParams = new EncryptParams(input.type()); net.named_data.jndn.encrypt.algo.Encryptor.encryptData(data, rawContent, keyName, eKey, encryptParams); Assert.AssertEquals(input.testName(), new Name("/FOR").append(keyName), data.getName()); EncryptedContent extractContent = new EncryptedContent(); extractContent.wireDecode(data.getContent()); Assert.AssertEquals(input.testName(), keyName, extractContent .getKeyLocator().getKeyName()); Assert.AssertEquals(input.testName(), 0, extractContent.getInitialVector() .size()); Assert.AssertEquals(input.testName(), input.type(), extractContent.getAlgorithmType()); Blob recovered = extractContent.getPayload(); Blob decrypted = net.named_data.jndn.encrypt.algo.RsaAlgorithm.decrypt(dKey, recovered, encryptParams); Assert.AssertTrue(input.testName(), rawContent.equals(decrypted)); } }
public void testConstructor() { // Check default settings. EncryptedContent content = new EncryptedContent(); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.NONE, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); // Check an encrypted content with IV. KeyLocator keyLocator = new KeyLocator(); keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); keyLocator.getKeyName().set("/test/key/locator"); EncryptedContent rsaOaepContent = new EncryptedContent(); rsaOaepContent.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep) .setKeyLocator(keyLocator).setPayload(new Blob(message, false)) .setInitialVector(new Blob(iv, false)); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContent.getAlgorithmType()); Assert.AssertTrue(rsaOaepContent.getPayload().equals(new Blob(message, false))); Assert.AssertTrue(rsaOaepContent.getInitialVector() .equals(new Blob(iv, false))); Assert.AssertTrue(rsaOaepContent.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContent.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Encoding. Blob encryptedBlob = new Blob(encrypted, false); Blob encoded = rsaOaepContent.wireEncode(); Assert.AssertTrue(encryptedBlob.equals(encoded)); // Decoding. EncryptedContent rsaOaepContent2 = new EncryptedContent(); rsaOaepContent2.wireDecode(encryptedBlob); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContent2.getAlgorithmType()); Assert.AssertTrue(rsaOaepContent2.getPayload() .equals(new Blob(message, false))); Assert.AssertTrue(rsaOaepContent2.getInitialVector().equals( new Blob(iv, false))); Assert.AssertTrue(rsaOaepContent2.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContent2.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Check the no IV case. EncryptedContent rsaOaepContentNoIv = new EncryptedContent(); rsaOaepContentNoIv.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep) .setKeyLocator(keyLocator).setPayload(new Blob(message, false)); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContentNoIv.getAlgorithmType()); Assert.AssertTrue(rsaOaepContentNoIv.getPayload().equals( new Blob(message, false))); Assert.AssertTrue(rsaOaepContentNoIv.getInitialVector().isNull()); Assert.AssertTrue(rsaOaepContentNoIv.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContentNoIv.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); // Encoding. Blob encryptedBlob2 = new Blob(encryptedNoIv, false); Blob encodedNoIV = rsaOaepContentNoIv.wireEncode(); Assert.AssertTrue(encryptedBlob2.equals(encodedNoIV)); // Decoding. EncryptedContent rsaOaepContentNoIv2 = new EncryptedContent(); rsaOaepContentNoIv2.wireDecode(encryptedBlob2); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, rsaOaepContentNoIv2.getAlgorithmType()); Assert.AssertTrue(rsaOaepContentNoIv2.getPayload().equals( new Blob(message, false))); Assert.AssertTrue(rsaOaepContentNoIv2.getInitialVector().isNull()); Assert.AssertTrue(rsaOaepContentNoIv2.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(rsaOaepContentNoIv2.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); }
public void testSetterGetter() { EncryptedContent content = new EncryptedContent(); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.NONE, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); content.setAlgorithmType(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep); Assert.AssertEquals(net.named_data.jndn.encrypt.algo.EncryptAlgorithmType.RsaOaep, content.getAlgorithmType()); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); Assert.AssertEquals(net.named_data.jndn.KeyLocatorType.NONE, content.getKeyLocator().getType()); KeyLocator keyLocator = new KeyLocator(); keyLocator.setType(net.named_data.jndn.KeyLocatorType.KEYNAME); keyLocator.getKeyName().set("/test/key/locator"); content.setKeyLocator(keyLocator); Assert.AssertTrue(content.getKeyLocator().getType() != net.named_data.jndn.KeyLocatorType.NONE); Assert.AssertTrue(content.getKeyLocator().getKeyName() .equals(new Name("/test/key/locator"))); Assert.AssertEquals(true, content.getPayload().isNull()); Assert.AssertEquals(true, content.getInitialVector().isNull()); content.setPayload(new Blob(message, false)); Assert.AssertTrue(content.getPayload().equals(new Blob(message, false))); content.setInitialVector(new Blob(iv, false)); Assert.AssertTrue(content.getInitialVector().equals(new Blob(iv, false))); Blob encoded = content.wireEncode(); Blob contentBlob = new Blob(encrypted, false); Assert.AssertTrue(contentBlob.equals(encoded)); }