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.

1690 lines
48 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: mscdfapi.cpp
  8. //
  9. // Contents: Microsoft Internet Security Catalog Utilities
  10. //
  11. // Functions: CryptCATCDFOpen
  12. // CryptCATCDFClose
  13. // CryptCATCDFEnumMembers
  14. // CryptCATCDFEnumAttributes
  15. //
  16. // *** local functions ***
  17. //
  18. // CDFGetAttributes
  19. // CDFTextToGUID
  20. // CDFPositionAtGroupTag
  21. // CDFGetNextMember
  22. // CDFGetParam
  23. // CDFGetLine
  24. // CDFSplitAttrLine
  25. // CDFEOLOut
  26. // CDFCheckOID
  27. // CDFCalcIndirectData
  28. //
  29. // History: 01-May-1997 pberkman created
  30. //
  31. //--------------------------------------------------------------------------
  32. #include "global.hxx"
  33. #include <objbase.h>
  34. #include "mscat32.h"
  35. #include "sipguids.h"
  36. #define STRSAFE_NO_DEPRECATE
  37. #include <strsafe.h>
  38. void CDFTextToGUID(LPWSTR pwszText, GUID *pgBin, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError);
  39. BOOL CDFPositionAtGroupTag(CRYPTCATCDF *pCDF, LPWSTR pwszTag);
  40. BOOL CDFPositionAtLastMember(CRYPTCATCDF *pCDF);
  41. BOOL CDFGetNextMember(CRYPTCATCDF *pCDF, LPWSTR pwszMember, int cchMember, LPWSTR pwszLastMember);
  42. BOOL CDFGetParam(CRYPTCATCDF *pCDF, LPWSTR pwszGroup, LPWSTR pwszItem,
  43. LPWSTR pwszDefault, LPWSTR *ppwszRet, LPWSTR pwszMemberTag);
  44. DWORD CDFGetLine(CRYPTCATCDF *pCDF, LPWSTR pwszLineBuf, DWORD dwMaxRead);
  45. BOOL CDFSplitAttrLine(LPWSTR pwszLine, DWORD *pdwType, LPWSTR *pwszOID,
  46. LPWSTR *pwszValue, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError);
  47. void CDFEOLOut(WCHAR *pwsz, DWORD ccLen);
  48. BOOL CDFCalcIndirectData(CRYPTCATCDF *pCDF, WCHAR *pwszFileName, GUID *pgSubjectType, DWORD *pcbIndirectData,
  49. BYTE **pIndirectData, DWORD *pdwCertVersion, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError);
  50. BOOL CDFCheckOID(LPWSTR pwszOID, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError);
  51. #define MAX_CDF_LINE_LEN 512
  52. #define CAT_HEADER_TAG L"[CatalogHeader]"
  53. #define CAT_HEADER_NAME_TAG L"Name"
  54. #define CAT_HEADER_RESDIR_TAG L"ResultDir"
  55. #define CAT_HEADER_VERSION_TAG L"PublicVersion"
  56. #define CAT_HEADER_ENCODETYPE_TAG L"EncodingType"
  57. #define CAT_HEADER_ATTR_TAG L"CATATTR"
  58. #define CAT_MEMBER_TAG L"[CatalogFiles]"
  59. #define CAT_MEMBER_ALTSIP_TAG L"ALTSIPID"
  60. #define CAT_MEMBER_ATTR_TAG L"ATTR"
  61. #define CAT_MEMBER_HASH_TAG L"<HASH>"
  62. /////////////////////////////////////////////////////////////////////////////
  63. //
  64. // Exported Functions
  65. //
  66. CRYPTCATCDF * WINAPI CryptCATCDFOpen(LPWSTR pwszFilePath,
  67. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  68. {
  69. CRYPTCATCDF *pCDF;
  70. HANDLE hFile;
  71. if (!(pwszFilePath))
  72. {
  73. SetLastError(ERROR_INVALID_PARAMETER);
  74. return(NULL);
  75. }
  76. if ((hFile = CreateFileU(pwszFilePath,
  77. GENERIC_READ,
  78. FILE_SHARE_READ,
  79. NULL,
  80. OPEN_EXISTING,
  81. FILE_ATTRIBUTE_NORMAL,
  82. NULL)) == INVALID_HANDLE_VALUE)
  83. {
  84. return(NULL);
  85. }
  86. if (!(pCDF = (CRYPTCATCDF *)CatalogNew(sizeof(CRYPTCATCDF))))
  87. {
  88. return(NULL);
  89. }
  90. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  91. LPWSTR pwsz;
  92. memset(pCDF, 0x00, sizeof(CRYPTCATCDF));
  93. pCDF->cbStruct = sizeof(CRYPTCATCDF);
  94. pCDF->hFile = hFile;
  95. //
  96. // Name
  97. //
  98. if (pwsz = wcsrchr(pwszFilePath, L'\\'))
  99. {
  100. if (FAILED(StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, &pwsz[1])))
  101. {
  102. CloseHandle(hFile);
  103. delete pCDF;
  104. return(NULL);
  105. }
  106. }
  107. else
  108. {
  109. if (FAILED(StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszFilePath)))
  110. {
  111. CloseHandle(hFile);
  112. delete pCDF;
  113. return(NULL);
  114. }
  115. }
  116. LPWSTR pwszStoreName;
  117. pwszStoreName = NULL;
  118. if (!(CDFPositionAtGroupTag(pCDF, CAT_HEADER_TAG)))
  119. {
  120. CloseHandle(hFile);
  121. delete pCDF;
  122. if (pfnParseError)
  123. {
  124. pfnParseError(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND, CAT_HEADER_TAG);
  125. }
  126. return(NULL);
  127. }
  128. if (!(CDFGetParam(pCDF, CAT_HEADER_TAG, CAT_HEADER_NAME_TAG, &wszRetValue[0], &pwszStoreName, NULL)))
  129. {
  130. DELETE_OBJECT(pwszStoreName);
  131. CloseHandle(hFile);
  132. delete pCDF;
  133. if (pfnParseError)
  134. {
  135. pfnParseError(CRYPTCAT_E_AREA_HEADER, CRYPTCAT_E_CDF_TAGNOTFOUND, CAT_HEADER_TAG);
  136. }
  137. return(NULL);
  138. }
  139. //
  140. // ResultDir
  141. //
  142. CDFPositionAtGroupTag(pCDF, CAT_HEADER_TAG);
  143. CDFGetParam(pCDF, CAT_HEADER_TAG, CAT_HEADER_RESDIR_TAG, NULL, &pCDF->pwszResultDir, NULL);
  144. //
  145. // actual file
  146. //
  147. DWORD cw;
  148. LPWSTR pwszFile = NULL;
  149. cw = wcslen( pwszStoreName );
  150. if ( pCDF->pwszResultDir != NULL )
  151. {
  152. cw += wcslen( pCDF->pwszResultDir );
  153. }
  154. cw += wcslen( CRYPTCAT_FILEEXT );
  155. cw += 3;
  156. pwszFile = new WCHAR [ cw ];
  157. if ( pwszFile == NULL )
  158. {
  159. DELETE_OBJECT(pwszStoreName);
  160. CloseHandle(hFile);
  161. delete pCDF;
  162. return( NULL );
  163. }
  164. pwszFile[ 0 ] = L'\0';
  165. if (pCDF->pwszResultDir)
  166. {
  167. wcscpy(pwszFile, pCDF->pwszResultDir);
  168. if (pCDF->pwszResultDir[wcslen(pCDF->pwszResultDir) - 1] != L'\\')
  169. {
  170. wcscat(pwszFile, L"\\");
  171. }
  172. }
  173. wcscat(pwszFile, pwszStoreName);
  174. if (!(wcsrchr(pwszFile, '.')))
  175. {
  176. wcscat(pwszFile, L".");
  177. wcscat(pwszFile, CRYPTCAT_FILEEXT);
  178. }
  179. DWORD dwPublicVersion;
  180. DWORD dwEncodingType;
  181. //
  182. // PublicVersion
  183. //
  184. CDFPositionAtGroupTag(pCDF, CAT_HEADER_TAG);
  185. wcscpy(&wszRetValue[0], L"0x00000001");
  186. CDFGetParam(pCDF, CAT_HEADER_TAG, CAT_HEADER_VERSION_TAG, &wszRetValue[0], &pwsz, NULL);
  187. if (pwsz)
  188. {
  189. dwPublicVersion = wcstol(pwsz, NULL, 16);
  190. delete pwsz;
  191. }
  192. //
  193. // EncodingType
  194. //
  195. CDFPositionAtGroupTag(pCDF, CAT_HEADER_TAG);
  196. wcscpy(&wszRetValue[0], L"0x00010001"); // PKCS_7_ASN_ENCODING | X509_ASN_ENCODING
  197. CDFGetParam(pCDF, CAT_HEADER_TAG, CAT_HEADER_ENCODETYPE_TAG, &wszRetValue[0], &pwsz, NULL);
  198. if (pwsz)
  199. {
  200. dwEncodingType = wcstol(pwsz, NULL, 16);
  201. delete pwsz;
  202. }
  203. pCDF->hCATStore = CryptCATOpen(pwszFile, CRYPTCAT_OPEN_CREATENEW, NULL, dwPublicVersion, dwEncodingType);
  204. delete pwszStoreName;
  205. delete pwszFile;
  206. if ((pCDF->hCATStore == INVALID_HANDLE_VALUE) ||
  207. (!(pCDF->hCATStore)))
  208. {
  209. CryptCATCDFClose(pCDF);
  210. pCDF = NULL;
  211. }
  212. return(pCDF);
  213. }
  214. BOOL WINAPI CryptCATCDFClose(CRYPTCATCDF *pCDF)
  215. {
  216. BOOL fRet;
  217. if (!(pCDF) ||
  218. (pCDF->cbStruct != sizeof(CRYPTCATCDF)))
  219. {
  220. SetLastError(ERROR_INVALID_PARAMETER);
  221. return(FALSE);
  222. }
  223. fRet = TRUE;
  224. if ((pCDF->hFile) && (pCDF->hFile != INVALID_HANDLE_VALUE))
  225. {
  226. fRet &= CloseHandle(pCDF->hFile);
  227. }
  228. if ((pCDF->hCATStore) && (pCDF->hCATStore != INVALID_HANDLE_VALUE))
  229. {
  230. fRet &= CatalogSaveP7UData((CRYPTCATSTORE *)pCDF->hCATStore);
  231. fRet &= CryptCATClose(pCDF->hCATStore);
  232. }
  233. DELETE_OBJECT(pCDF->pwszResultDir);
  234. delete pCDF;
  235. return(fRet);
  236. }
  237. CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumCatAttributes(CRYPTCATCDF *pCDF,
  238. CRYPTCATATTRIBUTE *pPrevAttr,
  239. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  240. {
  241. if (!(pCDF) ||
  242. (pCDF->cbStruct != sizeof(CRYPTCATCDF)))
  243. {
  244. SetLastError(ERROR_INVALID_PARAMETER);
  245. return(NULL);
  246. }
  247. LPWSTR pwsz;
  248. LPWSTR pwszOID;
  249. LPWSTR pwszValue;
  250. int iAttr;
  251. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  252. WCHAR wszTemp[64];
  253. DWORD dwType;
  254. CRYPTCATATTRIBUTE *pAttr;
  255. iAttr = (pPrevAttr) ? pPrevAttr->dwReserved + 1 : 1;
  256. wcscpy(&wszRetValue[0], CAT_HEADER_ATTR_TAG);
  257. wcscat(&wszRetValue[0], _itow(iAttr, &wszTemp[0], 10));
  258. pwsz = NULL;
  259. pAttr = NULL;
  260. CDFPositionAtGroupTag(pCDF, CAT_HEADER_TAG);
  261. if (CDFGetParam(pCDF, CAT_HEADER_TAG, &wszRetValue[0], NULL, &pwsz, NULL))
  262. {
  263. if (pwsz)
  264. {
  265. if (CDFSplitAttrLine(pwsz, &dwType, &pwszOID, &pwszValue, pfnParseError))
  266. {
  267. if (dwType & CRYPTCAT_ATTR_NAMEOBJID)
  268. {
  269. //
  270. // make sure we have a valid objid in the name.
  271. // we might do something better than this (???)
  272. //
  273. if (!(CDFCheckOID(pwszOID, pfnParseError)))
  274. {
  275. delete pwsz;
  276. return(NULL);
  277. }
  278. }
  279. if (dwType & CRYPTCAT_ATTR_UNAUTHENTICATED)
  280. {
  281. if (pfnParseError)
  282. {
  283. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_UNSUPPORTED, pwsz);
  284. }
  285. }
  286. else if (((dwType & CRYPTCAT_ATTR_NAMEOBJID) ||
  287. (dwType & CRYPTCAT_ATTR_NAMEASCII)) &&
  288. ((dwType & CRYPTCAT_ATTR_DATABASE64) ||
  289. (dwType & CRYPTCAT_ATTR_DATAASCII)))
  290. {
  291. pAttr = CryptCATPutCatAttrInfo(pCDF->hCATStore, pwszOID, dwType,
  292. (wcslen(pwszValue) + 1) * sizeof(WCHAR),
  293. (BYTE *)pwszValue);
  294. if (pAttr)
  295. {
  296. pAttr->dwReserved = iAttr;
  297. }
  298. }
  299. else
  300. {
  301. if (pfnParseError)
  302. {
  303. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TYPECOMBO,
  304. pwsz);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. DELETE_OBJECT(pwsz);
  311. return(pAttr);
  312. }
  313. CRYPTCATMEMBER * WINAPI CryptCATCDFEnumMembers(CRYPTCATCDF *pCDF, CRYPTCATMEMBER *pPrevMember,
  314. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  315. {
  316. LPWSTR pwszLastTag;
  317. pwszLastTag = NULL;
  318. if (pPrevMember)
  319. {
  320. if (pPrevMember->cbStruct != sizeof(CRYPTCATMEMBER))
  321. {
  322. SetLastError(ERROR_INVALID_PARAMETER);
  323. return(NULL);
  324. }
  325. if (pPrevMember->pwszReferenceTag)
  326. {
  327. if (!(pwszLastTag = (LPWSTR)CatalogNew(wcslen(pPrevMember->pwszReferenceTag) *
  328. sizeof(WCHAR) + 4)))
  329. {
  330. return(NULL);
  331. }
  332. wcscpy(pwszLastTag, pPrevMember->pwszReferenceTag);
  333. }
  334. }
  335. if (!(pCDF) ||
  336. (pCDF->hFile == INVALID_HANDLE_VALUE) ||
  337. !(pCDF->hFile))
  338. {
  339. DELETE_OBJECT(pwszLastTag);
  340. SetLastError(ERROR_INVALID_PARAMETER);
  341. return(NULL);
  342. }
  343. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  344. CDFPositionAtLastMember(pCDF);
  345. if (CDFGetNextMember(pCDF, &wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszLastTag))
  346. {
  347. LPWSTR pwsz;
  348. DELETE_OBJECT(pwszLastTag);
  349. //
  350. // file path/name (required!)
  351. //
  352. CDFPositionAtLastMember(pCDF);
  353. if (!(CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, &wszRetValue[0])))
  354. {
  355. if (pfnParseError)
  356. {
  357. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH,
  358. &wszRetValue[0]);
  359. }
  360. }
  361. else
  362. {
  363. CRYPTCATMEMBER *pMember;
  364. WCHAR *pwszFileName;
  365. WCHAR *pwszReferenceTag;
  366. GUID gSubjectType;
  367. HANDLE hFile;
  368. //
  369. // file path/name
  370. //
  371. pwszFileName = pwsz;
  372. // remember: don't delete pwsz this time!
  373. if ((hFile = CreateFileU(pwszFileName,
  374. GENERIC_READ,
  375. FILE_SHARE_READ,
  376. NULL,
  377. OPEN_EXISTING,
  378. FILE_ATTRIBUTE_NORMAL,
  379. NULL)) == INVALID_HANDLE_VALUE)
  380. {
  381. if (pfnParseError)
  382. {
  383. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND,
  384. pwszFileName);
  385. }
  386. DELETE_OBJECT(pwszFileName);
  387. return(NULL);
  388. }
  389. CloseHandle(hFile);
  390. //
  391. // reference tag
  392. //
  393. if (!(pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(&wszRetValue[0]) + 1) * sizeof(WCHAR))))
  394. {
  395. DELETE_OBJECT(pwszFileName);
  396. return(NULL);
  397. }
  398. wcscpy(pwszReferenceTag, &wszRetValue[0]);
  399. //
  400. // Alt SIP GUID
  401. //
  402. if (FAILED(StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszReferenceTag)))
  403. {
  404. DELETE_OBJECT(pwszFileName);
  405. return(NULL);
  406. }
  407. if (FAILED(StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, CAT_MEMBER_ALTSIP_TAG)))
  408. {
  409. DELETE_OBJECT(pwszFileName);
  410. return(NULL);
  411. }
  412. CDFPositionAtLastMember(pCDF);
  413. CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, pwszReferenceTag);
  414. if (pwsz)
  415. {
  416. CDFTextToGUID(pwsz, &gSubjectType, pfnParseError);
  417. DELETE_OBJECT(pwszFileName);
  418. DELETE_OBJECT(pwsz);
  419. }
  420. else
  421. {
  422. if (!(CryptSIPRetrieveSubjectGuidForCatalogFile(pwszFileName, NULL, &gSubjectType)))
  423. {
  424. return(NULL);
  425. }
  426. }
  427. //
  428. // Indirect Data
  429. //
  430. BYTE *pbIndirectData;
  431. DWORD cbIndirectData;
  432. DWORD dwCertVersion;
  433. if (!(CDFCalcIndirectData(pCDF, pwszFileName, &gSubjectType, &cbIndirectData, &pbIndirectData,
  434. &dwCertVersion, pfnParseError)))
  435. {
  436. DELETE_OBJECT(pwszReferenceTag);
  437. DELETE_OBJECT(pwszFileName);
  438. return(NULL);
  439. }
  440. pMember = CryptCATPutMemberInfo(pCDF->hCATStore,
  441. pwszFileName,
  442. pwszReferenceTag,
  443. &gSubjectType,
  444. dwCertVersion,
  445. cbIndirectData,
  446. pbIndirectData);
  447. if (!(pMember) && (GetLastError() == CRYPT_E_EXISTS))
  448. {
  449. if (pfnParseError)
  450. {
  451. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_DUPLICATE,
  452. pwszReferenceTag);
  453. }
  454. }
  455. DELETE_OBJECT(pbIndirectData);
  456. //
  457. // Done!
  458. //
  459. DELETE_OBJECT(pwszReferenceTag);
  460. DELETE_OBJECT(pwszFileName);
  461. return(pMember);
  462. }
  463. }
  464. DELETE_OBJECT(pwszLastTag);
  465. return(NULL);
  466. }
  467. LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTagEx(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
  468. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
  469. CRYPTCATMEMBER** ppMember, BOOL fContinueOnError,
  470. LPVOID pvReserved)
  471. {
  472. LPWSTR pwszLastTag;
  473. pwszLastTag = pwszPrevCDFTag;
  474. if (!(pCDF) ||
  475. (pCDF->hFile == INVALID_HANDLE_VALUE) ||
  476. !(pCDF->hFile))
  477. {
  478. DELETE_OBJECT(pwszLastTag);
  479. SetLastError(ERROR_INVALID_PARAMETER);
  480. return(NULL);
  481. }
  482. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  483. CDFPositionAtLastMember(pCDF);
  484. if (CDFGetNextMember(pCDF, &wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszLastTag))
  485. {
  486. LPWSTR pwsz;
  487. DELETE_OBJECT(pwszLastTag);
  488. //
  489. // file path/name (required!)
  490. //
  491. CDFPositionAtLastMember(pCDF);
  492. if (!(CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, &wszRetValue[0])))
  493. {
  494. if (pfnParseError)
  495. {
  496. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILE_PATH,
  497. &wszRetValue[0]);
  498. }
  499. SetLastError(E_FAIL);
  500. return(NULL);
  501. }
  502. else
  503. {
  504. CRYPTCATMEMBER *pMember;
  505. WCHAR *pwszFileName;
  506. WCHAR *pwszReferenceTag;
  507. GUID gSubjectType;
  508. HANDLE hFile;
  509. HRESULT hr;
  510. //
  511. // reference tag
  512. //
  513. if (!(pwszReferenceTag = (LPWSTR)CatalogNew((wcslen(&wszRetValue[0]) + 1) * sizeof(WCHAR))))
  514. {
  515. // LastError is set by CatalogNew
  516. return(NULL);
  517. }
  518. wcscpy(pwszReferenceTag, &wszRetValue[0]);
  519. //
  520. // From this point on we have the option of continuing on error.
  521. //
  522. //
  523. // file path/name
  524. //
  525. pwszFileName = pwsz;
  526. // remember: don't delete pwsz this time!
  527. if ((hFile = CreateFileU(pwszFileName,
  528. GENERIC_READ,
  529. FILE_SHARE_READ,
  530. NULL,
  531. OPEN_EXISTING,
  532. FILE_ATTRIBUTE_NORMAL,
  533. NULL)) == INVALID_HANDLE_VALUE)
  534. {
  535. if (pfnParseError)
  536. {
  537. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_FILENOTFOUND,
  538. pwszFileName);
  539. }
  540. DELETE_OBJECT(pwszFileName);
  541. if ( fContinueOnError == FALSE )
  542. {
  543. DELETE_OBJECT(pwszReferenceTag);
  544. pwszReferenceTag = NULL;
  545. }
  546. // LastError is set by CreateFile
  547. return(pwszReferenceTag);
  548. }
  549. CloseHandle(hFile);
  550. //
  551. // Alt SIP GUID
  552. //
  553. if (FAILED(hr = StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszReferenceTag)))
  554. {
  555. DELETE_OBJECT(pwszReferenceTag);
  556. SetLastError(HRESULT_CODE(hr));
  557. return(NULL);
  558. }
  559. if (FAILED(hr = StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, CAT_MEMBER_ALTSIP_TAG)))
  560. {
  561. DELETE_OBJECT(pwszReferenceTag);
  562. SetLastError(HRESULT_CODE(hr));
  563. return(NULL);
  564. }
  565. CDFPositionAtLastMember(pCDF);
  566. CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, pwszReferenceTag);
  567. if (pwsz)
  568. {
  569. CDFTextToGUID(pwsz, &gSubjectType, pfnParseError);
  570. // If this call fails, we won't know about it
  571. // but the caller will get a pfnParseError call.
  572. DELETE_OBJECT(pwszFileName);
  573. DELETE_OBJECT(pwsz);
  574. }
  575. else
  576. {
  577. if (!(CryptSIPRetrieveSubjectGuidForCatalogFile(pwszFileName, NULL, &gSubjectType)))
  578. {
  579. if ( fContinueOnError == FALSE )
  580. {
  581. DELETE_OBJECT(pwszReferenceTag);
  582. pwszReferenceTag = NULL;
  583. }
  584. DELETE_OBJECT(pwszFileName);
  585. SetLastError(E_FAIL);
  586. return(pwszReferenceTag);
  587. }
  588. }
  589. //
  590. // Indirect Data
  591. //
  592. BYTE *pbIndirectData;
  593. DWORD cbIndirectData;
  594. DWORD dwCertVersion;
  595. SIP_INDIRECT_DATA* pIndirectData;
  596. LPWSTR pwszTagToPut;
  597. BOOL fHashTagUsed = FALSE;
  598. if (!(CDFCalcIndirectData(pCDF, pwszFileName, &gSubjectType, &cbIndirectData, &pbIndirectData,
  599. &dwCertVersion, pfnParseError)))
  600. {
  601. DELETE_OBJECT(pwszFileName);
  602. if ( fContinueOnError == FALSE )
  603. {
  604. DELETE_OBJECT(pwszReferenceTag);
  605. pwszReferenceTag = NULL;
  606. }
  607. SetLastError(E_FAIL);
  608. return(pwszReferenceTag);
  609. }
  610. pIndirectData = (SIP_INDIRECT_DATA *)pbIndirectData;
  611. pwszTagToPut = pwszReferenceTag;
  612. if (_wcsnicmp(pwszReferenceTag, CAT_MEMBER_HASH_TAG, wcslen(CAT_MEMBER_HASH_TAG)) == 0)
  613. {
  614. fHashTagUsed = TRUE;
  615. if (MsCatConstructHashTag(
  616. pIndirectData->Digest.cbData,
  617. pIndirectData->Digest.pbData,
  618. &pwszTagToPut
  619. ) == FALSE)
  620. {
  621. DELETE_OBJECT(pwszFileName);
  622. if ( fContinueOnError == FALSE )
  623. {
  624. DELETE_OBJECT(pwszReferenceTag);
  625. pwszReferenceTag = NULL;
  626. }
  627. SetLastError(E_FAIL);
  628. return(pwszReferenceTag);
  629. }
  630. }
  631. pMember = CryptCATPutMemberInfo(pCDF->hCATStore,
  632. pwszFileName,
  633. pwszTagToPut,
  634. &gSubjectType,
  635. dwCertVersion,
  636. cbIndirectData,
  637. pbIndirectData);
  638. if (!(pMember) && (GetLastError() == CRYPT_E_EXISTS))
  639. {
  640. if (pfnParseError)
  641. {
  642. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_DUPLICATE,
  643. pwszReferenceTag);
  644. }
  645. }
  646. //
  647. // Some Cleanup to do regardless of error
  648. //
  649. DELETE_OBJECT(pbIndirectData);
  650. if ( fHashTagUsed == TRUE )
  651. {
  652. MsCatFreeHashTag(pwszTagToPut);
  653. }
  654. DELETE_OBJECT(pwszFileName);
  655. //
  656. // Final error determination
  657. //
  658. if (pMember)
  659. {
  660. *ppMember = pMember;
  661. SetLastError(ERROR_SUCCESS);
  662. }
  663. else
  664. {
  665. if ( fContinueOnError == FALSE )
  666. {
  667. DELETE_OBJECT(pwszReferenceTag);
  668. pwszReferenceTag = NULL;
  669. }
  670. SetLastError(E_FAIL);
  671. }
  672. //
  673. // Done!
  674. //
  675. return(pwszReferenceTag);
  676. }
  677. }
  678. else
  679. {
  680. // Successful completion of CDF. This means we hit the last member.
  681. DELETE_OBJECT(pwszLastTag);
  682. SetLastError(ERROR_SUCCESS);
  683. return(NULL);
  684. }
  685. // Execution never reaches this point.
  686. // The if/else above always returns.
  687. }
  688. LPWSTR WINAPI CryptCATCDFEnumMembersByCDFTag(CRYPTCATCDF *pCDF, LPWSTR pwszPrevCDFTag,
  689. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError,
  690. CRYPTCATMEMBER** ppMember)
  691. {
  692. return(CryptCATCDFEnumMembersByCDFTagEx(pCDF, pwszPrevCDFTag, pfnParseError, ppMember, FALSE, NULL));
  693. }
  694. BOOL CDFCalcIndirectData(CRYPTCATCDF *pCDF, WCHAR *pwszFileName, GUID *pgSubjectType, DWORD *pcbIndirectData,
  695. BYTE **ppbIndirectData, DWORD *pdwCertVersion, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  696. {
  697. SIP_SUBJECTINFO sSubjInfo;
  698. SIP_DISPATCH_INFO sSip;
  699. CRYPTCATSTORE *pCatStore;
  700. *pcbIndirectData = 0;
  701. *ppbIndirectData = NULL;
  702. pCatStore = (CRYPTCATSTORE *)pCDF->hCATStore;
  703. memset(&sSubjInfo, 0x00, sizeof(SIP_SUBJECTINFO));
  704. memset(&sSip, 0x00, sizeof(SIP_DISPATCH_INFO));
  705. sSubjInfo.cbSize = sizeof(SIP_SUBJECTINFO);
  706. sSubjInfo.hProv = pCatStore ->hProv;
  707. sSubjInfo.DigestAlgorithm.pszObjId = (char *)CertAlgIdToOID(CALG_SHA1);
  708. sSubjInfo.dwFlags = SPC_INC_PE_RESOURCES_FLAG | SPC_INC_PE_IMPORT_ADDR_TABLE_FLAG |
  709. MSSIP_FLAGS_PROHIBIT_RESIZE_ON_CREATE;
  710. sSubjInfo.dwEncodingType = pCatStore->dwEncodingType;
  711. sSubjInfo.pgSubjectType = pgSubjectType;
  712. sSubjInfo.pwsFileName = pwszFileName;
  713. if (!(CryptSIPLoad(pgSubjectType, 0, &sSip)))
  714. {
  715. if (pfnParseError)
  716. {
  717. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_INDIRECTDATA, pwszFileName);
  718. }
  719. return(FALSE);
  720. }
  721. sSip.pfCreate(&sSubjInfo,
  722. pcbIndirectData,
  723. NULL);
  724. if (*pcbIndirectData < 1)
  725. {
  726. if (pfnParseError)
  727. {
  728. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_INDIRECTDATA, pwszFileName);
  729. }
  730. return(FALSE);
  731. }
  732. if (!(*ppbIndirectData = (BYTE *)CatalogNew(*pcbIndirectData)))
  733. {
  734. *pcbIndirectData = 0;
  735. if (pfnParseError)
  736. {
  737. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_INDIRECTDATA, pwszFileName);
  738. }
  739. return(FALSE);
  740. }
  741. if (!(sSip.pfCreate(&sSubjInfo,
  742. pcbIndirectData,
  743. (SIP_INDIRECT_DATA *)*ppbIndirectData)))
  744. {
  745. DELETE_OBJECT(*ppbIndirectData);
  746. *pcbIndirectData = 0;
  747. if (pfnParseError)
  748. {
  749. pfnParseError(CRYPTCAT_E_AREA_MEMBER, CRYPTCAT_E_CDF_MEMBER_INDIRECTDATA, pwszFileName);
  750. }
  751. return(FALSE);
  752. }
  753. *pdwCertVersion = sSubjInfo.dwIntVersion;
  754. return(TRUE);
  755. }
  756. CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumAttributes(CRYPTCATCDF *pCDF, CRYPTCATMEMBER *pMember,
  757. CRYPTCATATTRIBUTE *pPrevAttr,
  758. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  759. {
  760. if (!(pCDF) ||
  761. (pCDF->cbStruct != sizeof(CRYPTCATCDF)) ||
  762. !(pMember) ||
  763. (pMember->cbStruct != sizeof(CRYPTCATMEMBER)))
  764. {
  765. SetLastError(ERROR_INVALID_PARAMETER);
  766. return(NULL);
  767. }
  768. LPWSTR pwsz;
  769. LPWSTR pwszOID;
  770. LPWSTR pwszValue;
  771. int iAttr;
  772. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  773. WCHAR wszTemp[64];
  774. DWORD dwType;
  775. CRYPTCATATTRIBUTE *pAttr;
  776. iAttr = (pPrevAttr) ? pPrevAttr->dwReserved + 1 : 1;
  777. if (FAILED(StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, pMember->pwszReferenceTag)))
  778. {
  779. return(NULL);
  780. }
  781. if (FAILED(StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, L"ATTR")))
  782. {
  783. return(NULL);
  784. }
  785. if (FAILED(StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, _itow(iAttr, &wszTemp[0], 10))))
  786. {
  787. return(NULL);
  788. }
  789. pwsz = NULL;
  790. pAttr = NULL;
  791. CDFPositionAtLastMember(pCDF);
  792. if (CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, pMember->pwszReferenceTag))
  793. {
  794. if (pwsz)
  795. {
  796. if (CDFSplitAttrLine(pwsz, &dwType, &pwszOID, &pwszValue, pfnParseError))
  797. {
  798. if (dwType & CRYPTCAT_ATTR_NAMEOBJID)
  799. {
  800. //
  801. // make sure we have a valid objid in the name.
  802. // we might do something better than this (???)
  803. //
  804. if (!(CDFCheckOID(pwszOID, pfnParseError)))
  805. {
  806. delete pwsz;
  807. return(NULL);
  808. }
  809. }
  810. if (dwType & CRYPTCAT_ATTR_UNAUTHENTICATED)
  811. {
  812. if (pfnParseError)
  813. {
  814. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_UNSUPPORTED, pwsz);
  815. }
  816. }
  817. else if (((dwType & CRYPTCAT_ATTR_NAMEOBJID) ||
  818. (dwType & CRYPTCAT_ATTR_NAMEASCII)) &&
  819. ((dwType & CRYPTCAT_ATTR_DATABASE64) ||
  820. (dwType & CRYPTCAT_ATTR_DATAASCII)))
  821. {
  822. pAttr = CryptCATPutAttrInfo(pCDF->hCATStore, pMember, pwszOID, dwType,
  823. (wcslen(pwszValue) + 1) * sizeof(WCHAR),
  824. (BYTE *)pwszValue);
  825. if (pAttr)
  826. {
  827. pAttr->dwReserved = iAttr;
  828. }
  829. }
  830. else
  831. {
  832. if (pfnParseError)
  833. {
  834. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TYPECOMBO,
  835. pwsz);
  836. }
  837. }
  838. }
  839. }
  840. }
  841. DELETE_OBJECT(pwsz);
  842. return(pAttr);
  843. }
  844. CRYPTCATATTRIBUTE * WINAPI CryptCATCDFEnumAttributesWithCDFTag(CRYPTCATCDF *pCDF, LPWSTR pwszMemberTag, CRYPTCATMEMBER *pMember,
  845. CRYPTCATATTRIBUTE *pPrevAttr,
  846. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  847. {
  848. if (!(pCDF) ||
  849. (pCDF->cbStruct != sizeof(CRYPTCATCDF)) ||
  850. !(pwszMemberTag) ||
  851. !(pMember) ||
  852. (pMember->cbStruct != sizeof(CRYPTCATMEMBER)))
  853. {
  854. SetLastError(ERROR_INVALID_PARAMETER);
  855. return(NULL);
  856. }
  857. LPWSTR pwsz;
  858. LPWSTR pwszOID;
  859. LPWSTR pwszValue;
  860. int iAttr;
  861. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  862. WCHAR wszTemp[64];
  863. DWORD dwType;
  864. CRYPTCATATTRIBUTE *pAttr;
  865. iAttr = (pPrevAttr) ? pPrevAttr->dwReserved + 1 : 1;
  866. if (FAILED(StringCchCopyW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, pwszMemberTag)))
  867. {
  868. return(NULL);
  869. }
  870. if (FAILED(StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, L"ATTR")))
  871. {
  872. return(NULL);
  873. }
  874. if (FAILED(StringCchCatW(&wszRetValue[0], MAX_CDF_LINE_LEN + 4, _itow(iAttr, &wszTemp[0], 10))))
  875. {
  876. return(NULL);
  877. }
  878. pwsz = NULL;
  879. pAttr = NULL;
  880. CDFPositionAtLastMember(pCDF);
  881. if (CDFGetParam(pCDF, CAT_MEMBER_TAG, &wszRetValue[0], NULL, &pwsz, pwszMemberTag))
  882. {
  883. if (pwsz)
  884. {
  885. if (CDFSplitAttrLine(pwsz, &dwType, &pwszOID, &pwszValue, pfnParseError))
  886. {
  887. if (dwType & CRYPTCAT_ATTR_NAMEOBJID)
  888. {
  889. //
  890. // make sure we have a valid objid in the name.
  891. // we might do something better than this (???)
  892. //
  893. if (!(CDFCheckOID(pwszOID, pfnParseError)))
  894. {
  895. delete pwsz;
  896. return(NULL);
  897. }
  898. }
  899. if (dwType & CRYPTCAT_ATTR_UNAUTHENTICATED)
  900. {
  901. if (pfnParseError)
  902. {
  903. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_UNSUPPORTED, pwsz);
  904. }
  905. }
  906. else if (((dwType & CRYPTCAT_ATTR_NAMEOBJID) ||
  907. (dwType & CRYPTCAT_ATTR_NAMEASCII)) &&
  908. ((dwType & CRYPTCAT_ATTR_DATABASE64) ||
  909. (dwType & CRYPTCAT_ATTR_DATAASCII)))
  910. {
  911. pAttr = CryptCATPutAttrInfo(pCDF->hCATStore, pMember, pwszOID, dwType,
  912. (wcslen(pwszValue) + 1) * sizeof(WCHAR),
  913. (BYTE *)pwszValue);
  914. if (pAttr)
  915. {
  916. pAttr->dwReserved = iAttr;
  917. }
  918. }
  919. else
  920. {
  921. if (pfnParseError)
  922. {
  923. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TYPECOMBO,
  924. pwsz);
  925. }
  926. }
  927. }
  928. }
  929. }
  930. DELETE_OBJECT(pwsz);
  931. return(pAttr);
  932. }
  933. /////////////////////////////////////////////////////////////////////////////
  934. //
  935. // Local Functions
  936. //
  937. BOOL CDFCheckOID(LPWSTR pwszOID, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  938. {
  939. DWORD cbConv;
  940. char *pszOID;
  941. cbConv = WideCharToMultiByte(0, 0,
  942. pwszOID, wcslen(pwszOID),
  943. NULL, 0, NULL, NULL);
  944. if (cbConv < 1)
  945. {
  946. if (pfnParseError)
  947. {
  948. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TYPECOMBO, pwszOID);
  949. }
  950. return(FALSE);
  951. }
  952. if (!(pszOID = (LPSTR)CatalogNew(cbConv)))
  953. {
  954. return(FALSE);
  955. }
  956. WideCharToMultiByte(0, 0,
  957. pwszOID, wcslen(pwszOID),
  958. pszOID, cbConv, NULL, NULL);
  959. DWORD i;
  960. BOOL fRet;
  961. fRet = TRUE;
  962. i = 0;
  963. while (i < cbConv)
  964. {
  965. if (((pszOID[i] < '0') || (pszOID[i] > '9')) &&
  966. (pszOID[i] != '.'))
  967. {
  968. fRet = FALSE;
  969. break;
  970. }
  971. i++;
  972. }
  973. delete pszOID;
  974. if (!(fRet))
  975. {
  976. if (pfnParseError)
  977. {
  978. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TYPECOMBO, pwszOID);
  979. }
  980. }
  981. return(fRet);
  982. }
  983. BOOL CDFSplitAttrLine(LPWSTR pwszLine, DWORD *pdwType, LPWSTR *ppwszOID, LPWSTR *ppwszValue,
  984. PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  985. {
  986. LPWSTR pwszColon;
  987. LPWSTR pwszStart;
  988. LPWSTR pwsz;
  989. *pdwType = 0;
  990. *ppwszValue = NULL;
  991. *ppwszOID = NULL;
  992. if (!(pwsz = (WCHAR *)CatalogNew((wcslen(pwszLine) + 1) * sizeof(WCHAR))))
  993. {
  994. return(FALSE);
  995. }
  996. wcscpy(pwsz, pwszLine);
  997. pwszStart = pwszLine;
  998. //
  999. // first one is type
  1000. //
  1001. if (!(pwszColon = wcschr(pwszStart, L':')))
  1002. {
  1003. if (pfnParseError)
  1004. {
  1005. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES, pwsz);
  1006. }
  1007. delete pwsz;
  1008. return(FALSE);
  1009. }
  1010. *pwszColon = NULL;
  1011. *pdwType = wcstol(pwszStart, NULL, 16);
  1012. pwszStart = &pwszColon[1];
  1013. //
  1014. // next, oid/name
  1015. //
  1016. if (!(pwszColon = wcschr(pwszStart, L':')))
  1017. {
  1018. if (pfnParseError)
  1019. {
  1020. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES, pwsz);
  1021. }
  1022. delete pwsz;
  1023. return(FALSE);
  1024. }
  1025. *pwszColon = NULL;
  1026. *ppwszOID = pwszStart;
  1027. pwszStart = &pwszColon[1];
  1028. //
  1029. // next, value
  1030. //
  1031. if (!(pwszStart[0]))
  1032. {
  1033. if (pfnParseError)
  1034. {
  1035. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_ATTR_TOOFEWVALUES, pwsz);
  1036. }
  1037. delete pwsz;
  1038. return(FALSE);
  1039. }
  1040. delete pwsz;
  1041. *ppwszValue = pwszStart;
  1042. return(TRUE);
  1043. }
  1044. void CDFTextToGUID(LPWSTR pwszText, GUID *pgBin, PFN_CDF_PARSE_ERROR_CALLBACK pfnParseError)
  1045. {
  1046. WCHAR wszGuid[256];
  1047. GUID gTemp;
  1048. memset(pgBin, 0x00, sizeof(GUID));
  1049. if ((pwszText[0] != L'[') &&
  1050. (pwszText[0] != L'{'))
  1051. {
  1052. if (FAILED(StringCchCopyW(&wszGuid[0], 256, L"{")))
  1053. {
  1054. return;
  1055. }
  1056. if (FAILED(StringCchCatW(&wszGuid[0], 256, pwszText)))
  1057. {
  1058. return;
  1059. }
  1060. if (FAILED(StringCchCatW(&wszGuid[0], 256, L"}")))
  1061. {
  1062. return;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. if (FAILED(StringCchCopyW(&wszGuid[0], 256, pwszText)))
  1068. {
  1069. return;
  1070. }
  1071. }
  1072. if (!(wstr2guid(&wszGuid[0], pgBin)))
  1073. {
  1074. if (pfnParseError)
  1075. {
  1076. pfnParseError(CRYPTCAT_E_AREA_ATTRIBUTE, CRYPTCAT_E_CDF_BAD_GUID_CONV, &wszGuid[0]);
  1077. }
  1078. }
  1079. }
  1080. BOOL CDFPositionAtGroupTag(CRYPTCATCDF *pCDF, LPWSTR pwszTag)
  1081. {
  1082. if (SetFilePointer(pCDF->hFile, 0, NULL, FILE_BEGIN) == 0xFFFFFFFF)
  1083. {
  1084. return(FALSE);
  1085. }
  1086. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  1087. DWORD ccRet;
  1088. while ((ccRet = CDFGetLine(pCDF, &wszRetValue[0], MAX_CDF_LINE_LEN * sizeof(WCHAR))) > 0)
  1089. {
  1090. if (wszRetValue[0] == L'#')
  1091. {
  1092. continue;
  1093. }
  1094. CDFEOLOut(&wszRetValue[0], ccRet);
  1095. if (wszRetValue[0] == L'[')
  1096. {
  1097. if (_memicmp(&wszRetValue[0], pwszTag, wcslen(pwszTag) * sizeof(WCHAR)) == 0)
  1098. {
  1099. return(TRUE);
  1100. }
  1101. }
  1102. }
  1103. return(FALSE);
  1104. }
  1105. BOOL CDFPositionAtLastMember(CRYPTCATCDF *pCDF)
  1106. {
  1107. if (pCDF->dwLastMemberOffset == 0)
  1108. {
  1109. return(CDFPositionAtGroupTag(pCDF, CAT_MEMBER_TAG));
  1110. }
  1111. else if (SetFilePointer(pCDF->hFile, pCDF->dwLastMemberOffset,
  1112. NULL, FILE_BEGIN) == 0xFFFFFFFF)
  1113. {
  1114. return(FALSE);
  1115. }
  1116. return(TRUE);
  1117. }
  1118. BOOL CDFGetNextMember(CRYPTCATCDF *pCDF, LPWSTR pwszMember, int cchMember, LPWSTR pwszLastMember)
  1119. {
  1120. WCHAR wszLine[MAX_CDF_LINE_LEN + 4];
  1121. WCHAR wszCheck[MAX_CDF_LINE_LEN + 1];
  1122. LPWSTR pwszEqual;
  1123. DWORD ccRet;
  1124. DWORD ccLastMember;
  1125. BOOL fFoundLast;
  1126. if (pwszLastMember)
  1127. {
  1128. if (FAILED(StringCchCopyW(&wszCheck[0], MAX_CDF_LINE_LEN + 1, pwszLastMember)))
  1129. {
  1130. return(FALSE);
  1131. }
  1132. ccLastMember = wcslen(&wszCheck[0]);
  1133. }
  1134. fFoundLast = FALSE;
  1135. while ((ccRet = CDFGetLine(pCDF, &wszLine[0], MAX_CDF_LINE_LEN * sizeof(WCHAR))) > 0)
  1136. {
  1137. if (wszLine[0] == L'#')
  1138. {
  1139. continue;
  1140. }
  1141. CDFEOLOut(&wszLine[0], ccRet);
  1142. if (wszLine[0] == L'[')
  1143. {
  1144. return(FALSE);
  1145. }
  1146. if (!(pwszEqual = wcschr(&wszLine[0], L'=')))
  1147. {
  1148. continue;
  1149. }
  1150. *pwszEqual = NULL;
  1151. if (pwszLastMember)
  1152. {
  1153. if (fFoundLast)
  1154. {
  1155. //
  1156. // before we make the determination that we are in fact on a
  1157. // different member tag, make sure that we aren't just on the
  1158. // same tag's ALTSIP or ATTRx!!!
  1159. //
  1160. if (FAILED(StringCchCopyW(
  1161. &wszCheck[ccLastMember],
  1162. MAX_CDF_LINE_LEN + 1 - ccLastMember,
  1163. CAT_MEMBER_ALTSIP_TAG)))
  1164. {
  1165. return(FALSE);
  1166. }
  1167. if (_memicmp(&wszLine[0], &wszCheck[0], wcslen(&wszCheck[0]) * sizeof(WCHAR)) == 0)
  1168. {
  1169. continue;
  1170. }
  1171. if (FAILED(StringCchCopyW(
  1172. &wszCheck[ccLastMember],
  1173. MAX_CDF_LINE_LEN + 1 - ccLastMember,
  1174. CAT_MEMBER_ATTR_TAG)))
  1175. {
  1176. return(FALSE);
  1177. }
  1178. if (_memicmp(&wszLine[0], &wszCheck[0], wcslen(&wszCheck[0]) * sizeof(WCHAR)) == 0)
  1179. {
  1180. continue;
  1181. }
  1182. if (_wcsicmp(&wszLine[0], pwszLastMember) != 0)
  1183. {
  1184. if (wcslen(&wszLine[0]) + 1 <= cchMember)
  1185. {
  1186. wcscpy(pwszMember, &wszLine[0]);
  1187. }
  1188. else
  1189. {
  1190. return(FALSE);
  1191. }
  1192. //
  1193. // remember the position of the last entry for this member
  1194. //
  1195. *pwszEqual = L'=';
  1196. pCDF->dwLastMemberOffset = pCDF->dwCurFilePos - wcslen(&wszLine[0]);
  1197. return(TRUE);
  1198. }
  1199. }
  1200. else if (_wcsicmp(&wszLine[0], pwszLastMember) == 0)
  1201. {
  1202. fFoundLast = TRUE;
  1203. }
  1204. continue;
  1205. }
  1206. if (wcslen(&wszLine[0]) + 1 <= cchMember)
  1207. {
  1208. wcscpy(pwszMember, &wszLine[0]);
  1209. }
  1210. else
  1211. {
  1212. return(FALSE);
  1213. }
  1214. //
  1215. // remember the position of the last entry for this member
  1216. //
  1217. *pwszEqual = L'=';
  1218. pCDF->dwLastMemberOffset = pCDF->dwCurFilePos - wcslen(&wszLine[0]);
  1219. return(TRUE);
  1220. }
  1221. return(FALSE);
  1222. }
  1223. BOOL CDFGetParam(CRYPTCATCDF *pCDF, LPWSTR pwszGroup, LPWSTR pwszItem, LPWSTR pwszDefault, LPWSTR *ppwszRet,
  1224. LPWSTR pwszMemberTag)
  1225. {
  1226. WCHAR wszRetValue[MAX_CDF_LINE_LEN + 4];
  1227. DWORD ccRet;
  1228. WCHAR *pwsz;
  1229. while ((ccRet = CDFGetLine(pCDF, &wszRetValue[0], MAX_CDF_LINE_LEN * sizeof(WCHAR))) > 0)
  1230. {
  1231. if (wszRetValue[0] == L'#')
  1232. {
  1233. continue;
  1234. }
  1235. CDFEOLOut(&wszRetValue[0], ccRet);
  1236. if (wszRetValue[0] == L'[')
  1237. {
  1238. break;
  1239. }
  1240. if (pwsz = wcschr(&wszRetValue[0], L'='))
  1241. {
  1242. //
  1243. // if we have a member tag and we are past it, get out!
  1244. //
  1245. if (pwszMemberTag)
  1246. {
  1247. if (_memicmp(&wszRetValue[0], pwszMemberTag, wcslen(pwszMemberTag) * sizeof(WCHAR)) != 0)
  1248. {
  1249. break;
  1250. }
  1251. }
  1252. *pwsz = NULL;
  1253. if (_memicmp(&wszRetValue[0], pwszItem, wcslen(pwszItem) * sizeof(WCHAR)) == 0)
  1254. {
  1255. if (wcslen(&pwsz[1]) < 1)
  1256. {
  1257. break;
  1258. }
  1259. if (*ppwszRet = (LPWSTR)CatalogNew((wcslen(&pwsz[1]) + 1) * sizeof(WCHAR)))
  1260. {
  1261. wcscpy(*ppwszRet, &pwsz[1]);
  1262. return(TRUE);
  1263. }
  1264. return(FALSE);
  1265. }
  1266. }
  1267. }
  1268. if (pwszDefault)
  1269. {
  1270. if (*ppwszRet = (LPWSTR)CatalogNew((wcslen(pwszDefault) + 1) * sizeof(WCHAR)))
  1271. {
  1272. wcscpy(*ppwszRet, pwszDefault);
  1273. return(TRUE);
  1274. }
  1275. }
  1276. *ppwszRet = NULL;
  1277. return(FALSE);
  1278. }
  1279. DWORD CDFGetLine(CRYPTCATCDF *pCDF, LPWSTR pwszLineBuf, DWORD cbMaxRead)
  1280. {
  1281. DWORD dwHold;
  1282. DWORD cbRead;
  1283. DWORD cwbRead;
  1284. DWORD dw;
  1285. int iAmt;
  1286. BYTE *pb;
  1287. if ((dwHold = SetFilePointer(pCDF->hFile, 0, NULL, FILE_CURRENT)) == 0xFFFFFFFF)
  1288. {
  1289. return(0);
  1290. }
  1291. if (!(pb = (BYTE *)CatalogNew(cbMaxRead + 2)))
  1292. {
  1293. return(0);
  1294. }
  1295. cbRead = 0;
  1296. if (ReadFile(pCDF->hFile, pb, cbMaxRead, &cbRead, NULL))
  1297. {
  1298. if (cbRead == 0)
  1299. {
  1300. pCDF->fEOF = TRUE;
  1301. delete pb;
  1302. return(0);
  1303. }
  1304. pb[cbRead] = 0x00;
  1305. pCDF->fEOF = FALSE;
  1306. if (cbRead > 0)
  1307. {
  1308. iAmt = 0;
  1309. for (dw = 0; dw < (cbRead - 1); dw++)
  1310. {
  1311. if ((pb[dw] == 0x0d) || (pb[dw] == 0x0a))
  1312. {
  1313. iAmt++;
  1314. if (pb[dw + 1] == 0x0a)
  1315. {
  1316. dw++;
  1317. iAmt++;
  1318. }
  1319. if (SetFilePointer(pCDF->hFile, dwHold + (dw + 1),
  1320. NULL, FILE_BEGIN) == 0xFFFFFFFF)
  1321. {
  1322. pCDF->dwCurFilePos = 0;
  1323. }
  1324. else
  1325. {
  1326. pCDF->dwCurFilePos = SetFilePointer(pCDF->hFile, 0, NULL, FILE_CURRENT) - iAmt;
  1327. }
  1328. pb[dw + 1] = 0x00;
  1329. cwbRead = MultiByteToWideChar(
  1330. CP_ACP,
  1331. 0,
  1332. (const char *)pb,
  1333. -1,
  1334. pwszLineBuf,
  1335. cbMaxRead / sizeof(WCHAR));
  1336. delete pb;
  1337. return(cwbRead + 1);
  1338. }
  1339. }
  1340. }
  1341. }
  1342. else
  1343. {
  1344. delete pb;
  1345. return(0);
  1346. }
  1347. if (pb[cbRead - 1] == 0x1a) /* EOF */
  1348. {
  1349. cbRead--;
  1350. pCDF->dwCurFilePos = 0;
  1351. pCDF->fEOF = TRUE;
  1352. }
  1353. else
  1354. {
  1355. pCDF->dwCurFilePos = dwHold;
  1356. }
  1357. pb[cbRead] = 0x00;
  1358. cwbRead = MultiByteToWideChar(
  1359. CP_ACP,
  1360. 0,
  1361. (const char *)pb,
  1362. -1,
  1363. pwszLineBuf,
  1364. cbMaxRead / sizeof(WCHAR));
  1365. delete pb;
  1366. return(cwbRead);
  1367. }
  1368. void CDFEOLOut(WCHAR *pwsz, DWORD ccLen)
  1369. {
  1370. DWORD i;
  1371. for (i = 0; i < ccLen; i++)
  1372. {
  1373. if ((pwsz[i] == (WCHAR)0x0a) || (pwsz[i] == (WCHAR)0x0d))
  1374. {
  1375. pwsz[i] = NULL;
  1376. return;
  1377. }
  1378. }
  1379. pwsz[ccLen] = NULL;
  1380. }