Source code of Windows XP (NT5)
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.

586 lines
12 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1999
  5. //
  6. // File: xelib.h
  7. //
  8. // Contents: defines and prototypes for functions moved from ca
  9. //
  10. //----------------------------------------------------------------------------
  11. #ifndef _XELIB_H_
  12. #define _XELIB_H_
  13. #include <certca.h>
  14. #define SECURITY_WIN32
  15. #include <security.h> // for EXTENDED_NAME_FORMAT
  16. //defines
  17. #define CCLOCKSKEWMINUTESDEFAULT 10
  18. #define CBMAX_CRYPT_HASH_LEN 20
  19. #define DWORDROUND(cb) (((cb) + (sizeof(DWORD) - 1)) & ~(sizeof(DWORD) - 1))
  20. #define POINTERROUND(cb) (((cb) + (sizeof(VOID *) - 1)) & ~(sizeof(VOID *) - 1))
  21. // Constants chosen to avoid DWORD overflow:
  22. #define CVT_WEEKS (7 * CVT_DAYS)
  23. #define CVT_DAYS (24 * CVT_HOURS)
  24. #define CVT_HOURS (60 * CVT_MINUTES)
  25. #define CVT_MINUTES (60 * CVT_SECONDS)
  26. #define CVT_SECONDS (1)
  27. #define CVT_BASE (1000 * 1000 * 10)
  28. // _XENROLL_SRC_ allows cs.h to define its own macros
  29. #if defined(_XENROLL_SRC_) || defined(_XELIBCA_SRC_) || defined(__CERTLIB_H__)
  30. // Size of a fixed array:
  31. #define ARRAYSIZE(a) (sizeof(a)/sizeof((a)[0]))
  32. #ifndef CSASSERT
  33. #if DBG
  34. # include <assert.h>
  35. # define CSASSERT(exp) assert(exp)
  36. # define DBGPRINTF(pszMessage, hr) \
  37. DbgPrintf(DBG_SS_APP, \
  38. "%s(%u): %s:error = 0x%x\n", \
  39. __FILE__, \
  40. __LINE__, \
  41. pszMessage, \
  42. hr)
  43. #else
  44. # define CSASSERT(exp)
  45. # define DBGPRINTF(pszMessage, hr)
  46. #endif //DBG
  47. #define _JumpIfError(hr, label, pszMessage) \
  48. { \
  49. if (S_OK != (hr)) \
  50. { \
  51. DBGPRINTF(pszMessage, hr); \
  52. goto label; \
  53. } \
  54. }
  55. #define _JumpError(hr, label, pszMessage) \
  56. { \
  57. DBGPRINTF(pszMessage, hr); \
  58. goto label; \
  59. }
  60. #define _PrintError(hr, pszMessage) \
  61. { \
  62. DBGPRINTF(pszMessage, hr); \
  63. }
  64. #define _PrintIfError(hr, pszMessage) \
  65. { \
  66. if (S_OK != (hr)) \
  67. { \
  68. DBGPRINTF(pszMessage, hr); \
  69. } \
  70. }
  71. #endif //CSASSERT
  72. #if !defined(_XENROLL_SRC_)
  73. # if DBG
  74. #define LocalFree myLocalFree
  75. #define SysFreeString mySysFreeString
  76. #define LocalReAlloc myLocalReAlloc
  77. #define LocalAlloc myLocalAlloc
  78. #define CoTaskMemAlloc myCoTaskMemAlloc
  79. #define SysAllocStringByteLen mySysAllocStringByteLen
  80. #define CoTaskMemFree myCoTaskMemFree
  81. #define FormatMessageW myFormatMessageW
  82. #define CoTaskMemRealloc myCoTaskMemRealloc
  83. #define StringFromCLSID myStringFromCLSID
  84. #define StringFromIID myStringFromIID
  85. #define SysAllocString mySysAllocString
  86. #define SysReAllocString mySysReAllocString
  87. #define SysAllocStringLen mySysAllocStringLen
  88. #define SysReAllocStringLen mySysReAllocStringLen
  89. #define PropVariantClear myPropVariantClear
  90. #define VariantClear myVariantClear
  91. #define VariantChangeType myVariantChangeType
  92. #define VariantChangeTypeEx myVariantChangeTypeEx
  93. void *myNew(IN size_t size);
  94. void myDelete(IN void *pv);
  95. __inline void *__cdecl operator new(
  96. IN size_t size)
  97. {
  98. return(myNew(size));
  99. }
  100. __inline void __cdecl operator delete(
  101. IN void *pv)
  102. {
  103. return(myDelete(pv));
  104. }
  105. DWORD
  106. myFormatMessageW(
  107. IN DWORD dwFlags,
  108. IN LPCVOID lpSource,
  109. IN DWORD dwMessageId,
  110. IN DWORD dwLanguageId,
  111. OUT LPWSTR lpBuffer,
  112. IN DWORD nSize,
  113. IN va_list *Arguments);
  114. HLOCAL
  115. myLocalAlloc(
  116. IN UINT uFlags,
  117. IN UINT uBytes);
  118. HLOCAL
  119. myLocalReAlloc(
  120. IN HLOCAL hMem,
  121. IN UINT uBytes,
  122. IN UINT uFlags);
  123. HLOCAL
  124. myLocalFree(
  125. IN HLOCAL hMem);
  126. VOID *
  127. myCoTaskMemAlloc(
  128. IN ULONG cb);
  129. VOID *
  130. myCoTaskMemRealloc(
  131. IN VOID *pv,
  132. IN ULONG cb);
  133. VOID
  134. myCoTaskMemFree(
  135. IN VOID *pv);
  136. HRESULT
  137. myStringFromCLSID(
  138. IN REFCLSID rclsid,
  139. OUT LPOLESTR FAR* lplpsz);
  140. HRESULT
  141. myStringFromIID(
  142. IN REFIID rclsid,
  143. OUT LPOLESTR FAR* lplpsz);
  144. BSTR
  145. mySysAllocString(
  146. IN const OLECHAR *pwszIn);
  147. INT
  148. mySysReAllocString(
  149. IN OUT BSTR *pstr,
  150. IN const OLECHAR *pwszIn);
  151. BSTR
  152. mySysAllocStringLen(
  153. IN const OLECHAR *pwcIn,
  154. IN UINT cwc);
  155. INT
  156. mySysReAllocStringLen(
  157. IN OUT BSTR *pstr,
  158. IN const OLECHAR *pwcIn,
  159. IN UINT cwc);
  160. VOID
  161. mySysFreeString(
  162. IN BSTR str);
  163. HRESULT
  164. myPropVariantClear(
  165. IN PROPVARIANT *pvar);
  166. HRESULT
  167. myVariantClear(
  168. IN VARIANTARG *pvar);
  169. HRESULT
  170. myVariantChangeType(
  171. OUT VARIANTARG *pvarDest,
  172. IN VARIANTARG *pvarSrc,
  173. IN unsigned short wFlags,
  174. IN VARTYPE vt);
  175. HRESULT
  176. myVariantChangeTypeEx(
  177. OUT VARIANTARG *pvarDest,
  178. IN VARIANTARG *pvarSrc,
  179. IN LCID lcid,
  180. IN unsigned short wFlags,
  181. IN VARTYPE vt);
  182. BSTR
  183. mySysAllocStringByteLen(
  184. LPCSTR pszIn,
  185. UINT cb);
  186. VOID
  187. mydbgDumpHex(
  188. IN DWORD dwSubSysId,
  189. IN DWORD Flags,
  190. IN BYTE const *pb,
  191. IN ULONG cb);
  192. #define CSM_LOCALALLOC 0
  193. #define CSM_COTASKALLOC 1
  194. #define CSM_SYSALLOC 2
  195. #define CSM_MALLOC 3
  196. #define CSM_NEW 4
  197. #define CSM_MAX 5
  198. #define CSM_MASK 0xff
  199. #define CSM_MIDLUSERALLOC CSM_COTASKALLOC
  200. #define CSM_VARIANT 100
  201. #define CSM_GLOBALDESTRUCTOR 0x200
  202. VOID
  203. myRegisterMemDump(VOID);
  204. VOID
  205. myRegisterMemAlloc(
  206. IN VOID const *pv,
  207. IN LONG cb,
  208. IN DWORD Flags);
  209. VOID
  210. myRegisterMemFree(
  211. IN VOID const *pv,
  212. IN DWORD Flags);
  213. # else //DBG
  214. #define myRegisterMemDump()
  215. #define myRegisterMemAlloc(pv, cb, Flags)
  216. #define myRegisterMemFree(pv, Flags)
  217. # endif //DBG
  218. #endif //!defined(_XENROLL_SRC_)
  219. // SUNDOWN-Safe pointer subtraction
  220. #ifndef SAFE_SUBTRACT_POINTERS
  221. #define SAFE_SUBTRACT_POINTERS(__x__, __y__) ( DW_PtrDiffc(__x__, sizeof(*(__x__)), __y__, sizeof(*(__y__))) )
  222. __inline DWORD
  223. DW_PtrDiffc(
  224. IN void const *pb1,
  225. IN DWORD dwPtrEltSize1,
  226. IN void const *pb2,
  227. IN DWORD dwPtrEltSize2)
  228. {
  229. // pb1 should be greater
  230. CSASSERT((ULONG_PTR)pb1 >= (ULONG_PTR)pb2);
  231. // both should have same elt size
  232. CSASSERT(dwPtrEltSize1 == dwPtrEltSize2);
  233. // assert that the result doesn't overflow 32-bits
  234. CSASSERT((DWORD)((ULONG_PTR)pb1 - (ULONG_PTR)pb2) == (ULONG_PTR)((ULONG_PTR)pb1 - (ULONG_PTR)pb2));
  235. // return number of objects between these pointers
  236. return (DWORD) ( ((ULONG_PTR)pb1 - (ULONG_PTR)pb2) / dwPtrEltSize1 );
  237. }
  238. #endif SAFE_SUBTRACT_POINTERS
  239. #endif //defined(_XENROLL_SRC_) || defined(__CERTLIB_H__)
  240. //xenroll implemented apis but called in xelib but not ca
  241. PCCRYPT_OID_INFO
  242. WINAPI
  243. xeCryptFindOIDInfo(
  244. IN DWORD dwKeyType,
  245. IN void *pvKey,
  246. IN DWORD dwGroupId // 0 => any group
  247. );
  248. //types
  249. enum CERTLIB_ALLOCATOR {
  250. CERTLIB_NO_ALLOCATOR = 0,
  251. CERTLIB_USE_COTASKMEMALLOC = 1,
  252. CERTLIB_USE_LOCALALLOC = 2,
  253. CERTLIB_USE_NEW = 3,
  254. };
  255. //prototypes
  256. VOID *
  257. myAlloc(IN size_t cbBytes, IN CERTLIB_ALLOCATOR allocType);
  258. VOID
  259. myFree(IN VOID *pv, IN CERTLIB_ALLOCATOR allocType);
  260. HRESULT
  261. myHError(IN HRESULT hr);
  262. HRESULT
  263. myHLastError(VOID);
  264. BOOL
  265. myEncodeObject(
  266. DWORD dwEncodingType,
  267. IN LPCSTR lpszStructType,
  268. IN VOID const *pvStructInfo,
  269. IN DWORD dwFlags,
  270. IN CERTLIB_ALLOCATOR allocType,
  271. OUT BYTE **ppbEncoded,
  272. OUT DWORD *pcbEncoded);
  273. BOOL
  274. myDecodeObject(
  275. IN DWORD dwEncodingType,
  276. IN LPCSTR lpszStructType,
  277. IN BYTE const *pbEncoded,
  278. IN DWORD cbEncoded,
  279. IN CERTLIB_ALLOCATOR allocType,
  280. OUT VOID **ppvStructInfo,
  281. OUT DWORD *pcbStructInfo);
  282. BOOL WINAPI
  283. myCryptExportPublicKeyInfo(
  284. IN HCRYPTPROV hCryptProv,
  285. IN DWORD dwKeySpec, // AT_SIGNATURE | AT_KEYEXCHANGE
  286. IN CERTLIB_ALLOCATOR allocType,
  287. OUT CERT_PUBLIC_KEY_INFO **ppPubKey,
  288. OUT DWORD *pcbPubKey);
  289. HRESULT
  290. myEncodeSignedContent(
  291. IN HCRYPTPROV hProv,
  292. IN DWORD dwCertEncodingType,
  293. IN char const *pszObjIdSignatureAlgorithm,
  294. IN BYTE *pbToBeSigned,
  295. IN DWORD cbToBeSigned,
  296. IN CERTLIB_ALLOCATOR allocType,
  297. OUT BYTE **ppbSigned,
  298. OUT DWORD *pcbSigned);
  299. VOID
  300. myMakeExprDateTime(
  301. IN OUT FILETIME *pft,
  302. IN LONG lDelta,
  303. IN enum ENUM_PERIOD enumPeriod);
  304. HRESULT
  305. myCryptMsgGetParam(
  306. IN HCRYPTMSG hMsg,
  307. IN DWORD dwParamType,
  308. IN DWORD dwIndex,
  309. IN CERTLIB_ALLOCATOR allocType,
  310. OUT VOID **ppvData,
  311. OUT DWORD *pcbData);
  312. HRESULT
  313. MultiByteIntegerToBstr(
  314. IN BOOL fOctetString,
  315. IN DWORD cbIn,
  316. IN BYTE const *pbIn,
  317. OUT BSTR *pstrOut);
  318. HRESULT
  319. MultiByteIntegerToWszBuf(
  320. IN BOOL fOctetString,
  321. IN DWORD cbIn,
  322. IN BYTE const *pbIn,
  323. IN OUT DWORD *pcbOut,
  324. OPTIONAL OUT WCHAR *pwszOut);
  325. typedef struct _CRYPT_REQUEST_CLIENT_INFO
  326. {
  327. DWORD dwClientId;
  328. WCHAR *pwszUser;
  329. WCHAR *pwszMachine;
  330. WCHAR *pwszProcess;
  331. } CRYPT_REQUEST_CLIENT_INFO;
  332. HRESULT
  333. myEncodeUTF8String(
  334. IN WCHAR const *pwszIn,
  335. OUT BYTE **ppbOut,
  336. OUT DWORD *pcbOut);
  337. HRESULT
  338. myDecodeUTF8String(
  339. IN BYTE const *pbIn,
  340. IN DWORD cbIn,
  341. OUT WCHAR **ppwszOut);
  342. HRESULT
  343. myEncodeRequestClientAttribute(
  344. IN CRYPT_REQUEST_CLIENT_INFO const *pcrci,
  345. OUT BYTE **ppbOut,
  346. OUT DWORD *pcbOut);
  347. HRESULT
  348. myDecodeRequestClientAttribute(
  349. IN BYTE const *pbIn,
  350. IN DWORD cbIn,
  351. OUT CRYPT_REQUEST_CLIENT_INFO **ppcrci);
  352. HRESULT
  353. myEncodeRequestClientAttributeFromClientId(
  354. IN DWORD dwClientId,
  355. OUT BYTE **ppbOut,
  356. OUT DWORD *pcbOut);
  357. HRESULT
  358. myGetUserNameEx(
  359. IN EXTENDED_NAME_FORMAT NameFormat,
  360. OUT WCHAR **ppwszUserName);
  361. HRESULT
  362. myGetMachineDnsName(
  363. OUT WCHAR **ppwszDnsName);
  364. HRESULT
  365. myGetProcessName(
  366. OUT WCHAR **ppwszProcessName);
  367. #define wszCNXCHGSUFFIX L"-Xchg"
  368. #define cchCOMMONNAMEMAX_XELIB 64
  369. HRESULT
  370. myAddNameSuffix(
  371. IN WCHAR const *pwszValue,
  372. IN WCHAR const *pwszSuffix,
  373. IN DWORD cwcNameMax,
  374. OUT WCHAR **ppwszOut);
  375. HRESULT
  376. BuildCMCAttributes(
  377. IN DWORD cAttribute,
  378. IN CRYPT_ATTRIBUTE const *rgAttribute,
  379. IN DWORD dwCMCDataReference,
  380. IN DWORD dwBodyPartIdOfRequest,
  381. IN DWORD dwBodyPartId,
  382. OUT CMC_TAGGED_ATTRIBUTE *pTaggedAttribute,
  383. OUT CRYPT_ATTR_BLOB *pBlob);
  384. HRESULT
  385. BuildCMCRequest(
  386. IN DWORD dwClientId,
  387. IN BOOL fNestedCMCRequest,
  388. IN BYTE const *pbReq,
  389. IN DWORD cbReq,
  390. OPTIONAL IN CERT_EXTENSION const *rgExt,
  391. IN DWORD cExt,
  392. OPTIONAL IN CRYPT_ATTRIBUTES const *rgAttributes,
  393. IN DWORD cAttributes,
  394. OPTIONAL IN CRYPT_ATTRIBUTE const *rgAttributeUnauth,
  395. IN DWORD cAttributeUnauth,
  396. OPTIONAL IN BYTE const *pbKeyIdRequest,
  397. IN DWORD cbKeyIdRequest,
  398. OPTIONAL IN HCRYPTPROV hProvRequest,
  399. IN DWORD dwKeySpecRequest,
  400. OPTIONAL IN LPCSTR pszOIDSignHashRequest,
  401. OPTIONAL IN CERT_CONTEXT const *pCertSigner,
  402. OPTIONAL IN HCRYPTPROV hProvSigner,
  403. IN DWORD dwKeySpecSigner,
  404. OPTIONAL IN LPCSTR pszObjIdHashSigner,
  405. OUT BYTE **ppbReqCMC,
  406. OUT DWORD *pcbReqCMC);
  407. typedef struct _XCMCRESPONSE
  408. {
  409. CMC_STATUS_INFO StatusInfo;
  410. WCHAR *pwszBodyPart; // Body Part Id string: "1.3.3.1"
  411. BYTE *pbCertHash;
  412. DWORD cbCertHash;
  413. BYTE *pbEncryptedKeyHash;
  414. DWORD cbEncryptedKeyHash;
  415. } XCMCRESPONSE;
  416. HRESULT
  417. ParseCMCResponse(
  418. IN BYTE *pbResponse,
  419. IN DWORD cbResponse,
  420. OPTIONAL OUT HCERTSTORE *phStoreResponse,
  421. OUT XCMCRESPONSE **prgResponse,
  422. OUT DWORD *pcResponse);
  423. VOID
  424. FreeCMCResponse(
  425. IN XCMCRESPONSE *rgResponse,
  426. IN DWORD cResponse);
  427. HRESULT
  428. myDupString(
  429. IN WCHAR const *pwszIn,
  430. IN WCHAR **ppwszOut);
  431. HRESULT
  432. myDecodePKCS7(
  433. IN BYTE const *pbIn,
  434. IN DWORD cbIn,
  435. OPTIONAL OUT BYTE **ppbContents,
  436. OPTIONAL OUT DWORD *pcbContents,
  437. OPTIONAL OUT DWORD *pdwMsgType,
  438. OPTIONAL OUT char **ppszInnerContentObjId,
  439. OPTIONAL OUT DWORD *pcSigner,
  440. OPTIONAL OUT DWORD *pcRecipient,
  441. OPTIONAL OUT HCERTSTORE *phStore,
  442. OPTIONAL OUT HCRYPTMSG *phMsg);
  443. HRESULT
  444. myGetPublicKeyHash(
  445. OPTIONAL IN CERT_INFO const *pCertInfo,
  446. IN CERT_PUBLIC_KEY_INFO const *pPublicKeyInfo,
  447. OUT BYTE **ppbData,
  448. OUT DWORD *pcbData);
  449. HRESULT
  450. myCreateSubjectKeyIdentifierExtension(
  451. IN CERT_PUBLIC_KEY_INFO const *pPubKey,
  452. OUT BYTE **ppbEncoded,
  453. OUT DWORD *pcbEncoded);
  454. HRESULT
  455. myCalculateKeyArchivalHash(
  456. IN const BYTE *pbEncryptedKey,
  457. IN DWORD cbEncryptedKey,
  458. OUT BYTE **ppbHash,
  459. OUT DWORD *pcbHash);
  460. HRESULT
  461. myInternetCanonicalizeUrl(
  462. IN WCHAR const *pwszIn,
  463. OUT WCHAR **ppwszOut);
  464. HRESULT
  465. myInternetUncanonicalizeURL(
  466. IN WCHAR const *pwszURLIn,
  467. OUT WCHAR **ppwszURLOut);
  468. BOOL
  469. myConvertWszToUTF8(
  470. OUT CHAR **ppsz,
  471. IN WCHAR const *pwc,
  472. IN LONG cwc);
  473. BOOL
  474. myConvertWszToSz(
  475. OUT CHAR **ppsz,
  476. IN WCHAR const *pwc,
  477. IN LONG cwc);
  478. BOOL
  479. myConvertUTF8ToWsz(
  480. OUT WCHAR **ppwsz,
  481. IN CHAR const *pch,
  482. IN LONG cch);
  483. BOOL
  484. myConvertSzToWsz(
  485. OUT WCHAR **ppwsz,
  486. IN CHAR const *pch,
  487. IN LONG cch);
  488. #endif //_XELIB_H_