public void DeleteTrustCenter(TrustCenterMetaInfo trustCenterMetaInfo) { this.ConfigManager.DeleteTrustCenterFromConfig(trustCenterMetaInfo, this.Config); this.ConfigManager.SaveConfig(this.Config); this.TrustCenterManager.DeleteTrustCenterFile(trustCenterMetaInfo.Name); this.TrustCenterManager.DeleteCertificatesOfTrustCenter(this.Certificates, trustCenterMetaInfo.Name); }
private static async Task <byte[]> ReadFileAsync(TrustCenterMetaInfo trustCenter, string dataFolderPath) { byte[] result; using (var stream = File.Open(dataFolderPath + trustCenter.Name + @".txt", FileMode.Open)) { result = new byte[stream.Length]; await stream.ReadAsync(result, 0, (int)stream.Length); } return(result); }
public async Task <IEnumerable <Certificate> > ImportCertificatesAsync(TrustCenterMetaInfo trustCenterMetaInfo, string dataFolderPath) { try { var fileContent = await ReadFileAsync(trustCenterMetaInfo, dataFolderPath).ConfigureAwait(false); var certificates = GetCertificatesFromByteArray(trustCenterMetaInfo, fileContent); return(certificates); } catch (Exception) { return(new HashSet <Certificate>()); } }
public void AddTrustCenterAsyncTest() { #region Arrange var moqCore = new Mock <Core>(); moqCore.Setup(m => m.IsTrustCenterInputValid(It.IsAny <string>(), It.IsAny <string>())) .Returns(true); var moqConfigManager = new Mock <ConfigManager> { CallBase = true }; moqConfigManager.Setup(m => m.SaveConfig(It.IsAny <Config>())) .Returns(Samples.ProvideSampleConfig); var moqTrustCenterManager = new Mock <TrustCenterManager>(); moqTrustCenterManager.Setup(m => m.DownloadCertificatesAsync(It.IsAny <TrustCenterMetaInfo>())) .Returns(Samples.ProvideSampleTaskByteArray); moqTrustCenterManager.Setup(m => m.ImportCertificatesAsync(It.IsAny <TrustCenterMetaInfo>())) .Returns(Samples.ProvideTaskIEnumerableCertificate); var core = moqCore.Object; core.ConfigManager = moqConfigManager.Object; core.TrustCenterManager = moqTrustCenterManager.Object; core.Config.TrustCenterMetaInfos.RemoveAll(m => true); #endregion #region Act var result = core.AddTrustCenterAsync("name", "url"); #endregion #region Assert var expectedCount = 1; var expectedResult = new TrustCenterMetaInfo("name", "url", DateTime.Now); Assert.Equal(expectedCount, core.Config.TrustCenterMetaInfos.Count); Assert.Equal(3, core.Certificates.Count); Equals(result, expectedResult); #endregion }
private static HashSet <Certificate> GetCertificatesFromByteArray(TrustCenterMetaInfo trustCenterMetaInfo, byte[] fileContent) { var certificatesTxt = System.Text.Encoding.UTF8.GetString(fileContent).Split( new[] { Environment.NewLine + Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var collection = new BlockingCollection <(X509Certificate2 cert, string keySize)>(certificatesTxt.Length); var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, }; Parallel.ForEach(certificatesTxt, parallelOptions, (s, state) => { try { var cert = new X509Certificate2(Convert.FromBase64String(s)); var keySize = cert.PublicKey.Key.KeySize.ToString(); collection.Add((cert, keySize)); } catch { } }); var cer = certificatesTxt.Select(certificateTxt => new X509Certificate2(Convert.FromBase64String(certificateTxt))); var certificates = new HashSet <Certificate>(); certificates.UnionWith(collection.Select(c => { var certificate = new Certificate(); certificate.Subject = GetSubjectElementsToDisplay(c.cert.Subject); certificate.SerialNumber = c.cert.SerialNumber; certificate.NotAfter = c.cert.NotAfter.Date.ToShortDateString(); certificate.NotBefore = c.cert.NotBefore.Date.ToShortDateString(); certificate.Thumbprint = c.cert.Thumbprint; certificate.PublicKeyLength = c.keySize; certificate.TrustCenterName = trustCenterMetaInfo.Name; return(certificate); })); return(certificates); }
public async Task <TrustCenterMetaInfo> AddTrustCenterAsync(string newTrustCenterName, string newTrustCenterUrl) { if (!this.IsTrustCenterInputValid(newTrustCenterName, newTrustCenterUrl)) { return(null); } var newTrustCenterMetaInfo = new TrustCenterMetaInfo(newTrustCenterName, newTrustCenterUrl, DateTime.Now); await this.TrustCenterManager.DownloadCertificatesAsync(newTrustCenterMetaInfo); var importedCertificates = await this.TrustCenterManager.ImportCertificatesAsync(newTrustCenterMetaInfo); this.Certificates.AddRange(importedCertificates); this.ConfigManager.AddTrustCenterToConfig(newTrustCenterMetaInfo, this.Config); this.ConfigManager.SaveConfig(this.Config); return(newTrustCenterMetaInfo); }
public async Task <byte[]> DownloadCertificates(TrustCenterMetaInfo trustCenterMetaInfo, string dataFolderPath) { if (!Directory.Exists(dataFolderPath)) { Directory.CreateDirectory(dataFolderPath); } var client = new HttpClient(); var response = await client.GetAsync(trustCenterMetaInfo.TrustCenterUrl); var content = await response.Content.ReadAsByteArrayAsync(); using (var stream = File.OpenWrite(GetFilePath(trustCenterMetaInfo.Name, dataFolderPath))) { await stream.WriteAsync(content, 0, content.Length - 1); } return(content); }
public virtual async Task <IEnumerable <Certificate> > ImportCertificatesAsync(TrustCenterMetaInfo trustCenterMetaInfo) { return(await this.ImportManager.ImportCertificatesAsync(trustCenterMetaInfo, _dataFolderPath).ConfigureAwait(false)); }
public virtual async Task <byte[]> DownloadCertificatesAsync(TrustCenterMetaInfo trustCenterMetaInfo) { return(await this.DownloadManager.DownloadCertificates(trustCenterMetaInfo, _dataFolderPath)); }
public Config DeleteTrustCenterFromConfig(TrustCenterMetaInfo trustCenterMetaInfo, Config config) { config.TrustCenterMetaInfos.RemoveAll(tc => tc.Name.Equals(trustCenterMetaInfo.Name)); return(config); }
public Config AddTrustCenterToConfig(TrustCenterMetaInfo trustCenterMetaInfo, Config config) { config.TrustCenterMetaInfos.Add(trustCenterMetaInfo); return(config); }
public async Task <TrustCenterMetaInfo> ReloadCertificatesOfTrustCenter(TrustCenterMetaInfo trustCenterMetaInfo) { this.DeleteTrustCenter(trustCenterMetaInfo); return(await this.AddTrustCenterAsync(trustCenterMetaInfo.Name, trustCenterMetaInfo.TrustCenterUrl)); }