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