PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

/ExpressInteropBinding/Microsoft.ServiceModel.Interop.ExtensionUtils/ConfigurationWizard/Service/CertificateStoreService.cs

#
C# | 101 lines | 51 code | 13 blank | 37 comment | 2 complexity | 368b9f4d3d9e42b936219672cfb7c5bf MD5 | raw file
  1// <copyright file="FileSelectionService.cs" company="Microsoft Corporation">
  2// Copyright (c) Microsoft Corporation.  All rights reserved.
  3// </copyright>
  4
  5namespace Microsoft.ServiceModel.Interop.ConfigurationWizard.Services
  6{
  7    using System;
  8    using System.Collections.Generic;
  9    using System.Security.Cryptography.X509Certificates;
 10
 11    /// <summary>
 12    /// Certificate selection service
 13    /// </summary>
 14    public interface ICertificateStoreService
 15    {
 16        /// <summary>
 17        /// Gets a list of available certificate store locations
 18        /// </summary>
 19        /// <returns>A list of available certificate store locations</returns>
 20        IEnumerable<string> GetStoreLocations();
 21
 22        /// <summary>
 23        /// Gets a list of available certificate stores
 24        /// </summary>
 25        /// <returns>A list of available certificate stores</returns>
 26        IEnumerable<string> GetStores();
 27
 28        /// <summary>
 29        /// Gets a list of available certificates in an certificate store
 30        /// </summary>
 31        /// <param name="location">A valid certificate store location</param>
 32        /// <param name="store">A valid certificate store</param>
 33        /// <returns>A list of available certificates in an store</returns>
 34        IEnumerable<string> GetCertificates(string location, string store);
 35    }
 36
 37    /// <summary>
 38    /// Certificate selection service implementation
 39    /// </summary>
 40    public class CertificateStoreService : ICertificateStoreService
 41    {
 42        /// <summary>
 43        /// Gets a list of available certificate store locations
 44        /// </summary>
 45        /// <returns>A list of available certificate store locations</returns>
 46        public IEnumerable<string> GetStoreLocations()
 47        {
 48            return Enum.GetNames(typeof(StoreLocation));
 49        }
 50
 51        /// <summary>
 52        /// Gets a list of available certificate stores
 53        /// </summary>
 54        /// <returns>A list of available certificate stores</returns>
 55        public IEnumerable<string> GetStores()
 56        {
 57            return Enum.GetNames(typeof(StoreName));
 58        }
 59
 60        /// <summary>
 61        /// Gets a list of available certificates in an certificate store
 62        /// </summary>
 63        /// <param name="location">A valid certificate store location</param>
 64        /// <param name="store">A valid certificate store</param>
 65        /// <returns>A list of available certificates in an store</returns>
 66        public IEnumerable<string> GetCertificates(string location, string store)
 67        {
 68            if (string.IsNullOrEmpty(location))
 69            {
 70                throw new ArgumentNullException("location");
 71            }
 72
 73            if (string.IsNullOrEmpty(store))
 74            {
 75                throw new ArgumentNullException("store");
 76            }
 77
 78            var typedLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), location, true);
 79            var typedStore = (StoreName)Enum.Parse(typeof(StoreName), store, true);
 80
 81            var certificateStore = new X509Store(typedStore, typedLocation);
 82            try
 83            {
 84                certificateStore.Open(OpenFlags.ReadOnly);
 85
 86                var certificates = new List<string>();
 87                foreach (var certificate in certificateStore.Certificates)
 88                {
 89                    certificates.Add(certificate.SubjectName.Name);
 90                }
 91
 92                return certificates;
 93            }
 94            finally
 95            {
 96                certificateStore.Close();
 97            }
 98        }
 99    }
100}
101