1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
39 # include <openssl/des.h>
40 # ifndef OPENSSL_NO_MD4
41 # include <openssl/md4.h>
43 # include <openssl/md5.h>
44 # include <openssl/ssl.h>
45 # include <openssl/rand.h>
48 # ifndef OPENSSL_NO_MD4
57 #elif defined(USE_GNUTLS)
61 # define MD5_DIGEST_LENGTH 16
62 # define MD4_DIGEST_LENGTH 16
64 #elif defined(USE_NSS)
70 # include "curl_md4.h"
71 # define MD5_DIGEST_LENGTH MD5_LENGTH
73 #elif defined(USE_WINDOWS_SSPI)
74 # include "curl_sspi.h"
76 # error "Can't compile NTLM support without a crypto library."
80 #include "non-ascii.h"
82 #include "curl_base64.h"
83 #include "curl_ntlm_core.h"
84 #include "curl_gethostname.h"
85 #include "curl_memory.h"
87 #define BUILDING_CURL_NTLM_MSGS_C
88 #include "curl_ntlm_msgs.h"
90 #define _MPRINTF_REPLACE /* use our functions only */
91 #include <curl/mprintf.h>
93 /* The last #include file should be: */
96 /* "NTLMSSP" signature is always in ASCII regardless of the platform */
97 #define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
99 #define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
100 #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
101 (((x) >> 16) & 0xff), (((x) >> 24) & 0xff)
104 # define DEBUG_OUT(x) x
105 static void ntlm_print_flags(FILE *handle, unsigned long flags)
107 if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
108 fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
109 if(flags & NTLMFLAG_NEGOTIATE_OEM)
110 fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
111 if(flags & NTLMFLAG_REQUEST_TARGET)
112 fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
114 fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
115 if(flags & NTLMFLAG_NEGOTIATE_SIGN)
116 fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
117 if(flags & NTLMFLAG_NEGOTIATE_SEAL)
118 fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
119 if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
120 fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
121 if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
122 fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
123 if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
124 fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
125 if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
126 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
128 fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
129 if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
130 fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
131 if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
132 fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
133 if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
134 fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
135 if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
136 fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
137 if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
138 fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
139 if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
140 fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
141 if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
142 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
143 if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
144 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
145 if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
146 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
147 if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
148 fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
149 if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
150 fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
151 if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
152 fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
153 if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
154 fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
156 fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
158 fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
160 fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
162 fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
164 fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
165 if(flags & NTLMFLAG_NEGOTIATE_128)
166 fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
167 if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
168 fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
169 if(flags & NTLMFLAG_NEGOTIATE_56)
170 fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
173 static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
177 fprintf(stderr, "0x");
179 fprintf(stderr, "%02.2x", (unsigned int)*p++);
182 # define DEBUG_OUT(x) Curl_nop_stmt
185 #ifndef USE_WINDOWS_SSPI
187 * This function converts from the little endian format used in the
188 * incoming package to whatever endian format we're using natively.
189 * Argument is a pointer to a 4 byte buffer.
191 static unsigned int readint_le(unsigned char *buf)
193 return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
194 ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
199 NTLM message structure notes:
201 A 'short' is a 'network short', a little-endian 16-bit unsigned value.
203 A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
205 A 'security buffer' represents a triplet used to point to a buffer,
206 consisting of two shorts and one long:
208 1. A 'short' containing the length of the buffer content in bytes.
209 2. A 'short' containing the allocated space for the buffer in bytes.
210 3. A 'long' containing the offset to the start of the buffer in bytes,
211 from the beginning of the NTLM message.
215 * Curl_ntlm_decode_type2_message()
217 * This is used to decode a ntlm type-2 message received from a: HTTP, SMTP
218 * or POP3 server. The message is first decoded from a base64 string into a
219 * raw ntlm message and checked for validity before the appropriate data for
220 * creating a type-3 message is written to the given ntlm data structure.
224 * data [in] - Pointer to session handle.
225 * header [in] - Pointer to the input buffer.
226 * ntlm [in] - Pointer to ntlm data struct being used and modified.
228 * Returns CURLE_OK on success.
230 CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
232 struct ntlmdata* ntlm)
234 #ifndef USE_WINDOWS_SSPI
235 static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
238 /* NTLM type-2 message structure:
240 Index Description Content
241 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
243 8 NTLM Message Type long (0x02000000)
244 12 Target Name security buffer
247 (32) Context 8 bytes (two consecutive longs) (*)
248 (40) Target Information security buffer (*)
249 (48) OS Version Structure 8 bytes (*)
250 32 (48) (56) Start of data block (*)
255 unsigned char *buffer = NULL;
258 #if defined(CURL_DISABLE_VERBOSE_STRINGS) || defined(USE_WINDOWS_SSPI)
262 error = Curl_base64_decode(header, &buffer, &size);
267 infof(data, "NTLM handshake failure (unhandled condition)\n");
268 return CURLE_REMOTE_ACCESS_DENIED;
271 #ifdef USE_WINDOWS_SSPI
272 ntlm->type_2 = malloc(size + 1);
273 if(ntlm->type_2 == NULL) {
275 return CURLE_OUT_OF_MEMORY;
277 ntlm->n_type_2 = (unsigned long)size;
278 memcpy(ntlm->type_2, buffer, size);
283 (memcmp(buffer, NTLMSSP_SIGNATURE, 8) != 0) ||
284 (memcmp(buffer + 8, type2_marker, sizeof(type2_marker)) != 0)) {
285 /* This was not a good enough type-2 message */
287 infof(data, "NTLM handshake failure (bad type-2 message)\n");
288 return CURLE_REMOTE_ACCESS_DENIED;
291 ntlm->flags = readint_le(&buffer[20]);
292 memcpy(ntlm->nonce, &buffer[24], 8);
295 fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
296 ntlm_print_flags(stderr, ntlm->flags);
297 fprintf(stderr, "\n nonce=");
298 ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
299 fprintf(stderr, "\n****\n");
300 fprintf(stderr, "**** Header %s\n ", header);
308 #ifdef USE_WINDOWS_SSPI
309 void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
315 if(ntlm->has_handles) {
316 s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
317 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
318 ntlm->has_handles = 0;
320 if(ntlm->p_identity) {
321 if(ntlm->identity.User) free(ntlm->identity.User);
322 if(ntlm->identity.Password) free(ntlm->identity.Password);
323 if(ntlm->identity.Domain) free(ntlm->identity.Domain);
324 ntlm->p_identity = NULL;
329 #ifndef USE_WINDOWS_SSPI
330 /* copy the source to the destination and fill in zeroes in every
331 other destination byte! */
332 static void unicodecpy(unsigned char *dest,
333 const char *src, size_t length)
336 for(i = 0; i < length; i++) {
337 dest[2 * i] = (unsigned char)src[i];
338 dest[2 * i + 1] = '\0';
344 * Curl_ntlm_create_type1_message()
346 * This is used to generate an already encoded NTLM type-1 message ready
347 * for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
348 * using the appropriate compile time crypo API.
352 * userp [in] - The user name in the format User or Domain\User.
353 * passdwp [in] - The user's password.
354 * ntlm [in/out] - The ntlm data struct being used and modified.
355 * outptr [in/out] - The adress where a pointer to newly allocated memory
356 * holding the result will be stored upon completion.
358 * Returns CURLE_OK on success.
360 CURLcode Curl_ntlm_create_type1_message(const char *userp,
362 struct ntlmdata *ntlm,
365 /* NTLM type-1 message structure:
367 Index Description Content
368 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
370 8 NTLM Message Type long (0x01000000)
372 (16) Supplied Domain security buffer (*)
373 (24) Supplied Workstation security buffer (*)
374 (32) OS Version Structure 8 bytes (*)
375 (32) (40) Start of data block (*)
379 unsigned char ntlmbuf[NTLM_BUFSIZE];
380 size_t base64_sz = 0;
383 #ifdef USE_WINDOWS_SSPI
387 SECURITY_STATUS status;
389 const char *dest = "";
391 const char *domain = "";
394 size_t passwdlen = 0;
395 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
397 Curl_ntlm_sspi_cleanup(ntlm);
399 user = strchr(userp, '\\');
401 user = strchr(userp, '/');
405 domlen = user - userp;
415 userlen = strlen(user);
418 passwdlen = strlen(passwdp);
421 /* note: initialize all of this before doing the mallocs so that
422 * it can be cleaned up later without leaking memory.
424 ntlm->p_identity = &ntlm->identity;
425 memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));
426 if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
427 return CURLE_OUT_OF_MEMORY;
429 ntlm->identity.UserLength = (unsigned long)userlen;
430 if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
431 return CURLE_OUT_OF_MEMORY;
433 ntlm->identity.PasswordLength = (unsigned long)strlen(passwdp);
434 if((ntlm->identity.Domain = malloc(domlen + 1)) == NULL)
435 return CURLE_OUT_OF_MEMORY;
437 strncpy((char *)ntlm->identity.Domain, domain, domlen);
438 ntlm->identity.Domain[domlen] = '\0';
439 ntlm->identity.DomainLength = (unsigned long)domlen;
440 ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
443 ntlm->p_identity = NULL;
445 status = s_pSecFn->AcquireCredentialsHandleA(NULL, (void *)"NTLM",
446 SECPKG_CRED_OUTBOUND, NULL,
447 ntlm->p_identity, NULL, NULL,
448 &ntlm->handle, &tsDummy);
449 if(status != SEC_E_OK)
450 return CURLE_OUT_OF_MEMORY;
452 desc.ulVersion = SECBUFFER_VERSION;
454 desc.pBuffers = &buf;
455 buf.cbBuffer = NTLM_BUFSIZE;
456 buf.BufferType = SECBUFFER_TOKEN;
457 buf.pvBuffer = ntlmbuf;
459 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle, NULL,
461 ISC_REQ_CONFIDENTIALITY |
462 ISC_REQ_REPLAY_DETECT |
464 0, SECURITY_NETWORK_DREP,
466 &ntlm->c_handle, &desc,
469 if(status == SEC_I_COMPLETE_AND_CONTINUE ||
470 status == SEC_I_CONTINUE_NEEDED)
471 s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &desc);
472 else if(status != SEC_E_OK) {
473 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
474 return CURLE_RECV_ERROR;
477 ntlm->has_handles = 1;
482 const char *host = ""; /* empty */
483 const char *domain = ""; /* empty */
487 size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
494 #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
498 snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
499 NTLMSSP_SIGNATURE "%c"
500 "\x01%c%c%c" /* 32-bit type = 1 */
501 "%c%c%c%c" /* 32-bit NTLM flag field */
502 "%c%c" /* domain length */
503 "%c%c" /* domain allocated space */
504 "%c%c" /* domain name offset */
505 "%c%c" /* 2 zeroes */
506 "%c%c" /* host length */
507 "%c%c" /* host allocated space */
508 "%c%c" /* host name offset */
509 "%c%c" /* 2 zeroes */
511 "%s", /* domain string */
512 0, /* trailing zero */
513 0, 0, 0, /* part of type-1 long */
515 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
516 NTLMFLAG_REQUEST_TARGET |
517 NTLMFLAG_NEGOTIATE_NTLM_KEY |
519 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
528 host, /* this is empty */
529 domain /* this is empty */);
531 /* Initial packet length */
532 size = 32 + hostlen + domlen;
537 fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
539 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
540 NTLMFLAG_REQUEST_TARGET |
541 NTLMFLAG_NEGOTIATE_NTLM_KEY |
543 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
544 NTLMFLAG_NEGOTIATE_OEM |
545 NTLMFLAG_REQUEST_TARGET |
546 NTLMFLAG_NEGOTIATE_NTLM_KEY |
548 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
549 ntlm_print_flags(stderr,
550 NTLMFLAG_NEGOTIATE_OEM |
551 NTLMFLAG_REQUEST_TARGET |
552 NTLMFLAG_NEGOTIATE_NTLM_KEY |
554 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
555 fprintf(stderr, "\n****\n");
558 /* Return with binary blob encoded into base64 */
559 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, &base64_sz);
563 * Curl_ntlm_create_type3_message()
565 * This is used to generate an already encoded NTLM type-3 message ready
566 * for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
567 * using the appropriate compile time crypo API.
571 * data [in] - The session handle.
572 * userp [in] - The user name in the format User or Domain\User.
573 * passdwp [in] - The user's password.
574 * ntlm [in/out] - The ntlm data struct being used and modified.
575 * outptr [in/out] - The adress where a pointer to newly allocated memory
576 * holding the result will be stored upon completion.
578 * Returns CURLE_OK on success.
580 CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
583 struct ntlmdata *ntlm,
586 /* NTLM type-3 message structure:
588 Index Description Content
589 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
591 8 NTLM Message Type long (0x03000000)
592 12 LM/LMv2 Response security buffer
593 20 NTLM/NTLMv2 Response security buffer
594 28 Target Name security buffer
595 36 User Name security buffer
596 44 Workstation Name security buffer
597 (52) Session Key security buffer (*)
599 (64) OS Version Structure 8 bytes (*)
600 52 (64) (72) Start of data block
604 unsigned char ntlmbuf[NTLM_BUFSIZE];
605 size_t base64_sz = 0;
608 #ifdef USE_WINDOWS_SSPI
609 const char *dest = "";
612 SecBufferDesc type_2_desc;
613 SecBufferDesc type_3_desc;
614 SECURITY_STATUS status;
616 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
622 type_2_desc.ulVersion = type_3_desc.ulVersion = SECBUFFER_VERSION;
623 type_2_desc.cBuffers = type_3_desc.cBuffers = 1;
624 type_2_desc.pBuffers = &type_2;
625 type_3_desc.pBuffers = &type_3;
627 type_2.BufferType = SECBUFFER_TOKEN;
628 type_2.pvBuffer = ntlm->type_2;
629 type_2.cbBuffer = ntlm->n_type_2;
630 type_3.BufferType = SECBUFFER_TOKEN;
631 type_3.pvBuffer = ntlmbuf;
632 type_3.cbBuffer = NTLM_BUFSIZE;
634 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle,
637 ISC_REQ_CONFIDENTIALITY |
638 ISC_REQ_REPLAY_DETECT |
640 0, SECURITY_NETWORK_DREP,
645 if(status != SEC_E_OK)
646 return CURLE_RECV_ERROR;
648 size = type_3.cbBuffer;
650 Curl_ntlm_sspi_cleanup(ntlm);
654 unsigned char lmresp[24]; /* fixed-size */
657 unsigned char ntresp[24]; /* fixed-size */
659 bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
660 char host[HOSTNAME_MAX + 1] = "";
662 const char *domain = "";
671 user = strchr(userp, '\\');
673 user = strchr(userp, '/');
677 domlen = (user - domain);
684 userlen = strlen(user);
686 /* Get the machine's un-qualified host name as NTLM doesn't like the fully
687 qualified domain name */
688 if(Curl_gethostname(host, sizeof(host))) {
689 infof(data, "gethostname() failed, continuing without!");
693 hostlen = strlen(host);
698 userlen = userlen * 2;
699 hostlen = hostlen * 2;
703 /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
704 if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
705 unsigned char ntbuffer[0x18];
706 unsigned char tmp[0x18];
707 unsigned char md5sum[MD5_DIGEST_LENGTH];
708 unsigned char entropy[8];
710 /* Need to create 8 bytes random data */
713 Curl_ossl_seed(data); /* Initiate the seed if not already done */
714 RAND_bytes(entropy, 8);
715 #elif defined(USE_GNUTLS)
717 Curl_gtls_seed(data); /* Initiate the seed if not already done */
718 gcry_randomize(entropy, 8, GCRY_STRONG_RANDOM);
719 #elif defined(USE_NSS)
722 Curl_nss_seed(data); /* Initiate the seed if not already done */
723 PK11_GenerateRandom(entropy, 8);
726 /* 8 bytes random data as challenge in lmresp */
727 memcpy(lmresp, entropy, 8);
730 memset(lmresp + 8, 0, 0x10);
732 /* Fill tmp with challenge(nonce?) + entropy */
733 memcpy(tmp, &ntlm->nonce[0], 8);
734 memcpy(tmp + 8, entropy, 8);
738 MD5_Update(&MD5pw, tmp, 16);
739 MD5_Final(md5sum, &MD5pw);
740 #elif defined(USE_GNUTLS)
741 gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
742 gcry_md_write(MD5pw, tmp, MD5_DIGEST_LENGTH);
743 memcpy(md5sum, gcry_md_read (MD5pw, 0), MD5_DIGEST_LENGTH);
744 gcry_md_close(MD5pw);
745 #elif defined(USE_NSS)
746 MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
747 PK11_DigestOp(MD5pw, tmp, 16);
748 PK11_DigestFinal(MD5pw, md5sum, &outlen, MD5_DIGEST_LENGTH);
749 PK11_DestroyContext(MD5pw, PR_TRUE);
752 /* We shall only use the first 8 bytes of md5sum, but the des
753 code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
754 if(CURLE_OUT_OF_MEMORY ==
755 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
756 return CURLE_OUT_OF_MEMORY;
757 Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
759 /* End of NTLM2 Session code */
766 unsigned char ntbuffer[0x18];
768 unsigned char lmbuffer[0x18];
771 if(CURLE_OUT_OF_MEMORY ==
772 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
773 return CURLE_OUT_OF_MEMORY;
774 Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
777 Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
778 Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
779 /* A safer but less compatible alternative is:
780 * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
781 * See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
784 lmrespoff = 64; /* size of the message header */
786 ntrespoff = lmrespoff + 0x18;
787 domoff = ntrespoff + 0x18;
789 domoff = lmrespoff + 0x18;
791 useroff = domoff + domlen;
792 hostoff = useroff + userlen;
794 /* Create the big type-3 message binary blob */
795 size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
796 NTLMSSP_SIGNATURE "%c"
797 "\x03%c%c%c" /* 32-bit type = 3 */
799 "%c%c" /* LanManager length */
800 "%c%c" /* LanManager allocated space */
801 "%c%c" /* LanManager offset */
802 "%c%c" /* 2 zeroes */
804 "%c%c" /* NT-response length */
805 "%c%c" /* NT-response allocated space */
806 "%c%c" /* NT-response offset */
807 "%c%c" /* 2 zeroes */
809 "%c%c" /* domain length */
810 "%c%c" /* domain allocated space */
811 "%c%c" /* domain name offset */
812 "%c%c" /* 2 zeroes */
814 "%c%c" /* user length */
815 "%c%c" /* user allocated space */
816 "%c%c" /* user offset */
817 "%c%c" /* 2 zeroes */
819 "%c%c" /* host length */
820 "%c%c" /* host allocated space */
821 "%c%c" /* host offset */
822 "%c%c" /* 2 zeroes */
824 "%c%c" /* session key length (unknown purpose) */
825 "%c%c" /* session key allocated space (unknown purpose) */
826 "%c%c" /* session key offset (unknown purpose) */
827 "%c%c" /* 2 zeroes */
829 "%c%c%c%c", /* flags */
834 /* LanManager response */
837 0, /* zero termination */
838 0, 0, 0, /* type-3 long, the 24 upper bits */
840 SHORTPAIR(0x18), /* LanManager response length, twice */
842 SHORTPAIR(lmrespoff),
846 SHORTPAIR(0x18), /* NT-response length, twice */
848 SHORTPAIR(ntrespoff),
876 LONGQUARTET(ntlm->flags));
878 DEBUGASSERT(size == 64);
879 DEBUGASSERT(size == (size_t)lmrespoff);
881 /* We append the binary hashes */
882 if(size < (NTLM_BUFSIZE - 0x18)) {
883 memcpy(&ntlmbuf[size], lmresp, 0x18);
888 fprintf(stderr, "**** TYPE3 header lmresp=");
889 ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
893 if(size < (NTLM_BUFSIZE - 0x18)) {
894 DEBUGASSERT(size == (size_t)ntrespoff);
895 memcpy(&ntlmbuf[size], ntresp, 0x18);
900 fprintf(stderr, "\n ntresp=");
901 ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], 0x18);
907 fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
908 LONGQUARTET(ntlm->flags), ntlm->flags);
909 ntlm_print_flags(stderr, ntlm->flags);
910 fprintf(stderr, "\n****\n");
913 /* Make sure that the domain, user and host strings fit in the
914 buffer before we copy them there. */
915 if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
916 failf(data, "user + domain + host name too big");
917 return CURLE_OUT_OF_MEMORY;
920 DEBUGASSERT(size == domoff);
922 unicodecpy(&ntlmbuf[size], domain, domlen / 2);
924 memcpy(&ntlmbuf[size], domain, domlen);
928 DEBUGASSERT(size == useroff);
930 unicodecpy(&ntlmbuf[size], user, userlen / 2);
932 memcpy(&ntlmbuf[size], user, userlen);
936 DEBUGASSERT(size == hostoff);
938 unicodecpy(&ntlmbuf[size], host, hostlen / 2);
940 memcpy(&ntlmbuf[size], host, hostlen);
944 /* Convert domain, user, and host to ASCII but leave the rest as-is */
945 res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
948 return CURLE_CONV_FAILED;
952 /* Return with binary blob encoded into base64 */
953 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, &base64_sz);
956 #endif /* USE_NTLM */