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.

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