Exemplo n.º 1
0
        public void testGenerateKey()
        {
            /* foreach */
            foreach (TestTpmPrivateKey.KeyTestData dataSet  in  keyTestData)
            {
                TpmPrivateKey key = net.named_data.jndn.security.tpm.TpmPrivateKey
                                    .generatePrivateKey(dataSet.keyParams);
                Blob publicKeyBits = key.derivePublicKey();

                Blob data = new Blob(new int[] { 0x01, 0x02, 0x03, 0x04 });

                // Sign and verify.
                Blob signature = key.sign(data.buf(), net.named_data.jndn.security.DigestAlgorithm.SHA256);

                bool result = net.named_data.jndn.security.VerificationHelpers.verifySignature(data,
                                                                                               signature, new PublicKey(publicKeyBits));
                Assert.AssertTrue(result);

                // Check that another generated private key is different.
                TpmPrivateKey key2 = net.named_data.jndn.security.tpm.TpmPrivateKey
                                     .generatePrivateKey(dataSet.keyParams);
                Assert.AssertTrue(!key.toPkcs8().equals(key2.toPkcs8()));
            }
        }
Exemplo n.º 2
0
        public void testSaveLoad()
        {
            /* foreach */
            foreach (TestTpmPrivateKey.KeyTestData dataSet  in  keyTestData)
            {
                // Load the key in PKCS #1 format.
                byte[]        pkcs1 = net.named_data.jndn.util.Common.base64Decode(dataSet.privateKeyPkcs1);
                TpmPrivateKey key1  = new TpmPrivateKey();
                try {
                    key1.loadPkcs1(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs1));
                } catch (Exception ex) {
                    Assert.Fail("Unexpected exception: " + ex.Message);
                }

                // Save the key in PKCS #1 format.
                Blob savedPkcs1Key = null;
                try {
                    savedPkcs1Key = key1.toPkcs1();
                } catch (Exception ex_0) {
                    Assert.Fail("Unexpected exception: " + ex_0.Message);
                }
                Assert.AssertTrue(savedPkcs1Key.equals(new Blob(pkcs1)));

                // Load the key in unencrypted PKCS #8 format.
                byte[] pkcs8 = net.named_data.jndn.util.Common
                               .base64Decode(dataSet.privateKeyPkcs8Unencrypted);
                TpmPrivateKey key8 = new TpmPrivateKey();
                try {
                    key8.loadPkcs8(ILOG.J2CsMapping.NIO.ByteBuffer.wrap(pkcs8));
                } catch (Exception ex_1) {
                    Assert.Fail("Unexpected exception: " + ex_1.Message);
                }

                // Save the key in unencrypted PKCS #8 format.
                Blob savedPkcs8Key = null;
                try {
                    savedPkcs8Key = key8.toPkcs8();
                } catch (Exception ex_2) {
                    Assert.Fail("Unexpected exception: " + ex_2.Message);
                }
                Assert.AssertTrue(savedPkcs8Key.equals(new Blob(pkcs8)));

                ByteBuffer password = new Blob("password").buf();

                // Load the key in encrypted PKCS #8 format.
                byte[] encryptedPkcs8 = net.named_data.jndn.util.Common
                                        .base64Decode(dataSet.privateKeyPkcs8);
                TpmPrivateKey encryptedKey8 = new TpmPrivateKey();
                try {
                    encryptedKey8.loadEncryptedPkcs8(
                        ILOG.J2CsMapping.NIO.ByteBuffer.wrap(encryptedPkcs8), password);
                } catch (Exception ex_3) {
                    Assert.Fail("Unexpected exception: " + ex_3.Message);
                }

                // Save the key in encrypted PKCS #8 format and resave as unencrypted.
                Blob savedEncryptedPkcs8Key = null;
                try {
                    savedEncryptedPkcs8Key = encryptedKey8
                                             .toEncryptedPkcs8(password);
                } catch (Exception ex_4) {
                    Assert.Fail("Unexpected exception: " + ex_4.Message);
                }
                key8 = new TpmPrivateKey();
                key8.loadEncryptedPkcs8(savedEncryptedPkcs8Key.buf(), password);
                Blob resavedPkcs8Key = key8.toPkcs8();
                Assert.AssertTrue(resavedPkcs8Key.equals(new Blob(pkcs8)));
            }
        }