Beispiel #1
0
        public void TestEncryptedKeysetForNonEncryptedData()
        {
            var nonEncryptedPath = Util.TestDataPath(TEST_DATA, "rsa");

            using (var pbereader = KeySet.LayerSecurity(
                       FileSystemKeySet.Creator(nonEncryptedPath),
                       PbeKeySet.Creator(() => "dummy")

                       ))
            {
                var key = pbereader.GetKey(1);
                Expect(key, Is.Not.Null);
            }


            using (var crypter = new Crypter(nonEncryptedPath))
                using (var encreader = KeySet.LayerSecurity(
                           FileSystemKeySet.Creator(nonEncryptedPath),
                           EncryptedKeySet.Creator(crypter)
                           ))
                {
                    var key = encreader.GetKey(1);
                    Expect(key, Is.Not.Null);
                }
        }
Beispiel #2
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new KeySet(_location);

            Func <string> prompt = CachedPrompt.Password(() =>
            {
                Console.WriteLine(Localized.MsgForKeySet);
                return(Util.PromptForPassword());
            }).Prompt;

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = new PbeKeySet(_crypterLocation, prompt);
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                    {
                        if (!ks.ExportPrimaryAsPkcs(_destination, CachedPrompt.Password(() =>
                        {
                            Console.WriteLine(
                                Localized.MsgForExport);
                            return
                            (Util.DoublePromptForPassword
                                 ());
                        }).Prompt))
                        {
                            ret = -1;
                        }
                        else
                        {
                            Console.WriteLine(Localized.MsgExportedPem);
                        }
                    }


            return(ret);
        }
 public void TestSign()
 {
     using (var stream = File.OpenRead(Util.TestDataPath(TEST_DATA, "cryptkey.zip")))
         using (var keySet = new BlobKeySet(stream))
             using (var crypter = new Crypter(keySet))
                 using (var signstream = File.OpenRead(Util.TestDataPath(TEST_DATA, "signkey.zip")))
                     using (var signkeySet = KeySet.LayerSecurity(
                                BlobKeySet.Creator(signstream),
                                EncryptedKeySet.Creator(crypter)
                                ))
                         using (var verifier = new Verifier(signkeySet))
                         {
                             var sig = (WebBase64)File.ReadAllText(Util.TestDataPath(TEST_DATA, "sign.out"));
                             Expect(verifier.Verify(input, sig), Is.True);
                         }
 }
Beispiel #4
0
        public void TestAesEncryptedKeyDecrypt(string subDir, string nestedDir)
        {
            // Test reading and using encrypted keys


            var basePath = Util.TestDataPath(TEST_DATA, nestedDir);
            var keyPath  = Path.Combine(basePath, subDir);
            var dataPath = Path.Combine(basePath, subDir + "-crypted");

            using (var keyDecrypter = new Crypter(keyPath))
                using (var ks = KeySet.LayerSecurity(FileSystemKeySet.Creator(dataPath),
                                                     EncryptedKeySet.Creator(keyDecrypter)))
                    using (var dataDecrypter = new Crypter(ks))
                    {
                        HelperDecrypt(dataDecrypter, dataPath);
                    }
        }
Beispiel #5
0
        public void TestEncryptedKeysetForNonEncryptedData()
        {
            var nonencryptedpath = Util.TestDataPath(TEST_DATA, "rsa");

            using (var pbereader = new PbeKeySet(nonencryptedpath, () => "dummy"))
            {
                var key = pbereader.GetKey(1);
                Expect(key, Is.Not.Null);
            }


            using (var crypter = new Crypter(nonencryptedpath))
            {
                var encreader = new EncryptedKeySet(nonencryptedpath, crypter);
                var key       = encreader.GetKey(1);
                Expect(key, Is.Not.Null);
            }
        }
Beispiel #6
0
        public void Decrypt()
        {
            var keyPath  = TestData("aes");
            var dataPath = TestData("aes-crypted");

            var activeCiphertext  = (WebBase64)File.ReadAllLines(Path.Combine(dataPath, "1.out")).First();
            var primaryCiphertext = (WebBase64)File.ReadAllLines(Path.Combine(dataPath, "2.out")).First();

            using (var keyDecrypter = new Crypter(keyPath))
                using (var ks = KeySet.LayerSecurity(FileSystemKeySet.Creator(dataPath),
                                                     EncryptedKeySet.Creator(keyDecrypter)))
                    using (var crypter = new Crypter(ks))
                    {
                        var activeDecrypted = crypter.Decrypt(activeCiphertext);
                        Expect(activeDecrypted, Is.EqualTo(Input));
                        var primaryDecrypted = crypter.Decrypt(primaryCiphertext);
                        Expect(primaryDecrypted, Is.EqualTo(Input));
                    }
        }
