Ejemplo n.º 1
0
 public SslClient(string ip, int port, UserCertificate certificate = null, int bufferSize = 1024) : base(ip, port)
 {
     try
     {
         _bufferSize = bufferSize;
         if (certificate != null)
         {
             _certificate = certificate;
         }
         else
         {
             _certificate = new X509Certificate2();
         }
         _stream = new SslStream(GetStream(), false, ServerCertificateValidation, UserCertificateSelection);
         _stream.AuthenticateAsClient(ip);
     }
     catch (AuthenticationException err)
     {
         ErrorOnAuthentication();
     }
     catch (SocketException)
     {
         ErrorOnAuthentication();
     }
     catch (Win32Exception)
     {
         ErrorOnAuthentication();
     }
 }
Ejemplo n.º 2
0
 public async static Task FileWithPrivateKey(string inputFile, string outputFile, UserCertificate certificate)
 {
     if (!certificate.HasPrivateKey)
     {
         throw new CertificateError("Certificate doesn't have private key.");
     }
     await File(inputFile, outputFile, certificate.PrivateKey as RSACryptoServiceProvider);
 }
Ejemplo n.º 3
0
        private async void connectButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DisableAll();
            var statusLabel = new ToolStripStatusLabel("Connecting");
            statusBar.Items.Add(statusLabel);
            if (_connected)
                DisconnectUser();
            try
            {
                _certificate = identitiesListBox.SelectedItem as UserCertificate;
                if (await ServerTransaction.Connect(_certificate))
                {
                    await ConnectUser();

                    var login = _certificate.CommonName;
                    loginLabel.Text = login;

                    var message = string.Format("Successfull logged as {0}", login);
                    MessageBox.Show(message, "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    _currentUser = _certificate;
                }
                else
                {
                    MessageBox.Show("Failed to authenticate", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch(AuthenticationError)
            {
                MessageBox.Show("Failed to authenticate", "Login failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch(CloudException cloud)
            {
                MessageBox.Show(cloud.Message, "Cloud problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch(Exception)
            {
                MessageBox.Show("Unexcepted error occured. Click OK to contiune", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            EnableIdentity();
            if (_connected)
                EnableGroups();
            statusBar.Items.Remove(statusLabel);
            Cursor = Cursors.Arrow;
        }
Ejemplo n.º 4
0
 public User(UserCertificate certificate)
 {
     ID = certificate.ID;
     CommonName = certificate.CommonName;
     Email = certificate.Email;
 }
Ejemplo n.º 5
0
 public async static Task FileWithPublicKey(string inputFile, string outputFile, UserCertificate certificate)
 {
     await File(inputFile, outputFile, certificate.PublicKey.Key as RSACryptoServiceProvider);
 }
Ejemplo n.º 6
0
        private async void newUserButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DisableAll();
            var statusLabel = new ToolStripStatusLabel("Creating new user");
            statusBar.Items.Add(statusLabel);
            using (var newUserForm = new NewUserForm())
            {
                if (newUserForm.ShowDialog() == DialogResult.OK)
                {
                    var newUserInfo = newUserForm.NewUserInfo;
                    var pathToSave = newUserForm.PathToSave;
                    try
                    {
                        DisconnectUser();
                        await ServerTransaction.CreateNewUser(newUserInfo, pathToSave);

                        _certificate = new UserCertificate(pathToSave);
                        _currentUser = _certificate;

                        Process addCertificateProcess = new Process();
                        addCertificateProcess.EnableRaisingEvents = false;
                        addCertificateProcess.StartInfo.FileName = pathToSave;
                        addCertificateProcess.Start();

                        loginLabel.Text = _currentUser.CommonName;

                        var message = string.Format("Welcome {0}.", _currentUser.CommonName);
                        MessageBox.Show(message, "New user", MessageBoxButtons.OK, MessageBoxIcon.Information);

                        _identitiesList.Add(_certificate);

                        await ConnectUser();
                    }
                    catch (UnknownCommadError error)
                    {
                        MessageBox.Show(error.Message);
                    }
                    catch (ServerResponseError error)
                    {
                        MessageBox.Show(error.Message);
                    }
                    catch (AuthenticationError error)
                    {
                        MessageBox.Show(error.Message);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Unexcepted error occured. Click OK to contiune", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
            EnableIdentity();
            if (_connected)
                EnableGroups();
            statusBar.Items.Remove(statusLabel);
            Cursor = Cursors.Arrow;
        }
Ejemplo n.º 7
0
        private async void getCertificatesButton_Click(object sender, EventArgs e)
        {
            Cursor = Cursors.WaitCursor;
            DisableAll();
            var statusLabel = new ToolStripStatusLabel("Downloadig certificates");
            statusBar.Items.Add(statusLabel);
            var selectedUsers = usersListBox.SelectedItems;
            if (selectedUsers.Count > 0)
            {
                try
                {
                    var certificates = new List<string>();
                    foreach (User user in selectedUsers)
                    {
                        var tempCertificate = Path.GetTempFileName();
                        statusLabel.Text = string.Format("Downloading certificate ({0})", user.CommonName);
                        await _cloud.DownloadCertificateAsync(tempCertificate, user);
                        certificates.Add(tempCertificate);
                    }
                    var certificatesStore = new X509Store(StoreName.AddressBook, StoreLocation.CurrentUser);
                    certificatesStore.Open(OpenFlags.ReadWrite);
                    foreach (var certificate in certificates)
                    {
                        var cert = new UserCertificate(certificate);
                        statusLabel.Text = string.Format("Adding certificate to user store ({0})", cert.CommonName);
                        certificatesStore.Add(cert);
                        File.Delete(certificate);
                    }
                    certificatesStore.Close();
                }
                catch (CloudException cloud)
                {
                    MessageBox.Show(cloud.Message, "Cloud problem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                catch (Exception)
                {
                    MessageBox.Show("Unexcepted error occured. Click OK to contiune", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                MessageBox.Show("Select users first", "Select users", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            EnableAll();
            statusBar.Items.Remove(statusLabel);
            Cursor = Cursors.Arrow;
        }
Ejemplo n.º 8
0
 private void loadFromFileButton_Click(object sender, EventArgs e)
 {
     Cursor = Cursors.WaitCursor;
     var statusLabel = new ToolStripStatusLabel("Creating new user");
     statusBar.Items.Add(statusLabel);
     var openFileDialog = new OpenFileDialog()
     {
         Filter = "Certificate (.pfx)|*.pfx",
         Multiselect = true
     };
     if (openFileDialog.ShowDialog() == DialogResult.OK)
     {
         foreach (var certificate in openFileDialog.FileNames)
         {
             try
             {
                 var cert = new UserCertificate(certificate);
                 if (cert.HasPrivateKey)
                 {
                     _identitiesList.Add(cert);
                 }
                 else
                 {
                     MessageBox.Show("Certificate doesn't include private key", "Ceritficate error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                 }
             }
             catch(UnknownCertificateError)
             {
                 MessageBox.Show("Certificate is not valid", "Certificate error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             }
             catch(Exception)
             {
                 MessageBox.Show("Unexcepted error occured. Click OK to contiune", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             }
         }
     }
     connectButton.Enabled = true;
     statusBar.Items.Remove(statusLabel);
     Cursor = Cursors.Arrow;
 }
Ejemplo n.º 9
0
        public async static Task<bool> CreateNewGroup(Group group, UserCertificate certificate)
        {
            string response;

            using (SslClient stream = new SslClient(_configuration.IP, _configuration.Port, certificate))
            {
                await stream.SendStringAsync("login");
                await Authenticate(stream, new User(certificate));
                await stream.SendStringAsync("new-group");
                response = await stream.ReceiveStringAsync();
                if (response != "provide-group-name")
                {
                    UnknownCommandError("provide-group-name", response);
                }
                await stream.SendStringAsync("group-name");
                await stream.SendStringAsync(group.Name);
                response = await stream.ReceiveStringAsync();
                if (response != "provide-group-password")
                {
                    UnknownCommandError("provide-group-password", response);
                }
                await stream.SendStringAsync("group-password");
                await stream.SendStringAsync(group.Password);
                response = await stream.ReceiveStringAsync();
                if (response == "group-exists")
                {
                    ServerResponseError(response);
                }
                else if (response != "group-added")
                {
                    UnknownCommandError("group-added", response);
                }
            }

            return true;

        }
Ejemplo n.º 10
0
 public async static Task<bool> Connect(UserCertificate certificate)
 {
     try
     {
         using (SslClient stream = new SslClient(_configuration.IP, _configuration.Port, certificate))
         {
             await stream.SendStringAsync("connect");
             await Authenticate(stream, new User(certificate));
         }
     }
     catch (AuthenticationError error)
     {
         return false;
     }
     return true;
 }