~ubuntu-branches/ubuntu/trusty/kvirc/trusty-proposed

« back to all changes in this revision

Viewing changes to src/kvilib/net/KviSASL.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Kai Wasserbäch, Kai Wasserbäch, Raúl Sánchez Siles
  • Date: 2011-02-12 10:40:21 UTC
  • mfrom: (14.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212104021-5mh4f75jlku20mnt
The combined "Twisted Experiment" and "Nocturnal Raid" release.

[ Kai Wasserbäch ]
* Synced to upstream's SVN revision 5467.
* debian/rules:
  - Added .PHONY line.
  - Resurrect -DMANUAL_REVISION, got lost somewhere and we build SVN
    revisions again.
  - Replace "-DWITH_NO_EMBEDDED_CODE=YES" with "-DWANT_CRYPTOPP=YES".
  - Change the remaining -DWITH/-DWITHOUT to the new -DWANT syntax.
* debian/control:
  - Removed DMUA, I'm a DD now.
  - Changed my e-mail address.
  - Removed unneeded relationships (no upgrades over two releases are
    supported).
  - Fix Suggests for kvirc-dbg.
  - kvirc-data: Make the "Suggests: kvirc" a Recommends, doesn't make much
    sense to install the -data package without the program.
* debian/source/local-options: Added with "unapply-patches".
* debian/kvirc.lintian-overrides: Updated to work for 4.1.1.
* debian/patches/21_make_shared-mime-info_B-D_superfluous.patch: Updated.
* debian/kvirc-data.install: Added .notifyrc.

[ Raúl Sánchez Siles ]
* Stating the right version where kvirc-data break and replace should happen.
* Fixing link to license file.
* Added French and Portuguese man pages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//=============================================================================
 
2
//
 
3
//   File : KviSASL.cpp
 
4
//   Creation date : Mon Feb 14 2010 19:36:12 CEST by Fabio Bas
 
5
//
 
6
//   This file is part of the KVIrc irc client distribution
 
7
//   Copyright (C) 2010 Fabio Bas (ctrlaltca at gmail dot com)
 
8
//
 
9
//   This program is FREE software. You can redistribute it and/or
 
10
//   modify it under the terms of the GNU General Public License
 
11
//   as published by the Free Software Foundation; either version 2
 
12
//   of the License, or (at your opinion) any later version.
 
13
//
 
14
//   This program is distributed in the HOPE that it will be USEFUL,
 
15
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
17
//   See the GNU General Public License for more details.
 
18
//
 
19
//   You should have received a copy of the GNU General Public License
 
20
//   along with this program. If not, write to the Free Software Foundation,
 
21
//   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
22
//
 
23
//=============================================================================
 
24
 
 
25
 
 
26
 
 
27
#include "KviSASL.h"
 
28
 
 
29
#include "KviMemory.h"
 
30
 
 
31
#ifdef COMPILE_SSL_SUPPORT
 
32
 
 
33
#include <openssl/blowfish.h>
 
34
#include <openssl/bn.h>
 
35
#include <openssl/dh.h>
 
36
#include <openssl/err.h>
 
37
#include <KviNetUtils.h>
 
38
 
 
39
#endif
 
40
 
 
41
 
 
42
namespace KviSASL
 
43
{
 
44
 
 
45
        bool plainMethod(KviCString & szIn, KviCString & szOut, QByteArray & baNick, QByteArray & baPass)
 
46
        {
 
47
                if(szIn=="+")
 
48
                {
 
49
                        int answerLen = 3 + (2 * baNick.size()) + baPass.size();
 
50
                        char * answer = (char *) KviMemory::allocate(answerLen);
 
51
                        char * answer2 = answer;
 
52
 
 
53
                        memcpy(answer, baNick.data(), baNick.size());
 
54
                        answer+=baNick.size();
 
55
                        memset(answer, 0, 1);
 
56
                        answer++;
 
57
 
 
58
                        memcpy(answer, baNick.data(), baNick.size());
 
59
                        answer+=baNick.size();
 
60
                        memset(answer, 0, 1);
 
61
                        answer++;
 
62
 
 
63
                        memcpy(answer, baPass.data(), baPass.size());
 
64
                        answer+=baPass.size();
 
65
                        memset(answer, 0, 1);
 
66
                        answer++;
 
67
 
 
68
                        szOut.bufferToBase64(answer2,answerLen);
 
69
                        KviMemory::free(answer2);
 
70
 
 
71
                        return true;
 
72
                }
 
73
                return false;
 
74
        }
 
75
 
 
76
#ifdef COMPILE_SSL_SUPPORT
 
77
        bool dh_blowfishMethod(KviCString & szIn, KviCString & szOut, QByteArray & baNick, QByteArray & baPass)
 
78
        {
 
79
                /*
 
80
                 * The format of the auth token is quite complex; the server sends us 3 strings:
 
81
                 * p - a prime number
 
82
                 * g - a generator number, tipically 2 or 5 are used.
 
83
                 * y - the server-generated public key
 
84
                 * These 3 strings are null-terminated and codified as pascal strings (they are prefixed
 
85
                 * with a 16-bit lenght identifiedr in "network" big-endian order)
 
86
                 * Then, the 3 strings are concatenated and base-64 encoded.
 
87
                 */
 
88
 
 
89
                BF_KEY key;
 
90
                quint16 size, pKlen;
 
91
                int secretLen;
 
92
                unsigned char *secret = NULL, *pubKey = NULL;
 
93
                char * tmpBuf;
 
94
                DH * dh = DH_new();
 
95
                int len = szIn.base64ToBuffer(&tmpBuf,false);
 
96
 
 
97
                if(len < 7) return false;
 
98
 
 
99
                // extract p
 
100
                size = ntohs(*(unsigned int*)tmpBuf);
 
101
                tmpBuf+=2;
 
102
                len-=2;
 
103
                if(size > len) return false;
 
104
 
 
105
                if(!(dh->p = BN_bin2bn((unsigned char*) tmpBuf, size, NULL)))
 
106
                        return false;
 
107
 
 
108
                tmpBuf+=size;
 
109
                len-=size;
 
110
 
 
111
                // extract g
 
112
                size = ntohs(*(unsigned int*)tmpBuf);
 
113
                tmpBuf+=2;
 
114
                len-=2;
 
115
                if(size > len) return false;
 
116
 
 
117
                if(!(dh->g = BN_bin2bn((unsigned char*) tmpBuf, size, NULL)))
 
118
                        return false;
 
119
 
 
120
                tmpBuf+=size;
 
121
                len-=size;
 
122
 
 
123
                // extract y
 
124
                size = ntohs(*(unsigned int*)tmpBuf);
 
125
                tmpBuf+=2;
 
126
                len-=2;
 
127
                if(size > len) return false;
 
128
 
 
129
                // create our keys and extract shared secret
 
130
                // note: any memory checking tool (as valgrind) will complain on this call. blame openssl
 
131
                if(!DH_generate_key(dh))
 
132
                        return false;
 
133
 
 
134
                secret=(unsigned char *) KviMemory::allocate(DH_size(dh));
 
135
                // note: any memory checking tool (as valgrind) will complain on this call. blame openssl
 
136
                if(-1 == (secretLen = DH_compute_key(secret, BN_bin2bn((unsigned char*) tmpBuf, size, NULL), dh)))
 
137
                        return false;
 
138
 
 
139
                pKlen=BN_num_bytes(dh->pub_key);
 
140
                pubKey = (unsigned char *) KviMemory::allocate(pKlen);
 
141
                BN_bn2bin(dh->pub_key, pubKey);
 
142
 
 
143
                //create crypto buffers
 
144
                int passLen = baPass.size() + ((8 -( baPass.size() % 8)) % 8);
 
145
                int passC = 0;
 
146
                unsigned char *passIn = (unsigned char *) KviMemory::allocate(passLen);
 
147
                unsigned char *passOut = (unsigned char *) KviMemory::allocate(passLen);
 
148
 
 
149
                memset(passIn, 0, passLen);
 
150
                memset(passOut, 0, passLen);
 
151
                memcpy(passIn, baPass.data(), baPass.size());
 
152
 
 
153
                // crypt our password
 
154
                BF_set_key(&key, secretLen, secret);
 
155
 
 
156
                for (passC=0; passC < passLen; passC += 8)
 
157
                        BF_ecb_encrypt(passIn + passC,  passOut + passC, &key, BF_ENCRYPT);
 
158
 
 
159
                /*
 
160
                 * Build up the answer
 
161
                 * The format of the auth answer is quite complex, and formed by the byte concatenation of:
 
162
                 * 1) a 16 bit integer containing the byte length of our public key
 
163
                 * 2) our public key
 
164
                 * 3) our username (nickname), null-terminated
 
165
                 * 4) our password, crypted using blowfish in ecb mode and the dh secret as the blowfish key
 
166
                 * Then, the answer is to be base64 encoded.
 
167
                 */
 
168
                
 
169
                int answerLen = 2 + pKlen + baNick.size() + 1 + passLen;
 
170
                char * answer = (char *) malloc(answerLen);
 
171
                char * answer2 = answer;
 
172
                *((unsigned int *)answer) = htons(pKlen);
 
173
                answer+=2;
 
174
                memcpy(answer, pubKey, pKlen);
 
175
                answer+=pKlen;
 
176
                memcpy(answer, baNick.data(), baNick.size());
 
177
                answer+=baNick.size();
 
178
                memset(answer, 0, 1);
 
179
                answer++;
 
180
                memcpy(answer, passOut, passLen);
 
181
                szOut.bufferToBase64(answer2,answerLen);
 
182
 
 
183
                //clean up
 
184
                KviMemory::free(secret);
 
185
                KviMemory::free(pubKey);
 
186
                KviMemory::free(passIn);
 
187
                KviMemory::free(passOut);
 
188
 
 
189
                return true;
 
190
        }
 
191
#else
 
192
        bool dh_blowfishMethod(KviCString & szIn, KviCString & szOut, QByteArray & baNick, QByteArray & baPass)
 
193
        {
 
194
                return false;
 
195
        }
 
196
#endif
 
197
 
 
198
};