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.
357 * outlen [out] - The length of the output message.
359 * Returns CURLE_OK on success.
361 CURLcode Curl_ntlm_create_type1_message(const char *userp,
363 struct ntlmdata *ntlm,
367 /* NTLM type-1 message structure:
369 Index Description Content
370 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
372 8 NTLM Message Type long (0x01000000)
374 (16) Supplied Domain security buffer (*)
375 (24) Supplied Workstation security buffer (*)
376 (32) OS Version Structure 8 bytes (*)
377 (32) (40) Start of data block (*)
381 unsigned char ntlmbuf[NTLM_BUFSIZE];
384 #ifdef USE_WINDOWS_SSPI
388 SECURITY_STATUS status;
390 const char *dest = "";
392 const char *domain = "";
395 size_t passwdlen = 0;
396 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
398 Curl_ntlm_sspi_cleanup(ntlm);
400 user = strchr(userp, '\\');
402 user = strchr(userp, '/');
406 domlen = user - userp;
416 userlen = strlen(user);
419 passwdlen = strlen(passwdp);
422 /* note: initialize all of this before doing the mallocs so that
423 * it can be cleaned up later without leaking memory.
425 ntlm->p_identity = &ntlm->identity;
426 memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));
427 if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
428 return CURLE_OUT_OF_MEMORY;
430 ntlm->identity.UserLength = (unsigned long)userlen;
431 if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
432 return CURLE_OUT_OF_MEMORY;
434 ntlm->identity.PasswordLength = (unsigned long)strlen(passwdp);
435 if((ntlm->identity.Domain = malloc(domlen + 1)) == NULL)
436 return CURLE_OUT_OF_MEMORY;
438 strncpy((char *)ntlm->identity.Domain, domain, domlen);
439 ntlm->identity.Domain[domlen] = '\0';
440 ntlm->identity.DomainLength = (unsigned long)domlen;
441 ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
444 ntlm->p_identity = NULL;
446 status = s_pSecFn->AcquireCredentialsHandleA(NULL, (void *)"NTLM",
447 SECPKG_CRED_OUTBOUND, NULL,
448 ntlm->p_identity, NULL, NULL,
449 &ntlm->handle, &tsDummy);
450 if(status != SEC_E_OK)
451 return CURLE_OUT_OF_MEMORY;
453 desc.ulVersion = SECBUFFER_VERSION;
455 desc.pBuffers = &buf;
456 buf.cbBuffer = NTLM_BUFSIZE;
457 buf.BufferType = SECBUFFER_TOKEN;
458 buf.pvBuffer = ntlmbuf;
460 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle, NULL,
462 ISC_REQ_CONFIDENTIALITY |
463 ISC_REQ_REPLAY_DETECT |
465 0, SECURITY_NETWORK_DREP,
467 &ntlm->c_handle, &desc,
470 if(status == SEC_I_COMPLETE_AND_CONTINUE ||
471 status == SEC_I_CONTINUE_NEEDED)
472 s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &desc);
473 else if(status != SEC_E_OK) {
474 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
475 return CURLE_RECV_ERROR;
478 ntlm->has_handles = 1;
483 const char *host = ""; /* empty */
484 const char *domain = ""; /* empty */
488 size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
495 #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
499 snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
500 NTLMSSP_SIGNATURE "%c"
501 "\x01%c%c%c" /* 32-bit type = 1 */
502 "%c%c%c%c" /* 32-bit NTLM flag field */
503 "%c%c" /* domain length */
504 "%c%c" /* domain allocated space */
505 "%c%c" /* domain name offset */
506 "%c%c" /* 2 zeroes */
507 "%c%c" /* host length */
508 "%c%c" /* host allocated space */
509 "%c%c" /* host name offset */
510 "%c%c" /* 2 zeroes */
512 "%s", /* domain string */
513 0, /* trailing zero */
514 0, 0, 0, /* part of type-1 long */
516 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
517 NTLMFLAG_REQUEST_TARGET |
518 NTLMFLAG_NEGOTIATE_NTLM_KEY |
520 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
529 host, /* this is empty */
530 domain /* this is empty */);
532 /* Initial packet length */
533 size = 32 + hostlen + domlen;
538 fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
540 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
541 NTLMFLAG_REQUEST_TARGET |
542 NTLMFLAG_NEGOTIATE_NTLM_KEY |
544 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
545 NTLMFLAG_NEGOTIATE_OEM |
546 NTLMFLAG_REQUEST_TARGET |
547 NTLMFLAG_NEGOTIATE_NTLM_KEY |
549 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
550 ntlm_print_flags(stderr,
551 NTLMFLAG_NEGOTIATE_OEM |
552 NTLMFLAG_REQUEST_TARGET |
553 NTLMFLAG_NEGOTIATE_NTLM_KEY |
555 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
556 fprintf(stderr, "\n****\n");
559 /* Return with binary blob encoded into base64 */
560 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
564 * Curl_ntlm_create_type3_message()
566 * This is used to generate an already encoded NTLM type-3 message ready
567 * for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
568 * using the appropriate compile time crypo API.
572 * data [in] - The session handle.
573 * userp [in] - The user name in the format User or Domain\User.
574 * passdwp [in] - The user's password.
575 * ntlm [in/out] - The ntlm data struct being used and modified.
576 * outptr [in/out] - The adress where a pointer to newly allocated memory
577 * holding the result will be stored upon completion.
578 * outlen [out] - The length of the output message.
580 * Returns CURLE_OK on success.
582 CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
585 struct ntlmdata *ntlm,
589 /* NTLM type-3 message structure:
591 Index Description Content
592 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
594 8 NTLM Message Type long (0x03000000)
595 12 LM/LMv2 Response security buffer
596 20 NTLM/NTLMv2 Response security buffer
597 28 Target Name security buffer
598 36 User Name security buffer
599 44 Workstation Name security buffer
600 (52) Session Key security buffer (*)
602 (64) OS Version Structure 8 bytes (*)
603 52 (64) (72) Start of data block
607 unsigned char ntlmbuf[NTLM_BUFSIZE];
610 #ifdef USE_WINDOWS_SSPI
611 const char *dest = "";
614 SecBufferDesc type_2_desc;
615 SecBufferDesc type_3_desc;
616 SECURITY_STATUS status;
618 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
624 type_2_desc.ulVersion = type_3_desc.ulVersion = SECBUFFER_VERSION;
625 type_2_desc.cBuffers = type_3_desc.cBuffers = 1;
626 type_2_desc.pBuffers = &type_2;
627 type_3_desc.pBuffers = &type_3;
629 type_2.BufferType = SECBUFFER_TOKEN;
630 type_2.pvBuffer = ntlm->type_2;
631 type_2.cbBuffer = ntlm->n_type_2;
632 type_3.BufferType = SECBUFFER_TOKEN;
633 type_3.pvBuffer = ntlmbuf;
634 type_3.cbBuffer = NTLM_BUFSIZE;
636 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle,
639 ISC_REQ_CONFIDENTIALITY |
640 ISC_REQ_REPLAY_DETECT |
642 0, SECURITY_NETWORK_DREP,
647 if(status != SEC_E_OK)
648 return CURLE_RECV_ERROR;
650 size = type_3.cbBuffer;
652 Curl_ntlm_sspi_cleanup(ntlm);
656 unsigned char lmresp[24]; /* fixed-size */
659 unsigned char ntresp[24]; /* fixed-size */
661 bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
662 char host[HOSTNAME_MAX + 1] = "";
664 const char *domain = "";
673 user = strchr(userp, '\\');
675 user = strchr(userp, '/');
679 domlen = (user - domain);
686 userlen = strlen(user);
688 /* Get the machine's un-qualified host name as NTLM doesn't like the fully
689 qualified domain name */
690 if(Curl_gethostname(host, sizeof(host))) {
691 infof(data, "gethostname() failed, continuing without!");
695 hostlen = strlen(host);
700 userlen = userlen * 2;
701 hostlen = hostlen * 2;
705 /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
706 if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
707 unsigned char ntbuffer[0x18];
708 unsigned char tmp[0x18];
709 unsigned char md5sum[MD5_DIGEST_LENGTH];
710 unsigned char entropy[8];
712 /* Need to create 8 bytes random data */
715 Curl_ossl_seed(data); /* Initiate the seed if not already done */
716 RAND_bytes(entropy, 8);
717 #elif defined(USE_GNUTLS)
719 Curl_gtls_seed(data); /* Initiate the seed if not already done */
720 gcry_randomize(entropy, 8, GCRY_STRONG_RANDOM);
721 #elif defined(USE_NSS)
724 Curl_nss_seed(data); /* Initiate the seed if not already done */
725 PK11_GenerateRandom(entropy, 8);
728 /* 8 bytes random data as challenge in lmresp */
729 memcpy(lmresp, entropy, 8);
732 memset(lmresp + 8, 0, 0x10);
734 /* Fill tmp with challenge(nonce?) + entropy */
735 memcpy(tmp, &ntlm->nonce[0], 8);
736 memcpy(tmp + 8, entropy, 8);
740 MD5_Update(&MD5pw, tmp, 16);
741 MD5_Final(md5sum, &MD5pw);
742 #elif defined(USE_GNUTLS)
743 gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
744 gcry_md_write(MD5pw, tmp, MD5_DIGEST_LENGTH);
745 memcpy(md5sum, gcry_md_read (MD5pw, 0), MD5_DIGEST_LENGTH);
746 gcry_md_close(MD5pw);
747 #elif defined(USE_NSS)
748 MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
749 PK11_DigestOp(MD5pw, tmp, 16);
750 PK11_DigestFinal(MD5pw, md5sum, &MD5len, MD5_DIGEST_LENGTH);
751 PK11_DestroyContext(MD5pw, PR_TRUE);
754 /* We shall only use the first 8 bytes of md5sum, but the des
755 code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
756 if(CURLE_OUT_OF_MEMORY ==
757 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
758 return CURLE_OUT_OF_MEMORY;
759 Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
761 /* End of NTLM2 Session code */
768 unsigned char ntbuffer[0x18];
770 unsigned char lmbuffer[0x18];
773 if(CURLE_OUT_OF_MEMORY ==
774 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
775 return CURLE_OUT_OF_MEMORY;
776 Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
779 Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
780 Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
781 /* A safer but less compatible alternative is:
782 * Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
783 * See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
786 lmrespoff = 64; /* size of the message header */
788 ntrespoff = lmrespoff + 0x18;
789 domoff = ntrespoff + 0x18;
791 domoff = lmrespoff + 0x18;
793 useroff = domoff + domlen;
794 hostoff = useroff + userlen;
796 /* Create the big type-3 message binary blob */
797 size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
798 NTLMSSP_SIGNATURE "%c"
799 "\x03%c%c%c" /* 32-bit type = 3 */
801 "%c%c" /* LanManager length */
802 "%c%c" /* LanManager allocated space */
803 "%c%c" /* LanManager offset */
804 "%c%c" /* 2 zeroes */
806 "%c%c" /* NT-response length */
807 "%c%c" /* NT-response allocated space */
808 "%c%c" /* NT-response offset */
809 "%c%c" /* 2 zeroes */
811 "%c%c" /* domain length */
812 "%c%c" /* domain allocated space */
813 "%c%c" /* domain name offset */
814 "%c%c" /* 2 zeroes */
816 "%c%c" /* user length */
817 "%c%c" /* user allocated space */
818 "%c%c" /* user offset */
819 "%c%c" /* 2 zeroes */
821 "%c%c" /* host length */
822 "%c%c" /* host allocated space */
823 "%c%c" /* host offset */
824 "%c%c" /* 2 zeroes */
826 "%c%c" /* session key length (unknown purpose) */
827 "%c%c" /* session key allocated space (unknown purpose) */
828 "%c%c" /* session key offset (unknown purpose) */
829 "%c%c" /* 2 zeroes */
831 "%c%c%c%c", /* flags */
836 /* LanManager response */
839 0, /* zero termination */
840 0, 0, 0, /* type-3 long, the 24 upper bits */
842 SHORTPAIR(0x18), /* LanManager response length, twice */
844 SHORTPAIR(lmrespoff),
848 SHORTPAIR(0x18), /* NT-response length, twice */
850 SHORTPAIR(ntrespoff),
878 LONGQUARTET(ntlm->flags));
880 DEBUGASSERT(size == 64);
881 DEBUGASSERT(size == (size_t)lmrespoff);
883 /* We append the binary hashes */
884 if(size < (NTLM_BUFSIZE - 0x18)) {
885 memcpy(&ntlmbuf[size], lmresp, 0x18);
890 fprintf(stderr, "**** TYPE3 header lmresp=");
891 ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
895 if(size < (NTLM_BUFSIZE - 0x18)) {
896 DEBUGASSERT(size == (size_t)ntrespoff);
897 memcpy(&ntlmbuf[size], ntresp, 0x18);
902 fprintf(stderr, "\n ntresp=");
903 ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], 0x18);
909 fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
910 LONGQUARTET(ntlm->flags), ntlm->flags);
911 ntlm_print_flags(stderr, ntlm->flags);
912 fprintf(stderr, "\n****\n");
915 /* Make sure that the domain, user and host strings fit in the
916 buffer before we copy them there. */
917 if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
918 failf(data, "user + domain + host name too big");
919 return CURLE_OUT_OF_MEMORY;
922 DEBUGASSERT(size == domoff);
924 unicodecpy(&ntlmbuf[size], domain, domlen / 2);
926 memcpy(&ntlmbuf[size], domain, domlen);
930 DEBUGASSERT(size == useroff);
932 unicodecpy(&ntlmbuf[size], user, userlen / 2);
934 memcpy(&ntlmbuf[size], user, userlen);
938 DEBUGASSERT(size == hostoff);
940 unicodecpy(&ntlmbuf[size], host, hostlen / 2);
942 memcpy(&ntlmbuf[size], host, hostlen);
946 /* Convert domain, user, and host to ASCII but leave the rest as-is */
947 res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
950 return CURLE_CONV_FAILED;
954 /* Return with binary blob encoded into base64 */
955 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
958 #endif /* USE_NTLM */