diff --git a/PEMStoreSSH/PEMStore.cs b/PEMStoreSSH/PEMStore.cs index d9b03e8..b8ed6b0 100644 --- a/PEMStoreSSH/PEMStore.cs +++ b/PEMStoreSSH/PEMStore.cs @@ -10,6 +10,7 @@ using System.Text; using System.Linq; using System.Security.Cryptography.X509Certificates; +using System.Threading; using PEMStoreSSH.RemoteHandlers; using Keyfactor.Extensions.Pam.Utilities; @@ -21,6 +22,8 @@ internal class PEMStore private const string NO_EXTENSION = "noext"; private const string FULL_SCAN = "fullscan"; + static Mutex mutex = new Mutex(false, "ModifyStore"); + public enum FormatTypeEnum { PEM, @@ -116,17 +119,23 @@ internal void RemoveCertificate(string alias) { try { + mutex.WaitOne(); CertificateHandler.RemoveCertificate(ServerType, StorePath, PrivateKeyPath, SSH, alias, String.IsNullOrEmpty(PrivateKeyPath)); } catch (Exception ex) { throw new PEMException($"Error attempting to remove certificate from store {StorePath}.", ex); } + finally + { + mutex.ReleaseMutex(); + } if (!string.IsNullOrEmpty(PrivateKeyPath)) { try { + mutex.WaitOne(); SSH.RemoveCertificateFile(PrivateKeyPath); SSH.CreateEmptyStoreFile(PrivateKeyPath); } @@ -134,6 +143,11 @@ internal void RemoveCertificate(string alias) { throw new PEMException($"Error attempting to remove private key {PrivateKeyPath}.", ex); } + finally + { + mutex.ReleaseMutex(); + } + } } @@ -141,6 +155,7 @@ internal void AddCertificateToStore(string cert, string alias, string pfxPasswor { try { + mutex.WaitOne(); List files = CertificateHandler.CreateCertificatePacket(cert, alias, pfxPassword, storePassword, !String.IsNullOrEmpty(PrivateKeyPath)); CertificateHandler.AddCertificateToStore(files, StorePath, PrivateKeyPath, SSH, ServerType, overwrite, containsPrivateKey, IsSingleCertificateStore); } @@ -148,6 +163,10 @@ internal void AddCertificateToStore(string cert, string alias, string pfxPasswor { throw new PEMException($"Error attempting to add certificate to store {StorePath}.", ex); } + finally + { + mutex.ReleaseMutex(); + } } internal bool IsValidStore(string path)