From 3ece4e62221366f0147f1c04b63b9e4bc4bb48e5 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 13 May 2020 11:42:28 +0200 Subject: [PATCH] GraphQL: Use cert from store --- GUIs.Test.GraphQLTest/Config.vb | 1 + GUIs.Test.GraphQLTest/frmMain.Designer.vb | 86 ++++++++++++++--------- GUIs.Test.GraphQLTest/frmMain.vb | 9 ++- Modules.Interfaces/GraphQLInterface.vb | 19 +++-- 4 files changed, 77 insertions(+), 38 deletions(-) diff --git a/GUIs.Test.GraphQLTest/Config.vb b/GUIs.Test.GraphQLTest/Config.vb index 4df9bd9e..d135c2d4 100644 --- a/GUIs.Test.GraphQLTest/Config.vb +++ b/GUIs.Test.GraphQLTest/Config.vb @@ -2,6 +2,7 @@ Public Property ConnectionString As String = "Server=SERVER;Database=DATABASE;User Id=sa;Password=dd" Public Property CertificateFile As String = "C:\Path\To\Cert.pfx" Public Property CertificatePass As String = "CertificatePassword" + Public Property CertificateFingerprint As String = "" Public Property Email As String = "foo.bar@wisag.de" Public Property Password As String = "Password" Public Property BaseUrl As String = "https://data.api.wisag.de:8443" diff --git a/GUIs.Test.GraphQLTest/frmMain.Designer.vb b/GUIs.Test.GraphQLTest/frmMain.Designer.vb index 0982fcbd..f164f995 100644 --- a/GUIs.Test.GraphQLTest/frmMain.Designer.vb +++ b/GUIs.Test.GraphQLTest/frmMain.Designer.vb @@ -40,6 +40,9 @@ Partial Class frmMain Me.Label7 = New System.Windows.Forms.Label() Me.TabControl1 = New System.Windows.Forms.TabControl() Me.pageRaw = New System.Windows.Forms.TabPage() + Me.pageQuery = New System.Windows.Forms.TabPage() + Me.txtQuery = New System.Windows.Forms.TextBox() + Me.txtOperation = New System.Windows.Forms.TextBox() Me.btnLogin = New System.Windows.Forms.Button() Me.ProgressBar1 = New System.Windows.Forms.ProgressBar() Me.txtProxyHost = New System.Windows.Forms.TextBox() @@ -50,9 +53,8 @@ Partial Class frmMain Me.Label10 = New System.Windows.Forms.Label() Me.txtProxyPort = New System.Windows.Forms.TextBox() Me.Label11 = New System.Windows.Forms.Label() - Me.pageQuery = New System.Windows.Forms.TabPage() - Me.txtQuery = New System.Windows.Forms.TextBox() - Me.txtOperation = New System.Windows.Forms.TextBox() + Me.txtCertFingerprint = New System.Windows.Forms.TextBox() + Me.Label12 = New System.Windows.Forms.Label() Me.TabControl1.SuspendLayout() Me.pageRaw.SuspendLayout() Me.pageQuery.SuspendLayout() @@ -165,16 +167,16 @@ Partial Class frmMain ' 'txtConnectionString ' - Me.txtConnectionString.Location = New System.Drawing.Point(106, 136) + Me.txtConnectionString.Location = New System.Drawing.Point(106, 167) Me.txtConnectionString.Multiline = True Me.txtConnectionString.Name = "txtConnectionString" - Me.txtConnectionString.Size = New System.Drawing.Size(338, 98) + Me.txtConnectionString.Size = New System.Drawing.Size(338, 67) Me.txtConnectionString.TabIndex = 1 ' 'Label6 ' Me.Label6.AutoSize = True - Me.Label6.Location = New System.Drawing.Point(12, 139) + Me.Label6.Location = New System.Drawing.Point(12, 167) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(88, 13) Me.Label6.TabIndex = 2 @@ -219,6 +221,36 @@ Partial Class frmMain Me.pageRaw.Text = "Raw GraphQL Result" Me.pageRaw.UseVisualStyleBackColor = True ' + 'pageQuery + ' + Me.pageQuery.Controls.Add(Me.txtQuery) + Me.pageQuery.Controls.Add(Me.txtOperation) + Me.pageQuery.Location = New System.Drawing.Point(4, 22) + Me.pageQuery.Name = "pageQuery" + Me.pageQuery.Padding = New System.Windows.Forms.Padding(3) + Me.pageQuery.Size = New System.Drawing.Size(464, 348) + Me.pageQuery.TabIndex = 1 + Me.pageQuery.Text = "Custom Query" + Me.pageQuery.UseVisualStyleBackColor = True + ' + 'txtQuery + ' + Me.txtQuery.Dock = System.Windows.Forms.DockStyle.Fill + Me.txtQuery.Font = New System.Drawing.Font("Consolas", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) + Me.txtQuery.Location = New System.Drawing.Point(3, 23) + Me.txtQuery.Multiline = True + Me.txtQuery.Name = "txtQuery" + Me.txtQuery.Size = New System.Drawing.Size(458, 322) + Me.txtQuery.TabIndex = 0 + ' + 'txtOperation + ' + Me.txtOperation.Dock = System.Windows.Forms.DockStyle.Top + Me.txtOperation.Location = New System.Drawing.Point(3, 3) + Me.txtOperation.Name = "txtOperation" + Me.txtOperation.Size = New System.Drawing.Size(458, 20) + Me.txtOperation.TabIndex = 1 + ' 'btnLogin ' Me.btnLogin.Location = New System.Drawing.Point(724, 386) @@ -299,35 +331,21 @@ Partial Class frmMain Me.Label11.TabIndex = 2 Me.Label11.Text = "Proxy Port" ' - 'pageQuery + 'txtCertFingerprint ' - Me.pageQuery.Controls.Add(Me.txtQuery) - Me.pageQuery.Controls.Add(Me.txtOperation) - Me.pageQuery.Location = New System.Drawing.Point(4, 22) - Me.pageQuery.Name = "pageQuery" - Me.pageQuery.Padding = New System.Windows.Forms.Padding(3) - Me.pageQuery.Size = New System.Drawing.Size(464, 348) - Me.pageQuery.TabIndex = 1 - Me.pageQuery.Text = "Custom Query" - Me.pageQuery.UseVisualStyleBackColor = True + Me.txtCertFingerprint.Location = New System.Drawing.Point(106, 136) + Me.txtCertFingerprint.Name = "txtCertFingerprint" + Me.txtCertFingerprint.Size = New System.Drawing.Size(338, 20) + Me.txtCertFingerprint.TabIndex = 1 ' - 'txtQuery + 'Label12 ' - Me.txtQuery.Dock = System.Windows.Forms.DockStyle.Fill - Me.txtQuery.Font = New System.Drawing.Font("Consolas", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) - Me.txtQuery.Location = New System.Drawing.Point(3, 23) - Me.txtQuery.Multiline = True - Me.txtQuery.Name = "txtQuery" - Me.txtQuery.Size = New System.Drawing.Size(458, 322) - Me.txtQuery.TabIndex = 0 - ' - 'txtOperation - ' - Me.txtOperation.Dock = System.Windows.Forms.DockStyle.Top - Me.txtOperation.Location = New System.Drawing.Point(3, 3) - Me.txtOperation.Name = "txtOperation" - Me.txtOperation.Size = New System.Drawing.Size(458, 20) - Me.txtOperation.TabIndex = 1 + Me.Label12.AutoSize = True + Me.Label12.Location = New System.Drawing.Point(12, 139) + Me.Label12.Name = "Label12" + Me.Label12.Size = New System.Drawing.Size(78, 13) + Me.Label12.TabIndex = 2 + Me.Label12.Text = "Cert Fingerprint" ' 'frmMain ' @@ -339,6 +357,7 @@ Partial Class frmMain Me.Controls.Add(Me.cmbQuery) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.Label6) + Me.Controls.Add(Me.Label12) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label10) @@ -349,6 +368,7 @@ Partial Class frmMain Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.txtConnectionString) + Me.Controls.Add(Me.txtCertFingerprint) Me.Controls.Add(Me.txtCertPass) Me.Controls.Add(Me.txtCertFile) Me.Controls.Add(Me.txtProxyPass) @@ -403,4 +423,6 @@ Partial Class frmMain Friend WithEvents pageQuery As TabPage Friend WithEvents txtQuery As TextBox Friend WithEvents txtOperation As TextBox + Friend WithEvents txtCertFingerprint As TextBox + Friend WithEvents Label12 As Label End Class diff --git a/GUIs.Test.GraphQLTest/frmMain.vb b/GUIs.Test.GraphQLTest/frmMain.vb index 88d597a7..de7d1b04 100644 --- a/GUIs.Test.GraphQLTest/frmMain.vb +++ b/GUIs.Test.GraphQLTest/frmMain.vb @@ -56,8 +56,11 @@ Public Class frmMain txtBaseUrl.Text = _Config.Config.BaseUrl txtUsername.Text = _Config.Config.Email txtPassword.Text = _Config.Config.Password + txtCertFile.Text = _Config.Config.CertificateFile txtCertPass.Text = _Config.Config.CertificatePass + txtCertFingerprint.Text = _Config.Config.CertificateFingerprint + txtConnectionString.Text = _Config.Config.ConnectionString txtProxyHost.Text = _Config.Config.ProxyHost txtProxyPort.Text = _Config.Config.ProxyPort @@ -81,8 +84,7 @@ Public Class frmMain txtBaseUrl.Text, txtUsername.Text, txtPassword.Text, - txtCertFile.Text, - txtCertPass.Text) + txtCertFingerprint.Text) If _Config.Config.HasProxySet() And _Config.Config.HasProxyCredentialsSet() Then Dim oURI As New Uri($"http://{_Config.Config.ProxyHost}:{_Config.Config.ProxyPort}") @@ -210,8 +212,11 @@ Public Class frmMain Try _Config.Config.ConnectionString = txtConnectionString.Text _Config.Config.BaseUrl = txtBaseUrl.Text + _Config.Config.CertificateFile = txtCertFile.Text _Config.Config.CertificatePass = txtCertPass.Text + _Config.Config.CertificateFingerprint = txtCertFingerprint.Text + _Config.Config.Email = txtUsername.Text _Config.Config.Password = txtPassword.Text _Config.Config.ProxyHost = txtProxyHost.Text diff --git a/Modules.Interfaces/GraphQLInterface.vb b/Modules.Interfaces/GraphQLInterface.vb index 81d6faa1..9956288d 100644 --- a/Modules.Interfaces/GraphQLInterface.vb +++ b/Modules.Interfaces/GraphQLInterface.vb @@ -11,14 +11,14 @@ Public Class GraphQLInterface Private _baseUrl As String Private _userEmail As String Private _userPassword As String - Private _certificate As X509Certificate + Private _certificate As X509Certificate2 Private _cookieJar As CookieContainer Private _Encoding As New UTF8Encoding Public Property Proxy As WebProxy Public Property Credentials As NetworkCredential - Public Sub New(LogConfig As LogConfig, BaseUrl As String, Email As String, Password As String, CertificateFile As String, CertificatePassword As String) + Public Sub New(LogConfig As LogConfig, BaseUrl As String, Email As String, Password As String, CertificateFingerprint As String) Try _logConfig = LogConfig _logger = LogConfig.GetLogger() @@ -26,18 +26,29 @@ Public Class GraphQLInterface _userEmail = Email _userPassword = Password - _certificate = New X509Certificate2(CertificateFile, CertificatePassword, X509KeyStorageFlags.UserKeySet) - Dim oStore As New X509Store(StoreName.My, StoreLocation.CurrentUser) oStore.Open(OpenFlags.ReadOnly) + _logger.Debug("Available Certificates ({0}):", oStore.Certificates.Count) 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.Debug("Looking for Certificate with Fingerprint [{0}]", CertificateFingerprint) + + Dim oFoundCerts = oStore.Certificates.Find(X509FindType.FindByThumbprint, CertificateFingerprint, False) + + If oFoundCerts.Count = 0 Then + MsgBox($"Das Zertifikat mit dem Fingerprint [{CertificateFingerprint}] konnte nicht im Store [{oStore.Name}] gefunden werden!") + Exit Sub + End If + + _certificate = oFoundCerts.Item(0) Catch ex As Exception _logger.Error(ex) End Try