vnc-auth-sasl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * QEMU VNC display driver: SASL auth protocol
  3. *
  4. * Copyright (C) 2009 Red Hat, Inc
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qapi/error.h"
  26. #include "authz/base.h"
  27. #include "vnc.h"
  28. #include "trace.h"
  29. /*
  30. * Apple has deprecated sasl.h functions in OS X 10.11. Therefore,
  31. * files that use SASL API need to disable -Wdeprecated-declarations.
  32. */
  33. #ifdef CONFIG_DARWIN
  34. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  35. #endif
  36. /* Max amount of data we send/recv for SASL steps to prevent DOS */
  37. #define SASL_DATA_MAX_LEN (1024 * 1024)
  38. bool vnc_sasl_server_init(Error **errp)
  39. {
  40. int saslErr = sasl_server_init(NULL, "qemu");
  41. if (saslErr != SASL_OK) {
  42. error_setg(errp, "Failed to initialize SASL auth: %s",
  43. sasl_errstring(saslErr, NULL, NULL));
  44. return false;
  45. }
  46. return true;
  47. }
  48. void vnc_sasl_client_cleanup(VncState *vs)
  49. {
  50. if (vs->sasl.conn) {
  51. vs->sasl.runSSF = false;
  52. vs->sasl.wantSSF = false;
  53. vs->sasl.waitWriteSSF = 0;
  54. vs->sasl.encodedLength = vs->sasl.encodedOffset = 0;
  55. vs->sasl.encoded = NULL;
  56. g_free(vs->sasl.username);
  57. g_free(vs->sasl.mechlist);
  58. vs->sasl.username = vs->sasl.mechlist = NULL;
  59. sasl_dispose(&vs->sasl.conn);
  60. vs->sasl.conn = NULL;
  61. }
  62. }
  63. size_t vnc_client_write_sasl(VncState *vs)
  64. {
  65. size_t ret;
  66. VNC_DEBUG("Write SASL: Pending output %p size %zd offset %zd "
  67. "Encoded: %p size %d offset %d\n",
  68. vs->output.buffer, vs->output.capacity, vs->output.offset,
  69. vs->sasl.encoded, vs->sasl.encodedLength, vs->sasl.encodedOffset);
  70. if (!vs->sasl.encoded) {
  71. int err;
  72. err = sasl_encode(vs->sasl.conn,
  73. (char *)vs->output.buffer,
  74. vs->output.offset,
  75. (const char **)&vs->sasl.encoded,
  76. &vs->sasl.encodedLength);
  77. if (err != SASL_OK)
  78. return vnc_client_io_error(vs, -1, NULL);
  79. vs->sasl.encodedRawLength = vs->output.offset;
  80. vs->sasl.encodedOffset = 0;
  81. }
  82. ret = vnc_client_write_buf(vs,
  83. vs->sasl.encoded + vs->sasl.encodedOffset,
  84. vs->sasl.encodedLength - vs->sasl.encodedOffset);
  85. if (!ret)
  86. return 0;
  87. vs->sasl.encodedOffset += ret;
  88. if (vs->sasl.encodedOffset == vs->sasl.encodedLength) {
  89. bool throttled = vs->force_update_offset != 0;
  90. size_t offset;
  91. if (vs->sasl.encodedRawLength >= vs->force_update_offset) {
  92. vs->force_update_offset = 0;
  93. } else {
  94. vs->force_update_offset -= vs->sasl.encodedRawLength;
  95. }
  96. if (throttled && vs->force_update_offset == 0) {
  97. trace_vnc_client_unthrottle_forced(vs, vs->ioc);
  98. }
  99. offset = vs->output.offset;
  100. buffer_advance(&vs->output, vs->sasl.encodedRawLength);
  101. if (offset >= vs->throttle_output_offset &&
  102. vs->output.offset < vs->throttle_output_offset) {
  103. trace_vnc_client_unthrottle_incremental(vs, vs->ioc,
  104. vs->output.offset);
  105. }
  106. vs->sasl.encoded = NULL;
  107. vs->sasl.encodedOffset = vs->sasl.encodedLength = 0;
  108. }
  109. /* Can't merge this block with one above, because
  110. * someone might have written more unencrypted
  111. * data in vs->output while we were processing
  112. * SASL encoded output
  113. */
  114. if (vs->output.offset == 0) {
  115. if (vs->ioc_tag) {
  116. g_source_remove(vs->ioc_tag);
  117. }
  118. vs->ioc_tag = qio_channel_add_watch(
  119. vs->ioc, G_IO_IN | G_IO_HUP | G_IO_ERR,
  120. vnc_client_io, vs, NULL);
  121. }
  122. return ret;
  123. }
  124. size_t vnc_client_read_sasl(VncState *vs)
  125. {
  126. size_t ret;
  127. uint8_t encoded[4096];
  128. const char *decoded;
  129. unsigned int decodedLen;
  130. int err;
  131. ret = vnc_client_read_buf(vs, encoded, sizeof(encoded));
  132. if (!ret)
  133. return 0;
  134. err = sasl_decode(vs->sasl.conn,
  135. (char *)encoded, ret,
  136. &decoded, &decodedLen);
  137. if (err != SASL_OK)
  138. return vnc_client_io_error(vs, -1, NULL);
  139. VNC_DEBUG("Read SASL Encoded %p size %ld Decoded %p size %d\n",
  140. encoded, ret, decoded, decodedLen);
  141. buffer_reserve(&vs->input, decodedLen);
  142. buffer_append(&vs->input, decoded, decodedLen);
  143. return decodedLen;
  144. }
  145. static int vnc_auth_sasl_check_access(VncState *vs)
  146. {
  147. const void *val;
  148. int rv;
  149. Error *err = NULL;
  150. bool allow;
  151. rv = sasl_getprop(vs->sasl.conn, SASL_USERNAME, &val);
  152. if (rv != SASL_OK) {
  153. trace_vnc_auth_fail(vs, vs->auth, "Cannot fetch SASL username",
  154. sasl_errstring(rv, NULL, NULL));
  155. return -1;
  156. }
  157. if (val == NULL) {
  158. trace_vnc_auth_fail(vs, vs->auth, "No SASL username set", "");
  159. return -1;
  160. }
  161. vs->sasl.username = g_strdup((const char*)val);
  162. trace_vnc_auth_sasl_username(vs, vs->sasl.username);
  163. if (vs->vd->sasl.authzid == NULL) {
  164. trace_vnc_auth_sasl_acl(vs, 1);
  165. return 0;
  166. }
  167. allow = qauthz_is_allowed_by_id(vs->vd->sasl.authzid,
  168. vs->sasl.username, &err);
  169. if (err) {
  170. trace_vnc_auth_fail(vs, vs->auth, "Error from authz",
  171. error_get_pretty(err));
  172. error_free(err);
  173. return -1;
  174. }
  175. trace_vnc_auth_sasl_acl(vs, allow);
  176. return allow ? 0 : -1;
  177. }
  178. static int vnc_auth_sasl_check_ssf(VncState *vs)
  179. {
  180. const void *val;
  181. int err, ssf;
  182. if (!vs->sasl.wantSSF)
  183. return 1;
  184. err = sasl_getprop(vs->sasl.conn, SASL_SSF, &val);
  185. if (err != SASL_OK)
  186. return 0;
  187. ssf = *(const int *)val;
  188. trace_vnc_auth_sasl_ssf(vs, ssf);
  189. if (ssf < 56)
  190. return 0; /* 56 is good for Kerberos */
  191. /* Only setup for read initially, because we're about to send an RPC
  192. * reply which must be in plain text. When the next incoming RPC
  193. * arrives, we'll switch on writes too
  194. *
  195. * cf qemudClientReadSASL in qemud.c
  196. */
  197. vs->sasl.runSSF = 1;
  198. /* We have a SSF that's good enough */
  199. return 1;
  200. }
  201. /*
  202. * Step Msg
  203. *
  204. * Input from client:
  205. *
  206. * u32 clientin-length
  207. * u8-array clientin-string
  208. *
  209. * Output to client:
  210. *
  211. * u32 serverout-length
  212. * u8-array serverout-strin
  213. * u8 continue
  214. */
  215. static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len);
  216. static int protocol_client_auth_sasl_step(VncState *vs, uint8_t *data, size_t len)
  217. {
  218. uint32_t datalen = len;
  219. const char *serverout;
  220. unsigned int serveroutlen;
  221. int err;
  222. char *clientdata = NULL;
  223. /* NB, distinction of NULL vs "" is *critical* in SASL */
  224. if (datalen) {
  225. clientdata = (char*)data;
  226. clientdata[datalen-1] = '\0'; /* Wire includes '\0', but make sure */
  227. datalen--; /* Don't count NULL byte when passing to _start() */
  228. }
  229. err = sasl_server_step(vs->sasl.conn,
  230. clientdata,
  231. datalen,
  232. &serverout,
  233. &serveroutlen);
  234. trace_vnc_auth_sasl_step(vs, data, len, serverout, serveroutlen, err);
  235. if (err != SASL_OK &&
  236. err != SASL_CONTINUE) {
  237. trace_vnc_auth_fail(vs, vs->auth, "Cannot step SASL auth",
  238. sasl_errdetail(vs->sasl.conn));
  239. sasl_dispose(&vs->sasl.conn);
  240. vs->sasl.conn = NULL;
  241. goto authabort;
  242. }
  243. if (serveroutlen > SASL_DATA_MAX_LEN) {
  244. trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", "");
  245. sasl_dispose(&vs->sasl.conn);
  246. vs->sasl.conn = NULL;
  247. goto authabort;
  248. }
  249. if (serveroutlen) {
  250. vnc_write_u32(vs, serveroutlen + 1);
  251. vnc_write(vs, serverout, serveroutlen + 1);
  252. } else {
  253. vnc_write_u32(vs, 0);
  254. }
  255. /* Whether auth is complete */
  256. vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
  257. if (err == SASL_CONTINUE) {
  258. /* Wait for step length */
  259. vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
  260. } else {
  261. if (!vnc_auth_sasl_check_ssf(vs)) {
  262. trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", "");
  263. goto authreject;
  264. }
  265. /* Check the username access control list */
  266. if (vnc_auth_sasl_check_access(vs) < 0) {
  267. goto authreject;
  268. }
  269. trace_vnc_auth_pass(vs, vs->auth);
  270. vnc_write_u32(vs, 0); /* Accept auth */
  271. /*
  272. * Delay writing in SSF encoded mode until pending output
  273. * buffer is written
  274. */
  275. if (vs->sasl.runSSF)
  276. vs->sasl.waitWriteSSF = vs->output.offset;
  277. start_client_init(vs);
  278. }
  279. return 0;
  280. authreject:
  281. vnc_write_u32(vs, 1); /* Reject auth */
  282. vnc_write_u32(vs, sizeof("Authentication failed"));
  283. vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
  284. vnc_flush(vs);
  285. vnc_client_error(vs);
  286. return -1;
  287. authabort:
  288. vnc_client_error(vs);
  289. return -1;
  290. }
  291. static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len)
  292. {
  293. uint32_t steplen = read_u32(data, 0);
  294. if (steplen > SASL_DATA_MAX_LEN) {
  295. trace_vnc_auth_fail(vs, vs->auth, "SASL step len too large", "");
  296. vnc_client_error(vs);
  297. return -1;
  298. }
  299. if (steplen == 0)
  300. return protocol_client_auth_sasl_step(vs, NULL, 0);
  301. else
  302. vnc_read_when(vs, protocol_client_auth_sasl_step, steplen);
  303. return 0;
  304. }
  305. /*
  306. * Start Msg
  307. *
  308. * Input from client:
  309. *
  310. * u32 clientin-length
  311. * u8-array clientin-string
  312. *
  313. * Output to client:
  314. *
  315. * u32 serverout-length
  316. * u8-array serverout-strin
  317. * u8 continue
  318. */
  319. #define SASL_DATA_MAX_LEN (1024 * 1024)
  320. static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len)
  321. {
  322. uint32_t datalen = len;
  323. const char *serverout;
  324. unsigned int serveroutlen;
  325. int err;
  326. char *clientdata = NULL;
  327. /* NB, distinction of NULL vs "" is *critical* in SASL */
  328. if (datalen) {
  329. clientdata = (char*)data;
  330. clientdata[datalen-1] = '\0'; /* Should be on wire, but make sure */
  331. datalen--; /* Don't count NULL byte when passing to _start() */
  332. }
  333. err = sasl_server_start(vs->sasl.conn,
  334. vs->sasl.mechlist,
  335. clientdata,
  336. datalen,
  337. &serverout,
  338. &serveroutlen);
  339. trace_vnc_auth_sasl_start(vs, data, len, serverout, serveroutlen, err);
  340. if (err != SASL_OK &&
  341. err != SASL_CONTINUE) {
  342. trace_vnc_auth_fail(vs, vs->auth, "Cannot start SASL auth",
  343. sasl_errdetail(vs->sasl.conn));
  344. sasl_dispose(&vs->sasl.conn);
  345. vs->sasl.conn = NULL;
  346. goto authabort;
  347. }
  348. if (serveroutlen > SASL_DATA_MAX_LEN) {
  349. trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", "");
  350. sasl_dispose(&vs->sasl.conn);
  351. vs->sasl.conn = NULL;
  352. goto authabort;
  353. }
  354. if (serveroutlen) {
  355. vnc_write_u32(vs, serveroutlen + 1);
  356. vnc_write(vs, serverout, serveroutlen + 1);
  357. } else {
  358. vnc_write_u32(vs, 0);
  359. }
  360. /* Whether auth is complete */
  361. vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
  362. if (err == SASL_CONTINUE) {
  363. /* Wait for step length */
  364. vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
  365. } else {
  366. if (!vnc_auth_sasl_check_ssf(vs)) {
  367. trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", "");
  368. goto authreject;
  369. }
  370. /* Check the username access control list */
  371. if (vnc_auth_sasl_check_access(vs) < 0) {
  372. goto authreject;
  373. }
  374. trace_vnc_auth_pass(vs, vs->auth);
  375. vnc_write_u32(vs, 0); /* Accept auth */
  376. start_client_init(vs);
  377. }
  378. return 0;
  379. authreject:
  380. vnc_write_u32(vs, 1); /* Reject auth */
  381. vnc_write_u32(vs, sizeof("Authentication failed"));
  382. vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
  383. vnc_flush(vs);
  384. vnc_client_error(vs);
  385. return -1;
  386. authabort:
  387. vnc_client_error(vs);
  388. return -1;
  389. }
  390. static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len)
  391. {
  392. uint32_t startlen = read_u32(data, 0);
  393. if (startlen > SASL_DATA_MAX_LEN) {
  394. trace_vnc_auth_fail(vs, vs->auth, "SASL start len too large", "");
  395. vnc_client_error(vs);
  396. return -1;
  397. }
  398. if (startlen == 0)
  399. return protocol_client_auth_sasl_start(vs, NULL, 0);
  400. vnc_read_when(vs, protocol_client_auth_sasl_start, startlen);
  401. return 0;
  402. }
  403. static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len)
  404. {
  405. char *mechname = g_strndup((const char *) data, len);
  406. trace_vnc_auth_sasl_mech_choose(vs, mechname);
  407. if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
  408. if (vs->sasl.mechlist[len] != '\0' &&
  409. vs->sasl.mechlist[len] != ',') {
  410. goto fail;
  411. }
  412. } else {
  413. char *offset = strstr(vs->sasl.mechlist, mechname);
  414. if (!offset) {
  415. goto fail;
  416. }
  417. if (offset[-1] != ',' ||
  418. (offset[len] != '\0'&&
  419. offset[len] != ',')) {
  420. goto fail;
  421. }
  422. }
  423. g_free(vs->sasl.mechlist);
  424. vs->sasl.mechlist = mechname;
  425. vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4);
  426. return 0;
  427. fail:
  428. trace_vnc_auth_fail(vs, vs->auth, "Unsupported mechname", mechname);
  429. vnc_client_error(vs);
  430. g_free(mechname);
  431. return -1;
  432. }
  433. static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len)
  434. {
  435. uint32_t mechlen = read_u32(data, 0);
  436. if (mechlen > 100) {
  437. trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too long", "");
  438. vnc_client_error(vs);
  439. return -1;
  440. }
  441. if (mechlen < 1) {
  442. trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too short", "");
  443. vnc_client_error(vs);
  444. return -1;
  445. }
  446. vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen);
  447. return 0;
  448. }
  449. static char *
  450. vnc_socket_ip_addr_string(QIOChannelSocket *ioc,
  451. bool local,
  452. Error **errp)
  453. {
  454. SocketAddress *addr;
  455. char *ret;
  456. if (local) {
  457. addr = qio_channel_socket_get_local_address(ioc, errp);
  458. } else {
  459. addr = qio_channel_socket_get_remote_address(ioc, errp);
  460. }
  461. if (!addr) {
  462. return NULL;
  463. }
  464. if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
  465. error_setg(errp, "Not an inet socket type");
  466. qapi_free_SocketAddress(addr);
  467. return NULL;
  468. }
  469. ret = g_strdup_printf("%s;%s", addr->u.inet.host, addr->u.inet.port);
  470. qapi_free_SocketAddress(addr);
  471. return ret;
  472. }
  473. void start_auth_sasl(VncState *vs)
  474. {
  475. const char *mechlist = NULL;
  476. sasl_security_properties_t secprops;
  477. int err;
  478. Error *local_err = NULL;
  479. char *localAddr, *remoteAddr;
  480. int mechlistlen;
  481. /* Get local & remote client addresses in form IPADDR;PORT */
  482. localAddr = vnc_socket_ip_addr_string(vs->sioc, true, &local_err);
  483. if (!localAddr) {
  484. trace_vnc_auth_fail(vs, vs->auth, "Cannot format local IP",
  485. error_get_pretty(local_err));
  486. goto authabort;
  487. }
  488. remoteAddr = vnc_socket_ip_addr_string(vs->sioc, false, &local_err);
  489. if (!remoteAddr) {
  490. trace_vnc_auth_fail(vs, vs->auth, "Cannot format remote IP",
  491. error_get_pretty(local_err));
  492. g_free(localAddr);
  493. goto authabort;
  494. }
  495. err = sasl_server_new("vnc",
  496. NULL, /* FQDN - just delegates to gethostname */
  497. NULL, /* User realm */
  498. localAddr,
  499. remoteAddr,
  500. NULL, /* Callbacks, not needed */
  501. SASL_SUCCESS_DATA,
  502. &vs->sasl.conn);
  503. g_free(localAddr);
  504. g_free(remoteAddr);
  505. localAddr = remoteAddr = NULL;
  506. if (err != SASL_OK) {
  507. trace_vnc_auth_fail(vs, vs->auth, "SASL context setup failed",
  508. sasl_errstring(err, NULL, NULL));
  509. vs->sasl.conn = NULL;
  510. goto authabort;
  511. }
  512. /* Inform SASL that we've got an external SSF layer from TLS/x509 */
  513. if (vs->auth == VNC_AUTH_VENCRYPT &&
  514. vs->subauth == VNC_AUTH_VENCRYPT_X509SASL) {
  515. int keysize;
  516. sasl_ssf_t ssf;
  517. keysize = qcrypto_tls_session_get_key_size(vs->tls,
  518. &local_err);
  519. if (keysize < 0) {
  520. trace_vnc_auth_fail(vs, vs->auth, "cannot TLS get cipher size",
  521. error_get_pretty(local_err));
  522. sasl_dispose(&vs->sasl.conn);
  523. vs->sasl.conn = NULL;
  524. goto authabort;
  525. }
  526. ssf = keysize * CHAR_BIT; /* tls key size is bytes, sasl wants bits */
  527. err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf);
  528. if (err != SASL_OK) {
  529. trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL external SSF",
  530. sasl_errstring(err, NULL, NULL));
  531. sasl_dispose(&vs->sasl.conn);
  532. vs->sasl.conn = NULL;
  533. goto authabort;
  534. }
  535. } else {
  536. vs->sasl.wantSSF = 1;
  537. }
  538. memset (&secprops, 0, sizeof secprops);
  539. /* Inform SASL that we've got an external SSF layer from TLS.
  540. *
  541. * Disable SSF, if using TLS+x509+SASL only. TLS without x509
  542. * is not sufficiently strong
  543. */
  544. if (vs->vd->is_unix ||
  545. (vs->auth == VNC_AUTH_VENCRYPT &&
  546. vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)) {
  547. /* If we've got TLS or UNIX domain sock, we don't care about SSF */
  548. secprops.min_ssf = 0;
  549. secprops.max_ssf = 0;
  550. secprops.maxbufsize = 8192;
  551. secprops.security_flags = 0;
  552. } else {
  553. /* Plain TCP, better get an SSF layer */
  554. secprops.min_ssf = 56; /* Good enough to require kerberos */
  555. secprops.max_ssf = 100000; /* Arbitrary big number */
  556. secprops.maxbufsize = 8192;
  557. /* Forbid any anonymous or trivially crackable auth */
  558. secprops.security_flags =
  559. SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
  560. }
  561. err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops);
  562. if (err != SASL_OK) {
  563. trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL security props",
  564. sasl_errstring(err, NULL, NULL));
  565. sasl_dispose(&vs->sasl.conn);
  566. vs->sasl.conn = NULL;
  567. goto authabort;
  568. }
  569. err = sasl_listmech(vs->sasl.conn,
  570. NULL, /* Don't need to set user */
  571. "", /* Prefix */
  572. ",", /* Separator */
  573. "", /* Suffix */
  574. &mechlist,
  575. NULL,
  576. NULL);
  577. if (err != SASL_OK) {
  578. trace_vnc_auth_fail(vs, vs->auth, "cannot list SASL mechanisms",
  579. sasl_errdetail(vs->sasl.conn));
  580. sasl_dispose(&vs->sasl.conn);
  581. vs->sasl.conn = NULL;
  582. goto authabort;
  583. }
  584. trace_vnc_auth_sasl_mech_list(vs, mechlist);
  585. vs->sasl.mechlist = g_strdup(mechlist);
  586. mechlistlen = strlen(mechlist);
  587. vnc_write_u32(vs, mechlistlen);
  588. vnc_write(vs, mechlist, mechlistlen);
  589. vnc_flush(vs);
  590. vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4);
  591. return;
  592. authabort:
  593. error_free(local_err);
  594. vnc_client_error(vs);
  595. }