Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GDS: fix Application unregistration in SQL DB for Winforms Server & fix private key storage in Pull Model GDS Client #598

Merged
60 changes: 32 additions & 28 deletions Samples/GDS/Client/Controls/ApplicationCertificateControl.cs
Expand Up @@ -93,8 +93,7 @@ public ApplicationCertificateControl()
}
else if (!String.IsNullOrEmpty(application.CertificateStorePath))
{
CertificateIdentifier id = new CertificateIdentifier
{
CertificateIdentifier id = new CertificateIdentifier {
StorePath = application.CertificateStorePath
};
id.StoreType = CertificateStoreIdentifier.DetermineStoreType(id.StorePath);
Expand Down Expand Up @@ -125,8 +124,7 @@ public ApplicationCertificateControl()
{
Uri url = new Uri(disoveryUrl);

CertificateIdentifier id = new CertificateIdentifier()
{
CertificateIdentifier id = new CertificateIdentifier() {
StoreType = CertificateStoreType.X509Store,
StorePath = "CurrentUser\\UA_MachineDefault",
SubjectName = "CN=" + url.DnsSafeHost
Expand Down Expand Up @@ -216,8 +214,7 @@ private async Task RequestNewCertificatePullMode(object sender, EventArgs e)
NodeId requestId = null;
if (!string.IsNullOrEmpty(m_application.CertificateStorePath))
{
CertificateIdentifier id = new CertificateIdentifier
{
CertificateIdentifier id = new CertificateIdentifier {
StoreType = CertificateStoreIdentifier.DetermineStoreType(m_application.CertificateStorePath),
StorePath = m_application.CertificateStorePath,
SubjectName = m_application.CertificateSubjectName.Replace("localhost", Utils.GetHostName())
Expand Down Expand Up @@ -316,41 +313,48 @@ private async void CertificateRequestTimer_Tick(object sender, EventArgs e)

if (!String.IsNullOrEmpty(m_application.CertificateStorePath) && !String.IsNullOrEmpty(m_application.CertificateSubjectName))
{
CertificateIdentifier cid = new CertificateIdentifier()
{
CertificateIdentifier cid = new CertificateIdentifier() {
StorePath = m_application.CertificateStorePath,
StoreType = CertificateStoreIdentifier.DetermineStoreType(m_application.CertificateStorePath),
SubjectName = m_application.CertificateSubjectName.Replace("localhost", Utils.GetHostName())
romanett marked this conversation as resolved.
Show resolved Hide resolved
};

// update store
using (var store = CertificateStoreIdentifier.OpenStore(m_application.CertificateStorePath))
ICertificateStore store;

romanett marked this conversation as resolved.
Show resolved Hide resolved
if (CertificateStoreIdentifier.DetermineStoreType(m_application.CertificateStorePath) == CertificateStoreType.Directory)
{
store = new DirectoryCertificateStore();
store.Open(m_application.CertificateStorePath, false);
}
else
{
store = CertificateStoreIdentifier.OpenStore(m_application.CertificateStorePath);
}

// if we used a CSR, we already have a private key and therefore didn't request one from the GDS
// in this case, privateKey is null
if (privateKeyPFX == null)
{
// if we used a CSR, we already have a private key and therefore didn't request one from the GDS
// in this case, privateKey is null
if (privateKeyPFX == null)
X509Certificate2 oldCertificate = await cid.Find(true);
if (oldCertificate != null && oldCertificate.HasPrivateKey)
{
X509Certificate2 oldCertificate = await cid.Find(true);
if (oldCertificate != null && oldCertificate.HasPrivateKey)
{
oldCertificate = await cid.LoadPrivateKey(string.Empty);
newCert = CertificateFactory.CreateCertificateWithPrivateKey(newCert, oldCertificate);
await store.Delete(oldCertificate.Thumbprint);
}
else
{
throw new ServiceResultException("Failed to merge signed certificate with the private key.");
}
oldCertificate = await cid.LoadPrivateKey(string.Empty);
newCert = CertificateFactory.CreateCertificateWithPrivateKey(newCert, oldCertificate);
await store.Delete(oldCertificate.Thumbprint);
}
else
{
newCert = new X509Certificate2(privateKeyPFX, string.Empty, X509KeyStorageFlags.Exportable);
newCert = CertificateFactory.Load(newCert, true);
throw new ServiceResultException("Failed to merge signed certificate with the private key.");
}

// bugbug: private key is not saved to store
await store.Add(newCert);
}
else
{
newCert = new X509Certificate2(privateKeyPFX, string.Empty, X509KeyStorageFlags.Exportable);
newCert = CertificateFactory.Load(newCert, true);
}
await store.Add(newCert);
store.Dispose();
}
else
{
Expand Down
5 changes: 5 additions & 0 deletions Samples/GDS/Server/SqlApplicationsDatabase.cs
Expand Up @@ -169,6 +169,11 @@ public override void UnregisterApplication(NodeId applicationId)
entities.CertificateRequests.Remove(entry);
}

foreach (var entry in new List<CertificateStore>(result.CertificateStores))
{
entities.CertificateStores.Remove(entry);
}

foreach (var entry in new List<ApplicationName>(result.ApplicationNames))
{
entities.ApplicationNames.Remove(entry);
Expand Down