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