Skip to content

Commit

Permalink
Fix test loading cert by name when multiple certs with same name exist (
Browse files Browse the repository at this point in the history
#4058)

* Fix cert load by name in tests

* Apply suggestions from code review

Co-authored-by: Gladwin Johnson <90415114+gladjohn@users.noreply.github.com>

---------

Co-authored-by: Gladwin Johnson <90415114+gladjohn@users.noreply.github.com>
  • Loading branch information
bgavrilMS and gladjohn committed Apr 3, 2023
1 parent 156beb8 commit 50c4d51
Showing 1 changed file with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ public static X509Certificate2 FindCertificateByName(string certName, StoreLocat
using (var store = new X509Store(name, location))
{
store.Open(OpenFlags.ReadOnly);
var collection = store.Certificates.Find(X509FindType.FindBySubjectName, certName, validateCerts);
X509Certificate2Collection collection = store.Certificates.Find(X509FindType.FindBySubjectName, certName, validateCerts);

return collection.Count == 0
? null
: collection[0];
X509Certificate2 certToUse = null;

// select the "freshest" certificate
foreach (X509Certificate2 cert in collection)
{
if (certToUse == null || cert.NotBefore > certToUse.NotBefore)
{
certToUse = cert;
}
}

return certToUse;

}
}
Expand Down

0 comments on commit 50c4d51

Please sign in to comment.