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