| 1 |
/* |
| 2 |
Handling of NTLM Authentication |
| 3 |
Copyright (C) 2003, Daniel Stenberg <[email protected]> |
| 4 |
Copyright (C) 2009, Kai Sommerfeld <[email protected]> |
| 5 |
|
| 6 |
This library is free software; you can redistribute it and/or |
| 7 |
modify it under the terms of the GNU Library General Public |
| 8 |
License as published by the Free Software Foundation; either |
| 9 |
version 2 of the License, or (at your option) any later version. |
| 10 |
|
| 11 |
This library is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
Library General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Library General Public |
| 17 |
License along with this library; if not, write to the Free |
| 18 |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 19 |
MA 02111-1307, USA |
| 20 |
|
| 21 |
*/ |
| 22 |
|
| 23 |
/* NTLM details: |
| 24 |
|
| 25 |
http://davenport.sourceforge.net/ntlm.html |
| 26 |
http://www.innovation.ch/java/ntlm.html |
| 27 |
|
| 28 |
*/ |
| 29 |
|
| 30 |
#include "ne_ntlm.h" |
| 31 |
|
| 32 |
#ifdef HAVE_NTLM |
| 33 |
|
| 34 |
#include "ne_string.h" |
| 35 |
|
| 36 |
typedef enum { |
| 37 |
NTLMSTATE_NONE, |
| 38 |
NTLMSTATE_TYPE1, |
| 39 |
NTLMSTATE_TYPE2, |
| 40 |
NTLMSTATE_TYPE3, |
| 41 |
NTLMSTATE_LAST |
| 42 |
} NTLMState; |
| 43 |
|
| 44 |
struct ne_ntlm_context_s { |
| 45 |
NTLMState state; |
| 46 |
unsigned char nonce[8]; |
| 47 |
char *user; |
| 48 |
char *passwd; |
| 49 |
char *requestToken; |
| 50 |
}; |
| 51 |
|
| 52 |
typedef enum { |
| 53 |
NTLM_NONE, /* not a ntlm */ |
| 54 |
NTLM_BAD, /* an ntlm, but one we don't like */ |
| 55 |
NTLM_FIRST, /* the first 401-reply we got with NTLM */ |
| 56 |
NTLM_FINE, /* an ntlm we act on */ |
| 57 |
|
| 58 |
NTLM_LAST /* last entry in this enum, don't use */ |
| 59 |
} ntlm; |
| 60 |
|
| 61 |
/* Flag bits definitions based on http://davenport.sourceforge.net/ntlm.html */ |
| 62 |
|
| 63 |
#define NTLMFLAG_NEGOTIATE_UNICODE (1<<0) |
| 64 |
/* Indicates that Unicode strings are supported for use in security buffer |
| 65 |
data. */ |
| 66 |
|
| 67 |
#define NTLMFLAG_NEGOTIATE_OEM (1<<1) |
| 68 |
/* Indicates that OEM strings are supported for use in security buffer data. */ |
| 69 |
|
| 70 |
#define NTLMFLAG_REQUEST_TARGET (1<<2) |
| 71 |
/* Requests that the server's authentication realm be included in the Type 2 |
| 72 |
message. */ |
| 73 |
|
| 74 |
/* unknown (1<<3) */ |
| 75 |
#define NTLMFLAG_NEGOTIATE_SIGN (1<<4) |
| 76 |
/* Specifies that authenticated communication between the client and server |
| 77 |
should carry a digital signature (message integrity). */ |
| 78 |
|
| 79 |
#define NTLMFLAG_NEGOTIATE_SEAL (1<<5) |
| 80 |
/* Specifies that authenticated communication between the client and server |
| 81 |
should be encrypted (message confidentiality). */ |
| 82 |
|
| 83 |
#define NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE (1<<6) |
| 84 |
/* unknown purpose */ |
| 85 |
|
| 86 |
#define NTLMFLAG_NEGOTIATE_LM_KEY (1<<7) |
| 87 |
/* Indicates that the LAN Manager session key should be used for signing and |
| 88 |
sealing authenticated communications. */ |
| 89 |
|
| 90 |
#define NTLMFLAG_NEGOTIATE_NETWARE (1<<8) |
| 91 |
/* unknown purpose */ |
| 92 |
|
| 93 |
#define NTLMFLAG_NEGOTIATE_NTLM_KEY (1<<9) |
| 94 |
/* Indicates that NTLM authentication is being used. */ |
| 95 |
|
| 96 |
/* unknown (1<<10) */ |
| 97 |
/* unknown (1<<11) */ |
| 98 |
|
| 99 |
#define NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED (1<<12) |
| 100 |
/* Sent by the client in the Type 1 message to indicate that a desired |
| 101 |
authentication realm is included in the message. */ |
| 102 |
|
| 103 |
#define NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED (1<<13) |
| 104 |
/* Sent by the client in the Type 1 message to indicate that the client |
| 105 |
workstation's name is included in the message. */ |
| 106 |
|
| 107 |
#define NTLMFLAG_NEGOTIATE_LOCAL_CALL (1<<14) |
| 108 |
/* Sent by the server to indicate that the server and client are on the same |
| 109 |
machine. Implies that the client may use a pre-established local security |
| 110 |
context rather than responding to the challenge. */ |
| 111 |
|
| 112 |
#define NTLMFLAG_NEGOTIATE_ALWAYS_SIGN (1<<15) |
| 113 |
/* Indicates that authenticated communication between the client and server |
| 114 |
should be signed with a "dummy" signature. */ |
| 115 |
|
| 116 |
#define NTLMFLAG_TARGET_TYPE_DOMAIN (1<<16) |
| 117 |
/* Sent by the server in the Type 2 message to indicate that the target |
| 118 |
authentication realm is a domain. */ |
| 119 |
|
| 120 |
#define NTLMFLAG_TARGET_TYPE_SERVER (1<<17) |
| 121 |
/* Sent by the server in the Type 2 message to indicate that the target |
| 122 |
authentication realm is a server. */ |
| 123 |
|
| 124 |
#define NTLMFLAG_TARGET_TYPE_SHARE (1<<18) |
| 125 |
/* Sent by the server in the Type 2 message to indicate that the target |
| 126 |
authentication realm is a share. Presumably, this is for share-level |
| 127 |
authentication. Usage is unclear. */ |
| 128 |
|
| 129 |
#define NTLMFLAG_NEGOTIATE_NTLM2_KEY (1<<19) |
| 130 |
/* Indicates that the NTLM2 signing and sealing scheme should be used for |
| 131 |
protecting authenticated communications. */ |
| 132 |
|
| 133 |
#define NTLMFLAG_REQUEST_INIT_RESPONSE (1<<20) |
| 134 |
/* unknown purpose */ |
| 135 |
|
| 136 |
#define NTLMFLAG_REQUEST_ACCEPT_RESPONSE (1<<21) |
| 137 |
/* unknown purpose */ |
| 138 |
|
| 139 |
#define NTLMFLAG_REQUEST_NONNT_SESSION_KEY (1<<22) |
| 140 |
/* unknown purpose */ |
| 141 |
|
| 142 |
#define NTLMFLAG_NEGOTIATE_TARGET_INFO (1<<23) |
| 143 |
/* Sent by the server in the Type 2 message to indicate that it is including a |
| 144 |
Target Information block in the message. */ |
| 145 |
|
| 146 |
/* unknown (1<24) */ |
| 147 |
/* unknown (1<25) */ |
| 148 |
/* unknown (1<26) */ |
| 149 |
/* unknown (1<27) */ |
| 150 |
/* unknown (1<28) */ |
| 151 |
|
| 152 |
#define NTLMFLAG_NEGOTIATE_128 (1<<29) |
| 153 |
/* Indicates that 128-bit encryption is supported. */ |
| 154 |
|
| 155 |
#define NTLMFLAG_NEGOTIATE_KEY_EXCHANGE (1<<30) |
| 156 |
/* unknown purpose */ |
| 157 |
|
| 158 |
#define NTLMFLAG_NEGOTIATE_56 (1<<31) |
| 159 |
/* Indicates that 56-bit encryption is supported. */ |
| 160 |
|
| 161 |
#ifdef HAVE_OPENSSL |
| 162 |
/* We need OpenSSL for the crypto lib to provide us with MD4 and DES */ |
| 163 |
|
| 164 |
/* -- WIN32 approved -- */ |
| 165 |
#include <stdio.h> |
| 166 |
#include <string.h> |
| 167 |
#include <stdarg.h> |
| 168 |
#include <stdlib.h> |
| 169 |
#include <ctype.h> |
| 170 |
|
| 171 |
#include <openssl/des.h> |
| 172 |
#include <openssl/md4.h> |
| 173 |
#include <openssl/ssl.h> |
| 174 |
|
| 175 |
#if OPENSSL_VERSION_NUMBER < 0x00907001L |
| 176 |
#define DES_key_schedule des_key_schedule |
| 177 |
#define DES_cblock des_cblock |
| 178 |
#define DES_set_odd_parity des_set_odd_parity |
| 179 |
#define DES_set_key des_set_key |
| 180 |
#define DES_ecb_encrypt des_ecb_encrypt |
| 181 |
|
| 182 |
/* This is how things were done in the old days */ |
| 183 |
#define DESKEY(x) x |
| 184 |
#define DESKEYARG(x) x |
| 185 |
#else |
| 186 |
/* Modern version */ |
| 187 |
#define DESKEYARG(x) *x |
| 188 |
#define DESKEY(x) &x |
| 189 |
#endif |
| 190 |
|
| 191 |
/* Define this to make the type-3 message include the NT response message */ |
| 192 |
#define USE_NTRESPONSES 1 |
| 193 |
|
| 194 |
/* |
| 195 |
(*) = A "security buffer" is a triplet consisting of two shorts and one |
| 196 |
long: |
| 197 |
|
| 198 |
1. a 'short' containing the length of the buffer in bytes |
| 199 |
2. a 'short' containing the allocated space for the buffer in bytes |
| 200 |
3. a 'long' containing the offset to the start of the buffer from the |
| 201 |
beginning of the NTLM message, in bytes. |
| 202 |
*/ |
| 203 |
|
| 204 |
static ntlm ne_input_ntlm(ne_ntlm_context *ctx, |
| 205 |
const char *responseToken) |
| 206 |
{ |
| 207 |
if(responseToken) { |
| 208 |
/* We got a type-2 message here: |
| 209 |
|
| 210 |
Index Description Content |
| 211 |
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP" |
| 212 |
(0x4e544c4d53535000) |
| 213 |
8 NTLM Message Type long (0x02000000) |
| 214 |
12 Target Name security buffer(*) |
| 215 |
20 Flags long |
| 216 |
24 Challenge 8 bytes |
| 217 |
(32) Context (optional) 8 bytes (two consecutive longs) |
| 218 |
(40) Target Information (optional) security buffer(*) |
| 219 |
32 (48) start of data block |
| 220 |
*/ |
| 221 |
unsigned char * buffer = NULL; |
| 222 |
|
| 223 |
int size = ne_unbase64(responseToken, &buffer); |
| 224 |
|
| 225 |
ctx->state = NTLMSTATE_TYPE2; /* we got a type-2 */ |
| 226 |
|
| 227 |
if(size >= 48) |
| 228 |
/* the nonce of interest is index [24 .. 31], 8 bytes */ |
| 229 |
memcpy(ctx->nonce, &buffer[24], 8); |
| 230 |
|
| 231 |
/* at index decimal 20, there's a 32bit NTLM flag field */ |
| 232 |
|
| 233 |
if (buffer) ne_free(buffer); |
| 234 |
} |
| 235 |
else { |
| 236 |
if(ctx->state >= NTLMSTATE_TYPE1) |
| 237 |
return NTLM_BAD; |
| 238 |
|
| 239 |
ctx->state = NTLMSTATE_TYPE1; /* we should sent away a type-1 */ |
| 240 |
} |
| 241 |
return NTLM_FINE; |
| 242 |
} |
| 243 |
|
| 244 |
/* |
| 245 |
* Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The |
| 246 |
* key schedule ks is also set. |
| 247 |
*/ |
| 248 |
static void setup_des_key(unsigned char *key_56, |
| 249 |
DES_key_schedule DESKEYARG(ks)) |
| 250 |
{ |
| 251 |
DES_cblock key; |
| 252 |
|
| 253 |
key[0] = key_56[0]; |
| 254 |
key[1] = ((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1); |
| 255 |
key[2] = ((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2); |
| 256 |
key[3] = ((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3); |
| 257 |
key[4] = ((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4); |
| 258 |
key[5] = ((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5); |
| 259 |
key[6] = ((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6); |
| 260 |
key[7] = (key_56[6] << 1) & 0xFF; |
| 261 |
|
| 262 |
DES_set_odd_parity(&key); |
| 263 |
DES_set_key(&key, ks); |
| 264 |
} |
| 265 |
|
| 266 |
/* |
| 267 |
* takes a 21 byte array and treats it as 3 56-bit DES keys. The |
| 268 |
* 8 byte plaintext is encrypted with each key and the resulting 24 |
| 269 |
* bytes are stored in the results array. |
| 270 |
*/ |
| 271 |
static void calc_resp(unsigned char *keys, |
| 272 |
unsigned char *plaintext, |
| 273 |
unsigned char *results) |
| 274 |
{ |
| 275 |
DES_key_schedule ks; |
| 276 |
|
| 277 |
setup_des_key(keys, DESKEY(ks)); |
| 278 |
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results, |
| 279 |
DESKEY(ks), DES_ENCRYPT); |
| 280 |
|
| 281 |
setup_des_key(keys+7, DESKEY(ks)); |
| 282 |
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+8), |
| 283 |
DESKEY(ks), DES_ENCRYPT); |
| 284 |
|
| 285 |
setup_des_key(keys+14, DESKEY(ks)); |
| 286 |
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+16), |
| 287 |
DESKEY(ks), DES_ENCRYPT); |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
* Set up lanmanager and nt hashed passwords |
| 292 |
*/ |
| 293 |
static void mkhash(char *password, |
| 294 |
unsigned char *nonce, /* 8 bytes */ |
| 295 |
unsigned char *lmresp /* must fit 0x18 bytes */ |
| 296 |
#ifdef USE_NTRESPONSES |
| 297 |
, unsigned char *ntresp /* must fit 0x18 bytes */ |
| 298 |
#endif |
| 299 |
) |
| 300 |
{ |
| 301 |
unsigned char lmbuffer[21]; |
| 302 |
#ifdef USE_NTRESPONSES |
| 303 |
unsigned char ntbuffer[21]; |
| 304 |
#endif |
| 305 |
unsigned char *pw; |
| 306 |
static const unsigned char magic[] = { |
| 307 |
0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 |
| 308 |
}; |
| 309 |
int i; |
| 310 |
int len = strlen(password); |
| 311 |
|
| 312 |
/* make it fit at least 14 bytes */ |
| 313 |
pw = ne_malloc(len<7?14:len*2); |
| 314 |
if(!pw) |
| 315 |
return; /* this will lead to a badly generated package */ |
| 316 |
|
| 317 |
if (len > 14) |
| 318 |
len = 14; |
| 319 |
|
| 320 |
for (i=0; i<len; i++) |
| 321 |
pw[i] = toupper(password[i]); |
| 322 |
|
| 323 |
for (; i<14; i++) |
| 324 |
pw[i] = 0; |
| 325 |
|
| 326 |
{ |
| 327 |
/* create LanManager hashed password */ |
| 328 |
DES_key_schedule ks; |
| 329 |
|
| 330 |
setup_des_key(pw, DESKEY(ks)); |
| 331 |
DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer, |
| 332 |
DESKEY(ks), DES_ENCRYPT); |
| 333 |
|
| 334 |
setup_des_key(pw+7, DESKEY(ks)); |
| 335 |
DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer+8), |
| 336 |
DESKEY(ks), DES_ENCRYPT); |
| 337 |
|
| 338 |
memset(lmbuffer+16, 0, 5); |
| 339 |
} |
| 340 |
/* create LM responses */ |
| 341 |
calc_resp(lmbuffer, nonce, lmresp); |
| 342 |
|
| 343 |
#ifdef USE_NTRESPONSES |
| 344 |
{ |
| 345 |
/* create NT hashed password */ |
| 346 |
MD4_CTX md4; |
| 347 |
|
| 348 |
len = strlen(password); |
| 349 |
|
| 350 |
for (i=0; i<len; i++) { |
| 351 |
pw[2*i] = password[i]; |
| 352 |
pw[2*i+1] = 0; |
| 353 |
} |
| 354 |
|
| 355 |
MD4_Init(&md4); |
| 356 |
MD4_Update(&md4, pw, 2*len); |
| 357 |
MD4_Final(ntbuffer, &md4); |
| 358 |
|
| 359 |
memset(ntbuffer+16, 0, 5); |
| 360 |
} |
| 361 |
|
| 362 |
calc_resp(ntbuffer, nonce, ntresp); |
| 363 |
#endif |
| 364 |
|
| 365 |
ne_free(pw); |
| 366 |
} |
| 367 |
|
| 368 |
#define SHORTPAIR(x) ((x) & 0xff), ((x) >> 8) |
| 369 |
#define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8)&0xff), \ |
| 370 |
(((x) >>16)&0xff), ((x)>>24) |
| 371 |
|
| 372 |
/* this is for creating ntlm header output */ |
| 373 |
static int ne_output_ntlm(ne_ntlm_context *ctx) |
| 374 |
{ |
| 375 |
const char *domain=""; /* empty */ |
| 376 |
const char *host=""; /* empty */ |
| 377 |
int domlen=strlen(domain); |
| 378 |
int hostlen = strlen(host); |
| 379 |
int hostoff; /* host name offset */ |
| 380 |
int domoff; /* domain name offset */ |
| 381 |
int size; |
| 382 |
unsigned char ntlmbuf[256]; /* enough, unless the host/domain is very long */ |
| 383 |
|
| 384 |
if(!ctx->user || !ctx->passwd) |
| 385 |
/* no user, no auth */ |
| 386 |
return 0; /* OK */ |
| 387 |
|
| 388 |
switch(ctx->state) { |
| 389 |
case NTLMSTATE_TYPE1: |
| 390 |
default: /* for the weird cases we (re)start here */ |
| 391 |
hostoff = 32; |
| 392 |
domoff = hostoff + hostlen; |
| 393 |
|
| 394 |
/* Create and send a type-1 message: |
| 395 |
|
| 396 |
Index Description Content |
| 397 |
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP" |
| 398 |
(0x4e544c4d53535000) |
| 399 |
8 NTLM Message Type long (0x01000000) |
| 400 |
12 Flags long |
| 401 |
16 Supplied Domain security buffer(*) |
| 402 |
24 Supplied Workstation security buffer(*) |
| 403 |
32 start of data block |
| 404 |
|
| 405 |
*/ |
| 406 |
|
| 407 |
ne_snprintf((char *)ntlmbuf, sizeof(ntlmbuf), "NTLMSSP%c" |
| 408 |
"\x01%c%c%c" /* 32-bit type = 1 */ |
| 409 |
"%c%c%c%c" /* 32-bit NTLM flag field */ |
| 410 |
"%c%c" /* domain length */ |
| 411 |
"%c%c" /* domain allocated space */ |
| 412 |
"%c%c" /* domain name offset */ |
| 413 |
"%c%c" /* 2 zeroes */ |
| 414 |
"%c%c" /* host length */ |
| 415 |
"%c%c" /* host allocated space */ |
| 416 |
"%c%c" /* host name offset */ |
| 417 |
"%c%c" /* 2 zeroes */ |
| 418 |
"%s" /* host name */ |
| 419 |
"%s", /* domain string */ |
| 420 |
0, /* trailing zero */ |
| 421 |
0,0,0, /* part of type-1 long */ |
| 422 |
|
| 423 |
LONGQUARTET( |
| 424 |
NTLMFLAG_NEGOTIATE_OEM| /* 2 */ |
| 425 |
NTLMFLAG_NEGOTIATE_NTLM_KEY /* 200 */ |
| 426 |
/* equals 0x0202 */ |
| 427 |
), |
| 428 |
SHORTPAIR(domlen), |
| 429 |
SHORTPAIR(domlen), |
| 430 |
SHORTPAIR(domoff), |
| 431 |
0,0, |
| 432 |
SHORTPAIR(hostlen), |
| 433 |
SHORTPAIR(hostlen), |
| 434 |
SHORTPAIR(hostoff), |
| 435 |
0,0, |
| 436 |
host, domain); |
| 437 |
|
| 438 |
/* initial packet length */ |
| 439 |
size = 32 + hostlen + domlen; |
| 440 |
|
| 441 |
/* now keeper of the base64 encoded package size */ |
| 442 |
if (ctx->requestToken) ne_free(ctx->requestToken); |
| 443 |
ctx->requestToken = ne_base64(ntlmbuf, size); |
| 444 |
|
| 445 |
break; |
| 446 |
|
| 447 |
case NTLMSTATE_TYPE2: |
| 448 |
/* We received the type-2 already, create a type-3 message: |
| 449 |
|
| 450 |
Index Description Content |
| 451 |
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP" |
| 452 |
(0x4e544c4d53535000) |
| 453 |
8 NTLM Message Type long (0x03000000) |
| 454 |
12 LM/LMv2 Response security buffer(*) |
| 455 |
20 NTLM/NTLMv2 Response security buffer(*) |
| 456 |
28 Domain Name security buffer(*) |
| 457 |
36 User Name security buffer(*) |
| 458 |
44 Workstation Name security buffer(*) |
| 459 |
(52) Session Key (optional) security buffer(*) |
| 460 |
(60) Flags (optional) long |
| 461 |
52 (64) start of data block |
| 462 |
|
| 463 |
*/ |
| 464 |
|
| 465 |
{ |
| 466 |
int lmrespoff; |
| 467 |
int ntrespoff; |
| 468 |
int useroff; |
| 469 |
unsigned char lmresp[0x18]; /* fixed-size */ |
| 470 |
#ifdef USE_NTRESPONSES |
| 471 |
unsigned char ntresp[0x18]; /* fixed-size */ |
| 472 |
#endif |
| 473 |
const char *user; |
| 474 |
int userlen; |
| 475 |
|
| 476 |
user = strchr(ctx->user, '\\'); |
| 477 |
if(!user) |
| 478 |
user = strchr(ctx->user, '/'); |
| 479 |
|
| 480 |
if (user) { |
| 481 |
domain = ctx->user; |
| 482 |
domlen = user - domain; |
| 483 |
user++; |
| 484 |
} |
| 485 |
else |
| 486 |
user = ctx->user; |
| 487 |
userlen = strlen(user); |
| 488 |
|
| 489 |
mkhash(ctx->passwd, &ctx->nonce[0], lmresp |
| 490 |
#ifdef USE_NTRESPONSES |
| 491 |
, ntresp |
| 492 |
#endif |
| 493 |
); |
| 494 |
|
| 495 |
domoff = 64; /* always */ |
| 496 |
useroff = domoff + domlen; |
| 497 |
hostoff = useroff + userlen; |
| 498 |
lmrespoff = hostoff + hostlen; |
| 499 |
ntrespoff = lmrespoff + 0x18; |
| 500 |
|
| 501 |
/* Create the big type-3 message binary blob */ |
| 502 |
size = ne_snprintf((char *)ntlmbuf, sizeof(ntlmbuf), |
| 503 |
"NTLMSSP%c" |
| 504 |
"\x03%c%c%c" /* type-3, 32 bits */ |
| 505 |
|
| 506 |
"%c%c%c%c" /* LanManager length + allocated space */ |
| 507 |
"%c%c" /* LanManager offset */ |
| 508 |
"%c%c" /* 2 zeroes */ |
| 509 |
|
| 510 |
"%c%c" /* NT-response length */ |
| 511 |
"%c%c" /* NT-response allocated space */ |
| 512 |
"%c%c" /* NT-response offset */ |
| 513 |
"%c%c" /* 2 zeroes */ |
| 514 |
|
| 515 |
"%c%c" /* domain length */ |
| 516 |
"%c%c" /* domain allocated space */ |
| 517 |
"%c%c" /* domain name offset */ |
| 518 |
"%c%c" /* 2 zeroes */ |
| 519 |
|
| 520 |
"%c%c" /* user length */ |
| 521 |
"%c%c" /* user allocated space */ |
| 522 |
"%c%c" /* user offset */ |
| 523 |
"%c%c" /* 2 zeroes */ |
| 524 |
|
| 525 |
"%c%c" /* host length */ |
| 526 |
"%c%c" /* host allocated space */ |
| 527 |
"%c%c" /* host offset */ |
| 528 |
"%c%c%c%c%c%c" /* 6 zeroes */ |
| 529 |
|
| 530 |
"\xff\xff" /* message length */ |
| 531 |
"%c%c" /* 2 zeroes */ |
| 532 |
|
| 533 |
"\x01\x82" /* flags */ |
| 534 |
"%c%c" /* 2 zeroes */ |
| 535 |
|
| 536 |
/* domain string */ |
| 537 |
/* user string */ |
| 538 |
/* host string */ |
| 539 |
/* LanManager response */ |
| 540 |
/* NT response */ |
| 541 |
, |
| 542 |
0, /* zero termination */ |
| 543 |
0,0,0, /* type-3 long, the 24 upper bits */ |
| 544 |
|
| 545 |
SHORTPAIR(0x18), /* LanManager response length, twice */ |
| 546 |
SHORTPAIR(0x18), |
| 547 |
SHORTPAIR(lmrespoff), |
| 548 |
0x0, 0x0, |
| 549 |
|
| 550 |
#ifdef USE_NTRESPONSES |
| 551 |
SHORTPAIR(0x18), /* NT-response length, twice */ |
| 552 |
SHORTPAIR(0x18), |
| 553 |
#else |
| 554 |
0x0, 0x0, |
| 555 |
0x0, 0x0, |
| 556 |
#endif |
| 557 |
SHORTPAIR(ntrespoff), |
| 558 |
0x0, 0x0, |
| 559 |
|
| 560 |
SHORTPAIR(domlen), |
| 561 |
SHORTPAIR(domlen), |
| 562 |
SHORTPAIR(domoff), |
| 563 |
0x0, 0x0, |
| 564 |
|
| 565 |
SHORTPAIR(userlen), |
| 566 |
SHORTPAIR(userlen), |
| 567 |
SHORTPAIR(useroff), |
| 568 |
0x0, 0x0, |
| 569 |
|
| 570 |
SHORTPAIR(hostlen), |
| 571 |
SHORTPAIR(hostlen), |
| 572 |
SHORTPAIR(hostoff), |
| 573 |
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 574 |
|
| 575 |
0x0, 0x0, |
| 576 |
|
| 577 |
0x0, 0x0); |
| 578 |
|
| 579 |
/* size is now 64 */ |
| 580 |
size=64; |
| 581 |
ntlmbuf[62]=ntlmbuf[63]=0; |
| 582 |
|
| 583 |
/* Make sure that the user and domain strings fit in the target buffer |
| 584 |
before we copy them there. */ |
| 585 |
if((size_t)size + userlen + domlen >= sizeof(ntlmbuf)) { |
| 586 |
return -1; |
| 587 |
} |
| 588 |
|
| 589 |
memcpy(&ntlmbuf[size], domain, domlen); |
| 590 |
size += domlen; |
| 591 |
|
| 592 |
memcpy(&ntlmbuf[size], user, userlen); |
| 593 |
size += userlen; |
| 594 |
|
| 595 |
/* we append the binary hashes to the end of the blob */ |
| 596 |
if(size < ((int)sizeof(ntlmbuf) - 0x18)) { |
| 597 |
memcpy(&ntlmbuf[size], lmresp, 0x18); |
| 598 |
size += 0x18; |
| 599 |
} |
| 600 |
|
| 601 |
#ifdef USE_NTRESPONSES |
| 602 |
if(size < ((int)sizeof(ntlmbuf) - 0x18)) { |
| 603 |
memcpy(&ntlmbuf[size], ntresp, 0x18); |
| 604 |
size += 0x18; |
| 605 |
} |
| 606 |
#endif |
| 607 |
|
| 608 |
ntlmbuf[56] = size & 0xff; |
| 609 |
ntlmbuf[57] = size >> 8; |
| 610 |
|
| 611 |
/* convert the binary blob into base64 */ |
| 612 |
ctx->requestToken = ne_base64(ntlmbuf, size); |
| 613 |
|
| 614 |
ctx->state = NTLMSTATE_TYPE3; /* we sent a type-3 */ |
| 615 |
} |
| 616 |
break; |
| 617 |
|
| 618 |
case NTLMSTATE_TYPE3: |
| 619 |
/* connection is already authenticated, |
| 620 |
* don't send a header in future requests */ |
| 621 |
if (ctx->requestToken) ne_free(ctx->requestToken); |
| 622 |
ctx->requestToken = NULL; |
| 623 |
break; |
| 624 |
} |
| 625 |
|
| 626 |
return 0; /* OK */ |
| 627 |
} |
| 628 |
|
| 629 |
ne_ntlm_context *ne__ntlm_create_context(const char *userName, const char *password) |
| 630 |
{ |
| 631 |
ne_ntlm_context *ctx = ne_calloc(sizeof(ne_ntlm_context)); |
| 632 |
|
| 633 |
ctx->state = NTLMSTATE_NONE; |
| 634 |
ctx->user = ne_strdup(userName); |
| 635 |
ctx->passwd = ne_strdup(password); |
| 636 |
|
| 637 |
return ctx; |
| 638 |
} |
| 639 |
|
| 640 |
void ne__ntlm_destroy_context(ne_ntlm_context *context) |
| 641 |
{ |
| 642 |
if (context->user) |
| 643 |
ne_free(context->user); |
| 644 |
|
| 645 |
if (context->passwd) |
| 646 |
ne_free(context->passwd); |
| 647 |
|
| 648 |
if (context->requestToken) |
| 649 |
ne_free(context->requestToken); |
| 650 |
|
| 651 |
ne_free(context); |
| 652 |
} |
| 653 |
|
| 654 |
int ne__ntlm_authenticate(ne_ntlm_context *context, const char *responseToken) |
| 655 |
{ |
| 656 |
if (context == NULL) { |
| 657 |
return -1; |
| 658 |
} else { |
| 659 |
if (!responseToken && (context->state == NTLMSTATE_TYPE3)) |
| 660 |
context->state = NTLMSTATE_NONE; |
| 661 |
|
| 662 |
if (context->state <= NTLMSTATE_TYPE3) { |
| 663 |
ntlm ntlmstatus = ne_input_ntlm(context, responseToken); |
| 664 |
|
| 665 |
if (ntlmstatus != NTLM_FINE) { |
| 666 |
return -1; |
| 667 |
} |
| 668 |
} |
| 669 |
} |
| 670 |
return ne_output_ntlm(context); |
| 671 |
} |
| 672 |
|
| 673 |
char *ne__ntlm_getRequestToken(ne_ntlm_context *context) |
| 674 |
{ |
| 675 |
char *ret; |
| 676 |
|
| 677 |
if (context == NULL || !context->requestToken) { |
| 678 |
return NULL; |
| 679 |
} |
| 680 |
|
| 681 |
ret = ne_strdup(context->requestToken); |
| 682 |
ne_free(context->requestToken); |
| 683 |
context->requestToken = NULL; |
| 684 |
return ret; |
| 685 |
} |
| 686 |
|
| 687 |
#endif /* HAVE_OPENSSL */ |
| 688 |
|
| 689 |
#endif /* HAVE_NTLM */ |