summaryrefslogtreecommitdiffstats
path: root/kio/misc/tdesasl/tdesasl.cpp
blob: 37195faf3f245243b7f82ef60639b1a2c7b1c4a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/* This file is part of the KDE libraries
   Copyright (C) 2001-2002 Michael Häckel <haeckel@kde.org>
   $Id$

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "tdesasl.h"

#include <kmdcodec.h>
#include <kurl.h>

#include <tqstrlist.h>

#include <stdlib.h>
#include <string.h>

KDESasl::KDESasl(const KURL &aUrl)
{
  mProtocol = aUrl.protocol();
  mUser = aUrl.user();
  mPass = aUrl.pass();
  mFirst = true;
}

KDESasl::KDESasl(const TQString &aUser, const TQString &aPass,
  const TQString &aProtocol)
{
  mProtocol = aProtocol;
  mUser = aUser;
  mPass = aPass;
  mFirst = true;
}

KDESasl::~KDESasl() {
}

TQCString KDESasl::chooseMethod(const TQStrIList aMethods)
{
  if (aMethods.contains("DIGEST-MD5")) mMethod = "DIGEST-MD5";
  else if (aMethods.contains("CRAM-MD5")) mMethod = "CRAM-MD5";
  else if (aMethods.contains("PLAIN")) mMethod = "PLAIN";
  else if (aMethods.contains("LOGIN")) mMethod = "LOGIN";
  else mMethod = TQCString();
  return mMethod;
}

void KDESasl::setMethod(const TQCString &aMethod)
{
  mMethod = aMethod.upper();
}

TQByteArray KDESasl::getPlainResponse()
{
  TQCString user = mUser.utf8();
  TQCString pass = mPass.utf8();
  int userlen = user.length();
  int passlen = pass.length();
  // result = $user\0$user\0$pass (no trailing \0)
  TQByteArray result(2 * userlen + passlen + 2);
  if ( userlen ) {
    memcpy( result.data(), user.data(), userlen );
    memcpy( result.data() + userlen + 1, user.data(), userlen );
  }
  if ( passlen )
    memcpy( result.data() + 2 * userlen + 2, pass.data(), passlen );
  result[userlen] = result[2*userlen+1] = '\0';
  return result;
}

TQByteArray KDESasl::getLoginResponse()
{
  TQByteArray result = (mFirst) ? mUser.utf8() : mPass.utf8();
  mFirst = !mFirst;
  if (result.size()) result.resize(result.size() - 1);
  return result;
}

TQByteArray KDESasl::getCramMd5Response(const TQByteArray &aChallenge)
{
  uint i;
  TQByteArray secret = mPass.utf8();
  int len = mPass.utf8().length();
  secret.resize(len);
  if (secret.size() > 64)
  {
    KMD5 md5(secret);
    secret.duplicate((const char*)(&(md5.rawDigest()[0])), 16);
    len = 16;
  }
  secret.resize(64);
  for (i = len; i < 64; i++) secret[i] = 0;
  TQByteArray XorOpad(64);
  for (i = 0; i < 64; i++) XorOpad[i] = secret[i] ^ 0x5C;
  TQByteArray XorIpad(64);
  for (i = 0; i < 64; i++) XorIpad[i] = secret[i] ^ 0x36;
  KMD5 md5;
  md5.update(XorIpad);
  md5.update(aChallenge);
  KMD5 md5a;
  md5a.update(XorOpad);
  md5a.update(md5.rawDigest(), 16);
  TQByteArray result = mUser.utf8();
  len = mUser.utf8().length();
  result.resize(len + 33);
  result[len] = ' ';
  TQCString ch = md5a.hexDigest();
  for (i = 0; i < 32; i++) result[i+len+1] = *(ch.data() + i);
  return result;
}

TQByteArray KDESasl::getDigestMd5Response(const TQByteArray &aChallenge)
{
  mFirst = !mFirst;
  if (mFirst) return TQByteArray();
  TQCString str, realm, nonce, qop, algorithm, charset;
  TQCString nc = "00000001";
  unsigned int a, b, c, d;
  a = 0;
  while (a < aChallenge.size())
  {
    b = a;
    while (b < aChallenge.size() && aChallenge[b] != '=') b++;
    c = b + 1;
    if (aChallenge[c] == '"')
    {
      d = c + 1;
      while (d < aChallenge.size() && aChallenge[d] != '"') d++;
      c++;
    } else {
      d = c;
      while (d < aChallenge.size() && aChallenge[d] != ',') d++;
    }
    str = TQCString(aChallenge.data() + c, d - c + 1);
    if (qstrnicmp(aChallenge.data() + a, "realm=", 6) == 0) realm = str;
    else if (qstrnicmp(aChallenge.data() + a, "nonce=", 6) == 0) nonce = str;
    else if (qstrnicmp(aChallenge.data() + a, "qop=", 4) == 0) qop = str;
    else if (qstrnicmp(aChallenge.data() + a, "algorithm=", 10) == 0)
      algorithm = str;
    else if (qstrnicmp(aChallenge.data() + a, "charset=", 8) == 0)
      charset = str;
    a = (d < aChallenge.size() && aChallenge[d] == '"') ? d + 2 : d + 1;
  }
  if (qop.isEmpty()) qop = "auth";
  qop = "auth";
  bool utf8 = qstricmp(charset, "utf-8") == 0;
  TQCString digestUri = TQCString(mProtocol.latin1()) + "/" + realm;

  /* Calculate the response */
  /* Code based on code from the http io-slave
     Copyright (C) 2000,2001 Dawit Alemayehu <adawit@kde.org>
     Copyright (C) 2000,2001 Waldo Bastian <bastian@kde.org>
     Copyright (C) 2000,2001 George Staikos <staikos@kde.org> */
  KMD5 md, md2;
  TQCString HA1, HA2;
  TQCString cnonce;
  cnonce.setNum((1 + static_cast<int>(100000.0*rand()/(RAND_MAX+1.0))));
  cnonce = KCodecs::base64Encode( cnonce );

  // Calculate H(A1)
  TQCString authStr = (utf8) ? mUser.utf8() : TQCString(mUser.latin1());
  authStr += ':';
  authStr += realm;
  authStr += ':';
  authStr += (utf8) ? mPass.utf8() : TQCString(mPass.latin1());

  md.update( authStr );
  authStr = "";
  if ( algorithm == "md5-sess" )
  {
    authStr += ':';
    authStr += nonce;
    authStr += ':';
    authStr += cnonce;
  }
  md2.reset();
  /* SASL authentication uses rawDigest here, whereas HTTP authentication uses
     hexDigest() */
  md2.update(md.rawDigest(), 16);
  md2.update( authStr );
  md2.hexDigest( HA1 );

  // Calcualte H(A2)
  authStr = "AUTHENTICATE:";
  authStr += digestUri;
  if ( qop == "auth-int" || qop == "auth-conf" )
  {
    authStr += ":00000000000000000000000000000000";
  }
  md.reset();
  md.update( authStr );
  md.hexDigest( HA2 );

  // Calcualte the response.
  authStr = HA1;
  authStr += ':';
  authStr += nonce;
  authStr += ':';
  if ( !qop.isEmpty() )
  {
    authStr += nc;
    authStr += ':';
    authStr += cnonce;
    authStr += ':';
    authStr += qop;
    authStr += ':';
  }
  authStr += HA2;
  md.reset();
  md.update( authStr );
  TQCString response = md.hexDigest();
  /* End of response calculation */

  TQCString result;
  if (utf8)
  {
    result = "charset=utf-8,username=\"" + mUser.utf8();
  } else {
    result = "charset=iso-8859-1,username=\"" + TQCString(mUser.latin1());
  }
  result += "\",realm=\"" + realm + "\",nonce=\"" + nonce;
  result += "\",nc=" + nc + ",cnonce=\"" + cnonce;
  result += "\",digest-uri=\"" + digestUri;
  result += "\",response=" + response + ",qop=" + qop;
  TQByteArray ba;
  ba.duplicate(result.data(), result.length());
  return ba;
}

