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 8.9KB

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