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 | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 55 | Security::Security(StringParameter &secTypes) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 56 | { |
Adam Tkac | fb99315 | 2010-08-12 14:17:28 +0000 | [diff] [blame] | 57 | char *secTypesStr; |
| 58 | |
Adam Tkac | bfd66c1 | 2010-10-01 08:33:29 +0000 | [diff] [blame] | 59 | secTypesStr = secTypes.getData(); |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 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 | |
DRC | 674bf06 | 2011-02-21 13:14:16 +0000 | [diff] [blame^] | 70 | result.push_back(secTypeVeNCrypt); |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 71 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 72 | if (*i < 0x100) |
| 73 | result.push_back(*i); |
| 74 | |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | const std::list<rdr::U32> Security::GetEnabledExtSecTypes(void) |
| 79 | { |
| 80 | list<rdr::U32> result; |
| 81 | list<U32>::iterator i; |
| 82 | |
| 83 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
Adam Tkac | 95e2fe8 | 2010-08-12 13:54:59 +0000 | [diff] [blame] | 84 | if (*i != secTypeVeNCrypt) /* Do not include VeNCrypt type to avoid loops */ |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 85 | result.push_back(*i); |
| 86 | |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | void Security::EnableSecType(U32 secType) |
| 91 | { |
| 92 | list<U32>::iterator i; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 93 | |
| 94 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 95 | if (*i == secType) |
| 96 | return; |
| 97 | |
| 98 | enabledSecTypes.push_back(secType); |
| 99 | } |
| 100 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 101 | bool Security::IsSupported(U32 secType) |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 102 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 103 | list<U32>::iterator i; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 104 | |
| 105 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) |
| 106 | if (*i == secType) |
| 107 | return true; |
DRC | 674bf06 | 2011-02-21 13:14:16 +0000 | [diff] [blame^] | 108 | if (secType == secTypeVeNCrypt) |
| 109 | return true; |
Adam Tkac | 1d15e2d | 2010-04-23 14:06:38 +0000 | [diff] [blame] | 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
Adam Tkac | a9a7b4b | 2011-02-01 14:34:55 +0000 | [diff] [blame] | 114 | char *Security::ToString(void) |
| 115 | { |
| 116 | list<U32>::iterator i; |
| 117 | static char out[128]; /* Should be enough */ |
| 118 | bool firstpass = true; |
| 119 | const char *name; |
| 120 | |
| 121 | memset(out, 0, sizeof(out)); |
| 122 | |
| 123 | for (i = enabledSecTypes.begin(); i != enabledSecTypes.end(); i++) { |
| 124 | name = secTypeName(*i); |
| 125 | if (name[0] == '[') /* Unknown security type */ |
| 126 | continue; |
| 127 | |
| 128 | if (!firstpass) |
| 129 | strncat(out, ",", sizeof(out) - 1); |
| 130 | else |
| 131 | firstpass = false; |
| 132 | strncat(out, name, sizeof(out) - 1); |
| 133 | } |
| 134 | |
| 135 | return out; |
| 136 | } |
| 137 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 138 | rdr::U32 rfb::secTypeNum(const char* name) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 139 | { |
| 140 | if (strcasecmp(name, "None") == 0) return secTypeNone; |
| 141 | if (strcasecmp(name, "VncAuth") == 0) return secTypeVncAuth; |
| 142 | if (strcasecmp(name, "Tight") == 0) return secTypeTight; |
| 143 | if (strcasecmp(name, "RA2") == 0) return secTypeRA2; |
| 144 | if (strcasecmp(name, "RA2ne") == 0) return secTypeRA2ne; |
| 145 | if (strcasecmp(name, "SSPI") == 0) return secTypeSSPI; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 146 | if (strcasecmp(name, "SSPIne") == 0) return secTypeSSPIne; |
| 147 | if (strcasecmp(name, "VeNCrypt") == 0) return secTypeVeNCrypt; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 148 | |
| 149 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 150 | if (strcasecmp(name, "Plain") == 0) return secTypePlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 151 | if (strcasecmp(name, "TLSNone") == 0) return secTypeTLSNone; |
| 152 | if (strcasecmp(name, "TLSVnc") == 0) return secTypeTLSVnc; |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 153 | if (strcasecmp(name, "TLSPlain") == 0) return secTypeTLSPlain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 154 | if (strcasecmp(name, "X509None") == 0) return secTypeX509None; |
| 155 | if (strcasecmp(name, "X509Vnc") == 0) return secTypeX509Vnc; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 156 | if (strcasecmp(name, "X509Plain") == 0) return secTypeX509Plain; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 157 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 158 | return secTypeInvalid; |
| 159 | } |
| 160 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 161 | const char* rfb::secTypeName(rdr::U32 num) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 162 | { |
| 163 | switch (num) { |
| 164 | case secTypeNone: return "None"; |
| 165 | case secTypeVncAuth: return "VncAuth"; |
| 166 | case secTypeTight: return "Tight"; |
| 167 | case secTypeRA2: return "RA2"; |
| 168 | case secTypeRA2ne: return "RA2ne"; |
| 169 | case secTypeSSPI: return "SSPI"; |
| 170 | case secTypeSSPIne: return "SSPIne"; |
Adam Tkac | dfe19cf | 2010-04-23 14:14:11 +0000 | [diff] [blame] | 171 | case secTypeVeNCrypt: return "VeNCrypt"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 172 | |
| 173 | /* VeNCrypt subtypes */ |
Adam Tkac | 957a5ae | 2010-07-20 15:12:41 +0000 | [diff] [blame] | 174 | case secTypePlain: return "Plain"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 175 | case secTypeTLSNone: return "TLSNone"; |
| 176 | case secTypeTLSVnc: return "TLSVnc"; |
Adam Tkac | b3e60c6 | 2010-07-20 15:10:59 +0000 | [diff] [blame] | 177 | case secTypeTLSPlain: return "TLSPlain"; |
| 178 | case secTypeX509None: return "X509None"; |
| 179 | case secTypeX509Vnc: return "X509Vnc"; |
| 180 | case secTypeX509Plain: return "X509Plain"; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 181 | default: return "[unknown secType]"; |
| 182 | } |
| 183 | } |
| 184 | |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 185 | std::list<rdr::U32> rfb::parseSecTypes(const char* types_) |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | { |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 187 | std::list<rdr::U32> result; |
Adam Tkac | d36b626 | 2009-09-04 10:57:20 +0000 | [diff] [blame] | 188 | CharArray types(strDup(types_)), type; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 189 | while (types.buf) { |
| 190 | strSplit(types.buf, ',', &type.buf, &types.buf); |
Adam Tkac | 0c77e51 | 2010-07-20 15:10:16 +0000 | [diff] [blame] | 191 | rdr::U32 typeNum = secTypeNum(type.buf); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 192 | if (typeNum != secTypeInvalid) |
| 193 | result.push_back(typeNum); |
| 194 | } |
| 195 | return result; |
| 196 | } |