2
0

vnc-auth-sasl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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. if (clientdata[datalen - 1] != '\0') {
  227. trace_vnc_auth_fail(vs, vs->auth, "Malformed SASL client data",
  228. "Missing SASL NUL padding byte");
  229. sasl_dispose(&vs->sasl.conn);
  230. vs->sasl.conn = NULL;
  231. goto authabort;
  232. }
  233. datalen--; /* Discard the extra NUL padding byte */
  234. }
  235. err = sasl_server_step(vs->sasl.conn,
  236. clientdata,
  237. datalen,
  238. &serverout,
  239. &serveroutlen);
  240. trace_vnc_auth_sasl_step(vs, data, len, serverout, serveroutlen, err);
  241. if (err != SASL_OK &&
  242. err != SASL_CONTINUE) {
  243. trace_vnc_auth_fail(vs, vs->auth, "Cannot step SASL auth",
  244. sasl_errdetail(vs->sasl.conn));
  245. sasl_dispose(&vs->sasl.conn);
  246. vs->sasl.conn = NULL;
  247. goto authabort;
  248. }
  249. if (serveroutlen > SASL_DATA_MAX_LEN) {
  250. trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", "");
  251. sasl_dispose(&vs->sasl.conn);
  252. vs->sasl.conn = NULL;
  253. goto authabort;
  254. }
  255. if (serverout) {
  256. vnc_write_u32(vs, serveroutlen + 1);
  257. vnc_write(vs, serverout, serveroutlen);
  258. vnc_write_u8(vs, '\0');
  259. } else {
  260. vnc_write_u32(vs, 0);
  261. }
  262. /* Whether auth is complete */
  263. vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
  264. if (err == SASL_CONTINUE) {
  265. /* Wait for step length */
  266. vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
  267. } else {
  268. if (!vnc_auth_sasl_check_ssf(vs)) {
  269. trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", "");
  270. goto authreject;
  271. }
  272. /* Check the username access control list */
  273. if (vnc_auth_sasl_check_access(vs) < 0) {
  274. goto authreject;
  275. }
  276. trace_vnc_auth_pass(vs, vs->auth);
  277. vnc_write_u32(vs, 0); /* Accept auth */
  278. /*
  279. * Delay writing in SSF encoded mode until pending output
  280. * buffer is written
  281. */
  282. if (vs->sasl.runSSF)
  283. vs->sasl.waitWriteSSF = vs->output.offset;
  284. start_client_init(vs);
  285. }
  286. return 0;
  287. authreject:
  288. vnc_write_u32(vs, 1); /* Reject auth */
  289. vnc_write_u32(vs, sizeof("Authentication failed"));
  290. vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
  291. vnc_flush(vs);
  292. vnc_client_error(vs);
  293. return -1;
  294. authabort:
  295. vnc_client_error(vs);
  296. return -1;
  297. }
  298. static int protocol_client_auth_sasl_step_len(VncState *vs, uint8_t *data, size_t len)
  299. {
  300. uint32_t steplen = read_u32(data, 0);
  301. if (steplen > SASL_DATA_MAX_LEN) {
  302. trace_vnc_auth_fail(vs, vs->auth, "SASL step len too large", "");
  303. vnc_client_error(vs);
  304. return -1;
  305. }
  306. if (steplen == 0)
  307. return protocol_client_auth_sasl_step(vs, NULL, 0);
  308. else
  309. vnc_read_when(vs, protocol_client_auth_sasl_step, steplen);
  310. return 0;
  311. }
  312. /*
  313. * Start Msg
  314. *
  315. * Input from client:
  316. *
  317. * u32 clientin-length
  318. * u8-array clientin-string
  319. *
  320. * Output to client:
  321. *
  322. * u32 serverout-length
  323. * u8-array serverout-strin
  324. * u8 continue
  325. */
  326. #define SASL_DATA_MAX_LEN (1024 * 1024)
  327. static int protocol_client_auth_sasl_start(VncState *vs, uint8_t *data, size_t len)
  328. {
  329. uint32_t datalen = len;
  330. const char *serverout;
  331. unsigned int serveroutlen;
  332. int err;
  333. char *clientdata = NULL;
  334. /* NB, distinction of NULL vs "" is *critical* in SASL */
  335. if (datalen) {
  336. clientdata = (char*)data;
  337. if (clientdata[datalen - 1] != '\0') {
  338. trace_vnc_auth_fail(vs, vs->auth, "Malformed SASL client data",
  339. "Missing SASL NUL padding byte");
  340. sasl_dispose(&vs->sasl.conn);
  341. vs->sasl.conn = NULL;
  342. goto authabort;
  343. }
  344. datalen--; /* Discard the extra NUL padding byte */
  345. }
  346. err = sasl_server_start(vs->sasl.conn,
  347. vs->sasl.mechlist,
  348. clientdata,
  349. datalen,
  350. &serverout,
  351. &serveroutlen);
  352. trace_vnc_auth_sasl_start(vs, data, len, serverout, serveroutlen, err);
  353. if (err != SASL_OK &&
  354. err != SASL_CONTINUE) {
  355. trace_vnc_auth_fail(vs, vs->auth, "Cannot start SASL auth",
  356. sasl_errdetail(vs->sasl.conn));
  357. sasl_dispose(&vs->sasl.conn);
  358. vs->sasl.conn = NULL;
  359. goto authabort;
  360. }
  361. if (serveroutlen > SASL_DATA_MAX_LEN) {
  362. trace_vnc_auth_fail(vs, vs->auth, "SASL data too long", "");
  363. sasl_dispose(&vs->sasl.conn);
  364. vs->sasl.conn = NULL;
  365. goto authabort;
  366. }
  367. if (serverout) {
  368. vnc_write_u32(vs, serveroutlen + 1);
  369. vnc_write(vs, serverout, serveroutlen);
  370. vnc_write_u8(vs, '\0');
  371. } else {
  372. vnc_write_u32(vs, 0);
  373. }
  374. /* Whether auth is complete */
  375. vnc_write_u8(vs, err == SASL_CONTINUE ? 0 : 1);
  376. if (err == SASL_CONTINUE) {
  377. /* Wait for step length */
  378. vnc_read_when(vs, protocol_client_auth_sasl_step_len, 4);
  379. } else {
  380. if (!vnc_auth_sasl_check_ssf(vs)) {
  381. trace_vnc_auth_fail(vs, vs->auth, "SASL SSF too weak", "");
  382. goto authreject;
  383. }
  384. /* Check the username access control list */
  385. if (vnc_auth_sasl_check_access(vs) < 0) {
  386. goto authreject;
  387. }
  388. trace_vnc_auth_pass(vs, vs->auth);
  389. vnc_write_u32(vs, 0); /* Accept auth */
  390. start_client_init(vs);
  391. }
  392. return 0;
  393. authreject:
  394. vnc_write_u32(vs, 1); /* Reject auth */
  395. vnc_write_u32(vs, sizeof("Authentication failed"));
  396. vnc_write(vs, "Authentication failed", sizeof("Authentication failed"));
  397. vnc_flush(vs);
  398. vnc_client_error(vs);
  399. return -1;
  400. authabort:
  401. vnc_client_error(vs);
  402. return -1;
  403. }
  404. static int protocol_client_auth_sasl_start_len(VncState *vs, uint8_t *data, size_t len)
  405. {
  406. uint32_t startlen = read_u32(data, 0);
  407. if (startlen > SASL_DATA_MAX_LEN) {
  408. trace_vnc_auth_fail(vs, vs->auth, "SASL start len too large", "");
  409. vnc_client_error(vs);
  410. return -1;
  411. }
  412. if (startlen == 0)
  413. return protocol_client_auth_sasl_start(vs, NULL, 0);
  414. vnc_read_when(vs, protocol_client_auth_sasl_start, startlen);
  415. return 0;
  416. }
  417. static int protocol_client_auth_sasl_mechname(VncState *vs, uint8_t *data, size_t len)
  418. {
  419. char *mechname = g_strndup((const char *) data, len);
  420. trace_vnc_auth_sasl_mech_choose(vs, mechname);
  421. if (strncmp(vs->sasl.mechlist, mechname, len) == 0) {
  422. if (vs->sasl.mechlist[len] != '\0' &&
  423. vs->sasl.mechlist[len] != ',') {
  424. goto fail;
  425. }
  426. } else {
  427. char *offset = strstr(vs->sasl.mechlist, mechname);
  428. if (!offset) {
  429. goto fail;
  430. }
  431. if (offset[-1] != ',' ||
  432. (offset[len] != '\0'&&
  433. offset[len] != ',')) {
  434. goto fail;
  435. }
  436. }
  437. g_free(vs->sasl.mechlist);
  438. vs->sasl.mechlist = mechname;
  439. vnc_read_when(vs, protocol_client_auth_sasl_start_len, 4);
  440. return 0;
  441. fail:
  442. trace_vnc_auth_fail(vs, vs->auth, "Unsupported mechname", mechname);
  443. vnc_client_error(vs);
  444. g_free(mechname);
  445. return -1;
  446. }
  447. static int protocol_client_auth_sasl_mechname_len(VncState *vs, uint8_t *data, size_t len)
  448. {
  449. uint32_t mechlen = read_u32(data, 0);
  450. if (mechlen > 100) {
  451. trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too long", "");
  452. vnc_client_error(vs);
  453. return -1;
  454. }
  455. if (mechlen < 1) {
  456. trace_vnc_auth_fail(vs, vs->auth, "SASL mechname too short", "");
  457. vnc_client_error(vs);
  458. return -1;
  459. }
  460. vnc_read_when(vs, protocol_client_auth_sasl_mechname,mechlen);
  461. return 0;
  462. }
  463. static int
  464. vnc_socket_ip_addr_string(QIOChannelSocket *ioc,
  465. bool local,
  466. char **addrstr,
  467. Error **errp)
  468. {
  469. SocketAddress *addr;
  470. if (local) {
  471. addr = qio_channel_socket_get_local_address(ioc, errp);
  472. } else {
  473. addr = qio_channel_socket_get_remote_address(ioc, errp);
  474. }
  475. if (!addr) {
  476. return -1;
  477. }
  478. if (addr->type != SOCKET_ADDRESS_TYPE_INET) {
  479. *addrstr = NULL;
  480. qapi_free_SocketAddress(addr);
  481. return 0;
  482. }
  483. *addrstr = g_strdup_printf("%s;%s", addr->u.inet.host, addr->u.inet.port);
  484. qapi_free_SocketAddress(addr);
  485. return 0;
  486. }
  487. static bool
  488. vnc_socket_is_unix(QIOChannelSocket *ioc)
  489. {
  490. SocketAddress *addr = qio_channel_socket_get_local_address(ioc, NULL);
  491. return addr && addr->type == SOCKET_ADDRESS_TYPE_UNIX;
  492. }
  493. void start_auth_sasl(VncState *vs)
  494. {
  495. const char *mechlist = NULL;
  496. sasl_security_properties_t secprops;
  497. int err;
  498. Error *local_err = NULL;
  499. char *localAddr, *remoteAddr;
  500. int mechlistlen;
  501. /* Get local & remote client addresses in form IPADDR;PORT */
  502. if (vnc_socket_ip_addr_string(vs->sioc, true,
  503. &localAddr, &local_err) < 0) {
  504. trace_vnc_auth_fail(vs, vs->auth, "Cannot format local IP",
  505. error_get_pretty(local_err));
  506. goto authabort;
  507. }
  508. if (vnc_socket_ip_addr_string(vs->sioc, false,
  509. &remoteAddr, &local_err) < 0) {
  510. trace_vnc_auth_fail(vs, vs->auth, "Cannot format remote IP",
  511. error_get_pretty(local_err));
  512. g_free(localAddr);
  513. goto authabort;
  514. }
  515. err = sasl_server_new("vnc",
  516. NULL, /* FQDN - just delegates to gethostname */
  517. NULL, /* User realm */
  518. localAddr,
  519. remoteAddr,
  520. NULL, /* Callbacks, not needed */
  521. SASL_SUCCESS_DATA,
  522. &vs->sasl.conn);
  523. g_free(localAddr);
  524. g_free(remoteAddr);
  525. localAddr = remoteAddr = NULL;
  526. if (err != SASL_OK) {
  527. trace_vnc_auth_fail(vs, vs->auth, "SASL context setup failed",
  528. sasl_errstring(err, NULL, NULL));
  529. vs->sasl.conn = NULL;
  530. goto authabort;
  531. }
  532. /* Inform SASL that we've got an external SSF layer from TLS/x509 */
  533. if (vs->auth == VNC_AUTH_VENCRYPT &&
  534. vs->subauth == VNC_AUTH_VENCRYPT_X509SASL) {
  535. int keysize;
  536. sasl_ssf_t ssf;
  537. keysize = qcrypto_tls_session_get_key_size(vs->tls,
  538. &local_err);
  539. if (keysize < 0) {
  540. trace_vnc_auth_fail(vs, vs->auth, "cannot TLS get cipher size",
  541. error_get_pretty(local_err));
  542. sasl_dispose(&vs->sasl.conn);
  543. vs->sasl.conn = NULL;
  544. goto authabort;
  545. }
  546. ssf = keysize * CHAR_BIT; /* tls key size is bytes, sasl wants bits */
  547. err = sasl_setprop(vs->sasl.conn, SASL_SSF_EXTERNAL, &ssf);
  548. if (err != SASL_OK) {
  549. trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL external SSF",
  550. sasl_errstring(err, NULL, NULL));
  551. sasl_dispose(&vs->sasl.conn);
  552. vs->sasl.conn = NULL;
  553. goto authabort;
  554. }
  555. } else {
  556. vs->sasl.wantSSF = !vnc_socket_is_unix(vs->sioc);
  557. }
  558. memset (&secprops, 0, sizeof secprops);
  559. /* Inform SASL that we've got an external SSF layer from TLS.
  560. *
  561. * Disable SSF, if using TLS+x509+SASL only, or UNIX sockets.
  562. * TLS without x509 is not sufficiently strong, nor is plain
  563. * TCP
  564. */
  565. if (vnc_socket_is_unix(vs->sioc) ||
  566. (vs->auth == VNC_AUTH_VENCRYPT &&
  567. vs->subauth == VNC_AUTH_VENCRYPT_X509SASL)) {
  568. /* If we've got TLS or UNIX domain sock, we don't care about SSF */
  569. secprops.min_ssf = 0;
  570. secprops.max_ssf = 0;
  571. secprops.maxbufsize = 8192;
  572. secprops.security_flags = 0;
  573. } else {
  574. /* Plain TCP, better get an SSF layer */
  575. secprops.min_ssf = 56; /* Good enough to require kerberos */
  576. secprops.max_ssf = 100000; /* Arbitrary big number */
  577. secprops.maxbufsize = 8192;
  578. /* Forbid any anonymous or trivially crackable auth */
  579. secprops.security_flags =
  580. SASL_SEC_NOANONYMOUS | SASL_SEC_NOPLAINTEXT;
  581. }
  582. err = sasl_setprop(vs->sasl.conn, SASL_SEC_PROPS, &secprops);
  583. if (err != SASL_OK) {
  584. trace_vnc_auth_fail(vs, vs->auth, "cannot set SASL security props",
  585. sasl_errstring(err, NULL, NULL));
  586. sasl_dispose(&vs->sasl.conn);
  587. vs->sasl.conn = NULL;
  588. goto authabort;
  589. }
  590. err = sasl_listmech(vs->sasl.conn,
  591. NULL, /* Don't need to set user */
  592. "", /* Prefix */
  593. ",", /* Separator */
  594. "", /* Suffix */
  595. &mechlist,
  596. NULL,
  597. NULL);
  598. if (err != SASL_OK) {
  599. trace_vnc_auth_fail(vs, vs->auth, "cannot list SASL mechanisms",
  600. sasl_errdetail(vs->sasl.conn));
  601. sasl_dispose(&vs->sasl.conn);
  602. vs->sasl.conn = NULL;
  603. goto authabort;
  604. }
  605. trace_vnc_auth_sasl_mech_list(vs, mechlist);
  606. if (g_str_equal(mechlist, "")) {
  607. trace_vnc_auth_fail(vs, vs->auth, "no available SASL mechanisms", "");
  608. sasl_dispose(&vs->sasl.conn);
  609. vs->sasl.conn = NULL;
  610. goto authabort;
  611. }
  612. vs->sasl.mechlist = g_strdup(mechlist);
  613. mechlistlen = strlen(mechlist);
  614. vnc_write_u32(vs, mechlistlen);
  615. vnc_write(vs, mechlist, mechlistlen);
  616. vnc_flush(vs);
  617. vnc_read_when(vs, protocol_client_auth_sasl_mechname_len, 4);
  618. return;
  619. authabort:
  620. error_free(local_err);
  621. vnc_client_error(vs);
  622. }