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.

1078 lines
28 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: mscatapi.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: CryptCATOpen
  12. // CryptCATClose
  13. // CryptCATStoreFromHandle
  14. // CryptCATGetMemberInfo
  15. // CryptCATPutMemberInfo
  16. // CryptCATVerifyMember
  17. // CryptCATGetAttrInfo
  18. // CryptCATPutAttrInfo
  19. // CryptCATEnumerateMember
  20. // CryptCATEnumerateAttr
  21. //
  22. // *** local functions ***
  23. //
  24. // CryptCATCreateStore
  25. // CryptCATOpenStore
  26. // FillNameValue
  27. // CatalogCheckForDuplicateMember
  28. //
  29. // History: 29-Apr-1997 pberkman created
  30. //
  31. //--------------------------------------------------------------------------
  32. #include "global.hxx"
  33. #include "mscat32.h"
  34. BOOL CryptCATCreateStore(CRYPTCATSTORE *pCat, LPWSTR pwszCatFile);
  35. BOOL CryptCATOpenStore(CRYPTCATSTORE *pCat, LPWSTR pwszCatFile);
  36. BOOL FillNameValue(CRYPTCATSTORE *pCatStore, CRYPTCATATTRIBUTE *pAttr);
  37. BOOL CatalogCheckForDuplicateMember(Stack_ *pMembers, WCHAR *pwszReferenceTag);
  38. /////////////////////////////////////////////////////////////////////////////
  39. //
  40. // Exported Functions
  41. //
  42. HANDLE WINAPI CryptCATOpen(LPWSTR pwszCatFile, DWORD fdwOpenFlags, HCRYPTPROV hProv,
  43. DWORD dwPublicVersion, DWORD dwEncodingType)
  44. {
  45. CRYPTCATSTORE *pCatStore;
  46. if (!(pwszCatFile))
  47. {
  48. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  49. return((HANDLE)INVALID_HANDLE_VALUE);
  50. }
  51. if (!(pCatStore = (CRYPTCATSTORE *)CatalogNew(sizeof(CRYPTCATSTORE))))
  52. {
  53. return((HANDLE)INVALID_HANDLE_VALUE);
  54. }
  55. memset(pCatStore, 0x00, sizeof(CRYPTCATSTORE));
  56. pCatStore->cbStruct = sizeof(CRYPTCATSTORE);
  57. pCatStore->hProv = hProv;
  58. pCatStore->dwPublicVersion = (dwPublicVersion) ? dwPublicVersion : 0x00000100;
  59. pCatStore->dwEncodingType = (dwEncodingType) ? dwEncodingType :
  60. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
  61. if (!(hProv))
  62. {
  63. pCatStore->hProv = I_CryptGetDefaultCryptProv(0); // get the default and DONT RELEASE IT!!!!
  64. if (!(pCatStore->hProv))
  65. {
  66. goto ErrorReturn;
  67. }
  68. }
  69. if (fdwOpenFlags & CRYPTCAT_OPEN_CREATENEW)
  70. {
  71. if (!CryptCATCreateStore(pCatStore, pwszCatFile))
  72. {
  73. goto ErrorReturn;
  74. }
  75. }
  76. else
  77. {
  78. BOOL fRet;
  79. fRet = CryptCATOpenStore(pCatStore, pwszCatFile);
  80. if (!(fRet) && (fdwOpenFlags & CRYPTCAT_OPEN_ALWAYS))
  81. {
  82. fRet = CryptCATCreateStore(pCatStore, pwszCatFile);
  83. }
  84. if (fRet == FALSE)
  85. {
  86. goto ErrorReturn;
  87. }
  88. }
  89. return((HANDLE)pCatStore);
  90. ErrorReturn:
  91. DWORD dwLastError;
  92. dwLastError = GetLastError();
  93. CryptCATClose((HANDLE)pCatStore);
  94. SetLastError(dwLastError);
  95. return(INVALID_HANDLE_VALUE);
  96. }
  97. BOOL WINAPI CryptCATClose(HANDLE hCatalog)
  98. {
  99. if (!(hCatalog) ||
  100. (hCatalog == INVALID_HANDLE_VALUE))
  101. {
  102. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  103. return(FALSE);
  104. }
  105. CRYPTCATSTORE *pCatStore;
  106. pCatStore = (CRYPTCATSTORE *)hCatalog;
  107. __try
  108. {
  109. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  110. {
  111. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  112. return(FALSE);
  113. }
  114. }
  115. __except(EXCEPTION_EXECUTE_HANDLER)
  116. {
  117. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  118. return(FALSE);
  119. }
  120. BOOL fRet;
  121. DWORD dwlerr;
  122. dwlerr = GetLastError();
  123. fRet = TRUE;
  124. //
  125. // file name
  126. //
  127. DELETE_OBJECT(pCatStore->pwszP7File);
  128. //
  129. // attributes
  130. //
  131. if (_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hAttrs))
  132. {
  133. if (pCatStore->hAttrs)
  134. {
  135. Stack_ *ps;
  136. DWORD cStack;
  137. CRYPTCATATTRIBUTE *pAttr;
  138. ps = (Stack_ *)pCatStore->hAttrs;
  139. cStack = 0;
  140. while (pAttr = (CRYPTCATATTRIBUTE *)ps->Get(cStack))
  141. {
  142. CatalogFreeAttribute(pAttr);
  143. cStack++;
  144. }
  145. DELETE_OBJECT(ps);
  146. pCatStore->hAttrs = NULL;
  147. }
  148. }
  149. //
  150. // check hReserved to see if we used pStack
  151. //
  152. if (pCatStore->hReserved)
  153. {
  154. Stack_ *pStack;
  155. CRYPTCATMEMBER *pCatMember;
  156. DWORD dwCurPos;
  157. pStack = (Stack_ *)pCatStore->hReserved;
  158. dwCurPos = 0;
  159. while (pCatMember = (CRYPTCATMEMBER *)pStack->Get(dwCurPos))
  160. {
  161. CatalogFreeMember(pCatMember);
  162. dwCurPos++;
  163. }
  164. DELETE_OBJECT(pStack);
  165. pCatStore->hReserved = NULL;
  166. }
  167. pCatStore->cbStruct = 0; // just in case they try to re-open it!
  168. DELETE_OBJECT(pCatStore);
  169. //
  170. // restore last error
  171. //
  172. SetLastError(dwlerr);
  173. return(fRet);
  174. }
  175. CRYPTCATSTORE * WINAPI CryptCATStoreFromHandle(IN HANDLE hCatalog)
  176. {
  177. return((CRYPTCATSTORE *)hCatalog);
  178. }
  179. HANDLE WINAPI CryptCATHandleFromStore(IN CRYPTCATSTORE *pCatStore)
  180. {
  181. return((HANDLE)pCatStore);
  182. }
  183. BOOL WINAPI CryptCATPersistStore(IN HANDLE hCatalog)
  184. {
  185. CRYPTCATSTORE *pStore;
  186. if (!(hCatalog) ||
  187. (hCatalog == INVALID_HANDLE_VALUE))
  188. {
  189. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  190. return(FALSE);
  191. }
  192. pStore = CryptCATStoreFromHandle(hCatalog);
  193. if (!(pStore->pwszP7File))
  194. {
  195. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  196. return(FALSE);
  197. }
  198. return(CatalogSaveP7UData(pStore));
  199. }
  200. CRYPTCATATTRIBUTE * WINAPI CryptCATGetCatAttrInfo(HANDLE hCatalog, LPWSTR pwszReferenceTag)
  201. {
  202. CRYPTCATATTRIBUTE *pAttr;
  203. CRYPTCATSTORE *pCatStore;
  204. pAttr = NULL;
  205. if (!(hCatalog) ||
  206. (hCatalog == INVALID_HANDLE_VALUE) ||
  207. !(pwszReferenceTag))
  208. {
  209. goto ErrorInvalidParam;
  210. }
  211. pCatStore = CryptCATStoreFromHandle(hCatalog);
  212. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hAttrs)))
  213. {
  214. goto ErrorInvalidParam;
  215. }
  216. while (pAttr = CryptCATEnumerateCatAttr(hCatalog, pAttr))
  217. {
  218. if (pAttr->pwszReferenceTag)
  219. {
  220. if (_wcsicmp(pwszReferenceTag, pAttr->pwszReferenceTag) == 0)
  221. {
  222. goto CommonReturn;
  223. }
  224. }
  225. }
  226. goto ErrorNotFound;
  227. CommonReturn:
  228. ErrorReturn:
  229. return(pAttr);
  230. SET_ERROR_VAR_EX(DBG_SS, ErrorInvalidParam, ERROR_INVALID_PARAMETER);
  231. SET_ERROR_VAR_EX(DBG_SS, ErrorNotFound, CRYPT_E_NOT_FOUND);
  232. }
  233. CRYPTCATATTRIBUTE * WINAPI CryptCATPutCatAttrInfo(HANDLE hCatalog, LPWSTR pwszReferenceTag,
  234. DWORD dwAttrTypeAndAction, DWORD cbData,
  235. BYTE *pbData)
  236. {
  237. CRYPTCATATTRIBUTE *pAttr;
  238. pAttr = NULL;
  239. if (!(hCatalog) ||
  240. (hCatalog == INVALID_HANDLE_VALUE) ||
  241. !(pwszReferenceTag) ||
  242. ((cbData > 0) && !(pbData)))
  243. {
  244. goto ErrorInvalidParam;
  245. }
  246. if (!(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATABASE64) &&
  247. !(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAASCII) &&
  248. !(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAREPLACE))
  249. {
  250. goto ErrorInvalidParam;
  251. }
  252. CRYPTCATSTORE *pCatStore;
  253. pCatStore = (CRYPTCATSTORE *)hCatalog;
  254. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hAttrs)))
  255. {
  256. goto ErrorInvalidParam;
  257. }
  258. Stack_ *pStack;
  259. if (!(pCatStore->hAttrs))
  260. {
  261. pStack = new Stack_(&MSCAT_CriticalSection);
  262. if (!(pStack))
  263. {
  264. goto ErrorMemory;
  265. }
  266. pCatStore->hAttrs = (HANDLE)pStack;
  267. }
  268. pStack = (Stack_ *)pCatStore->hAttrs;
  269. if (!(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAREPLACE))
  270. {
  271. if (!(pAttr = (CRYPTCATATTRIBUTE *)pStack->Add(sizeof(CRYPTCATATTRIBUTE))))
  272. {
  273. goto StackError;
  274. }
  275. memset(pAttr, 0x00, sizeof(CRYPTCATATTRIBUTE));
  276. pAttr->cbStruct = sizeof(CRYPTCATATTRIBUTE);
  277. if (!(pAttr->pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(pwszReferenceTag) + 1) *
  278. sizeof(WCHAR))))
  279. {
  280. goto ErrorMemory;
  281. }
  282. wcscpy(pAttr->pwszReferenceTag, pwszReferenceTag);
  283. pAttr->dwAttrTypeAndAction = dwAttrTypeAndAction;
  284. if (pbData)
  285. {
  286. if (dwAttrTypeAndAction & CRYPTCAT_ATTR_DATABASE64)
  287. {
  288. CryptStringToBinaryW((WCHAR *)pbData, cbData / sizeof(WCHAR), CRYPT_STRING_ANY, NULL, &pAttr->cbValue, NULL, NULL);
  289. if (pAttr->cbValue < 1)
  290. {
  291. goto DecodeError;
  292. }
  293. if (!(pAttr->pbValue = (BYTE *)CatalogNew(pAttr->cbValue)))
  294. {
  295. pAttr->cbValue = 0;
  296. goto ErrorMemory;
  297. }
  298. if (!CryptStringToBinaryW((WCHAR *)pbData, cbData / sizeof(WCHAR), CRYPT_STRING_ANY, pAttr->pbValue, &pAttr->cbValue, NULL, NULL))
  299. {
  300. goto DecodeError;
  301. }
  302. }
  303. else if (dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAASCII)
  304. {
  305. if (!(pAttr->pbValue = (BYTE *)CatalogNew(cbData)))
  306. {
  307. goto ErrorMemory;
  308. }
  309. memcpy(pAttr->pbValue, pbData, cbData);
  310. pAttr->cbValue = cbData;
  311. }
  312. }
  313. }
  314. else
  315. {
  316. DWORD pos = 0;
  317. pAttr = (CRYPTCATATTRIBUTE *) pStack->Get(pos++, NULL);
  318. while ((pAttr != NULL) && (wcscmp(pAttr->pwszReferenceTag, pwszReferenceTag) != 0))
  319. {
  320. pAttr = (CRYPTCATATTRIBUTE *) pStack->Get(pos++, NULL);
  321. }
  322. if (pAttr != NULL)
  323. {
  324. DELETE_OBJECT(pAttr->pbValue);
  325. pAttr->pbValue = NULL;
  326. pAttr->cbValue = 0;
  327. if (!(pAttr->pbValue = (BYTE *)CatalogNew(cbData)))
  328. {
  329. goto ErrorMemory;
  330. }
  331. memcpy(pAttr->pbValue, pbData, cbData);
  332. pAttr->cbValue = cbData;
  333. }
  334. }
  335. // CommonReturn:
  336. ErrorReturn:
  337. return(pAttr);
  338. TRACE_ERROR_EX(DBG_SS, StackError);
  339. TRACE_ERROR_EX(DBG_SS, DecodeError);
  340. SET_ERROR_VAR_EX(DBG_SS, ErrorMemory, ERROR_NOT_ENOUGH_MEMORY);
  341. SET_ERROR_VAR_EX(DBG_SS, ErrorInvalidParam, ERROR_INVALID_PARAMETER);
  342. }
  343. CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateCatAttr(HANDLE hCatalog, CRYPTCATATTRIBUTE *pPrevAttr)
  344. {
  345. CRYPTCATATTRIBUTE *pAttr;
  346. pAttr = NULL;
  347. if (!(hCatalog) ||
  348. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE))
  349. {
  350. goto ErrorInvalidParam;
  351. }
  352. CRYPTCATSTORE *pStore;
  353. pStore = CryptCATStoreFromHandle(hCatalog);
  354. if (!(_ISINSTRUCT(CRYPTCATSTORE, pStore->cbStruct, hAttrs)))
  355. {
  356. goto ErrorInvalidParam;
  357. }
  358. DWORD dwNext;
  359. dwNext = 0;
  360. if (pPrevAttr)
  361. {
  362. if (!(_ISINSTRUCT(CRYPTCATATTRIBUTE, pPrevAttr->cbStruct, dwReserved)))
  363. {
  364. goto ErrorInvalidParam;
  365. }
  366. dwNext = pPrevAttr->dwReserved + 1;
  367. }
  368. if (pStore->hAttrs)
  369. {
  370. Stack_ *ps;
  371. ps = (Stack_ *)pStore->hAttrs;
  372. if (pAttr = (CRYPTCATATTRIBUTE *)ps->Get(dwNext))
  373. {
  374. //
  375. // save our "id" for next time
  376. //
  377. pAttr->dwReserved = dwNext;
  378. //
  379. // Done!
  380. //
  381. goto CommonReturn;
  382. }
  383. }
  384. goto ErrorNotFound;
  385. CommonReturn:
  386. ErrorReturn:
  387. return(pAttr);
  388. SET_ERROR_VAR_EX(DBG_SS, ErrorInvalidParam, ERROR_INVALID_PARAMETER);
  389. SET_ERROR_VAR_EX(DBG_SS, ErrorNotFound, CRYPT_E_NOT_FOUND);
  390. }
  391. CRYPTCATMEMBER * WINAPI CryptCATGetMemberInfo(IN HANDLE hCatalog,
  392. IN LPWSTR pwszReferenceTag)
  393. {
  394. if (!(hCatalog) ||
  395. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE) ||
  396. !(pwszReferenceTag))
  397. {
  398. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  399. return(NULL);
  400. }
  401. CRYPTCATSTORE *pCatStore;
  402. CRYPTCATMEMBER *pCatMember;
  403. pCatStore = (CRYPTCATSTORE *)hCatalog;
  404. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  405. {
  406. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  407. return(NULL);
  408. }
  409. pCatMember = NULL;
  410. if (pCatStore->hReserved)
  411. {
  412. Stack_ *ps;
  413. ps = (Stack_ *)pCatStore->hReserved;
  414. if (pCatMember = (CRYPTCATMEMBER *)ps->Get(WVT_OFFSETOF(CRYPTCATMEMBER, pwszReferenceTag),
  415. sizeof(WCHAR *),
  416. STACK_SORTTYPE_PWSZ,
  417. pwszReferenceTag))
  418. {
  419. if (!(pCatMember->pIndirectData))
  420. {
  421. CatalogReallyDecodeIndirectData(pCatStore, pCatMember, &pCatMember->sEncodedIndirectData);
  422. }
  423. if ((pCatMember->gSubjectType.Data1 == 0) &&
  424. (pCatMember->gSubjectType.Data2 == 0) &&
  425. (pCatMember->gSubjectType.Data3 == 0))
  426. {
  427. CatalogReallyDecodeMemberInfo(pCatStore, pCatMember, &pCatMember->sEncodedMemberInfo);
  428. }
  429. return(pCatMember);
  430. }
  431. }
  432. return(NULL);
  433. }
  434. CRYPTCATMEMBER * WINAPI CryptCATPutMemberInfo(HANDLE hCatalog,
  435. LPWSTR pwszFileName,
  436. LPWSTR pwszReferenceTag,
  437. GUID *pgSubjectType,
  438. DWORD dwCertVersion,
  439. DWORD cbIndirectData,
  440. BYTE *pbIndirectData)
  441. {
  442. if (!(hCatalog) ||
  443. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE) ||
  444. !(pwszReferenceTag) ||
  445. !(pgSubjectType) ||
  446. !(pbIndirectData) ||
  447. (wcslen(pwszReferenceTag) > CRYPTCAT_MAX_MEMBERTAG))
  448. {
  449. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  450. return(NULL);
  451. }
  452. CRYPTCATSTORE *pCatStore;
  453. pCatStore = (CRYPTCATSTORE *)hCatalog;
  454. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  455. {
  456. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  457. return(NULL);
  458. }
  459. Stack_ *pStack;
  460. CRYPTCATMEMBER *pCatMember;
  461. if (!(pCatStore->hReserved))
  462. {
  463. pStack = new Stack_(&MSCAT_CriticalSection);
  464. if (!(pStack))
  465. {
  466. assert(0);
  467. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  468. return(NULL);
  469. }
  470. pCatStore->hReserved = (HANDLE)pStack;
  471. }
  472. //
  473. // the following is commented out -- too slow!!!!
  474. //
  475. // else if (CatalogCheckForDuplicateMember((Stack_ *)pCatStore->hReserved, pwszReferenceTag))
  476. // {
  477. // SetLastError(CRYPT_E_EXISTS);
  478. // return(NULL);
  479. // }
  480. pStack = (Stack_ *)pCatStore->hReserved;
  481. if (!(pCatMember = (CRYPTCATMEMBER *)pStack->Add(sizeof(CRYPTCATMEMBER))))
  482. {
  483. return(NULL);
  484. }
  485. memset(pCatMember, 0x00, sizeof(CRYPTCATMEMBER));
  486. pCatMember->cbStruct = sizeof(CRYPTCATMEMBER);
  487. if (pwszFileName)
  488. {
  489. if (!(pCatMember->pwszFileName = (LPWSTR)CatalogNew((wcslen(pwszFileName) + 1) *
  490. sizeof(WCHAR))))
  491. {
  492. return(NULL);
  493. }
  494. wcscpy(pCatMember->pwszFileName, pwszFileName);
  495. }
  496. if (!(pCatMember->pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(pwszReferenceTag) + 1) *
  497. sizeof(WCHAR))))
  498. {
  499. return(NULL);
  500. }
  501. wcscpy(pCatMember->pwszReferenceTag, pwszReferenceTag);
  502. if (cbIndirectData > 0)
  503. {
  504. SIP_INDIRECT_DATA *pInd;
  505. if (!(pCatMember->pIndirectData = (SIP_INDIRECT_DATA *)CatalogNew(sizeof(SIP_INDIRECT_DATA))))
  506. {
  507. return(NULL);
  508. }
  509. memset(pCatMember->pIndirectData, 0x00, sizeof(SIP_INDIRECT_DATA));
  510. pInd = (SIP_INDIRECT_DATA *)pbIndirectData;
  511. if (pInd->Data.pszObjId)
  512. {
  513. if (!(pCatMember->pIndirectData->Data.pszObjId =
  514. (LPSTR)CatalogNew(strlen(pInd->Data.pszObjId) + 1)))
  515. {
  516. return(NULL);
  517. }
  518. strcpy(pCatMember->pIndirectData->Data.pszObjId, pInd->Data.pszObjId);
  519. }
  520. if (pInd->Data.Value.cbData > 0)
  521. {
  522. if (!(pCatMember->pIndirectData->Data.Value.pbData =
  523. (BYTE *)CatalogNew(pInd->Data.Value.cbData)))
  524. {
  525. return(NULL);
  526. }
  527. memcpy(pCatMember->pIndirectData->Data.Value.pbData,
  528. pInd->Data.Value.pbData, pInd->Data.Value.cbData);
  529. }
  530. pCatMember->pIndirectData->Data.Value.cbData = pInd->Data.Value.cbData;
  531. if (!(pCatMember->pIndirectData->DigestAlgorithm.pszObjId =
  532. (LPSTR)CatalogNew(strlen(pInd->DigestAlgorithm.pszObjId) + 1)))
  533. {
  534. return(NULL);
  535. }
  536. strcpy(pCatMember->pIndirectData->DigestAlgorithm.pszObjId,
  537. pInd->DigestAlgorithm.pszObjId);
  538. if (!(pCatMember->pIndirectData->Digest.pbData =
  539. (BYTE *)CatalogNew(pInd->Digest.cbData)))
  540. {
  541. return(NULL);
  542. }
  543. memcpy(pCatMember->pIndirectData->Digest.pbData,
  544. pInd->Digest.pbData, pInd->Digest.cbData);
  545. pCatMember->pIndirectData->Digest.cbData = pInd->Digest.cbData;
  546. }
  547. memcpy(&pCatMember->gSubjectType, pgSubjectType, sizeof(GUID));
  548. pCatMember->dwCertVersion = dwCertVersion;
  549. return(pCatMember);
  550. }
  551. BOOL WINAPI CryptCATVerifyMember(HANDLE hCatalog,
  552. CRYPTCATMEMBER *pCatMember,
  553. HANDLE hFileOrMemory)
  554. {
  555. if (!(hCatalog) ||
  556. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE) ||
  557. !(pCatMember) ||
  558. !(_ISINSTRUCT(CRYPTCATMEMBER, pCatMember->cbStruct, hReserved)) ||
  559. !(hFileOrMemory) ||
  560. (hFileOrMemory == INVALID_HANDLE_VALUE))
  561. {
  562. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  563. return(FALSE);
  564. }
  565. CRYPTCATSTORE *pCatStore;
  566. pCatStore = (CRYPTCATSTORE *)hCatalog;
  567. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  568. {
  569. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  570. return(FALSE);
  571. }
  572. // TBDTBD!!!
  573. return(FALSE);
  574. }
  575. CRYPTCATATTRIBUTE * WINAPI CryptCATGetAttrInfo(HANDLE hCatalog,
  576. CRYPTCATMEMBER *pCatMember,
  577. LPWSTR pwszReferenceTag)
  578. {
  579. if (!(hCatalog) ||
  580. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE) ||
  581. !(pCatMember) ||
  582. !(_ISINSTRUCT(CRYPTCATMEMBER, pCatMember->cbStruct, hReserved)) ||
  583. !(pwszReferenceTag))
  584. {
  585. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  586. return(NULL);
  587. }
  588. CRYPTCATSTORE *pCatStore;
  589. CRYPTCATATTRIBUTE *pCatAttr;
  590. pCatStore = (CRYPTCATSTORE *)hCatalog;
  591. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  592. {
  593. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  594. return(NULL);
  595. }
  596. pCatAttr = NULL;
  597. while (pCatAttr = CryptCATEnumerateAttr(hCatalog, pCatMember, pCatAttr))
  598. {
  599. if (pCatAttr->pwszReferenceTag)
  600. {
  601. if (_wcsicmp(pwszReferenceTag, pCatAttr->pwszReferenceTag) == 0)
  602. {
  603. return(pCatAttr);
  604. }
  605. }
  606. }
  607. SetLastError(CRYPT_E_NOT_FOUND);
  608. return(NULL);
  609. }
  610. CRYPTCATATTRIBUTE * WINAPI CryptCATPutAttrInfo(HANDLE hCatalog,
  611. CRYPTCATMEMBER *pCatMember,
  612. LPWSTR pwszReferenceTag,
  613. DWORD dwAttrTypeAndAction,
  614. DWORD cbData,
  615. BYTE *pbData)
  616. {
  617. if (!(hCatalog) ||
  618. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE) ||
  619. !(pCatMember) ||
  620. !(_ISINSTRUCT(CRYPTCATMEMBER, pCatMember->cbStruct, hReserved)) ||
  621. !(pwszReferenceTag) ||
  622. ((cbData > 0) && !(pbData)))
  623. {
  624. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  625. return(NULL);
  626. }
  627. if (!(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATABASE64) &&
  628. !(dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAASCII))
  629. {
  630. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  631. return(NULL);
  632. }
  633. CRYPTCATSTORE *pCatStore;
  634. pCatStore = (CRYPTCATSTORE *)hCatalog;
  635. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  636. {
  637. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  638. return(NULL);
  639. }
  640. Stack_ *pStack;
  641. CRYPTCATATTRIBUTE *pAttr;
  642. if (!(pCatMember->hReserved))
  643. {
  644. pStack = new Stack_(&MSCAT_CriticalSection);
  645. if (!(pStack))
  646. {
  647. assert(0);
  648. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  649. return(NULL);
  650. }
  651. pCatMember->hReserved = (HANDLE)pStack;
  652. }
  653. pStack = (Stack_ *)pCatMember->hReserved;
  654. if (!(pAttr = (CRYPTCATATTRIBUTE *)pStack->Add(sizeof(CRYPTCATATTRIBUTE))))
  655. {
  656. return(NULL);
  657. }
  658. memset(pAttr, 0x00, sizeof(CRYPTCATATTRIBUTE));
  659. pAttr->cbStruct = sizeof(CRYPTCATATTRIBUTE);
  660. if (!(pAttr->pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(pwszReferenceTag) + 1) *
  661. sizeof(WCHAR))))
  662. {
  663. return(NULL);
  664. }
  665. wcscpy(pAttr->pwszReferenceTag, pwszReferenceTag);
  666. pAttr->dwAttrTypeAndAction = dwAttrTypeAndAction;
  667. if (pbData)
  668. {
  669. if (dwAttrTypeAndAction & CRYPTCAT_ATTR_DATABASE64)
  670. {
  671. CryptStringToBinaryW((WCHAR *)pbData, cbData / sizeof(WCHAR), CRYPT_STRING_ANY, NULL, &pAttr->cbValue, NULL, NULL);
  672. if (pAttr->cbValue < 1)
  673. {
  674. return(NULL);
  675. }
  676. if (!(pAttr->pbValue = (BYTE *)CatalogNew(pAttr->cbValue)))
  677. {
  678. pAttr->cbValue = 0;
  679. return(NULL);
  680. }
  681. memset(pAttr->pbValue, 0x00, pAttr->cbValue);
  682. if (!CryptStringToBinaryW((WCHAR *)pbData, cbData / sizeof(WCHAR), CRYPT_STRING_ANY, pAttr->pbValue, &pAttr->cbValue, NULL, NULL))
  683. {
  684. return(NULL);
  685. }
  686. }
  687. else if (dwAttrTypeAndAction & CRYPTCAT_ATTR_DATAASCII)
  688. {
  689. if (!(pAttr->pbValue = (BYTE *)CatalogNew(cbData)))
  690. {
  691. return(NULL);
  692. }
  693. memcpy(pAttr->pbValue, pbData, cbData);
  694. pAttr->cbValue = cbData;
  695. }
  696. }
  697. return(pAttr);
  698. }
  699. CRYPTCATMEMBER * WINAPI CryptCATEnumerateMember(HANDLE hCatalog,
  700. CRYPTCATMEMBER *pPrevMember)
  701. {
  702. if (!(hCatalog) ||
  703. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE))
  704. {
  705. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  706. return(NULL);
  707. }
  708. CRYPTCATSTORE *pCatStore;
  709. pCatStore = (CRYPTCATSTORE *)hCatalog;
  710. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  711. {
  712. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  713. return(NULL);
  714. }
  715. DWORD dwNext;
  716. dwNext = 0;
  717. if (pPrevMember)
  718. {
  719. if (!(_ISINSTRUCT(CRYPTCATMEMBER, pPrevMember->cbStruct, hReserved)))
  720. {
  721. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  722. return(NULL);
  723. }
  724. dwNext = pPrevMember->dwReserved + 1;
  725. }
  726. if (pCatStore->hReserved)
  727. {
  728. CRYPTCATMEMBER *pCatMember;
  729. Stack_ *ps;
  730. ps = (Stack_ *)pCatStore->hReserved;
  731. if (pCatMember = (CRYPTCATMEMBER *)ps->Get(dwNext))
  732. {
  733. //
  734. // save our "id" for next time
  735. //
  736. pCatMember->dwReserved = dwNext;
  737. if (!(pCatMember->pIndirectData))
  738. {
  739. CatalogReallyDecodeIndirectData(pCatStore, pCatMember, &pCatMember->sEncodedIndirectData);
  740. }
  741. if ((pCatMember->gSubjectType.Data1 == 0) &&
  742. (pCatMember->gSubjectType.Data2 == 0) &&
  743. (pCatMember->gSubjectType.Data3 == 0))
  744. {
  745. CatalogReallyDecodeMemberInfo(pCatStore, pCatMember, &pCatMember->sEncodedMemberInfo);
  746. }
  747. //
  748. // Done!
  749. //
  750. return(pCatMember);
  751. }
  752. }
  753. return(NULL);
  754. }
  755. CRYPTCATATTRIBUTE * WINAPI CryptCATEnumerateAttr(HANDLE hCatalog,
  756. CRYPTCATMEMBER *pCatMember,
  757. CRYPTCATATTRIBUTE *pPrevAttr)
  758. {
  759. if (!(hCatalog) ||
  760. (hCatalog == (HANDLE)INVALID_HANDLE_VALUE))
  761. {
  762. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  763. return(NULL);
  764. }
  765. CRYPTCATSTORE *pCatStore;
  766. pCatStore = (CRYPTCATSTORE *)hCatalog;
  767. if (!(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)))
  768. {
  769. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  770. return(NULL);
  771. }
  772. DWORD dwNext;
  773. dwNext = 0;
  774. if (pPrevAttr)
  775. {
  776. if (!(_ISINSTRUCT(CRYPTCATATTRIBUTE, pPrevAttr->cbStruct, dwReserved)))
  777. {
  778. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  779. return(NULL);
  780. }
  781. dwNext = pPrevAttr->dwReserved + 1;
  782. }
  783. if (!(pCatStore) ||
  784. !(_ISINSTRUCT(CRYPTCATSTORE, pCatStore->cbStruct, hReserved)) ||
  785. !(pCatMember) ||
  786. !(_ISINSTRUCT(CRYPTCATMEMBER, pCatMember->cbStruct, hReserved)))
  787. {
  788. SetLastError((DWORD)ERROR_INVALID_PARAMETER);
  789. return(NULL);
  790. }
  791. if (pCatMember->hReserved)
  792. {
  793. CRYPTCATATTRIBUTE *pCatAttr;
  794. Stack_ *ps;
  795. ps = (Stack_ *)pCatMember->hReserved;
  796. if (pCatAttr = (CRYPTCATATTRIBUTE *)ps->Get(dwNext))
  797. {
  798. //
  799. // save our "id" for next time
  800. //
  801. pCatAttr->dwReserved = dwNext;
  802. //
  803. // Done!
  804. //
  805. return(pCatAttr);
  806. }
  807. }
  808. return(NULL);
  809. }
  810. /////////////////////////////////////////////////////////////////////////////
  811. //
  812. // Local Functions
  813. //
  814. BOOL CryptCATCreateStore(CRYPTCATSTORE *pCatStore, LPWSTR pwszCatFile)
  815. {
  816. HANDLE hFile;
  817. DWORD lErr;
  818. lErr = GetLastError();
  819. if ((hFile = CreateFileU(pwszCatFile,
  820. GENERIC_WRITE | GENERIC_READ,
  821. 0, // no sharing!!
  822. NULL,
  823. CREATE_ALWAYS,
  824. FILE_ATTRIBUTE_NORMAL,
  825. NULL)) == INVALID_HANDLE_VALUE)
  826. {
  827. return(FALSE);
  828. }
  829. CloseHandle(hFile);
  830. SetLastError(lErr);
  831. return(CryptCATOpenStore(pCatStore, pwszCatFile));
  832. }
  833. BOOL CryptCATOpenStore(CRYPTCATSTORE *pCatStore, LPWSTR pwszCatFile)
  834. {
  835. if (!(pCatStore->pwszP7File = (LPWSTR)CatalogNew((wcslen(pwszCatFile) + 1) *
  836. sizeof(WCHAR))))
  837. {
  838. return(FALSE);
  839. }
  840. wcscpy(pCatStore->pwszP7File, pwszCatFile);
  841. return(CatalogLoadFileData(pCatStore));
  842. }