Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

7571 lines
214 KiB

  1. //Copyright (c) 1998 - 2001 Microsoft Corporation
  2. #include "precomp.h"
  3. #include "afxcoll.h"
  4. #if !defined(_WIN32_WINNT)
  5. #define _WIN32_WINNT 0x0400
  6. #endif
  7. #include <wincrypt.h>
  8. #include "tlsapip.h"
  9. #include "global.h"
  10. #include "utils.h"
  11. #include "assert.h"
  12. #include "lrwizapi.h"
  13. #include "lmcons.h"
  14. #include "lmerr.h"
  15. #include "lmserver.h"
  16. #include "trust.h"
  17. #include "chstruct.h"
  18. #include "lkplite.h"
  19. #include "licensecodelite.h"
  20. #include <wininet.h>
  21. #define ACTIVATIONMETHOD_KEY "ACTIVATIONMETHOD"
  22. #define CSRNUMBER_KEY "CSRNUMBER"
  23. #ifdef IGNORE_EXPIRATION
  24. #define LICENSE_EXPIRATION_IGNORE L"SOFTWARE\\Microsoft\\TermServLicensing\\IgnoreLicenseExpiration"
  25. #endif
  26. CGlobal::CGlobal()
  27. {
  28. m_hWndParent = NULL;
  29. m_hInstance = NULL;
  30. m_lpstrLSName = NULL;
  31. m_lpwstrLSName = NULL;
  32. m_lpstrCHServer = NULL;
  33. m_lpstrCHExtension = NULL;
  34. m_dwErrorCode = 0;
  35. m_pReqAttr = NULL;
  36. m_dwReqAttrCount = 0;
  37. m_pRegAttr = NULL;
  38. m_dwRegAttrCount = NULL;
  39. m_dwLSStatus = LSERVERSTATUS_UNREGISTER;
  40. m_ContactData.Initialize();
  41. m_LicData.Initialize();
  42. m_ActivationMethod = CONNECTION_INTERNET;
  43. m_dwExchangeCertificateLen = 0;
  44. m_pbExchangeCertificate = NULL;
  45. m_dwSignCertificateLen = 0;
  46. m_pbSignCertificate = NULL;
  47. m_dwExtenstionValueLen = 0;
  48. m_pbExtensionValue = NULL;
  49. m_lpstrPIN = NULL;
  50. m_dwRequestType = REQUEST_NULL;
  51. m_WizAction = WIZACTION_REGISTERLS;
  52. m_hOpenDirect = NULL;
  53. m_hConnect = NULL;
  54. m_hRequest = NULL;
  55. m_phLSContext = NULL;
  56. m_pRegistrationID[ 0] = m_pLicenseServerID[ 0] = 0;
  57. m_dwRefresh = 0;
  58. m_lpCSRNumber[ 0] = 0;
  59. m_lpWWWSite[0] = 0;
  60. m_pLSLKP[ 0] = m_pLSSPK[ 0] = 0;
  61. m_dwLastRetCode = 0;
  62. m_dwLangId = 0;
  63. m_fSupportConcurrent = FALSE;
  64. m_fSupportWhistlerCAL = FALSE;
  65. m_WizType = WIZTYPE_ACTIVATION;
  66. InitSPKList();
  67. // Initialize the Wizard Page stack
  68. ClearWizStack();
  69. }
  70. void CGlobal::FreeGlobal()
  71. {
  72. if (m_pbSignCertificate != NULL)
  73. {
  74. LocalFree(m_pbSignCertificate);
  75. m_pbSignCertificate = NULL;
  76. }
  77. if (m_pbExchangeCertificate != NULL)
  78. {
  79. LocalFree(m_pbExchangeCertificate);
  80. m_pbExchangeCertificate = NULL;
  81. }
  82. if (m_lpwstrLSName)
  83. {
  84. delete m_lpwstrLSName;
  85. m_lpwstrLSName = NULL;
  86. }
  87. if(m_lpstrCHServer)
  88. {
  89. delete m_lpstrCHServer;
  90. m_lpstrCHServer = NULL;
  91. }
  92. if (m_lpstrCHExtension)
  93. {
  94. delete m_lpstrCHExtension;
  95. m_lpstrCHExtension = NULL;
  96. }
  97. if(m_pbExtensionValue)
  98. {
  99. delete m_pbExtensionValue;
  100. m_pbExtensionValue = NULL;
  101. }
  102. if(m_lpstrPIN)
  103. {
  104. delete m_lpstrPIN;
  105. m_lpstrPIN = NULL;
  106. }
  107. m_csaCountryDesc.RemoveAll();
  108. m_csaCountryCode.RemoveAll();
  109. m_csaProductDesc.RemoveAll();
  110. m_csaProductCode.RemoveAll();
  111. m_csaDeactReasonCode.RemoveAll();
  112. m_csaDeactReasonDesc.RemoveAll();
  113. m_csaReactReasonCode.RemoveAll();
  114. m_csaReactReasonDesc.RemoveAll();
  115. }
  116. CGlobal::~CGlobal()
  117. {
  118. FreeGlobal();
  119. }
  120. void CGlobal::ClearWizStack()
  121. {
  122. DWORD dwIndex;
  123. m_dwTop = 0;
  124. for(dwIndex = 0 ; dwIndex < NO_OF_PAGES ; dwIndex++)
  125. m_dwWizStack[dwIndex] = 0;
  126. }
  127. PCONTACTINFO CGlobal::GetContactDataObject()
  128. {
  129. return &m_ContactData;
  130. }
  131. PTSLICINFO CGlobal::GetLicDataObject()
  132. {
  133. return &m_LicData;
  134. }
  135. DWORD CGlobal::InitGlobal()
  136. {
  137. DWORD dwRetCode = ERROR_SUCCESS;
  138. DWORD dwDataLen = 0;
  139. DWORD dwDisposition = 0;
  140. DWORD dwType = REG_SZ;
  141. HKEY hKey = NULL;
  142. LPTSTR lpszValue = NULL;
  143. LPTSTR lpszDelimiter = (LPTSTR)L"~";
  144. CString sCountryDesc;
  145. LPTSTR lpTemp = NULL;
  146. TLSPrivateDataUnion getParm;
  147. PTLSPrivateDataUnion pRtn = NULL;
  148. error_status_t esRPC = ERROR_SUCCESS;
  149. DWORD dwRetDataType = 0;
  150. DWORD dwSupportFlags;
  151. TCHAR lpBuffer[ 1024];
  152. m_ContactData.Initialize();
  153. m_LicData.Initialize();
  154. m_dwLSStatus = LSERVERSTATUS_UNREGISTER;
  155. m_phLSContext = NULL;
  156. //
  157. // Load Countries from the String Table
  158. //
  159. LoadCountries();
  160. LoadReasons();
  161. //
  162. // Get CH URL from the LS Registry
  163. //
  164. dwRetCode = ConnectToLSRegistry();
  165. if(dwRetCode != ERROR_SUCCESS)
  166. {
  167. goto done;
  168. }
  169. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  170. REG_LRWIZ_PARAMS,
  171. 0,
  172. NULL,
  173. REG_OPTION_NON_VOLATILE,
  174. KEY_ALL_ACCESS,
  175. NULL,
  176. &hKey,
  177. &dwDisposition);
  178. if(dwRetCode != ERROR_SUCCESS)
  179. {
  180. LRSetLastError(dwRetCode);
  181. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  182. goto done;
  183. }
  184. //
  185. //LR State
  186. //
  187. m_dwLRState = 0;
  188. dwType = REG_DWORD;
  189. dwDataLen = sizeof(m_dwLRState);
  190. RegQueryValueEx(hKey,
  191. REG_LRWIZ_STATE,
  192. 0,
  193. &dwType,
  194. (LPBYTE)&m_dwLRState,
  195. &dwDataLen
  196. );
  197. lpBuffer[ 0] = 0;
  198. GetFromRegistry(ACTIVATIONMETHOD_KEY, lpBuffer, FALSE);
  199. if (_tcslen(lpBuffer) != 0)
  200. {
  201. m_ActivationMethod = (WIZCONNECTION) _ttoi(lpBuffer);
  202. }
  203. else
  204. {
  205. m_ActivationMethod = CONNECTION_DEFAULT; //Partially fix bug # 577
  206. }
  207. if ((m_ActivationMethod != CONNECTION_DEFAULT)
  208. && (m_ActivationMethod != CONNECTION_INTERNET)
  209. && (m_ActivationMethod != CONNECTION_WWW)
  210. && (m_ActivationMethod != CONNECTION_PHONE))
  211. {
  212. m_ActivationMethod = CONNECTION_DEFAULT;
  213. }
  214. GetFromRegistry(CSRNUMBER_KEY, m_lpCSRNumber, FALSE);
  215. //
  216. // LKP Request Count
  217. //
  218. m_dwLRCount = 0;
  219. dwType = REG_DWORD;
  220. dwDataLen = sizeof(m_dwLRCount);
  221. RegQueryValueEx(hKey,
  222. REG_LR_COUNT,
  223. 0,
  224. &dwType,
  225. (LPBYTE)&m_dwLRCount,
  226. &dwDataLen
  227. );
  228. // dwDataLen includes the null terminating char.
  229. // So if the key is empty,dwDataLen is 2 bytes, not 0.
  230. // See raid bug id : 336.
  231. //
  232. //CH URL
  233. //
  234. dwType = REG_SZ;
  235. dwDataLen = 0;
  236. RegQueryValueEx(hKey,
  237. REG_CH_SERVER,
  238. 0,
  239. &dwType,
  240. NULL,
  241. &dwDataLen
  242. );
  243. if(dwDataLen <= sizeof(TCHAR))
  244. {
  245. dwRetCode = IDS_ERR_CHURLKEY_EMPTY;
  246. goto done;
  247. }
  248. m_lpstrCHServer = new TCHAR[dwDataLen+1];
  249. memset(m_lpstrCHServer, 0, (dwDataLen+1)*sizeof(TCHAR) );
  250. RegQueryValueEx(hKey,
  251. REG_CH_SERVER,
  252. 0,
  253. &dwType,
  254. (LPBYTE)m_lpstrCHServer,
  255. &dwDataLen
  256. );
  257. //
  258. //CH Extension
  259. //
  260. dwType = REG_SZ;
  261. dwDataLen = 0;
  262. RegQueryValueEx(hKey,
  263. REG_CH_EXTENSION,
  264. 0,
  265. &dwType,
  266. NULL,
  267. &dwDataLen
  268. );
  269. if(dwDataLen <= sizeof(TCHAR))
  270. {
  271. dwRetCode = IDS_ERR_CHURLKEY_EMPTY;
  272. goto done;
  273. }
  274. m_lpstrCHExtension = new TCHAR[dwDataLen+1];
  275. memset(m_lpstrCHExtension, 0, (dwDataLen+1)*sizeof(TCHAR) );
  276. RegQueryValueEx(hKey,
  277. REG_CH_EXTENSION,
  278. 0,
  279. &dwType,
  280. (LPBYTE)m_lpstrCHExtension,
  281. &dwDataLen
  282. );
  283. //
  284. // WWW site address
  285. //
  286. dwType = REG_SZ;
  287. dwDataLen = sizeof(m_lpWWWSite);
  288. dwRetCode = RegQueryValueEx(hKey,
  289. REG_WWW_SITE,
  290. 0,
  291. &dwType,
  292. (LPBYTE)m_lpWWWSite,
  293. &dwDataLen
  294. );
  295. if(dwRetCode != ERROR_SUCCESS)
  296. {
  297. LRSetLastError(dwRetCode);
  298. dwRetCode = IDS_ERR_CHURLKEY_EMPTY;
  299. goto done;
  300. }
  301. GetFromRegistry(szOID_STREET_ADDRESS, m_ContactData.sContactAddress.GetBuffer(CA_ADDRESS_LEN + 1), FALSE);
  302. m_ContactData.sContactAddress.ReleaseBuffer();
  303. GetFromRegistry(szOID_POSTAL_CODE, m_ContactData.sZip.GetBuffer(CA_ZIP_LEN + 1), FALSE);
  304. m_ContactData.sZip.ReleaseBuffer();
  305. GetFromRegistry(szOID_LOCALITY_NAME, m_ContactData.sCity.GetBuffer(CA_CITY_LEN + 1), FALSE);
  306. m_ContactData.sCity.ReleaseBuffer();
  307. GetFromRegistry(szOID_DESCRIPTION, m_ContactData.sCountryCode.GetBuffer(LR_COUNTRY_CODE_LEN + 1), FALSE);
  308. m_ContactData.sCountryCode.ReleaseBuffer();
  309. GetFromRegistry(szOID_COUNTRY_NAME, m_ContactData.sCountryDesc.GetBuffer(LR_COUNTRY_DESC_LEN + 1), FALSE);
  310. m_ContactData.sCountryDesc.ReleaseBuffer();
  311. GetFromRegistry(szOID_STATE_OR_PROVINCE_NAME, m_ContactData.sState.GetBuffer(CA_STATE_LEN + 1), FALSE);
  312. m_ContactData.sState.ReleaseBuffer();
  313. GetFromRegistry(szOID_ORGANIZATION_NAME, m_ContactData.sCompanyName.GetBuffer(CA_COMPANY_NAME_LEN + 1), FALSE);
  314. m_ContactData.sCompanyName.ReleaseBuffer();
  315. GetFromRegistry(szOID_ORGANIZATIONAL_UNIT_NAME, m_ContactData.sOrgUnit.GetBuffer(CA_ORG_UNIT_LEN + 1), FALSE);
  316. m_ContactData.sOrgUnit.ReleaseBuffer();
  317. GetFromRegistry(szOID_SUR_NAME, m_ContactData.sContactLName.GetBuffer(CA_NAME_LEN + 1), FALSE);
  318. m_ContactData.sContactLName.ReleaseBuffer();
  319. GetFromRegistry(szOID_COMMON_NAME, m_ContactData.sContactFName.GetBuffer(CA_NAME_LEN + 1), FALSE);
  320. m_ContactData.sContactFName.ReleaseBuffer();
  321. GetFromRegistry(szOID_RSA_emailAddr, m_ContactData.sContactEmail.GetBuffer(CA_EMAIL_LEN + 1), FALSE);
  322. m_ContactData.sContactEmail.ReleaseBuffer();
  323. GetFromRegistry(szOID_BUSINESS_CATEGORY, m_ContactData.sProgramName.GetBuffer(PROGRAM_NAME_MAX_LENGTH + 1), FALSE);
  324. m_ContactData.sProgramName.ReleaseBuffer();
  325. GetFromRegistry(REG_LRWIZ_CSPHONEREGION, m_ContactData.sCSRPhoneRegion.GetBuffer(MAX_COUNTRY_NAME_LENGTH + 1), FALSE);
  326. m_ContactData.sCSRPhoneRegion.ReleaseBuffer();
  327. //These are no longer being used
  328. m_ContactData.sContactFax = "";
  329. m_ContactData.sContactPhone = "";
  330. m_ContactData.sCSRFaxRegion = "";
  331. InitSPKList();
  332. SetLSLangId(GetUserDefaultUILanguage());
  333. //
  334. // Get the info for the License Server.
  335. //
  336. dwRetCode = ConnectToLS();
  337. if(dwRetCode != ERROR_SUCCESS)
  338. {
  339. goto done;
  340. }
  341. dwRetCode = TLSGetSupportFlags(
  342. m_phLSContext,
  343. &dwSupportFlags
  344. );
  345. if (dwRetCode == RPC_S_OK)
  346. {
  347. if ((dwRetCode == RPC_S_OK) && (dwSupportFlags & SUPPORT_CONCURRENT))
  348. {
  349. m_fSupportConcurrent = TRUE;
  350. }
  351. else
  352. {
  353. m_fSupportConcurrent = FALSE;
  354. }
  355. if (dwSupportFlags & SUPPORT_WHISTLER_CAL)
  356. {
  357. m_fSupportWhistlerCAL = TRUE;
  358. }
  359. else
  360. {
  361. m_fSupportWhistlerCAL = FALSE;
  362. }
  363. }
  364. else
  365. {
  366. m_fSupportConcurrent = FALSE;
  367. m_fSupportWhistlerCAL = FALSE;
  368. dwRetCode = RPC_S_OK; // OK if this fails
  369. }
  370. //
  371. // Load Products from the String Table
  372. //
  373. LoadProducts();
  374. done:
  375. DisconnectLS();
  376. if(pRtn)
  377. midl_user_free(pRtn);
  378. if(hKey)
  379. RegCloseKey(hKey);
  380. DisconnectLSRegistry();
  381. return dwRetCode;
  382. }
  383. DWORD CGlobal::CheckRequieredFields()
  384. {
  385. DWORD dwRetCode = ERROR_SUCCESS;
  386. //Validate sProgramName (Partially fix bug # 577)
  387. if ( (m_ContactData.sProgramName != PROGRAM_LICENSE_PAK &&
  388. m_ContactData.sProgramName != PROGRAM_MOLP &&
  389. m_ContactData.sProgramName != PROGRAM_SELECT &&
  390. m_ContactData.sProgramName != PROGRAM_ENTERPRISE &&
  391. m_ContactData.sProgramName != PROGRAM_CAMPUS_AGREEMENT &&
  392. m_ContactData.sProgramName != PROGRAM_SCHOOL_AGREEMENT &&
  393. m_ContactData.sProgramName != PROGRAM_APP_SERVICES &&
  394. m_ContactData.sProgramName != PROGRAM_OTHER) ||
  395. (m_ContactData.sCompanyName == "" ||
  396. m_ContactData.sContactLName == "" ||
  397. m_ContactData.sContactFName == "" ||
  398. m_ContactData.sCountryCode == "" ||
  399. m_ContactData.sCountryDesc == "") )
  400. {
  401. dwRetCode = IDS_ERR_REQ_FIELD_EMPTY;
  402. }
  403. return dwRetCode;
  404. }
  405. void CGlobal::SetLSStatus(DWORD dwStatus)
  406. {
  407. m_dwLSStatus = dwStatus;
  408. }
  409. DWORD CGlobal::GetLSStatus(void)
  410. {
  411. return m_dwLSStatus;
  412. }
  413. void CGlobal::SetInstanceHandle(HINSTANCE hInst)
  414. {
  415. m_hInstance = hInst;
  416. }
  417. HINSTANCE CGlobal::GetInstanceHandle()
  418. {
  419. return m_hInstance;
  420. }
  421. void CGlobal::SetLSName(LPCTSTR lpstrLSName)
  422. {
  423. if(m_lpwstrLSName)
  424. {
  425. delete m_lpwstrLSName;
  426. m_lpwstrLSName = NULL;
  427. }
  428. if (lpstrLSName != NULL)
  429. {
  430. m_lpwstrLSName = new WCHAR[MAX_COMPUTERNAME_LENGTH + 1];
  431. wcscpy(m_lpwstrLSName,(LPWSTR)lpstrLSName);
  432. m_lpstrLSName = (LPTSTR) lpstrLSName;
  433. }
  434. }
  435. WIZCONNECTION CGlobal::GetActivationMethod(void)
  436. {
  437. return m_ActivationMethod;
  438. }
  439. void CGlobal::SetActivationMethod(WIZCONNECTION conn)
  440. {
  441. TCHAR acBuf[ 32];
  442. _stprintf(acBuf, _T("%d"), conn);
  443. SetInRegistry(ACTIVATIONMETHOD_KEY, acBuf);
  444. m_ActivationMethod = conn;
  445. }
  446. WIZCONNECTION CGlobal::GetLSProp_ActivationMethod(void)
  447. {
  448. return m_LSProp_ActivationMethod;
  449. }
  450. void CGlobal::SetLSProp_ActivationMethod(WIZCONNECTION conn)
  451. {
  452. m_LSProp_ActivationMethod = conn;
  453. }
  454. WIZACTION CGlobal::GetWizAction(void)
  455. {
  456. return m_WizAction;
  457. }
  458. void CGlobal::SetWizAction(WIZACTION act)
  459. {
  460. m_WizAction = act;
  461. }
  462. DWORD CGlobal::GetEntryPoint(void)
  463. {
  464. DWORD dwReturn = 0;
  465. switch (m_ActivationMethod)
  466. {
  467. case CONNECTION_INTERNET:
  468. switch (m_WizAction)
  469. {
  470. case WIZACTION_REGISTERLS:
  471. dwReturn = IDD_CONTACTINFO1;
  472. break;
  473. case WIZACTION_CONTINUEREGISTERLS:
  474. //This is no longer considered a valid state since
  475. //PIN numbers aren't emailed anymore
  476. {
  477. // Restart
  478. SetLRState(LRSTATE_NEUTRAL);
  479. SetLSStatus(LSERVERSTATUS_UNREGISTER);
  480. SetWizAction(WIZACTION_REGISTERLS);
  481. ClearWizStack();
  482. dwReturn = IDD_DLG_GETREGMODE;
  483. }
  484. break;
  485. case WIZACTION_DOWNLOADLKP:
  486. dwReturn = IDD_LICENSETYPE;
  487. break;
  488. case WIZACTION_UNREGISTERLS:
  489. case WIZACTION_REREGISTERLS:
  490. dwReturn = IDD_DLG_CERTLOG_INFO;
  491. break;
  492. case WIZACTION_SHOWPROPERTIES:
  493. dwReturn = IDD_WELCOME;
  494. break;
  495. }
  496. break;
  497. case CONNECTION_PHONE:
  498. switch (m_WizAction)
  499. {
  500. case WIZACTION_REGISTERLS:
  501. case WIZACTION_CONTINUEREGISTERLS:
  502. dwReturn = IDD_DLG_TELREG;
  503. break;
  504. case WIZACTION_DOWNLOADLASTLKP:
  505. case WIZACTION_DOWNLOADLKP:
  506. // Calls Authenticate
  507. dwReturn = IDD_DLG_TELLKP;
  508. break;
  509. case WIZACTION_UNREGISTERLS:
  510. dwReturn = IDD_DLG_CONFREVOKE;
  511. break;
  512. case WIZACTION_REREGISTERLS:
  513. dwReturn = IDD_DLG_TELREG_REISSUE;
  514. break;
  515. case WIZACTION_SHOWPROPERTIES:
  516. dwReturn = IDD_WELCOME;
  517. break;
  518. }
  519. break;
  520. case CONNECTION_WWW:
  521. switch (m_WizAction)
  522. {
  523. case WIZACTION_REGISTERLS:
  524. case WIZACTION_CONTINUEREGISTERLS:
  525. dwReturn = IDD_DLG_WWWREG;
  526. break;
  527. case WIZACTION_DOWNLOADLASTLKP:
  528. case WIZACTION_DOWNLOADLKP:
  529. // Calls Authenticate
  530. dwReturn = IDD_DLG_WWWLKP;
  531. break;
  532. case WIZACTION_UNREGISTERLS:
  533. break;
  534. case WIZACTION_REREGISTERLS:
  535. dwReturn = IDD_DLG_WWWREG_REISSUE;
  536. break;
  537. case WIZACTION_SHOWPROPERTIES:
  538. dwReturn = IDD_WELCOME;
  539. break;
  540. }
  541. break;
  542. default:
  543. break;
  544. }
  545. return dwReturn;
  546. }
  547. DWORD CGlobal::LRGetLastError()
  548. {
  549. DWORD dwRet;
  550. dwRet = m_dwErrorCode;
  551. m_dwErrorCode = 0;
  552. return dwRet;
  553. }
  554. void CGlobal::LRSetLastError(DWORD dwErrorCode)
  555. {
  556. m_dwErrorCode = dwErrorCode;
  557. }
  558. int CGlobal::LRMessageBox(HWND hWndParent,DWORD dwMsgId,DWORD dwCaptionID /*=0*/,DWORD dwErrorCode /*=0*/)
  559. {
  560. TCHAR szBuf[LR_MAX_MSG_TEXT];
  561. TCHAR szMsg[LR_MAX_MSG_TEXT];
  562. TCHAR szCaption[LR_MAX_MSG_CAPTION];
  563. LoadString(GetInstanceHandle(),dwMsgId,szMsg,LR_MAX_MSG_TEXT);
  564. if (dwCaptionID == 0)
  565. LoadString(GetInstanceHandle(),IDS_TITLE,szCaption,LR_MAX_MSG_CAPTION);
  566. else
  567. LoadString(GetInstanceHandle(),dwCaptionID,szCaption,LR_MAX_MSG_CAPTION);
  568. if(dwErrorCode != 0)
  569. {
  570. DWORD dwRet = 0;
  571. LPTSTR lpszTemp = NULL;
  572. dwRet=FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
  573. NULL,
  574. dwErrorCode,
  575. LANG_NEUTRAL,
  576. (LPTSTR)&lpszTemp,
  577. 0,
  578. NULL);
  579. _stprintf(szBuf,szMsg,dwErrorCode);
  580. if(dwRet != 0 && lpszTemp != NULL)
  581. {
  582. lstrcat(szBuf, _T(", "));
  583. lstrcat(szBuf, lpszTemp);
  584. LocalFree(lpszTemp);
  585. }
  586. }
  587. else
  588. {
  589. _tcscpy(szBuf,szMsg);
  590. }
  591. return MessageBox(hWndParent,szBuf,szCaption,MB_OK|MB_ICONSTOP);
  592. }
  593. BOOL CGlobal::IsLSRunning()
  594. {
  595. DWORD dwRetCode = ERROR_SUCCESS;
  596. if (ConnectToLS() != ERROR_SUCCESS)
  597. {
  598. return FALSE;
  599. }
  600. DisconnectLS();
  601. return TRUE;
  602. }
  603. DWORD CGlobal::ResetLSSPK(BOOL bGenKey)
  604. {
  605. DWORD dwRetCode;
  606. error_status_t esRPC = ERROR_SUCCESS;
  607. dwRetCode = ConnectToLS();
  608. if(dwRetCode == ERROR_SUCCESS)
  609. {
  610. // Make LS Regen Key call HERE
  611. dwRetCode = TLSTriggerReGenKey(m_phLSContext, bGenKey, &esRPC);
  612. if(dwRetCode != RPC_S_OK || esRPC != ERROR_SUCCESS)
  613. {
  614. dwRetCode = IDS_ERR_RPC_FAILED;
  615. }
  616. else
  617. {
  618. dwRetCode = ERROR_SUCCESS;
  619. }
  620. }
  621. DisconnectLS();
  622. LRSetLastError(dwRetCode);
  623. return dwRetCode;
  624. }
  625. DWORD CGlobal::GetLSCertificates(PDWORD pdwServerStatus)
  626. {
  627. DWORD dwRetCode = ERROR_SUCCESS;
  628. PCONTEXT_HANDLE phLSContext = NULL;
  629. error_status_t esRPC = ERROR_SUCCESS;
  630. error_status_t esTemp = ERROR_SUCCESS;
  631. PBYTE pCertBlob = NULL;
  632. PBYTE pSignCertBlob = NULL;
  633. DWORD dwCertBlobLen = 0;
  634. DWORD dwSignCertBlobLen = 0;
  635. DWORD dwCertSize = 0;
  636. DWORD dwRegIDLength = 0;
  637. DWORD dwLSIDLen = 0;
  638. HCRYPTPROV hCryptProvider = NULL;
  639. CRYPT_DATA_BLOB CertBlob;
  640. HCERTSTORE hCertStore = NULL;
  641. PCCERT_CONTEXT pcCertContext = NULL;
  642. PCERT_EXTENSION pCertExtension = NULL;
  643. BYTE * pByte = NULL;
  644. m_dwExchangeCertificateLen = 0;
  645. if (m_pbExchangeCertificate != NULL)
  646. {
  647. LocalFree(m_pbExchangeCertificate);
  648. }
  649. if (m_pbSignCertificate != NULL)
  650. {
  651. LocalFree(m_pbSignCertificate);
  652. }
  653. m_pbSignCertificate = NULL;
  654. m_pbExchangeCertificate = NULL;
  655. *pdwServerStatus = LSERVERSTATUS_UNREGISTER;
  656. m_pRegistrationID[0] = NULL;
  657. m_pLicenseServerID[0] = NULL;
  658. dwRetCode = ConnectToLS();
  659. if (dwRetCode != ERROR_SUCCESS)
  660. {
  661. goto done;
  662. }
  663. // We need the License Server ID
  664. dwRetCode = TLSGetServerPID( m_phLSContext,
  665. &dwLSIDLen,
  666. &pByte,
  667. &esRPC );
  668. if (dwRetCode != RPC_S_OK)
  669. {
  670. LRSetLastError(dwRetCode);
  671. dwRetCode = IDS_ERR_RPC_FAILED;
  672. goto done;
  673. }
  674. if (esRPC == LSERVER_E_DATANOTFOUND ||
  675. dwLSIDLen != sizeof(TCHAR)*(LR_LICENSESERVERID_LEN+1))
  676. {
  677. if (pByte != NULL)
  678. {
  679. LocalFree(pByte);
  680. }
  681. dwRetCode = IDS_ERR_NOLSID;
  682. goto done;
  683. }
  684. assert(esRPC == ERROR_SUCCESS && pByte != NULL);
  685. memcpy(m_pLicenseServerID, pByte, sizeof(TCHAR)*(LR_LICENSESERVERID_LEN+1));
  686. LocalFree(pByte);
  687. //Try and get the LSServerCertificate first
  688. dwRetCode = TLSGetServerCertificate ( m_phLSContext,
  689. FALSE,
  690. &pCertBlob,
  691. &dwCertBlobLen,
  692. &esRPC );
  693. if(dwRetCode != RPC_S_OK)
  694. {
  695. LRSetLastError(dwRetCode);
  696. dwRetCode = IDS_ERR_RPC_FAILED;
  697. goto done;
  698. }
  699. if (esRPC == LSERVER_I_TEMP_SELFSIGN_CERT )
  700. {
  701. // Certificate is NOT signed & does not have the SPK
  702. dwRetCode = ERROR_SUCCESS;
  703. goto done;
  704. }
  705. else
  706. {
  707. // Certificate is either msft signed OR there is an SPK
  708. // in it.
  709. pByte = NULL;
  710. dwRetCode = TLSGetServerSPK( m_phLSContext,
  711. &dwRegIDLength,
  712. &pByte,
  713. &esTemp );
  714. if (dwRetCode != RPC_S_OK)
  715. {
  716. LRSetLastError(dwRetCode);
  717. dwRetCode = IDS_ERR_RPC_FAILED;
  718. goto done;
  719. }
  720. if (esTemp == LSERVER_E_DATANOTFOUND)
  721. {
  722. if (pByte != NULL)
  723. {
  724. LocalFree(pByte);
  725. }
  726. dwRetCode = ERROR_SUCCESS;
  727. goto done;
  728. }
  729. if (esTemp != ERROR_SUCCESS)
  730. {
  731. if (pByte != NULL)
  732. {
  733. LocalFree(pByte);
  734. }
  735. LRSetLastError(dwRetCode);
  736. dwRetCode = IDS_ERR_RPC_FAILED;
  737. goto done;
  738. }
  739. if (dwRegIDLength != sizeof(TCHAR)*(LR_REGISTRATIONID_LEN+1))
  740. {
  741. // What happened to the SPK's Length ??
  742. if (pByte != NULL)
  743. {
  744. LocalFree(pByte);
  745. }
  746. dwRetCode = IDS_ERR_INVALIDLENGTH;
  747. LRSetLastError(dwRetCode);
  748. goto done;
  749. }
  750. assert(pByte != NULL);
  751. memcpy(m_pRegistrationID, pByte, sizeof(TCHAR)*(LR_REGISTRATIONID_LEN+1));
  752. LocalFree(pByte);
  753. }
  754. if(esRPC != LSERVER_I_SELFSIGN_CERTIFICATE && esRPC != ERROR_SUCCESS )
  755. {
  756. LRSetLastError(esRPC);
  757. dwRetCode = IDS_ERR_LS_ERROR;
  758. goto done;
  759. }
  760. m_pbExchangeCertificate = pCertBlob;
  761. m_dwExchangeCertificateLen = dwCertBlobLen;
  762. // Now that everything has succeded, let us get thesigning cert
  763. dwRetCode = TLSGetServerCertificate ( m_phLSContext,
  764. TRUE,
  765. &pSignCertBlob,
  766. &dwSignCertBlobLen,
  767. &esRPC );
  768. if (dwRetCode == RPC_S_OK && esRPC == LSERVER_S_SUCCESS )
  769. {
  770. m_pbSignCertificate = pSignCertBlob;
  771. m_dwSignCertificateLen = dwSignCertBlobLen;
  772. }
  773. else
  774. {
  775. dwRetCode = ERROR_SUCCESS; // Ignore this error;
  776. m_pbSignCertificate = NULL;
  777. m_dwSignCertificateLen = 0;
  778. }
  779. //
  780. //Get the Extensions from the Certificate
  781. //
  782. if ( esRPC != LSERVER_I_SELFSIGN_CERTIFICATE )
  783. {
  784. CertBlob.cbData = m_dwExchangeCertificateLen;
  785. CertBlob.pbData = m_pbExchangeCertificate;
  786. //Create the PKCS7 store and get the first cert out of it!
  787. dwRetCode = GetTempCryptContext(&hCryptProvider);
  788. if( dwRetCode != ERROR_SUCCESS )
  789. {
  790. LRSetLastError(dwRetCode);
  791. dwRetCode = IDS_ERR_CRYPT_ERROR;
  792. goto done;
  793. }
  794. hCertStore = CertOpenStore( CERT_STORE_PROV_PKCS7,
  795. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  796. hCryptProvider,
  797. CERT_STORE_NO_CRYPT_RELEASE_FLAG,
  798. &CertBlob );
  799. if( NULL == hCertStore )
  800. {
  801. LRSetLastError(GetLastError());
  802. dwRetCode = IDS_ERR_CRYPT_ERROR;
  803. goto done;
  804. }
  805. //Get the cert from the store
  806. pcCertContext = CertEnumCertificatesInStore ( hCertStore, NULL );
  807. if ( !pcCertContext )
  808. {
  809. LRSetLastError(GetLastError());
  810. dwRetCode = IDS_ERR_CRYPT_ERROR;
  811. goto done;
  812. }
  813. //Get the extension and store the cert type in it
  814. pCertExtension = CertFindExtension ( szOID_NULL_EXT,
  815. pcCertContext->pCertInfo->cExtension,
  816. pcCertContext->pCertInfo->rgExtension
  817. );
  818. if ( !pCertExtension )
  819. {
  820. LRSetLastError(CRYPT_E_NOT_FOUND);
  821. dwRetCode = IDS_ERR_CRYPT_ERROR;
  822. goto done;
  823. }
  824. //Get the value and store it in the member function
  825. m_dwExtenstionValueLen = pCertExtension->Value.cbData;
  826. m_pbExtensionValue = new BYTE [m_dwExtenstionValueLen + 1 ];
  827. memset ( m_pbExtensionValue, 0, m_dwExtenstionValueLen + 1 );
  828. memcpy ( m_pbExtensionValue, pCertExtension->Value.pbData, m_dwExtenstionValueLen );
  829. dwRetCode = ERROR_SUCCESS;
  830. *pdwServerStatus = LSERVERSTATUS_REGISTER_INTERNET;
  831. }
  832. else
  833. {
  834. // There is an SPK
  835. dwRetCode = ERROR_SUCCESS;
  836. *pdwServerStatus = LSERVERSTATUS_REGISTER_OTHER;
  837. }
  838. done:
  839. DisconnectLS();
  840. if ( pcCertContext )
  841. {
  842. CertFreeCertificateContext ( pcCertContext );
  843. }
  844. if ( hCertStore )
  845. {
  846. CertCloseStore (hCertStore,CERT_CLOSE_STORE_CHECK_FLAG);
  847. }
  848. DoneWithTempCryptContext(hCryptProvider);
  849. return dwRetCode;
  850. }
  851. DWORD CGlobal::IsLicenseServerRegistered(PDWORD pdwServerStatus)
  852. {
  853. DWORD dwRetCode = ERROR_SUCCESS;
  854. PCONTEXT_HANDLE phLSContext = NULL;
  855. error_status_t esRPC = ERROR_SUCCESS;
  856. PBYTE pCertBlob = NULL;
  857. DWORD dwCertBlobLen = 0;
  858. *pdwServerStatus = LSERVERSTATUS_UNREGISTER;
  859. dwRetCode = ConnectToLS();
  860. if (dwRetCode != ERROR_SUCCESS)
  861. {
  862. goto done;
  863. }
  864. //Try and get the LSServerCertificate first
  865. dwRetCode = TLSGetServerCertificate ( m_phLSContext,
  866. FALSE,
  867. &pCertBlob,
  868. &dwCertBlobLen,
  869. &esRPC );
  870. if(dwRetCode != RPC_S_OK)
  871. {
  872. LRSetLastError(dwRetCode);
  873. dwRetCode = IDS_ERR_RPC_FAILED;
  874. goto done;
  875. }
  876. if ( esRPC == ERROR_SUCCESS)
  877. {
  878. *pdwServerStatus = LSERVERSTATUS_REGISTER_INTERNET;
  879. }
  880. else if ( esRPC == LSERVER_I_SELFSIGN_CERTIFICATE )
  881. {
  882. *pdwServerStatus = LSERVERSTATUS_REGISTER_OTHER;
  883. }
  884. else if (esRPC == LSERVER_I_TEMP_SELFSIGN_CERT )
  885. {
  886. *pdwServerStatus = LSERVERSTATUS_UNREGISTER;
  887. }
  888. else
  889. {
  890. LRSetLastError(esRPC);
  891. dwRetCode = IDS_ERR_LS_ERROR;
  892. }
  893. done:
  894. DisconnectLS();
  895. if ( pCertBlob )
  896. {
  897. LocalFree(pCertBlob);
  898. }
  899. return dwRetCode;
  900. }
  901. DWORD CGlobal::GetTempCryptContext(HCRYPTPROV * phCryptProv)
  902. {
  903. DWORD dwRetCode = ERROR_SUCCESS;
  904. *phCryptProv = NULL;
  905. if(!CryptAcquireContext( phCryptProv, // Address for handle to be returned.
  906. NULL, // Key Container Name.
  907. NULL, // Provider Name.
  908. PROV_RSA_FULL, // Need to do both encrypt & sign.
  909. 0
  910. ) )
  911. {
  912. if (!CryptAcquireContext( phCryptProv, // Address for handle to be returned.
  913. NULL, // Key Container Name.
  914. NULL, // Provider Name.
  915. PROV_RSA_FULL, // Need to do both encrypt & sign.
  916. CRYPT_VERIFYCONTEXT
  917. ) )
  918. {
  919. dwRetCode = GetLastError();
  920. }
  921. }
  922. return dwRetCode;
  923. }
  924. void CGlobal::DoneWithTempCryptContext(HCRYPTPROV hCryptProv)
  925. {
  926. if ( hCryptProv )
  927. CryptReleaseContext ( hCryptProv, 0 );
  928. }
  929. DWORD CGlobal::GetCHCert( LPTSTR lpstrRegKey , PBYTE * ppCert, DWORD * pdwLen )
  930. {
  931. DWORD dwRetCode = ERROR_SUCCESS;
  932. HKEY hKey = NULL;
  933. DWORD dwDisposition = 0;
  934. DWORD dwType = REG_BINARY;
  935. dwRetCode = ConnectToLSRegistry();
  936. if(dwRetCode != ERROR_SUCCESS)
  937. goto done;
  938. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  939. REG_LRWIZ_PARAMS,
  940. 0,
  941. NULL,
  942. REG_OPTION_NON_VOLATILE,
  943. KEY_ALL_ACCESS,
  944. NULL,
  945. &hKey,
  946. &dwDisposition);
  947. if(dwRetCode != ERROR_SUCCESS)
  948. {
  949. LRSetLastError(dwRetCode);
  950. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  951. goto done;
  952. }
  953. RegQueryValueEx(hKey,
  954. lpstrRegKey,
  955. 0,
  956. &dwType,
  957. NULL,
  958. pdwLen
  959. );
  960. if(*pdwLen == 0)
  961. {
  962. dwRetCode = IDS_ERR_CHCERTKEY_EMPTY;
  963. goto done;
  964. }
  965. *ppCert = new BYTE[*pdwLen];
  966. memset(*ppCert,0,*pdwLen);
  967. RegQueryValueEx ( hKey,
  968. lpstrRegKey,
  969. 0,
  970. &dwType,
  971. *ppCert,
  972. pdwLen
  973. );
  974. done:
  975. if (hKey != NULL)
  976. {
  977. RegCloseKey(hKey);
  978. }
  979. DisconnectLSRegistry();
  980. return dwRetCode;
  981. }
  982. DWORD CGlobal::SetCHCert ( LPTSTR lpstrRegKey, PBYTE pCert, DWORD dwLen )
  983. {
  984. DWORD dwRetCode = ERROR_SUCCESS;
  985. HKEY hKey = NULL;
  986. DWORD dwDisposition = 0;
  987. DWORD dwDecodedCertLen = 0;
  988. PBYTE pDecodedCert = NULL;
  989. /*
  990. //base 64 decode the blob
  991. LSBase64DecodeA( (const char *)pCert,
  992. dwLen,
  993. NULL,
  994. &dwDecodedCertLen);
  995. pDecodedCert = new BYTE[dwDecodedCertLen];
  996. LSBase64DecodeA( (const char *)pCert,
  997. dwLen,
  998. pDecodedCert,
  999. &dwDecodedCertLen);
  1000. */
  1001. dwRetCode = ConnectToLSRegistry();
  1002. if(dwRetCode != ERROR_SUCCESS)
  1003. goto done;
  1004. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  1005. REG_LRWIZ_PARAMS,
  1006. 0,
  1007. NULL,
  1008. REG_OPTION_NON_VOLATILE,
  1009. KEY_ALL_ACCESS,
  1010. NULL,
  1011. &hKey,
  1012. &dwDisposition);
  1013. if(dwRetCode != ERROR_SUCCESS)
  1014. {
  1015. LRSetLastError(dwRetCode);
  1016. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  1017. goto done;
  1018. }
  1019. RegSetValueEx ( hKey,
  1020. lpstrRegKey,
  1021. 0,
  1022. REG_BINARY,
  1023. pCert,
  1024. dwLen
  1025. );
  1026. done :
  1027. if(pDecodedCert)
  1028. delete pDecodedCert;
  1029. if(hKey)
  1030. RegCloseKey(hKey);
  1031. DisconnectLSRegistry();
  1032. return dwRetCode;
  1033. }
  1034. //
  1035. // This functions connects the LS Registry and stores the Reg Handle in
  1036. // in the Member variable.
  1037. //
  1038. DWORD CGlobal::ConnectToLSRegistry()
  1039. {
  1040. DWORD dwRetCode = ERROR_SUCCESS;
  1041. TCHAR szMachineName[MAX_COMPUTERNAME_LENGTH + 5];
  1042. _tcscpy(szMachineName,L"\\\\");
  1043. _tcscat(szMachineName,m_lpstrLSName);
  1044. m_hLSRegKey = NULL;
  1045. dwRetCode = RegConnectRegistry(szMachineName,HKEY_LOCAL_MACHINE,&m_hLSRegKey);
  1046. if(dwRetCode != ERROR_SUCCESS)
  1047. {
  1048. LRSetLastError(dwRetCode);
  1049. dwRetCode = IDS_ERR_REGCONNECT_FAILD;
  1050. }
  1051. return dwRetCode;
  1052. }
  1053. void CGlobal::DisconnectLSRegistry()
  1054. {
  1055. if(m_hLSRegKey)
  1056. RegCloseKey(m_hLSRegKey);
  1057. }
  1058. DWORD CGlobal::ConnectToLS()
  1059. {
  1060. DWORD dwRetCode = ERROR_SUCCESS;
  1061. error_status_t esRPC = ERROR_SUCCESS;
  1062. HCRYPTPROV hCryptProv;
  1063. m_phLSContext = TLSConnectToLsServer((LPTSTR)m_lpwstrLSName);
  1064. if (!m_phLSContext)
  1065. {
  1066. dwRetCode = IDS_ERR_LSCONNECT_FAILED;
  1067. }
  1068. else
  1069. {
  1070. GetTempCryptContext(&hCryptProv);
  1071. dwRetCode = TLSEstablishTrustWithServer(m_phLSContext, hCryptProv, CLIENT_TYPE_LRWIZ, &esRPC);
  1072. if ( dwRetCode != RPC_S_OK || esRPC != LSERVER_S_SUCCESS)
  1073. {
  1074. dwRetCode = IDS_ERR_LCONNECTTRUST_FAILED;
  1075. TLSDisconnectFromServer(m_phLSContext);
  1076. m_phLSContext = NULL;
  1077. }
  1078. DoneWithTempCryptContext(hCryptProv);
  1079. }
  1080. return dwRetCode;
  1081. }
  1082. void CGlobal::DisconnectLS()
  1083. {
  1084. if (m_phLSContext)
  1085. {
  1086. TLSDisconnectFromServer(m_phLSContext);
  1087. m_phLSContext = NULL;
  1088. }
  1089. }
  1090. //
  1091. // bstrPKCS7 is LS Client Auth Cert with BASE64 Encoding whereas
  1092. // bstrRootCert is plain X509_ASN_ENCODING
  1093. //
  1094. DWORD CGlobal::DepositLSCertificates(PBYTE pbExchangePKCS7,
  1095. DWORD dwExchangePKCS7Len,
  1096. PBYTE pbSignaturePKCS7,
  1097. DWORD dwSignaturePKCS7Len,
  1098. PBYTE pbRootCert,
  1099. DWORD dwRootCertLen)
  1100. {
  1101. //LS CA Root Certificate BLOB in X509_ASN_ENCODING & BASE 64 Encoded
  1102. PBYTE pbLSEncodedRootBLOB = pbRootCert;
  1103. DWORD dwLSEncodedRootBLOBLen = dwRootCertLen;
  1104. //LS CA Root Certificate BLOB in X509_ASN_ENCODING & BASE 64 Decoded
  1105. PBYTE pbLSDecodedRootBLOB = NULL;
  1106. DWORD dwLSDecodedRootBLOBLen = 0;
  1107. //LS Exchange Certificate BLOB(BASE64 encoded) along with LS CA Non-Root Certificate
  1108. PBYTE pbLSEncodedExchgBLOB = pbExchangePKCS7;
  1109. DWORD dwLSEncodedExchgBLOBLen = dwExchangePKCS7Len;
  1110. //LS Exchange Certificate BLOB(BASE64 decoded) along with LS CA Non-Root Certificate
  1111. PBYTE pbLSDecodedExchgBLOB = NULL;
  1112. DWORD dwLSDecodedExchgBLOBLen = 0;
  1113. //LS Signature Certificate BLOB(BASE64 encoded) along with LS CA Non-Root Certificate
  1114. PBYTE pbLSEncodedSigBLOB = pbSignaturePKCS7;
  1115. DWORD dwLSEncodedSigBLOBLen = dwSignaturePKCS7Len;
  1116. //LS Signature Certificate BLOB(BASE64 decoded) along with LS CA Non-Root Certificate
  1117. PBYTE pbLSDecodedSigBLOB = NULL;
  1118. DWORD dwLSDecodedSigBLOBLen = 0;
  1119. //Data blobs Required by CryptoAPIs
  1120. CRYPT_DATA_BLOB LSExchgCertBlob;
  1121. CRYPT_DATA_BLOB LSExchgCertStore;
  1122. CRYPT_DATA_BLOB LSSigCertBlob;
  1123. CRYPT_DATA_BLOB LSSigCertStore;
  1124. //Crypto Handles
  1125. HCRYPTPROV hCryptProv = NULL;
  1126. HCERTSTORE hExchgCertStore = NULL;
  1127. HCERTSTORE hSigCertStore = NULL;
  1128. DWORD dwRet = 0;
  1129. PCCERT_CONTEXT pCertContext = NULL;
  1130. error_status_t esRPC;
  1131. //Decode LS Exchange Cert BLOB(BASE64 Encoded)
  1132. LSBase64DecodeA((char *)pbLSEncodedExchgBLOB, dwLSEncodedExchgBLOBLen, NULL, &dwLSDecodedExchgBLOBLen);
  1133. pbLSDecodedExchgBLOB = new BYTE[dwLSDecodedExchgBLOBLen];
  1134. LSBase64DecodeA((char *)pbLSEncodedExchgBLOB, dwLSEncodedExchgBLOBLen, pbLSDecodedExchgBLOB, &dwLSDecodedExchgBLOBLen);
  1135. //Decode LS Signature Cert BLOB(BASE64 Encoded)
  1136. LSBase64DecodeA((char *)pbLSEncodedSigBLOB, dwLSEncodedSigBLOBLen, NULL, &dwLSDecodedSigBLOBLen);
  1137. pbLSDecodedSigBLOB = new BYTE[dwLSDecodedSigBLOBLen];
  1138. LSBase64DecodeA((char *)pbLSEncodedSigBLOB, dwLSEncodedSigBLOBLen, pbLSDecodedSigBLOB, &dwLSDecodedSigBLOBLen);
  1139. //Decode LS Root Cert BLOB(BASE64 Encoded)
  1140. LSBase64DecodeA((char *)pbLSEncodedRootBLOB, dwLSEncodedRootBLOBLen, NULL, &dwLSDecodedRootBLOBLen);
  1141. pbLSDecodedRootBLOB = new BYTE[dwLSDecodedRootBLOBLen];
  1142. LSBase64DecodeA((char *)pbLSEncodedRootBLOB, dwLSEncodedRootBLOBLen, pbLSDecodedRootBLOB, &dwLSDecodedRootBLOBLen);
  1143. LSExchgCertStore.cbData = 0;
  1144. LSExchgCertStore.pbData = NULL;
  1145. LSSigCertStore.cbData = 0;
  1146. LSSigCertStore.pbData = NULL;
  1147. if(!CryptAcquireContext(&hCryptProv,
  1148. NULL,
  1149. NULL,
  1150. PROV_RSA_FULL,
  1151. CRYPT_VERIFYCONTEXT ) )
  1152. {
  1153. dwRet = GetLastError();
  1154. LRSetLastError(dwRet);
  1155. dwRet = IDS_ERR_CRYPT_ERROR;
  1156. goto DepositExit;
  1157. }
  1158. //Create a new memory store for LS Exchange Certificate Chain
  1159. LSExchgCertBlob.cbData = dwLSDecodedExchgBLOBLen;
  1160. LSExchgCertBlob.pbData = pbLSDecodedExchgBLOB;
  1161. hExchgCertStore = CertOpenStore( CERT_STORE_PROV_PKCS7,
  1162. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  1163. hCryptProv,
  1164. CERT_STORE_NO_CRYPT_RELEASE_FLAG,
  1165. (void *)&LSExchgCertBlob);
  1166. if( hExchgCertStore == NULL )
  1167. {
  1168. dwRet = GetLastError();
  1169. LRSetLastError(dwRet);
  1170. dwRet = IDS_ERR_CRYPT_ERROR;
  1171. goto DepositExit;
  1172. }
  1173. //Add Root Certificate to the Store
  1174. if(!CertAddEncodedCertificateToStore( hExchgCertStore,
  1175. X509_ASN_ENCODING,
  1176. (const BYTE *)pbLSDecodedRootBLOB,
  1177. dwLSDecodedRootBLOBLen,
  1178. CERT_STORE_ADD_REPLACE_EXISTING,
  1179. &pCertContext))
  1180. {
  1181. dwRet = GetLastError();
  1182. LRSetLastError(dwRet);
  1183. dwRet = IDS_ERR_CRYPT_ERROR;
  1184. goto DepositExit;
  1185. }
  1186. //Save this store as PKCS7
  1187. //Get the Required Length
  1188. CertSaveStore( hExchgCertStore,
  1189. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1190. CERT_STORE_SAVE_AS_PKCS7,
  1191. CERT_STORE_SAVE_TO_MEMORY,
  1192. &LSExchgCertStore,
  1193. 0);
  1194. LSExchgCertStore.pbData = new BYTE[LSExchgCertStore.cbData];
  1195. //Save the Store
  1196. if(!CertSaveStore( hExchgCertStore,
  1197. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1198. CERT_STORE_SAVE_AS_PKCS7,
  1199. CERT_STORE_SAVE_TO_MEMORY,
  1200. &LSExchgCertStore,
  1201. 0)
  1202. )
  1203. {
  1204. dwRet = GetLastError();
  1205. LRSetLastError(dwRet);
  1206. dwRet = IDS_ERR_CRYPT_ERROR;
  1207. goto DepositExit;
  1208. }
  1209. /******** Do the Same thing for the Signature Certificate ********/
  1210. //Create a new memory store for LS Signature Certificate Chain
  1211. LSSigCertBlob.cbData = dwLSDecodedSigBLOBLen;
  1212. LSSigCertBlob.pbData = pbLSDecodedSigBLOB;
  1213. hSigCertStore = CertOpenStore( CERT_STORE_PROV_PKCS7,
  1214. PKCS_7_ASN_ENCODING | X509_ASN_ENCODING,
  1215. hCryptProv,
  1216. CERT_STORE_NO_CRYPT_RELEASE_FLAG,
  1217. (void *)&LSSigCertBlob);
  1218. if( hSigCertStore == NULL )
  1219. {
  1220. dwRet = GetLastError();
  1221. LRSetLastError(dwRet);
  1222. dwRet = IDS_ERR_CRYPT_ERROR;
  1223. goto DepositExit;
  1224. }
  1225. //Verify certificate
  1226. //Add Root Certificate to the Store
  1227. if(!CertAddEncodedCertificateToStore( hSigCertStore,
  1228. X509_ASN_ENCODING,
  1229. (const BYTE *)pbLSDecodedRootBLOB,
  1230. dwLSDecodedRootBLOBLen,
  1231. CERT_STORE_ADD_REPLACE_EXISTING,
  1232. &pCertContext))
  1233. {
  1234. dwRet = GetLastError();
  1235. LRSetLastError(dwRet);
  1236. dwRet = IDS_ERR_CRYPT_ERROR;
  1237. goto DepositExit;
  1238. }
  1239. //Save this store as PKCS7
  1240. //Get the Required Length
  1241. CertSaveStore( hSigCertStore,
  1242. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1243. CERT_STORE_SAVE_AS_PKCS7,
  1244. CERT_STORE_SAVE_TO_MEMORY,
  1245. &LSSigCertStore,
  1246. 0);
  1247. LSSigCertStore.pbData = new BYTE[LSSigCertStore.cbData];
  1248. //Save the Store
  1249. if(!CertSaveStore( hSigCertStore, // in
  1250. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  1251. CERT_STORE_SAVE_AS_PKCS7,
  1252. CERT_STORE_SAVE_TO_MEMORY,
  1253. &LSSigCertStore,
  1254. 0))
  1255. {
  1256. dwRet = GetLastError();
  1257. LRSetLastError(dwRet);
  1258. dwRet = IDS_ERR_CRYPT_ERROR;
  1259. goto DepositExit;
  1260. }
  1261. //Now verify the certificate chain for both exchange and
  1262. //signature certificates.
  1263. dwRet = VerifyCertChain ( hCryptProv,
  1264. hExchgCertStore,
  1265. pbLSDecodedRootBLOB,
  1266. dwLSDecodedRootBLOBLen
  1267. );
  1268. if ( dwRet != ERROR_SUCCESS )
  1269. {
  1270. LRSetLastError(dwRet);
  1271. goto DepositExit;
  1272. }
  1273. dwRet = VerifyCertChain ( hCryptProv,
  1274. hSigCertStore,
  1275. pbLSDecodedRootBLOB,
  1276. dwLSDecodedRootBLOBLen
  1277. );
  1278. if ( dwRet != ERROR_SUCCESS )
  1279. {
  1280. LRSetLastError(dwRet);
  1281. goto DepositExit;
  1282. }
  1283. //Now Send Both Signature & Exchange BLOBs to LS.
  1284. dwRet = ConnectToLS();
  1285. if(dwRet != ERROR_SUCCESS)
  1286. {
  1287. goto DepositExit;
  1288. }
  1289. dwRet = TLSInstallCertificate( m_phLSContext,
  1290. CERTIFICATE_CA_TYPE,
  1291. 1,
  1292. LSSigCertStore.cbData,
  1293. LSSigCertStore.pbData,
  1294. LSExchgCertStore.cbData,
  1295. LSExchgCertStore.pbData,
  1296. &esRPC
  1297. );
  1298. if(dwRet != RPC_S_OK)
  1299. {
  1300. LRSetLastError(dwRet);
  1301. dwRet = IDS_ERR_CERT_DEPOSIT_RPCERROR;
  1302. goto DepositExit;
  1303. }
  1304. else if ( esRPC != ERROR_SUCCESS && ( esRPC < LSERVER_I_NO_MORE_DATA || esRPC > LSERVER_I_TEMP_SELFSIGN_CERT ) )
  1305. {
  1306. dwRet = esRPC;
  1307. LRSetLastError(dwRet);
  1308. dwRet = IDS_ERR_CERT_DEPOSIT_RPCERROR; //IDS_ERR_CERT_DEPOSIT_LSERROR;
  1309. goto DepositExit;
  1310. }
  1311. DepositExit :
  1312. if(hCryptProv != NULL)
  1313. CryptReleaseContext(hCryptProv,0);
  1314. if(hExchgCertStore != NULL)
  1315. CertCloseStore(hExchgCertStore,CERT_CLOSE_STORE_FORCE_FLAG);
  1316. if(hSigCertStore != NULL)
  1317. CertCloseStore(hSigCertStore,CERT_CLOSE_STORE_FORCE_FLAG);
  1318. if(pbLSDecodedRootBLOB != NULL)
  1319. delete[] pbLSDecodedRootBLOB;
  1320. if(pbLSDecodedExchgBLOB != NULL)
  1321. delete[] pbLSDecodedExchgBLOB;
  1322. if(pbLSDecodedSigBLOB != NULL)
  1323. delete[] pbLSDecodedSigBLOB;
  1324. if(LSExchgCertStore.pbData != NULL)
  1325. delete LSExchgCertStore.pbData;
  1326. if(LSSigCertStore.pbData != NULL)
  1327. delete LSSigCertStore.pbData;
  1328. return dwRet;
  1329. }
  1330. DWORD CGlobal::GetCryptContextWithLSKeys(HCRYPTPROV * lphCryptProv )
  1331. {
  1332. DWORD dwRetVal = ERROR_SUCCESS;
  1333. DWORD esRPC = ERROR_SUCCESS;
  1334. PBYTE pbExchKey = NULL;
  1335. PBYTE pbSignKey = NULL;
  1336. DWORD cbExchKey = 0;
  1337. DWORD cbSignKey = 0;
  1338. HCRYPTKEY hSignKey;
  1339. HCRYPTKEY hExchKey;
  1340. //
  1341. //Create a new temp context
  1342. //
  1343. if (!CryptAcquireContext(lphCryptProv, LS_CRYPT_KEY_CONTAINER, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET) )
  1344. {
  1345. dwRetVal = GetLastError();
  1346. //If the key container exists , recreate it after deleting the existing one
  1347. if(dwRetVal == NTE_EXISTS)
  1348. {
  1349. // Delete
  1350. if(!CryptAcquireContext(lphCryptProv, LS_CRYPT_KEY_CONTAINER, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_DELETEKEYSET))
  1351. {
  1352. dwRetVal = GetLastError();
  1353. LRSetLastError(dwRetVal);
  1354. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1355. goto done;
  1356. }
  1357. // Recreate
  1358. if(!CryptAcquireContext(lphCryptProv, LS_CRYPT_KEY_CONTAINER, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET|CRYPT_NEWKEYSET))
  1359. {
  1360. dwRetVal = GetLastError();
  1361. LRSetLastError(dwRetVal);
  1362. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1363. goto done;
  1364. }
  1365. }
  1366. else
  1367. {
  1368. LRSetLastError(dwRetVal);
  1369. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1370. goto done;
  1371. }
  1372. }
  1373. dwRetVal = ConnectToLS();
  1374. if(dwRetVal != ERROR_SUCCESS)
  1375. {
  1376. goto done;
  1377. }
  1378. //
  1379. //Now call retrieve keys and import them
  1380. //
  1381. dwRetVal = TLSGetLSPKCS10CertRequest ( m_phLSContext,
  1382. TLSCERT_TYPE_EXCHANGE,
  1383. &cbExchKey,
  1384. &pbExchKey,
  1385. &esRPC
  1386. );
  1387. if ( dwRetVal != RPC_S_OK )
  1388. {
  1389. LRSetLastError(dwRetVal);
  1390. dwRetVal = IDS_ERR_RPC_ERROR;
  1391. goto done;
  1392. }
  1393. else if ( esRPC != ERROR_SUCCESS && esRPC != LSERVER_I_SELFSIGN_CERTIFICATE &&
  1394. esRPC != LSERVER_I_TEMP_SELFSIGN_CERT )
  1395. {
  1396. dwRetVal = esRPC;
  1397. LRSetLastError(dwRetVal);
  1398. dwRetVal = IDS_ERR_LSKEY_IMPORT_FAILED;
  1399. goto done;
  1400. }
  1401. dwRetVal = TLSGetLSPKCS10CertRequest ( m_phLSContext,
  1402. TLSCERT_TYPE_SIGNATURE,
  1403. &cbSignKey,
  1404. &pbSignKey,
  1405. &esRPC
  1406. );
  1407. if ( dwRetVal != RPC_S_OK )
  1408. {
  1409. LRSetLastError(dwRetVal);
  1410. dwRetVal = IDS_ERR_RPC_ERROR;
  1411. goto done;
  1412. }
  1413. else if ( esRPC != ERROR_SUCCESS && esRPC != LSERVER_I_SELFSIGN_CERTIFICATE &&
  1414. esRPC != LSERVER_I_TEMP_SELFSIGN_CERT )
  1415. {
  1416. dwRetVal = esRPC;
  1417. LRSetLastError(dwRetVal);
  1418. dwRetVal = IDS_ERR_LSKEY_IMPORT_FAILED;
  1419. goto done;
  1420. }
  1421. if(!CryptImportKey(*lphCryptProv, pbSignKey, cbSignKey, NULL, 0, &hSignKey))
  1422. {
  1423. dwRetVal = GetLastError();
  1424. LRSetLastError(dwRetVal);
  1425. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1426. goto done;
  1427. }
  1428. if(!CryptImportKey ( *lphCryptProv, pbExchKey, cbExchKey, NULL, 0, &hExchKey))
  1429. {
  1430. dwRetVal = GetLastError();
  1431. LRSetLastError(dwRetVal);
  1432. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1433. goto done;
  1434. }
  1435. done:
  1436. if ( pbExchKey )
  1437. LocalFree(pbExchKey);
  1438. if ( pbSignKey )
  1439. LocalFree(pbSignKey);
  1440. DisconnectLS();
  1441. return dwRetVal;
  1442. }
  1443. void CGlobal::DoneWithCryptContextWithLSKeys(HCRYPTPROV hProv)
  1444. {
  1445. if(hProv)
  1446. {
  1447. CryptReleaseContext (hProv, 0);
  1448. }
  1449. }
  1450. DWORD CGlobal::AskLSToCreatePKCS10(int nType,CHAR **lppszPKCS10)
  1451. {
  1452. DWORD dwRetVal = ERROR_SUCCESS;
  1453. CERT_RDN_ATTR *prgNameAttr = NULL;
  1454. DWORD dwErrCode;
  1455. LPBYTE pbRequest=NULL;
  1456. DWORD cbRequest=0;
  1457. DWORD cch=0;
  1458. dwRetVal = ConnectToLS();
  1459. if(dwRetVal != ERROR_SUCCESS)
  1460. {
  1461. goto cleanup;
  1462. }
  1463. prgNameAttr = CreateRDNAttr();
  1464. if(prgNameAttr == NULL)
  1465. {
  1466. dwRetVal = IDS_ERR_OUTOFMEM;
  1467. goto cleanup;
  1468. }
  1469. dwRetVal = TLSGenerateCustomerCert(m_phLSContext,
  1470. nType,
  1471. m_dwReqAttrCount,
  1472. prgNameAttr,
  1473. &cbRequest,
  1474. &pbRequest,
  1475. &dwErrCode);
  1476. if ((dwRetVal == RPC_S_OK) && (dwErrCode == ERROR_SUCCESS) && (pbRequest != NULL))
  1477. {
  1478. //
  1479. // base64 encoding
  1480. //
  1481. LSBase64EncodeA ( pbRequest, cbRequest, NULL, &cch);
  1482. *lppszPKCS10 = new CHAR [cch+1];
  1483. if(*lppszPKCS10 == NULL)
  1484. {
  1485. dwRetVal = IDS_ERR_OUTOFMEM;
  1486. goto cleanup;
  1487. }
  1488. memset ( *lppszPKCS10, 0, (cch+1)*sizeof(CHAR) );
  1489. LSBase64EncodeA ( pbRequest, cbRequest, *lppszPKCS10, &cch);
  1490. }
  1491. else
  1492. {
  1493. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1494. }
  1495. cleanup:
  1496. DisconnectLS();
  1497. if (NULL != pbRequest)
  1498. {
  1499. MIDL_user_free(pbRequest);
  1500. }
  1501. if(prgNameAttr != NULL)
  1502. delete prgNameAttr;
  1503. return dwRetVal;
  1504. }
  1505. DWORD CGlobal::CreateLSPKCS10(HCRYPTPROV hCryptProv,int nType,CHAR **lppszPKCS10)
  1506. {
  1507. DWORD dwRetVal = ERROR_SUCCESS;
  1508. CERT_SIGNED_CONTENT_INFO SignatureInfo;
  1509. CERT_REQUEST_INFO CertReqInfo;
  1510. //HCRYPTPROV hCryptProv=NULL;
  1511. CERT_EXTENSION rgExtension[MAX_NUM_EXTENSION];
  1512. int iExtCount=0;
  1513. CERT_EXTENSIONS Extensions;
  1514. CRYPT_ATTRIBUTE rgAttribute;
  1515. CRYPT_ATTR_BLOB bAttr;
  1516. CRYPT_BIT_BLOB bbKeyUsage;
  1517. CERT_POLICIES_INFO CertPolicyInfo;
  1518. CERT_POLICY_INFO CertPolicyOID;
  1519. LPBYTE pbRequest=NULL;
  1520. DWORD cbRequest=0;
  1521. DWORD cch=0;
  1522. CERT_RDN_ATTR * prgNameAttr = NULL;
  1523. // clean out the PKCS 10
  1524. memset(rgExtension, 0, sizeof(rgExtension));
  1525. memset(&Extensions, 0, sizeof(CERT_EXTENSIONS));
  1526. memset(&rgAttribute, 0, sizeof(rgAttribute));
  1527. memset(&bbKeyUsage, 0, sizeof(bbKeyUsage));
  1528. memset(&bAttr, 0, sizeof(bAttr));
  1529. memset(&SignatureInfo, 0, sizeof(SignatureInfo));
  1530. memset(&CertPolicyInfo, 0, sizeof(CERT_POLICIES_INFO));
  1531. memset(&CertPolicyOID, 0, sizeof(CERT_POLICY_INFO));
  1532. memset(&CertReqInfo, 0, sizeof(CERT_REQUEST_INFO));
  1533. CertReqInfo.dwVersion = CERT_REQUEST_V1;
  1534. PCERT_PUBLIC_KEY_INFO pPubKeyInfo=NULL;
  1535. DWORD cbPubKeyInfo=0;
  1536. do
  1537. {
  1538. //
  1539. // always strore everything in ANSI
  1540. //
  1541. prgNameAttr = CreateRDNAttr();
  1542. if(prgNameAttr == NULL)
  1543. {
  1544. dwRetVal = IDS_ERR_OUTOFMEM;
  1545. break;
  1546. }
  1547. CERT_RDN rgRDN[] = {m_dwReqAttrCount, prgNameAttr};
  1548. CERT_NAME_INFO Name = {1, rgRDN};
  1549. if(!CryptEncodeObject( CRYPT_ASN_ENCODING,
  1550. X509_NAME,
  1551. &Name,
  1552. NULL,
  1553. &CertReqInfo.Subject.cbData))
  1554. {
  1555. dwRetVal = GetLastError();
  1556. LRSetLastError(dwRetVal);
  1557. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1558. break;
  1559. }
  1560. CertReqInfo.Subject.pbData=(BYTE *) new BYTE[CertReqInfo.Subject.cbData];
  1561. if (CertReqInfo.Subject.pbData == NULL)
  1562. {
  1563. dwRetVal = IDS_ERR_OUTOFMEM;
  1564. break;
  1565. }
  1566. if(!CryptEncodeObject( CRYPT_ASN_ENCODING,
  1567. X509_NAME,
  1568. &Name,
  1569. CertReqInfo.Subject.pbData,
  1570. &CertReqInfo.Subject.cbData))
  1571. {
  1572. dwRetVal = GetLastError();
  1573. LRSetLastError(dwRetVal);
  1574. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1575. break;
  1576. }
  1577. // now get the public key out
  1578. if(!CryptExportPublicKeyInfo(hCryptProv, nType, X509_ASN_ENCODING, NULL, &cbPubKeyInfo))
  1579. {
  1580. dwRetVal = GetLastError();
  1581. LRSetLastError(dwRetVal);
  1582. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1583. break;
  1584. }
  1585. pPubKeyInfo=(PCERT_PUBLIC_KEY_INFO) new BYTE[cbPubKeyInfo];
  1586. if ( NULL == pPubKeyInfo )
  1587. {
  1588. dwRetVal = IDS_ERR_OUTOFMEM;
  1589. break;
  1590. }
  1591. if(!CryptExportPublicKeyInfo(hCryptProv, nType, X509_ASN_ENCODING, pPubKeyInfo, &cbPubKeyInfo))
  1592. {
  1593. dwRetVal = GetLastError();
  1594. LRSetLastError(dwRetVal);
  1595. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1596. break;
  1597. }
  1598. CertReqInfo.SubjectPublicKeyInfo = *pPubKeyInfo;
  1599. //no extensions here - we set them on the server side!
  1600. //sign cert request !
  1601. SignatureInfo.SignatureAlgorithm.pszObjId = szOID_OIWSEC_sha1RSASign;
  1602. memset(&SignatureInfo.SignatureAlgorithm.Parameters, 0, sizeof(SignatureInfo.SignatureAlgorithm.Parameters));
  1603. if(!CryptEncodeObject(CRYPT_ASN_ENCODING,
  1604. X509_CERT_REQUEST_TO_BE_SIGNED,
  1605. &CertReqInfo,
  1606. NULL,
  1607. &SignatureInfo.ToBeSigned.cbData))
  1608. {
  1609. dwRetVal = GetLastError();
  1610. LRSetLastError(dwRetVal);
  1611. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1612. break;
  1613. }
  1614. SignatureInfo.ToBeSigned.pbData = (LPBYTE)new BYTE [SignatureInfo.ToBeSigned.cbData];
  1615. if (NULL == SignatureInfo.ToBeSigned.pbData )
  1616. {
  1617. dwRetVal = IDS_ERR_OUTOFMEM;
  1618. break;
  1619. }
  1620. if(!CryptEncodeObject(CRYPT_ASN_ENCODING,
  1621. X509_CERT_REQUEST_TO_BE_SIGNED,
  1622. &CertReqInfo,
  1623. SignatureInfo.ToBeSigned.pbData,
  1624. &SignatureInfo.ToBeSigned.cbData))
  1625. {
  1626. dwRetVal = GetLastError();
  1627. LRSetLastError(dwRetVal);
  1628. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1629. break;
  1630. }
  1631. if(!CryptSignCertificate( hCryptProv,
  1632. nType,
  1633. CRYPT_ASN_ENCODING,
  1634. SignatureInfo.ToBeSigned.pbData,
  1635. SignatureInfo.ToBeSigned.cbData,
  1636. &SignatureInfo.SignatureAlgorithm,
  1637. NULL,
  1638. NULL,
  1639. &SignatureInfo.Signature.cbData))
  1640. {
  1641. dwRetVal = GetLastError();
  1642. LRSetLastError(dwRetVal);
  1643. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1644. break;
  1645. }
  1646. SignatureInfo.Signature.pbData = new BYTE[SignatureInfo.Signature.cbData];
  1647. if ( NULL == SignatureInfo.Signature.pbData )
  1648. {
  1649. dwRetVal = IDS_ERR_OUTOFMEM;
  1650. break;
  1651. }
  1652. if(!CryptSignCertificate( hCryptProv,
  1653. nType,
  1654. CRYPT_ASN_ENCODING,
  1655. SignatureInfo.ToBeSigned.pbData,
  1656. SignatureInfo.ToBeSigned.cbData,
  1657. &SignatureInfo.SignatureAlgorithm,
  1658. NULL,
  1659. SignatureInfo.Signature.pbData,
  1660. &SignatureInfo.Signature.cbData))
  1661. {
  1662. dwRetVal = GetLastError();
  1663. LRSetLastError(dwRetVal);
  1664. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1665. break;
  1666. }
  1667. // encode final signed request
  1668. if(!CryptEncodeObject( CRYPT_ASN_ENCODING,
  1669. X509_CERT,
  1670. &SignatureInfo,
  1671. NULL,
  1672. &cbRequest))
  1673. {
  1674. dwRetVal = GetLastError();
  1675. LRSetLastError(dwRetVal);
  1676. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1677. break;
  1678. }
  1679. pbRequest = new BYTE[cbRequest];
  1680. if ( NULL == pbRequest )
  1681. {
  1682. dwRetVal = IDS_ERR_OUTOFMEM;
  1683. break;
  1684. }
  1685. if(!CryptEncodeObject( CRYPT_ASN_ENCODING,
  1686. X509_CERT,
  1687. &SignatureInfo,
  1688. pbRequest,
  1689. &cbRequest))
  1690. {
  1691. dwRetVal = GetLastError();
  1692. LRSetLastError(dwRetVal);
  1693. dwRetVal = IDS_ERR_CRYPT_ERROR;
  1694. break;
  1695. }
  1696. //
  1697. // base64 encoding
  1698. //
  1699. LSBase64EncodeA ( pbRequest, cbRequest, NULL, &cch);
  1700. *lppszPKCS10 = new CHAR [cch+1];
  1701. if(*lppszPKCS10 == NULL)
  1702. {
  1703. dwRetVal = IDS_ERR_OUTOFMEM;
  1704. break;
  1705. }
  1706. memset ( *lppszPKCS10, 0, (cch+1)*sizeof(CHAR) );
  1707. LSBase64EncodeA ( pbRequest, cbRequest, *lppszPKCS10, &cch);
  1708. } while(FALSE);
  1709. //
  1710. // free up all
  1711. //
  1712. if(pPubKeyInfo != NULL)
  1713. delete[] pPubKeyInfo;
  1714. if(CertReqInfo.Subject.pbData != NULL)
  1715. delete CertReqInfo.Subject.pbData;
  1716. if(rgAttribute.rgValue)
  1717. delete rgAttribute.rgValue[0].pbData;
  1718. if(SignatureInfo.ToBeSigned.pbData != NULL)
  1719. delete SignatureInfo.ToBeSigned.pbData;
  1720. if(SignatureInfo.Signature.pbData != NULL)
  1721. delete SignatureInfo.Signature.pbData;
  1722. if(pbRequest != NULL)
  1723. delete[] pbRequest;
  1724. if(prgNameAttr != NULL)
  1725. delete [] prgNameAttr;
  1726. return dwRetVal;
  1727. }
  1728. DWORD CGlobal::SetDNAttribute(LPCSTR lpszOID, LPSTR lpszValue)
  1729. {
  1730. //store the item in an array here
  1731. //so that it is easy to populate the
  1732. //cert request later
  1733. //calling CreateLSPKCS10 will clear the array
  1734. DWORD dwRet = ERROR_SUCCESS;
  1735. if ( !m_pReqAttr )
  1736. {
  1737. m_pReqAttr = (PREQ_ATTR)malloc (sizeof (REQ_ATTR ) );
  1738. }
  1739. else
  1740. {
  1741. PREQ_ATTR reqAttrTmp;
  1742. reqAttrTmp = (PREQ_ATTR)realloc ( m_pReqAttr, sizeof(REQ_ATTR) * (m_dwReqAttrCount + 1));
  1743. if (NULL != reqAttrTmp)
  1744. {
  1745. m_pReqAttr = reqAttrTmp;
  1746. }
  1747. else
  1748. {
  1749. dwRet = IDS_ERR_OUTOFMEM;
  1750. goto done;
  1751. }
  1752. }
  1753. if ( !m_pReqAttr )
  1754. {
  1755. dwRet = IDS_ERR_OUTOFMEM;
  1756. goto done;
  1757. }
  1758. ( m_pReqAttr + m_dwReqAttrCount)->lpszOID = lpszOID;
  1759. ( m_pReqAttr + m_dwReqAttrCount)->lpszValue = new CHAR[lstrlenA(lpszValue) + 1];
  1760. lstrcpyA(( m_pReqAttr + m_dwReqAttrCount)->lpszValue,lpszValue);
  1761. m_dwReqAttrCount++;
  1762. done:
  1763. return dwRet;
  1764. }
  1765. DWORD CGlobal::GetFromRegistry(LPCSTR lpszOID, LPTSTR lpszBuffer, BOOL bConnect)
  1766. {
  1767. HKEY hKey = NULL;
  1768. DWORD dwDisposition;
  1769. DWORD dwRet = ERROR_SUCCESS;
  1770. DWORD dwDataLen = 0;
  1771. DWORD dwType = REG_SZ;
  1772. _tcscpy(lpszBuffer, _T(""));
  1773. if (bConnect)
  1774. {
  1775. dwRet = ConnectToLSRegistry();
  1776. if(dwRet != ERROR_SUCCESS)
  1777. {
  1778. //This is necessary because ConnectToLSRegistry actually returns the
  1779. //ID of a resource string, not the return code. Not good for exposed
  1780. //APIs that make use of this procedure and then pass the result on
  1781. dwRet = LRGetLastError();
  1782. goto done;
  1783. }
  1784. }
  1785. else
  1786. assert(m_hLSRegKey != NULL);
  1787. dwRet = RegCreateKeyEx(m_hLSRegKey, REG_LRWIZ_PARAMS, 0, NULL, REG_OPTION_NON_VOLATILE,
  1788. KEY_READ, NULL, &hKey, &dwDisposition);
  1789. if (dwRet != ERROR_SUCCESS)
  1790. {
  1791. LRSetLastError(dwRet);
  1792. goto done;
  1793. }
  1794. dwRet = RegQueryValueExA(hKey, lpszOID, 0, &dwType, NULL, &dwDataLen);
  1795. if (dwDataLen && (dwRet == ERROR_SUCCESS))
  1796. {
  1797. char* cpBuf = new char[dwDataLen + 2];
  1798. if (!cpBuf)
  1799. {
  1800. dwRet = ERROR_NOT_ENOUGH_MEMORY;
  1801. goto done;
  1802. }
  1803. memset(cpBuf, 0, dwDataLen + 2);
  1804. dwRet = RegQueryValueExA(hKey, lpszOID, 0, &dwType, (LPBYTE) cpBuf, &dwDataLen);
  1805. if (dwRet == ERROR_SUCCESS)
  1806. {
  1807. LSBase64DecodeA(cpBuf, lstrlenA(cpBuf), (PBYTE)lpszBuffer, &dwDataLen);
  1808. _tcscpy(&lpszBuffer[(dwDataLen / sizeof(TCHAR))], _T(""));
  1809. }
  1810. if (cpBuf)
  1811. delete[] cpBuf;
  1812. }
  1813. done:
  1814. if (hKey)
  1815. RegCloseKey(hKey);
  1816. if (bConnect)
  1817. DisconnectLSRegistry();
  1818. return dwRet;
  1819. }
  1820. DWORD CGlobal::SetInRegistry(LPCSTR lpszOID, LPCTSTR lpszValue)
  1821. {
  1822. HKEY hKey = NULL;
  1823. DWORD dwDisposition;
  1824. DWORD dwRet = ERROR_SUCCESS;
  1825. DWORD dwLen = 0;
  1826. char * cpOut;
  1827. dwRet = ConnectToLSRegistry();
  1828. if(dwRet != ERROR_SUCCESS)
  1829. {
  1830. goto done;
  1831. }
  1832. dwRet = RegCreateKeyEx ( m_hLSRegKey,
  1833. REG_LRWIZ_PARAMS,
  1834. 0,
  1835. NULL,
  1836. REG_OPTION_NON_VOLATILE,
  1837. KEY_WRITE,
  1838. NULL,
  1839. &hKey,
  1840. &dwDisposition);
  1841. if(dwRet != ERROR_SUCCESS)
  1842. {
  1843. LRSetLastError(dwRet);
  1844. dwRet = IDS_ERR_REGCREATE_FAILED;
  1845. goto done;
  1846. }
  1847. if (_tcslen(lpszValue) != 0)
  1848. {
  1849. LSBase64EncodeA ((PBYTE) lpszValue, _tcslen(lpszValue)*sizeof(TCHAR), NULL, &dwLen);
  1850. cpOut = new char[dwLen+1];
  1851. if (cpOut == NULL)
  1852. {
  1853. dwRet = IDS_ERR_OUTOFMEM;
  1854. goto done;
  1855. }
  1856. memset(cpOut, 0, dwLen+1);
  1857. LSBase64EncodeA ((PBYTE) lpszValue, _tcslen(lpszValue)*sizeof(TCHAR), cpOut, &dwLen);
  1858. }
  1859. else
  1860. {
  1861. cpOut = new char[2];
  1862. if (cpOut == NULL)
  1863. {
  1864. dwRet = IDS_ERR_OUTOFMEM;
  1865. goto done;
  1866. }
  1867. memset(cpOut, 0, 2);
  1868. }
  1869. RegSetValueExA ( hKey,
  1870. lpszOID,
  1871. 0,
  1872. REG_SZ,
  1873. (PBYTE) cpOut,
  1874. dwLen
  1875. );
  1876. delete[] cpOut;
  1877. done:
  1878. if (hKey != NULL)
  1879. {
  1880. RegCloseKey(hKey);
  1881. }
  1882. DisconnectLSRegistry();
  1883. return dwRet;
  1884. }
  1885. CERT_RDN_ATTR * CGlobal::CreateRDNAttr()
  1886. {
  1887. CERT_RDN_ATTR * prgNameAttr = ( CERT_RDN_ATTR * )new BYTE [sizeof ( CERT_RDN_ATTR ) * m_dwReqAttrCount];
  1888. DWORD dw = 0;
  1889. if ( !prgNameAttr )
  1890. goto done;
  1891. for ( dw = 0; dw < m_dwReqAttrCount; dw ++ )
  1892. {
  1893. ( prgNameAttr + dw )->pszObjId = (LPSTR)( m_pReqAttr + dw)->lpszOID;
  1894. ( prgNameAttr + dw )->dwValueType = CERT_RDN_PRINTABLE_STRING;
  1895. ( prgNameAttr + dw )->Value.cbData = lstrlenA(( m_pReqAttr + dw)->lpszValue);
  1896. ( prgNameAttr + dw )->Value.pbData = (PBYTE)( m_pReqAttr + dw)->lpszValue;
  1897. }
  1898. done:
  1899. return prgNameAttr;
  1900. }
  1901. TCHAR * CGlobal::GetRegistrationID(void)
  1902. {
  1903. return m_pRegistrationID;
  1904. }
  1905. TCHAR * CGlobal::GetLicenseServerID(void)
  1906. {
  1907. return m_pLicenseServerID;
  1908. }
  1909. DWORD CGlobal::GetRequestType()
  1910. {
  1911. return m_dwRequestType;
  1912. }
  1913. void CGlobal::SetRequestType(DWORD dwMode)
  1914. {
  1915. m_dwRequestType = dwMode;
  1916. }
  1917. BOOL CGlobal::IsOnlineCertRequestCreated()
  1918. {
  1919. DWORD dwRetCode = ERROR_SUCCESS;
  1920. DWORD dwLRState = 0;
  1921. DWORD dwDataLen = 0;
  1922. DWORD dwDisposition = 0;
  1923. DWORD dwType = REG_SZ;
  1924. HKEY hKey = NULL;
  1925. dwRetCode = ConnectToLSRegistry();
  1926. if(dwRetCode != ERROR_SUCCESS)
  1927. {
  1928. goto done;
  1929. }
  1930. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  1931. REG_LRWIZ_PARAMS,
  1932. 0,
  1933. NULL,
  1934. REG_OPTION_NON_VOLATILE,
  1935. KEY_ALL_ACCESS,
  1936. NULL,
  1937. &hKey,
  1938. &dwDisposition);
  1939. if(dwRetCode != ERROR_SUCCESS)
  1940. {
  1941. goto done;
  1942. }
  1943. dwLRState = 0;
  1944. dwType = REG_DWORD;
  1945. dwDataLen = sizeof(dwLRState);
  1946. RegQueryValueEx(hKey,
  1947. REG_LRWIZ_STATE,
  1948. 0,
  1949. &dwType,
  1950. (LPBYTE)&dwLRState,
  1951. &dwDataLen
  1952. );
  1953. done:
  1954. if(hKey)
  1955. RegCloseKey(hKey);
  1956. DisconnectLSRegistry();
  1957. if(dwRetCode == ERROR_SUCCESS)
  1958. return ( dwLRState == LRSTATE_ONLINE_CR_CREATED ) ? TRUE : FALSE;
  1959. else
  1960. return FALSE;
  1961. }
  1962. DWORD CGlobal::SetLRState(DWORD dwState)
  1963. {
  1964. DWORD dwRetCode = ERROR_SUCCESS;
  1965. DWORD dwDataLen = sizeof(dwState);
  1966. DWORD dwDisposition = 0;
  1967. DWORD dwType = REG_DWORD;
  1968. HKEY hKey = NULL;
  1969. dwRetCode = ConnectToLSRegistry();
  1970. if(dwRetCode != ERROR_SUCCESS)
  1971. goto done;
  1972. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  1973. REG_LRWIZ_PARAMS,
  1974. 0,
  1975. NULL,
  1976. REG_OPTION_NON_VOLATILE,
  1977. KEY_ALL_ACCESS,
  1978. NULL,
  1979. &hKey,
  1980. &dwDisposition);
  1981. if(dwRetCode != ERROR_SUCCESS)
  1982. {
  1983. LRSetLastError(dwRetCode);
  1984. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  1985. goto done;
  1986. }
  1987. //
  1988. // Persist LRCount
  1989. //
  1990. dwType = REG_DWORD;
  1991. dwDataLen = sizeof(m_dwLRCount);
  1992. RegSetValueEx ( hKey,
  1993. REG_LR_COUNT,
  1994. 0,
  1995. dwType,
  1996. (LPBYTE)&m_dwLRCount,
  1997. dwDataLen
  1998. );
  1999. //
  2000. // Persist LRState if it is not LRSTATE_OFFLINE_LR_INSTALLED
  2001. //
  2002. dwType = REG_DWORD;
  2003. dwDataLen = sizeof(dwState);
  2004. RegSetValueEx ( hKey,
  2005. REG_LRWIZ_STATE,
  2006. 0,
  2007. dwType,
  2008. (LPBYTE)&dwState,
  2009. dwDataLen
  2010. );
  2011. m_dwLRState = dwState;
  2012. done:
  2013. if(hKey)
  2014. RegCloseKey(hKey);
  2015. DisconnectLSRegistry();
  2016. return dwRetCode;
  2017. }
  2018. DWORD CGlobal::ProcessRequest()
  2019. {
  2020. DWORD dwRetCode = ERROR_SUCCESS;
  2021. //
  2022. // Before the processing the request, make sure LS is running
  2023. //
  2024. if(!IsLSRunning())
  2025. {
  2026. dwRetCode = IDS_ERR_LSCONNECT_FAILED;
  2027. goto done;
  2028. }
  2029. switch(GetActivationMethod())
  2030. {
  2031. case CONNECTION_INTERNET:
  2032. if (GetWizAction() == WIZACTION_REGISTERLS)
  2033. {
  2034. dwRetCode = ProcessIRegRequest();
  2035. }
  2036. else if (GetWizAction() == WIZACTION_CONTINUEREGISTERLS)
  2037. {
  2038. dwRetCode = ProcessCertDownload();
  2039. }
  2040. else if (GetWizAction() == WIZACTION_DOWNLOADLKP)
  2041. {
  2042. dwRetCode = ProcessDownloadLKP();
  2043. }
  2044. else if (GetWizAction() == WIZACTION_UNREGISTERLS)
  2045. {
  2046. dwRetCode = ProcessCHRevokeCert();
  2047. }
  2048. else if (GetWizAction() == WIZACTION_REREGISTERLS)
  2049. {
  2050. dwRetCode = ProcessCHReissueCert();
  2051. }
  2052. else if (GetWizAction() == WIZACTION_DOWNLOADLASTLKP)
  2053. {
  2054. dwRetCode = ProcessCHReissueLKPRequest();
  2055. }
  2056. break;
  2057. case CONNECTION_PHONE:
  2058. case CONNECTION_WWW:
  2059. if (GetWizAction() == WIZACTION_REGISTERLS ||
  2060. GetWizAction() == WIZACTION_REREGISTERLS ||
  2061. GetWizAction() == WIZACTION_CONTINUEREGISTERLS)
  2062. {
  2063. dwRetCode = DepositLSSPK();
  2064. if (dwRetCode != ERROR_SUCCESS)
  2065. {
  2066. dwRetCode = IDS_ERR_DEPOSITSPK;
  2067. }
  2068. }
  2069. else if (GetWizAction() == WIZACTION_DOWNLOADLKP)
  2070. {
  2071. dwRetCode = DepositLSLKP();
  2072. }
  2073. else if (GetWizAction() == WIZACTION_UNREGISTERLS)
  2074. {
  2075. dwRetCode = ResetLSSPK();
  2076. }
  2077. break;
  2078. }
  2079. done:
  2080. LRSetLastRetCode(dwRetCode);
  2081. return dwRetCode;
  2082. }
  2083. DWORD CGlobal::DepositLSSPK()
  2084. {
  2085. DWORD dwRetCode = ERROR_SUCCESS;
  2086. error_status_t esRPC = ERROR_SUCCESS;
  2087. CERT_EXTENSION certExtension;
  2088. CRYPT_OBJID_BLOB oidValue;
  2089. CERT_EXTENSIONS certExts;
  2090. TCHAR awBuffer[ 1024];
  2091. dwRetCode = ConnectToLS();
  2092. if(dwRetCode != ERROR_SUCCESS)
  2093. {
  2094. goto done;
  2095. }
  2096. swprintf(awBuffer, szCertEXTENSION_VALUE_FMT, L"SELECT" /* "BASIC" */);
  2097. assert(wcslen(awBuffer) < sizeof(awBuffer));
  2098. oidValue.cbData = (wcslen(awBuffer)+1)*sizeof(TCHAR);
  2099. oidValue.pbData = (unsigned char *) awBuffer;
  2100. certExtension.pszObjId = (char *) szCertEXTENSION_OID;
  2101. certExtension.fCritical = TRUE;
  2102. certExtension.Value = oidValue;
  2103. certExts.cExtension = 1;
  2104. certExts.rgExtension = &certExtension;
  2105. // We need the License Server ID
  2106. dwRetCode = TLSDepositeServerSPK( m_phLSContext,
  2107. (wcslen(m_pLSSPK) + 1)*sizeof(TCHAR),
  2108. (BYTE *) m_pLSSPK,
  2109. &certExts,
  2110. &esRPC );
  2111. if(dwRetCode != RPC_S_OK)
  2112. {
  2113. LRSetLastError(dwRetCode);
  2114. dwRetCode = IDS_ERR_RPC_FAILED;
  2115. goto done;
  2116. }
  2117. if (esRPC != LSERVER_S_SUCCESS)
  2118. {
  2119. // Some error occurred in depositing the SPK
  2120. LRSetLastError(esRPC);
  2121. dwRetCode = IDS_ERR_DEPOSITSPK;
  2122. }
  2123. else
  2124. {
  2125. // Everything suceeded
  2126. memcpy(m_pRegistrationID, m_pLSSPK, (wcslen(m_pLSSPK) + 1)*sizeof(TCHAR));
  2127. if (m_dwLRState == LRSTATE_ONLINE_CR_CREATED)
  2128. {
  2129. SetLRState(LRSTATE_NEUTRAL);
  2130. }
  2131. }
  2132. done:
  2133. DisconnectLS();
  2134. return dwRetCode;
  2135. }
  2136. DWORD CGlobal::SetLSLKP(TCHAR * tcLKP)
  2137. {
  2138. if (wcsspn(tcLKP, BASE24_CHARACTERS) != LR_REGISTRATIONID_LEN)
  2139. {
  2140. // Extraneous characters in the SPK string
  2141. return IDS_ERR_INVALIDID;
  2142. }
  2143. lstrcpy(m_pLSLKP, tcLKP);
  2144. return ERROR_SUCCESS;
  2145. }
  2146. DWORD CGlobal::SetLSSPK(TCHAR * tcLKP)
  2147. {
  2148. if (wcsspn(tcLKP, BASE24_CHARACTERS) != LR_REGISTRATIONID_LEN)
  2149. {
  2150. // Extraneous characters in the SPK string
  2151. return IDS_ERR_INVALIDLSID;
  2152. }
  2153. if (lstrcmp(m_pRegistrationID, tcLKP) == 0)
  2154. {
  2155. return IDS_DUPLICATESPK;
  2156. }
  2157. lstrcpy(m_pLSSPK, tcLKP);
  2158. return ERROR_SUCCESS;
  2159. }
  2160. DWORD CGlobal::DepositLSLKP(void)
  2161. {
  2162. DWORD dwRetCode = ERROR_SUCCESS;
  2163. error_status_t esRPC = ERROR_SUCCESS;
  2164. dwRetCode = ConnectToLS();
  2165. if(dwRetCode != ERROR_SUCCESS)
  2166. {
  2167. goto done;
  2168. }
  2169. // We need the License Server ID
  2170. dwRetCode = TLSTelephoneRegisterLKP( m_phLSContext,
  2171. (wcslen(m_pLSLKP))*sizeof(TCHAR),
  2172. (BYTE *) m_pLSLKP,
  2173. &esRPC );
  2174. if(dwRetCode != RPC_S_OK)
  2175. {
  2176. dwRetCode = IDS_ERR_RPC_FAILED;
  2177. goto done;
  2178. }
  2179. if (esRPC != LSERVER_S_SUCCESS)
  2180. {
  2181. // Some error occurred in depositing the SPK
  2182. if (esRPC == LSERVER_E_DUPLICATE)
  2183. {
  2184. dwRetCode = IDS_ERR_DUPLICATE_LKP;
  2185. }
  2186. else
  2187. {
  2188. dwRetCode = IDS_ERR_DEPOSITLKP;
  2189. LRSetLastError(esRPC);
  2190. }
  2191. }
  2192. done:
  2193. DisconnectLS();
  2194. return dwRetCode;
  2195. }
  2196. DWORD CGlobal::SetCertificatePIN(LPTSTR lpszPIN)
  2197. {
  2198. m_lpstrPIN = new TCHAR[_tcslen(lpszPIN)+1];
  2199. if(m_lpstrPIN == NULL)
  2200. return IDS_ERR_OUTOFMEM;
  2201. _tcscpy(m_lpstrPIN,lpszPIN);
  2202. return ERROR_SUCCESS;
  2203. }
  2204. void CGlobal::ClearCHRequestAttributes()
  2205. {
  2206. DWORD dwIndex = 0;
  2207. if ( m_pRegAttr )
  2208. {
  2209. for(dwIndex=0;dwIndex<m_dwRegAttrCount;dwIndex++)
  2210. {
  2211. if((m_pRegAttr + dwIndex)->lpszAttribute)
  2212. delete (m_pRegAttr + dwIndex)->lpszAttribute;
  2213. if((m_pRegAttr + dwIndex)->lpszValue)
  2214. delete (m_pRegAttr + dwIndex)->lpszValue;
  2215. }
  2216. free( m_pRegAttr );
  2217. m_pRegAttr = NULL;
  2218. }
  2219. m_dwRegAttrCount = 0;
  2220. }
  2221. void CGlobal::ClearCARequestAttributes()
  2222. {
  2223. DWORD dwIndex;
  2224. if ( m_pReqAttr )
  2225. {
  2226. for(dwIndex=0;dwIndex<m_dwReqAttrCount;dwIndex++)
  2227. {
  2228. if(( m_pReqAttr + dwIndex)->lpszValue)
  2229. delete ( m_pReqAttr + dwIndex)->lpszValue;
  2230. }
  2231. free( m_pReqAttr );
  2232. m_pReqAttr = NULL;
  2233. }
  2234. m_dwReqAttrCount = 0;
  2235. }
  2236. DWORD CGlobal::SetRegistrationAttribute ( LPWSTR lpszAttribute, LPCWSTR lpszValue, DWORD dwLen )
  2237. {
  2238. DWORD dwRet;
  2239. if ( !m_pRegAttr )
  2240. {
  2241. m_pRegAttr = (PREG_ATTR)malloc (sizeof (REG_ATTR ) );
  2242. }
  2243. else
  2244. {
  2245. PREG_ATTR regAttrTmp;
  2246. regAttrTmp = (PREG_ATTR)realloc ( m_pRegAttr, sizeof(REG_ATTR) * (m_dwRegAttrCount + 1));
  2247. if (NULL != regAttrTmp)
  2248. {
  2249. m_pRegAttr = regAttrTmp;
  2250. }
  2251. else
  2252. {
  2253. dwRet = IDS_ERR_OUTOFMEM;
  2254. goto done;
  2255. }
  2256. }
  2257. if ( !m_pRegAttr )
  2258. {
  2259. dwRet = IDS_ERR_OUTOFMEM;
  2260. goto done;
  2261. }
  2262. ( m_pRegAttr + m_dwRegAttrCount)->lpszAttribute = new WCHAR[lstrlenW(lpszAttribute) + 1];
  2263. lstrcpyW(( m_pRegAttr + m_dwRegAttrCount)->lpszAttribute,lpszAttribute );
  2264. ( m_pRegAttr + m_dwRegAttrCount)->lpszValue = new WCHAR[dwLen];
  2265. memset(( m_pRegAttr + m_dwRegAttrCount)->lpszValue,0,dwLen * sizeof(WCHAR));
  2266. memcpy(( m_pRegAttr + m_dwRegAttrCount)->lpszValue,lpszValue,dwLen * sizeof(WCHAR));
  2267. ( m_pRegAttr + m_dwRegAttrCount)->dwValueLen = dwLen * sizeof(WCHAR); //byte length
  2268. m_dwRegAttrCount++;
  2269. done:
  2270. return dwRet;
  2271. }
  2272. DWORD CGlobal::DepositLKPResponse(PBYTE pbResponseData, DWORD dwResponseLen)
  2273. {
  2274. DWORD dwRetCode = ERROR_SUCCESS;
  2275. DWORD dwLSRetCode = ERROR_SUCCESS;
  2276. LPBYTE pCHCertBlob = NULL;
  2277. DWORD dwCertBlobLen = 0;
  2278. LPBYTE pCHRootCertBlob = NULL;
  2279. DWORD dwRootCertBlobLen = 0;
  2280. LPBYTE lpDecodedKeyPackBlob = NULL;
  2281. DWORD dwDecodedKeyPackBlob = 0;
  2282. LPBYTE lpKeyPackBlob = NULL;
  2283. DWORD dwKeyPackBlobLen;
  2284. lpDecodedKeyPackBlob = lpKeyPackBlob = pbResponseData;
  2285. dwDecodedKeyPackBlob = dwKeyPackBlobLen = dwResponseLen;
  2286. /*
  2287. //Base64 decode the LKP!
  2288. LSBase64DecodeA((const char *)lpKeyPackBlob,
  2289. dwKeyPackBlobLen,
  2290. NULL,
  2291. &dwDecodedKeyPackBlob);
  2292. lpDecodedKeyPackBlob = new BYTE[dwDecodedKeyPackBlob];
  2293. if(lpDecodedKeyPackBlob == NULL)
  2294. {
  2295. dwRetCode = IDS_ERR_OUTOFMEM;
  2296. goto done;
  2297. }
  2298. LSBase64DecodeA((const char *)lpKeyPackBlob,
  2299. dwKeyPackBlobLen,
  2300. lpDecodedKeyPackBlob,
  2301. &dwDecodedKeyPackBlob);
  2302. */
  2303. //Get the CH Cert BLOB and CH Root Cert BLOB
  2304. dwRetCode = GetCHCert(REG_SIGN_CERT, &pCHCertBlob, &dwCertBlobLen );
  2305. if(dwRetCode != ERROR_SUCCESS)
  2306. goto done;
  2307. dwRetCode = GetCHCert(REG_ROOT_CERT, &pCHRootCertBlob, &dwRootCertBlobLen );
  2308. if(dwRetCode != ERROR_SUCCESS)
  2309. goto done;
  2310. dwRetCode = ConnectToLS();
  2311. if(dwRetCode != ERROR_SUCCESS)
  2312. goto done;
  2313. dwRetCode = TLSRegisterLicenseKeyPack( m_phLSContext,
  2314. pCHCertBlob,
  2315. dwCertBlobLen,
  2316. pCHRootCertBlob,
  2317. dwRootCertBlobLen,
  2318. lpDecodedKeyPackBlob,
  2319. dwDecodedKeyPackBlob,
  2320. &dwLSRetCode);
  2321. if(dwRetCode != RPC_S_OK)
  2322. {
  2323. LRSetLastError(dwRetCode);
  2324. dwRetCode = IDS_ERR_RPC_ERROR;
  2325. goto done;
  2326. }
  2327. if(dwLSRetCode != ERROR_SUCCESS )
  2328. {
  2329. LRSetLastError(dwLSRetCode);
  2330. if ( dwLSRetCode != LSERVER_E_DUPLICATE )
  2331. {
  2332. dwRetCode = IDS_ERR_DEPOSIT_LKP_FAILED;
  2333. }
  2334. else
  2335. {
  2336. dwRetCode = IDS_ERR_DUPLICATE_LKP;
  2337. }
  2338. goto done;
  2339. }
  2340. done:
  2341. /*
  2342. if ( lpDecodedKeyPackBlob )
  2343. delete lpDecodedKeyPackBlob;
  2344. */
  2345. if (pCHCertBlob != NULL)
  2346. {
  2347. delete pCHCertBlob;
  2348. }
  2349. if (pCHRootCertBlob != NULL)
  2350. {
  2351. delete pCHRootCertBlob;
  2352. }
  2353. DisconnectLS();
  2354. return dwRetCode;
  2355. }
  2356. DWORD CGlobal::EnvelopeData(
  2357. PCCERT_CONTEXT pCertContext, //Certificate context to use
  2358. DWORD cbMessage, //BLOB size
  2359. PBYTE pbMessage, //BLOB Pointer
  2360. PEnvData pEnvelopedData, //enveloped data
  2361. HCRYPTPROV hCryptProv, //crypt provider
  2362. HCERTSTORE hCertStore //certificate store
  2363. )
  2364. {
  2365. HCRYPTKEY hEncryptKey = 0, hPubKey = 0;
  2366. DWORD dwRetCode = ERROR_SUCCESS;
  2367. DWORD cbBufSize = 0;
  2368. //
  2369. //import public key data from the Certificate Context
  2370. //
  2371. if( !CryptImportPublicKeyInfoEx( hCryptProv, X509_ASN_ENCODING,
  2372. &pCertContext->pCertInfo->SubjectPublicKeyInfo,
  2373. CALG_RSA_KEYX, 0, NULL, &hPubKey ) )
  2374. {
  2375. goto ErrorReturn;
  2376. }
  2377. //
  2378. // Generate a session key to encrypt the message
  2379. //
  2380. if( !CryptGenKey( hCryptProv, CALG_RC4, CRYPT_EXPORTABLE, &hEncryptKey ) )
  2381. {
  2382. goto ErrorReturn;
  2383. }
  2384. //
  2385. // allocate enough memory to contain the encrypted data.
  2386. //
  2387. // Note:
  2388. //
  2389. // we are using the RC4 stream cipher, so the encrypted output buffer size will be the same
  2390. // as the plaintext input buffer size. If we change to block encryption algorithm,
  2391. // then we need to determine the output buffer size which may be larger than the
  2392. // input buffer size.
  2393. //
  2394. pEnvelopedData->cbEncryptedData = cbMessage;
  2395. pEnvelopedData->pbEncryptedData = (PBYTE)LocalAlloc( GPTR, pEnvelopedData->cbEncryptedData );
  2396. if( NULL == pEnvelopedData->pbEncryptedData )
  2397. {
  2398. goto ErrorReturn;
  2399. }
  2400. //
  2401. // encrypt the message with the session key
  2402. //
  2403. memcpy( pEnvelopedData->pbEncryptedData, pbMessage, cbMessage );
  2404. if( !CryptEncrypt( hEncryptKey, 0, TRUE, 0, pEnvelopedData->pbEncryptedData,
  2405. &pEnvelopedData->cbEncryptedData, cbMessage ) )
  2406. {
  2407. goto ErrorReturn;
  2408. }
  2409. //
  2410. // Determine the size of the buffer that we need to export the
  2411. // encryption key and then export the key.
  2412. // The exported encryption key is encrypted with the receipient's
  2413. // public key.
  2414. //
  2415. if( !CryptExportKey( hEncryptKey, hPubKey, SIMPLEBLOB, 0, NULL,
  2416. &pEnvelopedData->cbEncryptedKey ) )
  2417. {
  2418. goto ErrorReturn;
  2419. }
  2420. pEnvelopedData->pbEncryptedKey = (PBYTE)LocalAlloc( GPTR, pEnvelopedData->cbEncryptedKey );
  2421. if( NULL == pEnvelopedData->pbEncryptedKey )
  2422. {
  2423. goto ErrorReturn;
  2424. }
  2425. if( !CryptExportKey( hEncryptKey, hPubKey, SIMPLEBLOB, 0, pEnvelopedData->pbEncryptedKey,
  2426. &pEnvelopedData->cbEncryptedKey ) )
  2427. {
  2428. goto ErrorReturn;
  2429. }
  2430. done:
  2431. if( hPubKey )
  2432. {
  2433. CryptDestroyKey( hPubKey );
  2434. }
  2435. if( hEncryptKey )
  2436. {
  2437. CryptDestroyKey( hEncryptKey );
  2438. }
  2439. if( pCertContext )
  2440. {
  2441. CertFreeCertificateContext( pCertContext );
  2442. }
  2443. if( hCertStore )
  2444. {
  2445. CertCloseStore( hCertStore, CERT_CLOSE_STORE_FORCE_FLAG );
  2446. }
  2447. if( hCryptProv )
  2448. {
  2449. CryptReleaseContext( hCryptProv, 0 );
  2450. }
  2451. return( dwRetCode );
  2452. ErrorReturn:
  2453. dwRetCode = GetLastError();
  2454. goto done;
  2455. }
  2456. DWORD CGlobal::PackEnvData(
  2457. PEnvData pEnvelopedData,
  2458. PDWORD pcbPacked,
  2459. PBYTE *ppbPacked
  2460. )
  2461. {
  2462. DWORD dwRetCode = ERROR_SUCCESS;
  2463. PBYTE pCopyPos;
  2464. //
  2465. // determine the size of the buffer to allocate
  2466. //
  2467. *pcbPacked = pEnvelopedData->cbEncryptedData + pEnvelopedData->cbEncryptedKey +
  2468. ( sizeof( DWORD ) * 2 );
  2469. *ppbPacked = (PBYTE)LocalAlloc( GPTR, *pcbPacked );
  2470. if( NULL == ( *ppbPacked ) )
  2471. {
  2472. goto ErrorReturn;
  2473. }
  2474. pCopyPos = *ppbPacked;
  2475. memcpy( pCopyPos, &pEnvelopedData->cbEncryptedKey, sizeof( DWORD ) );
  2476. pCopyPos += sizeof( DWORD );
  2477. memcpy( pCopyPos, pEnvelopedData->pbEncryptedKey, pEnvelopedData->cbEncryptedKey );
  2478. pCopyPos += pEnvelopedData->cbEncryptedKey;
  2479. memcpy( pCopyPos, &pEnvelopedData->cbEncryptedData, sizeof( DWORD ) );
  2480. pCopyPos += sizeof( DWORD );
  2481. memcpy( pCopyPos, pEnvelopedData->pbEncryptedData, pEnvelopedData->cbEncryptedData );
  2482. done:
  2483. return( dwRetCode );
  2484. ErrorReturn:
  2485. dwRetCode = GetLastError();
  2486. goto done;
  2487. }
  2488. DWORD CGlobal::SetCARequestAttributes()
  2489. {
  2490. //Clear previous data if any
  2491. ClearCARequestAttributes();
  2492. LPSTR lpszTemp;
  2493. lpszTemp = UnicodeToAnsi((LPTSTR)(LPCTSTR)m_ContactData.sCompanyName);
  2494. if (lpszTemp != NULL)
  2495. {
  2496. SetDNAttribute(szOID_COMMON_NAME, lpszTemp);
  2497. delete lpszTemp;
  2498. }
  2499. lpszTemp = UnicodeToAnsi((LPTSTR)(LPCTSTR)m_lpstrLSName);
  2500. if (lpszTemp != NULL)
  2501. {
  2502. SetDNAttribute(szOID_SUR_NAME, lpszTemp);
  2503. delete lpszTemp;
  2504. }
  2505. return ERROR_SUCCESS;
  2506. }
  2507. DWORD CGlobal::SetCHRequestAttributes()
  2508. {
  2509. //Clear previous data if any
  2510. ClearCHRequestAttributes();
  2511. //
  2512. //Program information
  2513. //
  2514. //For these three program types we'll use the actual name
  2515. if ((m_ContactData.sProgramName == PROGRAM_LICENSE_PAK) ||
  2516. (m_ContactData.sProgramName == PROGRAM_MOLP) ||
  2517. (m_ContactData.sProgramName == PROGRAM_SELECT))
  2518. {
  2519. SetRegistrationAttribute ( _PROGRAMNAMETAG, (LPCTSTR)m_ContactData.sProgramName, m_ContactData.sProgramName.GetLength() );
  2520. }
  2521. else //for the rest, we'll send "Select"
  2522. {
  2523. SetRegistrationAttribute ( _PROGRAMNAMETAG, PROGRAM_SELECT, wcslen(PROGRAM_SELECT));
  2524. }
  2525. //
  2526. //Contact information
  2527. //
  2528. SetRegistrationAttribute ( _CONTACTLNAMETAG, (LPCTSTR)m_ContactData.sContactLName, m_ContactData.sContactLName.GetLength());
  2529. SetRegistrationAttribute ( _CONTACTFNAMETAG, (LPCTSTR)m_ContactData.sContactFName, m_ContactData.sContactFName.GetLength());
  2530. SetRegistrationAttribute ( _CONTACTADDRESSTAG, (LPCTSTR)m_ContactData.sContactAddress, m_ContactData.sContactAddress.GetLength());
  2531. SetRegistrationAttribute ( _CONTACTPHONETAG, (LPCTSTR)m_ContactData.sContactPhone, m_ContactData.sContactPhone.GetLength());
  2532. SetRegistrationAttribute ( _CONTACTFAXTAG, (LPCTSTR)m_ContactData.sContactFax, m_ContactData.sContactFax.GetLength());
  2533. SetRegistrationAttribute ( _CONTACTEMAILTAG, (LPCTSTR)m_ContactData.sContactEmail, m_ContactData.sContactEmail.GetLength());
  2534. SetRegistrationAttribute ( _CONTACTCITYTAG, (LPCTSTR)m_ContactData.sCity, m_ContactData.sCity.GetLength());
  2535. SetRegistrationAttribute ( _CONTACTCOUNTRYTAG, (LPCTSTR)m_ContactData.sCountryCode, m_ContactData.sCountryCode.GetLength());
  2536. SetRegistrationAttribute ( _CONTACTSTATE, (LPCTSTR)m_ContactData.sState, m_ContactData.sState.GetLength());
  2537. SetRegistrationAttribute ( _CONTACTZIP, (LPCTSTR)m_ContactData.sZip, m_ContactData.sZip.GetLength());
  2538. //
  2539. //customer information
  2540. //
  2541. SetRegistrationAttribute ( _CUSTOMERNAMETAG, (LPCTSTR)m_ContactData.sCompanyName, m_ContactData.sCompanyName.GetLength());
  2542. //Program related information
  2543. if ( m_ContactData.sProgramName == PROGRAM_MOLP )
  2544. {
  2545. //MOLP
  2546. SetRegistrationAttribute ( _MOLPAUTHNUMBERTAG, (LPCTSTR)m_LicData.sMOLPAuthNumber, m_LicData.sMOLPAuthNumber.GetLength());
  2547. SetRegistrationAttribute ( _MOLPAGREEMENTNUMBERTAG, (LPCTSTR)m_LicData.sMOLPAgreementNumber, m_LicData.sMOLPAgreementNumber.GetLength());
  2548. SetRegistrationAttribute ( _MOLPPRODUCTTYPETAG, (LPCTSTR)m_LicData.sMOLPProductType, m_LicData.sMOLPProductType.GetLength());
  2549. SetRegistrationAttribute ( _MOLPQTYTAG, (LPCTSTR)m_LicData.sMOLPQty, m_LicData.sMOLPQty.GetLength());
  2550. }
  2551. else if ( m_ContactData.sProgramName != PROGRAM_LICENSE_PAK )
  2552. {
  2553. //Select
  2554. SetRegistrationAttribute ( _SELMASTERAGRNUMBERTAG, (LPCTSTR)m_LicData.sSelMastAgrNumber, m_LicData.sSelMastAgrNumber.GetLength() );
  2555. SetRegistrationAttribute ( _SELENROLLNUMBERTAG, (LPCTSTR)m_LicData.sSelEnrollmentNumber, m_LicData.sSelEnrollmentNumber.GetLength());
  2556. SetRegistrationAttribute ( _SELPRODUCTTYPETAG, (LPCTSTR)m_LicData.sSelProductType, m_LicData.sSelProductType.GetLength());
  2557. SetRegistrationAttribute ( _SELQTYTAG, (LPCTSTR)m_LicData.sSelQty, m_LicData.sSelQty.GetLength());
  2558. }
  2559. //Shipping address information
  2560. //For Offline , always put the shipping address info
  2561. //for Online , no need to put the shipping address.
  2562. return ERROR_SUCCESS;
  2563. }
  2564. DWORD CGlobal::LoadCountries()
  2565. {
  2566. DWORD dwRetCode = ERROR_SUCCESS;
  2567. DWORD dwIndex = 0;
  2568. CString sCountry;
  2569. LPTSTR lpVal = NULL;
  2570. DWORD dwSize = LR_COUNTRY_DESC_LEN + LR_COUNTRY_CODE_LEN + MAX_COUNTRY_NAME_LENGTH;
  2571. LPTSTR szDelimiter = (LPTSTR)L":";
  2572. m_csaCountryDesc.SetSize(IDS_COUNTRY_END - IDS_COUNTRY_START + 1);
  2573. m_csaCountryCode.SetSize(IDS_COUNTRY_END - IDS_COUNTRY_START + 1);
  2574. for(dwIndex = IDS_COUNTRY_START;dwIndex <= IDS_COUNTRY_END;dwIndex++)
  2575. {
  2576. lpVal = sCountry.GetBuffer(dwSize);
  2577. LoadString(GetInstanceHandle(),dwIndex,lpVal,dwSize);
  2578. m_csaCountryDesc[dwIndex-IDS_COUNTRY_START] = _tcstok(lpVal,szDelimiter);
  2579. m_csaCountryCode[dwIndex-IDS_COUNTRY_START] = _tcstok(NULL,szDelimiter);
  2580. sCountry.ReleaseBuffer(-1);
  2581. }
  2582. return dwRetCode;
  2583. }
  2584. DWORD CGlobal::PopulateCountryComboBox(HWND hWndCmb)
  2585. {
  2586. DWORD dwRetCode = ERROR_SUCCESS;
  2587. DWORD dwIndex = 0;
  2588. CString sDesc;
  2589. LPTSTR lpVal = NULL;
  2590. for(dwIndex=0;dwIndex <= IDS_COUNTRY_END - IDS_COUNTRY_START;dwIndex++)
  2591. {
  2592. sDesc = m_csaCountryDesc[dwIndex];
  2593. lpVal = sDesc.GetBuffer(LR_COUNTRY_DESC_LEN + 1);
  2594. ComboBox_AddString(hWndCmb,lpVal);
  2595. sDesc.ReleaseBuffer(-1);
  2596. }
  2597. return dwRetCode;
  2598. }
  2599. DWORD CGlobal::GetCountryCode(CString sDesc,LPTSTR szCode)
  2600. {
  2601. DWORD dwRetCode = ERROR_SUCCESS;
  2602. DWORD dwIndex = 0;
  2603. for(dwIndex=0;dwIndex <= IDS_COUNTRY_END - IDS_COUNTRY_START;dwIndex++)
  2604. {
  2605. if(m_csaCountryDesc[dwIndex] == sDesc)
  2606. break;
  2607. }
  2608. if(dwIndex > IDS_COUNTRY_END - IDS_COUNTRY_START)
  2609. _tcscpy(szCode,CString("")); //Not found
  2610. else
  2611. _tcscpy(szCode,m_csaCountryCode[dwIndex]);
  2612. return dwRetCode;
  2613. }
  2614. DWORD CGlobal::GetCountryDesc(CString sCode,LPTSTR szDesc)
  2615. {
  2616. DWORD dwRetCode = ERROR_SUCCESS;
  2617. DWORD dwIndex = 0;
  2618. for(dwIndex=0;dwIndex <= IDS_COUNTRY_END - IDS_COUNTRY_START;dwIndex++)
  2619. {
  2620. if(m_csaCountryCode[dwIndex] == sCode)
  2621. break;
  2622. }
  2623. if(dwIndex > IDS_COUNTRY_END - IDS_COUNTRY_START)
  2624. _tcscpy(szDesc,CString("")); //Not found
  2625. else
  2626. _tcscpy(szDesc,m_csaCountryDesc[dwIndex]);
  2627. return dwRetCode;
  2628. }
  2629. DWORD CGlobal::LoadProducts()
  2630. {
  2631. DWORD dwRetCode = ERROR_SUCCESS;
  2632. DWORD dwIndex = 0;
  2633. CString sProduct;
  2634. LPTSTR lpVal = NULL;
  2635. DWORD dwSize = LR_PRODUCT_DESC_LEN + LR_PRODUCT_CODE_LEN;
  2636. BOOL fWin2000 = !m_fSupportConcurrent;
  2637. DWORD dwNumProducts = 0;
  2638. if ((!m_fSupportWhistlerCAL) && (!m_fSupportConcurrent))
  2639. {
  2640. dwNumProducts = IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START;
  2641. }
  2642. else if(m_fSupportConcurrent && !(m_fSupportWhistlerCAL))
  2643. {
  2644. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2645. }
  2646. else if((!m_fSupportConcurrent) && m_fSupportWhistlerCAL)
  2647. {
  2648. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2649. }
  2650. else if(m_fSupportConcurrent && m_fSupportWhistlerCAL)
  2651. {
  2652. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 2;
  2653. }
  2654. DWORD dwArray = 0;
  2655. LPTSTR szDelimiter = (LPTSTR)L":";
  2656. m_csaProductDesc.SetSize(dwNumProducts);
  2657. m_csaProductCode.SetSize(dwNumProducts);
  2658. for (dwIndex = IDS_PRODUCT_START; dwIndex < (IDS_PRODUCT_START + dwNumProducts); dwIndex++)
  2659. {
  2660. if (!m_fSupportConcurrent && (dwIndex == IDS_PRODUCT_CONCURRENT))
  2661. continue;
  2662. if (!m_fSupportWhistlerCAL && (dwIndex == IDS_PRODUCT_WHISTLER))
  2663. continue;
  2664. lpVal = sProduct.GetBuffer(dwSize);
  2665. LoadString(GetInstanceHandle(),dwIndex,lpVal,dwSize);
  2666. m_csaProductDesc[dwArray] = _tcstok(lpVal,szDelimiter);
  2667. m_csaProductCode[dwArray] = _tcstok(NULL,szDelimiter);
  2668. dwArray++;
  2669. sProduct.ReleaseBuffer(-1);
  2670. }
  2671. return dwRetCode;
  2672. }
  2673. DWORD CGlobal::PopulateProductComboBox(HWND hWndCmb, ProductVersionType VerType)
  2674. {
  2675. DWORD dwRetCode = ERROR_SUCCESS;
  2676. DWORD dwIndex = 0;
  2677. CString sDesc;
  2678. LPTSTR lpVal = NULL;
  2679. DWORD dwNumProducts = 0;
  2680. if ((!m_fSupportWhistlerCAL) && (!m_fSupportConcurrent))
  2681. {
  2682. dwNumProducts = IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START;
  2683. }
  2684. else if(m_fSupportConcurrent && !(m_fSupportWhistlerCAL))
  2685. {
  2686. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2687. }
  2688. else if((!m_fSupportConcurrent) && m_fSupportWhistlerCAL)
  2689. {
  2690. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2691. }
  2692. else if(m_fSupportConcurrent && m_fSupportWhistlerCAL)
  2693. {
  2694. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 2;
  2695. }
  2696. ComboBox_ResetContent(hWndCmb);
  2697. //We only want to show the product types for the given version, so find
  2698. //out what the product code range for the version is
  2699. int nVersionStart = 0;
  2700. int nVersionEnd = 0;
  2701. switch (VerType)
  2702. {
  2703. case PRODUCT_VERSION_W2K:
  2704. nVersionStart = IDS_PRODUCT_W2K_BEGIN;
  2705. nVersionEnd = IDS_PRODUCT_W2K_END;
  2706. break;
  2707. case PRODUCT_VERSION_WHISTLER:
  2708. nVersionStart = IDS_PRODUCT_WHISTLER_BEGIN;
  2709. nVersionEnd = IDS_PRODUCT_WHISTLER_END;
  2710. break;
  2711. default:
  2712. return dwRetCode;
  2713. break;
  2714. }
  2715. for(dwIndex=0;dwIndex < dwNumProducts ;dwIndex++)
  2716. {
  2717. if ((_wtoi(m_csaProductCode[dwIndex]) >= nVersionStart) &&
  2718. (_wtoi(m_csaProductCode[dwIndex]) <= nVersionEnd))
  2719. {
  2720. sDesc = m_csaProductDesc[dwIndex];
  2721. lpVal = sDesc.GetBuffer(LR_PRODUCT_DESC_LEN);
  2722. ComboBox_AddString(hWndCmb,lpVal);
  2723. sDesc.ReleaseBuffer(-1);
  2724. }
  2725. }
  2726. return dwRetCode;
  2727. }
  2728. DWORD CGlobal::GetProductCode(CString sDesc,LPTSTR szCode)
  2729. {
  2730. DWORD dwRetCode = ERROR_SUCCESS;
  2731. DWORD dwIndex = 0;
  2732. DWORD dwNumProducts = 0;
  2733. if ((!m_fSupportWhistlerCAL) && (!m_fSupportConcurrent))
  2734. {
  2735. dwNumProducts = IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START;
  2736. }
  2737. else if(m_fSupportConcurrent && !(m_fSupportWhistlerCAL))
  2738. {
  2739. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2740. }
  2741. else if((!m_fSupportConcurrent) && m_fSupportWhistlerCAL)
  2742. {
  2743. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  2744. }
  2745. else if(m_fSupportConcurrent && m_fSupportWhistlerCAL)
  2746. {
  2747. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 2;
  2748. }
  2749. for(dwIndex=0;dwIndex < dwNumProducts;dwIndex++)
  2750. {
  2751. if(m_csaProductDesc[dwIndex] == sDesc)
  2752. break;
  2753. }
  2754. if(dwIndex >= dwNumProducts )
  2755. _tcscpy(szCode,CString("")); //Not found
  2756. else
  2757. _tcscpy(szCode,m_csaProductCode[dwIndex]);
  2758. return dwRetCode;
  2759. }
  2760. //Load all react and deact reasons
  2761. DWORD CGlobal::LoadReasons()
  2762. {
  2763. DWORD dwRetCode = ERROR_SUCCESS;
  2764. DWORD dwIndex = 0;
  2765. CString sReason;
  2766. LPTSTR lpVal = NULL;
  2767. DWORD dwSize = LR_REASON_CODE_LEN+LR_REASON_DESC_LEN+128;
  2768. LPTSTR szDelimiter = (LPTSTR)L":";
  2769. m_csaReactReasonDesc.SetSize( IDS_REACT_REASONS_END - IDS_REACT_REASONS_START + 1);
  2770. m_csaReactReasonCode.SetSize( IDS_REACT_REASONS_END - IDS_REACT_REASONS_START + 1);
  2771. m_csaDeactReasonDesc.SetSize(IDS_DEACT_REASONS_END - IDS_DEACT_REASONS_START + 1);
  2772. m_csaDeactReasonCode.SetSize(IDS_DEACT_REASONS_END - IDS_DEACT_REASONS_START + 1);
  2773. //load the reacr
  2774. for(dwIndex = IDS_REACT_REASONS_START;dwIndex <= IDS_REACT_REASONS_END; dwIndex++)
  2775. {
  2776. lpVal = sReason.GetBuffer(dwSize);
  2777. LoadString(GetInstanceHandle(),dwIndex,lpVal,dwSize);
  2778. m_csaReactReasonDesc[dwIndex-IDS_REACT_REASONS_START] = _tcstok(lpVal,szDelimiter);
  2779. m_csaReactReasonCode[dwIndex-IDS_REACT_REASONS_START] = _tcstok(NULL,szDelimiter);
  2780. sReason.ReleaseBuffer(-1);
  2781. }
  2782. for ( dwIndex = IDS_DEACT_REASONS_START; dwIndex <= IDS_DEACT_REASONS_END; dwIndex ++ )
  2783. {
  2784. lpVal = sReason.GetBuffer(dwSize);
  2785. LoadString(GetInstanceHandle(),dwIndex,lpVal,dwSize);
  2786. m_csaDeactReasonDesc[dwIndex-IDS_DEACT_REASONS_START] = _tcstok(lpVal,szDelimiter);
  2787. m_csaDeactReasonCode[dwIndex-IDS_DEACT_REASONS_START] = _tcstok(NULL,szDelimiter);
  2788. sReason.ReleaseBuffer(-1);
  2789. }
  2790. return dwRetCode;
  2791. }
  2792. DWORD CGlobal::PopulateReasonComboBox(HWND hWndCmb, DWORD dwType)
  2793. {
  2794. DWORD dwRetCode = ERROR_SUCCESS;
  2795. DWORD dwIndex = 0;
  2796. CString sDesc;
  2797. LPTSTR lpVal = NULL;
  2798. DWORD dwNumItems = 0;
  2799. //If combo box is already populated,just return success
  2800. if(ComboBox_GetCount(hWndCmb) > 0)
  2801. return dwRetCode;
  2802. ComboBox_ResetContent( hWndCmb);
  2803. if ( dwType == CODE_TYPE_REACT )
  2804. {
  2805. dwNumItems = IDS_REACT_REASONS_END - IDS_REACT_REASONS_START ;
  2806. }
  2807. else if ( dwType == CODE_TYPE_DEACT )
  2808. {
  2809. dwNumItems = IDS_DEACT_REASONS_END - IDS_DEACT_REASONS_START ;
  2810. }
  2811. else
  2812. {
  2813. dwRetCode = ERROR_INVALID_PARAMETER;
  2814. goto done;
  2815. }
  2816. for(dwIndex=0;dwIndex <= dwNumItems;dwIndex++)
  2817. {
  2818. if ( dwType == CODE_TYPE_REACT )
  2819. {
  2820. sDesc = m_csaReactReasonDesc[dwIndex];
  2821. }
  2822. else if ( dwType == CODE_TYPE_DEACT )
  2823. {
  2824. sDesc = m_csaDeactReasonDesc[dwIndex];
  2825. }
  2826. lpVal = sDesc.GetBuffer(LR_REASON_DESC_LEN);
  2827. ComboBox_AddString(hWndCmb,lpVal);
  2828. sDesc.ReleaseBuffer(-1);
  2829. }
  2830. done:
  2831. return dwRetCode;
  2832. }
  2833. DWORD CGlobal::GetReasonCode(CString sDesc,LPTSTR szCode, DWORD dwType)
  2834. {
  2835. DWORD dwRetCode = ERROR_SUCCESS;
  2836. DWORD dwIndex = 0;
  2837. DWORD dwNumItems = 0;
  2838. if ( dwType == CODE_TYPE_REACT )
  2839. {
  2840. dwNumItems = IDS_REACT_REASONS_END - IDS_REACT_REASONS_START ;
  2841. }
  2842. else if ( dwType == CODE_TYPE_DEACT )
  2843. {
  2844. dwNumItems = IDS_DEACT_REASONS_END - IDS_DEACT_REASONS_START ;
  2845. }
  2846. for(dwIndex=0;dwIndex <= dwNumItems;dwIndex++)
  2847. {
  2848. if ( dwType == CODE_TYPE_REACT )
  2849. {
  2850. if ( m_csaReactReasonDesc[dwIndex] == sDesc )
  2851. break;
  2852. }
  2853. else if ( dwType == CODE_TYPE_DEACT )
  2854. {
  2855. if ( m_csaDeactReasonDesc[dwIndex] == sDesc )
  2856. break;
  2857. }
  2858. }
  2859. if(dwIndex > dwNumItems)
  2860. _tcscpy(szCode,CString("")); //Not found
  2861. else
  2862. {
  2863. if ( dwType == CODE_TYPE_REACT )
  2864. {
  2865. _tcscpy(szCode,m_csaReactReasonCode[dwIndex]);
  2866. }
  2867. else if ( dwType == CODE_TYPE_DEACT )
  2868. {
  2869. _tcscpy(szCode,m_csaDeactReasonCode[dwIndex]);
  2870. }
  2871. }
  2872. return dwRetCode;
  2873. }
  2874. DWORD CGlobal::GetReasonDesc(CString sCode,LPTSTR szDesc, DWORD dwType)
  2875. {
  2876. DWORD dwRetCode = ERROR_SUCCESS;
  2877. DWORD dwIndex = 0;
  2878. DWORD dwNumItems = 0;
  2879. if ( dwType == CODE_TYPE_REACT )
  2880. {
  2881. dwNumItems = IDS_REACT_REASONS_END - IDS_REACT_REASONS_START ;
  2882. }
  2883. else if ( dwType == CODE_TYPE_DEACT )
  2884. {
  2885. dwNumItems = IDS_DEACT_REASONS_END - IDS_DEACT_REASONS_START ;
  2886. }
  2887. for(dwIndex=0;dwIndex <= dwNumItems;dwIndex++)
  2888. {
  2889. if ( dwType == CODE_TYPE_REACT )
  2890. {
  2891. if ( m_csaReactReasonCode[dwIndex] == sCode )
  2892. break;
  2893. }
  2894. else if ( dwType == CODE_TYPE_DEACT )
  2895. {
  2896. if ( m_csaDeactReasonCode[dwIndex] == sCode )
  2897. break;
  2898. }
  2899. }
  2900. if(dwIndex > dwNumItems)
  2901. _tcscpy(szDesc,CString("")); //Not found
  2902. else
  2903. {
  2904. if ( dwType == CODE_TYPE_REACT )
  2905. {
  2906. _tcscpy(szDesc,m_csaReactReasonDesc[dwIndex]);
  2907. }
  2908. else if ( dwType == CODE_TYPE_DEACT )
  2909. {
  2910. _tcscpy(szDesc,m_csaDeactReasonDesc[dwIndex]);
  2911. }
  2912. }
  2913. return dwRetCode;
  2914. }
  2915. void CGlobal::ReadPhoneNumberFromRegistry(LPCTSTR lpCountry, LPTSTR lpPhoneNumber, DWORD nBufferSize)
  2916. {
  2917. HKEY hKey = NULL;
  2918. DWORD dwDisposition = 0;
  2919. // Try to open the required registry key
  2920. if (ConnectToLSRegistry() == ERROR_SUCCESS)
  2921. {
  2922. DWORD dwRetCode = ERROR_SUCCESS;
  2923. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  2924. REG_LRWIZ_CSNUMBERS,
  2925. 0,
  2926. NULL,
  2927. REG_OPTION_NON_VOLATILE,
  2928. KEY_ALL_ACCESS,
  2929. NULL,
  2930. &hKey,
  2931. &dwDisposition);
  2932. if (dwRetCode == ERROR_SUCCESS)
  2933. {
  2934. DWORD dwIndex = 0;
  2935. DWORD dwType = REG_SZ;
  2936. RegQueryValueEx(hKey, lpCountry, NULL, &dwType, (LPBYTE)lpPhoneNumber, &nBufferSize);
  2937. }
  2938. else
  2939. LRSetLastError(dwRetCode);
  2940. if (hKey)
  2941. RegCloseKey(hKey);
  2942. DisconnectLSRegistry();
  2943. }
  2944. }
  2945. DWORD CGlobal::CheckRegistryForPhoneNumbers()
  2946. {
  2947. DWORD dwRetCode = ERROR_SUCCESS;
  2948. DWORD dwIndex = 0;
  2949. HKEY hKey = NULL;
  2950. DWORD dwDisposition = 0;
  2951. DWORD dwType = REG_SZ;
  2952. DWORD dwValName;
  2953. DWORD dwCS_Number;
  2954. TCHAR lpValueName[MAX_COUNTRY_NAME_LENGTH + 1];
  2955. TCHAR lpCS_Number[MAX_COUNTRY_NUMBER_LENGTH + 1];
  2956. //
  2957. // Try to open the required registry key
  2958. //
  2959. dwRetCode = ConnectToLSRegistry();
  2960. if (dwRetCode != ERROR_SUCCESS)
  2961. {
  2962. goto done;
  2963. }
  2964. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  2965. REG_LRWIZ_CSNUMBERS,
  2966. 0,
  2967. NULL,
  2968. REG_OPTION_NON_VOLATILE,
  2969. KEY_ALL_ACCESS,
  2970. NULL,
  2971. &hKey,
  2972. &dwDisposition);
  2973. if (dwRetCode != ERROR_SUCCESS)
  2974. {
  2975. LRSetLastError(dwRetCode);
  2976. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  2977. goto done;
  2978. }
  2979. do {
  2980. dwValName = sizeof(lpValueName)/sizeof(TCHAR);
  2981. dwCS_Number = sizeof(lpCS_Number);
  2982. dwRetCode = RegEnumValue(hKey,
  2983. dwIndex,
  2984. lpValueName,
  2985. &dwValName,
  2986. NULL,
  2987. &dwType,
  2988. (BYTE *) lpCS_Number,
  2989. &dwCS_Number);
  2990. if (dwRetCode == ERROR_NO_MORE_ITEMS)
  2991. {
  2992. dwRetCode = ERROR_SUCCESS;
  2993. break;
  2994. }
  2995. else if (dwRetCode != ERROR_SUCCESS )
  2996. {
  2997. break;
  2998. }
  2999. if(dwType != REG_SZ)
  3000. continue;
  3001. dwIndex++;
  3002. } while (1);
  3003. if (dwIndex <= 0)
  3004. {
  3005. dwRetCode = IDS_ERR_REGERROR;
  3006. }
  3007. done:
  3008. if (hKey)
  3009. {
  3010. RegCloseKey(hKey);
  3011. }
  3012. DisconnectLSRegistry();
  3013. return dwRetCode;
  3014. }
  3015. DWORD CGlobal::PopulateCountryRegionComboBox(HWND hWndCmb)
  3016. {
  3017. DWORD dwRetCode = ERROR_SUCCESS;
  3018. DWORD dwIndex = 0;
  3019. HKEY hKey = NULL;
  3020. DWORD dwDisposition = 0;
  3021. DWORD dwType = REG_SZ;
  3022. DWORD dwValName;
  3023. DWORD dwCS_Number;
  3024. LVITEM lvItem;
  3025. DWORD nItem;
  3026. TCHAR lpValueName[MAX_COUNTRY_NAME_LENGTH + 1];
  3027. TCHAR lpCS_Number[MAX_COUNTRY_NUMBER_LENGTH + 1];
  3028. //
  3029. // Get CSR Numbers from the Reqgistry
  3030. //
  3031. dwRetCode = ConnectToLSRegistry();
  3032. if (dwRetCode != ERROR_SUCCESS)
  3033. {
  3034. goto done;
  3035. }
  3036. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  3037. REG_LRWIZ_CSNUMBERS,
  3038. 0,
  3039. NULL,
  3040. REG_OPTION_NON_VOLATILE,
  3041. KEY_ALL_ACCESS,
  3042. NULL,
  3043. &hKey,
  3044. &dwDisposition);
  3045. if (dwRetCode != ERROR_SUCCESS)
  3046. {
  3047. LRSetLastError(dwRetCode);
  3048. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  3049. goto done;
  3050. }
  3051. //Clear the List first
  3052. ComboBox_ResetContent(hWndCmb);
  3053. do {
  3054. dwValName = sizeof(lpValueName)/sizeof(TCHAR);
  3055. dwCS_Number = sizeof(lpCS_Number);
  3056. dwRetCode = RegEnumValue(hKey,
  3057. dwIndex,
  3058. lpValueName,
  3059. &dwValName,
  3060. NULL,
  3061. &dwType,
  3062. (BYTE *) lpCS_Number,
  3063. &dwCS_Number);
  3064. if (dwRetCode == ERROR_NO_MORE_ITEMS)
  3065. {
  3066. dwRetCode = ERROR_SUCCESS;
  3067. break;
  3068. }
  3069. else if (dwRetCode != ERROR_SUCCESS )
  3070. {
  3071. break;
  3072. }
  3073. dwIndex++;
  3074. if(dwType != REG_SZ)
  3075. continue;
  3076. //lvItem.mask = LVIF_TEXT;
  3077. //lvItem.iItem = 0;
  3078. //lvItem.iSubItem = 0;
  3079. //lvItem.pszText = lpValueName;
  3080. //lvItem.cchTextMax = lstrlen(lpValueName);
  3081. //nItem = ListView_InsertItem(hWndLst, &lvItem);
  3082. //lvItem.iSubItem = 1;
  3083. //lvItem.iItem = nItem;
  3084. //lvItem.pszText = lpCS_Number;
  3085. //lvItem.cchTextMax = lstrlen(lpCS_Number);
  3086. //ListView_SetItem(hWndLst, &lvItem);
  3087. int nItem = CB_ERR;
  3088. nItem = ComboBox_AddString(hWndCmb, lpValueName);
  3089. ComboBox_SetItemData(hWndCmb, nItem, lpCS_Number);
  3090. } while (1);
  3091. if (dwIndex <= 0)
  3092. {
  3093. dwRetCode = IDS_ERR_REGERROR;
  3094. }
  3095. done:
  3096. if (hKey)
  3097. {
  3098. RegCloseKey(hKey);
  3099. }
  3100. DisconnectLSRegistry();
  3101. return dwRetCode;
  3102. }
  3103. DWORD CGlobal::PopulateCountryRegionListBox(HWND hWndLst)
  3104. {
  3105. DWORD dwRetCode = ERROR_SUCCESS;
  3106. DWORD dwIndex = 0;
  3107. HKEY hKey = NULL;
  3108. DWORD dwDisposition = 0;
  3109. DWORD dwType = REG_SZ;
  3110. DWORD dwValName;
  3111. DWORD dwCS_Number;
  3112. LVITEM lvItem;
  3113. DWORD nItem;
  3114. TCHAR lpValueName[MAX_COUNTRY_NAME_LENGTH + 1];
  3115. TCHAR lpCS_Number[MAX_COUNTRY_NUMBER_LENGTH + 1];
  3116. //
  3117. // Get CSR Numbers from the Reqgistry
  3118. //
  3119. dwRetCode = ConnectToLSRegistry();
  3120. if (dwRetCode != ERROR_SUCCESS)
  3121. {
  3122. goto done;
  3123. }
  3124. dwRetCode = RegCreateKeyEx (m_hLSRegKey,
  3125. REG_LRWIZ_CSNUMBERS,
  3126. 0,
  3127. NULL,
  3128. REG_OPTION_NON_VOLATILE,
  3129. KEY_ALL_ACCESS,
  3130. NULL,
  3131. &hKey,
  3132. &dwDisposition);
  3133. if (dwRetCode != ERROR_SUCCESS)
  3134. {
  3135. LRSetLastError(dwRetCode);
  3136. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  3137. goto done;
  3138. }
  3139. //Clear the List first
  3140. ListView_DeleteAllItems(hWndLst);
  3141. do {
  3142. dwValName = sizeof(lpValueName)/sizeof(TCHAR);
  3143. dwCS_Number = sizeof(lpCS_Number);
  3144. dwRetCode = RegEnumValue(hKey,
  3145. dwIndex,
  3146. lpValueName,
  3147. &dwValName,
  3148. NULL,
  3149. &dwType,
  3150. (BYTE *) lpCS_Number,
  3151. &dwCS_Number);
  3152. if (dwRetCode == ERROR_NO_MORE_ITEMS)
  3153. {
  3154. dwRetCode = ERROR_SUCCESS;
  3155. break;
  3156. }
  3157. else if (dwRetCode != ERROR_SUCCESS )
  3158. {
  3159. break;
  3160. }
  3161. dwIndex++;
  3162. if(dwType != REG_SZ)
  3163. continue;
  3164. lvItem.mask = LVIF_TEXT;
  3165. lvItem.iItem = 0;
  3166. lvItem.iSubItem = 0;
  3167. lvItem.pszText = lpValueName;
  3168. lvItem.cchTextMax = lstrlen(lpValueName);
  3169. nItem = ListView_InsertItem(hWndLst, &lvItem);
  3170. lvItem.iSubItem = 1;
  3171. lvItem.iItem = nItem;
  3172. lvItem.pszText = lpCS_Number;
  3173. lvItem.cchTextMax = lstrlen(lpCS_Number);
  3174. ListView_SetItem(hWndLst, &lvItem);
  3175. // ComboBox_AddString(hWndCmb,lpVal);
  3176. } while (1);
  3177. if (dwIndex <= 0)
  3178. {
  3179. dwRetCode = IDS_ERR_REGERROR;
  3180. }
  3181. done:
  3182. if (hKey)
  3183. {
  3184. RegCloseKey(hKey);
  3185. }
  3186. DisconnectLSRegistry();
  3187. return dwRetCode;
  3188. }
  3189. void CGlobal::LRSetLastRetCode(DWORD dwCode)
  3190. {
  3191. m_dwLastRetCode = dwCode;
  3192. }
  3193. DWORD CGlobal::LRGetLastRetCode()
  3194. {
  3195. return m_dwLastRetCode;
  3196. }
  3197. LPWSTR CGlobal::AnsiToUnicode ( LPSTR lpszBuf )
  3198. {
  3199. LPWSTR lpwszRetBuf = NULL;
  3200. long lBufLen = ::lstrlenA(lpszBuf) + 1;
  3201. lpwszRetBuf = new WCHAR[ lBufLen ];
  3202. memset ( lpwszRetBuf, 0, lBufLen * sizeof(TCHAR));
  3203. MultiByteToWideChar ( GetACP(),
  3204. MB_PRECOMPOSED,
  3205. lpszBuf,
  3206. -1,
  3207. lpwszRetBuf,
  3208. lBufLen
  3209. );
  3210. return lpwszRetBuf;
  3211. }
  3212. LPSTR CGlobal::UnicodeToAnsi( LPWSTR lpwszBuf, DWORD dwLength )
  3213. {
  3214. LPSTR lpszRetBuf = NULL;
  3215. DWORD nBufferSize;
  3216. // We need to determine the buffer length first, if we pass in 0 for buffer
  3217. // size it will return the required buffer size in bytes (including null)
  3218. nBufferSize = WideCharToMultiByte(GetACP(),
  3219. 0,
  3220. lpwszBuf,
  3221. dwLength,
  3222. NULL,
  3223. 0,
  3224. NULL,
  3225. NULL);
  3226. if (nBufferSize > 0)
  3227. {
  3228. // Add one to the buffer size for null termination
  3229. nBufferSize++;
  3230. lpszRetBuf = new char[nBufferSize];
  3231. if (lpszRetBuf == NULL)
  3232. return NULL;
  3233. memset(lpszRetBuf, 0, nBufferSize);
  3234. WideCharToMultiByte(GetACP(), // code page
  3235. 0, // performance and mapping flags
  3236. lpwszBuf, // address of wide-character string
  3237. dwLength, // number of characters in string
  3238. lpszRetBuf, // address of buffer for new string
  3239. nBufferSize, // size of buffer in bytes
  3240. NULL, // address of default for unmappable characters
  3241. NULL // address of flag set when default char. used
  3242. );
  3243. }
  3244. return lpszRetBuf;
  3245. }
  3246. LPSTR CGlobal::UnicodeToAnsi(LPWSTR lpwszBuf)
  3247. {
  3248. long lBufLen = ::lstrlenW(lpwszBuf);
  3249. return UnicodeToAnsi(lpwszBuf, lBufLen);
  3250. }
  3251. void CGlobal::LRPush(DWORD dwPageId)
  3252. {
  3253. assert(m_dwTop < NO_OF_PAGES - 1);
  3254. m_dwWizStack[m_dwTop++] = dwPageId;
  3255. }
  3256. DWORD CGlobal::LRPop()
  3257. {
  3258. assert(m_dwTop > 0);
  3259. return m_dwWizStack[--m_dwTop];
  3260. }
  3261. BOOL CGlobal::ValidateEmailId(CString sEmailId)
  3262. {
  3263. BOOL bValid = FALSE;
  3264. int dwLen = 0;
  3265. dwLen = sEmailId.GetLength();
  3266. do
  3267. {
  3268. // Check the length
  3269. if(dwLen < EMAIL_MIN_LEN)
  3270. break;
  3271. // Make sure it does not have spaces
  3272. if(sEmailId.Find(EMAIL_SPACE_CHAR) != -1)
  3273. break;
  3274. // Make sure it has '@' & '.' in it
  3275. if(sEmailId.Find(EMAIL_AT_CHAR) == -1 || sEmailId.Find(EMAIL_DOT_CHAR) == -1)
  3276. break;
  3277. // Make sure first char is not either EMAIL_AT_CHAR or EMAIL_DOT_CHAR
  3278. if(sEmailId[0] == EMAIL_AT_CHAR || sEmailId[0] == EMAIL_DOT_CHAR)
  3279. break;
  3280. // Make sure last char is not either EMAIL_AT_CHAR or EMAIL_DOT_CHAR
  3281. if(sEmailId[dwLen-1] == EMAIL_AT_CHAR || sEmailId[dwLen-1] == EMAIL_DOT_CHAR)
  3282. break;
  3283. // EMAIL_AT_CHAR should come only once
  3284. if(sEmailId.Find(EMAIL_AT_CHAR) != sEmailId.ReverseFind(EMAIL_AT_CHAR))
  3285. break;
  3286. //It should not have these string "@." or ".@"
  3287. if(sEmailId.Find(EMAIL_AT_DOT_STR) != -1 || sEmailId.Find(EMAIL_DOT_AT_STR) != -1)
  3288. break;
  3289. bValid = TRUE;
  3290. }
  3291. while(FALSE);
  3292. return bValid;
  3293. }
  3294. BOOL CGlobal::CheckProgramValidity (CString sProgramName )
  3295. {
  3296. BOOL bRetCode = FALSE;
  3297. // if(sProgramName == PROGRAM_SELECT)
  3298. // {
  3299. // //select
  3300. // if(strstr ( (const char *)m_pbExtensionValue+3, CA_CERT_TYPE_SELECT ) )
  3301. // {
  3302. // bRetCode = TRUE;
  3303. // }
  3304. // }
  3305. // else if ( sProgramName == PROGRAM_MOLP || sProgramName == PROGRAM_RETAIL )
  3306. // {
  3307. //retail or MOLP
  3308. // if (strstr ( (const char *)m_pbExtensionValue+3, CA_CERT_TYPE_SELECT ) ||
  3309. // strstr ( (const char *)m_pbExtensionValue+3, CA_CERT_TYPE_OTHER )
  3310. // )
  3311. // {
  3312. bRetCode = TRUE;
  3313. // }
  3314. // }
  3315. //
  3316. return bRetCode;
  3317. }
  3318. //
  3319. // This function searches for Single quote (') and replaces it will two single quotes ('')
  3320. // This is because , SQL server gives error if the string contains single quote
  3321. //
  3322. void CGlobal::PrepareLRString(CString &sStr)
  3323. {
  3324. CString sTemp;
  3325. int nIndex = 0;
  3326. for(nIndex=0;nIndex < sStr.GetLength();nIndex++)
  3327. {
  3328. if(sStr[nIndex] == LR_SINGLE_QUOTE)
  3329. {
  3330. sTemp += LR_SINGLE_QUOTE;
  3331. sTemp += LR_SINGLE_QUOTE;
  3332. }
  3333. else
  3334. sTemp += sStr[nIndex];
  3335. }
  3336. sStr = sTemp;
  3337. }
  3338. //
  3339. // This functions checks for any invalid chars in the string
  3340. //
  3341. BOOL CGlobal::ValidateLRString(CString sStr)
  3342. {
  3343. CString sInvalidChars = LR_INVALID_CHARS;
  3344. if(sStr.FindOneOf(sInvalidChars) != -1)
  3345. return FALSE;
  3346. else
  3347. return TRUE;
  3348. }
  3349. //
  3350. //Validate the certificate chain for a given store. This is an overkill but
  3351. //will make it more robust!
  3352. //
  3353. DWORD CGlobal::VerifyCertChain ( HCRYPTPROV hCryptProvider, //handle to crypt prov
  3354. HCERTSTORE hCertStore, //HAndle to store for verification
  3355. PBYTE pbRootCert, //Root cert
  3356. DWORD dwcbRootCert
  3357. )
  3358. {
  3359. DWORD dwRetVal = ERROR_SUCCESS;
  3360. PCCERT_CONTEXT pRootCertContext = NULL;
  3361. PCCERT_CONTEXT pCertContext = NULL;
  3362. PCCERT_CONTEXT pIssuerCertContext = NULL;
  3363. DWORD dwFlags = CERT_STORE_SIGNATURE_FLAG;
  3364. if ( NULL == hCryptProvider || NULL == hCertStore || NULL == pbRootCert || dwcbRootCert <= 0 )
  3365. {
  3366. LRSetLastError (ERROR_INVALID_PARAMETER);
  3367. dwRetVal = IDS_ERR_CRYPT_ERROR;
  3368. goto done;
  3369. }
  3370. //Create a Root certificate context
  3371. pRootCertContext = CertCreateCertificateContext ( X509_ASN_ENCODING,
  3372. pbRootCert,
  3373. dwcbRootCert
  3374. );
  3375. if ( pRootCertContext == NULL )
  3376. {
  3377. LRSetLastError (GetLastError());
  3378. dwRetVal = IDS_ERR_CRYPT_ERROR;
  3379. goto done;
  3380. }
  3381. //check to see if there is a certificate with our extension in the store. Then use that as the
  3382. //starting point
  3383. dwRetVal = GetCertforExtension (hCryptProvider, hCertStore, szOID_NULL_EXT, &pCertContext);
  3384. if ( dwRetVal != ERROR_SUCCESS )
  3385. {
  3386. LRSetLastError(dwRetVal );
  3387. dwRetVal = IDS_ERR_CRYPT_ERROR;
  3388. goto done;
  3389. }
  3390. //Walk the chain here
  3391. do
  3392. {
  3393. pIssuerCertContext = CertGetIssuerCertificateFromStore( hCertStore,
  3394. pCertContext,
  3395. NULL, // pIssuerCertContext,
  3396. &dwFlags );
  3397. if ( pIssuerCertContext )
  3398. {
  3399. //check to see the result.
  3400. if ( dwFlags & CERT_STORE_SIGNATURE_FLAG )
  3401. {
  3402. LRSetLastError(GetLastError());
  3403. dwRetVal = IDS_ERR_INVALID_CERT_CHAIN;
  3404. break;
  3405. }
  3406. dwFlags = CERT_STORE_SIGNATURE_FLAG;
  3407. CertFreeCertificateContext (pCertContext);
  3408. pCertContext = pIssuerCertContext;
  3409. }
  3410. } while ( pIssuerCertContext );
  3411. if ( dwRetVal != ERROR_SUCCESS )
  3412. {
  3413. goto done;
  3414. }
  3415. //Verify the last issuer against the root passed in
  3416. dwFlags = CERT_STORE_REVOCATION_FLAG | CERT_STORE_SIGNATURE_FLAG | CERT_STORE_TIME_VALIDITY_FLAG;
  3417. if ( !CertVerifySubjectCertificateContext( pCertContext, pRootCertContext, &dwFlags ) )
  3418. {
  3419. dwRetVal = GetLastError();
  3420. goto done;
  3421. }
  3422. //check to see the result.
  3423. if ( dwFlags & CERT_STORE_SIGNATURE_FLAG )
  3424. {
  3425. LRSetLastError(GetLastError());
  3426. dwRetVal = IDS_ERR_INVALID_CERT_CHAIN;
  3427. }
  3428. done:
  3429. if (pCertContext != NULL)
  3430. {
  3431. CertFreeCertificateContext(pCertContext);
  3432. }
  3433. if (pRootCertContext != NULL)
  3434. {
  3435. CertFreeCertificateContext(pRootCertContext);
  3436. }
  3437. return dwRetVal;
  3438. }
  3439. DWORD CGlobal::GetCertforExtension (HCRYPTPROV hCryptProv, HCERTSTORE hCertStore, LPSTR szOID_EXT, PCCERT_CONTEXT * ppCertContext)
  3440. {
  3441. DWORD dwRetVal = ERROR_SUCCESS;
  3442. PCCERT_CONTEXT pCurrentContext = NULL;
  3443. PCCERT_CONTEXT pPrevContext = NULL;
  3444. PCERT_EXTENSION pCertExtension = NULL;
  3445. if ( hCryptProv == NULL || hCertStore == NULL || ppCertContext == NULL )
  3446. {
  3447. dwRetVal = ERROR_INVALID_PARAMETER;
  3448. goto done;
  3449. }
  3450. *ppCertContext = NULL;
  3451. do
  3452. {
  3453. //Get the cert context
  3454. pCurrentContext = CertEnumCertificatesInStore ( hCertStore, pPrevContext );
  3455. if ( pCurrentContext )
  3456. {
  3457. //Check to see if the Extension is present in the cert context
  3458. pCertExtension = CertFindExtension ( szOID_NULL_EXT,
  3459. pCurrentContext->pCertInfo->cExtension,
  3460. pCurrentContext->pCertInfo->rgExtension
  3461. );
  3462. if ( pCertExtension )
  3463. {
  3464. *ppCertContext = pCurrentContext;
  3465. goto done;
  3466. }
  3467. pPrevContext = pCurrentContext;
  3468. }
  3469. } while ( pCurrentContext );
  3470. dwRetVal = CRYPT_E_NOT_FOUND; //CErt not found
  3471. done:
  3472. // if ( pPrevContext )
  3473. // {
  3474. // CertFreeCertificateContext (pPrevContext);
  3475. // }
  3476. if ( !*ppCertContext && pCurrentContext )
  3477. {
  3478. CertFreeCertificateContext (pCurrentContext);
  3479. }
  3480. return dwRetVal;
  3481. }
  3482. DWORD CGlobal::FetchResponse(BYTE * bpResponse,
  3483. DWORD dwMaxLength,
  3484. PDWORD dwpDataLength)
  3485. {
  3486. DWORD dwCHRC = ERROR_SUCCESS;
  3487. DWORD dwBytesRead = 0;
  3488. BOOL bRC = FALSE;
  3489. BYTE * bpCurrent = bpResponse;
  3490. *dwpDataLength = 0;
  3491. assert(m_hOpenDirect != NULL);
  3492. assert(m_hRequest != NULL);
  3493. assert(m_hConnect != NULL);
  3494. while ( dwMaxLength > 0 && (bRC = InternetReadFile ( m_hRequest,
  3495. bpResponse,
  3496. dwMaxLength,
  3497. &dwBytesRead )) && dwBytesRead)
  3498. {
  3499. dwMaxLength -= dwBytesRead;
  3500. (*dwpDataLength) += dwBytesRead;
  3501. }
  3502. if (!bRC)
  3503. {
  3504. // dwCHRC = GetLastError();
  3505. dwCHRC = IDS_ERR_CHFETCHRESPONSE;
  3506. }
  3507. return dwCHRC;
  3508. }
  3509. DWORD CGlobal::InitCHRequest(void)
  3510. {
  3511. DWORD dwRetCode = ERROR_SUCCESS;
  3512. const char *pszAcceptedTypes[] = {"*/*",NULL};
  3513. LPSTR lpszCHName = UnicodeToAnsi((LPTSTR)(LPCTSTR) m_lpstrCHServer);
  3514. LPSTR lpszExtension = UnicodeToAnsi((LPTSTR)(LPCTSTR) m_lpstrCHExtension);
  3515. assert(m_hOpenDirect == NULL);
  3516. assert(m_hConnect == NULL);
  3517. assert(m_hRequest == NULL);
  3518. //m_hOpenDirect = InternetOpenA ( "LRWizDLL", NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, 0 ); //Bug # 526
  3519. m_hOpenDirect = InternetOpenA ( "LRWizDLL", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
  3520. if (m_hOpenDirect == NULL)
  3521. {
  3522. dwRetCode = GetLastError();
  3523. goto done;
  3524. }
  3525. m_hConnect = InternetConnectA (m_hOpenDirect,
  3526. lpszCHName,
  3527. INTERNET_DEFAULT_HTTPS_PORT ,
  3528. NULL,
  3529. NULL,
  3530. INTERNET_SERVICE_HTTP,
  3531. 0,
  3532. 0) ;
  3533. if ( !m_hConnect )
  3534. {
  3535. dwRetCode = GetLastError();
  3536. goto done;
  3537. }
  3538. m_hRequest = HttpOpenRequestA ( m_hConnect,
  3539. "POST",
  3540. lpszExtension,
  3541. "HTTP/1.0",
  3542. NULL,
  3543. pszAcceptedTypes,
  3544. INTERNET_FLAG_SECURE |
  3545. INTERNET_FLAG_IGNORE_CERT_CN_INVALID,
  3546. NULL ); //(DWORD)this);
  3547. if ( !m_hRequest )
  3548. {
  3549. dwRetCode = GetLastError();
  3550. goto done;
  3551. }
  3552. done:
  3553. if (lpszCHName)
  3554. {
  3555. delete lpszCHName;
  3556. }
  3557. if (lpszExtension)
  3558. {
  3559. delete lpszExtension;
  3560. }
  3561. if (dwRetCode != ERROR_SUCCESS)
  3562. {
  3563. CloseCHRequest();
  3564. }
  3565. return dwRetCode;
  3566. }
  3567. DWORD CGlobal::CloseCHRequest(void)
  3568. {
  3569. if (m_hRequest)
  3570. {
  3571. InternetCloseHandle(m_hRequest);
  3572. m_hRequest = NULL;
  3573. }
  3574. if (m_hConnect)
  3575. {
  3576. InternetCloseHandle(m_hConnect);
  3577. m_hConnect = NULL;
  3578. }
  3579. if (m_hOpenDirect)
  3580. {
  3581. InternetCloseHandle(m_hOpenDirect);
  3582. m_hOpenDirect = NULL;
  3583. }
  3584. return ERROR_SUCCESS;
  3585. }
  3586. DWORD CGlobal::Dispatch(BYTE * bpData,
  3587. DWORD dwLen)
  3588. {
  3589. DWORD dwCHRC = ERROR_SUCCESS;;
  3590. DWORD dwPostStatus = 0;
  3591. DWORD dwPostStatusSize = sizeof(dwPostStatus);
  3592. DWORD dwFlags;
  3593. DWORD dwBufLen = sizeof(dwFlags);
  3594. #ifdef IGNORE_EXPIRATION
  3595. LONG lRet;
  3596. HKEY hKey = NULL;
  3597. #endif
  3598. assert(m_hOpenDirect != NULL);
  3599. assert(m_hRequest != NULL);
  3600. assert(m_hConnect != NULL);
  3601. try
  3602. {
  3603. BOOL bRC = TRUE;
  3604. char szContentType[] = "Content-Type: application/octet-stream\r\n";
  3605. if ( !HttpAddRequestHeadersA ( m_hRequest, szContentType, -1L, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE ) )
  3606. {
  3607. DWORD dwError = GetLastError();
  3608. }
  3609. bRC = HttpSendRequestA( m_hRequest,
  3610. NULL,
  3611. 0,
  3612. bpData, //binary data
  3613. dwLen //length of the data
  3614. );
  3615. if (!bRC)
  3616. {
  3617. DWORD dwRetCode = GetLastError();
  3618. if(dwRetCode == ERROR_INTERNET_INVALID_CA || dwRetCode == ERROR_INTERNET_SEC_CERT_DATE_INVALID
  3619. || dwRetCode == ERROR_INTERNET_SEC_CERT_CN_INVALID)
  3620. {
  3621. InternetQueryOptionA(m_hRequest, INTERNET_OPTION_SECURITY_FLAGS,
  3622. (LPVOID) &dwFlags, &dwBufLen);
  3623. #ifdef IGNORE_EXPIRATION
  3624. lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  3625. LICENSE_EXPIRATION_IGNORE ,
  3626. 0,
  3627. KEY_READ ,
  3628. &hKey );
  3629. if( ERROR_SUCCESS == lRet )
  3630. {
  3631. dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
  3632. dwFlags |= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID;
  3633. dwFlags |= SECURITY_FLAG_IGNORE_CERT_CN_INVALID;
  3634. dwFlags |= SECURITY_FLAG_IGNORE_REVOCATION;
  3635. InternetSetOptionA(m_hRequest, INTERNET_OPTION_SECURITY_FLAGS,
  3636. (LPVOID) &dwFlags, sizeof(dwFlags));
  3637. bRC = HttpSendRequestA( m_hRequest,
  3638. NULL,
  3639. 0,
  3640. bpData, //binary data
  3641. dwLen); //length of the data
  3642. }
  3643. else
  3644. {
  3645. #endif
  3646. if(dwRetCode == ERROR_INTERNET_INVALID_CA)
  3647. {
  3648. dwFlags |=SECURITY_FLAG_IGNORE_UNKNOWN_CA;
  3649. InternetSetOptionA(m_hRequest, INTERNET_OPTION_SECURITY_FLAGS,
  3650. (LPVOID) &dwFlags, sizeof(dwFlags));
  3651. bRC = HttpSendRequestA( m_hRequest,
  3652. NULL,
  3653. 0,
  3654. bpData, //binary data
  3655. dwLen); //length of the data
  3656. }
  3657. #ifdef IGNORE_EXPIRATION
  3658. }
  3659. #endif
  3660. }
  3661. #ifdef IGNORE_EXPIRATION
  3662. if(hKey)
  3663. {
  3664. RegCloseKey(hKey);
  3665. }
  3666. #endif
  3667. }
  3668. if (bRC)
  3669. {
  3670. if ( HttpQueryInfoA( m_hRequest, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, (LPVOID)&dwPostStatus, &dwPostStatusSize, NULL ) )
  3671. {
  3672. if ( dwPostStatus != 200 )
  3673. {
  3674. dwCHRC = CH_ERROR_HTTPQRY_FAILED;
  3675. throw dwCHRC;
  3676. }
  3677. }
  3678. else
  3679. {
  3680. dwCHRC = GetLastError();
  3681. throw dwCHRC;
  3682. }
  3683. }
  3684. else
  3685. {
  3686. dwCHRC = GetLastError();
  3687. dwCHRC = CH_ERROR_SEND_FAILED;
  3688. throw dwCHRC;
  3689. }
  3690. }
  3691. catch (DWORD dwRC)
  3692. {
  3693. dwCHRC = dwRC;
  3694. }
  3695. catch (...)
  3696. {
  3697. assert("Exception in Dispatch() !");
  3698. dwCHRC = CH_ERROR_EXCEPTION;
  3699. }
  3700. if (dwCHRC != ERROR_SUCCESS)
  3701. {
  3702. if (!CheckIfICWCompleted())
  3703. dwCHRC = IDS_SEND_FAILED_NO_CONN;
  3704. else
  3705. {
  3706. if (!(GetSystemMetrics(SM_NETWORK) & RNC_NETWORKS))
  3707. dwCHRC = IDS_SEND_FAILED_NO_NIC;
  3708. else
  3709. dwCHRC = IDS_SEND_FAILED_NO_OTHER;
  3710. }
  3711. }
  3712. return dwCHRC;
  3713. }
  3714. // Check if ICW has already been run
  3715. BOOL CGlobal::CheckIfICWCompleted()
  3716. {
  3717. DWORD dwICWCompleted = FALSE;
  3718. DWORD dwSize = sizeof(dwICWCompleted);
  3719. HKEY hConnectionKey = NULL;
  3720. if (RegOpenKeyEx(HKEY_CURRENT_USER, REG_ICW_STATUS_PATH, 0, KEY_READ, &hConnectionKey) == ERROR_SUCCESS)
  3721. {
  3722. DWORD type;
  3723. RegQueryValueEx(hConnectionKey, REG_ICW_COMPLETED, NULL , &type, (BYTE*)&dwICWCompleted, &dwSize);
  3724. }
  3725. RegCloseKey(hConnectionKey);
  3726. return dwICWCompleted;
  3727. }
  3728. DWORD CGlobal::PingCH(void)
  3729. {
  3730. DWORD dwRetCode = ERROR_SUCCESS;
  3731. Ping_Request pingData;
  3732. Ping_Response pingResp;
  3733. BYTE bResponse[ 1024];
  3734. DWORD dwDataLength;
  3735. //
  3736. // Set Language Id
  3737. //
  3738. pingData.RequestHeader.SetLanguageId(GetLSLangId());
  3739. dwRetCode = InitCHRequest();
  3740. if (dwRetCode != ERROR_SUCCESS)
  3741. {
  3742. goto done;
  3743. }
  3744. dwRetCode = Dispatch((BYTE *) &pingData, sizeof(Ping_Request));
  3745. if (dwRetCode != ERROR_SUCCESS)
  3746. {
  3747. LRSetLastError(dwRetCode);
  3748. goto done;
  3749. }
  3750. dwRetCode = FetchResponse(bResponse, sizeof(bResponse), &dwDataLength);
  3751. if (dwRetCode != ERROR_SUCCESS)
  3752. {
  3753. goto done;
  3754. }
  3755. // Now let us ensure that we are getting the expected byte stream back
  3756. // AND if we are, we are good to go.
  3757. memcpy(&pingResp, bResponse, sizeof(Ping_Response));
  3758. if (lstrcmp(pingResp.tszPingResponse, L"Beam'er up Scottie!") != 0)
  3759. {
  3760. // Expected Response
  3761. dwRetCode = IDS_ERR_CHBAD_DATA;
  3762. goto done;
  3763. }
  3764. done:
  3765. CloseCHRequest();
  3766. return dwRetCode;
  3767. }
  3768. DWORD CGlobal::ProcessIRegRequest()
  3769. {
  3770. DWORD dwRetCode = ERROR_SUCCESS;
  3771. NewCert_Request certRequest;
  3772. NewCert_Response certResponse;
  3773. Certificate_AckRequest certackRequest;
  3774. Certificate_AckResponse certackResponse;
  3775. HCRYPTPROV hCryptProv = NULL;
  3776. LPWSTR lpwszExchgPKCS10 = NULL;
  3777. LPWSTR lpwszSignPKCS10 = NULL;
  3778. LPBYTE lpszReqData = NULL;
  3779. LPBYTE lpszNextCopyPos = NULL;
  3780. LPSTR lpszExchgPKCS10 = NULL;
  3781. LPSTR lpszSigPKCS10 = NULL;
  3782. DWORD dwExchangeLen = 0;
  3783. DWORD dwSignLen = 0;
  3784. DWORD dwResponseLength = 0;
  3785. PBYTE lpszResData = NULL;
  3786. DWORD dwExchgCertLen = 0;
  3787. DWORD dwSigCertLen = 0;
  3788. DWORD dwRootCertLen = 0;
  3789. LPSTR lpszExchCert = NULL;
  3790. LPSTR lpszSignCert = NULL;
  3791. LPSTR lpszRootCert = NULL;
  3792. bool bToSendAck = false;
  3793. //
  3794. // Set the LangId
  3795. //
  3796. certRequest.RequestHeader.SetLanguageId(GetLSLangId());
  3797. dwRetCode = InitCHRequest();
  3798. if (dwRetCode != ERROR_SUCCESS)
  3799. {
  3800. return dwRetCode;
  3801. }
  3802. try
  3803. {
  3804. swprintf(certRequest.stCertInfo.OrgName, L"%.*s", CA_CUSTMER_NAME_LEN, (LPCTSTR) m_ContactData.sCompanyName);
  3805. swprintf(certRequest.stCertInfo.OrgUnit, L"%.*s", CA_ORG_UNIT_LEN, (LPCTSTR) m_ContactData.sOrgUnit);
  3806. swprintf(certRequest.stCertInfo.Address, L"%.*s", CA_ADDRESS_LEN, (LPCTSTR) m_ContactData.sContactAddress);
  3807. swprintf(certRequest.stCertInfo.City, L"%.*s", CA_CITY_LEN, (LPCTSTR) m_ContactData.sCity);
  3808. swprintf(certRequest.stCertInfo.State, L"%.*s", CA_STATE_LEN, (LPCTSTR) m_ContactData.sState);
  3809. swprintf(certRequest.stCertInfo.Country, L"%.*s", CA_COUNTRY_LEN, (LPCTSTR) m_ContactData.sCountryCode);
  3810. swprintf(certRequest.stCertInfo.Zip, L"%.*s", CA_ZIP_LEN, (LPCTSTR) m_ContactData.sZip);
  3811. swprintf(certRequest.stCertInfo.LName, L"%.*s", CA_NAME_LEN, (LPCTSTR) m_ContactData.sContactLName);
  3812. swprintf(certRequest.stCertInfo.FName, L"%.*s", CA_NAME_LEN, (LPCTSTR) m_ContactData.sContactFName);
  3813. swprintf(certRequest.stCertInfo.Phone, L"%.*s", CA_PHONE_LEN, (LPCTSTR) m_ContactData.sContactPhone);
  3814. swprintf(certRequest.stCertInfo.Fax, L"%.*s", CA_FAX_LEN, (LPCTSTR) m_ContactData.sContactFax);
  3815. swprintf(certRequest.stCertInfo.Email, L"%.*s", CA_EMAIL_LEN, (LPCTSTR) m_ContactData.sContactEmail);
  3816. swprintf(certRequest.stCertInfo.LSID, L"%.*s", CA_LSERVERID_LEN, (LPCTSTR) m_pLicenseServerID );
  3817. swprintf(certRequest.stCertInfo.ProgramName, L"%.*s", 63,
  3818. (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_SELECT ? L"SELECT" : L"BASIC"));
  3819. //
  3820. // GetGlobalContext()->GetContactDataObject()->sCertType is not set anywhere but is passed
  3821. // to the back end as part of the PKCS10 Request.Not sure what it is used for in the back end
  3822. // Anyway set it to proper value here. Arvind 06/28/99.
  3823. //
  3824. if(GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_SELECT)
  3825. GetGlobalContext()->GetContactDataObject()->sCertType = CA_CERT_TYPE_SELECT;
  3826. else
  3827. GetGlobalContext()->GetContactDataObject()->sCertType = CA_CERT_TYPE_OTHER;
  3828. do
  3829. {
  3830. //
  3831. //Set the attributes required for creating PKCS10
  3832. //
  3833. SetCARequestAttributes();
  3834. //
  3835. // This function will call the CryptAcquireContext and import
  3836. // the LS Keys
  3837. //
  3838. if ((dwRetCode = GetCryptContextWithLSKeys(&hCryptProv)) != ERROR_SUCCESS)
  3839. {
  3840. //
  3841. // Newer LSs don't support this call - use alternate
  3842. //
  3843. dwRetCode = AskLSToCreatePKCS10(AT_KEYEXCHANGE, &lpszExchgPKCS10);
  3844. if(dwRetCode != ERROR_SUCCESS)
  3845. break;
  3846. dwRetCode = AskLSToCreatePKCS10(AT_SIGNATURE, &lpszSigPKCS10);
  3847. if(dwRetCode != ERROR_SUCCESS)
  3848. break;
  3849. }
  3850. else
  3851. {
  3852. dwRetCode = CreateLSPKCS10(hCryptProv,AT_KEYEXCHANGE, &lpszExchgPKCS10);
  3853. if(dwRetCode != ERROR_SUCCESS)
  3854. break;
  3855. dwRetCode = CreateLSPKCS10(hCryptProv,AT_SIGNATURE, &lpszSigPKCS10);
  3856. if(dwRetCode != ERROR_SUCCESS)
  3857. break;
  3858. // Release the context
  3859. if(hCryptProv)
  3860. {
  3861. DoneWithCryptContextWithLSKeys ( hCryptProv );
  3862. }
  3863. }
  3864. //
  3865. //Certificate Type
  3866. //
  3867. //Convert from multibyte to unicode
  3868. lpwszExchgPKCS10 = AnsiToUnicode(lpszExchgPKCS10);
  3869. lpwszSignPKCS10 = AnsiToUnicode(lpszSigPKCS10);
  3870. dwExchangeLen = lstrlen(lpwszExchgPKCS10) * sizeof(WCHAR);
  3871. dwSignLen = lstrlen(lpwszSignPKCS10) * sizeof(WCHAR);
  3872. certRequest.SetExchgPKCS10Length(dwExchangeLen);
  3873. certRequest.SetSignPKCS10Length(dwSignLen);
  3874. certRequest.SetDataLen(dwExchangeLen+dwSignLen);
  3875. certRequest.SetServerName(m_lpstrLSName);
  3876. //Allocate buffer for the request
  3877. lpszReqData = (LPBYTE) LocalAlloc( GPTR, dwExchangeLen+dwSignLen+sizeof(certRequest) );
  3878. if(lpszReqData == NULL)
  3879. {
  3880. dwRetCode = IDS_ERR_OUTOFMEM;
  3881. break;
  3882. }
  3883. lpszNextCopyPos = lpszReqData;
  3884. memcpy(lpszNextCopyPos, &certRequest, sizeof(certRequest));
  3885. lpszNextCopyPos += sizeof(certRequest);
  3886. memcpy ( lpszNextCopyPos, lpwszExchgPKCS10, dwExchangeLen);
  3887. lpszNextCopyPos += dwExchangeLen;
  3888. memcpy ( lpszNextCopyPos, lpwszSignPKCS10, dwSignLen);
  3889. dwRetCode = Dispatch(lpszReqData, dwExchangeLen+dwSignLen+sizeof(certRequest));
  3890. if ( lpszReqData )
  3891. {
  3892. LocalFree(lpszReqData);
  3893. }
  3894. if (dwRetCode != ERROR_SUCCESS)
  3895. {
  3896. LRSetLastError(dwRetCode);
  3897. break;
  3898. }
  3899. dwRetCode = FetchResponse((BYTE *) &certResponse, sizeof(NewCert_Response), &dwResponseLength);
  3900. if (dwRetCode != ERROR_SUCCESS)
  3901. {
  3902. break;
  3903. }
  3904. if (dwResponseLength != sizeof(NewCert_Response))
  3905. {
  3906. // Got an invalid response back
  3907. dwRetCode = IDS_ERR_CHBAD_DATA;
  3908. break;
  3909. }
  3910. switch(certResponse.RequestHeader.GetResponseType())
  3911. {
  3912. case Response_Success:
  3913. dwRetCode = ERROR_SUCCESS;
  3914. break;
  3915. case Response_Failure:
  3916. dwRetCode = IDS_ERR_CHFAILURE;
  3917. break;
  3918. case Response_InvalidData:
  3919. dwRetCode = IDS_ERR_CHINVALID_DATA;
  3920. break;
  3921. case Response_NotYetImplemented:
  3922. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  3923. break;
  3924. case Response_ServerError:
  3925. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  3926. break;
  3927. case Response_Invalid_Response:
  3928. default:
  3929. dwRetCode = IDS_ERR_CHBAD_DATA;
  3930. break;
  3931. }
  3932. if (dwRetCode != ERROR_SUCCESS)
  3933. {
  3934. break;
  3935. }
  3936. //dwRetCode = SetLRState(LRSTATE_ONLINE_CR_CREATED);
  3937. // OK, allocate enough memory to read the rest fo the data
  3938. lpszResData = (PBYTE) LocalAlloc(GPTR, certResponse.GetDataLen() + 1);
  3939. if(lpszResData == NULL)
  3940. {
  3941. dwRetCode = IDS_ERR_OUTOFMEM;
  3942. break;
  3943. }
  3944. dwRetCode = FetchResponse(lpszResData, certResponse.GetDataLen() + 1,
  3945. &dwResponseLength);
  3946. if (dwRetCode != ERROR_SUCCESS)
  3947. {
  3948. break;
  3949. }
  3950. if (dwResponseLength != certResponse.GetDataLen())
  3951. {
  3952. // Didn't get the expected number of Bytes, also a problem
  3953. dwRetCode = IDS_ERR_CHBAD_DATA;
  3954. break;
  3955. }
  3956. bToSendAck = true;
  3957. dwExchgCertLen = certResponse.GetExchgPKCS7Length();
  3958. dwSigCertLen = certResponse.GetSignPKCS7Length();
  3959. dwRootCertLen = certResponse.GetRootCertLength();
  3960. if(dwRootCertLen == 0 || dwExchgCertLen == 0 || dwSigCertLen == 0 )
  3961. {
  3962. dwRetCode = IDS_ERR_INVALID_PIN;
  3963. break;
  3964. }
  3965. //
  3966. // Exchange Certificate
  3967. //
  3968. lpszExchCert = UnicodeToAnsi((LPWSTR)lpszResData, dwExchgCertLen/sizeof(WCHAR));
  3969. if ( lpszExchCert == NULL )
  3970. {
  3971. dwRetCode = IDS_ERR_OUTOFMEM;
  3972. break;
  3973. }
  3974. //
  3975. // Signature Certificate
  3976. //
  3977. lpszSignCert = UnicodeToAnsi((LPWSTR)(lpszResData + dwExchgCertLen), dwSigCertLen/sizeof(WCHAR));
  3978. if(lpszSignCert == NULL)
  3979. {
  3980. dwRetCode = IDS_ERR_OUTOFMEM;
  3981. break;
  3982. }
  3983. //
  3984. // Root Certificate
  3985. //
  3986. lpszRootCert = UnicodeToAnsi ((LPWSTR)(lpszResData+dwExchgCertLen+dwSigCertLen),
  3987. dwRootCertLen/sizeof(WCHAR));
  3988. if(lpszRootCert == NULL)
  3989. {
  3990. dwRetCode = IDS_ERR_OUTOFMEM;
  3991. break;
  3992. }
  3993. dwRetCode = SetLSSPK(certResponse.GetSPK());
  3994. if (dwRetCode != ERROR_SUCCESS)
  3995. {
  3996. break;
  3997. }
  3998. dwRetCode = DepositLSSPK();
  3999. if (dwRetCode != ERROR_SUCCESS)
  4000. {
  4001. if (dwRetCode == IDS_ERR_DEPOSITSPK)
  4002. {
  4003. dwRetCode = IDS_ERR_CERT_DEPOSIT_LSERROR;
  4004. }
  4005. break;
  4006. }
  4007. //
  4008. //Deposit the Certs
  4009. //
  4010. dwRetCode = DepositLSCertificates( (PBYTE)lpszExchCert,
  4011. lstrlenA(lpszExchCert),
  4012. (PBYTE)lpszSignCert,
  4013. lstrlenA(lpszSignCert),
  4014. (PBYTE)lpszRootCert,
  4015. lstrlenA(lpszRootCert)
  4016. );
  4017. if ( dwRetCode != ERROR_SUCCESS )
  4018. {
  4019. // If this happened and the SPK deposit succeeded, we have an
  4020. // inconsistent state, now
  4021. DWORD dwReturn;
  4022. DWORD dwOriginal = LRGetLastError();
  4023. dwReturn = ResetLSSPK(FALSE);
  4024. if (dwReturn != ERROR_SUCCESS)
  4025. {
  4026. // what to do, if even this failed. OUCH OUCH
  4027. dwRetCode = dwReturn;
  4028. }
  4029. LRSetLastError(dwOriginal);
  4030. m_pRegistrationID[ 0] = 0;
  4031. break;
  4032. }
  4033. else
  4034. {
  4035. dwRetCode = SetLRState(LRSTATE_NEUTRAL);
  4036. }
  4037. /*
  4038. lpszResData = (PBYTE) LocalAlloc(GPTR, certResponse.GetDataLen() + 1);
  4039. if(lpszResData == NULL)
  4040. {
  4041. dwRetCode = IDS_ERR_OUTOFMEM;
  4042. break;
  4043. }
  4044. dwRetCode = FetchResponse(lpszResData, certResponse.GetDataLen() + 1,
  4045. &dwResponseLength);
  4046. if (dwRetCode != ERROR_SUCCESS)
  4047. {
  4048. break;
  4049. }
  4050. if (dwResponseLength != certResponse.GetDataLen())
  4051. {
  4052. // Didn't get the expected number of Bytes, also a problem
  4053. dwRetCode = IDS_ERR_CHBAD_DATA;
  4054. break;
  4055. }
  4056. bToSendAck = true;
  4057. dwExchgCertLen = certResponse.GetExchgPKCS7Length();
  4058. dwSigCertLen = certResponse.GetSignPKCS7Length();
  4059. dwRootCertLen = certResponse.GetRootCertLength();
  4060. if(dwRootCertLen == 0 || dwExchgCertLen == 0 || dwSigCertLen == 0 )
  4061. {
  4062. dwRetCode = IDS_ERR_CHBAD_DATA; //IDS_ERR_INVALID_PIN;
  4063. break;
  4064. }
  4065. //
  4066. // Exchange Certificate
  4067. //
  4068. lpszExchCert = UnicodeToAnsi((LPWSTR)lpszResData, dwExchgCertLen/sizeof(WCHAR));
  4069. if ( lpszExchCert == NULL )
  4070. {
  4071. dwRetCode = IDS_ERR_OUTOFMEM;
  4072. break;
  4073. }
  4074. //
  4075. // Signature Certificate
  4076. //
  4077. lpszSignCert = UnicodeToAnsi((LPWSTR)(lpszResData + dwExchgCertLen), dwSigCertLen/sizeof(WCHAR));
  4078. if(lpszSignCert == NULL)
  4079. {
  4080. dwRetCode = IDS_ERR_OUTOFMEM;
  4081. break;
  4082. }
  4083. //
  4084. // Root Certificate
  4085. //
  4086. lpszRootCert = UnicodeToAnsi ((LPWSTR)(lpszResData+dwExchgCertLen+dwSigCertLen),
  4087. dwRootCertLen/sizeof(WCHAR));
  4088. if(lpszRootCert == NULL)
  4089. {
  4090. dwRetCode = IDS_ERR_OUTOFMEM;
  4091. break;
  4092. }
  4093. dwRetCode = DepositLSSPK(certResponse.GetSPK());
  4094. if (dwRetCode != ERROR_SUCCESS)
  4095. {
  4096. //if (dwRetCode == IDS_ERR_DEPOSITSPK)
  4097. //{
  4098. // dwRetCode = IDS_ERR_CERT_DEPOSIT_LSERROR;
  4099. //}
  4100. break;
  4101. }
  4102. //
  4103. //Deposit the Certs
  4104. //
  4105. dwRetCode = DepositLSCertificates( (PBYTE)lpszExchCert,
  4106. lstrlenA(lpszExchCert),
  4107. (PBYTE)lpszSignCert,
  4108. lstrlenA(lpszSignCert),
  4109. (PBYTE)lpszRootCert,
  4110. lstrlenA(lpszRootCert)
  4111. );
  4112. if ( dwRetCode != ERROR_SUCCESS )
  4113. {
  4114. break;
  4115. }
  4116. //dwRetCode = SetLRState(LRSTATE_NEUTRAL);
  4117. //if the response comming back is SUCCESS, check for certificates
  4118. //in the response structure. If there is response
  4119. //perform the deposit LS Certificates routine
  4120. if (dwRetCode != ERROR_SUCCESS)
  4121. {
  4122. break;
  4123. }
  4124. //
  4125. // Now save the State in the Registry
  4126. //
  4127. //dwRetCode = SetLRState(LRSTATE_ONLINE_CR_CREATED);
  4128. */
  4129. }
  4130. while(false);
  4131. }
  4132. catch(...)
  4133. {
  4134. dwRetCode = IDS_ERR_EXCEPTION;
  4135. }
  4136. CloseCHRequest();
  4137. if (bToSendAck == true)
  4138. {
  4139. if (InitCHRequest() == ERROR_SUCCESS)
  4140. {
  4141. // Everything deposited OK
  4142. // Time to send the Ack
  4143. certackRequest.SetRegRequestId((BYTE *) certResponse.GetRegRequestId(),
  4144. (lstrlen(certResponse.GetRegRequestId())+1)*sizeof(TCHAR));
  4145. certackRequest.SetAckType((dwRetCode == ERROR_SUCCESS));
  4146. Dispatch((BYTE *) &certackRequest, sizeof(certackRequest));
  4147. // Ignore the Return value --- So what if the Ack gets lost
  4148. // Read the response
  4149. FetchResponse((BYTE *) &certackResponse, sizeof(certackResponse),
  4150. &dwResponseLength);
  4151. // Ignore the Return value --- So what if the Ack gets lost
  4152. CloseCHRequest();
  4153. }
  4154. }
  4155. //
  4156. //Free up Mem
  4157. //
  4158. ClearCARequestAttributes();
  4159. if(lpszExchgPKCS10)
  4160. {
  4161. delete lpszExchgPKCS10;
  4162. }
  4163. if(lpszSigPKCS10)
  4164. {
  4165. delete lpszSigPKCS10;
  4166. }
  4167. if ( lpwszExchgPKCS10 )
  4168. {
  4169. delete lpwszExchgPKCS10;
  4170. }
  4171. if (lpwszSignPKCS10)
  4172. {
  4173. delete lpwszSignPKCS10;
  4174. }
  4175. if ( lpszExchCert )
  4176. {
  4177. delete lpszExchCert;
  4178. }
  4179. if ( lpszSignCert )
  4180. {
  4181. delete lpszSignCert;
  4182. }
  4183. if ( lpszRootCert )
  4184. {
  4185. delete lpszRootCert;
  4186. }
  4187. if (lpszResData)
  4188. {
  4189. LocalFree(lpszResData);
  4190. }
  4191. return dwRetCode;
  4192. }
  4193. DWORD CGlobal::ProcessCertDownload()
  4194. {
  4195. DWORD dwRetCode = ERROR_SUCCESS;
  4196. CertificateDownload_Request certdownloadRequest;
  4197. CertificateDownload_Response certdownloadResponse;
  4198. Certificate_AckRequest certackRequest;
  4199. Certificate_AckResponse certackResponse;
  4200. PBYTE lpszResData = NULL;
  4201. DWORD dwExchgCertLen = 0;
  4202. DWORD dwSigCertLen = 0;
  4203. DWORD dwRootCertLen = 0;
  4204. LPSTR lpszExchCert = NULL;
  4205. LPSTR lpszSignCert = NULL;
  4206. LPSTR lpszRootCert = NULL;
  4207. DWORD dwResponseLength;
  4208. bool bToSendAck = false;
  4209. //
  4210. // Set the Language Id
  4211. //
  4212. certdownloadRequest.RequestHeader.SetLanguageId(GetLSLangId());
  4213. dwRetCode = InitCHRequest();
  4214. if (dwRetCode != ERROR_SUCCESS)
  4215. {
  4216. return dwRetCode;
  4217. }
  4218. try
  4219. {
  4220. do
  4221. {
  4222. //
  4223. // Create CHRequest for Certificate Download
  4224. //
  4225. certdownloadRequest.SetPIN((BYTE *) m_lpstrPIN, (lstrlenW(m_lpstrPIN)+1) * sizeof(WCHAR));
  4226. dwRetCode = Dispatch((BYTE *) &certdownloadRequest, sizeof(CertificateDownload_Request));
  4227. if(dwRetCode != ERROR_SUCCESS)
  4228. {
  4229. LRSetLastError(dwRetCode);
  4230. break;
  4231. }
  4232. // Let us first Fetch the certdownloadResponse
  4233. dwRetCode = FetchResponse((BYTE *) &certdownloadResponse,
  4234. sizeof(CertificateDownload_Response), &dwResponseLength);
  4235. if (dwRetCode != ERROR_SUCCESS)
  4236. {
  4237. break;
  4238. }
  4239. if (dwResponseLength != sizeof(CertificateDownload_Response))
  4240. {
  4241. // Didn't get the expected number of Bytes, also a problem
  4242. dwRetCode = IDS_ERR_CHBAD_DATA;
  4243. break;
  4244. }
  4245. switch(certdownloadResponse.RequestHeader.GetResponseType())
  4246. {
  4247. case Response_Success:
  4248. dwRetCode = ERROR_SUCCESS;
  4249. break;
  4250. case Response_Failure:
  4251. // dwRetCode = IDS_ERR_CHFAILURE;
  4252. //dwRetCode = IDS_ERR_CERT_DEPOSIT_LSERROR;
  4253. dwRetCode = IDS_ERR_INVALID_PIN;
  4254. break;
  4255. case Response_InvalidData:
  4256. dwRetCode = IDS_ERR_CHINVALID_DATA;
  4257. break;
  4258. case Response_NotYetImplemented:
  4259. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  4260. break;
  4261. case Response_ServerError:
  4262. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  4263. break;
  4264. case Response_Invalid_Response:
  4265. default:
  4266. dwRetCode = IDS_ERR_CHBAD_DATA;
  4267. break;
  4268. }
  4269. if (dwRetCode != ERROR_SUCCESS)
  4270. {
  4271. break;
  4272. }
  4273. // OK, allocate enough memory to read the rest fo the data
  4274. lpszResData = (PBYTE) LocalAlloc(GPTR, certdownloadResponse.GetDataLen() + 1);
  4275. if(lpszResData == NULL)
  4276. {
  4277. dwRetCode = IDS_ERR_OUTOFMEM;
  4278. break;
  4279. }
  4280. dwRetCode = FetchResponse(lpszResData, certdownloadResponse.GetDataLen() + 1,
  4281. &dwResponseLength);
  4282. if (dwRetCode != ERROR_SUCCESS)
  4283. {
  4284. break;
  4285. }
  4286. if (dwResponseLength != certdownloadResponse.GetDataLen())
  4287. {
  4288. // Didn't get the expected number of Bytes, also a problem
  4289. dwRetCode = IDS_ERR_CHBAD_DATA;
  4290. break;
  4291. }
  4292. bToSendAck = true;
  4293. dwExchgCertLen = certdownloadResponse.GetExchgPKCS7Length();
  4294. dwSigCertLen = certdownloadResponse.GetSignPKCS7Length();
  4295. dwRootCertLen = certdownloadResponse.GetRootCertLength();
  4296. if(dwRootCertLen == 0 || dwExchgCertLen == 0 || dwSigCertLen == 0 )
  4297. {
  4298. dwRetCode = IDS_ERR_INVALID_PIN;
  4299. break;
  4300. }
  4301. //
  4302. // Exchange Certificate
  4303. //
  4304. lpszExchCert = UnicodeToAnsi((LPWSTR)lpszResData, dwExchgCertLen/sizeof(WCHAR));
  4305. if ( lpszExchCert == NULL )
  4306. {
  4307. dwRetCode = IDS_ERR_OUTOFMEM;
  4308. break;
  4309. }
  4310. //
  4311. // Signature Certificate
  4312. //
  4313. lpszSignCert = UnicodeToAnsi((LPWSTR)(lpszResData + dwExchgCertLen), dwSigCertLen/sizeof(WCHAR));
  4314. if(lpszSignCert == NULL)
  4315. {
  4316. dwRetCode = IDS_ERR_OUTOFMEM;
  4317. break;
  4318. }
  4319. //
  4320. // Root Certificate
  4321. //
  4322. lpszRootCert = UnicodeToAnsi ((LPWSTR)(lpszResData+dwExchgCertLen+dwSigCertLen),
  4323. dwRootCertLen/sizeof(WCHAR));
  4324. if(lpszRootCert == NULL)
  4325. {
  4326. dwRetCode = IDS_ERR_OUTOFMEM;
  4327. break;
  4328. }
  4329. dwRetCode = SetLSSPK(certdownloadResponse.GetSPK());
  4330. if (dwRetCode != ERROR_SUCCESS)
  4331. {
  4332. break;
  4333. }
  4334. dwRetCode = DepositLSSPK();
  4335. if (dwRetCode != ERROR_SUCCESS)
  4336. {
  4337. if (dwRetCode == IDS_ERR_DEPOSITSPK)
  4338. {
  4339. dwRetCode = IDS_ERR_CERT_DEPOSIT_LSERROR;
  4340. }
  4341. break;
  4342. }
  4343. //
  4344. //Deposit the Certs
  4345. //
  4346. dwRetCode = DepositLSCertificates( (PBYTE)lpszExchCert,
  4347. lstrlenA(lpszExchCert),
  4348. (PBYTE)lpszSignCert,
  4349. lstrlenA(lpszSignCert),
  4350. (PBYTE)lpszRootCert,
  4351. lstrlenA(lpszRootCert)
  4352. );
  4353. if ( dwRetCode != ERROR_SUCCESS )
  4354. {
  4355. // If this happened and the SPK deposit succeeded, we have an
  4356. // inconsistent state, now
  4357. DWORD dwReturn;
  4358. DWORD dwOriginal = LRGetLastError();
  4359. dwReturn = ResetLSSPK(FALSE);
  4360. if (dwReturn != ERROR_SUCCESS)
  4361. {
  4362. // what to do, if even this failed. OUCH OUCH
  4363. dwRetCode = dwReturn;
  4364. }
  4365. LRSetLastError(dwOriginal);
  4366. m_pRegistrationID[ 0] = 0;
  4367. break;
  4368. }
  4369. else
  4370. {
  4371. dwRetCode = SetLRState(LRSTATE_NEUTRAL);
  4372. }
  4373. }
  4374. while(false);
  4375. }
  4376. catch(...)
  4377. {
  4378. dwRetCode = IDS_ERR_EXCEPTION;
  4379. }
  4380. CloseCHRequest();
  4381. // Now to send the Ack
  4382. if (bToSendAck == true)
  4383. {
  4384. if (InitCHRequest() == ERROR_SUCCESS)
  4385. {
  4386. // Everything deposited OK
  4387. // Time to send the Ack
  4388. certackRequest.SetRegRequestId((BYTE *) certdownloadResponse.GetRegRequestId(),
  4389. (lstrlen(certdownloadResponse.GetRegRequestId())+1)*sizeof(TCHAR));
  4390. certackRequest.SetAckType((dwRetCode == ERROR_SUCCESS));
  4391. Dispatch((BYTE *) &certackRequest, sizeof(certackRequest));
  4392. // Ignore the Return value --- So what if the Ack gets lost
  4393. // Read the response
  4394. FetchResponse((BYTE *) &certackResponse, sizeof(certackResponse),
  4395. &dwResponseLength);
  4396. // Ignore the Return value --- So what if the Ack gets lost
  4397. CloseCHRequest();
  4398. }
  4399. }
  4400. if ( lpszExchCert )
  4401. {
  4402. delete lpszExchCert;
  4403. }
  4404. if ( lpszSignCert )
  4405. {
  4406. delete lpszSignCert;
  4407. }
  4408. if ( lpszRootCert )
  4409. {
  4410. delete lpszRootCert;
  4411. }
  4412. if (lpszResData)
  4413. {
  4414. LocalFree(lpszResData);
  4415. }
  4416. return dwRetCode;
  4417. }
  4418. DWORD CGlobal::AuthenticateLS()
  4419. {
  4420. DWORD dwRetCode = ERROR_SUCCESS;
  4421. DWORD dwResponseLength;
  4422. BYTE * lpszReqData = NULL;
  4423. BYTE * lpszResData = NULL;
  4424. Validate_Response valResponse;
  4425. Validate_Request valRequest;
  4426. if (GetLSStatus() == LSERVERSTATUS_REGISTER_OTHER)
  4427. {
  4428. DWORD dwStatus;
  4429. // This LS was registered on the phone. First perform SignOnly, Then read the certs into memory
  4430. dwRetCode = ProcessCASignOnlyRequest();
  4431. if (dwRetCode != ERROR_SUCCESS)
  4432. {
  4433. goto done;
  4434. }
  4435. dwRetCode = GetLSCertificates(&dwStatus);
  4436. if (dwRetCode != ERROR_SUCCESS)
  4437. {
  4438. goto done;
  4439. }
  4440. assert(dwStatus == LSERVERSTATUS_REGISTER_INTERNET);
  4441. }
  4442. //
  4443. // Set Language Id
  4444. //
  4445. valRequest.RequestHeader.SetLanguageId(GetLSLangId());
  4446. dwRetCode = InitCHRequest();
  4447. if (dwRetCode != ERROR_SUCCESS)
  4448. {
  4449. goto done;
  4450. }
  4451. lpszReqData = (PBYTE) LocalAlloc(GPTR, sizeof(Validate_Request)+m_dwExchangeCertificateLen);
  4452. if(lpszReqData == NULL)
  4453. {
  4454. dwRetCode = IDS_ERR_OUTOFMEM;
  4455. goto done;
  4456. }
  4457. valRequest.SetDataLen(m_dwExchangeCertificateLen);
  4458. valRequest.SetCertBlobLen(m_dwExchangeCertificateLen);
  4459. valRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  4460. memcpy(lpszReqData, &valRequest, sizeof(Validate_Request));
  4461. memcpy(lpszReqData+sizeof(Validate_Request), m_pbExchangeCertificate, m_dwExchangeCertificateLen);
  4462. dwRetCode = Dispatch(lpszReqData, sizeof(Validate_Request)+m_dwExchangeCertificateLen);
  4463. if(dwRetCode != ERROR_SUCCESS)
  4464. {
  4465. LRSetLastError(dwRetCode);
  4466. goto done;
  4467. }
  4468. // Let us read the response
  4469. dwRetCode = FetchResponse((BYTE *) &valResponse, sizeof(Validate_Response),
  4470. &dwResponseLength);
  4471. if (dwRetCode != ERROR_SUCCESS)
  4472. {
  4473. goto done;
  4474. }
  4475. if (dwResponseLength != sizeof(Validate_Response))
  4476. {
  4477. // Didn't get the expected number of Bytes, also a problem
  4478. dwRetCode = IDS_ERR_CHBAD_DATA;
  4479. goto done;
  4480. }
  4481. // if (valResponse.m_dwRetCode != ERROR_SUCCESS)
  4482. // {
  4483. // dwRetCode = valResponse.m_dwRetCode;
  4484. // goto done;
  4485. // }
  4486. switch(valResponse.RequestHeader.GetResponseType())
  4487. {
  4488. case Response_Success:
  4489. dwRetCode = ERROR_SUCCESS;
  4490. break;
  4491. case Response_Failure:
  4492. // dwRetCode = IDS_ERR_CHFAILURE;
  4493. dwRetCode = IDS_ERR_AUTH_FAILED;
  4494. break;
  4495. case Response_Reg_Bad_SPK:
  4496. dwRetCode = IDS_ERR_SPKBAD;
  4497. break;
  4498. case Response_Reg_Bad_Cert:
  4499. dwRetCode = IDS_ERR_CERTBAD;
  4500. break;
  4501. case Response_Reg_Expired:
  4502. dwRetCode = IDS_ERR_CERTEXPIRED;
  4503. break;
  4504. case Response_Reg_Revoked:
  4505. dwRetCode = IDS_ERR_CERTREVOKED;
  4506. break;
  4507. case Response_InvalidData:
  4508. dwRetCode = IDS_ERR_CHINVALID_DATA;
  4509. break;
  4510. case Response_NotYetImplemented:
  4511. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  4512. break;
  4513. case Response_ServerError:
  4514. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  4515. break;
  4516. case Response_Invalid_Response:
  4517. default:
  4518. dwRetCode = IDS_ERR_CHBAD_DATA;
  4519. break;
  4520. }
  4521. if (dwRetCode != ERROR_SUCCESS)
  4522. {
  4523. goto done;
  4524. }
  4525. // OK, allocate enough memory to read the rest fo the data
  4526. lpszResData = (PBYTE) LocalAlloc(GPTR, valResponse.GetDataLen() + 1);
  4527. if(lpszResData == NULL)
  4528. {
  4529. dwRetCode = IDS_ERR_OUTOFMEM;
  4530. goto done;
  4531. }
  4532. dwRetCode = FetchResponse(lpszResData, valResponse.GetDataLen() + 1,
  4533. &dwResponseLength);
  4534. if (dwRetCode != ERROR_SUCCESS)
  4535. {
  4536. goto done;
  4537. }
  4538. if (dwResponseLength != valResponse.GetDataLen())
  4539. {
  4540. // Didn't get the expected number of Bytes, also a problem
  4541. dwRetCode = IDS_ERR_CHBAD_DATA;
  4542. goto done;
  4543. }
  4544. dwRetCode = SetCHCert ( REG_ROOT_CERT,
  4545. lpszResData,
  4546. valResponse.GetCHRootCertLen());
  4547. if(dwRetCode != ERROR_SUCCESS)
  4548. {
  4549. goto done;
  4550. }
  4551. dwRetCode = SetCHCert ( REG_EXCHG_CERT,
  4552. lpszResData+valResponse.GetCHRootCertLen(),
  4553. valResponse.GetCHExchCertLen() );
  4554. if(dwRetCode != ERROR_SUCCESS)
  4555. goto done;
  4556. dwRetCode = SetCHCert ( REG_SIGN_CERT,
  4557. lpszResData+valResponse.GetCHRootCertLen()+valResponse.GetCHExchCertLen(),
  4558. valResponse.GetCHSignCertLen() );
  4559. done:
  4560. CloseCHRequest();
  4561. if (lpszReqData)
  4562. LocalFree(lpszReqData);
  4563. if (lpszResData)
  4564. LocalFree(lpszResData);
  4565. return dwRetCode;
  4566. }
  4567. DWORD CGlobal::DownloadLKPBatch(ProductType productTypeFilter)
  4568. {
  4569. DWORD dwRetCode = ERROR_SUCCESS;
  4570. NewLKP_Request lkpRequest;
  4571. NewLKP_AckRequest lkpAckRequest;
  4572. PBYTE pbLKPRequest = NULL;
  4573. DWORD dwReqLen = 0;
  4574. NewLKP_Response lkpResponse;
  4575. DWORD dwResponseLen = 0;
  4576. NewLKP_AckResponse lkpAckResponse;
  4577. PBYTE lpszResData = NULL;
  4578. bool bToSendAck = false;
  4579. // Set Language ID
  4580. lkpRequest.RequestHeader.SetLanguageId(GetLSLangId());
  4581. dwRetCode = InitCHRequest();
  4582. if (dwRetCode != ERROR_SUCCESS)
  4583. goto done;
  4584. // Set LKP Request Attributes
  4585. SetCHRequestAttributes();
  4586. lkpRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  4587. dwRetCode = CreateLKPRequest(&pbLKPRequest, &lkpRequest, dwReqLen, productTypeFilter);
  4588. if(dwRetCode != ERROR_SUCCESS)
  4589. goto done;
  4590. dwRetCode = Dispatch(pbLKPRequest, dwReqLen);
  4591. if(dwRetCode != ERROR_SUCCESS)
  4592. {
  4593. LRSetLastError(dwRetCode);
  4594. goto done;
  4595. }
  4596. // Let us first Fetch the certdownloadResponse
  4597. dwRetCode = FetchResponse((BYTE *) &lkpResponse,
  4598. sizeof(NewLKP_Response), &dwResponseLen);
  4599. if (dwRetCode != ERROR_SUCCESS)
  4600. goto done;
  4601. if (dwResponseLen != sizeof(NewLKP_Response))
  4602. {
  4603. // Didn't get the expected number of Bytes, also a problem
  4604. dwRetCode = IDS_ERR_CHBAD_DATA;
  4605. goto done;
  4606. }
  4607. bToSendAck = true;
  4608. // OK, allocate enough memory to read the rest fo the data
  4609. lpszResData = (PBYTE) LocalAlloc(GPTR, lkpResponse.GetDataLen() + 1);
  4610. if(lpszResData == NULL)
  4611. {
  4612. dwRetCode = IDS_ERR_OUTOFMEM;
  4613. goto done;
  4614. }
  4615. dwRetCode = FetchResponse(lpszResData, lkpResponse.GetDataLen() + 1,
  4616. &dwResponseLen);
  4617. if (dwRetCode != ERROR_SUCCESS)
  4618. goto done;
  4619. if (dwResponseLen != lkpResponse.GetDataLen())
  4620. {
  4621. // Didn't get the expected number of Bytes, also a problem
  4622. dwRetCode = IDS_ERR_CHBAD_DATA;
  4623. goto done;
  4624. }
  4625. switch(lkpResponse.RequestHeader.GetResponseType())
  4626. {
  4627. case Response_Success:
  4628. if (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_LICENSE_PAK)
  4629. {
  4630. //Modify the statuses, because some other batch may fail, and the
  4631. //list will be shown to the user
  4632. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4633. {
  4634. if (GetProductCodeFromKey(m_listRetailSPK[i].lpszSPK) == productTypeFilter)
  4635. m_listRetailSPK[i].tcStatus = RETAIL_SPK_OK;
  4636. }
  4637. }
  4638. dwRetCode = ERROR_SUCCESS;
  4639. break;
  4640. case Response_Failure:
  4641. dwRetCode = IDS_ERR_CHFAILURE;
  4642. break;
  4643. case Response_SelectMloLicense_NotValid:
  4644. dwRetCode = IDS_ERR_INVALID_PROGINFO;
  4645. break;
  4646. case Response_InvalidData:
  4647. if (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_LICENSE_PAK)
  4648. {
  4649. // For retail, if all the LKP were not approved, show the list
  4650. // to the user
  4651. unsigned int filteredIndex = 0;
  4652. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4653. {
  4654. if (GetProductCodeFromKey(m_listRetailSPK[i].lpszSPK) == productTypeFilter)
  4655. {
  4656. //We can't allow it to be tagged with ok, since it won't be
  4657. //sent to the license server until everything in the batch is ok
  4658. if (lkpResponse.m_dwRetailSPKStatus[filteredIndex] == RETAIL_SPK_OK)
  4659. m_listRetailSPK[i].tcStatus = RETAIL_SPK_NULL;
  4660. else
  4661. m_listRetailSPK[i].tcStatus = lkpResponse.m_dwRetailSPKStatus[filteredIndex];
  4662. filteredIndex++;
  4663. }
  4664. }
  4665. bToSendAck = false;
  4666. dwRetCode = IDS_ERR_SPKERRORS;
  4667. }
  4668. else
  4669. dwRetCode = IDS_ERR_CHINVALID_DATA;
  4670. break;
  4671. case Response_NotYetImplemented:
  4672. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  4673. break;
  4674. case Response_ServerError:
  4675. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  4676. break;
  4677. case Response_Invalid_Response:
  4678. default:
  4679. dwRetCode = IDS_ERR_CHBAD_DATA;
  4680. break;
  4681. }
  4682. if (dwRetCode != ERROR_SUCCESS)
  4683. goto done;
  4684. DWORD* dwQuantity = NULL;
  4685. dwQuantity = (DWORD*)(&lpszResData[lkpResponse.GetLKPLength() - sizeof(DWORD)]);
  4686. m_ProductQuantity[productTypeFilter] += *dwQuantity;
  4687. dwRetCode = DepositLKPResponse(lpszResData, lkpResponse.GetLKPLength());
  4688. done:
  4689. ClearCHRequestAttributes();
  4690. CloseCHRequest();
  4691. // Now to send the Ack
  4692. if (bToSendAck == true)
  4693. {
  4694. if (InitCHRequest() == ERROR_SUCCESS)
  4695. {
  4696. // Everything deposited OK
  4697. // Time to send the Ack
  4698. lkpAckRequest.SetRegRequestId((BYTE *) lkpResponse.GetRegRequestId(),
  4699. (lstrlen(lkpResponse.GetRegRequestId())+1)*sizeof(TCHAR));
  4700. lkpAckRequest.SetLicenseReqId((BYTE *) lkpResponse.GetLicenseReqId(),
  4701. (lstrlen(lkpResponse.GetLicenseReqId())+1)*sizeof(TCHAR));
  4702. lkpAckRequest.SetAckType((dwRetCode == ERROR_SUCCESS));
  4703. Dispatch((BYTE *) &lkpAckRequest, sizeof(NewLKP_AckRequest));
  4704. // Ignore the Return value --- So what if the Ack gets lost
  4705. // Read the response
  4706. FetchResponse((BYTE *) &lkpAckResponse, sizeof(NewLKP_AckResponse),
  4707. &dwResponseLen);
  4708. // Ignore the Return value --- So what if the Ack gets lost
  4709. CloseCHRequest();
  4710. }
  4711. }
  4712. if(dwRetCode == ERROR_SUCCESS)
  4713. {
  4714. if ( pbLKPRequest )
  4715. free(pbLKPRequest);
  4716. }
  4717. if (lpszResData)
  4718. LocalFree(lpszResData);
  4719. return dwRetCode;
  4720. }
  4721. DWORD CGlobal::ProcessDownloadLKP()
  4722. {
  4723. DWORD dwRetCode = ERROR_SUCCESS;
  4724. if (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_LICENSE_PAK)
  4725. {
  4726. // Send individual LKP Requests
  4727. for (int nProductTypeIndex = WIN2K_PERSEAT; nProductTypeIndex < NUM_PRODUCT_TYPES; nProductTypeIndex++)
  4728. {
  4729. if (FilteredRetailSPKEntered((ProductType)nProductTypeIndex) > 0)
  4730. {
  4731. DWORD dwIndRetCode = ERROR_SUCCESS;
  4732. dwIndRetCode = DownloadLKPBatch((ProductType)nProductTypeIndex);
  4733. if (dwIndRetCode != ERROR_SUCCESS)
  4734. dwRetCode = dwIndRetCode;
  4735. }
  4736. }
  4737. //if (dwRetCode == ERROR_SUCCESS)
  4738. // InitSPKList();
  4739. }
  4740. else
  4741. dwRetCode = DownloadLKPBatch(UNKNOWN_PRODUCT_TYPE);
  4742. return dwRetCode;
  4743. }
  4744. //Only count codes that are NULL (haven't already been sent) and
  4745. //that match the product type filter
  4746. DWORD CGlobal::FilteredRetailSPKEntered(ProductType productTypeFilter)
  4747. {
  4748. DWORD dwFilteredCount = 0;
  4749. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4750. {
  4751. if ((GetProductCodeFromKey(m_listRetailSPK[i].lpszSPK) == productTypeFilter) &&
  4752. (m_listRetailSPK[i].tcStatus == RETAIL_SPK_NULL))
  4753. {
  4754. dwFilteredCount++;
  4755. }
  4756. }
  4757. return dwFilteredCount;
  4758. }
  4759. DWORD CGlobal::CreateLKPRequest(PBYTE * ppRequest, NewLKP_Request * nlkppRequest,
  4760. DWORD &dwDataLen, ProductType productTypeFilter)
  4761. {
  4762. DWORD dwRetCode = ERROR_SUCCESS;
  4763. STREAM_HDR aStreamHdr;
  4764. BLOCK_HDR aBlkHdr;
  4765. DWORD dwBufSize = 0;
  4766. BYTE * pBuf = NULL;
  4767. PBYTE pbCHCert = NULL;
  4768. DWORD dwCHCert = 0;
  4769. PBYTE pbEncryptedBuf = NULL;
  4770. PBYTE pbEncodedBlob = NULL;
  4771. DWORD dwEncodedBlob = 0;
  4772. DWORD dwBufLen = 0;
  4773. DWORD i =0;
  4774. dwDataLen = 0;
  4775. HANDLE hFile = INVALID_HANDLE_VALUE;
  4776. DWORD dwRetSize = 0;
  4777. //Create the stream header
  4778. _tcscpy ( aStreamHdr.m_szTitle, STREAM_HDR_TITLE );
  4779. aStreamHdr.SetHeader(STREAM_HDR_TYPE);
  4780. aStreamHdr.SetItemCount(0);
  4781. dwBufSize = sizeof(STREAM_HDR);
  4782. if ( ( pBuf = (BYTE *)malloc ( dwBufSize ) ) )
  4783. {
  4784. memcpy ( pBuf, &aStreamHdr, dwBufSize );
  4785. }
  4786. else
  4787. {
  4788. dwRetCode = IDS_ERR_OUTOFMEM;
  4789. goto done;
  4790. }
  4791. for ( i = 0; i < m_dwRegAttrCount; i++ )
  4792. {
  4793. //Setup the header here - put name/value pair into a data buffer
  4794. aBlkHdr.m_wType = BLOCK_TYPE_PROP_PAIR;
  4795. aBlkHdr.SetNameSize(lstrlenW( ( m_pRegAttr + i)->lpszAttribute ) * sizeof(WCHAR) );
  4796. aBlkHdr.SetValueSize(( m_pRegAttr + i)->dwValueLen );
  4797. BYTE* pTmpBuf;
  4798. if ( ( pTmpBuf = (BYTE *)realloc (pBuf, dwBufSize + sizeof(BLOCK_HDR) + aBlkHdr.GetNameSize() + aBlkHdr.GetValueSize()) ) )
  4799. {
  4800. if (NULL != pTmpBuf)
  4801. {
  4802. pBuf = pTmpBuf;
  4803. }
  4804. else
  4805. {
  4806. dwRetCode = IDS_ERR_OUTOFMEM;
  4807. goto done;
  4808. }
  4809. memcpy ( pBuf + dwBufSize, &aBlkHdr, sizeof ( BLOCK_HDR ) );
  4810. memcpy ( pBuf + dwBufSize + sizeof(BLOCK_HDR) , ( m_pRegAttr + i)->lpszAttribute , aBlkHdr.GetNameSize());
  4811. memcpy ( pBuf + dwBufSize + sizeof (BLOCK_HDR ) + aBlkHdr.GetNameSize() , ( m_pRegAttr + i)->lpszValue , aBlkHdr.GetValueSize() );
  4812. dwBufSize += sizeof(BLOCK_HDR) + aBlkHdr.GetNameSize()+ aBlkHdr.GetValueSize();
  4813. ((STREAM_HDR*)pBuf)->SetItemCount(((STREAM_HDR*)pBuf)->GetItemCount() + 1 );
  4814. }
  4815. else
  4816. {
  4817. dwRetCode = IDS_ERR_OUTOFMEM;
  4818. goto done;
  4819. }
  4820. }
  4821. dwBufLen = dwBufSize + m_dwExchangeCertificateLen;
  4822. DWORD dwFilteredProducts = FilteredRetailSPKEntered(productTypeFilter);
  4823. // Also need to allocate the extra memory to hold the retail stuff
  4824. if (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_LICENSE_PAK)
  4825. dwBufLen += ((dwFilteredProducts)*LR_RETAILSPK_LEN*sizeof(TCHAR));
  4826. nlkppRequest->RequestHeader.SetLanguageId(GetLSLangId());
  4827. nlkppRequest->SetDataLen(dwBufLen);
  4828. nlkppRequest->SetNewLKPRequestLen(dwBufSize);
  4829. nlkppRequest->SetCertBlobLen(m_dwExchangeCertificateLen);
  4830. nlkppRequest->SetRetailSPKCount(dwFilteredProducts);
  4831. *ppRequest = (PBYTE) malloc ( dwBufLen + sizeof(NewLKP_Request));
  4832. if ( NULL == *ppRequest )
  4833. {
  4834. dwRetCode = IDS_ERR_OUTOFMEM;
  4835. goto done;
  4836. }
  4837. memset ( *ppRequest, 0, dwBufLen + sizeof(NewLKP_Request));
  4838. memcpy((*ppRequest), nlkppRequest, sizeof(NewLKP_Request));
  4839. memcpy ( ( *ppRequest )+sizeof(NewLKP_Request), m_pbExchangeCertificate, m_dwExchangeCertificateLen );
  4840. memcpy ( ( *ppRequest )+sizeof(NewLKP_Request)+m_dwExchangeCertificateLen, pBuf, dwBufSize);
  4841. if (GetGlobalContext()->GetContactDataObject()->sProgramName == PROGRAM_LICENSE_PAK)
  4842. {
  4843. PBYTE pbCur = (*ppRequest)+sizeof(NewLKP_Request)+m_dwExchangeCertificateLen+dwBufSize;
  4844. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4845. {
  4846. if (GetProductCodeFromKey(m_listRetailSPK[i].lpszSPK) == productTypeFilter)
  4847. {
  4848. memcpy(pbCur, m_listRetailSPK[i].lpszSPK, LR_RETAILSPK_LEN*sizeof(TCHAR));
  4849. pbCur += LR_RETAILSPK_LEN*sizeof(TCHAR);
  4850. }
  4851. }
  4852. }
  4853. dwDataLen = sizeof(NewLKP_Request) + dwBufLen;
  4854. done:
  4855. if ( pbEncryptedBuf )
  4856. {
  4857. LocalFree(pbEncryptedBuf);
  4858. }
  4859. if ( pBuf )
  4860. {
  4861. free ( pBuf );
  4862. }
  4863. if ( dwRetCode != ERROR_SUCCESS )
  4864. {
  4865. if (*ppRequest != NULL)
  4866. {
  4867. free ( *ppRequest );
  4868. }
  4869. dwBufLen = 0;
  4870. *ppRequest = NULL;
  4871. }
  4872. return dwRetCode;
  4873. }
  4874. DWORD CGlobal::SetConfirmationNumber(TCHAR * tcConf)
  4875. {
  4876. DWORD dwRetCode = ERROR_SUCCESS;
  4877. if (wcsspn(tcConf, BASE24_CHARACTERS) != LR_CONFIRMATION_LEN)
  4878. {
  4879. // Extraneous characters in the SPK string
  4880. dwRetCode = IDS_ERR_INVALID_CONFIRMATION_NUMBER;
  4881. }
  4882. else if (LKPLiteValConfNumber(m_pRegistrationID, m_pLicenseServerID, tcConf)
  4883. != ERROR_SUCCESS)
  4884. {
  4885. dwRetCode = IDS_ERR_INVALID_CONFIRMATION_NUMBER;
  4886. }
  4887. return dwRetCode;
  4888. }
  4889. DWORD CGlobal::InitSPKList(void)
  4890. {
  4891. for (register int i = 0; i < MAX_RETAILSPKS_IN_BATCH; i++)
  4892. {
  4893. m_listRetailSPK[ i].lpszSPK[ 0] = 0;
  4894. m_listRetailSPK[ i].tcStatus = RETAIL_SPK_NULL;
  4895. }
  4896. m_dwRetailSPKEntered = 0;
  4897. //Initialize the list of product quantities
  4898. for (int nProducts = UNKNOWN_PRODUCT_TYPE; nProducts < NUM_PRODUCT_TYPES; nProducts++)
  4899. m_ProductQuantity[nProducts] = 0;
  4900. return ERROR_SUCCESS;
  4901. }
  4902. void CGlobal::DeleteRetailSPKFromList(TCHAR * lpszRetailSPK)
  4903. {
  4904. bool bFound = false;
  4905. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4906. {
  4907. if (lstrcmp(m_listRetailSPK[ i].lpszSPK, lpszRetailSPK) == 0)
  4908. {
  4909. if (i < MAX_RETAILSPKS_IN_BATCH-1)
  4910. {
  4911. memcpy(m_listRetailSPK + i,
  4912. m_listRetailSPK + i + 1,
  4913. sizeof(RETAILSPK)*(MAX_RETAILSPKS_IN_BATCH-i));
  4914. }
  4915. m_listRetailSPK[ MAX_RETAILSPKS_IN_BATCH-1].lpszSPK[ 0] = 0;
  4916. m_listRetailSPK[ MAX_RETAILSPKS_IN_BATCH-1].tcStatus = RETAIL_SPK_NULL;
  4917. bFound = true;
  4918. m_dwRetailSPKEntered--;
  4919. break;
  4920. }
  4921. }
  4922. assert(bFound == true);
  4923. return;
  4924. }
  4925. void CGlobal::ModifyRetailSPKFromList(TCHAR * lpszOldSPK,TCHAR * lpszNewSPK)
  4926. {
  4927. bool bFound = false;
  4928. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  4929. {
  4930. if (lstrcmp(m_listRetailSPK[ i].lpszSPK, lpszOldSPK) == 0)
  4931. {
  4932. if (i < MAX_RETAILSPKS_IN_BATCH-1)
  4933. {
  4934. _tcscpy(m_listRetailSPK[i].lpszSPK,lpszNewSPK);
  4935. m_listRetailSPK[i].tcStatus = RETAIL_SPK_NULL;
  4936. }
  4937. bFound = true;
  4938. break;
  4939. }
  4940. }
  4941. assert(bFound == true);
  4942. return;
  4943. }
  4944. int CGlobal::FindStringTruncationPoint(TCHAR* sOriginal, unsigned int nTruncationLength)
  4945. {
  4946. if (wcslen(sOriginal) <= nTruncationLength)
  4947. return 0;
  4948. int nOriginalStringIndex = 0;
  4949. for (nOriginalStringIndex = nTruncationLength - 1; nOriginalStringIndex >= 0; nOriginalStringIndex--)
  4950. {
  4951. if (sOriginal[nOriginalStringIndex] == L' ')
  4952. break;
  4953. }
  4954. if (nOriginalStringIndex < 0)
  4955. return nTruncationLength;
  4956. return nOriginalStringIndex;
  4957. }
  4958. int CGlobal::GetProductWindowMaxStringLength(HWND hListView)
  4959. {
  4960. LONG nMaxSize = 0;
  4961. SIZE URLsize;
  4962. HDC hURLWindowDC = GetDC(hListView);
  4963. for (int nListBoxIndex = 0; nListBoxIndex < SendMessage(hListView, LB_GETCOUNT, 0, 0); nListBoxIndex++)
  4964. {
  4965. TCHAR * pszProductString = NULL;
  4966. // Get the text length so we can allocate enough room to hold it
  4967. LRESULT nTextLength = SendMessage(hListView, LB_GETTEXTLEN, nListBoxIndex, 0);
  4968. if (nTextLength == LB_ERR)
  4969. break;
  4970. // Allocate our string, add one for null termination
  4971. pszProductString = (TCHAR*)LocalAlloc(LPTR, (nTextLength + 1) * sizeof(TCHAR));
  4972. if (pszProductString == NULL)
  4973. break;
  4974. SendMessage(hListView, LB_GETTEXT, nListBoxIndex, (LPARAM)pszProductString);
  4975. GetTextExtentPoint32(hURLWindowDC, pszProductString, wcslen(pszProductString), &URLsize);
  4976. if (URLsize.cx > nMaxSize)
  4977. nMaxSize = URLsize.cx;
  4978. LocalFree(pszProductString);
  4979. }
  4980. return (int)nMaxSize;
  4981. }
  4982. void CGlobal::AddStringToProductWindow(HWND hListView, TCHAR* sProductInfo, int nCurrentPoint, int nTruncationPoint)
  4983. {
  4984. TCHAR sTruncatedMessage[PRODUCT_WINDOW_STRING_LENGTH + PRODUCT_WINDOW_NUM_OFFSET_SPACES + 1];
  4985. memset(sTruncatedMessage, 0, sizeof(sTruncatedMessage));
  4986. if (nCurrentPoint > 0)
  4987. {
  4988. for (int nOffsetIndex = 0; nOffsetIndex < PRODUCT_WINDOW_NUM_OFFSET_SPACES; nOffsetIndex++)
  4989. sTruncatedMessage[nOffsetIndex] = L' ';
  4990. }
  4991. if (nTruncationPoint > 0)
  4992. wcsncat(sTruncatedMessage, &sProductInfo[nCurrentPoint], nTruncationPoint);
  4993. else
  4994. wcscat(sTruncatedMessage, &sProductInfo[nCurrentPoint]);
  4995. SendMessage(hListView, LB_ADDSTRING, 0, (LPARAM)(sTruncatedMessage));
  4996. SendMessage(hListView, LB_SETHORIZONTALEXTENT, GetProductWindowMaxStringLength(hListView), 0);
  4997. }
  4998. //Only include those where the status is 'OK'
  4999. void CGlobal::LoadFinishedFromList(HWND hListView)
  5000. {
  5001. for (int nProducts = WIN2K_PERSEAT; nProducts < NUM_PRODUCT_TYPES; nProducts++)
  5002. {
  5003. if (m_ProductQuantity[nProducts] > 0)
  5004. {
  5005. //Create the buffer which will be written to the control
  5006. TCHAR sProductInfo[128];
  5007. memset(sProductInfo, 0, sizeof(sProductInfo));
  5008. //Write in the quantity
  5009. TCHAR wQuantityBuf[12];
  5010. memset(wQuantityBuf, 0, sizeof(wQuantityBuf));
  5011. _ltow(m_ProductQuantity[nProducts], wQuantityBuf, 10);
  5012. wcscpy(sProductInfo, wQuantityBuf);
  5013. //Add a space character after the quantity
  5014. sProductInfo[wcslen(sProductInfo)] = L' ';
  5015. DWORD dwProductDescriptionID = 0;
  5016. switch (nProducts)
  5017. {
  5018. case WIN2K_PERSEAT:
  5019. dwProductDescriptionID = IDS_INSTALLED_WIN2K_CLIENT_ACCESS;
  5020. break;
  5021. case WIN2K_INTERNET:
  5022. dwProductDescriptionID = IDS_INSTALLED_WIN2K_INTERNET_CONNECTOR;
  5023. break;
  5024. case WHISTLER_PERUSER:
  5025. dwProductDescriptionID = IDS_INSTALLED_WHISTLER_PER_USER;
  5026. break;
  5027. case WHISTLER_PERSEAT:
  5028. dwProductDescriptionID = IDS_INSTALLED_WHISTLER_PER_SEAT;
  5029. break;
  5030. default:
  5031. break;
  5032. }
  5033. if (dwProductDescriptionID)
  5034. {
  5035. //Add the string to the window, but break it up so it will all fit within the width
  5036. LoadString(GetInstanceHandle(), dwProductDescriptionID, &sProductInfo[wcslen(sProductInfo)], ((sizeof(sProductInfo)/ sizeof(TCHAR)) - wcslen(sProductInfo)) );
  5037. int nCurrentPoint = 0;
  5038. int nTruncationPoint = 0;
  5039. while (nTruncationPoint = FindStringTruncationPoint(&sProductInfo[nCurrentPoint], PRODUCT_WINDOW_STRING_LENGTH))
  5040. {
  5041. AddStringToProductWindow(hListView, sProductInfo, nCurrentPoint, nTruncationPoint);
  5042. nCurrentPoint += nTruncationPoint;
  5043. }
  5044. AddStringToProductWindow(hListView, sProductInfo, nCurrentPoint, 0);
  5045. }
  5046. }
  5047. }
  5048. }
  5049. //Exclude those where the status is 'OK'
  5050. void CGlobal::LoadUnfinishedFromList(HWND hListView)
  5051. {
  5052. DWORD dwErr;
  5053. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++) {
  5054. if (m_listRetailSPK[i].tcStatus != RETAIL_SPK_OK) {
  5055. dwErr =
  5056. InsertIntoSPKDisplayList(hListView,
  5057. m_listRetailSPK[ i].lpszSPK,
  5058. m_listRetailSPK[ i].tcStatus);
  5059. if (dwErr != ERROR_SUCCESS) {
  5060. LRMessageBox(NULL, dwErr, IDS_WIZARD_MESSAGE_TITLE);
  5061. }
  5062. }
  5063. }
  5064. return;
  5065. }
  5066. void CGlobal::LoadFromList(HWND hListView)
  5067. {
  5068. DWORD dwErr;
  5069. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++) {
  5070. dwErr =
  5071. InsertIntoSPKDisplayList(hListView,
  5072. m_listRetailSPK[ i].lpszSPK,
  5073. m_listRetailSPK[ i].tcStatus);
  5074. if (dwErr != ERROR_SUCCESS) {
  5075. LRMessageBox(NULL, dwErr, IDS_WIZARD_MESSAGE_TITLE);
  5076. }
  5077. }
  5078. return;
  5079. }
  5080. void CGlobal::UpdateSPKStatus(TCHAR * lpszRetailSPK,
  5081. TCHAR tcStatus)
  5082. {
  5083. bool bFound = false;
  5084. for (register unsigned int i = 0; i < m_dwRetailSPKEntered; i++)
  5085. {
  5086. if (lstrcmp(m_listRetailSPK[ i].lpszSPK, lpszRetailSPK) == 0)
  5087. {
  5088. m_listRetailSPK[ i].tcStatus = tcStatus;
  5089. bFound = true;
  5090. break;
  5091. }
  5092. }
  5093. assert(bFound == true);
  5094. return;
  5095. }
  5096. //Fake code generator just used for testing
  5097. /*ProductType CGlobal::GetProductCodeFromKey(LPCTSTR lpLicenseKey)
  5098. {
  5099. ProductType licenseKeyType = UNKNOWN_PRODUCT_TYPE;
  5100. if (lpLicenseKey)
  5101. {
  5102. switch (lpLicenseKey[0] % 4)
  5103. {
  5104. case 0:
  5105. licenseKeyType = WIN2K_PERSEAT;
  5106. break;
  5107. case 1:
  5108. licenseKeyType = WIN2K_INTERNET;
  5109. break;
  5110. case 2:
  5111. licenseKeyType = WHISTLER_PERUSER;
  5112. break;
  5113. case 3:
  5114. licenseKeyType = WHISTLER_PERSEAT;
  5115. break;
  5116. }
  5117. }
  5118. return licenseKeyType;
  5119. }*/
  5120. ProductType CGlobal::GetProductCodeFromKey(LPCTSTR lpLicenseKey)
  5121. {
  5122. ProductType licenseKeyType = UNKNOWN_PRODUCT_TYPE;
  5123. TCHAR* lpProductType = NULL;
  5124. DWORD dwGroupId = 0;
  5125. DWORD retval = 0;
  5126. retval = GetLCProductType((TCHAR*)lpLicenseKey, &lpProductType, &dwGroupId);
  5127. if (retval == 0)
  5128. {
  5129. if (lpProductType)
  5130. {
  5131. BOOL fWin2000 = !m_fSupportConcurrent;
  5132. DWORD dwNumProducts = 0;
  5133. if ((!m_fSupportWhistlerCAL) && (!m_fSupportConcurrent))
  5134. dwNumProducts = IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START;
  5135. else if(m_fSupportConcurrent && !(m_fSupportWhistlerCAL))
  5136. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  5137. else if((!m_fSupportConcurrent) && m_fSupportWhistlerCAL)
  5138. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 1;
  5139. else if(m_fSupportConcurrent && m_fSupportWhistlerCAL)
  5140. dwNumProducts = (IDS_PRODUCT_CONCURRENT - IDS_PRODUCT_START) + 2;
  5141. for (DWORD dwIndex = IDS_PRODUCT_START; dwIndex < (IDS_PRODUCT_START + dwNumProducts); dwIndex++)
  5142. {
  5143. if (!m_fSupportConcurrent && (dwIndex == IDS_PRODUCT_CONCURRENT))
  5144. continue;
  5145. if (!m_fSupportWhistlerCAL && (dwIndex == IDS_PRODUCT_WHISTLER))
  5146. continue;
  5147. TCHAR lpProductDescription[LR_PRODUCT_DESC_LEN + LR_PRODUCT_CODE_LEN + 1];
  5148. LoadString(GetInstanceHandle(), dwIndex, lpProductDescription, LR_PRODUCT_DESC_LEN + LR_PRODUCT_CODE_LEN + 1);
  5149. TCHAR* lpProductCode = NULL;
  5150. lpProductCode = wcsrchr(lpProductDescription, L':') + 1;
  5151. if (lpProductCode)
  5152. {
  5153. if (wcsncmp(lpProductType, lpProductCode, LR_PRODUCT_CODE_LEN) == 0)
  5154. {
  5155. //the code matches, so see which of our enumerations is corresponds to
  5156. switch (dwIndex)
  5157. {
  5158. case IDS_PRODUCT_W2K_CLIENT_ACCESS:
  5159. licenseKeyType = WIN2K_PERSEAT;
  5160. break;
  5161. case IDS_PRODUCT_W2K_INTERNET_CONNECTOR:
  5162. licenseKeyType = WIN2K_INTERNET;
  5163. break;
  5164. case IDS_PRODUCT_WHISTLER_PER_USER:
  5165. licenseKeyType = WHISTLER_PERUSER;
  5166. break;
  5167. case IDS_PRODUCT_WHISTLER_PER_SEAT:
  5168. licenseKeyType = WHISTLER_PERSEAT;
  5169. break;
  5170. default:
  5171. licenseKeyType = UNKNOWN_PRODUCT_TYPE;
  5172. break;
  5173. }
  5174. }
  5175. }
  5176. }
  5177. }
  5178. }
  5179. return licenseKeyType;
  5180. }
  5181. //
  5182. // Return status
  5183. //
  5184. DWORD CGlobal::InsertIntoSPKDisplayList(HWND hListView,
  5185. TCHAR * lpszRetailSPK,
  5186. TCHAR tcStatus)
  5187. {
  5188. LVITEM lvItem;
  5189. TCHAR lpszBuffer[128];
  5190. DWORD dwStringToLoad = IDS_RETAILSPKSTATUS_UNKNOWN;
  5191. DWORD nItem;
  5192. DWORD dwRet = ERROR_SUCCESS;
  5193. //Let's first make sure we recognize the product type. Otherwise
  5194. //we don't want to insert it into the list
  5195. ProductType nProductType = GetProductCodeFromKey(lpszRetailSPK);
  5196. TCHAR lpProductType[LR_MAX_MSG_CAPTION];
  5197. memset(lpProductType, 0, LR_MAX_MSG_CAPTION * sizeof(TCHAR));
  5198. switch (nProductType) {
  5199. case WIN2K_PERSEAT:
  5200. LoadString(GetInstanceHandle(),IDS_WIN2K_PERSEAT,lpProductType,LR_MAX_MSG_CAPTION);
  5201. break;
  5202. case WIN2K_INTERNET:
  5203. LoadString(GetInstanceHandle(),IDS_WIN2K_INTERNET,lpProductType,LR_MAX_MSG_CAPTION);
  5204. break;
  5205. case WHISTLER_PERUSER:
  5206. LoadString(GetInstanceHandle(),IDS_WHISTLER_PERUSER,lpProductType,LR_MAX_MSG_CAPTION);
  5207. break;
  5208. case WHISTLER_PERSEAT:
  5209. LoadString(GetInstanceHandle(),IDS_WHISTLER_PERSEAT,lpProductType,LR_MAX_MSG_CAPTION);
  5210. break;
  5211. default:
  5212. dwRet = IDS_UNKNOWN_PRODUCTTYPE;
  5213. return dwRet;
  5214. break;
  5215. }
  5216. //Okay, now let's do the insertion
  5217. lvItem.mask = LVIF_TEXT;
  5218. lvItem.iItem = 0;
  5219. lvItem.iSubItem = 0;
  5220. lvItem.pszText = lpszRetailSPK;
  5221. lvItem.cchTextMax = lstrlen(lpszRetailSPK);
  5222. nItem = ListView_InsertItem(hListView, &lvItem);
  5223. switch (tcStatus) {
  5224. case RETAIL_SPK_NULL:
  5225. dwStringToLoad = IDS_RETAILSPKSTATUS_PENDING;
  5226. break;
  5227. case RETAIL_SPK_OK:
  5228. dwStringToLoad = IDS_RETAILSPKSTATUS_OK;
  5229. break;
  5230. case RETAIL_SPK_INVALID_SIGNATURE:
  5231. dwStringToLoad = IDS_RETAILSPKSTATUS_INVALID_SIGNATURE;
  5232. break;
  5233. case RETAIL_SPK_INVALID_PRODUCT_TYPE:
  5234. dwStringToLoad = IDS_RETAILSPKSTATUS_INVALID_PRODUCT_TYPE;
  5235. break;
  5236. case RETAIL_SPK_INVALID_SERIAL_NUMBER:
  5237. dwStringToLoad = IDS_RETAILSPKSTATUS_INVALID_SERIAL_NUMBER;
  5238. break;
  5239. case RETAIL_SPK_ALREADY_REGISTERED:
  5240. dwStringToLoad = IDS_RETAILSPKSTATUS_ALREADY_REGISTERED;
  5241. break;
  5242. }
  5243. LoadString(GetInstanceHandle(), dwStringToLoad, lpszBuffer, sizeof(lpszBuffer)/sizeof(TCHAR));
  5244. lvItem.iSubItem = 1;
  5245. lvItem.iItem = nItem;
  5246. lvItem.pszText = lpszBuffer;
  5247. lvItem.cchTextMax = lstrlen(lpszBuffer);
  5248. ListView_SetItem(hListView, &lvItem);
  5249. //lpProductType was determined above
  5250. lvItem.iSubItem = 2;
  5251. lvItem.iItem = nItem;
  5252. lvItem.pszText = lpProductType;
  5253. lvItem.cchTextMax = lstrlen(lpProductType);
  5254. ListView_SetItem(hListView, &lvItem);
  5255. return dwRet;
  5256. }
  5257. DWORD CGlobal::AddRetailSPKToList(HWND hListView,
  5258. TCHAR *lpszRetailSPK)
  5259. {
  5260. if (m_dwRetailSPKEntered == MAX_RETAILSPKS_IN_BATCH) {
  5261. return IDS_ERR_TOOMANYSPK;
  5262. }
  5263. assert(m_listRetailSPK[ m_dwRetailSPKEntered].lpszSPK[ 0] == 0);
  5264. assert(m_listRetailSPK[ m_dwRetailSPKEntered].tcStatus == RETAIL_SPK_NULL);
  5265. DWORD dwRetCode = ERROR_SUCCESS;
  5266. if (_tcsspn(lpszRetailSPK, BASE24_CHARACTERS) != LR_RETAILSPK_LEN) {
  5267. // Extraneous characters in the SPK string
  5268. dwRetCode = IDS_ERR_INVALIDSPK;
  5269. }
  5270. // Now check for duplication
  5271. for (register unsigned int i = 0; dwRetCode == ERROR_SUCCESS && i < m_dwRetailSPKEntered; i++) {
  5272. if (lstrcmp(m_listRetailSPK[ i].lpszSPK, lpszRetailSPK) == 0) {
  5273. dwRetCode = IDS_ERR_DUPLICATESPK;
  5274. }
  5275. }
  5276. if (dwRetCode == ERROR_SUCCESS) {
  5277. lstrcpy(m_listRetailSPK[ m_dwRetailSPKEntered].lpszSPK, lpszRetailSPK);
  5278. m_listRetailSPK[ m_dwRetailSPKEntered].tcStatus = RETAIL_SPK_NULL;
  5279. dwRetCode =
  5280. InsertIntoSPKDisplayList(
  5281. hListView,
  5282. lpszRetailSPK,
  5283. m_listRetailSPK[ m_dwRetailSPKEntered].tcStatus);
  5284. if (dwRetCode == ERROR_SUCCESS) {
  5285. m_dwRetailSPKEntered++;
  5286. } else {
  5287. //
  5288. // Error so cancel the add
  5289. //
  5290. m_listRetailSPK[ m_dwRetailSPKEntered].lpszSPK[0] = 0;
  5291. m_listRetailSPK[ m_dwRetailSPKEntered].tcStatus = RETAIL_SPK_NULL;
  5292. }
  5293. }
  5294. return dwRetCode;
  5295. }
  5296. DWORD CGlobal::ValidateRetailSPK(TCHAR * lpszRetailSPK)
  5297. {
  5298. DWORD dwRetCode = ERROR_SUCCESS;
  5299. if (_tcsspn(lpszRetailSPK, BASE24_CHARACTERS) != LR_RETAILSPK_LEN)
  5300. {
  5301. // Extraneous characters in the SPK string
  5302. dwRetCode = IDS_ERR_INVALIDSPK;
  5303. }
  5304. // Now check for duplication
  5305. for (register unsigned int i = 0; dwRetCode == ERROR_SUCCESS && i < m_dwRetailSPKEntered; i++)
  5306. {
  5307. if (lstrcmp(m_listRetailSPK[ i].lpszSPK, lpszRetailSPK) == 0)
  5308. {
  5309. dwRetCode = IDS_ERR_DUPLICATESPK;
  5310. }
  5311. }
  5312. return dwRetCode;
  5313. }
  5314. DWORD CGlobal::ProcessCASignOnlyRequest()
  5315. {
  5316. DWORD dwRetCode = ERROR_SUCCESS;
  5317. CertificateSignOnly_Request certsoRequest;
  5318. CertificateSignOnly_Response certsoResponse;
  5319. HCRYPTPROV hCryptProv = NULL;
  5320. LPWSTR lpwszExchgPKCS10 = NULL;
  5321. LPWSTR lpwszSignPKCS10 = NULL;
  5322. LPBYTE lpszReqData = NULL;
  5323. LPBYTE lpszResData = NULL;
  5324. LPBYTE lpszNextCopyPos = NULL;
  5325. LPSTR lpszExchgPKCS10 = NULL;
  5326. LPSTR lpszSigPKCS10 = NULL;
  5327. DWORD dwExchangeLen = 0;
  5328. DWORD dwSignLen = 0;
  5329. DWORD dwRootLen = 0;
  5330. DWORD dwResponseLength = 0;
  5331. LPSTR lpszExchCert = NULL;
  5332. LPSTR lpszSignCert = NULL;
  5333. LPSTR lpszRootCert = NULL;
  5334. //
  5335. // Set Language Id
  5336. //
  5337. certsoRequest.RequestHeader.SetLanguageId(GetLSLangId());
  5338. dwRetCode = InitCHRequest();
  5339. if (dwRetCode != ERROR_SUCCESS)
  5340. {
  5341. return dwRetCode;
  5342. }
  5343. SetCARequestAttributes();
  5344. //
  5345. //This function will call the CryptAcquireContext and import the LS Keys
  5346. //
  5347. if ( ( dwRetCode = GetCryptContextWithLSKeys (&hCryptProv ) )!= ERROR_SUCCESS )
  5348. {
  5349. //
  5350. // Newer LSs don't support this call - use alternate
  5351. //
  5352. dwRetCode = AskLSToCreatePKCS10(AT_KEYEXCHANGE, &lpszExchgPKCS10);
  5353. if(dwRetCode != ERROR_SUCCESS)
  5354. {
  5355. goto done;
  5356. }
  5357. dwRetCode = AskLSToCreatePKCS10(AT_SIGNATURE, &lpszSigPKCS10);
  5358. if(dwRetCode != ERROR_SUCCESS)
  5359. {
  5360. goto done;
  5361. }
  5362. }
  5363. else
  5364. {
  5365. dwRetCode = CreateLSPKCS10(hCryptProv,AT_KEYEXCHANGE,&lpszExchgPKCS10);
  5366. if(dwRetCode != ERROR_SUCCESS)
  5367. {
  5368. goto done;
  5369. }
  5370. dwRetCode = CreateLSPKCS10(hCryptProv,AT_SIGNATURE, &lpszSigPKCS10);
  5371. if(dwRetCode != ERROR_SUCCESS)
  5372. {
  5373. goto done;
  5374. }
  5375. // Release the context
  5376. if(hCryptProv)
  5377. {
  5378. DoneWithCryptContextWithLSKeys ( hCryptProv );
  5379. }
  5380. }
  5381. //
  5382. //Certificate Type
  5383. //
  5384. //Convert from multibyte to unicode
  5385. lpwszExchgPKCS10 = AnsiToUnicode(lpszExchgPKCS10);
  5386. lpwszSignPKCS10 = AnsiToUnicode(lpszSigPKCS10);
  5387. dwExchangeLen = lstrlen(lpwszExchgPKCS10) * sizeof(WCHAR);
  5388. dwSignLen = lstrlen(lpwszSignPKCS10) * sizeof(WCHAR);
  5389. certsoRequest.SetExchgPKCS10Length(dwExchangeLen);
  5390. certsoRequest.SetSignPKCS10Length(dwSignLen);
  5391. certsoRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  5392. certsoRequest.SetDataLen(dwExchangeLen+dwSignLen);
  5393. certsoRequest.SetServerName(m_lpstrLSName);
  5394. //Allocate buffer for the request
  5395. lpszReqData = (LPBYTE) LocalAlloc( GPTR, dwExchangeLen+dwSignLen+sizeof(certsoRequest) );
  5396. if(lpszReqData == NULL)
  5397. {
  5398. dwRetCode = IDS_ERR_OUTOFMEM;
  5399. goto done;
  5400. }
  5401. lpszNextCopyPos = lpszReqData;
  5402. memcpy(lpszNextCopyPos, &certsoRequest, sizeof(certsoRequest));
  5403. lpszNextCopyPos += sizeof(certsoRequest);
  5404. memcpy ( lpszNextCopyPos, lpwszExchgPKCS10, dwExchangeLen);
  5405. lpszNextCopyPos += dwExchangeLen;
  5406. memcpy ( lpszNextCopyPos, lpwszSignPKCS10, dwSignLen);
  5407. dwRetCode = Dispatch(lpszReqData, dwExchangeLen+dwSignLen+sizeof(certsoRequest));
  5408. if ( lpszReqData )
  5409. {
  5410. LocalFree(lpszReqData);
  5411. }
  5412. if (dwRetCode != ERROR_SUCCESS)
  5413. {
  5414. LRSetLastError(dwRetCode);
  5415. goto done;
  5416. }
  5417. dwRetCode = FetchResponse((BYTE *) &certsoResponse,
  5418. sizeof(CertificateSignOnly_Response),
  5419. &dwResponseLength);
  5420. if (dwRetCode != ERROR_SUCCESS)
  5421. {
  5422. goto done;
  5423. }
  5424. if (dwResponseLength != sizeof(CertificateSignOnly_Response))
  5425. {
  5426. // Got an invalid response back
  5427. dwRetCode = IDS_ERR_CHBAD_DATA;
  5428. goto done;
  5429. }
  5430. switch(certsoResponse.RequestHeader.GetResponseType())
  5431. {
  5432. case Response_Success:
  5433. dwRetCode = ERROR_SUCCESS;
  5434. break;
  5435. case Response_Failure:
  5436. // dwRetCode = IDS_ERR_CHFAILURE;
  5437. dwRetCode = IDS_ERR_AUTH_FAILED;
  5438. break;
  5439. case Response_Reg_Bad_SPK:
  5440. dwRetCode = IDS_ERR_SPKBAD;
  5441. break;
  5442. case Response_Reg_Expired:
  5443. dwRetCode = IDS_ERR_CERTEXPIRED;
  5444. break;
  5445. case Response_Reg_Revoked:
  5446. dwRetCode = IDS_ERR_CERTREVOKED;
  5447. break;
  5448. case Response_InvalidData:
  5449. dwRetCode = IDS_ERR_CHINVALID_DATA;
  5450. break;
  5451. case Response_NotYetImplemented:
  5452. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  5453. break;
  5454. case Response_ServerError:
  5455. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  5456. break;
  5457. case Response_Invalid_Response:
  5458. default:
  5459. dwRetCode = IDS_ERR_CHBAD_DATA;
  5460. break;
  5461. }
  5462. if (dwRetCode != ERROR_SUCCESS)
  5463. {
  5464. goto done;
  5465. }
  5466. // OK, allocate enough memory to read the rest fo the data
  5467. lpszResData = (PBYTE) LocalAlloc(GPTR, certsoResponse.GetDataLen() + 1);
  5468. if(lpszResData == NULL)
  5469. {
  5470. dwRetCode = IDS_ERR_OUTOFMEM;
  5471. goto done;
  5472. }
  5473. dwRetCode = FetchResponse(lpszResData, certsoResponse.GetDataLen() + 1,
  5474. &dwResponseLength);
  5475. if (dwRetCode != ERROR_SUCCESS)
  5476. {
  5477. goto done;
  5478. }
  5479. if (dwResponseLength != certsoResponse.GetDataLen() ||
  5480. dwResponseLength <= 0)
  5481. {
  5482. // Didn't get the expected number of Bytes, also a problem
  5483. dwRetCode = IDS_ERR_CHBAD_DATA;
  5484. goto done;
  5485. }
  5486. dwExchangeLen = certsoResponse.GetExchgPKCS7Length();
  5487. dwSignLen = certsoResponse.GetSignPKCS7Length();
  5488. dwRootLen = certsoResponse.GetRootCertLength();
  5489. if(dwRootLen == 0 || dwExchangeLen == 0 || dwSignLen == 0 )
  5490. {
  5491. dwRetCode = IDS_ERR_CHBAD_DATA;
  5492. goto done;
  5493. }
  5494. //
  5495. // Exchange Certificate
  5496. //
  5497. lpszExchCert = UnicodeToAnsi((LPWSTR)lpszResData, dwExchangeLen/sizeof(WCHAR));
  5498. if ( lpszExchCert == NULL )
  5499. {
  5500. dwRetCode = IDS_ERR_OUTOFMEM;
  5501. goto done;
  5502. }
  5503. //
  5504. // Signature Certificate
  5505. //
  5506. lpszSignCert = UnicodeToAnsi((LPWSTR)(lpszResData + dwExchangeLen), dwSignLen/sizeof(WCHAR));
  5507. if(lpszSignCert == NULL)
  5508. {
  5509. dwRetCode = IDS_ERR_OUTOFMEM;
  5510. goto done;
  5511. }
  5512. //
  5513. // Root Certificate
  5514. //
  5515. lpszRootCert = UnicodeToAnsi ((LPWSTR)(lpszResData+dwExchangeLen+dwSignLen),
  5516. dwRootLen/sizeof(WCHAR));
  5517. if(lpszRootCert == NULL)
  5518. {
  5519. dwRetCode = IDS_ERR_OUTOFMEM;
  5520. goto done;
  5521. }
  5522. //
  5523. //Deposit the Certs
  5524. //
  5525. dwRetCode = DepositLSCertificates( (PBYTE)lpszExchCert,
  5526. lstrlenA(lpszExchCert),
  5527. (PBYTE)lpszSignCert,
  5528. lstrlenA(lpszSignCert),
  5529. (PBYTE)lpszRootCert,
  5530. lstrlenA(lpszRootCert)
  5531. );
  5532. if ( dwRetCode != ERROR_SUCCESS )
  5533. {
  5534. goto done;
  5535. }
  5536. done:
  5537. ClearCARequestAttributes();
  5538. CloseCHRequest();
  5539. //
  5540. //Free up Certificate Mem
  5541. //
  5542. if(lpszExchgPKCS10)
  5543. {
  5544. delete lpszExchgPKCS10;
  5545. }
  5546. if(lpszSigPKCS10)
  5547. {
  5548. delete lpszSigPKCS10;
  5549. }
  5550. if ( lpwszExchgPKCS10 )
  5551. {
  5552. delete lpwszExchgPKCS10;
  5553. }
  5554. if (lpwszSignPKCS10)
  5555. {
  5556. delete lpwszSignPKCS10;
  5557. }
  5558. if ( lpszExchCert )
  5559. {
  5560. delete lpszExchCert;
  5561. }
  5562. if ( lpszSignCert )
  5563. {
  5564. delete lpszSignCert;
  5565. }
  5566. if ( lpszRootCert )
  5567. {
  5568. delete lpszRootCert;
  5569. }
  5570. if (lpszResData)
  5571. {
  5572. LocalFree(lpszResData);
  5573. }
  5574. return dwRetCode;
  5575. }
  5576. DWORD CGlobal::ProcessCHReissueLKPRequest()
  5577. {
  5578. DWORD dwRetCode = ERROR_SUCCESS;
  5579. ReissueLKP_Request lkpRequest;
  5580. ReissueLKP_Response lkpResponse;
  5581. PBYTE lpszReqData = NULL;
  5582. PBYTE lpszResData = NULL;
  5583. DWORD dwResponseLen;
  5584. //
  5585. // Set Language Id
  5586. //
  5587. lkpRequest.RequestHeader.SetLanguageId(GetLSLangId());
  5588. dwRetCode = InitCHRequest();
  5589. if (dwRetCode != ERROR_SUCCESS)
  5590. {
  5591. goto done;
  5592. }
  5593. //
  5594. // Set LKP Request Attributes
  5595. //
  5596. lkpRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  5597. lkpRequest.SetCertBlobLen(m_dwExchangeCertificateLen);
  5598. lkpRequest.SetDataLen(m_dwExchangeCertificateLen);
  5599. // OK, allocate enough memory to read the rest fo the data
  5600. lpszReqData = (PBYTE) LocalAlloc(GPTR, sizeof(ReissueLKP_Request)+m_dwExchangeCertificateLen);
  5601. if(lpszReqData == NULL)
  5602. {
  5603. dwRetCode = IDS_ERR_OUTOFMEM;
  5604. goto done;
  5605. }
  5606. memcpy(lpszReqData, &lkpRequest, sizeof(ReissueLKP_Request));
  5607. memcpy(lpszReqData+sizeof(ReissueLKP_Request), m_pbExchangeCertificate, m_dwExchangeCertificateLen );
  5608. dwRetCode = Dispatch(lpszReqData, sizeof(ReissueLKP_Request)+m_dwExchangeCertificateLen);
  5609. if(dwRetCode != ERROR_SUCCESS)
  5610. {
  5611. LRSetLastError(dwRetCode);
  5612. goto done;
  5613. }
  5614. // Let us first Fetch the certdownloadResponse
  5615. dwRetCode = FetchResponse((BYTE *) &lkpResponse,
  5616. sizeof(ReissueLKP_Response), &dwResponseLen);
  5617. if (dwRetCode != ERROR_SUCCESS)
  5618. {
  5619. goto done;
  5620. }
  5621. if (dwResponseLen != sizeof(ReissueLKP_Response))
  5622. {
  5623. // Didn't get the expected number of Bytes, also a problem
  5624. dwRetCode = IDS_ERR_CHBAD_DATA;
  5625. goto done;
  5626. }
  5627. switch(lkpResponse.RequestHeader.GetResponseType())
  5628. {
  5629. case Response_Success:
  5630. dwRetCode = ERROR_SUCCESS;
  5631. break;
  5632. case Response_Failure:
  5633. dwRetCode = IDS_ERR_CHFAILURE;
  5634. break;
  5635. case Response_InvalidData:
  5636. dwRetCode = IDS_ERR_CHINVALID_DATA;
  5637. break;
  5638. case Response_NotYetImplemented:
  5639. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  5640. break;
  5641. case Response_ServerError:
  5642. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  5643. break;
  5644. case Response_Invalid_Response:
  5645. default:
  5646. dwRetCode = IDS_ERR_CHBAD_DATA;
  5647. break;
  5648. }
  5649. if (dwRetCode != ERROR_SUCCESS)
  5650. {
  5651. goto done;
  5652. }
  5653. // OK, allocate enough memory to read the rest fo the data
  5654. lpszResData = (PBYTE) LocalAlloc(GPTR, lkpResponse.GetDataLen() + 1);
  5655. if(lpszResData == NULL)
  5656. {
  5657. dwRetCode = IDS_ERR_OUTOFMEM;
  5658. goto done;
  5659. }
  5660. dwRetCode = FetchResponse(lpszResData, lkpResponse.GetDataLen() + 1, &dwResponseLen);
  5661. if (dwRetCode != ERROR_SUCCESS)
  5662. {
  5663. goto done;
  5664. }
  5665. if (dwResponseLen != lkpResponse.GetDataLen() || dwResponseLen <= 0)
  5666. {
  5667. // Didn't get the expected number of Bytes, also a problem
  5668. dwRetCode = IDS_ERR_CHBAD_DATA;
  5669. goto done;
  5670. }
  5671. dwRetCode = DepositLKPResponse(lpszResData, lkpResponse.GetLKPLength());
  5672. done:
  5673. CloseCHRequest();
  5674. if (lpszReqData)
  5675. {
  5676. LocalFree(lpszReqData);
  5677. }
  5678. if (lpszResData)
  5679. {
  5680. LocalFree(lpszResData);
  5681. }
  5682. return dwRetCode;
  5683. }
  5684. DWORD CGlobal::ProcessCHRevokeCert()
  5685. {
  5686. DWORD dwRetCode = ERROR_SUCCESS;
  5687. CertRevoke_Request crRequest;
  5688. CertRevoke_Response crResponse;
  5689. PBYTE lpszReqData = NULL;
  5690. DWORD dwResponseLen;
  5691. error_status_t esRPC = ERROR_SUCCESS;
  5692. //
  5693. // Set Language Id
  5694. //
  5695. crRequest.RequestHeader.SetLanguageId(GetLSLangId());
  5696. dwRetCode = ConnectToLS();
  5697. if(dwRetCode != ERROR_SUCCESS)
  5698. {
  5699. goto done;
  5700. }
  5701. dwRetCode = InitCHRequest();
  5702. if (dwRetCode != ERROR_SUCCESS)
  5703. {
  5704. goto done;
  5705. }
  5706. //
  5707. // Set CR Request Attributes
  5708. //
  5709. crRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  5710. crRequest.SetLName((BYTE *) (LPCTSTR) m_ContactData.sContactLName, (wcslen(m_ContactData.sContactLName)+1)*sizeof(TCHAR));
  5711. crRequest.SetFName((BYTE *) (LPCTSTR) m_ContactData.sContactFName, (wcslen(m_ContactData.sContactFName)+1)*sizeof(TCHAR));
  5712. crRequest.SetPhone((BYTE *) (LPCTSTR) m_ContactData.sContactPhone, (wcslen(m_ContactData.sContactPhone)+1)*sizeof(TCHAR));
  5713. crRequest.SetFax((BYTE *) (LPCTSTR) m_ContactData.sContactFax, (wcslen(m_ContactData.sContactFax)+1)*sizeof(TCHAR));
  5714. crRequest.SetEMail((BYTE *) (LPCTSTR) m_ContactData.sContactEmail, (wcslen(m_ContactData.sContactEmail)+1)*sizeof(TCHAR));
  5715. crRequest.SetReasonCode((BYTE *) (LPCTSTR) m_ContactData.sReasonCode, (wcslen(m_ContactData.sReasonCode)+1)*sizeof(TCHAR));
  5716. crRequest.SetExchgCertLen(m_dwExchangeCertificateLen);
  5717. crRequest.SetSignCertLen(m_dwSignCertificateLen);
  5718. crRequest.SetDataLen(m_dwExchangeCertificateLen+m_dwSignCertificateLen);
  5719. // OK, allocate enough memory to read the rest fo the data
  5720. lpszReqData = (PBYTE) LocalAlloc(GPTR, sizeof(CertRevoke_Request)+m_dwExchangeCertificateLen+m_dwSignCertificateLen);
  5721. if(lpszReqData == NULL)
  5722. {
  5723. dwRetCode = IDS_ERR_OUTOFMEM;
  5724. goto done;
  5725. }
  5726. memcpy(lpszReqData, &crRequest, sizeof(CertRevoke_Request));
  5727. memcpy(lpszReqData+sizeof(CertRevoke_Request), m_pbExchangeCertificate, m_dwExchangeCertificateLen );
  5728. memcpy(lpszReqData+sizeof(CertRevoke_Request)+m_dwExchangeCertificateLen,
  5729. m_pbSignCertificate,
  5730. m_dwSignCertificateLen );
  5731. dwRetCode = Dispatch(lpszReqData, sizeof(CertRevoke_Request)+m_dwExchangeCertificateLen+m_dwSignCertificateLen);
  5732. if(dwRetCode != ERROR_SUCCESS)
  5733. {
  5734. LRSetLastError(dwRetCode);
  5735. goto done;
  5736. }
  5737. dwRetCode = FetchResponse((BYTE *) &crResponse,
  5738. sizeof(CertRevoke_Response), &dwResponseLen);
  5739. if (dwRetCode != ERROR_SUCCESS)
  5740. {
  5741. goto done;
  5742. }
  5743. if (dwResponseLen != sizeof(CertRevoke_Response))
  5744. {
  5745. // Didn't get the expected number of Bytes, also a problem
  5746. dwRetCode = IDS_ERR_CHBAD_DATA;
  5747. goto done;
  5748. }
  5749. switch(crResponse.RequestHeader.GetResponseType())
  5750. {
  5751. case Response_Success:
  5752. dwRetCode = ERROR_SUCCESS;
  5753. break;
  5754. case Response_Failure:
  5755. dwRetCode = IDS_ERR_CHFAILURE;
  5756. break;
  5757. case Response_InvalidData:
  5758. dwRetCode = IDS_ERR_CHINVALID_DATA;
  5759. break;
  5760. case Response_NotYetImplemented:
  5761. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  5762. break;
  5763. case Response_ServerError:
  5764. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  5765. break;
  5766. case Response_Invalid_Response:
  5767. default:
  5768. dwRetCode = IDS_ERR_CHBAD_DATA;
  5769. break;
  5770. }
  5771. if (dwRetCode != ERROR_SUCCESS)
  5772. {
  5773. goto done;
  5774. }
  5775. // Make LS Regen Key call HERE
  5776. dwRetCode = TLSTriggerReGenKey(m_phLSContext, TRUE, &esRPC);
  5777. if(dwRetCode != RPC_S_OK || esRPC != ERROR_SUCCESS)
  5778. {
  5779. LRSetLastError(dwRetCode);
  5780. dwRetCode = IDS_ERR_RPC_FAILED;
  5781. goto done;
  5782. }
  5783. done:
  5784. CloseCHRequest();
  5785. DisconnectLS();
  5786. if (lpszReqData)
  5787. {
  5788. LocalFree(lpszReqData);
  5789. }
  5790. return dwRetCode;
  5791. }
  5792. DWORD CGlobal::ProcessCHReissueCert()
  5793. {
  5794. HKEY hKey = NULL;
  5795. DWORD dwDisposition = 0;
  5796. LPBYTE lpszReqData = NULL;
  5797. LPBYTE lpszNextCopyPos = NULL;
  5798. DWORD dwLicenseServerIDLen = 0;
  5799. CString sName = m_ContactData.sContactLName + "~" + m_ContactData.sContactFName;
  5800. CString sPhone = m_ContactData.sContactPhone;
  5801. CString sFax = m_ContactData.sContactFax;
  5802. CString sEmail = m_ContactData.sContactEmail;
  5803. DWORD dwRetCode = ERROR_SUCCESS;
  5804. CertReissue_Request crRequest;
  5805. CertReissue_Response crResponse;
  5806. DWORD dwResponseLen;
  5807. error_status_t esRPC = ERROR_SUCCESS;
  5808. //
  5809. // Set Language Id
  5810. //
  5811. crRequest.RequestHeader.SetLanguageId(GetLSLangId());
  5812. dwRetCode = ConnectToLS();
  5813. if(dwRetCode != ERROR_SUCCESS)
  5814. {
  5815. goto done;
  5816. }
  5817. dwRetCode = InitCHRequest();
  5818. if (dwRetCode != ERROR_SUCCESS)
  5819. {
  5820. goto done;
  5821. }
  5822. //
  5823. // Set CR Request Attributes
  5824. //
  5825. crRequest.SetSPK((BYTE *) m_pRegistrationID, (lstrlen(m_pRegistrationID)+1)*sizeof(TCHAR));
  5826. crRequest.SetLName((BYTE *) (LPCTSTR) m_ContactData.sContactLName, (wcslen(m_ContactData.sContactLName)+1)*sizeof(TCHAR));
  5827. crRequest.SetFName((BYTE *) (LPCTSTR) m_ContactData.sContactFName, (wcslen(m_ContactData.sContactFName)+1)*sizeof(TCHAR));
  5828. crRequest.SetPhone((BYTE *) (LPCTSTR) m_ContactData.sContactPhone, (wcslen(m_ContactData.sContactPhone)+1)*sizeof(TCHAR));
  5829. crRequest.SetFax((BYTE *) (LPCTSTR) m_ContactData.sContactFax, (wcslen(m_ContactData.sContactFax)+1)*sizeof(TCHAR));
  5830. crRequest.SetEMail((BYTE *) (LPCTSTR) m_ContactData.sContactEmail, (wcslen(m_ContactData.sContactEmail)+1)*sizeof(TCHAR));
  5831. crRequest.SetReasonCode((BYTE *) (LPCTSTR) m_ContactData.sReasonCode, (wcslen(m_ContactData.sReasonCode)+1)*sizeof(TCHAR));
  5832. dwLicenseServerIDLen = sizeof(m_pLicenseServerID);
  5833. crRequest.SetDataLen(dwLicenseServerIDLen);
  5834. //Allocate buffer for the request
  5835. lpszReqData = (LPBYTE) LocalAlloc( GPTR, dwLicenseServerIDLen+sizeof(crRequest) );
  5836. if(lpszReqData == NULL)
  5837. {
  5838. dwRetCode = IDS_ERR_OUTOFMEM;
  5839. goto done;
  5840. }
  5841. lpszNextCopyPos = lpszReqData;
  5842. memcpy(lpszNextCopyPos, &crRequest, sizeof(crRequest));
  5843. lpszNextCopyPos += sizeof(crRequest);
  5844. memcpy ( lpszNextCopyPos, m_pLicenseServerID, dwLicenseServerIDLen);
  5845. dwRetCode = Dispatch(lpszReqData, dwLicenseServerIDLen+sizeof(crRequest));
  5846. if ( lpszReqData )
  5847. {
  5848. LocalFree(lpszReqData);
  5849. }
  5850. if (dwRetCode != ERROR_SUCCESS)
  5851. {
  5852. LRSetLastError(dwRetCode);
  5853. goto done;
  5854. }
  5855. dwRetCode = FetchResponse((BYTE *) &crResponse, sizeof(CertReissue_Response), &dwResponseLen);
  5856. if (dwRetCode != ERROR_SUCCESS)
  5857. {
  5858. goto done;
  5859. }
  5860. if (dwResponseLen != sizeof(CertReissue_Response))
  5861. {
  5862. // Didn't get the expected number of Bytes, also a problem
  5863. dwRetCode = IDS_ERR_CHBAD_DATA;
  5864. goto done;
  5865. }
  5866. switch(crResponse.RequestHeader.GetResponseType())
  5867. {
  5868. case Response_Success:
  5869. dwRetCode = ERROR_SUCCESS;
  5870. break;
  5871. case Response_Failure:
  5872. dwRetCode = IDS_ERR_CHFAILURE;
  5873. break;
  5874. case Response_InvalidData:
  5875. dwRetCode = IDS_ERR_CHINVALID_DATA;
  5876. break;
  5877. case Response_NotYetImplemented:
  5878. dwRetCode = IDS_ERR_CHNOT_IMPLEMENTED;
  5879. break;
  5880. case Response_ServerError:
  5881. dwRetCode = IDS_ERR_CHSERVER_PROBLEM;
  5882. break;
  5883. case Response_Invalid_Response:
  5884. default:
  5885. dwRetCode = IDS_ERR_CHBAD_DATA;
  5886. break;
  5887. }
  5888. if (dwRetCode != ERROR_SUCCESS)
  5889. {
  5890. goto done;
  5891. }
  5892. // Make LS Regen Key call HERE
  5893. dwRetCode = TLSTriggerReGenKey(m_phLSContext, TRUE, &esRPC);
  5894. if(dwRetCode != RPC_S_OK || esRPC != ERROR_SUCCESS)
  5895. {
  5896. LRSetLastError(dwRetCode);
  5897. dwRetCode = IDS_ERR_RPC_FAILED;
  5898. goto done;
  5899. }
  5900. DisconnectLS();
  5901. // Deposit the New SPK
  5902. dwRetCode = SetLSSPK(crResponse.GetSPK());
  5903. if (dwRetCode != ERROR_SUCCESS)
  5904. {
  5905. goto done;
  5906. }
  5907. dwRetCode = DepositLSSPK();
  5908. if(dwRetCode != ERROR_SUCCESS)
  5909. goto done;
  5910. // Set the new values in the Registry.
  5911. dwRetCode = ConnectToLSRegistry();
  5912. if(dwRetCode != ERROR_SUCCESS)
  5913. {
  5914. goto done;
  5915. }
  5916. dwRetCode = RegCreateKeyEx ( m_hLSRegKey,
  5917. REG_LRWIZ_PARAMS,
  5918. 0,
  5919. NULL,
  5920. REG_OPTION_NON_VOLATILE,
  5921. KEY_ALL_ACCESS,
  5922. NULL,
  5923. &hKey,
  5924. &dwDisposition);
  5925. if(dwRetCode != ERROR_SUCCESS)
  5926. {
  5927. LRSetLastError(dwRetCode);
  5928. dwRetCode = IDS_ERR_REGCREATE_FAILED;
  5929. goto done;
  5930. }
  5931. /*
  5932. //Name
  5933. RegSetValueEx ( hKey,
  5934. szOID_GIVEN_NAME_W,
  5935. 0,
  5936. REG_SZ,
  5937. (CONST BYTE *)(LPCTSTR)sName,
  5938. sName.GetLength() * sizeof(TCHAR)
  5939. );
  5940. //Phone
  5941. RegSetValueEx ( hKey,
  5942. szOID_TELEPHONE_NUMBER_W,
  5943. 0,
  5944. REG_SZ,
  5945. (CONST BYTE *)(LPCTSTR)sPhone,
  5946. sPhone.GetLength() * sizeof(TCHAR)
  5947. );
  5948. //Email Address
  5949. RegSetValueEx ( hKey,
  5950. szOID_RSA_emailAddr_W,
  5951. 0,
  5952. REG_SZ,
  5953. (CONST BYTE *)(LPCTSTR)sEmail,
  5954. sEmail.GetLength() * sizeof(TCHAR)
  5955. );
  5956. */
  5957. done:
  5958. if(hKey)
  5959. RegCloseKey(hKey);
  5960. DisconnectLSRegistry();
  5961. CloseCHRequest();
  5962. DisconnectLS();
  5963. return dwRetCode;
  5964. }
  5965. void CGlobal::SetCSRNumber(TCHAR * tcp)
  5966. {
  5967. SetInRegistry(CSRNUMBER_KEY, tcp);
  5968. lstrcpy(m_lpCSRNumber, tcp);
  5969. return;
  5970. }
  5971. TCHAR * CGlobal::GetCSRNumber(void)
  5972. {
  5973. return m_lpCSRNumber;
  5974. }
  5975. void CGlobal::SetWWWSite(TCHAR * tcp)
  5976. {
  5977. lstrcpy(m_lpWWWSite, tcp);
  5978. return;
  5979. }
  5980. TCHAR * CGlobal::GetWWWSite(void)
  5981. {
  5982. return m_lpWWWSite;
  5983. }
  5984. void CGlobal::SetModifiedRetailSPK(CString sRetailSPK)
  5985. {
  5986. m_sModifiedRetailsSPK = sRetailSPK;
  5987. }
  5988. void CGlobal::GetModifiedRetailSPK(CString &sRetailSPK)
  5989. {
  5990. sRetailSPK = m_sModifiedRetailsSPK;
  5991. }
  5992. DWORD CGlobal::GetLSLangId()
  5993. {
  5994. return m_dwLangId;
  5995. }
  5996. void CGlobal::SetLSLangId(DWORD dwLangId)
  5997. {
  5998. m_dwLangId = dwLangId;
  5999. }
  6000. int CALLBACK EnumFontFamExProc(
  6001. CONST LOGFONTW *lpelfe, // pointer to logical-font data
  6002. CONST TEXTMETRICW *lpntme, // pointer to physical-font data
  6003. DWORD FontType, // type of font
  6004. LPARAM lParam // application-defined data
  6005. )
  6006. {
  6007. LOCALESIGNATURE ls;
  6008. CHARSETINFO cs;
  6009. BOOL rc ;
  6010. DWORD dwLCID = LOCALE_USER_DEFAULT ;
  6011. rc = GetLocaleInfo(dwLCID, LOCALE_FONTSIGNATURE, (LPWSTR)& ls, sizeof(ls) / sizeof(TCHAR));
  6012. rc = TranslateCharsetInfo((ULONG *)lpelfe->lfCharSet, &cs, TCI_SRCCHARSET);
  6013. if (rc != 0)
  6014. rc = GetLastError();
  6015. if (cs.fs.fsCsb[0] & ls.lsCsbSupported[0]){
  6016. // return fontname
  6017. _tcscpy((TCHAR *)lParam, lpelfe->lfFaceName);
  6018. return(0); // return 0 to finish the enumeration
  6019. }
  6020. return(1); // return 1 to continue
  6021. }
  6022. void GetDefaultFont(TCHAR *szFontName, HDC hdc)
  6023. {
  6024. //retrieve the list of installed fonts
  6025. LOGFONT lf ;
  6026. //to enumerate all styles and charsets of all fonts:
  6027. lf.lfFaceName[0] = '\0';
  6028. lf.lfCharSet = DEFAULT_CHARSET;
  6029. lf.lfWeight = FW_BOLD;
  6030. EnumFontFamiliesEx(
  6031. hdc, // handle to device context
  6032. &lf, // pointer to LOGFONT structure
  6033. EnumFontFamExProc, // pointer to callback function
  6034. (LPARAM) szFontName, // application-supplied data
  6035. 0// reserved; must be zero
  6036. );
  6037. }
  6038. #define MARGINX 25//50 // X Margin in 100th of an inch
  6039. #define MARGINY 25//50 // Y Margin in 100th of an inch
  6040. //#define MAX_LABEL 30 // Max Number of chars in a label
  6041. #define MAX_PRINT_CHARS 32
  6042. int WordWrapAndPrint(HDC hdc, LPCTSTR lpcText, HFONT hBoldFont, long lCharHt, int iValueStartPosX, int iLineStartPosY)
  6043. {
  6044. TCHAR szBuffer[1024];
  6045. TCHAR *lpTemp = NULL;
  6046. if (NULL == lpcText)
  6047. {
  6048. return 0;
  6049. }
  6050. _tcscpy(szBuffer, lpcText);
  6051. lpTemp = _tcstok(szBuffer,L"\r\n");
  6052. // If no data , just go to the next line
  6053. if(lpTemp == NULL)
  6054. iLineStartPosY -= lCharHt;
  6055. while(lpTemp)
  6056. {
  6057. while (_tcslen(lpTemp) > MAX_PRINT_CHARS){
  6058. SelectObject(hdc, hBoldFont);
  6059. TextOut(hdc,iValueStartPosX,iLineStartPosY,lpTemp,MAX_PRINT_CHARS);
  6060. iLineStartPosY -= lCharHt;
  6061. lpTemp += MAX_PRINT_CHARS ;
  6062. }
  6063. if (_tcslen(lpTemp) > 0){
  6064. SelectObject(hdc, hBoldFont);
  6065. TextOut(hdc,iValueStartPosX,iLineStartPosY,lpTemp,_tcslen(lpTemp));
  6066. iLineStartPosY -= lCharHt;
  6067. lpTemp = _tcstok(NULL,L"\r\n");
  6068. }
  6069. }
  6070. return iLineStartPosY ;
  6071. }
  6072. UINT GetMaxLabelLength(HDC hdc, HFONT m_hNormalFont, HINSTANCE hInstance)
  6073. {
  6074. TCHAR tcLabel[512] = {0};
  6075. int iTextExtent;
  6076. int iLen;
  6077. SIZE size;
  6078. LoadString(hInstance, IDS_FAX, tcLabel, 512);
  6079. iLen = _tcslen(tcLabel);
  6080. SelectObject(hdc, m_hNormalFont);
  6081. GetTextExtentPoint32( hdc,
  6082. tcLabel,
  6083. iLen,
  6084. &size );
  6085. iTextExtent = size.cx;
  6086. LoadString(hInstance, IDS_RETURN_FAX, tcLabel, 512);
  6087. iLen = _tcslen(tcLabel);
  6088. GetTextExtentPoint32( hdc,
  6089. tcLabel,
  6090. iLen,
  6091. &size );
  6092. if (size.cx > iTextExtent)
  6093. iTextExtent = size.cx;
  6094. return(iTextExtent ) ;
  6095. }