From 06f64e9c041bd856627f16ffd4be69db0da6733a Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 24 Oct 2022 13:16:38 +0200 Subject: [PATCH] Interfaces/GraphQL: Improve Logging for Certificates --- Interfaces/GraphQLInterface.vb | 64 +++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/Interfaces/GraphQLInterface.vb b/Interfaces/GraphQLInterface.vb index 9ca6f136..8b2d5ab2 100644 --- a/Interfaces/GraphQLInterface.vb +++ b/Interfaces/GraphQLInterface.vb @@ -31,17 +31,24 @@ Public Class GraphQLInterface _userPassword = Password Dim oStoreNames As New List(Of StoreName) From {StoreName.Root, StoreName.My} + Dim oStoreLocations As New List(Of StoreLocation) From {StoreLocation.CurrentUser, StoreLocation.LocalMachine} + Dim oCertificate As X509Certificate2 = Nothing - For Each oStoreName In oStoreNames - oCertificate = FindCertificateByFingerprint(oStoreName, CertificateFingerprint, False) + For Each oStoreLocation In oStoreLocations + _logger.Debug("Checking Stores in Location [{0}]", oStoreLocation.ToString) + + For Each oStoreName In oStoreNames + oCertificate = FindCertificateByFingerprint(oStoreLocation, oStoreName, CertificateFingerprint, False) - If oCertificate IsNot Nothing Then - _logger.Info("Certificate found in Store [{0}]!", oStoreName.ToString) - Exit For - End If + If oCertificate IsNot Nothing Then + _logger.Info("Certificate found in Store [{0}]/[{1}]!", oStoreName.ToString, oStoreLocation.ToString) + Exit For + End If + Next Next + If oCertificate Is Nothing Then _logger.Warn("Certificate could not be found! Exiting.") Exit Sub @@ -54,31 +61,40 @@ Public Class GraphQLInterface End Try End Sub - Private Function FindCertificateByFingerprint(pStoreName As StoreName, pFingerprint As String, pValidOnly As Boolean) As X509Certificate2 - Dim oStore As New X509Store(pStoreName, StoreLocation.CurrentUser) - oStore.Open(OpenFlags.ReadOnly) - - _logger.Info("Available Certificates in Store [{0}]: [{1}]", oStore.Name, oStore.Certificates.Count) + Private Function FindCertificateByFingerprint(pLocation As StoreLocation, pStoreName As StoreName, pFingerprint As String, pValidOnly As Boolean) As X509Certificate2 + Try + Dim oStore As New X509Store(pStoreName, pLocation) + Dim oLocation As String = pLocation.ToString - For Each oCert In oStore.Certificates - _logger.Debug("FriendlyName: {0}", oCert.FriendlyName) - _logger.Debug("IssuerName: {0}", oCert.IssuerName.Name) - _logger.Debug("SubjectName: {0}", oCert.SubjectName.Name) - _logger.Debug("Fingerprint: {0}", oCert.Thumbprint) - Next + _logger.Info("Opening Store [{0}]/[{1}]..", oLocation, oStore.Name) + oStore.Open(OpenFlags.ReadOnly) - _logger.Debug("Looking for Certificate with Fingerprint [{0}]", pFingerprint) + _logger.Info("Available Certificates in Store [{0}]/[{1}]: [{2}]", oLocation, oStore.Name, oStore.Certificates.Count) - Dim oFoundCerts = oStore.Certificates.Find(X509FindType.FindByThumbprint, pFingerprint, pValidOnly) + For Each oCert In oStore.Certificates + _logger.Debug("FriendlyName: {0}", oCert.FriendlyName) + _logger.Debug("IssuerName: {0}", oCert.IssuerName.Name) + _logger.Debug("SubjectName: {0}", oCert.SubjectName.Name) + _logger.Debug("Fingerprint: {0}", oCert.Thumbprint) + Next - oStore.Close() + _logger.Debug("Looking for Certificate with Fingerprint [{0}]", pFingerprint) + Dim oFoundCerts = oStore.Certificates.Find(X509FindType.FindByThumbprint, pFingerprint, pValidOnly) - If oFoundCerts.Count = 0 Then - Return Nothing - End If + _logger.Debug("Closing store..") + oStore.Close() - Return oFoundCerts.Item(0) + If oFoundCerts.Count = 0 Then + _logger.Debug("Certificate with Fingerprint [{0}] not found in Store [{1}]/[{2}]", pFingerprint, oLocation, oStore.Name) + Return Nothing + End If + Return oFoundCerts.Item(0) + Catch ex As Exception + _logger.Warn("Unexpected error while searching for certificate with Fingerprint [{0}].", pFingerprint) + _logger.Error(ex) + Return Nothing + End Try End Function Public Sub SaveCookies(Cookie As Cookie)