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.

313 lines
10 KiB

  1. /*
  2. * REG.c Registry functions
  3. *
  4. */
  5. #include <windows.h>
  6. #include <cryptdlg.h>
  7. #include <wab.h>
  8. #include "demand.h"
  9. #include "data.h"
  10. #include "reg.h"
  11. #include "smimetst.h"
  12. #include "receipt.h"
  13. // Options
  14. TCHAR szSenderEmail[CCH_OPTION_STRING] = "";
  15. TCHAR szSenderName[CCH_OPTION_STRING] = "";
  16. SBinary SenderEntryID = {0};
  17. TCHAR szRecipientEmail[CCH_OPTION_STRING] = "";
  18. TCHAR szRecipientName[CCH_OPTION_STRING] = "";
  19. SBinary RecipientEntryID = {0};
  20. TCHAR szOutputFile[CCH_OPTION_STRING] = "c:\\SMimeTst.eml";
  21. TCHAR szInputFile[CCH_OPTION_STRING] = "";
  22. CRYPT_HASH_BLOB SignHash = {0, NULL};
  23. DWORD CMyNames = 0;
  24. PCERT_NAME_BLOB RgMyNames = {0};
  25. const TCHAR szRegKey[] = "Software\\Microsoft\\SMimeTest";
  26. DWORD SaveMailListKeys(HKEY);
  27. DWORD GetMailListKeys(HKEY);
  28. DWORD RegSaveLong(HKEY hKey, LPTSTR szName, ULONG lData) {
  29. DWORD dwErr;
  30. dwErr = RegSetValueEx(hKey, szName, 0, REG_DWORD, (LPBYTE)&lData, sizeof(lData));
  31. return(dwErr);
  32. }
  33. DWORD RegSaveShort(HKEY hKey, LPTSTR szName, USHORT sData) {
  34. return(RegSaveLong(hKey, szName, (ULONG)sData));
  35. }
  36. DWORD RegSaveBinary(HKEY hKey, LPTSTR szName, LPBYTE pData, ULONG cbData) {
  37. DWORD dwErr;
  38. dwErr = RegSetValueEx(hKey, szName, 0, REG_BINARY, pData, cbData);
  39. return(dwErr);
  40. }
  41. DWORD RegSaveString(HKEY hKey, LPTSTR szName, LPTSTR pszData) {
  42. DWORD dwErr;
  43. dwErr = RegSetValueEx(hKey, szName, 0, REG_SZ, (LPBYTE)pszData, lstrlen(pszData) + 1);
  44. return(dwErr);
  45. }
  46. DWORD RegGetShort(HKEY hKey, LPTSTR szName, USHORT * psData) {
  47. DWORD dwData = 0;
  48. DWORD cbData = sizeof(dwData);
  49. DWORD dwErr;
  50. DWORD dwType = 0;
  51. dwErr = RegQueryValueEx(hKey, szName, 0, &dwType, (LPBYTE)&dwData, &cbData);
  52. *psData = (USHORT)dwData;
  53. return(dwErr);
  54. }
  55. DWORD RegGetLong(HKEY hKey, LPTSTR szName, ULONG * plData) {
  56. DWORD dwErr;
  57. DWORD cbData = sizeof(*plData);
  58. DWORD dwType = 0;
  59. dwErr = RegQueryValueEx(hKey, szName, 0, &dwType, (LPBYTE)plData, &cbData);
  60. return(dwErr);
  61. }
  62. DWORD RegGetBinary(HKEY hKey, LPTSTR szName, LPBYTE * ppData, ULONG * lpcbData) {
  63. DWORD dwErr;
  64. DWORD dwType = 0;
  65. DWORD cbData = 0;
  66. LPBYTE pData = NULL;
  67. if (*ppData) {
  68. LocalFree(*ppData);
  69. *ppData = NULL;
  70. *lpcbData = 0;
  71. }
  72. dwErr = RegQueryValueEx(hKey, szName, 0, &dwType, pData, &cbData);
  73. if ((dwErr == ERROR_SUCCESS) && (cbData > 0)) {
  74. pData = (LPBYTE)LocalAlloc(LPTR, cbData);
  75. *lpcbData = cbData;
  76. dwErr = RegQueryValueEx(hKey, szName, 0, &dwType, pData, lpcbData);
  77. *ppData = pData;
  78. }
  79. return(dwErr);
  80. }
  81. DWORD RegGetString(HKEY hKey, LPTSTR szName, LPTSTR pszData) {
  82. DWORD dwErr;
  83. DWORD dwType = 0;
  84. DWORD cbData = CCH_OPTION_STRING;
  85. TCHAR szExpanded[CCH_OPTION_STRING];
  86. dwErr = RegQueryValueEx(hKey, szName, 0, &dwType, (LPBYTE)pszData, &cbData);
  87. if ((REG_EXPAND_SZ == dwType) && (cbData > 0)) {
  88. ExpandEnvironmentStrings(pszData, szExpanded, cbData);
  89. lstrcpy(pszData, szExpanded);
  90. }
  91. return(dwErr);
  92. }
  93. void SaveOptions(void) {
  94. HKEY hKey = NULL;
  95. DWORD dwErr;
  96. DWORD dwDisposition;
  97. if (! (dwErr = RegCreateKeyEx(HKEY_CURRENT_USER, szRegKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey, &dwDisposition))) {
  98. RegSaveString(hKey, "Sender Email", szSenderEmail);
  99. RegSaveString(hKey, "Sender Name", szSenderName);
  100. if (SenderEntryID.lpb) {
  101. RegSaveBinary(hKey, "Sender EntryID", (LPBYTE)SenderEntryID.lpb, SenderEntryID.cb);
  102. } else {
  103. RegDeleteValue(hKey, "Sender EntryID");
  104. }
  105. RegSaveString(hKey, "Recipient Email", szRecipientEmail);
  106. RegSaveString(hKey, "Recipient Name", szRecipientName);
  107. if (RecipientEntryID.lpb) {
  108. RegSaveBinary(hKey, "Recipient EntryID", (LPBYTE)RecipientEntryID.lpb, RecipientEntryID.cb);
  109. } else {
  110. RegDeleteValue(hKey, "Recipient EntryID");
  111. }
  112. RegSaveString(hKey, "Output File", szOutputFile);
  113. RegSaveString(hKey, "Input File", szInputFile);
  114. RegSaveBinary(hKey, "Signing Hash", SignHash.pbData, SignHash.cbData);
  115. if (CMyNames > 0) {
  116. CRYPT_SEQUENCE_OF_ANY any = {CMyNames, RgMyNames};
  117. DWORD cb;
  118. BOOL f;
  119. LPBYTE pb;
  120. f = CryptEncodeObjectEx(X509_ASN_ENCODING, X509_SEQUENCE_OF_ANY,
  121. &any, CRYPT_ENCODE_ALLOC_FLAG, NULL,
  122. &pb, &cb);
  123. RegSaveBinary(hKey, "My Names", pb, cb);
  124. LocalFree(pb);
  125. }
  126. else {
  127. RegDeleteValue(hKey, "My Names");
  128. }
  129. SaveMailListKeys(hKey);
  130. RegCloseKey(hKey);
  131. }
  132. }
  133. void GetOptions(void) {
  134. HKEY hKey = NULL;
  135. DWORD dwErr;
  136. if (! (dwErr = RegOpenKeyEx(HKEY_CURRENT_USER, szRegKey, 0, KEY_READ, &hKey))) {
  137. RegGetString(hKey, "Sender Email", szSenderEmail);
  138. RegGetString(hKey, "Sender Name", szSenderName);
  139. RegGetBinary(hKey, "Sender EntryID", &SenderEntryID.lpb, &SenderEntryID.cb);
  140. RegGetString(hKey, "Recipient Email", szRecipientEmail);
  141. RegGetString(hKey, "Recipient Name", szRecipientName);
  142. RegGetBinary(hKey, "Recipient EntryID", &RecipientEntryID.lpb, &RecipientEntryID.cb);
  143. RegGetString(hKey, "Output File", szOutputFile);
  144. RegGetString(hKey, "Input File", szInputFile);
  145. RegGetBinary(hKey, "Signing Hash", &SignHash.pbData, &SignHash.cbData);
  146. {
  147. DWORD cb = 0;
  148. PCRYPT_SEQUENCE_OF_ANY pany;
  149. LPBYTE pb = NULL;
  150. RegGetBinary(hKey, "My Names", &pb, &cb);
  151. if (cb > 0) {
  152. CryptDecodeObjectEx(X509_ASN_ENCODING, X509_SEQUENCE_OF_ANY,
  153. pb, cb, CRYPT_ENCODE_ALLOC_FLAG, NULL,
  154. &pany, &cb);
  155. RgMyNames = (PCERT_NAME_BLOB) LocalAlloc(0, cb);
  156. memcpy(RgMyNames, pany->rgValue, cb);
  157. CMyNames = pany->cValue;
  158. LocalFree(pany);
  159. }
  160. if (pb != NULL) LocalFree(pb);
  161. }
  162. GetMailListKeys(hKey);
  163. RegCloseKey(hKey);
  164. }
  165. }
  166. void CleanupOptions(void) {
  167. if (SenderEntryID.lpb) {
  168. LocalFree(SenderEntryID.lpb);
  169. SenderEntryID.lpb = NULL;
  170. }
  171. if (RecipientEntryID.lpb) {
  172. LocalFree(RecipientEntryID.lpb);
  173. RecipientEntryID.lpb = NULL;
  174. }
  175. if (RgMyNames != NULL) {
  176. LocalFree(RgMyNames);
  177. RgMyNames = 0;
  178. }
  179. }
  180. //// OptionsDlgProc
  181. //
  182. // Description: This is the dialog proc function which controls the preset
  183. // options for a user. These options are stored in the registry code and
  184. // are on a per-user basis.
  185. //
  186. BOOL OptionsDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  187. {
  188. PCCERT_CONTEXT pccert;
  189. char rgch[256];
  190. switch (message) {
  191. case WM_INITDIALOG:
  192. // Fill in the fields from the options
  193. SetDlgItemText(hwnd, IDC_SENDER_EMAIL, szSenderEmail);
  194. // SetDlgItemText(hwnd, IDC_SENDER_NAME, szSenderName);
  195. SetDlgItemText(hwnd, IDC_RECIPIENT_EMAIL, szRecipientEmail);
  196. // SetDlgItemText(hwnd, IDC_RECIPIENT_NAME, szRecipientName);
  197. if (HCertStoreMy == NULL) {
  198. HCertStoreMy = CertOpenStore(CERT_STORE_PROV_SYSTEM, X509_ASN_ENCODING,
  199. NULL, CERT_SYSTEM_STORE_CURRENT_USER,
  200. L"MY");
  201. if (HCertStoreMy == NULL) {
  202. break;
  203. }
  204. }
  205. if (SignHash.cbData != 0) {
  206. pccert = CertFindCertificateInStore(HCertStoreMy,
  207. X509_ASN_ENCODING, 0,
  208. CERT_FIND_SHA1_HASH,
  209. &SignHash, NULL);
  210. GetFriendlyNameOfCertA(pccert, rgch, sizeof(rgch));
  211. SetDlgItemText(hwnd, IDC_O_CERT_NAME, rgch);
  212. CertFreeCertificateContext(pccert);
  213. }
  214. if (CMyNames > 0) {
  215. FormatNames(CMyNames, RgMyNames, hwnd, IDC_O_MY_NAMES);
  216. }
  217. break;
  218. case WM_COMMAND :
  219. switch (wParam) {
  220. case IDOK:
  221. GetDlgItemText(hwnd, IDC_SENDER_EMAIL, szSenderEmail, CCH_OPTION_STRING);
  222. // GetDlgItemText(hwnd, IDC_SENDER_NAME, szSenderName, CCH_OPTION_STRING);
  223. GetDlgItemText(hwnd, IDC_RECIPIENT_EMAIL, szRecipientEmail, CCH_OPTION_STRING);
  224. // GetDlgItemText(hwnd, IDC_RECIPIENT_NAME, szRecipientName, CCH_OPTION_STRING);
  225. if (!ParseNames(&CMyNames, &RgMyNames, hwnd, IDC_O_MY_NAMES)) {
  226. return FALSE;
  227. }
  228. EndDialog(hwnd, 0);
  229. break;
  230. case IDC_O_CERT_CHOOSE:
  231. pccert = NULL;
  232. if (SignHash.cbData != 0) {
  233. pccert = CertFindCertificateInStore(HCertStoreMy,
  234. X509_ASN_ENCODING, 0,
  235. CERT_FIND_SHA1_HASH,
  236. &SignHash, NULL);
  237. }
  238. if (DoCertDialog(hwnd, "Choose Signature Certificate",
  239. HCertStoreMy, &pccert, FILTER_NONE)) {
  240. SignHash.pbData = RgbSignHash;
  241. SignHash.cbData = sizeof(RgbSignHash);
  242. CertGetCertificateContextProperty(pccert, CERT_SHA1_HASH_PROP_ID,
  243. SignHash.pbData,
  244. &SignHash.cbData);
  245. GetFriendlyNameOfCertA(pccert, rgch, sizeof(rgch));
  246. SetDlgItemText(hwnd, IDC_O_CERT_NAME, rgch);
  247. }
  248. CertFreeCertificateContext(pccert);
  249. break;
  250. default:
  251. return(FALSE);
  252. }
  253. break ;
  254. default:
  255. return(FALSE);
  256. }
  257. return(TRUE);
  258. }