CertificateGenerator.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.InteropServices;
  7. using System.Security;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using MediaBrowser.Model.Logging;
  11. namespace MediaBrowser.ServerApplication.Networking
  12. {
  13. // Copied from: http://blogs.msdn.com/b/dcook/archive/2014/05/16/9143036.aspx
  14. // 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 --
  15. // 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,
  16. // 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.
  17. internal class CertificateGenerator
  18. {
  19. internal static void CreateSelfSignCertificatePfx(
  20. string fileName,
  21. string hostname,
  22. ILogger logger)
  23. {
  24. try
  25. {
  26. if (string.IsNullOrWhiteSpace(fileName))
  27. {
  28. logger.Info("No certificate filename specified.");
  29. return;
  30. }
  31. if (File.Exists(fileName))
  32. {
  33. logger.Info("Certificate file already exists. To regenerate, delete {0}", fileName);
  34. return;
  35. }
  36. string x500 = string.Format("CN={0}", hostname);
  37. DateTime startTime = DateTime.Now.AddDays(-2);
  38. DateTime endTime = DateTime.Now.AddYears(10);
  39. byte[] pfxData = CreateSelfSignCertificatePfx(
  40. x500,
  41. startTime,
  42. endTime);
  43. File.WriteAllBytes(fileName, pfxData);
  44. }
  45. catch (Exception e)
  46. {
  47. logger.ErrorException("Error generating self signed ssl certificate: {0}", e, fileName);
  48. }
  49. }
  50. private static byte[] CreateSelfSignCertificatePfx(
  51. string x500,
  52. DateTime startTime,
  53. DateTime endTime)
  54. {
  55. byte[] pfxData;
  56. if (x500 == null)
  57. {
  58. x500 = "";
  59. }
  60. SystemTime startSystemTime = ToSystemTime(startTime);
  61. SystemTime endSystemTime = ToSystemTime(endTime);
  62. string containerName = Guid.NewGuid().ToString();
  63. GCHandle dataHandle = new GCHandle();
  64. IntPtr providerContext = IntPtr.Zero;
  65. IntPtr cryptKey = IntPtr.Zero;
  66. IntPtr certContext = IntPtr.Zero;
  67. IntPtr certStore = IntPtr.Zero;
  68. IntPtr storeCertContext = IntPtr.Zero;
  69. IntPtr passwordPtr = IntPtr.Zero;
  70. RuntimeHelpers.PrepareConstrainedRegions();
  71. try
  72. {
  73. Check(NativeMethods.CryptAcquireContextW(
  74. out providerContext,
  75. containerName,
  76. null,
  77. 1, // PROV_RSA_FULL
  78. 8)); // CRYPT_NEWKEYSET
  79. Check(NativeMethods.CryptGenKey(
  80. providerContext,
  81. 1, // AT_KEYEXCHANGE
  82. 1 | 2048 << 16, // CRYPT_EXPORTABLE 2048 bit key
  83. out cryptKey));
  84. IntPtr errorStringPtr;
  85. int nameDataLength = 0;
  86. byte[] nameData;
  87. // errorStringPtr gets a pointer into the middle of the x500 string,
  88. // so x500 needs to be pinned until after we've copied the value
  89. // of errorStringPtr.
  90. dataHandle = GCHandle.Alloc(x500, GCHandleType.Pinned);
  91. if (!NativeMethods.CertStrToNameW(
  92. 0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
  93. dataHandle.AddrOfPinnedObject(),
  94. 3, // CERT_X500_NAME_STR = 3
  95. IntPtr.Zero,
  96. null,
  97. ref nameDataLength,
  98. out errorStringPtr))
  99. {
  100. string error = Marshal.PtrToStringUni(errorStringPtr);
  101. throw new ArgumentException(error);
  102. }
  103. nameData = new byte[nameDataLength];
  104. if (!NativeMethods.CertStrToNameW(
  105. 0x00010001, // X509_ASN_ENCODING | PKCS_7_ASN_ENCODING
  106. dataHandle.AddrOfPinnedObject(),
  107. 3, // CERT_X500_NAME_STR = 3
  108. IntPtr.Zero,
  109. nameData,
  110. ref nameDataLength,
  111. out errorStringPtr))
  112. {
  113. string error = Marshal.PtrToStringUni(errorStringPtr);
  114. throw new ArgumentException(error);
  115. }
  116. dataHandle.Free();
  117. dataHandle = GCHandle.Alloc(nameData, GCHandleType.Pinned);
  118. CryptoApiBlob nameBlob = new CryptoApiBlob(
  119. nameData.Length,
  120. dataHandle.AddrOfPinnedObject());
  121. CryptKeyProviderInformation kpi = new CryptKeyProviderInformation();
  122. kpi.ContainerName = containerName;
  123. kpi.ProviderType = 1; // PROV_RSA_FULL
  124. kpi.KeySpec = 1; // AT_KEYEXCHANGE
  125. CryptAlgorithmIdentifier sha256Identifier = new CryptAlgorithmIdentifier();
  126. sha256Identifier.pszObjId = "1.2.840.113549.1.1.11";
  127. certContext = NativeMethods.CertCreateSelfSignCertificate(
  128. providerContext,
  129. ref nameBlob,
  130. 0,
  131. ref kpi,
  132. ref sha256Identifier,
  133. ref startSystemTime,
  134. ref endSystemTime,
  135. IntPtr.Zero);
  136. Check(certContext != IntPtr.Zero);
  137. dataHandle.Free();
  138. certStore = NativeMethods.CertOpenStore(
  139. "Memory", // sz_CERT_STORE_PROV_MEMORY
  140. 0,
  141. IntPtr.Zero,
  142. 0x2000, // CERT_STORE_CREATE_NEW_FLAG
  143. IntPtr.Zero);
  144. Check(certStore != IntPtr.Zero);
  145. Check(NativeMethods.CertAddCertificateContextToStore(
  146. certStore,
  147. certContext,
  148. 1, // CERT_STORE_ADD_NEW
  149. out storeCertContext));
  150. NativeMethods.CertSetCertificateContextProperty(
  151. storeCertContext,
  152. 2, // CERT_KEY_PROV_INFO_PROP_ID
  153. 0,
  154. ref kpi);
  155. CryptoApiBlob pfxBlob = new CryptoApiBlob();
  156. Check(NativeMethods.PFXExportCertStoreEx(
  157. certStore,
  158. ref pfxBlob,
  159. passwordPtr,
  160. IntPtr.Zero,
  161. 7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
  162. pfxData = new byte[pfxBlob.DataLength];
  163. dataHandle = GCHandle.Alloc(pfxData, GCHandleType.Pinned);
  164. pfxBlob.Data = dataHandle.AddrOfPinnedObject();
  165. Check(NativeMethods.PFXExportCertStoreEx(
  166. certStore,
  167. ref pfxBlob,
  168. passwordPtr,
  169. IntPtr.Zero,
  170. 7)); // EXPORT_PRIVATE_KEYS | REPORT_NO_PRIVATE_KEY | REPORT_NOT_ABLE_TO_EXPORT_PRIVATE_KEY
  171. dataHandle.Free();
  172. }
  173. finally
  174. {
  175. if (passwordPtr != IntPtr.Zero)
  176. {
  177. Marshal.ZeroFreeCoTaskMemUnicode(passwordPtr);
  178. }
  179. if (dataHandle.IsAllocated)
  180. {
  181. dataHandle.Free();
  182. }
  183. if (certContext != IntPtr.Zero)
  184. {
  185. NativeMethods.CertFreeCertificateContext(certContext);
  186. }
  187. if (storeCertContext != IntPtr.Zero)
  188. {
  189. NativeMethods.CertFreeCertificateContext(storeCertContext);
  190. }
  191. if (certStore != IntPtr.Zero)
  192. {
  193. NativeMethods.CertCloseStore(certStore, 0);
  194. }
  195. if (cryptKey != IntPtr.Zero)
  196. {
  197. NativeMethods.CryptDestroyKey(cryptKey);
  198. }
  199. if (providerContext != IntPtr.Zero)
  200. {
  201. NativeMethods.CryptReleaseContext(providerContext, 0);
  202. NativeMethods.CryptAcquireContextW(
  203. out providerContext,
  204. containerName,
  205. null,
  206. 1, // PROV_RSA_FULL
  207. 0x10); // CRYPT_DELETEKEYSET
  208. }
  209. }
  210. return pfxData;
  211. }
  212. private static SystemTime ToSystemTime(DateTime dateTime)
  213. {
  214. long fileTime = dateTime.ToFileTime();
  215. SystemTime systemTime;
  216. Check(NativeMethods.FileTimeToSystemTime(ref fileTime, out systemTime));
  217. return systemTime;
  218. }
  219. private static void Check(bool nativeCallSucceeded)
  220. {
  221. if (!nativeCallSucceeded)
  222. {
  223. int error = Marshal.GetHRForLastWin32Error();
  224. Marshal.ThrowExceptionForHR(error);
  225. }
  226. }
  227. }
  228. }