Coverage Report

Created: 2022-09-28 06:21

/src/curl/lib/vauth/ntlm.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
25
#include "curl_setup.h"
26
27
#if defined(USE_NTLM) && !defined(USE_WINDOWS_SSPI)
28
29
/*
30
 * NTLM details:
31
 *
32
 * https://davenport.sourceforge.io/ntlm.html
33
 * https://www.innovation.ch/java/ntlm.html
34
 */
35
36
#define DEBUG_ME 0
37
38
#include "urldata.h"
39
#include "sendf.h"
40
#include "curl_ntlm_core.h"
41
#include "curl_gethostname.h"
42
#include "curl_multibyte.h"
43
#include "curl_md5.h"
44
#include "warnless.h"
45
#include "rand.h"
46
#include "vtls/vtls.h"
47
48
/* SSL backend-specific #if branches in this file must be kept in the order
49
   documented in curl_ntlm_core. */
50
#if defined(NTLM_NEEDS_NSS_INIT)
51
#include "vtls/nssg.h" /* for Curl_nss_force_init() */
52
#endif
53
54
#define BUILDING_CURL_NTLM_MSGS_C
55
#include "vauth/vauth.h"
56
#include "vauth/ntlm.h"
57
#include "curl_endian.h"
58
#include "curl_printf.h"
59
60
/* The last #include files should be: */
61
#include "curl_memory.h"
62
#include "memdebug.h"
63
64
/* "NTLMSSP" signature is always in ASCII regardless of the platform */
65
1.42k
#define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
66
67
/* The fixed host name we provide, in order to not leak our real local host
68
   name. Copy the name used by Firefox. */
69
234
#define NTLM_HOSTNAME "WORKSTATION"
70
71
#if DEBUG_ME
72
# define DEBUG_OUT(x) x
73
static void ntlm_print_flags(FILE *handle, unsigned long flags)
74
{
75
  if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
76
    fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
77
  if(flags & NTLMFLAG_NEGOTIATE_OEM)
78
    fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
79
  if(flags & NTLMFLAG_REQUEST_TARGET)
80
    fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
81
  if(flags & (1<<3))
82
    fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
83
  if(flags & NTLMFLAG_NEGOTIATE_SIGN)
84
    fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
85
  if(flags & NTLMFLAG_NEGOTIATE_SEAL)
86
    fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
87
  if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
88
    fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
89
  if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
90
    fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
91
  if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
92
    fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
93
  if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
94
    fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
95
  if(flags & (1<<10))
96
    fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
97
  if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
98
    fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
99
  if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
100
    fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
101
  if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
102
    fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
103
  if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
104
    fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
105
  if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
106
    fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
107
  if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
108
    fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
109
  if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
110
    fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
111
  if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
112
    fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
113
  if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
114
    fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
115
  if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
116
    fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
117
  if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
118
    fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
119
  if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
120
    fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
121
  if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
122
    fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
123
  if(flags & (1<<24))
124
    fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
125
  if(flags & (1<<25))
126
    fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
127
  if(flags & (1<<26))
128
    fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
129
  if(flags & (1<<27))
130
    fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
131
  if(flags & (1<<28))
132
    fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
133
  if(flags & NTLMFLAG_NEGOTIATE_128)
134
    fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
135
  if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
136
    fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
137
  if(flags & NTLMFLAG_NEGOTIATE_56)
138
    fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
139
}
140
141
static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
142
{
143
  const char *p = buf;
144
145
  (void) handle;
146
147
  fprintf(stderr, "0x");
148
  while(len-- > 0)
149
    fprintf(stderr, "%02.2x", (unsigned int)*p++);
150
}
151
#else
152
1.27k
# define DEBUG_OUT(x) Curl_nop_stmt
153
#endif
154
155
/*
156
 * ntlm_decode_type2_target()
157
 *
158
 * This is used to decode the "target info" in the NTLM type-2 message
159
 * received.
160
 *
161
 * Parameters:
162
 *
163
 * data      [in]     - The session handle.
164
 * type2ref  [in]     - The type-2 message.
165
 * ntlm      [in/out] - The NTLM data struct being used and modified.
166
 *
167
 * Returns CURLE_OK on success.
168
 */
