1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
37 #include "non-ascii.h"
39 #include "curl_base64.h"
40 #include "curl_ntlm_core.h"
41 #include "curl_gethostname.h"
42 #include "curl_multibyte.h"
44 #include "curl_memory.h"
46 #ifdef USE_WINDOWS_SSPI
47 # include "curl_sspi.h"
52 #define BUILDING_CURL_NTLM_MSGS_C
53 #include "curl_ntlm_msgs.h"
55 #define _MPRINTF_REPLACE /* use our functions only */
56 #include <curl/mprintf.h>
58 /* The last #include file should be: */
61 /* "NTLMSSP" signature is always in ASCII regardless of the platform */
62 #define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
64 #define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
65 #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
66 (((x) >> 16) & 0xff), (((x) >> 24) & 0xff)
69 # define DEBUG_OUT(x) x
70 static void ntlm_print_flags(FILE *handle, unsigned long flags)
72 if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
73 fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
74 if(flags & NTLMFLAG_NEGOTIATE_OEM)
75 fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
76 if(flags & NTLMFLAG_REQUEST_TARGET)
77 fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
79 fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
80 if(flags & NTLMFLAG_NEGOTIATE_SIGN)
81 fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
82 if(flags & NTLMFLAG_NEGOTIATE_SEAL)
83 fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
84 if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
85 fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
86 if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
87 fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
88 if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
89 fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
90 if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
91 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
93 fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
94 if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
95 fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
96 if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
97 fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
98 if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
99 fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
100 if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
101 fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
102 if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
103 fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
104 if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
105 fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
106 if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
107 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
108 if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
109 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
110 if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
111 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
112 if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
113 fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
114 if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
115 fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
116 if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
117 fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
118 if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
119 fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
121 fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
123 fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
125 fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
127 fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
129 fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
130 if(flags & NTLMFLAG_NEGOTIATE_128)
131 fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
132 if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
133 fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
134 if(flags & NTLMFLAG_NEGOTIATE_56)
135 fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
138 static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
142 fprintf(stderr, "0x");
144 fprintf(stderr, "%02.2x", (unsigned int)*p++);
147 # define DEBUG_OUT(x) Curl_nop_stmt
150 #ifndef USE_WINDOWS_SSPI
152 * This function converts from the little endian format used in the
153 * incoming package to whatever endian format we're using natively.
154 * Argument is a pointer to a 4 byte buffer.
156 static unsigned int readint_le(unsigned char *buf)
158 return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
159 ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
164 NTLM message structure notes:
166 A 'short' is a 'network short', a little-endian 16-bit unsigned value.
168 A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
170 A 'security buffer' represents a triplet used to point to a buffer,
171 consisting of two shorts and one long:
173 1. A 'short' containing the length of the buffer content in bytes.
174 2. A 'short' containing the allocated space for the buffer in bytes.
175 3. A 'long' containing the offset to the start of the buffer in bytes,
176 from the beginning of the NTLM message.
180 * Curl_ntlm_decode_type2_message()
182 * This is used to decode a ntlm type-2 message received from a HTTP or SASL
183 * based (such as SMTP, POP3 or IMAP) server. The message is first decoded
184 * from a base64 string into a raw ntlm message and checked for validity
185 * before the appropriate data for creating a type-3 message is written to
186 * the given ntlm data structure.
190 * data [in] - Pointer to session handle.
191 * header [in] - Pointer to the input buffer.
192 * ntlm [in] - Pointer to ntlm data struct being used and modified.
194 * Returns CURLE_OK on success.
196 CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
198 struct ntlmdata* ntlm)
200 #ifndef USE_WINDOWS_SSPI
201 static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
204 /* NTLM type-2 message structure:
206 Index Description Content
207 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
209 8 NTLM Message Type long (0x02000000)
210 12 Target Name security buffer
213 (32) Context 8 bytes (two consecutive longs) (*)
214 (40) Target Information security buffer (*)
215 (48) OS Version Structure 8 bytes (*)
216 32 (48) (56) Start of data block (*)
221 unsigned char *buffer = NULL;
224 #if defined(CURL_DISABLE_VERBOSE_STRINGS) || defined(USE_WINDOWS_SSPI)
228 error = Curl_base64_decode(header, &buffer, &size);
233 infof(data, "NTLM handshake failure (unhandled condition)\n");
234 return CURLE_REMOTE_ACCESS_DENIED;
237 #ifdef USE_WINDOWS_SSPI
238 ntlm->type_2 = malloc(size + 1);
239 if(ntlm->type_2 == NULL) {
241 return CURLE_OUT_OF_MEMORY;
243 ntlm->n_type_2 = curlx_uztoul(size);
244 memcpy(ntlm->type_2, buffer, size);
249 (memcmp(buffer, NTLMSSP_SIGNATURE, 8) != 0) ||
250 (memcmp(buffer + 8, type2_marker, sizeof(type2_marker)) != 0)) {
251 /* This was not a good enough type-2 message */
253 infof(data, "NTLM handshake failure (bad type-2 message)\n");
254 return CURLE_REMOTE_ACCESS_DENIED;
257 ntlm->flags = readint_le(&buffer[20]);
258 memcpy(ntlm->nonce, &buffer[24], 8);
261 fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
262 ntlm_print_flags(stderr, ntlm->flags);
263 fprintf(stderr, "\n nonce=");
264 ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
265 fprintf(stderr, "\n****\n");
266 fprintf(stderr, "**** Header %s\n ", header);
274 #ifdef USE_WINDOWS_SSPI
275 void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
277 Curl_safefree(ntlm->type_2);
278 if(ntlm->has_handles) {
279 s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
280 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
281 ntlm->has_handles = 0;
283 if(ntlm->p_identity) {
284 Curl_safefree(ntlm->identity.User);
285 Curl_safefree(ntlm->identity.Password);
286 Curl_safefree(ntlm->identity.Domain);
287 ntlm->p_identity = NULL;
292 #ifndef USE_WINDOWS_SSPI
293 /* copy the source to the destination and fill in zeroes in every
294 other destination byte! */
295 static void unicodecpy(unsigned char *dest,
296 const char *src, size_t length)
299 for(i = 0; i < length; i++) {
300 dest[2 * i] = (unsigned char)src[i];
301 dest[2 * i + 1] = '\0';
307 * Curl_ntlm_create_type1_message()
309 * This is used to generate an already encoded NTLM type-1 message ready for
310 * sending to the recipient, be it a HTTP or SASL based (such as SMTP, POP3
311 * or IMAP) server, using the appropriate compile time crypo API.
315 * userp [in] - The user name in the format User or Domain\User.
316 * passdwp [in] - The user's password.
317 * ntlm [in/out] - The ntlm data struct being used and modified.
318 * outptr [in/out] - The address where a pointer to newly allocated memory
319 * holding the result will be stored upon completion.
320 * outlen [out] - The length of the output message.
322 * Returns CURLE_OK on success.
324 CURLcode Curl_ntlm_create_type1_message(const char *userp,
326 struct ntlmdata *ntlm,
330 /* NTLM type-1 message structure:
332 Index Description Content
333 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
335 8 NTLM Message Type long (0x01000000)
337 (16) Supplied Domain security buffer (*)
338 (24) Supplied Workstation security buffer (*)
339 (32) OS Version Structure 8 bytes (*)
340 (32) (40) Start of data block (*)
344 unsigned char ntlmbuf[NTLM_BUFSIZE];
347 #ifdef USE_WINDOWS_SSPI
351 SECURITY_STATUS status;
353 xcharp_u useranddomain;
354 xcharp_u user, dup_user;
355 xcharp_u domain, dup_domain;
356 xcharp_u passwd, dup_passwd;
358 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
360 domain.const_tchar_ptr = TEXT("");
362 Curl_ntlm_sspi_cleanup(ntlm);
364 if(userp && *userp) {
366 /* null initialize ntlm identity's data to allow proper cleanup */
367 ntlm->p_identity = &ntlm->identity;
368 memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));
370 useranddomain.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)userp);
371 if(!useranddomain.tchar_ptr)
372 return CURLE_OUT_OF_MEMORY;
374 user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('\\'));
375 if(!user.const_tchar_ptr)
376 user.const_tchar_ptr = _tcschr(useranddomain.const_tchar_ptr, TEXT('/'));
379 domain.tchar_ptr = useranddomain.tchar_ptr;
380 domlen = user.tchar_ptr - useranddomain.tchar_ptr;
384 user.tchar_ptr = useranddomain.tchar_ptr;
385 domain.const_tchar_ptr = TEXT("");
389 /* setup ntlm identity's user and length */
390 dup_user.tchar_ptr = _tcsdup(user.tchar_ptr);
391 if(!dup_user.tchar_ptr) {
392 Curl_unicodefree(useranddomain.tchar_ptr);
393 return CURLE_OUT_OF_MEMORY;
395 ntlm->identity.User = dup_user.tbyte_ptr;
396 ntlm->identity.UserLength = curlx_uztoul(_tcslen(dup_user.tchar_ptr));
397 dup_user.tchar_ptr = NULL;
399 /* setup ntlm identity's domain and length */
400 dup_domain.tchar_ptr = malloc(sizeof(TCHAR) * (domlen + 1));
401 if(!dup_domain.tchar_ptr) {
402 Curl_unicodefree(useranddomain.tchar_ptr);
403 return CURLE_OUT_OF_MEMORY;
405 _tcsncpy(dup_domain.tchar_ptr, domain.tchar_ptr, domlen);
406 *(dup_domain.tchar_ptr + domlen) = TEXT('\0');
407 ntlm->identity.Domain = dup_domain.tbyte_ptr;
408 ntlm->identity.DomainLength = curlx_uztoul(domlen);
409 free(dup_domain.tchar_ptr);
410 dup_domain.tchar_ptr = NULL;
412 Curl_unicodefree(useranddomain.tchar_ptr);
414 /* setup ntlm identity's password and length */
415 passwd.tchar_ptr = Curl_convert_UTF8_to_tchar((char *)passwdp);
416 if(!passwd.tchar_ptr)
417 return CURLE_OUT_OF_MEMORY;
418 dup_passwd.tchar_ptr = _tcsdup(passwd.tchar_ptr);
419 if(!dup_passwd.tchar_ptr) {
420 Curl_unicodefree(passwd.tchar_ptr);
421 return CURLE_OUT_OF_MEMORY;
423 ntlm->identity.Password = dup_passwd.tbyte_ptr;
424 ntlm->identity.PasswordLength =
425 curlx_uztoul(_tcslen(dup_passwd.tchar_ptr));
426 dup_passwd.tchar_ptr = NULL;
428 Curl_unicodefree(passwd.tchar_ptr);
430 /* setup ntlm identity's flags */
431 ntlm->identity.Flags = SECFLAG_WINNT_AUTH_IDENTITY;
434 ntlm->p_identity = NULL;
436 status = s_pSecFn->AcquireCredentialsHandle(NULL,
437 (TCHAR *) TEXT("NTLM"),
438 SECPKG_CRED_OUTBOUND, NULL,
439 ntlm->p_identity, NULL, NULL,
440 &ntlm->handle, &tsDummy);
441 if(status != SEC_E_OK)
442 return CURLE_OUT_OF_MEMORY;
444 desc.ulVersion = SECBUFFER_VERSION;
446 desc.pBuffers = &buf;
447 buf.cbBuffer = NTLM_BUFSIZE;
448 buf.BufferType = SECBUFFER_TOKEN;
449 buf.pvBuffer = ntlmbuf;
451 status = s_pSecFn->InitializeSecurityContext(&ntlm->handle, NULL,
453 ISC_REQ_CONFIDENTIALITY |
454 ISC_REQ_REPLAY_DETECT |
456 0, SECURITY_NETWORK_DREP,
458 &ntlm->c_handle, &desc,
461 if(status == SEC_I_COMPLETE_AND_CONTINUE ||
462 status == SEC_I_CONTINUE_NEEDED)
463 s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &desc);
464 else if(status != SEC_E_OK) {
465 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
466 return CURLE_RECV_ERROR;
469 ntlm->has_handles = 1;
474 const char *host = ""; /* empty */
475 const char *domain = ""; /* empty */
479 size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
486 #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
490 snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
491 NTLMSSP_SIGNATURE "%c"
492 "\x01%c%c%c" /* 32-bit type = 1 */
493 "%c%c%c%c" /* 32-bit NTLM flag field */
494 "%c%c" /* domain length */
495 "%c%c" /* domain allocated space */
496 "%c%c" /* domain name offset */
497 "%c%c" /* 2 zeroes */
498 "%c%c" /* host length */
499 "%c%c" /* host allocated space */
500 "%c%c" /* host name offset */
501 "%c%c" /* 2 zeroes */
503 "%s", /* domain string */
504 0, /* trailing zero */
505 0, 0, 0, /* part of type-1 long */
507 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
508 NTLMFLAG_REQUEST_TARGET |
509 NTLMFLAG_NEGOTIATE_NTLM_KEY |
511 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
520 host, /* this is empty */
521 domain /* this is empty */);
523 /* Initial packet length */
524 size = 32 + hostlen + domlen;
529 fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
531 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
532 NTLMFLAG_REQUEST_TARGET |
533 NTLMFLAG_NEGOTIATE_NTLM_KEY |
535 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
536 NTLMFLAG_NEGOTIATE_OEM |
537 NTLMFLAG_REQUEST_TARGET |
538 NTLMFLAG_NEGOTIATE_NTLM_KEY |
540 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
541 ntlm_print_flags(stderr,
542 NTLMFLAG_NEGOTIATE_OEM |
543 NTLMFLAG_REQUEST_TARGET |
544 NTLMFLAG_NEGOTIATE_NTLM_KEY |
546 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
547 fprintf(stderr, "\n****\n");
550 /* Return with binary blob encoded into base64 */
551 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
555 * Curl_ntlm_create_type3_message()
557 * This is used to generate an already encoded NTLM type-3 message ready for
558 * sending to the recipient, be it a HTTP or SASL based (such as SMTP, POP3
559 * or IMAP) server, using the appropriate compile time crypo API.
563 * data [in] - The session handle.
564 * userp [in] - The user name in the format User or Domain\User.
565 * passdwp [in] - The user's password.
566 * ntlm [in/out] - The ntlm data struct being used and modified.
567 * outptr [in/out] - The address where a pointer to newly allocated memory
568 * holding the result will be stored upon completion.
569 * outlen [out] - The length of the output message.
571 * Returns CURLE_OK on success.
573 CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
576 struct ntlmdata *ntlm,
580 /* NTLM type-3 message structure:
582 Index Description Content
583 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
585 8 NTLM Message Type long (0x03000000)
586 12 LM/LMv2 Response security buffer
587 20 NTLM/NTLMv2 Response security buffer
588 28 Target Name security buffer
589 36 User Name security buffer
590 44 Workstation Name security buffer
591 (52) Session Key security buffer (*)
593 (64) OS Version Structure 8 bytes (*)
594 52 (64) (72) Start of data block
598 unsigned char ntlmbuf[NTLM_BUFSIZE];
601 #ifdef USE_WINDOWS_SSPI
604 SecBufferDesc type_2_desc;
605 SecBufferDesc type_3_desc;
606 SECURITY_STATUS status;
608 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
614 type_2_desc.ulVersion = type_3_desc.ulVersion = SECBUFFER_VERSION;
615 type_2_desc.cBuffers = type_3_desc.cBuffers = 1;
616 type_2_desc.pBuffers = &type_2;
617 type_3_desc.pBuffers = &type_3;
619 type_2.BufferType = SECBUFFER_TOKEN;
620 type_2.pvBuffer = ntlm->type_2;
621 type_2.cbBuffer = ntlm->n_type_2;
622 type_3.BufferType = SECBUFFER_TOKEN;
623 type_3.pvBuffer = ntlmbuf;
624 type_3.cbBuffer = NTLM_BUFSIZE;
626 status = s_pSecFn->InitializeSecurityContext(&ntlm->handle,
629 ISC_REQ_CONFIDENTIALITY |
630 ISC_REQ_REPLAY_DETECT |
632 0, SECURITY_NETWORK_DREP,
637 if(status != SEC_E_OK)
638 return CURLE_RECV_ERROR;
640 size = type_3.cbBuffer;
642 Curl_ntlm_sspi_cleanup(ntlm);
646 unsigned char lmresp[24]; /* fixed-size */
649 unsigned char ntresp[24]; /* fixed-size */
651 bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
652 char host[HOSTNAME_MAX + 1] = "";
654 const char *domain = "";
663 user = strchr(userp, '\\');
665 user = strchr(userp, '/');
669 domlen = (user - domain);
676 userlen = strlen(user);
678 /* Get the machine's un-qualified host name as NTLM doesn't like the fully
679 qualified domain name */
680 if(Curl_gethostname(host, sizeof(host))) {
681 infof(data, "gethostname() failed, continuing without!\n");
685 hostlen = strlen(host);
690 userlen = userlen * 2;
691 hostlen = hostlen * 2;
695 /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
696 if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
697 unsigned char ntbuffer[0x18];
698 unsigned char tmp[0x18];
699 unsigned char md5sum[MD5_DIGEST_LENGTH];
700 unsigned char entropy[8];
702 /* Need to create 8 bytes random data */
703 Curl_ssl_random(data, entropy, sizeof(entropy));
705 /* 8 bytes random data as challenge in lmresp */
706 memcpy(lmresp, entropy, 8);
709 memset(lmresp + 8, 0, 0x10);
711 /* Fill tmp with challenge(nonce?) + entropy */
712 memcpy(tmp, &ntlm->nonce[0], 8);
713 memcpy(tmp + 8, entropy, 8);
715 Curl_ssl_md5sum(tmp, 16, md5sum, MD5_DIGEST_LENGTH);
717 /* We shall only use the first 8 bytes of md5sum, but the des
718 code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
719 if(CURLE_OUT_OF_MEMORY ==
720 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
721 return CURLE_OUT_OF_MEMORY;
722 Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
724 /* End of NTLM2 Session code */
731 unsigned char ntbuffer[0x18];
733 unsigned char lmbuffer[0x18];
736 if(CURLE_OUT_OF_MEMORY ==
737 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
738 return CURLE_OUT_OF_MEMORY;
739 Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
742 Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
743 Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
744 /* A safer but less compatible alternative is:
745 * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
746 * See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
749 lmrespoff = 64; /* size of the message header */
751 ntrespoff = lmrespoff + 0x18;
752 domoff = ntrespoff + 0x18;
754 domoff = lmrespoff + 0x18;
756 useroff = domoff + domlen;
757 hostoff = useroff + userlen;
759 /* Create the big type-3 message binary blob */
760 size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
761 NTLMSSP_SIGNATURE "%c"
762 "\x03%c%c%c" /* 32-bit type = 3 */
764 "%c%c" /* LanManager length */
765 "%c%c" /* LanManager allocated space */
766 "%c%c" /* LanManager offset */
767 "%c%c" /* 2 zeroes */
769 "%c%c" /* NT-response length */
770 "%c%c" /* NT-response allocated space */
771 "%c%c" /* NT-response offset */
772 "%c%c" /* 2 zeroes */
774 "%c%c" /* domain length */
775 "%c%c" /* domain allocated space */
776 "%c%c" /* domain name offset */
777 "%c%c" /* 2 zeroes */
779 "%c%c" /* user length */
780 "%c%c" /* user allocated space */
781 "%c%c" /* user offset */
782 "%c%c" /* 2 zeroes */
784 "%c%c" /* host length */
785 "%c%c" /* host allocated space */
786 "%c%c" /* host offset */
787 "%c%c" /* 2 zeroes */
789 "%c%c" /* session key length (unknown purpose) */
790 "%c%c" /* session key allocated space (unknown purpose) */
791 "%c%c" /* session key offset (unknown purpose) */
792 "%c%c" /* 2 zeroes */
794 "%c%c%c%c", /* flags */
799 /* LanManager response */
802 0, /* zero termination */
803 0, 0, 0, /* type-3 long, the 24 upper bits */
805 SHORTPAIR(0x18), /* LanManager response length, twice */
807 SHORTPAIR(lmrespoff),
811 SHORTPAIR(0x18), /* NT-response length, twice */
813 SHORTPAIR(ntrespoff),
841 LONGQUARTET(ntlm->flags));
843 DEBUGASSERT(size == 64);
844 DEBUGASSERT(size == (size_t)lmrespoff);
846 /* We append the binary hashes */
847 if(size < (NTLM_BUFSIZE - 0x18)) {
848 memcpy(&ntlmbuf[size], lmresp, 0x18);
853 fprintf(stderr, "**** TYPE3 header lmresp=");
854 ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
858 if(size < (NTLM_BUFSIZE - 0x18)) {
859 DEBUGASSERT(size == (size_t)ntrespoff);
860 memcpy(&ntlmbuf[size], ntresp, 0x18);
865 fprintf(stderr, "\n ntresp=");
866 ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], 0x18);
872 fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
873 LONGQUARTET(ntlm->flags), ntlm->flags);
874 ntlm_print_flags(stderr, ntlm->flags);
875 fprintf(stderr, "\n****\n");
878 /* Make sure that the domain, user and host strings fit in the
879 buffer before we copy them there. */
880 if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
881 failf(data, "user + domain + host name too big");
882 return CURLE_OUT_OF_MEMORY;
885 DEBUGASSERT(size == domoff);
887 unicodecpy(&ntlmbuf[size], domain, domlen / 2);
889 memcpy(&ntlmbuf[size], domain, domlen);
893 DEBUGASSERT(size == useroff);
895 unicodecpy(&ntlmbuf[size], user, userlen / 2);
897 memcpy(&ntlmbuf[size], user, userlen);
901 DEBUGASSERT(size == hostoff);
903 unicodecpy(&ntlmbuf[size], host, hostlen / 2);
905 memcpy(&ntlmbuf[size], host, hostlen);
909 /* Convert domain, user, and host to ASCII but leave the rest as-is */
910 res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
913 return CURLE_CONV_FAILED;
917 /* Return with binary blob encoded into base64 */
918 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
921 #endif /* USE_NTLM */