Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 2 | * Copyright (C) 2010 TigerVNC Team |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 3 | * |
| 4 | * This is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This software is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this software; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdlib.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 26 | #include <string.h> |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 27 | #include <rfb/CSecurityNone.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 28 | #include <rfb/CSecurityStack.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 29 | #include <rfb/CSecurityVeNCrypt.h> |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 30 | #include <rfb/CSecurityVncAuth.h> |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 31 | #include <rfb/CSecurityPlain.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 32 | #include <rdr/Exception.h> |
| 33 | #include <rfb/LogWriter.h> |
Adam Tkac | b6eb399 | 2010-04-23 14:05:00 +0000 | [diff] [blame] | 34 | #include <rfb/Security.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 35 | #include <rfb/SSecurityNone.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 36 | #include <rfb/SSecurityStack.h> |
Adam Tkac | 520fc41 | 2010-09-02 14:13:24 +0000 | [diff] [blame] | 37 | #include <rfb/SSecurityPlain.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 38 | #include <rfb/SSecurityVncAuth.h> |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 39 | #include <rfb/SSecurityVeNCrypt.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 40 | #ifdef HAVE_GNUTLS |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 41 | #include <rfb/CSecurityTLS.h> |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 42 | #include <rfb/SSecurityTLS.h> |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 43 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | #include <rfb/util.h> |
| 45 | |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 46 | using namespace rdr; |
| 47 | using namespace rfb; |
| 48 | using namespace std; |
| 49 | |
| 50 | static LogWriter vlog("Security"); |
| 51 | |
Pierre Ossman | 27eb55e | 2015-01-29 13:31:06 +0100 | [diff] [blame] | 52 | #ifdef HAVE_GNUTLS |
| 53 | StringParameter Security::GnuTLSPriority("GnuTLSPriority", |
| 54 | "GnuTLS priority string that controls the TLS session’s handshake algorithms", |
| 55 | "NORMAL"); |
| 56 | #endif |
| 57 | |
Pierre Ossman | e5fe070 | 2011-05-16 12:46:16 +0000 | [diff] [blame] | 58 | Security::Security() |
| 59 | { |
| 60 | } |
| 61 | |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 62 | Security::Security(StringParameter &secTypes) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 63 | { |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 64 | char *secTypesStr; |
| 65 | |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 66 | secTypesStr = secTypes.getData(); |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 67 | enabledSecTypes = parseSecTypes(secTypesStr); |
| 68 | |
Pierre Ossman | 5e04c26 | 2011-11-09 11:31:12 +0000 | [diff] [blame] | 69 | delete [] secTypesStr; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 72 | const std::list<rdr::U8> Security::GetEnabledSecTypes(void) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 73 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 74 | list<rdr::U8> result; |
| 75 | list<U32>::iterator i; |
| 76 | |
Pierre Ossman | 1f8aba3 | 2015-12-29 15:02:11 +0100 | [diff] [blame^] | 77 | /* Partial workaround for Vino's stupid behaviour. It doesn't allow |
| 78 | * the basic authentication types as part of the VeNCrypt handshake, |
| 79 | * making it impossible for a client to do opportunistic encryption. |
| 80 | * At least make it possible to connect when encryption is explicitly |
| 81 | * disabled. */ |
| 82 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) { |
| 83 | if (*i >= 0x100) { |
| 84 | result.push_back(secTypeVeNCrypt); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 89 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 90 | if (*i < 0x100) |
| 91 | result.push_back(*i); |
| 92 | |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void) |
| 97 | { |
| 98 | list<rdr::U32> result; |
| 99 | list<U32>::iterator i; |
| 100 | |
| 101 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
Adam Tkac | 95e2fe8 | 2010-08-12 13:54:59 +0000 | [diff] [blame] | 102 | if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */ |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 103 | result.push_back(*i); |
| 104 | |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | void Security::EnableSecType(U32 secType) |
| 109 | { |
| 110 | list<U32>::iterator i; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 111 | |
| 112 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 113 | if (*i == secType) |
| 114 | return; |
| 115 | |
| 116 | enabledSecTypes.push_back(secType); |
| 117 | } |
| 118 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 119 | bool Security::IsSupported(U32 secType) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 120 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 121 | list<U32>::iterator i; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 122 | |
| 123 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 124 | if (*i == secType) |
| 125 | return true; |
DRC | 674bf06 | 2011-02-21 13:14:16 +0000 | [diff] [blame] | 126 | if (secType == secTypeVeNCrypt) |
| 127 | return true; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 128 | |
| 129 | return false; |
| 130 | } |
| 131 | |
Adam Tkac | a9a7b4b | 2011-02-01 14:34:55 +0000 | [diff] [blame] | 132 | char *Security::ToString(void) |
| 133 | { |
| 134 | list<U32>::iterator i; |
| 135 | static char out[128]; /* Should be enough */ |
| 136 | bool firstpass = true; |
| 137 | const char *name; |
| 138 | |
| 139 | memset(out, 0, sizeof(out)); |
| 140 | |
| 141 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) { |
| 142 | name = secTypeName(*i); |
| 143 | if (name[0] == '[') /* Unknown security type */ |
| 144 | continue; |
| 145 | |
| 146 | if (!firstpass) |
| 147 | strncat(out, ",", sizeof(out) - 1); |
| 148 | else |
| 149 | firstpass = false; |
| 150 | strncat(out, name, sizeof(out) - 1); |
| 151 | } |
| 152 | |
| 153 | return out; |
| 154 | } |
| 155 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 156 | rdr::U32 rfb::secTypeNum(const char* name) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | { |
| 158 | if (strcasecmp(name, "None") == 0) return secTypeNone; |
| 159 | if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth; |
| 160 | if (strcasecmp(name, "Tight") == 0) return secTypeTight; |
| 161 | if (strcasecmp(name, "RA2") == 0) return secTypeRA2; |
| 162 | if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne; |
| 163 | if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 164 | if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne; |
| 165 | if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 166 | |
| 167 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 168 | if (strcasecmp(name, "Plain") == 0) return secTypePlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 169 | if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone; |
| 170 | if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc; |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 171 | if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 172 | if (strcasecmp(name, "X509None") == 0) return secTypeX509None; |
| 173 | if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 174 | if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 175 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 176 | return secTypeInvalid; |
| 177 | } |
| 178 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 179 | const char* rfb::secTypeName(rdr::U32 num) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 180 | { |
| 181 | switch (num) { |
| 182 | case secTypeNone: return "None"; |
| 183 | case secTypeVncAuth: return "VncAuth"; |
| 184 | case secTypeTight: return "Tight"; |
| 185 | case secTypeRA2: return "RA2"; |
| 186 | case secTypeRA2ne: return "RA2ne"; |
| 187 | case secTypeSSPI: return "SSPI"; |
| 188 | case secTypeSSPIne: return "SSPIne"; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 189 | case secTypeVeNCrypt: return "VeNCrypt"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 190 | |
| 191 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 192 | case secTypePlain: return "Plain"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 193 | case secTypeTLSNone: return "TLSNone"; |
| 194 | case secTypeTLSVnc: return "TLSVnc"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 195 | case secTypeTLSPlain: return "TLSPlain"; |
| 196 | case secTypeX509None: return "X509None"; |
| 197 | case secTypeX509Vnc: return "X509Vnc"; |
| 198 | case secTypeX509Plain: return "X509Plain"; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 199 | default: return "[unknown secType]"; |
| 200 | } |
| 201 | } |
| 202 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 203 | std::list<rdr::U32> rfb::parseSecTypes(const char* types_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 204 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 205 | std::list<rdr::U32> result; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 206 | CharArray types(strDup(types_)), type; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 207 | while (types.buf) { |
| 208 | strSplit(types.buf, ',', &type.buf, &types.buf); |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 209 | rdr::U32 typeNum = secTypeNum(type.buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | if (typeNum != secTypeInvalid) |
| 211 | result.push_back(typeNum); |
| 212 | } |
| 213 | return result; |
| 214 | } |