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.

5823 lines
126 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999
  5. //
  6. // File: certlib.cpp
  7. //
  8. // Contents: Cert Server wrapper routines
  9. //
  10. //---------------------------------------------------------------------------
  11. #include <pch.cpp>
  12. #pragma hdrstop
  13. #include <stdlib.h>
  14. #include <time.h>
  15. #include <locale.h>
  16. extern "C" {
  17. #include <luidate.h>
  18. }
  19. #include <wininet.h>
  20. #include "csdisp.h"
  21. #include "csprop.h"
  22. #include <winnlsp.h>
  23. #include <winldap.h>
  24. #include <ntsecapi.h>
  25. #include "csldap.h"
  26. #include <tfc.h>
  27. #include "cainfop.h"
  28. #include <dsgetdc.h>
  29. #include <lm.h>
  30. #define __dwFILE__ __dwFILE_CERTLIB_CERTLIB_CPP__
  31. #define wszSANITIZEESCAPECHAR L"!"
  32. #define wszURLESCAPECHAR L"%"
  33. #define wcSANITIZEESCAPECHAR L'!'
  34. char
  35. PrintableChar(char ch)
  36. {
  37. if (ch < ' ' || ch > '~')
  38. {
  39. ch = '.';
  40. }
  41. return(ch);
  42. }
  43. VOID
  44. mydbgDumpHex(
  45. IN DWORD dwSubSysId,
  46. IN DWORD Flags,
  47. IN BYTE const *pb,
  48. IN ULONG cb)
  49. {
  50. if (DbgIsSSActive(dwSubSysId))
  51. {
  52. DumpHex(Flags, pb, cb);
  53. }
  54. }
  55. VOID
  56. DumpHex(
  57. IN DWORD Flags,
  58. IN BYTE const *pb,
  59. IN ULONG cb)
  60. {
  61. char const *pszsep;
  62. ULONG r;
  63. ULONG i;
  64. ULONG cbremain;
  65. BOOL fZero = FALSE;
  66. DWORD cchIndent;
  67. DWORD cchAsciiSep;
  68. char szAddress[8 + 1];
  69. char szHexDump[1 + 1 + 3 * 16 + 1];
  70. char szAsciiDump[16 + 1];
  71. char *psz;
  72. cchIndent = DH_INDENTMASK & Flags;
  73. if ((DH_MULTIADDRESS & Flags) && 16 >= cb)
  74. {
  75. Flags |= DH_NOADDRESS;
  76. }
  77. cbremain = 0;
  78. for (r = 0; r < cb; r += 16)
  79. {
  80. szAddress[0] = '\0';
  81. if (0 == (DH_NOADDRESS & Flags))
  82. {
  83. sprintf(szAddress, 64 * 1024 < cb? "%06x": "%04x", r);
  84. CSASSERT(strlen(szAddress) < ARRAYSIZE(szAddress));
  85. }
  86. cbremain = cb - r;
  87. if (r != 0 && pb[r] == 0 && cbremain >= 16)
  88. {
  89. ULONG j;
  90. for (j = r + 1; j < cb; j++)
  91. {
  92. if (pb[j] != 0)
  93. {
  94. break;
  95. }
  96. }
  97. if (j == cb)
  98. {
  99. fZero = TRUE;
  100. break;
  101. }
  102. }
  103. psz = szHexDump;
  104. for (i = 0; i < min(cbremain, 16); i++)
  105. {
  106. pszsep = " ";
  107. if ((i % 8) == 0) // 0 or 8
  108. {
  109. pszsep = " ";
  110. if (i == 0) // 0
  111. {
  112. if (0 == (DH_NOTABPREFIX & Flags))
  113. {
  114. pszsep = "\t";
  115. }
  116. else if (DH_NOADDRESS & Flags)
  117. {
  118. pszsep = "";
  119. }
  120. }
  121. }
  122. CSASSERT(strlen(pszsep) <= 2);
  123. psz += sprintf(psz, "%hs%02x", pszsep, pb[r + i]);
  124. }
  125. *psz = '\0';
  126. CSASSERT(strlen(szHexDump) < ARRAYSIZE(szHexDump));
  127. cchAsciiSep = 0;
  128. szAsciiDump[0] = '\0';
  129. if (0 == (DH_NOASCIIHEX & Flags) && 0 != i)
  130. {
  131. cchAsciiSep = 3 + (16 - i)*3 + ((i <= 8)? 1 : 0);
  132. for (i = 0; i < min(cbremain, 16); i++)
  133. {
  134. szAsciiDump[i] = PrintableChar(pb[r + i]);
  135. }
  136. szAsciiDump[i] = '\0';
  137. CSASSERT(strlen(szAsciiDump) < ARRAYSIZE(szAsciiDump));
  138. }
  139. char szBuf[
  140. sizeof(szAddress) +
  141. sizeof(szHexDump) +
  142. 3 +
  143. sizeof(szAsciiDump)];
  144. _snprintf(
  145. szBuf,
  146. ARRAYSIZE(szBuf),
  147. "%hs%hs%*hs%hs",
  148. szAddress,
  149. szHexDump,
  150. cchAsciiSep,
  151. "",
  152. szAsciiDump);
  153. szBuf[ARRAYSIZE(szBuf) - 1] = '\0';
  154. CSASSERT(strlen(szBuf) + 1 < ARRAYSIZE(szBuf));
  155. #define szFMTHEXDUMP "%*hs%hs\n"
  156. if (DH_PRIVATEDATA & Flags)
  157. {
  158. wprintf(TEXT(szFMTHEXDUMP), cchIndent, "", szBuf);
  159. }
  160. else
  161. {
  162. CONSOLEPRINT7((MAXDWORD, szFMTHEXDUMP, cchIndent, "", szBuf));
  163. }
  164. }
  165. if (fZero)
  166. {
  167. CONSOLEPRINT1((MAXDWORD, "\tRemaining %lx bytes are zero\n", cbremain));
  168. }
  169. }
  170. HRESULT
  171. myDateToFileTime(
  172. IN DATE const *pDate,
  173. OUT FILETIME *pft)
  174. {
  175. SYSTEMTIME st;
  176. HRESULT hr = S_OK;
  177. if (*pDate == 0.0)
  178. {
  179. GetSystemTime(&st);
  180. }
  181. else
  182. {
  183. if (!VariantTimeToSystemTime(*pDate, &st))
  184. {
  185. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  186. _JumpError(hr, error, "VariantTimeToSystemTime");
  187. }
  188. }
  189. if (!SystemTimeToFileTime(&st, pft))
  190. {
  191. hr = myHLastError();
  192. _JumpError(hr, error, "SystemTimeToFileTime");
  193. }
  194. error:
  195. return(hr);
  196. }
  197. HRESULT
  198. myMakeExprDate(
  199. IN OUT DATE *pDate,
  200. IN LONG lDelta,
  201. IN enum ENUM_PERIOD enumPeriod)
  202. {
  203. HRESULT hr;
  204. FILETIME ft;
  205. hr = myDateToFileTime(pDate, &ft);
  206. _JumpIfError(hr, error, "myDateToFileTime");
  207. myMakeExprDateTime(&ft, lDelta, enumPeriod);
  208. hr = myFileTimeToDate(&ft, pDate);
  209. _JumpIfError(hr, error, "myFileTimeToDate");
  210. error:
  211. return(hr);
  212. }
  213. typedef struct _UNITSTABLE
  214. {
  215. WCHAR const *pwszString;
  216. enum ENUM_PERIOD enumPeriod;
  217. } UNITSTABLE;
  218. UNITSTABLE g_aut[] =
  219. {
  220. { wszPERIODSECONDS, ENUM_PERIOD_SECONDS },
  221. { wszPERIODMINUTES, ENUM_PERIOD_MINUTES },
  222. { wszPERIODHOURS, ENUM_PERIOD_HOURS },
  223. { wszPERIODDAYS, ENUM_PERIOD_DAYS },
  224. { wszPERIODWEEKS, ENUM_PERIOD_WEEKS },
  225. { wszPERIODMONTHS, ENUM_PERIOD_MONTHS },
  226. { wszPERIODYEARS, ENUM_PERIOD_YEARS },
  227. };
  228. #define CUNITSTABLEMAX (sizeof(g_aut)/sizeof(g_aut[0]))
  229. HRESULT
  230. myTranslatePeriodUnits(
  231. IN WCHAR const *pwszPeriod,
  232. IN LONG lCount,
  233. OUT enum ENUM_PERIOD *penumPeriod,
  234. OUT LONG *plCount)
  235. {
  236. HRESULT hr;
  237. UNITSTABLE const *put;
  238. for (put = g_aut; put < &g_aut[CUNITSTABLEMAX]; put++)
  239. {
  240. if (0 == mylstrcmpiS(pwszPeriod, put->pwszString))
  241. {
  242. *penumPeriod = put->enumPeriod;
  243. if (0 > lCount)
  244. {
  245. lCount = MAXLONG;
  246. }
  247. *plCount = lCount;
  248. hr = S_OK;
  249. goto error;
  250. }
  251. }
  252. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  253. error:
  254. CSASSERT(hr == S_OK);
  255. return(hr);
  256. }
  257. HRESULT
  258. myTranslateUnlocalizedPeriodString(
  259. IN enum ENUM_PERIOD enumPeriod,
  260. OUT WCHAR const **ppwszPeriodString)
  261. {
  262. HRESULT hr;
  263. UNITSTABLE const *put;
  264. *ppwszPeriodString = NULL;
  265. for (put = g_aut; put < &g_aut[CUNITSTABLEMAX]; put++)
  266. {
  267. if (enumPeriod == put->enumPeriod)
  268. {
  269. *ppwszPeriodString = put->pwszString;
  270. hr = S_OK;
  271. goto error;
  272. }
  273. }
  274. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  275. error:
  276. CSASSERT(hr == S_OK);
  277. return(hr);
  278. }
  279. HRESULT
  280. myFileTimePeriodToWszTimePeriod(
  281. IN FILETIME const *pftGMT,
  282. IN BOOL fExact,
  283. OUT WCHAR **ppwszTimePeriod)
  284. {
  285. HRESULT hr;
  286. DWORD cPeriodUnits;
  287. PERIODUNITS *rgPeriodUnits = NULL;
  288. WCHAR const *pwszUnitSep;
  289. DWORD i;
  290. WCHAR awc[MAX_PATH];
  291. WCHAR *pwsz;
  292. *ppwszTimePeriod = NULL;
  293. hr = caTranslateFileTimePeriodToPeriodUnits(
  294. pftGMT,
  295. fExact,
  296. &cPeriodUnits,
  297. &rgPeriodUnits);
  298. _JumpIfError(hr, error, "caTranslateFileTimePeriodToPeriodUnits");
  299. CSASSERT(0 < cPeriodUnits);
  300. pwszUnitSep = L"";
  301. pwsz = awc;
  302. for (i = 0; i < cPeriodUnits; i++)
  303. {
  304. WCHAR const *pwszPeriodString;
  305. hr = myTranslateUnlocalizedPeriodString(
  306. rgPeriodUnits[i].enumPeriod,
  307. &pwszPeriodString);
  308. _JumpIfError(hr, error, "myTranslateUnlocalizedPeriodString");
  309. pwsz += wsprintf(
  310. pwsz,
  311. L"%ws%u %ws",
  312. pwszUnitSep,
  313. rgPeriodUnits[i].lCount,
  314. pwszPeriodString);
  315. pwszUnitSep = L", ";
  316. }
  317. hr = myDupString(awc, ppwszTimePeriod);
  318. _JumpIfError(hr, error, "myDupString");
  319. error:
  320. if (NULL != rgPeriodUnits)
  321. {
  322. LocalFree(rgPeriodUnits);
  323. }
  324. return(hr);
  325. }
  326. HRESULT
  327. myFileTimeToDate(
  328. IN FILETIME const *pft,
  329. OUT DATE *pDate)
  330. {
  331. SYSTEMTIME st;
  332. HRESULT hr = S_OK;
  333. if (!FileTimeToSystemTime(pft, &st))
  334. {
  335. hr = myHLastError();
  336. _JumpError(hr, error, "FileTimeToSystemTime");
  337. }
  338. if (!SystemTimeToVariantTime(&st, pDate))
  339. {
  340. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  341. _JumpError(hr, error, "SystemTimeToVariantTime");
  342. }
  343. error:
  344. return(hr);
  345. }
  346. HRESULT
  347. mySystemTimeToGMTSystemTime(
  348. IN OUT SYSTEMTIME *pSys)
  349. {
  350. // Conversion path: SystemTimeLocal -> ftLocal -> ftGMT -> SystemTimeGMT
  351. FILETIME ftLocal, ftGMT;
  352. HRESULT hr = S_OK;
  353. if (!SystemTimeToFileTime(pSys, &ftLocal))
  354. {
  355. hr = myHLastError();
  356. _JumpError(hr, error, "SystemTimeToFileTime");
  357. }
  358. if (!LocalFileTimeToFileTime(&ftLocal, &ftGMT))
  359. {
  360. hr = myHLastError();
  361. _JumpError(hr, error, "LocalFileTimeToFileTime");
  362. }
  363. if (!FileTimeToSystemTime(&ftGMT, pSys))
  364. {
  365. hr = myHLastError();
  366. _JumpError(hr, error, "FileTimeToSystemTime");
  367. }
  368. error:
  369. return hr;
  370. }
  371. HRESULT
  372. myGMTFileTimeToWszLocalTime(
  373. IN FILETIME const *pftGMT,
  374. IN BOOL fSeconds,
  375. OUT WCHAR **ppwszLocalTime)
  376. {
  377. HRESULT hr;
  378. FILETIME ftLocal;
  379. *ppwszLocalTime = NULL;
  380. if (!FileTimeToLocalFileTime(pftGMT, &ftLocal))
  381. {
  382. hr = myHLastError();
  383. _JumpError(hr, error, "FileTimeToLocalFileTime");
  384. }
  385. hr = myFileTimeToWszTime(&ftLocal, fSeconds, ppwszLocalTime);
  386. _JumpIfError(hr, error, "myFileTimeToWszTime");
  387. error:
  388. return(hr);
  389. }
  390. HRESULT
  391. myFileTimeToWszTime(
  392. IN FILETIME const *pft,
  393. IN BOOL fSeconds,
  394. OUT WCHAR **ppwszTime)
  395. {
  396. HRESULT hr;
  397. SYSTEMTIME st;
  398. WCHAR awcDate[128];
  399. WCHAR awcTime[128];
  400. WCHAR *pwsz;
  401. *ppwszTime = NULL;
  402. if (!FileTimeToSystemTime(pft, &st))
  403. {
  404. hr = myHLastError();
  405. _JumpError(hr, error, "FileTimeToSystemTime");
  406. }
  407. if (0 == GetDateFormat(
  408. LOCALE_USER_DEFAULT,
  409. DATE_SHORTDATE,
  410. &st,
  411. NULL,
  412. awcDate,
  413. sizeof(awcDate)/sizeof(awcDate[0])))
  414. {
  415. hr = myHLastError();
  416. _JumpError(hr, error, "GetDateFormat");
  417. }
  418. if (0 == GetTimeFormat(
  419. LOCALE_USER_DEFAULT,
  420. TIME_NOSECONDS,
  421. &st,
  422. NULL,
  423. awcTime,
  424. sizeof(awcTime)/sizeof(awcTime[0])))
  425. {
  426. hr = myHLastError();
  427. _JumpError(hr, error, "GetTimeFormat");
  428. }
  429. if (fSeconds)
  430. {
  431. wsprintf(
  432. &awcTime[wcslen(awcTime)],
  433. L" %02u.%03us",
  434. st.wSecond,
  435. st.wMilliseconds);
  436. }
  437. pwsz = (WCHAR *) LocalAlloc(
  438. LMEM_FIXED,
  439. (wcslen(awcDate) + 1 + wcslen(awcTime) + 1) * sizeof(WCHAR));
  440. if (NULL == pwsz)
  441. {
  442. hr = E_OUTOFMEMORY;
  443. _JumpError(hr, error, "LocalAlloc");
  444. }
  445. wcscpy(pwsz, awcDate);
  446. wcscat(pwsz, L" ");
  447. wcscat(pwsz, awcTime);
  448. *ppwszTime = pwsz;
  449. hr = S_OK;
  450. error:
  451. return(hr);
  452. }
  453. HRESULT
  454. myGMTDateToWszLocalTime(
  455. IN DATE const *pDateGMT,
  456. IN BOOL fSeconds,
  457. OUT WCHAR **ppwszLocalTime)
  458. {
  459. HRESULT hr;
  460. FILETIME ftGMT;
  461. *ppwszLocalTime = NULL;
  462. hr = myDateToFileTime(pDateGMT, &ftGMT);
  463. _JumpIfError(hr, error, "myDateToFileTime");
  464. hr = myGMTFileTimeToWszLocalTime(&ftGMT, fSeconds, ppwszLocalTime);
  465. _JumpIfError(hr, error, "FileTimeToLocalFileTime");
  466. hr = S_OK;
  467. error:
  468. return(hr);
  469. }
  470. HRESULT
  471. myWszLocalTimeToGMTDate(
  472. IN WCHAR const *pwszLocalTime,
  473. OUT DATE *pDateGMT)
  474. {
  475. HRESULT hr;
  476. FILETIME ftGMT;
  477. hr = myWszLocalTimeToGMTFileTime(pwszLocalTime, &ftGMT);
  478. _JumpIfError2(hr, error, "myWszLocalTimeToGMTFileTime", E_INVALIDARG);
  479. hr = myFileTimeToDate(&ftGMT, pDateGMT);
  480. _JumpIfError(hr, error, "myFileTimeToDate");
  481. error:
  482. return(hr);
  483. }
  484. HRESULT
  485. myWszLocalTimeToGMTFileTime(
  486. IN WCHAR const *pwszLocalTime,
  487. OUT FILETIME *pftGMT)
  488. {
  489. HRESULT hr;
  490. time_t time;
  491. USHORT parselen;
  492. struct tm *ptm;
  493. SYSTEMTIME stLocal;
  494. FILETIME ftLocal;
  495. char *pszLocalTime = NULL;
  496. if (!ConvertWszToSz(&pszLocalTime, pwszLocalTime, -1))
  497. {
  498. hr = E_OUTOFMEMORY;
  499. _JumpError(hr, error, "ConvertWszToSz");
  500. }
  501. hr = LUI_ParseDateTime(pszLocalTime, (time_t *) &time, &parselen, 0);
  502. if (S_OK != hr)
  503. {
  504. _PrintError2(hr, "LUI_ParseDateTime", hr);
  505. hr = E_INVALIDARG;
  506. _JumpError2(hr, error, "LUI_ParseDateTime", hr);
  507. }
  508. DBGPRINT((
  509. DBG_SS_CERTLIBI,
  510. "myWszLocalTimeToGMTDate = %ws: %x -- %ws\n",
  511. pwszLocalTime,
  512. time,
  513. _wctime(&time)));
  514. ptm = gmtime(&time);
  515. if (ptm == NULL)
  516. {
  517. hr = E_UNEXPECTED;
  518. _JumpError(hr, error, "gmTime");
  519. }
  520. stLocal.wYear = (WORD) ptm->tm_year + 1900;
  521. stLocal.wMonth = (WORD) ptm->tm_mon + 1;
  522. stLocal.wDayOfWeek = (WORD) ptm->tm_wday;
  523. stLocal.wDay = (WORD) ptm->tm_mday;
  524. stLocal.wHour = (WORD) ptm->tm_hour;
  525. stLocal.wMinute = (WORD) ptm->tm_min;
  526. stLocal.wSecond = (WORD) ptm->tm_sec;
  527. stLocal.wMilliseconds = 0;
  528. DBGPRINT((
  529. DBG_SS_CERTLIBI,
  530. "%u/%u/%u %u:%02u:%02u.%03u DayOfWeek=%u\n",
  531. stLocal.wMonth,
  532. stLocal.wDay,
  533. stLocal.wYear,
  534. stLocal.wHour,
  535. stLocal.wMinute,
  536. stLocal.wSecond,
  537. stLocal.wMilliseconds,
  538. stLocal.wDayOfWeek));
  539. if (!SystemTimeToFileTime(&stLocal, &ftLocal))
  540. {
  541. hr = myHLastError();
  542. _JumpError(hr, error, "SystemTimeToFileTime");
  543. }
  544. if (!LocalFileTimeToFileTime(&ftLocal, pftGMT))
  545. {
  546. hr = myHLastError();
  547. _JumpError(hr, error, "LocalFileTimeToFileTime");
  548. }
  549. error:
  550. if (NULL != pszLocalTime)
  551. {
  552. LocalFree(pszLocalTime);
  553. }
  554. return(hr);
  555. }
  556. // counts # of string in a multisz string
  557. DWORD CountMultiSz(LPCWSTR pcwszString)
  558. {
  559. DWORD dwCount = 0;
  560. if(!pcwszString)
  561. return 0;
  562. while(*pcwszString)
  563. {
  564. dwCount++;
  565. pcwszString += wcslen(pcwszString)+1;
  566. }
  567. return dwCount;
  568. }
  569. //
  570. // myRegValueToVariant and myVariantToRegValue map registry values
  571. // to/from variant:
  572. //
  573. // REG_SZ <-> VT_BSTR
  574. // REG_BINARY <-> VT_ARRAY|VT_UI1
  575. // REG_DWORD <-> VT_I4 (VT_I2)
  576. // REG_MULTI_SZ <-> VT_ARRAY|VT_BSTR
  577. //
  578. HRESULT myRegValueToVariant(
  579. IN DWORD dwType,
  580. IN DWORD cbValue,
  581. IN BYTE const *pbValue,
  582. OUT VARIANT *pVar)
  583. {
  584. HRESULT hr = S_OK;
  585. SAFEARRAYBOUND sab;
  586. LPWSTR pwszCrtString = (LPWSTR)pbValue;
  587. BSTR bstr = NULL;
  588. SAFEARRAY* psa;
  589. VariantInit(pVar);
  590. switch(dwType)
  591. {
  592. case REG_SZ:
  593. if(sizeof(WCHAR)<=cbValue)
  594. cbValue -= sizeof(WCHAR);
  595. V_BSTR(pVar) = NULL;
  596. if (!ConvertWszToBstr(
  597. &(V_BSTR(pVar)),
  598. (WCHAR const *) pbValue,
  599. cbValue))
  600. {
  601. hr = E_OUTOFMEMORY;
  602. _JumpError(hr, error, "ConvertWszToBstr");
  603. }
  604. V_VT(pVar) = VT_BSTR;
  605. break;
  606. case REG_BINARY:
  607. sab.cElements = cbValue;
  608. sab.lLbound = 0;
  609. psa = SafeArrayCreate(
  610. VT_UI1,
  611. 1,
  612. &sab);
  613. if(!psa)
  614. {
  615. hr = E_OUTOFMEMORY;
  616. _JumpError(hr, error, "SafeArrayCreate");
  617. }
  618. for(DWORD c=0; c<sab.cElements; c++)
  619. {
  620. hr = SafeArrayPutElement(psa, (LONG*)&c, (LPVOID)&pbValue[c]);
  621. _JumpIfError(hr, error, "SafeArrayPutElement");
  622. }
  623. V_VT(pVar) = VT_ARRAY|VT_UI1;
  624. V_ARRAY(pVar) = psa;
  625. break;
  626. case REG_DWORD:
  627. V_VT(pVar) = VT_I4;
  628. V_I4(pVar) = *(LONG const *) pbValue;
  629. break;
  630. case REG_MULTI_SZ:
  631. sab.cElements = CountMultiSz((LPCWSTR)pbValue);
  632. sab.lLbound = 0;
  633. psa = SafeArrayCreate(
  634. VT_BSTR,
  635. 1,
  636. &sab);
  637. if(!psa)
  638. {
  639. hr = E_OUTOFMEMORY;
  640. _JumpError(hr, error, "SafeArrayCreate");
  641. }
  642. for(DWORD cStr=0; cStr<sab.cElements; cStr++)
  643. {
  644. if(!ConvertWszToBstr(
  645. &bstr,
  646. pwszCrtString,
  647. -1))
  648. {
  649. hr = E_OUTOFMEMORY;
  650. _JumpError(hr, error, "ConvertWszToBstr");
  651. }
  652. hr = SafeArrayPutElement(psa, (LONG*)&cStr, bstr);
  653. _JumpIfError(hr, error, "SafeArrayPutElement");
  654. pwszCrtString += wcslen(pwszCrtString)+1;
  655. SysFreeString(bstr);
  656. bstr = NULL;
  657. }
  658. V_VT(pVar) = VT_ARRAY|VT_BSTR;
  659. V_ARRAY(pVar) = psa;
  660. break;
  661. default:
  662. hr = E_INVALIDARG;
  663. _JumpError(hr, error, "invalid type");
  664. }
  665. error:
  666. if(bstr)
  667. SysFreeString(bstr);
  668. return hr;
  669. }
  670. HRESULT
  671. myVariantToRegValue(
  672. IN VARIANT const *pvarPropertyValue,
  673. OUT DWORD *pdwType,
  674. OUT DWORD *pcbprop,
  675. OUT BYTE **ppbprop)
  676. {
  677. HRESULT hr = S_OK;
  678. DWORD cbprop = 0;
  679. BYTE *pbprop = NULL; // no free
  680. LONG lLbound, lUbound;
  681. void * pData = NULL;
  682. LPSAFEARRAY psa = NULL; // no free
  683. BSTR bstr = NULL; // no free
  684. *ppbprop = NULL;
  685. *pcbprop = 0;
  686. switch (pvarPropertyValue->vt)
  687. {
  688. case VT_BYREF|VT_BSTR:
  689. case VT_BSTR:
  690. bstr = (pvarPropertyValue->vt & VT_BYREF)?
  691. *V_BSTRREF(pvarPropertyValue):
  692. V_BSTR(pvarPropertyValue);
  693. *pdwType = REG_SZ;
  694. if (NULL == bstr)
  695. {
  696. bstr = L"";
  697. }
  698. pbprop = (BYTE *) bstr;
  699. cbprop = (wcslen(bstr) + 1) * sizeof(WCHAR);
  700. break;
  701. case VT_BYREF|VT_ARRAY|VT_UI1:
  702. case VT_ARRAY|VT_UI1:
  703. psa = (pvarPropertyValue->vt & VT_BYREF)?
  704. *V_ARRAYREF(pvarPropertyValue):
  705. V_ARRAY(pvarPropertyValue);
  706. *pdwType = REG_BINARY;
  707. if(1!=SafeArrayGetDim(psa))
  708. {
  709. hr = E_INVALIDARG;
  710. _JumpError(hr, error, "only 1 dim array of bytes allowed");
  711. }
  712. hr = SafeArrayGetLBound(
  713. psa,
  714. 1,
  715. &lLbound);
  716. _JumpIfError(hr, error, "SafeArrayGetLBound");
  717. hr = SafeArrayGetUBound(
  718. psa,
  719. 1,
  720. &lUbound);
  721. _JumpIfError(hr, error, "SafeArrayGetLBound");
  722. hr = SafeArrayAccessData(psa, &pData);
  723. _JumpIfError(hr, error, "SafeArrayGetLBound");
  724. cbprop = lUbound-lLbound+1;
  725. pbprop = (LPBYTE)pData;
  726. break;
  727. case VT_BYREF|VT_I2:
  728. case VT_I2:
  729. *pdwType = REG_DWORD;
  730. pbprop = (BYTE *)((pvarPropertyValue->vt & VT_BYREF)?
  731. V_I2REF(pvarPropertyValue):
  732. &V_I2(pvarPropertyValue));
  733. cbprop = sizeof(V_I2(pvarPropertyValue));
  734. break;
  735. case VT_BYREF|VT_I4:
  736. case VT_I4:
  737. *pdwType = REG_DWORD;
  738. pbprop = (BYTE *) ((pvarPropertyValue->vt & VT_BYREF)?
  739. V_I4REF(pvarPropertyValue):
  740. &V_I4(pvarPropertyValue));
  741. cbprop = sizeof(pvarPropertyValue->lVal);
  742. break;
  743. case VT_BYREF|VT_ARRAY|VT_BSTR:
  744. case VT_ARRAY|VT_BSTR:
  745. psa = (pvarPropertyValue->vt & VT_BYREF)?
  746. *V_ARRAYREF(pvarPropertyValue):
  747. V_ARRAY(pvarPropertyValue);
  748. *pdwType = REG_MULTI_SZ;
  749. if(1!=SafeArrayGetDim(psa))
  750. {
  751. hr = E_INVALIDARG;
  752. _JumpError(hr, error, "only 1 dim array of bstr allowed");
  753. }
  754. hr = SafeArrayGetLBound(
  755. psa,
  756. 1,
  757. &lLbound);
  758. _JumpIfError(hr, error, "SafeArrayGetLBound");
  759. hr = SafeArrayGetUBound(
  760. psa,
  761. 1,
  762. &lUbound);
  763. _JumpIfError(hr, error, "SafeArrayGetLBound");
  764. hr = SafeArrayAccessData(psa, &pData);
  765. _JumpIfError(hr, error, "SafeArrayGetLBound");
  766. for(LONG c=0;c<=lUbound-lLbound;c++)
  767. {
  768. BSTR str = ((BSTR *) pData)[c];
  769. if (NULL == str)
  770. {
  771. hr = E_POINTER;
  772. _JumpError(hr, error, "BSTR NULL");
  773. }
  774. cbprop += wcslen(str)+1;
  775. }
  776. cbprop += 1;
  777. cbprop *= sizeof(WCHAR);
  778. {
  779. BYTE* pbprop2 = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  780. if (NULL == pbprop2)
  781. {
  782. hr = E_OUTOFMEMORY;
  783. _JumpError(hr, error, "LocalAlloc");
  784. }
  785. *ppbprop = pbprop2;
  786. for(LONG c=0;c<=lUbound-lLbound;c++)
  787. {
  788. wcscpy((LPWSTR)pbprop2, ((BSTR*)pData)[c]);
  789. pbprop2 += (wcslen((LPWSTR)pbprop2)+1)*sizeof(WCHAR);
  790. }
  791. *(LPWSTR)pbprop2 = L'\0';
  792. }
  793. break;
  794. default:
  795. hr = E_INVALIDARG;
  796. _JumpError(hr, error, "invalid variant type");
  797. }
  798. if (NULL != pbprop)
  799. {
  800. *ppbprop = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  801. if (NULL == *ppbprop)
  802. {
  803. hr = E_OUTOFMEMORY;
  804. _JumpError(hr, error, "LocalAlloc");
  805. }
  806. CopyMemory(*ppbprop, pbprop, cbprop);
  807. }
  808. *pcbprop = cbprop;
  809. error:
  810. if(pData)
  811. {
  812. CSASSERT(psa);
  813. SafeArrayUnaccessData(psa);
  814. }
  815. return(hr);
  816. }
  817. HRESULT
  818. myUnmarshalVariant(
  819. IN DWORD PropType,
  820. IN DWORD cbValue,
  821. IN BYTE const *pbValue,
  822. OUT VARIANT *pvarValue)
  823. {
  824. HRESULT hr = S_OK;
  825. CSASSERT(NULL != pvarValue);
  826. VariantInit(pvarValue);
  827. // pb = NULL, cb = 0 always returns VT_EMPTY
  828. if (NULL == pbValue)
  829. {
  830. CSASSERT(0 == cbValue);
  831. CSASSERT(VT_EMPTY == pvarValue->vt);
  832. goto error;
  833. }
  834. switch (PROPTYPE_MASK & PropType)
  835. {
  836. case PROPTYPE_STRING:
  837. if (0 == (PROPMARSHAL_LOCALSTRING & PropType) &&
  838. sizeof(WCHAR) <= cbValue)
  839. {
  840. cbValue -= sizeof(WCHAR);
  841. }
  842. if (*(WCHAR const*)(pbValue+cbValue) != L'\0' &&
  843. wcslen((WCHAR const *) pbValue) * sizeof(WCHAR) != cbValue)
  844. {
  845. hr = E_INVALIDARG;
  846. _JumpError(hr, error, "bad string len");
  847. }
  848. // FALLTHROUGH:
  849. case PROPTYPE_BINARY:
  850. // CSASSERT(0 != cbValue);
  851. pvarValue->bstrVal = NULL;
  852. if (!ConvertWszToBstr(
  853. &pvarValue->bstrVal,
  854. (WCHAR const *) pbValue,
  855. cbValue))
  856. {
  857. hr = E_OUTOFMEMORY;
  858. _JumpError(hr, error, "ConvertWszToBstr");
  859. }
  860. pvarValue->vt = VT_BSTR;
  861. break;
  862. case PROPTYPE_LONG:
  863. if (sizeof(LONG) != cbValue)
  864. {
  865. hr = E_INVALIDARG;
  866. _JumpError(hr, error, "bad PROPTYPE_LONG len");
  867. }
  868. pvarValue->vt = VT_I4;
  869. pvarValue->lVal = *(LONG const *) pbValue;
  870. break;
  871. case PROPTYPE_DATE:
  872. if (sizeof(FILETIME) != cbValue)
  873. {
  874. hr = E_INVALIDARG;
  875. _JumpError(hr, error, "bad PROPTYPE_DATE len");
  876. }
  877. hr = myFileTimeToDate(
  878. (FILETIME const *) pbValue,
  879. &pvarValue->date);
  880. _JumpIfError(hr, error, "myFileTimeToDate");
  881. pvarValue->vt = VT_DATE;
  882. break;
  883. default:
  884. hr = E_INVALIDARG;
  885. _JumpError(hr, error, "PropType parameter");
  886. }
  887. error:
  888. return(hr);
  889. }
  890. HRESULT
  891. myUnmarshalFormattedVariant(
  892. IN DWORD Flags,
  893. IN DWORD PropId,
  894. IN DWORD PropType,
  895. IN DWORD cbValue,
  896. IN BYTE const *pbValue,
  897. OUT VARIANT *pvarValue)
  898. {
  899. HRESULT hr;
  900. BSTR strCert;
  901. hr = myUnmarshalVariant(
  902. PropType,
  903. cbValue,
  904. pbValue,
  905. pvarValue);
  906. _JumpIfError(hr, error, "myUnmarshalVariant");
  907. if (PROPTYPE_BINARY == (PROPTYPE_MASK & PropType))
  908. {
  909. CSASSERT(VT_BSTR == pvarValue->vt);
  910. CSASSERT(CSExpr(CV_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER));
  911. CSASSERT(CSExpr(CV_OUT_BASE64 == CRYPT_STRING_BASE64));
  912. CSASSERT(CSExpr(CV_OUT_BINARY == CRYPT_STRING_BINARY));
  913. CSASSERT(CSExpr(CV_OUT_BASE64REQUESTHEADER == CRYPT_STRING_BASE64REQUESTHEADER));
  914. CSASSERT(CSExpr(CV_OUT_HEX == CRYPT_STRING_HEX));
  915. CSASSERT(CSExpr(CV_OUT_HEXASCII == CRYPT_STRING_HEXASCII));
  916. CSASSERT(CSExpr(CV_OUT_HEXADDR == CRYPT_STRING_HEXADDR));
  917. CSASSERT(CSExpr(CV_OUT_HEXASCIIADDR == CRYPT_STRING_HEXASCIIADDR));
  918. switch (Flags)
  919. {
  920. case CV_OUT_BASE64HEADER:
  921. if (CR_PROP_BASECRL == PropId || CR_PROP_DELTACRL == PropId)
  922. {
  923. Flags = CV_OUT_BASE64X509CRLHEADER;
  924. }
  925. else
  926. if (MAXDWORD == PropId)
  927. {
  928. Flags = CV_OUT_BASE64REQUESTHEADER;
  929. }
  930. break;
  931. case CV_OUT_BASE64:
  932. case CV_OUT_BINARY:
  933. case CV_OUT_BASE64REQUESTHEADER:
  934. case CV_OUT_BASE64X509CRLHEADER:
  935. case CV_OUT_HEX:
  936. case CV_OUT_HEXASCII:
  937. case CV_OUT_HEXADDR:
  938. case CV_OUT_HEXASCIIADDR:
  939. break;
  940. default:
  941. hr = E_INVALIDARG;
  942. _JumpError(hr, error, "Flags");
  943. }
  944. if (CV_OUT_BINARY != Flags)
  945. {
  946. strCert = NULL;
  947. hr = EncodeCertString(pbValue, cbValue, Flags, &strCert);
  948. _JumpIfError(hr, error, "EncodeCertString");
  949. SysFreeString(pvarValue->bstrVal);
  950. pvarValue->bstrVal = strCert;
  951. }
  952. }
  953. hr = S_OK;
  954. error:
  955. return(hr);
  956. }
  957. HRESULT
  958. myMarshalVariant(
  959. IN VARIANT const *pvarPropertyValue,
  960. IN DWORD PropType,
  961. OUT DWORD *pcbprop,
  962. OUT BYTE **ppbprop)
  963. {
  964. HRESULT hr = S_OK;
  965. DWORD cbprop = 0;
  966. BYTE *pbprop = NULL;
  967. BSTR str = NULL;
  968. FILETIME ft;
  969. LONG lval;
  970. *ppbprop = NULL;
  971. // VT_EMPTY always produces same result: *ppbprop = NULL, *pcbprop = 0
  972. if (VT_EMPTY != pvarPropertyValue->vt)
  973. {
  974. switch (PROPTYPE_MASK & PropType)
  975. {
  976. case PROPTYPE_BINARY:
  977. case PROPTYPE_STRING:
  978. switch (pvarPropertyValue->vt)
  979. {
  980. case VT_BYREF | VT_BSTR:
  981. if (NULL != pvarPropertyValue->pbstrVal)
  982. {
  983. str = *pvarPropertyValue->pbstrVal;
  984. }
  985. break;
  986. case VT_BSTR:
  987. str = pvarPropertyValue->bstrVal;
  988. break;
  989. }
  990. if (NULL == str)
  991. {
  992. if (PROPTYPE_STRING == (PROPTYPE_MASK & PropType) &&
  993. (PROPMARSHAL_NULLBSTROK & PropType) &&
  994. VT_NULL == pvarPropertyValue->vt)
  995. {
  996. cbprop = 0;
  997. }
  998. else
  999. {
  1000. hr = E_INVALIDARG;
  1001. _JumpError(
  1002. pvarPropertyValue->vt,
  1003. error,
  1004. "variant BSTR type/value");
  1005. }
  1006. }
  1007. else
  1008. {
  1009. pbprop = (BYTE *) str;
  1010. if (PROPTYPE_BINARY == (PROPTYPE_MASK & PropType))
  1011. {
  1012. cbprop = SysStringByteLen(str) + sizeof(WCHAR);
  1013. }
  1014. else
  1015. {
  1016. cbprop = (wcslen(str) + 1) * sizeof(WCHAR);
  1017. }
  1018. }
  1019. break;
  1020. case PROPTYPE_LONG:
  1021. // VB likes to send small constant integers as VT_I2
  1022. if (VT_I2 == pvarPropertyValue->vt)
  1023. {
  1024. lval = pvarPropertyValue->iVal;
  1025. }
  1026. else if (VT_I4 == pvarPropertyValue->vt)
  1027. {
  1028. lval = pvarPropertyValue->lVal;
  1029. }
  1030. else if (VT_EMPTY == pvarPropertyValue->vt)
  1031. {
  1032. pbprop = NULL;
  1033. cbprop = 0;
  1034. break;
  1035. }
  1036. else
  1037. {
  1038. hr = E_INVALIDARG;
  1039. _JumpError(pvarPropertyValue->vt, error, "variant LONG type");
  1040. }
  1041. pbprop = (BYTE *) &lval;
  1042. cbprop = sizeof(lval);
  1043. break;
  1044. case PROPTYPE_DATE:
  1045. if (VT_DATE == pvarPropertyValue->vt)
  1046. {
  1047. hr = myDateToFileTime(&pvarPropertyValue->date, &ft);
  1048. _JumpIfError(hr, error, "myDateToFileTime");
  1049. }
  1050. else if (VT_EMPTY == pvarPropertyValue->vt)
  1051. {
  1052. pbprop = NULL;
  1053. cbprop = 0;
  1054. break;
  1055. }
  1056. else
  1057. {
  1058. hr = E_INVALIDARG;
  1059. _JumpError(pvarPropertyValue->vt, error, "variant DATE type");
  1060. }
  1061. pbprop = (BYTE *) &ft;
  1062. cbprop = sizeof(ft);
  1063. break;
  1064. default:
  1065. hr = E_INVALIDARG;
  1066. _JumpError(pvarPropertyValue->vt, error, "variant type/value");
  1067. }
  1068. if (NULL != pbprop)
  1069. {
  1070. *ppbprop = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  1071. if (NULL == *ppbprop)
  1072. {
  1073. hr = E_OUTOFMEMORY;
  1074. _JumpError(hr, error, "LocalAlloc");
  1075. }
  1076. CopyMemory(*ppbprop, pbprop, cbprop);
  1077. if (NULL != str &&
  1078. sizeof(WCHAR) <= cbprop &&
  1079. ((PROPMARSHAL_LOCALSTRING & PropType) ||
  1080. PROPTYPE_BINARY == (PROPTYPE_MASK & PropType)))
  1081. {
  1082. cbprop -= sizeof(WCHAR);
  1083. }
  1084. }
  1085. }
  1086. *pcbprop = cbprop;
  1087. error:
  1088. return(hr);
  1089. }
  1090. BOOL
  1091. myIsMinusSignString(
  1092. IN WCHAR const *pwsz)
  1093. {
  1094. return(
  1095. NULL != pwsz &&
  1096. L'\0' != pwsz[0] &&
  1097. L'\0' == pwsz[1] &&
  1098. myIsMinusSign(pwsz[0]));
  1099. }
  1100. // IsCharRegKeyChar -- Determines if a character is valid for use in a file
  1101. // name AND in a registry key name.
  1102. #define wszInvalidFileAndKeyChars L"<>\"/\\:|?*"
  1103. #define wszUnsafeURLChars L"#\"&<>[]^`{}|"
  1104. #define wszUnsafeDSChars L"()='\"`,;+"
  1105. BOOL
  1106. myIsCharSanitized(
  1107. IN WCHAR wc)
  1108. {
  1109. BOOL fCharOk = TRUE;
  1110. if (L' ' > wc ||
  1111. L'~' < wc ||
  1112. NULL != wcschr(
  1113. wszInvalidFileAndKeyChars
  1114. wszUnsafeURLChars
  1115. wszSANITIZEESCAPECHAR
  1116. wszURLESCAPECHAR
  1117. wszUnsafeDSChars,
  1118. wc))
  1119. {
  1120. fCharOk = FALSE;
  1121. }
  1122. return(fCharOk);
  1123. }
  1124. HRESULT
  1125. mySanitizeName(
  1126. IN WCHAR const *pwszName,
  1127. OUT WCHAR **ppwszNameOut)
  1128. {
  1129. HRESULT hr = S_OK;
  1130. WCHAR const *pwszPassedName;
  1131. WCHAR *pwszDst;
  1132. WCHAR *pwszOut = NULL;
  1133. WCHAR wcChar;
  1134. DWORD dwSize;
  1135. *ppwszNameOut = NULL;
  1136. pwszPassedName = pwszName;
  1137. dwSize = 0;
  1138. if (NULL == pwszName)
  1139. {
  1140. hr = E_POINTER;
  1141. _JumpError(hr, error, "pwszName NULL");
  1142. }
  1143. while (L'\0' != (wcChar = *pwszPassedName++))
  1144. {
  1145. if (myIsCharSanitized(wcChar))
  1146. {
  1147. dwSize++;
  1148. }
  1149. else
  1150. {
  1151. dwSize += 5; // format !XXXX
  1152. }
  1153. }
  1154. if (0 == dwSize)
  1155. {
  1156. goto error; // done
  1157. }
  1158. pwszOut = (WCHAR *) LocalAlloc(LMEM_ZEROINIT, (dwSize + 1) * sizeof(WCHAR));
  1159. if (NULL == pwszOut)
  1160. {
  1161. hr = E_OUTOFMEMORY;
  1162. _JumpError(hr, error, "LocalAlloc");
  1163. }
  1164. pwszDst = pwszOut;
  1165. while (L'\0' != (wcChar = *pwszName++))
  1166. {
  1167. if (myIsCharSanitized(wcChar))
  1168. {
  1169. *pwszDst++ = wcChar;
  1170. }
  1171. else
  1172. {
  1173. pwszDst += wsprintf(
  1174. pwszDst,
  1175. L"%ws%04x",
  1176. wszSANITIZEESCAPECHAR,
  1177. wcChar);
  1178. }
  1179. }
  1180. *pwszDst = wcChar; // L'\0' terminator
  1181. *ppwszNameOut = pwszOut;
  1182. pwszOut = NULL;
  1183. hr = S_OK;
  1184. error:
  1185. if (NULL != pwszOut)
  1186. {
  1187. LocalFree(pwszOut);
  1188. }
  1189. return(hr);
  1190. }
  1191. BOOL
  1192. ScanHexEscapeDigits(
  1193. IN WCHAR const *pwszHex,
  1194. OUT WCHAR *pwcRevert)
  1195. {
  1196. BOOL ret = FALSE;
  1197. DWORD i;
  1198. WCHAR wc;
  1199. WCHAR wszValue[5];
  1200. for (i = 0; i < 4; i++)
  1201. {
  1202. wc = pwszHex[i];
  1203. wszValue[i] = wc;
  1204. if (!isascii(wc) || !isxdigit((char) wc))
  1205. {
  1206. goto error;
  1207. }
  1208. }
  1209. wszValue[4] = L'\0';
  1210. if (1 != swscanf(wszValue, L"%04x", &i))
  1211. {
  1212. goto error;
  1213. }
  1214. *pwcRevert = (WCHAR) i;
  1215. ret = TRUE;
  1216. error:
  1217. return(ret);
  1218. }
  1219. // This function will truncate the output if pwszName contains "!0000".
  1220. // The output string is L'\0' terminated, so the length is not returned.
  1221. HRESULT
  1222. myRevertSanitizeName(
  1223. IN WCHAR const *pwszName,
  1224. OUT WCHAR **ppwszNameOut)
  1225. {
  1226. HRESULT hr;
  1227. DWORD cwc;
  1228. WCHAR wc;
  1229. WCHAR wcRevert;
  1230. WCHAR *pwszRevert;
  1231. *ppwszNameOut = NULL;
  1232. if (NULL == pwszName)
  1233. {
  1234. hr = E_POINTER;
  1235. _JumpError(hr, error, "NULL sanitized name");
  1236. }
  1237. cwc = wcslen(pwszName);
  1238. *ppwszNameOut = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  1239. if (NULL == *ppwszNameOut)
  1240. {
  1241. hr = E_OUTOFMEMORY;
  1242. _JumpError(hr, error, "Out of memory");
  1243. }
  1244. pwszRevert = *ppwszNameOut;
  1245. while (L'\0' != *pwszName)
  1246. {
  1247. wc = *pwszName++;
  1248. if (wcSANITIZEESCAPECHAR == wc &&
  1249. ScanHexEscapeDigits(pwszName, &wcRevert))
  1250. {
  1251. wc = wcRevert;
  1252. pwszName += 4;
  1253. }
  1254. *pwszRevert++ = wc;
  1255. }
  1256. *pwszRevert = L'\0';
  1257. CSASSERT(wcslen(*ppwszNameOut) <= cwc);
  1258. hr = S_OK;
  1259. error:
  1260. return hr;
  1261. }
  1262. #define cwcCNMAX 64 // 64 chars max for DS CN
  1263. #define cwcCHOPHASHMAX (1 + 5) // "-%05hu" decimal USHORT hash digits
  1264. #define cwcCHOPBASE (cwcCNMAX - (cwcCHOPHASHMAX + cwcSUFFIXMAX))
  1265. HRESULT
  1266. mySanitizedNameToDSName(
  1267. IN WCHAR const *pwszSanitizedName,
  1268. OUT WCHAR **ppwszNameOut)
  1269. {
  1270. HRESULT hr;
  1271. DWORD cwc;
  1272. DWORD cwcCopy;
  1273. WCHAR wszDSName[cwcCHOPBASE + cwcCHOPHASHMAX + 1];
  1274. *ppwszNameOut = NULL;
  1275. if (NULL == pwszSanitizedName)
  1276. {
  1277. hr = E_POINTER;
  1278. _JumpError(hr, error, "NULL sanitized name");
  1279. }
  1280. cwc = wcslen(pwszSanitizedName);
  1281. cwcCopy = cwc;
  1282. if (cwcCHOPBASE < cwcCopy)
  1283. {
  1284. cwcCopy = cwcCHOPBASE;
  1285. }
  1286. CopyMemory(wszDSName, pwszSanitizedName, cwcCopy * sizeof(WCHAR));
  1287. wszDSName[cwcCopy] = L'\0';
  1288. if (cwcCHOPBASE < cwc)
  1289. {
  1290. // Hash the rest of the name into a USHORT
  1291. USHORT usHash = 0;
  1292. DWORD i;
  1293. WCHAR *pwsz;
  1294. // Truncate an incomplete sanitized Unicode character
  1295. pwsz = wcsrchr(wszDSName, L'!');
  1296. if (NULL != pwsz && wcslen(pwsz) < 5)
  1297. {
  1298. cwcCopy -= wcslen(pwsz);
  1299. *pwsz = L'\0';
  1300. }
  1301. for (i = cwcCopy; i < cwc; i++)
  1302. {
  1303. USHORT usLowBit = (USHORT) ((0x8000 & usHash)? 1 : 0);
  1304. usHash = ((usHash << 1) | usLowBit) + pwszSanitizedName[i];
  1305. }
  1306. wsprintf(&wszDSName[cwcCopy], L"-%05hu", usHash);
  1307. CSASSERT(wcslen(wszDSName) < ARRAYSIZE(wszDSName));
  1308. }
  1309. hr = myDupString(wszDSName, ppwszNameOut);
  1310. _JumpIfError(hr, error, "myDupString");
  1311. DBGPRINT((DBG_SS_CERTLIBI, "mySanitizedNameToDSName(%ws)\n", *ppwszNameOut));
  1312. error:
  1313. return(hr);
  1314. }
  1315. HRESULT
  1316. CertNameToHashString(
  1317. IN CERT_NAME_BLOB const *pCertName,
  1318. OUT WCHAR **ppwszHash)
  1319. {
  1320. HRESULT hr = S_OK;
  1321. WCHAR wszHash[CBMAX_CRYPT_HASH_LEN * 3]; // 20 bytes @ 3 WCHARs/byte
  1322. DWORD cbString;
  1323. HCRYPTPROV hProv = NULL;
  1324. HCRYPTHASH hHash = NULL;
  1325. BYTE rgbHashVal[CBMAX_CRYPT_HASH_LEN];
  1326. DWORD cbHashVal;
  1327. if (0 == pCertName->cbData)
  1328. {
  1329. hr = E_INVALIDARG;
  1330. _JumpError(hr, error, "empty cert name");
  1331. }
  1332. CSASSERT(NULL != ppwszHash);
  1333. // get a cryptographic provider
  1334. if (!CryptAcquireContext(
  1335. &hProv,
  1336. NULL, // container
  1337. MS_DEF_PROV, // provider name
  1338. PROV_RSA_FULL, // provider type
  1339. CRYPT_VERIFYCONTEXT)) // dwflags
  1340. {
  1341. hr = myHLastError();
  1342. _JumpError(hr, error, "CryptAcquireContext");
  1343. }
  1344. // get a hash
  1345. if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
  1346. {
  1347. hr = myHLastError();
  1348. _JumpError(hr, error, "CryptCreateHash");
  1349. }
  1350. // hash the name
  1351. if (!CryptHashData(hHash, pCertName->pbData, pCertName->cbData, 0))
  1352. {
  1353. hr = myHLastError();
  1354. _JumpError(hr, error, "CryptHashData");
  1355. }
  1356. cbHashVal = CBMAX_CRYPT_HASH_LEN;
  1357. if (!CryptGetHashParam(hHash, HP_HASHVAL, rgbHashVal, &cbHashVal, 0))
  1358. {
  1359. hr = myHLastError();
  1360. _JumpError(hr, error, "CryptGetHashParam");
  1361. }
  1362. cbString = sizeof(wszHash);
  1363. hr = MultiByteIntegerToWszBuf(
  1364. TRUE, // byte multiple
  1365. cbHashVal,
  1366. rgbHashVal,
  1367. &cbString,
  1368. wszHash);
  1369. _JumpIfError(hr, error, "MultiByteIntegerToWszBuf");
  1370. // Generated string looks like this:
  1371. //
  1372. // 04 e7 23 92 98 9f d8 45 80 c9 ef 87 81 29 41 5d bc 4f 63 20
  1373. //
  1374. // We need to trim the spaces. We'll do it inplace.
  1375. {
  1376. WCHAR *pwcSrc;
  1377. WCHAR *pwcDest;
  1378. for (pwcSrc = pwcDest = wszHash; L'\0' != *pwcSrc; pwcSrc++)
  1379. {
  1380. if (L' ' != *pwcSrc)
  1381. {
  1382. *pwcDest++ = *pwcSrc;
  1383. }
  1384. }
  1385. *pwcDest = L'\0';
  1386. }
  1387. *ppwszHash = (WCHAR *) LocalAlloc(LMEM_FIXED, cbString);
  1388. _JumpIfAllocFailed(*ppwszHash, error);
  1389. wcscpy(*ppwszHash, wszHash);
  1390. error:
  1391. if (NULL != hHash)
  1392. {
  1393. CryptDestroyHash(hHash);
  1394. }
  1395. if (NULL != hProv)
  1396. {
  1397. CryptReleaseContext(hProv, 0);
  1398. }
  1399. return(hr);
  1400. }
  1401. HRESULT
  1402. myCombineStrings(
  1403. IN WCHAR const *pwszNew,
  1404. IN BOOL fAppendNew,
  1405. OPTIONAL IN WCHAR const *pwszSeparator,
  1406. IN OUT WCHAR **ppwszInOut)
  1407. {
  1408. HRESULT hr;
  1409. DWORD cwc;
  1410. WCHAR *pwsz;
  1411. if (NULL == pwszSeparator)
  1412. {
  1413. pwszSeparator = L"";
  1414. }
  1415. cwc = wcslen(pwszNew);
  1416. if (NULL != *ppwszInOut)
  1417. {
  1418. cwc += wcslen(*ppwszInOut) + wcslen(pwszSeparator);
  1419. }
  1420. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  1421. if (NULL == pwsz)
  1422. {
  1423. hr = E_OUTOFMEMORY;
  1424. _JumpError(hr, error, "Policy:LocalAlloc");
  1425. }
  1426. *pwsz = L'\0';
  1427. if (fAppendNew)
  1428. {
  1429. if (NULL != *ppwszInOut)
  1430. {
  1431. wcscat(pwsz, *ppwszInOut);
  1432. wcscat(pwsz, pwszSeparator);
  1433. }
  1434. wcscat(pwsz, pwszNew);
  1435. }
  1436. else
  1437. {
  1438. wcscat(pwsz, pwszNew);
  1439. if (NULL != *ppwszInOut)
  1440. {
  1441. wcscat(pwsz, pwszSeparator);
  1442. wcscat(pwsz, *ppwszInOut);
  1443. }
  1444. }
  1445. if (NULL != *ppwszInOut)
  1446. {
  1447. LocalFree(*ppwszInOut);
  1448. }
  1449. *ppwszInOut = pwsz;
  1450. hr = S_OK;
  1451. error:
  1452. return(hr);
  1453. }
  1454. HRESULT
  1455. myAppendString(
  1456. IN WCHAR const *pwszNew,
  1457. OPTIONAL IN WCHAR const *pwszSeparator,
  1458. IN OUT WCHAR **ppwszInOut)
  1459. {
  1460. return(myCombineStrings(pwszNew, TRUE, pwszSeparator, ppwszInOut));
  1461. }
  1462. HRESULT
  1463. myPrependString(
  1464. IN WCHAR const *pwszNew,
  1465. OPTIONAL IN WCHAR const *pwszSeparator,
  1466. IN OUT WCHAR **ppwszInOut)
  1467. {
  1468. return(myCombineStrings(pwszNew, FALSE, pwszSeparator, ppwszInOut));
  1469. }
  1470. HRESULT
  1471. myGetRDNAttributeFromNameBlob(
  1472. IN CERT_NAME_BLOB const *pNameBlob,
  1473. IN LPCSTR pcszAttributeOID,
  1474. OUT WCHAR **ppwszCN)
  1475. {
  1476. HRESULT hr;
  1477. CERT_NAME_INFO *pNameInfo = NULL;
  1478. DWORD cbNameInfo;
  1479. CERT_RDN *prdn;
  1480. CERT_RDN *prdnEnd;
  1481. WCHAR const *pwszCN = NULL;
  1482. *ppwszCN = NULL;
  1483. if (!myDecodeName(
  1484. X509_ASN_ENCODING,
  1485. X509_UNICODE_NAME,
  1486. pNameBlob->pbData,
  1487. pNameBlob->cbData,
  1488. CERTLIB_USE_LOCALALLOC,
  1489. &pNameInfo,
  1490. &cbNameInfo))
  1491. {
  1492. hr = myHLastError();
  1493. _JumpError(hr, error, "myDecodeName");
  1494. }
  1495. for (
  1496. prdn = pNameInfo->rgRDN, prdnEnd = &prdn[pNameInfo->cRDN];
  1497. NULL == pwszCN && prdn < prdnEnd;
  1498. prdn++)
  1499. {
  1500. CERT_RDN_ATTR *prdna;
  1501. CERT_RDN_ATTR *prdnaEnd;
  1502. for (
  1503. prdna = prdn->rgRDNAttr, prdnaEnd = &prdna[prdn->cRDNAttr];
  1504. prdna < prdnaEnd;
  1505. prdna++)
  1506. {
  1507. CSASSERT(
  1508. prdna->dwValueType == CERT_RDN_PRINTABLE_STRING ||
  1509. prdna->dwValueType == CERT_RDN_UNICODE_STRING ||
  1510. prdna->dwValueType == CERT_RDN_TELETEX_STRING ||
  1511. prdna->dwValueType == CERT_RDN_IA5_STRING ||
  1512. prdna->dwValueType == CERT_RDN_UTF8_STRING);
  1513. if (0 != strcmp(pcszAttributeOID, prdna->pszObjId) ||
  1514. NULL == prdna->Value.pbData ||
  1515. sizeof(WCHAR) > prdna->Value.cbData ||
  1516. L'\0' == *(WCHAR *) prdna->Value.pbData)
  1517. {
  1518. continue;
  1519. }
  1520. pwszCN = (WCHAR const *) prdna->Value.pbData;
  1521. //break; don't break, we're interested in the last CN found, not the first
  1522. }
  1523. }
  1524. if (NULL != pwszCN)
  1525. {
  1526. hr = myDupString(pwszCN, ppwszCN);
  1527. _JumpIfError(hr, error, "myDupString");
  1528. }
  1529. else
  1530. {
  1531. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  1532. }
  1533. error:
  1534. if (NULL != pNameInfo)
  1535. {
  1536. LocalFree(pNameInfo);
  1537. }
  1538. return(hr);
  1539. }
  1540. HRESULT
  1541. myGetCommonName(
  1542. IN CERT_NAME_BLOB const *pNameBlob,
  1543. IN BOOL fAllowDefault,
  1544. OUT WCHAR **ppwszCN)
  1545. {
  1546. HRESULT hr;
  1547. hr = myGetRDNAttributeFromNameBlob(pNameBlob, szOID_COMMON_NAME, ppwszCN);
  1548. if (S_OK != hr)
  1549. {
  1550. _PrintError(hr, "myGetRDNAttributeFromNameBlob");
  1551. *ppwszCN = NULL;
  1552. if (HRESULT_FROM_WIN32(ERROR_NOT_FOUND) != hr)
  1553. {
  1554. _JumpError(hr, error, "myGetRDNAttributeFromNameBlob");
  1555. }
  1556. }
  1557. if (NULL == *ppwszCN)
  1558. {
  1559. if (!fAllowDefault)
  1560. {
  1561. hr = CERTSRV_E_BAD_REQUESTSUBJECT;
  1562. _JumpError(hr, error, "No CN");
  1563. }
  1564. hr = CertNameToHashString(pNameBlob, ppwszCN);
  1565. _JumpIfError(hr, error, "CertNameToHashString");
  1566. }
  1567. hr = S_OK;
  1568. error:
  1569. return(hr);
  1570. }
  1571. // Decode OCTET string, and convert UTF8 string to Unicode.
  1572. // Can return S_OK and NULL pointer.
  1573. HRESULT
  1574. myDecodeCMCRegInfo(
  1575. IN BYTE const *pbOctet,
  1576. IN DWORD cbOctet,
  1577. OUT WCHAR **ppwszRA)
  1578. {
  1579. HRESULT hr;
  1580. CRYPT_DATA_BLOB *pBlob = NULL;
  1581. DWORD cb;
  1582. *ppwszRA = NULL;
  1583. if (!myDecodeObject(
  1584. X509_ASN_ENCODING,
  1585. X509_OCTET_STRING,
  1586. pbOctet,
  1587. cbOctet,
  1588. CERTLIB_USE_LOCALALLOC,
  1589. (VOID **) &pBlob,
  1590. &cb))
  1591. {
  1592. hr = myHLastError();
  1593. _JumpError(hr, error, "myDecodeObject");
  1594. }
  1595. if (0 != pBlob->cbData && NULL != pBlob->pbData)
  1596. {
  1597. if (!myConvertUTF8ToWsz(
  1598. ppwszRA,
  1599. (CHAR const *) pBlob->pbData,
  1600. pBlob->cbData))
  1601. {
  1602. hr = myHLastError();
  1603. _JumpError(hr, error, "myConvertUTF8ToWsz");
  1604. }
  1605. }
  1606. hr = S_OK;
  1607. error:
  1608. if (NULL != pBlob)
  1609. {
  1610. LocalFree(pBlob);
  1611. }
  1612. return(hr);
  1613. }
  1614. DWORD
  1615. myGetCertNameProperty(
  1616. IN BOOL fFirstRDN, // else last matching RDN
  1617. IN CERT_NAME_INFO const *pNameInfo,
  1618. IN char const *pszObjId,
  1619. OUT WCHAR const **ppwszName)
  1620. {
  1621. HRESULT hr;
  1622. CERT_RDN_ATTR const *prdnaT;
  1623. CERT_RDN const *prdn;
  1624. CERT_RDN const *prdnEnd;
  1625. prdnaT = NULL;
  1626. for (
  1627. prdn = pNameInfo->rgRDN, prdnEnd = &prdn[pNameInfo->cRDN];
  1628. prdn < prdnEnd;
  1629. prdn++)
  1630. {
  1631. CERT_RDN_ATTR *prdna;
  1632. CERT_RDN_ATTR *prdnaEnd;
  1633. for (
  1634. prdna = prdn->rgRDNAttr, prdnaEnd = &prdna[prdn->cRDNAttr];
  1635. prdna < prdnaEnd;
  1636. prdna++)
  1637. {
  1638. if (0 == strcmp(prdna->pszObjId, pszObjId))
  1639. {
  1640. prdnaT = prdna;
  1641. if (fFirstRDN)
  1642. {
  1643. goto done;
  1644. }
  1645. }
  1646. }
  1647. }
  1648. if (NULL == prdnaT)
  1649. {
  1650. hr = CERTSRV_E_PROPERTY_EMPTY;
  1651. goto error;
  1652. }
  1653. done:
  1654. *ppwszName = (WCHAR const *) prdnaT->Value.pbData;
  1655. hr = S_OK;
  1656. error:
  1657. return(hr);
  1658. }
  1659. VOID
  1660. myUuidCreate(
  1661. OUT UUID *pUuid)
  1662. {
  1663. HRESULT hr;
  1664. BYTE *pb;
  1665. ZeroMemory(pUuid, sizeof(*pUuid));
  1666. hr = UuidCreate(pUuid);
  1667. if (S_OK != hr)
  1668. {
  1669. BYTE *pbEnd;
  1670. CSASSERT((HRESULT) RPC_S_UUID_LOCAL_ONLY == hr);
  1671. // No net card? Fake up a GUID:
  1672. pb = (BYTE *) pUuid;
  1673. pbEnd = (BYTE *) pb + sizeof(*pUuid);
  1674. GetSystemTimeAsFileTime((FILETIME *) pb);
  1675. pb += sizeof(FILETIME);
  1676. while (pb < pbEnd)
  1677. {
  1678. *(DWORD *) pb = GetTickCount();
  1679. pb += sizeof(DWORD);
  1680. }
  1681. CSASSERT(pb == pbEnd);
  1682. }
  1683. }
  1684. VOID
  1685. myGenerateGuidSerialNumber(
  1686. OUT GUID *pguidSerialNumber)
  1687. {
  1688. BYTE *pb;
  1689. myUuidCreate(pguidSerialNumber);
  1690. pb = &((BYTE *) pguidSerialNumber)[sizeof(*pguidSerialNumber) - 1];
  1691. // make sure the last byte is never zero
  1692. if (0 == *pb)
  1693. {
  1694. *pb = 'z';
  1695. }
  1696. // Some clients can't handle negative serial numbers:
  1697. *pb &= 0x7f;
  1698. }
  1699. BOOL
  1700. myAreBlobsSame(
  1701. IN BYTE const *pbData1,
  1702. IN DWORD cbData1,
  1703. IN BYTE const *pbData2,
  1704. IN DWORD cbData2)
  1705. {
  1706. BOOL ret = FALSE;
  1707. if (cbData1 != cbData2)
  1708. {
  1709. goto error;
  1710. }
  1711. if (NULL != pbData1 && NULL != pbData2)
  1712. {
  1713. if (0 != memcmp(pbData1, pbData2, cbData1))
  1714. {
  1715. goto error;
  1716. }
  1717. }
  1718. // else at least one is NULL -- they'd better both be NULL, & the count 0.
  1719. else if (pbData1 != pbData2 || 0 != cbData1)
  1720. {
  1721. goto error;
  1722. }
  1723. ret = TRUE;
  1724. error:
  1725. return(ret);
  1726. }
  1727. BOOL
  1728. myAreSerialNumberBlobsSame(
  1729. IN CRYPT_INTEGER_BLOB const *pBlob1,
  1730. IN CRYPT_INTEGER_BLOB const *pBlob2)
  1731. {
  1732. DWORD cbData1 = pBlob1->cbData;
  1733. DWORD cbData2 = pBlob2->cbData;
  1734. if (NULL != pBlob1->pbData)
  1735. {
  1736. while (0 != cbData1 && 0 == pBlob1->pbData[cbData1 - 1])
  1737. {
  1738. cbData1--;
  1739. }
  1740. }
  1741. if (NULL != pBlob2->pbData)
  1742. {
  1743. while (0 != cbData2 && 0 == pBlob2->pbData[cbData2 - 1])
  1744. {
  1745. cbData2--;
  1746. }
  1747. }
  1748. return(myAreBlobsSame(pBlob1->pbData, cbData1, pBlob2->pbData, cbData2));
  1749. }
  1750. // myAreCertContextBlobsSame -- return TRUE if the certs are identical.
  1751. BOOL
  1752. myAreCertContextBlobsSame(
  1753. IN CERT_CONTEXT const *pcc1,
  1754. IN CERT_CONTEXT const *pcc2)
  1755. {
  1756. return(myAreBlobsSame(
  1757. pcc1->pbCertEncoded,
  1758. pcc1->cbCertEncoded,
  1759. pcc2->pbCertEncoded,
  1760. pcc2->cbCertEncoded));
  1761. }
  1762. WCHAR const g_wszCert[] = L"cert";
  1763. HRESULT
  1764. myIsDirWriteable(
  1765. IN WCHAR const *pwszPath,
  1766. IN BOOL fFilePath)
  1767. {
  1768. HRESULT hr;
  1769. WCHAR *pwszBase;
  1770. WCHAR *pwsz;
  1771. WCHAR wszDir[MAX_PATH];
  1772. WCHAR wszTempFile[MAX_PATH];
  1773. if (fFilePath &&
  1774. iswalpha(pwszPath[0]) &&
  1775. L':' == pwszPath[1] &&
  1776. L'\0' == pwszPath[2])
  1777. {
  1778. hr = HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME);
  1779. _JumpErrorStr(hr, error, "not a file path", pwszPath);
  1780. }
  1781. if (!GetFullPathName(
  1782. pwszPath,
  1783. ARRAYSIZE(wszDir),
  1784. wszDir,
  1785. &pwsz))
  1786. {
  1787. hr = myHLastError();
  1788. _JumpErrorStr(hr, error, "GetFullPathName", pwszPath);
  1789. }
  1790. if (fFilePath)
  1791. {
  1792. if (NULL == pwsz)
  1793. {
  1794. hr = HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME);
  1795. _JumpErrorStr(hr, error, "not a file path", pwszPath);
  1796. }
  1797. pwszBase = wszDir;
  1798. if (iswalpha(wszDir[0]) &&
  1799. L':' == wszDir[1] &&
  1800. L'\\' == wszDir[2])
  1801. {
  1802. pwszBase += 3;
  1803. }
  1804. else if (L'\\' == wszDir[0] && L'\\' == wszDir[1])
  1805. {
  1806. pwszBase += 2;
  1807. }
  1808. if (pwsz > pwszBase && L'\\' == pwsz[-1])
  1809. {
  1810. pwsz--;
  1811. }
  1812. *pwsz = L'\0';
  1813. }
  1814. if (!GetTempFileName(wszDir, g_wszCert, 0, wszTempFile))
  1815. {
  1816. hr = myHLastError();
  1817. _JumpErrorStr(hr, error, "GetTempFileName", wszDir);
  1818. }
  1819. if (!DeleteFile(wszTempFile))
  1820. {
  1821. hr = myHLastError();
  1822. _JumpErrorStr(hr, error, "DeleteFile", wszTempFile);
  1823. }
  1824. hr = S_OK;
  1825. error:
  1826. return(hr);
  1827. }
  1828. HRESULT
  1829. myGetComputerObjectName(
  1830. IN EXTENDED_NAME_FORMAT NameFormat,
  1831. OUT WCHAR **ppwszComputerObjectName)
  1832. {
  1833. HRESULT hr;
  1834. WCHAR *pwszComputerObjectName = NULL;
  1835. DWORD cwc;
  1836. *ppwszComputerObjectName = NULL;
  1837. cwc = 0;
  1838. if (!GetComputerObjectName(NameFormat, NULL, &cwc))
  1839. {
  1840. hr = myHLastError();
  1841. if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) != hr)
  1842. {
  1843. _JumpError(hr, error, "GetComputerObjectName");
  1844. }
  1845. }
  1846. pwszComputerObjectName = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1847. if (NULL == pwszComputerObjectName)
  1848. {
  1849. hr = E_OUTOFMEMORY;
  1850. _JumpError(hr, error, "LocalAlloc");
  1851. }
  1852. if (!GetComputerObjectName(NameFormat, pwszComputerObjectName, &cwc))
  1853. {
  1854. hr = myHLastError();
  1855. _JumpError(hr, error, "GetComputerObjectName");
  1856. }
  1857. *ppwszComputerObjectName = pwszComputerObjectName;
  1858. pwszComputerObjectName = NULL;
  1859. hr = S_OK;
  1860. error:
  1861. if (NULL != pwszComputerObjectName)
  1862. {
  1863. LocalFree(pwszComputerObjectName);
  1864. }
  1865. return(hr);
  1866. }
  1867. HRESULT
  1868. myGetComputerNames(
  1869. OUT WCHAR **ppwszDnsName,
  1870. OUT WCHAR **ppwszOldName)
  1871. {
  1872. HRESULT hr;
  1873. DWORD cwc;
  1874. WCHAR *pwszOldName = NULL;
  1875. *ppwszOldName = NULL;
  1876. *ppwszDnsName = NULL;
  1877. cwc = MAX_COMPUTERNAME_LENGTH + 1;
  1878. pwszOldName = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1879. if (NULL == pwszOldName)
  1880. {
  1881. hr = E_OUTOFMEMORY;
  1882. _JumpError(hr, error, "LocalAlloc");
  1883. }
  1884. if (!GetComputerName(pwszOldName, &cwc))
  1885. {
  1886. hr = myHLastError();
  1887. _JumpError(hr, error, "GetComputerName");
  1888. }
  1889. hr = myGetMachineDnsName(ppwszDnsName);
  1890. _JumpIfError(hr, error, "myGetMachineDnsName");
  1891. *ppwszOldName = pwszOldName;
  1892. pwszOldName = NULL;
  1893. error:
  1894. if (NULL != pwszOldName)
  1895. {
  1896. LocalFree(pwszOldName);
  1897. }
  1898. return(hr);
  1899. }
  1900. HRESULT
  1901. myGetComputerNameEx(
  1902. IN COMPUTER_NAME_FORMAT NameFormat,
  1903. OUT WCHAR **ppwszName)
  1904. {
  1905. HRESULT hr;
  1906. WCHAR *pwszName = NULL;
  1907. DWORD cwc;
  1908. *ppwszName = NULL;
  1909. cwc = 0;
  1910. if (!GetComputerNameEx(NameFormat, NULL, &cwc))
  1911. {
  1912. hr = myHLastError();
  1913. if (HRESULT_FROM_WIN32(ERROR_MORE_DATA) != hr)
  1914. {
  1915. _JumpError(hr, error, "GetComputerNameEx");
  1916. }
  1917. }
  1918. pwszName = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1919. if (NULL == pwszName)
  1920. {
  1921. hr = E_OUTOFMEMORY;
  1922. _JumpError(hr, error, "LocalAlloc");
  1923. }
  1924. if (!GetComputerNameEx(NameFormat, pwszName, &cwc))
  1925. {
  1926. hr = myHLastError();
  1927. _JumpError(hr, error, "GetComputerNameEx");
  1928. }
  1929. *ppwszName = pwszName;
  1930. pwszName = NULL;
  1931. hr = S_OK;
  1932. error:
  1933. if (NULL != pwszName)
  1934. {
  1935. LocalFree(pwszName);
  1936. }
  1937. return(hr);
  1938. }
  1939. typedef LANGID (WINAPI FNSETTHREADUILANGUAGE)(
  1940. IN WORD wReserved);
  1941. LANGID
  1942. mySetThreadUILanguage(
  1943. IN WORD wReserved)
  1944. {
  1945. HMODULE hModule;
  1946. LANGID lang = 0;
  1947. HANDLE hStdOut;
  1948. static FNSETTHREADUILANGUAGE *s_pfn = NULL;
  1949. // Make the SetThreadUILanguage call only if a console exists:
  1950. // FILE_TYPE_CHAR (what about FILE_TYPE_PIPE, FILE_TYPE_DISK)
  1951. hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1952. if (INVALID_HANDLE_VALUE != hStdOut &&
  1953. FILE_TYPE_CHAR == (~FILE_TYPE_REMOTE & GetFileType(hStdOut)))
  1954. {
  1955. if (NULL == s_pfn)
  1956. {
  1957. hModule = GetModuleHandle(TEXT("kernel32.dll"));
  1958. if (NULL == hModule)
  1959. {
  1960. goto error;
  1961. }
  1962. // load system function
  1963. s_pfn = (FNSETTHREADUILANGUAGE *) GetProcAddress(
  1964. hModule,
  1965. "SetThreadUILanguage");
  1966. if (NULL == s_pfn)
  1967. {
  1968. goto error;
  1969. }
  1970. }
  1971. lang = (*s_pfn)(wReserved);
  1972. }
  1973. error:
  1974. return(lang);
  1975. }
  1976. HRESULT
  1977. myFormConfigString(
  1978. IN WCHAR const *pwszServer,
  1979. IN WCHAR const *pwszCAName,
  1980. OUT WCHAR **ppwszConfig)
  1981. {
  1982. HRESULT hr;
  1983. WCHAR *pwszConfig = NULL;
  1984. *ppwszConfig = NULL;
  1985. pwszConfig = (WCHAR *) LocalAlloc(LPTR,
  1986. (wcslen(pwszServer) + wcslen(pwszCAName) + 2) * sizeof(WCHAR));
  1987. if (NULL == pwszConfig)
  1988. {
  1989. hr = E_OUTOFMEMORY;
  1990. _JumpError(hr, error, "LocalAlloc");
  1991. }
  1992. wcscpy(pwszConfig, pwszServer);
  1993. wcscat(pwszConfig, L"\\");
  1994. wcscat(pwszConfig, pwszCAName);
  1995. *ppwszConfig = pwszConfig;
  1996. hr = S_OK;
  1997. error:
  1998. return hr;
  1999. }
  2000. HRESULT
  2001. myCLSIDToWsz(
  2002. IN CLSID const *pclsid,
  2003. OUT WCHAR **ppwsz)
  2004. {
  2005. HRESULT hr;
  2006. WCHAR *pwsz = NULL;
  2007. WCHAR *pwsz1;
  2008. WCHAR *pwsz2;
  2009. *ppwsz = NULL;
  2010. hr = StringFromCLSID(*pclsid, &pwsz);
  2011. _JumpIfError(hr, error, "StringFromCLSID");
  2012. for (pwsz1 = pwsz; L'\0' != *pwsz1; pwsz1++)
  2013. {
  2014. if (L'A' <= *pwsz1 && L'F' >= *pwsz1)
  2015. {
  2016. *pwsz1 += L'a' - L'A';
  2017. }
  2018. }
  2019. pwsz1 = pwsz;
  2020. pwsz2 = &pwsz[wcslen(pwsz) - 1];
  2021. if (wcLBRACE == *pwsz1 && wcRBRACE == *pwsz2)
  2022. {
  2023. pwsz1++;
  2024. *pwsz2 = L'\0';
  2025. }
  2026. hr = myDupString(pwsz1, ppwsz);
  2027. _JumpIfError(hr, error, "myDupString");
  2028. error:
  2029. if (NULL != pwsz)
  2030. {
  2031. CoTaskMemFree(pwsz);
  2032. }
  2033. return(hr);
  2034. }
  2035. HRESULT
  2036. myLoadRCString(
  2037. IN HINSTANCE hInstance,
  2038. IN int iRCId,
  2039. OUT WCHAR **ppwsz)
  2040. {
  2041. #define CS_RCALLOCATEBLOCK 256
  2042. HRESULT hr;
  2043. WCHAR *pwszTemp = NULL;
  2044. int sizeTemp;
  2045. int size;
  2046. int cBlocks = 1;
  2047. *ppwsz = NULL;
  2048. size = 0;
  2049. while (NULL == pwszTemp)
  2050. {
  2051. sizeTemp = cBlocks * CS_RCALLOCATEBLOCK;
  2052. pwszTemp = (WCHAR*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT,
  2053. sizeTemp * sizeof(WCHAR));
  2054. if (NULL == pwszTemp)
  2055. {
  2056. hr = E_OUTOFMEMORY;
  2057. _JumpError(hr, error, "LocalAlloc");
  2058. }
  2059. size = LoadString(
  2060. hInstance,
  2061. iRCId,
  2062. pwszTemp,
  2063. sizeTemp);
  2064. if (0 == size)
  2065. {
  2066. hr = myHLastError();
  2067. DBGPRINT((
  2068. DBG_SS_CERTLIB,
  2069. "myLoadRCString(hInstance=%x, iRCId=%u) --> %x\n",
  2070. hInstance,
  2071. iRCId,
  2072. hr));
  2073. if (S_OK != hr &&
  2074. HRESULT_FROM_WIN32(ERROR_RESOURCE_NAME_NOT_FOUND) != hr)
  2075. {
  2076. _JumpError(hr, error, "LoadString");
  2077. }
  2078. }
  2079. if (size < sizeTemp - 1)
  2080. {
  2081. // ok, size is big enough
  2082. break;
  2083. }
  2084. ++cBlocks;
  2085. LocalFree(pwszTemp);
  2086. pwszTemp = NULL;
  2087. }
  2088. *ppwsz = (WCHAR*) LocalAlloc(LPTR, (size+1) * sizeof(WCHAR));
  2089. if (NULL == *ppwsz)
  2090. {
  2091. hr = E_OUTOFMEMORY;
  2092. _JumpError(hr, error, "LocalAlloc");
  2093. }
  2094. if (0 == size)
  2095. {
  2096. // two possible cases, 1) real empty string or
  2097. // 2) id not found in resource. either case make it empty string
  2098. **ppwsz = L'\0';
  2099. }
  2100. else
  2101. {
  2102. // copy it
  2103. wcscpy(*ppwsz, pwszTemp);
  2104. }
  2105. hr = S_OK;
  2106. error:
  2107. if (NULL != pwszTemp)
  2108. {
  2109. LocalFree(pwszTemp);
  2110. }
  2111. return hr;
  2112. }
  2113. HRESULT
  2114. _IsConfigLocal(
  2115. IN WCHAR const *pwszConfig,
  2116. IN WCHAR const *pwszDnsName,
  2117. IN WCHAR const *pwszOldName,
  2118. OPTIONAL OUT WCHAR **ppwszMachine,
  2119. OUT BOOL *pfLocal)
  2120. {
  2121. HRESULT hr;
  2122. WCHAR *pwszMachine = NULL;
  2123. WCHAR const *pwsz;
  2124. DWORD cwc;
  2125. *pfLocal = FALSE;
  2126. if (NULL != ppwszMachine)
  2127. {
  2128. *ppwszMachine = NULL;
  2129. }
  2130. while (L'\\' == *pwszConfig)
  2131. {
  2132. pwszConfig++;
  2133. }
  2134. pwsz = wcschr(pwszConfig, L'\\');
  2135. if (NULL != pwsz)
  2136. {
  2137. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pwszConfig);
  2138. }
  2139. else
  2140. {
  2141. cwc = wcslen(pwszConfig);
  2142. }
  2143. pwszMachine = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  2144. if (NULL == pwszMachine)
  2145. {
  2146. hr = E_OUTOFMEMORY;
  2147. _JumpError(hr, error, "LocalAlloc");
  2148. }
  2149. CopyMemory(pwszMachine, pwszConfig, cwc * sizeof(WCHAR));
  2150. pwszMachine[cwc] = L'\0';
  2151. if (0 == mylstrcmpiL(pwszMachine, pwszDnsName) ||
  2152. 0 == mylstrcmpiL(pwszMachine, pwszOldName))
  2153. {
  2154. *pfLocal = TRUE;
  2155. }
  2156. if (NULL != ppwszMachine)
  2157. {
  2158. *ppwszMachine = pwszMachine;
  2159. pwszMachine = NULL;
  2160. }
  2161. hr = S_OK;
  2162. error:
  2163. if (NULL != pwszMachine)
  2164. {
  2165. LocalFree(pwszMachine);
  2166. }
  2167. return(hr);
  2168. }
  2169. HRESULT
  2170. myIsConfigLocal(
  2171. IN WCHAR const *pwszConfig,
  2172. OPTIONAL OUT WCHAR **ppwszMachine,
  2173. OUT BOOL *pfLocal)
  2174. {
  2175. HRESULT hr;
  2176. WCHAR *pwszDnsName = NULL;
  2177. WCHAR *pwszOldName = NULL;
  2178. *pfLocal = FALSE;
  2179. if (NULL != ppwszMachine)
  2180. {
  2181. *ppwszMachine = NULL;
  2182. }
  2183. hr = myGetComputerNames(&pwszDnsName, &pwszOldName);
  2184. _JumpIfError(hr, error, "myGetComputerNames");
  2185. hr = _IsConfigLocal(
  2186. pwszConfig,
  2187. pwszDnsName,
  2188. pwszOldName,
  2189. ppwszMachine,
  2190. pfLocal);
  2191. _JumpIfError(hr, error, "_IsConfigLocal");
  2192. error:
  2193. if (NULL != pwszDnsName)
  2194. {
  2195. LocalFree(pwszDnsName);
  2196. }
  2197. if (NULL != pwszOldName)
  2198. {
  2199. LocalFree(pwszOldName);
  2200. }
  2201. return(hr);
  2202. }
  2203. HRESULT
  2204. myIsConfigLocal2(
  2205. IN WCHAR const *pwszConfig,
  2206. IN WCHAR const *pwszDnsName,
  2207. IN WCHAR const *pwszOldName,
  2208. OUT BOOL *pfLocal)
  2209. {
  2210. HRESULT hr;
  2211. hr = _IsConfigLocal(
  2212. pwszConfig,
  2213. pwszDnsName,
  2214. pwszOldName,
  2215. NULL,
  2216. pfLocal);
  2217. _JumpIfError(hr, error, "_IsConfigLocal");
  2218. error:
  2219. return(hr);
  2220. }
  2221. HRESULT
  2222. myGetConfig(
  2223. IN DWORD dwUIFlag,
  2224. OUT WCHAR **ppwszConfig)
  2225. {
  2226. HRESULT hr;
  2227. HRESULT hr2;
  2228. BSTR strConfig = NULL;
  2229. WCHAR *pwszConfig = NULL;
  2230. WCHAR *pwszActiveCA = NULL;
  2231. WCHAR *pwszCommonName = NULL;
  2232. WCHAR *pwszDnsName = NULL;
  2233. CSASSERT(NULL != ppwszConfig);
  2234. *ppwszConfig = NULL;
  2235. hr = ConfigGetConfig(DISPSETUP_COM, dwUIFlag, &strConfig);
  2236. if (S_OK != hr)
  2237. {
  2238. if (CC_LOCALCONFIG != dwUIFlag)
  2239. {
  2240. _JumpError(hr, error, "ConfigGetConfig");
  2241. }
  2242. hr2 = hr;
  2243. hr = myGetCertRegStrValue(
  2244. NULL,
  2245. NULL,
  2246. NULL,
  2247. wszREGACTIVE,
  2248. &pwszActiveCA);
  2249. _PrintIfError(hr, "myGetCertRegStrValue");
  2250. if (S_OK == hr)
  2251. {
  2252. hr = myGetCertRegStrValue(
  2253. pwszActiveCA,
  2254. NULL,
  2255. NULL,
  2256. wszREGCOMMONNAME,
  2257. &pwszCommonName);
  2258. _PrintIfError(hr, "myGetCertRegStrValue");
  2259. }
  2260. if (S_OK != hr)
  2261. {
  2262. hr = hr2;
  2263. _JumpError(hr, error, "ConfigGetConfig");
  2264. }
  2265. hr = myGetMachineDnsName(&pwszDnsName);
  2266. _JumpIfError(hr, error, "myGetMachineDnsName");
  2267. hr = myFormConfigString(pwszDnsName, pwszCommonName, &pwszConfig);
  2268. _JumpIfError(hr, error, "myFormConfigString");
  2269. if (!ConvertWszToBstr(&strConfig, pwszConfig, MAXDWORD))
  2270. {
  2271. hr = E_OUTOFMEMORY;
  2272. _JumpError(hr, error, "ConvertWszToBstr");
  2273. }
  2274. }
  2275. *ppwszConfig = (WCHAR *) LocalAlloc(
  2276. LMEM_FIXED,
  2277. SysStringByteLen(strConfig) + sizeof(WCHAR));
  2278. if (NULL == *ppwszConfig)
  2279. {
  2280. hr = E_OUTOFMEMORY;
  2281. _JumpError(hr, error, "LocalAlloc");
  2282. }
  2283. wcscpy(*ppwszConfig, strConfig);
  2284. CSASSERT(
  2285. wcslen(*ppwszConfig) * sizeof(WCHAR) ==
  2286. SysStringByteLen(strConfig));
  2287. hr = S_OK;
  2288. error:
  2289. if (NULL != pwszActiveCA)
  2290. {
  2291. LocalFree(pwszActiveCA);
  2292. }
  2293. if (NULL != pwszCommonName)
  2294. {
  2295. LocalFree(pwszCommonName);
  2296. }
  2297. if (NULL != pwszDnsName)
  2298. {
  2299. LocalFree(pwszDnsName);
  2300. }
  2301. if (NULL != pwszConfig)
  2302. {
  2303. LocalFree(pwszConfig);
  2304. }
  2305. if (NULL != strConfig)
  2306. {
  2307. SysFreeString(strConfig);
  2308. }
  2309. return(hr);
  2310. }
  2311. HRESULT
  2312. myBuildPathAndExt(
  2313. IN WCHAR const *pwszDir,
  2314. IN WCHAR const *pwszFile,
  2315. OPTIONAL IN WCHAR const *pwszExt,
  2316. OUT WCHAR **ppwszPath)
  2317. {
  2318. HRESULT hr;
  2319. WCHAR *pwsz;
  2320. DWORD cwc;
  2321. *ppwszPath = NULL;
  2322. cwc = wcslen(pwszDir) + 1 + wcslen(pwszFile) + 1;
  2323. if (NULL != pwszExt)
  2324. {
  2325. cwc += wcslen(pwszExt);
  2326. }
  2327. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2328. if (NULL == pwsz)
  2329. {
  2330. hr = E_OUTOFMEMORY;
  2331. _JumpError(hr, error, "LocalAlloc");
  2332. }
  2333. wcscpy(pwsz, pwszDir);
  2334. if (L'\\' != pwsz[wcslen(pwsz) - 1])
  2335. {
  2336. wcscat(pwsz, L"\\");
  2337. }
  2338. wcscat(pwsz, pwszFile);
  2339. if (NULL != pwszExt)
  2340. {
  2341. wcscat(pwsz, pwszExt);
  2342. }
  2343. *ppwszPath = pwsz;
  2344. hr = S_OK;
  2345. error:
  2346. return(hr);
  2347. }
  2348. HRESULT
  2349. myDeleteFilePattern(
  2350. IN WCHAR const *pwszDir,
  2351. OPTIONAL IN WCHAR const *pwszPattern, // defaults to L"*.*"
  2352. IN BOOL fRecurse)
  2353. {
  2354. HRESULT hr;
  2355. HRESULT hr2;
  2356. HANDLE hf;
  2357. WIN32_FIND_DATA wfd;
  2358. WCHAR *pwszFindPath = NULL;
  2359. WCHAR *pwszDeleteFile;
  2360. WCHAR *pwszDeletePath = NULL;
  2361. DBGPRINT((
  2362. DBG_SS_CERTLIBI,
  2363. "myDeleteFilePattern(%ws, %ws, %ws)\n",
  2364. pwszDir,
  2365. pwszPattern,
  2366. fRecurse? L"Recurse" : L"NO Recurse"));
  2367. if (NULL == pwszPattern)
  2368. {
  2369. pwszPattern = L"*.*";
  2370. }
  2371. hr = myBuildPathAndExt(pwszDir, pwszPattern, NULL, &pwszFindPath);
  2372. _JumpIfError(hr, error, "myBuildPathAndExt");
  2373. pwszDeletePath = (WCHAR *) LocalAlloc(
  2374. LMEM_FIXED,
  2375. (wcslen(pwszDir) + 1 + MAX_PATH) * sizeof(WCHAR));
  2376. if (NULL == pwszDeletePath)
  2377. {
  2378. hr = E_OUTOFMEMORY;
  2379. _JumpError(hr, error, "LocalAlloc");
  2380. }
  2381. wcscpy(pwszDeletePath, pwszFindPath);
  2382. pwszDeleteFile = wcsrchr(pwszDeletePath, L'\\');
  2383. CSASSERT(NULL != pwszDeleteFile);
  2384. pwszDeleteFile++;
  2385. hf = FindFirstFile(pwszFindPath, &wfd);
  2386. if (INVALID_HANDLE_VALUE == hf)
  2387. {
  2388. hr = myHLastError();
  2389. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr ||
  2390. HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
  2391. {
  2392. hr = S_OK;
  2393. goto error;
  2394. }
  2395. _JumpErrorStr(hr, error, "FindFirstFile", pwszFindPath);
  2396. }
  2397. do
  2398. {
  2399. wcscpy(pwszDeleteFile, wfd.cFileName);
  2400. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  2401. {
  2402. if (fRecurse &&
  2403. 0 != lstrcmp(L".", wfd.cFileName) &&
  2404. 0 != lstrcmp(L"..", wfd.cFileName))
  2405. {
  2406. DBGPRINT((
  2407. DBG_SS_CERTLIBI,
  2408. "myDeleteFilePattern(DIR): %ws\n",
  2409. pwszDeletePath));
  2410. hr2 = myRemoveFilesAndDirectory(pwszDeletePath, TRUE);
  2411. if (S_OK != hr2)
  2412. {
  2413. if (S_OK == hr)
  2414. {
  2415. hr = hr2; // only return first error
  2416. }
  2417. _PrintErrorStr(hr2, "myRemoveFilesAndDirectory", pwszDeletePath);
  2418. }
  2419. }
  2420. }
  2421. else
  2422. {
  2423. DBGPRINT((
  2424. DBG_SS_CERTLIBI,
  2425. "myDeleteFilePattern: %ws\n",
  2426. pwszDeletePath));
  2427. if (!DeleteFile(pwszDeletePath))
  2428. {
  2429. hr2 = myHLastError();
  2430. if (S_OK == hr)
  2431. {
  2432. hr = hr2; // only return first error
  2433. }
  2434. _PrintErrorStr(hr2, "DeleteFile", pwszDeletePath);
  2435. }
  2436. }
  2437. } while (FindNextFile(hf, &wfd));
  2438. FindClose(hf);
  2439. error:
  2440. if (NULL != pwszFindPath)
  2441. {
  2442. LocalFree(pwszFindPath);
  2443. }
  2444. if (NULL != pwszDeletePath)
  2445. {
  2446. LocalFree(pwszDeletePath);
  2447. }
  2448. return(hr);
  2449. }
  2450. HRESULT
  2451. myRemoveFilesAndDirectory(
  2452. IN WCHAR const *pwszPath,
  2453. IN BOOL fRecurse)
  2454. {
  2455. HRESULT hr;
  2456. HRESULT hr2;
  2457. hr = myDeleteFilePattern(pwszPath, NULL, fRecurse);
  2458. if (S_OK != hr)
  2459. {
  2460. _PrintErrorStr(hr, "myDeleteFilePattern", pwszPath);
  2461. }
  2462. if (!RemoveDirectory(pwszPath))
  2463. {
  2464. hr2 = myHLastError();
  2465. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) != hr2 &&
  2466. HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) != hr2)
  2467. {
  2468. if (S_OK == hr)
  2469. {
  2470. hr = hr2; // only return first error
  2471. }
  2472. _JumpErrorStr(hr2, error, "RemoveDirectory", pwszPath);
  2473. }
  2474. }
  2475. error:
  2476. return(hr);
  2477. }
  2478. BOOL
  2479. myIsFullPath(
  2480. IN WCHAR const *pwszPath,
  2481. OUT DWORD *pdwFlag)
  2482. {
  2483. BOOL fFullPath = FALSE;
  2484. *pdwFlag = 0;
  2485. if (NULL != pwszPath)
  2486. {
  2487. if (L'\\' == pwszPath[0] && L'\\' == pwszPath[1])
  2488. {
  2489. fFullPath = TRUE;
  2490. *pdwFlag = UNC_PATH;
  2491. }
  2492. else
  2493. if (iswalpha(pwszPath[0]) &&
  2494. L':' == pwszPath[1] &&
  2495. L'\\' == pwszPath[2])
  2496. {
  2497. fFullPath = TRUE;
  2498. *pdwFlag = LOCAL_PATH;
  2499. }
  2500. }
  2501. return(fFullPath);
  2502. }
  2503. // Convert local full path to UNC, as in c:\foo... --> \\server\c$\foo...
  2504. // If pwszServer is NULL or empty, preserve the local full path
  2505. HRESULT
  2506. myConvertLocalPathToUNC(
  2507. OPTIONAL IN WCHAR const *pwszServer,
  2508. IN WCHAR const *pwszFile,
  2509. OUT WCHAR **ppwszFileUNC)
  2510. {
  2511. HRESULT hr;
  2512. DWORD cwc;
  2513. WCHAR const *pwsz;
  2514. WCHAR *pwszDst;
  2515. WCHAR *pwszFileUNC = NULL;
  2516. if (!iswalpha(pwszFile[0]) || L':' != pwszFile[1] || L'\\' != pwszFile[2])
  2517. {
  2518. hr = E_INVALIDARG;
  2519. _JumpError(hr, error, "non-local path");
  2520. }
  2521. if (NULL != pwszServer && L'\0' == *pwszServer)
  2522. {
  2523. pwszServer = NULL;
  2524. }
  2525. cwc = wcslen(pwszFile) + 1;
  2526. if (NULL != pwszServer)
  2527. {
  2528. cwc += 2 + wcslen(pwszServer) + 1;
  2529. }
  2530. pwszFileUNC = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2531. if (NULL == pwszFileUNC)
  2532. {
  2533. hr = E_OUTOFMEMORY;
  2534. _JumpError(hr, error, "LocalAlloc pwszFiles");
  2535. }
  2536. pwsz = pwszFile;
  2537. pwszDst = pwszFileUNC;
  2538. if (NULL != pwszServer)
  2539. {
  2540. wcscpy(pwszDst, L"\\\\"); // --> "\\"
  2541. wcscat(pwszDst, pwszServer); // --> "\\server"
  2542. pwszDst += wcslen(pwszDst);
  2543. *pwszDst++ = L'\\'; // --> "\\server\"
  2544. *pwszDst++ = *pwsz++; // --> "\\server\c"
  2545. *pwszDst++ = L'$'; // --> "\\server\c$"
  2546. pwsz++; // skip colon
  2547. }
  2548. wcscpy(pwszDst, pwsz); // --> "\\server\c$\foo..."
  2549. *ppwszFileUNC = pwszFileUNC;
  2550. hr = S_OK;
  2551. error:
  2552. return(hr);
  2553. }
  2554. WCHAR const *
  2555. LocalStart(
  2556. IN WCHAR const *pwsz,
  2557. OUT BOOL *pfUNC)
  2558. {
  2559. WCHAR const *pwc;
  2560. *pfUNC = FALSE;
  2561. pwc = pwsz;
  2562. if (L'\\' != *pwc)
  2563. {
  2564. pwc++;
  2565. }
  2566. if (L'\\' == pwc[0] || L'\\' == pwc[1])
  2567. {
  2568. pwc = wcschr(&pwc[2], L'\\');
  2569. if (NULL != pwc &&
  2570. iswalpha(pwc[1]) &&
  2571. L'$' == pwc[2] &&
  2572. L'\\' == pwc[3])
  2573. {
  2574. pwsz = &pwc[1];
  2575. *pfUNC = TRUE;
  2576. }
  2577. }
  2578. return(pwsz);
  2579. }
  2580. ULONG
  2581. myLocalPathwcslen(
  2582. IN WCHAR const *pwsz)
  2583. {
  2584. BOOL fUNC;
  2585. return(wcslen(LocalStart(pwsz, &fUNC)));
  2586. }
  2587. VOID
  2588. myLocalPathwcscpy(
  2589. OUT WCHAR *pwszOut,
  2590. IN WCHAR const *pwszIn)
  2591. {
  2592. BOOL fUNC;
  2593. wcscpy(pwszOut, LocalStart(pwszIn, &fUNC));
  2594. if (fUNC)
  2595. {
  2596. CSASSERT(L'$' == pwszOut[1]);
  2597. pwszOut[1] = L':';
  2598. }
  2599. }
  2600. HRESULT
  2601. myConvertUNCPathToLocal(
  2602. IN WCHAR const *pwszUNCPath,
  2603. OUT WCHAR **ppwszLocalPath)
  2604. {
  2605. HRESULT hr;
  2606. DWORD cwc;
  2607. WCHAR *pwszLocalPath;
  2608. CSASSERT(NULL != pwszUNCPath);
  2609. CSASSERT(NULL != ppwszLocalPath);
  2610. *ppwszLocalPath = NULL;
  2611. if (L'\\' != pwszUNCPath[0] || L'\\' != pwszUNCPath[1])
  2612. {
  2613. hr = E_INVALIDARG;
  2614. _JumpError(hr, error, "bad parm");
  2615. }
  2616. cwc = myLocalPathwcslen(pwszUNCPath) + 1;
  2617. pwszLocalPath = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2618. if (NULL == pwszLocalPath)
  2619. {
  2620. hr = E_OUTOFMEMORY;
  2621. _JumpError(hr, error, "LocalAlloc");
  2622. }
  2623. myLocalPathwcscpy(pwszLocalPath, pwszUNCPath);
  2624. *ppwszLocalPath = pwszLocalPath;
  2625. hr = S_OK;
  2626. error:
  2627. return(hr);
  2628. }
  2629. //+-------------------------------------------------------------------------
  2630. // Description: create any number of directories in one call
  2631. //--------------------------------------------------------------------------
  2632. HRESULT
  2633. myCreateNestedDirectories(
  2634. WCHAR const *pwszDirectory)
  2635. {
  2636. HRESULT hr;
  2637. WCHAR rgszDir[MAX_PATH]; // static buffer
  2638. WCHAR *pszNext = const_cast<WCHAR*>(pwszDirectory); // point to end of current directory
  2639. // skip "X:\"
  2640. if ((pszNext[1] == L':') &&
  2641. (pszNext[2] == L'\\'))
  2642. pszNext += 3;
  2643. while (pszNext) // incr past
  2644. {
  2645. DWORD ch;
  2646. // find the next occurence of '\'
  2647. pszNext = wcschr(pszNext, L'\\');
  2648. if (pszNext == NULL)
  2649. {
  2650. // last directory: copy everything
  2651. wcscpy(rgszDir, pwszDirectory);
  2652. }
  2653. else
  2654. {
  2655. // else copy up to Next ptr
  2656. ch = SAFE_SUBTRACT_POINTERS(pszNext, pwszDirectory);
  2657. if (0 != ch)
  2658. {
  2659. CopyMemory(rgszDir, pwszDirectory, ch*sizeof(WCHAR));
  2660. // zero-term
  2661. rgszDir[ch] = L'\0';
  2662. // incr past '\\'
  2663. pszNext++;
  2664. }
  2665. else
  2666. {
  2667. //if ch = 0, means the first char is \, skip CreateDirectory
  2668. pszNext++; //must shift to next char to get out of loop
  2669. continue;
  2670. }
  2671. }
  2672. // UNDONE: PeteSk - add in directory security
  2673. if (!CreateDirectory(rgszDir, NULL))
  2674. {
  2675. hr = myHLastError();
  2676. if (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr)
  2677. {
  2678. // something must be wrong with the path
  2679. _JumpError(hr, error, "CreateDirectory");
  2680. }
  2681. }
  2682. }
  2683. hr = S_OK;
  2684. error:
  2685. return hr;
  2686. }
  2687. HRESULT
  2688. myUncanonicalizeURLParm(
  2689. IN WCHAR const *pwszParmIn,
  2690. OUT WCHAR **ppwszParmOut)
  2691. {
  2692. HRESULT hr;
  2693. DWORD cwc;
  2694. WCHAR *pwszCanon = NULL;
  2695. WCHAR *pwszUncanon = NULL;
  2696. static const WCHAR s_wszLdap[] = L"ldap:///";
  2697. *ppwszParmOut = NULL;
  2698. cwc = WSZARRAYSIZE(s_wszLdap) + wcslen(pwszParmIn);
  2699. pwszCanon = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  2700. if (NULL == pwszCanon)
  2701. {
  2702. hr = E_OUTOFMEMORY;
  2703. _JumpError(hr, error, "LocalAlloc");
  2704. }
  2705. wcscpy(pwszCanon, s_wszLdap);
  2706. wcscat(&pwszCanon[WSZARRAYSIZE(s_wszLdap)], pwszParmIn);
  2707. hr = myInternetUncanonicalizeURL(pwszCanon, &pwszUncanon);
  2708. _JumpIfError(hr, error, "myInternetUncanonicalizeURL");
  2709. hr = myDupString(&pwszUncanon[WSZARRAYSIZE(s_wszLdap)], ppwszParmOut);
  2710. _JumpIfError(hr, error, "myDupString");
  2711. error:
  2712. if (NULL != pwszCanon)
  2713. {
  2714. LocalFree(pwszCanon);
  2715. }
  2716. if (NULL != pwszUncanon)
  2717. {
  2718. LocalFree(pwszUncanon);
  2719. }
  2720. return(hr);
  2721. }
  2722. // myFormatCertsrvStringArray FormatMessage arguments:
  2723. //
  2724. // %1 -- Machine full DNS name: pwszServerName_p1_2;
  2725. //
  2726. // %2 -- Machine short name: first DNS component of pwszServerName_p1_2
  2727. //
  2728. // %3 -- Sanitized CA name: pwszSanitizedName_p3_7
  2729. //
  2730. // %4 -- Cert Filename Suffix:
  2731. // if 0 == iCert_p4 && MAXDWORD == iCertTarget_p4: L""
  2732. // else if MAXDWORD != iCertTarget_p4 L"(%u-%u)"
  2733. // else L"(%u)"
  2734. //
  2735. // %5 -- DS DN path to Domain root: pwszDomainDN_p5
  2736. //
  2737. // %6 -- DS DN path to Configuration container: pwszConfigDN_p6
  2738. //
  2739. // %7 -- Sanitized CA name, truncated and hash suffix added if too long:
  2740. // pwszSanitizedName_p3_7
  2741. //
  2742. // %8 -- CRL Filename/Key Name Suffix: L"" if 0 == iCRL_p8; else L"(%u)"
  2743. //
  2744. // %9 -- CRL Filename Suffix: L"" if !fDeltaCRL_p9; else L"+"
  2745. //
  2746. // %10 -- DS CRL attribute: L"" if !fDSAttrib_p10_11; depends on fDeltaCRL_p9
  2747. //
  2748. // %11 -- DS CA Cert attribute: L"" if !fDSAttrib_p10_11
  2749. //
  2750. // %12 -- DS user cert attribute
  2751. //
  2752. // %13 -- DS KRA cert attribute
  2753. //
  2754. // %14 -- DS cross cert pair attribute
  2755. HRESULT
  2756. myFormatCertsrvStringArray(
  2757. IN BOOL fURL,
  2758. IN LPCWSTR pwszServerName_p1_2,
  2759. IN LPCWSTR pwszSanitizedName_p3_7,
  2760. IN DWORD iCert_p4,
  2761. IN DWORD iCertTarget_p4,
  2762. IN LPCWSTR pwszDomainDN_p5,
  2763. IN LPCWSTR pwszConfigDN_p6,
  2764. IN DWORD iCRL_p8,
  2765. IN BOOL fDeltaCRL_p9,
  2766. IN BOOL fDSAttrib_p10_11,
  2767. IN DWORD cStrings,
  2768. IN LPCWSTR *apwszStringsIn,
  2769. OUT LPWSTR *apwszStringsOut)
  2770. {
  2771. HRESULT hr = S_OK;
  2772. LPCWSTR apwszInsertionArray[100]; // 100 'cause this is the max number of insertion numbers allowed by FormatMessage
  2773. LPWSTR pwszCurrent = NULL;
  2774. BSTR strShortMachineName = NULL;
  2775. DWORD i;
  2776. WCHAR *pwszSanitizedDSName = NULL;
  2777. WCHAR wszCertSuffix[2 * cwcFILENAMESUFFIXMAX];
  2778. WCHAR wszCRLSuffix[cwcFILENAMESUFFIXMAX];
  2779. WCHAR wszDeltaCRLSuffix[cwcFILENAMESUFFIXMAX];
  2780. WCHAR const *pwszT;
  2781. ZeroMemory(apwszStringsOut, cStrings * sizeof(apwszStringsOut[0]));
  2782. ZeroMemory(apwszInsertionArray, sizeof(apwszInsertionArray));
  2783. // Format the template into a real name
  2784. // Initialize the insertion string array.
  2785. //+================================================
  2786. // Machine DNS name (%1)
  2787. CSASSERT(L'1' == wszFCSAPARM_SERVERDNSNAME[1]);
  2788. apwszInsertionArray[1 - 1] = pwszServerName_p1_2;
  2789. //+================================================
  2790. // Short Machine Name (%2)
  2791. CSASSERT(L'2' == wszFCSAPARM_SERVERSHORTNAME[1]);
  2792. strShortMachineName = SysAllocString(pwszServerName_p1_2);
  2793. if (strShortMachineName == NULL)
  2794. {
  2795. hr = E_OUTOFMEMORY;
  2796. _JumpIfError(hr, error, "SysAllocString");
  2797. }
  2798. pwszCurrent = wcschr(strShortMachineName, L'.');
  2799. if (NULL != pwszCurrent)
  2800. {
  2801. *pwszCurrent = 0;
  2802. }
  2803. apwszInsertionArray[2 - 1] = strShortMachineName;
  2804. //+================================================
  2805. // sanitized name (%3)
  2806. CSASSERT(L'3' == wszFCSAPARM_SANITIZEDCANAME[1]);
  2807. apwszInsertionArray[3 - 1] = pwszSanitizedName_p3_7;
  2808. //+================================================
  2809. // Cert filename suffix (%4) | (%4-%4)
  2810. CSASSERT(L'4' == wszFCSAPARM_CERTFILENAMESUFFIX[1]);
  2811. wszCertSuffix[0] = L'\0';
  2812. if (0 != iCert_p4 || MAXDWORD != iCertTarget_p4)
  2813. {
  2814. wsprintf(
  2815. wszCertSuffix,
  2816. MAXDWORD != iCertTarget_p4? L"(%u-%u)" : L"(%u)",
  2817. iCert_p4,
  2818. iCertTarget_p4);
  2819. }
  2820. apwszInsertionArray[4 - 1] = wszCertSuffix;
  2821. //+================================================
  2822. // Domain DN (%5)
  2823. if (NULL == pwszDomainDN_p5 || L'\0' == *pwszDomainDN_p5)
  2824. {
  2825. pwszDomainDN_p5 = L"DC=UnavailableDomainDN";
  2826. }
  2827. CSASSERT(L'5' == wszFCSAPARM_DOMAINDN[1]);
  2828. apwszInsertionArray[5 - 1] = pwszDomainDN_p5;
  2829. //+================================================
  2830. // Config DN (%6)
  2831. if (NULL == pwszConfigDN_p6 || L'\0' == *pwszConfigDN_p6)
  2832. {
  2833. pwszConfigDN_p6 = L"DC=UnavailableConfigDN";
  2834. }
  2835. CSASSERT(L'6' == wszFCSAPARM_CONFIGDN[1]);
  2836. apwszInsertionArray[6 - 1] = pwszConfigDN_p6;
  2837. // Don't pass pwszSanitizedName_p3_7 to SysAllocStringLen with the extended
  2838. // length to avoid faulting past end of pwszSanitizedName_p3_7.
  2839. //+================================================
  2840. // Sanitized Short Name (%7)
  2841. CSASSERT(L'7' == wszFCSAPARM_SANITIZEDCANAMEHASH[1]);
  2842. hr = mySanitizedNameToDSName(pwszSanitizedName_p3_7, &pwszSanitizedDSName);
  2843. _JumpIfError(hr, error, "mySanitizedNameToDSName");
  2844. apwszInsertionArray[7 - 1] = pwszSanitizedDSName;
  2845. //+================================================
  2846. // CRL filename suffix (%8)
  2847. CSASSERT(L'8' == wszFCSAPARM_CRLFILENAMESUFFIX[1]);
  2848. wszCRLSuffix[0] = L'\0';
  2849. if (0 != iCRL_p8)
  2850. {
  2851. wsprintf(wszCRLSuffix, L"(%u)", iCRL_p8);
  2852. }
  2853. apwszInsertionArray[8 - 1] = wszCRLSuffix;
  2854. //+================================================
  2855. // Delta CRL filename suffix (%9)
  2856. CSASSERT(L'9' == wszFCSAPARM_CRLDELTAFILENAMESUFFIX[1]);
  2857. wszDeltaCRLSuffix[0] = L'\0';
  2858. if (fDeltaCRL_p9)
  2859. {
  2860. wcscpy(wszDeltaCRLSuffix, L"+");
  2861. }
  2862. apwszInsertionArray[9 - 1] = wszDeltaCRLSuffix;
  2863. //+================================================
  2864. // CRL attribute (%10)
  2865. CSASSERT(L'1' == wszFCSAPARM_DSCRLATTRIBUTE[1]);
  2866. CSASSERT(L'0' == wszFCSAPARM_DSCRLATTRIBUTE[2]);
  2867. pwszT = L"";
  2868. if (fDSAttrib_p10_11)
  2869. {
  2870. pwszT = fDeltaCRL_p9?
  2871. wszDSSEARCHDELTACRLATTRIBUTE :
  2872. wszDSSEARCHBASECRLATTRIBUTE;
  2873. }
  2874. apwszInsertionArray[10 - 1] = pwszT;
  2875. //+================================================
  2876. // CA cert attribute (%11)
  2877. CSASSERT(L'1' == wszFCSAPARM_DSCACERTATTRIBUTE[1]);
  2878. CSASSERT(L'1' == wszFCSAPARM_DSCACERTATTRIBUTE[2]);
  2879. pwszT = L"";
  2880. if (fDSAttrib_p10_11)
  2881. {
  2882. pwszT = wszDSSEARCHCACERTATTRIBUTE;
  2883. }
  2884. apwszInsertionArray[11 - 1] = pwszT;
  2885. //+================================================
  2886. // User cert attribute (%12)
  2887. CSASSERT(L'1' == wszFCSAPARM_DSUSERCERTATTRIBUTE[1]);
  2888. CSASSERT(L'2' == wszFCSAPARM_DSUSERCERTATTRIBUTE[2]);
  2889. pwszT = L"";
  2890. if (fDSAttrib_p10_11)
  2891. {
  2892. pwszT = wszDSSEARCHUSERCERTATTRIBUTE;
  2893. }
  2894. apwszInsertionArray[12 - 1] = pwszT;
  2895. //+================================================
  2896. // KRA cert attribute (%13)
  2897. CSASSERT(L'1' == wszFCSAPARM_DSKRACERTATTRIBUTE[1]);
  2898. CSASSERT(L'3' == wszFCSAPARM_DSKRACERTATTRIBUTE[2]);
  2899. pwszT = L"";
  2900. if (fDSAttrib_p10_11)
  2901. {
  2902. pwszT = wszDSSEARCHKRACERTATTRIBUTE;
  2903. }
  2904. apwszInsertionArray[13 - 1] = pwszT;
  2905. //+================================================
  2906. // Cross cert pair attribute (%14)
  2907. CSASSERT(L'1' == wszFCSAPARM_DSCROSSCERTPAIRATTRIBUTE[1]);
  2908. CSASSERT(L'4' == wszFCSAPARM_DSCROSSCERTPAIRATTRIBUTE[2]);
  2909. pwszT = L"";
  2910. if (fDSAttrib_p10_11)
  2911. {
  2912. pwszT = wszDSSEARCHCROSSCERTPAIRATTRIBUTE;
  2913. }
  2914. apwszInsertionArray[14 - 1] = pwszT;
  2915. //+================================================
  2916. // Now format the strings...
  2917. for (i = 0; i < cStrings; i++)
  2918. {
  2919. if (0 == FormatMessage(
  2920. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2921. FORMAT_MESSAGE_FROM_STRING |
  2922. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2923. (VOID *) apwszStringsIn[i],
  2924. 0, // dwMessageID
  2925. 0, // dwLanguageID
  2926. (LPWSTR) &apwszStringsOut[i],
  2927. wcslen(apwszStringsIn[i]),
  2928. (va_list *) apwszInsertionArray))
  2929. {
  2930. hr = myHLastError();
  2931. _JumpError(hr, error, "FormatMessage");
  2932. }
  2933. if (fURL)
  2934. {
  2935. WCHAR *pwsz;
  2936. hr = myInternetCanonicalizeUrl(apwszStringsOut[i], &pwsz);
  2937. _JumpIfError(hr, error, "myInternetCanonicalizeUrl");
  2938. LocalFree(apwszStringsOut[i]);
  2939. apwszStringsOut[i] = pwsz;
  2940. }
  2941. }
  2942. error:
  2943. if (S_OK != hr)
  2944. {
  2945. for (i = 0; i < cStrings; i++)
  2946. {
  2947. if (NULL != apwszStringsOut[i])
  2948. {
  2949. LocalFree(apwszStringsOut[i]);
  2950. apwszStringsOut[i] = NULL;
  2951. }
  2952. }
  2953. }
  2954. if (NULL != strShortMachineName)
  2955. {
  2956. SysFreeString(strShortMachineName);
  2957. }
  2958. if (NULL != pwszSanitizedDSName)
  2959. {
  2960. LocalFree(pwszSanitizedDSName);
  2961. }
  2962. return (hr);
  2963. }
  2964. HRESULT
  2965. myAllocIndexedName(
  2966. IN WCHAR const *pwszName,
  2967. IN DWORD Index,
  2968. IN DWORD IndexTarget,
  2969. OUT WCHAR **ppwszIndexedName)
  2970. {
  2971. HRESULT hr;
  2972. WCHAR wszIndex[1 + 2 * cwcDWORDSPRINTF + 3]; // L"(%u-%u)"
  2973. WCHAR *pwszIndexedName;
  2974. *ppwszIndexedName = NULL;
  2975. wszIndex[0] = L'\0';
  2976. if (0 != Index || MAXDWORD != IndexTarget)
  2977. {
  2978. wsprintf(
  2979. wszIndex,
  2980. MAXDWORD != IndexTarget? L"(%u-%u)" : L"(%u)",
  2981. Index,
  2982. IndexTarget);
  2983. }
  2984. pwszIndexedName = (WCHAR *) LocalAlloc(
  2985. LMEM_FIXED,
  2986. (wcslen(pwszName) + wcslen(wszIndex) + 1) * sizeof(WCHAR));
  2987. if (NULL == pwszIndexedName)
  2988. {
  2989. hr = E_OUTOFMEMORY;
  2990. _JumpError(hr, error, "LocalAlloc");
  2991. }
  2992. wcscpy(pwszIndexedName, pwszName);
  2993. wcscat(pwszIndexedName, wszIndex);
  2994. *ppwszIndexedName = pwszIndexedName;
  2995. hr = S_OK;
  2996. error:
  2997. return(hr);
  2998. }
  2999. int
  3000. myWtoI(
  3001. IN WCHAR const *string,
  3002. OUT BOOL *pfValid)
  3003. {
  3004. HRESULT hr;
  3005. WCHAR wszBuf[16];
  3006. WCHAR *pwszT = wszBuf;
  3007. int cTmp = ARRAYSIZE(wszBuf);
  3008. int i = 0;
  3009. WCHAR const *pwsz;
  3010. BOOL fSawDigit = FALSE;
  3011. CSASSERT(NULL != pfValid);
  3012. *pfValid = FALSE;
  3013. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, pwszT, cTmp);
  3014. if (cTmp == 0)
  3015. {
  3016. hr = myHLastError();
  3017. if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) == hr)
  3018. {
  3019. hr = S_OK;
  3020. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, NULL, 0);
  3021. pwszT = (WCHAR *) LocalAlloc(LMEM_FIXED, cTmp * sizeof(WCHAR));
  3022. if (NULL == pwszT)
  3023. {
  3024. hr = E_OUTOFMEMORY;
  3025. _JumpError(hr, error, "LocalAlloc");
  3026. }
  3027. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, pwszT, cTmp);
  3028. if (cTmp == 0)
  3029. {
  3030. hr = myHLastError();
  3031. }
  3032. }
  3033. _JumpIfError(hr, error, "FoldString");
  3034. }
  3035. pwsz = pwszT;
  3036. while (iswspace(*pwsz))
  3037. {
  3038. pwsz++;
  3039. }
  3040. if (L'0' == *pwsz && (L'x' == pwsz[1] || L'X' == pwsz[1]))
  3041. {
  3042. pwsz += 2;
  3043. while (iswxdigit(*pwsz))
  3044. {
  3045. i <<= 4;
  3046. if (iswdigit(*pwsz))
  3047. {
  3048. i |= *pwsz - L'0';
  3049. }
  3050. else if (L'A' <= *pwsz && L'F' >= *pwsz)
  3051. {
  3052. i |= *pwsz - L'A' + 10;
  3053. }
  3054. else
  3055. {
  3056. i |= *pwsz - L'a' + 10;
  3057. }
  3058. fSawDigit = TRUE;
  3059. pwsz++;
  3060. }
  3061. }
  3062. else
  3063. {
  3064. while (iswdigit(*pwsz))
  3065. {
  3066. fSawDigit = TRUE;
  3067. pwsz++;
  3068. }
  3069. i = _wtoi(pwszT);
  3070. }
  3071. while (iswspace(*pwsz))
  3072. {
  3073. pwsz++;
  3074. }
  3075. if (L'\0' == *pwsz)
  3076. {
  3077. *pfValid = fSawDigit;
  3078. }
  3079. error:
  3080. if (NULL != pwszT && pwszT != wszBuf)
  3081. {
  3082. LocalFree(pwszT);
  3083. }
  3084. return(i);
  3085. }
  3086. HRESULT
  3087. myGetEnvString(
  3088. OUT WCHAR **ppwszOut,
  3089. IN WCHAR const *pwszVariable)
  3090. {
  3091. HRESULT hr;
  3092. WCHAR awcBuf[MAX_PATH];
  3093. DWORD len;
  3094. len = GetEnvironmentVariable(pwszVariable, awcBuf, ARRAYSIZE(awcBuf));
  3095. if (0 == len)
  3096. {
  3097. hr = myHLastError();
  3098. _JumpErrorStr2(
  3099. hr,
  3100. error,
  3101. "GetEnvironmentVariable",
  3102. pwszVariable,
  3103. HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND));
  3104. }
  3105. if (ARRAYSIZE(awcBuf) <= len)
  3106. {
  3107. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  3108. _JumpError(hr, error, "GetEnvironmentVariable");
  3109. }
  3110. *ppwszOut = (WCHAR *) LocalAlloc(
  3111. LMEM_FIXED,
  3112. (wcslen(awcBuf) + 1) * sizeof(WCHAR));
  3113. if (NULL == *ppwszOut)
  3114. {
  3115. hr = E_OUTOFMEMORY;
  3116. _JumpError(hr, error, "LocalAlloc");
  3117. }
  3118. wcscpy(*ppwszOut, awcBuf);
  3119. hr = S_OK;
  3120. error:
  3121. return(hr);
  3122. }
  3123. BOOL
  3124. IsValidAttributeChar(
  3125. IN WCHAR wc)
  3126. {
  3127. BOOL fOk = TRUE;
  3128. if (myIsMinusSign(wc) || iswspace(wc))
  3129. {
  3130. fOk = FALSE;
  3131. }
  3132. return(fOk);
  3133. }
  3134. // myParseNextAttribute -- destructively parse a name, value attribute pair.
  3135. // Allow CR and/or LF delimiters -- MAC web pages seem to convert LFs into CRs.
  3136. HRESULT
  3137. myParseNextAttribute(
  3138. IN OUT WCHAR **ppwszBuf,
  3139. IN BOOL fURL, // Use = and & instead of : and \r\n
  3140. OUT WCHAR const **ppwszName,
  3141. OUT WCHAR const **ppwszValue)
  3142. {
  3143. HRESULT hr;
  3144. WCHAR *pwszBuf = *ppwszBuf;
  3145. WCHAR wcSep = fURL? L'=' : L':';
  3146. WCHAR *wszTerm = fURL? L"&" : L"\r\n";
  3147. for (;;)
  3148. {
  3149. WCHAR *pwcToken;
  3150. WCHAR *pwcDst;
  3151. WCHAR *pwc;
  3152. WCHAR const *pwszTerm;
  3153. WCHAR wszQuote[2];
  3154. // Find the beginning of the next token
  3155. while (iswspace(*pwszBuf))
  3156. {
  3157. pwszBuf++;
  3158. }
  3159. pwcToken = pwszBuf;
  3160. pwszBuf = wcschr(pwszBuf, wcSep);
  3161. if (NULL == pwszBuf)
  3162. {
  3163. hr = S_FALSE;
  3164. goto error;
  3165. }
  3166. // If there's a wszTerm char before the next wcSep char, start over.
  3167. pwc = &pwcToken[wcscspn(pwcToken, wszTerm)];
  3168. if (pwc < pwszBuf)
  3169. {
  3170. pwszBuf = pwc + 1;
  3171. continue;
  3172. }
  3173. for (pwc = pwcDst = pwcToken; pwc < pwszBuf; pwc++)
  3174. {
  3175. if (IsValidAttributeChar(*pwc))
  3176. {
  3177. *pwcDst++ = *pwc;
  3178. }
  3179. }
  3180. pwszBuf++; // skip past the wcSep before it gets stomped
  3181. *pwcDst = L'\0'; // may stomp the wcSep separator
  3182. *ppwszName = pwcToken;
  3183. // Find beginning of Value string
  3184. while (NULL == wcschr(wszTerm, *pwszBuf) && iswspace(*pwszBuf))
  3185. {
  3186. pwszBuf++;
  3187. }
  3188. wszQuote[0] = L'\0';
  3189. pwszTerm = wszTerm;
  3190. if (fURL && (L'"' == *pwszBuf || L'\'' == *pwszBuf))
  3191. {
  3192. wszQuote[0] = *pwszBuf;
  3193. wszQuote[1] = L'\0';
  3194. pwszTerm = wszQuote;
  3195. pwszBuf++;
  3196. }
  3197. pwcToken = pwszBuf;
  3198. // find end of Value string
  3199. pwc = &pwcToken[wcscspn(pwcToken, pwszTerm)];
  3200. pwszBuf = pwc;
  3201. if (L'\0' != *pwszBuf)
  3202. {
  3203. // for case when last Value *is* terminated by a wszTerm char:
  3204. *pwszBuf++ = L'\0';
  3205. }
  3206. // trim trailing whitespace from Value string
  3207. while (--pwc >= pwcToken && iswspace(*pwc))
  3208. {
  3209. *pwc = L'\0';
  3210. }
  3211. if (L'\0' != wszQuote[0] && pwc >= pwcToken && wszQuote[0] == *pwc)
  3212. {
  3213. *pwc = L'\0';
  3214. }
  3215. if (L'\0' == **ppwszName || L'\0' == *pwcToken)
  3216. {
  3217. continue;
  3218. }
  3219. *ppwszValue = pwcToken;
  3220. break;
  3221. }
  3222. hr = S_OK;
  3223. error:
  3224. *ppwszBuf = pwszBuf;
  3225. return(hr);
  3226. }
  3227. // Destructively parse an old-style 4 byte IP Address.
  3228. HRESULT
  3229. infParseIPV4Address(
  3230. IN WCHAR *pwszValue,
  3231. OUT BYTE *pb,
  3232. IN OUT DWORD *pcb)
  3233. {
  3234. HRESULT hr;
  3235. DWORD cb;
  3236. DBGPRINT((DBG_SS_CERTLIBI, "infParseIPV4Address(%ws)\n", pwszValue));
  3237. if (CB_IPV4ADDRESS > *pcb)
  3238. {
  3239. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  3240. _JumpError(hr, error, "buffer too small");
  3241. }
  3242. for (cb = 0; cb < CB_IPV4ADDRESS; cb++)
  3243. {
  3244. WCHAR *pwszNext;
  3245. BOOL fValid;
  3246. DWORD dw;
  3247. pwszNext = &pwszValue[wcscspn(pwszValue, L".")];
  3248. if (L'.' == *pwszNext)
  3249. {
  3250. *pwszNext++ = L'\0';
  3251. }
  3252. dw = myWtoI(pwszValue, &fValid);
  3253. if (!fValid || 255 < dw)
  3254. {
  3255. hr = E_INVALIDARG;
  3256. _JumpErrorStr(hr, error, "bad IP Address digit string", pwszValue);
  3257. }
  3258. pb[cb] = (BYTE) dw;
  3259. pwszValue = pwszNext;
  3260. }
  3261. if (L'\0' != *pwszValue)
  3262. {
  3263. hr = E_INVALIDARG;
  3264. _JumpError(hr, error, "extra data");
  3265. }
  3266. *pcb = cb;
  3267. DBGPRINT((
  3268. DBG_SS_CERTLIBI,
  3269. "infParseIPV4Address: %u.%u.%u.%u\n",
  3270. pb[0],
  3271. pb[1],
  3272. pb[2],
  3273. pb[3]));
  3274. hr = S_OK;
  3275. error:
  3276. return(hr);
  3277. }
  3278. // Destructively parse a new-style 16 byte IP Address.
  3279. #define MAKE16(b0, b1) (((b0) << 8) | (b1))
  3280. HRESULT
  3281. infParseIPV6AddressSub(
  3282. IN WCHAR *pwszValue,
  3283. OUT BYTE *pb,
  3284. IN OUT DWORD *pcb)
  3285. {
  3286. HRESULT hr;
  3287. DWORD cbMax = *pcb;
  3288. DWORD cb;
  3289. DBGPRINT((DBG_SS_CERTLIBI, "infParseIPV6AddressSub(%ws)\n", pwszValue));
  3290. ZeroMemory(pb, cbMax);
  3291. for (cb = 0; cb < cbMax; cb += 2)
  3292. {
  3293. WCHAR *pwszNext;
  3294. BOOL fValid;
  3295. DWORD dw;
  3296. WCHAR awc[7];
  3297. pwszNext = &pwszValue[wcscspn(pwszValue, L":")];
  3298. if (L':' == *pwszNext)
  3299. {
  3300. *pwszNext++ = L'\0';
  3301. }
  3302. if (L'\0' == *pwszValue)
  3303. {
  3304. break;
  3305. }
  3306. if (4 < wcslen(pwszValue))
  3307. {
  3308. hr = E_INVALIDARG;
  3309. _JumpErrorStr(hr, error, "too many IP Address digits", pwszValue);
  3310. }
  3311. wcscpy(awc, L"0x");
  3312. wcscat(awc, pwszValue);
  3313. CSASSERT(wcslen(awc) < ARRAYSIZE(awc));
  3314. dw = myWtoI(awc, &fValid);
  3315. if (!fValid || 64 * 1024 <= dw)
  3316. {
  3317. hr = E_INVALIDARG;
  3318. _JumpErrorStr(hr, error, "bad IP Address digit string", pwszValue);
  3319. }
  3320. pb[cb] = (BYTE) (dw >> 8);
  3321. pb[cb + 1] = (BYTE) dw;
  3322. pwszValue = pwszNext;
  3323. }
  3324. if (L'\0' != *pwszValue)
  3325. {
  3326. hr = E_INVALIDARG;
  3327. _JumpErrorStr(hr, error, "extra data", pwszValue);
  3328. }
  3329. *pcb = cb;
  3330. hr = S_OK;
  3331. error:
  3332. return(hr);
  3333. }
  3334. // Destructively parse a new-style 16 byte IP Address.
  3335. HRESULT
  3336. infParseIPV6Address(
  3337. IN WCHAR *pwszValue,
  3338. OUT BYTE *pb,
  3339. IN OUT DWORD *pcb)
  3340. {
  3341. HRESULT hr;
  3342. DWORD cbMax;
  3343. WCHAR *pwsz;
  3344. WCHAR *pwszLeft;
  3345. WCHAR *pwszRight;
  3346. BYTE abRight[CB_IPV6ADDRESS];
  3347. DWORD cbLeft;
  3348. DWORD cbRight;
  3349. DWORD i;
  3350. BOOL fV4Compat;
  3351. DBGPRINT((DBG_SS_CERTLIBI, "infParseIPV6Address(%ws)\n", pwszValue));
  3352. if (CB_IPV6ADDRESS > *pcb)
  3353. {
  3354. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  3355. _JumpError(hr, error, "buffer too small");
  3356. }
  3357. ZeroMemory(pb, CB_IPV6ADDRESS);
  3358. cbMax = CB_IPV6ADDRESS;
  3359. // If there's a period after the last colon, an IP V4 Address is attached.
  3360. // Parse it as an IP V4 Address, and reduce the expected size of the IP V6
  3361. // Address to be parsed from eight to six 16-bit address chunks.
  3362. pwsz = wcsrchr(pwszValue, L':');
  3363. if (NULL != pwsz)
  3364. {
  3365. if (NULL != wcschr(pwsz, L'.'))
  3366. {
  3367. DWORD cb = CB_IPV4ADDRESS;
  3368. hr = infParseIPV4Address(
  3369. &pwsz[1],
  3370. &pb[CB_IPV6ADDRESS - CB_IPV4ADDRESS],
  3371. &cb);
  3372. _JumpIfError(hr, error, "infParseIPV4Address");
  3373. CSASSERT(CB_IPV4ADDRESS == cb);
  3374. pwsz[1] = L'\0'; // get rid of the trailing IP V4 Address
  3375. // drop the trailing colon -- if it's not part of a double colon.
  3376. if (pwsz > pwszValue && L':' != *--pwsz)
  3377. {
  3378. pwsz[1] = L'\0';
  3379. }
  3380. cbMax -= CB_IPV4ADDRESS;
  3381. }
  3382. }
  3383. cbLeft = 0;
  3384. cbRight = 0;
  3385. pwszLeft = pwszValue;
  3386. pwszRight = wcsstr(pwszValue, L"::");
  3387. if (NULL != pwszRight)
  3388. {
  3389. *pwszRight = L'\0';
  3390. pwszRight += 2;
  3391. if (L'\0' != *pwszRight)
  3392. {
  3393. cbRight = cbMax;
  3394. hr = infParseIPV6AddressSub(pwszRight, abRight, &cbRight);
  3395. _JumpIfError(hr, error, "infParseIPV6AddressSub");
  3396. }
  3397. }
  3398. if (L'\0' != *pwszLeft)
  3399. {
  3400. cbLeft = cbMax;
  3401. hr = infParseIPV6AddressSub(pwszLeft, pb, &cbLeft);
  3402. _JumpIfError(hr, error, "infParseIPV6AddressSub");
  3403. }
  3404. if (NULL == pwszRight && cbLeft != cbMax)
  3405. {
  3406. hr = E_INVALIDARG;
  3407. _JumpError(hr, error, "too few IP Address chunks");
  3408. }
  3409. if (cbLeft + cbRight + (NULL != pwszRight? 1 : 0) > cbMax)
  3410. {
  3411. hr = E_INVALIDARG;
  3412. _JumpError(hr, error, "too many IP Address chunks");
  3413. }
  3414. if (0 != cbRight)
  3415. {
  3416. CopyMemory(&pb[cbMax - cbRight], abRight, cbRight);
  3417. }
  3418. *pcb = CB_IPV6ADDRESS;
  3419. fV4Compat = TRUE;
  3420. for (i = 0; i < CB_IPV6ADDRESS - CB_IPV4ADDRESS - 2; i++)
  3421. {
  3422. if (0 != pb[i])
  3423. {
  3424. fV4Compat = FALSE;
  3425. break;
  3426. }
  3427. }
  3428. if (fV4Compat)
  3429. {
  3430. CSASSERT(i == CB_IPV6ADDRESS - CB_IPV4ADDRESS - 2);
  3431. fV4Compat = (0 == pb[i] && 0 == pb[i + 1]) ||
  3432. (0xff == pb[i] && 0xff == pb[i + 1]);
  3433. }
  3434. if (fV4Compat)
  3435. {
  3436. CSASSERT(i == CB_IPV6ADDRESS - CB_IPV4ADDRESS - 2);
  3437. DBGPRINT((
  3438. DBG_SS_CERTLIBI,
  3439. "infParseIPV6Address: ::%hs%u.%u.%u.%u\n",
  3440. 0 == pb[i]? "" : "ffff:",
  3441. pb[12],
  3442. pb[13],
  3443. pb[14],
  3444. pb[15]));
  3445. }
  3446. else
  3447. {
  3448. DBGPRINT((
  3449. DBG_SS_CERTLIBI,
  3450. "infParseIPV6Address: %x:%x:%x:%x:%x:%x:%x:%x\n",
  3451. MAKE16(pb[0], pb[1]),
  3452. MAKE16(pb[2], pb[3]),
  3453. MAKE16(pb[4], pb[5]),
  3454. MAKE16(pb[6], pb[7]),
  3455. MAKE16(pb[8], pb[9]),
  3456. MAKE16(pb[10], pb[11]),
  3457. MAKE16(pb[12], pb[13]),
  3458. MAKE16(pb[14], pb[15])));
  3459. }
  3460. hr = S_OK;
  3461. error:
  3462. return(hr);
  3463. }
  3464. HRESULT
  3465. myParseIPAddress(
  3466. IN WCHAR const *pwszValue,
  3467. OUT BYTE *pbData,
  3468. OUT DWORD *pcbData)
  3469. {
  3470. HRESULT hr;
  3471. DWORD cb = *pcbData;
  3472. WCHAR *pwszDup = NULL;
  3473. // if pwszValue is an empty string, return zero length.
  3474. *pcbData = 0;
  3475. if (L'\0' != *pwszValue)
  3476. {
  3477. hr = myDupString(pwszValue, &pwszDup);
  3478. _JumpIfError(hr, error, "myDupString");
  3479. if (NULL == wcschr(pwszDup, L':'))
  3480. {
  3481. hr = infParseIPV4Address(pwszDup, pbData, &cb);
  3482. _JumpIfError(hr, error, "infParseIPV4Address");
  3483. }
  3484. else
  3485. {
  3486. hr = infParseIPV6Address(pwszDup, pbData, &cb);
  3487. _JumpIfError(hr, error, "infParseIPV6Address");
  3488. }
  3489. *pcbData = cb;
  3490. DBGDUMPHEX((
  3491. DBG_SS_CERTLIBI,
  3492. DH_NOADDRESS | DH_NOTABPREFIX | 8,
  3493. pbData,
  3494. *pcbData));
  3495. }
  3496. hr = S_OK;
  3497. error:
  3498. if (NULL != pwszDup)
  3499. {
  3500. LocalFree(pwszDup);
  3501. }
  3502. return(hr);
  3503. }
  3504. HRESULT
  3505. myBuildOSVersionAttribute(
  3506. OUT BYTE **ppbVersion,
  3507. OUT DWORD *pcbVersion)
  3508. {
  3509. HRESULT hr;
  3510. DWORD i;
  3511. OSVERSIONINFO osvInfo;
  3512. CERT_NAME_VALUE cnvOSVer;
  3513. #define cwcVERSIONMAX 128
  3514. WCHAR wszVersion[12 * 4 + cwcVERSIONMAX];
  3515. *ppbVersion = NULL;
  3516. ZeroMemory(&osvInfo, sizeof(osvInfo));
  3517. // get the OSVersion
  3518. osvInfo.dwOSVersionInfoSize = sizeof(osvInfo);
  3519. if (!GetVersionEx(&osvInfo))
  3520. {
  3521. hr = myHLastError();
  3522. _JumpError(hr, error, "GetVersionEx");
  3523. }
  3524. for (i = 0; ; i++)
  3525. {
  3526. swprintf(
  3527. wszVersion,
  3528. 0 == i? L"%d.%d.%d.%d.%.*ws" : L"%d.%d.%d.%d",
  3529. osvInfo.dwMajorVersion,
  3530. osvInfo.dwMinorVersion,
  3531. osvInfo.dwBuildNumber,
  3532. osvInfo.dwPlatformId,
  3533. cwcVERSIONMAX,
  3534. osvInfo.szCSDVersion);
  3535. CSASSERT(ARRAYSIZE(wszVersion) > wcslen(wszVersion));
  3536. cnvOSVer.dwValueType = CERT_RDN_IA5_STRING;
  3537. cnvOSVer.Value.pbData = (BYTE *) wszVersion;
  3538. cnvOSVer.Value.cbData = 0;
  3539. if (!myEncodeObject(
  3540. X509_ASN_ENCODING,
  3541. X509_UNICODE_ANY_STRING,
  3542. &cnvOSVer,
  3543. 0,
  3544. CERTLIB_USE_LOCALALLOC,
  3545. ppbVersion,
  3546. pcbVersion))
  3547. {
  3548. hr = myHLastError();
  3549. _PrintError(hr, "myEncodeObject");
  3550. if (0 == i)
  3551. {
  3552. continue;
  3553. }
  3554. goto error;
  3555. }
  3556. break;
  3557. }
  3558. hr = S_OK;
  3559. error:
  3560. return(hr);
  3561. }
  3562. WCHAR const *
  3563. myFixTemplateCase(
  3564. IN WCHAR const *pwszCertType)
  3565. {
  3566. DWORD i;
  3567. static WCHAR const *apwszCertType[] = {
  3568. wszCERTTYPE_CA,
  3569. wszCERTTYPE_SUBORDINATE_CA,
  3570. wszCERTTYPE_CROSS_CA,
  3571. };
  3572. for (i = 0; i < ARRAYSIZE(apwszCertType); i++)
  3573. {
  3574. if (0 == mylstrcmpiS(pwszCertType, apwszCertType[i]))
  3575. {
  3576. pwszCertType = apwszCertType[i];
  3577. break;
  3578. }
  3579. }
  3580. return(pwszCertType);
  3581. }
  3582. HRESULT
  3583. myBuildCertTypeExtension(
  3584. IN WCHAR const *pwszCertType,
  3585. OUT CERT_EXTENSION *pExt)
  3586. {
  3587. HRESULT hr;
  3588. CERT_TEMPLATE_EXT Template;
  3589. CERT_NAME_VALUE NameValue;
  3590. LPCSTR pszStructType;
  3591. char *pszObjId = NULL;
  3592. VOID *pv;
  3593. char *pszObjIdExt;
  3594. if (!ConvertWszToSz(&pszObjId, pwszCertType, -1))
  3595. {
  3596. hr = E_OUTOFMEMORY;
  3597. _JumpError(hr, error, "ConvertWszToSz");
  3598. }
  3599. hr = myVerifyObjIdA(pszObjId);
  3600. if (S_OK == hr)
  3601. {
  3602. ZeroMemory(&Template, sizeof(Template));
  3603. Template.pszObjId = pszObjId;
  3604. //Template.dwMajorVersion = 0;
  3605. //Template.fMinorVersion = FALSE; // TRUE for a minor version
  3606. //Template.dwMinorVersion = 0;
  3607. pszStructType = X509_CERTIFICATE_TEMPLATE;
  3608. pv = &Template;
  3609. pszObjIdExt = szOID_CERTIFICATE_TEMPLATE;
  3610. }
  3611. else
  3612. {
  3613. NameValue.dwValueType = CERT_RDN_UNICODE_STRING;
  3614. NameValue.Value.pbData = (BYTE *) myFixTemplateCase(pwszCertType);
  3615. NameValue.Value.cbData = 0;
  3616. pszStructType = X509_UNICODE_ANY_STRING;
  3617. pv = &NameValue;
  3618. pszObjIdExt = szOID_ENROLL_CERTTYPE_EXTENSION;
  3619. }
  3620. if (!myEncodeObject(
  3621. X509_ASN_ENCODING,
  3622. pszStructType,
  3623. pv,
  3624. 0,
  3625. CERTLIB_USE_LOCALALLOC,
  3626. &pExt->Value.pbData,
  3627. &pExt->Value.cbData))
  3628. {
  3629. hr = myHLastError();
  3630. _JumpError(hr, error, "myEncodeObject");
  3631. }
  3632. pExt->pszObjId = pszObjIdExt;
  3633. pExt->fCritical = FALSE;
  3634. hr = S_OK;
  3635. error:
  3636. if (NULL != pszObjId)
  3637. {
  3638. LocalFree(pszObjId);
  3639. }
  3640. return(hr);
  3641. }
  3642. VOID
  3643. myPackExtensionArray(
  3644. IN BOOL fFreeData,
  3645. IN OUT DWORD *pcExt,
  3646. IN OUT CERT_EXTENSION **prgExt)
  3647. {
  3648. CERT_EXTENSION *rgExt = *prgExt;
  3649. DWORD cExt = *pcExt;
  3650. DWORD i;
  3651. DWORD j;
  3652. CERT_EXTENSION *pExti;
  3653. CERT_EXTENSION *pExtj;
  3654. for (i = 0; i < cExt; i++)
  3655. {
  3656. pExti = &rgExt[i];
  3657. if (NULL != pExti->pszObjId)
  3658. {
  3659. for (j = i + 1; j < cExt; j++)
  3660. {
  3661. pExtj = &rgExt[j];
  3662. if (NULL != pExtj->pszObjId &&
  3663. 0 == strcmp(pExti->pszObjId, pExtj->pszObjId))
  3664. {
  3665. if (fFreeData)
  3666. {
  3667. LocalFree(pExtj->pszObjId);
  3668. if (NULL != pExtj->Value.pbData)
  3669. {
  3670. LocalFree(pExtj->Value.pbData);
  3671. }
  3672. }
  3673. pExtj->pszObjId = NULL;
  3674. pExtj->Value.pbData = NULL;
  3675. }
  3676. }
  3677. if (NULL == pExti->Value.pbData)
  3678. {
  3679. if (fFreeData)
  3680. {
  3681. LocalFree(pExti->pszObjId);
  3682. }
  3683. pExti->pszObjId = NULL;
  3684. }
  3685. }
  3686. }
  3687. for (i = j = 0; i < cExt; i++)
  3688. {
  3689. pExti = &rgExt[i];
  3690. pExtj = &rgExt[j];
  3691. CSASSERT((NULL != pExti->pszObjId) ^ (NULL == pExti->Value.pbData));
  3692. if (NULL != pExti->pszObjId && NULL != pExti->Value.pbData)
  3693. {
  3694. *pExtj = *pExti;
  3695. j++;
  3696. }
  3697. }
  3698. if (j < cExt)
  3699. {
  3700. ZeroMemory(&rgExt[j], (cExt - j) * sizeof(rgExt[0]));
  3701. *pcExt = j;
  3702. }
  3703. }
  3704. HRESULT
  3705. myMergeExtensions(
  3706. IN DWORD cExtOrg,
  3707. IN CERT_EXTENSION *rgExtOrg,
  3708. IN DWORD cExtInf,
  3709. IN CERT_EXTENSION *rgExtInf,
  3710. OUT DWORD *pcExtMerged,
  3711. OUT CERT_EXTENSION **prgExtMerged)
  3712. {
  3713. HRESULT hr;
  3714. CERT_EXTENSION *rgExtMerged;
  3715. *prgExtMerged = NULL;
  3716. rgExtMerged = (CERT_EXTENSION *) LocalAlloc(
  3717. LMEM_FIXED,
  3718. (cExtInf + cExtOrg) * sizeof(rgExtMerged[0]));
  3719. if (NULL == rgExtMerged)
  3720. {
  3721. hr = E_OUTOFMEMORY;
  3722. _JumpError(hr, error, "LocalAlloc");
  3723. }
  3724. if (0 != cExtInf)
  3725. {
  3726. CopyMemory(
  3727. &rgExtMerged[0],
  3728. &rgExtInf[0],
  3729. cExtInf * sizeof(rgExtMerged[0]));
  3730. }
  3731. if (0 != cExtOrg)
  3732. {
  3733. CopyMemory(
  3734. &rgExtMerged[cExtInf],
  3735. &rgExtOrg[0],
  3736. cExtOrg * sizeof(rgExtMerged[0]));
  3737. }
  3738. *pcExtMerged = cExtInf + cExtOrg;
  3739. *prgExtMerged = rgExtMerged;
  3740. myPackExtensionArray(FALSE, pcExtMerged, prgExtMerged);
  3741. hr = S_OK;
  3742. error:
  3743. return(hr);
  3744. }
  3745. BOOL
  3746. IsWhistler(VOID)
  3747. {
  3748. HRESULT hr;
  3749. OSVERSIONINFO ovi;
  3750. static BOOL s_fDone = FALSE;
  3751. static BOOL s_fWhistler = FALSE;
  3752. if (!s_fDone)
  3753. {
  3754. s_fDone = TRUE;
  3755. // get and confirm platform info
  3756. ovi.dwOSVersionInfoSize = sizeof(ovi);
  3757. if (!GetVersionEx(&ovi))
  3758. {
  3759. hr = myHLastError();
  3760. _JumpError(hr, error, "GetVersionEx");
  3761. }
  3762. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  3763. {
  3764. hr = ERROR_CANCELLED;
  3765. _JumpError(hr, error, "Not a supported OS");
  3766. }
  3767. if (5 < ovi.dwMajorVersion ||
  3768. (5 <= ovi.dwMajorVersion && 0 < ovi.dwMinorVersion))
  3769. {
  3770. s_fWhistler = TRUE;
  3771. }
  3772. }
  3773. error:
  3774. return(s_fWhistler);
  3775. }
  3776. static BOOL
  3777. GetFlags(
  3778. OUT DWORD *pdw)
  3779. {
  3780. HRESULT hr = S_FALSE;
  3781. *pdw = 0;
  3782. #if defined(_ALLOW_GET_FLAGS_)
  3783. hr = myGetCertRegDWValue(NULL, NULL, NULL, L"SFlags", pdw);
  3784. if (S_OK == hr)
  3785. {
  3786. DBGPRINT((
  3787. DBG_SS_CERTLIB,
  3788. "CertSrv\\Configuration\\SFlags override: %u\n",
  3789. *pdw));
  3790. }
  3791. #endif
  3792. return(S_OK == hr);
  3793. }
  3794. BOOL
  3795. FIsAdvancedServer(VOID)
  3796. {
  3797. HRESULT hr;
  3798. OSVERSIONINFOEX ovi;
  3799. static BOOL s_fDone = FALSE;
  3800. static BOOL s_fIsAdvSvr = FALSE;
  3801. if (!s_fDone)
  3802. {
  3803. s_fDone = TRUE;
  3804. // get and confirm platform info
  3805. ovi.dwOSVersionInfoSize = sizeof(ovi);
  3806. if (!GetVersionEx((OSVERSIONINFO *) &ovi))
  3807. {
  3808. hr = myHLastError();
  3809. _JumpError(hr, error, "GetVersionEx");
  3810. }
  3811. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  3812. {
  3813. hr = ERROR_CANCELLED;
  3814. _JumpError(hr, error, "Not a supported OS");
  3815. }
  3816. // if server or DC, if DTC or ADS bits are set, return TRUE
  3817. s_fIsAdvSvr =
  3818. (ovi.wProductType == VER_NT_SERVER ||
  3819. ovi.wProductType == VER_NT_DOMAIN_CONTROLLER) &&
  3820. (ovi.wSuiteMask & VER_SUITE_DATACENTER ||
  3821. ovi.wSuiteMask & VER_SUITE_ENTERPRISE);
  3822. {
  3823. DWORD dw;
  3824. if (GetFlags(&dw))
  3825. {
  3826. s_fIsAdvSvr = dw;
  3827. }
  3828. }
  3829. }
  3830. error:
  3831. return(s_fIsAdvSvr);
  3832. }
  3833. BOOL
  3834. FIsServer(VOID)
  3835. {
  3836. HRESULT hr;
  3837. OSVERSIONINFOEX ovi;
  3838. static BOOL s_fDone = FALSE;
  3839. static BOOL s_fIsSvr = FALSE;
  3840. if (!s_fDone)
  3841. {
  3842. s_fDone = TRUE;
  3843. // get and confirm platform info
  3844. ovi.dwOSVersionInfoSize = sizeof(ovi);
  3845. if (!GetVersionEx((OSVERSIONINFO *) &ovi))
  3846. {
  3847. hr = myHLastError();
  3848. _JumpError(hr, error, "GetVersionEx");
  3849. }
  3850. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  3851. {
  3852. hr = ERROR_CANCELLED;
  3853. _JumpError(hr, error, "Not a supported OS");
  3854. }
  3855. // if server or DC, if DTC or ADS bits are set, return TRUE
  3856. s_fIsSvr =
  3857. (ovi.wProductType == VER_NT_SERVER ||
  3858. ovi.wProductType == VER_NT_DOMAIN_CONTROLLER) &&
  3859. 0 == ((VER_SUITE_PERSONAL | VER_SUITE_BLADE) & ovi.wSuiteMask);
  3860. if (!s_fIsSvr && VER_NT_WORKSTATION == ovi.wProductType)
  3861. {
  3862. DWORD dw;
  3863. if (GetFlags(&dw))
  3864. {
  3865. s_fIsSvr = TRUE;
  3866. }
  3867. }
  3868. }
  3869. error:
  3870. return(s_fIsSvr);
  3871. }
  3872. HRESULT
  3873. myAddLogSourceToRegistry(
  3874. IN LPWSTR pwszMsgDLL,
  3875. IN LPWSTR pwszApp)
  3876. {
  3877. HRESULT hr=S_OK;
  3878. DWORD dwData=0;
  3879. WCHAR const *pwszRegPath = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
  3880. WCHAR NameBuf[MAX_PATH];
  3881. HKEY hkey = NULL;
  3882. if (wcslen(pwszRegPath) + wcslen(pwszApp) >= ARRAYSIZE(NameBuf))
  3883. {
  3884. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  3885. _JumpErrorStr(hr, error, "NameBuf", pwszApp);
  3886. }
  3887. wcscpy(NameBuf, pwszRegPath);
  3888. wcscat(NameBuf, pwszApp);
  3889. // Create a new key for our application
  3890. hr = RegOpenKey(HKEY_LOCAL_MACHINE, NameBuf, &hkey);
  3891. if (S_OK != hr)
  3892. {
  3893. hr = RegCreateKey(HKEY_LOCAL_MACHINE, NameBuf, &hkey);
  3894. _JumpIfError(hr, error, "RegCreateKey");
  3895. }
  3896. // Add the Event-ID message-file name to the subkey
  3897. hr = RegSetValueEx(
  3898. hkey,
  3899. L"EventMessageFile",
  3900. 0,
  3901. REG_EXPAND_SZ,
  3902. (const BYTE *) pwszMsgDLL,
  3903. (wcslen(pwszMsgDLL) + 1) * sizeof(WCHAR));
  3904. _JumpIfError(hr, error, "RegSetValueEx");
  3905. // Set the supported types flags and add it to the subkey
  3906. dwData = EVENTLOG_ERROR_TYPE |
  3907. EVENTLOG_WARNING_TYPE |
  3908. EVENTLOG_INFORMATION_TYPE;
  3909. hr = RegSetValueEx(
  3910. hkey,
  3911. L"TypesSupported",
  3912. 0,
  3913. REG_DWORD,
  3914. (LPBYTE) &dwData,
  3915. sizeof(DWORD));
  3916. _JumpIfError(hr, error, "RegSetValueEx");
  3917. error:
  3918. if (NULL != hkey)
  3919. {
  3920. RegCloseKey(hkey);
  3921. }
  3922. return(myHError(hr));
  3923. }
  3924. HRESULT
  3925. myDupStringA(
  3926. IN CHAR const *pszIn,
  3927. IN CHAR **ppszOut)
  3928. {
  3929. DWORD cb;
  3930. HRESULT hr;
  3931. cb = (strlen(pszIn) + 1) * sizeof(CHAR);
  3932. *ppszOut = (CHAR *) LocalAlloc(LMEM_FIXED, cb);
  3933. if (NULL == *ppszOut)
  3934. {
  3935. hr = E_OUTOFMEMORY;
  3936. _JumpError(hr, error, "LocalAlloc");
  3937. }
  3938. CopyMemory(*ppszOut, pszIn, cb);
  3939. hr = S_OK;
  3940. error:
  3941. return(hr);
  3942. }
  3943. HRESULT
  3944. myIsCurrentUserBuiltinAdmin(
  3945. OUT bool *pfIsMember)
  3946. {
  3947. HANDLE hAccessToken = NULL, hDupToken = NULL;
  3948. PSID psidAdministrators = NULL;
  3949. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  3950. HRESULT hr = S_OK;
  3951. BOOL fIsMember = FALSE;
  3952. CSASSERT(pfIsMember);
  3953. if (!AllocateAndInitializeSid(
  3954. &siaNtAuthority,
  3955. 2,
  3956. SECURITY_BUILTIN_DOMAIN_RID,
  3957. DOMAIN_ALIAS_RID_ADMINS,
  3958. 0,
  3959. 0,
  3960. 0,
  3961. 0,
  3962. 0,
  3963. 0,
  3964. &psidAdministrators))
  3965. {
  3966. hr = myHLastError();
  3967. _JumpError(hr, error, "AllocateAndInitializeSid");
  3968. }
  3969. {
  3970. HANDLE hThread = GetCurrentThread();
  3971. if (NULL == hThread)
  3972. {
  3973. hr = myHLastError();
  3974. _JumpIfError(hr, error, "GetCurrentThread");
  3975. }
  3976. // Get the access token for current thread
  3977. if (!OpenThreadToken(
  3978. hThread,
  3979. TOKEN_QUERY | TOKEN_DUPLICATE,
  3980. FALSE,
  3981. &hAccessToken))
  3982. {
  3983. hr = myHLastError();
  3984. if(hr==HRESULT_FROM_WIN32(ERROR_NO_TOKEN))
  3985. {
  3986. HANDLE hProcess = GetCurrentProcess();
  3987. if (NULL == hProcess)
  3988. {
  3989. hr = myHLastError();
  3990. _JumpError(hr, error, "GetCurrentProcess");
  3991. }
  3992. if (!OpenProcessToken(hProcess,
  3993. TOKEN_DUPLICATE,
  3994. &hAccessToken))
  3995. {
  3996. hr = myHLastError();
  3997. _JumpError(hr, error, "OpenProcessToken");
  3998. }
  3999. }
  4000. else
  4001. {
  4002. _JumpError(hr, error, "OpenThreadToken");
  4003. }
  4004. }
  4005. }
  4006. // CheckTokenMembership must operate on impersonation token, so make one
  4007. if (!DuplicateToken(hAccessToken, SecurityIdentification, &hDupToken))
  4008. {
  4009. hr = myHLastError();
  4010. _JumpError(hr, error, "DuplicateToken");
  4011. }
  4012. if (!CheckTokenMembership(
  4013. hDupToken,
  4014. psidAdministrators,
  4015. &fIsMember))
  4016. {
  4017. hr = myHLastError();
  4018. _JumpError(hr, error, "CheckTokenMembership");
  4019. }
  4020. *pfIsMember = fIsMember?true:false;
  4021. hr = S_OK;
  4022. error:
  4023. if (hAccessToken)
  4024. CloseHandle(hAccessToken);
  4025. if (hDupToken)
  4026. CloseHandle(hDupToken);
  4027. // Free the SID we allocated
  4028. if (psidAdministrators)
  4029. FreeSid(psidAdministrators);
  4030. return(hr);
  4031. }
  4032. HRESULT
  4033. mySetRegistryLocalPathString(
  4034. IN HKEY hkey,
  4035. IN WCHAR const *pwszRegValueName,
  4036. IN WCHAR const *pwszUNCPath)
  4037. {
  4038. HRESULT hr;
  4039. WCHAR *pwszLocalPath = NULL;
  4040. hr = myConvertUNCPathToLocal(pwszUNCPath, &pwszLocalPath);
  4041. _JumpIfError(hr, error, "myConvertUNCPathToLocal");
  4042. hr = RegSetValueEx(
  4043. hkey,
  4044. pwszRegValueName,
  4045. 0,
  4046. REG_SZ,
  4047. (BYTE *) pwszLocalPath,
  4048. (wcslen(pwszLocalPath) + 1) * sizeof(WCHAR));
  4049. _JumpIfError(hr, error, "RegSetValueEx");
  4050. error:
  4051. if (NULL != pwszLocalPath)
  4052. {
  4053. LocalFree(pwszLocalPath);
  4054. }
  4055. return(hr);
  4056. }
  4057. HRESULT
  4058. myLocalMachineIsDomainMember(
  4059. OUT bool *pfIsDomainMember)
  4060. {
  4061. HRESULT hr = S_OK;
  4062. NTSTATUS status;
  4063. LSA_HANDLE PolicyHandle = NULL;
  4064. PPOLICY_PRIMARY_DOMAIN_INFO pPDI = NULL;
  4065. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  4066. CSASSERT(pfIsDomainMember);
  4067. *pfIsDomainMember = FALSE;
  4068. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  4069. status = LsaOpenPolicy(
  4070. NULL,
  4071. &ObjectAttributes,
  4072. GENERIC_READ | POLICY_VIEW_LOCAL_INFORMATION,
  4073. &PolicyHandle);
  4074. if(ERROR_SUCCESS != status)
  4075. {
  4076. hr = myHError(LsaNtStatusToWinError(status));
  4077. _JumpError(hr, error, "LsaOpenPolicy");
  4078. }
  4079. status = LsaQueryInformationPolicy( PolicyHandle,
  4080. PolicyPrimaryDomainInformation,
  4081. (PVOID*)&pPDI);
  4082. if(status)
  4083. {
  4084. hr = myHError(LsaNtStatusToWinError(status));
  4085. _JumpError(hr, error, "LsaQueryInformationPolicy");
  4086. }
  4087. if( pPDI->Sid )
  4088. {
  4089. // domain member if has domain SID
  4090. *pfIsDomainMember = TRUE;
  4091. }
  4092. error:
  4093. if(pPDI)
  4094. {
  4095. LsaFreeMemory((LPVOID)pPDI);
  4096. }
  4097. if(PolicyHandle)
  4098. {
  4099. LsaClose(PolicyHandle);
  4100. }
  4101. return hr;
  4102. }
  4103. HRESULT
  4104. myComputeMAC(
  4105. IN WCHAR const *pcwsFileName,
  4106. OUT WCHAR **ppwszMAC)
  4107. {
  4108. HRESULT hr;
  4109. HANDLE hFile = INVALID_HANDLE_VALUE;
  4110. HANDLE hFileMapping = NULL;
  4111. BYTE *pbFile = NULL;
  4112. DWORD cbImage, cbImageHigh = 0;
  4113. __int64 icbImage, icbHashed;
  4114. WCHAR rgwszMAC[CBMAX_CRYPT_HASH_LEN * 3]; // 20 bytes @ 3 WCHARs/byte
  4115. DWORD cbString;
  4116. DWORD dwFileMappingSize;
  4117. HCRYPTPROV hProv = NULL;
  4118. HCRYPTHASH hHash = NULL;
  4119. BYTE rgbHashVal[CBMAX_CRYPT_HASH_LEN];
  4120. DWORD cbHashVal = sizeof(rgbHashVal);
  4121. *ppwszMAC = NULL;
  4122. // find allocation granularity we can use
  4123. SYSTEM_INFO systemInfo;
  4124. GetSystemInfo(&systemInfo);
  4125. dwFileMappingSize = systemInfo.dwAllocationGranularity;
  4126. // get the file size
  4127. hFile = CreateFile(
  4128. pcwsFileName,
  4129. GENERIC_READ,
  4130. FILE_SHARE_READ,
  4131. NULL,
  4132. OPEN_EXISTING,
  4133. FILE_ATTRIBUTE_NORMAL,
  4134. 0);
  4135. if (INVALID_HANDLE_VALUE == hFile)
  4136. {
  4137. hr = myHLastError();
  4138. if (S_OK == hr)
  4139. {
  4140. _PrintError(hr, "CreateFile");
  4141. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  4142. }
  4143. _JumpErrorStr(hr, error, "CreateFile", pcwsFileName);
  4144. }
  4145. if (0xffffffff == (cbImage = GetFileSize(hFile, &cbImageHigh)))
  4146. {
  4147. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  4148. _JumpError(hr, error, "GetFileSize");
  4149. }
  4150. icbImage = ((__int64) cbImageHigh << 32) | cbImage;
  4151. // create mapping, indicating we will map the entire file sooner or later
  4152. hFileMapping = CreateFileMapping(hFile,
  4153. NULL,
  4154. PAGE_READONLY,
  4155. 0,
  4156. 0,
  4157. NULL);
  4158. if(hFileMapping == NULL)
  4159. {
  4160. hr = myHLastError();
  4161. _JumpError(hr, error, "CreateFileMapping");
  4162. }
  4163. // get a cryptographic provider
  4164. if (!CryptAcquireContext(
  4165. &hProv,
  4166. NULL, // container
  4167. MS_DEF_PROV, // provider name
  4168. PROV_RSA_FULL, // provider type
  4169. CRYPT_VERIFYCONTEXT)) // dwflags
  4170. {
  4171. hr = myHLastError();
  4172. _JumpError(hr, error, "CryptAcquireContext");
  4173. }
  4174. // get a hash
  4175. if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
  4176. {
  4177. hr = myHLastError();
  4178. _JumpError(hr, error, "CryptCreateHash");
  4179. }
  4180. // begin looping over data
  4181. for (icbHashed = 0; icbHashed < icbImage; icbHashed += dwFileMappingSize)
  4182. {
  4183. DWORD cbBytesLeft = (DWORD) min(
  4184. (__int64) dwFileMappingSize,
  4185. icbImage - icbHashed);
  4186. // map the next blob into memory
  4187. pbFile = (BYTE *) MapViewOfFile(
  4188. hFileMapping,
  4189. FILE_MAP_READ,
  4190. (DWORD) (icbHashed>>32), //hi32
  4191. (DWORD) (icbHashed), //lo32
  4192. cbBytesLeft); // max num bytes to map
  4193. if (NULL == pbFile)
  4194. {
  4195. hr = myHLastError();
  4196. _JumpError(hr, error, "MapViewOfFile");
  4197. }
  4198. // hash file
  4199. if (!CryptHashData(hHash, pbFile, cbBytesLeft, 0))
  4200. {
  4201. hr = myHLastError();
  4202. _JumpError(hr, error, "CryptHashData");
  4203. }
  4204. // unmap this portion
  4205. if (!UnmapViewOfFile(pbFile))
  4206. {
  4207. pbFile = NULL;
  4208. hr = myHLastError();
  4209. _JumpError(hr, error, "UnmapViewOfFile");
  4210. }
  4211. pbFile = NULL;
  4212. }
  4213. // end looping over data
  4214. // retry the hash
  4215. if (!CryptGetHashParam(hHash, HP_HASHVAL, rgbHashVal, &cbHashVal, 0))
  4216. {
  4217. hr = myHLastError();
  4218. _JumpError(hr, error, "CryptGetHashParam");
  4219. }
  4220. cbString = sizeof(rgwszMAC);
  4221. hr = MultiByteIntegerToWszBuf(
  4222. TRUE, // byte multiple
  4223. cbHashVal,
  4224. rgbHashVal,
  4225. &cbString,
  4226. rgwszMAC);
  4227. _JumpIfError(hr, error, "MultiByteIntegerToWszBuf");
  4228. hr = myDupString(rgwszMAC, ppwszMAC);
  4229. _JumpIfError(hr, error, "myDupString");
  4230. error:
  4231. if (NULL != pbFile)
  4232. {
  4233. UnmapViewOfFile(pbFile);
  4234. }
  4235. if (NULL != hFileMapping)
  4236. {
  4237. CloseHandle(hFileMapping);
  4238. }
  4239. if (INVALID_HANDLE_VALUE != hFile)
  4240. {
  4241. CloseHandle(hFile);
  4242. }
  4243. if (NULL != hHash)
  4244. {
  4245. if (!CryptDestroyHash(hHash))
  4246. {
  4247. if (hr == S_OK)
  4248. {
  4249. hr = myHLastError();
  4250. }
  4251. }
  4252. }
  4253. if (NULL != hProv)
  4254. {
  4255. if (!CryptReleaseContext(hProv, 0))
  4256. {
  4257. if (hr == S_OK)
  4258. {
  4259. hr = myHLastError();
  4260. }
  4261. }
  4262. }
  4263. return(hr);
  4264. }
  4265. HRESULT
  4266. myHExceptionCodePrintLineFile(
  4267. IN EXCEPTION_POINTERS const *pep,
  4268. IN DWORD dwLineFile)
  4269. {
  4270. return(myHExceptionCodePrint(
  4271. pep,
  4272. NULL,
  4273. __LINEFILETOFILE__(dwLineFile),
  4274. __LINEFILETOLINE__(dwLineFile)));
  4275. }
  4276. BOOL
  4277. myShouldPrintError(
  4278. IN HRESULT hr,
  4279. IN HRESULT hrquiet)
  4280. {
  4281. BOOL fPrint = TRUE;
  4282. if (myIsDelayLoadHResult(hr))
  4283. {
  4284. #if DBG_CERTSRV
  4285. if (!DbgIsSSActive(DBG_SS_MODLOAD))
  4286. #endif
  4287. {
  4288. fPrint = FALSE;
  4289. }
  4290. }
  4291. if (S_OK != hrquiet && hrquiet == hr)
  4292. {
  4293. #if DBG_CERTSRV
  4294. if (!DbgIsSSActive(DBG_SS_NOQUIET))
  4295. #endif
  4296. {
  4297. fPrint = FALSE;
  4298. }
  4299. }
  4300. return(fPrint);
  4301. }
  4302. VOID
  4303. CSPrintErrorLineFileData2(
  4304. OPTIONAL IN WCHAR const *pwszData,
  4305. IN DWORD dwLineFile,
  4306. IN HRESULT hr,
  4307. IN HRESULT hrquiet)
  4308. {
  4309. WCHAR awchr[cwcHRESULTSTRING];
  4310. if (DbgIsSSActive(DBG_SS_ERROR) &&
  4311. myShouldPrintError(hr, hrquiet))
  4312. {
  4313. DbgPrintf(
  4314. MAXDWORD,
  4315. "%u.%u.%u: %ws%ws%ws\n",
  4316. __LINEFILETOFILE__(dwLineFile),
  4317. __LINEFILETOLINE__(dwLineFile),
  4318. 0,
  4319. myHResultToString(awchr, hr),
  4320. NULL != pwszData? L": " : L"",
  4321. NULL != pwszData? pwszData : L"");
  4322. DBGPRINT((
  4323. DBG_SS_ERROR,
  4324. "%u.%u.%u: %ws%ws%ws\n",
  4325. __LINEFILETOFILE__(dwLineFile),
  4326. __LINEFILETOLINE__(dwLineFile),
  4327. 0,
  4328. myHResultToString(awchr, hr),
  4329. NULL != pwszData? L": " : L"",
  4330. NULL != pwszData? pwszData : L""));
  4331. }
  4332. }
  4333. VOID
  4334. CSPrintErrorLineFile(
  4335. IN DWORD dwLineFile,
  4336. IN HRESULT hr)
  4337. {
  4338. CSPrintErrorLineFileData2(NULL, dwLineFile, hr, S_OK);
  4339. }
  4340. VOID
  4341. CSPrintErrorLineFile2(
  4342. IN DWORD dwLineFile,
  4343. IN HRESULT hr,
  4344. IN HRESULT hrquiet)
  4345. {
  4346. CSPrintErrorLineFileData2(NULL, dwLineFile, hr, hrquiet);
  4347. }
  4348. VOID
  4349. CSPrintErrorLineFileData(
  4350. OPTIONAL IN WCHAR const *pwszData,
  4351. IN DWORD dwLineFile,
  4352. IN HRESULT hr)
  4353. {
  4354. CSPrintErrorLineFileData2(pwszData, dwLineFile, hr, S_OK);
  4355. }
  4356. // Finds a template in list based on name or OID.
  4357. // Caller is responsible for CACloseCertType(hCertType) in case of success
  4358. HRESULT
  4359. myFindCertTypeByNameOrOID(
  4360. IN const HCERTTYPE &hCertTypeList,
  4361. IN OPTIONAL LPCWSTR pcwszCertName,
  4362. IN OPTIONAL LPCWSTR pcwszCertOID,
  4363. OUT HCERTTYPE& hCertType)
  4364. {
  4365. HRESULT hr;
  4366. HCERTTYPE hCrtCertType;
  4367. HCERTTYPE hPrevCertType;
  4368. CSASSERT(pcwszCertName || pcwszCertOID);
  4369. hCertType = NULL;
  4370. hCrtCertType = hCertTypeList;
  4371. while (NULL != hCrtCertType)
  4372. {
  4373. LPWSTR *apwszCrtCertType;
  4374. BOOL fFound;
  4375. hr = CAGetCertTypeProperty(
  4376. hCrtCertType,
  4377. CERTTYPE_PROP_CN,
  4378. &apwszCrtCertType);
  4379. _JumpIfError(hr, error, "CAGetCertTypeProperty CERTTYPE_PROP_CN");
  4380. if (NULL != apwszCrtCertType)
  4381. {
  4382. fFound = NULL != apwszCrtCertType[0] &&
  4383. NULL != pcwszCertName &&
  4384. 0 == mylstrcmpiS(apwszCrtCertType[0], pcwszCertName);
  4385. CAFreeCertTypeProperty(hCrtCertType, apwszCrtCertType);
  4386. if (fFound)
  4387. {
  4388. break;
  4389. }
  4390. }
  4391. hr = CAGetCertTypeProperty(
  4392. hCrtCertType,
  4393. CERTTYPE_PROP_OID,
  4394. &apwszCrtCertType);
  4395. // ignore errors, V1 templates don't have OIDs
  4396. if (S_OK == hr && NULL != apwszCrtCertType)
  4397. {
  4398. fFound = NULL != apwszCrtCertType[0] &&
  4399. NULL != pcwszCertOID &&
  4400. 0 == mylstrcmpiS(apwszCrtCertType[0], pcwszCertOID);
  4401. CAFreeCertTypeProperty(hCrtCertType, apwszCrtCertType);
  4402. if (fFound)
  4403. {
  4404. break;
  4405. }
  4406. }
  4407. hPrevCertType = hCrtCertType;
  4408. hr = CAEnumNextCertType(hPrevCertType, &hCrtCertType);
  4409. _JumpIfError(hr, error, "CAEnumNextCertType");
  4410. // hold on to the initial list
  4411. if (hCertTypeList != hPrevCertType)
  4412. {
  4413. CACloseCertType(hPrevCertType);
  4414. }
  4415. }
  4416. if (NULL == hCrtCertType)
  4417. {
  4418. hr = HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
  4419. _JumpError2(hr, error, "NULL hCrtCertType", hr);
  4420. }
  4421. hCertType = hCrtCertType;
  4422. hCrtCertType = NULL;
  4423. hr = S_OK;
  4424. error:
  4425. if (NULL != hCrtCertType && hCertTypeList != hCrtCertType)
  4426. {
  4427. CACloseCertType(hCrtCertType);
  4428. }
  4429. return(hr);
  4430. }
  4431. ///////////////////////////////////////////////////////////////////////////////
  4432. // ConvertToString*
  4433. ///////////////////////////////////////////////////////////////////////////////
  4434. HRESULT ConvertToStringI2I4(
  4435. LONG lVal,
  4436. LPWSTR *ppwszOut)
  4437. {
  4438. WCHAR wszVal[cwcDWORDSPRINTF]; // big enough to hold a LONG as string
  4439. _itow(lVal, wszVal, 10);
  4440. *ppwszOut = (LPWSTR) LocalAlloc(
  4441. LMEM_FIXED,
  4442. sizeof(WCHAR)*(wcslen(wszVal)+1));
  4443. if(!*ppwszOut)
  4444. {
  4445. return E_OUTOFMEMORY;
  4446. }
  4447. wcscpy(*ppwszOut, wszVal);
  4448. return S_OK;
  4449. }
  4450. ///////////////////////////////////////////////////////////////////////////////
  4451. HRESULT ConvertToStringUI2UI4(
  4452. ULONG ulVal,
  4453. LPWSTR *ppwszOut)
  4454. {
  4455. WCHAR wszVal[cwcDWORDSPRINTF]; // big enough to hold a LONG as string
  4456. _itow(ulVal, wszVal, 10);
  4457. *ppwszOut = (LPWSTR) LocalAlloc(
  4458. LMEM_FIXED,
  4459. sizeof(WCHAR)*(wcslen(wszVal)+1));
  4460. if(!*ppwszOut)
  4461. {
  4462. return E_OUTOFMEMORY;
  4463. }
  4464. wcscpy(*ppwszOut, wszVal);
  4465. return S_OK;
  4466. }
  4467. ///////////////////////////////////////////////////////////////////////////////
  4468. HRESULT ConvertToStringUI8(
  4469. ULARGE_INTEGER *puliVal,
  4470. LPWSTR *ppwszOut)
  4471. {
  4472. WCHAR wszVal[cwcULONG_INTEGERSPRINTF]; // big enough to hold a LONG as string
  4473. _ui64tow(puliVal->QuadPart, wszVal, 10);
  4474. *ppwszOut = (LPWSTR) LocalAlloc(
  4475. LMEM_FIXED,
  4476. sizeof(WCHAR)*(wcslen(wszVal)+1));
  4477. if(!*ppwszOut)
  4478. {
  4479. return E_OUTOFMEMORY;
  4480. }
  4481. wcscpy(*ppwszOut, wszVal);
  4482. return S_OK;
  4483. }
  4484. ///////////////////////////////////////////////////////////////////////////////
  4485. HRESULT ConvertToStringWSZ(
  4486. LPCWSTR pcwszVal,
  4487. LPWSTR *ppwszOut,
  4488. bool fDoublePercentsInString)
  4489. {
  4490. if(fDoublePercentsInString)
  4491. {
  4492. // replace each occurence of % with %%
  4493. return DoublePercentsInString(
  4494. pcwszVal,
  4495. ppwszOut);
  4496. }
  4497. else
  4498. {
  4499. *ppwszOut = (LPWSTR) LocalAlloc(
  4500. LMEM_FIXED,
  4501. sizeof(WCHAR)*(wcslen(pcwszVal)+1));
  4502. if(!*ppwszOut)
  4503. {
  4504. return E_OUTOFMEMORY;
  4505. }
  4506. wcscpy(*ppwszOut, pcwszVal);
  4507. }
  4508. return S_OK;
  4509. }
  4510. ///////////////////////////////////////////////////////////////////////////////
  4511. HRESULT ConvertToStringArrayUI1(
  4512. LPSAFEARRAY psa,
  4513. LPWSTR *ppwszOut)
  4514. {
  4515. SafeArrayEnum<BYTE> saenum(psa);
  4516. if(!saenum.IsValid())
  4517. {
  4518. return E_INVALIDARG;
  4519. }
  4520. BYTE b;
  4521. // byte array is formated as "0x00 0x00..." ie 5
  4522. // chars per byte
  4523. *ppwszOut = (LPWSTR) LocalAlloc(
  4524. LMEM_FIXED,
  4525. sizeof(WCHAR)*(saenum.GetCount()*5 + 1));
  4526. if(!*ppwszOut)
  4527. return E_OUTOFMEMORY;
  4528. LPWSTR pwszCrt = *ppwszOut;
  4529. while(S_OK==saenum.Next(b))
  4530. {
  4531. wsprintf(pwszCrt, L"0x%02X ", b); // eg "0x0f" or "0xa4"
  4532. pwszCrt+=5;
  4533. }
  4534. return S_OK;
  4535. }
  4536. ///////////////////////////////////////////////////////////////////////////////
  4537. HRESULT ConvertToStringArrayBSTR(
  4538. LPSAFEARRAY psa,
  4539. LPWSTR *ppwszOut,
  4540. bool fDoublePercentsInString)
  4541. {
  4542. SafeArrayEnum<BSTR> saenum(psa);
  4543. if(!saenum.IsValid())
  4544. {
  4545. return E_INVALIDARG;
  4546. }
  4547. DWORD dwLen = 1;
  4548. BSTR bstr;
  4549. while(S_OK==saenum.Next(bstr))
  4550. {
  4551. dwLen+=2*wcslen(bstr)+10;
  4552. }
  4553. *ppwszOut = (LPWSTR) LocalAlloc(
  4554. LMEM_FIXED,
  4555. sizeof(WCHAR)*(dwLen));
  4556. if(!*ppwszOut)
  4557. return E_OUTOFMEMORY;
  4558. **ppwszOut = L'\0';
  4559. saenum.Reset();
  4560. WCHAR* pwszTemp = NULL;
  4561. while(S_OK==saenum.Next(bstr))
  4562. {
  4563. if(fDoublePercentsInString)
  4564. {
  4565. if (NULL != pwszTemp)
  4566. {
  4567. LocalFree(pwszTemp);
  4568. pwszTemp = NULL;
  4569. }
  4570. if (S_OK != DoublePercentsInString(bstr, &pwszTemp))
  4571. {
  4572. LocalFree(*ppwszOut);
  4573. *ppwszOut = NULL;
  4574. return E_OUTOFMEMORY;
  4575. }
  4576. wcscat(*ppwszOut, pwszTemp);
  4577. }
  4578. else
  4579. {
  4580. wcscat(*ppwszOut, bstr);
  4581. }
  4582. wcscat(*ppwszOut, L"\n");
  4583. }
  4584. LOCAL_FREE(pwszTemp);
  4585. return S_OK;
  4586. }
  4587. ///////////////////////////////////////////////////////////////////////////////
  4588. HRESULT DoublePercentsInString(
  4589. LPCWSTR pcwszIn,
  4590. LPWSTR *ppwszOut)
  4591. {
  4592. const WCHAR *pchSrc;
  4593. WCHAR *pchDest;
  4594. *ppwszOut = (LPWSTR) LocalAlloc(
  4595. LMEM_FIXED,
  4596. sizeof(WCHAR)*(2*wcslen(pcwszIn)+1));
  4597. if(!*ppwszOut)
  4598. return E_OUTOFMEMORY;
  4599. for(pchSrc = pcwszIn, pchDest = *ppwszOut;
  4600. L'\0'!=*pchSrc;
  4601. pchSrc++, pchDest++)
  4602. {
  4603. *pchDest = *pchSrc;
  4604. if(L'%'==*pchSrc)
  4605. *(++pchDest) = L'%';
  4606. }
  4607. *pchDest = L'\0';
  4608. return S_OK;
  4609. }
  4610. HRESULT
  4611. ConvertToStringDATE(
  4612. IN DATE const *pDate,
  4613. IN BOOL fGMT,
  4614. OUT LPWSTR *ppwszOut)
  4615. {
  4616. HRESULT hr;
  4617. FILETIME ft;
  4618. hr = myDateToFileTime(pDate, &ft);
  4619. if (S_OK == hr)
  4620. {
  4621. if (fGMT)
  4622. {
  4623. hr = myFileTimeToWszTime(&ft, FALSE, ppwszOut);
  4624. }
  4625. else
  4626. {
  4627. hr = myGMTFileTimeToWszLocalTime(&ft, FALSE, ppwszOut);
  4628. }
  4629. }
  4630. return(hr);
  4631. }
  4632. typedef DWORD (WINAPI fnDsEnumerateDomainTrusts) (
  4633. IN LPWSTR ServerName OPTIONAL,
  4634. IN ULONG Flags,
  4635. OUT PDS_DOMAIN_TRUSTSW *Domains,
  4636. OUT PULONG DomainCount
  4637. );
  4638. typedef NET_API_STATUS (NET_API_FUNCTION fnNetApiBufferFree) (
  4639. IN LPVOID Buffer
  4640. );
  4641. ///////////////////////////////////////////////////////////////////////////////
  4642. HRESULT myGetComputerDomainSid(PSID& rpsidDomain)
  4643. {
  4644. HRESULT hr = S_OK;
  4645. PDS_DOMAIN_TRUSTS pDomainInfo = NULL;
  4646. ULONG cDomainInfo = 0;
  4647. HMODULE hModule = NULL;
  4648. fnDsEnumerateDomainTrusts *pfnDsEnumerateDomainTrusts = NULL;
  4649. fnNetApiBufferFree *pfnNetApiBufferFree = NULL;
  4650. hModule = LoadLibrary(L"netapi32.dll");
  4651. if (NULL == hModule)
  4652. {
  4653. hr = myHLastError();
  4654. _JumpError(hr, error, "LoadLibrary(netapi32.dll)");
  4655. }
  4656. pfnDsEnumerateDomainTrusts = (fnDsEnumerateDomainTrusts *) GetProcAddress(
  4657. hModule,
  4658. "DsEnumerateDomainTrustsW");
  4659. if (NULL == pfnDsEnumerateDomainTrusts)
  4660. {
  4661. hr = myHLastError();
  4662. _JumpError(hr, error, "DsEnumerateDomainTrustsW");
  4663. }
  4664. pfnNetApiBufferFree = (fnNetApiBufferFree *) GetProcAddress(
  4665. hModule,
  4666. "NetApiBufferFree");
  4667. if(NULL == pfnNetApiBufferFree)
  4668. {
  4669. hr = myHLastError();
  4670. _JumpError(hr, error, "NetApiBufferFree");
  4671. }
  4672. hr = (*pfnDsEnumerateDomainTrusts)(
  4673. NULL,
  4674. DS_DOMAIN_PRIMARY,
  4675. &pDomainInfo,
  4676. &cDomainInfo);
  4677. _JumpIfError(hr, error, "DsEnumerateDomainTrusts");
  4678. CSASSERT(1==cDomainInfo);
  4679. rpsidDomain = (PSID)LocalAlloc(
  4680. LMEM_FIXED,
  4681. GetLengthSid(pDomainInfo->DomainSid));
  4682. _JumpIfAllocFailed(rpsidDomain, error);
  4683. CopySid(
  4684. GetLengthSid(pDomainInfo->DomainSid),
  4685. rpsidDomain,
  4686. pDomainInfo->DomainSid);
  4687. error:
  4688. if(pDomainInfo)
  4689. {
  4690. (*pfnNetApiBufferFree)(pDomainInfo);
  4691. }
  4692. if(hModule)
  4693. {
  4694. FreeLibrary(hModule);
  4695. }
  4696. return hr;
  4697. }
  4698. ///////////////////////////////////////////////////////////////////////////////
  4699. HRESULT myGetSidFromRid(
  4700. DWORD dwGroupRid,
  4701. OPTIONAL PSID *ppSid,
  4702. OPTIONAL LPWSTR* ppwszSid)
  4703. {
  4704. HRESULT hr;
  4705. LPWSTR pwszDomain = NULL;
  4706. LPWSTR pwszDomainSid = NULL;
  4707. PSID psidDomain = NULL;
  4708. LPWSTR pwszSid = NULL;
  4709. PSID psid = NULL;
  4710. static const cchRid = 10;
  4711. hr = myGetComputerDomainSid(psidDomain);
  4712. _JumpIfError(hr, error, "myGetComputerDomainSid");
  4713. if(!ConvertSidToStringSid(
  4714. psidDomain,
  4715. &pwszDomainSid))
  4716. {
  4717. hr = myHLastError();
  4718. _JumpError(hr, error, "ConvertSidToStringSid");
  4719. }
  4720. myRegisterMemAlloc(pwszDomainSid, -1, CSM_LOCALALLOC);
  4721. pwszSid = (LPWSTR) LocalAlloc(LMEM_FIXED,
  4722. sizeof(WCHAR)*(wcslen(pwszDomainSid)+cchRid+2)); // add 2 for dash and '\0'
  4723. _JumpIfAllocFailed(pwszSid, error);
  4724. wsprintf(pwszSid, L"%s-%d", pwszDomainSid, dwGroupRid);
  4725. if(ppSid)
  4726. {
  4727. if(!ConvertStringSidToSid(
  4728. pwszSid,
  4729. &psid))
  4730. {
  4731. hr = myHLastError();
  4732. _JumpErrorStr(hr, error, "ConvertSidToStringSid", pwszSid);
  4733. }
  4734. myRegisterMemAlloc(psid, -1, CSM_LOCALALLOC);
  4735. *ppSid = psid;
  4736. psid = NULL;
  4737. }
  4738. if(ppwszSid)
  4739. {
  4740. *ppwszSid = pwszSid;
  4741. pwszSid = NULL;
  4742. }
  4743. hr = S_OK;
  4744. error:
  4745. LOCAL_FREE(pwszDomain);
  4746. LOCAL_FREE(pwszDomainSid);
  4747. LOCAL_FREE(psidDomain);
  4748. LOCAL_FREE(psid);
  4749. LOCAL_FREE(pwszSid);
  4750. return hr;
  4751. }
  4752. HRESULT
  4753. myEncodeUTF8(
  4754. IN WCHAR const *pwszIn,
  4755. OUT BYTE **ppbOut,
  4756. OUT DWORD *pcbOut)
  4757. {
  4758. HRESULT hr;
  4759. CERT_NAME_VALUE name;
  4760. *ppbOut = NULL;
  4761. name.dwValueType = CERT_RDN_UTF8_STRING;
  4762. name.Value.pbData = (BYTE *) pwszIn;
  4763. name.Value.cbData = 0;
  4764. if (!myEncodeObject(
  4765. X509_ASN_ENCODING,
  4766. X509_UNICODE_ANY_STRING,
  4767. &name,
  4768. 0,
  4769. CERTLIB_USE_LOCALALLOC,
  4770. ppbOut,
  4771. pcbOut))
  4772. {
  4773. hr = myHLastError();
  4774. _JumpError(hr, error, "myEncodeObject");
  4775. }
  4776. hr = S_OK;
  4777. error:
  4778. return(hr);
  4779. }
  4780. HRESULT
  4781. myEncodeOtherNameBinary(
  4782. IN WCHAR const *pwszIn,
  4783. OUT BYTE **ppbOut,
  4784. OUT DWORD *pcbOut)
  4785. {
  4786. HRESULT hr;
  4787. CRYPT_DATA_BLOB blob;
  4788. *ppbOut = NULL;
  4789. blob.pbData = NULL;
  4790. if (0 == _wcsnicmp(wszPROPUTF8TAG, pwszIn, WSZARRAYSIZE(wszPROPUTF8TAG)))
  4791. {
  4792. hr = myEncodeUTF8(
  4793. &pwszIn[WSZARRAYSIZE(wszPROPUTF8TAG)],
  4794. ppbOut,
  4795. pcbOut);
  4796. _JumpIfError(hr, error, "Policy:myEncodeUTF8");
  4797. }
  4798. else
  4799. {
  4800. WCHAR const *pwsz;
  4801. BOOL fOctet = FALSE;
  4802. if (0 == _wcsnicmp(
  4803. wszPROPOCTETTAG,
  4804. pwszIn,
  4805. WSZARRAYSIZE(wszPROPOCTETTAG)))
  4806. {
  4807. pwsz = &pwszIn[WSZARRAYSIZE(wszPROPOCTETTAG)];
  4808. fOctet = TRUE;
  4809. }
  4810. else
  4811. if (0 == _wcsnicmp(wszPROPASNTAG, pwszIn, WSZARRAYSIZE(wszPROPASNTAG)))
  4812. {
  4813. pwsz = &pwszIn[WSZARRAYSIZE(wszPROPASNTAG)];
  4814. }
  4815. else
  4816. {
  4817. hr = HRESULT_FROM_WIN32(ERROR_DS_BAD_ATT_SCHEMA_SYNTAX);
  4818. _JumpError(hr, error, "Policy:polReencodeBinary");
  4819. }
  4820. // CryptStringToBinaryW(CRYPT_STRING_BASE64) fails on empty strings,
  4821. // because zero length means use wcslen + 1, which passes the L'\0'
  4822. // terminator to the base 64 conversion code.
  4823. if (L'\0' == *pwsz)
  4824. {
  4825. *ppbOut = (BYTE *) LocalAlloc(LMEM_FIXED, 0);
  4826. if (NULL == *ppbOut)
  4827. {
  4828. hr = E_OUTOFMEMORY;
  4829. _JumpError(hr, error, "LocalAlloc");
  4830. }
  4831. *pcbOut = 0;
  4832. }
  4833. else
  4834. {
  4835. hr = myCryptStringToBinary(
  4836. pwsz,
  4837. wcslen(pwsz),
  4838. CRYPT_STRING_BASE64,
  4839. ppbOut,
  4840. pcbOut,
  4841. NULL,
  4842. NULL);
  4843. _JumpIfError(hr, error, "myCryptStringToBinary");
  4844. }
  4845. if (fOctet)
  4846. {
  4847. blob.pbData = *ppbOut;
  4848. blob.cbData = *pcbOut;
  4849. *ppbOut = NULL;
  4850. if (!myEncodeObject(
  4851. X509_ASN_ENCODING,
  4852. X509_OCTET_STRING,
  4853. &blob,
  4854. 0,
  4855. CERTLIB_USE_LOCALALLOC,
  4856. ppbOut,
  4857. pcbOut))
  4858. {
  4859. hr = myHLastError();
  4860. _JumpError(hr, error, "Policy:myEncodeObject");
  4861. }
  4862. }
  4863. }
  4864. hr = S_OK;
  4865. error:
  4866. if (NULL != blob.pbData)
  4867. {
  4868. LocalFree(blob.pbData);
  4869. }
  4870. return(hr);
  4871. }
  4872. // Wrappers to make sure zeroing memory survives compiler optimizations.
  4873. // Use to wipe private key and password data.
  4874. VOID
  4875. myZeroDataString(
  4876. IN WCHAR *pwsz)
  4877. {
  4878. HRESULT hr;
  4879. hr = S_OK;
  4880. __try
  4881. {
  4882. SecureZeroMemory(pwsz, wcslen(pwsz) * sizeof(*pwsz));
  4883. }
  4884. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  4885. {
  4886. _PrintError(hr, "Exception");
  4887. }
  4888. }
  4889. VOID
  4890. myZeroDataStringA(
  4891. IN char *psz)
  4892. {
  4893. HRESULT hr;
  4894. hr = S_OK;
  4895. __try
  4896. {
  4897. SecureZeroMemory(psz, strlen(psz) * sizeof(*psz));
  4898. }
  4899. __except(hr = myHEXCEPTIONCODE(), EXCEPTION_EXECUTE_HANDLER)
  4900. {
  4901. _PrintError(hr, "Exception");
  4902. }
  4903. }
  4904. // Locale-independent case-ignore string compare
  4905. int
  4906. mylstrcmpiL(
  4907. IN WCHAR const *pwsz1,
  4908. IN WCHAR const *pwsz2)
  4909. {
  4910. // CSTR_LESS_THAN(1) - CSTR_EQUAL(2) == -1 string 1 less than string 2
  4911. // CSTR_EQUAL(2) - CSTR_EQUAL(2) == 0 string 1 equal to string 2
  4912. // CSTR_GREATER_THAN(3) - CSTR_EQUAL(2) == 1 string 1 greater than string 2
  4913. return(CompareString(
  4914. IsWhistler()?
  4915. LOCALE_INVARIANT :
  4916. MAKELCID(
  4917. MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  4918. SORT_DEFAULT),
  4919. NORM_IGNORECASE,
  4920. pwsz1,
  4921. -1,
  4922. pwsz2,
  4923. -1) -
  4924. CSTR_EQUAL);
  4925. }
  4926. // Locale-independent case-ignore string compare
  4927. // asserts the static string contains a strict subset of 7-bit ASCII characters.
  4928. int
  4929. mylstrcmpiS(
  4930. IN WCHAR const *pwszDynamic,
  4931. IN WCHAR const *pwszStatic)
  4932. {
  4933. #if DBG_CERTSRV
  4934. WCHAR const *pwszS;
  4935. for (pwszS = pwszStatic; L'\0' != *pwszS; pwszS++)
  4936. {
  4937. CSASSERT(L' ' <= *pwszS && L'~' >= *pwszS);
  4938. }
  4939. #endif //DBG_CERTSRV
  4940. return(mylstrcmpiL(pwszDynamic, pwszStatic));
  4941. }
  4942. HRESULT
  4943. myGetLong(
  4944. WCHAR const *pwszIn,
  4945. LONG *pLong)
  4946. {
  4947. HRESULT hr = E_INVALIDARG;
  4948. WCHAR const *pwsz;
  4949. LONG l;
  4950. pwsz = pwszIn;
  4951. if (NULL == pwsz)
  4952. {
  4953. _JumpError(hr, error, "NULL parm");
  4954. }
  4955. if (L'\0' == *pwsz)
  4956. {
  4957. _JumpError(hr, error, "empty string");
  4958. }
  4959. if (L'0' == *pwsz && (L'x' == pwsz[1] || L'X' == pwsz[1]))
  4960. {
  4961. pwsz += 2;
  4962. l = 0;
  4963. for ( ; L'\0' != *pwsz; pwsz++)
  4964. {
  4965. if (!iswxdigit(*pwsz))
  4966. {
  4967. _JumpErrorStr(hr, error, "Non-hex digit", pwszIn);
  4968. }
  4969. l <<= 4;
  4970. if (iswdigit(*pwsz))
  4971. {
  4972. l |= *pwsz - L'0';
  4973. }
  4974. else if (L'A' <= *pwsz && L'F' >= *pwsz)
  4975. {
  4976. l |= *pwsz - L'A' + 10;
  4977. }
  4978. else
  4979. {
  4980. l |= *pwsz - L'a' + 10;
  4981. }
  4982. }
  4983. *pLong = l;
  4984. }
  4985. else
  4986. {
  4987. for ( ; L'\0' != *pwsz; pwsz++)
  4988. {
  4989. if (!iswdigit(*pwsz))
  4990. {
  4991. _JumpErrorStr2(hr, error, "Non-decimal digit", pwszIn, hr);
  4992. }
  4993. }
  4994. *pLong = _wtol(pwszIn);
  4995. }
  4996. hr = S_OK;
  4997. //wprintf(L"myGetLong(%ws) --> %x (%d)\n", pwszIn, *pLong, *pLong);
  4998. error:
  4999. return(hr);
  5000. }
  5001. HRESULT
  5002. myGetSignedLong(
  5003. WCHAR const *pwszIn,
  5004. LONG *pLong)
  5005. {
  5006. HRESULT hr = E_INVALIDARG;
  5007. WCHAR const *pwsz;
  5008. LONG sign = 1;
  5009. pwsz = pwszIn;
  5010. if (NULL == pwsz)
  5011. {
  5012. _JumpError(hr, error, "NULL parm");
  5013. }
  5014. if (myIsMinusSign(*pwsz))
  5015. {
  5016. pwsz++;
  5017. sign = -1;
  5018. }
  5019. else if (L'+' == *pwsz)
  5020. {
  5021. pwsz++;
  5022. }
  5023. hr = myGetLong(pwsz, pLong);
  5024. _JumpIfError2(hr, error, "myGetLong", hr);
  5025. *pLong *= sign;
  5026. //wprintf(L"myGetSignedLong(%ws) --> %x (%d)\n", pwszIn, *pLong, *pLong);
  5027. error:
  5028. return(hr);
  5029. }