Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* 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> |
| 34 | extern "C" { |
| 35 | #include <rfb/d3des.h> |
| 36 | } |
| 37 | |
| 38 | |
| 39 | using namespace rfb; |
| 40 | |
| 41 | static LogWriter vlog("SVncAuth"); |
| 42 | |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 43 | StringParameter SSecurityVncAuth::vncAuthPasswdFile |
| 44 | ("PasswordFile", "Password file for VNC authentication", "", ConfServer); |
| 45 | AliasParameter rfbauth("rfbauth", "Alias for PasswordFile", |
| 46 | &SSecurityVncAuth::vncAuthPasswdFile, ConfServer); |
| 47 | VncAuthPasswdParameter SSecurityVncAuth::vncAuthPasswd |
| 48 | ("Password", "Obfuscated binary encoding of the password which clients must supply to " |
| 49 | "access the server", &SSecurityVncAuth::vncAuthPasswdFile); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 50 | |
Pierre Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 51 | SSecurityVncAuth::SSecurityVncAuth(SConnection* sc) |
| 52 | : SSecurity(sc), sentChallenge(false), responsePos(0), |
| 53 | pg(&vncAuthPasswd), accessRights(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 57 | bool 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 Ossman | ad2b3c4 | 2018-09-21 15:31:11 +0200 | [diff] [blame] | 74 | bool SSecurityVncAuth::processMsg() |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 75 | { |
| 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 Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 93 | PlainPasswd passwd, passwdReadOnly; |
| 94 | pg->getVncAuthPasswd(&passwd, &passwdReadOnly); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 95 | |
| 96 | if (!passwd.buf) |
| 97 | throw AuthFailureException("No password configured for VNC Auth"); |
| 98 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 99 | if (verifyResponse(passwd)) { |
| 100 | accessRights = SConnection::AccessDefault; |
| 101 | return true; |
| 102 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 103 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 104 | if (passwdReadOnly.buf && verifyResponse(passwdReadOnly)) { |
| 105 | accessRights = SConnection::AccessView; |
| 106 | return true; |
| 107 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 108 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 109 | throw AuthFailureException(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 110 | } |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 111 | |
| 112 | VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name, |
| 113 | const char* desc, |
| 114 | StringParameter* passwdFile_) |
| 115 | : BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) { |
| 116 | } |
| 117 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 118 | void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) { |
| 119 | ObfuscatedPasswd obfuscated, obfuscatedReadOnly; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 120 | 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 Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 127 | return; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | FILE* fp = fopen(fname.buf, "r"); |
| 131 | if (!fp) { |
| 132 | vlog.error("opening password file '%s' failed",fname.buf); |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 133 | return; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | vlog.debug("reading password file"); |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 137 | 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 Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 141 | fclose(fp); |
| 142 | } else { |
| 143 | vlog.info("%s parameter not set", getName()); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | try { |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 148 | PlainPasswd plainPassword(obfuscated); |
| 149 | password->replaceBuf(plainPassword.takeBuf()); |
| 150 | PlainPasswd plainPasswordReadOnly(obfuscatedReadOnly); |
| 151 | readOnlyPassword->replaceBuf(plainPasswordReadOnly.takeBuf()); |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 152 | } catch (...) { |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |