Skip to content

Commit 9af5f8e

Browse files
committed
Check that a certificate with the given thumbprint exists when the user clicks the 'Change' button. This prevents the user from setting the ISE to attempt to use a nonexistent certificate
1 parent 69e4c91 commit 9af5f8e

File tree

3 files changed

+34
-43
lines changed

3 files changed

+34
-43
lines changed

AutomationISE/AutomationISEControl.xaml.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1056,13 +1056,15 @@ private void certificateButton_Click(object sender, RoutedEventArgs e)
10561056
{
10571057
/* Strip bad character that appears when you copy/paste from certmgr */
10581058
String cleanedString = certificateTextBox.Text.Trim(new char[] { '\u200E' });
1059+
/* Throw exception if the given thumbprint is not a valid certificate */
1060+
AutomationSelfSignedCertificate.GetCertificateWithThumbprint(cleanedString);
10591061
AutomationSelfSignedCertificate.SetCertificateInConfigFile(cleanedString);
10601062
certificateThumbprint = cleanedString;
10611063
UpdateStatusBox(configurationStatusTextBox, "Updated thumbprint of certificate used to encrypt local assets: " + certificateThumbprint);
10621064
}
10631065
catch (Exception ex)
10641066
{
1065-
MessageBox.Show("The thumbprint could not be updated " + ex.Message, "Error");
1067+
MessageBox.Show("The thumbprint could not be updated:\r\n" + ex.Message + ".", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
10661068
}
10671069
}
10681070

AutomationISE/Model/AutomationSelfSignedCertificate.cs

+25
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using System.Diagnostics;
1818
using System.IO;
1919
using System.Web.Script.Serialization;
20+
using System.Security.Cryptography;
21+
using System.Security.Cryptography.X509Certificates;
2022

2123
namespace AutomationISE.Model
2224
{
@@ -148,6 +150,29 @@ private static String GetConfigPath()
148150
string configFilePath = System.IO.Path.Combine(modulePath, PSModuleConfiguration.ModuleData.ConfigFileName);
149151

150152
return configFilePath;
153+
}
154+
155+
public static X509Certificate2 GetCertificateWithThumbprint(string thumbprint)
156+
{
157+
X509Store CertStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
158+
try
159+
{
160+
CertStore.Open(OpenFlags.ReadOnly);
161+
}
162+
catch (Exception ex)
163+
{
164+
throw new Exception("Error reading certificate store", ex);
165+
}
166+
167+
var CertCollection = CertStore.Certificates;
168+
var EncryptCert = CertCollection.Find(X509FindType.FindByThumbprint, thumbprint, false);
169+
CertStore.Close();
170+
171+
if (EncryptCert.Count == 0)
172+
{
173+
throw new Exception("Certificate with thumbprint " + thumbprint + " does not exist in HKLM\\My");
174+
}
175+
return EncryptCert[0];
151176
}
152177
}
153178
}

AutomationISE/Model/LocalAssetsStore.cs

+6-42
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,9 @@ public static String Encrypt(Object Value, String Thumbprint)
219219
return null;
220220
}
221221
else
222-
{
223-
X509Store CertStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
224-
try
225-
{
226-
CertStore.Open(OpenFlags.ReadOnly);
227-
}
228-
catch (Exception ex)
229-
{
230-
throw new Exception("Error reading certificate store", ex);
231-
}
232-
233-
var CertCollection = CertStore.Certificates;
234-
var EncryptCert = CertCollection.Find(X509FindType.FindByThumbprint, Thumbprint, false);
235-
CertStore.Close();
236-
237-
if (EncryptCert.Count == 0)
238-
{
239-
throw new Exception("Certificate:" + Thumbprint + " does not exist in HKLM\\Root");
240-
}
241-
242-
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)EncryptCert[0].PublicKey.Key;
222+
{
223+
X509Certificate2 EncryptCert = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(Thumbprint);
224+
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)EncryptCert.PublicKey.Key;
243225
var valueJson = JsonConvert.SerializeObject(Value);
244226
var EncryptedBytes = System.Text.Encoding.Default.GetBytes(valueJson);
245227
byte[] EncryptedData = rsaEncryptor.Encrypt(EncryptedBytes, true);
@@ -258,28 +240,10 @@ public static Object Decrypt(Object EncryptedValue, String Thumbprint)
258240
throw new Exception("Cannot decrypt value. Value to decrypt was not a string.");
259241
}
260242
else
261-
{
262-
X509Store CertStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
263-
try
264-
{
265-
CertStore.Open(OpenFlags.ReadOnly);
266-
}
267-
catch (Exception ex)
268-
{
269-
throw new Exception("Error reading certificate store", ex);
270-
}
271-
272-
var CertCollection = CertStore.Certificates;
273-
var EncryptCert = CertCollection.Find(X509FindType.FindByThumbprint, Thumbprint, false);
274-
CertStore.Close();
275-
276-
if (EncryptCert.Count == 0)
277-
{
278-
throw new Exception("Certificate:" + Thumbprint + " does not exist in HKLM\\My");
279-
}
280-
243+
{
244+
X509Certificate2 EncryptCert = AutomationSelfSignedCertificate.GetCertificateWithThumbprint(Thumbprint);
281245
Byte[] EncryptedString = Convert.FromBase64String((string)EncryptedValue);
282-
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)EncryptCert[0].PrivateKey;
246+
RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)EncryptCert.PrivateKey;
283247
byte[] EncryptedData = rsaEncryptor.Decrypt(EncryptedString, true);
284248
var valueJson = System.Text.Encoding.Default.GetString(EncryptedData);
285249
return JsonConvert.DeserializeObject(valueJson);

0 commit comments

Comments
 (0)