Пример #1
0
        public void TestSignAndVerify(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var signedoutput = signer.Sign(input);

                    Expect(signer.Verify(signedoutput), Is.True);
                    Expect(verifier.Verify(signedoutput), Is.True);
                }
        }
Пример #2
0
        public void TestSignAndVerifyBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var signedoutput = signer.Sign(input);
                    var badoutput    = signedoutput.ToBytes();
                    badoutput[10] ^= 9;

                    Expect(signer.Verify(badoutput), Is.False);
                    Expect(verifier.Verify(badoutput), Is.False);
                }
        }
Пример #3
0
        public void TestSignAndVerifyMessageBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var signedoutput = signer.Sign(input);
                    var badoutput    = signedoutput.ToBytes();
                    badoutput[10] ^= 9;

                    Expect(() => signer.VerifiedMessage(badoutput), Throws.InstanceOf <InvalidCryptoDataException>());
                    Expect(() => verifier.VerifiedMessage(badoutput), Throws.InstanceOf <InvalidCryptoDataException>());
                }
        }
Пример #4
0
        public void TestSignAndVerifyShort(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var signedoutput = signer.Sign(input);
                    var badoutput    = signedoutput.ToBytes();
                    badoutput[10] ^= 9;
                    var badlength = new byte[12];
                    Array.Copy(badoutput, badlength, badlength.Length);

                    Expect(() => signer.Verify(badlength), Throws.InstanceOf <InvalidCryptoDataException>());
                    Expect(() => verifier.Verify(badlength), Throws.InstanceOf <InvalidCryptoDataException>());
                }
        }
Пример #5
0
        public void TestSignAndTryVerifyMessageBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);
                byte[] verifiedOutput;
                byte[] verifiedOutput2;
                var badoutput = signedoutput.ToBytes();
                badoutput[10] ^= 9;

                Expect(signer.TryGetVerifiedMessage(badoutput, out verifiedOutput), Is.False);

                Expect(verifier.TryGetVerifiedMessage(badoutput, out verifiedOutput2), Is.False);
            }
        }
Пример #6
0
        public void TestSignAndTryVerifyMessageBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var    signedoutput = signer.Sign(input);
                    byte[] verifiedOutput;
                    byte[] verifiedOutput2;
                    var    badoutput = signedoutput.ToBytes();
                    badoutput[10] ^= 9;

                    Expect(signer.TryGetVerifiedMessage(badoutput, out verifiedOutput), Is.False);

                    Expect(verifier.TryGetVerifiedMessage(badoutput, out verifiedOutput2), Is.False);
                }
        }
Пример #7
0
        public void TestSignAndTryVerifyMessage(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);
                string verifiedOutput;
                string verifiedOutput2;

                Expect(signer.TryGetVerifiedMessage(signedoutput, out verifiedOutput), Is.True);

                Expect(verifier.TryGetVerifiedMessage(signedoutput, out verifiedOutput2), Is.True);

                Expect(verifiedOutput, Is.EqualTo(input));

                Expect(verifiedOutput2, Is.EqualTo(input));
            }
        }
Пример #8
0
        public void TestSignAndTryVerifyMessage(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);

            using (var signer = new AttachedSigner(subPath))
                using (var verifier = new AttachedVerifier(subPath))
                {
                    var    signedoutput = signer.Sign(input);
                    string verifiedOutput;
                    string verifiedOutput2;

                    Expect(signer.TryGetVerifiedMessage(signedoutput, out verifiedOutput), Is.True);

                    Expect(verifier.TryGetVerifiedMessage(signedoutput, out verifiedOutput2), Is.True);

                    Expect(verifiedOutput, Is.EqualTo(input));

                    Expect(verifiedOutput2, Is.EqualTo(input));
                }
        }
Пример #9
0
        public void TestSignAndVerifyMessage(String subDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir);

            using (var signer = new AttachedSigner(subPath))
            {
                var streamOutput = new MemoryStream();
                streamOutput.Write(new byte[ExtraDataLength], 0, ExtraDataLength);
                long signedLength;
                using (Utility.ResetStreamWhenFinished(streamOutput))
                {
                    var position = streamOutput.Position;
                    signer.Sign(InputStream, streamOutput);
                    signedLength = streamOutput.Length - position;
                    streamOutput.Write(new byte[ExtraDataLength], 0, ExtraDataLength);
                }
                Expect(streamOutput.Position, Is.EqualTo(10));
                var streamMessage = new MemoryStream();
                signer.VerifiedMessage(streamOutput, streamMessage, inputLength: signedLength);

                Expect(Encoding.UTF8.GetString(streamMessage.ToArray()), Is.EqualTo(input));
                Expect(streamOutput.Position, Is.EqualTo(signedLength + ExtraDataLength));
            }
        }
