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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. certfile = X509_CertFile.getData();
  67. keyfile = X509_KeyFile.getData();
  68. if (gnutls_global_init() != GNUTLS_E_SUCCESS)
  69. throw AuthFailureException("gnutls_global_init failed");
  70. }
  71. void SSecurityTLS::shutdown()
  72. {
  73. if (session) {
  74. int ret;
  75. // FIXME: We can't currently wait for the response, so we only send
  76. // our close and hope for the best
  77. ret = gnutls_bye(session, GNUTLS_SHUT_WR);
  78. if ((ret != GNUTLS_E_SUCCESS) && (ret != GNUTLS_E_INVALID_SESSION))
  79. vlog.error("TLS shutdown failed: %s", gnutls_strerror(ret));
  80. }
  81. #if defined (SSECURITYTLS__USE_DEPRECATED_DH)
  82. if (dh_params) {
  83. gnutls_dh_params_deinit(dh_params);
  84. dh_params = 0;
  85. }
  86. #endif
  87. if (anon_cred) {
  88. gnutls_anon_free_server_credentials(anon_cred);
  89. anon_cred = 0;
  90. }
  91. if (cert_cred) {
  92. gnutls_certificate_free_credentials(cert_cred);
  93. cert_cred = 0;
  94. }
  95. if (rawis && rawos) {
  96. sc->setStreams(rawis, rawos);
  97. rawis = NULL;
  98. rawos = NULL;
  99. }
  100. if (tlsis) {
  101. delete tlsis;
  102. tlsis = NULL;
  103. }
  104. if (tlsos) {
  105. delete tlsos;
  106. tlsos = NULL;
  107. }
  108. if (session) {
  109. gnutls_deinit(session);
  110. session = 0;
  111. }
  112. }
  113. SSecurityTLS::~SSecurityTLS()
  114. {
  115. shutdown();
  116. delete[] keyfile;
  117. delete[] certfile;
  118. gnutls_global_deinit();
  119. }
  120. bool SSecurityTLS::processMsg()
  121. {
  122. vlog.debug("Process security message (session %p)", session);
  123. if (!session) {
  124. rdr::InStream* is = sc->getInStream();
  125. rdr::OutStream* os = sc->getOutStream();
  126. if (gnutls_init(&session, GNUTLS_SERVER) != GNUTLS_E_SUCCESS)
  127. throw AuthFailureException("gnutls_init failed");
  128. if (gnutls_set_default_priority(session) != GNUTLS_E_SUCCESS)
  129. throw AuthFailureException("gnutls_set_default_priority failed");
  130. try {
  131. setParams(session);
  132. }
  133. catch(...) {
  134. os->writeU8(0);
  135. throw;
  136. }
  137. os->writeU8(1);
  138. os->flush();
  139. // Create these early as they set up the push/pull functions
  140. // for GnuTLS
  141. tlsis = new rdr::TLSInStream(is, session);
  142. tlsos = new rdr::TLSOutStream(os, session);
  143. rawis = is;
  144. rawos = os;
  145. }
  146. int err;
  147. err = gnutls_handshake(session);
  148. if (err != GNUTLS_E_SUCCESS) {
  149. if (!gnutls_error_is_fatal(err)) {
  150. vlog.debug("Deferring completion of TLS handshake: %s", gnutls_strerror(err));
  151. return false;
  152. }
  153. vlog.error("TLS Handshake failed: %s", gnutls_strerror (err));
  154. shutdown();
  155. throw AuthFailureException("TLS Handshake failed");
  156. }
  157. vlog.debug("TLS handshake completed with %s",
  158. gnutls_session_get_desc(session));
  159. sc->setStreams(tlsis, tlsos);
  160. return true;
  161. }
  162. void SSecurityTLS::setParams(gnutls_session_t session)
  163. {
  164. static const char kx_anon_priority[] = ":+ANON-ECDH:+ANON-DH";
  165. int ret;
  166. // Custom priority string specified?
  167. if (strcmp(Security::GnuTLSPriority, "") != 0) {
  168. char *prio;
  169. const char *err;
  170. prio = (char*)malloc(strlen(Security::GnuTLSPriority) +
  171. strlen(kx_anon_priority) + 1);
  172. if (prio == NULL)
  173. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  174. strcpy(prio, Security::GnuTLSPriority);
  175. if (anon)
  176. strcat(prio, kx_anon_priority);
  177. ret = gnutls_priority_set_direct(session, prio, &err);
  178. free(prio);
  179. if (ret != GNUTLS_E_SUCCESS) {
  180. if (ret == GNUTLS_E_INVALID_REQUEST)
  181. vlog.error("GnuTLS priority syntax error at: %s", err);
  182. throw AuthFailureException("gnutls_set_priority_direct failed");
  183. }
  184. } else if (anon) {
  185. const char *err;
  186. #if GNUTLS_VERSION_NUMBER >= 0x030603
  187. // gnutls_set_default_priority_appends() expects a normal priority string that
  188. // doesn't start with ":".
  189. ret = gnutls_set_default_priority_append(session, kx_anon_priority + 1, &err, 0);
  190. if (ret != GNUTLS_E_SUCCESS) {
  191. if (ret == GNUTLS_E_INVALID_REQUEST)
  192. vlog.error("GnuTLS priority syntax error at: %s", err);
  193. throw AuthFailureException("gnutls_set_default_priority_append failed");
  194. }
  195. #else
  196. // We don't know what the system default priority is, so we guess
  197. // it's what upstream GnuTLS has
  198. static const char gnutls_default_priority[] = "NORMAL";
  199. char *prio;
  200. prio = (char*)malloc(strlen(gnutls_default_priority) +
  201. strlen(kx_anon_priority) + 1);
  202. if (prio == NULL)
  203. throw AuthFailureException("Not enough memory for GnuTLS priority string");
  204. strcpy(prio, gnutls_default_priority);
  205. strcat(prio, kx_anon_priority);
  206. ret = gnutls_priority_set_direct(session, prio, &err);
  207. free(prio);
  208. if (ret != GNUTLS_E_SUCCESS) {
  209. if (ret == GNUTLS_E_INVALID_REQUEST)
  210. vlog.error("GnuTLS priority syntax error at: %s", err);
  211. throw AuthFailureException("gnutls_set_priority_direct failed");
  212. }
  213. #endif
  214. }
  215. #if defined (SSECURITYTLS__USE_DEPRECATED_DH)
  216. if (gnutls_dh_params_init(&dh_params) != GNUTLS_E_SUCCESS)
  217. throw AuthFailureException("gnutls_dh_params_init failed");
  218. if (gnutls_dh_params_import_pkcs3(dh_params, &ffdhe_pkcs3_param, GNUTLS_X509_FMT_PEM) != GNUTLS_E_SUCCESS)
  219. throw AuthFailureException("gnutls_dh_params_import_pkcs3 failed");
  220. #endif
  221. if (anon) {
  222. if (gnutls_anon_allocate_server_credentials(&anon_cred) != GNUTLS_E_SUCCESS)
  223. throw AuthFailureException("gnutls_anon_allocate_server_credentials failed");
  224. #if defined (SSECURITYTLS__USE_DEPRECATED_DH)
  225. gnutls_anon_set_server_dh_params(anon_cred, dh_params);
  226. #endif
  227. if (gnutls_credentials_set(session, GNUTLS_CRD_ANON, anon_cred)
  228. != GNUTLS_E_SUCCESS)
  229. throw AuthFailureException("gnutls_credentials_set failed");
  230. vlog.debug("Anonymous session has been set");
  231. } else {
  232. if (gnutls_certificate_allocate_credentials(&cert_cred) != GNUTLS_E_SUCCESS)
  233. throw AuthFailureException("gnutls_certificate_allocate_credentials failed");
  234. #if defined (SSECURITYTLS__USE_DEPRECATED_DH)
  235. gnutls_certificate_set_dh_params(cert_cred, dh_params);
  236. #endif
  237. switch (gnutls_certificate_set_x509_key_file(cert_cred, certfile, keyfile, GNUTLS_X509_FMT_PEM)) {
  238. case GNUTLS_E_SUCCESS:
  239. break;
  240. case GNUTLS_E_CERTIFICATE_KEY_MISMATCH:
  241. throw AuthFailureException("Private key does not match certificate");
  242. case GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE:
  243. throw AuthFailureException("Unsupported certificate type");
  244. default:
  245. throw AuthFailureException("Error loading X509 certificate or key");
  246. }
  247. if (gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cert_cred)
  248. != GNUTLS_E_SUCCESS)
  249. throw AuthFailureException("gnutls_credentials_set failed");
  250. vlog.debug("X509 session has been set");
  251. }
  252. }