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> |
| 27 | #ifdef _WIN32 |
| 28 | #define strcasecmp _stricmp |
| 29 | #endif |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 30 | #include <rfb/CSecurityNone.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 31 | #include <rfb/CSecurityStack.h> |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 32 | #include <rfb/CSecurityVeNCrypt.h> |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 33 | #include <rfb/CSecurityVncAuth.h> |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 34 | #include <rfb/CSecurityPlain.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 35 | #include <rdr/Exception.h> |
| 36 | #include <rfb/LogWriter.h> |
Adam Tkac | b6eb399 | 2010-04-23 14:05:00 +0000 | [diff] [blame] | 37 | #include <rfb/Security.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 38 | #include <rfb/SSecurityNone.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 39 | #include <rfb/SSecurityStack.h> |
Adam Tkac | 520fc41 | 2010-09-02 14:13:24 +0000 | [diff] [blame^] | 40 | #include <rfb/SSecurityPlain.h> |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 41 | #include <rfb/SSecurityVncAuth.h> |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 42 | #include <rfb/SSecurityVeNCrypt.h> |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 43 | #ifdef HAVE_GNUTLS |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 44 | #include <rfb/CSecurityTLS.h> |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 45 | #include <rfb/SSecurityTLS.h> |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 46 | #endif |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 47 | #include <rfb/util.h> |
| 48 | |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 49 | using namespace rdr; |
| 50 | using namespace rfb; |
| 51 | using namespace std; |
| 52 | |
| 53 | static LogWriter vlog("Security"); |
| 54 | |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 55 | UserPasswdGetter *CSecurity::upg = NULL; |
| 56 | |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 57 | StringParameter Security::secTypesViewer |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 58 | ("SecurityTypes", |
| 59 | "Specify which security scheme to use (None, VncAuth)", |
Adam Tkac | 80fb7ef | 2010-08-12 14:24:43 +0000 | [diff] [blame] | 60 | #ifdef HAVE_GNUTLS |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 61 | "VeNCrypt,X509Plain,TLSPlain,X509Vnc,TLSVnc,X509None,TLSNone,VncAuth,None", |
Adam Tkac | 80fb7ef | 2010-08-12 14:24:43 +0000 | [diff] [blame] | 62 | #else |
| 63 | "VncAuth,None", |
| 64 | #endif |
| 65 | ConfViewer); |
Adam Tkac | a6578bf | 2010-04-23 14:07:41 +0000 | [diff] [blame] | 66 | |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 67 | StringParameter Security::secTypesServer |
| 68 | ("SecurityTypes", |
| 69 | "Specify which security scheme to use (None, VncAuth)", |
Adam Tkac | 80fb7ef | 2010-08-12 14:24:43 +0000 | [diff] [blame] | 70 | #ifdef HAVE_GNUTLS |
| 71 | "VeNCrypt,TLSVnc,VncAuth", |
| 72 | #else |
| 73 | "VncAuth", |
| 74 | #endif |
| 75 | ConfServer); |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 76 | |
| 77 | Security::Security(SecurityClassType secClassType) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 78 | { |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 79 | char *secTypesStr; |
| 80 | |
| 81 | switch (secClassType) { |
| 82 | case SecurityViewer: |
| 83 | secTypesStr = secTypesViewer.getData(); |
| 84 | break; |
| 85 | case SecurityServer: |
| 86 | secTypesStr = secTypesServer.getData(); |
| 87 | break; |
| 88 | }; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 89 | |
| 90 | enabledSecTypes = parseSecTypes(secTypesStr); |
| 91 | |
| 92 | delete secTypesStr; |
| 93 | } |
| 94 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 95 | const std::list<rdr::U8> Security::GetEnabledSecTypes(void) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 96 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 97 | list<rdr::U8> result; |
| 98 | list<U32>::iterator i; |
| 99 | |
| 100 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 101 | if (*i < 0x100) |
| 102 | result.push_back(*i); |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void) |
| 108 | { |
| 109 | list<rdr::U32> result; |
| 110 | list<U32>::iterator i; |
| 111 | |
| 112 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
Adam Tkac | 95e2fe8 | 2010-08-12 13:54:59 +0000 | [diff] [blame] | 113 | if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */ |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 114 | result.push_back(*i); |
| 115 | |
| 116 | return result; |
| 117 | } |
| 118 | |
| 119 | void Security::EnableSecType(U32 secType) |
| 120 | { |
| 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; |
| 126 | |
| 127 | enabledSecTypes.push_back(secType); |
| 128 | } |
| 129 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 130 | bool Security::IsSupported(U32 secType) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 131 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 132 | list<U32>::iterator i; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 133 | |
| 134 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 135 | if (*i == secType) |
| 136 | return true; |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 141 | SSecurity* Security::GetSSecurity(U32 secType) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 142 | { |
| 143 | if (!IsSupported(secType)) |
| 144 | goto bail; |
| 145 | |
| 146 | switch (secType) { |
| 147 | case secTypeNone: return new SSecurityNone(); |
| 148 | case secTypeVncAuth: return new SSecurityVncAuth(); |
Adam Tkac | a032593 | 2010-07-20 15:14:08 +0000 | [diff] [blame] | 149 | case secTypeVeNCrypt: return new SSecurityVeNCrypt(this); |
Adam Tkac | 520fc41 | 2010-09-02 14:13:24 +0000 | [diff] [blame^] | 150 | case secTypePlain: return new SSecurityPlain(); |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 151 | #ifdef HAVE_GNUTLS |
| 152 | case secTypeTLSNone: |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 153 | return new SSecurityStack(secTypeTLSNone, new SSecurityTLS(true)); |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 154 | case secTypeTLSVnc: |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 155 | return new SSecurityStack(secTypeTLSVnc, new SSecurityTLS(true), new SSecurityVncAuth()); |
Adam Tkac | 520fc41 | 2010-09-02 14:13:24 +0000 | [diff] [blame^] | 156 | case secTypeTLSPlain: |
| 157 | return new SSecurityStack(secTypeTLSPlain, new SSecurityTLS(true), new SSecurityPlain()); |
Adam Tkac | 5bf73fb | 2010-07-21 09:08:24 +0000 | [diff] [blame] | 158 | case secTypeX509None: |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 159 | return new SSecurityStack(secTypeX509None, new SSecurityTLS(false)); |
Adam Tkac | 5bf73fb | 2010-07-21 09:08:24 +0000 | [diff] [blame] | 160 | case secTypeX509Vnc: |
Adam Tkac | 21b61a5 | 2010-07-21 09:19:00 +0000 | [diff] [blame] | 161 | return new SSecurityStack(secTypeX509None, new SSecurityTLS(false), new SSecurityVncAuth()); |
Adam Tkac | 520fc41 | 2010-09-02 14:13:24 +0000 | [diff] [blame^] | 162 | case secTypeX509Plain: |
| 163 | return new SSecurityStack(secTypeX509Plain, new SSecurityTLS(false), new SSecurityPlain()); |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 164 | #endif |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bail: |
| 168 | throw Exception("Security type not supported"); |
| 169 | } |
| 170 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 171 | CSecurity* Security::GetCSecurity(U32 secType) |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 172 | { |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 173 | assert (CSecurity::upg != NULL); /* (upg == NULL) means bug in the viewer */ |
Adam Tkac | c210e8a | 2010-04-23 14:09:16 +0000 | [diff] [blame] | 174 | |
| 175 | if (!IsSupported(secType)) |
| 176 | goto bail; |
| 177 | |
| 178 | switch (secType) { |
| 179 | case secTypeNone: return new CSecurityNone(); |
Adam Tkac | b10489b | 2010-04-23 14:16:04 +0000 | [diff] [blame] | 180 | case secTypeVncAuth: return new CSecurityVncAuth(); |
Adam Tkac | a032593 | 2010-07-20 15:14:08 +0000 | [diff] [blame] | 181 | case secTypeVeNCrypt: return new CSecurityVeNCrypt(this); |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 182 | case secTypePlain: return new CSecurityPlain(); |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 183 | #ifdef HAVE_GNUTLS |
| 184 | case secTypeTLSNone: |
| 185 | return new CSecurityStack(secTypeTLSNone, "TLS with no password", |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 186 | new CSecurityTLS(true)); |
Adam Tkac | 707d361 | 2010-07-20 15:16:10 +0000 | [diff] [blame] | 187 | case secTypeTLSVnc: |
| 188 | return new CSecurityStack(secTypeTLSVnc, "TLS with VNCAuth", |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 189 | new CSecurityTLS(true), new CSecurityVncAuth()); |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 190 | case secTypeTLSPlain: |
| 191 | return new CSecurityStack(secTypeTLSPlain, "TLS with Username/Password", |
| 192 | new CSecurityTLS(true), new CSecurityPlain()); |
Adam Tkac | f5f6a00 | 2010-07-21 09:09:19 +0000 | [diff] [blame] | 193 | case secTypeX509None: |
| 194 | return new CSecurityStack(secTypeX509None, "X509 with no password", |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 195 | new CSecurityTLS(false)); |
Adam Tkac | f5f6a00 | 2010-07-21 09:09:19 +0000 | [diff] [blame] | 196 | case secTypeX509Vnc: |
| 197 | return new CSecurityStack(secTypeX509None, "X509 with VNCAuth", |
Adam Tkac | 3c5be39 | 2010-07-21 09:27:34 +0000 | [diff] [blame] | 198 | new CSecurityTLS(false), new CSecurityVncAuth()); |
Adam Tkac | 8c04838 | 2010-09-02 12:37:00 +0000 | [diff] [blame] | 199 | case secTypeX509Plain: |
| 200 | return new CSecurityStack(secTypeX509Plain, "X509 with Username/Password", |
| 201 | new CSecurityTLS(false), new CSecurityPlain()); |
Adam Tkac | df79970 | 2010-04-28 15:45:53 +0000 | [diff] [blame] | 202 | #endif |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bail: |
| 206 | throw Exception("Security type not supported"); |
| 207 | } |
| 208 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 209 | rdr::U32 rfb::secTypeNum(const char* name) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 210 | { |
| 211 | if (strcasecmp(name, "None") == 0) return secTypeNone; |
| 212 | if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth; |
| 213 | if (strcasecmp(name, "Tight") == 0) return secTypeTight; |
| 214 | if (strcasecmp(name, "RA2") == 0) return secTypeRA2; |
| 215 | if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne; |
| 216 | if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 217 | if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne; |
| 218 | if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 219 | |
| 220 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 221 | if (strcasecmp(name, "Plain") == 0) return secTypePlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 222 | if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone; |
| 223 | if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc; |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 224 | if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 225 | if (strcasecmp(name, "X509None") == 0) return secTypeX509None; |
| 226 | if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 227 | if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 228 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 229 | return secTypeInvalid; |
| 230 | } |
| 231 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 232 | const char* rfb::secTypeName(rdr::U32 num) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 233 | { |
| 234 | switch (num) { |
| 235 | case secTypeNone: return "None"; |
| 236 | case secTypeVncAuth: return "VncAuth"; |
| 237 | case secTypeTight: return "Tight"; |
| 238 | case secTypeRA2: return "RA2"; |
| 239 | case secTypeRA2ne: return "RA2ne"; |
| 240 | case secTypeSSPI: return "SSPI"; |
| 241 | case secTypeSSPIne: return "SSPIne"; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 242 | case secTypeVeNCrypt: return "VeNCrypt"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 243 | |
| 244 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 245 | case secTypePlain: return "Plain"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 246 | case secTypeTLSNone: return "TLSNone"; |
| 247 | case secTypeTLSVnc: return "TLSVnc"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 248 | case secTypeTLSPlain: return "TLSPlain"; |
| 249 | case secTypeX509None: return "X509None"; |
| 250 | case secTypeX509Vnc: return "X509Vnc"; |
| 251 | case secTypeX509Plain: return "X509Plain"; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 252 | default: return "[unknown secType]"; |
| 253 | } |
| 254 | } |
| 255 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 256 | std::list<rdr::U32> rfb::parseSecTypes(const char* types_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 257 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 258 | std::list<rdr::U32> result; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 259 | CharArray types(strDup(types_)), type; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 260 | while (types.buf) { |
| 261 | strSplit(types.buf, ',', &type.buf, &types.buf); |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 262 | rdr::U32 typeNum = secTypeNum(type.buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 263 | if (typeNum != secTypeInvalid) |
| 264 | result.push_back(typeNum); |
| 265 | } |
| 266 | return result; |
| 267 | } |