Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft Windows
  3. //
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: crypthlp.h
  7. //
  8. // Contents: Misc internal crypt/certificate helper APIs
  9. //
  10. // APIs: I_CryptGetDefaultCryptProv
  11. // I_CryptGetDefaultCryptProvForEncrypt
  12. // I_CryptGetFileVersion
  13. // I_CertSyncStoreEx
  14. // I_CertSyncStore
  15. // I_CertUpdateStore
  16. // I_RecursiveCreateDirectory
  17. // I_RecursiveDeleteDirectory
  18. // I_CryptReadTrustedPublisherDWORDValueFromRegistry
  19. // I_CryptZeroFileTime
  20. // I_CryptIsZeroFileTime
  21. // I_CryptIncrementFileTimeBySeconds
  22. // I_CryptDecrementFileTimeBySeconds
  23. // I_CryptSubtractFileTimes
  24. // I_CryptIncrementFileTimeByMilliseconds
  25. // I_CryptDecrementFileTimeByMilliseconds
  26. // I_CryptRemainingMilliseconds
  27. //
  28. // History: 01-Jun-97 philh created
  29. //--------------------------------------------------------------------------
  30. #ifndef __CRYPTHLP_H__
  31. #define __CRYPTHLP_H__
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. //
  36. // Cross Cert Distribution Retrieval Times
  37. //
  38. // 8 hours
  39. #define XCERT_DEFAULT_SYNC_DELTA_TIME (60 * 60 * 8)
  40. // 1 hour
  41. #define XCERT_MIN_SYNC_DELTA_TIME (60 * 60)
  42. //+-------------------------------------------------------------------------
  43. // Acquire default CryptProv according to the public key algorithm supported
  44. // by the provider type. The provider is acquired with only
  45. // CRYPT_VERIFYCONTEXT.
  46. //
  47. // Setting aiPubKey to 0, gets the default provider for RSA_FULL.
  48. //
  49. // Note, the returned CryptProv must not be released. Once acquired, the
  50. // CryptProv isn't released until ProcessDetach. This allows the returned
  51. // HCRYPTPROVs to be shared.
  52. //--------------------------------------------------------------------------
  53. HCRYPTPROV
  54. WINAPI
  55. I_CryptGetDefaultCryptProv(
  56. IN ALG_ID aiPubKey
  57. );
  58. //+-------------------------------------------------------------------------
  59. // Acquire default CryptProv according to the public key algorithm, encrypt
  60. // key algorithm and encrypt key length supported by the provider type.
  61. //
  62. // dwBitLen = 0, assumes the aiEncrypt's default bit length. For example,
  63. // CALG_RC2 has a default bit length of 40.
  64. //
  65. // Note, the returned CryptProv must not be released. Once acquired, the
  66. // CryptProv isn't released until ProcessDetach. This allows the returned
  67. // CryptProvs to be shared.
  68. //--------------------------------------------------------------------------
  69. HCRYPTPROV
  70. WINAPI
  71. I_CryptGetDefaultCryptProvForEncrypt(
  72. IN ALG_ID aiPubKey,
  73. IN ALG_ID aiEncrypt,
  74. IN DWORD dwBitLen
  75. );
  76. //+-------------------------------------------------------------------------
  77. // crypt32.dll release version numbers
  78. //--------------------------------------------------------------------------
  79. #define IE4_CRYPT32_DLL_VER_MS (( 5 << 16) | 101 )
  80. #define IE4_CRYPT32_DLL_VER_LS (( 1670 << 16) | 1 )
  81. //+-------------------------------------------------------------------------
  82. // Get file version of the specified file
  83. //--------------------------------------------------------------------------
  84. BOOL
  85. WINAPI
  86. I_CryptGetFileVersion(
  87. IN LPCWSTR pwszFilename,
  88. OUT DWORD *pdwFileVersionMS, /* e.g. 0x00030075 = "3.75" */
  89. OUT DWORD *pdwFileVersionLS /* e.g. 0x00000031 = "0.31" */
  90. );
  91. //+-------------------------------------------------------------------------
  92. // Synchronize the original store with the new store.
  93. //
  94. // Assumptions: Both are cache stores. The new store is temporary
  95. // and local to the caller. The new store's contexts can be deleted or
  96. // moved to the original store.
  97. //--------------------------------------------------------------------------
  98. BOOL
  99. WINAPI
  100. I_CertSyncStore(
  101. IN OUT HCERTSTORE hOriginalStore,
  102. IN OUT HCERTSTORE hNewStore
  103. );
  104. //+-------------------------------------------------------------------------
  105. // Synchronize the original store with the new store.
  106. //
  107. // Assumptions: Both are cache stores. The new store is temporary
  108. // and local to the caller. The new store's contexts can be deleted or
  109. // moved to the original store.
  110. //
  111. // Setting ICERT_SYNC_STORE_INHIBIT_SYNC_PROPERTY_IN_FLAG in dwInFlags
  112. // inhibits the syncing of properties.
  113. //
  114. // ICERT_SYNC_STORE_CHANGED_OUT_FLAG is returned and set in *pdwOutFlags
  115. // if any contexts were added or deleted from the original store.
  116. //--------------------------------------------------------------------------
  117. BOOL
  118. WINAPI
  119. I_CertSyncStoreEx(
  120. IN OUT HCERTSTORE hOriginalStore,
  121. IN OUT HCERTSTORE hNewStore,
  122. IN DWORD dwInFlags,
  123. OUT OPTIONAL DWORD *pdwOutFlags,
  124. IN OUT OPTIONAL void *pvReserved
  125. );
  126. #define ICERT_SYNC_STORE_INHIBIT_SYNC_PROPERTY_IN_FLAG 0x00000001
  127. #define ICERT_SYNC_STORE_CHANGED_OUT_FLAG 0x00010000
  128. //+-------------------------------------------------------------------------
  129. // Update the original store with contexts from the new store.
  130. //
  131. // Assumptions: Both are cache stores. The new store is temporary
  132. // and local to the caller. The new store's contexts can be deleted or
  133. // moved to the original store.
  134. //--------------------------------------------------------------------------
  135. BOOL
  136. WINAPI
  137. I_CertUpdateStore(
  138. IN OUT HCERTSTORE hOriginalStore,
  139. IN OUT HCERTSTORE hNewStore,
  140. IN DWORD dwReserved,
  141. IN OUT void *pvReserved
  142. );
  143. //+-------------------------------------------------------------------------
  144. // Recursively creates a full directory path
  145. //--------------------------------------------------------------------------
  146. BOOL
  147. I_RecursiveCreateDirectory(
  148. IN LPCWSTR pwszDir,
  149. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes
  150. );
  151. //+-------------------------------------------------------------------------
  152. // Recursively deletes a whole directory
  153. //--------------------------------------------------------------------------
  154. BOOL
  155. I_RecursiveDeleteDirectory(
  156. IN LPCWSTR pwszDelete
  157. );
  158. //+-------------------------------------------------------------------------
  159. // Recursively copies a whole directory
  160. //--------------------------------------------------------------------------
  161. BOOL
  162. I_RecursiveCopyDirectory(
  163. IN LPCWSTR pwszDirFrom,
  164. IN LPCWSTR pwszDirTo
  165. );
  166. //+-------------------------------------------------------------------------
  167. // First checks if the registry value exists in GPO Policies section. If
  168. // not, checks the LocalMachine section.
  169. //--------------------------------------------------------------------------
  170. BOOL
  171. I_CryptReadTrustedPublisherDWORDValueFromRegistry(
  172. IN LPCWSTR pwszValueName,
  173. OUT DWORD *pdwValue
  174. );
  175. //+-------------------------------------------------------------------------
  176. // Zero's the filetime
  177. //--------------------------------------------------------------------------
  178. __inline
  179. void
  180. WINAPI
  181. I_CryptZeroFileTime(
  182. OUT LPFILETIME pft
  183. )
  184. {
  185. pft->dwLowDateTime = 0;
  186. pft->dwHighDateTime = 0;
  187. }
  188. //+-------------------------------------------------------------------------
  189. // Check for a filetime of 0. Normally, this indicates the filetime
  190. // wasn't specified.
  191. //--------------------------------------------------------------------------
  192. __inline
  193. BOOL
  194. WINAPI
  195. I_CryptIsZeroFileTime(
  196. IN LPFILETIME pft
  197. )
  198. {
  199. if (0 == pft->dwLowDateTime && 0 == pft->dwHighDateTime)
  200. return TRUE;
  201. else
  202. return FALSE;
  203. }
  204. //+-------------------------------------------------------------------------
  205. // Increment the filetime by the specified number of seconds.
  206. //
  207. // Filetime is in units of 100 nanoseconds. Each second has
  208. // 10**7 100 nanoseconds.
  209. //--------------------------------------------------------------------------
  210. __inline
  211. void
  212. WINAPI
  213. I_CryptIncrementFileTimeBySeconds(
  214. IN LPFILETIME pftSrc,
  215. IN DWORD dwSeconds,
  216. OUT LPFILETIME pftDst
  217. )
  218. {
  219. *(((DWORDLONG UNALIGNED *) pftDst)) =
  220. *(((DWORDLONG UNALIGNED *) pftSrc)) +
  221. (((DWORDLONG) dwSeconds) * 10000000i64);
  222. }
  223. //+-------------------------------------------------------------------------
  224. // Decrement the filetime by the specified number of seconds.
  225. //
  226. // Filetime is in units of 100 nanoseconds. Each second has
  227. // 10**7 100 nanoseconds.
  228. //--------------------------------------------------------------------------
  229. __inline
  230. void
  231. WINAPI
  232. I_CryptDecrementFileTimeBySeconds(
  233. IN LPFILETIME pftSrc,
  234. IN DWORD dwSeconds,
  235. OUT LPFILETIME pftDst
  236. )
  237. {
  238. *(((DWORDLONG UNALIGNED *) pftDst)) =
  239. *(((DWORDLONG UNALIGNED *) pftSrc)) -
  240. (((DWORDLONG) dwSeconds) * 10000000i64);
  241. }
  242. //+-------------------------------------------------------------------------
  243. // Subtract two filetimes and return the number of seconds.
  244. //
  245. // The second filetime is subtracted from the first. If the first filetime
  246. // is before the second, then, 0 seconds is returned.
  247. //
  248. // Filetime is in units of 100 nanoseconds. Each second has
  249. // 10**7 100 nanoseconds.
  250. //--------------------------------------------------------------------------
  251. __inline
  252. DWORD
  253. WINAPI
  254. I_CryptSubtractFileTimes(
  255. IN LPFILETIME pftFirst,
  256. IN LPFILETIME pftSecond
  257. )
  258. {
  259. DWORDLONG qwDiff;
  260. if (0 >= CompareFileTime(pftFirst, pftSecond))
  261. return 0;
  262. qwDiff = *(((DWORDLONG UNALIGNED *) pftFirst)) -
  263. *(((DWORDLONG UNALIGNED *) pftSecond));
  264. return (DWORD) (qwDiff / 10000000i64);
  265. }
  266. //+-------------------------------------------------------------------------
  267. // Increment the filetime by the specified number of milliseconds.
  268. //
  269. // Filetime is in units of 100 nanoseconds. Each millisecond has
  270. // 10**4 100 nanoseconds.
  271. //--------------------------------------------------------------------------
  272. __inline
  273. void
  274. WINAPI
  275. I_CryptIncrementFileTimeByMilliseconds(
  276. IN LPFILETIME pftSrc,
  277. IN DWORD dwMilliseconds,
  278. OUT LPFILETIME pftDst
  279. )
  280. {
  281. *(((DWORDLONG UNALIGNED *) pftDst)) =
  282. *(((DWORDLONG UNALIGNED *) pftSrc)) +
  283. (((DWORDLONG) dwMilliseconds) * 10000i64);
  284. }
  285. //+-------------------------------------------------------------------------
  286. // Decrement the filetime by the specified number of milliseconds.
  287. //
  288. // Filetime is in units of 100 nanoseconds. Each millisecond has
  289. // 10**4 100 nanoseconds.
  290. //--------------------------------------------------------------------------
  291. __inline
  292. void
  293. WINAPI
  294. I_CryptDecrementFileTimeByMilliseconds(
  295. IN LPFILETIME pftSrc,
  296. IN DWORD dwMilliseconds,
  297. OUT LPFILETIME pftDst
  298. )
  299. {
  300. *(((DWORDLONG UNALIGNED *) pftDst)) =
  301. *(((DWORDLONG UNALIGNED *) pftSrc)) -
  302. (((DWORDLONG) dwMilliseconds) * 10000i64);
  303. }
  304. //+-------------------------------------------------------------------------
  305. // Return the number of milliseconds remaining before the specified end
  306. // filetime.
  307. //
  308. // The current filetime is subtracted from the end filetime. If the current
  309. // filetime is after or the same as the end filetime, then, 0 milliseconds
  310. // is returned.
  311. //
  312. // Filetime is in units of 100 nanoseconds. Each millisecond has
  313. // 10**4 100 nanoseconds.
  314. //--------------------------------------------------------------------------
  315. __inline
  316. DWORD
  317. WINAPI
  318. I_CryptRemainingMilliseconds(
  319. IN LPFILETIME pftEnd
  320. )
  321. {
  322. FILETIME ftCurrent;
  323. DWORDLONG qwDiff;
  324. GetSystemTimeAsFileTime(&ftCurrent);
  325. if (0 >= CompareFileTime(pftEnd, &ftCurrent))
  326. return 0;
  327. qwDiff = *(((DWORDLONG UNALIGNED *) pftEnd)) -
  328. *(((DWORDLONG UNALIGNED *) &ftCurrent));
  329. return (DWORD) (qwDiff / 10000i64);
  330. }
  331. #ifdef __cplusplus
  332. } // Balance extern "C" above
  333. #endif
  334. #endif