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.

293 lines
8.8 KiB

  1. /* context.h */
  2. #ifndef DSSCSP_CONTEXT_H
  3. #define DSSCSP_CONTEXT_H
  4. #include <nt.h>
  5. #include <ntrtl.h>
  6. #include <nturtl.h>
  7. #include <crypt.h>
  8. #include <windows.h>
  9. #include <crtdbg.h>
  10. #include <csp.h>
  11. #include <randlib.h>
  12. #include <des.h>
  13. #include <sha.h>
  14. #ifdef CSP_USE_MD5
  15. #include "md5.h"
  16. #endif
  17. #ifdef CSP_USE_SHA1
  18. #include "sha.h"
  19. #endif
  20. // definition for disabling encryption in France
  21. #define CRYPT_DISABLE_CRYPT 0x1
  22. /*********************************/
  23. /* Definitions */
  24. /*********************************/
  25. #define KEY_MAGIC 0xBADF
  26. /* State definitions */
  27. #define KEY_INIT 0x0001
  28. #define MAX_BLOCKLEN 8
  29. // types of key storage
  30. #define PROTECTED_STORAGE_KEYS 1
  31. #define PROTECTION_API_KEYS 2
  32. #define HASH_MAGIC 0xBADE
  33. /* State Flags */
  34. #define HASH_INIT 0x0001
  35. #define HASH_DATA 0x0002
  36. #define HASH_FINISH 0x0004
  37. #define MAX_HASH_LEN 20
  38. #define CRYPT_BLKLEN 8
  39. #define HMAC_DEFAULT_STRING_LEN 64
  40. #define HMAC_STARTED 1
  41. #define HMAC_FINISHED 2
  42. /*********************************/
  43. /* Structure Definitions */
  44. /*********************************/
  45. typedef struct _Key_t_ {
  46. int magic; // Magic number
  47. void *pContext;
  48. int state; // State of object
  49. ALG_ID algId; // Algorithm Id
  50. DWORD flags; // General flags associated with key
  51. void *algParams; // Parameters for algorithm
  52. uchar IV[MAX_BLOCKLEN];
  53. uchar Temp_IV[MAX_BLOCKLEN];
  54. uchar *pbKey;
  55. DWORD cbKey;
  56. uchar *pbSalt;
  57. DWORD cbSalt;
  58. BYTE *pbData;
  59. DWORD cbData;
  60. DWORD cbEffectiveKeyLen;
  61. int mode;
  62. int pad;
  63. int mode_bits;
  64. BOOL InProgress; // if key is being used
  65. BOOL fUIOnKey; // flag to indicate if UI was to be set on the key
  66. } Key_t;
  67. // Packed version of Key_t. This is used when building opaque
  68. // blobs, and is necessary to properly support WOW64 operation.
  69. typedef struct _Packed_Key_t_ {
  70. // BLOBHEADER
  71. int magic; // Magic number
  72. int state; // State of object
  73. ALG_ID algId; // Algorithm Id
  74. DWORD flags; // General flags associated with key
  75. uchar IV[MAX_BLOCKLEN];
  76. uchar Temp_IV[MAX_BLOCKLEN];
  77. DWORD cbKey;
  78. DWORD cbData;
  79. DWORD cbEffectiveKeyLen;
  80. int mode;
  81. int pad;
  82. int mode_bits;
  83. BOOL InProgress; // if key is being used
  84. BOOL fUIOnKey; // flag to indicate if UI was to be set on the key
  85. // cbKey data bytes
  86. // cbData data bytes
  87. } Packed_Key_t;
  88. typedef struct {
  89. int magic; // Magic number
  90. void *pContext; // associated context
  91. int state; // State of hash object
  92. ALG_ID algId; // Algorithm Id
  93. DWORD size; // Size of hash
  94. void *pMAC; // pointer to mac state
  95. BYTE hashval[MAX_HASH_LEN];
  96. BYTE *pbData;
  97. DWORD cbData;
  98. Key_t *pKey;
  99. BOOL fInternalKey;
  100. ALG_ID HMACAlgid;
  101. DWORD HMACState;
  102. BYTE *pbHMACInner;
  103. DWORD cbHMACInner;
  104. BYTE *pbHMACOuter;
  105. DWORD cbHMACOuter;
  106. union {
  107. #if _WIN64
  108. //
  109. // fake member to cause 8byte alignment.
  110. //
  111. ULONGLONG Padding;
  112. #endif
  113. #ifdef CSP_USE_MD5
  114. MD5_CTX md5;
  115. #endif // CSP_USE_MD5
  116. #ifdef CSP_USE_SHA1
  117. A_SHA_CTX sha;
  118. #endif // CSP_USE_SHA1
  119. } algData;
  120. } Hash_t;
  121. /*********************************/
  122. /* Definitions */
  123. /*********************************/
  124. #define CONTEXT_MAGIC 0xDEADBEEF
  125. #define CONTEXT_RANDOM_LENGTH 20
  126. typedef struct _PStore_Info
  127. {
  128. HINSTANCE hInst;
  129. void *pProv;
  130. GUID SigType;
  131. GUID SigSubtype;
  132. GUID ExchType;
  133. GUID ExchSubtype;
  134. LPWSTR szPrompt;
  135. DWORD cbPrompt;
  136. } PSTORE_INFO;
  137. /*********************************/
  138. /* Structure Definitions */
  139. /*********************************/
  140. typedef struct {
  141. DWORD magic; // Magic number
  142. DWORD dwProvType; // Type of provider being called as
  143. LPSTR szProvName; // Name of provider being called as
  144. BOOL fMachineKeyset; // TRUE if keyset is for machine
  145. DWORD rights; // Privileges
  146. BOOL fIsLocalSystem; // check if running as local system
  147. KEY_CONTAINER_INFO ContInfo;
  148. Key_t *pSigKey; // pointer to the DSS sig key
  149. Key_t *pKExKey; // pointer to the DH key exchange key
  150. HKEY hKeys; // Handle to registry
  151. DWORD dwEnumalgs; // index for enumerating algorithms
  152. DWORD dwEnumalgsEx; // index for enumerating algorithms
  153. DWORD dwiSubKey; // index for enumerating containers
  154. DWORD dwMaxSubKey; // max number of containers
  155. void *contextData; // Context specific data
  156. CRITICAL_SECTION CritSec; // critical section for decrypting keys
  157. HWND hWnd; // handle to window for UI
  158. PSTORE_INFO *pPStore; // pointer to PStore information
  159. LPWSTR pwszPrompt; // UI prompt to be used
  160. DWORD dwOldKeyFlags; // flags to tell how keys should be migrated
  161. DWORD dwKeysetType; // type of storage used
  162. HANDLE hRNGDriver; // handle to hardware RNG driver
  163. EXPO_OFFLOAD_STRUCT *pOffloadInfo; // info for offloading modular expo
  164. DWORD dwPolicyId; // Index into policy keylengh arrays.
  165. BYTE rgbSigEncryptedX [RTL_ENCRYPT_MEMORY_SIZE + (SHA_DWORDS * sizeof(DWORD))];
  166. BYTE rgbKExEncryptedX [RTL_ENCRYPT_MEMORY_SIZE + (DSA_P_MAXDWORDS * sizeof(DWORD))];
  167. } Context_t;
  168. /*********************************/
  169. /* Policy Definitions */
  170. /*********************************/
  171. extern PROV_ENUMALGS_EX *g_AlgTables[];
  172. // NOTE -- These definitions must match the order of entries in g_AlgTables.
  173. #define POLICY_DSS_BASE 0 // Policy for MS_DEF_DSS_PROV
  174. #define POLICY_DSSDH_BASE 1 // Policy for MS_DEF_DSS_DH_PROV
  175. #define POLICY_DSSDH_ENHANCED 2 // Policy for MS_ENH_DSS_DH_PROV
  176. #define POLICY_DSSDH_SCHANNEL 3 // Policy for MS_DEF_DH_SCHANNEL_PROV
  177. /*********************************/
  178. /* Function Definitions */
  179. /*********************************/
  180. extern void
  181. freeContext(
  182. Context_t *pContext);
  183. extern Context_t *
  184. checkContext(
  185. HCRYPTPROV hProv);
  186. extern Context_t *
  187. allocContext(
  188. void);
  189. // Initialize a context
  190. extern DWORD
  191. initContext(
  192. IN OUT Context_t *pContext,
  193. IN DWORD dwFlags,
  194. IN DWORD dwProvType,
  195. IN LPCSTR szProvName,
  196. IN DWORD dwPolicyId);
  197. extern HCRYPTPROV
  198. AddContext(
  199. Context_t *pContext);
  200. extern HCRYPTHASH
  201. addContextHash(
  202. Context_t *pContext,
  203. Hash_t *pHash);
  204. extern Hash_t *
  205. checkContextHash(
  206. Context_t *pContext,
  207. HCRYPTHASH hHash);
  208. // Add key to context
  209. extern HCRYPTKEY
  210. addContextKey(
  211. Context_t *pContext,
  212. Key_t *pKey);
  213. // Check if key exists in context
  214. extern Key_t *
  215. checkContextKey(
  216. IN Context_t *pContext,
  217. IN HCRYPTKEY hKey);
  218. // random number generation prototype
  219. extern DWORD
  220. FIPS186GenRandom(
  221. IN HANDLE hRNGDriver,
  222. IN BYTE **ppbContextSeed,
  223. IN DWORD *pcbContextSeed,
  224. IN OUT BYTE *pb,
  225. IN DWORD cb);
  226. // Scrub sensitive data from memory
  227. extern void
  228. memnuke(
  229. volatile BYTE *pData,
  230. DWORD dwLen);
  231. #include "dh_key.h"
  232. extern void ScrubPrivateKeyInMemory(
  233. IN DHKey_t *pDH,
  234. IN BOOL fSigKey);
  235. extern DWORD EncryptPrivateKeyInMemory(
  236. IN Context_t *pContext,
  237. IN DHKey_t *pDH,
  238. IN ALG_ID AlgId);
  239. extern DWORD DecryptPrivateKeyInMemory(
  240. IN Context_t *pContext,
  241. IN DHKey_t *pDH,
  242. IN ALG_ID AlgId);
  243. #endif