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.

SSecurityTLS.cxx 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. char *prio;
  144. const char *err;
  145. prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
  146. strlen(kx_anon_priority) + 1);
  147. if (prio == NULL)
  148. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  149. strcpy(prio, Security::GnuTLSPriority);
  150. if (anon)
  151. strcat(prio, kx_anon_priority);
  152. ret = gnutls_priority_set_direct(session, prio, &err);
  153. free(prio);
  154. if (ret != GNUTLS_E_SUCCESS) {
  155. if (ret == GNUTLS_E_INVALID_REQUEST)
  156. vlog.error("GnuTLS priority syntax error at: %s", err);
  157. throw AuthFailureException("gnutls_set_priority_direct failed");
  158. }
  159. if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
  160. throw AuthFailureException("gnutls_dh_params_init failed");
  161. if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
  162. throw AuthFailureException("gnutls_dh_params_generate2 failed");
  163. if (anon) {
  164. if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
  165. throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
  166. gnutls_anon_set_server_dh_params(anon_cred, dh_params);
  167. if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
  168. != GNUTLS_E_SUCCESS)
  169. throw AuthFailureException("gnutls_credentials_set failed");
  170. vlog.debug("Anonymous session has been set");
  171. } else {
  172. if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
  173. throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
  174. gnutls_certificate_set_dh_params(cert_cred, dh_params);
  175. switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
  176. case GNUTLS_E_SUCCESS:
  177. break;
  178. case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
  179. throw AuthFailureException("Private key does not match certificate");
  180. case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
  181. throw AuthFailureException("Unsupported certificate type");
  182. default:
  183. throw AuthFailureException("Error loading X509 certificate or key");
  184. }
  185. if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
  186. != GNUTLS_E_SUCCESS)
  187. throw AuthFailureException("gnutls_credentials_set failed");
  188. vlog.debug("X509 session has been set");
  189. }
  190. }