private X509Certificate2Collection LoadClientCertificates()

in src/NMS.AMQP/Transport/SecureTransportContext.cs [174:226]


        private X509Certificate2Collection LoadClientCertificates()
        {
            X509Certificate2Collection certificates = new X509Certificate2Collection();

            if(!String.IsNullOrWhiteSpace(this.ClientCertFileName))
            {
                Tracer.DebugFormat("Attempting to load Client Certificate file: {0}", this.ClientCertFileName);
                X509Certificate2 certificate = new X509Certificate2(this.ClientCertFileName, this.ClientCertPassword);
                Tracer.DebugFormat("Loaded Client Certificate: {0}", certificate.Subject);

                certificates.Add(certificate);
            }
            else
            {
                string storeName = String.IsNullOrWhiteSpace(this.KeyStoreName) ? StoreName.My.ToString() : this.KeyStoreName;
                StoreLocation storeLocation = StoreLocation.CurrentUser;
                if(!String.IsNullOrWhiteSpace(this.KeyStoreLocation))
                {
                    bool found = false;
                    foreach(string location in Enum.GetNames(typeof(StoreLocation)))
                    {
                        if(String.Compare(this.KeyStoreLocation, location, true) == 0)
                        {
                            storeLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), location, true);
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                    {
                        throw new NMSException(string.Format("Invalid Store location {0}", this.KeyStoreLocation), NMSErrorCode.PROPERTY_ERROR);
                    }
                }

                Tracer.DebugFormat("Loading store {0}, from location {1}.", storeName, storeLocation.ToString());
                try
                {
                    X509Store store = new X509Store(storeName, storeLocation);

                    store.Open(OpenFlags.ReadOnly);
                    X509Certificate2[] storeCertificates = new X509Certificate2[store.Certificates.Count];
                    store.Certificates.CopyTo(storeCertificates, 0);
                    certificates.AddRange(storeCertificates);
                }
                catch(Exception ex)
                {
                    Tracer.WarnFormat("Error loading KeyStore, name : {0}; location : {1}. Cause {2}", storeName, storeLocation, ex);
                    throw ExceptionSupport.Wrap(ex, "Error loading KeyStore.", storeName, storeLocation.ToString());
                }
            }

            return certificates;
        }