Exemple #1
0
        bool CreateKey(Config cfg)
        {
            string name = cfg.ContainerName;
            KeyContainerCollection kc;

            Console.WriteLine("Creating RSA key container '{0}'...", name);
            try {
                kc = new KeyContainerCollection(cfg.UseMachinePath);
                if (kc.Contains(name))
                {
                    Failure(cfg, "The RSA container already exists.");
                    return(true);
                }

                var k = new Key(name, cfg.KeySize, cfg.UseMachinePath);
                if (!k.IsValid)
                {
                    Failure(cfg, "Failed to generate RSA key pair.");
                    return(true);
                }
                k.Save();
                Success();
            } catch (Exception ex) {
                Failure(ex, cfg);
                return(true);
            }

            return(false);
        }
Exemple #2
0
        bool ExportKey(Config cfg)
        {
            string containerName = cfg.ContainerName;
            string fileName      = cfg.FileName;

            Console.WriteLine("Exporting an RSA key from container '{0}' to file '{1}'...", containerName, fileName);
            if (String.IsNullOrEmpty(containerName))
            {
                Failure(cfg, "Unspecified container name.");
                return(true);
            }

            if (String.IsNullOrEmpty(fileName))
            {
                Failure(cfg, "Unspecified file name.");
                return(true);
            }

            KeyContainerCollection kcc;
            Key          key;
            KeyContainer kc;

            try
            {
                kcc = new KeyContainerCollection(cfg.UseMachinePath);
                kc  = kcc [containerName];

                if (kc != null)
                {
                    key = kc [0];
                }
                else
                {
                    Failure(cfg, "Container '{0}' does not exist.", containerName);
                    return(true);
                }

                if (key == null)
                {
                    Failure(cfg, "Container '{0}' exists but it does not contain any keys.", containerName);
                    return(true);
                }

                File.WriteAllText(fileName, key.KeyValue);
                Console.WriteLine("Success.");
            }
            catch (Exception ex)
            {
                Failure(ex, cfg);
                return(true);
            }

            return(false);
        }
Exemple #3
0
		bool ListContainers (Config cfg)
		{
			try {
				var kc = new KeyContainerCollection (cfg.UseMachinePath);
				int count;
				foreach (KeyContainer c in kc) {
					count = c.Count;
					Console.WriteLine ("{0} container '{1}' ({2} key{3})", c.Local ? "Local" : "Global", c.Name, count,
							   count == 1 ? String.Empty : "s");
				}
			} catch (Exception ex) {
				Failure (ex, cfg);
			}
			
			return true;
		}
Exemple #4
0
        bool ListContainers(Config cfg)
        {
            try {
                var kc = new KeyContainerCollection(cfg.UseMachinePath);
                int count;
                foreach (KeyContainer c in kc)
                {
                    count = c.Count;
                    Console.WriteLine("{0} container '{1}' ({2} key{3})", c.Local ? "Local" : "Global", c.Name, count,
                                      count == 1 ? String.Empty : "s");
                }
            } catch (Exception ex) {
                Failure(ex, cfg);
            }

            return(true);
        }
Exemple #5
0
		bool ExportKey (Config cfg)
		{
			string containerName = cfg.ContainerName;
			string fileName = cfg.FileName;

			Console.WriteLine ("Exporting an RSA key from container '{0}' to file '{1}'...", containerName, fileName);
			if (String.IsNullOrEmpty (containerName)) {
				Failure (cfg, "Unspecified container name.");
				return true;
			}

			if (String.IsNullOrEmpty (fileName)) {
				Failure (cfg, "Unspecified file name.");
				return true;
			}

			KeyContainerCollection kcc;
			Key key;
			KeyContainer kc;
			try {
				kcc = new KeyContainerCollection (cfg.UseMachinePath);
				kc = kcc [containerName];
				
				if (kc != null)
					key = kc [0];
				else {
					Failure (cfg, "Container '{0}' does not exist.", containerName);
					return true;
				}

				if (key == null) {
					Failure (cfg, "Container '{0}' exists but it does not contain any keys.", containerName);
					return true;
				}
				
				File.WriteAllText (fileName, key.KeyValue);
				Console.WriteLine ("Success.");
			} catch (Exception ex) {
				Failure (ex, cfg);
				return true;
			}
			
			return false;
		}
