public static String Encrypt(Object Value, String Thumbprint)
 {
     if (Value == null)
     {
         return(null);
     }
     else
     {
         X509Certificate2         EncryptCert  = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(Thumbprint);
         RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)EncryptCert.PublicKey.Key;
         var    valueJson      = JsonConvert.SerializeObject(Value);
         var    EncryptedBytes = System.Text.Encoding.Default.GetBytes(valueJson);
         byte[] EncryptedData  = rsaEncryptor.Encrypt(EncryptedBytes, true);
         return(Convert.ToBase64String(EncryptedData));
     }
 }
 public static Object Decrypt(Object EncryptedValue, String Thumbprint)
 {
     if (EncryptedValue == null)
     {
         return(null);
     }
     else if (!(EncryptedValue is string))
     {
         throw new Exception("Cannot decrypt value. Value to decrypt was not a string.");
     }
     else
     {
         X509Certificate2         EncryptCert     = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(Thumbprint);
         Byte[]                   EncryptedString = Convert.FromBase64String((string)EncryptedValue);
         RSACryptoServiceProvider rsaEncryptor    = (RSACryptoServiceProvider)EncryptCert.PrivateKey;
         byte[]                   EncryptedData   = rsaEncryptor.Decrypt(EncryptedString, true);
         var valueJson = System.Text.Encoding.Default.GetString(EncryptedData);
         return(JsonConvert.DeserializeObject(valueJson));
     }
 }
Пример #3
0
        public async Task <X509Certificate2> CreateLocalRunAs(string applicationID, String certName)
        {
            X509Certificate2 cert = null;
            var runAsApplication  = await graphClient.Applications.ListAsync("$filter=appId eq '" + applicationID + "'");

            foreach (var app in runAsApplication)
            {
                if (app.AppId == applicationID)
                {
                    var existingCredentialKeys = await graphClient.Applications.ListKeyCredentialsAsync(app.ObjectId);

                    if (existingCredentialKeys != null)
                    {
                        var thumbprint = CreateSelfSignedCertificate(certName);
                        cert = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(thumbprint);
                        await UpdateADApplication(cert.NotBefore, cert.NotAfter, Convert.ToBase64String(cert.RawData), app.ObjectId);
                    }
                }
            }
            return(cert);
        }
