PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

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