Beispiel #7
0
        protected IKeySet ProduceKeySet(string location, string crypterLocation, bool password, out IDisposable d1, out IDisposable d2, out IDisposable d3)
        {
            if (String.IsNullOrWhiteSpace(location))
            {
                d1 = null;
                d2 = null;
                d3 = null;
                return(null);
            }

            Crypter       crypter = null;
            IKeySet       ks      = new FileSystemKeySet(location);
            Func <string> prompt  = CachedPrompt.Password(Util.PromptForPassword).Prompt;

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(crypterLocation))
            {
                if (password)
                {
                    var cks = KeySet.LayerSecurity(FileSystemKeySet.Creator(crypterLocation),
                                                   PbeKeySet.Creator(prompt)
                                                   );
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }

            d1 = crypter;
            d2 = dks;
            d3 = ks;
            return(ks);
        }
Beispiel #8
0
        public void CreateAndCrypted(string keyType, string topDir, string subDir)
        {
            KeyType type   = keyType;
            var     kspath = Util.TestDataPath(WRITE_DATA, topDir, subDir);
            var     writer = new FileSystemKeySetWriter(kspath, overwrite: true);

            using (var ks = CreateNewKeySet(type.Kind, KeyPurpose.DecryptAndEncrypt))
            {
                var success = ks.Save(writer);
                Expect(success, Is.True);
            }


            HelperCryptCreate(writer, new FileSystemKeySet(kspath), kspath, type);


            var kscryptpath = Util.TestDataPath(WRITE_DATA, topDir + "-crypted", subDir);


            var baseWriter = new FileSystemKeySetWriter(kscryptpath, overwrite: true);

            using (var ks = CreateNewKeySet(type.Kind, KeyPurpose.DecryptAndEncrypt))
            {
                var success = ks.Save(baseWriter);
                Expect(success, Is.True);
            }

            using (var encrypter = new Crypter(kspath))
            {
                var cryptedwriter = new EncryptedKeySetWriter(baseWriter, encrypter);
                using (var eks = KeySet.LayerSecurity(FileSystemKeySet.Creator(kscryptpath),
                                                      EncryptedKeySet.Creator(encrypter)))
                {
                    HelperCryptCreate(cryptedwriter, eks, kscryptpath, type,
                                      new FileSystemKeySet(kscryptpath), baseWriter);
                }
            }
        }
Beispiel #9
0
        public void TestOperateOnPbeCryptKeys()
        {
            string result;


            var path  = Util.TestDataPath(TEST_DATA, "rsa-pbe2");
            var pathc = Util.TestDataPath(TEST_DATA, "rsa-crypted2");

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
            if (Directory.Exists(pathc))
            {
                Directory.Delete(pathc, true);
            }

            result = Util.KeyczarTool(create: null, location: path, purpose: "crypt", unofficial: null);
            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKeySet));

            result = Util.KeyczarTool("cartman", "cartman", addkey: null, location: path, password: null,
                                      status: "primary");
            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKey));

            result = Util.KeyczarTool(create: null, location: pathc, purpose: "crypt", asymmetric: null);
            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKeySet));

            result = Util.KeyczarTool("cartman", addkey: null, location: pathc, crypter: path, password: null,
                                      status: "primary");
            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgCreatedKey));

            var pathi = Path.Combine(pathc, "out.pem");

            result = Util.KeyczarTool(
                "cartman",
                "pass",
                "pass",
                export: null,
                location: pathc,
                password: null,
                crypter: path,
                destination: pathi);

            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgExportedPem));

            result = Util.KeyczarTool("cartman", "pass",
                                      importkey: null,
                                      location: pathc,
                                      status: "primary",
                                      crypter: path,
                                      password: null,
                                      importlocation: pathi);

            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgImportedNewKey));


            var pathp = Path.Combine(pathc, "export");

            result = Util.KeyczarTool(
                "cartman",
                pubkey: null,
                location: pathc,
                crypter: path,
                password: null,
                destination: pathp
                );
            Expect(result, Is.StringContaining(KeyczarTool.Localized.MsgNewPublicKeySet));

            var patho = Path.Combine(pathc, "1.out");

            result = Util.KeyczarTool(
                "cartman",
                usekey: null,
                location: pathc,
                crypter: path,
                password: null,
                destination: patho,
                additionalArgs: new[] { input }
                );

            using (var pks = new PbeKeySet(path, () => "cartman" /*hardcoding because this is a test*/))
                using (var kcrypter = new Crypter(pks))
                {
                    var eks = new EncryptedKeySet(pathc, kcrypter);
                    using (var crypter = new Crypter(eks))
                    {
                        Expect(pks.Metadata.Encrypted, Is.True);
                        Expect(eks.Metadata.Encrypted, Is.True);
                        result = crypter.Decrypt((WebBase64)File.ReadAllText(patho));
                        Expect(result, Is.EqualTo(input));
                    }
                }
            Directory.Delete(pathc, true);
        }
