2011-08-27 19:19:08 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-02-18 05:42:46 -05:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-08-27 19:19:08 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-08-27 19:19:08 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2020-08-03 04:33:17 -04:00
|
|
|
#if defined(USE_CURL_NTLM_CORE)
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2011-08-28 01:15:46 -04:00
|
|
|
/*
|
|
|
|
* NTLM details:
|
|
|
|
*
|
2017-02-06 14:21:05 -05:00
|
|
|
* https://davenport.sourceforge.io/ntlm.html
|
2016-04-06 05:57:48 -04:00
|
|
|
* https://www.innovation.ch/java/ntlm.html
|
2011-08-28 01:15:46 -04:00
|
|
|
*/
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2017-08-30 17:10:38 -04:00
|
|
|
/* Please keep the SSL backend-specific #if branches in this order:
|
|
|
|
|
|
|
|
1. USE_OPENSSL
|
2021-02-18 05:42:46 -05:00
|
|
|
2. USE_GNUTLS
|
|
|
|
3. USE_NSS
|
|
|
|
4. USE_MBEDTLS
|
|
|
|
5. USE_SECTRANSP
|
|
|
|
6. USE_OS400CRYPTO
|
|
|
|
7. USE_WIN32_CRYPTO
|
2017-08-30 17:10:38 -04:00
|
|
|
|
|
|
|
This ensures that:
|
|
|
|
- the same SSL branch gets activated throughout this source
|
|
|
|
file even if multiple backends are enabled at the same time.
|
|
|
|
- OpenSSL and NSS have higher priority than Windows Crypt, due
|
|
|
|
to issues with the latter supporting NTLM2Session responses
|
|
|
|
in NTLM type-3 messages.
|
|
|
|
*/
|
|
|
|
|
2020-06-11 11:14:43 -04:00
|
|
|
#if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
|
|
|
|
|
|
|
|
#ifdef USE_WOLFSSL
|
|
|
|
#include <wolfssl/options.h>
|
|
|
|
#endif
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2018-11-14 07:12:34 -05:00
|
|
|
# include <openssl/des.h>
|
|
|
|
# include <openssl/md5.h>
|
|
|
|
# include <openssl/ssl.h>
|
|
|
|
# include <openssl/rand.h>
|
2020-06-11 11:14:43 -04:00
|
|
|
# if (defined(OPENSSL_VERSION_NUMBER) && \
|
|
|
|
(OPENSSL_VERSION_NUMBER < 0x00907001L)) && !defined(USE_WOLFSSL)
|
2011-08-28 01:15:46 -04:00
|
|
|
# define DES_key_schedule des_key_schedule
|
|
|
|
# define DES_cblock des_cblock
|
|
|
|
# define DES_set_odd_parity des_set_odd_parity
|
|
|
|
# define DES_set_key des_set_key
|
|
|
|
# define DES_ecb_encrypt des_ecb_encrypt
|
|
|
|
# define DESKEY(x) x
|
|
|
|
# define DESKEYARG(x) x
|
|
|
|
# else
|
|
|
|
# define DESKEYARG(x) *x
|
|
|
|
# define DESKEY(x) &x
|
|
|
|
# endif
|
2011-08-27 19:19:08 -04:00
|
|
|
|
|
|
|
#elif defined(USE_GNUTLS)
|
|
|
|
|
2021-02-18 05:42:46 -05:00
|
|
|
# include <nettle/des.h>
|
2011-08-27 19:19:08 -04:00
|
|
|
|
|
|
|
#elif defined(USE_NSS)
|
|
|
|
|
2011-08-28 01:15:46 -04:00
|
|
|
# include <nss.h>
|
|
|
|
# include <pk11pub.h>
|
|
|
|
# include <hasht.h>
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2017-08-30 17:10:38 -04:00
|
|
|
#elif defined(USE_MBEDTLS)
|
|
|
|
|
|
|
|
# include <mbedtls/des.h>
|
2019-04-13 22:27:21 -04:00
|
|
|
# include "curl_md4.h"
|
2017-08-30 17:10:38 -04:00
|
|
|
|
2019-02-26 03:21:12 -05:00
|
|
|
#elif defined(USE_SECTRANSP)
|
2012-06-27 05:57:31 -04:00
|
|
|
|
|
|
|
# include <CommonCrypto/CommonCryptor.h>
|
|
|
|
# include <CommonCrypto/CommonDigest.h>
|
|
|
|
|
2014-12-05 10:11:07 -05:00
|
|
|
#elif defined(USE_OS400CRYPTO)
|
|
|
|
# include "cipher.mih" /* mih/cipher */
|
2014-12-02 13:21:52 -05:00
|
|
|
#elif defined(USE_WIN32_CRYPTO)
|
|
|
|
# include <wincrypt.h>
|
2011-08-27 19:19:08 -04:00
|
|
|
#else
|
2011-08-28 01:15:46 -04:00
|
|
|
# error "Can't compile NTLM support without a crypto library."
|
2011-08-27 19:19:08 -04:00
|
|
|
#endif
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "non-ascii.h"
|
2016-09-30 12:54:02 -04:00
|
|
|
#include "strcase.h"
|
2011-08-27 19:19:08 -04:00
|
|
|
#include "curl_ntlm_core.h"
|
2014-01-24 08:01:29 -05:00
|
|
|
#include "curl_md5.h"
|
|
|
|
#include "curl_hmac.h"
|
|
|
|
#include "warnless.h"
|
2014-12-31 09:02:25 -05:00
|
|
|
#include "curl_endian.h"
|
2015-01-28 15:59:27 -05:00
|
|
|
#include "curl_des.h"
|
2019-04-14 05:33:46 -04:00
|
|
|
#include "curl_md4.h"
|
2016-04-29 09:46:40 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2015-03-24 18:12:03 -04:00
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
#define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
|
|
|
|
#define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
|
|
|
|
|
2011-08-27 19:19:08 -04:00
|
|
|
/*
|
2015-01-02 14:59:41 -05:00
|
|
|
* Turns a 56-bit key into being 64-bit wide.
|
|
|
|
*/
|
|
|
|
static void extend_key_56_to_64(const unsigned char *key_56, char *key)
|
2011-08-27 19:19:08 -04:00
|
|
|
{
|
|
|
|
key[0] = key_56[0];
|
|
|
|
key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
|
|
|
|
key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
|
|
|
|
key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
|
|
|
|
key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
|
|
|
|
key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
|
|
|
|
key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
|
|
|
|
key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
|
|
|
|
}
|
|
|
|
|
2020-06-11 11:14:43 -04:00
|
|
|
#if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
|
2011-08-27 19:19:08 -04:00
|
|
|
/*
|
2015-01-02 14:59:41 -05:00
|
|
|
* Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
|
|
|
|
* key schedule ks is also set.
|
2011-08-27 19:19:08 -04:00
|
|
|
*/
|
2015-01-02 14:59:41 -05:00
|
|
|
static void setup_des_key(const unsigned char *key_56,
|
|
|
|
DES_key_schedule DESKEYARG(ks))
|
2011-08-27 19:19:08 -04:00
|
|
|
{
|
2015-01-02 14:59:41 -05:00
|
|
|
DES_cblock key;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Expand the 56-bit key to 64-bits */
|
2016-02-08 23:19:31 -05:00
|
|
|
extend_key_56_to_64(key_56, (char *) &key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
|
|
|
/* Set the key parity to odd */
|
2015-01-02 14:59:41 -05:00
|
|
|
DES_set_odd_parity(&key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
|
|
|
/* Set the key */
|
2015-01-02 14:59:41 -05:00
|
|
|
DES_set_key(&key, ks);
|
2011-08-27 19:19:08 -04:00
|
|
|
}
|
|
|
|
|
2021-02-18 05:42:46 -05:00
|
|
|
#elif defined(USE_GNUTLS)
|
2011-11-15 04:52:32 -05:00
|
|
|
|
|
|
|
static void setup_des_key(const unsigned char *key_56,
|
|
|
|
struct des_ctx *des)
|
|
|
|
{
|
|
|
|
char key[8];
|
2015-01-28 15:53:40 -05:00
|
|
|
|
|
|
|
/* Expand the 56-bit key to 64-bits */
|
2011-11-15 04:52:32 -05:00
|
|
|
extend_key_56_to_64(key_56, key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2015-01-28 15:59:27 -05:00
|
|
|
/* Set the key parity to odd */
|
|
|
|
Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Set the key */
|
2015-01-28 15:55:06 -05:00
|
|
|
des_set_key(des, (const uint8_t *) key);
|
2011-11-15 04:52:32 -05:00
|
|
|
}
|
|
|
|
|
2011-08-27 19:19:08 -04:00
|
|
|
#elif defined(USE_NSS)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
|
|
|
|
* the expanded key. The caller is responsible for giving 64 bit of valid
|
|
|
|
* data is IN and (at least) 64 bit large buffer as OUT.
|
|
|
|
*/
|
|
|
|
static bool encrypt_des(const unsigned char *in, unsigned char *out,
|
|
|
|
const unsigned char *key_56)
|
|
|
|
{
|
|
|
|
const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
|
|
|
|
char key[8]; /* expanded 64 bit key */
|
|
|
|
SECItem key_item;
|
|
|
|
PK11SymKey *symkey = NULL;
|
|
|
|
SECItem *param = NULL;
|
|
|
|
PK11Context *ctx = NULL;
|
|
|
|
int out_len; /* not used, required by NSS */
|
|
|
|
bool rv = FALSE;
|
|
|
|
|
|
|
|
/* use internal slot for DES encryption (requires NSS to be initialized) */
|
2019-05-25 04:06:08 -04:00
|
|
|
PK11SlotInfo *slot = PK11_GetInternalKeySlot();
|
2011-08-27 19:19:08 -04:00
|
|
|
if(!slot)
|
|
|
|
return FALSE;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Expand the 56-bit key to 64-bits */
|
2011-08-27 19:19:08 -04:00
|
|
|
extend_key_56_to_64(key_56, key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2015-01-28 15:59:27 -05:00
|
|
|
/* Set the key parity to odd */
|
|
|
|
Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Import the key */
|
2011-08-27 19:19:08 -04:00
|
|
|
key_item.data = (unsigned char *)key;
|
2015-01-28 15:55:06 -05:00
|
|
|
key_item.len = sizeof(key);
|
2011-08-27 19:19:08 -04:00
|
|
|
symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
|
|
|
|
&key_item, NULL);
|
|
|
|
if(!symkey)
|
|
|
|
goto fail;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Create the DES encryption context */
|
2011-08-27 19:19:08 -04:00
|
|
|
param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
|
|
|
|
if(!param)
|
|
|
|
goto fail;
|
|
|
|
ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
|
|
|
|
if(!ctx)
|
|
|
|
goto fail;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Perform the encryption */
|
2011-08-27 19:19:08 -04:00
|
|
|
if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
|
|
|
|
(unsigned char *)in, /* inbuflen */ 8)
|
|
|
|
&& SECSuccess == PK11_Finalize(ctx))
|
|
|
|
rv = /* all OK */ TRUE;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
/* cleanup */
|
|
|
|
if(ctx)
|
|
|
|
PK11_DestroyContext(ctx, PR_TRUE);
|
|
|
|
if(symkey)
|
|
|
|
PK11_FreeSymKey(symkey);
|
|
|
|
if(param)
|
|
|
|
SECITEM_FreeItem(param, PR_TRUE);
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-08-30 17:10:38 -04:00
|
|
|
#elif defined(USE_MBEDTLS)
|
|
|
|
|
|
|
|
static bool encrypt_des(const unsigned char *in, unsigned char *out,
|
|
|
|
const unsigned char *key_56)
|
|
|
|
{
|
|
|
|
mbedtls_des_context ctx;
|
|
|
|
char key[8];
|
|
|
|
|
|
|
|
/* Expand the 56-bit key to 64-bits */
|
|
|
|
extend_key_56_to_64(key_56, key);
|
|
|
|
|
|
|
|
/* Set the key parity to odd */
|
|
|
|
mbedtls_des_key_set_parity((unsigned char *) key);
|
|
|
|
|
|
|
|
/* Perform the encryption */
|
|
|
|
mbedtls_des_init(&ctx);
|
|
|
|
mbedtls_des_setkey_enc(&ctx, (unsigned char *) key);
|
|
|
|
return mbedtls_des_crypt_ecb(&ctx, in, out) == 0;
|
|
|
|
}
|
|
|
|
|
2019-02-26 03:21:12 -05:00
|
|
|
#elif defined(USE_SECTRANSP)
|
2012-06-27 05:57:31 -04:00
|
|
|
|
|
|
|
static bool encrypt_des(const unsigned char *in, unsigned char *out,
|
|
|
|
const unsigned char *key_56)
|
|
|
|
{
|
|
|
|
char key[8];
|
|
|
|
size_t out_len;
|
|
|
|
CCCryptorStatus err;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Expand the 56-bit key to 64-bits */
|
2012-06-27 05:57:31 -04:00
|
|
|
extend_key_56_to_64(key_56, key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2015-01-28 15:59:27 -05:00
|
|
|
/* Set the key parity to odd */
|
|
|
|
Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Perform the encryption */
|
2012-06-27 05:57:31 -04:00
|
|
|
err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
|
|
|
|
kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
|
|
|
|
8 /* outbuflen */, &out_len);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2012-06-27 05:57:31 -04:00
|
|
|
return err == kCCSuccess;
|
|
|
|
}
|
|
|
|
|
2014-12-05 10:11:07 -05:00
|
|
|
#elif defined(USE_OS400CRYPTO)
|
|
|
|
|
|
|
|
static bool encrypt_des(const unsigned char *in, unsigned char *out,
|
|
|
|
const unsigned char *key_56)
|
|
|
|
{
|
|
|
|
char key[8];
|
|
|
|
_CIPHER_Control_T ctl;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Setup the cipher control structure */
|
2014-12-05 10:11:07 -05:00
|
|
|
ctl.Func_ID = ENCRYPT_ONLY;
|
2015-01-28 15:55:06 -05:00
|
|
|
ctl.Data_Len = sizeof(key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
|
|
|
/* Expand the 56-bit key to 64-bits */
|
2014-12-05 10:11:07 -05:00
|
|
|
extend_key_56_to_64(key_56, ctl.Crypto_Key);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2015-01-28 15:59:27 -05:00
|
|
|
/* Set the key parity to odd */
|
|
|
|
Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Perform the encryption */
|
2014-12-05 10:11:07 -05:00
|
|
|
_CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
|
2015-01-28 15:53:40 -05:00
|
|
|
|
2014-12-05 10:11:07 -05:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-12-02 13:21:52 -05:00
|
|
|
#elif defined(USE_WIN32_CRYPTO)
|
|
|
|
|
|
|
|
static bool encrypt_des(const unsigned char *in, unsigned char *out,
|
|
|
|
const unsigned char *key_56)
|
|
|
|
{
|
|
|
|
HCRYPTPROV hprov;
|
|
|
|
HCRYPTKEY hkey;
|
|
|
|
struct {
|
|
|
|
BLOBHEADER hdr;
|
|
|
|
unsigned int len;
|
|
|
|
char key[8];
|
|
|
|
} blob;
|
2014-12-24 17:22:07 -05:00
|
|
|
DWORD len = 8;
|
2014-12-02 13:21:52 -05:00
|
|
|
|
|
|
|
/* Acquire the crypto provider */
|
|
|
|
if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
|
2020-03-17 19:06:05 -04:00
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
|
2014-12-02 13:21:52 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Setup the key blob structure */
|
2014-12-02 13:21:52 -05:00
|
|
|
memset(&blob, 0, sizeof(blob));
|
|
|
|
blob.hdr.bType = PLAINTEXTKEYBLOB;
|
|
|
|
blob.hdr.bVersion = 2;
|
|
|
|
blob.hdr.aiKeyAlg = CALG_DES;
|
|
|
|
blob.len = sizeof(blob.key);
|
|
|
|
|
2015-01-28 15:53:40 -05:00
|
|
|
/* Expand the 56-bit key to 64-bits */
|
|
|
|
extend_key_56_to_64(key_56, blob.key);
|
|
|
|
|
2015-01-28 15:59:27 -05:00
|
|
|
/* Set the key parity to odd */
|
|
|
|
Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
|
|
|
|
|
2014-12-02 13:21:52 -05:00
|
|
|
/* Import the key */
|
2014-12-24 17:22:07 -05:00
|
|
|
if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
|
2014-12-02 13:21:52 -05:00
|
|
|
CryptReleaseContext(hprov, 0);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(out, in, 8);
|
|
|
|
|
|
|
|
/* Perform the encryption */
|
|
|
|
CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
|
|
|
|
|
|
|
|
CryptDestroyKey(hkey);
|
|
|
|
CryptReleaseContext(hprov, 0);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(USE_WIN32_CRYPTO) */
|
2011-08-27 19:19:08 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* takes a 21 byte array and treats it as 3 56-bit DES keys. The
|
|
|
|
* 8 byte plaintext is encrypted with each key and the resulting 24
|
|
|
|
* bytes are stored in the results array.
|
|
|
|
*/
|
|
|
|
void Curl_ntlm_core_lm_resp(const unsigned char *keys,
|
|
|
|
const unsigned char *plaintext,
|
|
|
|
unsigned char *results)
|
|
|
|
{
|
2020-06-11 11:14:43 -04:00
|
|
|
#if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
|
2011-08-27 19:19:08 -04:00
|
|
|
DES_key_schedule ks;
|
|
|
|
|
|
|
|
setup_des_key(keys, DESKEY(ks));
|
|
|
|
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
|
|
|
|
DESKEY(ks), DES_ENCRYPT);
|
|
|
|
|
|
|
|
setup_des_key(keys + 7, DESKEY(ks));
|
|
|
|
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
|
|
|
|
DESKEY(ks), DES_ENCRYPT);
|
|
|
|
|
|
|
|
setup_des_key(keys + 14, DESKEY(ks));
|
|
|
|
DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
|
|
|
|
DESKEY(ks), DES_ENCRYPT);
|
2021-02-18 05:42:46 -05:00
|
|
|
#elif defined(USE_GNUTLS)
|
2011-11-15 04:52:32 -05:00
|
|
|
struct des_ctx des;
|
|
|
|
setup_des_key(keys, &des);
|
|
|
|
des_encrypt(&des, 8, results, plaintext);
|
|
|
|
setup_des_key(keys + 7, &des);
|
|
|
|
des_encrypt(&des, 8, results + 8, plaintext);
|
|
|
|
setup_des_key(keys + 14, &des);
|
|
|
|
des_encrypt(&des, 8, results + 16, plaintext);
|
2019-02-26 03:21:12 -05:00
|
|
|
#elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
|
2016-07-07 11:40:45 -04:00
|
|
|
|| defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
|
2011-08-27 19:19:08 -04:00
|
|
|
encrypt_des(plaintext, results, keys);
|
|
|
|
encrypt_des(plaintext, results + 8, keys + 7);
|
|
|
|
encrypt_des(plaintext, results + 16, keys + 14);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up lanmanager hashed password
|
|
|
|
*/
|
2016-06-21 09:47:12 -04:00
|
|
|
CURLcode Curl_ntlm_core_mk_lm_hash(struct Curl_easy *data,
|
2014-10-26 06:26:46 -04:00
|
|
|
const char *password,
|
|
|
|
unsigned char *lmbuffer /* 21 bytes */)
|
2011-08-27 19:19:08 -04:00
|
|
|
{
|
2014-10-26 06:16:59 -04:00
|
|
|
CURLcode result;
|
2011-08-27 19:19:08 -04:00
|
|
|
unsigned char pw[14];
|
|
|
|
static const unsigned char magic[] = {
|
|
|
|
0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
|
|
|
|
};
|
|
|
|
size_t len = CURLMIN(strlen(password), 14);
|
|
|
|
|
|
|
|
Curl_strntoupper((char *)pw, password, len);
|
|
|
|
memset(&pw[len], 0, 14 - len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The LanManager hashed password needs to be created using the
|
|
|
|
* password in the network encoding not the host encoding.
|
|
|
|
*/
|
2014-10-26 06:16:59 -04:00
|
|
|
result = Curl_convert_to_network(data, (char *)pw, 14);
|
|
|
|
if(result)
|
2014-10-26 06:26:46 -04:00
|
|
|
return result;
|
2011-08-27 19:19:08 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
/* Create LanManager hashed password. */
|
|
|
|
|
2020-06-11 11:14:43 -04:00
|
|
|
#if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
|
2011-08-27 19:19:08 -04:00
|
|
|
DES_key_schedule ks;
|
|
|
|
|
|
|
|
setup_des_key(pw, DESKEY(ks));
|
|
|
|
DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
|
|
|
|
DESKEY(ks), DES_ENCRYPT);
|
|
|
|
|
|
|
|
setup_des_key(pw + 7, DESKEY(ks));
|
|
|
|
DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
|
|
|
|
DESKEY(ks), DES_ENCRYPT);
|
2021-02-18 05:42:46 -05:00
|
|
|
#elif defined(USE_GNUTLS)
|
2011-11-15 04:52:32 -05:00
|
|
|
struct des_ctx des;
|
|
|
|
setup_des_key(pw, &des);
|
|
|
|
des_encrypt(&des, 8, lmbuffer, magic);
|
|
|
|
setup_des_key(pw + 7, &des);
|
|
|
|
des_encrypt(&des, 8, lmbuffer + 8, magic);
|
2019-02-26 03:21:12 -05:00
|
|
|
#elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
|
2016-07-07 11:40:45 -04:00
|
|
|
|| defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
|
2011-08-27 19:19:08 -04:00
|
|
|
encrypt_des(magic, lmbuffer, pw);
|
|
|
|
encrypt_des(magic, lmbuffer + 8, pw + 7);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
memset(lmbuffer + 16, 0, 21 - 16);
|
|
|
|
}
|
2014-10-26 06:26:46 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
2011-08-27 19:19:08 -04:00
|
|
|
}
|
|
|
|
|
2017-03-29 07:16:25 -04:00
|
|
|
#ifdef USE_NTRESPONSES
|
2011-08-27 19:19:08 -04:00
|
|
|
static void ascii_to_unicode_le(unsigned char *dest, const char *src,
|
|
|
|
size_t srclen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < srclen; i++) {
|
|
|
|
dest[2 * i] = (unsigned char)src[i];
|
|
|
|
dest[2 * i + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 07:16:25 -04:00
|
|
|
#if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
|
2015-01-02 11:28:03 -05:00
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
static void ascii_uppercase_to_unicode_le(unsigned char *dest,
|
|
|
|
const char *src, size_t srclen)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for(i = 0; i < srclen; i++) {
|
2017-06-04 18:27:29 -04:00
|
|
|
dest[2 * i] = (unsigned char)(Curl_raw_toupper(src[i]));
|
2014-01-24 08:01:29 -05:00
|
|
|
dest[2 * i + 1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 11:28:03 -05:00
|
|
|
#endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
|
|
|
|
|
2011-08-27 19:19:08 -04:00
|
|
|
/*
|
|
|
|
* Set up nt hashed passwords
|
2015-02-03 14:59:54 -05:00
|
|
|
* @unittest: 1600
|
2011-08-27 19:19:08 -04:00
|
|
|
*/
|
2016-06-21 09:47:12 -04:00
|
|
|
CURLcode Curl_ntlm_core_mk_nt_hash(struct Curl_easy *data,
|
2011-08-27 19:19:08 -04:00
|
|
|
const char *password,
|
|
|
|
unsigned char *ntbuffer /* 21 bytes */)
|
|
|
|
{
|
|
|
|
size_t len = strlen(password);
|
2018-08-13 04:35:52 -04:00
|
|
|
unsigned char *pw;
|
2011-08-27 19:19:08 -04:00
|
|
|
CURLcode result;
|
2018-08-13 04:35:52 -04:00
|
|
|
if(len > SIZE_T_MAX/2) /* avoid integer overflow */
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2019-03-26 09:23:28 -04:00
|
|
|
pw = len ? malloc(len * 2) : (unsigned char *)strdup("");
|
2011-08-27 19:19:08 -04:00
|
|
|
if(!pw)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
ascii_to_unicode_le(pw, password, len);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The NT hashed password needs to be created using the password in the
|
|
|
|
* network encoding not the host encoding.
|
|
|
|
*/
|
|
|
|
result = Curl_convert_to_network(data, (char *)pw, len * 2);
|
|
|
|
if(result)
|
|
|
|
return result;
|
|
|
|
|
2019-04-13 22:52:16 -04:00
|
|
|
/* Create NT hashed password. */
|
|
|
|
Curl_md4it(ntbuffer, pw, 2 * len);
|
2011-08-27 19:19:08 -04:00
|
|
|
|
2019-04-13 22:52:16 -04:00
|
|
|
memset(ntbuffer + 16, 0, 21 - 16);
|
2011-08-27 19:19:08 -04:00
|
|
|
|
|
|
|
free(pw);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2017-03-29 07:16:25 -04:00
|
|
|
#if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
|
2014-12-02 13:21:52 -05:00
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
/* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
|
|
|
|
* (uppercase UserName + Domain) as the data
|
|
|
|
*/
|
|
|
|
CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
|
|
|
|
const char *domain, size_t domlen,
|
|
|
|
unsigned char *ntlmhash,
|
|
|
|
unsigned char *ntlmv2hash)
|
|
|
|
{
|
|
|
|
/* Unicode representation */
|
2017-11-06 17:51:52 -05:00
|
|
|
size_t identity_len;
|
|
|
|
unsigned char *identity;
|
2014-10-26 06:16:59 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2020-11-28 16:29:59 -05:00
|
|
|
if((userlen > CURL_MAX_INPUT_LENGTH) || (domlen > CURL_MAX_INPUT_LENGTH))
|
2017-11-06 17:51:52 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
identity_len = (userlen + domlen) * 2;
|
2020-11-28 16:29:59 -05:00
|
|
|
identity = malloc(identity_len + 1);
|
2017-11-06 17:51:52 -05:00
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
if(!identity)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
ascii_uppercase_to_unicode_le(identity, user, userlen);
|
|
|
|
ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
|
|
|
|
|
2020-02-23 17:37:43 -05:00
|
|
|
result = Curl_hmacit(Curl_HMAC_MD5, ntlmhash, 16, identity, identity_len,
|
|
|
|
ntlmv2hash);
|
2015-03-16 10:01:15 -04:00
|
|
|
free(identity);
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2014-10-26 06:16:59 -04:00
|
|
|
return result;
|
2014-01-24 08:01:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ntlm_core_mk_ntlmv2_resp()
|
|
|
|
*
|
|
|
|
* This creates the NTLMv2 response as set in the ntlm type-3 message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
|
|
|
|
* challenge_client [in] - The client nonce (8 bytes)
|
|
|
|
* ntlm [in] - The ntlm data struct being used to read TargetInfo
|
|
|
|
and Server challenge received in the type-2 message
|
|
|
|
* ntresp [out] - The address where a pointer to newly allocated
|
|
|
|
* memory holding the NTLMv2 response.
|
|
|
|
* ntresp_len [out] - The length of the output message.
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
|
|
|
|
unsigned char *challenge_client,
|
|
|
|
struct ntlmdata *ntlm,
|
|
|
|
unsigned char **ntresp,
|
|
|
|
unsigned int *ntresp_len)
|
|
|
|
{
|
|
|
|
/* NTLMv2 response structure :
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
0 HMAC MD5 16 bytes
|
|
|
|
------BLOB--------------------------------------------------------------------
|
|
|
|
16 Signature 0x01010000
|
|
|
|
20 Reserved long (0x00000000)
|
|
|
|
24 Timestamp LE, 64-bit signed value representing the number of
|
|
|
|
tenths of a microsecond since January 1, 1601.
|
|
|
|
32 Client Nonce 8 bytes
|
|
|
|
40 Unknown 4 bytes
|
|
|
|
44 Target Info N bytes (from the type-2 message)
|
|
|
|
44+N Unknown 4 bytes
|
|
|
|
------------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2014-01-26 09:04:46 -05:00
|
|
|
unsigned int len = 0;
|
2014-01-24 08:01:29 -05:00
|
|
|
unsigned char *ptr = NULL;
|
2020-02-23 17:37:43 -05:00
|
|
|
unsigned char hmac_output[HMAC_MD5_LENGTH];
|
2015-10-08 06:30:22 -04:00
|
|
|
curl_off_t tw;
|
|
|
|
|
2014-10-26 06:16:59 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2015-10-09 17:51:54 -04:00
|
|
|
#if CURL_SIZEOF_CURL_OFF_T < 8
|
|
|
|
#error "this section needs 64bit support to work"
|
|
|
|
#endif
|
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
/* Calculate the timestamp */
|
2014-03-20 06:17:40 -04:00
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
char *force_timestamp = getenv("CURL_FORCETIME");
|
|
|
|
if(force_timestamp)
|
2015-11-05 09:20:43 -05:00
|
|
|
tw = CURL_OFF_T_C(11644473600) * 10000000;
|
2014-03-20 06:17:40 -04:00
|
|
|
else
|
2014-05-04 13:59:55 -04:00
|
|
|
#endif
|
2015-11-05 09:20:43 -05:00
|
|
|
tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
|
2014-03-20 06:17:40 -04:00
|
|
|
|
2014-01-24 08:01:29 -05:00
|
|
|
/* Calculate the response len */
|
2020-02-23 17:37:43 -05:00
|
|
|
len = HMAC_MD5_LENGTH + NTLMv2_BLOB_LEN;
|
2014-01-24 08:01:29 -05:00
|
|
|
|
|
|
|
/* Allocate the response */
|
2018-04-14 16:42:04 -04:00
|
|
|
ptr = calloc(1, len);
|
2014-01-26 09:04:46 -05:00
|
|
|
if(!ptr)
|
2014-01-24 08:01:29 -05:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
/* Create the BLOB structure */
|
2020-02-23 17:37:43 -05:00
|
|
|
msnprintf((char *)ptr + HMAC_MD5_LENGTH, NTLMv2_BLOB_LEN,
|
2018-11-22 03:01:24 -05:00
|
|
|
"%c%c%c%c" /* NTLMv2_BLOB_SIGNATURE */
|
|
|
|
"%c%c%c%c", /* Reserved = 0 */
|
|
|
|
NTLMv2_BLOB_SIGNATURE[0], NTLMv2_BLOB_SIGNATURE[1],
|
|
|
|
NTLMv2_BLOB_SIGNATURE[2], NTLMv2_BLOB_SIGNATURE[3],
|
|
|
|
0, 0, 0, 0);
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2014-12-31 06:56:08 -05:00
|
|
|
Curl_write64_le(tw, ptr + 24);
|
2014-01-24 08:01:29 -05:00
|
|
|
memcpy(ptr + 32, challenge_client, 8);
|
|
|
|
memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
|
|
|
|
|
|
|
|
/* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
|
|
|
|
memcpy(ptr + 8, &ntlm->nonce[0], 8);
|
2020-02-23 17:37:43 -05:00
|
|
|
result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, HMAC_MD5_LENGTH, ptr + 8,
|
2019-02-08 03:33:42 -05:00
|
|
|
NTLMv2_BLOB_LEN + 8, hmac_output);
|
2014-10-26 06:16:59 -04:00
|
|
|
if(result) {
|
2015-03-16 10:01:15 -04:00
|
|
|
free(ptr);
|
2014-10-26 06:16:59 -04:00
|
|
|
return result;
|
2014-01-24 08:01:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Concatenate the HMAC MD5 output with the BLOB */
|
2020-02-23 17:37:43 -05:00
|
|
|
memcpy(ptr, hmac_output, HMAC_MD5_LENGTH);
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2014-01-26 09:04:46 -05:00
|
|
|
/* Return the response */
|
|
|
|
*ntresp = ptr;
|
|
|
|
*ntresp_len = len;
|
|
|
|
|
2014-10-26 06:16:59 -04:00
|
|
|
return result;
|
2014-01-24 08:01:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_ntlm_core_mk_lmv2_resp()
|
|
|
|
*
|
|
|
|
* This creates the LMv2 response as used in the ntlm type-3 message.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
*
|
|
|
|
* ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
|
|
|
|
* challenge_client [in] - The client nonce (8 bytes)
|
|
|
|
* challenge_client [in] - The server challenge (8 bytes)
|
|
|
|
* lmresp [out] - The LMv2 response (24 bytes)
|
|
|
|
*
|
|
|
|
* Returns CURLE_OK on success.
|
|
|
|
*/
|
|
|
|
CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
|
|
|
|
unsigned char *challenge_client,
|
|
|
|
unsigned char *challenge_server,
|
|
|
|
unsigned char *lmresp)
|
|
|
|
{
|
|
|
|
unsigned char data[16];
|
|
|
|
unsigned char hmac_output[16];
|
2014-10-26 06:16:59 -04:00
|
|
|
CURLcode result = CURLE_OK;
|
2014-01-24 08:01:29 -05:00
|
|
|
|
|
|
|
memcpy(&data[0], challenge_server, 8);
|
|
|
|
memcpy(&data[8], challenge_client, 8);
|
|
|
|
|
2020-02-23 17:37:43 -05:00
|
|
|
result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, 16, &data[0], 16,
|
|
|
|
hmac_output);
|
2014-10-26 06:16:59 -04:00
|
|
|
if(result)
|
|
|
|
return result;
|
2014-01-24 08:01:29 -05:00
|
|
|
|
|
|
|
/* Concatenate the HMAC MD5 output with the client nonce */
|
|
|
|
memcpy(lmresp, hmac_output, 16);
|
2017-09-09 17:55:08 -04:00
|
|
|
memcpy(lmresp + 16, challenge_client, 8);
|
2014-01-24 08:01:29 -05:00
|
|
|
|
2014-10-26 06:16:59 -04:00
|
|
|
return result;
|
2014-01-24 08:01:29 -05:00
|
|
|
}
|
|
|
|
|
2015-01-02 11:28:03 -05:00
|
|
|
#endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
|
2014-12-02 13:21:52 -05:00
|
|
|
|
2011-08-27 19:19:08 -04:00
|
|
|
#endif /* USE_NTRESPONSES */
|
|
|
|
|
2020-08-03 04:33:17 -04:00
|
|
|
#endif /* USE_CURL_NTLM_CORE */
|