1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if defined(USE_CURL_NTLM_CORE)
28
29 /*
30 * NTLM details:
31 *
32 * https://davenport.sourceforge.net/ntlm.html
33 * https://www.innovation.ch/java/ntlm.html
34 */
35
36 /* Please keep the SSL backend-specific #if branches in this order:
37
38 1. USE_OPENSSL
39 2. USE_WOLFSSL
40 3. USE_GNUTLS
41 4. -
42 5. USE_MBEDTLS
43 6. USE_SECTRANSP
44 7. USE_OS400CRYPTO
45 8. USE_WIN32_CRYPTO
46
47 This ensures that:
48 - the same SSL branch gets activated throughout this source
49 file even if multiple backends are enabled at the same time.
50 - OpenSSL has higher priority than Windows Crypt, due
51 to issues with the latter supporting NTLM2Session responses
52 in NTLM type-3 messages.
53 */
54
55 #if defined(USE_OPENSSL)
56 #include <openssl/opensslconf.h>
57 #if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_DEPRECATED_3_0)
58 #define USE_OPENSSL_DES
59 #endif
60 #endif
61
62 #if defined(USE_OPENSSL_DES) || defined(USE_WOLFSSL)
63
64 #if defined(USE_OPENSSL)
65 # include <openssl/des.h>
66 # include <openssl/md5.h>
67 # include <openssl/ssl.h>
68 # include <openssl/rand.h>
69 #else
70 # include <wolfssl/options.h>
71 # include <wolfssl/openssl/des.h>
72 # include <wolfssl/openssl/md5.h>
73 # include <wolfssl/openssl/ssl.h>
74 # include <wolfssl/openssl/rand.h>
75 #endif
76
77 # if (defined(OPENSSL_VERSION_NUMBER) && \
78 (OPENSSL_VERSION_NUMBER < 0x00907001L)) && !defined(USE_WOLFSSL)
79 # define DES_key_schedule des_key_schedule
80 # define DES_cblock des_cblock
81 # define DES_set_odd_parity des_set_odd_parity
82 # define DES_set_key des_set_key
83 # define DES_ecb_encrypt des_ecb_encrypt
84 # define DESKEY(x) x
85 # define DESKEYARG(x) x
86 # elif defined(OPENSSL_IS_AWSLC)
87 # define DES_set_key_unchecked (void)DES_set_key
88 # define DESKEYARG(x) *x
89 # define DESKEY(x) &x
90 # else
91 # define DESKEYARG(x) *x
92 # define DESKEY(x) &x
93 # endif
94
95 #elif defined(USE_GNUTLS)
96
97 # include <nettle/des.h>
98
99 #elif defined(USE_MBEDTLS)
100
101 # include <mbedtls/des.h>
102
103 #elif defined(USE_SECTRANSP)
104
105 # include <CommonCrypto/CommonCryptor.h>
106 # include <CommonCrypto/CommonDigest.h>
107
108 #elif defined(USE_OS400CRYPTO)
109 # include "cipher.mih" /* mih/cipher */
110 #elif defined(USE_WIN32_CRYPTO)
111 # include <wincrypt.h>
112 #else
113 # error "Can't compile NTLM support without a crypto library with DES."
114 #endif
115
116 #include "urldata.h"
117 #include "strcase.h"
118 #include "curl_ntlm_core.h"
119 #include "curl_md5.h"
120 #include "curl_hmac.h"
121 #include "warnless.h"
122 #include "curl_endian.h"
123 #include "curl_des.h"
124 #include "curl_md4.h"
125 /* The last 3 #include files should be in this order */
126 #include "curl_printf.h"
127 #include "curl_memory.h"
128 #include "memdebug.h"
129
130 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
131 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
132
133 /*
134 * Turns a 56-bit key into being 64-bit wide.
135 */
extend_key_56_to_64(const unsigned char * key_56,char * key)136 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
137 {
138 key[0] = key_56[0];
139 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
140 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
141 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
142 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
143 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
144 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
145 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
146 }
147
148 #if defined(USE_OPENSSL_DES) || defined(USE_WOLFSSL)
149 /*
150 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
151 * key schedule ks is also set.
152 */
setup_des_key(const unsigned char * key_56,DES_key_schedule DESKEYARG (ks))153 static void setup_des_key(const unsigned char *key_56,
154 DES_key_schedule DESKEYARG(ks))
155 {
156 DES_cblock key;
157
158 /* Expand the 56-bit key to 64-bits */
159 extend_key_56_to_64(key_56, (char *) &key);
160
161 /* Set the key parity to odd */
162 DES_set_odd_parity(&key);
163
164 /* Set the key */
165 DES_set_key_unchecked(&key, ks);
166 }
167
168 #elif defined(USE_GNUTLS)
169
setup_des_key(const unsigned char * key_56,struct des_ctx * des)170 static void setup_des_key(const unsigned char *key_56,
171 struct des_ctx *des)
172 {
173 char key[8];
174
175 /* Expand the 56-bit key to 64-bits */
176 extend_key_56_to_64(key_56, key);
177
178 /* Set the key parity to odd */
179 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
180
181 /* Set the key */
182 des_set_key(des, (const uint8_t *) key);
183 }
184
185 #elif defined(USE_MBEDTLS)
186
encrypt_des(const unsigned char * in,unsigned char * out,const unsigned char * key_56)187 static bool encrypt_des(const unsigned char *in, unsigned char *out,
188 const unsigned char *key_56)
189 {
190 mbedtls_des_context ctx;
191 char key[8];
192
193 /* Expand the 56-bit key to 64-bits */
194 extend_key_56_to_64(key_56, key);
195
196 /* Set the key parity to odd */
197 mbedtls_des_key_set_parity((unsigned char *) key);
198
199 /* Perform the encryption */
200 mbedtls_des_init(&ctx);
201 mbedtls_des_setkey_enc(&ctx, (unsigned char *) key);
202 return mbedtls_des_crypt_ecb(&ctx, in, out) == 0;
203 }
204
205 #elif defined(USE_SECTRANSP)
206
encrypt_des(const unsigned char * in,unsigned char * out,const unsigned char * key_56)207 static bool encrypt_des(const unsigned char *in, unsigned char *out,
208 const unsigned char *key_56)
209 {
210 char key[8];
211 size_t out_len;
212 CCCryptorStatus err;
213
214 /* Expand the 56-bit key to 64-bits */
215 extend_key_56_to_64(key_56, key);
216
217 /* Set the key parity to odd */
218 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
219
220 /* Perform the encryption */
221 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
222 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
223 8 /* outbuflen */, &out_len);
224
225 return err == kCCSuccess;
226 }
227
228 #elif defined(USE_OS400CRYPTO)
229
encrypt_des(const unsigned char * in,unsigned char * out,const unsigned char * key_56)230 static bool encrypt_des(const unsigned char *in, unsigned char *out,
231 const unsigned char *key_56)
232 {
233 char key[8];
234 _CIPHER_Control_T ctl;
235
236 /* Setup the cipher control structure */
237 ctl.Func_ID = ENCRYPT_ONLY;
238 ctl.Data_Len = sizeof(key);
239
240 /* Expand the 56-bit key to 64-bits */
241 extend_key_56_to_64(key_56, ctl.Crypto_Key);
242
243 /* Set the key parity to odd */
244 Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
245
246 /* Perform the encryption */
247 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
248
249 return TRUE;
250 }
251
252 #elif defined(USE_WIN32_CRYPTO)
253
encrypt_des(const unsigned char * in,unsigned char * out,const unsigned char * key_56)254 static bool encrypt_des(const unsigned char *in, unsigned char *out,
255 const unsigned char *key_56)
256 {
257 HCRYPTPROV hprov;
258 HCRYPTKEY hkey;
259 struct {
260 BLOBHEADER hdr;
261 unsigned int len;
262 char key[8];
263 } blob;
264 DWORD len = 8;
265
266 /* Acquire the crypto provider */
267 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
268 CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
269 return FALSE;
270
271 /* Setup the key blob structure */
272 memset(&blob, 0, sizeof(blob));
273 blob.hdr.bType = PLAINTEXTKEYBLOB;
274 blob.hdr.bVersion = 2;
275 blob.hdr.aiKeyAlg = CALG_DES;
276 blob.len = sizeof(blob.key);
277
278 /* Expand the 56-bit key to 64-bits */
279 extend_key_56_to_64(key_56, blob.key);
280
281 /* Set the key parity to odd */
282 Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
283
284 /* Import the key */
285 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
286 CryptReleaseContext(hprov, 0);
287
288 return FALSE;
289 }
290
291 memcpy(out, in, 8);
292
293 /* Perform the encryption */
294 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
295
296 CryptDestroyKey(hkey);
297 CryptReleaseContext(hprov, 0);
298
299 return TRUE;
300 }
301
302 #endif /* defined(USE_WIN32_CRYPTO) */
303
304 /*
305 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
306 * 8 byte plaintext is encrypted with each key and the resulting 24
307 * bytes are stored in the results array.
308 */
Curl_ntlm_core_lm_resp(const unsigned char * keys,const unsigned char * plaintext,unsigned char * results)309 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
310 const unsigned char *plaintext,
311 unsigned char *results)
312 {
313 #if defined(USE_OPENSSL_DES) || defined(USE_WOLFSSL)
314 DES_key_schedule ks;
315
316 setup_des_key(keys, DESKEY(ks));
317 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
318 DESKEY(ks), DES_ENCRYPT);
319
320 setup_des_key(keys + 7, DESKEY(ks));
321 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
322 DESKEY(ks), DES_ENCRYPT);
323
324 setup_des_key(keys + 14, DESKEY(ks));
325 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
326 DESKEY(ks), DES_ENCRYPT);
327 #elif defined(USE_GNUTLS)
328 struct des_ctx des;
329 setup_des_key(keys, &des);
330 des_encrypt(&des, 8, results, plaintext);
331 setup_des_key(keys + 7, &des);
332 des_encrypt(&des, 8, results + 8, plaintext);
333 setup_des_key(keys + 14, &des);
334 des_encrypt(&des, 8, results + 16, plaintext);
335 #elif defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
336 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
337 encrypt_des(plaintext, results, keys);
338 encrypt_des(plaintext, results + 8, keys + 7);
339 encrypt_des(plaintext, results + 16, keys + 14);
340 #endif
341 }
342
343 /*
344 * Set up lanmanager hashed password
345 */
Curl_ntlm_core_mk_lm_hash(const char * password,unsigned char * lmbuffer)346 CURLcode Curl_ntlm_core_mk_lm_hash(const char *password,
347 unsigned char *lmbuffer /* 21 bytes */)
348 {
349 unsigned char pw[14];
350 static const unsigned char magic[] = {
351 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
352 };
353 size_t len = CURLMIN(strlen(password), 14);
354
355 Curl_strntoupper((char *)pw, password, len);
356 memset(&pw[len], 0, 14 - len);
357
358 {
359 /* Create LanManager hashed password. */
360
361 #if defined(USE_OPENSSL_DES) || defined(USE_WOLFSSL)
362 DES_key_schedule ks;
363
364 setup_des_key(pw, DESKEY(ks));
365 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
366 DESKEY(ks), DES_ENCRYPT);
367
368 setup_des_key(pw + 7, DESKEY(ks));
369 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
370 DESKEY(ks), DES_ENCRYPT);
371 #elif defined(USE_GNUTLS)
372 struct des_ctx des;
373 setup_des_key(pw, &des);
374 des_encrypt(&des, 8, lmbuffer, magic);
375 setup_des_key(pw + 7, &des);
376 des_encrypt(&des, 8, lmbuffer + 8, magic);
377 #elif defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
378 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
379 encrypt_des(magic, lmbuffer, pw);
380 encrypt_des(magic, lmbuffer + 8, pw + 7);
381 #endif
382
383 memset(lmbuffer + 16, 0, 21 - 16);
384 }
385
386 return CURLE_OK;
387 }
388
ascii_to_unicode_le(unsigned char * dest,const char * src,size_t srclen)389 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
390 size_t srclen)
391 {
392 size_t i;
393 for(i = 0; i < srclen; i++) {
394 dest[2 * i] = (unsigned char)src[i];
395 dest[2 * i + 1] = '\0';
396 }
397 }
398
399 #if !defined(USE_WINDOWS_SSPI)
400
ascii_uppercase_to_unicode_le(unsigned char * dest,const char * src,size_t srclen)401 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
402 const char *src, size_t srclen)
403 {
404 size_t i;
405 for(i = 0; i < srclen; i++) {
406 dest[2 * i] = (unsigned char)(Curl_raw_toupper(src[i]));
407 dest[2 * i + 1] = '\0';
408 }
409 }
410
411 #endif /* !USE_WINDOWS_SSPI */
412
413 /*
414 * Set up nt hashed passwords
415 * @unittest: 1600
416 */
Curl_ntlm_core_mk_nt_hash(const char * password,unsigned char * ntbuffer)417 CURLcode Curl_ntlm_core_mk_nt_hash(const char *password,
418 unsigned char *ntbuffer /* 21 bytes */)
419 {
420 size_t len = strlen(password);
421 unsigned char *pw;
422 CURLcode result;
423 if(len > SIZE_T_MAX/2) /* avoid integer overflow */
424 return CURLE_OUT_OF_MEMORY;
425 pw = len ? malloc(len * 2) : (unsigned char *)strdup("");
426 if(!pw)
427 return CURLE_OUT_OF_MEMORY;
428
429 ascii_to_unicode_le(pw, password, len);
430
431 /* Create NT hashed password. */
432 result = Curl_md4it(ntbuffer, pw, 2 * len);
433 if(!result)
434 memset(ntbuffer + 16, 0, 21 - 16);
435
436 free(pw);
437
438 return result;
439 }
440
441 #if !defined(USE_WINDOWS_SSPI)
442
443 /* Timestamp in tenths of a microsecond since January 1, 1601 00:00:00 UTC. */
444 struct ms_filetime {
445 unsigned int dwLowDateTime;
446 unsigned int dwHighDateTime;
447 };
448
449 /* Convert a time_t to an MS FILETIME (MS-DTYP section 2.3.3). */
time2filetime(struct ms_filetime * ft,time_t t)450 static void time2filetime(struct ms_filetime *ft, time_t t)
451 {
452 #if SIZEOF_TIME_T > 4
453 t = (t + CURL_OFF_T_C(11644473600)) * 10000000;
454 ft->dwLowDateTime = (unsigned int) (t & 0xFFFFFFFF);
455 ft->dwHighDateTime = (unsigned int) (t >> 32);
456 #else
457 unsigned int r, s;
458 unsigned int i;
459
460 ft->dwLowDateTime = t & 0xFFFFFFFF;
461 ft->dwHighDateTime = 0;
462
463 # ifndef HAVE_TIME_T_UNSIGNED
464 /* Extend sign if needed. */
465 if(ft->dwLowDateTime & 0x80000000)
466 ft->dwHighDateTime = ~0;
467 # endif
468
469 /* Bias seconds to Jan 1, 1601.
470 134774 days = 11644473600 seconds = 0x2B6109100 */
471 r = ft->dwLowDateTime;
472 ft->dwLowDateTime = (ft->dwLowDateTime + 0xB6109100U) & 0xFFFFFFFF;
473 ft->dwHighDateTime += ft->dwLowDateTime < r? 0x03: 0x02;
474
475 /* Convert to tenths of microseconds. */
476 ft->dwHighDateTime *= 10000000;
477 i = 32;
478 do {
479 i -= 8;
480 s = ((ft->dwLowDateTime >> i) & 0xFF) * (10000000 - 1);
481 r = (s << i) & 0xFFFFFFFF;
482 s >>= 1; /* Split shift to avoid width overflow. */
483 s >>= 31 - i;
484 ft->dwLowDateTime = (ft->dwLowDateTime + r) & 0xFFFFFFFF;
485 if(ft->dwLowDateTime < r)
486 s++;
487 ft->dwHighDateTime += s;
488 } while(i);
489 ft->dwHighDateTime &= 0xFFFFFFFF;
490 #endif
491 }
492
493 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
494 * (uppercase UserName + Domain) as the data
495 */
Curl_ntlm_core_mk_ntlmv2_hash(const char * user,size_t userlen,const char * domain,size_t domlen,unsigned char * ntlmhash,unsigned char * ntlmv2hash)496 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
497 const char *domain, size_t domlen,
498 unsigned char *ntlmhash,
499 unsigned char *ntlmv2hash)
500 {
501 /* Unicode representation */
502 size_t identity_len;
503 unsigned char *identity;
504 CURLcode result = CURLE_OK;
505
506 if((userlen > CURL_MAX_INPUT_LENGTH) || (domlen > CURL_MAX_INPUT_LENGTH))
507 return CURLE_OUT_OF_MEMORY;
508
509 identity_len = (userlen + domlen) * 2;
510 identity = malloc(identity_len + 1);
511
512 if(!identity)
513 return CURLE_OUT_OF_MEMORY;
514
515 ascii_uppercase_to_unicode_le(identity, user, userlen);
516 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
517
518 result = Curl_hmacit(Curl_HMAC_MD5, ntlmhash, 16, identity, identity_len,
519 ntlmv2hash);
520 free(identity);
521
522 return result;
523 }
524
525 /*
526 * Curl_ntlm_core_mk_ntlmv2_resp()
527 *
528 * This creates the NTLMv2 response as set in the ntlm type-3 message.
529 *
530 * Parameters:
531 *
532 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
533 * challenge_client [in] - The client nonce (8 bytes)
534 * ntlm [in] - The ntlm data struct being used to read TargetInfo
535 and Server challenge received in the type-2 message
536 * ntresp [out] - The address where a pointer to newly allocated
537 * memory holding the NTLMv2 response.
538 * ntresp_len [out] - The length of the output message.
539 *
540 * Returns CURLE_OK on success.
541 */
Curl_ntlm_core_mk_ntlmv2_resp(unsigned char * ntlmv2hash,unsigned char * challenge_client,struct ntlmdata * ntlm,unsigned char ** ntresp,unsigned int * ntresp_len)542 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
543 unsigned char *challenge_client,
544 struct ntlmdata *ntlm,
545 unsigned char **ntresp,
546 unsigned int *ntresp_len)
547 {
548 /* NTLMv2 response structure :
549 ------------------------------------------------------------------------------
550 0 HMAC MD5 16 bytes
551 ------BLOB--------------------------------------------------------------------
552 16 Signature 0x01010000
553 20 Reserved long (0x00000000)
554 24 Timestamp LE, 64-bit signed value representing the number of
555 tenths of a microsecond since January 1, 1601.
556 32 Client Nonce 8 bytes
557 40 Unknown 4 bytes
558 44 Target Info N bytes (from the type-2 message)
559 44+N Unknown 4 bytes
560 ------------------------------------------------------------------------------
561 */
562
563 unsigned int len = 0;
564 unsigned char *ptr = NULL;
565 unsigned char hmac_output[HMAC_MD5_LENGTH];
566 struct ms_filetime tw;
567
568 CURLcode result = CURLE_OK;
569
570 /* Calculate the timestamp */
571 #ifdef DEBUGBUILD
572 char *force_timestamp = getenv("CURL_FORCETIME");
573 if(force_timestamp)
574 time2filetime(&tw, (time_t) 0);
575 else
576 #endif
577 time2filetime(&tw, time(NULL));
578
579 /* Calculate the response len */
580 len = HMAC_MD5_LENGTH + NTLMv2_BLOB_LEN;
581
582 /* Allocate the response */
583 ptr = calloc(1, len);
584 if(!ptr)
585 return CURLE_OUT_OF_MEMORY;
586
587 /* Create the BLOB structure */
588 msnprintf((char *)ptr + HMAC_MD5_LENGTH, NTLMv2_BLOB_LEN,
589 "%c%c%c%c" /* NTLMv2_BLOB_SIGNATURE */
590 "%c%c%c%c" /* Reserved = 0 */
591 "%c%c%c%c%c%c%c%c", /* Timestamp */
592 NTLMv2_BLOB_SIGNATURE[0], NTLMv2_BLOB_SIGNATURE[1],
593 NTLMv2_BLOB_SIGNATURE[2], NTLMv2_BLOB_SIGNATURE[3],
594 0, 0, 0, 0,
595 LONGQUARTET(tw.dwLowDateTime), LONGQUARTET(tw.dwHighDateTime));
596
597 memcpy(ptr + 32, challenge_client, 8);
598 if(ntlm->target_info_len)
599 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
600
601 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
602 memcpy(ptr + 8, &ntlm->nonce[0], 8);
603 result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, HMAC_MD5_LENGTH, ptr + 8,
604 NTLMv2_BLOB_LEN + 8, hmac_output);
605 if(result) {
606 free(ptr);
607 return result;
608 }
609
610 /* Concatenate the HMAC MD5 output with the BLOB */
611 memcpy(ptr, hmac_output, HMAC_MD5_LENGTH);
612
613 /* Return the response */
614 *ntresp = ptr;
615 *ntresp_len = len;
616
617 return result;
618 }
619
620 /*
621 * Curl_ntlm_core_mk_lmv2_resp()
622 *
623 * This creates the LMv2 response as used in the ntlm type-3 message.
624 *
625 * Parameters:
626 *
627 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
628 * challenge_client [in] - The client nonce (8 bytes)
629 * challenge_client [in] - The server challenge (8 bytes)
630 * lmresp [out] - The LMv2 response (24 bytes)
631 *
632 * Returns CURLE_OK on success.
633 */
Curl_ntlm_core_mk_lmv2_resp(unsigned char * ntlmv2hash,unsigned char * challenge_client,unsigned char * challenge_server,unsigned char * lmresp)634 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
635 unsigned char *challenge_client,
636 unsigned char *challenge_server,
637 unsigned char *lmresp)
638 {
639 unsigned char data[16];
640 unsigned char hmac_output[16];
641 CURLcode result = CURLE_OK;
642
643 memcpy(&data[0], challenge_server, 8);
644 memcpy(&data[8], challenge_client, 8);
645
646 result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, 16, &data[0], 16,
647 hmac_output);
648 if(result)
649 return result;
650
651 /* Concatenate the HMAC MD5 output with the client nonce */
652 memcpy(lmresp, hmac_output, 16);
653 memcpy(lmresp + 16, challenge_client, 8);
654
655 return result;
656 }
657
658 #endif /* !USE_WINDOWS_SSPI */
659
660 #endif /* USE_CURL_NTLM_CORE */
661