Пример #10
0
        protected int TakeAction(IKeySet keyset, Stream inStream, Stream outStream, Stream outStream2, IKeySet keyset2)
        {
            if ((WireFormat.IsNullOrEmpty(_format) &&
                 (keyset.Metadata.Purpose == KeyPurpose.DecryptAndEncrypt ||
                  keyset.Metadata.Purpose == KeyPurpose.Encrypt)) ||
                _format == WireFormat.Crypt
                )
            {
                using (var ucrypter = new Encrypter(keyset))
                {
                    var err = UseCompression(ucrypter);
                    if (err != 0)
                    {
                        return(err);
                    }
                    ucrypter.Encrypt(inStream, outStream);
                }
            }
            else if (WireFormat.IsNullOrEmpty(_format) || _format == WireFormat.Sign)
            {
                using (var signer = new Signer(keyset))
                {
                    var sig = signer.Sign(inStream);
                    outStream.Write(sig, 0, sig.Length);
                }
            }
            else if (_format == WireFormat.SignTimeout)
            {
                using (var signer = new TimeoutSigner(keyset))
                {
                    var sig = signer.Sign(inStream, _expires.GetValueOrDefault());
                    outStream.Write(sig, 0, sig.Length);
                }
            }
            else if (_format == WireFormat.SignAttached)
            {
                using (var signer = new AttachedSigner(keyset))
                {
                    byte[] hidden = null;
                    if (!String.IsNullOrWhiteSpace(_attachedHidden))
                    {
                        hidden = Keyczar.Keyczar.RawStringEncoding.GetBytes(_attachedHidden);
                    }
                    signer.Sign(inStream, outStream, hidden);
                }
            }
            else if (_format == WireFormat.SignVanilla || _format == WireFormat.SignUnversioned)
            {
                using (var signer = new Keyczar.Compat.VanillaSigner(keyset))
                {
                    var sig = signer.Sign(inStream);
                    outStream.Write(sig, 0, sig.Length);
                }
            }
            else if (_format == WireFormat.CryptSession)
            {
                using (var crypter = new Encrypter(keyset))
                    using (var sessionCrypter = new SessionCrypter(crypter))
                    {
                        var err = UseCompression(sessionCrypter);
                        if (err != 0)
                        {
                            return(err);
                        }
                        var materials = sessionCrypter.SessionMaterial.ToBytes();
                        outStream.Write(materials, 0, materials.Length);

                        sessionCrypter.Encrypt(inStream, outStream2);
                    }
            }
            else if (_format == WireFormat.CryptSignedSession)
            {
                if (keyset2 == null)
                {
                    Console.WriteLine(Localized.MsgRequiresLocation2);
                    return(-1);
                }

                using (var crypter = new Encrypter(keyset))
                    using (var signer = new AttachedSigner(keyset2))
                        using (var sessionCrypter = new SessionCrypter(crypter, signer))
                        {
                            var err = UseCompression(sessionCrypter);
                            if (err != 0)
                            {
                                return(err);
                            }
                            var materials = sessionCrypter.SessionMaterial.ToBytes();
                            outStream.Write(materials, 0, materials.Length);

                            sessionCrypter.Encrypt(inStream, outStream2);
                        }
            }
            else
            {
                Console.WriteLine(Localized.MsgUnknownFormat, _format);
                return(-1);
            }
            return(0);
        }
