Snyggat till koden lite samt skriver ut fel även om debug inte är satt.
Showing
1 changed file
with
60 additions
and
56 deletions
... | @@ -568,8 +568,8 @@ namespace LIL_VSTT_Plugins | ... | @@ -568,8 +568,8 @@ namespace LIL_VSTT_Plugins |
568 | [DefaultValue("")] | 568 | [DefaultValue("")] |
569 | public string pCertificatePasswordParameter { get; set; } | 569 | public string pCertificatePasswordParameter { get; set; } |
570 | 570 | ||
571 | [DisplayName("Log Certificate Data")] | 571 | [DisplayName("Log Debug Info")] |
572 | [Description("Sätt till True om certifikatinfo ska loggas i början av varje test")] | 572 | [Description("Sätt till True om extra info ska loggas i början av varje test")] |
573 | [DefaultValue(false)] | 573 | [DefaultValue(false)] |
574 | public bool pDebug { get; set; } | 574 | public bool pDebug { get; set; } |
575 | 575 | ||
... | @@ -609,7 +609,7 @@ namespace LIL_VSTT_Plugins | ... | @@ -609,7 +609,7 @@ namespace LIL_VSTT_Plugins |
609 | certPass = pCertificatePassword; | 609 | certPass = pCertificatePassword; |
610 | } | 610 | } |
611 | 611 | ||
612 | if(string.IsNullOrWhiteSpace(certPass)) | 612 | if(string.IsNullOrWhiteSpace(certPath)) |
613 | { | 613 | { |
614 | // Cant continue, cert is missing | 614 | // Cant continue, cert is missing |
615 | if (pDebug) e.WebTest.AddCommentToResult("No certificate loaded, since both Certificate Path and Certificate Path Parameter are empty"); | 615 | 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 | ... | @@ -620,7 +620,7 @@ namespace LIL_VSTT_Plugins |
620 | myClientCert = new X509Certificate(certPath, certPass); | 620 | myClientCert = new X509Certificate(certPath, certPass); |
621 | } catch (Exception ex) | 621 | } catch (Exception ex) |
622 | { | 622 | { |
623 | if (pDebug) e.WebTest.AddCommentToResult("Exception during loading of certificate: " + certPath + " Exception: " + ex.Message); | 623 | e.WebTest.AddCommentToResult("Error during loading of certificate: " + certPath + " Message: " + ex.Message); |
624 | return; | 624 | return; |
625 | } | 625 | } |
626 | 626 | ||
... | @@ -632,69 +632,73 @@ namespace LIL_VSTT_Plugins | ... | @@ -632,69 +632,73 @@ namespace LIL_VSTT_Plugins |
632 | if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath); | 632 | if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath); |
633 | } | 633 | } |
634 | 634 | ||
635 | if (!string.IsNullOrWhiteSpace(myClientCert.GetCertHashString())) | 635 | if (string.IsNullOrWhiteSpace(myClientCert.GetCertHashString())) |
636 | { | 636 | { |
637 | if (pDebug) e.WebTest.AddCommentToResult("Loaded client certificate for Subject: [" + myClientCert.Subject + "] Issued by: [" + myClientCert.Issuer + "] Expires: [" + myClientCert.GetExpirationDateString() + "]"); | 637 | if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath + " contains no SHA1 hash. Not using it."); |
638 | return; | ||
639 | } | ||
638 | 640 | ||
639 | // Check if the certificate is trusted (i.e. chain can be validated) | 641 | if (pDebug) e.WebTest.AddCommentToResult("Loaded client certificate for Subject: [" + myClientCert.Subject + "] Issued by: [" + myClientCert.Issuer + "] Expires: [" + myClientCert.GetExpirationDateString() + "]"); |
640 | bool myCertTrusted = false; | ||
641 | X509Certificate2 cer = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.PersistKeySet); | ||
642 | cer.FriendlyName = "VSTT"; | ||
643 | if (cer.Verify()) | ||
644 | { | ||
645 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is TRUSTED"); | ||
646 | myCertTrusted = true; | ||
647 | } else | ||
648 | { | ||
649 | 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."); | ||
650 | myCertTrusted = false; | ||
651 | } | ||
652 | 642 | ||
653 | // Check if we have a private key | 643 | // Check if the certificate is trusted (i.e. chain can be validated) |
654 | if (!cer.HasPrivateKey) | 644 | bool myCertTrusted = false; |
655 | { | 645 | X509Certificate2 cer = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.PersistKeySet); |
656 | // Cant use it without private key | 646 | cer.FriendlyName = "VSTT"; |
657 | if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS NO PRIVATE KEY, cannot use it without one."); | 647 | if (cer.Verify()) |
658 | return; | 648 | { |
659 | } else | 649 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is TRUSTED"); |
660 | { | 650 | myCertTrusted = true; |
661 | if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS PRIVATE KEY"); | 651 | } else |
662 | } | 652 | { |
653 | 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."); | ||
654 | myCertTrusted = false; | ||
655 | } | ||
663 | 656 | ||
664 | // Check that the certificate exists in the cert store | 657 | if(cer.NotAfter < DateTime.Now || cer.NotBefore > DateTime.Now) |
665 | X509Store cuStore = new X509Store(); | 658 | { |
666 | cuStore.Open(OpenFlags.ReadWrite); | 659 | e.WebTest.AddCommentToResult("Warning: Client Certificate has expired. Might not be trusted on server."); |
667 | if(cuStore.Certificates.Contains(cer)) { | 660 | } |
668 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is INSTALLED"); | 661 | |
669 | } else | 662 | // Check if we have a private key |
663 | if (!cer.HasPrivateKey) | ||
664 | { | ||
665 | // Cant use it without private key | ||
666 | e.WebTest.AddCommentToResult("Error: Certificate HAS NO PRIVATE KEY, cannot use it without one."); | ||
667 | return; | ||
668 | } else | ||
669 | { | ||
670 | if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS PRIVATE KEY"); | ||
671 | } | ||
672 | |||
673 | // Check that the certificate exists in the cert store | ||
674 | X509Store cuStore = new X509Store(); | ||
675 | cuStore.Open(OpenFlags.ReadWrite); | ||
676 | if(cuStore.Certificates.Contains(cer)) { | ||
677 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is INSTALLED"); | ||
678 | } else | ||
679 | { | ||
680 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is NOT INSTALLED"); | ||
681 | if(pInstallTrusted && myCertTrusted || pInstallUntrusted) | ||
670 | { | 682 | { |
671 | if (pDebug) e.WebTest.AddCommentToResult("Certificate is NOT INSTALLED"); | 683 | // Try to install certificate |
672 | if(pInstallTrusted && myCertTrusted || pInstallUntrusted) | 684 | if (myCertTrusted || !myCertTrusted) |
673 | { | 685 | { |
674 | // Try to install certificate | 686 | // Install in user store |
675 | if (myCertTrusted || !myCertTrusted) | 687 | try { |
688 | cuStore.Add(cer); | ||
689 | if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS BEEN INSTALLED in the Windows Certificate Store"); | ||
690 | } catch (Exception ex) | ||
676 | { | 691 | { |
677 | // Install in user store | 692 | e.WebTest.AddCommentToResult("Error: COULD NOT INSTALL in the Windows Certificate Store, Message: " + ex.Message); |
678 | try { | 693 | return; |
679 | cuStore.Add(cer); | ||
680 | if (pDebug) e.WebTest.AddCommentToResult("Certificate HAS BEEN INSTALLED in the Windows Certificate Store"); | ||
681 | } catch (Exception ex) | ||
682 | { | ||
683 | if (pDebug) e.WebTest.AddCommentToResult("Error: COULD NOT INSTALL in the Windows Certificate Store, Exception: " + ex.Message); | ||
684 | return; | ||
685 | } | ||
686 | } | 694 | } |
687 | } | 695 | } |
688 | } | 696 | } |
689 | |||
690 | // Set the PreRequest method to add the certificate on requests | ||
691 | haveCert = true; | ||
692 | if (pDebug) e.WebTest.AddCommentToResult("Certificate will be ADDED TO REQUESTS"); | ||
693 | } else | ||
694 | { | ||
695 | if (pDebug) e.WebTest.AddCommentToResult("Certificate File " + certPath + " contains no SHA1 hash. Not using it."); | ||
696 | return; | ||
697 | } | 697 | } |
698 | |||
699 | // Set the PreRequest method to add the certificate on requests | ||
700 | haveCert = true; | ||
701 | if (pDebug) e.WebTest.AddCommentToResult("Certificate will be ADDED TO REQUESTS"); | ||
698 | } | 702 | } |
699 | 703 | ||
700 | public override void PreRequest(object sender, PreRequestEventArgs e) | 704 | public override void PreRequest(object sender, PreRequestEventArgs e) | ... | ... |
-
Please register or sign in to post a comment