1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2012, 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_NETTLE)
59 # include <nettle/md5.h>
60 # include <gnutls/gnutls.h>
61 # include <gnutls/crypto.h>
62 # define MD5_DIGEST_LENGTH 16
64 #elif defined(USE_GNUTLS)
68 # define MD5_DIGEST_LENGTH 16
69 # define MD4_DIGEST_LENGTH 16
71 #elif defined(USE_NSS)
77 # include "curl_md4.h"
78 # define MD5_DIGEST_LENGTH MD5_LENGTH
80 #elif defined(USE_WINDOWS_SSPI)
81 # include "curl_sspi.h"
83 # error "Can't compile NTLM support without a crypto library."
87 #include "non-ascii.h"
89 #include "curl_base64.h"
90 #include "curl_ntlm_core.h"
91 #include "curl_gethostname.h"
92 #include "curl_memory.h"
94 #define BUILDING_CURL_NTLM_MSGS_C
95 #include "curl_ntlm_msgs.h"
97 #define _MPRINTF_REPLACE /* use our functions only */
98 #include <curl/mprintf.h>
100 /* The last #include file should be: */
101 #include "memdebug.h"
103 /* "NTLMSSP" signature is always in ASCII regardless of the platform */
104 #define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
106 #define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
107 #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
108 (((x) >> 16) & 0xff), (((x) >> 24) & 0xff)
111 # define DEBUG_OUT(x) x
112 static void ntlm_print_flags(FILE *handle, unsigned long flags)
114 if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
115 fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
116 if(flags & NTLMFLAG_NEGOTIATE_OEM)
117 fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
118 if(flags & NTLMFLAG_REQUEST_TARGET)
119 fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
121 fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
122 if(flags & NTLMFLAG_NEGOTIATE_SIGN)
123 fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
124 if(flags & NTLMFLAG_NEGOTIATE_SEAL)
125 fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
126 if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
127 fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
128 if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
129 fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
130 if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
131 fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
132 if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
133 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
135 fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
136 if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
137 fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
138 if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
139 fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
140 if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
141 fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
142 if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
143 fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
144 if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
145 fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
146 if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
147 fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
148 if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
149 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
150 if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
151 fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
152 if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
153 fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
154 if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
155 fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
156 if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
157 fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
158 if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
159 fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
160 if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
161 fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
163 fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
165 fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
167 fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
169 fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
171 fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
172 if(flags & NTLMFLAG_NEGOTIATE_128)
173 fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
174 if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
175 fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
176 if(flags & NTLMFLAG_NEGOTIATE_56)
177 fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
180 static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
184 fprintf(stderr, "0x");
186 fprintf(stderr, "%02.2x", (unsigned int)*p++);
189 # define DEBUG_OUT(x) Curl_nop_stmt
192 #ifndef USE_WINDOWS_SSPI
194 * This function converts from the little endian format used in the
195 * incoming package to whatever endian format we're using natively.
196 * Argument is a pointer to a 4 byte buffer.
198 static unsigned int readint_le(unsigned char *buf)
200 return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
201 ((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
206 NTLM message structure notes:
208 A 'short' is a 'network short', a little-endian 16-bit unsigned value.
210 A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
212 A 'security buffer' represents a triplet used to point to a buffer,
213 consisting of two shorts and one long:
215 1. A 'short' containing the length of the buffer content in bytes.
216 2. A 'short' containing the allocated space for the buffer in bytes.
217 3. A 'long' containing the offset to the start of the buffer in bytes,
218 from the beginning of the NTLM message.
222 * Curl_ntlm_decode_type2_message()
224 * This is used to decode a ntlm type-2 message received from a: HTTP, SMTP
225 * or POP3 server. The message is first decoded from a base64 string into a
226 * raw ntlm message and checked for validity before the appropriate data for
227 * creating a type-3 message is written to the given ntlm data structure.
231 * data [in] - Pointer to session handle.
232 * header [in] - Pointer to the input buffer.
233 * ntlm [in] - Pointer to ntlm data struct being used and modified.
235 * Returns CURLE_OK on success.
237 CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
239 struct ntlmdata* ntlm)
241 #ifndef USE_WINDOWS_SSPI
242 static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
245 /* NTLM type-2 message structure:
247 Index Description Content
248 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
250 8 NTLM Message Type long (0x02000000)
251 12 Target Name security buffer
254 (32) Context 8 bytes (two consecutive longs) (*)
255 (40) Target Information security buffer (*)
256 (48) OS Version Structure 8 bytes (*)
257 32 (48) (56) Start of data block (*)
262 unsigned char *buffer = NULL;
265 #if defined(CURL_DISABLE_VERBOSE_STRINGS) || defined(USE_WINDOWS_SSPI)
269 error = Curl_base64_decode(header, &buffer, &size);
274 infof(data, "NTLM handshake failure (unhandled condition)\n");
275 return CURLE_REMOTE_ACCESS_DENIED;
278 #ifdef USE_WINDOWS_SSPI
279 ntlm->type_2 = malloc(size + 1);
280 if(ntlm->type_2 == NULL) {
282 return CURLE_OUT_OF_MEMORY;
284 ntlm->n_type_2 = (unsigned long)size;
285 memcpy(ntlm->type_2, buffer, size);
290 (memcmp(buffer, NTLMSSP_SIGNATURE, 8) != 0) ||
291 (memcmp(buffer + 8, type2_marker, sizeof(type2_marker)) != 0)) {
292 /* This was not a good enough type-2 message */
294 infof(data, "NTLM handshake failure (bad type-2 message)\n");
295 return CURLE_REMOTE_ACCESS_DENIED;
298 ntlm->flags = readint_le(&buffer[20]);
299 memcpy(ntlm->nonce, &buffer[24], 8);
302 fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
303 ntlm_print_flags(stderr, ntlm->flags);
304 fprintf(stderr, "\n nonce=");
305 ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
306 fprintf(stderr, "\n****\n");
307 fprintf(stderr, "**** Header %s\n ", header);
315 #ifdef USE_WINDOWS_SSPI
316 void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
322 if(ntlm->has_handles) {
323 s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
324 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
325 ntlm->has_handles = 0;
327 if(ntlm->p_identity) {
328 if(ntlm->identity.User) free(ntlm->identity.User);
329 if(ntlm->identity.Password) free(ntlm->identity.Password);
330 if(ntlm->identity.Domain) free(ntlm->identity.Domain);
331 ntlm->p_identity = NULL;
336 #ifndef USE_WINDOWS_SSPI
337 /* copy the source to the destination and fill in zeroes in every
338 other destination byte! */
339 static void unicodecpy(unsigned char *dest,
340 const char *src, size_t length)
343 for(i = 0; i < length; i++) {
344 dest[2 * i] = (unsigned char)src[i];
345 dest[2 * i + 1] = '\0';
351 * Curl_ntlm_create_type1_message()
353 * This is used to generate an already encoded NTLM type-1 message ready
354 * for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
355 * using the appropriate compile time crypo API.
359 * userp [in] - The user name in the format User or Domain\User.
360 * passdwp [in] - The user's password.
361 * ntlm [in/out] - The ntlm data struct being used and modified.
362 * outptr [in/out] - The address where a pointer to newly allocated memory
363 * holding the result will be stored upon completion.
364 * outlen [out] - The length of the output message.
366 * Returns CURLE_OK on success.
368 CURLcode Curl_ntlm_create_type1_message(const char *userp,
370 struct ntlmdata *ntlm,
374 /* NTLM type-1 message structure:
376 Index Description Content
377 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
379 8 NTLM Message Type long (0x01000000)
381 (16) Supplied Domain security buffer (*)
382 (24) Supplied Workstation security buffer (*)
383 (32) OS Version Structure 8 bytes (*)
384 (32) (40) Start of data block (*)
388 unsigned char ntlmbuf[NTLM_BUFSIZE];
391 #ifdef USE_WINDOWS_SSPI
395 SECURITY_STATUS status;
397 const char *dest = "";
399 const char *domain = "";
402 size_t passwdlen = 0;
403 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
405 Curl_ntlm_sspi_cleanup(ntlm);
407 user = strchr(userp, '\\');
409 user = strchr(userp, '/');
413 domlen = user - userp;
423 userlen = strlen(user);
426 passwdlen = strlen(passwdp);
429 /* note: initialize all of this before doing the mallocs so that
430 * it can be cleaned up later without leaking memory.
432 ntlm->p_identity = &ntlm->identity;
433 memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));
434 if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
435 return CURLE_OUT_OF_MEMORY;
437 ntlm->identity.UserLength = (unsigned long)userlen;
438 if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
439 return CURLE_OUT_OF_MEMORY;
441 ntlm->identity.PasswordLength = (unsigned long)strlen(passwdp);
442 if((ntlm->identity.Domain = malloc(domlen + 1)) == NULL)
443 return CURLE_OUT_OF_MEMORY;
445 strncpy((char *)ntlm->identity.Domain, domain, domlen);
446 ntlm->identity.Domain[domlen] = '\0';
447 ntlm->identity.DomainLength = (unsigned long)domlen;
448 ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
451 ntlm->p_identity = NULL;
453 status = s_pSecFn->AcquireCredentialsHandleA(NULL, (void *)"NTLM",
454 SECPKG_CRED_OUTBOUND, NULL,
455 ntlm->p_identity, NULL, NULL,
456 &ntlm->handle, &tsDummy);
457 if(status != SEC_E_OK)
458 return CURLE_OUT_OF_MEMORY;
460 desc.ulVersion = SECBUFFER_VERSION;
462 desc.pBuffers = &buf;
463 buf.cbBuffer = NTLM_BUFSIZE;
464 buf.BufferType = SECBUFFER_TOKEN;
465 buf.pvBuffer = ntlmbuf;
467 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle, NULL,
469 ISC_REQ_CONFIDENTIALITY |
470 ISC_REQ_REPLAY_DETECT |
472 0, SECURITY_NETWORK_DREP,
474 &ntlm->c_handle, &desc,
477 if(status == SEC_I_COMPLETE_AND_CONTINUE ||
478 status == SEC_I_CONTINUE_NEEDED)
479 s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &desc);
480 else if(status != SEC_E_OK) {
481 s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
482 return CURLE_RECV_ERROR;
485 ntlm->has_handles = 1;
490 const char *host = ""; /* empty */
491 const char *domain = ""; /* empty */
495 size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
502 #define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
506 snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
507 NTLMSSP_SIGNATURE "%c"
508 "\x01%c%c%c" /* 32-bit type = 1 */
509 "%c%c%c%c" /* 32-bit NTLM flag field */
510 "%c%c" /* domain length */
511 "%c%c" /* domain allocated space */
512 "%c%c" /* domain name offset */
513 "%c%c" /* 2 zeroes */
514 "%c%c" /* host length */
515 "%c%c" /* host allocated space */
516 "%c%c" /* host name offset */
517 "%c%c" /* 2 zeroes */
519 "%s", /* domain string */
520 0, /* trailing zero */
521 0, 0, 0, /* part of type-1 long */
523 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
524 NTLMFLAG_REQUEST_TARGET |
525 NTLMFLAG_NEGOTIATE_NTLM_KEY |
527 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
536 host, /* this is empty */
537 domain /* this is empty */);
539 /* Initial packet length */
540 size = 32 + hostlen + domlen;
545 fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
547 LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
548 NTLMFLAG_REQUEST_TARGET |
549 NTLMFLAG_NEGOTIATE_NTLM_KEY |
551 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
552 NTLMFLAG_NEGOTIATE_OEM |
553 NTLMFLAG_REQUEST_TARGET |
554 NTLMFLAG_NEGOTIATE_NTLM_KEY |
556 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
557 ntlm_print_flags(stderr,
558 NTLMFLAG_NEGOTIATE_OEM |
559 NTLMFLAG_REQUEST_TARGET |
560 NTLMFLAG_NEGOTIATE_NTLM_KEY |
562 NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
563 fprintf(stderr, "\n****\n");
566 /* Return with binary blob encoded into base64 */
567 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
571 * Curl_ntlm_create_type3_message()
573 * This is used to generate an already encoded NTLM type-3 message ready
574 * for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
575 * using the appropriate compile time crypo API.
579 * data [in] - The session handle.
580 * userp [in] - The user name in the format User or Domain\User.
581 * passdwp [in] - The user's password.
582 * ntlm [in/out] - The ntlm data struct being used and modified.
583 * outptr [in/out] - The address where a pointer to newly allocated memory
584 * holding the result will be stored upon completion.
585 * outlen [out] - The length of the output message.
587 * Returns CURLE_OK on success.
589 CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
592 struct ntlmdata *ntlm,
596 /* NTLM type-3 message structure:
598 Index Description Content
599 0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
601 8 NTLM Message Type long (0x03000000)
602 12 LM/LMv2 Response security buffer
603 20 NTLM/NTLMv2 Response security buffer
604 28 Target Name security buffer
605 36 User Name security buffer
606 44 Workstation Name security buffer
607 (52) Session Key security buffer (*)
609 (64) OS Version Structure 8 bytes (*)
610 52 (64) (72) Start of data block
614 unsigned char ntlmbuf[NTLM_BUFSIZE];
617 #ifdef USE_WINDOWS_SSPI
618 const char *dest = "";
621 SecBufferDesc type_2_desc;
622 SecBufferDesc type_3_desc;
623 SECURITY_STATUS status;
625 TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
631 type_2_desc.ulVersion = type_3_desc.ulVersion = SECBUFFER_VERSION;
632 type_2_desc.cBuffers = type_3_desc.cBuffers = 1;
633 type_2_desc.pBuffers = &type_2;
634 type_3_desc.pBuffers = &type_3;
636 type_2.BufferType = SECBUFFER_TOKEN;
637 type_2.pvBuffer = ntlm->type_2;
638 type_2.cbBuffer = ntlm->n_type_2;
639 type_3.BufferType = SECBUFFER_TOKEN;
640 type_3.pvBuffer = ntlmbuf;
641 type_3.cbBuffer = NTLM_BUFSIZE;
643 status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle,
646 ISC_REQ_CONFIDENTIALITY |
647 ISC_REQ_REPLAY_DETECT |
649 0, SECURITY_NETWORK_DREP,
654 if(status != SEC_E_OK)
655 return CURLE_RECV_ERROR;
657 size = type_3.cbBuffer;
659 Curl_ntlm_sspi_cleanup(ntlm);
663 unsigned char lmresp[24]; /* fixed-size */
666 unsigned char ntresp[24]; /* fixed-size */
668 bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
669 char host[HOSTNAME_MAX + 1] = "";
671 const char *domain = "";
680 user = strchr(userp, '\\');
682 user = strchr(userp, '/');
686 domlen = (user - domain);
693 userlen = strlen(user);
695 /* Get the machine's un-qualified host name as NTLM doesn't like the fully
696 qualified domain name */
697 if(Curl_gethostname(host, sizeof(host))) {
698 infof(data, "gethostname() failed, continuing without!\n");
702 hostlen = strlen(host);
707 userlen = userlen * 2;
708 hostlen = hostlen * 2;
712 /* We don't support NTLM2 if we don't have USE_NTRESPONSES */
713 if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
714 unsigned char ntbuffer[0x18];
715 unsigned char tmp[0x18];
716 unsigned char md5sum[MD5_DIGEST_LENGTH];
717 unsigned char entropy[8];
719 /* Need to create 8 bytes random data */
722 Curl_ossl_seed(data); /* Initiate the seed if not already done */
723 RAND_bytes(entropy, 8);
724 #elif defined(USE_GNUTLS_NETTLE)
725 struct md5_ctx MD5pw;
726 gnutls_rnd(GNUTLS_RND_RANDOM, entropy, 8);
727 #elif defined(USE_GNUTLS)
729 Curl_gtls_seed(data); /* Initiate the seed if not already done */
730 gcry_randomize(entropy, 8, GCRY_STRONG_RANDOM);
731 #elif defined(USE_NSS)
734 Curl_nss_seed(data); /* Initiate the seed if not already done */
735 PK11_GenerateRandom(entropy, 8);
738 /* 8 bytes random data as challenge in lmresp */
739 memcpy(lmresp, entropy, 8);
742 memset(lmresp + 8, 0, 0x10);
744 /* Fill tmp with challenge(nonce?) + entropy */
745 memcpy(tmp, &ntlm->nonce[0], 8);
746 memcpy(tmp + 8, entropy, 8);
750 MD5_Update(&MD5pw, tmp, 16);
751 MD5_Final(md5sum, &MD5pw);
752 #elif defined(USE_GNUTLS_NETTLE)
754 md5_update(&MD5pw, 16, tmp);
755 md5_digest(&MD5pw, 16, md5sum);
756 #elif defined(USE_GNUTLS)
757 gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
758 gcry_md_write(MD5pw, tmp, MD5_DIGEST_LENGTH);
759 memcpy(md5sum, gcry_md_read (MD5pw, 0), MD5_DIGEST_LENGTH);
760 gcry_md_close(MD5pw);
761 #elif defined(USE_NSS)
762 MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
763 PK11_DigestOp(MD5pw, tmp, 16);
764 PK11_DigestFinal(MD5pw, md5sum, &MD5len, MD5_DIGEST_LENGTH);
765 PK11_DestroyContext(MD5pw, PR_TRUE);
768 /* We shall only use the first 8 bytes of md5sum, but the des
769 code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
770 if(CURLE_OUT_OF_MEMORY ==
771 Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
772 return CURLE_OUT_OF_MEMORY;
773 Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
775 /* 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 */
800 lmrespoff = 64; /* size of the message header */
802 ntrespoff = lmrespoff + 0x18;
803 domoff = ntrespoff + 0x18;
805 domoff = lmrespoff + 0x18;
807 useroff = domoff + domlen;
808 hostoff = useroff + userlen;
810 /* Create the big type-3 message binary blob */
811 size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
812 NTLMSSP_SIGNATURE "%c"
813 "\x03%c%c%c" /* 32-bit type = 3 */
815 "%c%c" /* LanManager length */
816 "%c%c" /* LanManager allocated space */
817 "%c%c" /* LanManager offset */
818 "%c%c" /* 2 zeroes */
820 "%c%c" /* NT-response length */
821 "%c%c" /* NT-response allocated space */
822 "%c%c" /* NT-response offset */
823 "%c%c" /* 2 zeroes */
825 "%c%c" /* domain length */
826 "%c%c" /* domain allocated space */
827 "%c%c" /* domain name offset */
828 "%c%c" /* 2 zeroes */
830 "%c%c" /* user length */
831 "%c%c" /* user allocated space */
832 "%c%c" /* user offset */
833 "%c%c" /* 2 zeroes */
835 "%c%c" /* host length */
836 "%c%c" /* host allocated space */
837 "%c%c" /* host offset */
838 "%c%c" /* 2 zeroes */
840 "%c%c" /* session key length (unknown purpose) */
841 "%c%c" /* session key allocated space (unknown purpose) */
842 "%c%c" /* session key offset (unknown purpose) */
843 "%c%c" /* 2 zeroes */
845 "%c%c%c%c", /* flags */
850 /* LanManager response */
853 0, /* zero termination */
854 0, 0, 0, /* type-3 long, the 24 upper bits */
856 SHORTPAIR(0x18), /* LanManager response length, twice */
858 SHORTPAIR(lmrespoff),
862 SHORTPAIR(0x18), /* NT-response length, twice */
864 SHORTPAIR(ntrespoff),
892 LONGQUARTET(ntlm->flags));
894 DEBUGASSERT(size == 64);
895 DEBUGASSERT(size == (size_t)lmrespoff);
897 /* We append the binary hashes */
898 if(size < (NTLM_BUFSIZE - 0x18)) {
899 memcpy(&ntlmbuf[size], lmresp, 0x18);
904 fprintf(stderr, "**** TYPE3 header lmresp=");
905 ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
909 if(size < (NTLM_BUFSIZE - 0x18)) {
910 DEBUGASSERT(size == (size_t)ntrespoff);
911 memcpy(&ntlmbuf[size], ntresp, 0x18);
916 fprintf(stderr, "\n ntresp=");
917 ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], 0x18);
923 fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
924 LONGQUARTET(ntlm->flags), ntlm->flags);
925 ntlm_print_flags(stderr, ntlm->flags);
926 fprintf(stderr, "\n****\n");
929 /* Make sure that the domain, user and host strings fit in the
930 buffer before we copy them there. */
931 if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
932 failf(data, "user + domain + host name too big");
933 return CURLE_OUT_OF_MEMORY;
936 DEBUGASSERT(size == domoff);
938 unicodecpy(&ntlmbuf[size], domain, domlen / 2);
940 memcpy(&ntlmbuf[size], domain, domlen);
944 DEBUGASSERT(size == useroff);
946 unicodecpy(&ntlmbuf[size], user, userlen / 2);
948 memcpy(&ntlmbuf[size], user, userlen);
952 DEBUGASSERT(size == hostoff);
954 unicodecpy(&ntlmbuf[size], host, hostlen / 2);
956 memcpy(&ntlmbuf[size], host, hostlen);
960 /* Convert domain, user, and host to ASCII but leave the rest as-is */
961 res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
964 return CURLE_CONV_FAILED;
968 /* Return with binary blob encoded into base64 */
969 return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, outlen);
972 #endif /* USE_NTLM */