TQByteArray KDESasl::getBinaryResponse(const TQByteArray &aChallenge, bool aBase64)
{
  if (aBase64)
  {
    TQByteArray ba;
    KCodecs::base64Decode(aChallenge, ba);
    KCodecs::base64Encode(getBinaryResponse(ba, false), ba);
    return ba;
  }
  if (qstricmp(mMethod, "PLAIN") == 0) return getPlainResponse();
  if (qstricmp(mMethod, "LOGIN") == 0) return getLoginResponse();
  if (qstricmp(mMethod, "CRAM-MD5") == 0)
    return getCramMd5Response(aChallenge);
  if (qstricmp(mMethod, "DIGEST-MD5") == 0)
    return getDigestMd5Response(aChallenge);
//    return getDigestMd5Response(TQCString("realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",qop=\"auth\",algorithm=md5-sess,charset=utf-8"));
  return TQByteArray();
}

TQCString KDESasl::getResponse(const TQByteArray &aChallenge, bool aBase64)
{
  TQByteArray ba = getBinaryResponse(aChallenge, aBase64);
  return TQCString(ba.data(), ba.size() + 1);
}

TQCString KDESasl::method() const {
  return mMethod;
}

bool KDESasl::clientStarts() const {
  return method() == "PLAIN";
}

bool KDESasl::dialogComplete( int n ) const {
  if ( method() == "PLAIN" || method() == "CRAM-MD5" )
    return n >= 1;
  if ( method() == "LOGIN" || method() == "DIGEST-MD5" )
    return n >= 2;
  return true;
}

bool KDESasl::isClearTextMethod() const {
  return method() == "PLAIN" || method() == "LOGIN" ;
}