Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4130 lines
86 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 "certlibp.h"
  21. #include "csdisp.h"
  22. #include "csprop.h"
  23. #include <winnlsp.h>
  24. #include <winldap.h>
  25. #include <ntsecapi.h>
  26. #include "csldap.h"
  27. #define wszSANITIZEESCAPECHAR L"!"
  28. #define wszURLESCAPECHAR L"%"
  29. #define wcSANITIZEESCAPECHAR L'!'
  30. char
  31. PrintableChar(char ch)
  32. {
  33. if (ch < ' ' || ch > '~')
  34. {
  35. ch = '.';
  36. }
  37. return(ch);
  38. }
  39. #if DBG_CERTSRV
  40. VOID
  41. mydbgDumpHex(
  42. IN DWORD dwSubSysId,
  43. IN DWORD Flags,
  44. IN BYTE const *pb,
  45. IN ULONG cb)
  46. {
  47. if (DbgIsSSActive(dwSubSysId))
  48. {
  49. DumpHex(Flags, pb, cb);
  50. }
  51. }
  52. #endif
  53. VOID
  54. DumpHex(
  55. IN DWORD Flags,
  56. IN BYTE const *pb,
  57. IN ULONG cb)
  58. {
  59. char const *pszsep;
  60. ULONG r;
  61. ULONG i;
  62. ULONG cbremain;
  63. BOOL fZero = FALSE;
  64. DWORD cchIndent;
  65. DWORD cchAsciiSep;
  66. char szAddress[8 + 1];
  67. char szHexDump[1 + 1 + 3 * 16 + 1];
  68. char szAsciiDump[16 + 1];
  69. char *psz;
  70. cchIndent = DH_INDENTMASK & Flags;
  71. if ((DH_MULTIADDRESS & Flags) && 16 >= cb)
  72. {
  73. Flags |= DH_NOADDRESS;
  74. }
  75. for (r = 0; r < cb; r += 16)
  76. {
  77. szAddress[0] = '\0';
  78. if (0 == (DH_NOADDRESS & Flags))
  79. {
  80. sprintf(szAddress, 64 * 1024 < cb? "%06x": "%04x", r);
  81. CSASSERT(strlen(szAddress) < ARRAYSIZE(szAddress));
  82. }
  83. cbremain = cb - r;
  84. if (r != 0 && pb[r] == 0 && cbremain >= 16)
  85. {
  86. ULONG j;
  87. for (j = r + 1; j < cb; j++)
  88. {
  89. if (pb[j] != 0)
  90. {
  91. break;
  92. }
  93. }
  94. if (j == cb)
  95. {
  96. fZero = TRUE;
  97. break;
  98. }
  99. }
  100. psz = szHexDump;
  101. for (i = 0; i < min(cbremain, 16); i++)
  102. {
  103. pszsep = " ";
  104. if ((i % 8) == 0) // 0 or 8
  105. {
  106. pszsep = " ";
  107. if (i == 0) // 0
  108. {
  109. if (0 == (DH_NOTABPREFIX & Flags))
  110. {
  111. pszsep = "\t";
  112. }
  113. else if (DH_NOADDRESS & Flags)
  114. {
  115. pszsep = "";
  116. }
  117. }
  118. }
  119. CSASSERT(strlen(pszsep) <= 2);
  120. psz += sprintf(psz, "%hs%02x", pszsep, pb[r + i]);
  121. }
  122. *psz = '\0';
  123. CSASSERT(strlen(szHexDump) < ARRAYSIZE(szHexDump));
  124. cchAsciiSep = 0;
  125. szAsciiDump[0] = '\0';
  126. if (0 == (DH_NOASCIIHEX & Flags) && 0 != i)
  127. {
  128. cchAsciiSep = 3 + (16 - i)*3 + ((i <= 8)? 1 : 0);
  129. for (i = 0; i < min(cbremain, 16); i++)
  130. {
  131. szAsciiDump[i] = PrintableChar(pb[r + i]);
  132. }
  133. szAsciiDump[i] = '\0';
  134. CSASSERT(strlen(szAsciiDump) < ARRAYSIZE(szAsciiDump));
  135. }
  136. CONSOLEPRINT7((
  137. MAXDWORD,
  138. "%*hs%hs%hs%*hs%hs\n",
  139. cchIndent,
  140. "",
  141. szAddress,
  142. szHexDump,
  143. cchAsciiSep,
  144. "",
  145. szAsciiDump));
  146. }
  147. if (fZero)
  148. {
  149. CONSOLEPRINT1((MAXDWORD, "\tRemaining %lx bytes are zero\n", cbremain));
  150. }
  151. }
  152. HRESULT
  153. myDateToFileTime(
  154. IN DATE const *pDate,
  155. OUT FILETIME *pft)
  156. {
  157. SYSTEMTIME st;
  158. HRESULT hr = S_OK;
  159. if (*pDate == 0.0)
  160. {
  161. GetSystemTime(&st);
  162. }
  163. else
  164. {
  165. if (!VariantTimeToSystemTime(*pDate, &st))
  166. {
  167. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  168. _JumpError(hr, error, "VariantTimeToSystemTime");
  169. }
  170. }
  171. if (!SystemTimeToFileTime(&st, pft))
  172. {
  173. hr = myHLastError();
  174. _JumpError(hr, error, "SystemTimeToFileTime");
  175. }
  176. error:
  177. return(hr);
  178. }
  179. HRESULT
  180. myMakeExprDate(
  181. IN OUT DATE *pDate,
  182. IN LONG lDelta,
  183. IN enum ENUM_PERIOD enumPeriod)
  184. {
  185. HRESULT hr;
  186. FILETIME ft;
  187. hr = myDateToFileTime(pDate, &ft);
  188. _JumpIfError(hr, error, "myDateToFileTime");
  189. myMakeExprDateTime(&ft, lDelta, enumPeriod);
  190. hr = myFileTimeToDate(&ft, pDate);
  191. _JumpIfError(hr, error, "myFileTimeToDate");
  192. error:
  193. return(hr);
  194. }
  195. typedef struct _UNITSTABLE
  196. {
  197. WCHAR const *pwszString;
  198. enum ENUM_PERIOD enumPeriod;
  199. } UNITSTABLE;
  200. UNITSTABLE g_aut[] =
  201. {
  202. { wszPERIODSECONDS, ENUM_PERIOD_SECONDS },
  203. { wszPERIODMINUTES, ENUM_PERIOD_MINUTES },
  204. { wszPERIODHOURS, ENUM_PERIOD_HOURS },
  205. { wszPERIODDAYS, ENUM_PERIOD_DAYS },
  206. { wszPERIODWEEKS, ENUM_PERIOD_WEEKS },
  207. { wszPERIODMONTHS, ENUM_PERIOD_MONTHS },
  208. { wszPERIODYEARS, ENUM_PERIOD_YEARS },
  209. };
  210. #define CUNITSTABLEMAX (sizeof(g_aut)/sizeof(g_aut[0]))
  211. HRESULT
  212. myTranslatePeriodUnits(
  213. IN WCHAR const *pwszPeriod,
  214. IN LONG lCount,
  215. OUT enum ENUM_PERIOD *penumPeriod,
  216. OUT LONG *plCount)
  217. {
  218. HRESULT hr;
  219. UNITSTABLE const *put;
  220. for (put = g_aut; put < &g_aut[CUNITSTABLEMAX]; put++)
  221. {
  222. if (0 == lstrcmpi(pwszPeriod, put->pwszString))
  223. {
  224. *penumPeriod = put->enumPeriod;
  225. if (0 > lCount)
  226. {
  227. lCount = MAXLONG;
  228. }
  229. *plCount = lCount;
  230. hr = S_OK;
  231. goto error;
  232. }
  233. }
  234. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  235. error:
  236. CSASSERT(hr == S_OK);
  237. return(hr);
  238. }
  239. HRESULT
  240. myTranslateUnlocalizedPeriodString(
  241. IN enum ENUM_PERIOD enumPeriod,
  242. OUT WCHAR const **ppwszPeriodString)
  243. {
  244. HRESULT hr;
  245. UNITSTABLE const *put;
  246. *ppwszPeriodString = NULL;
  247. for (put = g_aut; put < &g_aut[CUNITSTABLEMAX]; put++)
  248. {
  249. if (enumPeriod == put->enumPeriod)
  250. {
  251. *ppwszPeriodString = put->pwszString;
  252. hr = S_OK;
  253. goto error;
  254. }
  255. }
  256. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  257. error:
  258. CSASSERT(hr == S_OK);
  259. return(hr);
  260. }
  261. HRESULT
  262. myFileTimePeriodToWszTimePeriod(
  263. IN FILETIME const *pftGMT,
  264. IN BOOL fExact,
  265. OUT WCHAR **ppwszTimePeriod)
  266. {
  267. HRESULT hr;
  268. DWORD cPeriodUnits;
  269. PERIODUNITS *rgPeriodUnits = NULL;
  270. WCHAR const *pwszUnitSep;
  271. DWORD i;
  272. WCHAR awc[MAX_PATH];
  273. WCHAR *pwsz;
  274. *ppwszTimePeriod = NULL;
  275. hr = caTranslateFileTimePeriodToPeriodUnits(
  276. pftGMT,
  277. fExact,
  278. &cPeriodUnits,
  279. &rgPeriodUnits);
  280. _JumpIfError(hr, error, "caTranslateFileTimePeriodToPeriodUnits");
  281. CSASSERT(0 < cPeriodUnits);
  282. pwszUnitSep = L"";
  283. pwsz = awc;
  284. for (i = 0; i < cPeriodUnits; i++)
  285. {
  286. WCHAR const *pwszPeriodString;
  287. hr = myTranslateUnlocalizedPeriodString(
  288. rgPeriodUnits[i].enumPeriod,
  289. &pwszPeriodString);
  290. _JumpIfError(hr, error, "myTranslateUnlocalizedPeriodString");
  291. pwsz += wsprintf(
  292. pwsz,
  293. L"%ws%u %ws",
  294. pwszUnitSep,
  295. rgPeriodUnits[i].lCount,
  296. pwszPeriodString);
  297. pwszUnitSep = L", ";
  298. }
  299. hr = myDupString(awc, ppwszTimePeriod);
  300. _JumpIfError(hr, error, "myDupString");
  301. error:
  302. if (NULL != rgPeriodUnits)
  303. {
  304. LocalFree(rgPeriodUnits);
  305. }
  306. return(hr);
  307. }
  308. HRESULT
  309. myFileTimeToDate(
  310. IN FILETIME const *pft,
  311. OUT DATE *pDate)
  312. {
  313. SYSTEMTIME st;
  314. HRESULT hr = S_OK;
  315. if (!FileTimeToSystemTime(pft, &st))
  316. {
  317. hr = myHLastError();
  318. _JumpError(hr, error, "FileTimeToSystemTime");
  319. }
  320. if (!SystemTimeToVariantTime(&st, pDate))
  321. {
  322. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  323. _JumpError(hr, error, "SystemTimeToVariantTime");
  324. }
  325. error:
  326. return(hr);
  327. }
  328. HRESULT
  329. mySystemTimeToGMTSystemTime(
  330. IN OUT SYSTEMTIME *pSys)
  331. {
  332. // Conversion path: SystemTimeLocal -> ftLocal -> ftGMT -> SystemTimeGMT
  333. FILETIME ftLocal, ftGMT;
  334. HRESULT hr = S_OK;
  335. if (!SystemTimeToFileTime(pSys, &ftLocal))
  336. {
  337. hr = myHLastError();
  338. _JumpError(hr, error, "SystemTimeToFileTime");
  339. }
  340. if (!LocalFileTimeToFileTime(&ftLocal, &ftGMT))
  341. {
  342. hr = myHLastError();
  343. _JumpError(hr, error, "LocalFileTimeToFileTime");
  344. }
  345. if (!FileTimeToSystemTime(&ftGMT, pSys))
  346. {
  347. hr = myHLastError();
  348. _JumpError(hr, error, "FileTimeToSystemTime");
  349. }
  350. error:
  351. return hr;
  352. }
  353. HRESULT
  354. myGMTFileTimeToWszLocalTime(
  355. IN FILETIME const *pftGMT,
  356. IN BOOL fSeconds,
  357. OUT WCHAR **ppwszLocalTime)
  358. {
  359. HRESULT hr;
  360. FILETIME ftLocal;
  361. *ppwszLocalTime = NULL;
  362. if (!FileTimeToLocalFileTime(pftGMT, &ftLocal))
  363. {
  364. hr = myHLastError();
  365. _JumpError(hr, error, "FileTimeToLocalFileTime");
  366. }
  367. hr = myFileTimeToWszTime(&ftLocal, fSeconds, ppwszLocalTime);
  368. _JumpIfError(hr, error, "myFileTimeToWszTime");
  369. error:
  370. return(hr);
  371. }
  372. HRESULT
  373. myFileTimeToWszTime(
  374. IN FILETIME const *pft,
  375. IN BOOL fSeconds,
  376. OUT WCHAR **ppwszTime)
  377. {
  378. HRESULT hr;
  379. SYSTEMTIME st;
  380. WCHAR awcDate[128];
  381. WCHAR awcTime[128];
  382. WCHAR *pwsz;
  383. *ppwszTime = NULL;
  384. if (!FileTimeToSystemTime(pft, &st))
  385. {
  386. hr = myHLastError();
  387. _JumpError(hr, error, "FileTimeToSystemTime");
  388. }
  389. if (0 == GetDateFormat(
  390. LOCALE_USER_DEFAULT,
  391. DATE_SHORTDATE,
  392. &st,
  393. NULL,
  394. awcDate,
  395. sizeof(awcDate)/sizeof(awcDate[0])))
  396. {
  397. hr = myHLastError();
  398. _JumpError(hr, error, "GetDateFormat");
  399. }
  400. if (0 == GetTimeFormat(
  401. LOCALE_USER_DEFAULT,
  402. TIME_NOSECONDS,
  403. &st,
  404. NULL,
  405. awcTime,
  406. sizeof(awcTime)/sizeof(awcTime[0])))
  407. {
  408. hr = myHLastError();
  409. _JumpError(hr, error, "GetTimeFormat");
  410. }
  411. if (fSeconds)
  412. {
  413. wsprintf(
  414. &awcTime[wcslen(awcTime)],
  415. L" %02u.%03us",
  416. st.wSecond,
  417. st.wMilliseconds);
  418. }
  419. pwsz = (WCHAR *) LocalAlloc(
  420. LMEM_FIXED,
  421. (wcslen(awcDate) + 1 + wcslen(awcTime) + 1) * sizeof(WCHAR));
  422. if (NULL == pwsz)
  423. {
  424. hr = E_OUTOFMEMORY;
  425. _JumpError(hr, error, "LocalAlloc");
  426. }
  427. wcscpy(pwsz, awcDate);
  428. wcscat(pwsz, L" ");
  429. wcscat(pwsz, awcTime);
  430. *ppwszTime = pwsz;
  431. hr = S_OK;
  432. error:
  433. return(hr);
  434. }
  435. HRESULT
  436. myGMTDateToWszLocalTime(
  437. IN DATE const *pDateGMT,
  438. IN BOOL fSeconds,
  439. OUT WCHAR **ppwszLocalTime)
  440. {
  441. HRESULT hr;
  442. FILETIME ftGMT;
  443. FILETIME ftLocal;
  444. SYSTEMTIME stLocal;
  445. WCHAR awcDate[128];
  446. WCHAR awcTime[128];
  447. WCHAR *pwsz;
  448. *ppwszLocalTime = NULL;
  449. hr = myDateToFileTime(pDateGMT, &ftGMT);
  450. _JumpIfError(hr, error, "myDateToFileTime");
  451. hr = myGMTFileTimeToWszLocalTime(&ftGMT, fSeconds, ppwszLocalTime);
  452. _JumpIfError(hr, error, "FileTimeToLocalFileTime");
  453. hr = S_OK;
  454. error:
  455. return(hr);
  456. }
  457. HRESULT
  458. myWszLocalTimeToGMTDate(
  459. IN WCHAR const *pwszLocalTime,
  460. OUT DATE *pDateGMT)
  461. {
  462. HRESULT hr;
  463. FILETIME ftGMT;
  464. hr = myWszLocalTimeToGMTFileTime(pwszLocalTime, &ftGMT);
  465. _JumpIfError(hr, error, "myWszLocalTimeToGMTFileTime");
  466. hr = myFileTimeToDate(&ftGMT, pDateGMT);
  467. _JumpIfError(hr, error, "myFileTimeToDate");
  468. error:
  469. return(hr);
  470. }
  471. HRESULT
  472. myWszLocalTimeToGMTFileTime(
  473. IN WCHAR const *pwszLocalTime,
  474. OUT FILETIME *pftGMT)
  475. {
  476. HRESULT hr;
  477. time_t time;
  478. USHORT parselen;
  479. struct tm *ptm;
  480. SYSTEMTIME stLocal;
  481. FILETIME ftLocal;
  482. char *pszLocalTime = NULL;
  483. if (!ConvertWszToSz(&pszLocalTime, pwszLocalTime, -1))
  484. {
  485. hr = E_OUTOFMEMORY;
  486. _JumpError(hr, error, "ConvertWszToSz");
  487. }
  488. hr = LUI_ParseDateTime(pszLocalTime, (time_t *) &time, &parselen, 0);
  489. if (S_OK != hr)
  490. {
  491. _PrintError(hr, "LUI_ParseDateTime");
  492. hr = E_INVALIDARG;
  493. _JumpError(hr, error, "LUI_ParseDateTime");
  494. }
  495. DBGPRINT((
  496. DBG_SS_CERTLIBI,
  497. "myWszLocalTimeToGMTDate = %ws: %x -- %ws\n",
  498. pwszLocalTime,
  499. time,
  500. _wctime(&time)));
  501. ptm = gmtime(&time);
  502. if (ptm == NULL)
  503. {
  504. hr = E_UNEXPECTED;
  505. _JumpError(hr, error, "gmTime");
  506. }
  507. stLocal.wYear = ptm->tm_year + 1900;
  508. stLocal.wMonth = ptm->tm_mon + 1;
  509. stLocal.wDayOfWeek = (WORD)ptm->tm_wday;
  510. stLocal.wDay = (WORD)ptm->tm_mday;
  511. stLocal.wHour = (WORD)ptm->tm_hour;
  512. stLocal.wMinute = (WORD)ptm->tm_min;
  513. stLocal.wSecond = (WORD)ptm->tm_sec;
  514. stLocal.wMilliseconds = 0;
  515. DBGPRINT((
  516. DBG_SS_CERTLIBI,
  517. "%u/%u/%u %u:%02u:%02u.%03u DayOfWeek=%u\n",
  518. stLocal.wMonth,
  519. stLocal.wDay,
  520. stLocal.wYear,
  521. stLocal.wHour,
  522. stLocal.wMinute,
  523. stLocal.wSecond,
  524. stLocal.wMilliseconds,
  525. stLocal.wDayOfWeek));
  526. if (!SystemTimeToFileTime(&stLocal, &ftLocal))
  527. {
  528. hr = myHLastError();
  529. _JumpError(hr, error, "SystemTimeToFileTime");
  530. }
  531. if (!LocalFileTimeToFileTime(&ftLocal, pftGMT))
  532. {
  533. hr = myHLastError();
  534. _JumpError(hr, error, "LocalFileTimeToFileTime");
  535. }
  536. error:
  537. if (NULL != pszLocalTime)
  538. {
  539. LocalFree(pszLocalTime);
  540. }
  541. return(hr);
  542. }
  543. // counts # of string in a multisz string
  544. DWORD CountMultiSz(LPCWSTR pcwszString)
  545. {
  546. DWORD dwCount = 0;
  547. DWORD dwLen;
  548. if(!pcwszString)
  549. return 0;
  550. while(*pcwszString)
  551. {
  552. dwCount++;
  553. pcwszString += wcslen(pcwszString)+1;
  554. }
  555. return dwCount;
  556. }
  557. //
  558. // myRegValueToVariant and myVariantToRegValue map registry values
  559. // to/from variant:
  560. //
  561. // REG_SZ <-> VT_BSTR
  562. // REG_BINARY <-> VT_ARRAY|VT_UI1
  563. // REG_DWORD <-> VT_I4 (VT_I2)
  564. // REG_MULTI_SZ <-> VT_ARRAY|VT_BSTR
  565. //
  566. HRESULT myRegValueToVariant(
  567. IN DWORD dwType,
  568. IN DWORD cbValue,
  569. IN BYTE const *pbValue,
  570. OUT VARIANT *pVar)
  571. {
  572. HRESULT hr = S_OK;
  573. SAFEARRAYBOUND sab;
  574. LPWSTR pwszCrtString = (LPWSTR)pbValue;
  575. BSTR bstr = NULL;
  576. SAFEARRAY* psa;
  577. VariantInit(pVar);
  578. switch(dwType)
  579. {
  580. case REG_SZ:
  581. if(sizeof(WCHAR)<=cbValue)
  582. cbValue -= sizeof(WCHAR);
  583. V_BSTR(pVar) = NULL;
  584. if (!ConvertWszToBstr(
  585. &(V_BSTR(pVar)),
  586. (WCHAR const *) pbValue,
  587. cbValue))
  588. {
  589. hr = E_OUTOFMEMORY;
  590. _JumpError(hr, error, "ConvertWszToBstr");
  591. }
  592. V_VT(pVar) = VT_BSTR;
  593. break;
  594. case REG_BINARY:
  595. sab.cElements = cbValue;
  596. sab.lLbound = 0;
  597. psa = SafeArrayCreate(
  598. VT_UI1,
  599. 1,
  600. &sab);
  601. if(!psa)
  602. {
  603. hr = E_OUTOFMEMORY;
  604. _JumpError(hr, error, "SafeArrayCreate");
  605. }
  606. for(DWORD c=0; c<sab.cElements; c++)
  607. {
  608. hr = SafeArrayPutElement(psa, (LONG*)&c, (LPVOID)&pbValue[c]);
  609. _JumpIfError(hr, error, "SafeArrayPutElement");
  610. }
  611. V_VT(pVar) = VT_ARRAY|VT_UI1;
  612. V_ARRAY(pVar) = psa;
  613. break;
  614. case REG_DWORD:
  615. V_VT(pVar) = VT_I4;
  616. V_I4(pVar) = *(LONG const *) pbValue;
  617. break;
  618. case REG_MULTI_SZ:
  619. sab.cElements = CountMultiSz((LPCWSTR)pbValue);
  620. sab.lLbound = 0;
  621. psa = SafeArrayCreate(
  622. VT_BSTR,
  623. 1,
  624. &sab);
  625. if(!psa)
  626. {
  627. hr = E_OUTOFMEMORY;
  628. _JumpError(hr, error, "SafeArrayCreate");
  629. }
  630. for(DWORD cStr=0; cStr<sab.cElements; cStr++)
  631. {
  632. if(!ConvertWszToBstr(
  633. &bstr,
  634. pwszCrtString,
  635. -1))
  636. {
  637. hr = E_OUTOFMEMORY;
  638. _JumpError(hr, error, "ConvertWszToBstr");
  639. }
  640. hr = SafeArrayPutElement(psa, (LONG*)&cStr, bstr);
  641. _JumpIfError(hr, error, "SafeArrayPutElement");
  642. pwszCrtString += wcslen(pwszCrtString)+1;
  643. SysFreeString(bstr);
  644. bstr = NULL;
  645. }
  646. V_VT(pVar) = VT_ARRAY|VT_BSTR;
  647. V_ARRAY(pVar) = psa;
  648. break;
  649. default:
  650. hr = E_INVALIDARG;
  651. _JumpError(hr, error, "invalid type");
  652. }
  653. error:
  654. if(bstr)
  655. SysFreeString(bstr);
  656. return hr;
  657. }
  658. HRESULT
  659. myVariantToRegValue(
  660. IN VARIANT const *pvarPropertyValue,
  661. OUT DWORD *pdwType,
  662. OUT DWORD *pcbprop,
  663. OUT BYTE **ppbprop)
  664. {
  665. HRESULT hr = S_OK;
  666. DWORD cbprop = 0;
  667. BYTE *pbprop = NULL;
  668. LONG lVal = 0L;
  669. LONG lLbound, lUbound;
  670. void * pData = NULL;
  671. LPSAFEARRAY psa = NULL; // no free
  672. BSTR bstr = NULL; // no free
  673. *ppbprop = NULL;
  674. *pcbprop = 0;
  675. switch (pvarPropertyValue->vt)
  676. {
  677. case VT_BYREF|VT_BSTR:
  678. case VT_BSTR:
  679. bstr = (pvarPropertyValue->vt & VT_BYREF)?
  680. *V_BSTRREF(pvarPropertyValue):
  681. V_BSTR(pvarPropertyValue);
  682. *pdwType = REG_SZ;
  683. if (NULL == bstr)
  684. {
  685. cbprop = 0;
  686. }
  687. else
  688. {
  689. pbprop = (BYTE *) bstr;
  690. cbprop = (wcslen(bstr) + 1) * sizeof(WCHAR);
  691. }
  692. break;
  693. case VT_BYREF|VT_ARRAY|VT_UI1:
  694. case VT_ARRAY|VT_UI1:
  695. psa = (pvarPropertyValue->vt & VT_BYREF)?
  696. *V_ARRAYREF(pvarPropertyValue):
  697. V_ARRAY(pvarPropertyValue);
  698. *pdwType = REG_BINARY;
  699. if(1!=SafeArrayGetDim(psa))
  700. {
  701. hr = E_INVALIDARG;
  702. _JumpError(hr, error, "only 1 dim array of bytes allowed");
  703. }
  704. hr = SafeArrayGetLBound(
  705. psa,
  706. 1,
  707. &lLbound);
  708. _JumpIfError(hr, error, "SafeArrayGetLBound");
  709. hr = SafeArrayGetUBound(
  710. psa,
  711. 1,
  712. &lUbound);
  713. _JumpIfError(hr, error, "SafeArrayGetLBound");
  714. hr = SafeArrayAccessData(psa, &pData);
  715. _JumpIfError(hr, error, "SafeArrayGetLBound");
  716. cbprop = lUbound-lLbound+1;
  717. pbprop = (LPBYTE)pData;
  718. break;
  719. case VT_BYREF|VT_I2:
  720. case VT_I2:
  721. *pdwType = REG_DWORD;
  722. pbprop = (BYTE *)((pvarPropertyValue->vt & VT_BYREF)?
  723. V_I2REF(pvarPropertyValue):
  724. &V_I2(pvarPropertyValue));
  725. cbprop = sizeof(V_I2(pvarPropertyValue));
  726. break;
  727. case VT_BYREF|VT_I4:
  728. case VT_I4:
  729. *pdwType = REG_DWORD;
  730. pbprop = (BYTE *) ((pvarPropertyValue->vt & VT_BYREF)?
  731. V_I4REF(pvarPropertyValue):
  732. &V_I4(pvarPropertyValue));
  733. cbprop = sizeof(pvarPropertyValue->lVal);
  734. break;
  735. case VT_BYREF|VT_ARRAY|VT_BSTR:
  736. case VT_ARRAY|VT_BSTR:
  737. psa = (pvarPropertyValue->vt & VT_BYREF)?
  738. *V_ARRAYREF(pvarPropertyValue):
  739. V_ARRAY(pvarPropertyValue);
  740. *pdwType = REG_MULTI_SZ;
  741. if(1!=SafeArrayGetDim(psa))
  742. {
  743. hr = E_INVALIDARG;
  744. _JumpError(hr, error, "only 1 dim array of bstr allowed");
  745. }
  746. hr = SafeArrayGetLBound(
  747. psa,
  748. 1,
  749. &lLbound);
  750. _JumpIfError(hr, error, "SafeArrayGetLBound");
  751. hr = SafeArrayGetUBound(
  752. psa,
  753. 1,
  754. &lUbound);
  755. _JumpIfError(hr, error, "SafeArrayGetLBound");
  756. hr = SafeArrayAccessData(psa, &pData);
  757. _JumpIfError(hr, error, "SafeArrayGetLBound");
  758. for(LONG c=0;c<=lUbound-lLbound;c++)
  759. {
  760. cbprop += wcslen(((BSTR*)pData)[c])+1;
  761. }
  762. cbprop += 1;
  763. cbprop *= sizeof(WCHAR);
  764. pbprop = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  765. if (NULL == pbprop)
  766. {
  767. hr = E_OUTOFMEMORY;
  768. _JumpError(hr, error, "LocalAlloc");
  769. }
  770. *ppbprop = pbprop;
  771. for(LONG c=0;c<=lUbound-lLbound;c++)
  772. {
  773. wcscpy((LPWSTR)pbprop, ((BSTR*)pData)[c]);
  774. pbprop += (wcslen((LPWSTR)pbprop)+1)*sizeof(WCHAR);
  775. }
  776. *(LPWSTR)pbprop = L'\0';
  777. pbprop = NULL; //avoid copying twice to ppbprop
  778. break;
  779. default:
  780. hr = E_INVALIDARG;
  781. _JumpError(hr, error, "invalid variant type");
  782. }
  783. if (NULL != pbprop)
  784. {
  785. *ppbprop = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  786. if (NULL == *ppbprop)
  787. {
  788. hr = E_OUTOFMEMORY;
  789. _JumpError(hr, error, "LocalAlloc");
  790. }
  791. CopyMemory(*ppbprop, pbprop, cbprop);
  792. }
  793. *pcbprop = cbprop;
  794. error:
  795. if(S_OK!=hr && pbprop)
  796. {
  797. LocalFree(pbprop);
  798. }
  799. if(pData)
  800. {
  801. CSASSERT(psa);
  802. SafeArrayUnaccessData(psa);
  803. }
  804. return(hr);
  805. }
  806. HRESULT
  807. myUnmarshalVariant(
  808. IN DWORD PropType,
  809. IN DWORD cbValue,
  810. IN BYTE const *pbValue,
  811. OUT VARIANT *pvarValue)
  812. {
  813. HRESULT hr = S_OK;
  814. CSASSERT(NULL != pvarValue);
  815. VariantInit(pvarValue);
  816. // pb = NULL, cb = 0 always returns VT_EMPTY
  817. if (NULL == pbValue)
  818. {
  819. CSASSERT(0 == cbValue);
  820. CSASSERT(VT_EMPTY == pvarValue->vt);
  821. goto error;
  822. }
  823. switch (PROPTYPE_MASK & PropType)
  824. {
  825. case PROPTYPE_STRING:
  826. if (0 == (PROPMARSHAL_LOCALSTRING & PropType) &&
  827. sizeof(WCHAR) <= cbValue)
  828. {
  829. cbValue -= sizeof(WCHAR);
  830. }
  831. if (wcslen((WCHAR const *) pbValue) * sizeof(WCHAR) != cbValue)
  832. {
  833. hr = E_INVALIDARG;
  834. _JumpError(hr, error, "bad string len");
  835. }
  836. // FALLTHROUGH:
  837. case PROPTYPE_BINARY:
  838. // CSASSERT(0 != cbValue);
  839. pvarValue->bstrVal = NULL;
  840. if (!ConvertWszToBstr(
  841. &pvarValue->bstrVal,
  842. (WCHAR const *) pbValue,
  843. cbValue))
  844. {
  845. hr = E_OUTOFMEMORY;
  846. _JumpError(hr, error, "ConvertWszToBstr");
  847. }
  848. pvarValue->vt = VT_BSTR;
  849. break;
  850. case PROPTYPE_LONG:
  851. CSASSERT(sizeof(LONG) == cbValue);
  852. pvarValue->vt = VT_I4;
  853. pvarValue->lVal = *(LONG const *) pbValue;
  854. break;
  855. case PROPTYPE_DATE:
  856. CSASSERT(sizeof(FILETIME) == cbValue);
  857. hr = myFileTimeToDate(
  858. (FILETIME const *) pbValue,
  859. &pvarValue->date);
  860. _JumpIfError(hr, error, "myFileTimeToDate");
  861. pvarValue->vt = VT_DATE;
  862. break;
  863. default:
  864. hr = E_INVALIDARG;
  865. _JumpError(hr, error, "PropType parameter");
  866. }
  867. error:
  868. return(hr);
  869. }
  870. HRESULT
  871. myUnmarshalFormattedVariant(
  872. IN DWORD Flags,
  873. IN DWORD PropId,
  874. IN DWORD PropType,
  875. IN DWORD cbValue,
  876. IN BYTE const *pbValue,
  877. OUT VARIANT *pvarValue)
  878. {
  879. HRESULT hr;
  880. BSTR strCert;
  881. hr = myUnmarshalVariant(
  882. PropType,
  883. cbValue,
  884. pbValue,
  885. pvarValue);
  886. _JumpIfError(hr, error, "myUnmarshalVariant");
  887. if (PROPTYPE_BINARY == (PROPTYPE_MASK & PropType))
  888. {
  889. CSASSERT(VT_BSTR == pvarValue->vt);
  890. CSASSERT(CV_OUT_BASE64HEADER == CRYPT_STRING_BASE64HEADER);
  891. CSASSERT(CV_OUT_BASE64 == CRYPT_STRING_BASE64);
  892. CSASSERT(CV_OUT_BINARY == CRYPT_STRING_BINARY);
  893. CSASSERT(CV_OUT_BASE64REQUESTHEADER == CRYPT_STRING_BASE64REQUESTHEADER);
  894. CSASSERT(CV_OUT_HEX == CRYPT_STRING_HEX);
  895. CSASSERT(CV_OUT_HEXASCII == CRYPT_STRING_HEXASCII);
  896. CSASSERT(CV_OUT_HEXADDR == CRYPT_STRING_HEXADDR);
  897. CSASSERT(CV_OUT_HEXASCIIADDR == CRYPT_STRING_HEXASCIIADDR);
  898. switch (Flags)
  899. {
  900. case CV_OUT_BASE64HEADER:
  901. if (CR_PROP_BASECRL == PropId || CR_PROP_DELTACRL == PropId)
  902. {
  903. Flags = CV_OUT_BASE64X509CRLHEADER;
  904. }
  905. else
  906. if (MAXDWORD == PropId)
  907. {
  908. Flags = CV_OUT_BASE64REQUESTHEADER;
  909. }
  910. break;
  911. case CV_OUT_BASE64:
  912. case CV_OUT_BINARY:
  913. case CV_OUT_BASE64REQUESTHEADER:
  914. case CV_OUT_BASE64X509CRLHEADER:
  915. case CV_OUT_HEX:
  916. case CV_OUT_HEXASCII:
  917. case CV_OUT_HEXADDR:
  918. case CV_OUT_HEXASCIIADDR:
  919. break;
  920. default:
  921. hr = E_INVALIDARG;
  922. _JumpError(hr, error, "Flags");
  923. }
  924. if (CV_OUT_BINARY != Flags)
  925. {
  926. strCert = NULL;
  927. hr = EncodeCertString(pbValue, cbValue, Flags, &strCert);
  928. _JumpIfError(hr, error, "EncodeCertString");
  929. SysFreeString(pvarValue->bstrVal);
  930. pvarValue->bstrVal = strCert;
  931. }
  932. }
  933. hr = S_OK;
  934. error:
  935. return(hr);
  936. }
  937. HRESULT
  938. myMarshalVariant(
  939. IN VARIANT const *pvarPropertyValue,
  940. IN DWORD PropType,
  941. OUT DWORD *pcbprop,
  942. OUT BYTE **ppbprop)
  943. {
  944. HRESULT hr = S_OK;
  945. DWORD cbprop = 0;
  946. BYTE *pbprop = NULL;
  947. BSTR str = NULL;
  948. FILETIME ft;
  949. LONG lval;
  950. *ppbprop = NULL;
  951. // VT_EMPTY always produces same result: *ppbprop = NULL, *pcbprop = 0
  952. if (VT_EMPTY != pvarPropertyValue->vt)
  953. {
  954. switch (PROPTYPE_MASK & PropType)
  955. {
  956. case PROPTYPE_BINARY:
  957. case PROPTYPE_STRING:
  958. switch (pvarPropertyValue->vt)
  959. {
  960. case VT_BYREF | VT_BSTR:
  961. if (NULL != pvarPropertyValue->pbstrVal)
  962. {
  963. str = *pvarPropertyValue->pbstrVal;
  964. }
  965. break;
  966. case VT_BSTR:
  967. str = pvarPropertyValue->bstrVal;
  968. break;
  969. }
  970. if (NULL == str)
  971. {
  972. if (PROPTYPE_STRING == (PROPTYPE_MASK & PropType) &&
  973. (PROPMARSHAL_NULLBSTROK & PropType) &&
  974. VT_NULL == pvarPropertyValue->vt)
  975. {
  976. cbprop = 0;
  977. }
  978. else
  979. {
  980. hr = E_INVALIDARG;
  981. _JumpError(
  982. pvarPropertyValue->vt,
  983. error,
  984. "variant BSTR type/value");
  985. }
  986. }
  987. else
  988. {
  989. pbprop = (BYTE *) str;
  990. if (PROPTYPE_BINARY == (PROPTYPE_MASK & PropType))
  991. {
  992. cbprop = SysStringByteLen(str) + sizeof(WCHAR);
  993. }
  994. else
  995. {
  996. cbprop = (wcslen(str) + 1) * sizeof(WCHAR);
  997. }
  998. }
  999. break;
  1000. case PROPTYPE_LONG:
  1001. // VB likes to send small constant integers as VT_I2
  1002. if (VT_I2 == pvarPropertyValue->vt)
  1003. {
  1004. lval = pvarPropertyValue->iVal;
  1005. }
  1006. else if (VT_I4 == pvarPropertyValue->vt)
  1007. {
  1008. lval = pvarPropertyValue->lVal;
  1009. }
  1010. else if (VT_EMPTY == pvarPropertyValue->vt)
  1011. {
  1012. pbprop = NULL;
  1013. cbprop = 0;
  1014. break;
  1015. }
  1016. else
  1017. {
  1018. hr = E_INVALIDARG;
  1019. _JumpError(pvarPropertyValue->vt, error, "variant LONG type");
  1020. }
  1021. pbprop = (BYTE *) &lval;
  1022. cbprop = sizeof(lval);
  1023. break;
  1024. case PROPTYPE_DATE:
  1025. if (VT_DATE == pvarPropertyValue->vt)
  1026. {
  1027. hr = myDateToFileTime(&pvarPropertyValue->date, &ft);
  1028. _JumpIfError(hr, error, "myDateToFileTime");
  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 DATE type");
  1040. }
  1041. pbprop = (BYTE *) &ft;
  1042. cbprop = sizeof(ft);
  1043. break;
  1044. default:
  1045. hr = E_INVALIDARG;
  1046. _JumpError(pvarPropertyValue->vt, error, "variant type/value");
  1047. }
  1048. if (NULL != pbprop)
  1049. {
  1050. *ppbprop = (BYTE *) LocalAlloc(LMEM_FIXED, cbprop);
  1051. if (NULL == *ppbprop)
  1052. {
  1053. hr = E_OUTOFMEMORY;
  1054. _JumpError(hr, error, "LocalAlloc");
  1055. }
  1056. CopyMemory(*ppbprop, pbprop, cbprop);
  1057. if (NULL != str &&
  1058. sizeof(WCHAR) <= cbprop &&
  1059. ((PROPMARSHAL_LOCALSTRING & PropType) ||
  1060. PROPTYPE_BINARY == (PROPTYPE_MASK & PropType)))
  1061. {
  1062. cbprop -= sizeof(WCHAR);
  1063. }
  1064. }
  1065. }
  1066. *pcbprop = cbprop;
  1067. error:
  1068. return(hr);
  1069. }
  1070. // IsCharRegKeyChar -- Determines if a character is valid for use in a file
  1071. // name AND in a registry key name.
  1072. #define wszInvalidFileAndKeyChars L"<>\"/\\:|?*"
  1073. #define wszUnsafeURLChars L"#\"&<>[]^`{}|"
  1074. #define wszUnsafeDSChars L"()='\"`,;+"
  1075. BOOL
  1076. myIsCharSanitized(
  1077. IN WCHAR wc)
  1078. {
  1079. BOOL fCharOk = TRUE;
  1080. if (L' ' > wc ||
  1081. L'~' < wc ||
  1082. NULL != wcschr(
  1083. wszInvalidFileAndKeyChars
  1084. wszUnsafeURLChars
  1085. wszSANITIZEESCAPECHAR
  1086. wszURLESCAPECHAR
  1087. wszUnsafeDSChars,
  1088. wc))
  1089. {
  1090. fCharOk = FALSE;
  1091. }
  1092. return(fCharOk);
  1093. }
  1094. HRESULT
  1095. mySanitizeName(
  1096. IN WCHAR const *pwszName,
  1097. OUT WCHAR **ppwszNameOut)
  1098. {
  1099. HRESULT hr = S_OK;
  1100. WCHAR const *pwszPassedName;
  1101. WCHAR *pwszDst;
  1102. WCHAR *pwszOut = NULL;
  1103. WCHAR wcChar;
  1104. DWORD dwSize;
  1105. *ppwszNameOut = NULL;
  1106. pwszPassedName = pwszName;
  1107. dwSize = 0;
  1108. if (NULL == pwszName)
  1109. {
  1110. hr = HRESULT_FROM_WIN32(ERROR_INVALID_DATA);
  1111. _JumpError(hr, error, "pwszName NULL");
  1112. }
  1113. while (L'\0' != (wcChar = *pwszPassedName++))
  1114. {
  1115. if (myIsCharSanitized(wcChar))
  1116. {
  1117. dwSize++;
  1118. }
  1119. else
  1120. {
  1121. dwSize += 5; // format !XXXX
  1122. }
  1123. }
  1124. if (0 == dwSize)
  1125. {
  1126. goto error; // done
  1127. }
  1128. pwszOut = (WCHAR *) LocalAlloc(LMEM_ZEROINIT, (dwSize + 1) * sizeof(WCHAR));
  1129. if (NULL == pwszOut)
  1130. {
  1131. hr = E_OUTOFMEMORY;
  1132. _JumpError(hr, error, "LocalAlloc");
  1133. }
  1134. pwszDst = pwszOut;
  1135. while (L'\0' != (wcChar = *pwszName++))
  1136. {
  1137. if (myIsCharSanitized(wcChar))
  1138. {
  1139. *pwszDst = wcChar;
  1140. pwszDst++;
  1141. }
  1142. else
  1143. {
  1144. wsprintf(pwszDst, L"%ws", wszSANITIZEESCAPECHAR);
  1145. pwszDst++;
  1146. wsprintf(pwszDst, L"%04x", wcChar);
  1147. pwszDst += 4;
  1148. }
  1149. }
  1150. *pwszDst = wcChar; // L'\0' terminator
  1151. *ppwszNameOut = pwszOut;
  1152. pwszOut = NULL;
  1153. hr = S_OK;
  1154. error:
  1155. if (NULL != pwszOut)
  1156. {
  1157. LocalFree(pwszOut);
  1158. }
  1159. return(hr);
  1160. }
  1161. BOOL
  1162. ScanHexEscapeDigits(
  1163. IN WCHAR const *pwszHex,
  1164. OUT WCHAR *pwcRevert)
  1165. {
  1166. BOOL ret = FALSE;
  1167. DWORD i;
  1168. WCHAR wc;
  1169. WCHAR wszValue[5];
  1170. for (i = 0; i < 4; i++)
  1171. {
  1172. wc = pwszHex[i];
  1173. wszValue[i] = wc;
  1174. if (!isascii(wc) || !isxdigit((char) wc))
  1175. {
  1176. goto error;
  1177. }
  1178. }
  1179. wszValue[4] = L'\0';
  1180. swscanf(wszValue, L"%04x", &i);
  1181. *pwcRevert = (WCHAR) i;
  1182. ret = TRUE;
  1183. error:
  1184. return(ret);
  1185. }
  1186. // This function will truncate the output if pwszName contains "!0000".
  1187. // The output string is L'\0' terminated, so the length is not returned.
  1188. HRESULT
  1189. myRevertSanitizeName(
  1190. IN WCHAR const *pwszName,
  1191. OUT WCHAR **ppwszNameOut)
  1192. {
  1193. HRESULT hr;
  1194. DWORD i;
  1195. DWORD cwc;
  1196. WCHAR wc;
  1197. WCHAR wcRevert;
  1198. WCHAR *pwszRevert;
  1199. *ppwszNameOut = NULL;
  1200. if (NULL == pwszName)
  1201. {
  1202. hr = E_POINTER;
  1203. _JumpError(hr, error, "NULL sanitized name");
  1204. }
  1205. cwc = wcslen(pwszName);
  1206. *ppwszNameOut = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  1207. if (NULL == *ppwszNameOut)
  1208. {
  1209. hr = E_OUTOFMEMORY;
  1210. _JumpError(hr, error, "Out of memory");
  1211. }
  1212. pwszRevert = *ppwszNameOut;
  1213. while (L'\0' != *pwszName)
  1214. {
  1215. wc = *pwszName++;
  1216. if (wcSANITIZEESCAPECHAR == wc &&
  1217. ScanHexEscapeDigits(pwszName, &wcRevert))
  1218. {
  1219. wc = wcRevert;
  1220. pwszName += 4;
  1221. }
  1222. *pwszRevert++ = wc;
  1223. }
  1224. *pwszRevert = L'\0';
  1225. CSASSERT(wcslen(*ppwszNameOut) <= cwc);
  1226. hr = S_OK;
  1227. error:
  1228. return hr;
  1229. }
  1230. #define cwcCNMAX 64 // 64 chars max for CN
  1231. #define cwcCHOPHASHMAX (1 + 5) // "-%05hu" decimal USHORT hash digits
  1232. #define cwcCHOPBASE (cwcCNMAX - (cwcCHOPHASHMAX + cwcSUFFIXMAX))
  1233. HRESULT
  1234. mySanitizedNameToDSName(
  1235. IN WCHAR const *pwszSanitizedName,
  1236. OUT WCHAR **ppwszNameOut)
  1237. {
  1238. HRESULT hr;
  1239. DWORD cwc;
  1240. DWORD cwcCopy;
  1241. WCHAR wszDSName[cwcCHOPBASE + cwcCHOPHASHMAX + 1];
  1242. *ppwszNameOut = NULL;
  1243. cwc = wcslen(pwszSanitizedName);
  1244. cwcCopy = cwc;
  1245. if (cwcCHOPBASE < cwcCopy)
  1246. {
  1247. cwcCopy = cwcCHOPBASE;
  1248. }
  1249. CopyMemory(wszDSName, pwszSanitizedName, cwcCopy * sizeof(WCHAR));
  1250. wszDSName[cwcCopy] = L'\0';
  1251. if (cwcCHOPBASE < cwc)
  1252. {
  1253. // Hash the rest of the name into a USHORT
  1254. USHORT usHash = 0;
  1255. DWORD i;
  1256. WCHAR *pwsz;
  1257. // Truncate an incomplete sanitized Unicode character
  1258. pwsz = wcsrchr(wszDSName, L'!');
  1259. if (NULL != pwsz && wcslen(pwsz) < 5)
  1260. {
  1261. cwcCopy -= wcslen(pwsz);
  1262. *pwsz = L'\0';
  1263. }
  1264. for (i = cwcCopy; i < cwc; i++)
  1265. {
  1266. USHORT usLowBit = (0x8000 & usHash)? 1 : 0;
  1267. usHash = ((usHash << 1) | usLowBit) + pwszSanitizedName[i];
  1268. }
  1269. wsprintf(&wszDSName[cwcCopy], L"-%05hu", usHash);
  1270. CSASSERT(wcslen(wszDSName) < ARRAYSIZE(wszDSName));
  1271. }
  1272. hr = myDupString(wszDSName, ppwszNameOut);
  1273. _JumpIfError(hr, error, "myDupString");
  1274. DBGPRINT((DBG_SS_CERTLIBI, "mySanitizedNameToDSName(%ws)\n", *ppwszNameOut));
  1275. error:
  1276. return(hr);
  1277. }
  1278. // Decode OCTET string, and convert UTF8 string to Unicode.
  1279. // Can return S_OK and NULL pointer.
  1280. HRESULT
  1281. myDecodeCMCRegInfo(
  1282. IN BYTE const *pbOctet,
  1283. IN DWORD cbOctet,
  1284. OUT WCHAR **ppwszRA)
  1285. {
  1286. HRESULT hr;
  1287. CRYPT_DATA_BLOB *pBlob = NULL;
  1288. DWORD cb;
  1289. *ppwszRA = NULL;
  1290. if (!myDecodeObject(
  1291. X509_ASN_ENCODING,
  1292. X509_OCTET_STRING,
  1293. pbOctet,
  1294. cbOctet,
  1295. CERTLIB_USE_LOCALALLOC,
  1296. (VOID **) &pBlob,
  1297. &cb))
  1298. {
  1299. hr = myHLastError();
  1300. _JumpError(hr, error, "myDecodeObject");
  1301. }
  1302. if (0 != pBlob->cbData && NULL != pBlob->pbData)
  1303. {
  1304. if (!myConvertUTF8ToWsz(
  1305. ppwszRA,
  1306. (CHAR const *) pBlob->pbData,
  1307. pBlob->cbData))
  1308. {
  1309. hr = myHLastError();
  1310. _JumpError(hr, error, "myConvertUTF8ToWsz");
  1311. }
  1312. }
  1313. hr = S_OK;
  1314. error:
  1315. if (NULL != pBlob)
  1316. {
  1317. LocalFree(pBlob);
  1318. }
  1319. return(hr);
  1320. }
  1321. DWORD
  1322. myGetCertNameProperty(
  1323. IN CERT_NAME_INFO const *pNameInfo,
  1324. IN char const *pszObjId,
  1325. OUT WCHAR const **ppwszName)
  1326. {
  1327. HRESULT hr;
  1328. CERT_RDN_ATTR const *prdnaT;
  1329. CERT_RDN const *prdn;
  1330. CERT_RDN const *prdnEnd;
  1331. prdnaT = NULL;
  1332. for (
  1333. prdn = pNameInfo->rgRDN, prdnEnd = &prdn[pNameInfo->cRDN];
  1334. prdn < prdnEnd;
  1335. prdn++)
  1336. {
  1337. CERT_RDN_ATTR *prdna;
  1338. CERT_RDN_ATTR *prdnaEnd;
  1339. for (
  1340. prdna = prdn->rgRDNAttr, prdnaEnd = &prdna[prdn->cRDNAttr];
  1341. prdna < prdnaEnd;
  1342. prdna++)
  1343. {
  1344. if (0 == strcmp(prdna->pszObjId, pszObjId))
  1345. {
  1346. prdnaT = prdna;
  1347. }
  1348. }
  1349. }
  1350. if (NULL == prdnaT)
  1351. {
  1352. hr = CERTSRV_E_PROPERTY_EMPTY;
  1353. goto error;
  1354. }
  1355. *ppwszName = (WCHAR const *) prdnaT->Value.pbData;
  1356. hr = S_OK;
  1357. error:
  1358. return(hr);
  1359. }
  1360. VOID
  1361. myGenerateGuidSerialNumber(
  1362. OUT GUID *pguidSerialNumber)
  1363. {
  1364. HRESULT hr;
  1365. BYTE *pb;
  1366. ZeroMemory(pguidSerialNumber, sizeof(*pguidSerialNumber));
  1367. hr = UuidCreate(pguidSerialNumber);
  1368. if (S_OK != hr)
  1369. {
  1370. BYTE *pbEnd;
  1371. CSASSERT(RPC_S_UUID_LOCAL_ONLY == hr);
  1372. // No net card? Fake up a GUID:
  1373. pb = (BYTE *) pguidSerialNumber;
  1374. pbEnd = (BYTE *) pb + sizeof(*pguidSerialNumber);
  1375. GetSystemTimeAsFileTime((FILETIME *) pb);
  1376. pb += sizeof(FILETIME);
  1377. while (pb < pbEnd)
  1378. {
  1379. *(DWORD *) pb = GetTickCount();
  1380. pb += sizeof(DWORD);
  1381. }
  1382. CSASSERT(pb == pbEnd);
  1383. }
  1384. pb = &((BYTE *) pguidSerialNumber)[sizeof(*pguidSerialNumber) - 1];
  1385. // make sure the last byte is never zero
  1386. if (0 == *pb)
  1387. {
  1388. *pb = 'z';
  1389. }
  1390. // Some clients can't handle negative serial numbers:
  1391. *pb &= 0x7f;
  1392. }
  1393. BOOL
  1394. myAreBlobsSame(
  1395. IN BYTE const *pbData1,
  1396. IN DWORD cbData1,
  1397. IN BYTE const *pbData2,
  1398. IN DWORD cbData2)
  1399. {
  1400. BOOL ret = FALSE;
  1401. if (cbData1 != cbData2)
  1402. {
  1403. goto error;
  1404. }
  1405. if (NULL != pbData1 && NULL != pbData2)
  1406. {
  1407. if (0 != memcmp(pbData1, pbData2, cbData1))
  1408. {
  1409. goto error;
  1410. }
  1411. }
  1412. // else at least one is NULL -- they'd better both be NULL, & the count 0.
  1413. else if (pbData1 != pbData2 || 0 != cbData1)
  1414. {
  1415. goto error;
  1416. }
  1417. ret = TRUE;
  1418. error:
  1419. return(ret);
  1420. }
  1421. BOOL
  1422. myAreSerialNumberBlobsSame(
  1423. IN CRYPT_INTEGER_BLOB const *pBlob1,
  1424. IN CRYPT_INTEGER_BLOB const *pBlob2)
  1425. {
  1426. DWORD cbData1 = pBlob1->cbData;
  1427. DWORD cbData2 = pBlob2->cbData;
  1428. if (NULL != pBlob1->pbData)
  1429. {
  1430. while (0 != cbData1 && 0 == pBlob1->pbData[cbData1 - 1])
  1431. {
  1432. cbData1--;
  1433. }
  1434. }
  1435. if (NULL != pBlob2->pbData)
  1436. {
  1437. while (0 != cbData2 && 0 == pBlob2->pbData[cbData2 - 1])
  1438. {
  1439. cbData2--;
  1440. }
  1441. }
  1442. return(myAreBlobsSame(pBlob1->pbData, cbData1, pBlob2->pbData, cbData2));
  1443. }
  1444. WCHAR const g_wszCert[] = L"cert";
  1445. HRESULT
  1446. myIsDirWriteable(
  1447. IN WCHAR const *pwszPath,
  1448. IN BOOL fFilePath)
  1449. {
  1450. HRESULT hr;
  1451. WCHAR *pwszBase;
  1452. WCHAR *pwsz;
  1453. WCHAR wszDir[MAX_PATH];
  1454. WCHAR wszTempFile[MAX_PATH];
  1455. if (fFilePath &&
  1456. iswalpha(pwszPath[0]) &&
  1457. L':' == pwszPath[1] &&
  1458. L'\0' == pwszPath[2])
  1459. {
  1460. hr = HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME);
  1461. _JumpErrorStr(hr, error, "not a file path", pwszPath);
  1462. }
  1463. if (!GetFullPathName(
  1464. pwszPath,
  1465. ARRAYSIZE(wszDir),
  1466. wszDir,
  1467. &pwsz))
  1468. {
  1469. hr = myHLastError();
  1470. _JumpErrorStr(hr, error, "GetFullPathName", pwszPath);
  1471. }
  1472. if (fFilePath)
  1473. {
  1474. if (NULL == pwsz)
  1475. {
  1476. hr = HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME);
  1477. _JumpErrorStr(hr, error, "not a file path", pwszPath);
  1478. }
  1479. pwszBase = wszDir;
  1480. if (iswalpha(wszDir[0]) &&
  1481. L':' == wszDir[1] &&
  1482. L'\\' == wszDir[2])
  1483. {
  1484. pwszBase += 3;
  1485. }
  1486. else if (L'\\' == wszDir[0] && L'\\' == wszDir[1])
  1487. {
  1488. pwszBase += 2;
  1489. }
  1490. if (pwsz > pwszBase && L'\\' == pwsz[-1])
  1491. {
  1492. pwsz--;
  1493. }
  1494. *pwsz = L'\0';
  1495. }
  1496. if (!GetTempFileName(wszDir, g_wszCert, 0, wszTempFile))
  1497. {
  1498. hr = myHLastError();
  1499. _JumpErrorStr(hr, error, "GetTempFileName", wszDir);
  1500. }
  1501. if (!DeleteFile(wszTempFile))
  1502. {
  1503. hr = myHLastError();
  1504. _JumpErrorStr(hr, error, "DeleteFile", wszTempFile);
  1505. }
  1506. hr = S_OK;
  1507. error:
  1508. return(hr);
  1509. }
  1510. HRESULT
  1511. myGetComputerObjectName(
  1512. IN EXTENDED_NAME_FORMAT NameFormat,
  1513. OUT WCHAR **ppwszComputerObjectName)
  1514. {
  1515. HRESULT hr;
  1516. WCHAR *pwszComputerObjectName = NULL;
  1517. DWORD cwc;
  1518. *ppwszComputerObjectName = NULL;
  1519. cwc = 0;
  1520. if (!GetComputerObjectName(NameFormat, NULL, &cwc))
  1521. {
  1522. hr = myHLastError();
  1523. if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) != hr)
  1524. {
  1525. _JumpError(hr, error, "GetComputerObjectName");
  1526. }
  1527. }
  1528. pwszComputerObjectName = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1529. if (NULL == pwszComputerObjectName)
  1530. {
  1531. hr = E_OUTOFMEMORY;
  1532. _JumpError(hr, error, "LocalAlloc");
  1533. }
  1534. if (!GetComputerObjectName(NameFormat, pwszComputerObjectName, &cwc))
  1535. {
  1536. hr = myHLastError();
  1537. _JumpError(hr, error, "GetComputerObjectName");
  1538. }
  1539. *ppwszComputerObjectName = pwszComputerObjectName;
  1540. pwszComputerObjectName = NULL;
  1541. hr = S_OK;
  1542. error:
  1543. if (NULL != pwszComputerObjectName)
  1544. {
  1545. LocalFree(pwszComputerObjectName);
  1546. }
  1547. return(hr);
  1548. }
  1549. HRESULT
  1550. myGetComputerNames(
  1551. OUT WCHAR **ppwszDnsName,
  1552. OUT WCHAR **ppwszOldName)
  1553. {
  1554. HRESULT hr;
  1555. DWORD cwc;
  1556. WCHAR *pwszOldName = NULL;
  1557. *ppwszOldName = NULL;
  1558. *ppwszDnsName = NULL;
  1559. cwc = MAX_COMPUTERNAME_LENGTH + 1;
  1560. pwszOldName = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1561. if (NULL == pwszOldName)
  1562. {
  1563. hr = E_OUTOFMEMORY;
  1564. _JumpError(hr, error, "LocalAlloc");
  1565. }
  1566. if (!GetComputerName(pwszOldName, &cwc))
  1567. {
  1568. hr = myHLastError();
  1569. _JumpError(hr, error, "GetComputerName");
  1570. }
  1571. hr = myGetMachineDnsName(ppwszDnsName);
  1572. _JumpIfError(hr, error, "myGetMachineDnsName");
  1573. *ppwszOldName = pwszOldName;
  1574. pwszOldName = NULL;
  1575. error:
  1576. if (NULL != pwszOldName)
  1577. {
  1578. LocalFree(pwszOldName);
  1579. }
  1580. return(hr);
  1581. }
  1582. typedef LANGID (WINAPI FNSETTHREADUILANGUAGE)(
  1583. IN WORD wReserved);
  1584. LANGID
  1585. mySetThreadUILanguage(
  1586. IN WORD wReserved)
  1587. {
  1588. HMODULE hModule;
  1589. LANGID lang = 0;
  1590. DWORD err;
  1591. static FNSETTHREADUILANGUAGE *s_pfn = NULL;
  1592. if (NULL == s_pfn)
  1593. {
  1594. hModule = GetModuleHandle(TEXT("kernel32.dll"));
  1595. if (NULL == hModule)
  1596. {
  1597. goto error;
  1598. }
  1599. // load system function
  1600. s_pfn = (FNSETTHREADUILANGUAGE *) GetProcAddress(
  1601. hModule,
  1602. "SetThreadUILanguage");
  1603. if (NULL == s_pfn)
  1604. {
  1605. goto error;
  1606. }
  1607. }
  1608. lang = (*s_pfn)(wReserved);
  1609. error:
  1610. return(lang);
  1611. }
  1612. HRESULT
  1613. myFormConfigString(
  1614. IN WCHAR const *pwszServer,
  1615. IN WCHAR const *pwszCAName,
  1616. OUT WCHAR **ppwszConfig)
  1617. {
  1618. HRESULT hr;
  1619. WCHAR *pwszConfig = NULL;
  1620. *ppwszConfig = NULL;
  1621. pwszConfig = (WCHAR *) LocalAlloc(LPTR,
  1622. (wcslen(pwszServer) + wcslen(pwszCAName) + 2) * sizeof(WCHAR));
  1623. if (NULL == pwszConfig)
  1624. {
  1625. hr = E_OUTOFMEMORY;
  1626. _JumpError(hr, error, "LocalAlloc");
  1627. }
  1628. wcscpy(pwszConfig, pwszServer);
  1629. wcscat(pwszConfig, L"\\");
  1630. wcscat(pwszConfig, pwszCAName);
  1631. *ppwszConfig = pwszConfig;
  1632. hr = S_OK;
  1633. error:
  1634. return hr;
  1635. }
  1636. HRESULT
  1637. myCLSIDToWsz(
  1638. IN CLSID const *pclsid,
  1639. OUT WCHAR **ppwsz)
  1640. {
  1641. HRESULT hr;
  1642. WCHAR *pwsz = NULL;
  1643. WCHAR *pwsz1;
  1644. WCHAR *pwsz2;
  1645. *ppwsz = NULL;
  1646. hr = StringFromCLSID(*pclsid, &pwsz);
  1647. _JumpIfError(hr, error, "StringFromCLSID");
  1648. for (pwsz1 = pwsz; L'\0' != *pwsz1; pwsz1++)
  1649. {
  1650. if (L'A' <= *pwsz1 && L'F' >= *pwsz1)
  1651. {
  1652. *pwsz1 += L'a' - L'A';
  1653. }
  1654. }
  1655. pwsz1 = pwsz;
  1656. pwsz2 = &pwsz[wcslen(pwsz) - 1];
  1657. if (wcLBRACE == *pwsz1 && wcRBRACE == *pwsz2)
  1658. {
  1659. pwsz1++;
  1660. *pwsz2 = L'\0';
  1661. }
  1662. hr = myDupString(pwsz1, ppwsz);
  1663. _JumpIfError(hr, error, "myDupString");
  1664. error:
  1665. if (NULL != pwsz)
  1666. {
  1667. CoTaskMemFree(pwsz);
  1668. }
  1669. return(hr);
  1670. }
  1671. HRESULT
  1672. myLoadRCString(
  1673. IN HINSTANCE hInstance,
  1674. IN int iRCId,
  1675. OUT WCHAR **ppwsz)
  1676. {
  1677. #define CS_RCALLOCATEBLOCK 256
  1678. HRESULT hr;
  1679. WCHAR *pwszTemp = NULL;
  1680. int sizeTemp;
  1681. int size;
  1682. int cBlocks = 1;
  1683. *ppwsz = NULL;
  1684. while (NULL == pwszTemp)
  1685. {
  1686. sizeTemp = cBlocks * CS_RCALLOCATEBLOCK;
  1687. pwszTemp = (WCHAR*)LocalAlloc(LMEM_FIXED | LMEM_ZEROINIT,
  1688. sizeTemp * sizeof(WCHAR));
  1689. if (NULL == pwszTemp)
  1690. {
  1691. hr = E_OUTOFMEMORY;
  1692. _JumpError(hr, error, "LocalAlloc");
  1693. }
  1694. size = LoadString(
  1695. hInstance,
  1696. iRCId,
  1697. pwszTemp,
  1698. sizeTemp);
  1699. if (0 == size)
  1700. {
  1701. hr = myHLastError();
  1702. DBGPRINT((
  1703. DBG_SS_CERTLIB,
  1704. "myLoadRCString(hInstance=%x, iRCId=%u) --> %x\n",
  1705. hInstance,
  1706. iRCId,
  1707. hr));
  1708. if (S_OK != hr &&
  1709. HRESULT_FROM_WIN32(ERROR_RESOURCE_NAME_NOT_FOUND) != hr)
  1710. {
  1711. _JumpError(hr, error, "LoadString");
  1712. }
  1713. }
  1714. if (size < sizeTemp - 1)
  1715. {
  1716. // ok, size is big enough
  1717. break;
  1718. }
  1719. ++cBlocks;
  1720. LocalFree(pwszTemp);
  1721. pwszTemp = NULL;
  1722. }
  1723. *ppwsz = (WCHAR*) LocalAlloc(LPTR, (size+1) * sizeof(WCHAR));
  1724. if (NULL == *ppwsz)
  1725. {
  1726. hr = E_OUTOFMEMORY;
  1727. _JumpError(hr, error, "LocalAlloc");
  1728. }
  1729. if (0 == size)
  1730. {
  1731. // two possible cases, 1) real empty string or
  1732. // 2) id not found in resource. either case make it empty string
  1733. **ppwsz = L'\0';
  1734. }
  1735. else
  1736. {
  1737. // copy it
  1738. wcscpy(*ppwsz, pwszTemp);
  1739. }
  1740. hr = S_OK;
  1741. error:
  1742. if (NULL != pwszTemp)
  1743. {
  1744. LocalFree(pwszTemp);
  1745. }
  1746. return hr;
  1747. }
  1748. HRESULT
  1749. _IsConfigLocal(
  1750. IN WCHAR const *pwszConfig,
  1751. IN WCHAR const *pwszDnsName,
  1752. IN WCHAR const *pwszOldName,
  1753. OPTIONAL OUT WCHAR **ppwszMachine,
  1754. OUT BOOL *pfLocal)
  1755. {
  1756. HRESULT hr;
  1757. WCHAR *pwszMachine = NULL;
  1758. WCHAR const *pwsz;
  1759. DWORD cwc;
  1760. *pfLocal = FALSE;
  1761. if (NULL != ppwszMachine)
  1762. {
  1763. *ppwszMachine = NULL;
  1764. }
  1765. while (L'\\' == *pwszConfig)
  1766. {
  1767. pwszConfig++;
  1768. }
  1769. pwsz = wcschr(pwszConfig, L'\\');
  1770. if (NULL != pwsz)
  1771. {
  1772. cwc = SAFE_SUBTRACT_POINTERS(pwsz, pwszConfig);
  1773. }
  1774. else
  1775. {
  1776. cwc = wcslen(pwszConfig);
  1777. }
  1778. pwszMachine = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  1779. if (NULL == pwszMachine)
  1780. {
  1781. hr = E_OUTOFMEMORY;
  1782. _JumpError(hr, error, "LocalAlloc");
  1783. }
  1784. CopyMemory(pwszMachine, pwszConfig, cwc * sizeof(WCHAR));
  1785. pwszMachine[cwc] = L'\0';
  1786. if (0 == lstrcmpi(pwszMachine, pwszDnsName) ||
  1787. 0 == lstrcmpi(pwszMachine, pwszOldName))
  1788. {
  1789. *pfLocal = TRUE;
  1790. }
  1791. if (NULL != ppwszMachine)
  1792. {
  1793. *ppwszMachine = pwszMachine;
  1794. pwszMachine = NULL;
  1795. }
  1796. hr = S_OK;
  1797. error:
  1798. if (NULL != pwszMachine)
  1799. {
  1800. LocalFree(pwszMachine);
  1801. }
  1802. return(hr);
  1803. }
  1804. HRESULT
  1805. myIsConfigLocal(
  1806. IN WCHAR const *pwszConfig,
  1807. OPTIONAL OUT WCHAR **ppwszMachine,
  1808. OUT BOOL *pfLocal)
  1809. {
  1810. HRESULT hr;
  1811. WCHAR *pwszDnsName = NULL;
  1812. WCHAR *pwszOldName = NULL;
  1813. *pfLocal = FALSE;
  1814. if (NULL != ppwszMachine)
  1815. {
  1816. *ppwszMachine = NULL;
  1817. }
  1818. hr = myGetComputerNames(&pwszDnsName, &pwszOldName);
  1819. _JumpIfError(hr, error, "myGetComputerNames");
  1820. hr = _IsConfigLocal(
  1821. pwszConfig,
  1822. pwszDnsName,
  1823. pwszOldName,
  1824. ppwszMachine,
  1825. pfLocal);
  1826. _JumpIfError(hr, error, "_IsConfigLocal");
  1827. error:
  1828. if (NULL != pwszDnsName)
  1829. {
  1830. LocalFree(pwszDnsName);
  1831. }
  1832. if (NULL != pwszOldName)
  1833. {
  1834. LocalFree(pwszOldName);
  1835. }
  1836. return(hr);
  1837. }
  1838. HRESULT
  1839. myIsConfigLocal2(
  1840. IN WCHAR const *pwszConfig,
  1841. IN WCHAR const *pwszDnsName,
  1842. IN WCHAR const *pwszOldName,
  1843. OUT BOOL *pfLocal)
  1844. {
  1845. HRESULT hr;
  1846. hr = _IsConfigLocal(
  1847. pwszConfig,
  1848. pwszDnsName,
  1849. pwszOldName,
  1850. NULL,
  1851. pfLocal);
  1852. _JumpIfError(hr, error, "_IsConfigLocal");
  1853. error:
  1854. return(hr);
  1855. }
  1856. HRESULT
  1857. myGetConfig(
  1858. IN DWORD dwUIFlag,
  1859. OUT WCHAR **ppwszConfig)
  1860. {
  1861. HRESULT hr;
  1862. HRESULT hr2;
  1863. BSTR strConfig = NULL;
  1864. WCHAR *pwszConfig = NULL;
  1865. WCHAR *pwszActiveCA = NULL;
  1866. WCHAR *pwszCommonName = NULL;
  1867. WCHAR *pwszDnsName = NULL;
  1868. CSASSERT(NULL != ppwszConfig);
  1869. *ppwszConfig = NULL;
  1870. hr = ConfigGetConfig(DISPSETUP_COM, dwUIFlag, &strConfig);
  1871. if (S_OK != hr)
  1872. {
  1873. if (CC_LOCALCONFIG != dwUIFlag)
  1874. {
  1875. _JumpError(hr, error, "ConfigGetConfig");
  1876. }
  1877. hr2 = hr;
  1878. hr = myGetCertRegStrValue(
  1879. NULL,
  1880. NULL,
  1881. NULL,
  1882. wszREGACTIVE,
  1883. &pwszActiveCA);
  1884. _PrintIfError(hr, "myGetCertRegStrValue");
  1885. if (S_OK == hr)
  1886. {
  1887. hr = myGetCertRegStrValue(
  1888. pwszActiveCA,
  1889. NULL,
  1890. NULL,
  1891. wszREGCOMMONNAME,
  1892. &pwszCommonName);
  1893. _PrintIfError(hr, "myGetCertRegStrValue");
  1894. }
  1895. if (S_OK != hr)
  1896. {
  1897. hr = hr2;
  1898. _JumpError(hr, error, "ConfigGetConfig");
  1899. }
  1900. hr = myGetMachineDnsName(&pwszDnsName);
  1901. _JumpIfError(hr, error, "myGetMachineDnsName");
  1902. hr = myFormConfigString(pwszDnsName, pwszCommonName, &pwszConfig);
  1903. _JumpIfError(hr, error, "myFormConfigString");
  1904. if (!ConvertWszToBstr(&strConfig, pwszConfig, MAXDWORD))
  1905. {
  1906. hr = E_OUTOFMEMORY;
  1907. _JumpError(hr, error, "ConvertWszToBstr");
  1908. }
  1909. }
  1910. *ppwszConfig = (WCHAR *) LocalAlloc(
  1911. LMEM_FIXED,
  1912. SysStringByteLen(strConfig) + sizeof(WCHAR));
  1913. if (NULL == *ppwszConfig)
  1914. {
  1915. hr = E_OUTOFMEMORY;
  1916. _JumpError(hr, error, "LocalAlloc");
  1917. }
  1918. wcscpy(*ppwszConfig, strConfig);
  1919. CSASSERT(
  1920. wcslen(*ppwszConfig) * sizeof(WCHAR) ==
  1921. SysStringByteLen(strConfig));
  1922. hr = S_OK;
  1923. error:
  1924. if (NULL != pwszActiveCA)
  1925. {
  1926. LocalFree(pwszActiveCA);
  1927. }
  1928. if (NULL != pwszCommonName)
  1929. {
  1930. LocalFree(pwszCommonName);
  1931. }
  1932. if (NULL != pwszDnsName)
  1933. {
  1934. LocalFree(pwszDnsName);
  1935. }
  1936. if (NULL != pwszConfig)
  1937. {
  1938. LocalFree(pwszConfig);
  1939. }
  1940. if (NULL != strConfig)
  1941. {
  1942. SysFreeString(strConfig);
  1943. }
  1944. return(hr);
  1945. }
  1946. HRESULT
  1947. myBuildPathAndExt(
  1948. IN WCHAR const *pwszDir,
  1949. IN WCHAR const *pwszFile,
  1950. OPTIONAL IN WCHAR const *pwszExt,
  1951. OUT WCHAR **ppwszPath)
  1952. {
  1953. HRESULT hr;
  1954. WCHAR *pwsz;
  1955. DWORD cwc;
  1956. *ppwszPath = NULL;
  1957. cwc = wcslen(pwszDir) + 1 + wcslen(pwszFile) + 1;
  1958. if (NULL != pwszExt)
  1959. {
  1960. cwc += wcslen(pwszExt);
  1961. }
  1962. pwsz = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  1963. if (NULL == pwsz)
  1964. {
  1965. hr = E_OUTOFMEMORY;
  1966. _JumpError(hr, error, "LocalAlloc");
  1967. }
  1968. wcscpy(pwsz, pwszDir);
  1969. if (L'\\' != pwsz[wcslen(pwsz) - 1])
  1970. {
  1971. wcscat(pwsz, L"\\");
  1972. }
  1973. wcscat(pwsz, pwszFile);
  1974. if (NULL != pwszExt)
  1975. {
  1976. wcscat(pwsz, pwszExt);
  1977. }
  1978. *ppwszPath = pwsz;
  1979. hr = S_OK;
  1980. error:
  1981. return(hr);
  1982. }
  1983. HRESULT
  1984. myDeleteFilePattern(
  1985. IN WCHAR const *pwszDir,
  1986. OPTIONAL IN WCHAR const *pwszPattern, // defaults to L"*.*"
  1987. IN BOOL fRecurse)
  1988. {
  1989. HRESULT hr;
  1990. HRESULT hr2;
  1991. HANDLE hf;
  1992. WIN32_FIND_DATA wfd;
  1993. WCHAR *pwszFindPath = NULL;
  1994. WCHAR *pwszDeleteFile;
  1995. WCHAR *pwszDeletePath = NULL;
  1996. DBGPRINT((
  1997. DBG_SS_CERTLIBI,
  1998. "myDeleteFilePattern(%ws, %ws, %ws)\n",
  1999. pwszDir,
  2000. pwszPattern,
  2001. fRecurse? L"Recurse" : L"NO Recurse"));
  2002. if (NULL == pwszPattern)
  2003. {
  2004. pwszPattern = L"*.*";
  2005. }
  2006. hr = myBuildPathAndExt(pwszDir, pwszPattern, NULL, &pwszFindPath);
  2007. _JumpIfError(hr, error, "myBuildPathAndExt");
  2008. pwszDeletePath = (WCHAR *) LocalAlloc(
  2009. LMEM_FIXED,
  2010. (wcslen(pwszDir) + 1 + MAX_PATH) * sizeof(WCHAR));
  2011. if (NULL == pwszDeletePath)
  2012. {
  2013. hr = E_OUTOFMEMORY;
  2014. _JumpError(hr, error, "LocalAlloc");
  2015. }
  2016. wcscpy(pwszDeletePath, pwszFindPath);
  2017. pwszDeleteFile = wcsrchr(pwszDeletePath, L'\\');
  2018. CSASSERT(NULL != pwszDeleteFile);
  2019. pwszDeleteFile++;
  2020. hf = FindFirstFile(pwszFindPath, &wfd);
  2021. if (INVALID_HANDLE_VALUE == hf)
  2022. {
  2023. hr = myHLastError();
  2024. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr ||
  2025. HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) == hr)
  2026. {
  2027. hr = S_OK;
  2028. goto error;
  2029. }
  2030. _JumpErrorStr(hr, error, "FindFirstFile", pwszFindPath);
  2031. }
  2032. do
  2033. {
  2034. wcscpy(pwszDeleteFile, wfd.cFileName);
  2035. if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  2036. {
  2037. if (fRecurse &&
  2038. 0 != lstrcmp(L".", wfd.cFileName) &&
  2039. 0 != lstrcmp(L"..", wfd.cFileName))
  2040. {
  2041. DBGPRINT((
  2042. DBG_SS_CERTLIBI,
  2043. "myDeleteFilePattern(DIR): %ws\n",
  2044. pwszDeletePath));
  2045. hr2 = myRemoveFilesAndDirectory(pwszDeletePath, TRUE);
  2046. if (S_OK != hr2)
  2047. {
  2048. if (S_OK == hr)
  2049. {
  2050. hr = hr2; // only return first error
  2051. }
  2052. _PrintErrorStr(hr2, "myRemoveFilesAndDirectory", pwszDeletePath);
  2053. }
  2054. }
  2055. }
  2056. else
  2057. {
  2058. DBGPRINT((
  2059. DBG_SS_CERTLIBI,
  2060. "myDeleteFilePattern: %ws\n",
  2061. pwszDeletePath));
  2062. if (!DeleteFile(pwszDeletePath))
  2063. {
  2064. hr2 = myHLastError();
  2065. if (S_OK == hr)
  2066. {
  2067. hr = hr2; // only return first error
  2068. }
  2069. _PrintErrorStr(hr2, "DeleteFile", pwszDeletePath);
  2070. }
  2071. }
  2072. } while (FindNextFile(hf, &wfd));
  2073. FindClose(hf);
  2074. error:
  2075. if (NULL != pwszFindPath)
  2076. {
  2077. LocalFree(pwszFindPath);
  2078. }
  2079. if (NULL != pwszDeletePath)
  2080. {
  2081. LocalFree(pwszDeletePath);
  2082. }
  2083. return(hr);
  2084. }
  2085. HRESULT
  2086. myRemoveFilesAndDirectory(
  2087. IN WCHAR const *pwszPath,
  2088. IN BOOL fRecurse)
  2089. {
  2090. HRESULT hr;
  2091. HRESULT hr2;
  2092. hr = myDeleteFilePattern(pwszPath, NULL, fRecurse);
  2093. if (S_OK != hr)
  2094. {
  2095. _PrintErrorStr(hr, "myDeleteFilePattern", pwszPath);
  2096. }
  2097. if (!RemoveDirectory(pwszPath))
  2098. {
  2099. hr2 = myHLastError();
  2100. if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) != hr2 &&
  2101. HRESULT_FROM_WIN32(ERROR_PATH_NOT_FOUND) != hr2)
  2102. {
  2103. if (S_OK == hr)
  2104. {
  2105. hr = hr2; // only return first error
  2106. }
  2107. _JumpErrorStr(hr2, error, "RemoveDirectory", pwszPath);
  2108. }
  2109. }
  2110. error:
  2111. return(hr);
  2112. }
  2113. BOOL
  2114. myIsFullPath(
  2115. IN WCHAR const *pwszPath,
  2116. OUT DWORD *pdwFlag)
  2117. {
  2118. BOOL fFullPath = FALSE;
  2119. *pdwFlag = 0;
  2120. if (NULL != pwszPath)
  2121. {
  2122. if (L'\\' == pwszPath[0] && L'\\' == pwszPath[1])
  2123. {
  2124. fFullPath = TRUE;
  2125. *pdwFlag = UNC_PATH;
  2126. }
  2127. else
  2128. if (iswalpha(pwszPath[0]) &&
  2129. L':' == pwszPath[1] &&
  2130. L'\\' == pwszPath[2])
  2131. {
  2132. fFullPath = TRUE;
  2133. *pdwFlag = LOCAL_PATH;
  2134. }
  2135. }
  2136. return(fFullPath);
  2137. }
  2138. // Convert local full path to UNC, as in c:\foo... --> \\server\c$\foo...
  2139. // If pwszServer is NULL or empty, preserve the local full path
  2140. HRESULT
  2141. myConvertLocalPathToUNC(
  2142. OPTIONAL IN WCHAR const *pwszServer,
  2143. IN WCHAR const *pwszFile,
  2144. OUT WCHAR **ppwszFileUNC)
  2145. {
  2146. HRESULT hr;
  2147. DWORD cwc;
  2148. WCHAR const *pwsz;
  2149. WCHAR *pwszDst;
  2150. WCHAR *pwszFileUNC = NULL;
  2151. if (!iswalpha(pwszFile[0]) || L':' != pwszFile[1] || L'\\' != pwszFile[2])
  2152. {
  2153. hr = E_INVALIDARG;
  2154. _JumpError(hr, error, "non-local path");
  2155. }
  2156. if (NULL != pwszServer && L'\0' == *pwszServer)
  2157. {
  2158. pwszServer = NULL;
  2159. }
  2160. cwc = wcslen(pwszFile) + 1;
  2161. if (NULL != pwszServer)
  2162. {
  2163. cwc += 2 + wcslen(pwszServer) + 1;
  2164. }
  2165. pwszFileUNC = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2166. if (NULL == pwszFileUNC)
  2167. {
  2168. hr = E_OUTOFMEMORY;
  2169. _JumpError(hr, error, "LocalAlloc pwszFiles");
  2170. }
  2171. pwsz = pwszFile;
  2172. pwszDst = pwszFileUNC;
  2173. if (NULL != pwszServer)
  2174. {
  2175. wcscpy(pwszDst, L"\\\\"); // --> "\\"
  2176. wcscat(pwszDst, pwszServer); // --> "\\server"
  2177. pwszDst += wcslen(pwszDst);
  2178. *pwszDst++ = L'\\'; // --> "\\server\"
  2179. *pwszDst++ = *pwsz++; // --> "\\server\c"
  2180. *pwszDst++ = L'$'; // --> "\\server\c$"
  2181. pwsz++; // skip colon
  2182. }
  2183. wcscpy(pwszDst, pwsz); // --> "\\server\c$\foo..."
  2184. *ppwszFileUNC = pwszFileUNC;
  2185. hr = S_OK;
  2186. error:
  2187. return(hr);
  2188. }
  2189. WCHAR const *
  2190. LocalStart(
  2191. IN WCHAR const *pwsz,
  2192. OUT BOOL *pfUNC)
  2193. {
  2194. WCHAR const *pwc;
  2195. *pfUNC = FALSE;
  2196. pwc = pwsz;
  2197. if (L'\\' != *pwc)
  2198. {
  2199. pwc++;
  2200. }
  2201. if (L'\\' == pwc[0] || L'\\' == pwc[1])
  2202. {
  2203. pwc = wcschr(&pwc[2], L'\\');
  2204. if (NULL != pwc &&
  2205. iswalpha(pwc[1]) &&
  2206. L'$' == pwc[2] &&
  2207. L'\\' == pwc[3])
  2208. {
  2209. pwsz = &pwc[1];
  2210. *pfUNC = TRUE;
  2211. }
  2212. }
  2213. return(pwsz);
  2214. }
  2215. ULONG
  2216. myLocalPathwcslen(
  2217. IN WCHAR const *pwsz)
  2218. {
  2219. BOOL fUNC;
  2220. return(wcslen(LocalStart(pwsz, &fUNC)));
  2221. }
  2222. VOID
  2223. myLocalPathwcscpy(
  2224. OUT WCHAR *pwszOut,
  2225. IN WCHAR const *pwszIn)
  2226. {
  2227. BOOL fUNC;
  2228. wcscpy(pwszOut, LocalStart(pwszIn, &fUNC));
  2229. if (fUNC)
  2230. {
  2231. CSASSERT(L'$' == pwszOut[1]);
  2232. pwszOut[1] = L':';
  2233. }
  2234. }
  2235. HRESULT
  2236. myConvertUNCPathToLocal(
  2237. IN WCHAR const *pwszUNCPath,
  2238. OUT WCHAR **ppwszLocalPath)
  2239. {
  2240. HRESULT hr;
  2241. DWORD cwc;
  2242. WCHAR *pwszLocalPath;
  2243. CSASSERT(NULL != pwszUNCPath);
  2244. CSASSERT(NULL != ppwszLocalPath);
  2245. *ppwszLocalPath = NULL;
  2246. if (L'\\' != pwszUNCPath[0] || L'\\' != pwszUNCPath[1])
  2247. {
  2248. hr = E_INVALIDARG;
  2249. _JumpError(hr, error, "bad parm");
  2250. }
  2251. cwc = myLocalPathwcslen(pwszUNCPath) + 1;
  2252. pwszLocalPath = (WCHAR *) LocalAlloc(LMEM_FIXED, cwc * sizeof(WCHAR));
  2253. if (NULL == pwszLocalPath)
  2254. {
  2255. hr = E_OUTOFMEMORY;
  2256. _JumpError(hr, error, "LocalAlloc");
  2257. }
  2258. myLocalPathwcscpy(pwszLocalPath, pwszUNCPath);
  2259. *ppwszLocalPath = pwszLocalPath;
  2260. hr = S_OK;
  2261. error:
  2262. return(hr);
  2263. }
  2264. //+-------------------------------------------------------------------------
  2265. // Description: create any number of directories in one call
  2266. //--------------------------------------------------------------------------
  2267. HRESULT
  2268. myCreateNestedDirectories(
  2269. WCHAR const *pwszDirectory)
  2270. {
  2271. HRESULT hr;
  2272. WCHAR rgszDir[MAX_PATH]; // static buffer
  2273. WCHAR *pszNext = const_cast<WCHAR*>(pwszDirectory); // point to end of current directory
  2274. // skip "X:\"
  2275. if ((pszNext[1] == L':') &&
  2276. (pszNext[2] == L'\\'))
  2277. pszNext += 3;
  2278. while (pszNext) // incr past
  2279. {
  2280. DWORD ch;
  2281. // find the next occurence of '\'
  2282. pszNext = wcschr(pszNext, L'\\');
  2283. if (pszNext == NULL)
  2284. {
  2285. // last directory: copy everything
  2286. wcscpy(rgszDir, pwszDirectory);
  2287. }
  2288. else
  2289. {
  2290. // else copy up to Next ptr
  2291. ch = SAFE_SUBTRACT_POINTERS(pszNext, pwszDirectory);
  2292. if (0 != ch)
  2293. {
  2294. CopyMemory(rgszDir, pwszDirectory, ch*sizeof(WCHAR));
  2295. // zero-term
  2296. rgszDir[ch] = L'\0';
  2297. // incr past '\\'
  2298. pszNext++;
  2299. }
  2300. else
  2301. {
  2302. //if ch = 0, means the first char is \, skip CreateDirectory
  2303. pszNext++; //must shift to next char to get out of loop
  2304. continue;
  2305. }
  2306. }
  2307. // UNDONE: PeteSk - add in directory security
  2308. if (!CreateDirectory(rgszDir, NULL))
  2309. {
  2310. hr = myHLastError();
  2311. if (HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS) != hr)
  2312. {
  2313. // something must be wrong with the path
  2314. _JumpError(hr, error, "CreateDirectory");
  2315. }
  2316. }
  2317. }
  2318. hr = S_OK;
  2319. error:
  2320. return hr;
  2321. }
  2322. HRESULT
  2323. myUncanonicalizeURLParm(
  2324. IN WCHAR const *pwszParmIn,
  2325. OUT WCHAR **ppwszParmOut)
  2326. {
  2327. HRESULT hr;
  2328. DWORD cwc;
  2329. WCHAR *pwszCanon = NULL;
  2330. WCHAR *pwszUncanon = NULL;
  2331. static const WCHAR s_wszLdap[] = L"ldap:///";
  2332. *ppwszParmOut = NULL;
  2333. cwc = WSZARRAYSIZE(s_wszLdap) + wcslen(pwszParmIn);
  2334. pwszCanon = (WCHAR *) LocalAlloc(LMEM_FIXED, (cwc + 1) * sizeof(WCHAR));
  2335. if (NULL == pwszCanon)
  2336. {
  2337. hr = E_OUTOFMEMORY;
  2338. _JumpError(hr, error, "LocalAlloc");
  2339. }
  2340. wcscpy(pwszCanon, s_wszLdap);
  2341. wcscat(&pwszCanon[WSZARRAYSIZE(s_wszLdap)], pwszParmIn);
  2342. hr = myInternetUncanonicalizeURL(pwszCanon, &pwszUncanon);
  2343. _JumpIfError(hr, error, "myInternetUncanonicalizeURL");
  2344. hr = myDupString(&pwszUncanon[WSZARRAYSIZE(s_wszLdap)], ppwszParmOut);
  2345. _JumpIfError(hr, error, "myDupString");
  2346. error:
  2347. if (NULL != pwszCanon)
  2348. {
  2349. LocalFree(pwszCanon);
  2350. }
  2351. if (NULL != pwszUncanon)
  2352. {
  2353. LocalFree(pwszUncanon);
  2354. }
  2355. return(hr);
  2356. }
  2357. // myFormatCertsrvStringArray FormatMessage arguments:
  2358. //
  2359. // %1 -- Machine full DNS name: pwszServerName_p1_2;
  2360. //
  2361. // %2 -- Machine short name: first DNS component of pwszServerName_p1_2
  2362. //
  2363. // %3 -- Sanitized CA name: pwszSanitizedName_p3_7
  2364. //
  2365. // %4 -- Cert Filename Suffix: L"" if 0 == iCert_p4; else L"(%u)"
  2366. //
  2367. // %5 -- DS DN path to Domain root: pwszDomainDN_p5
  2368. //
  2369. // %6 -- DS DN path to Configuration container: pwszConfigDN_p6
  2370. //
  2371. // %7 -- Sanitized CA name, truncated and hash suffix added if too long:
  2372. // pwszSanitizedName_p3_7
  2373. //
  2374. // %8 -- CRL Filename/Key Name Suffix: L"" if 0 == iCRL_p8; else L"(%u)"
  2375. //
  2376. // %9 -- CRL Filename Suffix: L"" if !fDeltaCRL_p9; else L"+"
  2377. //
  2378. // %10 -- DS CRL attribute: L"" if !fDSAttrib_p10_11; depends on fDeltaCRL_p9
  2379. //
  2380. // %11 -- DS CA Cert attribute: L"" if !fDSAttrib_p10_11
  2381. //
  2382. // %12 -- DS user cert attribute
  2383. //
  2384. // %13 -- DS KRA cert attribute
  2385. //
  2386. // %14 -- DS cross cert pair attribute
  2387. HRESULT
  2388. myFormatCertsrvStringArray(
  2389. IN BOOL fURL,
  2390. IN LPCWSTR pwszServerName_p1_2,
  2391. IN LPCWSTR pwszSanitizedName_p3_7,
  2392. IN DWORD iCert_p4,
  2393. IN LPCWSTR pwszDomainDN_p5,
  2394. IN LPCWSTR pwszConfigDN_p6,
  2395. IN DWORD iCRL_p8,
  2396. IN BOOL fDeltaCRL_p9,
  2397. IN BOOL fDSAttrib_p10_11,
  2398. IN DWORD cStrings,
  2399. IN LPCWSTR *apwszStringsIn,
  2400. OUT LPWSTR *apwszStringsOut)
  2401. {
  2402. HRESULT hr = S_OK;
  2403. LPCWSTR apwszInsertionArray[100]; // 100 'cause this is the max number of insertion numbers allowed by FormatMessage
  2404. LPWSTR pwszCurrent = NULL;
  2405. BSTR strShortMachineName = NULL;
  2406. DWORD i;
  2407. WCHAR *pwszSanitizedDSName = NULL;
  2408. WCHAR wszCertSuffix[cwcFILENAMESUFFIXMAX];
  2409. WCHAR wszCRLSuffix[cwcFILENAMESUFFIXMAX];
  2410. WCHAR wszDeltaCRLSuffix[cwcFILENAMESUFFIXMAX];
  2411. WCHAR const *pwszT;
  2412. ZeroMemory(apwszStringsOut, cStrings * sizeof(apwszStringsOut[0]));
  2413. ZeroMemory(apwszInsertionArray, sizeof(apwszInsertionArray));
  2414. // Format the template into a real name
  2415. // Initialize the insertion string array.
  2416. //+================================================
  2417. // Machine DNS name (%1)
  2418. CSASSERT(L'1' == wszFCSAPARM_SERVERDNSNAME[1]);
  2419. apwszInsertionArray[1 - 1] = pwszServerName_p1_2;
  2420. //+================================================
  2421. // Short Machine Name (%2)
  2422. CSASSERT(L'2' == wszFCSAPARM_SERVERSHORTNAME[1]);
  2423. strShortMachineName = SysAllocString(pwszServerName_p1_2);
  2424. if (strShortMachineName == NULL)
  2425. {
  2426. hr = E_OUTOFMEMORY;
  2427. _JumpIfError(hr, error, "SysAllocString");
  2428. }
  2429. pwszCurrent = wcschr(strShortMachineName, L'.');
  2430. if (NULL != pwszCurrent)
  2431. {
  2432. *pwszCurrent = 0;
  2433. }
  2434. apwszInsertionArray[2 - 1] = strShortMachineName;
  2435. //+================================================
  2436. // sanitized name (%3)
  2437. CSASSERT(L'3' == wszFCSAPARM_SANITIZEDCANAME[1]);
  2438. apwszInsertionArray[3 - 1] = pwszSanitizedName_p3_7;
  2439. //+================================================
  2440. // Cert filename suffix (%4)
  2441. CSASSERT(L'4' == wszFCSAPARM_CERTFILENAMESUFFIX[1]);
  2442. wszCertSuffix[0] = L'\0';
  2443. if (0 != iCert_p4)
  2444. {
  2445. wsprintf(wszCertSuffix, L"(%u)", iCert_p4);
  2446. }
  2447. apwszInsertionArray[4 - 1] = wszCertSuffix;
  2448. //+================================================
  2449. // Domain DN (%5)
  2450. CSASSERT(L'5' == wszFCSAPARM_DOMAINDN[1]);
  2451. apwszInsertionArray[5 - 1] = pwszDomainDN_p5;
  2452. //+================================================
  2453. // Config DN (%6)
  2454. CSASSERT(L'6' == wszFCSAPARM_CONFIGDN[1]);
  2455. apwszInsertionArray[6 - 1] = pwszConfigDN_p6;
  2456. // Don't pass pwszSanitizedName_p3_7 to SysAllocStringLen with the extended
  2457. // length to avoid faulting past end of pwszSanitizedName_p3_7.
  2458. //+================================================
  2459. // Sanitized Short Name (%7)
  2460. CSASSERT(L'7' == wszFCSAPARM_SANITIZEDCANAMEHASH[1]);
  2461. hr = mySanitizedNameToDSName(pwszSanitizedName_p3_7, &pwszSanitizedDSName);
  2462. _JumpIfError(hr, error, "mySanitizedNameToDSName");
  2463. apwszInsertionArray[7 - 1] = pwszSanitizedDSName;
  2464. //+================================================
  2465. // CRL filename suffix (%8)
  2466. CSASSERT(L'8' == wszFCSAPARM_CRLFILENAMESUFFIX[1]);
  2467. wszCRLSuffix[0] = L'\0';
  2468. if (0 != iCRL_p8)
  2469. {
  2470. wsprintf(wszCRLSuffix, L"(%u)", iCRL_p8);
  2471. }
  2472. apwszInsertionArray[8 - 1] = wszCRLSuffix;
  2473. //+================================================
  2474. // Delta CRL filename suffix (%9)
  2475. CSASSERT(L'9' == wszFCSAPARM_CRLDELTAFILENAMESUFFIX[1]);
  2476. wszDeltaCRLSuffix[0] = L'\0';
  2477. if (fDeltaCRL_p9)
  2478. {
  2479. wcscpy(wszDeltaCRLSuffix, L"+");
  2480. }
  2481. apwszInsertionArray[9 - 1] = wszDeltaCRLSuffix;
  2482. //+================================================
  2483. // CRL attribute (%10)
  2484. CSASSERT(L'1' == wszFCSAPARM_DSCRLATTRIBUTE[1]);
  2485. CSASSERT(L'0' == wszFCSAPARM_DSCRLATTRIBUTE[2]);
  2486. pwszT = L"";
  2487. if (fDSAttrib_p10_11)
  2488. {
  2489. pwszT = fDeltaCRL_p9?
  2490. wszDSSEARCHDELTACRLATTRIBUTE :
  2491. wszDSSEARCHBASECRLATTRIBUTE;
  2492. }
  2493. apwszInsertionArray[10 - 1] = pwszT;
  2494. //+================================================
  2495. // CA cert attribute (%11)
  2496. CSASSERT(L'1' == wszFCSAPARM_DSCACERTATTRIBUTE[1]);
  2497. CSASSERT(L'1' == wszFCSAPARM_DSCACERTATTRIBUTE[2]);
  2498. pwszT = L"";
  2499. if (fDSAttrib_p10_11)
  2500. {
  2501. pwszT = wszDSSEARCHCACERTATTRIBUTE;
  2502. }
  2503. apwszInsertionArray[11 - 1] = pwszT;
  2504. //+================================================
  2505. // User cert attribute (%12)
  2506. CSASSERT(L'1' == wszFCSAPARM_DSUSERCERTATTRIBUTE[1]);
  2507. CSASSERT(L'2' == wszFCSAPARM_DSUSERCERTATTRIBUTE[2]);
  2508. pwszT = L"";
  2509. if (fDSAttrib_p10_11)
  2510. {
  2511. pwszT = wszDSSEARCHUSERCERTATTRIBUTE;
  2512. }
  2513. apwszInsertionArray[12 - 1] = pwszT;
  2514. //+================================================
  2515. // KRA cert attribute (%13)
  2516. CSASSERT(L'1' == wszFCSAPARM_DSKRACERTATTRIBUTE[1]);
  2517. CSASSERT(L'3' == wszFCSAPARM_DSKRACERTATTRIBUTE[2]);
  2518. pwszT = L"";
  2519. if (fDSAttrib_p10_11)
  2520. {
  2521. pwszT = wszDSSEARCHKRACERTATTRIBUTE;
  2522. }
  2523. apwszInsertionArray[13 - 1] = pwszT;
  2524. //+================================================
  2525. // Cross cert pair attribute (%14)
  2526. CSASSERT(L'1' == wszFCSAPARM_DSCROSSCERTPAIRATTRIBUTE[1]);
  2527. CSASSERT(L'4' == wszFCSAPARM_DSCROSSCERTPAIRATTRIBUTE[2]);
  2528. pwszT = L"";
  2529. if (fDSAttrib_p10_11)
  2530. {
  2531. pwszT = wszDSSEARCHCROSSCERTPAIRATTRIBUTE;
  2532. }
  2533. apwszInsertionArray[14 - 1] = pwszT;
  2534. //+================================================
  2535. // Now format the strings...
  2536. for (i = 0; i < cStrings; i++)
  2537. {
  2538. if (0 == FormatMessage(
  2539. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2540. FORMAT_MESSAGE_FROM_STRING |
  2541. FORMAT_MESSAGE_ARGUMENT_ARRAY,
  2542. (VOID *) apwszStringsIn[i],
  2543. 0, // dwMessageID
  2544. 0, // dwLanguageID
  2545. (LPWSTR) &apwszStringsOut[i],
  2546. wcslen(apwszStringsIn[i]),
  2547. (va_list *) apwszInsertionArray))
  2548. {
  2549. hr = myHLastError();
  2550. _JumpError(hr, error, "FormatMessage");
  2551. }
  2552. if (fURL)
  2553. {
  2554. WCHAR *pwsz;
  2555. hr = myInternetCanonicalizeUrl(apwszStringsOut[i], &pwsz);
  2556. _JumpIfError(hr, error, "myInternetCanonicalizeUrl");
  2557. LocalFree(apwszStringsOut[i]);
  2558. apwszStringsOut[i] = pwsz;
  2559. }
  2560. }
  2561. error:
  2562. if (S_OK != hr)
  2563. {
  2564. for (i = 0; i < cStrings; i++)
  2565. {
  2566. if (NULL != apwszStringsOut[i])
  2567. {
  2568. LocalFree(apwszStringsOut[i]);
  2569. apwszStringsOut[i] = NULL;
  2570. }
  2571. }
  2572. }
  2573. if (NULL != strShortMachineName)
  2574. {
  2575. SysFreeString(strShortMachineName);
  2576. }
  2577. if (NULL != pwszSanitizedDSName)
  2578. {
  2579. LocalFree(pwszSanitizedDSName);
  2580. }
  2581. return (hr);
  2582. }
  2583. HRESULT
  2584. myAllocIndexedName(
  2585. IN WCHAR const *pwszName,
  2586. IN DWORD Index,
  2587. OUT WCHAR **ppwszIndexedName)
  2588. {
  2589. HRESULT hr;
  2590. WCHAR wszIndex[1 + 10 + 1 + 1]; // L"(%u)"
  2591. WCHAR *pwszIndexedName;
  2592. *ppwszIndexedName = NULL;
  2593. wszIndex[0] = L'\0';
  2594. if (0 != Index)
  2595. {
  2596. wsprintf(wszIndex, L"(%u)", Index);
  2597. }
  2598. pwszIndexedName = (WCHAR *) LocalAlloc(
  2599. LMEM_FIXED,
  2600. (wcslen(pwszName) + wcslen(wszIndex) + 1) * sizeof(WCHAR));
  2601. if (NULL == pwszIndexedName)
  2602. {
  2603. hr = E_OUTOFMEMORY;
  2604. _JumpError(hr, error, "LocalAlloc");
  2605. }
  2606. wcscpy(pwszIndexedName, pwszName);
  2607. wcscat(pwszIndexedName, wszIndex);
  2608. *ppwszIndexedName = pwszIndexedName;
  2609. hr = S_OK;
  2610. error:
  2611. return(hr);
  2612. }
  2613. int
  2614. myWtoI(
  2615. IN WCHAR const *string,
  2616. OUT BOOL *pfValid)
  2617. {
  2618. HRESULT hr;
  2619. WCHAR wszBuf[16];
  2620. WCHAR *pwszT = wszBuf;
  2621. int cTmp = ARRAYSIZE(wszBuf);
  2622. int i = 0;
  2623. WCHAR const *pwsz;
  2624. BOOL fSawDigit = FALSE;
  2625. CSASSERT(NULL != pfValid);
  2626. *pfValid = FALSE;
  2627. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, pwszT, cTmp);
  2628. if (cTmp == 0)
  2629. {
  2630. hr = myHLastError();
  2631. if (HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER) == hr)
  2632. {
  2633. hr = S_OK;
  2634. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, NULL, 0);
  2635. pwszT = (WCHAR *) LocalAlloc(LMEM_FIXED, cTmp * sizeof(WCHAR));
  2636. if (NULL == pwszT)
  2637. {
  2638. hr = E_OUTOFMEMORY;
  2639. _JumpError(hr, error, "LocalAlloc");
  2640. }
  2641. cTmp = FoldString(MAP_FOLDDIGITS, string, -1, pwszT, cTmp);
  2642. if (cTmp == 0)
  2643. {
  2644. hr = myHLastError();
  2645. }
  2646. }
  2647. _JumpIfError(hr, error, "FoldString");
  2648. }
  2649. pwsz = pwszT;
  2650. while (iswspace(*pwsz))
  2651. {
  2652. pwsz++;
  2653. }
  2654. if (L'0' == *pwsz && (L'x' == pwsz[1] || L'X' == pwsz[1]))
  2655. {
  2656. pwsz += 2;
  2657. while (iswxdigit(*pwsz))
  2658. {
  2659. i <<= 4;
  2660. if (iswdigit(*pwsz))
  2661. {
  2662. i |= *pwsz - L'0';
  2663. }
  2664. else if (L'A' <= *pwsz && L'F' >= *pwsz)
  2665. {
  2666. i |= *pwsz - L'A' + 10;
  2667. }
  2668. else
  2669. {
  2670. i |= *pwsz - L'a' + 10;
  2671. }
  2672. fSawDigit = TRUE;
  2673. pwsz++;
  2674. }
  2675. }
  2676. else
  2677. {
  2678. while (iswdigit(*pwsz))
  2679. {
  2680. fSawDigit = TRUE;
  2681. pwsz++;
  2682. }
  2683. i = _wtoi(pwszT);
  2684. }
  2685. while (iswspace(*pwsz))
  2686. {
  2687. pwsz++;
  2688. }
  2689. if (L'\0' == *pwsz)
  2690. {
  2691. *pfValid = fSawDigit;
  2692. }
  2693. error:
  2694. if (NULL != pwszT && pwszT != wszBuf)
  2695. {
  2696. LocalFree(pwszT);
  2697. }
  2698. return(i);
  2699. }
  2700. HRESULT
  2701. myGetEnvString(
  2702. OUT WCHAR **ppwszOut,
  2703. IN WCHAR const *pwszVariable)
  2704. {
  2705. HRESULT hr;
  2706. WCHAR awcBuf[MAX_PATH];
  2707. DWORD len;
  2708. len = GetEnvironmentVariable(pwszVariable, awcBuf, ARRAYSIZE(awcBuf));
  2709. if (0 == len)
  2710. {
  2711. hr = myHLastError();
  2712. _JumpErrorStr2(
  2713. hr,
  2714. error,
  2715. "GetEnvironmentVariable",
  2716. pwszVariable,
  2717. HRESULT_FROM_WIN32(ERROR_ENVVAR_NOT_FOUND));
  2718. }
  2719. if (MAX_PATH <= len)
  2720. {
  2721. hr = HRESULT_FROM_WIN32(ERROR_BUFFER_OVERFLOW);
  2722. _JumpError(hr, error, "GetEnvironmentVariable");
  2723. }
  2724. *ppwszOut = (WCHAR *) LocalAlloc(
  2725. LMEM_FIXED,
  2726. (wcslen(awcBuf) + 1) * sizeof(WCHAR));
  2727. if (NULL == *ppwszOut)
  2728. {
  2729. hr = E_OUTOFMEMORY;
  2730. _JumpError(hr, error, "LocalAlloc");
  2731. }
  2732. wcscpy(*ppwszOut, awcBuf);
  2733. hr = S_OK;
  2734. error:
  2735. return(hr);
  2736. }
  2737. BOOL
  2738. IsValidAttributeChar(
  2739. IN WCHAR wc)
  2740. {
  2741. BOOL fOk = TRUE;
  2742. switch (wc)
  2743. {
  2744. case L'-':
  2745. fOk = FALSE;
  2746. break;
  2747. default:
  2748. if (iswspace(wc))
  2749. {
  2750. fOk = FALSE;
  2751. }
  2752. break;
  2753. }
  2754. return(fOk);
  2755. }
  2756. // myParseNextAttribute -- destructively parse a name, value attribute pair.
  2757. // Allow CR and/or LF delimiters -- MAC web pages seem to convert LFs into CRs.
  2758. HRESULT
  2759. myParseNextAttribute(
  2760. IN OUT WCHAR **ppwszBuf,
  2761. IN BOOL fURL, // Use = and & instead of : and \r\n
  2762. OUT WCHAR const **ppwszName,
  2763. OUT WCHAR const **ppwszValue)
  2764. {
  2765. HRESULT hr;
  2766. WCHAR *pwszBuf = *ppwszBuf;
  2767. WCHAR *pwcToken;
  2768. WCHAR *pwc;
  2769. WCHAR *pwcDst;
  2770. WCHAR wcSep = fURL? L'=' : L':';
  2771. WCHAR *wszTerm = fURL? L"&" : L"\r\n";
  2772. while (TRUE)
  2773. {
  2774. // Find the beginning of the next token
  2775. while (iswspace(*pwszBuf))
  2776. {
  2777. pwszBuf++;
  2778. }
  2779. pwcToken = pwszBuf;
  2780. pwszBuf = wcschr(pwszBuf, wcSep);
  2781. if (NULL == pwszBuf)
  2782. {
  2783. hr = S_FALSE;
  2784. goto error;
  2785. }
  2786. // If there's a wszTerm char before the next wcSep char, start over.
  2787. pwc = &pwcToken[wcscspn(pwcToken, wszTerm)];
  2788. if (pwc < pwszBuf)
  2789. {
  2790. pwszBuf = pwc + 1;
  2791. continue;
  2792. }
  2793. for (pwc = pwcDst = pwcToken; pwc < pwszBuf; pwc++)
  2794. {
  2795. if (IsValidAttributeChar(*pwc))
  2796. {
  2797. *pwcDst++ = *pwc;
  2798. }
  2799. }
  2800. pwszBuf++; // skip past the wcSep before it gets stomped
  2801. *pwcDst = L'\0'; // may stomp the wcSep separator
  2802. *ppwszName = pwcToken;
  2803. // Find beginning of Value string
  2804. while (NULL == wcschr(wszTerm, *pwszBuf) && iswspace(*pwszBuf))
  2805. {
  2806. pwszBuf++;
  2807. }
  2808. pwcToken = pwszBuf;
  2809. // find end of Value string
  2810. pwc = &pwcToken[wcscspn(pwcToken, wszTerm)];
  2811. pwszBuf = pwc;
  2812. if (L'\0' != *pwszBuf)
  2813. {
  2814. // for case when last Value *is* terminated by a wszTerm char:
  2815. *pwszBuf++ = L'\0';
  2816. }
  2817. // trim trailing whitespace from Value string
  2818. while (--pwc >= pwcToken && iswspace(*pwc))
  2819. {
  2820. *pwc = L'\0';
  2821. }
  2822. if (L'\0' == **ppwszName || L'\0' == *pwcToken)
  2823. {
  2824. continue;
  2825. }
  2826. *ppwszValue = pwcToken;
  2827. break;
  2828. }
  2829. hr = S_OK;
  2830. error:
  2831. *ppwszBuf = pwszBuf;
  2832. return(hr);
  2833. }
  2834. HRESULT
  2835. myBuildOSVersionAttribute(
  2836. OUT BYTE **ppbVersion,
  2837. OUT DWORD *pcbVersion)
  2838. {
  2839. HRESULT hr;
  2840. DWORD i;
  2841. OSVERSIONINFO osvInfo;
  2842. CERT_NAME_VALUE cnvOSVer;
  2843. #define cwcVERSIONMAX 128
  2844. WCHAR wszVersion[12 * 4 + cwcVERSIONMAX];
  2845. *ppbVersion = NULL;
  2846. ZeroMemory(&osvInfo, sizeof(osvInfo));
  2847. // get the OSVersion
  2848. osvInfo.dwOSVersionInfoSize = sizeof(osvInfo);
  2849. if (!GetVersionEx(&osvInfo))
  2850. {
  2851. hr = myHLastError();
  2852. _JumpError(hr, error, "GetVersionEx");
  2853. }
  2854. for (i = 0; ; i++)
  2855. {
  2856. swprintf(
  2857. wszVersion,
  2858. 0 == i? L"%d.%d.%d.%d.%.*ws" : L"%d.%d.%d.%d",
  2859. osvInfo.dwMajorVersion,
  2860. osvInfo.dwMinorVersion,
  2861. osvInfo.dwBuildNumber,
  2862. osvInfo.dwPlatformId,
  2863. cwcVERSIONMAX,
  2864. osvInfo.szCSDVersion);
  2865. CSASSERT(ARRAYSIZE(wszVersion) > wcslen(wszVersion));
  2866. cnvOSVer.dwValueType = CERT_RDN_IA5_STRING;
  2867. cnvOSVer.Value.pbData = (BYTE *) wszVersion;
  2868. cnvOSVer.Value.cbData = 0;
  2869. if (!myEncodeObject(
  2870. X509_ASN_ENCODING,
  2871. X509_UNICODE_ANY_STRING,
  2872. &cnvOSVer,
  2873. 0,
  2874. CERTLIB_USE_LOCALALLOC,
  2875. ppbVersion,
  2876. pcbVersion))
  2877. {
  2878. hr = myHLastError();
  2879. _PrintError(hr, "myEncodeObject");
  2880. if (0 == i)
  2881. {
  2882. continue;
  2883. }
  2884. goto error;
  2885. }
  2886. break;
  2887. }
  2888. hr = S_OK;
  2889. error:
  2890. return(hr);
  2891. }
  2892. HRESULT
  2893. myBuildCertTypeExtension(
  2894. IN WCHAR const *pwszCertType,
  2895. OUT CERT_EXTENSION *pExt)
  2896. {
  2897. HRESULT hr;
  2898. CERT_TEMPLATE_EXT Template;
  2899. CERT_NAME_VALUE NameValue;
  2900. LPCSTR pszStructType;
  2901. char *pszObjId = NULL;
  2902. VOID *pv;
  2903. char *pszObjIdExt;
  2904. if (!ConvertWszToSz(&pszObjId, pwszCertType, -1))
  2905. {
  2906. hr = E_OUTOFMEMORY;
  2907. _JumpError(hr, error, "ConvertWszToSz");
  2908. }
  2909. hr = myVerifyObjIdA(pszObjId);
  2910. if (S_OK == hr)
  2911. {
  2912. ZeroMemory(&Template, sizeof(Template));
  2913. Template.pszObjId = pszObjId;
  2914. //Template.dwMajorVersion = 0;
  2915. //Template.fMinorVersion = FALSE; // TRUE for a minor version
  2916. //Template.dwMinorVersion = 0;
  2917. pszStructType = X509_CERTIFICATE_TEMPLATE;
  2918. pv = &Template;
  2919. pszObjIdExt = szOID_CERTIFICATE_TEMPLATE;
  2920. }
  2921. else
  2922. {
  2923. NameValue.dwValueType = CERT_RDN_UNICODE_STRING;
  2924. NameValue.Value.pbData = (BYTE *) pwszCertType;
  2925. NameValue.Value.cbData = 0;
  2926. pszStructType = X509_UNICODE_ANY_STRING;
  2927. pv = &NameValue;
  2928. pszObjIdExt = szOID_ENROLL_CERTTYPE_EXTENSION;
  2929. }
  2930. if (!myEncodeObject(
  2931. X509_ASN_ENCODING,
  2932. pszStructType,
  2933. pv,
  2934. 0,
  2935. CERTLIB_USE_LOCALALLOC,
  2936. &pExt->Value.pbData,
  2937. &pExt->Value.cbData))
  2938. {
  2939. hr = myHLastError();
  2940. _JumpError(hr, error, "myEncodeObject");
  2941. }
  2942. pExt->pszObjId = pszObjIdExt;
  2943. pExt->fCritical = FALSE;
  2944. hr = S_OK;
  2945. error:
  2946. if (NULL != pszObjId)
  2947. {
  2948. LocalFree(pszObjId);
  2949. }
  2950. return(hr);
  2951. }
  2952. BOOL
  2953. IsWhistler(VOID)
  2954. {
  2955. HRESULT hr;
  2956. OSVERSIONINFO ovi;
  2957. static BOOL s_fDone = FALSE;
  2958. static BOOL s_fWhistler = FALSE;
  2959. if (!s_fDone)
  2960. {
  2961. s_fDone = TRUE;
  2962. // get and confirm platform info
  2963. ovi.dwOSVersionInfoSize = sizeof(ovi);
  2964. if (!GetVersionEx(&ovi))
  2965. {
  2966. hr = myHLastError();
  2967. _JumpError(hr, error, "GetVersionEx");
  2968. }
  2969. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  2970. {
  2971. hr = ERROR_CANCELLED;
  2972. _JumpError(hr, error, "Not a supported OS");
  2973. }
  2974. if (5 < ovi.dwMajorVersion ||
  2975. (5 <= ovi.dwMajorVersion && 0 < ovi.dwMinorVersion))
  2976. {
  2977. s_fWhistler = TRUE;
  2978. }
  2979. }
  2980. error:
  2981. return(s_fWhistler);
  2982. }
  2983. static BOOL
  2984. GetFlags(
  2985. OUT DWORD *pdw)
  2986. {
  2987. HRESULT hr = S_FALSE;
  2988. *pdw = 0;
  2989. #if defined(_ALLOW_GET_FLAGS_)
  2990. hr = myGetCertRegDWValue(NULL, NULL, NULL, L"SFlags", pdw);
  2991. if (S_OK == hr)
  2992. {
  2993. DBGPRINT((
  2994. DBG_SS_CERTLIB,
  2995. "CertSrv\\Configuration\\SFlags override: %u\n",
  2996. *pdw));
  2997. }
  2998. #endif
  2999. return(S_OK == hr);
  3000. }
  3001. BOOL
  3002. FIsAdvancedServer(VOID)
  3003. {
  3004. HRESULT hr;
  3005. OSVERSIONINFOEX ovi;
  3006. static BOOL s_fDone = FALSE;
  3007. static BOOL s_fIsAdvSvr = FALSE;
  3008. if (!s_fDone)
  3009. {
  3010. s_fDone = TRUE;
  3011. // get and confirm platform info
  3012. ovi.dwOSVersionInfoSize = sizeof(ovi);
  3013. if (!GetVersionEx((OSVERSIONINFO *) &ovi))
  3014. {
  3015. hr = myHLastError();
  3016. _JumpError(hr, error, "GetVersionEx");
  3017. }
  3018. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  3019. {
  3020. hr = ERROR_CANCELLED;
  3021. _JumpError(hr, error, "Not a supported OS");
  3022. }
  3023. // if server or DC, if DTC or ADS bits are set, return TRUE
  3024. s_fIsAdvSvr =
  3025. (ovi.wProductType == VER_NT_SERVER ||
  3026. ovi.wProductType == VER_NT_DOMAIN_CONTROLLER) &&
  3027. (ovi.wSuiteMask & VER_SUITE_DATACENTER ||
  3028. ovi.wSuiteMask & VER_SUITE_ENTERPRISE);
  3029. {
  3030. DWORD dw;
  3031. if (GetFlags(&dw))
  3032. {
  3033. s_fIsAdvSvr = dw;
  3034. }
  3035. }
  3036. }
  3037. error:
  3038. return(s_fIsAdvSvr);
  3039. }
  3040. BOOL
  3041. FIsServer(VOID)
  3042. {
  3043. HRESULT hr;
  3044. OSVERSIONINFOEX ovi;
  3045. static BOOL s_fDone = FALSE;
  3046. static BOOL s_fIsSvr = FALSE;
  3047. if (!s_fDone)
  3048. {
  3049. s_fDone = TRUE;
  3050. // get and confirm platform info
  3051. ovi.dwOSVersionInfoSize = sizeof(ovi);
  3052. if (!GetVersionEx((OSVERSIONINFO *) &ovi))
  3053. {
  3054. hr = myHLastError();
  3055. _JumpError(hr, error, "GetVersionEx");
  3056. }
  3057. if (VER_PLATFORM_WIN32_NT != ovi.dwPlatformId)
  3058. {
  3059. hr = ERROR_CANCELLED;
  3060. _JumpError(hr, error, "Not a supported OS");
  3061. }
  3062. // if server or DC, if DTC or ADS bits are set, return TRUE
  3063. s_fIsSvr =
  3064. (ovi.wProductType == VER_NT_SERVER ||
  3065. ovi.wProductType == VER_NT_DOMAIN_CONTROLLER) &&
  3066. 0 == ((VER_SUITE_PERSONAL | VER_SUITE_BLADE) & ovi.wSuiteMask);
  3067. if (!s_fIsSvr && VER_NT_WORKSTATION == ovi.wProductType)
  3068. {
  3069. DWORD dw;
  3070. if (GetFlags(&dw))
  3071. {
  3072. s_fIsSvr = TRUE;
  3073. }
  3074. }
  3075. }
  3076. error:
  3077. return(s_fIsSvr);
  3078. }
  3079. HRESULT
  3080. myAddLogSourceToRegistry(
  3081. IN LPWSTR pwszMsgDLL,
  3082. IN LPWSTR pwszApp)
  3083. {
  3084. HRESULT hr=S_OK;
  3085. DWORD dwData=0;
  3086. WCHAR const *pwszRegPath = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\";
  3087. WCHAR NameBuf[MAX_PATH];
  3088. HKEY hkey = NULL;
  3089. CSASSERT((wcslen(pwszRegPath) + wcslen(pwszApp) + 1) < MAX_PATH);
  3090. wcscpy(NameBuf, pwszRegPath);
  3091. wcscat(NameBuf, pwszApp);
  3092. // Create a new key for our application
  3093. hr = RegOpenKey(HKEY_LOCAL_MACHINE, NameBuf, &hkey);
  3094. if (S_OK != hr)
  3095. {
  3096. hr = RegCreateKey(HKEY_LOCAL_MACHINE, NameBuf, &hkey);
  3097. _JumpIfError(hr, error, "RegCreateKey");
  3098. }
  3099. // Add the Event-ID message-file name to the subkey
  3100. hr = RegSetValueEx(
  3101. hkey,
  3102. L"EventMessageFile",
  3103. 0,
  3104. REG_EXPAND_SZ,
  3105. (const BYTE *) pwszMsgDLL,
  3106. (wcslen(pwszMsgDLL) + 1) * sizeof(WCHAR));
  3107. _JumpIfError(hr, error, "RegSetValueEx");
  3108. // Set the supported types flags and add it to the subkey
  3109. dwData = EVENTLOG_ERROR_TYPE |
  3110. EVENTLOG_WARNING_TYPE |
  3111. EVENTLOG_INFORMATION_TYPE;
  3112. hr = RegSetValueEx(
  3113. hkey,
  3114. L"TypesSupported",
  3115. 0,
  3116. REG_DWORD,
  3117. (LPBYTE) &dwData,
  3118. sizeof(DWORD));
  3119. _JumpIfError(hr, error, "RegSetValueEx");
  3120. error:
  3121. if (NULL != hkey)
  3122. {
  3123. RegCloseKey(hkey);
  3124. }
  3125. return(myHError(hr));
  3126. }
  3127. HRESULT
  3128. myDupStringA(
  3129. IN CHAR const *pszIn,
  3130. IN CHAR **ppszOut)
  3131. {
  3132. DWORD cb;
  3133. HRESULT hr;
  3134. cb = (strlen(pszIn) + 1) * sizeof(CHAR);
  3135. *ppszOut = (CHAR *) LocalAlloc(LMEM_FIXED, cb);
  3136. if (NULL == *ppszOut)
  3137. {
  3138. hr = E_OUTOFMEMORY;
  3139. _JumpError(hr, error, "LocalAlloc");
  3140. }
  3141. CopyMemory(*ppszOut, pszIn, cb);
  3142. hr = S_OK;
  3143. error:
  3144. return(hr);
  3145. }
  3146. HRESULT
  3147. IsCurrentUserBuiltinAdmin(
  3148. OUT bool *pfIsMember)
  3149. {
  3150. HANDLE hAccessToken = NULL, hDupToken = NULL;
  3151. PSID psidAdministrators = NULL;
  3152. SID_IDENTIFIER_AUTHORITY siaNtAuthority = SECURITY_NT_AUTHORITY;
  3153. HRESULT hr = S_OK;
  3154. BOOL fIsMember = FALSE;
  3155. CSASSERT(pfIsMember);
  3156. if (!AllocateAndInitializeSid(
  3157. &siaNtAuthority,
  3158. 2,
  3159. SECURITY_BUILTIN_DOMAIN_RID,
  3160. DOMAIN_ALIAS_RID_ADMINS,
  3161. 0,
  3162. 0,
  3163. 0,
  3164. 0,
  3165. 0,
  3166. 0,
  3167. &psidAdministrators))
  3168. {
  3169. hr = myHLastError();
  3170. _JumpError(hr, error, "AllocateAndInitializeSid");
  3171. }
  3172. {
  3173. HANDLE hThread = GetCurrentThread();
  3174. if (NULL == hThread)
  3175. {
  3176. hr = myHLastError();
  3177. _JumpIfError(hr, error, "GetCurrentThread");
  3178. }
  3179. // Get the access token for current thread
  3180. if (!OpenThreadToken(
  3181. hThread,
  3182. TOKEN_QUERY | TOKEN_DUPLICATE,
  3183. FALSE,
  3184. &hAccessToken))
  3185. {
  3186. hr = myHLastError();
  3187. if(hr==HRESULT_FROM_WIN32(ERROR_NO_TOKEN))
  3188. {
  3189. HANDLE hProcess = GetCurrentProcess();
  3190. if (NULL == hProcess)
  3191. {
  3192. hr = myHLastError();
  3193. _JumpError(hr, error, "GetCurrentProcess");
  3194. }
  3195. if (!OpenProcessToken(hProcess,
  3196. TOKEN_DUPLICATE,
  3197. &hAccessToken))
  3198. {
  3199. hr = myHLastError();
  3200. _JumpError(hr, error, "OpenProcessToken");
  3201. }
  3202. }
  3203. else
  3204. {
  3205. _JumpError(hr, error, "OpenThreadToken");
  3206. }
  3207. }
  3208. }
  3209. // CheckTokenMembership must operate on impersonation token, so make one
  3210. if (!DuplicateToken(hAccessToken, SecurityIdentification, &hDupToken))
  3211. {
  3212. hr = myHLastError();
  3213. _JumpError(hr, error, "DuplicateToken");
  3214. }
  3215. if (!CheckTokenMembership(
  3216. hDupToken,
  3217. psidAdministrators,
  3218. &fIsMember))
  3219. {
  3220. hr = myHLastError();
  3221. _JumpError(hr, error, "CheckTokenMembership");
  3222. }
  3223. *pfIsMember = fIsMember?true:false;
  3224. hr = S_OK;
  3225. error:
  3226. if (hAccessToken)
  3227. CloseHandle(hAccessToken);
  3228. if (hDupToken)
  3229. CloseHandle(hDupToken);
  3230. // Free the SID we allocated
  3231. if (psidAdministrators)
  3232. FreeSid(psidAdministrators);
  3233. return(hr);
  3234. }
  3235. HRESULT
  3236. SetRegistryLocalPathString(
  3237. IN HKEY hkey,
  3238. IN WCHAR const *pwszRegValueName,
  3239. IN WCHAR const *pwszUNCPath)
  3240. {
  3241. HRESULT hr;
  3242. WCHAR *pwszLocalPath = NULL;
  3243. hr = myConvertUNCPathToLocal(pwszUNCPath, &pwszLocalPath);
  3244. _JumpIfError(hr, error, "myConvertUNCPathToLocal");
  3245. hr = RegSetValueEx(
  3246. hkey,
  3247. pwszRegValueName,
  3248. 0,
  3249. REG_SZ,
  3250. (BYTE *) pwszLocalPath,
  3251. (wcslen(pwszLocalPath) + 1) * sizeof(WCHAR));
  3252. _JumpIfError(hr, error, "RegSetValueEx");
  3253. error:
  3254. if (NULL != pwszLocalPath)
  3255. {
  3256. LocalFree(pwszLocalPath);
  3257. }
  3258. return(hr);
  3259. }
  3260. HRESULT
  3261. LocalMachineIsDomainMember(
  3262. OUT bool *pfIsDomainMember)
  3263. {
  3264. HRESULT hr = S_OK;
  3265. NTSTATUS status;
  3266. LSA_HANDLE PolicyHandle = NULL;
  3267. PPOLICY_PRIMARY_DOMAIN_INFO pPDI = NULL;
  3268. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  3269. CSASSERT(pfIsDomainMember);
  3270. *pfIsDomainMember = FALSE;
  3271. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  3272. status = LsaOpenPolicy(
  3273. NULL,
  3274. &ObjectAttributes,
  3275. GENERIC_READ | POLICY_VIEW_LOCAL_INFORMATION,
  3276. &PolicyHandle);
  3277. if(ERROR_SUCCESS != status)
  3278. {
  3279. hr = HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  3280. _JumpError(hr, error, "LsaOpenPolicy");
  3281. }
  3282. status = LsaQueryInformationPolicy( PolicyHandle,
  3283. PolicyPrimaryDomainInformation,
  3284. (PVOID*)&pPDI);
  3285. if(status)
  3286. {
  3287. hr = HRESULT_FROM_WIN32(LsaNtStatusToWinError(status));
  3288. _JumpError(hr, error, "LsaQueryInformationPolicy");
  3289. }
  3290. if( pPDI->Sid )
  3291. {
  3292. // domain member if has domain SID
  3293. *pfIsDomainMember = TRUE;
  3294. }
  3295. error:
  3296. if(pPDI)
  3297. {
  3298. LsaFreeMemory((LPVOID)pPDI);
  3299. }
  3300. if(PolicyHandle)
  3301. {
  3302. LsaClose(PolicyHandle);
  3303. }
  3304. return hr;
  3305. }
  3306. HRESULT
  3307. ComputeMAC(
  3308. LPCWSTR pcwsFileName,
  3309. LPWSTR *ppwszMAC)
  3310. {
  3311. HRESULT hr = S_OK;
  3312. HANDLE hFile = INVALID_HANDLE_VALUE;
  3313. HANDLE hFileMapping = NULL;
  3314. BYTE *pbFile = NULL;
  3315. DWORD cbImage, cbImageHigh=0;
  3316. __int64 icbImage, icbHashed;
  3317. DWORD i;
  3318. WCHAR rgwszMAC[64]; // 20 bytes @ 1 char/byte
  3319. DWORD cbString = 64*sizeof(WCHAR);
  3320. DWORD dwFileMappingSize;
  3321. HCRYPTPROV hProv = NULL;
  3322. HCRYPTHASH hHash = NULL;
  3323. BYTE rgbHashVal[CBMAX_CRYPT_HASH_LEN];
  3324. DWORD cbHashVal = CBMAX_CRYPT_HASH_LEN;
  3325. // find allocation granularity we can use
  3326. SYSTEM_INFO systemInfo;
  3327. GetSystemInfo(&systemInfo);
  3328. dwFileMappingSize = systemInfo.dwAllocationGranularity;
  3329. // get the file size
  3330. hFile = CreateFile(
  3331. pcwsFileName,
  3332. GENERIC_READ,
  3333. FILE_SHARE_READ,
  3334. NULL,
  3335. OPEN_EXISTING,
  3336. FILE_ATTRIBUTE_NORMAL,
  3337. 0);
  3338. if (INVALID_HANDLE_VALUE == hFile)
  3339. {
  3340. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  3341. _JumpError(hr, error, "CreateFile");
  3342. }
  3343. if (0xffffffff == (cbImage = GetFileSize(hFile, &cbImageHigh)))
  3344. {
  3345. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  3346. _JumpError(hr, error, "GetFileSize");
  3347. }
  3348. icbImage = ((__int64)cbImageHigh << 32) | cbImage;
  3349. // create mapping, indicating we will map the entire file sooner or later
  3350. hFileMapping = CreateFileMapping(hFile,
  3351. NULL,
  3352. PAGE_READONLY,
  3353. 0,
  3354. 0,
  3355. NULL);
  3356. if(hFileMapping == NULL)
  3357. {
  3358. hr = myHLastError();
  3359. _JumpError(hr, error, "CreateFileMapping");
  3360. }
  3361. // get a cryptographic provider
  3362. if (!CryptAcquireContext(
  3363. &hProv,
  3364. NULL, // container
  3365. MS_DEF_PROV, // provider name
  3366. PROV_RSA_FULL, // provider type
  3367. CRYPT_VERIFYCONTEXT)) // dwflags
  3368. {
  3369. hr = myHLastError();
  3370. _JumpError(hr, error, "CryptAcquireContext");
  3371. }
  3372. // get a hash
  3373. if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
  3374. {
  3375. hr = myHLastError();
  3376. _JumpError(hr, error, "CryptCreateHash");
  3377. }
  3378. // begin looping over data
  3379. for (icbHashed = 0; icbHashed < icbImage; icbHashed += dwFileMappingSize)
  3380. {
  3381. DWORD cbBytesLeft = (DWORD)min((__int64)dwFileMappingSize, icbImage-icbHashed);
  3382. // map the next blob into memory
  3383. pbFile = (BYTE *) MapViewOfFile(
  3384. hFileMapping,
  3385. FILE_MAP_READ,
  3386. (DWORD) (icbHashed>>32), //hi32
  3387. (DWORD) (icbHashed), //lo32
  3388. cbBytesLeft); // max num bytes to map
  3389. if (NULL == pbFile)
  3390. {
  3391. hr = myHLastError();
  3392. _JumpError(hr, error, "MapViewOfFile");
  3393. }
  3394. // hash file
  3395. if (!CryptHashData(hHash, pbFile, cbBytesLeft, 0))
  3396. {
  3397. hr = myHLastError();
  3398. _JumpError(hr, error, "CryptHashData");
  3399. }
  3400. // unmap this portion
  3401. if (!UnmapViewOfFile(pbFile))
  3402. {
  3403. pbFile = NULL;
  3404. hr = myHLastError();
  3405. _JumpError(hr, error, "UnmapViewOfFile");
  3406. }
  3407. pbFile = NULL;
  3408. }
  3409. // end looping over data
  3410. // retry the hash
  3411. if (!CryptGetHashParam(hHash, HP_HASHVAL, rgbHashVal, &cbHashVal, 0))
  3412. {
  3413. hr = myHLastError();
  3414. _JumpError(hr, error, "CryptGetHashParam");
  3415. }
  3416. hr = MultiByteIntegerToWszBuf(
  3417. TRUE, // byte multiple
  3418. cbHashVal,
  3419. rgbHashVal,
  3420. &cbString,
  3421. rgwszMAC);
  3422. _JumpIfError(hr, error, "MultiByteIntegerToWszBuf");
  3423. *ppwszMAC = (LPWSTR)LocalAlloc(LMEM_FIXED, cbString);
  3424. _JumpIfAllocFailed(*ppwszMAC, error);
  3425. wcscpy(*ppwszMAC, rgwszMAC);
  3426. error:
  3427. if (NULL != pbFile)
  3428. {
  3429. UnmapViewOfFile(pbFile);
  3430. }
  3431. if (NULL != hFileMapping)
  3432. {
  3433. CloseHandle(hFileMapping);
  3434. }
  3435. if (INVALID_HANDLE_VALUE != hFile)
  3436. {
  3437. CloseHandle(hFile);
  3438. }
  3439. if (NULL != hHash)
  3440. {
  3441. if (!CryptDestroyHash(hHash))
  3442. {
  3443. if (hr == S_OK)
  3444. {
  3445. hr = myHLastError();
  3446. }
  3447. }
  3448. }
  3449. if (NULL != hProv)
  3450. {
  3451. if (!CryptReleaseContext(hProv, 0))
  3452. {
  3453. if (hr == S_OK)
  3454. {
  3455. hr = myHLastError();
  3456. }
  3457. }
  3458. }
  3459. return(hr);
  3460. }
  3461. HRESULT
  3462. CertNameToHashString(
  3463. CERT_NAME_BLOB const *pCertName,
  3464. WCHAR **ppwszHash)
  3465. {
  3466. HRESULT hr = S_OK;
  3467. WCHAR wszHash[64]; // 20 bytes @ 1 char/byte
  3468. DWORD cbString = 64*sizeof(WCHAR);
  3469. HCRYPTPROV hProv = NULL;
  3470. HCRYPTHASH hHash = NULL;
  3471. BYTE rgbHashVal[CBMAX_CRYPT_HASH_LEN];
  3472. DWORD cbHashVal = CBMAX_CRYPT_HASH_LEN;
  3473. if(0==pCertName->cbData)
  3474. {
  3475. hr = E_INVALIDARG;
  3476. _JumpError(hr, error, "empty cert name");
  3477. }
  3478. CSASSERT(ppwszHash);
  3479. // get a cryptographic provider
  3480. if (!CryptAcquireContext(
  3481. &hProv,
  3482. NULL, // container
  3483. MS_DEF_PROV, // provider name
  3484. PROV_RSA_FULL, // provider type
  3485. CRYPT_VERIFYCONTEXT)) // dwflags
  3486. {
  3487. hr = myHLastError();
  3488. _JumpError(hr, error, "CryptAcquireContext");
  3489. }
  3490. // get a hash
  3491. if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
  3492. {
  3493. hr = myHLastError();
  3494. _JumpError(hr, error, "CryptCreateHash");
  3495. }
  3496. // hash file
  3497. if (!CryptHashData(hHash, pCertName->pbData, pCertName->cbData, 0))
  3498. {
  3499. hr = myHLastError();
  3500. _JumpError(hr, error, "CryptHashData");
  3501. }
  3502. if (!CryptGetHashParam(hHash, HP_HASHVAL, rgbHashVal, &cbHashVal, 0))
  3503. {
  3504. hr = myHLastError();
  3505. _JumpError(hr, error, "CryptGetHashParam");
  3506. }
  3507. hr = MultiByteIntegerToWszBuf(
  3508. TRUE, // byte multiple
  3509. cbHashVal,
  3510. rgbHashVal,
  3511. &cbString,
  3512. wszHash);
  3513. _JumpIfError(hr, error, "MultiByteIntegerToWszBuf");
  3514. // Generated string looks like this:
  3515. //
  3516. // 04 e7 23 92 98 9f d8 45 80 c9 ef 87 81 29 41 5d bc 4f 63 20
  3517. //
  3518. // We need to trim the spaces. We'll do it inplace.
  3519. {
  3520. WCHAR *pchSrc, *pchDest;
  3521. for(pchSrc=pchDest=wszHash; L'\0'!=*pchSrc; pchSrc++)
  3522. {
  3523. if(L' ' != *pchSrc)
  3524. *pchDest++ = *pchSrc;
  3525. }
  3526. *pchDest = L'\0';
  3527. }
  3528. *ppwszHash = (LPWSTR)LocalAlloc(LMEM_FIXED, cbString);
  3529. _JumpIfAllocFailed(*ppwszHash, error);
  3530. wcscpy(*ppwszHash, wszHash);
  3531. error:
  3532. if (NULL != hHash)
  3533. {
  3534. CryptDestroyHash(hHash);
  3535. }
  3536. if (NULL != hProv)
  3537. {
  3538. CryptReleaseContext(hProv, 0);
  3539. }
  3540. return hr;
  3541. }