public void TestCreateSessions(string topDir, string subDir, string signed, string packer,
                                       string sessionFilename, string ciphertextFilename)
        {
            var kspath = Util.TestDataPath(WRITE_DATA, topDir, subDir);

            Directory.CreateDirectory(kspath);

            ISessionKeyPacker keyPacker = null;
            int?    keySize             = null;
            KeyType keyType             = null;

            if (!String.IsNullOrWhiteSpace(packer))
            {
                keyPacker = new BsonSessionKeyPacker();
                keySize   = 256;
                keyType   = UnofficialKeyType.AesAead;
            }
            using (var encrypter = new Encrypter(Util.TestDataPath(WRITE_DATA, "rsa.public")))
                using (var signer = String.IsNullOrWhiteSpace(signed)
                                    ? null
                                    : new AttachedSigner(Util.TestDataPath(WRITE_DATA, "dsa")))
                    using (var session = new SessionCrypter(encrypter, signer, keySize, keyType, keyPacker))
                    {
                        var material = session.SessionMaterial;

                        var ciphertext = session.Encrypt(input);

                        File.WriteAllText(Path.Combine(kspath, sessionFilename), material);
                        File.WriteAllText(Path.Combine(kspath, ciphertextFilename), ciphertext);
                    }
        }
 public void Write(byte[] keyData, int version)
 {
     using (var sessionCrypter = new SessionCrypter(_encrypter, keySize: 256,
                                                    symmetricKeyType: UnofficialKeyType.AesAead, keyPacker: _sessionPacker))
     {
         var sessionMaterial = sessionCrypter.SessionMaterial;
         var cipherData      = sessionCrypter.Encrypt(keyData);
         var session         = new CertEncryptedKeySet.SessionPack(sessionMaterial, cipherData);
         var json            = Keyczar.Util.Utility.ToJson(session);
         var jsonData        = this.GetConfig().RawStringEncoding.GetBytes(json);
         _writer.Write(jsonData, version);
     }
 }
Exemple #3
0
        public static string[] Encrypter(string textToEncrypt, PathProvider p)
        {
            WebBase64 sessionMaterial;
            WebBase64 cipherText;
            string[] data;
            string path = p.GetPublicPath();

            using (var encrypter = new Encrypter(path))
            using (var sessionCrypter = new SessionCrypter(encrypter))
            {
                sessionMaterial = sessionCrypter.SessionMaterial;
                cipherText = sessionCrypter.Encrypt(textToEncrypt);
                data = new string[] { sessionMaterial.ToString(), cipherText.ToString() };
                //data.Add("sessionmaterial", sessionMaterial);
                //data.Add("data", cipherText);

            }
            return data;
        }
Exemple #4
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);
        }