annotate src/ntlm.c @ 11819:1452a3a9f07a

[gaim-migrate @ 14110] SF Patch #1249461 from David Fiander "When a buddy toggles Allow/Disallow mobile pages the server sends an NLN command to the client with an updated client id number (see http://www.hypothetic.org/docs/msn/notification/presence.php). This patch adds support for properly tracking the mobile paging status of buddies." committer: Tailor Script <tailor@pidgin.im>
author Richard Laager <rlaager@wiktel.com>
date Tue, 25 Oct 2005 19:18:44 +0000
parents 03cd74ca2562
children 4669e7461968
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
1 /**
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
2 * @file ntlm.c
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
3 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
4 * gaim
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
5 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
6 * Copyright (C) 2005 Thomas Butter <butter@uni-mannheim.de>
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
7 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
8 * hashing done according to description of NTLM on
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
9 * http://www.innovation.ch/java/ntlm.html
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
10 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
11 * This program is free software; you can redistribute it and/or modify
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
12 * it under the terms of the GNU General Public License as published by
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
13 * the Free Software Foundation; either version 2 of the License, or
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
14 * (at your option) any later version.
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
15 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
16 * This program is distributed in the hope that it will be useful,
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
19 * GNU General Public License for more details.
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
20 *
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
21 * You should have received a copy of the GNU General Public License
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
22 * along with this program; if not, write to the Free Software
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
24 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
25
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
26 #include <glib.h>
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
27 #include <stdlib.h>
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
28 #include "util.h"
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
29 #include "ntlm.h"
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
30 #include "cipher.h"
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
31 #include <string.h>
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
32
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
33 struct type1_message {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
34 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0' */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
35 guint8 type; /* 0x01 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
36 guint8 zero1[3];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
37 short flags; /* 0xb203 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
38 guint8 zero2[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
39
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
40 short dom_len1; /* domain string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
41 short dom_len2; /* domain string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
42 short dom_off; /* domain string offset */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
43 guint8 zero3[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
44
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
45 short host_len1; /* host string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
46 short host_len2; /* host string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
47 short host_off; /* host string offset (always 0x20) */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
48 guint8 zero4[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
49
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
50 /* guint8 host[*]; // host string (ASCII)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
51 guint8 dom[*]; // domain string (ASCII) */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
52 };
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
53
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
54 struct type2_message {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
55 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0'*/
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
56 guint8 type; /* 0x02 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
57 guint8 zero1[7];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
58 short msg_len; /* 0x28 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
59 guint8 zero2[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
60 short flags; /* 0x8201 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
61 guint8 zero3[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
62
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
63 guint8 nonce[8]; /* nonce */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
64 guint8 zero[8];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
65 };
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
66
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
67 struct type3_message {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
68 guint8 protocol[8]; /* 'N', 'T', 'L', 'M', 'S', 'S', 'P', '\0'*/
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
69 guint8 type; /* 0x03 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
70 guint8 zero1[3];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
71
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
72 short lm_resp_len1; /* LanManager response length (always 0x18)*/
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
73 short lm_resp_len2; /* LanManager response length (always 0x18)*/
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
74 short lm_resp_off; /* LanManager response offset */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
75 guint8 zero2[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
76
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
77 short nt_resp_len1; /* NT response length (always 0x18) */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
78 short nt_resp_len2; /* NT response length (always 0x18) */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
79 short nt_resp_off; /* NT response offset */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
80 guint8 zero3[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
81
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
82 short dom_len1; /* domain string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
83 short dom_len2; /* domain string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
84 short dom_off; /* domain string offset (always 0x40) */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
85 guint8 zero4[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
86
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
87 short user_len1; /* username string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
88 short user_len2; /* username string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
89 short user_off; /* username string offset */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
90 guint8 zero5[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
91
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
92 short host_len1; /* host string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
93 short host_len2; /* host string length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
94 short host_off; /* host string offset */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
95 guint8 zero6[6];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
96
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
97 short msg_len; /* message length */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
98 guint8 zero7[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
99
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
100 short flags; /* 0x8201 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
101 guint8 zero8[2];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
102
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
103 /* guint8 dom[*]; // domain string (unicode UTF-16LE)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
104 guint8 user[*]; // username string (unicode UTF-16LE)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
105 guint8 host[*]; // host string (unicode UTF-16LE)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
106 guint8 lm_resp[*]; // LanManager response
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
107 guint8 nt_resp[*]; // NT response*/
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
108 };
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
109
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
110 gchar *gaim_ntlm_gen_type1(gchar *hostname, gchar *domain) {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
111 char *msg = g_malloc0(sizeof(struct type1_message) + strlen(hostname) + strlen(domain));
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
112 struct type1_message *tmsg = (struct type1_message*)msg;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
113 tmsg->protocol[0] = 'N';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
114 tmsg->protocol[1] = 'T';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
115 tmsg->protocol[2] = 'L';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
116 tmsg->protocol[3] = 'M';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
117 tmsg->protocol[4] = 'S';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
118 tmsg->protocol[5] = 'S';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
119 tmsg->protocol[6] = 'P';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
120 tmsg->protocol[7] = '\0';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
121 tmsg->type= 0x01;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
122 tmsg->flags = 0xb202;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
123 tmsg->dom_len1 = tmsg->dom_len2 = strlen(domain);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
124 tmsg->dom_off = 32+strlen(hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
125 tmsg->host_len1 = tmsg->host_len2 = strlen(hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
126 tmsg->host_off= 32;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
127 memcpy(msg+sizeof(struct type1_message),hostname,strlen(hostname));
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
128 memcpy(msg+sizeof(struct type1_message)+strlen(hostname),domain,strlen(domain));
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
129
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
130 return gaim_base64_encode((guchar*)msg, sizeof(struct type1_message) + strlen(hostname) + strlen(domain));
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
131 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
132
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
133 gchar *gaim_ntlm_parse_type2(gchar *type2) {
11586
03cd74ca2562 [gaim-migrate @ 13856]
Nathan Walp <nwalp@pidgin.im>
parents: 11427
diff changeset
134 gsize retlen;
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
135 static gchar nonce[8];
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
136 struct type2_message *tmsg = (struct type2_message*)gaim_base64_decode((char*)type2, &retlen);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
137 memcpy(nonce, tmsg->nonce, 8);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
138 g_free(tmsg);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
139 return nonce;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
140 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
141
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
142 static void setup_des_key(unsigned char key_56[], char *key)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
143 {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
144 key[0] = key_56[0];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
145 key[1] = ((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
146 key[2] = ((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
147 key[3] = ((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
148 key[4] = ((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
149 key[5] = ((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
150 key[6] = ((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
151 key[7] = (key_56[6] << 1) & 0xFF;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
152 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
153
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
154 /*
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
155 * helper function for gaim cipher.c
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
156 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
157 static void des_ecb_encrypt(char *plaintext, char *result, char *key) {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
158 GaimCipher *cipher;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
159 GaimCipherContext *context;
11586
03cd74ca2562 [gaim-migrate @ 13856]
Nathan Walp <nwalp@pidgin.im>
parents: 11427
diff changeset
160 gsize outlen;
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
161
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
162 cipher = gaim_ciphers_find_cipher("des");
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
163 context = gaim_cipher_context_new(cipher, NULL);
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
164 gaim_cipher_context_set_key(context, (guchar*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
165 gaim_cipher_context_encrypt(context, (guchar*)plaintext, 8, (guchar*)result, &outlen);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
166 gaim_cipher_context_destroy(context);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
167 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
168
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
169 /*
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
170 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
171 * 8 byte plaintext is encrypted with each key and the resulting 24
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
172 * bytes are stored in the results array.
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
173 */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
174 static void calc_resp(unsigned char *keys, unsigned char *plaintext, unsigned char *results)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
175 {
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
176 guchar key[8];
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
177 setup_des_key(keys, (char*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
178 des_ecb_encrypt((char*)plaintext, (char*)results, (char*)key);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
179
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
180 setup_des_key(keys+7, (char*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
181 des_ecb_encrypt((char*)plaintext, (char*)(results+8), (char*)key);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
182
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
183 setup_des_key(keys+14, (char*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
184 des_ecb_encrypt((char*)plaintext, (char*)(results+16), (char*)key);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
185 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
186
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
187 gchar *gaim_ntlm_gen_type3(gchar *username, gchar *passw, gchar *hostname, gchar *domain, gchar *nonce) {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
188 char lm_pw[14];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
189 unsigned char lm_hpw[21];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
190 gchar key[8];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
191 struct type3_message *tmsg = g_malloc0(sizeof(struct type3_message)+
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
192 strlen(domain) + strlen(username) + strlen(hostname) + 24 +24);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
193 int len = strlen(passw);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
194 unsigned char lm_resp[24], nt_resp[24];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
195 unsigned char magic[] = { 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 };
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
196 unsigned char nt_hpw[21];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
197 int lennt;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
198 char nt_pw[128];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
199 GaimCipher *cipher;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
200 GaimCipherContext *context;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
201 char *tmp = 0;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
202 int idx = 0;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
203
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
204 /* type3 message initialization */
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
205 tmsg->protocol[0] = 'N';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
206 tmsg->protocol[1] = 'T';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
207 tmsg->protocol[2] = 'L';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
208 tmsg->protocol[3] = 'M';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
209 tmsg->protocol[4] = 'S';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
210 tmsg->protocol[5] = 'S';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
211 tmsg->protocol[6] = 'P';
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
212 tmsg->type = 0x03;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
213 tmsg->lm_resp_len1 = tmsg->lm_resp_len2 = 0x18;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
214 tmsg->lm_resp_off = sizeof(struct type3_message) + strlen(domain) + strlen(username) + strlen(hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
215 tmsg->nt_resp_len1 = tmsg->nt_resp_len2 = 0x18;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
216 tmsg->nt_resp_off = sizeof(struct type3_message) + strlen(domain) + strlen(username) + strlen(hostname) + 0x18;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
217
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
218 tmsg->dom_len1 = tmsg->dom_len2 = strlen(domain);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
219 tmsg->dom_off = 0x40;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
220
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
221 tmsg->user_len1 = tmsg->user_len2 = strlen(username);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
222 tmsg->user_off = sizeof(struct type3_message) + strlen(domain);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
223
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
224 tmsg->host_len1 = tmsg->host_len2 = strlen(hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
225 tmsg->host_off = sizeof(struct type3_message) + strlen(domain) + strlen(username);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
226
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
227 tmsg->msg_len = sizeof(struct type3_message) + strlen(domain) + strlen(username) + strlen(hostname) + 0x18 + 0x18;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
228 tmsg->flags = 0x8200;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
229
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
230 tmp = ((char*) tmsg) + sizeof(struct type3_message);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
231 strcpy(tmp, domain);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
232 tmp += strlen(domain);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
233 strcpy(tmp, username);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
234 tmp += strlen(username);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
235 strcpy(tmp, hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
236 tmp += strlen(hostname);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
237
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
238 if (len > 14) len = 14;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
239
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
240 for (idx=0; idx<len; idx++)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
241 lm_pw[idx] = g_ascii_toupper(passw[idx]);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
242 for (; idx<14; idx++)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
243 lm_pw[idx] = 0;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
244
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
245 setup_des_key((unsigned char*)lm_pw, (char*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
246 des_ecb_encrypt((char*)magic, (char*)lm_hpw, (char*)key);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
247
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
248 setup_des_key((unsigned char*)(lm_pw+7), (char*)key);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
249 des_ecb_encrypt((char*)magic, (char*)lm_hpw+8, (char*)key);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
250
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
251 memset(lm_hpw+16, 0, 5);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
252
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
253
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
254 lennt = strlen(passw);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
255 for (idx=0; idx<lennt; idx++)
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
256 {
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
257 nt_pw[2*idx] = passw[idx];
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
258 nt_pw[2*idx+1] = 0;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
259 }
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
260
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
261 cipher = gaim_ciphers_find_cipher("md4");
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
262 context = gaim_cipher_context_new(cipher, NULL);
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
263 gaim_cipher_context_append(context, (guchar*)nt_pw, 2*lennt);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
264 gaim_cipher_context_digest(context, 21, (guchar*)nt_hpw, NULL);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
265 gaim_cipher_context_destroy(context);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
266
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
267 memset(nt_hpw+16, 0, 5);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
268
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
269
11427
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
270 calc_resp(lm_hpw, (guchar*)nonce, lm_resp);
e2ebebcae270 [gaim-migrate @ 13664]
Thomas Butter <tbutter>
parents: 11375
diff changeset
271 calc_resp(nt_hpw, (guchar*)nonce, nt_resp);
11375
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
272 memcpy(tmp, lm_resp, 0x18);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
273 memcpy(tmp+0x18, nt_resp, 0x18);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
274 tmp = gaim_base64_encode((guchar*) tmsg, tmsg->msg_len);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
275 g_free(tmsg);
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
276 return tmp;
7e98b3bf2fdf [gaim-migrate @ 13601]
Thomas Butter <tbutter>
parents:
diff changeset
277 }