CertificateGenerator.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using MediaBrowser.Model.Logging;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace MediaBrowser.ServerApplication.Networking
  7. {
  8. // Copied from: http://blogs.msdn.com/b/dcook/archive/2014/05/16/9143036.aspx
  9. // In case anybody is interested, source code is attached and is free for use by anybody as long as you don't hold me or Microsoft liable for it --
  10. // I have no idea whether this is actually the right or best way to do this. Give it the X500 distinguished name, validity start and end dates,
  11. // and an optional password for encrypting the key data, and it will give you the PFX file data. Let me know if you find any bugs or have any suggestions.
  12. internal class CertificateGenerator
  13. {
  14. internal static void CreateSelfSignCertificatePfx(
  15. string fileName,
  16. string hostname,
  17. ILogger logger)
  18. {
  19. if (string.IsNullOrWhiteSpace(fileName))
  20. {
  21. throw new ArgumentNullException("fileName");
  22. }
  23. string x500 = string.Format("CN={0}", hostname);
  24. DateTime startTime = DateTime.Now.AddDays(-2);
  25. DateTime endTime = DateTime.Now.AddYears(10);
  26. byte[] pfxData = CreateSelfSignCertificatePfx(
  27. x500,
  28. startTime,
  29. endTime);
  30. File.WriteAllBytes(fileName, pfxData);
  31. }
  32. private static byte[] CreateSelfSignCertificatePfx(
  33. string x500,
  34. DateTime startTime,
  35. DateTime endTime)
  36. {
  37. byte[] pfxData;
  38. if (x500 == null)
  39. {
  40. x500 = "";
  41. }
  42. SystemTime startSystemTime = ToSystemTime(startTime);
  43. SystemTime endSystemTime = ToSystemTime(endTime);
  44. string containerName = Guid.NewGuid().ToString();
  45. GCHandle dataHandle = new GCHandle();
  46. IntPtr providerContext = IntPtr.Zero;
  47. IntPtr cryptKey = IntPtr.Zero;
  48. IntPtr certContext = IntPtr.Zero;
  49. IntPtr certStore = IntPtr.Zero;
  50. IntPtr storeCertContext = IntPtr.Zero;
  51. IntPtr passwordPtr = IntPtr.Zero;
  52. RuntimeHelpers.PrepareConstrainedRegions();
  53. try
  54. {
  55. Check(NativeMethods.CryptAcquireContextW(
  56. out providerContext,
  57. containerName,
  58. null,
  59. 1, // PROV_RSA_FULL
  60. 8)); // CRYPT_NEWKEYSET
  61. Check(NativeMethods.CryptGenKey(
  62. providerContext,
  63. 1, // AT_KEYEXCHANGE
  64. 1 | 2048 << 16, // CRYPT_EXPORTABLE 2048 bit key
  65. out cryptKey));
  66. IntPtr errorStringPtr;
  67. int nameDataLength = 0;
  68. byte[] nameData;
  69. // errorStringPtr gets a pointer into the middle of the x500 string,
  70. // so x500 needs to be pinned until after we've copied the value
  71. // of errorStringPtr.
  72. dataHandle = GCHandle.Alloc(x500, GCHandleType.Pinned);
  73. if (!NativeMethods.CertStrToNameW(
  74. 0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
  75. dataHandle.AddrOfPinnedObject(),
  76. 3, // CERT_X500_NAME_STR = 3
  77. IntPtr.Zero,
  78. null,
  79. ref nameDataLength,
  80. out errorStringPtr))
  81. {
  82. string error = Marshal.PtrToStringUni(errorStringPtr);
  83. throw new ArgumentException(error);
  84. }
  85. nameData = new byte[nameDataLength];
  86. if (!NativeMethods.CertStrToNameW(
  87. 0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
  88. dataHandle.AddrOfPinnedObject(),
  89. 3, // CERT_X500_NAME_STR = 3
  90. IntPtr.Zero,
  91. nameData,
  92. ref nameDataLength,
  93. out errorStringPtr))
  94. {
  95. string error = Marshal.PtrToStringUni(errorStringPtr);
  96. throw new ArgumentException(error);
  97. }
  98. dataHandle.Free();
  99. dataHandle = GCHandle.Alloc(nameData, GCHandleType.Pinned);
  100. CryptoApiBlob nameBlob = new CryptoApiBlob(
  101. nameData.Length,
  102. dataHandle.AddrOfPinnedObject());
  103. CryptKeyProviderInformation kpi = new CryptKeyProviderInformation();
  104. kpi.ContainerName = containerName;
  105. kpi.ProviderType = 1; // PROV_RSA_FULL
  106. kpi.KeySpec = 1; // AT_KEYEXCHANGE
  107. CryptAlgorithmIdentifier sha256Identifier = new CryptAlgorithmIdentifier();
  108. sha256Identifier.pszObjId = "1.2.840.113549.1.1.11";
  109. certContext = NativeMethods.CertCreateSelfSignCertificate(
  110. providerContext,
  111. ref nameBlob,
  112. 0,
  113. ref kpi,
  114. ref sha256Identifier,
  115. ref startSystemTime,
  116. ref endSystemTime,
  117. IntPtr.Zero);
  118. Check(certContext != IntPtr.Zero);
  119. dataHandle.Free();
  120. certStore = NativeMethods.CertOpenStore(
  121. "Memory", // sz_CERT_STORE_PROV_MEMORY
  122. 0,
  123. IntPtr.Zero,
  124. 0x2000, // CERT_STORE_CREATE_NEW_FLAG
  125. IntPtr.Zero);
  126. Check(certStore != IntPtr.Zero);
  127. Check(NativeMethods.CertAddCertificateContextToStore(
  128. certStore,
  129. certContext,
  130. 1, // CERT_STORE_ADD_NEW
  131. out storeCertContext));
  132. NativeMethods.CertSetCertificateContextProperty(
  133. storeCertContext,
  134. 2, // CERT_KEY_PROV_INFO_PROP_ID
  135. 0,
  136. ref kpi);
  137. CryptoApiBlob pfxBlob = new CryptoApiBlob();
  138. Check(NativeMethods.PFXExportCertStoreEx(
  139. certStore,
  140. ref pfxBlob,
  141. passwordPtr,
  142. IntPtr.Zero,
  143. 7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
  144. pfxData = new byte[pfxBlob.DataLength];
  145. dataHandle = GCHandle.Alloc(pfxData, GCHandleType.Pinned);
  146. pfxBlob.Data = dataHandle.AddrOfPinnedObject();
  147. Check(NativeMethods.PFXExportCertStoreEx(
  148. certStore,
  149. ref pfxBlob,
  150. passwordPtr,
  151. IntPtr.Zero,
  152. 7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
  153. dataHandle.Free();
  154. }
  155. finally
  156. {
  157. if (passwordPtr != IntPtr.Zero)
  158. {
  159. Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr);
  160. }
  161. if (dataHandle.IsAllocated)
  162. {
  163. dataHandle.Free();
  164. }
  165. if (certContext != IntPtr.Zero)
  166. {
  167. NativeMethods.CertFreeCertificateContext(certContext);
  168. }
  169. if (storeCertContext != IntPtr.Zero)
  170. {
  171. NativeMethods.CertFreeCertificateContext(storeCertContext);
  172. }
  173. if (certStore != IntPtr.Zero)
  174. {
  175. NativeMethods.CertCloseStore(certStore, 0);
  176. }
  177. if (cryptKey != IntPtr.Zero)
  178. {
  179. NativeMethods.CryptDestroyKey(cryptKey);
  180. }
  181. if (providerContext != IntPtr.Zero)
  182. {
  183. NativeMethods.CryptReleaseContext(providerContext, 0);
  184. NativeMethods.CryptAcquireContextW(
  185. out providerContext,
  186. containerName,
  187. null,
  188. 1, // PROV_RSA_FULL
  189. 0x10); // CRYPT_DELETEKEYSET
  190. }
  191. }
  192. return pfxData;
  193. }
  194. private static SystemTime ToSystemTime(DateTime dateTime)
  195. {
  196. long fileTime = dateTime.ToFileTime();
  197. SystemTime systemTime;
  198. Check(NativeMethods.FileTimeToSystemTime(ref fileTime, out systemTime));
  199. return systemTime;
  200. }
  201. private static void Check(bool nativeCallSucceeded)
  202. {
  203. if (!nativeCallSucceeded)
  204. {
  205. int error = Marshal.GetHRForLastWin32Error();
  206. Marshal.ThrowExceptionForHR(error);
  207. }
  208. }
  209. }
  210. }