blob: 882f0b086154f25c1ef169c6cf7162d0636033cc [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18//
19// SSecurityVncAuth
20//
21// XXX not thread-safe, because d3des isn't - do we need to worry about this?
22//
23
24#include <rfb/SSecurityVncAuth.h>
25#include <rdr/RandomStream.h>
26#include <rfb/SConnection.h>
27#include <rfb/Password.h>
28#include <rfb/Configuration.h>
29#include <rfb/LogWriter.h>
30#include <rfb/util.h>
31#include <rfb/Exception.h>
32#include <string.h>
33#include <stdio.h>
34extern "C" {
35#include <rfb/d3des.h>
36}
37
38
39using namespace rfb;
40
41static LogWriter vlog("SVncAuth");
42
Adam Tkac162ac352010-04-23 14:02:43 +000043StringParameter SSecurityVncAuth::vncAuthPasswdFile
44("PasswordFile", "Password file for VNC authentication", "", ConfServer);
45AliasParameter rfbauth("rfbauth", "Alias for PasswordFile",
46 &SSecurityVncAuth::vncAuthPasswdFile, ConfServer);
47VncAuthPasswdParameter SSecurityVncAuth::vncAuthPasswd
48("Password", "Obfuscated binary encoding of the password which clients must supply to "
49 "access the server", &SSecurityVncAuth::vncAuthPasswdFile);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050
Pierre Ossmanad2b3c42018-09-21 15:31:11 +020051SSecurityVncAuth::SSecurityVncAuth(SConnection* sc)
52 : SSecurity(sc), sentChallenge(false), responsePos(0),
53 pg(&vncAuthPasswd), accessRights(0)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054{
55}
56
Michal Srb270a31c2014-11-10 15:32:00 +020057bool SSecurityVncAuth::verifyResponse(const PlainPasswd &password)
58{
59 rdr::U8 expectedResponse[vncAuthChallengeSize];
60
61 // Calculate the expected response
62 rdr::U8 key[8];
63 int pwdLen = strlen(password.buf);
64 for (int i=0; i<8; i++)
65 key[i] = i<pwdLen ? password.buf[i] : 0;
66 deskey(key, EN0);
67 for (int j = 0; j < vncAuthChallengeSize; j += 8)
68 des(challenge+j, expectedResponse+j);
69
70 // Check the actual response
71 return memcmp(response, expectedResponse, vncAuthChallengeSize) == 0;
72}
73
Pierre Ossmanad2b3c42018-09-21 15:31:11 +020074bool SSecurityVncAuth::processMsg()
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000075{
76 rdr::InStream* is = sc->getInStream();
77 rdr::OutStream* os = sc->getOutStream();
78
79 if (!sentChallenge) {
80 rdr::RandomStream rs;
81 rs.readBytes(challenge, vncAuthChallengeSize);
82 os->writeBytes(challenge, vncAuthChallengeSize);
83 os->flush();
84 sentChallenge = true;
85 return false;
86 }
87
88 while (responsePos < vncAuthChallengeSize && is->checkNoWait(1))
89 response[responsePos++] = is->readU8();
90
91 if (responsePos < vncAuthChallengeSize) return false;
92
Michal Srb270a31c2014-11-10 15:32:00 +020093 PlainPasswd passwd, passwdReadOnly;
94 pg->getVncAuthPasswd(&passwd, &passwdReadOnly);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000095
96 if (!passwd.buf)
97 throw AuthFailureException("No password configured for VNC Auth");
98
Michal Srb270a31c2014-11-10 15:32:00 +020099 if (verifyResponse(passwd)) {
100 accessRights = SConnection::AccessDefault;
101 return true;
102 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000103
Michal Srb270a31c2014-11-10 15:32:00 +0200104 if (passwdReadOnly.buf && verifyResponse(passwdReadOnly)) {
105 accessRights = SConnection::AccessView;
106 return true;
107 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000108
Michal Srb270a31c2014-11-10 15:32:00 +0200109 throw AuthFailureException();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000110}
Adam Tkac162ac352010-04-23 14:02:43 +0000111
112VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name,
113 const char* desc,
114 StringParameter* passwdFile_)
115: BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) {
116}
117
Michal Srb270a31c2014-11-10 15:32:00 +0200118void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) {
119 ObfuscatedPasswd obfuscated, obfuscatedReadOnly;
Adam Tkac162ac352010-04-23 14:02:43 +0000120 getData((void**)&obfuscated.buf, &obfuscated.length);
121
122 if (obfuscated.length == 0) {
123 if (passwdFile) {
124 CharArray fname(passwdFile->getData());
125 if (!fname.buf[0]) {
126 vlog.info("neither %s nor %s params set", getName(), passwdFile->getName());
Michal Srb270a31c2014-11-10 15:32:00 +0200127 return;
Adam Tkac162ac352010-04-23 14:02:43 +0000128 }
129
130 FILE* fp = fopen(fname.buf, "r");
131 if (!fp) {
132 vlog.error("opening password file '%s' failed",fname.buf);
Michal Srb270a31c2014-11-10 15:32:00 +0200133 return;
Adam Tkac162ac352010-04-23 14:02:43 +0000134 }
135
136 vlog.debug("reading password file");
Michal Srb270a31c2014-11-10 15:32:00 +0200137 obfuscated.buf = new char[8];
138 obfuscated.length = fread(obfuscated.buf, 1, 8, fp);
139 obfuscatedReadOnly.buf = new char[8];
140 obfuscatedReadOnly.length = fread(obfuscatedReadOnly.buf, 1, 8, fp);
Adam Tkac162ac352010-04-23 14:02:43 +0000141 fclose(fp);
142 } else {
143 vlog.info("%s parameter not set", getName());
144 }
145 }
146
147 try {
Michal Srb270a31c2014-11-10 15:32:00 +0200148 PlainPasswd plainPassword(obfuscated);
149 password->replaceBuf(plainPassword.takeBuf());
150 PlainPasswd plainPasswordReadOnly(obfuscatedReadOnly);
151 readOnlyPassword->replaceBuf(plainPasswordReadOnly.takeBuf());
Adam Tkac162ac352010-04-23 14:02:43 +0000152 } catch (...) {
Adam Tkac162ac352010-04-23 14:02:43 +0000153 }
154}
155