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