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.

Authentication.h 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #ifndef WINVNCCONF_AUTHENTICATION
  19. #define WINVNCCONF_AUTHENTICATION
  20. #include <windows.h>
  21. #include <commctrl.h>
  22. #ifdef HAVE_GNUTLS
  23. #include <assert.h>
  24. #include <gnutls/gnutls.h>
  25. #include <gnutls/x509.h>
  26. #define CHECK(x) assert((x)>=0)
  27. #endif
  28. #include <vncconfig/PasswordDialog.h>
  29. #include <rfb_win32/Registry.h>
  30. #include <rfb_win32/SecurityPage.h>
  31. #include <rfb_win32/MsgBox.h>
  32. #include <rfb/ServerCore.h>
  33. #include <rfb/Security.h>
  34. #include <rfb/SecurityServer.h>
  35. #include <rfb/SSecurityVncAuth.h>
  36. #include <rfb/SSecurityTLS.h>
  37. #include <rfb/Password.h>
  38. static rfb::BoolParameter queryOnlyIfLoggedOn("QueryOnlyIfLoggedOn",
  39. "Only prompt for a local user to accept incoming connections if there is a user logged on", false);
  40. namespace rfb {
  41. namespace win32 {
  42. class SecPage : public SecurityPage {
  43. public:
  44. SecPage(const RegKey& rk)
  45. : SecurityPage(NULL), regKey(rk) {
  46. security = new SecurityServer();
  47. }
  48. void initDialog() {
  49. SecurityPage::initDialog();
  50. setItemChecked(IDC_QUERY_CONNECT, rfb::Server::queryConnect);
  51. setItemChecked(IDC_QUERY_LOGGED_ON, queryOnlyIfLoggedOn);
  52. onCommand(IDC_AUTH_NONE, 0);
  53. }
  54. bool onCommand(int id, int cmd) {
  55. SecurityPage::onCommand(id, cmd);
  56. setChanged(true);
  57. if (id == IDC_AUTH_VNC_PASSWD) {
  58. PasswordDialog passwdDlg(regKey, registryInsecure);
  59. passwdDlg.showDialog(handle);
  60. } else if (id == IDC_LOAD_CERT) {
  61. const TCHAR* title = _T("X509Cert");
  62. const TCHAR* filter =
  63. _T("X.509 Certificates (*.crt;*.cer;*.pem)\0*.crt;*.cer;*.pem\0All\0*.*\0");
  64. showFileChooser(regKey, title, filter, handle);
  65. } else if (id == IDC_LOAD_CERTKEY) {
  66. const TCHAR* title = _T("X509Key");
  67. const TCHAR* filter = _T("X.509 Keys (*.key;*.pem)\0*.key;*.pem\0All\0*.*\0");
  68. showFileChooser(regKey, title, filter, handle);
  69. } else if (id == IDC_QUERY_LOGGED_ON) {
  70. enableItem(IDC_QUERY_LOGGED_ON, enableQueryOnlyIfLoggedOn());
  71. }
  72. return true;
  73. }
  74. bool onOk() {
  75. SecurityPage::onOk();
  76. if (isItemChecked(IDC_AUTH_VNC))
  77. verifyVncPassword(regKey);
  78. else if (haveVncPassword() &&
  79. MsgBox(0, _T("The VNC authentication method is disabled, but a password is still stored for it.\n")
  80. _T("Do you want to remove the VNC authentication password from the registry?"),
  81. MB_ICONWARNING | MB_YESNO) == IDYES) {
  82. regKey.setBinary(_T("Password"), 0, 0);
  83. }
  84. #ifdef HAVE_GNUTLS
  85. if (isItemChecked(IDC_ENC_X509)) {
  86. gnutls_certificate_credentials_t xcred;
  87. CHECK(gnutls_global_init());
  88. CHECK(gnutls_certificate_allocate_credentials(&xcred));
  89. int ret = gnutls_certificate_set_x509_key_file (xcred,
  90. regKey.getString("X509Cert"),
  91. regKey.getString("X509Key"),
  92. GNUTLS_X509_FMT_PEM);
  93. if (ret >= 0) {
  94. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Cert"));
  95. SSecurityTLS::X509_CertFile.setParam(regKey.getString("X509Key"));
  96. } else {
  97. if (ret == GNUTLS_E_CERTIFICATE_KEY_MISMATCH) {
  98. MsgBox(0, _T("Private key does not match certificate.\n")
  99. _T("X.509 security types will not be enabled!"),
  100. MB_ICONWARNING | MB_OK);
  101. } else if (ret == GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE) {
  102. MsgBox(0, _T("Unsupported certificate type.\n")
  103. _T("X.509 security types will not be enabled!"),
  104. MB_ICONWARNING | MB_OK);
  105. } else {
  106. MsgBox(0, _T("Unknown error while importing X.509 certificate or private key.\n")
  107. _T("X.509 security types will not be enabled!"),
  108. MB_ICONWARNING | MB_OK);
  109. }
  110. }
  111. gnutls_global_deinit();
  112. }
  113. #endif
  114. regKey.setString(_T("SecurityTypes"), security->ToString());
  115. regKey.setBool(_T("QueryConnect"), isItemChecked(IDC_QUERY_CONNECT));
  116. regKey.setBool(_T("QueryOnlyIfLoggedOn"), isItemChecked(IDC_QUERY_LOGGED_ON));
  117. return true;
  118. }
  119. void setWarnPasswdInsecure(bool warn) {
  120. registryInsecure = warn;
  121. }
  122. bool enableQueryOnlyIfLoggedOn() {
  123. return isItemChecked(IDC_QUERY_CONNECT);
  124. }
  125. static bool haveVncPassword() {
  126. PlainPasswd password, passwordReadOnly;
  127. SSecurityVncAuth::vncAuthPasswd.getVncAuthPasswd(&password, &passwordReadOnly);
  128. return password.buf && strlen(password.buf) != 0;
  129. }
  130. static void verifyVncPassword(const RegKey& regKey) {
  131. if (!haveVncPassword()) {
  132. MsgBox(0, _T("The VNC authentication method is enabled, but no password is specified.\n")
  133. _T("The password dialog will now be shown."), MB_ICONINFORMATION | MB_OK);
  134. PasswordDialog passwd(regKey, registryInsecure);
  135. passwd.showDialog();
  136. }
  137. }
  138. virtual void loadX509Certs(void) {}
  139. virtual void enableX509Dialogs(void) {
  140. enableItem(IDC_LOAD_CERT, true);
  141. enableItem(IDC_LOAD_CERTKEY, true);
  142. }
  143. virtual void disableX509Dialogs(void) {
  144. enableItem(IDC_LOAD_CERT, false);
  145. enableItem(IDC_LOAD_CERTKEY, false);
  146. }
  147. virtual void loadVncPasswd() {
  148. enableItem(IDC_AUTH_VNC_PASSWD, isItemChecked(IDC_AUTH_VNC));
  149. }
  150. protected:
  151. RegKey regKey;
  152. static bool registryInsecure;
  153. private:
  154. inline void modifyAuthMethod(int enc_idc, int auth_idc, bool enable)
  155. {
  156. setItemChecked(enc_idc, enable);
  157. setItemChecked(auth_idc, enable);
  158. }
  159. inline bool showFileChooser(const RegKey& rk,
  160. const char* title,
  161. const char* filter,
  162. HWND hwnd)
  163. {
  164. OPENFILENAME ofn;
  165. char filename[MAX_PATH];
  166. ZeroMemory(&ofn, sizeof(ofn));
  167. ZeroMemory(&filename, sizeof(filename));
  168. filename[0] = '\0';
  169. ofn.lStructSize = sizeof(ofn);
  170. ofn.hwndOwner = hwnd;
  171. ofn.lpstrFile = filename;
  172. ofn.nMaxFile = sizeof(filename);
  173. ofn.lpstrFilter = (char*)filter;
  174. ofn.nFilterIndex = 1;
  175. ofn.lpstrFileTitle = NULL;
  176. ofn.nMaxFileTitle = 0;
  177. ofn.lpstrTitle = (char*)title;
  178. ofn.lpstrInitialDir = NULL;
  179. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  180. if (GetOpenFileName(&ofn)==TRUE) {
  181. regKey.setString(title, filename);
  182. return true;
  183. }
  184. return false;
  185. }
  186. };
  187. };
  188. bool SecPage::registryInsecure = false;
  189. };
  190. #endif