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