Exemplo n.º 1
0
        public override int Run(string[] remainingArguments)
        {
            var ret = 0;
            Crypter crypter = null;
            IKeySet ks = new KeySet(_location);

            Func<string> prompt = CachedPrompt.Password(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)
            using (var keySet = new MutableKeySet(ks))
            {
                var pubKeySet = keySet.PublicKey();
                if (pubKeySet != null)
                {
                    using (pubKeySet)
                    {
                        IKeySetWriter writer = new KeySetWriter(_destination, overwrite: false);

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

            return ret;
        }
Exemplo n.º 2
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);
                }
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public override int Run(string[] remainingArguments)
        {
            IKeySet ks = new FileSystemKeySet(_location);


            bool add = !ks.Metadata.Encrypted;

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

            if (!add)
            {
                Console.WriteLine(Localized.PasswordPromptOldPassword);
                ks = new PbeKeySet(ks, prompt);
            }


            using (ks as PbeKeySet)
                using (var keySet = new MutableKeySet(ks))
                {
                    keySet.ForceKeyDataChange();

                    IKeySetWriter writer = new FileSystemKeySetWriter(_location, overwrite: true);
                    if (!_remove)
                    {
                        Console.WriteLine(Localized.PasswordPromptNewPassword);
                        Func <string> newPrompt = CachedPrompt.Password(Util.DoublePromptForPassword).Prompt;
                        writer = new PbeKeySetWriter(writer, newPrompt);
                    }
                    else
                    {
                        keySet.Metadata.Encrypted = false;
                    }
                    using (writer as PbeKeySetWriter)
                    {
                        if (keySet.Save(writer))
                        {
                            if (_remove)
                            {
                                Console.WriteLine(Localized.MsgRemovedPassword);
                            }
                            else if (add)
                            {
                                Console.WriteLine(Localized.MsgAddedPasssword);
                            }
                            else
                            {
                                Console.WriteLine(Localized.MsgChangedPassword);
                            }
                            return(0);
                        }
                        return(-1);
                    }
                }
        }
Exemplo n.º 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);
            }
        }
        public void CreatePbeKeySet()
        {
            var           kspath     = Util.TestDataPath(WRITE_DATA, "pbe_json");
            var           writer     = new KeySetWriter(kspath, overwrite: true);
            Func <string> passPrompt = () => "cartman"; //hardcoded because this is a test;

            using (var encwriter = new PbeKeySetWriter(writer, passPrompt))
            {
                using (var ks = CreateNewKeySet(KeyType.Aes, KeyPurpose.DecryptAndEncrypt))
                {
                    var success = ks.Save(writer);
                    Expect(success, Is.True);
                }
                using (var eks = new PbeKeySet(kspath, passPrompt))
                {
                    HelperCryptCreate(encwriter, eks, kspath);
                }
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 8
0
        public void TestPbeKeysetRead()
        {
            Func <string> password = () => "cartman"; //Hardcoded because this is a test

            using (var reader = new PbeKeySet(new KeySet(Util.TestDataPath(TEST_DATA, "pbe_json")), password))
            {
                Expect(reader.Metadata.Encrypted, Is.True);

                var data1 = Encoding.UTF8.GetString(reader.GetKeyData(1));
                var data2 = Encoding.UTF8.GetString(reader.GetKeyData(1));

                var token1 = JToken.Parse(data1);

                var size = token1["size"];
                Expect(size.ToString(), Is.EqualTo("128"));

                var token2 = JToken.Parse(data2);
                var mode   = token2["mode"];
                Expect(mode.ToString(), Is.EqualTo("CBC"));
            }
        }
Exemplo n.º 9
0
        public void TestOperateOnPbeKeys()
        {
            string result;

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

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

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

            result = Util.KeyczarTool("cartman", "cartman", addkey: null, location: pathc, 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,
                destination: pathi);

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

            result = Util.KeyczarTool("cartman", "pass",
                                      importkey: null,
                                      location: pathc,
                                      status: "primary",
                                      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,
                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,
                password: null,
                destination: patho,
                additionalArgs: new[] { input }
                );
            using (var pks = new PbeKeySet(pathc, () => "cartman" /*hardcoding because this is a test*/))
                using (var crypter = new Crypter(pks))
                {
                    Expect(pks.Metadata.Encrypted, Is.True);
                    result = crypter.Decrypt((WebBase64)File.ReadAllText(patho));
                    Expect(result, Is.EqualTo(input));
                }

            Directory.Delete(pathc, true);
        }
Exemplo n.º 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);
        }
Exemplo n.º 11
0
        public void TestOperateOnPbeKeys()
        {
            string result;

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

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

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

            result = Util.KeyczarTool("cartman", "cartman", addkey: null, location: pathc, 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,
                destination: pathi);

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

            result = Util.KeyczarTool("cartman", "pass",
                                      importkey: null,
                                      location: pathc,
                                      status: "primary",
                                      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,
                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,
                password: null,
                destination: patho,
                additionalArgs: new[] {input}
                );
            using (var pks = new PbeKeySet(pathc, () => "cartman" /*hardcoding because this is a test*/))
            using (var crypter = new Crypter(pks))
            {
                Expect(pks.Metadata.Encrypted, Is.True);
                result = crypter.Decrypt((WebBase64) File.ReadAllText(patho));
                Expect(result, Is.EqualTo(input));
            }

            Directory.Delete(pathc, true);
        }
Exemplo n.º 12
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);
        }
Exemplo n.º 13
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);
        }
Exemplo n.º 14
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);
        }
Exemplo n.º 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);
        }