169
static CURLcode ntlm_decode_type2_target(struct Curl_easy *data,
170
                                         const struct bufref *type2ref,
171
                                         struct ntlmdata *ntlm)
172
275
{
173
275
  unsigned short target_info_len = 0;
174
275
  unsigned int target_info_offset = 0;
175
275
  const unsigned char *type2 = Curl_bufref_ptr(type2ref);
176
275
  size_t type2len = Curl_bufref_len(type2ref);
177
178
#if defined(CURL_DISABLE_VERBOSE_STRINGS)
179
  (void) data;
180
#endif
181
182
275
  if(type2len >= 48) {
183
171
    target_info_len = Curl_read16_le(&type2[40]);
184
171
    target_info_offset = Curl_read32_le(&type2[44]);
185
171
    if(target_info_len > 0) {
186
152
      if((target_info_offset > type2len) ||
187
152
         (target_info_offset + target_info_len) > type2len ||
188
152
         target_info_offset < 48) {
189
123
        infof(data, "NTLM handshake failure (bad type-2 message). "
190
123
              "Target Info Offset Len is set incorrect by the peer");
191
123
        return CURLE_BAD_CONTENT_ENCODING;
192
123
      }
193
194
29
      free(ntlm->target_info); /* replace any previous data */
195
29
      ntlm->target_info = malloc(target_info_len);
196
29
      if(!ntlm->target_info)
197
0
        return CURLE_OUT_OF_MEMORY;
198
199
29
      memcpy(ntlm->target_info, &type2[target_info_offset], target_info_len);
200
29
    }
201
171
  }
202
203
152
  ntlm->target_info_len = target_info_len;
204
205
152
  return CURLE_OK;
206
275
}
207
208
/*
209
  NTLM message structure notes:
210
211
  A 'short' is a 'network short', a little-endian 16-bit unsigned value.
212
213
  A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
214
215
  A 'security buffer' represents a triplet used to point to a buffer,
216
  consisting of two shorts and one long:
217
218
    1. A 'short' containing the length of the buffer content in bytes.
219
    2. A 'short' containing the allocated space for the buffer in bytes.
220
    3. A 'long' containing the offset to the start of the buffer in bytes,
221
       from the beginning of the NTLM message.
222
*/
223
224
/*
225
 * Curl_auth_is_ntlm_supported()
226
 *
227
 * This is used to evaluate if NTLM is supported.
228
 *
229
 * Parameters: None
230
 *
231
 * Returns TRUE as NTLM as handled by libcurl.
232
 */
233
bool Curl_auth_is_ntlm_supported(void)
234
948
{
235
948
  return TRUE;
236
948
}
237
238
/*
239
 * Curl_auth_decode_ntlm_type2_message()
240
 *
241
 * This is used to decode an NTLM type-2 message. The raw NTLM message is
242
 * checked * for validity before the appropriate data for creating a type-3
243
 * message is * written to the given NTLM data structure.
244
 *
245
 * Parameters:
246
 *
247
 * data     [in]     - The session handle.
248
 * type2ref [in]     - The type-2 message.
249
 * ntlm     [in/out] - The NTLM data struct being used and modified.
250
 *
251
 * Returns CURLE_OK on success.
252
 */
253
CURLcode Curl_auth_decode_ntlm_type2_message(struct Curl_easy *data,
254
                                             const struct bufref *type2ref,
255
                                             struct ntlmdata *ntlm)
256
901
{
257
901
  static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
258
259
  /* NTLM type-2 message structure:
260
261
          Index  Description            Content
262
            0    NTLMSSP Signature      Null-terminated ASCII "NTLMSSP"
263
                                        (0x4e544c4d53535000)
264
            8    NTLM Message Type      long (0x02000000)
265
           12    Target Name            security buffer
266
           20    Flags                  long
267
           24    Challenge              8 bytes
268
          (32)   Context                8 bytes (two consecutive longs) (*)
269
          (40)   Target Information     security buffer (*)
270
          (48)   OS Version Structure   8 bytes (*)
271
  32 (48) (56)   Start of data block    (*)
272
                                        (*) -> Optional
273
  */
274
275
901
  CURLcode result = CURLE_OK;
276
901
  const unsigned char *type2 = Curl_bufref_ptr(type2ref);
277
901
  size_t type2len = Curl_bufref_len(type2ref);
278
279
#if defined(NTLM_NEEDS_NSS_INIT)
280
  /* Make sure the crypto backend is initialized */
281
  result = Curl_nss_force_init(data);
282
  if(result)
283
    return result;
284
#elif defined(CURL_DISABLE_VERBOSE_STRINGS)
285
  (void)data;
286
#endif
287
288
901
  ntlm->flags = 0;
289
290
901
  if((type2len < 32) ||
291
901
     (memcmp(type2, NTLMSSP_SIGNATURE, 8) != 0) ||
292
901
     (memcmp(type2 + 8, type2_marker, sizeof(type2_marker)) != 0)) {
293
    /* This was not a good enough type-2 message */
294
551
    infof(data, "NTLM handshake failure (bad type-2 message)");
295
551
    return CURLE_BAD_CONTENT_ENCODING;
296
551
  }
297
298
350
  ntlm->flags = Curl_read32_le(&type2[20]);
299
350
  memcpy(ntlm->nonce, &type2[24], 8);
300
301
350
  if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) {
302
275
    result = ntlm_decode_type2_target(data, type2ref, ntlm);
303
275
    if(result) {
304
123
      infof(data, "NTLM handshake failure (bad type-2 message)");
305
123
      return result;
306
123
    }
307
275
  }
308
309
227
  DEBUG_OUT({
310
227
    fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
311
227
    ntlm_print_flags(stderr, ntlm->flags);
312
227
    fprintf(stderr, "\n                  nonce=");
313
227
    ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
314
227
    fprintf(stderr, "\n****\n");
315
227
    fprintf(stderr, "**** Header %s\n ", header);
316
227
  });
317
318
227
  return result;
319
350
}
320
321
/* copy the source to the destination and fill in zeroes in every
322
   other destination byte! */
323
static void unicodecpy(unsigned char *dest, const char *src, size_t length)
324
201
{
325
201
  size_t i;
326
7.34k
  for(i = 0; i < length; i++) {
327
7.14k
    dest[2 * i] = (unsigned char)src[i];
328
7.14k
    dest[2 * i + 1] = '\0';
329
7.14k
  }
330
201
}
331
332
/*
333
 * Curl_auth_create_ntlm_type1_message()
334
 *
335
 * This is used to generate an NTLM type-1 message ready for sending to the
336
 * recipient using the appropriate compile time crypto API.
337
 *
338
 * Parameters:
339
 *
340
 * data    [in]     - The session handle.
341
 * userp   [in]     - The user name in the format User or Domain\User.
342
 * passwdp [in]     - The user's password.
343
 * service [in]     - The service type such as http, smtp, pop or imap.
344
 * host    [in]     - The host name.
345
 * ntlm    [in/out] - The NTLM data struct being used and modified.
346
 * out     [out]    - The result storage.
347
 *
348
 * Returns CURLE_OK on success.
349
 */
350
CURLcode Curl_auth_create_ntlm_type1_message(struct Curl_easy *data,
351
                                             const char *userp,
352
                                             const char *passwdp,
353
                                             const char *service,
354
                                             const char *hostname,
355
                                             struct ntlmdata *ntlm,
356
                                             struct bufref *out)
357
692
{
358
  /* NTLM type-1 message structure:
359
360
       Index  Description            Content
361
         0    NTLMSSP Signature      Null-terminated ASCII "NTLMSSP"
362
                                     (0x4e544c4d53535000)
363
         8    NTLM Message Type      long (0x01000000)
364
        12    Flags                  long
365
       (16)   Supplied Domain        security buffer (*)
366
       (24)   Supplied Workstation   security buffer (*)
367
       (32)   OS Version Structure   8 bytes (*)
368
  (32) (40)   Start of data block    (*)
369
                                     (*) -> Optional
370
  */
371
372
692
  size_t size;
373
374
692
  char *ntlmbuf;
375
692
  const char *host = "";              /* empty */
376
692
  const char *domain = "";            /* empty */
377
692
  size_t hostlen = 0;
378
692
  size_t domlen = 0;
379
692
  size_t hostoff = 0;
380
692
  size_t domoff = hostoff + hostlen;  /* This is 0: remember that host and
381
                                         domain are empty */
382
692
  (void)data;
383
692
  (void)userp;
384
692
  (void)passwdp;
385
692
  (void)service,
386
692
  (void)hostname,
387
388
  /* Clean up any former leftovers and initialise to defaults */
389
692
  Curl_auth_cleanup_ntlm(ntlm);
390
391
692
  ntlmbuf = aprintf(NTLMSSP_SIGNATURE "%c"
392
692
                    "\x01%c%c%c" /* 32-bit type = 1 */
393
692
                    "%c%c%c%c"   /* 32-bit NTLM flag field */
394
692
                    "%c%c"       /* domain length */
395
692
                    "%c%c"       /* domain allocated space */
396
692
                    "%c%c"       /* domain name offset */
397
692
                    "%c%c"       /* 2 zeroes */
398
692
                    "%c%c"       /* host length */
399
692
                    "%c%c"       /* host allocated space */
400
692
                    "%c%c"       /* host name offset */
401
692
                    "%c%c"       /* 2 zeroes */
402
692
                    "%s"         /* host name */
403
692
                    "%s",        /* domain string */
404
692
                    0,           /* trailing zero */
405
692
                    0, 0, 0,     /* part of type-1 long */
406
407
692
                    LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
408
692
                                NTLMFLAG_REQUEST_TARGET |
409
692
                                NTLMFLAG_NEGOTIATE_NTLM_KEY |
410
692
                                NTLMFLAG_NEGOTIATE_NTLM2_KEY |
411
692
                                NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
412
692
                    SHORTPAIR(domlen),
413
692
                    SHORTPAIR(domlen),
414
692
                    SHORTPAIR(domoff),
415
692
                    0, 0,
416
692
                    SHORTPAIR(hostlen),
417
692
                    SHORTPAIR(hostlen),
418
692
                    SHORTPAIR(hostoff),
419
692
                    0, 0,
420
692
                    host,  /* this is empty */
421
692
                    domain /* this is empty */);
422
423
692
  if(!ntlmbuf)
424
0
    return CURLE_OUT_OF_MEMORY;
425
426
  /* Initial packet length */
427
692
  size = 32 + hostlen + domlen;
428
429
692
  DEBUG_OUT({
430
692
    fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
431
692
            "0x%08.8x ",
432
692
            LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
433
692
                        NTLMFLAG_REQUEST_TARGET |
434
692
                        NTLMFLAG_NEGOTIATE_NTLM_KEY |
435
692
                        NTLMFLAG_NEGOTIATE_NTLM2_KEY |
436
692
                        NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
437
692
            NTLMFLAG_NEGOTIATE_OEM |
438
692
            NTLMFLAG_REQUEST_TARGET |
439
692
            NTLMFLAG_NEGOTIATE_NTLM_KEY |
440
692
            NTLMFLAG_NEGOTIATE_NTLM2_KEY |
441
692
            NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
442
692
    ntlm_print_flags(stderr,
443
692
                     NTLMFLAG_NEGOTIATE_OEM |
444
692
                     NTLMFLAG_REQUEST_TARGET |
445
692
                     NTLMFLAG_NEGOTIATE_NTLM_KEY |
446
692
                     NTLMFLAG_NEGOTIATE_NTLM2_KEY |
447
692
                     NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
448
692
    fprintf(stderr, "\n****\n");
449
692
  });
450
451
692
  Curl_bufref_set(out, ntlmbuf, size, curl_free);
452
692
  return CURLE_OK;
453
692
}
454
455
/*
456
 * Curl_auth_create_ntlm_type3_message()
457
 *
458
 * This is used to generate an already encoded NTLM type-3 message ready for
459
 * sending to the recipient using the appropriate compile time crypto API.
460
 *
461
 * Parameters:
462
 *
463
 * data    [in]     - The session handle.
464
 * userp   [in]     - The user name in the format User or Domain\User.
465
 * passwdp [in]     - The user's password.
466
 * ntlm    [in/out] - The NTLM data struct being used and modified.
467
 * out     [out]    - The result storage.
468
 *
469
 * Returns CURLE_OK on success.
470
 */
471
CURLcode Curl_auth_create_ntlm_type3_message(struct Curl_easy *data,
472
                                             const char *userp,
473
                                             const char *passwdp,
474
                                             struct ntlmdata *ntlm,
475
                                             struct bufref *out)
476
117
{
477
  /* NTLM type-3 message structure:
478
479
          Index  Description            Content
480
            0    NTLMSSP Signature      Null-terminated ASCII "NTLMSSP"
481
                                        (0x4e544c4d53535000)
482
            8    NTLM Message Type      long (0x03000000)
483
           12    LM/LMv2 Response       security buffer
484
           20    NTLM/NTLMv2 Response   security buffer
485
           28    Target Name            security buffer
486
           36    User Name              security buffer
487
           44    Workstation Name       security buffer
488
          (52)   Session Key            security buffer (*)
489
          (60)   Flags                  long (*)
490
          (64)   OS Version Structure   8 bytes (*)
491
  52 (64) (72)   Start of data block
492
                                          (*) -> Optional
493
  */
494
495
117
  CURLcode result = CURLE_OK;
496
117
  size_t size;
497
117
  unsigned char ntlmbuf[NTLM_BUFSIZE];
498
117
  int lmrespoff;
499
117
  unsigned char lmresp[24]; /* fixed-size */
500
117
  int ntrespoff;
501
117
  unsigned int ntresplen = 24;
502
117
  unsigned char ntresp[24]; /* fixed-size */
503
117
  unsigned char *ptr_ntresp = &ntresp[0];
504
117
  unsigned char *ntlmv2resp = NULL;
505
117
  bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
506
117
  char host[HOSTNAME_MAX + 1] = "";
507
117
  const char *user;
508
117
  const char *domain = "";
509
117
  size_t hostoff = 0;
510
117
  size_t useroff = 0;
511
117
  size_t domoff = 0;
512
117
  size_t hostlen = 0;
513
117
  size_t userlen = 0;
514
117
  size_t domlen = 0;
515
516
117
  user = strchr(userp, '\\');
517
117
  if(!user)
518
99
    user = strchr(userp, '/');
519
520
117
  if(user) {
521
36
    domain = userp;
522
36
    domlen = (user - domain);
523
36
    user++;
524
36
  }
525
81
  else
526
81
    user = userp;
527
528
117
  userlen = strlen(user);
529
530
#ifndef NTLM_HOSTNAME
531
  /* Get the machine's un-qualified host name as NTLM doesn't like the fully
532
     qualified domain name */
533
  if(Curl_gethostname(host, sizeof(host))) {
534
    infof(data, "gethostname() failed, continuing without");
535
    hostlen = 0;
536
  }
537
  else {
538
    hostlen = strlen(host);
539
  }
540
#else
541
117
  (void)msnprintf(host, sizeof(host), "%s", NTLM_HOSTNAME);
542
117
  hostlen = sizeof(NTLM_HOSTNAME)-1;
543
117
#endif
544
545
117
  if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
546
96
    unsigned char ntbuffer[0x18];
547
96
    unsigned char entropy[8];
548
96
    unsigned char ntlmv2hash[0x18];
549
550
    /* Full NTLM version 2
551
       Although this cannot be negotiated, it is used here if available, as
552
       servers featuring extended security are likely supporting also
553
       NTLMv2. */
554
96
    result = Curl_rand(data, entropy, 8);
555
96
    if(result)
556
0
      return result;
557
558
96
    result = Curl_ntlm_core_mk_nt_hash(passwdp, ntbuffer);
559
96
    if(result)
560
0
      return result;
561
562
96
    result = Curl_ntlm_core_mk_ntlmv2_hash(user, userlen, domain, domlen,
563
96
                                           ntbuffer, ntlmv2hash);
564
96
    if(result)
565
0
      return result;
566
567
    /* LMv2 response */
568
96
    result = Curl_ntlm_core_mk_lmv2_resp(ntlmv2hash, entropy,
569
96
                                         &ntlm->nonce[0], lmresp);
570
96
    if(result)
571
0
      return result;
572
573
    /* NTLMv2 response */
574
96
    result = Curl_ntlm_core_mk_ntlmv2_resp(ntlmv2hash, entropy,
575
96
                                           ntlm, &ntlmv2resp, &ntresplen);
576
96
    if(result)
577
0
      return result;
578
579
96
    ptr_ntresp = ntlmv2resp;
580
96
  }
581
21
  else {
582
583
21
    unsigned char ntbuffer[0x18];
584
21
    unsigned char lmbuffer[0x18];
585
586
    /* NTLM version 1 */
587
588
21
    result = Curl_ntlm_core_mk_nt_hash(passwdp, ntbuffer);
589
21
    if(result)
590
0
      return result;
591
592
21
    Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
593
594
21
    result = Curl_ntlm_core_mk_lm_hash(passwdp, lmbuffer);
595
21
    if(result)
596
0
      return result;
597
598
21
    Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
599
21
    ntlm->flags &= ~NTLMFLAG_NEGOTIATE_NTLM2_KEY;
600
601
    /* A safer but less compatible alternative is:
602
     *   Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
603
     * See https://davenport.sourceforge.io/ntlm.html#ntlmVersion2 */
604
21
  }
605
606
117
  if(unicode) {
607
79
    domlen = domlen * 2;
608
79
    userlen = userlen * 2;
609
79
    hostlen = hostlen * 2;
610
79
  }
611
612
117
  lmrespoff = 64; /* size of the message header */
613
117
  ntrespoff = lmrespoff + 0x18;
614
117
  domoff = ntrespoff + ntresplen;
615
117
  useroff = domoff + domlen;
616
117
  hostoff = useroff + userlen;
617
618
  /* Create the big type-3 message binary blob */
619
117
  size = msnprintf((char *)ntlmbuf, NTLM_BUFSIZE,
620
117
                   NTLMSSP_SIGNATURE "%c"
621
117
                   "\x03%c%c%c"  /* 32-bit type = 3 */
622
623
117
                   "%c%c"  /* LanManager length */
624
117
                   "%c%c"  /* LanManager allocated space */
625
117
                   "%c%c"  /* LanManager offset */
626
117
                   "%c%c"  /* 2 zeroes */
627
628
117
                   "%c%c"  /* NT-response length */
629
117
                   "%c%c"  /* NT-response allocated space */
630
117
                   "%c%c"  /* NT-response offset */
631
117
                   "%c%c"  /* 2 zeroes */
632
633
117
                   "%c%c"  /* domain length */
634
117
                   "%c%c"  /* domain allocated space */
635
117
                   "%c%c"  /* domain name offset */
636
117
                   "%c%c"  /* 2 zeroes */
637
638
117
                   "%c%c"  /* user length */
639
117
                   "%c%c"  /* user allocated space */
640
117
                   "%c%c"  /* user offset */
641
117
                   "%c%c"  /* 2 zeroes */
642
643
117
                   "%c%c"  /* host length */
644
117
                   "%c%c"  /* host allocated space */
645
117
                   "%c%c"  /* host offset */
646
117
                   "%c%c"  /* 2 zeroes */
647
648
117
                   "%c%c"  /* session key length (unknown purpose) */
649
117
                   "%c%c"  /* session key allocated space (unknown purpose) */
650
117
                   "%c%c"  /* session key offset (unknown purpose) */
651
117
                   "%c%c"  /* 2 zeroes */
652
653
117
                   "%c%c%c%c",  /* flags */
654
655
                   /* domain string */
656
                   /* user string */
657
                   /* host string */
658
                   /* LanManager response */
659
                   /* NT response */
660
661
117
                   0,                /* null-termination */
662
117
                   0, 0, 0,          /* type-3 long, the 24 upper bits */
663
664
117
                   SHORTPAIR(0x18),  /* LanManager response length, twice */
665
117
                   SHORTPAIR(0x18),
666
117
                   SHORTPAIR(lmrespoff),
667
117
                   0x0, 0x0,
668
669
117
                   SHORTPAIR(ntresplen),  /* NT-response length, twice */
670
117
                   SHORTPAIR(ntresplen),
671
117
                   SHORTPAIR(ntrespoff),
672
117
                   0x0, 0x0,
673
674
117
                   SHORTPAIR(domlen),
675
117
                   SHORTPAIR(domlen),
676
117
                   SHORTPAIR(domoff),
677
117
                   0x0, 0x0,
678
679
117
                   SHORTPAIR(userlen),
680
117
                   SHORTPAIR(userlen),
681
117
                   SHORTPAIR(useroff),
682
117
                   0x0, 0x0,
683
684
117
                   SHORTPAIR(hostlen),
685
117
                   SHORTPAIR(hostlen),
686
117
                   SHORTPAIR(hostoff),
687
117
                   0x0, 0x0,
688
689
117
                   0x0, 0x0,
690
117
                   0x0, 0x0,
691
117
                   0x0, 0x0,
692
117
                   0x0, 0x0,
693
694
117
                   LONGQUARTET(ntlm->flags));
695
696
117
  DEBUGASSERT(size == 64);
697
117
  DEBUGASSERT(size == (size_t)lmrespoff);
698
699
  /* We append the binary hashes */
700
117
  if(size < (NTLM_BUFSIZE - 0x18)) {
701
117
    memcpy(&ntlmbuf[size], lmresp, 0x18);
702
117
    size += 0x18;
703
117
  }
704
705
117
  DEBUG_OUT({
706
117
    fprintf(stderr, "**** TYPE3 header lmresp=");
707
117
    ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
708
117
  });
709
710
  /* ntresplen + size should not be risking an integer overflow here */
711
117
  if(ntresplen + size > sizeof(ntlmbuf)) {
712
0
    failf(data, "incoming NTLM message too big");
713
0
    return CURLE_OUT_OF_MEMORY;
714
0
  }
715
117
  DEBUGASSERT(size == (size_t)ntrespoff);
716
117
  memcpy(&ntlmbuf[size], ptr_ntresp, ntresplen);
717
117
  size += ntresplen;
718
719
117
  DEBUG_OUT({
720
117
    fprintf(stderr, "\n   ntresp=");
721
117
    ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], ntresplen);
722
117
  });
723
724
117
  free(ntlmv2resp);/* Free the dynamic buffer allocated for NTLMv2 */
725
726
117
  DEBUG_OUT({
727
117
    fprintf(stderr, "\n   flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
728
117
            LONGQUARTET(ntlm->flags), ntlm->flags);
729
117
    ntlm_print_flags(stderr, ntlm->flags);
730
117
    fprintf(stderr, "\n****\n");
731
117
  });
732
733
  /* Make sure that the domain, user and host strings fit in the
734
     buffer before we copy them there. */
735
117
  if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
736
20
    failf(data, "user + domain + host name too big");
737
20
    return CURLE_OUT_OF_MEMORY;
738
20
  }
739
740
97
  DEBUGASSERT(size == domoff);
741
97
  if(unicode)
742
67
    unicodecpy(&ntlmbuf[size], domain, domlen / 2);
743
30
  else
744
30
    memcpy(&ntlmbuf[size], domain, domlen);
745
746
97
  size += domlen;
747
748
97
  DEBUGASSERT(size == useroff);
749
97
  if(unicode)
750
67
    unicodecpy(&ntlmbuf[size], user, userlen / 2);
751
30
  else
752
30
    memcpy(&ntlmbuf[size], user, userlen);
753
754
97
  size += userlen;
755
756
97
  DEBUGASSERT(size == hostoff);
757
97
  if(unicode)
758
67
    unicodecpy(&ntlmbuf[size], host, hostlen / 2);
759
30
  else
760
30
    memcpy(&ntlmbuf[size], host, hostlen);
761
762
97
  size += hostlen;
763
764
  /* Return the binary blob. */
765
97
  result = Curl_bufref_memdup(out, ntlmbuf, size);
766
767
97
  Curl_auth_cleanup_ntlm(ntlm);
768
769
97
  return result;
770
97
}
771
772
/*
773
 * Curl_auth_cleanup_ntlm()
774
 *
775
 * This is used to clean up the NTLM specific data.
776
 *
777
 * Parameters:
778
 *
779
 * ntlm    [in/out] - The NTLM data struct being cleaned up.
780
 *
781
 */
782
void Curl_auth_cleanup_ntlm(struct ntlmdata *ntlm)
783
180k
{
784
  /* Free the target info */
785
180k
  Curl_safefree(ntlm->target_info);
786
787
  /* Reset any variables */
788
180k
  ntlm->target_info_len = 0;
789
180k
}
790
791
#endif /* USE_NTLM && !USE_WINDOWS_SSPI */