Repository: PowerShell/PowerShell
File: src/System.Management.Automation/security/SecuritySupport.cs (C#)

Code before:
        {
            error = null;
            
            try
            {
                X509Certificate2Collection certificatesToProcess = new X509Certificate2Collection();

                using (X509Store storeCU = new X509Store("my", StoreLocation.CurrentUser))
                {
                    storeCU.Open(OpenFlags.ReadOnly);
                    X509Certificate2Collection storeCerts = storeCU.Certificates;

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        using (X509Store storeLM = new X509Store("my", StoreLocation.LocalMachine))
                        {
                            storeLM.Open(OpenFlags.ReadOnly);
                            storeCerts.AddRange(storeLM.Certificates);
                        }
                    }

                    certificatesToProcess.AddRange(storeCerts.Find(X509FindType.FindByThumbprint, _identifier, false));

                    if (certificatesToProcess.Count == 0)
                    {   // FindBySubjectName is case insensitive and acts like "contains"
                        String subjectName = _identifier.Trim().ToUpperInvariant().TrimStart('C', 'N', '=');
                        certificatesToProcess.AddRange(storeCerts.Find(X509FindType.FindBySubjectName, subjectName, false));
                    }

                    ProcessResolvedCertificates(purpose, certificatesToProcess, out error);
                }

            }
            catch (SessionStateException)
            {
            }
        }

        private void ProcessResolvedCertificates(ResolutionPurpose purpose, X509Certificate2Collection certificatesToProcess, out ErrorRecord error)
        {
            error = null;
            HashSet<string> processedThumbprints = new HashSet<string>();

            foreach (X509Certificate2 certificate in certificatesToProcess)
            {
                if (!SecuritySupport.CertIsGoodForEncryption(certificate))
                {
                    // If they specified a specific cert, generate an error if it isn't good
                    // for encryption.
                    if (!WildcardPattern.ContainsWildcardCharacters(_identifier))
                    {

Review comment (suggestion):
```suggestion
                        string subjectName = _identifier.Trim().ToUpperInvariant().TrimStart('C', 'N', '=');
```

Code after:
        private void ResolveFromStoreById(ResolutionPurpose purpose, out ErrorRecord error)
        {
            error = null;
            WildcardPattern subjectNamePattern = WildcardPattern.Get(_identifier, WildcardOptions.IgnoreCase);

            try
            {
                var certificatesToProcess = new X509Certificate2Collection();

                using (var storeCU = new X509Store("my", StoreLocation.CurrentUser))
                {
                    storeCU.Open(OpenFlags.ReadOnly);
                    X509Certificate2Collection storeCerts = storeCU.Certificates;

                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        using (var storeLM = new X509Store("my", StoreLocation.LocalMachine))
                        {
                            storeLM.Open(OpenFlags.ReadOnly);
                            storeCerts.AddRange(storeLM.Certificates);
                        }
                    }

                    certificatesToProcess.AddRange(storeCerts.Find(X509FindType.FindByThumbprint, _identifier, validOnly: false));

                    if (certificatesToProcess.Count == 0)
                    {
                        foreach (var cert in storeCerts)
                        {
                            if (subjectNamePattern.IsMatch(cert.Subject) || subjectNamePattern.IsMatch(cert.GetNameInfo(X509NameType.SimpleName, forIssuer: false)))
                            {
                                certificatesToProcess.Add(cert);
                            }
                        }
                    }

                    ProcessResolvedCertificates(purpose, certificatesToProcess, out error);
                }
            }
            catch (SessionStateException)
            {
            }
        }

        private void ProcessResolvedCertificates(ResolutionPurpose purpose, X509Certificate2Collection certificatesToProcess, out ErrorRecord error)
        {
            error = null;
            HashSet<string> processedThumbprints = new HashSet<string>();

            foreach (X509Certificate2 certificate in certificatesToProcess)
            {