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 | |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 51 | SSecurityVncAuth::SSecurityVncAuth(void) |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 52 | : sentChallenge(false), responsePos(0), pg(&vncAuthPasswd), accessRights(0) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 56 | bool SSecurityVncAuth::verifyResponse(const PlainPasswd &password) |
| 57 | { |
| 58 | rdr::U8 expectedResponse[vncAuthChallengeSize]; |
| 59 | |
| 60 | // Calculate the expected response |
| 61 | rdr::U8 key[8]; |
| 62 | int pwdLen = strlen(password.buf); |
| 63 | for (int i=0; i<8; i++) |
| 64 | key[i] = i<pwdLen ? password.buf[i] : 0; |
| 65 | deskey(key, EN0); |
| 66 | for (int j = 0; j < vncAuthChallengeSize; j += 8) |
| 67 | des(challenge+j, expectedResponse+j); |
| 68 | |
| 69 | // Check the actual response |
| 70 | return memcmp(response, expectedResponse, vncAuthChallengeSize) == 0; |
| 71 | } |
| 72 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 73 | bool SSecurityVncAuth::processMsg(SConnection* sc) |
| 74 | { |
| 75 | rdr::InStream* is = sc->getInStream(); |
| 76 | rdr::OutStream* os = sc->getOutStream(); |
| 77 | |
| 78 | if (!sentChallenge) { |
| 79 | rdr::RandomStream rs; |
| 80 | rs.readBytes(challenge, vncAuthChallengeSize); |
| 81 | os->writeBytes(challenge, vncAuthChallengeSize); |
| 82 | os->flush(); |
| 83 | sentChallenge = true; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | while (responsePos < vncAuthChallengeSize && is->checkNoWait(1)) |
| 88 | response[responsePos++] = is->readU8(); |
| 89 | |
| 90 | if (responsePos < vncAuthChallengeSize) return false; |
| 91 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 92 | PlainPasswd passwd, passwdReadOnly; |
| 93 | pg->getVncAuthPasswd(&passwd, &passwdReadOnly); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 94 | |
| 95 | if (!passwd.buf) |
| 96 | throw AuthFailureException("No password configured for VNC Auth"); |
| 97 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 98 | if (verifyResponse(passwd)) { |
| 99 | accessRights = SConnection::AccessDefault; |
| 100 | return true; |
| 101 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 103 | if (passwdReadOnly.buf && verifyResponse(passwdReadOnly)) { |
| 104 | accessRights = SConnection::AccessView; |
| 105 | return true; |
| 106 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 107 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 108 | throw AuthFailureException(); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 109 | } |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 110 | |
| 111 | VncAuthPasswdParameter::VncAuthPasswdParameter(const char* name, |
| 112 | const char* desc, |
| 113 | StringParameter* passwdFile_) |
| 114 | : BinaryParameter(name, desc, 0, 0, ConfServer), passwdFile(passwdFile_) { |
| 115 | } |
| 116 | |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 117 | void VncAuthPasswdParameter::getVncAuthPasswd(PlainPasswd *password, PlainPasswd *readOnlyPassword) { |
| 118 | ObfuscatedPasswd obfuscated, obfuscatedReadOnly; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 119 | getData((void**)&obfuscated.buf, &obfuscated.length); |
| 120 | |
| 121 | if (obfuscated.length == 0) { |
| 122 | if (passwdFile) { |
| 123 | CharArray fname(passwdFile->getData()); |
| 124 | if (!fname.buf[0]) { |
| 125 | vlog.info("neither %s nor %s params set", getName(), passwdFile->getName()); |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 126 | return; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | FILE* fp = fopen(fname.buf, "r"); |
| 130 | if (!fp) { |
| 131 | vlog.error("opening password file '%s' failed",fname.buf); |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 132 | return; |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | vlog.debug("reading password file"); |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 136 | obfuscated.buf = new char[8]; |
| 137 | obfuscated.length = fread(obfuscated.buf, 1, 8, fp); |
| 138 | obfuscatedReadOnly.buf = new char[8]; |
| 139 | obfuscatedReadOnly.length = fread(obfuscatedReadOnly.buf, 1, 8, fp); |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 140 | fclose(fp); |
| 141 | } else { |
| 142 | vlog.info("%s parameter not set", getName()); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | try { |
Michal Srb | 270a31c | 2014-11-10 15:32:00 +0200 | [diff] [blame] | 147 | PlainPasswd plainPassword(obfuscated); |
| 148 | password->replaceBuf(plainPassword.takeBuf()); |
| 149 | PlainPasswd plainPasswordReadOnly(obfuscatedReadOnly); |
| 150 | readOnlyPassword->replaceBuf(plainPasswordReadOnly.takeBuf()); |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 151 | } catch (...) { |
Adam Tkac | 162ac35 | 2010-04-23 14:02:43 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |