Esempio n. 1
0
        static void Main(string[] args)
        {
            try
            {
                Logger.Initialize();

                byte[] data = "Hello world".GetBytes();

                SignKeys signKeys = DataSigner.GenerateSignKeyPair();

                byte[] signature = DataSigner.Sign(data, signKeys.PublicAndPrivate);

                bool valid = DataSigner.IsSignatureValid(data, signature, signKeys.PublicOnly);
                Console.WriteLine(valid ? "Valid" : "NOT Valid");

                data[0] = 41;

                valid = DataSigner.IsSignatureValid(data, signature, signKeys.PublicOnly);
                Console.WriteLine(valid ? "Valid" : "NOT Valid");

                data[0] = "H".GetBytes()[0];

                valid = DataSigner.IsSignatureValid(data, signature, signKeys.PublicOnly);
                Console.WriteLine(valid ? "Valid" : "NOT Valid");
            }
            catch (Exception e)
            {
                Logger.LogError("Unhandled exception", e);
                Console.WriteLine("Error: " + e.Message);
            }

            Console.WriteLine("Press a key to exit");
            Console.ReadKey();
        }
Esempio n. 2
0
        private void buttonGenerateAndSaveMasterKeypair_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.textBoxDOUsername.Text))
                {
                    MessageBox.Show("You must enter a DO user name");
                    return;
                }

                if (string.IsNullOrEmpty(this.textBoxDORoleName.Text))
                {
                    MessageBox.Show("You must enter a DO role name");
                    return;
                }

                IPreService proxy = GetPreProxy();
                this.masterKeypair = proxy.GenerateKeyPair();

                SignKeys doSignKeyPair = DataSigner.GenerateSignKeyPair();

                proxy = GetPreProxy();
                byte[] doUserName = proxy.Encrypt(this.masterKeypair.Public, this.textBoxDOUsername.Text.GetBytes());

                proxy = GetPreProxy();
                byte[] doRoleName = proxy.Encrypt(this.masterKeypair.Public, this.textBoxDORoleName.Text.GetBytes());


                IGatewayService gwProxy = GetServiceProxy();
                gwProxy.InitializeSystem(this.myId, doUserName, doRoleName, doSignKeyPair.PublicOnly);

                string filename = FileDialogs.AskUserForFileNameToSaveIn();
                if (!string.IsNullOrEmpty(filename))
                {
                    if (!Path.HasExtension(filename))
                    {
                        filename = filename + ".xml";
                    }

                    KeyCollection keys = new KeyCollection();
                    keys.MasterPublicKey  = Convert.ToBase64String(this.masterKeypair.Public);
                    keys.MasterPrivateKey = Convert.ToBase64String(this.masterKeypair.Private);
                    keys.PrivateKey       = keys.MasterPrivateKey;
                    keys.PublicKey        = keys.MasterPublicKey;
                    keys.SignKeys         = Convert.ToBase64String(doSignKeyPair.PublicAndPrivate);

                    XmlFile.WriteFile(keys, filename);

                    this.labelKeyStatus.Text = "Keys including MASTER KEYS loaded";

                    MessageBox.Show("Done");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                Logger.LogError("Error generating master keypair", ex);
            }
        }
Esempio n. 3
0
        private void buttonGenerateKeypairsForUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.textBoxNewUserId.Text))
                {
                    MessageBox.Show("You must enter a username");
                    return;
                }
                this.newUserId = GuidCreator.CreateGuidFromString(this.textBoxNewUserId.Text);

                if (this.masterKeypair == null)
                {
                    MessageBox.Show("You must load master key pair first");
                    return;
                }

                string filename = FileDialogs.AskUserForFileNameToSaveIn();
                if (!string.IsNullOrEmpty(filename))
                {
                    if (!Path.HasExtension(filename))
                    {
                        filename = filename + ".xml";
                    }


                    this.signKeyPair = DataSigner.GenerateSignKeyPair();

                    IPreService proxy = GetPreProxy();
                    this.userKeypair = proxy.GenerateKeyPair();

                    proxy = GetPreProxy();
                    this.delegationToken.ToUser = proxy.GenerateDelegationKey(this.masterKeypair.Private, this.userKeypair.Public);

                    IGatewayService gateWayproxy = GetServiceProxy();
                    gateWayproxy.RegisterUser(this.myId, this.newUserId, this.delegationToken, this.signKeyPair.PublicOnly);


                    UserKeys uk = new UserKeys();
                    uk.MasterKeyPublicKey = Convert.ToBase64String(this.masterKeypair.Public);
                    uk.UserPrivateKey     = Convert.ToBase64String(this.userKeypair.Private);
                    uk.UserSignKeys       = Convert.ToBase64String(this.signKeyPair.PublicAndPrivate);

                    XmlFile.WriteFile(uk, filename);

                    MessageBox.Show("Done");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                Logger.LogError("Error generating user keypair", ex);
            }
        }
Esempio n. 4
0
        private void buttonCreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.treeViewRoles.SelectedNode == null ||
                    !(this.treeViewRoles.SelectedNode.Tag is RoleDescription))
                {
                    return;
                }

                if (string.IsNullOrEmpty(this.textBoxNewUserName.Text))
                {
                    MessageBox.Show("You must enter a username");
                    return;
                }
                Guid newUserId = GuidCreator.CreateGuidFromString(this.textBoxNewUserName.Text);

                if (this.masterKeypair == null && this.keyPair == null)
                {
                    MessageBox.Show("You must load your key pair first");
                    return;
                }

                string filename = FileDialogs.AskUserForFileNameToSaveIn();
                if (!string.IsNullOrEmpty(filename))
                {
                    if (!Path.HasExtension(filename))
                    {
                        filename = filename + ".xml";
                    }

                    SignKeys        userSignKeyPair = DataSigner.GenerateSignKeyPair();
                    IPreService     proxy;
                    KeyPair         userKeypair;
                    DelegationToken userDelegationToken;

                    if (this.masterKeypair != null)
                    {
                        proxy       = GetPreProxy();
                        userKeypair = proxy.GenerateKeyPair();

                        userDelegationToken = new DelegationToken();
                        proxy = GetPreProxy();
                        userDelegationToken.ToUser = proxy.GenerateDelegationKey(this.masterKeypair.Private, userKeypair.Public);
                    }
                    else
                    {
                        userKeypair         = this.keyPair; // I am not a DO, so when creating a new user then reuse my key
                        userDelegationToken = null;         // I do not know my own delegation key. The server will put it in for me.
                    }

                    proxy = GetPreProxy();
                    byte[] username = proxy.Encrypt(this.keyPair.Public, this.textBoxNewUserName.Text.GetBytes());

                    User user = new User();
                    user.DelegationToken = userDelegationToken;
                    user.Id            = newUserId;
                    user.Name          = username;
                    user.SignPublicKey = userSignKeyPair.PublicOnly;


                    RoleDescription role         = (RoleDescription)this.treeViewRoles.SelectedNode.Tag;
                    IGatewayService gateWayproxy = GetServiceProxy();
                    gateWayproxy.CreateUser(this.myId, role.Id, user);


                    KeyCollection uk = new KeyCollection();
                    uk.PublicKey  = Convert.ToBase64String(this.keyPair.Public); // use original DO public key
                    uk.PrivateKey = Convert.ToBase64String(userKeypair.Private);
                    uk.SignKeys   = Convert.ToBase64String(userSignKeyPair.PublicAndPrivate);

                    XmlFile.WriteFile(uk, filename);

                    buttonRefreshRolesAndUsers_Click(this, EventArgs.Empty);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                Logger.LogError("Error generating user keypair", ex);
            }
        }