Пример #11
0
        //Asymentric key genteration is too slow
        //[TestCase("rsa_priv", "rsa", "1024", "crypt")]
        //[TestCase("rsa_priv", "rsa-sign", "1024", "sign")]
        //[TestCase("dsa_priv", "dsa", "1024", "sign")]
        public void CreateKeyCollision(string key, string dir, string sizeString, string purpose)
        {
            var     crypt = purpose == "crypt";
            var     purp  = crypt ? KeyPurpose.DecryptAndEncrypt : KeyPurpose.SignAndVerify;
            KeyType ktype = key;
            int     size  = int.Parse(sizeString);

            IDictionary <int, Key> keys = new Dictionary <int, Key>();
            var kspath = Util.TestDataPath(WRITE_DATA, dir, "key-collision");
            var writer = new KeySetWriter(kspath, overwrite: true);

            using (var ks = CreateNewKeySet(ktype, purp))
            {
                var success = ks.Save(writer);
                Expect(success, Is.True);
            }


            long count = 0;
            Key  newKey2;

            using (var ks = new MutableKeySet(kspath))
            {
                Key newKey1;
                while (true)
                {
                    newKey1 = Key.Generate(ktype, size);
                    int newHash = Utility.ToInt32(newKey1.GetKeyHash());
                    count++;
                    if (keys.TryGetValue(newHash, out newKey2))
                    {
                        break;
                    }
                    keys.Add(newHash, newKey1);
                }
                Console.WriteLine("Created {1} collision after {0} iterations", count, dir);

                var ver = ks.AddKey(KeyStatus.Primary, newKey1);

                Expect(ver, Is.EqualTo(1));

                var success = ks.Save(writer);
                Expect(success, Is.True);
            }

            if (crypt)
            {
                using (var encrypter = new Encrypter(kspath))
                {
                    var ciphertext = encrypter.Encrypt(input);
                    File.WriteAllText(Path.Combine(kspath, "1.out"), ciphertext);
                }
            }
            else
            {
                using (var signer = new Signer(kspath))
                {
                    var ciphertext = signer.Sign(input);
                    File.WriteAllText(Path.Combine(kspath, "1.out"), ciphertext);
                }

                using (var signer = new TimeoutSigner(kspath))
                {
                    var ciphertext = signer.Sign(input, new DateTime(2012, 12, 21, 11, 11, 0, DateTimeKind.Utc));
                    File.WriteAllText(Path.Combine(kspath, "1.timeout"), ciphertext);
                }

                using (var signer = new AttachedSigner(kspath))
                {
                    var ciphertext = signer.Sign(input);
                    File.WriteAllText(Path.Combine(kspath, "1.attached"), ciphertext);
                }

                using (var signer = new AttachedSigner(kspath))
                {
                    var ciphertext = signer.Sign(input, Encoding.UTF8.GetBytes("secret"));
                    File.WriteAllText(Path.Combine(kspath, "1.secret.attached"), ciphertext);
                }
            }

            using (var ks = new MutableKeySet(kspath))
            {
                var ver = ks.AddKey(KeyStatus.Primary, newKey2);
                Expect(ver, Is.EqualTo(2));

                var success = ks.Save(writer);
                Expect(success, Is.True);
            }
            if (crypt)
            {
                using (var encrypter = new Encrypter(kspath))
                {
                    var ciphertext = encrypter.Encrypt(input);
                    File.WriteAllText(Path.Combine(kspath, "2.out"), ciphertext);
                }
            }
            else
            {
                using (var signer = new Signer(kspath))
                {
                    var ciphertext = signer.Sign(input);
                    File.WriteAllText(Path.Combine(kspath, "2.out"), ciphertext);
                }


                using (var signer = new TimeoutSigner(kspath))
                {
                    var ciphertext = signer.Sign(input, new DateTime(2012, 12, 21, 11, 11, 0, DateTimeKind.Utc));
                    File.WriteAllText(Path.Combine(kspath, "2.timeout"), ciphertext);
                }

                using (var signer = new AttachedSigner(kspath))
                {
                    var ciphertext = signer.Sign(input);
                    File.WriteAllText(Path.Combine(kspath, "2.atttached"), ciphertext);
                }

                using (var signer = new AttachedSigner(kspath))
                {
                    var ciphertext = signer.Sign(input, Encoding.UTF8.GetBytes("secret"));
                    File.WriteAllText(Path.Combine(kspath, "2.secret.atttached"), ciphertext);
                }
            }
        }
Пример #12
0
        public void TestSignAndVerifyShort(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);
                var badoutput = signedoutput.ToBytes();
                badoutput[10] ^= 9;
                var badlength = new byte[12];
                Array.Copy(badoutput, badlength, badlength.Length);

                Expect(() => signer.Verify(badlength), Throws.InstanceOf<InvalidCryptoDataException>());
                Expect(() => verifier.Verify(badlength), Throws.InstanceOf<InvalidCryptoDataException>());
            }
        }
Пример #13
0
        public void TestSignAndVerifyMessageBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);
                var badoutput = signedoutput.ToBytes();
                badoutput[10] ^= 9;

                Expect(() => signer.VerifiedMessage(badoutput), Throws.InstanceOf<InvalidCryptoDataException>());
                Expect(() => verifier.VerifiedMessage(badoutput), Throws.InstanceOf<InvalidCryptoDataException>());
            }
        }
Пример #14
0
        public void TestSignAndVerifyBad(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);
                var badoutput = signedoutput.ToBytes();
                badoutput[10] ^= 9;

                Expect(signer.Verify(badoutput), Is.False);
                Expect(verifier.Verify(badoutput), Is.False);
            }
        }
Пример #15
0
        public void TestSignAndVerify(String subDir, string nestDir)
        {
            var subPath = Util.TestDataPath(TEST_DATA, subDir, nestDir);
            using (var signer = new AttachedSigner(subPath))
            using (var verifier = new AttachedVerifier(subPath))
            {
                var signedoutput = signer.Sign(input);

                Expect(signer.Verify(signedoutput), Is.True);
                Expect(verifier.Verify(signedoutput), Is.True);
            }
        }