예제 #1
0
        //private void doTestUserAttributeEncoding()
        //{
        //    PgpPublicKeyRing pkr = new PgpPublicKeyRing(okAttr);

        //    CheckUserAttribute("normal", pkr, pkr.GetPublicKey());

        //    pkr = new PgpPublicKeyRing(attrLongLength);

        //    CheckUserAttribute("long", pkr, pkr.GetPublicKey());
        //}

        //private void CheckUserAttribute(String type, PgpPublicKeyRing pkr, PgpPublicKey masterPk)
        //{
        //    foreach (PgpUserAttributeSubpacketVector attr in pkr.GetPublicKey().GetUserAttributes())
        //    {
        //        foreach (PgpSignature sig in masterPk.GetSignaturesForUserAttribute(attr))
        //        {
        //            sig.InitVerify(masterPk);
        //            if (!sig.VerifyCertification(attr, masterPk))
        //            {
        //                Fail("user attribute sig failed to verify on " + type);
        //            }
        //        }
        //    }
        //}

        /*private void doTestKeyFlagsValues()
         * {
         *  checkValue(KeyFlags.CertifyOther, 0x01);
         *  checkValue(KeyFlags.SignData, 0x02);
         *  checkValue(KeyFlags.EncryptCommunications, 0x04);
         *  checkValue(KeyFlags.EncryptStorage, 0x08);
         *  checkValue(KeyFlags.Split, 0x10);
         *  checkValue(KeyFlags.Authentication, 0x20);
         *  checkValue(KeyFlags.Shared, 0x80);
         *
         *  // yes this actually happens
         *  checkValue(new byte[] { 4, 0, 0, 0 }, 0x04);
         *  checkValue(new byte[] { 4, 0, 0 }, 0x04);
         *  checkValue(new byte[] { 4, 0 }, 0x04);
         *  checkValue(new byte[] { 4 }, 0x04);
         * }
         *
         * private void checkValue(KeyFlags flag, int val)
         * {
         *  Sig.KeyFlags f = new Sig.KeyFlags(true, flag);
         *  if ((int)f.Flags != val)
         *  {
         *      Fail("flag value mismatch");
         *  }
         * }
         *
         * private void checkValue(byte[] flag, int val)
         * {
         *  Sig.KeyFlags f = new Sig.KeyFlags(true, false, flag);
         *  if ((int)f.Flags != val)
         *  {
         *      Fail("flag value mismatch");
         *  }
         * }*/

        private void doTestMissingSubpackets(byte[] signature)
        {
            PgpSignature sig = new PgpSignature(signature);

            if (sig.Version > 3)
            {
                PgpSignatureAttributes v = sig.HashedAttributes;
                Assert.IsFalse(v.KeyExpirationTime.HasValue);
            }
            else
            {
                /*if (sig.GetHashedSubPackets() != null)
                 * {
                 *  Fail("hashed sub packets found when none expected");
                 * }
                 *
                 * if (sig.GetUnhashedSubPackets() != null)
                 * {
                 *  Fail("unhashed sub packets found when none expected");
                 * }
                 *
                 * if (sig.HasSubpackets)
                 * {
                 *  Fail("HasSubpackets property was true with no packets");
                 * }*/
            }
        }
