1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 https://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 * https://davenport.sourceforge.io/ntlm.html
31 * https://www.innovation.ch/java/ntlm.html
34 /* Please keep the SSL backend-specific #if branches in this order:
46 - the same SSL branch gets activated throughout this source
47 file even if multiple backends are enabled at the same time.
48 - OpenSSL and NSS have higher priority than Windows Crypt, due
49 to issues with the latter supporting NTLM2Session responses
50 in NTLM type-3 messages.
53 #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
58 # include <openssl/des.h>
59 # ifndef OPENSSL_NO_MD4
60 # include <openssl/md4.h>
62 # include <openssl/md5.h>
63 # include <openssl/ssl.h>
64 # include <openssl/rand.h>
67 # ifndef OPENSSL_NO_MD4
74 # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
75 # define DES_key_schedule des_key_schedule
76 # define DES_cblock des_cblock
77 # define DES_set_odd_parity des_set_odd_parity
78 # define DES_set_key des_set_key
79 # define DES_ecb_encrypt des_ecb_encrypt
81 # define DESKEYARG(x) x
83 # define DESKEYARG(x) *x
87 #elif defined(USE_GNUTLS_NETTLE)
89 # include <nettle/des.h>
90 # include <nettle/md4.h>
92 #elif defined(USE_GNUTLS)
95 # define MD5_DIGEST_LENGTH 16
96 # define MD4_DIGEST_LENGTH 16
98 #elif defined(USE_NSS)
101 # include <pk11pub.h>
103 # include "curl_md4.h"
104 # define MD5_DIGEST_LENGTH MD5_LENGTH
106 #elif defined(USE_MBEDTLS)
108 # include <mbedtls/des.h>
109 # include <mbedtls/md4.h>
110 # if !defined(MBEDTLS_MD4_C)
111 # include "curl_md4.h"
114 #elif defined(USE_DARWINSSL)
116 # include <CommonCrypto/CommonCryptor.h>
117 # include <CommonCrypto/CommonDigest.h>
119 #elif defined(USE_OS400CRYPTO)
120 # include "cipher.mih" /* mih/cipher */
121 # include "curl_md4.h"
122 #elif defined(USE_WIN32_CRYPTO)
123 # include <wincrypt.h>
125 # error "Can't compile NTLM support without a crypto library."
129 #include "non-ascii.h"
131 #include "curl_ntlm_core.h"
132 #include "curl_md5.h"
133 #include "curl_hmac.h"
134 #include "warnless.h"
135 #include "curl_endian.h"
136 #include "curl_des.h"
137 /* The last 3 #include files should be in this order */
138 #include "curl_printf.h"
139 #include "curl_memory.h"
140 #include "memdebug.h"
142 #define NTLM_HMAC_MD5_LEN (16)
143 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
144 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
147 * Turns a 56-bit key into being 64-bit wide.
149 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
152 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
153 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
154 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
155 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
156 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
157 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
158 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
163 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
164 * key schedule ks is also set.
166 static void setup_des_key(const unsigned char *key_56,
167 DES_key_schedule DESKEYARG(ks))
171 /* Expand the 56-bit key to 64-bits */
172 extend_key_56_to_64(key_56, (char *) &key);
174 /* Set the key parity to odd */
175 DES_set_odd_parity(&key);
178 DES_set_key(&key, ks);
181 #elif defined(USE_GNUTLS_NETTLE)
183 static void setup_des_key(const unsigned char *key_56,
188 /* Expand the 56-bit key to 64-bits */
189 extend_key_56_to_64(key_56, key);
191 /* Set the key parity to odd */
192 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
195 des_set_key(des, (const uint8_t *) key);
198 #elif defined(USE_GNUTLS)
201 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
203 static void setup_des_key(const unsigned char *key_56,
204 gcry_cipher_hd_t *des)
208 /* Expand the 56-bit key to 64-bits */
209 extend_key_56_to_64(key_56, key);
211 /* Set the key parity to odd */
212 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
215 gcry_cipher_setkey(*des, key, sizeof(key));
218 #elif defined(USE_NSS)
221 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
222 * the expanded key. The caller is responsible for giving 64 bit of valid
223 * data is IN and (at least) 64 bit large buffer as OUT.
225 static bool encrypt_des(const unsigned char *in, unsigned char *out,
226 const unsigned char *key_56)
228 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
229 PK11SlotInfo *slot = NULL;
230 char key[8]; /* expanded 64 bit key */
232 PK11SymKey *symkey = NULL;
233 SECItem *param = NULL;
234 PK11Context *ctx = NULL;
235 int out_len; /* not used, required by NSS */
238 /* use internal slot for DES encryption (requires NSS to be initialized) */
239 slot = PK11_GetInternalKeySlot();
243 /* Expand the 56-bit key to 64-bits */
244 extend_key_56_to_64(key_56, key);
246 /* Set the key parity to odd */
247 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
250 key_item.data = (unsigned char *)key;
251 key_item.len = sizeof(key);
252 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
257 /* Create the DES encryption context */
258 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
261 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
265 /* Perform the encryption */
266 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
267 (unsigned char *)in, /* inbuflen */ 8)
268 && SECSuccess == PK11_Finalize(ctx))
269 rv = /* all OK */ TRUE;
274 PK11_DestroyContext(ctx, PR_TRUE);
276 PK11_FreeSymKey(symkey);
278 SECITEM_FreeItem(param, PR_TRUE);
283 #elif defined(USE_MBEDTLS)
285 static bool encrypt_des(const unsigned char *in, unsigned char *out,
286 const unsigned char *key_56)
288 mbedtls_des_context ctx;
291 /* Expand the 56-bit key to 64-bits */
292 extend_key_56_to_64(key_56, key);
294 /* Set the key parity to odd */
295 mbedtls_des_key_set_parity((unsigned char *) key);
297 /* Perform the encryption */
298 mbedtls_des_init(&ctx);
299 mbedtls_des_setkey_enc(&ctx, (unsigned char *) key);
300 return mbedtls_des_crypt_ecb(&ctx, in, out) == 0;
303 #elif defined(USE_DARWINSSL)
305 static bool encrypt_des(const unsigned char *in, unsigned char *out,
306 const unsigned char *key_56)
312 /* Expand the 56-bit key to 64-bits */
313 extend_key_56_to_64(key_56, key);
315 /* Set the key parity to odd */
316 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
318 /* Perform the encryption */
319 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
320 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
321 8 /* outbuflen */, &out_len);
323 return err == kCCSuccess;
326 #elif defined(USE_OS400CRYPTO)
328 static bool encrypt_des(const unsigned char *in, unsigned char *out,
329 const unsigned char *key_56)
332 _CIPHER_Control_T ctl;
334 /* Setup the cipher control structure */
335 ctl.Func_ID = ENCRYPT_ONLY;
336 ctl.Data_Len = sizeof(key);
338 /* Expand the 56-bit key to 64-bits */
339 extend_key_56_to_64(key_56, ctl.Crypto_Key);
341 /* Set the key parity to odd */
342 Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
344 /* Perform the encryption */
345 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
350 #elif defined(USE_WIN32_CRYPTO)
352 static bool encrypt_des(const unsigned char *in, unsigned char *out,
353 const unsigned char *key_56)
364 /* Acquire the crypto provider */
365 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
366 CRYPT_VERIFYCONTEXT))
369 /* Setup the key blob structure */
370 memset(&blob, 0, sizeof(blob));
371 blob.hdr.bType = PLAINTEXTKEYBLOB;
372 blob.hdr.bVersion = 2;
373 blob.hdr.aiKeyAlg = CALG_DES;
374 blob.len = sizeof(blob.key);
376 /* Expand the 56-bit key to 64-bits */
377 extend_key_56_to_64(key_56, blob.key);
379 /* Set the key parity to odd */
380 Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
383 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
384 CryptReleaseContext(hprov, 0);
391 /* Perform the encryption */
392 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
394 CryptDestroyKey(hkey);
395 CryptReleaseContext(hprov, 0);
400 #endif /* defined(USE_WIN32_CRYPTO) */
403 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
404 * 8 byte plaintext is encrypted with each key and the resulting 24
405 * bytes are stored in the results array.
407 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
408 const unsigned char *plaintext,
409 unsigned char *results)
414 setup_des_key(keys, DESKEY(ks));
415 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
416 DESKEY(ks), DES_ENCRYPT);
418 setup_des_key(keys + 7, DESKEY(ks));
419 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
420 DESKEY(ks), DES_ENCRYPT);
422 setup_des_key(keys + 14, DESKEY(ks));
423 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
424 DESKEY(ks), DES_ENCRYPT);
425 #elif defined(USE_GNUTLS_NETTLE)
427 setup_des_key(keys, &des);
428 des_encrypt(&des, 8, results, plaintext);
429 setup_des_key(keys + 7, &des);
430 des_encrypt(&des, 8, results + 8, plaintext);
431 setup_des_key(keys + 14, &des);
432 des_encrypt(&des, 8, results + 16, plaintext);
433 #elif defined(USE_GNUTLS)
434 gcry_cipher_hd_t des;
436 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
437 setup_des_key(keys, &des);
438 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
439 gcry_cipher_close(des);
441 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
442 setup_des_key(keys + 7, &des);
443 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
444 gcry_cipher_close(des);
446 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
447 setup_des_key(keys + 14, &des);
448 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
449 gcry_cipher_close(des);
450 #elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_DARWINSSL) \
451 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
452 encrypt_des(plaintext, results, keys);
453 encrypt_des(plaintext, results + 8, keys + 7);
454 encrypt_des(plaintext, results + 16, keys + 14);
459 * Set up lanmanager hashed password
461 CURLcode Curl_ntlm_core_mk_lm_hash(struct Curl_easy *data,
462 const char *password,
463 unsigned char *lmbuffer /* 21 bytes */)
466 unsigned char pw[14];
467 static const unsigned char magic[] = {
468 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
470 size_t len = CURLMIN(strlen(password), 14);
472 Curl_strntoupper((char *)pw, password, len);
473 memset(&pw[len], 0, 14 - len);
476 * The LanManager hashed password needs to be created using the
477 * password in the network encoding not the host encoding.
479 result = Curl_convert_to_network(data, (char *)pw, 14);
484 /* Create LanManager hashed password. */
489 setup_des_key(pw, DESKEY(ks));
490 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
491 DESKEY(ks), DES_ENCRYPT);
493 setup_des_key(pw + 7, DESKEY(ks));
494 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
495 DESKEY(ks), DES_ENCRYPT);
496 #elif defined(USE_GNUTLS_NETTLE)
498 setup_des_key(pw, &des);
499 des_encrypt(&des, 8, lmbuffer, magic);
500 setup_des_key(pw + 7, &des);
501 des_encrypt(&des, 8, lmbuffer + 8, magic);
502 #elif defined(USE_GNUTLS)
503 gcry_cipher_hd_t des;
505 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
506 setup_des_key(pw, &des);
507 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
508 gcry_cipher_close(des);
510 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
511 setup_des_key(pw + 7, &des);
512 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
513 gcry_cipher_close(des);
514 #elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_DARWINSSL) \
515 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
516 encrypt_des(magic, lmbuffer, pw);
517 encrypt_des(magic, lmbuffer + 8, pw + 7);
520 memset(lmbuffer + 16, 0, 21 - 16);
526 #ifdef USE_NTRESPONSES
527 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
531 for(i = 0; i < srclen; i++) {
532 dest[2 * i] = (unsigned char)src[i];
533 dest[2 * i + 1] = '\0';
537 #if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
539 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
540 const char *src, size_t srclen)
543 for(i = 0; i < srclen; i++) {
544 dest[2 * i] = (unsigned char)(Curl_raw_toupper(src[i]));
545 dest[2 * i + 1] = '\0';
549 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
552 * Set up nt hashed passwords
555 CURLcode Curl_ntlm_core_mk_nt_hash(struct Curl_easy *data,
556 const char *password,
557 unsigned char *ntbuffer /* 21 bytes */)
559 size_t len = strlen(password);
560 unsigned char *pw = len ? malloc(len * 2) : strdup("");
563 return CURLE_OUT_OF_MEMORY;
565 ascii_to_unicode_le(pw, password, len);
568 * The NT hashed password needs to be created using the password in the
569 * network encoding not the host encoding.
571 result = Curl_convert_to_network(data, (char *)pw, len * 2);
576 /* Create NT hashed password. */
580 MD4_Update(&MD4pw, pw, 2 * len);
581 MD4_Final(ntbuffer, &MD4pw);
582 #elif defined(USE_GNUTLS_NETTLE)
583 struct md4_ctx MD4pw;
585 md4_update(&MD4pw, (unsigned int)(2 * len), pw);
586 md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
587 #elif defined(USE_GNUTLS)
589 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
590 gcry_md_write(MD4pw, pw, 2 * len);
591 memcpy(ntbuffer, gcry_md_read(MD4pw, 0), MD4_DIGEST_LENGTH);
592 gcry_md_close(MD4pw);
593 #elif defined(USE_NSS)
594 Curl_md4it(ntbuffer, pw, 2 * len);
595 #elif defined(USE_MBEDTLS)
596 #if defined(MBEDTLS_MD4_C)
597 mbedtls_md4(pw, 2 * len, ntbuffer);
599 Curl_md4it(ntbuffer, pw, 2 * len);
601 #elif defined(USE_DARWINSSL)
602 (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
603 #elif defined(USE_OS400CRYPTO)
604 Curl_md4it(ntbuffer, pw, 2 * len);
605 #elif defined(USE_WIN32_CRYPTO)
607 if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
608 CRYPT_VERIFYCONTEXT)) {
610 if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
612 CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
613 CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
614 CryptDestroyHash(hhash);
616 CryptReleaseContext(hprov, 0);
620 memset(ntbuffer + 16, 0, 21 - 16);
628 #if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
630 /* This returns the HMAC MD5 digest */
631 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
632 const unsigned char *data, unsigned int datalen,
633 unsigned char *output)
635 HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
638 return CURLE_OUT_OF_MEMORY;
640 /* Update the digest with the given challenge */
641 Curl_HMAC_update(ctxt, data, datalen);
643 /* Finalise the digest */
644 Curl_HMAC_final(ctxt, output);
650 /* some limits.h headers have this defined, some don't */
651 #if defined(SIZEOF_SIZE_T) && (SIZEOF_SIZE_T > 4)
652 #define SIZE_T_MAX 18446744073709551615U
654 #define SIZE_T_MAX 4294967295U
658 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
659 * (uppercase UserName + Domain) as the data
661 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
662 const char *domain, size_t domlen,
663 unsigned char *ntlmhash,
664 unsigned char *ntlmv2hash)
666 /* Unicode representation */
668 unsigned char *identity;
669 CURLcode result = CURLE_OK;
671 /* we do the length checks below separately to avoid integer overflow risk
672 on extreme data lengths */
673 if((userlen > SIZE_T_MAX/2) ||
674 (domlen > SIZE_T_MAX/2) ||
675 ((userlen + domlen) > SIZE_T_MAX/2))
676 return CURLE_OUT_OF_MEMORY;
678 identity_len = (userlen + domlen) * 2;
679 identity = malloc(identity_len);
682 return CURLE_OUT_OF_MEMORY;
684 ascii_uppercase_to_unicode_le(identity, user, userlen);
685 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
687 result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
696 * Curl_ntlm_core_mk_ntlmv2_resp()
698 * This creates the NTLMv2 response as set in the ntlm type-3 message.
702 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
703 * challenge_client [in] - The client nonce (8 bytes)
704 * ntlm [in] - The ntlm data struct being used to read TargetInfo
705 and Server challenge received in the type-2 message
706 * ntresp [out] - The address where a pointer to newly allocated
707 * memory holding the NTLMv2 response.
708 * ntresp_len [out] - The length of the output message.
710 * Returns CURLE_OK on success.
712 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
713 unsigned char *challenge_client,
714 struct ntlmdata *ntlm,
715 unsigned char **ntresp,
716 unsigned int *ntresp_len)
718 /* NTLMv2 response structure :
719 ------------------------------------------------------------------------------
721 ------BLOB--------------------------------------------------------------------
722 16 Signature 0x01010000
723 20 Reserved long (0x00000000)
724 24 Timestamp LE, 64-bit signed value representing the number of
725 tenths of a microsecond since January 1, 1601.
726 32 Client Nonce 8 bytes
728 44 Target Info N bytes (from the type-2 message)
730 ------------------------------------------------------------------------------
733 unsigned int len = 0;
734 unsigned char *ptr = NULL;
735 unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
738 CURLcode result = CURLE_OK;
740 #if CURL_SIZEOF_CURL_OFF_T < 8
741 #error "this section needs 64bit support to work"
744 /* Calculate the timestamp */
746 char *force_timestamp = getenv("CURL_FORCETIME");
748 tw = CURL_OFF_T_C(11644473600) * 10000000;
751 tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
753 /* Calculate the response len */
754 len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
756 /* Allocate the response */
759 return CURLE_OUT_OF_MEMORY;
763 /* Create the BLOB structure */
764 snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
765 "%c%c%c%c" /* NTLMv2_BLOB_SIGNATURE */
766 "%c%c%c%c", /* Reserved = 0 */
767 NTLMv2_BLOB_SIGNATURE[0], NTLMv2_BLOB_SIGNATURE[1],
768 NTLMv2_BLOB_SIGNATURE[2], NTLMv2_BLOB_SIGNATURE[3],
771 Curl_write64_le(tw, ptr + 24);
772 memcpy(ptr + 32, challenge_client, 8);
773 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
775 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
776 memcpy(ptr + 8, &ntlm->nonce[0], 8);
777 result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
778 NTLMv2_BLOB_LEN + 8, hmac_output);
784 /* Concatenate the HMAC MD5 output with the BLOB */
785 memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
787 /* Return the response */
795 * Curl_ntlm_core_mk_lmv2_resp()
797 * This creates the LMv2 response as used in the ntlm type-3 message.
801 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
802 * challenge_client [in] - The client nonce (8 bytes)
803 * challenge_client [in] - The server challenge (8 bytes)
804 * lmresp [out] - The LMv2 response (24 bytes)
806 * Returns CURLE_OK on success.
808 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
809 unsigned char *challenge_client,
810 unsigned char *challenge_server,
811 unsigned char *lmresp)
813 unsigned char data[16];
814 unsigned char hmac_output[16];
815 CURLcode result = CURLE_OK;
817 memcpy(&data[0], challenge_server, 8);
818 memcpy(&data[8], challenge_client, 8);
820 result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
824 /* Concatenate the HMAC MD5 output with the client nonce */
825 memcpy(lmresp, hmac_output, 16);
826 memcpy(lmresp + 16, challenge_client, 8);
831 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
833 #endif /* USE_NTRESPONSES */
835 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
837 #endif /* USE_NTLM */