1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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"
50 #include "vtls/vtls.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);
163 * This function converts from the little endian format used in the incoming
164 * package to whatever endian format we're using natively. Argument is a
165 * pointer to a 2 byte buffer.
167 static unsigned int readshort_le(unsigned char *buf)
169 return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8);
173 * Curl_ntlm_decode_type2_target()
175 * This is used to decode the "target info" in the ntlm type-2 message
180 * data [in] - Pointer to the session handle
181 * buffer [in] - The decoded base64 ntlm header of Type 2
182 * size [in] - The input buffer size, atleast 32 bytes
183 * ntlm [in] - Pointer to ntlm data struct being used and modified.
185 * Returns CURLE_OK on success.
187 CURLcode Curl_ntlm_decode_type2_target(struct SessionHandle *data,
188 unsigned char *buffer,
190 struct ntlmdata *ntlm)
192 unsigned int target_info_len = 0;
193 unsigned int target_info_offset = 0;
195 Curl_safefree(ntlm->target_info);
196 ntlm->target_info_len = 0;
199 target_info_len = readshort_le(&buffer[40]);
200 target_info_offset = readint_le(&buffer[44]);
201 if(target_info_len > 0) {
202 if(((target_info_offset + target_info_len) > size) ||
203 (target_info_offset < 48)) {
204 infof(data, "NTLM handshake failure (bad type-2 message). "
205 "Target Info Offset Len is set incorrect by the peer\n");
206 return CURLE_REMOTE_ACCESS_DENIED;
209 ntlm->target_info = malloc(target_info_len);
210 if(!ntlm->target_info)
211 return CURLE_OUT_OF_MEMORY;
213 memcpy(ntlm->target_info, &buffer[target_info_offset], target_info_len);
214 ntlm->target_info_len = target_info_len;
226 NTLM message structure notes:
228 A 'short' is a 'network short', a little-endian 16-bit unsigned value.
230 A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
232 A 'security buffer' represents a triplet used to point to a buffer,
233 consisting of two shorts and one long:
235 1. A 'short' containing the length of the buffer content in bytes.
236 2. A 'short' containing the allocated space for the buffer in bytes.
237 3. A 'long' containing the offset to the start of the buffer in bytes,
238 from the beginning of the NTLM message.
242 * Curl_ntlm_decode_type2_message()
244 * This is used to decode a ntlm type-2 message received from a HTTP or SASL
245 * based (such as SMTP, POP3 or IMAP) server. The message is first decoded
246 * from a base64 string into a raw ntlm message and checked for validity
247 * before the appropriate data for creating a type-3 message is written to
248 * the given ntlm data structure.
252 * data [in] - Pointer to session handle.
253 * header [in] - Pointer to the input buffer.
254 * ntlm [in] - Pointer to ntlm data struct being used and modified.
256 * Returns CURLE_OK on success.
258 CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
260 struct ntlmdata *ntlm)
262 #ifndef USE_WINDOWS_SSPI
263 static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
266 /* NTLM type-2 message structure:
268 Index Description Content
269 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
271 8 NTLM Message Type long (0x02000000)
272 12 Target Name security buffer
275 (32) Context 8 bytes (two consecutive longs) (*)
276 (40) Target Information security buffer (*)
277 (48) OS Version Structure 8 bytes (*)
278 32 (48) (56) Start of data block (*)
283 unsigned char *buffer = NULL;
286 #if defined(CURL_DISABLE_VERBOSE_STRINGS) || defined(USE_WINDOWS_SSPI)
290 error = Curl_base64_decode(header, &buffer, &size);
295 infof(data, "NTLM handshake failure (unhandled condition)\n");
296 return CURLE_REMOTE_ACCESS_DENIED;
299 #ifdef USE_WINDOWS_SSPI
300 ntlm->type_2 = malloc(size + 1);
301 if(ntlm->type_2 == NULL) {
303 return CURLE_OUT_OF_MEMORY;
305 ntlm->n_type_2 = curlx_uztoul(size);
306 memcpy(ntlm->type_2, buffer, size);
311 (memcmp(buffer, NTLMSSP_SIGNATURE, 8) != 0) ||
312 (memcmp(buffer + 8, type2_marker, sizeof(type2_marker)) != 0)) {
313 /* This was not a good enough type-2 message */
315 infof(data, "NTLM handshake failure (bad type-2 message)\n");
316 return CURLE_REMOTE_ACCESS_DENIED;
319 ntlm->flags = readint_le(&buffer[20]);
320 memcpy(ntlm->nonce, &buffer[24], 8);
322 if(ntlm->flags & NTLMFLAG_NEGOTIATE_TARGET_INFO) {
323 error = Curl_ntlm_decode_type2_target(data, buffer, size, ntlm);
326 infof(data, "NTLM handshake failure (bad type-2 message)\n");
332 fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
333 ntlm_print_flags(stderr, ntlm->flags);
334 fprintf(stderr, "\n nonce=");
335 ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
336 fprintf(stderr, "\n****\n");
337 fprintf(stderr, "**** Header %s\n ", header);
345 #ifdef USE_WINDOWS_SSPI
346 void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
348 Curl_safefree(ntlm->type_2);
350 if(ntlm->has_handles) {
351 s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
352 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
353 ntlm->has_handles = 0;
356 Curl_sspi_free_identity(ntlm->p_identity);
357 ntlm->p_identity = NULL;
361 #ifndef USE_WINDOWS_SSPI
362 /* copy the source to the destination and fill in zeroes in every
363 other destination byte! */
364 static void unicodecpy(unsigned char *dest, const char *src, size_t length)
367 for(i = 0; i < length; i++) {
368 dest[2 * i] = (unsigned char)src[i];
369 dest[2 * i + 1] = '\0';
375 * Curl_ntlm_create_type1_message()
377 * This is used to generate an already encoded NTLM type-1 message ready for
378 * sending to the recipient, be it a HTTP or SASL based (such as SMTP, POP3
379 * or IMAP) server, using the appropriate compile time crypo API.
383 * userp [in] - The user name in the format User or Domain\User.
384 * passdwp [in] - The user's password.
385 * ntlm [in/out] - The ntlm data struct being used and modified.
386 * outptr [in/out] - The address where a pointer to newly allocated memory
387 * holding the result will be stored upon completion.
388 * outlen [out] - The length of the output message.
390 * Returns CURLE_OK on success.
392 CURLcode Curl_ntlm_create_type1_message(const char *userp,
394 struct ntlmdata *ntlm,
398 /* NTLM type-1 message structure:
400 Index Description Content
401 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
403 8 NTLM Message Type long (0x01000000)
405 (16) Supplied Domain security buffer (*)
406 (24) Supplied Workstation security buffer (*)
407 (32) OS Version Structure 8 bytes (*)
408 (32) (40) Start of data block (*)
412 unsigned char ntlmbuf[NTLM_BUFSIZE];
415 #ifdef USE_WINDOWS_SSPI
417 SecBuffer type_1_buf;
418 SecBufferDesc type_1_desc;
419 SECURITY_STATUS status;
421 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
423 Curl_ntlm_sspi_cleanup(ntlm);
425 if(userp && *userp) {
428 /* Populate our identity structure */
429 result = Curl_create_sspi_identity(userp, passwdp, &ntlm->identity);
433 /* Allow proper cleanup of the identity structure */
434 ntlm->p_identity = &ntlm->identity;
437 /* Use the current Windows user */
438 ntlm->p_identity = NULL;
440 /* Acquire our credientials handle */
441 status = s_pSecFn->AcquireCredentialsHandle(NULL,
442 (TCHAR *) TEXT("NTLM"),
443 SECPKG_CRED_OUTBOUND, NULL,
444 ntlm->p_identity, NULL, NULL,
445 &ntlm->handle, &tsDummy);
446 if(status != SEC_E_OK)
447 return CURLE_OUT_OF_MEMORY;
449 /* Setup the type-1 "output" security buffer */
450 type_1_desc.ulVersion = SECBUFFER_VERSION;
451 type_1_desc.cBuffers = 1;
452 type_1_desc.pBuffers = &type_1_buf;
453 type_1_buf.cbBuffer = NTLM_BUFSIZE;
454 type_1_buf.BufferType = SECBUFFER_TOKEN;
455 type_1_buf.pvBuffer = ntlmbuf;
457 /* Generate our type-1 message */
458 status = s_pSecFn->InitializeSecurityContext(&ntlm->handle, NULL,
460 ISC_REQ_CONFIDENTIALITY |
461 ISC_REQ_REPLAY_DETECT |
463 0, SECURITY_NETWORK_DREP,
465 &ntlm->c_handle, &type_1_desc,
468 if(status == SEC_I_COMPLETE_AND_CONTINUE ||
469 status == SEC_I_CONTINUE_NEEDED)
470 s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &type_1_desc);
471 else if(status != SEC_E_OK) {
472 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
473 return CURLE_RECV_ERROR;
476 ntlm->has_handles = 1;
477 size = type_1_buf.cbBuffer;
481 const char *host = ""; /* empty */
482 const char *domain = ""; /* empty */
486 size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
493 #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
497 snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
498 NTLMSSP_SIGNATURE "%c"
499 "\x01%c%c%c" /* 32-bit type = 1 */
500 "%c%c%c%c" /* 32-bit NTLM flag field */
501 "%c%c" /* domain length */
502 "%c%c" /* domain allocated space */
503 "%c%c" /* domain name offset */
504 "%c%c" /* 2 zeroes */
505 "%c%c" /* host length */
506 "%c%c" /* host allocated space */
507 "%c%c" /* host name offset */
508 "%c%c" /* 2 zeroes */
510 "%s", /* domain string */
511 0, /* trailing zero */
512 0, 0, 0, /* part of type-1 long */
514 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
515 NTLMFLAG_REQUEST_TARGET |
516 NTLMFLAG_NEGOTIATE_NTLM_KEY |
518 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
527 host, /* this is empty */
528 domain /* this is empty */);
530 /* Initial packet length */
531 size = 32 + hostlen + domlen;
536 fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
538 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
539 NTLMFLAG_REQUEST_TARGET |
540 NTLMFLAG_NEGOTIATE_NTLM_KEY |
542 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
543 NTLMFLAG_NEGOTIATE_OEM |
544 NTLMFLAG_REQUEST_TARGET |
545 NTLMFLAG_NEGOTIATE_NTLM_KEY |
547 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
548 ntlm_print_flags(stderr,
549 NTLMFLAG_NEGOTIATE_OEM |
550 NTLMFLAG_REQUEST_TARGET |
551 NTLMFLAG_NEGOTIATE_NTLM_KEY |
553 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
554 fprintf(stderr, "\n****\n");
557 /* Return with binary blob encoded into base64 */
558 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
562 * Curl_ntlm_create_type3_message()
564 * This is used to generate an already encoded NTLM type-3 message ready for
565 * sending to the recipient, be it a HTTP or SASL based (such as SMTP, POP3
566 * or IMAP) server, using the appropriate compile time crypo API.
570 * data [in] - The session handle.
571 * userp [in] - The user name in the format User or Domain\User.
572 * passdwp [in] - The user's password.
573 * ntlm [in/out] - The ntlm data struct being used and modified.
574 * outptr [in/out] - The address where a pointer to newly allocated memory
575 * holding the result will be stored upon completion.
576 * outlen [out] - The length of the output message.
578 * Returns CURLE_OK on success.
580 CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
583 struct ntlmdata *ntlm,
587 /* NTLM type-3 message structure:
589 Index Description Content
590 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
592 8 NTLM Message Type long (0x03000000)
593 12 LM/LMv2 Response security buffer
594 20 NTLM/NTLMv2 Response security buffer
595 28 Target Name security buffer
596 36 User Name security buffer
597 44 Workstation Name security buffer
598 (52) Session Key security buffer (*)
600 (64) OS Version Structure 8 bytes (*)
601 52 (64) (72) Start of data block
605 unsigned char ntlmbuf[NTLM_BUFSIZE];
608 #ifdef USE_WINDOWS_SSPI
609 SecBuffer type_2_buf;
610 SecBuffer type_3_buf;
611 SecBufferDesc type_2_desc;
612 SecBufferDesc type_3_desc;
613 SECURITY_STATUS status;
615 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
621 /* Setup the type-2 "input" security buffer */
622 type_2_desc.ulVersion = SECBUFFER_VERSION;
623 type_2_desc.cBuffers = 1;
624 type_2_desc.pBuffers = &type_2_buf;
625 type_2_buf.BufferType = SECBUFFER_TOKEN;
626 type_2_buf.pvBuffer = ntlm->type_2;
627 type_2_buf.cbBuffer = ntlm->n_type_2;
629 /* Setup the type-3 "output" security buffer */
630 type_3_desc.ulVersion = SECBUFFER_VERSION;
631 type_3_desc.cBuffers = 1;
632 type_3_desc.pBuffers = &type_3_buf;
633 type_3_buf.BufferType = SECBUFFER_TOKEN;
634 type_3_buf.pvBuffer = ntlmbuf;
635 type_3_buf.cbBuffer = NTLM_BUFSIZE;
637 /* Generate our type-3 message */
638 status = s_pSecFn->InitializeSecurityContext(&ntlm->handle,
641 ISC_REQ_CONFIDENTIALITY |
642 ISC_REQ_REPLAY_DETECT |
644 0, SECURITY_NETWORK_DREP,
649 if(status != SEC_E_OK)
650 return CURLE_RECV_ERROR;
652 size = type_3_buf.cbBuffer;
654 Curl_ntlm_sspi_cleanup(ntlm);
658 unsigned char lmresp[24]; /* fixed-size */
661 unsigned int ntresplen = 24;
662 unsigned char ntresp[24]; /* fixed-size */
663 unsigned char *ptr_ntresp = &ntresp[0];
664 unsigned char *ntlmv2resp = NULL;
666 bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
667 char host[HOSTNAME_MAX + 1] = "";
669 const char *domain = "";
676 CURLcode res = CURLE_OK;
678 user = strchr(userp, '\\');
680 user = strchr(userp, '/');
684 domlen = (user - domain);
691 userlen = strlen(user);
693 /* Get the machine's un-qualified host name as NTLM doesn't like the fully
694 qualified domain name */
695 if(Curl_gethostname(host, sizeof(host))) {
696 infof(data, "gethostname() failed, continuing without!\n");
700 hostlen = strlen(host);
704 if(ntlm->target_info_len) {
705 unsigned char ntbuffer[0x18];
706 unsigned char entropy[8];
707 unsigned char ntlmv2hash[0x18];
709 #if defined(DEBUGBUILD)
710 /* Use static client nonce in debug (Test Suite) builds */
711 memcpy(entropy, "12345678", sizeof(entropy));
713 /* Create an 8 byte random client nonce */
714 Curl_ssl_random(data, entropy, sizeof(entropy));
717 res = Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer);
721 res = Curl_ntlm_core_mk_ntlmv2_hash(user, userlen, domain, domlen,
722 ntbuffer, ntlmv2hash);
727 res = Curl_ntlm_core_mk_lmv2_resp(ntlmv2hash, entropy, &ntlm->nonce[0],
732 /* NTLMv2 response */
733 res = Curl_ntlm_core_mk_ntlmv2_resp(ntlmv2hash, entropy, ntlm, &ntlmv2resp,
738 ptr_ntresp = ntlmv2resp;
744 /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
745 if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
746 unsigned char ntbuffer[0x18];
747 unsigned char tmp[0x18];
748 unsigned char md5sum[MD5_DIGEST_LENGTH];
749 unsigned char entropy[8];
751 /* Need to create 8 bytes random data */
752 Curl_ssl_random(data, entropy, sizeof(entropy));
754 /* 8 bytes random data as challenge in lmresp */
755 memcpy(lmresp, entropy, 8);
758 memset(lmresp + 8, 0, 0x10);
760 /* Fill tmp with challenge(nonce?) + entropy */
761 memcpy(tmp, &ntlm->nonce[0], 8);
762 memcpy(tmp + 8, entropy, 8);
764 Curl_ssl_md5sum(tmp, 16, md5sum, MD5_DIGEST_LENGTH);
766 /* We shall only use the first 8 bytes of md5sum, but the des
767 code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
768 if(CURLE_OUT_OF_MEMORY ==
769 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
770 return CURLE_OUT_OF_MEMORY;
772 Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
774 /* End of NTLM2 Session code */
782 unsigned char ntbuffer[0x18];
784 unsigned char lmbuffer[0x18];
787 if(CURLE_OUT_OF_MEMORY ==
788 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
789 return CURLE_OUT_OF_MEMORY;
790 Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
793 Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
794 Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
795 /* A safer but less compatible alternative is:
796 * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
797 * See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
802 userlen = userlen * 2;
803 hostlen = hostlen * 2;
806 lmrespoff = 64; /* size of the message header */
808 ntrespoff = lmrespoff + 0x18;
809 domoff = ntrespoff + ntresplen;
811 domoff = lmrespoff + 0x18;
813 useroff = domoff + domlen;
814 hostoff = useroff + userlen;
816 /* Create the big type-3 message binary blob */
817 size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
818 NTLMSSP_SIGNATURE "%c"
819 "\x03%c%c%c" /* 32-bit type = 3 */
821 "%c%c" /* LanManager length */
822 "%c%c" /* LanManager allocated space */
823 "%c%c" /* LanManager offset */
824 "%c%c" /* 2 zeroes */
826 "%c%c" /* NT-response length */
827 "%c%c" /* NT-response allocated space */
828 "%c%c" /* NT-response offset */
829 "%c%c" /* 2 zeroes */
831 "%c%c" /* domain length */
832 "%c%c" /* domain allocated space */
833 "%c%c" /* domain name offset */
834 "%c%c" /* 2 zeroes */
836 "%c%c" /* user length */
837 "%c%c" /* user allocated space */
838 "%c%c" /* user offset */
839 "%c%c" /* 2 zeroes */
841 "%c%c" /* host length */
842 "%c%c" /* host allocated space */
843 "%c%c" /* host offset */
844 "%c%c" /* 2 zeroes */
846 "%c%c" /* session key length (unknown purpose) */
847 "%c%c" /* session key allocated space (unknown purpose) */
848 "%c%c" /* session key offset (unknown purpose) */
849 "%c%c" /* 2 zeroes */
851 "%c%c%c%c", /* flags */
856 /* LanManager response */
859 0, /* zero termination */
860 0, 0, 0, /* type-3 long, the 24 upper bits */
862 SHORTPAIR(0x18), /* LanManager response length, twice */
864 SHORTPAIR(lmrespoff),
868 SHORTPAIR(ntresplen), /* NT-response length, twice */
869 SHORTPAIR(ntresplen),
870 SHORTPAIR(ntrespoff),
898 LONGQUARTET(ntlm->flags));
900 DEBUGASSERT(size == 64);
901 DEBUGASSERT(size == (size_t)lmrespoff);
903 /* We append the binary hashes */
904 if(size < (NTLM_BUFSIZE - 0x18)) {
905 memcpy(&ntlmbuf[size], lmresp, 0x18);
910 fprintf(stderr, "**** TYPE3 header lmresp=");
911 ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
915 if(size < (NTLM_BUFSIZE - ntresplen)) {
916 DEBUGASSERT(size == (size_t)ntrespoff);
917 memcpy(&ntlmbuf[size], ptr_ntresp, ntresplen);
922 fprintf(stderr, "\n ntresp=");
923 ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], ntresplen);
926 Curl_safefree(ntlmv2resp);/* Free the dynamic buffer allocated for NTLMv2 */
931 fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
932 LONGQUARTET(ntlm->flags), ntlm->flags);
933 ntlm_print_flags(stderr, ntlm->flags);
934 fprintf(stderr, "\n****\n");
937 /* Make sure that the domain, user and host strings fit in the
938 buffer before we copy them there. */
939 if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
940 failf(data, "user + domain + host name too big");
941 return CURLE_OUT_OF_MEMORY;
944 DEBUGASSERT(size == domoff);
946 unicodecpy(&ntlmbuf[size], domain, domlen / 2);
948 memcpy(&ntlmbuf[size], domain, domlen);
952 DEBUGASSERT(size == useroff);
954 unicodecpy(&ntlmbuf[size], user, userlen / 2);
956 memcpy(&ntlmbuf[size], user, userlen);
960 DEBUGASSERT(size == hostoff);
962 unicodecpy(&ntlmbuf[size], host, hostlen / 2);
964 memcpy(&ntlmbuf[size], host, hostlen);
968 /* Convert domain, user, and host to ASCII but leave the rest as-is */
969 res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
972 return CURLE_CONV_FAILED;
976 /* Return with binary blob encoded into base64 */
977 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
980 #endif /* USE_NTLM */