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

6903 lines
151 KiB

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