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.

324 lines
7.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. icryptp.h
  5. Abstract:
  6. This include file contains private constants, type definitions, and
  7. function prototypes for the IIS cryptographic routines.
  8. Author:
  9. Keith Moore (keithmo) 02-Dec-1996
  10. Revision History:
  11. --*/
  12. #ifndef _ICRYPTP_H_
  13. #define _ICRYPTP_H_
  14. //
  15. // Set this to a non-zero value to enable various object counters.
  16. //
  17. #if DBG
  18. #define IC_ENABLE_COUNTERS 1
  19. #else
  20. #define IC_ENABLE_COUNTERS 0
  21. #endif
  22. //
  23. // Constants defining our target crypto provider.
  24. //
  25. #define IC_CONTAINER TEXT("Microsoft Internet Information Server")
  26. #define IC_PROVIDER MS_DEF_PROV
  27. #define IC_PROVTYPE PROV_RSA_FULL
  28. #define IC_HASH_ALG CALG_MD5
  29. //
  30. // Alignment macros.
  31. //
  32. #define ALIGN_DOWN(count,size) \
  33. ((ULONG)(count) & ~((ULONG)(size) - 1))
  34. #define ALIGN_UP(count,size) \
  35. (ALIGN_DOWN( (ULONG)(count)+(ULONG)(size)-1, (ULONG)(size) ))
  36. #define ALIGN_8(count) \
  37. (ALIGN_UP( (ULONG)(count), 8 ))
  38. //
  39. // A blob. Note that we use these blobs for storing exported keys,
  40. // encrypted data, and hash results. Outside of this package, only
  41. // the IIS_CRYPTO_BLOB header is exposed; the blob internals are kept
  42. // private.
  43. //
  44. typedef struct _IC_BLOB {
  45. //
  46. // The standard header.
  47. //
  48. IIS_CRYPTO_BLOB Header;
  49. //
  50. // The data length. This will always be >0.
  51. //
  52. DWORD DataLength;
  53. //
  54. // The digital signature length. This may be 0 if no digital
  55. // signature is present.
  56. //
  57. DWORD SignatureLength;
  58. //
  59. // The actual data and digital signature go here, at the end
  60. // of the structure, but part of the same memory allocation
  61. // block. Use the following macros to access these fields.
  62. //
  63. // UCHAR Data[];
  64. // UCHAR Signature[];
  65. //
  66. } IC_BLOB;
  67. typedef UNALIGNED64 IC_BLOB *PIC_BLOB;
  68. #define BLOB_TO_DATA(p) \
  69. ((BYTE *)(((PIC_BLOB)(p)) + 1))
  70. #define BLOB_TO_SIGNATURE(p) \
  71. ((BYTE *)(((PCHAR)(((PIC_BLOB)(p)) + 1)) + \
  72. ALIGN_8(((PIC_BLOB)(p))->DataLength)))
  73. //
  74. // The following data structure is for specific metabase Backup/Restore
  75. //
  76. typedef struct _IC_BLOB2 {
  77. //
  78. // The standard header.
  79. //
  80. IIS_CRYPTO_BLOB Header;
  81. //
  82. // The data length. This will always be >0.
  83. //
  84. DWORD DataLength;
  85. //
  86. // The random salt length. At least 80 bits( 8 bytes ) long
  87. //
  88. DWORD SaltLength;
  89. //
  90. // The actual data and random salt go here, at the end
  91. // of the structure, but part of the same memory allocation
  92. // block. Use the following macros to access these fields.
  93. //
  94. // UCHAR Data[];
  95. // UCHAR Salt[];
  96. //
  97. } IC_BLOB2, *PIC_BLOB2;
  98. #define RANDOM_SALT_LENGTH 16
  99. #define BLOB_TO_DATA2(p) \
  100. ((BYTE *)(((PIC_BLOB2)(p)) + 1))
  101. #define BLOB_TO_SALT2(p) \
  102. ((BYTE *)(((PCHAR)(((PIC_BLOB2)(p)) + 1)) + \
  103. ALIGN_8(((PIC_BLOB2)(p))->DataLength)))
  104. //
  105. // Macro to calculate the data length of a blob, given the data and
  106. // signature lengths. To ensure natural alignment of the signature, we
  107. // quad-word align the data length if a signature is present.
  108. //
  109. #define CALC_BLOB_DATA_LENGTH(datalen,siglen) \
  110. ((sizeof(IC_BLOB) - sizeof(IIS_CRYPTO_BLOB)) + \
  111. ((siglen) + ( (siglen) ? ALIGN_8(datalen) : (datalen) )))
  112. //
  113. // Macro to calculate the data length of a blob, given the data and
  114. // salt lengths. To ensure natural alignment of the signature, we
  115. // quad-word align the data length if a signature is present.
  116. //
  117. #define CALC_BLOB_DATA_LENGTH2(datalen,saltlen) \
  118. ((sizeof(IC_BLOB2) - sizeof(IIS_CRYPTO_BLOB)) + \
  119. (saltlen) + (ALIGN_8(datalen)))
  120. //
  121. // Globals defined in globals.c.
  122. //
  123. typedef struct _IC_GLOBALS {
  124. //
  125. // Global synchronization lock (used sparingly).
  126. //
  127. CRITICAL_SECTION GlobalLock;
  128. //
  129. // Hash length for digital signatures. Since we always use the
  130. // same crypto provider & signature algorithm, we can retrieve
  131. // this once up front, and save some cycles later on.
  132. //
  133. DWORD HashLength;
  134. //
  135. // Set to TRUE if cryptography is enabled, FALSE if disabled.
  136. //
  137. BOOL EnableCryptography;
  138. //
  139. // Set to TRUE if we've been succesfully initialized.
  140. //
  141. BOOL Initialized;
  142. } IC_GLOBALS, *PIC_GLOBALS;
  143. extern IC_GLOBALS IcpGlobals;
  144. //
  145. // Private functions.
  146. //
  147. BOOL
  148. IcpIsEncryptionPermitted(
  149. VOID
  150. );
  151. HRESULT
  152. IcpGetLastError(
  153. VOID
  154. );
  155. HRESULT
  156. IcpGetHashLength(
  157. OUT LPDWORD pdwHashLength,
  158. IN HCRYPTPROV hProv
  159. );
  160. PIC_BLOB
  161. IcpCreateBlob(
  162. IN DWORD dwBlobSignature,
  163. IN DWORD dwDataLength,
  164. IN DWORD dwSignatureLength OPTIONAL
  165. );
  166. PIC_BLOB2
  167. IcpCreateBlob2(
  168. IN DWORD dwBlobSignature,
  169. IN DWORD dwDataLength,
  170. IN DWORD dwSaltLength OPTIONAL
  171. );
  172. #if IC_ENABLE_COUNTERS
  173. //
  174. // Object counters.
  175. //
  176. typedef struct _IC_COUNTERS {
  177. LONG ContainersOpened;
  178. LONG ContainersClosed;
  179. LONG KeysOpened;
  180. LONG KeysClosed;
  181. LONG HashCreated;
  182. LONG HashDestroyed;
  183. LONG BlobsCreated;
  184. LONG BlobsFreed;
  185. LONG Allocs;
  186. LONG Frees;
  187. } IC_COUNTERS, *PIC_COUNTERS;
  188. extern IC_COUNTERS IcpCounters;
  189. #define UpdateContainersOpened() InterlockedIncrement( &IcpCounters.ContainersOpened )
  190. #define UpdateContainersClosed() InterlockedIncrement( &IcpCounters.ContainersClosed )
  191. #define UpdateKeysOpened() InterlockedIncrement( &IcpCounters.KeysOpened )
  192. #define UpdateKeysClosed() InterlockedIncrement( &IcpCounters.KeysClosed )
  193. #define UpdateHashCreated() InterlockedIncrement( &IcpCounters.HashCreated )
  194. #define UpdateHashDestroyed() InterlockedIncrement( &IcpCounters.HashDestroyed )
  195. #define UpdateBlobsCreated() InterlockedIncrement( &IcpCounters.BlobsCreated )
  196. #define UpdateBlobsFreed() InterlockedIncrement( &IcpCounters.BlobsFreed )
  197. #define UpdateAllocs() InterlockedIncrement( &IcpCounters.Allocs )
  198. #define UpdateFrees() InterlockedIncrement( &IcpCounters.Frees )
  199. PVOID
  200. WINAPI
  201. IcpAllocMemory(
  202. IN DWORD Size
  203. );
  204. VOID
  205. WINAPI
  206. IcpFreeMemory(
  207. IN PVOID Buffer
  208. );
  209. #else // !IC_ENABLE_COUNTERS
  210. #define UpdateContainersOpened()
  211. #define UpdateContainersClosed()
  212. #define UpdateKeysOpened()
  213. #define UpdateKeysClosed()
  214. #define UpdateHashCreated()
  215. #define UpdateHashDestroyed()
  216. #define UpdateBlobsCreated()
  217. #define UpdateBlobsFreed()
  218. #define UpdateAllocs()
  219. #define UpdateFrees()
  220. #define IcpAllocMemory(cb) IISCryptoAllocMemory(cb)
  221. #define IcpFreeMemory(p) IISCryptoFreeMemory(p)
  222. #endif // IC_ENABLE_COUNTERS
  223. //
  224. // Dummy crypto handles returned in cryptography is disabled.
  225. //
  226. #define DUMMY_HPROV ((HCRYPTPROV)'vOrP')
  227. #define DUMMY_HHASH ((HCRYPTHASH)'hSaH')
  228. #define DUMMY_HSESSIONKEY ((HCRYPTKEY)'kSeS')
  229. #define DUMMY_HSIGNATUREKEY ((HCRYPTKEY)'kGiS')
  230. #define DUMMY_HKEYEXCHANGEKEY ((HCRYPTKEY)'kYeK')
  231. #endif // _ICRYPTP_H_