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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(bool _anon) : session(0), dh_params(0),
  43. anon_cred(0), cert_cred(0),
  44. anon(_anon), fis(0), fos(0)
  45. {
  46. certfile = X509_CertFile.getData();
  47. keyfile = X509_KeyFile.getData();
  48. if (gnutls_global_init() != GNUTLS_E_SUCCESS)
  49. throw AuthFailureException("gnutls_global_init failed");
  50. }
  51. void SSecurityTLS::shutdown()
  52. {
  53. if (session) {
  54. if (gnutls_bye(session, GNUTLS_SHUT_RDWR) != GNUTLS_E_SUCCESS) {
  55. /* FIXME: Treat as non-fatal error */
  56. vlog.error("TLS session wasn't terminated gracefully");
  57. }
  58. }
  59. if (dh_params) {
  60. gnutls_dh_params_deinit(dh_params);
  61. dh_params = 0;
  62. }
  63. if (anon_cred) {
  64. gnutls_anon_free_server_credentials(anon_cred);
  65. anon_cred = 0;
  66. }
  67. if (cert_cred) {
  68. gnutls_certificate_free_credentials(cert_cred);
  69. cert_cred = 0;
  70. }
  71. if (session) {
  72. gnutls_deinit(session);
  73. session = 0;
  74. }
  75. }
  76. SSecurityTLS::~SSecurityTLS()
  77. {
  78. shutdown();
  79. if (fis)
  80. delete fis;
  81. if (fos)
  82. delete fos;
  83. delete[] keyfile;
  84. delete[] certfile;
  85. gnutls_global_deinit();
  86. }
  87. bool SSecurityTLS::processMsg(SConnection *sc)
  88. {
  89. rdr::InStream* is = sc->getInStream();
  90. rdr::OutStream* os = sc->getOutStream();
  91. vlog.debug("Process security message (session %p)", session);
  92. if (!session) {
  93. if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
  94. throw AuthFailureException("gnutls_init failed");
  95. if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
  96. throw AuthFailureException("gnutls_set_default_priority failed");
  97. try {
  98. setParams(session);
  99. }
  100. catch(...) {
  101. os->writeU8(0);
  102. throw;
  103. }
  104. os->writeU8(1);
  105. os->flush();
  106. }
  107. rdr::TLSInStream *tlsis = new rdr::TLSInStream(is, session);
  108. rdr::TLSOutStream *tlsos = new rdr::TLSOutStream(os, session);
  109. int err;
  110. err = gnutls_handshake(session);
  111. if (err != GNUTLS_E_SUCCESS) {
  112. delete tlsis;
  113. delete tlsos;
  114. if (!gnutls_error_is_fatal(err)) {
  115. vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
  116. return false;
  117. }
  118. vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
  119. shutdown();
  120. throw AuthFailureException("TLS Handshake failed");
  121. }
  122. vlog.debug("Handshake completed");
  123. sc->setStreams(fis = tlsis, fos = tlsos);
  124. return true;
  125. }
  126. void SSecurityTLS::setParams(gnutls_session_t session)
  127. {
  128. static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
  129. int ret;
  130. char *prio;
  131. const char *err;
  132. prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
  133. strlen(kx_anon_priority) + 1);
  134. if (prio == NULL)
  135. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  136. strcpy(prio, Security::GnuTLSPriority);
  137. if (anon)
  138. strcat(prio, kx_anon_priority);
  139. ret = gnutls_priority_set_direct(session, prio, &err);
  140. free(prio);
  141. if (ret != GNUTLS_E_SUCCESS) {
  142. if (ret == GNUTLS_E_INVALID_REQUEST)
  143. vlog.error("GnuTLS priority syntax error at: %s", err);
  144. throw AuthFailureException("gnutls_set_priority_direct failed");
  145. }
  146. if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
  147. throw AuthFailureException("gnutls_dh_params_init failed");
  148. if (gnutls_dh_params_generate2(dh_params, DH_BITS) != GNUTLS_E_SUCCESS)
  149. throw AuthFailureException("gnutls_dh_params_generate2 failed");
  150. if (anon) {
  151. if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
  152. throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
  153. gnutls_anon_set_server_dh_params(anon_cred, dh_params);
  154. if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
  155. != GNUTLS_E_SUCCESS)
  156. throw AuthFailureException("gnutls_credentials_set failed");
  157. vlog.debug("Anonymous session has been set");
  158. } else {
  159. if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
  160. throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
  161. gnutls_certificate_set_dh_params(cert_cred, dh_params);
  162. switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
  163. case GNUTLS_E_SUCCESS:
  164. break;
  165. case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
  166. throw AuthFailureException("Private key does not match certificate");
  167. case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
  168. throw AuthFailureException("Unsupported certificate type");
  169. default:
  170. throw AuthFailureException("Error loading X509 certificate or key");
  171. }
  172. if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
  173. != GNUTLS_E_SUCCESS)
  174. throw AuthFailureException("gnutls_credentials_set failed");
  175. vlog.debug("X509 session has been set");
  176. }
  177. }