Home | History | Annotate | Download | only in lib
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel (at) 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.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  * https://www.innovation.ch/java/ntlm.html
     32  */
     33 
     34 #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
     35 
     36 #ifdef USE_OPENSSL
     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_ntlm_core.h"
    105 #include "curl_md5.h"
    106 #include "curl_hmac.h"
    107 #include "warnless.h"
    108 #include "curl_endian.h"
    109 #include "curl_des.h"
    110 /* The last 3 #include files should be in this order */
    111 #include "curl_printf.h"
    112 #include "curl_memory.h"
    113 #include "memdebug.h"
    114 
    115 #define NTLM_HMAC_MD5_LEN     (16)
    116 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
    117 #define NTLMv2_BLOB_LEN       (44 -16 + ntlm->target_info_len + 4)
    118 
    119 /*
    120 * Turns a 56-bit key into being 64-bit wide.
    121 */
    122 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
    123 {
    124   key[0] = key_56[0];
    125   key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
    126   key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
    127   key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
    128   key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
    129   key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
    130   key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
    131   key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
    132 }
    133 
    134 #ifdef USE_OPENSSL
    135 /*
    136  * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.  The
    137  * key schedule ks is also set.
    138  */
    139 static void setup_des_key(const unsigned char *key_56,
    140                           DES_key_schedule DESKEYARG(ks))
    141 {
    142   DES_cblock key;
    143 
    144   /* Expand the 56-bit key to 64-bits */
    145   extend_key_56_to_64(key_56, (char *) &key);
    146 
    147   /* Set the key parity to odd */
    148   DES_set_odd_parity(&key);
    149 
    150   /* Set the key */
    151   DES_set_key(&key, ks);
    152 }
    153 
    154 #elif defined(USE_GNUTLS_NETTLE)
    155 
    156 static void setup_des_key(const unsigned char *key_56,
    157                           struct des_ctx *des)
    158 {
    159   char key[8];
    160 
    161   /* Expand the 56-bit key to 64-bits */
    162   extend_key_56_to_64(key_56, key);
    163 
    164   /* Set the key parity to odd */
    165   Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
    166 
    167   /* Set the key */
    168   des_set_key(des, (const uint8_t *) key);
    169 }
    170 
    171 #elif defined(USE_GNUTLS)
    172 
    173 /*
    174  * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
    175  */
    176 static void setup_des_key(const unsigned char *key_56,
    177                           gcry_cipher_hd_t *des)
    178 {
    179   char key[8];
    180 
    181   /* Expand the 56-bit key to 64-bits */
    182   extend_key_56_to_64(key_56, key);
    183 
    184   /* Set the key parity to odd */
    185   Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
    186 
    187   /* Set the key */
    188   gcry_cipher_setkey(*des, key, sizeof(key));
    189 }
    190 
    191 #elif defined(USE_NSS)
    192 
    193 /*
    194  * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
    195  * the expanded key.  The caller is responsible for giving 64 bit of valid
    196  * data is IN and (at least) 64 bit large buffer as OUT.
    197  */
    198 static bool encrypt_des(const unsigned char *in, unsigned char *out,
    199                         const unsigned char *key_56)
    200 {
    201   const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
    202   PK11SlotInfo *slot = NULL;
    203   char key[8];                                /* expanded 64 bit key */
    204   SECItem key_item;
    205   PK11SymKey *symkey = NULL;
    206   SECItem *param = NULL;
    207   PK11Context *ctx = NULL;
    208   int out_len;                                /* not used, required by NSS */
    209   bool rv = FALSE;
    210 
    211   /* use internal slot for DES encryption (requires NSS to be initialized) */
    212   slot = PK11_GetInternalKeySlot();
    213   if(!slot)
    214     return FALSE;
    215 
    216   /* Expand the 56-bit key to 64-bits */
    217   extend_key_56_to_64(key_56, key);
    218 
    219   /* Set the key parity to odd */
    220   Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
    221 
    222   /* Import the key */
    223   key_item.data = (unsigned char *)key;
    224   key_item.len = sizeof(key);
    225   symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
    226                              &key_item, NULL);
    227   if(!symkey)
    228     goto fail;
    229 
    230   /* Create the DES encryption context */
    231   param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
    232   if(!param)
    233     goto fail;
    234   ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
    235   if(!ctx)
    236     goto fail;
    237 
    238   /* Perform the encryption */
    239   if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
    240                                  (unsigned char *)in, /* inbuflen */ 8)
    241       && SECSuccess == PK11_Finalize(ctx))
    242     rv = /* all OK */ TRUE;
    243 
    244 fail:
    245   /* cleanup */
    246   if(ctx)
    247     PK11_DestroyContext(ctx, PR_TRUE);
    248   if(symkey)
    249     PK11_FreeSymKey(symkey);
    250   if(param)
    251     SECITEM_FreeItem(param, PR_TRUE);
    252   PK11_FreeSlot(slot);
    253   return rv;
    254 }
    255 
    256 #elif defined(USE_DARWINSSL)
    257 
    258 static bool encrypt_des(const unsigned char *in, unsigned char *out,
    259                         const unsigned char *key_56)
    260 {
    261   char key[8];
    262   size_t out_len;
    263   CCCryptorStatus err;
    264 
    265   /* Expand the 56-bit key to 64-bits */
    266   extend_key_56_to_64(key_56, key);
    267 
    268   /* Set the key parity to odd */
    269   Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
    270 
    271   /* Perform the encryption */
    272   err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
    273                 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
    274                 8 /* outbuflen */, &out_len);
    275 
    276   return err == kCCSuccess;
    277 }
    278 
    279 #elif defined(USE_OS400CRYPTO)
    280 
    281 static bool encrypt_des(const unsigned char *in, unsigned char *out,
    282                         const unsigned char *key_56)
    283 {
    284   char key[8];
    285   _CIPHER_Control_T ctl;
    286 
    287   /* Setup the cipher control structure */
    288   ctl.Func_ID = ENCRYPT_ONLY;
    289   ctl.Data_Len = sizeof(key);
    290 
    291   /* Expand the 56-bit key to 64-bits */
    292   extend_key_56_to_64(key_56, ctl.Crypto_Key);
    293 
    294   /* Set the key parity to odd */
    295   Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
    296 
    297   /* Perform the encryption */
    298   _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
    299 
    300   return TRUE;
    301 }
    302 
    303 #elif defined(USE_WIN32_CRYPTO)
    304 
    305 static bool encrypt_des(const unsigned char *in, unsigned char *out,
    306                         const unsigned char *key_56)
    307 {
    308   HCRYPTPROV hprov;
    309   HCRYPTKEY hkey;
    310   struct {
    311     BLOBHEADER hdr;
    312     unsigned int len;
    313     char key[8];
    314   } blob;
    315   DWORD len = 8;
    316 
    317   /* Acquire the crypto provider */
    318   if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
    319                           CRYPT_VERIFYCONTEXT))
    320     return FALSE;
    321 
    322   /* Setup the key blob structure */
    323   memset(&blob, 0, sizeof(blob));
    324   blob.hdr.bType = PLAINTEXTKEYBLOB;
    325   blob.hdr.bVersion = 2;
    326   blob.hdr.aiKeyAlg = CALG_DES;
    327   blob.len = sizeof(blob.key);
    328 
    329   /* Expand the 56-bit key to 64-bits */
    330   extend_key_56_to_64(key_56, blob.key);
    331 
    332   /* Set the key parity to odd */
    333   Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
    334 
    335   /* Import the key */
    336   if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
    337     CryptReleaseContext(hprov, 0);
    338 
    339     return FALSE;
    340   }
    341 
    342   memcpy(out, in, 8);
    343 
    344   /* Perform the encryption */
    345   CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
    346 
    347   CryptDestroyKey(hkey);
    348   CryptReleaseContext(hprov, 0);
    349 
    350   return TRUE;
    351 }
    352 
    353 #endif /* defined(USE_WIN32_CRYPTO) */
    354 
    355  /*
    356   * takes a 21 byte array and treats it as 3 56-bit DES keys. The
    357   * 8 byte plaintext is encrypted with each key and the resulting 24
    358   * bytes are stored in the results array.
    359   */
    360 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
    361                             const unsigned char *plaintext,
    362                             unsigned char *results)
    363 {
    364 #ifdef USE_OPENSSL
    365   DES_key_schedule ks;
    366 
    367   setup_des_key(keys, DESKEY(ks));
    368   DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
    369                   DESKEY(ks), DES_ENCRYPT);
    370 
    371   setup_des_key(keys + 7, DESKEY(ks));
    372   DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
    373                   DESKEY(ks), DES_ENCRYPT);
    374 
    375   setup_des_key(keys + 14, DESKEY(ks));
    376   DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
    377                   DESKEY(ks), DES_ENCRYPT);
    378 #elif defined(USE_GNUTLS_NETTLE)
    379   struct des_ctx des;
    380   setup_des_key(keys, &des);
    381   des_encrypt(&des, 8, results, plaintext);
    382   setup_des_key(keys + 7, &des);
    383   des_encrypt(&des, 8, results + 8, plaintext);
    384   setup_des_key(keys + 14, &des);
    385   des_encrypt(&des, 8, results + 16, plaintext);
    386 #elif defined(USE_GNUTLS)
    387   gcry_cipher_hd_t des;
    388 
    389   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
    390   setup_des_key(keys, &des);
    391   gcry_cipher_encrypt(des, results, 8, plaintext, 8);
    392   gcry_cipher_close(des);
    393 
    394   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
    395   setup_des_key(keys + 7, &des);
    396   gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
    397   gcry_cipher_close(des);
    398 
    399   gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
    400   setup_des_key(keys + 14, &des);
    401   gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
    402   gcry_cipher_close(des);
    403 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
    404   || defined(USE_WIN32_CRYPTO)
    405   encrypt_des(plaintext, results, keys);
    406   encrypt_des(plaintext, results + 8, keys + 7);
    407   encrypt_des(plaintext, results + 16, keys + 14);
    408 #endif
    409 }
    410 
    411 /*
    412  * Set up lanmanager hashed password
    413  */
    414 CURLcode Curl_ntlm_core_mk_lm_hash(struct Curl_easy *data,
    415                                    const char *password,
    416                                    unsigned char *lmbuffer /* 21 bytes */)
    417 {
    418   CURLcode result;
    419   unsigned char pw[14];
    420   static const unsigned char magic[] = {
    421     0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
    422   };
    423   size_t len = CURLMIN(strlen(password), 14);
    424 
    425   Curl_strntoupper((char *)pw, password, len);
    426   memset(&pw[len], 0, 14 - len);
    427 
    428   /*
    429    * The LanManager hashed password needs to be created using the
    430    * password in the network encoding not the host encoding.
    431    */
    432   result = Curl_convert_to_network(data, (char *)pw, 14);
    433   if(result)
    434     return result;
    435 
    436   {
    437     /* Create LanManager hashed password. */
    438 
    439 #ifdef USE_OPENSSL
    440     DES_key_schedule ks;
    441 
    442     setup_des_key(pw, DESKEY(ks));
    443     DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
    444                     DESKEY(ks), DES_ENCRYPT);
    445 
    446     setup_des_key(pw + 7, DESKEY(ks));
    447     DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
    448                     DESKEY(ks), DES_ENCRYPT);
    449 #elif defined(USE_GNUTLS_NETTLE)
    450     struct des_ctx des;
    451     setup_des_key(pw, &des);
    452     des_encrypt(&des, 8, lmbuffer, magic);
    453     setup_des_key(pw + 7, &des);
    454     des_encrypt(&des, 8, lmbuffer + 8, magic);
    455 #elif defined(USE_GNUTLS)
    456     gcry_cipher_hd_t des;
    457 
    458     gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
    459     setup_des_key(pw, &des);
    460     gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
    461     gcry_cipher_close(des);
    462 
    463     gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
    464     setup_des_key(pw + 7, &des);
    465     gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
    466     gcry_cipher_close(des);
    467 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
    468   || defined(USE_WIN32_CRYPTO)
    469     encrypt_des(magic, lmbuffer, pw);
    470     encrypt_des(magic, lmbuffer + 8, pw + 7);
    471 #endif
    472 
    473     memset(lmbuffer + 16, 0, 21 - 16);
    474   }
    475 
    476   return CURLE_OK;
    477 }
    478 
    479 #if USE_NTRESPONSES
    480 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
    481                                 size_t srclen)
    482 {
    483   size_t i;
    484   for(i = 0; i < srclen; i++) {
    485     dest[2 * i] = (unsigned char)src[i];
    486     dest[2 * i + 1] = '\0';
    487   }
    488 }
    489 
    490 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
    491 
    492 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
    493                                           const char *src, size_t srclen)
    494 {
    495   size_t i;
    496   for(i = 0; i < srclen; i++) {
    497     dest[2 * i] = (unsigned char)(toupper(src[i]));
    498     dest[2 * i + 1] = '\0';
    499   }
    500 }
    501 
    502 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
    503 
    504 /*
    505  * Set up nt hashed passwords
    506  * @unittest: 1600
    507  */
    508 CURLcode Curl_ntlm_core_mk_nt_hash(struct Curl_easy *data,
    509                                    const char *password,
    510                                    unsigned char *ntbuffer /* 21 bytes */)
    511 {
    512   size_t len = strlen(password);
    513   unsigned char *pw = malloc(len * 2);
    514   CURLcode result;
    515   if(!pw)
    516     return CURLE_OUT_OF_MEMORY;
    517 
    518   ascii_to_unicode_le(pw, password, len);
    519 
    520   /*
    521    * The NT hashed password needs to be created using the password in the
    522    * network encoding not the host encoding.
    523    */
    524   result = Curl_convert_to_network(data, (char *)pw, len * 2);
    525   if(result)
    526     return result;
    527 
    528   {
    529     /* Create NT hashed password. */
    530 #ifdef USE_OPENSSL
    531     MD4_CTX MD4pw;
    532     MD4_Init(&MD4pw);
    533     MD4_Update(&MD4pw, pw, 2 * len);
    534     MD4_Final(ntbuffer, &MD4pw);
    535 #elif defined(USE_GNUTLS_NETTLE)
    536     struct md4_ctx MD4pw;
    537     md4_init(&MD4pw);
    538     md4_update(&MD4pw, (unsigned int)(2 * len), pw);
    539     md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
    540 #elif defined(USE_GNUTLS)
    541     gcry_md_hd_t MD4pw;
    542     gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
    543     gcry_md_write(MD4pw, pw, 2 * len);
    544     memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
    545     gcry_md_close(MD4pw);
    546 #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
    547     Curl_md4it(ntbuffer, pw, 2 * len);
    548 #elif defined(USE_DARWINSSL)
    549     (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
    550 #elif defined(USE_WIN32_CRYPTO)
    551     HCRYPTPROV hprov;
    552     if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
    553                            CRYPT_VERIFYCONTEXT)) {
    554       HCRYPTHASH hhash;
    555       if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
    556         DWORD length = 16;
    557         CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
    558         CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
    559         CryptDestroyHash(hhash);
    560       }
    561       CryptReleaseContext(hprov, 0);
    562     }
    563 #endif
    564 
    565     memset(ntbuffer + 16, 0, 21 - 16);
    566   }
    567 
    568   free(pw);
    569 
    570   return CURLE_OK;
    571 }
    572 
    573 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
    574 
    575 /* This returns the HMAC MD5 digest */
    576 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
    577                        const unsigned char *data, unsigned int datalen,
    578                        unsigned char *output)
    579 {
    580   HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
    581 
    582   if(!ctxt)
    583     return CURLE_OUT_OF_MEMORY;
    584 
    585   /* Update the digest with the given challenge */
    586   Curl_HMAC_update(ctxt, data, datalen);
    587 
    588   /* Finalise the digest */
    589   Curl_HMAC_final(ctxt, output);
    590 
    591   return CURLE_OK;
    592 }
    593 
    594 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
    595  * (uppercase UserName + Domain) as the data
    596  */
    597 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
    598                                        const char *domain, size_t domlen,
    599                                        unsigned char *ntlmhash,
    600                                        unsigned char *ntlmv2hash)
    601 {
    602   /* Unicode representation */
    603   size_t identity_len = (userlen + domlen) * 2;
    604   unsigned char *identity = malloc(identity_len);
    605   CURLcode result = CURLE_OK;
    606 
    607   if(!identity)
    608     return CURLE_OUT_OF_MEMORY;
    609 
    610   ascii_uppercase_to_unicode_le(identity, user, userlen);
    611   ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
    612 
    613   result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
    614                          ntlmv2hash);
    615 
    616   free(identity);
    617 
    618   return result;
    619 }
    620 
    621 /*
    622  * Curl_ntlm_core_mk_ntlmv2_resp()
    623  *
    624  * This creates the NTLMv2 response as set in the ntlm type-3 message.
    625  *
    626  * Parameters:
    627  *
    628  * ntlmv2hash       [in] - The ntlmv2 hash (16 bytes)
    629  * challenge_client [in] - The client nonce (8 bytes)
    630  * ntlm             [in] - The ntlm data struct being used to read TargetInfo
    631                            and Server challenge received in the type-2 message
    632  * ntresp          [out] - The address where a pointer to newly allocated
    633  *                         memory holding the NTLMv2 response.
    634  * ntresp_len      [out] - The length of the output message.
    635  *
    636  * Returns CURLE_OK on success.
    637  */
    638 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
    639                                        unsigned char *challenge_client,
    640                                        struct ntlmdata *ntlm,
    641                                        unsigned char **ntresp,
    642                                        unsigned int *ntresp_len)
    643 {
    644 /* NTLMv2 response structure :
    645 ------------------------------------------------------------------------------
    646 0     HMAC MD5         16 bytes
    647 ------BLOB--------------------------------------------------------------------
    648 16    Signature        0x01010000
    649 20    Reserved         long (0x00000000)
    650 24    Timestamp        LE, 64-bit signed value representing the number of
    651                        tenths of a microsecond since January 1, 1601.
    652 32    Client Nonce     8 bytes
    653 40    Unknown          4 bytes
    654 44    Target Info      N bytes (from the type-2 message)
    655 44+N  Unknown          4 bytes
    656 ------------------------------------------------------------------------------
    657 */
    658 
    659   unsigned int len = 0;
    660   unsigned char *ptr = NULL;
    661   unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
    662   curl_off_t tw;
    663 
    664   CURLcode result = CURLE_OK;
    665 
    666 #if CURL_SIZEOF_CURL_OFF_T < 8
    667 #error "this section needs 64bit support to work"
    668 #endif
    669 
    670   /* Calculate the timestamp */
    671 #ifdef DEBUGBUILD
    672   char *force_timestamp = getenv("CURL_FORCETIME");
    673   if(force_timestamp)
    674     tw = CURL_OFF_T_C(11644473600) * 10000000;
    675   else
    676 #endif
    677     tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
    678 
    679   /* Calculate the response len */
    680   len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
    681 
    682   /* Allocate the response */
    683   ptr = malloc(len);
    684   if(!ptr)
    685     return CURLE_OUT_OF_MEMORY;
    686 
    687   memset(ptr, 0, len);
    688 
    689   /* Create the BLOB structure */
    690   snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
    691            NTLMv2_BLOB_SIGNATURE
    692            "%c%c%c%c",  /* Reserved = 0 */
    693            0, 0, 0, 0);
    694 
    695   Curl_write64_le(tw, ptr + 24);
    696   memcpy(ptr + 32, challenge_client, 8);
    697   memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
    698 
    699   /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
    700   memcpy(ptr + 8, &ntlm->nonce[0], 8);
    701   result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
    702                          NTLMv2_BLOB_LEN + 8, hmac_output);
    703   if(result) {
    704     free(ptr);
    705     return result;
    706   }
    707 
    708   /* Concatenate the HMAC MD5 output  with the BLOB */
    709   memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
    710 
    711   /* Return the response */
    712   *ntresp = ptr;
    713   *ntresp_len = len;
    714 
    715   return result;
    716 }
    717 
    718 /*
    719  * Curl_ntlm_core_mk_lmv2_resp()
    720  *
    721  * This creates the LMv2 response as used in the ntlm type-3 message.
    722  *
    723  * Parameters:
    724  *
    725  * ntlmv2hash        [in] - The ntlmv2 hash (16 bytes)
    726  * challenge_client  [in] - The client nonce (8 bytes)
    727  * challenge_client  [in] - The server challenge (8 bytes)
    728  * lmresp           [out] - The LMv2 response (24 bytes)
    729  *
    730  * Returns CURLE_OK on success.
    731  */
    732 CURLcode  Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
    733                                       unsigned char *challenge_client,
    734                                       unsigned char *challenge_server,
    735                                       unsigned char *lmresp)
    736 {
    737   unsigned char data[16];
    738   unsigned char hmac_output[16];
    739   CURLcode result = CURLE_OK;
    740 
    741   memcpy(&data[0], challenge_server, 8);
    742   memcpy(&data[8], challenge_client, 8);
    743 
    744   result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
    745   if(result)
    746     return result;
    747 
    748   /* Concatenate the HMAC MD5 output  with the client nonce */
    749   memcpy(lmresp, hmac_output, 16);
    750   memcpy(lmresp+16, challenge_client, 8);
    751 
    752   return result;
    753 }
    754 
    755 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
    756 
    757 #endif /* USE_NTRESPONSES */
    758 
    759 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
    760 
    761 #endif /* USE_NTLM */
    762