Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

SSecurityTLS.cxx 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2004 Red Hat Inc.
  3. * Copyright (C) 2005 Martin Koegler
  4. * Copyright (C) 2010 TigerVNC Team
  5. *
  6. * This is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This software is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this software; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  19. * USA.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #ifndef HAVE_GNUTLS
  25. #error "This source should not be compiled without HAVE_GNUTLS defined"
  26. #endif
  27. #include <stdlib.h>
  28. #include <rfb/SSecurityTLS.h>
  29. #include <rfb/SConnection.h>
  30. #include <rfb/LogWriter.h>
  31. #include <rfb/Exception.h>
  32. #include <rdr/TLSInStream.h>
  33. #include <rdr/TLSOutStream.h>
  34. #include <gnutls/x509.h>
  35. #define DH_BITS 1024 /* XXX This should be configurable! */
  36. using namespace rfb;
  37. StringParameter SSecurityTLS::X509_CertFile
  38. ("X509Cert", "Path to the X509 certificate in PEM format", "", ConfServer);
  39. StringParameter SSecurityTLS::X509_KeyFile
  40. ("X509Key", "Path to the key of the X509 certificate in PEM format", "", ConfServer);
  41. static LogWriter vlog("TLS");
  42. SSecurityTLS::SSecurityTLS(SConnection* sc, bool _anon)
  43. : SSecurity(sc), session(NULL), dh_params(NULL), anon_cred(NULL),
  44. cert_cred(NULL), anon(_anon), tlsis(NULL), tlsos(NULL),
  45. rawis(NULL), rawos(NULL)
  46. {
  47. certfile = X509_CertFile.getData();
  48. keyfile = X509_KeyFile.getData();
  49. if (gnutls_global_init() != GNUTLS_E_SUCCESS)
  50. throw AuthFailureException("gnutls_global_init failed");
  51. }
  52. void SSecurityTLS::shutdown()
  53. {
  54. if (session) {
  55. if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) {
  56. /* FIXME: Treat as non-fatal error */
  57. vlog.error("TLS session wasn't terminated gracefully");
  58. }
  59. }
  60. if (dh_params) {
  61. gnutls_dh_params_deinit(dh_params);
  62. dh_params = 0;
  63. }
  64. if (anon_cred) {
  65. gnutls_anon_free_server_credentials(anon_cred);
  66. anon_cred = 0;
  67. }
  68. if (cert_cred) {
  69. gnutls_certificate_free_credentials(cert_cred);
  70. cert_cred = 0;
  71. }
  72. if (rawis && rawos) {
  73. sc->setStreams(rawis, rawos);
  74. rawis = NULL;
  75. rawos = NULL;
  76. }
  77. if (tlsis) {
  78. delete tlsis;
  79. tlsis = NULL;
  80. }
  81. if (tlsos) {
  82. delete tlsos;
  83. tlsos = NULL;
  84. }
  85. if (session) {
  86. gnutls_deinit(session);
  87. session = 0;
  88. }
  89. }
  90. SSecurityTLS::~SSecurityTLS()
  91. {
  92. shutdown();
  93. delete[] keyfile;
  94. delete[] certfile;
  95. gnutls_global_deinit();
  96. }
  97. bool SSecurityTLS::processMsg()
  98. {
  99. vlog.debug("Process security message (session %p)", session);
  100. if (!session) {
  101. rdr::InStream* is = sc->getInStream();
  102. rdr::OutStream* os = sc->getOutStream();
  103. if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
  104. throw AuthFailureException("gnutls_init failed");
  105. if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
  106. throw AuthFailureException("gnutls_set_default_priority failed");
  107. try {
  108. setParams(session);
  109. }
  110. catch(...) {
  111. os->writeU8(0);
  112. throw;
  113. }
  114. os->writeU8(1);
  115. os->flush();
  116. // Create these early as they set up the push/pull functions
  117. // for GnuTLS
  118. tlsis = new rdr::TLSInStream(is, session);
  119. tlsos = new rdr::TLSOutStream(os, session);
  120. rawis = is;
  121. rawos = os;
  122. }
  123. int err;
  124. err = gnutls_handshake(session);
  125. if (err != GNUTLS_E_SUCCESS) {
  126. if (!gnutls_error_is_fatal(err)) {
  127. vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
  128. return false;
  129. }
  130. vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
  131. shutdown();
  132. throw AuthFailureException("TLS Handshake failed");
  133. }
  134. vlog.debug("TLS handshake completed with %s",
  135. gnutls_session_get_desc(session));
  136. sc->setStreams(tlsis, tlsos);
  137. return true;
  138. }
  139. void SSecurityTLS::setParams(gnutls_session_t session)
  140. {
  141. static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
  142. int ret;
  143. // Custom priority string specified?
  144. if (strcmp(Security::GnuTLSPriority, "") != 0) {
  145. char *prio;
  146. const char *err;
  147. prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
  148. strlen(kx_anon_priority) + 1);
  149. if (prio == NULL)
  150. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  151. strcpy(prio, Security::GnuTLSPriority);
  152. if (anon)
  153. strcat(prio, kx_anon_priority);
  154. ret = gnutls_priority_set_direct(session, prio, &err);
  155. free(prio);
  156. if (ret != GNUTLS_E_SUCCESS) {
  157. if (ret == GNUTLS_E_INVALID_REQUEST)
  158. vlog.error("GnuTLS priority syntax error at: %s", err);
  159. throw AuthFailureException("gnutls_set_priority_direct failed");
  160. }
  161. } else if (anon) {
  162. const char *err;
  163. #if GNUTLS_VERSION_NUMBER >= 0x030603
  164. ret = gnutls_set_default_priority_append(session, kx_anon_priority, &err, 0);
  165. if (ret != GNUTLS_E_SUCCESS) {
  166. if (ret == GNUTLS_E_INVALID_REQUEST)
  167. vlog.error("GnuTLS priority syntax error at: %s", err);
  168. throw AuthFailureException("gnutls_set_default_priority_append failed");
  169. }
  170. #else
  171. // We don't know what the system default priority is, so we guess
  172. // it's what upstream GnuTLS has
  173. static const char gnutls_default_priority[] = "NORMAL";
  174. char *prio;
  175. prio = (char*)malloc(strlen(gnutls_default_priority) +
  176. strlen(kx_anon_priority) + 1);
  177. if (prio == NULL)
  178. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  179. strcpy(prio, gnutls_default_priority);
  180. strcat(prio, kx_anon_priority);
  181. ret = gnutls_priority_set_direct(session, prio, &err);
  182. free(prio);
  183. if (ret != GNUTLS_E_SUCCESS) {
  184. if (ret == GNUTLS_E_INVALID_REQUEST)
  185. vlog.error("GnuTLS priority syntax error at: %s", err);
  186. throw AuthFailureException("gnutls_set_priority_direct failed");
  187. }
  188. #endif
  189. }
  190. if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
  191. throw AuthFailureException("gnutls_dh_params_init failed");
  192. if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
  193. throw AuthFailureException("gnutls_dh_params_generate2 failed");
  194. if (anon) {
  195. if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
  196. throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
  197. gnutls_anon_set_server_dh_params(anon_cred, dh_params);
  198. if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
  199. != GNUTLS_E_SUCCESS)
  200. throw AuthFailureException("gnutls_credentials_set failed");
  201. vlog.debug("Anonymous session has been set");
  202. } else {
  203. if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
  204. throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
  205. gnutls_certificate_set_dh_params(cert_cred, dh_params);
  206. switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
  207. case GNUTLS_E_SUCCESS:
  208. break;
  209. case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
  210. throw AuthFailureException("Private key does not match certificate");
  211. case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
  212. throw AuthFailureException("Unsupported certificate type");
  213. default:
  214. throw AuthFailureException("Error loading X509 certificate or key");
  215. }
  216. if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
  217. != GNUTLS_E_SUCCESS)
  218. throw AuthFailureException("gnutls_credentials_set failed");
  219. vlog.debug("X509 session has been set");
  220. }
  221. }