Beispiel #10
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new FileSystemKeySet(_location);

            Func <string> prompt = CachedPrompt.Password(Util.PromptForPassword).Prompt;

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = KeySet.LayerSecurity(FileSystemKeySet.Creator(_crypterLocation),
                                                   PbeKeySet.Creator(prompt)
                                                   );
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                        using (var keySet = new MutableKeySet(ks))
                        {
                            var pubKeySet = keySet.PublicKey();
                            if (pubKeySet != null)
                            {
                                using (pubKeySet)
                                {
                                    IKeySetWriter writer = new FileSystemKeySetWriter(_destination, overwrite: false);

                                    if (pubKeySet.Save(writer))
                                    {
                                        Console.WriteLine(Localized.MsgNewPublicKeySet);
                                        ret = 0;
                                    }
                                    else
                                    {
                                        ret = -1;
                                    }
                                }
                            }
                            else
                            {
                                ret = -1;
                            }
                        }

            return(ret);
        }
Beispiel #11
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new KeySet(_location);

            Func <string> singlePrompt = CachedPrompt.Password(() =>
            {
                Console.WriteLine(Localized.MsgForKeySet);
                return(Util.PromptForPassword());
            }).Prompt;

            var prompt = ks.Metadata.Encrypted
                             ? singlePrompt
                             : CachedPrompt.Password(() =>
            {
                Console.WriteLine(Localized.MsgForKeySet);
                return(Util.PromptForPassword());
            }).Prompt;

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = new PbeKeySet(_crypterLocation, singlePrompt);
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                        using (var keySet = new MutableKeySet(ks))
                        {
                            if (_status != KeyStatus.Primary && _status != KeyStatus.Active)
                            {
                                Console.WriteLine("{0} {1}.", Localized.Status, _status.Identifier);
                                return(-1);
                            }
                            ImportedKeySet importedKeySet = null;
                            try
                            {
                                importedKeySet = ImportedKeySet.Import.X509Certificate(keySet.Metadata.Purpose, _importLocation);
                            }
                            catch
                            {
                                importedKeySet = ImportedKeySet.Import.PkcsKey(
                                    keySet.Metadata.Purpose, _importLocation,
                                    CachedPrompt.Password(() =>
                                {
                                    Console.WriteLine(Localized.MsgForImport);
                                    return(Util.PromptForPassword());
                                }).Prompt);
                            }
                            if (importedKeySet == null)
                            {
                                Console.WriteLine(Localized.MsgUnparsableImport);
                                ret = -1;
                            }
                            else
                            {
                                if (keySet.Metadata.KeyType != importedKeySet.Metadata.KeyType)
                                {
                                    if (!keySet.Metadata.Versions.Any())
                                    {
                                        keySet.Metadata.KeyType = importedKeySet.Metadata.KeyType;
                                    }
                                    else
                                    {
                                        ret = -1;
                                        Console.WriteLine(Localized.MsgConflictingKeyTypes,
                                                          keySet.Metadata.KeyType.Identifier,
                                                          importedKeySet.Metadata.KeyType);
                                    }
                                }

                                using (importedKeySet)
                                {
                                    if (ret != -1)
                                    {
                                        var ver = keySet.AddKey(_status, importedKeySet.GetKey(1));


                                        IKeySetWriter writer = new KeySetWriter(_location, overwrite: true);

                                        if (crypter != null)
                                        {
                                            writer = new EncryptedKeySetWriter(writer, crypter);
                                        }
                                        else if (_password)
                                        {
                                            writer = new PbeKeySetWriter(writer, prompt);
                                        }

                                        if (keySet.Save(writer))
                                        {
                                            Console.WriteLine("{0} {1}.", Localized.MsgImportedNewKey, ver);
                                            ret = 0;
                                        }
                                        else
                                        {
                                            ret = -1;
                                        }
                                    }
                                }
                            }
                        }

            return(ret);
        }
