Commit a140f7f3 a140f7f39322bde37c71590a85fa4a040b5859c5 by Christian Gerdes

Snyggat till koden lite samt skriver ut fel även om debug inte är satt.

1 parent 269f3061
......@@ -568,8 +568,8 @@ namespace LIL_VSTT_Plugins
[DefaultValue("")]
public string pCertificatePasswordParameter { get; set; }
[DisplayName("Log Certificate Data")]
[Description("Sätt till True om certifikatinfo ska loggas i början av varje test")]
[DisplayName("Log Debug Info")]
[Description("Sätt till True om extra info ska loggas i början av varje test")]
[DefaultValue(false)]
public bool pDebug { get; set; }
......@@ -609,7 +609,7 @@ namespace LIL_VSTT_Plugins
certPass = pCertificatePassword;
}
if(string.IsNullOrWhiteSpace(certPass))
if(string.IsNullOrWhiteSpace(certPath))
{
// Cant continue, cert is missing
if (pDebug) e.WebTest.AddCommentToResult("No certificate loaded, since both Certificate Path and Certificate Path Parameter are empty");
......@@ -620,7 +620,7 @@ namespace LIL_VSTT_Plugins
myClientCert = new X509Certificate(certPath, certPass);
} catch (Exception ex)
{
if (pDebug) e.WebTest.AddCommentToResult("Exception during loading of certificate: " + certPath + " Exception: " + ex.Message);
e.WebTest.AddCommentToResult("Error during loading of certificate: " + certPath + " Message: " + ex.Message);
return;
}
......@@ -632,69 +632,73 @@ namespace LIL_VSTT_Plugins
if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath);
}
if (!string.IsNullOrWhiteSpace(myClientCert.GetCertHashString()))
if (string.IsNullOrWhiteSpace(myClientCert.GetCertHashString()))
{
if (pDebug) e.WebTest.AddCommentToResult("Loaded client certificate for Subject: [" + myClientCert.Subject + "] Issued by: [" + myClientCert.Issuer + "] Expires: [" + myClientCert.GetExpirationDateString() + "]");
if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath + " contains no SHA1 hash. Not using it.");
return;
}
// Check if the certificate is trusted (i.e. chain can be validated)
bool myCertTrusted = false;
X509Certificate2 cer = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
cer.FriendlyName = "VSTT";
if (cer.Verify())
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate is TRUSTED");
myCertTrusted = true;
} else
{
if (pDebug) e.WebTest.AddCommentToResult("Waring: Certificate is NOT TRUSTED by client. Might not be trusted on server either. Check that the Issuer/CA root and intermediary certificates are installed on the client and server.");
myCertTrusted = false;
}
if (pDebug) e.WebTest.AddCommentToResult("Loaded client certificate for Subject: [" + myClientCert.Subject + "] Issued by: [" + myClientCert.Issuer + "] Expires: [" + myClientCert.GetExpirationDateString() + "]");
// Check if we have a private key
if (!cer.HasPrivateKey)
{
// Cant use it without private key
if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS NO PRIVATE KEY, cannot use it without one.");
return;
} else
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS PRIVATE KEY");
}
// Check if the certificate is trusted (i.e. chain can be validated)
bool myCertTrusted = false;
X509Certificate2 cer = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
cer.FriendlyName = "VSTT";
if (cer.Verify())
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate is TRUSTED");
myCertTrusted = true;
} else
{
e.WebTest.AddCommentToResult("Warning: Certificate is NOT TRUSTED by client. Might not be trusted on server either. Check that the Issuer/CA root and intermediary certificates are installed on the client and server.");
myCertTrusted = false;
}
// Check that the certificate exists in the cert store
X509Store cuStore = new X509Store();
cuStore.Open(OpenFlags.ReadWrite);
if(cuStore.Certificates.Contains(cer)) {
if (pDebug) e.WebTest.AddCommentToResult("Certificate is INSTALLED");
} else
if(cer.NotAfter < DateTime.Now || cer.NotBefore > DateTime.Now)
{
e.WebTest.AddCommentToResult("Warning: Client Certificate has expired. Might not be trusted on server.");
}
// Check if we have a private key
if (!cer.HasPrivateKey)
{
// Cant use it without private key
e.WebTest.AddCommentToResult("Error: Certificate HAS NO PRIVATE KEY, cannot use it without one.");
return;
} else
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS PRIVATE KEY");
}
// Check that the certificate exists in the cert store
X509Store cuStore = new X509Store();
cuStore.Open(OpenFlags.ReadWrite);
if(cuStore.Certificates.Contains(cer)) {
if (pDebug) e.WebTest.AddCommentToResult("Certificate is INSTALLED");
} else
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate is NOT INSTALLED");
if(pInstallTrusted && myCertTrusted || pInstallUntrusted)
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate is NOT INSTALLED");
if(pInstallTrusted && myCertTrusted || pInstallUntrusted)
// Try to install certificate
if (myCertTrusted || !myCertTrusted)
{
// Try to install certificate
if (myCertTrusted || !myCertTrusted)
// Install in user store
try {
cuStore.Add(cer);
if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS BEEN INSTALLED in the Windows Certificate Store");
} catch (Exception ex)
{
// Install in user store
try {
cuStore.Add(cer);
if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS BEEN INSTALLED in the Windows Certificate Store");
} catch (Exception ex)
{
if (pDebug) e.WebTest.AddCommentToResult("Error: COULD NOT INSTALL in the Windows Certificate Store, Exception: " + ex.Message);
return;
}
e.WebTest.AddCommentToResult("Error: COULD NOT INSTALL in the Windows Certificate Store, Message: " + ex.Message);
return;
}
}
}
// Set the PreRequest method to add the certificate on requests
haveCert = true;
if (pDebug) e.WebTest.AddCommentToResult("Certificate will be ADDED TO REQUESTS");
} else
{
if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath + " contains no SHA1 hash. Not using it.");
return;
}
// Set the PreRequest method to add the certificate on requests
haveCert = true;
if (pDebug) e.WebTest.AddCommentToResult("Certificate will be ADDED TO REQUESTS");
}
public override void PreRequest(object sender, PreRequestEventArgs e)
......