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

404 lines
12 KiB

  1. #include <windows.h>
  2. #include <wincrypt.h>
  3. #include <unicode.h>
  4. #include "ui.h"
  5. #include "instres.h"
  6. #include "resource.h"
  7. #include <malloc.h>
  8. #include <assert.h>
  9. //+-------------------------------------------------------------------------
  10. // Formats multi bytes into WCHAR hex. Includes a space after every 4 bytes.
  11. //
  12. // Needs (cb * 2 + cb/4 + 1) characters in wsz
  13. //--------------------------------------------------------------------------
  14. static void FormatMsgBoxMultiBytes(DWORD cb, BYTE *pb, LPWSTR wsz)
  15. {
  16. for (DWORD i = 0; i<cb; i++) {
  17. int b;
  18. if (i && 0 == (i & 1))
  19. *wsz++ = L' ';
  20. b = (*pb & 0xF0) >> 4;
  21. *wsz++ = (b <= 9) ? b + L'0' : (b - 10) + L'A';
  22. b = *pb & 0x0F;
  23. *wsz++ = (b <= 9) ? b + L'0' : (b - 10) + L'A';
  24. pb++;
  25. }
  26. *wsz++ = 0;
  27. }
  28. INT_PTR CALLBACK MoreInfoDialogProc(
  29. HWND hwndDlg, // handle to dialog box
  30. UINT uMsg, // message
  31. WPARAM wParam, // first message parameter
  32. LPARAM lParam // second message parameter
  33. ) {
  34. PMIU pmiu = NULL;
  35. FILETIME ftLocal;
  36. SYSTEMTIME stLocal;
  37. DWORD dwChar;
  38. LPWSTR wszName;
  39. BYTE rgbHash[MAX_HASH_LEN];
  40. DWORD cbHash = MAX_HASH_LEN;
  41. HWND hwnd;
  42. CRYPTUI_VIEWCERTIFICATE_STRUCTW cryptUI;
  43. WCHAR wsz[128];
  44. switch(uMsg) {
  45. case WM_CLOSE:
  46. EndDialog(hwndDlg, 0);
  47. return(0);
  48. break;
  49. case WM_INITDIALOG:
  50. // remember my imput data
  51. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  52. pmiu = (PMIU) lParam;
  53. // hide the window if we don't have a cryptUI dll
  54. if(NULL == pmiu->pfnCryptUIDlgViewCertificateW &&
  55. NULL != (hwnd = GetDlgItem(hwndDlg, IDC_CAINFO_VIEWCERT)) )
  56. ShowWindow(hwnd, SW_HIDE);
  57. // put in the name
  58. if(0 != (dwChar=CertNameToStrW(
  59. X509_ASN_ENCODING,
  60. &pmiu->pCertContext->pCertInfo->Subject,
  61. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  62. NULL,
  63. 0
  64. ) )) {
  65. wszName = (LPWSTR) _alloca(sizeof(WCHAR) * dwChar);
  66. if(dwChar == CertNameToStrW(
  67. X509_ASN_ENCODING,
  68. &pmiu->pCertContext->pCertInfo->Subject,
  69. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  70. wszName,
  71. dwChar
  72. ) ) {
  73. SendDlgItemMessageU(
  74. hwndDlg,
  75. IDC_CAINFO_NAME,
  76. WM_SETTEXT,
  77. 0,
  78. (LPARAM) wszName);
  79. }
  80. }
  81. wsz[0] = 0;
  82. FileTimeToLocalFileTime(&pmiu->pCertContext->pCertInfo->NotAfter, &ftLocal);
  83. FileTimeToSystemTime(&ftLocal, &stLocal);
  84. GetDateFormatU(LOCALE_USER_DEFAULT, DATE_LONGDATE, &stLocal, NULL, wsz, 128);
  85. // put not after date
  86. SendDlgItemMessageU(
  87. hwndDlg,
  88. IDC_CAINFO_EXPIRATION_DATE,
  89. WM_SETTEXT,
  90. 0,
  91. (LPARAM) wsz);
  92. // get the sha1 thumbprint
  93. if (CertGetCertificateContextProperty(
  94. pmiu->pCertContext,
  95. CERT_SHA1_HASH_PROP_ID,
  96. rgbHash,
  97. &cbHash)) {
  98. FormatMsgBoxMultiBytes(cbHash, rgbHash, wsz);
  99. SendDlgItemMessageU(
  100. hwndDlg,
  101. IDC_CAINFO_THUMBPRINT,
  102. WM_SETTEXT,
  103. 0,
  104. (LPARAM) wsz);
  105. }
  106. // put in the thumbprint alg
  107. // no localization needed
  108. SendDlgItemMessageU(
  109. hwndDlg,
  110. IDC_CAINFO_THUMBPRINT_ALGORITHM,
  111. WM_SETTEXT,
  112. 0,
  113. (LPARAM) L"SHA1");
  114. return(TRUE);
  115. break;
  116. case WM_COMMAND:
  117. switch(HIWORD(wParam)) {
  118. case BN_CLICKED:
  119. switch(LOWORD(wParam)) {
  120. case IDOK:
  121. case IDCANCEL:
  122. EndDialog(hwndDlg, LOWORD(wParam));
  123. return(TRUE);
  124. break;
  125. case IDC_CAINFO_VIEWCERT:
  126. GetWindowLongPtr(hwndDlg, DWLP_USER);
  127. if(NULL != (pmiu = (PMIU) GetWindowLongPtr(hwndDlg, DWLP_USER)) &&
  128. NULL != pmiu->pfnCryptUIDlgViewCertificateW ) {
  129. memset(&cryptUI, 0, sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW));
  130. cryptUI.dwSize = sizeof(CRYPTUI_VIEWCERTIFICATE_STRUCTW);
  131. cryptUI.pCertContext = pmiu->pCertContext;
  132. cryptUI.hwndParent = hwndDlg;
  133. cryptUI.dwFlags =
  134. CRYPTUI_DISABLE_ADDTOSTORE | CRYPTUI_IGNORE_UNTRUSTED_ROOT;
  135. pmiu->pfnCryptUIDlgViewCertificateW(&cryptUI, NULL);
  136. return(TRUE);
  137. }
  138. break;
  139. }
  140. break;
  141. }
  142. break;
  143. }
  144. return(FALSE);
  145. }
  146. int MoreInfoDlg(
  147. HWND hDlgBox,
  148. int idLB
  149. ) {
  150. PCCERT_CONTEXT pCertContext;
  151. PMDI pmdi = (PMDI) GetWindowLongPtr(hDlgBox, DWLP_USER);
  152. INT_PTR iItem;
  153. MIU miu;
  154. // What is currently selected
  155. iItem = SendDlgItemMessageA(
  156. hDlgBox,
  157. idLB,
  158. LB_GETCURSEL,
  159. 0,
  160. 0
  161. );
  162. if(iItem == LB_ERR)
  163. return(LB_ERR);
  164. // get the pCertContext
  165. pCertContext = (PCCERT_CONTEXT) SendDlgItemMessageA(
  166. hDlgBox,
  167. idLB,
  168. LB_GETITEMDATA,
  169. (WPARAM) iItem,
  170. 0
  171. );
  172. if(pCertContext == (PCCERT_CONTEXT) LB_ERR || pCertContext == NULL)
  173. return(LB_ERR);
  174. // set up the parameters for the more info dialog
  175. miu.pCertContext = pCertContext;
  176. miu.hInstance = pmdi->hInstance;
  177. miu.pfnCryptUIDlgViewCertificateW = pmdi->pfnCryptUIDlgViewCertificateW;
  178. // put the dialog up
  179. DialogBoxParam(
  180. pmdi->hInstance,
  181. (LPSTR) MAKEINTRESOURCE(IDD_CAINFO),
  182. hDlgBox,
  183. MoreInfoDialogProc,
  184. (LPARAM) &miu);
  185. return(0);
  186. }
  187. int AddCertNameToListBox(
  188. PCCERT_CONTEXT pCertContext,
  189. HWND hDlgBox,
  190. int idLB
  191. ) {
  192. int itemIndex;
  193. DWORD dwChar;
  194. LPWSTR wszName;
  195. if(0 == (dwChar=CertNameToStrW(
  196. X509_ASN_ENCODING,
  197. &pCertContext->pCertInfo->Subject,
  198. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  199. NULL,
  200. 0
  201. ) ))
  202. return(LB_ERR);
  203. wszName = (LPWSTR) _alloca(sizeof(WCHAR) * dwChar); // no error checking, will stack fault, not return NULL
  204. if(dwChar != CertNameToStrW(
  205. X509_ASN_ENCODING,
  206. &pCertContext->pCertInfo->Subject,
  207. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  208. wszName,
  209. dwChar
  210. ) )
  211. return(LB_ERR);
  212. itemIndex = (int) SendDlgItemMessageU(
  213. hDlgBox,
  214. idLB,
  215. LB_ADDSTRING,
  216. 0,
  217. (LPARAM) wszName) ;
  218. if(LB_ERR == itemIndex || LB_ERRSPACE == itemIndex)
  219. return(itemIndex);
  220. if(LB_ERR == SendDlgItemMessageA(
  221. hDlgBox,
  222. idLB,
  223. LB_SETITEMDATA,
  224. (WPARAM) itemIndex,
  225. (LPARAM) CertDuplicateCertificateContext(pCertContext)
  226. ) )
  227. return(LB_ERR);
  228. return(0);
  229. }
  230. INT_PTR CALLBACK MainDialogProc(
  231. HWND hwndDlg, // handle to dialog box
  232. UINT uMsg, // message
  233. WPARAM wParam, // first message parameter
  234. LPARAM lParam // second message parameter
  235. ) {
  236. PMDI pmdi = NULL;
  237. PCCERT_CONTEXT pCertContext = NULL;
  238. WCHAR wrgDisclaimer[4096]; // because legal stuff is long
  239. DWORD dwChar;
  240. LPWSTR wszName;
  241. switch(uMsg) {
  242. case WM_CLOSE:
  243. EndDialog(hwndDlg, IDNO);
  244. return(0);
  245. break;
  246. case WM_INITDIALOG:
  247. pmdi = (PMDI) lParam;
  248. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  249. // put in the signer name
  250. if(0 != (dwChar=CertNameToStrW(
  251. X509_ASN_ENCODING,
  252. &pmdi->pCertSigner->pCertInfo->Subject,
  253. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  254. NULL,
  255. 0
  256. ) )) {
  257. wszName = (LPWSTR) _alloca(sizeof(WCHAR) * dwChar);
  258. if(dwChar == CertNameToStrW(
  259. X509_ASN_ENCODING,
  260. &pmdi->pCertSigner->pCertInfo->Subject,
  261. CERT_SIMPLE_NAME_STR | CERT_NAME_STR_REVERSE_FLAG,
  262. wszName,
  263. dwChar
  264. ) ) {
  265. SendDlgItemMessageU(
  266. hwndDlg,
  267. IDC_INSTALLCA_VERIFIER,
  268. WM_SETTEXT,
  269. 0,
  270. (LPARAM) wszName);
  271. }
  272. }
  273. // set legal disclaimer
  274. LoadStringU(pmdi->hInstance, IDS_LEGALDISCLAIMER, wrgDisclaimer, sizeof(wrgDisclaimer)/sizeof(WCHAR));
  275. SendDlgItemMessageU(
  276. hwndDlg,
  277. IDC_INSTALLCA_LEGALDISCLAIMER,
  278. WM_SETTEXT,
  279. 0,
  280. (LPARAM) wrgDisclaimer) ;
  281. // add each cert to the list box
  282. while(NULL != (pCertContext = CertEnumCertificatesInStore(pmdi->hStore, pCertContext)))
  283. AddCertNameToListBox(pCertContext, hwndDlg, IDC_INSTALLCA_CALIST);
  284. // set the selection to the first item, don't worry about errors
  285. SendDlgItemMessageU(
  286. hwndDlg,
  287. IDC_INSTALLCA_CALIST,
  288. LB_SETCURSEL,
  289. 0,
  290. 0);
  291. return(TRUE);
  292. break;
  293. case WM_COMMAND:
  294. switch(HIWORD(wParam)) {
  295. case BN_CLICKED:
  296. switch(LOWORD(wParam)) {
  297. case IDYES:
  298. case IDNO:
  299. case IDCANCEL:
  300. EndDialog(hwndDlg, LOWORD(wParam));
  301. return(TRUE);
  302. case IDC_INSTALLCA_MOREINFO:
  303. MoreInfoDlg(hwndDlg, IDC_INSTALLCA_CALIST);
  304. return(TRUE);
  305. }
  306. break;
  307. case LBN_DBLCLK:
  308. switch(LOWORD(wParam)) {
  309. case IDC_INSTALLCA_CALIST:
  310. MoreInfoDlg(hwndDlg, IDC_INSTALLCA_CALIST);
  311. return(TRUE);
  312. }
  313. break;
  314. }
  315. }
  316. return(FALSE);
  317. }
  318. BOOL FIsTooManyCertsOK(DWORD cCerts, HINSTANCE hInstanceUI) {
  319. WCHAR wszT[MAX_MSG_LEN];
  320. WCHAR wszT1[MAX_MSG_LEN];
  321. // if too many, ask the user if you wan to continue
  322. if(cCerts > CACERTWARNINGLEVEL) {
  323. LoadStringU(hInstanceUI, IDS_INSTALLCA, wszT1, sizeof(wszT1)/sizeof(WCHAR));
  324. LoadStringU(hInstanceUI, IDS_TOO_MANY_CA_CERTS, wszT, sizeof(wszT)/sizeof(WCHAR));
  325. return(IDYES == MessageBoxU(NULL, wszT, wszT1, MB_YESNO));
  326. }
  327. return(TRUE);
  328. }