Guest User

Untitled

a guest
Sep 18th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. Create Self Signed Certificate that is Exportable
  2. Certificate.Check(Certificate.NativeMethods.CryptAcquireContextW(
  3. out providerContext,
  4. containerName,
  5. null,
  6. 1, // PROV_RSA_FULL
  7. 8)); // CRYPT_NEWKEYSET
  8.  
  9. Certificate.Check(Certificate.NativeMethods.CryptGenKey(
  10. providerContext,
  11. 1, // AT_KEYEXCHANGE
  12. 1, // CRYPT_EXPORTABLE
  13. out cryptKey));
  14.  
  15. IntPtr errorStringPtr;
  16. int nameDataLength = 0;
  17. byte[] nameData;
  18.  
  19. // errorStringPtr gets a pointer into the middle of the x500 string,
  20. // so x500 needs to be pinned until after we've copied the value
  21. // of errorStringPtr.
  22. dataHandle = GCHandle.Alloc(commonName, GCHandleType.Pinned);
  23.  
  24. if (!Certificate.NativeMethods.CertStrToNameW(
  25. 0x00000001, // X509_ASN_ENCODING
  26. dataHandle.AddrOfPinnedObject(),
  27. 3, // CERT_X500_NAME_STR = 3
  28. IntPtr.Zero,
  29. null,
  30. ref nameDataLength,
  31. out errorStringPtr))
  32. {
  33. string error = Marshal.PtrToStringUni(errorStringPtr);
  34. throw new ArgumentException(error);
  35. }
  36.  
  37. nameData = new byte[nameDataLength];
  38.  
  39. if (!Certificate.NativeMethods.CertStrToNameW(
  40. 0x00000001, // X509_ASN_ENCODING
  41. dataHandle.AddrOfPinnedObject(),
  42. 3, // CERT_X500_NAME_STR = 3
  43. IntPtr.Zero,
  44. nameData,
  45. ref nameDataLength,
  46. out errorStringPtr))
  47. {
  48. string error = Marshal.PtrToStringUni(errorStringPtr);
  49. throw new ArgumentException(error);
  50. }
  51. Console.WriteLine("THIS IS CHANGED");
  52.  
  53. dataHandle.Free();
  54.  
  55. dataHandle = GCHandle.Alloc(nameData, GCHandleType.Pinned);
  56. Certificate.CryptoApiBlob nameBlob = new Certificate.CryptoApiBlob(
  57. nameData.Length,
  58. dataHandle.AddrOfPinnedObject());
  59.  
  60. Certificate.CryptKeyProviderInformation kpi = new Certificate.CryptKeyProviderInformation();
  61. kpi.ContainerName = containerName;
  62. kpi.ProviderType = 1; // PROV_RSA_FULL
  63. kpi.KeySpec = 1; // AT_KEYEXCHANGE
  64.  
  65. certContext = Certificate.NativeMethods.CertCreateSelfSignCertificate(
  66. IntPtr.Zero,
  67. ref nameBlob,
  68. 0,
  69. ref kpi,
  70. IntPtr.Zero, // default = SHA1RSA
  71. ref startSystemTime,
  72. ref endSystemTime,
  73. IntPtr.Zero);
  74. Certificate.Check(certContext != IntPtr.Zero);
  75. dataHandle.Free();
  76.  
  77. X509Certificate2 tempCert = new X509Certificate2(certContext);
  78. //result = new X509Certificate2(tempCert.RawData, "", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
  79. result = tempCert;
  80.  
  81. X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  82. store.Open(OpenFlags.ReadWrite);
  83. store.Add(result);
  84. store.Close();
  85.  
  86. internal static class NativeMethods
  87. {
  88. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  89. [return: MarshalAs(UnmanagedType.Bool)]
  90. public static extern bool FileTimeToSystemTime(
  91. [In] ref long fileTime,
  92. out SystemTime systemTime);
  93.  
  94. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  95. [return: MarshalAs(UnmanagedType.Bool)]
  96. public static extern bool CryptAcquireContextW(
  97. out IntPtr providerContext,
  98. [MarshalAs(UnmanagedType.LPWStr)] string container,
  99. [MarshalAs(UnmanagedType.LPWStr)] string provider,
  100. int providerType,
  101. int flags);
  102.  
  103. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  104. [return: MarshalAs(UnmanagedType.Bool)]
  105. public static extern bool CryptReleaseContext(
  106. IntPtr providerContext,
  107. int flags);
  108.  
  109. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  110. [return: MarshalAs(UnmanagedType.Bool)]
  111. public static extern bool CryptGenKey(
  112. IntPtr providerContext,
  113. int algorithmId,
  114. int flags,
  115. out IntPtr cryptKeyHandle);
  116.  
  117. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  118. [return: MarshalAs(UnmanagedType.Bool)]
  119. public static extern bool CryptDestroyKey(
  120. IntPtr cryptKeyHandle);
  121.  
  122. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  123. [return: MarshalAs(UnmanagedType.Bool)]
  124. public static extern bool CertStrToNameW(
  125. int certificateEncodingType,
  126. IntPtr x500,
  127. int strType,
  128. IntPtr reserved,
  129. [MarshalAs(UnmanagedType.LPArray)] [Out] byte[] encoded,
  130. ref int encodedLength,
  131. out IntPtr errorString);
  132.  
  133. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  134. public static extern IntPtr CertCreateSelfSignCertificate(
  135. IntPtr providerHandle,
  136. [In] ref CryptoApiBlob subjectIssuerBlob,
  137. int flags,
  138. [In] ref CryptKeyProviderInformation keyProviderInformation,
  139. IntPtr signatureAlgorithm,
  140. [In] ref SystemTime startTime,
  141. [In] ref SystemTime endTime,
  142. IntPtr extensions);
  143.  
  144. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  145. [return: MarshalAs(UnmanagedType.Bool)]
  146. public static extern bool CertFreeCertificateContext(
  147. IntPtr certificateContext);
  148.  
  149. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  150. public static extern IntPtr CertOpenStore(
  151. [MarshalAs(UnmanagedType.LPStr)] string storeProvider,
  152. int messageAndCertificateEncodingType,
  153. IntPtr cryptProvHandle,
  154. int flags,
  155. IntPtr parameters);
  156.  
  157. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  158. [return: MarshalAs(UnmanagedType.Bool)]
  159. public static extern bool CertCloseStore(
  160. IntPtr certificateStoreHandle,
  161. int flags);
  162.  
  163. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  164. [return: MarshalAs(UnmanagedType.Bool)]
  165. public static extern bool CertAddCertificateContextToStore(
  166. IntPtr certificateStoreHandle,
  167. IntPtr certificateContext,
  168. int addDisposition,
  169. out IntPtr storeContextPtr);
  170.  
  171. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  172. [return: MarshalAs(UnmanagedType.Bool)]
  173. public static extern bool CertSetCertificateContextProperty(
  174. IntPtr certificateContext,
  175. int propertyId,
  176. int flags,
  177. [In] ref CryptKeyProviderInformation data);
  178.  
  179. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  180. [return: MarshalAs(UnmanagedType.Bool)]
  181. public static extern bool PFXExportCertStoreEx(
  182. IntPtr certificateStoreHandle,
  183. ref CryptoApiBlob pfxBlob,
  184. IntPtr password,
  185. IntPtr reserved,
  186. int flags);
  187. }
  188.  
  189. NativeMethods.CryptAcquireContextW(
  190. out providerContext,
  191. containerName,
  192. null,
  193. 1, // PROV_RSA_FULL
  194. 0x10); // CRYPT_DELETEKEYSET
Add Comment
Please, Sign In to add comment