Constantin Kaplinsky | 47ed8d3 | 2004-10-08 09:43:57 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2003 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 | |
| 22 | #include <rfb/SSecurityVncAuth.h> |
| 23 | #include <rdr/RandomStream.h> |
| 24 | #include <rfb/SConnection.h> |
| 25 | #include <rfb/vncAuth.h> |
| 26 | #include <rfb/Configuration.h> |
| 27 | #include <rfb/LogWriter.h> |
| 28 | #include <rfb/util.h> |
| 29 | #include <string.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | using namespace rfb; |
| 33 | |
| 34 | static LogWriter vlog("VncAuth"); |
| 35 | |
| 36 | |
| 37 | SSecurityVncAuth::SSecurityVncAuth(VncAuthPasswdGetter* pg_) |
| 38 | : sentChallenge(false), responsePos(0), pg(pg_) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | bool SSecurityVncAuth::processMsg(SConnection* sc, bool* done) |
| 43 | { |
| 44 | *done = false; |
| 45 | rdr::InStream* is = sc->getInStream(); |
| 46 | rdr::OutStream* os = sc->getOutStream(); |
| 47 | |
| 48 | if (!sentChallenge) { |
| 49 | rdr::RandomStream rs; |
| 50 | rs.readBytes(challenge, vncAuthChallengeSize); |
| 51 | os->writeBytes(challenge, vncAuthChallengeSize); |
| 52 | os->flush(); |
| 53 | sentChallenge = true; |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | if (responsePos >= vncAuthChallengeSize) return false; |
| 58 | while (is->checkNoWait(1) && responsePos < vncAuthChallengeSize) { |
| 59 | response[responsePos++] = is->readU8(); |
| 60 | } |
| 61 | |
| 62 | if (responsePos < vncAuthChallengeSize) return true; |
| 63 | |
| 64 | CharArray passwd(pg->getVncAuthPasswd()); |
| 65 | |
| 66 | // Beyond this point, there is no more VNCAuth protocol to perform. |
| 67 | *done = true; |
| 68 | |
| 69 | if (!passwd.buf) { |
| 70 | failureMessage_.buf = strDup("No password configured for VNC Auth"); |
| 71 | vlog.error(failureMessage_.buf); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | vncAuthEncryptChallenge(challenge, passwd.buf); |
| 76 | memset(passwd.buf, 0, strlen(passwd.buf)); |
| 77 | |
| 78 | if (memcmp(challenge, response, vncAuthChallengeSize) != 0) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | return true; |
| 83 | } |