Beispiel #12
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new KeySet(_location);

            Func <string> crypterPrompt = CachedPrompt.Password(Util.PromptForPassword).Prompt;

            var prompt = ks.Metadata.Encrypted
                             ? new Func <string>(CachedPrompt.Password(Util.PromptForPassword).Prompt)
                             : new Func <string>(CachedPrompt.Password(Util.DoublePromptForPassword).Prompt);

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = new PbeKeySet(_crypterLocation, crypterPrompt);
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                        using (var keySet = new MutableKeySet(ks))
                        {
                            if (_status != KeyStatus.Primary && _status != KeyStatus.Active)
                            {
                                Console.WriteLine("{0} {1}.", Localized.MsgInvalidStatus, _status.Identifier);
                                return(-1);
                            }

                            object options = null;
                            if (!String.IsNullOrWhiteSpace(_padding))
                            {
                                options = new { Padding = _padding };
                            }

                            var ver = keySet.AddKey(_status, _size, options);


                            IKeySetWriter writer = new KeySetWriter(_location, overwrite: true);

                            if (crypter != null)
                            {
                                writer = new EncryptedKeySetWriter(writer, crypter);
                            }
                            else if (_password)
                            {
                                writer = new PbeKeySetWriter(writer, prompt);
                            }

                            using (writer as IDisposable)
                            {
                                try
                                {
                                    if (keySet.Save(writer))
                                    {
                                        Console.WriteLine("{0} {1}.", Localized.MsgCreatedKey, ver);
                                        ret = 0;
                                    }
                                    else
                                    {
                                        ret = -1;
                                    }
                                }
                                catch
                                {
                                    ret = -1;
                                }
                            }
                        }

            if (ret != 0)
            {
                Console.WriteLine("{0} {1}.", Localized.MsgCouldNotWrite, _location);
            }

            return(ret);
        }
Beispiel #13
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new FileSystemKeySet(_location);

            Func <string> singlePrompt = CachedPrompt.Password(() =>
            {
                Console.WriteLine(Localized.MsgForKeySet);
                return(Util.PromptForPassword());
            }).Prompt;

            var prompt = ks.Metadata.Encrypted
                             ? singlePrompt
                             : CachedPrompt.Password(() =>
            {
                Console.WriteLine(Localized.MsgForKeySet);
                return(Util.PromptForPassword());
            }).Prompt;

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = KeySet.LayerSecurity(FileSystemKeySet.Creator(_crypterLocation),
                                                   PbeKeySet.Creator(singlePrompt));
                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                        using (var keySet = new MutableKeySet(ks))
                        {
                            var official = keySet.Metadata.OriginallyOfficial && keySet.Metadata.ValidOfficial() && !_force;


                            KeyType hint = null;

                            if (!String.IsNullOrWhiteSpace(_type))
                            {
                                hint = AddKey.KeyTypeForString(_type);
                            }

                            if (_status != KeyStatus.Primary && _status != KeyStatus.Active)
                            {
                                Console.WriteLine("{0} {1}.", Localized.Status, _status.Identifier);
                                return(-1);
                            }
                            ImportedKeySet importedKeySet = null;
                            try
                            {
                                importedKeySet = ImportedKeySet.Import.X509Certificate(keySet.Metadata.Purpose, _importLocation, official, hint);
                            }
                            catch
                            {
                                if (_importLocation.EndsWith("pfx", StringComparison.Ordinal) ||
                                    _importLocation.EndsWith("p12", StringComparison.Ordinal)
                                    )
                                {
                                    importedKeySet = ImportedKeySet.Import.Pkcs12Keys(
                                        keySet.Metadata.Purpose, _importLocation,
                                        CachedPrompt.Password(() =>
                                    {
                                        Console.WriteLine(Localized.MsgForImport);
                                        return(Util.PromptForPassword());
                                    }).Prompt, official, hint);
                                }
                                else
                                {
                                    importedKeySet = ImportedKeySet.Import.PkcsKey(
                                        keySet.Metadata.Purpose, _importLocation,
                                        CachedPrompt.Password(() =>
                                    {
                                        Console.WriteLine(Localized.MsgForImport);
                                        return(Util.PromptForPassword());
                                    }).Prompt, official, hint);
                                }
                            }
                            if (importedKeySet == null)
                            {
                                Console.WriteLine(Localized.MsgUnparsableImport);
                                ret = -1;
                            }
                            else
                            {
                                if (keySet.Metadata.OriginallyOfficial && keySet.Metadata.ValidOfficial())
                                {
                                    if (!_force && !importedKeySet.Metadata.ValidOfficial())
                                    {
                                        var keySetKeyType = keySet.Metadata.OfficialKeyType();
                                        ret = -1;
                                        Console.WriteLine(Localized.MsgMismatchedType,
                                                          "Multiple Types",
                                                          keySetKeyType);
                                    }
                                    else
                                    {
                                        var importedKeyType = importedKeySet.Metadata.OfficialKeyType();
                                        var keySetKeyType   = keySet.Metadata.OfficialKeyType();
                                        if (importedKeyType != keySetKeyType && !_force)
                                        {
                                            ret = -1;
                                            Console.WriteLine(Localized.MsgMismatchedType,
                                                              importedKeyType,
                                                              keySetKeyType);
                                        }
                                    }
                                }

                                if (keySet.Metadata.Kind != importedKeySet.Metadata.Kind)
                                {
                                    if (!keySet.Metadata.Versions.Any())
                                    {
                                        keySet.Metadata.Kind = importedKeySet.Metadata.Kind;
                                    }
                                    else
                                    {
                                        ret = -1;
                                        Console.WriteLine(Localized.MsgConflictingKeyTypes,
                                                          keySet.Metadata.Kind.Identifier,
                                                          importedKeySet.Metadata.Kind);
                                    }
                                }

                                using (importedKeySet)
                                {
                                    if (ret != -1)
                                    {
                                        var count  = importedKeySet.Metadata.Versions.Count();
                                        var outMsg = "";
                                        foreach (var v in importedKeySet.Metadata.Versions)
                                        {
                                            var status = v.Status;
                                            if (count == 1 || _status != KeyStatus.Primary)
                                            {
                                                status = _status;
                                            }

                                            var ver = keySet.AddKey(status, importedKeySet.GetKey(v.VersionNumber));
                                            outMsg = ver.ToString();
                                        }
                                        if (count > 1)
                                        {
                                            outMsg = $"{count} keys";
                                        }


                                        IKeySetWriter writer = new FileSystemKeySetWriter(_location, overwrite: true);

                                        if (crypter != null)
                                        {
                                            writer = new EncryptedKeySetWriter(writer, crypter);
                                        }
                                        else if (_password)
                                        {
                                            writer = new PbeKeySetWriter(writer, prompt);
                                        }

                                        if (keySet.Save(writer))
                                        {
                                            Console.WriteLine("{0} {1}.", Localized.MsgImportedNewKey, outMsg);
                                            ret = 0;
                                        }
                                        else
                                        {
                                            ret = -1;
                                        }
                                    }
                                }
                            }
                        }

            return(ret);
        }
Beispiel #14
0
        public void TestOperateOnCryptedKeys()
        {
            string result;
            var    path = Util.TestDataPath(TEST_DATA, "crypting");

            var pathc = Util.TestDataPath(TEST_DATA, "rsa-crypted");

            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            if (Directory.Exists(pathc))
            {
                Directory.Delete(pathc, true);
            }

            result = Util.KeyczarTool(create: null, location: path, purpose: "crypt");
            Expect(result, Does.Contain(KeyczarTool.Localized.MsgCreatedKeySet));

            result = Util.KeyczarTool(addkey: null, location: path, status: "primary");

            Expect(result, Does.Contain(KeyczarTool.Localized.MsgCreatedKey));

            result = Util.KeyczarTool(create: null, location: pathc, purpose: "crypt", asymmetric: null);
            Expect(result, Does.Contain(KeyczarTool.Localized.MsgCreatedKeySet));

            result = Util.KeyczarTool(addkey: null, location: pathc, crypter: path, status: "primary");
            Expect(result, Does.Contain(KeyczarTool.Localized.MsgCreatedKey));

            var pathi = Path.Combine(pathc, "out.pem");

            result = Util.KeyczarTool(
                "pass",
                "pass",
                export: null,
                location: pathc,
                crypter: path,
                destination: pathi);

            Expect(result, Does.Contain(KeyczarTool.Localized.MsgExportedPem));

            result = Util.KeyczarTool("pass",
                                      importkey: null,
                                      location: pathc,
                                      status: "primary",
                                      crypter: path,
                                      importlocation: pathi);

            Expect(result, Does.Contain(KeyczarTool.Localized.MsgImportedNewKey));


            var pathp = Path.Combine(pathc, "export");

            result = Util.KeyczarTool(
                pubkey: null,
                location: pathc,
                crypter: path,
                destination: pathp
                );
            Expect(result, Does.Contain(KeyczarTool.Localized.MsgNewPublicKeySet));

            var patho = Path.Combine(pathc, "1.out");

            result = Util.KeyczarTool(
                usekey: null,
                location: pathc,
                crypter: path,
                destination: patho,
                additionalArgs: new[] { input }
                );


            using (var kcrypter = new Crypter(path))
            {
                using (var eks = KeySet.LayerSecurity(FileSystemKeySet.Creator(pathc),
                                                      EncryptedKeySet.Creator(kcrypter))){
                    Expect(eks.Metadata.Encrypted, Is.True);
                    using (var crypter = new Crypter(eks))
                    {
                        result = crypter.Decrypt((WebBase64)File.ReadAllText(patho));
                        Expect(result, Is.EqualTo(input));
                    }
                }
            }

            Directory.Delete(path, true);
        }