Exemple #6
0
		bool ImportKey (Config cfg)
		{
			string containerName = cfg.ContainerName;
			string fileName = cfg.FileName;

			Console.WriteLine ("Importing an RSA key from file '{0}' into the container '{1}'...", fileName, containerName);
			if (String.IsNullOrEmpty (containerName)) {
				Failure (cfg, "Unspecified container name.");
				return true;
			}

			if (String.IsNullOrEmpty (fileName)) {
				Failure (cfg, "Unspecified file name.");
				return true;
			}

			if (!File.Exists (fileName)) {
				Failure (cfg, "Key file '{0}' does not exist.", fileName);
				return true;
			}

			KeyContainerCollection kcc;
			Key key;
			KeyContainer kc;
			try {
				kcc = new KeyContainerCollection (cfg.UseMachinePath);
				kc = kcc [containerName];
				
				if (kc != null)
					key = kc [0];
				else
					key = null;

				// No validation is performed on the key - this is left for the
				// encryption algorithm implementation to do.
				string keyvalue = File.ReadAllText (fileName);
				if (key == null)
					key = new Key (containerName, keyvalue, cfg.UseMachinePath);
				else {
					key.KeyValue = keyvalue;
					key.ContainerName = containerName;
				}

				key.Save ();
				Console.WriteLine ("Success.");
			} catch (Exception ex) {
				Failure (ex, cfg);
				return true;
			}
			
			return false;
		}
Exemple #7
0
		bool CreateKey (Config cfg)
		{
			string name = cfg.ContainerName;
			KeyContainerCollection kc;

			Console.WriteLine ("Creating RSA key container '{0}'...", name);
			try {
				kc = new KeyContainerCollection (cfg.UseMachinePath);
				if (kc.Contains (name)) {
					Failure (cfg, "The RSA container already exists.");
					return true;
				}

				var k = new Key (name, cfg.KeySize, cfg.UseMachinePath);
				if (!k.IsValid) {
					Failure (cfg, "Failed to generate RSA key pair.");
					return true;
				}
				k.Save ();
				Success ();
			} catch (Exception ex) {
				Failure (ex, cfg);
				return true;
			}
			
			return false;
		}
Exemple #8
0
        bool ImportKey(Config cfg)
        {
            string containerName = cfg.ContainerName;
            string fileName      = cfg.FileName;

            Console.WriteLine("Importing an RSA key from file '{0}' into the container '{1}'...", fileName, containerName);
            if (String.IsNullOrEmpty(containerName))
            {
                Failure(cfg, "Unspecified container name.");
                return(true);
            }

            if (String.IsNullOrEmpty(fileName))
            {
                Failure(cfg, "Unspecified file name.");
                return(true);
            }

            if (!File.Exists(fileName))
            {
                Failure(cfg, "Key file '{0}' does not exist.", fileName);
                return(true);
            }

            KeyContainerCollection kcc;
            Key          key;
            KeyContainer kc;

            try {
                kcc = new KeyContainerCollection(cfg.UseMachinePath);
                kc  = kcc [containerName];

                if (kc != null)
                {
                    key = kc [0];
                }
                else
                {
                    key = null;
                }

                // No validation is performed on the key - this is left for the
                // encryption algorithm implementation to do.
                string keyvalue = File.ReadAllText(fileName);
                if (key == null)
                {
                    key = new Key(containerName, keyvalue, cfg.UseMachinePath);
                }
                else
                {
                    key.KeyValue      = keyvalue;
                    key.ContainerName = containerName;
                }

                key.Save();
                Console.WriteLine("Success.");
            } catch (Exception ex) {
                Failure(ex, cfg);
                return(true);
            }

            return(false);
        }