Пример #4
0
        private string updateEncryptionCertificateIfExpiring(String baseWorkspace, String thumbprint)
        {
            if (thumbprint != null)
            {
                var encryptionCert = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(thumbprint);
                // If the certificate will expire 30 days from now, ask to create a new one and encyprt assets with new thumbprint.
                if (Convert.ToDateTime(encryptionCert.GetExpirationDateString()) < DateTime.Now.AddDays(30))
                {
                    var messageBoxResult = System.Windows.Forms.MessageBox.Show(
                        string.Format("Your certificate to encrypt local assets will expire on '{0}'. Do you want to generate a new certificate?", encryptionCert.GetExpirationDateString())
                        , "Expiring certificate", System.Windows.Forms.MessageBoxButtons.YesNoCancel, System.Windows.Forms.MessageBoxIcon.Warning
                        );

                    if (messageBoxResult == System.Windows.Forms.DialogResult.Yes)
                    {
                        // Create new certificate for encryption
                        certObj.CreateCertificateRequest(Properties.Settings.Default.certName);
                        var selfSignedCert = certObj.InstallCertficate();
                        var newThumbprint  = selfSignedCert.Thumbprint;

                        // Reset local assets with new encryption thumbprint
                        string[] secureAssetFiles = Directory.GetFiles(baseWorkspace, "SecureLocalAssets.json", SearchOption.AllDirectories);
                        foreach (var secureAssetFile in secureAssetFiles)
                        {
                            var localAssets = AutomationAssetManager.GetLocalEncryptedAssets(Path.GetDirectoryName(secureAssetFile), thumbprint);
                            AutomationAssetManager.SetLocalEncryptedAssets(Path.GetDirectoryName(secureAssetFile), localAssets, newThumbprint);
                        }

                        // Set new thumbprint in configuration file.
                        SetCertificateInConfigFile(newThumbprint);

                        // Remove old thumbprint
                        RemoveCertificateWithThumbprint(thumbprint);
                        return(newThumbprint);
                    }
                }
            }
            return(thumbprint);
        }
        public AutomationISEControl()
        {
            try
            {
                InitializeComponent();
                iseClient = new AutomationISEClient();
                fileTransferQueue = new BlockingCollection<RunbookTransferJob>(new ConcurrentQueue<RunbookTransferJob>(),50);
                fileTransferWorkerProgress = new Progress<RunbookTransferProgress>(updateUiWithTransferProgress);
                fileTransferWorker = Task.Factory.StartNew(() => processJobsFromQueue(fileTransferWorkerProgress), TaskCreationOptions.LongRunning);

                /* Determine working directory */
                String localWorkspace = Properties.Settings.Default["localWorkspace"].ToString();
                if (localWorkspace == "")
                {
                    String systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
                    localWorkspace = System.IO.Path.Combine(systemDrive, "AutomationWorkspace");
                    Properties.Settings.Default["localWorkspace"] = localWorkspace;
                    Properties.Settings.Default.Save();
                }
                iseClient.baseWorkspace = localWorkspace;

                /* Update UI */
                workspaceTextBox.Text = iseClient.baseWorkspace;
                userNameTextBox.Text = Properties.Settings.Default["ADUserName"].ToString();
                subscriptionComboBox.IsEnabled = false;
                accountsComboBox.IsEnabled = false;

                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetVariable);
                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCredential);
                //assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCertificate);
                //assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetConnection);

                setRunbookAndAssetNonSelectionButtonState(false);
                setAssetSelectionButtonState(false);
                setRunbookSelectionButtonState(false);

                // Generate self signed certificate for encrypting local assets in the current user store Cert:\CurrentUser\My\
                var certObj = new AutomationSelfSignedCertificate();
                String selfSignedThumbprint = certObj.CreateSelfSignedCertificate();
                certificateTextBox.Text = selfSignedThumbprint;
                UpdateStatusBox(configurationStatusTextBox, "Certificate to use for encrypting local assets is " + selfSignedThumbprint);

                // Load feedback page to increase load time before users clicks on feedback tab
                surveyBrowserControl.Navigate(new Uri(Constants.feedbackURI));

                startContinualGet();
            }
            catch (Exception exception)
            {
                var detailsDialog = System.Windows.Forms.MessageBox.Show(exception.Message);
            }
        }
        public AutomationISEControl()
        {
            try
            {
                InitializeComponent();
                iseClient = new AutomationISEClient();
                /* Spinner animation stuff */
                backgroundWorkLock = new Object();
                progressSpinnerStoryboard = (Storyboard)FindResource("bigGearRotationStoryboard");
                progressSpinnerStoryboardReverse = (Storyboard)FindResource("bigGearRotationStoryboardReverse");
                miniProgressSpinnerStoryboard = (Storyboard)FindResource("smallGearRotationStoryboard");
                miniProgressSpinnerStoryboardReverse = (Storyboard)FindResource("smallGearRotationStoryboardReverse");

                /* Determine working directory */
                String localWorkspace = Properties.Settings.Default["localWorkspace"].ToString();
                if (localWorkspace == "")
                {
                    String userProfile = Environment.GetEnvironmentVariable("USERPROFILE") + "\\";
                    localWorkspace = System.IO.Path.Combine(userProfile, "AutomationWorkspace");
                    Properties.Settings.Default["localWorkspace"] = localWorkspace;
                    Properties.Settings.Default.Save();
                }
                iseClient.baseWorkspace = localWorkspace;
                promptShortened = false;

                /* Initialize Timers */
                refreshAccountDataTimer = new System.Timers.Timer();
                refreshAccountDataTimer.Interval = 30000; //30 seconds
                refreshAccountDataTimer.Elapsed += new ElapsedEventHandler(refreshAccountData);

                refreshAuthTokenTimer = new System.Timers.Timer();
                refreshAuthTokenTimer.Interval = Constants.tokenRefreshInterval * 60000;
                refreshAuthTokenTimer.Elapsed += new ElapsedEventHandler(refreshAuthToken);

                /* Update UI */
                workspaceTextBox.Text = iseClient.baseWorkspace;
                userNameTextBox.Text = Properties.Settings.Default["ADUserName"].ToString();
                subscriptionComboBox.IsEnabled = false;
                accountsComboBox.IsEnabled = false;

                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetConnection);
                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCredential);
                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetVariable);
                //assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCertificate);

                setCreationButtonStatesTo(false);
                setAllAssetButtonStatesTo(false);
                assetsComboBox.IsEnabled = false;
                setAllRunbookButtonStatesTo(false);

                // Generate self-signed certificate for encrypting local assets in the current user store Cert:\CurrentUser\My\
                var certObj = new AutomationSelfSignedCertificate();
                certificateThumbprint = certObj.CreateSelfSignedCertificate();
                certificateTextBox.Text = certificateThumbprint;
                UpdateStatusBox(configurationStatusTextBox, "Thumbprint of certificate used to encrypt local assets: " + certificateThumbprint);

                // Load feedback and help page preemptively
                surveyBrowserControl.Navigate(new Uri(Constants.feedbackURI));
                helpBrowserControl.Navigate(new Uri(Constants.helpURI));
            }
            catch (Exception exception)
            {
                var detailsDialog = System.Windows.Forms.MessageBox.Show(exception.Message);
            }
        }
        public AutomationISEControl()
        {
            try
            {
                InitializeComponent();
                iseClient = new AutomationISEClient();
                backgroundWorkLock = new Object();
                progressSpinnerStoryboard = (Storyboard)FindResource("bigGearRotationStoryboard");
                progressSpinnerStoryboardReverse = (Storyboard)FindResource("bigGearRotationStoryboardReverse");
                miniProgressSpinnerStoryboard = (Storyboard)FindResource("smallGearRotationStoryboard");
                miniProgressSpinnerStoryboardReverse = (Storyboard)FindResource("smallGearRotationStoryboardReverse");

                /* Determine working directory */
                String localWorkspace = Properties.Settings.Default["localWorkspace"].ToString();
                if (localWorkspace == "")
                {
                    String systemDrive = Environment.GetEnvironmentVariable("SystemDrive") + "\\";
                    localWorkspace = System.IO.Path.Combine(systemDrive, "AutomationWorkspace");
                    Properties.Settings.Default["localWorkspace"] = localWorkspace;
                    Properties.Settings.Default.Save();
                }
                iseClient.baseWorkspace = localWorkspace;

                /* Update UI */
                workspaceTextBox.Text = iseClient.baseWorkspace;
                userNameTextBox.Text = Properties.Settings.Default["ADUserName"].ToString();
                subscriptionComboBox.IsEnabled = false;
                accountsComboBox.IsEnabled = false;

                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetVariable);
                assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCredential);
                //assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetCertificate);
                //assetsComboBox.Items.Add(AutomationISE.Model.Constants.assetConnection);

                setRunbookAndAssetNonSelectionButtonState(false);
                setAssetSelectionButtonState(false);
                assetsComboBox.IsEnabled = false;
                setRunbookSelectionButtonState(false);

                // Generate self signed certificate for encrypting local assets in the current user store Cert:\CurrentUser\My\
                var certObj = new AutomationSelfSignedCertificate();
                certificateThumbprint = certObj.CreateSelfSignedCertificate();
                certificateTextBox.Text = certificateThumbprint;
                UpdateStatusBox(configurationStatusTextBox, "Thumbprint of certificate used to encrypt local assets: " + certificateThumbprint);

                // Load feedback and help page to increase load time before users clicks on these tabs
                surveyBrowserControl.Navigate(new Uri(Constants.feedbackURI));
                helpBrowserControl.Navigate(new Uri(Constants.helpURI));

                startContinualGet();
                startRefreshTokenTimer();
            }
            catch (Exception exception)
            {
                var detailsDialog = System.Windows.Forms.MessageBox.Show(exception.Message);
            }
        }