NativeMethods.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security;
  4. namespace MediaBrowser.ServerApplication.Networking
  5. {
  6. /// <summary>
  7. /// Class NativeMethods
  8. /// </summary>
  9. [SuppressUnmanagedCodeSecurity]
  10. public static class NativeMethods
  11. {
  12. //declare the Netapi32 : NetServerEnum method import
  13. /// <summary>
  14. /// Nets the server enum.
  15. /// </summary>
  16. /// <param name="ServerName">Name of the server.</param>
  17. /// <param name="dwLevel">The dw level.</param>
  18. /// <param name="pBuf">The p buf.</param>
  19. /// <param name="dwPrefMaxLen">The dw pref max len.</param>
  20. /// <param name="dwEntriesRead">The dw entries read.</param>
  21. /// <param name="dwTotalEntries">The dw total entries.</param>
  22. /// <param name="dwServerType">Type of the dw server.</param>
  23. /// <param name="domain">The domain.</param>
  24. /// <param name="dwResumeHandle">The dw resume handle.</param>
  25. /// <returns>System.Int32.</returns>
  26. [DllImport("Netapi32", CharSet = CharSet.Auto, SetLastError = true),
  27. SuppressUnmanagedCodeSecurity]
  28. public static extern int NetServerEnum(
  29. string ServerName, // must be null
  30. int dwLevel,
  31. ref IntPtr pBuf,
  32. int dwPrefMaxLen,
  33. out int dwEntriesRead,
  34. out int dwTotalEntries,
  35. int dwServerType,
  36. string domain, // null for login domain
  37. out int dwResumeHandle
  38. );
  39. //declare the Netapi32 : NetApiBufferFree method import
  40. /// <summary>
  41. /// Nets the API buffer free.
  42. /// </summary>
  43. /// <param name="pBuf">The p buf.</param>
  44. /// <returns>System.Int32.</returns>
  45. [DllImport("Netapi32", SetLastError = true),
  46. SuppressUnmanagedCodeSecurity]
  47. public static extern int NetApiBufferFree(
  48. IntPtr pBuf);
  49. [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
  50. [return: MarshalAs(UnmanagedType.Bool)]
  51. public static extern bool FileTimeToSystemTime(
  52. [In] ref long fileTime,
  53. out SystemTime systemTime);
  54. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  55. [return: MarshalAs(UnmanagedType.Bool)]
  56. public static extern bool CryptAcquireContextW(
  57. out IntPtr providerContext,
  58. [MarshalAs(UnmanagedType.LPWStr)] string container,
  59. [MarshalAs(UnmanagedType.LPWStr)] string provider,
  60. int providerType,
  61. int flags);
  62. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  63. [return: MarshalAs(UnmanagedType.Bool)]
  64. public static extern bool CryptReleaseContext(
  65. IntPtr providerContext,
  66. int flags);
  67. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  68. [return: MarshalAs(UnmanagedType.Bool)]
  69. public static extern bool CryptGenKey(
  70. IntPtr providerContext,
  71. int algorithmId,
  72. int flags,
  73. out IntPtr cryptKeyHandle);
  74. [DllImport("AdvApi32.dll", SetLastError = true, ExactSpelling = true)]
  75. [return: MarshalAs(UnmanagedType.Bool)]
  76. public static extern bool CryptDestroyKey(
  77. IntPtr cryptKeyHandle);
  78. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  79. [return: MarshalAs(UnmanagedType.Bool)]
  80. public static extern bool CertStrToNameW(
  81. int certificateEncodingType,
  82. IntPtr x500,
  83. int strType,
  84. IntPtr reserved,
  85. [MarshalAs(UnmanagedType.LPArray)] [Out] byte[] encoded,
  86. ref int encodedLength,
  87. out IntPtr errorString);
  88. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  89. public static extern IntPtr CertCreateSelfSignCertificate(
  90. IntPtr providerHandle,
  91. [In] ref CryptoApiBlob subjectIssuerBlob,
  92. int flags,
  93. [In] ref CryptKeyProviderInformation keyProviderInformation,
  94. [In] ref CryptAlgorithmIdentifier algorithmIdentifier,
  95. [In] ref SystemTime startTime,
  96. [In] ref SystemTime endTime,
  97. IntPtr extensions);
  98. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  99. [return: MarshalAs(UnmanagedType.Bool)]
  100. public static extern bool CertFreeCertificateContext(
  101. IntPtr certificateContext);
  102. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  103. public static extern IntPtr CertOpenStore(
  104. [MarshalAs(UnmanagedType.LPStr)] string storeProvider,
  105. int messageAndCertificateEncodingType,
  106. IntPtr cryptProvHandle,
  107. int flags,
  108. IntPtr parameters);
  109. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  110. [return: MarshalAs(UnmanagedType.Bool)]
  111. public static extern bool CertCloseStore(
  112. IntPtr certificateStoreHandle,
  113. int flags);
  114. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  115. [return: MarshalAs(UnmanagedType.Bool)]
  116. public static extern bool CertAddCertificateContextToStore(
  117. IntPtr certificateStoreHandle,
  118. IntPtr certificateContext,
  119. int addDisposition,
  120. out IntPtr storeContextPtr);
  121. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  122. [return: MarshalAs(UnmanagedType.Bool)]
  123. public static extern bool CertSetCertificateContextProperty(
  124. IntPtr certificateContext,
  125. int propertyId,
  126. int flags,
  127. [In] ref CryptKeyProviderInformation data);
  128. [DllImport("Crypt32.dll", SetLastError = true, ExactSpelling = true)]
  129. [return: MarshalAs(UnmanagedType.Bool)]
  130. public static extern bool PFXExportCertStoreEx(
  131. IntPtr certificateStoreHandle,
  132. ref CryptoApiBlob pfxBlob,
  133. IntPtr password,
  134. IntPtr reserved,
  135. int flags);
  136. }
  137. //create a _SERVER_INFO_100 STRUCTURE
  138. /// <summary>
  139. /// Struct _SERVER_INFO_100
  140. /// </summary>
  141. [StructLayout(LayoutKind.Sequential)]
  142. public struct _SERVER_INFO_100
  143. {
  144. /// <summary>
  145. /// The sv100_platform_id
  146. /// </summary>
  147. internal int sv100_platform_id;
  148. /// <summary>
  149. /// The sv100_name
  150. /// </summary>
  151. [MarshalAs(UnmanagedType.LPWStr)]
  152. internal string sv100_name;
  153. }
  154. [StructLayout(LayoutKind.Sequential)]
  155. public struct SystemTime
  156. {
  157. public short Year;
  158. public short Month;
  159. public short DayOfWeek;
  160. public short Day;
  161. public short Hour;
  162. public short Minute;
  163. public short Second;
  164. public short Milliseconds;
  165. }
  166. [StructLayout(LayoutKind.Sequential)]
  167. public struct CryptObjIdBlob
  168. {
  169. public uint cbData;
  170. public IntPtr pbData;
  171. }
  172. [StructLayout(LayoutKind.Sequential)]
  173. public struct CryptAlgorithmIdentifier
  174. {
  175. [MarshalAs(UnmanagedType.LPStr)]
  176. public String pszObjId;
  177. public CryptObjIdBlob Parameters;
  178. }
  179. [StructLayout(LayoutKind.Sequential)]
  180. public struct CryptoApiBlob
  181. {
  182. public int DataLength;
  183. public IntPtr Data;
  184. public CryptoApiBlob(int dataLength, IntPtr data)
  185. {
  186. this.DataLength = dataLength;
  187. this.Data = data;
  188. }
  189. }
  190. [StructLayout(LayoutKind.Sequential)]
  191. public struct CryptKeyProviderInformation
  192. {
  193. [MarshalAs(UnmanagedType.LPWStr)]
  194. public string ContainerName;
  195. [MarshalAs(UnmanagedType.LPWStr)]
  196. public string ProviderName;
  197. public int ProviderType;
  198. public int Flags;
  199. public int ProviderParameterCount;
  200. public IntPtr ProviderParameters; // PCRYPT_KEY_PROV_PARAM
  201. public int KeySpec;
  202. }
  203. }