NTLM: don't hard-code key length in RC4K()
[siplcs.git] / src / core / sip-sec-ntlm.c
blob75b61df0ff7b5cff420b95bf9c58ff27d8c1c9ab
1 /**
2 * @file sip-sec-ntlm.c
4 * pidgin-sipe
6 * Copyright (C) 2009, 2010 pier11 <pier11@operamail.com>
7 * Copyright (C) 2008 Novell, Inc.
8 * Modify 2007, Anibal Avelar <avelar@gmail.com>
9 * Copyright (C) 2005, Thomas Butter <butter@uni-mannheim.de>
11 * Implemented with reference to the follow documentation:
12 * - http://davenport.sourceforge.net/ntlm.html
13 * - MS-NLMP: http://msdn.microsoft.com/en-us/library/cc207842.aspx
14 * - MS-SIP : http://msdn.microsoft.com/en-us/library/cc246115.aspx
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <glib.h>
32 #include <glib/gprintf.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include "debug.h"
39 #ifndef _WIN32
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
42 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #else /* _WIN32 */
46 #include "libc_interface.h"
47 #ifdef _DLL
48 #define _WS2TCPIP_H_
49 #define _WINSOCK2API_
50 #define _LIBC_INTERNAL_
51 #endif /* _DLL */
52 #include "network.h"
53 #include "internal.h"
54 #endif /* _WIN32 */
56 #ifdef HAVE_LANGINFO_CODESET
57 #include <langinfo.h>
58 #endif /* HAVE_LANGINFO_CODESET */
60 #include "util.h"
61 #include "cipher.h"
63 #include "sipe.h"
64 #include "sipe-utils.h"
65 #include "sip-sec-mech.h"
66 #include "sip-sec-ntlm.h"
68 /* [MS-NLMP] */
69 #define NTLMSSP_NEGOTIATE_UNICODE 0x00000001 /* A */
70 #define NTLMSSP_NEGOTIATE_OEM 0x00000002 /* B */
71 #define NTLMSSP_REQUEST_TARGET 0x00000004 /* C */
72 #define r9 0x00000008 /* r9 */
73 #define NTLMSSP_NEGOTIATE_SIGN 0x00000010 /* D */
74 #define NTLMSSP_NEGOTIATE_SEAL 0x00000020 /* E */
75 #define NTLMSSP_NEGOTIATE_DATAGRAM 0x00000040 /* F */
76 #define NTLMSSP_NEGOTIATE_LM_KEY 0x00000080 /* G */
77 #define r8 0x00000100 /* r8 */
78 #define NTLMSSP_NEGOTIATE_NTLM 0x00000200 /* H */
79 #define NTLMSSP_NEGOTIATE_NT_ONLY 0x00000400 /* I */
80 #define anonymous 0x00000800 /* J */
81 #define NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED 0x00001000 /* K */
82 #define NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED 0x00002000 /* L */
83 #define r7 0x00004000 /* r7 */
84 #define NTLMSSP_NEGOTIATE_ALWAYS_SIGN 0x00008000 /* M */
85 #define NTLMSSP_TARGET_TYPE_DOMAIN 0x00010000 /* N */
86 #define NTLMSSP_TARGET_TYPE_SERVER 0x00020000 /* O */
87 #define r6 0x00040000 /* r6 */
88 #define NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY 0x00080000 /* P */
89 #define NTLMSSP_NEGOTIATE_IDENTIFY 0x00100000 /* Q */
90 #define r5 0x00200000 /* r5 */
91 #define NTLMSSP_REQUEST_NON_NT_SESSION_KEY 0x00400000 /* R */
92 #define NTLMSSP_NEGOTIATE_TARGET_INFO 0x00800000 /* S */
93 #define r4 0x01000000 /* r4 */
94 #define NTLMSSP_NEGOTIATE_VERSION 0x02000000 /* T */
95 #define r3 0x04000000 /* r3 */
96 #define r2 0x08000000 /* r2 */
97 #define r1 0x10000000 /* r1 */
98 #define NTLMSSP_NEGOTIATE_128 0x20000000 /* U */
99 #define NTLMSSP_NEGOTIATE_KEY_EXCH 0x40000000 /* V */
100 #define NTLMSSP_NEGOTIATE_56 0x80000000 /* W */
102 /* AvId */
103 #define MsvAvEOL 0
104 #define MsvAvNbComputerName 1
105 #define MsvAvNbDomainName 2
106 #define MsvAvDnsComputerName 3
107 #define MsvAvDnsDomainName 4
108 /** @since Windows XP */
109 #define MsvAvDnsTreeName 5
110 /** @since Windows XP */
111 #define MsvAvFlags 6
112 /** @since Windows Vista */
113 #define MsvAvTimestamp 7
114 /** @since Windows Vista */
115 #define MsAvRestrictions 8
116 /** @since Windows 7 */
117 #define MsvAvTargetName 9
118 /** @since Windows 7 */
119 #define MsvChannelBindings 10
121 /***********************************************
123 * Start of merged code from original sip-ntlm.c
125 ***********************************************/
127 /* Negotiate flags required in connection-oriented NTLM */
128 #define NEGOTIATE_FLAGS_CONN \
129 ( NTLMSSP_NEGOTIATE_UNICODE | \
130 NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY | \
131 NTLMSSP_REQUEST_TARGET | \
132 NTLMSSP_NEGOTIATE_NTLM | \
133 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | \
136 /* Negotiate flags required in connectionless NTLM */
137 #define NEGOTIATE_FLAGS \
138 ( NTLMSSP_NEGOTIATE_UNICODE | \
139 NTLMSSP_NEGOTIATE_SIGN | \
140 NTLMSSP_NEGOTIATE_DATAGRAM | \
141 NTLMSSP_NEGOTIATE_NTLM | \
142 NTLMSSP_NEGOTIATE_ALWAYS_SIGN | \
143 NTLMSSP_NEGOTIATE_KEY_EXCH )
145 #define NTLM_NEGOTIATE_NTLM2_KEY 0x00080000
147 #define NTLMSSP_NT_OR_LM_KEY_LEN 24
148 #define NTLMSSP_SESSION_KEY_LEN 16
149 #define MD4_DIGEST_LEN 16
150 #define MD5_DIGEST_LEN 16
152 #define IS_FLAG(flags, flag) ((flags & flag) == flag)
154 struct av_pair {
155 guint16 av_id;
156 guint16 av_len;
157 /* value */
160 /* 8 bytes */
161 struct version {
162 guint8 product_major_version;
163 guint8 product_minor_version;
164 guint16 product_build;
165 guint8 zero2[3];
166 guint8 ntlm_revision_current;
169 /* 8 bytes */
170 struct smb_header {
171 guint16 len;
172 guint16 maxlen;
173 guint32 offset;
176 struct ntlm_message {
177 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0'*/
178 guint32 type; /* 0x00000003 */
181 struct negotiate_message {
182 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0' */
183 guint32 type; /* 0x00000001 */
184 guint32 flags; /* 0xb203 */
185 struct smb_header domain;
186 struct smb_header host;
187 struct version ver;
188 /* payload
189 * - DomainName (always ASCII)
190 * - WorkstationName (always ASCII)
194 struct challenge_message {
195 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0'*/
196 guint32 type; /* 0x00000002 */
197 struct smb_header target_name;
198 guint32 flags; /* 0x8201 */
199 guint8 nonce[8];
200 guint8 zero1[8];
201 struct smb_header target_info;
202 struct version ver;
203 /* payload
204 * - TargetName (negotiated encoding)
205 * - TargetInfo (a sequence of AV_PAIR structures) (always Unicode)
209 struct authenticate_message {
210 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0'*/
211 guint32 type; /* 0x00000003 */
212 /** LmChallengeResponseFields */
213 struct smb_header lm_resp;
214 /** NtChallengeResponseFields */
215 struct smb_header nt_resp;
216 /** DomainNameFields */
217 struct smb_header domain;
218 /** UserNameFields */
219 struct smb_header user;
220 /** WorkstationFields */
221 struct smb_header host;
222 /** EncryptedRandomSessionKeyFields */
223 struct smb_header session_key;
224 guint32 flags;
225 struct version ver;
226 guint8 mic[16];
227 /* payload
228 * - LmChallengeResponse
229 * - NtChallengeResponse
230 * - DomainName (negotiated encoding)
231 * - UserName (negotiated encoding)
232 * - Workstation (negotiated encoding)
233 * - EncryptedRandomSessionKey
237 #ifndef HAVE_LANGINFO_CODESET
238 static char SIPE_DEFAULT_CODESET[] = "ANSI_X3.4-1968";
239 #endif
241 /* Private Methods */
243 static void setup_des_key(const unsigned char key_56[], unsigned char *key)
245 key[0] = key_56[0];
246 key[1] = ((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1);
247 key[2] = ((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2);
248 key[3] = ((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3);
249 key[4] = ((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4);
250 key[5] = ((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5);
251 key[6] = ((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6);
252 key[7] = (key_56[6] << 1) & 0xFF;
255 static void des_ecb_encrypt(const unsigned char *plaintext, unsigned char *result, const unsigned char *key)
257 PurpleCipher *cipher;
258 PurpleCipherContext *context;
259 size_t outlen;
261 cipher = purple_ciphers_find_cipher("des");
262 context = purple_cipher_context_new(cipher, NULL);
263 purple_cipher_context_set_key(context, (guchar*)key);
264 purple_cipher_context_encrypt(context, (guchar*)plaintext, 8, (guchar*)result, &outlen);
265 purple_cipher_context_destroy(context);
268 static int
269 unicode_strconvcopy_dir(gchar *dest, const gchar *source, int remlen, gsize source_len, gboolean to_16LE)
271 GIConv fd;
272 gchar *inbuf = (gchar *) source;
273 gchar *outbuf = dest;
274 gsize inbytes = source_len;
275 gsize outbytes = remlen;
276 #ifdef HAVE_LANGINFO_CODESET
277 char *sys_cp = nl_langinfo(CODESET);
278 #else
279 char *sys_cp = SIPE_DEFAULT_CODESET;
280 #endif /* HAVE_LANGINFO_CODESET */
282 /* fall back to utf-8 */
283 if (!sys_cp) sys_cp = "UTF-8";
285 fd = to_16LE ? g_iconv_open("UTF-16LE", sys_cp) : g_iconv_open(sys_cp, "UTF-16LE");
286 if( fd == (GIConv)-1 ) {
287 purple_debug_error( "sipe", "iconv_open returned -1, cannot continue\n" );
289 g_iconv(fd, &inbuf, &inbytes, &outbuf, &outbytes);
290 g_iconv_close(fd);
291 return (remlen - outbytes);
294 static int
295 unicode_strconvcopy(gchar *dest, const gchar *source, int remlen)
297 return unicode_strconvcopy_dir(dest, source, remlen, strlen(source), TRUE);
300 /* UTF-16LE to native encoding
301 * Must be g_free'd after use */
302 static gchar *
303 unicode_strconvcopy_back(const gchar *source,
304 int len)
306 char *res = NULL;
307 int dest_len = 2 * len;
308 gchar *dest = g_new0(gchar, dest_len);
310 dest_len = unicode_strconvcopy_dir(dest, source, dest_len, len, FALSE);
311 res = g_strndup(dest, dest_len);
312 g_free(dest);
314 return res;
317 // (k = 7 byte key, d = 8 byte data) returns 8 bytes in results
318 static void
319 DES (const unsigned char *k, const unsigned char *d, unsigned char * results)
321 unsigned char key[8];
322 setup_des_key(k, key);
323 des_ecb_encrypt(d, results, key);
326 // (K = 21 byte key, D = 8 bytes of data) returns 24 bytes in results:
327 static void
328 DESL (unsigned char *k, const unsigned char *d, unsigned char * results)
330 unsigned char keys[21];
332 // Copy the first 16 bytes
333 memcpy(keys, k, 16);
335 // Zero out the last 5 bytes of the key
336 memset(keys + 16, 0, 5);
338 DES(keys, d, results);
339 DES(keys + 7, d, results + 8);
340 DES(keys + 14, d, results + 16);
343 /* out 16 bytes */
344 static void
345 MD4 (const unsigned char * d, int len, unsigned char * result)
347 PurpleCipher * cipher = purple_ciphers_find_cipher("md4");
348 PurpleCipherContext * context = purple_cipher_context_new(cipher, NULL);
349 purple_cipher_context_append(context, (guchar*)d, len);
350 purple_cipher_context_digest(context, MD4_DIGEST_LEN, (guchar*)result, NULL);
351 purple_cipher_context_destroy(context);
354 /* out 16 bytes */
355 static void
356 MD5 (const unsigned char * d, int len, unsigned char * result)
358 PurpleCipher * cipher = purple_ciphers_find_cipher("md5");
359 PurpleCipherContext * context = purple_cipher_context_new(cipher, NULL);
360 purple_cipher_context_append(context, (guchar*)d, len);
361 purple_cipher_context_digest(context, MD5_DIGEST_LEN, (guchar*)result, NULL);
362 purple_cipher_context_destroy(context);
365 /* out 16 bytes */
367 static void
368 HMACT64 (const unsigned char *key, int key_len, const unsigned char *data, int data_len, unsigned char *result)
370 int i;
371 unsigned char ibuff[64 + data_len];
372 unsigned char obuff[64 + 16];
374 if (key_len > 64)
375 key_len = 64;
377 for (i = 0; i < key_len; i++) {
378 ibuff[i] = key[i] ^ 0x36;
379 obuff[i] = key[i] ^ 0x5c;
381 for (i = key_len; i < 64; i++) {
382 ibuff[i] = 0x36;
383 obuff[i] = 0x5c;
386 memcpy(ibuff+64, data, data_len);
388 MD5 (ibuff, 64 + data_len, obuff+64);
389 MD5 (obuff, 64 + 16, result);
391 #define HMAC_MD5 HMACT64
394 /* out 16 bytes */
395 static void
396 HMAC_MD5 (const unsigned char *key, int key_len, const unsigned char *data, int data_len, unsigned char *result)
398 PurpleCipher *cipher = purple_ciphers_find_cipher("hmac");
399 PurpleCipherContext *context = purple_cipher_context_new(cipher, NULL);
401 purple_cipher_context_set_option(context, "hash", "md5");
402 purple_cipher_context_set_key_with_len(context, (guchar *)key, (key_len));
404 purple_cipher_context_append(context, (guchar *)data, data_len);
405 purple_cipher_context_digest(context, 16, (guchar*)result, NULL);
406 purple_cipher_context_destroy(context);
410 Define NTOWFv1(Passwd, User, UserDom) as
411 MD4(UNICODE(Passwd))
412 EndDefine
414 /* out 16 bytes */
415 static void
416 NTOWFv1 (const char* password, SIPE_UNUSED_PARAMETER const char *user, SIPE_UNUSED_PARAMETER const char *domain, unsigned char *result)
418 int len = 2 * strlen(password); // utf16 should not be more
419 unsigned char *unicode_password = g_new0(unsigned char, len);
421 len = unicode_strconvcopy((gchar *) unicode_password, password, len);
422 MD4 (unicode_password, len, result);
423 g_free(unicode_password);
427 Define NTOWFv2(Passwd, User, UserDom) as
428 HMAC_MD5( MD4(UNICODE(Passwd)), ConcatenationOf( Uppercase(User), UserDom ) )
429 EndDefine
431 /* out 16 bytes */
432 void
433 NTOWFv2 (const char* password, const char *user, const char *domain, unsigned char *result)
435 unsigned char response_key_nt_v1 [16];
436 int len_user = user ? strlen(user) : 0;
437 int len_domain = domain ? strlen(domain) : 0;
438 unsigned char user_upper[len_user + 1];
439 int len_user_u = 2 * len_user; // utf16 should not be more
440 int len_domain_u = 2 * len_domain; // utf16 should not be more
441 unsigned char buff[(len_user + len_domain)*2];
442 int i;
444 /* Uppercase user */
445 for (i = 0; i < len_user; i++) {
446 user_upper[i] = g_ascii_toupper(user[i]);
448 user_upper[len_user] = 0;
450 len_user_u = unicode_strconvcopy((gchar *)buff, (gchar *)user_upper, len_user_u);
451 len_domain_u = unicode_strconvcopy((gchar *)(buff+len_user_u), (gchar *)domain, len_domain_u);
453 NTOWFv1(password, user, domain, response_key_nt_v1);
455 HMAC_MD5(response_key_nt_v1, 16, buff, len_user_u + len_domain_u, result);
458 static void
459 RC4K (const unsigned char * k, unsigned long key_len, const unsigned char * d, int len, unsigned char * result)
461 PurpleCipherContext * context = purple_cipher_context_new_by_name("rc4", NULL);
462 purple_cipher_context_set_option(context, "key_len", (gpointer)key_len);
463 purple_cipher_context_set_key(context, k);
464 purple_cipher_context_encrypt(context, (const guchar *)d, len, result, NULL);
465 purple_cipher_context_destroy(context);
468 static void
469 KXKEY ( guint32 flags,
470 const unsigned char * session_base_key,
471 const unsigned char * lm_challenge_resonse,
472 const guint8 * server_challenge, /* 8-bytes, nonce */
473 unsigned char * key_exchange_key)
475 if (IS_FLAG(flags, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY)) {
476 // Define KXKEY(SessionBaseKey, LmChallengeResponse, ServerChallenge) as
477 // Set KeyExchangeKey to HMAC_MD5(SessionBaseKey, ConcatenationOf(ServerChallenge, LmChallengeResponse [0..7]))
478 // EndDefine
479 guint8 tmp[16];
480 memcpy(tmp, server_challenge, 8);
481 memcpy(tmp+8, lm_challenge_resonse, 8);
482 HMAC_MD5(session_base_key, 16, tmp, 16, key_exchange_key);
483 } else {
484 // Assume v1 and NTLMSSP_REQUEST_NON_NT_SESSION_KEY not set
485 memcpy(key_exchange_key, session_base_key, 16);
490 // This method is only used for NTLMv2 and extended session security
492 If (Mode equals "Client")
493 Set SignKey to MD5(ConcatenationOf(RandomSessionKey,
494 "session key to client-to-server signing key magic constant"))
495 Else
496 Set SignKey to MD5(ConcatenationOf(RandomSessionKey,
497 "session key to server-to-client signing key magic constant"))
498 Endif
500 void
501 SIGNKEY (const unsigned char * random_session_key, gboolean client, unsigned char * result)
503 char * magic = client
504 ? "session key to client-to-server signing key magic constant"
505 : "session key to server-to-client signing key magic constant";
507 int len = strlen(magic) + 1;
508 unsigned char md5_input [16 + len];
509 memcpy(md5_input, random_session_key, 16);
510 memcpy(md5_input + 16, magic, len);
512 MD5 (md5_input, len + 16, result);
515 static void
516 LMOWFv1 (const char *password, SIPE_UNUSED_PARAMETER const char *user, SIPE_UNUSED_PARAMETER const char *domain, unsigned char *result)
518 /* "KGS!@#$%" */
519 unsigned char magic[] = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
520 unsigned char uppercase_password[14];
521 int i;
523 int len = strlen(password);
524 if (len > 14) {
525 len = 14;
528 // Uppercase password
529 for (i = 0; i < len; i++) {
530 uppercase_password[i] = g_ascii_toupper(password[i]);
533 // Zero the rest
534 for (; i < 14; i++) {
535 uppercase_password[i] = 0;
538 DES (uppercase_password, magic, result);
539 DES (uppercase_password + 7, magic, result + 8);
542 static void
543 NONCE(unsigned char *buffer, int num)
545 int i;
546 for (i = 0; i < num; i++) {
547 buffer[i] = (rand() & 0xff);
551 static void
552 Z(unsigned char *buffer, int num)
554 int i;
555 for (i = 0; i < num; i++) {
556 buffer[i] = 0;
560 /* End Private Methods */
562 static gchar *
563 sip_sec_ntlm_challenge_message_describe(struct challenge_message *cmsg);
565 static gchar *
566 purple_ntlm_parse_challenge(SipSecBuffer in_buff,
567 gboolean is_connection_based,
568 guint32 *flags)
570 guint32 our_flags = is_connection_based ? NEGOTIATE_FLAGS_CONN : NEGOTIATE_FLAGS;
571 static gchar nonce[8];
572 struct challenge_message *cmsg = (struct challenge_message*)in_buff.value;
574 memcpy(nonce, cmsg->nonce, 8);
576 purple_debug_info("sipe", "received NTLM NegotiateFlags = %X; OK? %i\n", cmsg->flags, (cmsg->flags & our_flags) == our_flags);
578 if (flags) {
579 *flags = cmsg->flags;
581 return nonce;
584 /* source copy from gg's common.c */
585 static guint32 crc32_table[256];
586 static int crc32_initialized = 0;
588 static void crc32_make_table()
590 guint32 h = 1;
591 unsigned int i, j;
593 memset(crc32_table, 0, sizeof(crc32_table));
595 for (i = 128; i; i >>= 1) {
596 h = (h >> 1) ^ ((h & 1) ? 0xedb88320L : 0);
598 for (j = 0; j < 256; j += 2 * i)
599 crc32_table[i + j] = crc32_table[j] ^ h;
602 crc32_initialized = 1;
605 static guint32 crc32(guint32 crc, const guint8 *buf, int len)
607 if (!crc32_initialized)
608 crc32_make_table();
610 if (!buf || len < 0)
611 return crc;
613 crc ^= 0xffffffffL;
615 while (len--)
616 crc = (crc >> 8) ^ crc32_table[(crc ^ *buf++) & 0xff];
618 return crc ^ 0xffffffffL;
621 static guint32
622 CRC32 (const char *msg, int len)
624 guint32 crc = 0L;//crc32(0L, Z_NULL, 0);
625 crc = crc32(crc, (guint8 *) msg, len);
626 //char * ptr = (char*) &crc;
627 //return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | (ptr[3] & 0xff);
628 return crc;
632 Version (4 bytes): A 32-bit unsigned integer that contains the signature version. This field MUST be 0x00000001.
633 RandomPad (4 bytes): A 4-byte array that contains the random pad for the message.
634 Checksum (4 bytes): A 4-byte array that contains the checksum for the message.
635 SeqNum (4 bytes): A 32-bit unsigned integer that contains the NTLM sequence number for this application message.
637 Version (4 bytes): A 32-bit unsigned integer that contains the signature version. This field MUST be 0x00000001.
638 Checksum (8 bytes): An 8-byte array that contains the checksum for the message.
639 SeqNum (4 bytes): A 32-bit unsigned integer that contains the NTLM sequence number for this application message.
641 0x00000001, RC4K(RandomPad), RC4K(CRC32(Message)), RC4K(0x00000000) XOR (application supplied SeqNum) -- RC4(X) xor X xor Y = RC4(Y)
643 // Version(4), RandomPad(4), Checksum(4), SeqNum(4)
644 // Version(4), Checksum(8), SeqNum(4) -- for ext.sess.sec.
645 // MAC(Handle, SigningKey, SeqNum, Message)
646 static gchar *
647 MAC (guint32 flags, const char * buf, unsigned char * signing_key, guint32 random_pad, long sequence, unsigned long key_len)
649 guchar result [16];
650 gint32 *res_ptr;
651 gchar signature [33];
652 int i, j;
654 if (IS_FLAG(flags, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY)) {
656 Define MAC(Handle, SigningKey, SeqNum, Message) as
657 Set NTLMSSP_MESSAGE_SIGNATURE.Version to 0x00000001
658 Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to
659 HMAC_MD5(SigningKey, ConcatenationOf(SeqNum, Message))[0..7]
660 Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to SeqNum
661 Set SeqNum to SeqNum + 1
662 EndDefine
664 /* If a key exchange key is negotiated
665 Define MAC(Handle, SigningKey, SeqNum, Message) as
666 Set NTLMSSP_MESSAGE_SIGNATURE.Version to 0x00000001
667 Set NTLMSSP_MESSAGE_SIGNATURE.Checksum to RC4(Handle,
668 HMAC_MD5(SigningKey, ConcatenationOf(SeqNum, Message))[0..7])
669 Set NTLMSSP_MESSAGE_SIGNATURE.SeqNum to SeqNum
670 Set SeqNum to SeqNum + 1
671 EndDefine
673 int buf_len = strlen(buf);
674 guchar hmac[16];
675 guchar tmp[4 + buf_len];
677 res_ptr = (gint32 *)result;
678 res_ptr[0] = 0x00000001; // 4 bytes
679 res_ptr[3] = sequence;
681 res_ptr = (gint32 *)tmp;
682 res_ptr[0] = sequence;
683 memcpy(tmp+4, buf, buf_len);
685 HMAC_MD5(signing_key, 16, tmp, 4 + buf_len, hmac);
687 if (IS_FLAG(flags, NTLMSSP_NEGOTIATE_KEY_EXCH)) {
688 RC4K(signing_key, key_len, hmac, 8, result+4);
689 } else {
690 memcpy(result+4, hmac, 8);
692 } else {
693 ///gint32 plaintext [] = {0, CRC32(buf), sequence}; // 4, 4, 4 bytes
694 gint32 plaintext [] = {random_pad, CRC32(buf, strlen(buf)), sequence}; // 4, 4, 4 bytes
696 RC4K(signing_key, key_len, (const guchar *)plaintext, 12, result+4);
698 res_ptr = (gint32 *)result;
699 // Highest four bytes are the Version
700 res_ptr[0] = 0x00000001; // 4 bytes
702 // Replace the first four bytes of the ciphertext with a counter value
703 // currently set to this hardcoded value
704 ///res_ptr[1] = random_pad; // 4 bytes
707 for (i = 0, j = 0; i < 16; i++, j+=2) {
708 g_sprintf(&signature[j], "%02X", result[i]);
711 return g_strdup(signature);
714 static gchar *
715 purple_ntlm_sipe_signature_make (guint32 flags, const char * msg, unsigned char * signing_key)
717 return MAC(flags, msg, signing_key, 0, 100, 16);
720 static gboolean
721 purple_ntlm_verify_signature (char * a, char * b)
723 // Make sure the last 16 bytes match
724 gboolean ret = g_ascii_strncasecmp(a + 16, b + 16, 16) == 0;
725 return ret;
728 static void
729 purple_ntlm_gen_authenticate(guchar **ntlm_key, /* signing key */
730 const gchar *user,
731 const gchar *password,
732 const gchar *hostname,
733 const gchar *domain,
734 const guint8 *nonce, /* server challenge */
735 gboolean is_connection_based,
736 SipSecBuffer *out_buff,
737 guint32 *flags)
739 guint32 neg_flags = is_connection_based ? NEGOTIATE_FLAGS_CONN : NEGOTIATE_FLAGS;
740 gboolean is_key_exch = IS_FLAG(neg_flags, NTLMSSP_NEGOTIATE_KEY_EXCH);
741 int msglen = sizeof(struct authenticate_message) + 2*(strlen(domain)
742 + strlen(user)+ strlen(hostname) + NTLMSSP_NT_OR_LM_KEY_LEN)
743 + (is_key_exch ? NTLMSSP_SESSION_KEY_LEN : 0);
744 struct authenticate_message *tmsg = g_malloc0(msglen);
745 char *tmp;
746 int remlen;
747 unsigned char response_key_lm [16];
748 unsigned char lm_challenge_response [NTLMSSP_NT_OR_LM_KEY_LEN];
749 unsigned char response_key_nt [16];
750 unsigned char nt_challenge_response [NTLMSSP_NT_OR_LM_KEY_LEN];
751 unsigned char session_base_key [16];
752 unsigned char key_exchange_key [16];
753 unsigned char exported_session_key[16];
754 unsigned char encrypted_random_session_key [16];
755 unsigned char client_signing_key [16];
757 NTOWFv1 (password, user, domain, response_key_nt);
758 LMOWFv1 (password, user, domain, response_key_lm);
760 if (IS_FLAG(neg_flags, NTLMSSP_NEGOTIATE_LM_KEY)) {
761 // @TODO do not even reference nt_challenge_response
762 Z (nt_challenge_response, NTLMSSP_NT_OR_LM_KEY_LEN);
763 DESL (response_key_lm, nonce, lm_challenge_response);
764 } else if (IS_FLAG(neg_flags, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY)) {
765 unsigned char client_challenge [8];
766 unsigned char prehash [16];
767 unsigned char hash [16];
769 NONCE (client_challenge, 8);
771 /* nt_challenge_response */
772 memcpy(prehash, nonce, 8);
773 memcpy(prehash + 8, client_challenge, 8);
774 MD5 (prehash, 16, hash);
775 DESL (response_key_nt, hash, nt_challenge_response);
777 /* lm_challenge_response */
778 memcpy(lm_challenge_response, client_challenge, 8);
779 Z (lm_challenge_response+8, 16);
780 } else {
781 DESL (response_key_nt, nonce, nt_challenge_response);
782 if (IS_FLAG(neg_flags, NTLMSSP_NEGOTIATE_NT_ONLY)) {
783 memcpy(lm_challenge_response, nt_challenge_response, NTLMSSP_NT_OR_LM_KEY_LEN);
784 } else {
785 DESL (response_key_lm, nonce, lm_challenge_response);
789 /* authenticate message initialization */
790 memcpy(tmsg->protocol, "NTLMSSP\0", 8);
791 tmsg->type = 3;
793 /* Set Negotiate Flags */
794 tmsg->flags = neg_flags;
796 /* Domain */
797 tmsg->domain.offset = sizeof(struct authenticate_message);
798 tmp = ((char*) tmsg) + sizeof(struct authenticate_message);
799 remlen = ((char *)tmsg)+msglen-tmp;
800 tmsg->domain.len = tmsg->domain.maxlen = unicode_strconvcopy(tmp, domain, remlen);
801 tmp += tmsg->domain.len;
802 remlen = ((char *)tmsg)+msglen-tmp;
804 /* User */
805 tmsg->user.offset = tmsg->domain.offset + tmsg->domain.len;
806 tmsg->user.len = tmsg->user.maxlen = unicode_strconvcopy(tmp, user, remlen);
807 tmp += tmsg->user.len;
808 remlen = ((char *)tmsg)+msglen-tmp;
810 /* Host */
811 tmsg->host.offset = tmsg->user.offset + tmsg->user.len;
812 tmsg->host.len = tmsg->host.maxlen = unicode_strconvcopy(tmp, hostname, remlen);
813 tmp += tmsg->host.len;
815 /* LM */
816 tmsg->lm_resp.len = tmsg->lm_resp.maxlen = NTLMSSP_NT_OR_LM_KEY_LEN;
817 tmsg->lm_resp.offset = tmsg->host.offset + tmsg->host.len;
818 memcpy(tmp, lm_challenge_response, NTLMSSP_NT_OR_LM_KEY_LEN);
819 tmp += NTLMSSP_NT_OR_LM_KEY_LEN;
821 /* NT */
822 tmsg->nt_resp.len = tmsg->nt_resp.maxlen = NTLMSSP_NT_OR_LM_KEY_LEN;
823 tmsg->nt_resp.offset = tmsg->lm_resp.offset + tmsg->lm_resp.len;
824 memcpy(tmp, nt_challenge_response, NTLMSSP_NT_OR_LM_KEY_LEN);
825 tmp += NTLMSSP_NT_OR_LM_KEY_LEN;
827 /* Session Key */
828 MD4(response_key_nt, 16, session_base_key);
829 KXKEY(neg_flags, session_base_key, lm_challenge_response, nonce, key_exchange_key);
831 if (is_key_exch)
833 tmsg->session_key.len = tmsg->session_key.maxlen = NTLMSSP_SESSION_KEY_LEN;
834 tmsg->session_key.offset = tmsg->nt_resp.offset + tmsg->nt_resp.len;
836 NONCE (exported_session_key, 16);
837 RC4K (key_exchange_key, 16, exported_session_key, 16, encrypted_random_session_key);
839 memcpy(tmp, encrypted_random_session_key, 16);
840 tmp += NTLMSSP_SESSION_KEY_LEN;
842 else
844 tmsg->session_key.len = tmsg->session_key.maxlen = 0;
845 tmsg->session_key.offset = tmsg->nt_resp.offset + tmsg->nt_resp.len;
847 memcpy(exported_session_key, key_exchange_key, 16);
849 // p.46
850 //Set ClientSigningKey to SIGNKEY(ExportedSessionKey, "Client")
851 //Set ServerSigningKey to SIGNKEY(ExportedSessionKey, "Server")
852 if (IS_FLAG(neg_flags, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY)) {
853 SIGNKEY(exported_session_key, FALSE, client_signing_key); //Server
854 } else {
855 memcpy(client_signing_key, exported_session_key, 16);
857 *ntlm_key = (guchar *)g_strndup((gchar *)client_signing_key, 16);
859 *flags = neg_flags;
861 tmp = purple_base64_encode(exported_session_key, 16);
862 purple_debug_info("sipe", "Generated NTLM AUTHENTICATE session key: %s\n", tmp);
863 g_free(tmp);
865 out_buff->value = tmsg;
866 out_buff->length = msglen;
870 * Generates Type 1 (Negotiate) message for connection-oriented cases (only)
872 static void
873 purple_ntlm_gen_negotiate(SipSecBuffer *out_buff)
875 int msglen = sizeof(struct negotiate_message);
876 struct negotiate_message *tmsg = g_malloc0(msglen);
878 /* negotiate message initialization */
879 memcpy(tmsg->protocol, "NTLMSSP\0", 8);
880 tmsg->type = 1;
882 /* Set Negotiate Flags */
883 tmsg->flags = NEGOTIATE_FLAGS_CONN;
885 /* Domain */
886 tmsg->domain.offset = sizeof(struct negotiate_message);
887 tmsg->domain.len = tmsg->domain.maxlen = 0;
889 /* Host */
890 tmsg->host.offset = tmsg->domain.offset + tmsg->domain.len;
891 tmsg->host.len = tmsg->host.maxlen = 0;
893 /* Version */
894 //tmsg->ver.product_major_version = 5; /* 5.1.2600 (Windows XP SP2) */
895 //tmsg->ver.product_minor_version = 1;
896 //tmsg->ver.product_build = 2600;
897 //tmsg->ver.ntlm_revision_current = 0x0F; /* NTLMSSP_REVISION_W2K3 */
899 out_buff->value = tmsg;
900 out_buff->length = msglen;
903 /***********************************************
905 * End of merged code from original sip-ntlm.c
907 ***********************************************/
909 #define APPEND_NEG_FLAG(str, flags, flag, desc) \
910 if ((flags & flag) == flag) g_string_append_printf(str, "\t%s\n", desc);
912 static gchar *
913 sip_sec_ntlm_negotiate_flags_describe(guint32 flags)
915 GString* str = g_string_new(NULL);
917 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_UNICODE, "NTLMSSP_NEGOTIATE_UNICODE");
918 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_OEM, "NTLMSSP_NEGOTIATE_OEM");
919 APPEND_NEG_FLAG(str, flags, NTLMSSP_REQUEST_TARGET, "NTLMSSP_REQUEST_TARGET");
920 APPEND_NEG_FLAG(str, flags, r9, "r9");
921 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_SIGN, "NTLMSSP_NEGOTIATE_SIGN");
922 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_SEAL, "NTLMSSP_NEGOTIATE_SEAL");
923 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_DATAGRAM, "NTLMSSP_NEGOTIATE_DATAGRAM");
924 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_LM_KEY, "NTLMSSP_NEGOTIATE_LM_KEY");
925 APPEND_NEG_FLAG(str, flags, r8, "r8");
926 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_NTLM, "NTLMSSP_NEGOTIATE_NTLM");
927 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_NT_ONLY, "NTLMSSP_NEGOTIATE_NT_ONLY");
928 APPEND_NEG_FLAG(str, flags, anonymous, "anonymous");
929 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED, "NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED");
930 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED, "NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED");
931 APPEND_NEG_FLAG(str, flags, r7, "r7");
932 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_ALWAYS_SIGN, "NTLMSSP_NEGOTIATE_ALWAYS_SIGN");
933 APPEND_NEG_FLAG(str, flags, NTLMSSP_TARGET_TYPE_DOMAIN, "NTLMSSP_TARGET_TYPE_DOMAIN");
934 APPEND_NEG_FLAG(str, flags, NTLMSSP_TARGET_TYPE_SERVER, "NTLMSSP_TARGET_TYPE_SERVER");
935 APPEND_NEG_FLAG(str, flags, r6, "r6");
936 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY, "NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY");
937 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_IDENTIFY, "NTLMSSP_NEGOTIATE_IDENTIFY");
938 APPEND_NEG_FLAG(str, flags, r5, "r5");
939 APPEND_NEG_FLAG(str, flags, NTLMSSP_REQUEST_NON_NT_SESSION_KEY, "NTLMSSP_REQUEST_NON_NT_SESSION_KEY");
940 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_TARGET_INFO, "NTLMSSP_NEGOTIATE_TARGET_INFO");
941 APPEND_NEG_FLAG(str, flags, r4, "r4");
942 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_VERSION, "NTLMSSP_NEGOTIATE_VERSION");
943 APPEND_NEG_FLAG(str, flags, r3, "r3");
944 APPEND_NEG_FLAG(str, flags, r2, "r2");
945 APPEND_NEG_FLAG(str, flags, r1, "r1");
946 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_128, "NTLMSSP_NEGOTIATE_128");
947 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_KEY_EXCH, "NTLMSSP_NEGOTIATE_KEY_EXCH");
948 APPEND_NEG_FLAG(str, flags, NTLMSSP_NEGOTIATE_56, "NTLMSSP_NEGOTIATE_56");
950 return g_string_free(str, FALSE);
953 #define AV_DESC(av, av_value, av_len, av_name) \
954 gchar *tmp = unicode_strconvcopy_back(av_value, av_len); \
955 g_string_append_printf(str, "\t%s: %s\n", av_name, tmp); \
956 g_free(tmp);
958 static gchar *
959 sip_sec_ntlm_describe_version(struct version *ver) {
960 GString* str = g_string_new(NULL);
961 gchar *ver_desc = "";
962 gchar *ntlm_revision_desc = "";
964 if (ver->product_major_version == 6) {
965 ver_desc = "Windows Vista, Windows Server 2008, Windows 7 or Windows Server 2008 R2";
966 } else if (ver->product_major_version == 5 && ver->product_minor_version == 2) {
967 ver_desc = "Windows Server 2003";
968 } else if (ver->product_major_version == 5 && ver->product_minor_version == 1) {
969 ver_desc = "Windows XP SP2";
972 if (ver->ntlm_revision_current == 0x0F) {
973 ntlm_revision_desc = "NTLMSSP_REVISION_W2K3";
974 } else if (ver->ntlm_revision_current == 0x0A) {
975 ntlm_revision_desc = "NTLMSSP_REVISION_W2K3_RC1";
978 g_string_append_printf(str, "\tproduct: %d.%d.%d (%s)\n",
979 ver->product_major_version, ver->product_minor_version, ver->product_build, ver_desc);
980 g_string_append_printf(str, "\tntlm_revision_current: 0x%02X (%s)\n", ver->ntlm_revision_current, ntlm_revision_desc);
982 return g_string_free(str, FALSE);
985 static gchar *
986 sip_sec_ntlm_describe_smb_header(struct smb_header *header,
987 const char* name)
989 GString* str = g_string_new(NULL);
991 g_string_append_printf(str, "\t%s.len : %d\n", name, header->len);
992 g_string_append_printf(str, "\t%s.maxlen: %d\n", name, header->maxlen);
993 g_string_append_printf(str, "\t%s.offset: %d\n", name, header->offset);
995 return g_string_free(str, FALSE);
998 static gchar *
999 sip_sec_ntlm_negotiate_message_describe(struct negotiate_message *cmsg)
1001 GString* str = g_string_new(NULL);
1002 char *tmp;
1004 g_string_append(str, (tmp = sip_sec_ntlm_negotiate_flags_describe(cmsg->flags)));
1005 g_free(tmp);
1007 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->domain), "domain")));
1008 g_free(tmp);
1010 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->host), "host")));
1011 g_free(tmp);
1013 tmp = sip_sec_ntlm_describe_version(&(cmsg->ver));
1014 g_string_append(str, tmp);
1015 g_free(tmp);
1017 if (cmsg->domain.len && cmsg->domain.offset) {
1018 gchar *domain = g_strndup(((gchar *)cmsg + cmsg->domain.offset), cmsg->domain.len);
1019 g_string_append_printf(str, "\tdomain: %s\n", domain);
1020 g_free(domain);
1023 if (cmsg->host.len && cmsg->host.offset) {
1024 gchar *host = g_strndup(((gchar *)cmsg + cmsg->host.offset), cmsg->host.len);
1025 g_string_append_printf(str, "\thost: %s\n", host);
1026 g_free(host);
1029 return g_string_free(str, FALSE);
1032 static gchar *
1033 sip_sec_ntlm_authenticate_message_describe(struct authenticate_message *cmsg)
1035 GString* str = g_string_new(NULL);
1036 char *tmp;
1037 SipSecBuffer buff;
1039 g_string_append(str, (tmp = sip_sec_ntlm_negotiate_flags_describe(cmsg->flags)));
1040 g_free(tmp);
1042 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->lm_resp), "lm_resp")));
1043 g_free(tmp);
1045 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->nt_resp), "nt_resp")));
1046 g_free(tmp);
1048 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->domain), "domain")));
1049 g_free(tmp);
1051 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->user), "user")));
1052 g_free(tmp);
1054 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->host), "host")));
1055 g_free(tmp);
1057 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->session_key), "session_key")));
1058 g_free(tmp);
1060 tmp = sip_sec_ntlm_describe_version(&(cmsg->ver));
1061 g_string_append(str, tmp);
1062 g_free(tmp);
1064 /* mic */
1065 buff.length = 16;
1066 buff.value = cmsg->mic;
1067 g_string_append_printf(str, "\t%s: %s\n", "mic", (tmp = bytes_to_hex_str(&buff)));
1068 g_free(tmp);
1070 if (cmsg->lm_resp.len && cmsg->lm_resp.offset) {
1071 buff.length = cmsg->lm_resp.len;
1072 buff.value = (gchar *)cmsg + cmsg->lm_resp.offset;
1073 g_string_append_printf(str, "\t%s: %s\n", "lm_resp", (tmp = bytes_to_hex_str(&buff)));
1074 g_free(tmp);
1077 if (cmsg->nt_resp.len && cmsg->nt_resp.offset) {
1078 buff.length = cmsg->nt_resp.len;
1079 buff.value = (gchar *)cmsg + cmsg->nt_resp.offset;
1080 g_string_append_printf(str, "\t%s: %s\n", "nt_resp", (tmp = bytes_to_hex_str(&buff)));
1081 g_free(tmp);
1084 if (cmsg->domain.len && cmsg->domain.offset) {
1085 gchar *domain = unicode_strconvcopy_back(((gchar *)cmsg + cmsg->domain.offset), cmsg->domain.len);
1086 g_string_append_printf(str, "\t%s: %s\n", "domain", domain);
1087 g_free(domain);
1090 if (cmsg->user.len && cmsg->user.offset) {
1091 gchar *user = unicode_strconvcopy_back(((gchar *)cmsg + cmsg->user.offset), cmsg->user.len);
1092 g_string_append_printf(str, "\t%s: %s\n", "user", user);
1093 g_free(user);
1096 if (cmsg->host.len && cmsg->host.offset) {
1097 gchar *host = unicode_strconvcopy_back(((gchar *)cmsg + cmsg->host.offset), cmsg->host.len);
1098 g_string_append_printf(str, "\t%s: %s\n", "host", host);
1099 g_free(host);
1102 if (cmsg->session_key.len && cmsg->session_key.offset) {
1103 buff.length = cmsg->session_key.len;
1104 buff.value = (gchar *)cmsg + cmsg->session_key.offset;
1105 g_string_append_printf(str, "\t%s: %s\n", "session_key", (tmp = bytes_to_hex_str(&buff)));
1106 g_free(tmp);
1109 return g_string_free(str, FALSE);
1112 static gchar *
1113 sip_sec_ntlm_challenge_message_describe(struct challenge_message *cmsg)
1115 GString* str = g_string_new(NULL);
1116 char *tmp;
1118 g_string_append(str, (tmp = sip_sec_ntlm_negotiate_flags_describe(cmsg->flags)));
1119 g_free(tmp);
1121 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->target_name), "target_name")));
1122 g_free(tmp);
1124 g_string_append(str, (tmp = sip_sec_ntlm_describe_smb_header(&(cmsg->target_info), "target_info")));
1125 g_free(tmp);
1127 g_string_append(str, (tmp = sip_sec_ntlm_describe_version(&(cmsg->ver))));
1128 g_free(tmp);
1130 if (cmsg->target_name.len && cmsg->target_name.offset) {
1131 gchar *target_name = unicode_strconvcopy_back(((gchar *)cmsg + cmsg->target_name.offset), cmsg->target_name.len);
1132 g_string_append_printf(str, "\ttarget_name: %s\n", target_name);
1133 g_free(target_name);
1136 if (cmsg->target_info.len && cmsg->target_info.offset) {
1137 void *target_info = ((gchar *)cmsg + cmsg->target_info.offset);
1138 struct av_pair *av = (struct av_pair*)target_info;
1140 while (av->av_id != MsvAvEOL) {
1141 gchar *av_value = ((gchar *)av) + 4;
1143 switch (av->av_id) {
1144 case MsvAvEOL:
1145 g_string_append_printf(str, "\t%s\n", "MsvAvEOL");
1146 break;
1147 case MsvAvNbComputerName:
1148 { AV_DESC(av, av_value, av->av_len, "MsvAvNbComputerName") break; }
1149 case MsvAvNbDomainName:
1150 { AV_DESC(av, av_value, av->av_len, "MsvAvNbDomainName") break; }
1151 case MsvAvDnsComputerName:
1152 { AV_DESC(av, av_value, av->av_len, "MsvAvDnsComputerName") break; }
1153 case MsvAvDnsDomainName:
1154 { AV_DESC(av, av_value, av->av_len, "MsvAvDnsDomainName") break; }
1155 case MsvAvDnsTreeName:
1156 { AV_DESC(av, av_value, av->av_len, "MsvAvDnsTreeName") break; }
1157 case MsvAvFlags:
1158 g_string_append_printf(str, "\t%s: %d\n", "MsvAvFlags", *((guint32*)av_value));
1159 break;
1160 case MsvAvTimestamp:
1161 g_string_append_printf(str, "\t%s\n", "MsvAvTimestamp");
1162 break;
1163 case MsAvRestrictions:
1164 g_string_append_printf(str, "\t%s\n", "MsAvRestrictions");
1165 break;
1166 case MsvAvTargetName:
1167 { AV_DESC(av, av_value, av->av_len, "MsvAvTargetName") break; }
1168 case MsvChannelBindings:
1169 g_string_append_printf(str, "\t%s\n", "MsvChannelBindings");
1170 break;
1173 av = (struct av_pair*)(((guint8*)av) + 4 + av->av_len);
1177 return g_string_free(str, FALSE);
1180 gchar *
1181 sip_sec_ntlm_message_describe(SipSecBuffer buff)
1183 struct ntlm_message *msg;
1185 if (buff.length == 0 || buff.value == NULL || buff.length < 12) return NULL;
1187 msg = buff.value;
1188 if(!sipe_strequal("NTLMSSP", (char*)msg)) return NULL;
1190 if (msg->type == 1) return sip_sec_ntlm_negotiate_message_describe((struct negotiate_message *)msg);
1191 if (msg->type == 2) return sip_sec_ntlm_challenge_message_describe((struct challenge_message *)msg);
1192 if (msg->type == 3) return sip_sec_ntlm_authenticate_message_describe((struct authenticate_message *)msg);
1194 return NULL;
1197 /* sip-sec-mech.h API implementation for NTLM */
1199 /* Security context for NTLM */
1200 typedef struct _context_ntlm {
1201 struct sip_sec_context common;
1202 char* domain;
1203 char *username;
1204 char *password;
1205 int step;
1206 /* signing key */
1207 guchar *key;
1208 guint32 flags;
1209 } *context_ntlm;
1212 static sip_uint32
1213 sip_sec_acquire_cred__ntlm(SipSecContext context,
1214 const char *domain,
1215 const char *username,
1216 const char *password)
1218 context_ntlm ctx = (context_ntlm)context;
1220 /* NTLM requires a domain, username & password */
1221 if (!domain || !username || !password)
1222 return SIP_SEC_E_INTERNAL_ERROR;
1224 ctx->domain = g_strdup(domain);
1225 ctx->username = g_strdup(username);
1226 ctx->password = g_strdup(password);
1228 return SIP_SEC_E_OK;
1231 static sip_uint32
1232 sip_sec_init_sec_context__ntlm(SipSecContext context,
1233 SipSecBuffer in_buff,
1234 SipSecBuffer *out_buff,
1235 SIPE_UNUSED_PARAMETER const char *service_name)
1237 context_ntlm ctx = (context_ntlm) context;
1239 purple_debug_info("sipe", "sip_sec_init_sec_context__ntlm: in use\n");
1241 ctx->step++;
1242 if (ctx->step == 1) {
1243 if (!context->is_connection_based) {
1244 out_buff->length = 0;
1245 out_buff->value = NULL;
1246 } else {
1247 purple_ntlm_gen_negotiate(out_buff);
1249 return SIP_SEC_I_CONTINUE_NEEDED;
1251 } else {
1252 guchar *ntlm_key;
1253 guchar *nonce;
1254 guint32 flags;
1255 gchar *tmp;
1257 if (!in_buff.value || !in_buff.length) {
1258 return SIP_SEC_E_INTERNAL_ERROR;
1261 nonce = g_memdup(purple_ntlm_parse_challenge(in_buff, context->is_connection_based, &flags), 8);
1263 purple_ntlm_gen_authenticate(&ntlm_key,
1264 ctx->username,
1265 ctx->password,
1266 (tmp = g_ascii_strup(sipe_get_host_name(), -1)),
1267 ctx->domain,
1268 nonce,
1269 context->is_connection_based,
1270 out_buff,
1271 &flags);
1272 g_free(nonce);
1273 g_free(tmp);
1275 g_free(ctx->key);
1276 ctx->key = ntlm_key;
1277 ctx->flags = flags;
1278 return SIP_SEC_E_OK;
1283 * @param message a NULL terminated string to sign
1286 static sip_uint32
1287 sip_sec_make_signature__ntlm(SipSecContext context,
1288 const char *message,
1289 SipSecBuffer *signature)
1291 gchar *signature_hex = purple_ntlm_sipe_signature_make(((context_ntlm) context)->flags,
1292 message,
1293 ((context_ntlm) context)->key);
1295 hex_str_to_bytes(signature_hex, signature);
1296 g_free(signature_hex);
1298 return SIP_SEC_E_OK;
1302 * @param message a NULL terminated string to check signature of
1303 * @return SIP_SEC_E_OK on success
1305 static sip_uint32
1306 sip_sec_verify_signature__ntlm(SipSecContext context,
1307 const char *message,
1308 SipSecBuffer signature)
1310 char *signature_hex = bytes_to_hex_str(&signature);
1311 gchar *signature_calc = purple_ntlm_sipe_signature_make(((context_ntlm) context)->flags,
1312 message,
1313 ((context_ntlm) context)->key);
1314 sip_uint32 res;
1316 if (purple_ntlm_verify_signature(signature_calc, signature_hex)) {
1317 res = SIP_SEC_E_OK;
1318 } else {
1319 res = SIP_SEC_E_INTERNAL_ERROR;
1321 g_free(signature_calc);
1322 g_free(signature_hex);
1323 return(res);
1326 static void
1327 sip_sec_destroy_sec_context__ntlm(SipSecContext context)
1329 context_ntlm ctx = (context_ntlm) context;
1331 g_free(ctx->domain);
1332 g_free(ctx->username);
1333 g_free(ctx->password);
1334 g_free(ctx->key);
1335 g_free(ctx);
1338 SipSecContext
1339 sip_sec_create_context__ntlm(SIPE_UNUSED_PARAMETER SipSecAuthType type)
1341 context_ntlm context = g_malloc0(sizeof(struct _context_ntlm));
1342 if (!context) return(NULL);
1344 context->common.acquire_cred_func = sip_sec_acquire_cred__ntlm;
1345 context->common.init_context_func = sip_sec_init_sec_context__ntlm;
1346 context->common.destroy_context_func = sip_sec_destroy_sec_context__ntlm;
1347 context->common.make_signature_func = sip_sec_make_signature__ntlm;
1348 context->common.verify_signature_func = sip_sec_verify_signature__ntlm;
1350 return((SipSecContext) context);
1355 Local Variables:
1356 mode: c
1357 c-file-style: "bsd"
1358 indent-tabs-mode: t
1359 tab-width: 8
1360 End: