Inheritance: ISerializable
Exemplo n.º 1
0
        /// <summary>
        /// Handles before save event for <see cref="SubscriberUserControl"/>.
        /// </summary>
        /// <param name="sender">Source of the event.</param>
        /// <param name="e">Cancel event arguments.</param>
        private void SubscriberUserControl_BeforeSave(object sender, CancelEventArgs e)
        {
            try
            {
                string sharedSecret = m_dataContext.CurrentItem.SharedSecret;
                WindowsServiceClient windowsServiceClient;
                ClientRequest request;

                if (m_dataContext.SecurityMode == SecurityMode.Gateway)
                {
                    if (string.IsNullOrWhiteSpace(sharedSecret) || string.IsNullOrWhiteSpace(m_key) || string.IsNullOrWhiteSpace(m_iv))
                    {
                        MessageBox.Show("Failed to import key and initialization vectors for associated shared secret - these fields cannot be blank.", "Crypto Key Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        e.Cancel = true;
                    }
                    else
                    {
                        // Import key and initialization vector for subscriber into common crypto cache
                        if (ImportCipherKey(sharedSecret.Trim(), 256, m_key.Trim() + "|" + m_iv.Trim()))
                        {
                            ReloadServiceCryptoCache();
                            Cipher.ReloadCache();
                        }
                        else
                        {
                            MessageBox.Show("Failed to import key and initialization vectors for associated shared secret.", "Crypto Key Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            e.Cancel = true;
                        }
                    }
                }
                else
                {
                    try
                    {
                        if (SelfSignedCheckBox.IsChecked == true)
                        {
                            // If remote certificate is self-signed, ensure that we expect
                            // UntrustedRoot error to occur during certificate validation
                            m_dataContext.CurrentItem.ValidPolicyErrors |= SslPolicyErrors.RemoteCertificateChainErrors;
                            m_dataContext.CurrentItem.ValidChainFlags |= X509ChainStatusFlags.UntrustedRoot;
                        }

                        if ((object)m_dataContext.RemoteCertificateData != null)
                        {
                            windowsServiceClient = CommonFunctions.GetWindowsServiceClient();
                            windowsServiceClient.Helper.ReceivedServiceResponse += WindowsServiceClient_ReceivedServiceResponse;
                            m_certificateWaitHandle.Reset();

                            // If an srq file was imported to populate the fields on this page,
                            // then we will need to copy the attached certificate file from the
                            // temp folder to the correct location
                            request = new ClientRequest("INVOKE");
                            request.Arguments = new Arguments(string.Format("TLS!DATAPUBLISHER ImportCertificate {0}", m_dataContext.CurrentItem.RemoteCertificateFile));
                            request.Attachments.Add(m_dataContext.RemoteCertificateData);
                            windowsServiceClient.Helper.SendRequest(request);

                            if (!m_certificateWaitHandle.Wait(5000))
                                throw new InvalidOperationException("Timeout waiting for response to ImportCertificate command.");

                            m_dataContext.RemoteCertificateData = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        const string ErrorMessage = "Successfully imported subscription request, but was" +
                            " unable to import remote certificate. Check that the manager is properly" +
                            " connected to the service.";

                        CommonFunctions.LogException(null, "Import Subscription Request", ex);
                        MessageBox.Show(ErrorMessage, "Import Subscription Request Error");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to import key and initialization vectors for associated shared secret due to exception: " + ex.Message, "Crypto Key Import Error", MessageBoxButton.OK, MessageBoxImage.Error);
                e.Cancel = true;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Raises the <see cref="ReceivedClientRequest"/> event.
 /// </summary>
 /// <param name="request">The <see cref="ClientRequest"/> that was received.</param>
 /// <param name="requestSender">The <see cref="ClientInfo"/> object of the <paramref name="request"/> sender.</param>
 protected virtual void OnReceivedClientRequest(ClientRequest request, ClientInfo requestSender)
 {
     if ((object)ReceivedClientRequest != null)
         ReceivedClientRequest(this, new EventArgs<Guid, ClientRequest>(requestSender.ClientID, request));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Converts <see cref="string"/> to a <see cref="ClientRequest"/>.
        /// </summary>
        /// <param name="text">Text to be converted to a <see cref="ClientRequest"/>.</param>
        /// <returns><see cref="ClientRequest"/> object if parsing is successful; otherwise null.</returns>
        public static ClientRequest Parse(string text)
        {
            // Input text can't be null.
            if ((object)text == null)
                return null;

            // Input text can't be empty.
            text = text.Trim();
            if (text == "")
                return null;

            string[] textSegments = text.Split(' ');
            ClientRequest request = new ClientRequest();
            request.Command = textSegments[0].ToUpper();
            if (textSegments.Length == 1)
                request.Arguments = new Arguments("");
            else
                request.Arguments = new Arguments(text.Remove(0, text.IndexOf(' ') + 1).Trim());

            return request;
        }
Exemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientRequestInfo"/> class.
 /// </summary>
 /// <param name="sender"><see cref="ClientInfo"/> object of the <paramref name="request"/> sender.</param>
 /// <param name="request"><see cref="ClientRequest"/> object sent by the <paramref name="sender"/>.</param>
 public ClientRequestInfo(ClientInfo sender, ClientRequest request)
 {
     Request = request;
     Sender = sender;
     ReceivedAt = DateTime.UtcNow;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Sends a request to the <see cref="ServiceHelper"/> using <see cref="RemotingClient"/>.
        /// </summary>
        /// <param name="request"><see cref="ClientRequest"/> object to be sent.</param>
        public void SendRequest(ClientRequest request)
        {
            try
            {
                // Intercept TRANSFER requests for uploads.
                if (request.Command == "TRANSFER" && request.Arguments.Exists("upload"))
                {
                    string source = FilePath.GetAbsolutePath(request.Arguments["orderedarg1"]);
                    if (File.Exists(source))
                    {
                        // Attach the file content.
                        request.Attachments.Add(File.ReadAllBytes(source));
                    }
                    else
                    {
                        // File does not exist.
                        throw new FileNotFoundException(string.Format("File '{0}' does not exist", source));
                    }
                }

                // Pass the request along.
                m_remotingClient.SendAsync(request);
            }
            catch (Exception ex)
            {
                UpdateStatus(UpdateType.Alarm, "Error processing request - {0}", ex.Message);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Sends a request to the <see cref="ServiceHelper"/> using <see cref="RemotingClient"/>.
 /// </summary>
 /// <param name="request"><see cref="ClientRequest"/> object to be sent.</param>
 public void SendRequest(ClientRequest request)
 {
     m_remotingClient.SendAsync(request);
 }
        private bool TryCreateRequest()
        {
            try
            {
                // Generate authorization request
                SaveFileDialog saveFileDialog;
                WindowsServiceClient serviceClient;
                ClientRequest clientRequest;
                AuthenticationRequest request;
                string[] keyIV;

                saveFileDialog = new SaveFileDialog();
                saveFileDialog.DefaultExt = ".srq";
                saveFileDialog.Filter = "Subscription Requests|*.srq|All Files|*.*";

                if (saveFileDialog.ShowDialog() == true)
                {
                    request = new AuthenticationRequest();

                    // Set up the request
                    request.Acronym = SubscriberAcronym;
                    request.Name = SubscriberName;
                    request.ValidIPAddresses = ValidIPAddresses;

                    // Cipher key only applies to Gateway security
                    if (SecurityMode == SecurityMode.Gateway)
                    {
                        // Export cipher key to common crypto cache
                        if (!ExportCipherKey(SharedKey, 256))
                            throw new Exception("Failed to export cipher keys from common key cache.");

                        // Reload local crypto cache and get key and IV
                        // that go into the authentication request
                        Cipher.ReloadCache();
                        keyIV = Cipher.ExportKeyIV(SharedKey, 256).Split('|');

                        // Set up crypto settings in the request
                        request.SharedSecret = SharedKey;
                        request.AuthenticationID = IdentityCertificate;
                        request.Key = keyIV[0];
                        request.IV = keyIV[1];
                    }

                    // Local certificate only applies to TLS security
                    if (SecurityMode == SecurityMode.TLS)
                    {
                        if (File.Exists(m_localCertificateFile))
                        {
                            request.CertificateFile = File.ReadAllBytes(m_localCertificateFile);
                        }
                        else
                        {
                            try
                            {
                                serviceClient = CommonFunctions.GetWindowsServiceClient();
                                serviceClient.Helper.ReceivedServiceResponse += Helper_ReceivedServiceResponse;
                                serviceClient.Helper.SendRequest("INVOKE TLS!DATAPUBLISHER GetLocalCertificate");

                                // Wait for command response allowing for processing time
                                if ((object)m_responseComplete != null)
                                {
                                    if (!m_responseComplete.WaitOne(5000))
                                        throw new TimeoutException("Timed-out after 5 seconds waiting for service response.");
                                }

                                request.CertificateFile = m_localCertificateData;
                            }
                            catch (Exception ex)
                            {
                                string message = string.Format("Unable to get the local certificate used by the service: {0}", ex.Message);
                                throw new InvalidOperationException(message, ex);
                            }
                        }

                        if ((object)m_remoteCertificateData != null)
                        {
                            serviceClient = CommonFunctions.GetWindowsServiceClient();
                            serviceClient.Helper.ReceivedServiceResponse += Helper_ReceivedServiceResponse;

                            clientRequest = new ClientRequest("INVOKE");
                            clientRequest.Arguments = new Arguments(string.Format("TLS!DATAPUBLISHER ImportCertificate {0}", m_remoteCertificateFile));
                            clientRequest.Attachments.Add(m_remoteCertificateData);
                            serviceClient.Helper.SendRequest(clientRequest);

                            if ((object)m_responseComplete != null)
                            {
                                if (!m_responseComplete.WaitOne(5000))
                                    throw new InvalidOperationException("Timeout waiting for response to ImportCertificate command.");
                            }

                            m_remoteCertificateData = null;
                        }
                    }

                    // Create the request
                    using (FileStream requestStream = File.OpenWrite(saveFileDialog.FileName))
                    {
                        Serialization.Serialize(request, SerializationFormat.Binary, requestStream);
                    }

                    // Send ReloadCryptoCache to service
                    if (SecurityMode == SecurityMode.Gateway)
                        ReloadServiceCryptoCache();

                    return true;
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("Error creating authorization request: {0}", ex.Message);
                Popup(message, "Subscription Request Error", MessageBoxImage.Error);
                CommonFunctions.LogException(null, "Subscription Request", ex);
            }

            return false;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ClientRequestInfo"/> class.
 /// </summary>
 /// <param name="sender"><see cref="ClientInfo"/> object of the <paramref name="request"/> sender.</param>
 /// <param name="request"><see cref="ClientRequest"/> object sent by the <paramref name="sender"/>.</param>
 public ClientRequestInfo(ClientInfo sender, ClientRequest request)
 {
     Request    = request;
     Sender     = sender;
     ReceivedAt = DateTime.UtcNow;
 }