private static bool IsPFXBlob(byte[] pfxBlobData) { if (pfxBlobData == null) { throw new ArgumentNullException("pfxBlobData"); } EnableLiveId.CRYPTOAPI_BLOB cryptoapi_BLOB; cryptoapi_BLOB.cbData = (uint)pfxBlobData.Length; cryptoapi_BLOB.pbData = pfxBlobData; bool result = EnableLiveId.PFXIsPFXBlob(ref cryptoapi_BLOB); int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error != 0) { throw new Win32Exception(lastWin32Error); } return(result); }
private SearchResultByNonUniqueKey FindCertificateBySubject(string trimmedIssuedTo, out X509Certificate2 x509Certificate2) { x509Certificate2 = null; X509Store x509Store = new X509Store(StoreLocation.LocalMachine); x509Store.Open(OpenFlags.ReadOnly); base.WriteVerbose(Strings.SearchingForCertificateBySubject(trimmedIssuedTo)); try { foreach (X509Certificate2 x509Certificate3 in x509Store.Certificates) { base.WriteVerbose(Strings.TryingCertificate(x509Certificate3.Subject, x509Certificate3.Thumbprint)); string text = x509Certificate3.SubjectName.Decode(X500DistinguishedNameFlags.Reversed | X500DistinguishedNameFlags.DoNotUsePlusSign | X500DistinguishedNameFlags.DoNotUseQuotes | X500DistinguishedNameFlags.UseCommas); if (string.Equals(trimmedIssuedTo, text, StringComparison.InvariantCultureIgnoreCase) || string.Equals(trimmedIssuedTo, EnableLiveId.GetCNValueFromX500DN(text), StringComparison.InvariantCultureIgnoreCase)) { base.WriteVerbose(Strings.CertificateFound); if (x509Certificate2 != null) { return(SearchResultByNonUniqueKey.FoundMultiple); } x509Certificate2 = x509Certificate3; } } } finally { x509Store.Close(); } if (x509Certificate2 != null) { return(SearchResultByNonUniqueKey.FoundSingle); } base.WriteVerbose(Strings.CertificateWasNotFound); return(SearchResultByNonUniqueKey.NotFound); }
protected override void InternalValidate() { TaskLogger.LogEnter(); ServiceControllerStatus serviceControllerStatus = ServiceControllerStatus.Stopped; Exception ex = null; try { using (ServiceController serviceController = new ServiceController("RPSSvc")) { serviceControllerStatus = serviceController.Status; } } catch (Win32Exception ex2) { ex = ex2; } catch (InvalidOperationException ex3) { ex = ex3; } if (ex != null || serviceControllerStatus != ServiceControllerStatus.Running) { base.WriteError(new ArgumentException(Strings.RPSSvcNotRunning), ErrorCategory.InvalidArgument, null); } if (!Directory.Exists(EnableLiveId.RPSInstallLocation) || !Directory.Exists(EnableLiveId.RPSConfigLocation) || !Directory.Exists(EnableLiveId.RPSConfigCertsLocation)) { base.WriteError(new ArgumentException(Strings.CannotFindRPSInstallLocation(EnableLiveId.RPSInstallLocation)), ErrorCategory.InvalidArgument, null); } if (!File.Exists(EnableLiveId.RPSServerFile)) { base.WriteError(new ArgumentException(Strings.CannotFindRPSServerFile(EnableLiveId.RPSServerFile)), ErrorCategory.InvalidArgument, null); } DirectoryInfo directoryInfo = new DirectoryInfo(EnableLiveId.RPSConfigCertsLocation); FileInfo[] files = directoryInfo.GetFiles("*", SearchOption.AllDirectories); if (files.Length > 0) { this.WriteWarning(Strings.DirectoryMustBeEmpty(EnableLiveId.RPSConfigCertsLocation)); this.doNothing = true; } if (this.MsoSiteId <= 0U) { base.WriteError(new ArgumentException(Strings.SiteIdMustBePositive(this.MsoSiteId)), ErrorCategory.InvalidArgument, null); } if (this.SiteId < 0U) { base.WriteError(new ArgumentException(Strings.SiteIdMustBePositive(this.SiteId)), ErrorCategory.InvalidArgument, null); } if (base.ParameterSetName == "PfxFileAndPassword") { if (!File.Exists(this.CertFile)) { base.WriteError(new ArgumentException(Strings.CertFileNotFound(this.CertFile)), ErrorCategory.InvalidArgument, null); } byte[] array = null; try { array = EnableLiveId.ReadBinaryFile(this.CertFile); } catch (IOException ex4) { base.WriteError(new InvalidOperationException(Strings.ErrorTryingToReadPfx(this.CertFile, ex4.Message), ex4), ErrorCategory.InvalidOperation, null); } try { if (!EnableLiveId.IsPFXBlob(array)) { base.WriteError(new ArgumentException(Strings.CertFileIsNotPfx(this.CertFile)), ErrorCategory.InvalidArgument, null); } } catch (Win32Exception ex5) { base.WriteError(new InvalidOperationException(Strings.ErrorTryingToReadPfx(this.CertFile, ex5.Message), ex5), ErrorCategory.InvalidOperation, null); } try { this.x509Certificate2 = new X509Certificate2(array, this.Password, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); goto IL_3D7; } catch (CryptographicException ex6) { base.WriteError(new InvalidOperationException(ex6.Message, ex6), ErrorCategory.InvalidOperation, null); goto IL_3D7; } } if (base.ParameterSetName == "IssuedTo") { string trimmedIssuedTo = this.IssuedTo.Trim(); SearchResultByNonUniqueKey searchResultByNonUniqueKey = this.FindCertificateBySubject(trimmedIssuedTo, out this.x509Certificate2); if (searchResultByNonUniqueKey == SearchResultByNonUniqueKey.NotFound) { base.WriteError(new ArgumentException(Strings.CantFindCertBySubject(this.IssuedTo)), ErrorCategory.InvalidArgument, null); } else if (searchResultByNonUniqueKey == SearchResultByNonUniqueKey.FoundMultiple) { base.WriteError(new ArgumentException(Strings.MultipleCertsFoundBySubject(this.IssuedTo)), ErrorCategory.InvalidArgument, null); } } else { string text; if (this.SiteId > 0U) { text = this.SHA1Thumbprint.Trim().Replace(" ", "").ToUpper(); if (text.Length != 40 || Regex.IsMatch(text, "[^0-9A-F]")) { base.WriteError(new ArgumentException(Strings.InvalidThumbprintFormat(this.SHA1Thumbprint)), ErrorCategory.InvalidArgument, null); } if (!this.FindCertificateByThumbprint(text, out this.x509Certificate2)) { base.WriteError(new ArgumentException(Strings.CantFindCertByThumbprint(this.SHA1Thumbprint)), ErrorCategory.InvalidArgument, null); } } text = this.MsoSHA1Thumbprint.Trim().Replace(" ", "").ToUpper(); if (text.Length != 40 || Regex.IsMatch(text, "[^0-9A-F]")) { base.WriteError(new ArgumentException(Strings.InvalidThumbprintFormat(this.MsoSHA1Thumbprint)), ErrorCategory.InvalidArgument, null); } if (!this.FindCertificateByThumbprint(text, out this.msox509Certificate2)) { base.WriteError(new ArgumentException(Strings.CantFindCertByThumbprint(this.MsoSHA1Thumbprint)), ErrorCategory.InvalidArgument, null); } } IL_3D7: TaskLogger.LogExit(); }