1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
|
/*
* auth.c - deal with authentication.
*
* This file implements the VNC authentication protocol when setting up an RFB
* connection.
*/
/*
* OSXvnc Copyright (C) 2001 Dan McGuirk <mcguirk@incompleteness.net>.
* Original Xvnc code Copyright (C) 1999 AT&T Laboratories Cambridge.
* All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include "rfb/rfb.h"
void
rfbAuthInitScreen(rfbScreenInfoPtr rfbScreen)
{
#ifdef VINO_HAVE_GNUTLS
#define DH_BITS 1024
gnutls_global_init();
gnutls_anon_allocate_server_credentials(&rfbScreen->anonCredentials);
gnutls_dh_params_init(&rfbScreen->dhParams);
gnutls_dh_params_generate2(rfbScreen->dhParams, DH_BITS);
gnutls_anon_set_server_dh_params(rfbScreen->anonCredentials,
rfbScreen->dhParams);
#undef DH_BITS
#endif /* VINO_HAVE_GNUTLS */
}
void
rfbAuthCleanupScreen(rfbScreenInfoPtr rfbScreen)
{
#ifdef VINO_HAVE_GNUTLS
gnutls_dh_params_deinit(rfbScreen->dhParams);
gnutls_anon_free_server_credentials(rfbScreen->anonCredentials);
gnutls_global_deinit();
#endif /* VINO_HAVE_GNUTLS */
}
#ifdef VINO_HAVE_GNUTLS
static rfbBool
rfbAuthTLSHandshake(rfbClientPtr cl)
{
/* TODO: Perform non-anonymous key exchange to prevent man-in-the-middle
* attacks. */
static const char kx_priority[] = "NORMAL:+ANON-DH";
int err;
gnutls_init(&cl->tlsSession, GNUTLS_SERVER);
gnutls_set_default_priority(cl->tlsSession);
gnutls_priority_set_direct(cl->tlsSession, kx_priority, NULL);
gnutls_credentials_set(cl->tlsSession,
GNUTLS_CRD_ANON,
cl->screen->anonCredentials);
gnutls_transport_set_ptr(cl->tlsSession, (gnutls_transport_ptr_t) cl->sock);
err = gnutls_handshake(cl->tlsSession);
if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err)) {
cl->state = RFB_TLS_HANDSHAKE;
return FALSE;
}
if (err != GNUTLS_E_SUCCESS) {
rfbErr("TLS Handshake failed: %s\n", gnutls_strerror(err));
gnutls_deinit (cl->tlsSession);
cl->tlsSession = NULL;
rfbCloseClient(cl);
return FALSE;
}
cl->useTLS = TRUE;
return TRUE;
}
#endif /* VINO_HAVE_GNUTLS */
static rfbBool
rfbAuthClientAuthenticated(rfbClientPtr cl)
{
rfbBool accepted = FALSE;
if (cl->state == RFB_INITIALISATION &&
cl->screen->authenticatedClientHook) {
switch (cl->screen->authenticatedClientHook(cl)) {
case RFB_CLIENT_ON_HOLD:
cl->onHold = TRUE;
break;
case RFB_CLIENT_ACCEPT:
accepted = TRUE;
break;
case RFB_CLIENT_REFUSE:
rfbCloseClient(cl);
rfbClientConnectionGone(cl);
break;
}
}
return accepted;
}
/*
* rfbAuthNewClient is called when we reach the point of authenticating
* a new client. If authentication isn't being used then we simply send
* rfbNoAuth. Otherwise we send rfbVncAuth plus the challenge.
*/
static void
rfbAuthNewClient3_7(rfbClientPtr cl)
{
rfbSecurityTypesMsg st;
int i;
cl->state = RFB_SECURITY_TYPE;
st.nSecurityTypes = cl->screen->nSecurityTypes;
for (i = 0; i < st.nSecurityTypes; i++) {
rfbLog("Advertising security type %d\n", cl->screen->securityTypes[i]);
st.securityTypes[i] = cl->screen->securityTypes[i];
}
if (WriteExact(cl, (char *)&st, st.nSecurityTypes + 1) < 0) {
rfbLogPerror("rfbAuthNewClient: write");
rfbCloseClient(cl);
return;
}
}
static void
rfbAuthNewClient3_3(rfbClientPtr cl)
{
union {
uint32_t authType;
char buf[4 + CHALLENGESIZE];
} auth;
int len, i;
for (i = 0; i < cl->screen->nSecurityTypes; i++)
if (cl->screen->securityTypes [i] == rfbVncAuth ||
cl->screen->securityTypes [i] == rfbNoAuth)
break;
if (i == cl->screen->nSecurityTypes) {
rfbClientConnFailed(cl, "No security type suitable for RFB 3.3 supported");
return;
}
switch (cl->screen->securityTypes [i]) {
case rfbVncAuth:
auth.authType = Swap32IfLE(rfbVncAuth);
vncRandomBytes(cl->authChallenge);
memcpy(&(auth.buf[4]), (char *)cl->authChallenge, CHALLENGESIZE);
len = 4 + CHALLENGESIZE;
cl->state = RFB_AUTHENTICATION;
break;
case rfbNoAuth:
auth.authType = Swap32IfLE(rfbNoAuth);
len = 4;
cl->state = RFB_INITIALISATION;
break;
default:
/* can't be reached */
return;
}
if (WriteExact(cl, auth.buf, len) < 0) {
rfbLogPerror("rfbAuthNewClient: write");
rfbCloseClient(cl);
return;
}
rfbAuthClientAuthenticated(cl);
}
void
rfbAuthNewClient(rfbClientPtr cl)
{
if (cl->minorVersion >= rfbProtocolMinorVersion7)
rfbAuthNewClient3_7(cl);
else
rfbAuthNewClient3_3(cl);
}
void
rfbAuthCleanupClient(rfbClientPtr cl)
{
#ifdef VINO_HAVE_GNUTLS
if (cl->tlsSession) {
if (cl->sock)
gnutls_bye(cl->tlsSession, GNUTLS_SHUT_WR);
gnutls_deinit(cl->tlsSession);
cl->tlsSession = NULL;
}
#endif /* VINO_HAVE_GNUTLS */
}
#ifdef VINO_HAVE_GNUTLS
static void
rfbAuthListAuthTypes(rfbClientPtr cl)
{
rfbAuthTypesMsg st;
int i;
cl->state = RFB_AUTH_TYPE;
st.nAuthTypes = cl->screen->nAuthTypes;
for (i = 0; i < cl->screen->nAuthTypes; i++) {
rfbLog("Advertising authentication type %d\n", cl->screen->authTypes[i]);
st.authTypes[i] = cl->screen->authTypes[i];
}
if (WriteExact(cl, (char *)&st, cl->screen->nAuthTypes + 1) < 0) {
rfbLogPerror("rfbAuthNewClient: write");
rfbCloseClient(cl);
return;
}
}
#endif /* VINO_HAVE_GNUTLS */
void
rfbAuthProcessSecurityTypeMessage(rfbClientPtr cl)
{
uint8_t securityType;
int n, i;
if ((n = ReadExact(cl, (char *)&securityType, 1)) <= 0) {
if (n != 0)
rfbLogPerror("rfbAuthProcessSecurityTypeMessage: read");
rfbCloseClient(cl);
return;
}
rfbLog("Client returned security type %d\n", securityType);
for (i = 0; i < cl->screen->nSecurityTypes; i++)
if (cl->screen->securityTypes[i] == securityType)
break;
if (i == cl->screen->nSecurityTypes) {
rfbErr("rfbAuthProcessSecurityTypeMessage: client returned unadvertised security type %d\n",
securityType);
rfbCloseClient(cl);
return;
}
switch (securityType) {
#ifdef VINO_HAVE_GNUTLS
case rfbTLS:
if (!rfbAuthTLSHandshake(cl))
return;
rfbAuthListAuthTypes(cl);
break;
#endif
case rfbVncAuth:
vncRandomBytes(cl->authChallenge);
if (WriteExact(cl, (char *)&cl->authChallenge, CHALLENGESIZE) < 0) {
rfbLogPerror("rfbAuthProcessSecurityTypeMessage: write");
rfbCloseClient(cl);
return;
}
cl->state = RFB_AUTHENTICATION;
break;
case rfbNoAuth:
if (cl->minorVersion >= rfbProtocolMinorVersion8)
rfbAuthPasswordChecked(cl, RFB_CLIENT_ACCEPT);
else {
cl->state = RFB_INITIALISATION;
if (rfbAuthClientAuthenticated(cl))
rfbProcessClientInitMessage(cl);
}
break;
default:
/* can't be reached */
break;
}
}
#ifdef VINO_HAVE_GNUTLS
void
rfbAuthProcessTLSHandshake(rfbClientPtr cl)
{
int err;
err = gnutls_handshake(cl->tlsSession);
if (err != GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(err))
return;
if (err != GNUTLS_E_SUCCESS) {
rfbErr("TLS Handshake failed: %s\n", gnutls_strerror(err));
gnutls_deinit (cl->tlsSession);
cl->tlsSession = NULL;
rfbCloseClient(cl);
return;
}
cl->useTLS = TRUE;
rfbAuthListAuthTypes(cl);
}
#endif /* VINO_HAVE_GNUTLS */
void
rfbAuthProcessAuthTypeMessage(rfbClientPtr cl)
{
uint8_t authType;
int n, i;
if ((n = ReadExact(cl, (char *)&authType, 1)) <= 0) {
if (n != 0)
rfbLogPerror("rfbAuthProcessAuthTypeMessage: read");
rfbCloseClient(cl);
return;
}
rfbLog("Client returned authentication type %d\n", authType);
for (i = 0; i < cl->screen->nAuthTypes; i++)
if (cl->screen->authTypes[i] == authType)
break;
if (i == cl->screen->nAuthTypes) {
rfbErr("rfbAuthProcessAuthTypeMessage: client returned unadvertised authentication type %d\n",
authType);
rfbCloseClient(cl);
return;
}
switch (authType) {
case rfbVncAuth:
vncRandomBytes(cl->authChallenge);
if (WriteExact(cl, (char *)&cl->authChallenge, CHALLENGESIZE) < 0) {
rfbLogPerror("rfbAuthProcessAuthTypeMessage: write");
rfbCloseClient(cl);
return;
}
cl->state = RFB_AUTHENTICATION;
break;
case rfbNoAuth:
cl->state = RFB_INITIALISATION;
if (rfbAuthClientAuthenticated(cl))
rfbProcessClientInitMessage(cl);
break;
default:
/* can't be reached */
break;
}
}
/*
* rfbAuthProcessClientMessage is called when the client sends its
* authentication response.
*/
void
rfbAuthProcessClientMessage(rfbClientPtr cl)
{
int n;
uint8_t response[CHALLENGESIZE];
enum rfbNewClientAction result;
if ((n = ReadExact(cl, (char *)response, CHALLENGESIZE)) <= 0) {
if (n != 0)
rfbLogPerror("rfbAuthProcessClientMessage: read");
rfbCloseClient(cl);
return;
}
result = RFB_CLIENT_REFUSE;
if (cl->screen->passwordCheck)
result = cl->screen->passwordCheck(cl, (const char *)response, CHALLENGESIZE);
rfbAuthPasswordChecked(cl, result);
}
void
rfbAuthPasswordChecked(rfbClientPtr cl,
enum rfbNewClientAction result)
{
uint32_t authResult;
switch (result) {
case RFB_CLIENT_ON_HOLD:
cl->state = RFB_AUTH_DEFERRED;
cl->onHold = TRUE;
break;
case RFB_CLIENT_ACCEPT:
cl->onHold = FALSE;
authResult = Swap32IfLE(rfbVncAuthOK);
if (WriteExact(cl, (char *)&authResult, 4) < 0) {
rfbLogPerror("rfbAuthPasswordChecked: write");
rfbCloseClient(cl);
return;
}
cl->state = RFB_INITIALISATION;
rfbAuthClientAuthenticated(cl);
break;
default:
case RFB_CLIENT_REFUSE:
rfbErr("rfbAuthPasswordChecked: password check failed\n");
authResult = Swap32IfLE(rfbVncAuthFailed);
if (WriteExact(cl, (char *)&authResult, 4) < 0) {
rfbLogPerror("rfbAuthPasswordChecked: write");
rfbCloseClient(cl);
return;
}
if (cl->minorVersion >= rfbProtocolMinorVersion8) {
/* We can't really localize this string, since it has to
* be iso8859-1 encoded. However, the string will only be
* returned when we're using protocol version 3.8, and we
* only advertise support for 3.7, so this only gets used
* at all if you have a broken client.
*/
const char errmsg[] = "Password incorrect";
uint32_t len, wireLen;
len = sizeof(errmsg) - 1;
wireLen = Swap32IfLE(len);
if (WriteExact(cl, (char *)&wireLen, 4) < 0 ||
WriteExact(cl, errmsg, len) < 0) {
rfbLogPerror("rfbAuthPasswordChecked: write");
rfbCloseClient(cl);
return;
}
}
rfbCloseClient(cl);
break;
}
}
|