Revert "Update to 7.44.0"
[platform/upstream/curl.git] / lib / curl_ntlm_core.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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 http://curl.haxx.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  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if defined(USE_NTLM)
26
27 /*
28  * NTLM details:
29  *
30  * http://davenport.sourceforge.net/ntlm.html
31  * http://www.innovation.ch/java/ntlm.html
32  */
33
34 #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
35
36 #ifdef USE_SSLEAY
37
38 #  ifdef USE_OPENSSL
39 #    include <openssl/des.h>
40 #    ifndef OPENSSL_NO_MD4
41 #      include <openssl/md4.h>
42 #    endif
43 #    include <openssl/md5.h>
44 #    include <openssl/ssl.h>
45 #    include <openssl/rand.h>
46 #  else
47 #    include <des.h>
48 #    ifndef OPENSSL_NO_MD4
49 #      include <md4.h>
50 #    endif
51 #    include <md5.h>
52 #    include <ssl.h>
53 #    include <rand.h>
54 #  endif
55 #  if (OPENSSL_VERSION_NUMBER < 0x00907001L)
56 #    define DES_key_schedule des_key_schedule
57 #    define DES_cblock des_cblock
58 #    define DES_set_odd_parity des_set_odd_parity
59 #    define DES_set_key des_set_key
60 #    define DES_ecb_encrypt des_ecb_encrypt
61 #    define DESKEY(x) x
62 #    define DESKEYARG(x) x
63 #  else
64 #    define DESKEYARG(x) *x
65 #    define DESKEY(x) &x
66 #  endif
67
68 #elif defined(USE_GNUTLS_NETTLE)
69
70 #  include <nettle/des.h>
71 #  include <nettle/md4.h>
72
73 #elif defined(USE_GNUTLS)
74
75 #  include <gcrypt.h>
76 #  define MD5_DIGEST_LENGTH 16
77 #  define MD4_DIGEST_LENGTH 16
78
79 #elif defined(USE_NSS)
80
81 #  include <nss.h>
82 #  include <pk11pub.h>
83 #  include <hasht.h>
84 #  include "curl_md4.h"
85 #  define MD5_DIGEST_LENGTH MD5_LENGTH
86
87 #elif defined(USE_DARWINSSL)
88
89 #  include <CommonCrypto/CommonCryptor.h>
90 #  include <CommonCrypto/CommonDigest.h>
91
92 #elif defined(USE_OS400CRYPTO)
93 #  include "cipher.mih"  /* mih/cipher */
94 #  include "curl_md4.h"
95 #elif defined(USE_WIN32_CRYPTO)
96 #  include <wincrypt.h>
97 #else
98 #  error "Can't compile NTLM support without a crypto library."
99 #endif
100
101 #include "urldata.h"
102 #include "non-ascii.h"
103 #include "rawstr.h"
104 #include "curl_memory.h"
105 #include "curl_ntlm_core.h"
106 #include "curl_md5.h"
107 #include "curl_hmac.h"
108 #include "warnless.h"
109 #include "curl_endian.h"
110
111 #define _MPRINTF_REPLACE /* use our functions only */
112 #include <curl/mprintf.h>
113
114 /* The last #include file should be: */
115 #include "memdebug.h"
116
117 #define NTLM_HMAC_MD5_LEN     (16)
118 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
119 #define NTLMv2_BLOB_LEN       (44 -16 + ntlm->target_info_len + 4)
120
121 /*
122 * Turns a 56-bit key into being 64-bit wide.
123 */
124 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
125 {
126   key[0] = key_56[0];
127   key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
128   key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
129   key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
130   key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
131   key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
132   key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
133   key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
134 }
135
136 #ifdef USE_SSLEAY
137 /*
138  * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.  The
139  * key schedule ks is also set.
140  */
141 static void setup_des_key(const unsigned char *key_56,
142                           DES_key_schedule DESKEYARG(ks))
143 {
144   DES_cblock key;
145
146   extend_key_56_to_64(key_56, (char *) key);
147   DES_set_odd_parity(&key);
148   DES_set_key(&key, ks);
149 }
150
151 #elif defined(USE_GNUTLS_NETTLE)
152
153 static void setup_des_key(const unsigned char *key_56,
154                           struct des_ctx *des)
155 {
156   char key[8];
157   extend_key_56_to_64(key_56, key);
158   des_set_key(des, (const uint8_t*)key);
159 }
160
161 #elif defined(USE_GNUTLS)
162
163 /*
164  * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
165  */
166 static void setup_des_key(const unsigned char *key_56,
167                           gcry_cipher_hd_t *des)
168 {
169   char key[8];
170   extend_key_56_to_64(key_56, key);
171   gcry_cipher_setkey(*des, key, 8);
172 }
173
174 #elif defined(USE_NSS)
175
176 /*
177  * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
178  * the expanded key.  The caller is responsible for giving 64 bit of valid
179  * data is IN and (at least) 64 bit large buffer as OUT.
180  */
181 static bool encrypt_des(const unsigned char *in, unsigned char *out,
182                         const unsigned char *key_56)
183 {
184   const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
185   PK11SlotInfo *slot = NULL;
186   char key[8];                                /* expanded 64 bit key */
187   SECItem key_item;
188   PK11SymKey *symkey = NULL;
189   SECItem *param = NULL;
190   PK11Context *ctx = NULL;
191   int out_len;                                /* not used, required by NSS */
192   bool rv = FALSE;
193
194   /* use internal slot for DES encryption (requires NSS to be initialized) */
195   slot = PK11_GetInternalKeySlot();
196   if(!slot)
197     return FALSE;
198
199   /* expand the 56 bit key to 64 bit and wrap by NSS */
200   extend_key_56_to_64(key_56, key);
201   key_item.data = (unsigned char *)key;
202   key_item.len = /* hard-wired */ 8;
203   symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
204                              &key_item, NULL);
205   if(!symkey)
206     goto fail;
207
208   /* create DES encryption context */
209   param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
210   if(!param)
211     goto fail;
212   ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
213   if(!ctx)
214     goto fail;
215
216   /* perform the encryption */
217   if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
218                                  (unsigned char *)in, /* inbuflen */ 8)
219       && SECSuccess == PK11_Finalize(ctx))
220     rv = /* all OK */ TRUE;
221
222 fail:
223   /* cleanup */
224   if(ctx)
225     PK11_DestroyContext(ctx, PR_TRUE);
226   if(symkey)
227     PK11_FreeSymKey(symkey);
228   if(param)
229     SECITEM_FreeItem(param, PR_TRUE);
230   PK11_FreeSlot(slot);
231   return rv;
232 }
233
234 #elif defined(USE_DARWINSSL)
235
236 static bool encrypt_des(const unsigned char *in, unsigned char *out,
237                         const unsigned char *key_56)
238 {
239   char key[8];
240   size_t out_len;
241   CCCryptorStatus err;
242
243   extend_key_56_to_64(key_56, key);
244   err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
245                 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
246                 8 /* outbuflen */, &out_len);
247   return err == kCCSuccess;
248 }
249
250 #elif defined(USE_OS400CRYPTO)
251
252 static bool encrypt_des(const unsigned char *in, unsigned char *out,
253                         const unsigned char *key_56)
254 {
255   char key[8];
256   _CIPHER_Control_T ctl;
257
258   ctl.Func_ID = ENCRYPT_ONLY;
259   ctl.Data_Len = 8;
260   extend_key_56_to_64(key_56, ctl.Crypto_Key);
261   _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
262   return TRUE;
263 }
264
265 #elif defined(USE_WIN32_CRYPTO)
266
267 static bool encrypt_des(const unsigned char *in, unsigned char *out,
268                         const unsigned char *key_56)
269 {
270   HCRYPTPROV hprov;
271   HCRYPTKEY hkey;
272   struct {
273     BLOBHEADER hdr;
274     unsigned int len;
275     char key[8];
276   } blob;
277   DWORD len = 8;
278
279   /* Acquire the crypto provider */
280   if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
281                           CRYPT_VERIFYCONTEXT))
282     return FALSE;
283
284   memset(&blob, 0, sizeof(blob));
285   extend_key_56_to_64(key_56, blob.key);
286   blob.hdr.bType = PLAINTEXTKEYBLOB;
287   blob.hdr.bVersion = 2;
288   blob.hdr.aiKeyAlg = CALG_DES;
289   blob.len = 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   */
316 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
317                             const unsigned char *plaintext,
318                             unsigned char *results)
319 {
320 #ifdef USE_SSLEAY
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_NETTLE)
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_GNUTLS)
343   gcry_cipher_hd_t des;
344
345   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
346   setup_des_key(keys, &des);
347   gcry_cipher_encrypt(des, results, 8, plaintext, 8);
348   gcry_cipher_close(des);
349
350   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
351   setup_des_key(keys + 7, &des);
352   gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
353   gcry_cipher_close(des);
354
355   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
356   setup_des_key(keys + 14, &des);
357   gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
358   gcry_cipher_close(des);
359 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
360   || defined(USE_WIN32_CRYPTO)
361   encrypt_des(plaintext, results, keys);
362   encrypt_des(plaintext, results + 8, keys + 7);
363   encrypt_des(plaintext, results + 16, keys + 14);
364 #endif
365 }
366
367 /*
368  * Set up lanmanager hashed password
369  */
370 CURLcode Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
371                                    const char *password,
372                                    unsigned char *lmbuffer /* 21 bytes */)
373 {
374   CURLcode result;
375   unsigned char pw[14];
376   static const unsigned char magic[] = {
377     0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
378   };
379   size_t len = CURLMIN(strlen(password), 14);
380
381   Curl_strntoupper((char *)pw, password, len);
382   memset(&pw[len], 0, 14 - len);
383
384   /*
385    * The LanManager hashed password needs to be created using the
386    * password in the network encoding not the host encoding.
387    */
388   result = Curl_convert_to_network(data, (char *)pw, 14);
389   if(result)
390     return result;
391
392   {
393     /* Create LanManager hashed password. */
394
395 #ifdef USE_SSLEAY
396     DES_key_schedule ks;
397
398     setup_des_key(pw, DESKEY(ks));
399     DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
400                     DESKEY(ks), DES_ENCRYPT);
401
402     setup_des_key(pw + 7, DESKEY(ks));
403     DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
404                     DESKEY(ks), DES_ENCRYPT);
405 #elif defined(USE_GNUTLS_NETTLE)
406     struct des_ctx des;
407     setup_des_key(pw, &des);
408     des_encrypt(&des, 8, lmbuffer, magic);
409     setup_des_key(pw + 7, &des);
410     des_encrypt(&des, 8, lmbuffer + 8, magic);
411 #elif defined(USE_GNUTLS)
412     gcry_cipher_hd_t des;
413
414     gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
415     setup_des_key(pw, &des);
416     gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
417     gcry_cipher_close(des);
418
419     gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
420     setup_des_key(pw + 7, &des);
421     gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
422     gcry_cipher_close(des);
423 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
424   || defined(USE_WIN32_CRYPTO)
425     encrypt_des(magic, lmbuffer, pw);
426     encrypt_des(magic, lmbuffer + 8, pw + 7);
427 #endif
428
429     memset(lmbuffer + 16, 0, 21 - 16);
430   }
431
432   return CURLE_OK;
433 }
434
435 #if USE_NTRESPONSES
436 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
437                                 size_t srclen)
438 {
439   size_t i;
440   for(i = 0; i < srclen; i++) {
441     dest[2 * i] = (unsigned char)src[i];
442     dest[2 * i + 1] = '\0';
443   }
444 }
445
446 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
447
448 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
449                                           const char *src, size_t srclen)
450 {
451   size_t i;
452   for(i = 0; i < srclen; i++) {
453     dest[2 * i] = (unsigned char)(toupper(src[i]));
454     dest[2 * i + 1] = '\0';
455   }
456 }
457
458 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
459
460 /*
461  * Set up nt hashed passwords
462  */
463 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
464                                    const char *password,
465                                    unsigned char *ntbuffer /* 21 bytes */)
466 {
467   size_t len = strlen(password);
468   unsigned char *pw = malloc(len * 2);
469   CURLcode result;
470   if(!pw)
471     return CURLE_OUT_OF_MEMORY;
472
473   ascii_to_unicode_le(pw, password, len);
474
475   /*
476    * The NT hashed password needs to be created using the password in the
477    * network encoding not the host encoding.
478    */
479   result = Curl_convert_to_network(data, (char *)pw, len * 2);
480   if(result)
481     return result;
482
483   {
484     /* Create NT hashed password. */
485 #ifdef USE_SSLEAY
486     MD4_CTX MD4pw;
487     MD4_Init(&MD4pw);
488     MD4_Update(&MD4pw, pw, 2 * len);
489     MD4_Final(ntbuffer, &MD4pw);
490 #elif defined(USE_GNUTLS_NETTLE)
491     struct md4_ctx MD4pw;
492     md4_init(&MD4pw);
493     md4_update(&MD4pw, (unsigned int)(2 * len), pw);
494     md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
495 #elif defined(USE_GNUTLS)
496     gcry_md_hd_t MD4pw;
497     gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
498     gcry_md_write(MD4pw, pw, 2 * len);
499     memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
500     gcry_md_close(MD4pw);
501 #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
502     Curl_md4it(ntbuffer, pw, 2 * len);
503 #elif defined(USE_DARWINSSL)
504     (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
505 #elif defined(USE_WIN32_CRYPTO)
506     HCRYPTPROV hprov;
507     if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
508                            CRYPT_VERIFYCONTEXT)) {
509       HCRYPTHASH hhash;
510       if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
511         DWORD length = 16;
512         CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
513         CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
514         CryptDestroyHash(hhash);
515       }
516       CryptReleaseContext(hprov, 0);
517     }
518 #endif
519
520     memset(ntbuffer + 16, 0, 21 - 16);
521   }
522
523   free(pw);
524
525   return CURLE_OK;
526 }
527
528 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
529
530 /* This returns the HMAC MD5 digest */
531 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
532                        const unsigned char *data, unsigned int datalen,
533                        unsigned char *output)
534 {
535   HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
536
537   if(!ctxt)
538     return CURLE_OUT_OF_MEMORY;
539
540   /* Update the digest with the given challenge */
541   Curl_HMAC_update(ctxt, data, datalen);
542
543   /* Finalise the digest */
544   Curl_HMAC_final(ctxt, output);
545
546   return CURLE_OK;
547 }
548
549 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
550  * (uppercase UserName + Domain) as the data
551  */
552 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
553                                        const char *domain, size_t domlen,
554                                        unsigned char *ntlmhash,
555                                        unsigned char *ntlmv2hash)
556 {
557   /* Unicode representation */
558   size_t identity_len = (userlen + domlen) * 2;
559   unsigned char *identity = malloc(identity_len);
560   CURLcode result = CURLE_OK;
561
562   if(!identity)
563     return CURLE_OUT_OF_MEMORY;
564
565   ascii_uppercase_to_unicode_le(identity, user, userlen);
566   ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
567
568   result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
569                          ntlmv2hash);
570
571   Curl_safefree(identity);
572
573   return result;
574 }
575
576 /*
577  * Curl_ntlm_core_mk_ntlmv2_resp()
578  *
579  * This creates the NTLMv2 response as set in the ntlm type-3 message.
580  *
581  * Parameters:
582  *
583  * ntlmv2hash       [in] - The ntlmv2 hash (16 bytes)
584  * challenge_client [in] - The client nonce (8 bytes)
585  * ntlm             [in] - The ntlm data struct being used to read TargetInfo
586                            and Server challenge received in the type-2 message
587  * ntresp          [out] - The address where a pointer to newly allocated
588  *                         memory holding the NTLMv2 response.
589  * ntresp_len      [out] - The length of the output message.
590  *
591  * Returns CURLE_OK on success.
592  */
593 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
594                                        unsigned char *challenge_client,
595                                        struct ntlmdata *ntlm,
596                                        unsigned char **ntresp,
597                                        unsigned int *ntresp_len)
598 {
599 /* NTLMv2 response structure :
600 ------------------------------------------------------------------------------
601 0     HMAC MD5         16 bytes
602 ------BLOB--------------------------------------------------------------------
603 16    Signature        0x01010000
604 20    Reserved         long (0x00000000)
605 24    Timestamp        LE, 64-bit signed value representing the number of
606                        tenths of a microsecond since January 1, 1601.
607 32    Client Nonce     8 bytes
608 40    Unknown          4 bytes
609 44    Target Info      N bytes (from the type-2 message)
610 44+N  Unknown          4 bytes
611 ------------------------------------------------------------------------------
612 */
613
614   unsigned int len = 0;
615   unsigned char *ptr = NULL;
616   unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
617 #if defined(HAVE_LONGLONG)
618   long long tw;
619 #else
620   __int64 tw;
621 #endif
622   CURLcode result = CURLE_OK;
623
624   /* Calculate the timestamp */
625 #ifdef DEBUGBUILD
626   char *force_timestamp = getenv("CURL_FORCETIME");
627   if(force_timestamp)
628     tw = 11644473600ULL * 10000000ULL;
629   else
630 #endif
631   tw = ((long long)time(NULL) + 11644473600ULL) * 10000000ULL;
632
633   /* Calculate the response len */
634   len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
635
636   /* Allocate the response */
637   ptr = malloc(len);
638   if(!ptr)
639     return CURLE_OUT_OF_MEMORY;
640
641   memset(ptr, 0, len);
642
643   /* Create the BLOB structure */
644   snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
645            NTLMv2_BLOB_SIGNATURE
646            "%c%c%c%c",  /* Reserved = 0 */
647            0, 0, 0, 0);
648
649   Curl_write64_le(tw, ptr + 24);
650   memcpy(ptr + 32, challenge_client, 8);
651   memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
652
653   /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
654   memcpy(ptr + 8, &ntlm->nonce[0], 8);
655   result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
656                          NTLMv2_BLOB_LEN + 8, hmac_output);
657   if(result) {
658     Curl_safefree(ptr);
659
660     return result;
661   }
662
663   /* Concatenate the HMAC MD5 output  with the BLOB */
664   memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
665
666   /* Return the response */
667   *ntresp = ptr;
668   *ntresp_len = len;
669
670   return result;
671 }
672
673 /*
674  * Curl_ntlm_core_mk_lmv2_resp()
675  *
676  * This creates the LMv2 response as used in the ntlm type-3 message.
677  *
678  * Parameters:
679  *
680  * ntlmv2hash        [in] - The ntlmv2 hash (16 bytes)
681  * challenge_client  [in] - The client nonce (8 bytes)
682  * challenge_client  [in] - The server challenge (8 bytes)
683  * lmresp           [out] - The LMv2 response (24 bytes)
684  *
685  * Returns CURLE_OK on success.
686  */
687 CURLcode  Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
688                                       unsigned char *challenge_client,
689                                       unsigned char *challenge_server,
690                                       unsigned char *lmresp)
691 {
692   unsigned char data[16];
693   unsigned char hmac_output[16];
694   CURLcode result = CURLE_OK;
695
696   memcpy(&data[0], challenge_server, 8);
697   memcpy(&data[8], challenge_client, 8);
698
699   result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
700   if(result)
701     return result;
702
703   /* Concatenate the HMAC MD5 output  with the client nonce */
704   memcpy(lmresp, hmac_output, 16);
705   memcpy(lmresp+16, challenge_client, 8);
706
707   return result;
708 }
709
710 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
711
712 #endif /* USE_NTRESPONSES */
713
714 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
715
716 #endif /* USE_NTLM */