Beispiel #15
0
        public override int Run(string[] remainingArguments)
        {
            var     ret     = 0;
            Crypter crypter = null;
            IKeySet ks      = new FileSystemKeySet(_location);

            Func <string> crypterPrompt = CachedPrompt.Password(Util.PromptForPassword).Prompt;

            var prompt = ks.Metadata.Encrypted
                             ? new Func <string>(CachedPrompt.Password(Util.PromptForPassword).Prompt)
                             : new Func <string>(CachedPrompt.Password(Util.DoublePromptForPassword).Prompt);

            IDisposable dks = null;

            if (!String.IsNullOrWhiteSpace(_crypterLocation))
            {
                if (_password)
                {
                    var cks = KeySet.LayerSecurity(
                        FileSystemKeySet.Creator(_crypterLocation),
                        PbeKeySet.Creator(crypterPrompt)
                        );

                    crypter = new Crypter(cks);
                    dks     = cks;
                }
                else
                {
                    crypter = new Crypter(_crypterLocation);
                }
                ks = new EncryptedKeySet(ks, crypter);
            }
            else if (_password)
            {
                ks = new PbeKeySet(ks, prompt);
            }
            var d2ks = ks as IDisposable;


            using (crypter)
                using (dks)
                    using (d2ks)
                        using (var keySet = new MutableKeySet(ks))
                        {
                            if (_status != KeyStatus.Primary && _status != KeyStatus.Active)
                            {
                                Console.WriteLine("{0} {1}.", Localized.MsgInvalidStatus, _status.Identifier);
                                return(-1);
                            }

                            object options = null;
                            if (!String.IsNullOrWhiteSpace(_padding))
                            {
                                options = new { Padding = _padding };
                            }

                            int ver;
                            var type = KeyTypeForString(_type);

                            if (ks.Metadata.OriginallyOfficial && ks.Metadata.ValidOfficial())
                            {
                                var keytype = ks.Metadata.OfficialKeyType();
                                if (type == null)
                                {
                                    type = keytype;
                                }
                                else if (type != keytype && !_force)
                                {
                                    throw new ConsoleHelpAsException(String.Format(Localized.MsgMismatchedType, type, keytype));
                                }
                            }


                            try
                            {
                                ver = keySet.AddKey(_status, _size, type, options);
                            }
#pragma warning disable 168
                            catch (InvalidKeyTypeException ex)
#pragma warning restore 168
                            {
                                throw new ConsoleHelpAsException(String.Format(Localized.MsgMismatchedKind, type?.Kind, keySet.Metadata.Kind));
                            }


                            IKeySetWriter writer = new FileSystemKeySetWriter(_location, overwrite: true);

                            if (crypter != null)
                            {
                                writer = new EncryptedKeySetWriter(writer, crypter);
                            }
                            else if (_password)
                            {
                                writer = new PbeKeySetWriter(writer, prompt);
                            }

                            using (writer as IDisposable)
                            {
                                try
                                {
                                    if (keySet.Save(writer))
                                    {
                                        Console.WriteLine("{0} {1}.", Localized.MsgCreatedKey, ver);
                                        ret = 0;
                                    }
                                    else
                                    {
                                        ret = -1;
                                    }
                                }
                                catch
                                {
                                    ret = -1;
                                }
                            }
                        }

            if (ret != 0)
            {
                Console.WriteLine("{0} {1}.", Localized.MsgCouldNotWrite, _location);
            }

            return(ret);
        }