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