Пример #1
0
        /// <summary>
        /// Lists the thumbprint value for all encryption certificates which exist in the specified store location. Certificates which do not have the "Key Encipherment" key usage flag set are not included in the list.
        /// </summary>
        /// <param name="Context">The X509Context from which to list certificates</param>
        /// <param name="includeExpired">If true, expired certificates will be included in the resulting list</param>
        /// <returns>Line-break-separated list of certificate details</returns>
        public static string ListCerts(X509Context Context, bool includeExpired = false)
        {
            StringBuilder expression = new StringBuilder($"Key Encipherment certificates found in {Context.Name} context:\r\n\r\n");
            bool          firstAdded = false;

            List <X509Alias> Aliases           = Context.GetAliases();
            X509Alias        AssignedAlias     = null;
            string           assignedAliasName = string.Empty;

            X509Store Store = new X509Store(Context.Location);

            Store.Open(OpenFlags.ReadOnly);
            foreach (X509Certificate2 cert in Store.Certificates)
            {
                if (IsUsable(cert, includeExpired))
                {
                    if (!firstAdded)
                    {
                        expression.AppendLine(ListCertFormat.HeaderRow);
                        firstAdded = true;
                    }

                    AssignedAlias     = Aliases.FirstOrDefault(p => p.Thumbprint.Matches(cert.Thumbprint));
                    assignedAliasName = AssignedAlias == null ? Constants.NoAliasAssigned : AssignedAlias.Name;
                    expression.AppendLine($"{cert.Thumbprint.LeftAlign(Padding.Thumbprint)}   {assignedAliasName.LeftAlign(Padding.Assigned_Alias)}   {cert.NotAfter.ToString(Constants.DateFormat)}");
                }
            }

            if (!firstAdded)
            {
                expression.AppendLine(@"None.");
            }

            return(expression.ToString());
        }
Пример #2
0
        internal static string GetOne(string thumbprint, X509Context Context)
        {
            foreach (X509Alias Alias in Context.GetAliases())
            {
                if (Alias.Thumbprint.Matches(thumbprint))
                {
                    return(Alias.Name);
                }
            }

            throw new X509AliasNotFoundException(thumbprint, Context);
        }