예제 #2
0
        public void PerformTest()
        {
            //
            // RSA tests
            //
            PgpSecretKeyRing pgpPriv    = new PgpSecretKeyRing(rsaKeyRing);
            PgpSecretKey     secretKey  = pgpPriv.GetSecretKey();
            PgpPrivateKey    pgpPrivKey = secretKey.ExtractPrivateKey(rsaPass);

            //
            // certifications
            //
            var revocation = PgpCertification.GenerateKeyRevocation(secretKey, pgpPrivKey, secretKey);

            Assert.IsTrue(revocation.Verify(secretKey));
            Assert.IsTrue(revocation.Verify());

            PgpSecretKeyRing pgpDSAPriv    = new PgpSecretKeyRing(dsaKeyRing);
            PgpSecretKey     secretDSAKey  = pgpDSAPriv.GetSecretKey();
            PgpPrivateKey    pgpPrivDSAKey = secretDSAKey.ExtractPrivateKey(dsaPass);

            var hashedAttributes = new PgpSignatureAttributes();

            hashedAttributes.SetSignatureExpirationTime(false, TEST_EXPIRATION_TIME);
            hashedAttributes.SetSignerUserId(true, TEST_USER_ID);
            hashedAttributes.SetPreferredCompressionAlgorithms(false, PREFERRED_COMPRESSION_ALGORITHMS);
            hashedAttributes.SetPreferredHashAlgorithms(false, PREFERRED_HASH_ALGORITHMS);
            hashedAttributes.SetPreferredSymmetricAlgorithms(false, PREFERRED_SYMMETRIC_ALGORITHMS);

            var subkeyBinding = PgpCertification.GenerateSubkeyBinding(
                secretDSAKey,
                pgpPrivDSAKey,
                secretKey,
                hashedAttributes);

            Assert.IsTrue(subkeyBinding.Verify(secretDSAKey));

            PgpSignatureAttributes hashedPcks   = subkeyBinding.HashedAttributes;
            PgpSignatureAttributes unhashedPcks = subkeyBinding.UnhashedAttributes;

            Assert.AreEqual(TEST_USER_ID, hashedPcks.SignerUserId);
            Assert.AreEqual(TEST_EXPIRATION_TIME, hashedPcks.SignatureExpirationTime);
            Assert.AreEqual(secretDSAKey.KeyId, unhashedPcks.IssuerKeyId);

            preferredAlgorithmCheck("compression", PREFERRED_COMPRESSION_ALGORITHMS, hashedPcks.PreferredCompressionAlgorithms);
            preferredAlgorithmCheck("hash", PREFERRED_HASH_ALGORITHMS, hashedPcks.PreferredHashAlgorithms);
            preferredAlgorithmCheck("symmetric", PREFERRED_SYMMETRIC_ALGORITHMS, hashedPcks.PreferredSymmetricAlgorithms);

            /*SignatureSubpacketTag[] criticalHashed = hashedPcks.GetCriticalTags();
             *
             * if (criticalHashed.Length != 1)
             * {
             *  Fail("wrong number of critical packets found.");
             * }
             *
             * if (criticalHashed[0] != SignatureSubpacketTag.SignerUserId)
             * {
             *  Fail("wrong critical packet found in tag list.");
             * }*/

            //
            // no packets passed
            //

            subkeyBinding = PgpCertification.GenerateSubkeyBinding(
                secretDSAKey,
                pgpPrivDSAKey,
                secretKey);

            Assert.IsTrue(subkeyBinding.Verify(secretDSAKey));

            hashedPcks = subkeyBinding.HashedAttributes;
            Assert.IsTrue(hashedPcks.SignatureCreationTime.HasValue);

            unhashedPcks = subkeyBinding.UnhashedAttributes;
            Assert.IsTrue(unhashedPcks.IssuerKeyId.HasValue);

            /*try
             * {
             *  sig.VerifyRevocation(secretKey.PublicKey);
             *
             *  Fail("failed to detect non-key signature.");
             * }
             * catch (InvalidOperationException)
             * {
             *  // expected
             * }*/

            //
            // override hash packets
            //
            DateTime creationTime = new DateTime(1973, 7, 27);

            hashedAttributes = new PgpSignatureAttributes();
            hashedAttributes.SetSignatureCreationTime(false, creationTime);

            subkeyBinding = PgpCertification.GenerateSubkeyBinding(
                secretDSAKey,
                pgpPrivDSAKey,
                secretKey,
                hashedAttributes);

            Assert.IsTrue(subkeyBinding.Verify(secretDSAKey));

            hashedPcks = subkeyBinding.HashedAttributes;

            Assert.IsTrue(hashedPcks.SignatureCreationTime.HasValue);
            Assert.AreEqual(creationTime, hashedPcks.SignatureCreationTime);

            preferredAlgorithmCheck("compression", null, hashedPcks.PreferredCompressionAlgorithms);
            preferredAlgorithmCheck("hash", null, hashedPcks.PreferredHashAlgorithms);
            preferredAlgorithmCheck("symmetric", null, hashedPcks.PreferredSymmetricAlgorithms);

            Assert.AreEqual(null, hashedPcks.KeyExpirationTime);
            Assert.AreEqual(null, hashedPcks.SignatureExpirationTime);
            Assert.AreEqual(null, hashedPcks.SignerUserId);

            unhashedPcks = subkeyBinding.UnhashedAttributes;

            Assert.IsTrue(unhashedPcks.IssuerKeyId.HasValue);

            //
            // general signatures
            //
            doTestSig(PgpHashAlgorithm.Sha256, secretKey, pgpPrivKey);
            doTestSig(PgpHashAlgorithm.Sha384, secretKey, pgpPrivKey);
            doTestSig(PgpHashAlgorithm.Sha512, secretKey, pgpPrivKey);
            doTestSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, version: 3);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA_WITH_CRLF, TEST_DATA_WITH_CRLF);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA, TEST_DATA_WITH_CRLF);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA_WITH_CRLF, TEST_DATA_WITH_CRLF, version: 3);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA, TEST_DATA_WITH_CRLF, version: 3);

            //
            // DSA Tests
            //
            pgpPriv    = new PgpSecretKeyRing(dsaKeyRing);
            secretKey  = pgpPriv.GetSecretKey();
            pgpPrivKey = secretKey.ExtractPrivateKey(dsaPass);


            doTestSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey);
            doTestSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, version: 3);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA_WITH_CRLF, TEST_DATA_WITH_CRLF);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA, TEST_DATA_WITH_CRLF);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA_WITH_CRLF, TEST_DATA_WITH_CRLF, version: 3);
            doTestTextSig(PgpHashAlgorithm.Sha1, secretKey, pgpPrivKey, TEST_DATA, TEST_DATA_WITH_CRLF, version: 3);

            // special cases
            //
            doTestMissingSubpackets(nullPacketsSubKeyBinding);

            //doTestMissingSubpackets(generateV3BinarySig(pgpPrivKey, HashAlgorithmTag.Sha1));

            // keyflags
            //doTestKeyFlagsValues();

            // TODO Seems to depend on some other functionality that's yet to be ported
            //doTestUserAttributeEncoding();
        }