public void testDatabaseFunctions() { Blob scheduleBlob = new Blob(SCHEDULE, false); // Create a schedule. Schedule schedule = new Schedule(); try { schedule.wireDecode(scheduleBlob); } catch (EncodingException ex) { // We don't expect this to happen. Fail("Error decoding Schedule: " + ex.Message); } // Create a member. RsaKeyParams // Create a member. paras = new RsaKeyParams(); DecryptKey decryptKey; EncryptKey encryptKey; try { decryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()); } catch (Exception ex_0) { // Don't expect this to happen. Fail("Error creating test keys: " + ex_0.Message); return; } Blob keyBlob = encryptKey.getKeyBits(); Name name1 = new Name("/ndn/BoyA/ksk-123"); Name name2 = new Name("/ndn/BoyB/ksk-1233"); Name name3 = new Name("/ndn/GirlC/ksk-123"); Name name4 = new Name("/ndn/GirlD/ksk-123"); Name name5 = new Name("/ndn/Hello/ksk-123"); // Add schedules into the database. try { database.addSchedule("work-time", schedule); database.addSchedule("rest-time", schedule); database.addSchedule("play-time", schedule); database.addSchedule("boelter-time", schedule); } catch (Exception ex_1) { Fail("Unexpected error adding a schedule: " + ex_1.Message); } // Throw an exception when adding a schedule with an existing name. bool gotError = true; try { database.addSchedule("boelter-time", schedule); gotError = false; } catch (GroupManagerDb.Error ex_2) { } if (!gotError) Fail("Expected an error adding a duplicate schedule"); // Add members into the database. try { database.addMember("work-time", name1, keyBlob); database.addMember("rest-time", name2, keyBlob); database.addMember("play-time", name3, keyBlob); database.addMember("play-time", name4, keyBlob); } catch (Exception ex_3) { Fail("Unexpected error adding a member: " + ex_3.Message); } // Throw an exception when adding a member with a non-existing schedule name. gotError = true; try { database.addMember("false-time", name5, keyBlob); gotError = false; } catch (GroupManagerDb.Error ex_4) { } if (!gotError) Fail("Expected an error adding a member with non-existing schedule"); try { database.addMember("boelter-time", name5, keyBlob); } catch (Exception ex_5) { Fail("Unexpected error adding a member: " + ex_5.Message); } // Throw an exception when adding a member having an existing identity. gotError = true; try { database.addMember("work-time", name5, keyBlob); gotError = false; } catch (GroupManagerDb.Error ex_6) { } if (!gotError) Fail("Expected an error adding a member with an existing identity"); // Test has functions. AssertEquals(true, database.hasSchedule("work-time")); AssertEquals(true, database.hasSchedule("rest-time")); AssertEquals(true, database.hasSchedule("play-time")); AssertEquals(false, database.hasSchedule("sleep-time")); AssertEquals(false, database.hasSchedule("")); AssertEquals(true, database.hasMember(new Name("/ndn/BoyA"))); AssertEquals(true, database.hasMember(new Name("/ndn/BoyB"))); AssertEquals(false, database.hasMember(new Name("/ndn/BoyC"))); // Get a schedule. Schedule scheduleResult = database.getSchedule("work-time"); AssertTrue(scheduleResult.wireEncode().equals(scheduleBlob)); scheduleResult = database.getSchedule("play-time"); AssertTrue(scheduleResult.wireEncode().equals(scheduleBlob)); // Throw an exception when when there is no such schedule in the database. gotError = true; try { database.getSchedule("work-time-11"); gotError = false; } catch (GroupManagerDb.Error ex_7) { } if (!gotError) Fail("Expected an error getting a non-existing schedule"); // List all schedule names. IList names = database.listAllScheduleNames(); AssertTrue(names.Contains("work-time")); AssertTrue(names.Contains("play-time")); AssertTrue(names.Contains("rest-time")); AssertTrue(!names.Contains("sleep-time")); // List members of a schedule. IDictionary memberMap = database.getScheduleMembers("play-time"); AssertTrue(memberMap.Count != 0); // When there's no such schedule, the return map's size should be 0. AssertEquals(0, database.getScheduleMembers("sleep-time").Count); // List all members. IList members = database.listAllMembers(); AssertTrue(members.Contains(new Name("/ndn/GirlC"))); AssertTrue(members.Contains(new Name("/ndn/GirlD"))); AssertTrue(members.Contains(new Name("/ndn/BoyA"))); AssertTrue(members.Contains(new Name("/ndn/BoyB"))); // Rename a schedule. AssertEquals(true, database.hasSchedule("boelter-time")); database.renameSchedule("boelter-time", "rieber-time"); AssertEquals(false, database.hasSchedule("boelter-time")); AssertEquals(true, database.hasSchedule("rieber-time")); AssertEquals("rieber-time", database.getMemberSchedule(new Name("/ndn/Hello"))); // Update a schedule. Schedule newSchedule = new Schedule(); try { newSchedule.wireDecode(scheduleBlob); } catch (EncodingException ex_8) { // We don't expect this to happen. Fail("Error decoding Schedule: " + ex_8.Message); } RepetitiveInterval repetitiveInterval = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150921T000000"), 2, 10, 5, net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.DAY); newSchedule.addWhiteInterval(repetitiveInterval); database.updateSchedule("rieber-time", newSchedule); scheduleResult = database.getSchedule("rieber-time"); AssertTrue(!scheduleResult.wireEncode().equals(scheduleBlob)); AssertTrue(scheduleResult.wireEncode().equals(newSchedule.wireEncode())); // Add a new schedule when updating a non-existing schedule. AssertEquals(false, database.hasSchedule("ralphs-time")); database.updateSchedule("ralphs-time", newSchedule); AssertEquals(true, database.hasSchedule("ralphs-time")); // Update the schedule of a member. database.updateMemberSchedule(new Name("/ndn/Hello"), "play-time"); AssertEquals("play-time", database.getMemberSchedule(new Name("/ndn/Hello"))); // Delete a member. AssertEquals(true, database.hasMember(new Name("/ndn/Hello"))); database.deleteMember(new Name("/ndn/Hello")); AssertEquals(false, database.hasMember(new Name("/ndn/Hello"))); // Delete a non-existing member. try { database.deleteMember(new Name("/ndn/notExisting")); } catch (Exception ex_9) { Fail("Unexpected error deleting a non-existing member: " + ex_9.Message); } // Delete a schedule. All the members using this schedule should be deleted. database.deleteSchedule("play-time"); AssertEquals(false, database.hasSchedule("play-time")); AssertEquals(false, database.hasMember(new Name("/ndn/GirlC"))); AssertEquals(false, database.hasMember(new Name("/ndn/GirlD"))); // Delete a non-existing schedule. try { database.deleteSchedule("not-existing-time"); } catch (Exception ex_10) { Fail("Unexpected error deleting a non-existing schedule: " + ex_10.Message); } }
/// <summary> /// Generate a new random decrypt key for RSA based on the given params. /// </summary> /// /// <param name="params">The key params with the key size (in bits).</param> /// <returns>The new decrypt key (PKCS8-encoded private key).</returns> public static DecryptKey generateKey(RsaKeyParams paras) { KeyPairGenerator generator = System.KeyPairGenerator.getInstance("RSA"); generator.initialize(paras.getKeySize()); KeyPair pair = generator.generateKeyPair(); return new DecryptKey(new Blob(pair.getPrivate().getEncoded(), false)); }
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, 0xc4, 0x6b, 0x01, 0xe9, 0x12, 0x28, 0x6d, 0xf5, 0x78, 0xe9, 0x99, 0x0b, 0x9c, 0x4f, 0x90, 0x34, 0x3e, 0x06, 0x92, 0x57, 0xe3, 0x7a, 0x8f, 0x13, 0xc7, 0xf3, 0xfe, 0xf0, 0xe2, 0x59, 0x48, 0x15, 0xb9, 0xdb, 0x77, 0x07, 0x1d, 0x6d, 0xb5, 0x65, 0x17, 0xdf, 0x76, 0x6f, 0xb5, 0x43, 0xde, 0x71, 0xac, 0xf1, 0x22, 0xbf, 0xb2, 0xe5, 0xd9, 0x22, 0xf1, 0x67, 0x76, 0x71, 0x0c, 0xff, 0x99, 0x7b, 0x94, 0x9b, 0x24, 0x20, 0x80, 0xe3, 0xcc, 0x06, 0x4a, 0xed, 0xdf, 0xec, 0x50, 0xd5, 0x87, 0x3d, 0xa0, 0x7d, 0x9c, 0xe5, 0x13, 0x10, 0x98, 0x14, 0xc3, 0x90, 0x10, 0xd9, 0x25, 0x9a, 0x59, 0xe9, 0x37, 0x26, 0xfd, 0x87, 0xd7, 0xf4, 0xf9, 0x11, 0x91, 0xad, 0x5c, 0x00, 0x95, 0xf5, 0x2b, 0x37, 0xf7, 0x4e, 0xb4, 0x4b, 0x42, 0x7c, 0xb3, 0xad, 0xd6, 0x33, 0x5f, 0x0b, 0x84, 0x57, 0x7f, 0xa7, 0x07, 0x73, 0x37, 0x4b, 0xab, 0x2e, 0xfb, 0xfe, 0x1e, 0xcb, 0xb6, 0x4a, 0xc1, 0x21, 0x5f, 0xec, 0x92, 0xb7, 0xac, 0x97, 0x75, 0x20, 0xc9, 0xd8, 0x9e, 0x93, 0xd5, 0x12, 0x7a, 0x64, 0xb9, 0x4c, 0xed, 0x49, 0x87, 0x44, 0x5b, 0x4f, 0x90, 0x34, 0x3e, 0x06, 0x92, 0x57, 0xe3, 0x7a, 0x8f, 0x13, 0xc7, 0xf3, 0xfe, 0xf0, 0xe2, 0x59, 0x48, 0x15, 0xb9, 0xdb, 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)); } }
/// <summary> /// Generate an RSA key pair according to keySize_. /// </summary> /// /// <param name="privateKeyBlob"></param> /// <param name="publicKeyBlob"></param> private void generateKeyPair(Blob[] privateKeyBlob, Blob[] publicKeyBlob) { RsaKeyParams paras = new RsaKeyParams(keySize_); DecryptKey privateKey; try { privateKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); } catch (Exception ex) { // We don't expect this error. throw new Exception("Error in RsaAlgorithm.generateKey: " + ex.Message); } privateKeyBlob[0] = privateKey.getKeyBits(); EncryptKey publicKey; try { publicKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(privateKeyBlob[0]); } catch (InvalidKeySpecException ex_0) { // We don't expect this error. throw new Exception("Error in RsaAlgorithm.deriveEncryptKey: " + ex_0.Message); } catch (DerDecodingException ex_1) { // We don't expect this error. throw new Exception("Error in RsaAlgorithm.deriveEncryptKey: " + ex_1.Message); } publicKeyBlob[0] = publicKey.getKeyBits(); }
internal static void generateRsaKeys(Blob[] encryptionKeyBlob, Blob[] decryptionKeyBlob) { RsaKeyParams paras = new RsaKeyParams(); DecryptKey decryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); decryptionKeyBlob[0] = decryptKey.getKeyBits(); EncryptKey encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm .deriveEncryptKey(decryptionKeyBlob[0]); encryptionKeyBlob[0] = encryptKey.getKeyBits(); }
public void setUp() { // Don't show INFO log messages. ILOG.J2CsMapping.Util.Logging.Logger.getLogger("").setLevel(ILOG.J2CsMapping.Util.Logging.Level.WARNING); FileInfo policyConfigDirectory = net.named_data.jndn.tests.integration_tests.IntegrationTestsCommon .getPolicyConfigDirectory(); dKeyDatabaseFilePath = new FileInfo(System.IO.Path.Combine(policyConfigDirectory.FullName,"manager-d-key-test.db")); dKeyDatabaseFilePath.delete(); eKeyDatabaseFilePath = new FileInfo(System.IO.Path.Combine(policyConfigDirectory.FullName,"manager-e-key-test.db")); eKeyDatabaseFilePath.delete(); intervalDatabaseFilePath = new FileInfo(System.IO.Path.Combine(policyConfigDirectory.FullName,"manager-interval-test.db")); intervalDatabaseFilePath.delete(); groupKeyDatabaseFilePath = new FileInfo(System.IO.Path.Combine(policyConfigDirectory.FullName,"manager-group-key-test.db")); groupKeyDatabaseFilePath.delete(); RsaKeyParams paras = new RsaKeyParams(); DecryptKey memberDecryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); decryptKeyBlob = memberDecryptKey.getKeyBits(); EncryptKey memberEncryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm .deriveEncryptKey(decryptKeyBlob); encryptKeyBlob = memberEncryptKey.getKeyBits(); // Generate the certificate. certificate.setName(new Name("/ndn/memberA/KEY/ksk-123/ID-CERT/123")); PublicKey contentPublicKey = new PublicKey(encryptKeyBlob); certificate.setPublicKeyInfo(contentPublicKey); certificate.encode(); Blob signatureInfoBlob = new Blob(SIG_INFO, false); Blob signatureValueBlob = new Blob(SIG_VALUE, false); Signature signature = net.named_data.jndn.encoding.TlvWireFormat.get().decodeSignatureInfoAndValue( signatureInfoBlob.buf(), signatureValueBlob.buf()); certificate.setSignature(signature); certificate.wireEncode(); // Set up the keyChain. MemoryIdentityStorage identityStorage = new MemoryIdentityStorage(); MemoryPrivateKeyStorage privateKeyStorage = new MemoryPrivateKeyStorage(); keyChain = new KeyChain(new IdentityManager(identityStorage, privateKeyStorage), new NoVerifyPolicyManager()); Name identityName = new Name("TestGroupManager"); keyChain.createIdentityAndCertificate(identityName); keyChain.getIdentityManager().setDefaultIdentity(identityName); net.named_data.jndn.encrypt.GroupManager.setFriendAccess(this); }