1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #if defined(USE_CURL_NTLM_CORE)
30 * https://davenport.sourceforge.io/ntlm.html
31 * https://www.innovation.ch/java/ntlm.html
34 /* Please keep the SSL backend-specific #if branches in this order:
46 - the same SSL branch gets activated throughout this source
47 file even if multiple backends are enabled at the same time.
48 - OpenSSL and NSS have higher priority than Windows Crypt, due
49 to issues with the latter supporting NTLM2Session responses
50 in NTLM type-3 messages.
53 #if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
56 #include <wolfssl/options.h>
59 # include <openssl/des.h>
60 # include <openssl/md5.h>
61 # include <openssl/ssl.h>
62 # include <openssl/rand.h>
63 # if (defined(OPENSSL_VERSION_NUMBER) && \
64 (OPENSSL_VERSION_NUMBER < 0x00907001L)) && !defined(USE_WOLFSSL)
65 # define DES_key_schedule des_key_schedule
66 # define DES_cblock des_cblock
67 # define DES_set_odd_parity des_set_odd_parity
68 # define DES_set_key des_set_key
69 # define DES_ecb_encrypt des_ecb_encrypt
71 # define DESKEYARG(x) x
73 # define DESKEYARG(x) *x
77 #elif defined(USE_GNUTLS_NETTLE)
79 # include <nettle/des.h>
81 #elif defined(USE_GNUTLS)
85 #elif defined(USE_NSS)
91 #elif defined(USE_MBEDTLS)
93 # include <mbedtls/des.h>
94 # include "curl_md4.h"
96 #elif defined(USE_SECTRANSP)
98 # include <CommonCrypto/CommonCryptor.h>
99 # include <CommonCrypto/CommonDigest.h>
101 #elif defined(USE_OS400CRYPTO)
102 # include "cipher.mih" /* mih/cipher */
103 #elif defined(USE_WIN32_CRYPTO)
104 # include <wincrypt.h>
106 # error "Can't compile NTLM support without a crypto library."
110 #include "non-ascii.h"
112 #include "curl_ntlm_core.h"
113 #include "curl_md5.h"
114 #include "curl_hmac.h"
115 #include "warnless.h"
116 #include "curl_endian.h"
117 #include "curl_des.h"
118 #include "curl_md4.h"
119 /* The last 3 #include files should be in this order */
120 #include "curl_printf.h"
121 #include "curl_memory.h"
122 #include "memdebug.h"
124 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
125 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
128 * Turns a 56-bit key into being 64-bit wide.
130 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
133 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
134 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
135 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
136 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
137 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
138 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
139 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
142 #if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
144 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
145 * key schedule ks is also set.
147 static void setup_des_key(const unsigned char *key_56,
148 DES_key_schedule DESKEYARG(ks))
152 /* Expand the 56-bit key to 64-bits */
153 extend_key_56_to_64(key_56, (char *) &key);
155 /* Set the key parity to odd */
156 DES_set_odd_parity(&key);
159 DES_set_key(&key, ks);
162 #elif defined(USE_GNUTLS_NETTLE)
164 static void setup_des_key(const unsigned char *key_56,
169 /* Expand the 56-bit key to 64-bits */
170 extend_key_56_to_64(key_56, key);
172 /* Set the key parity to odd */
173 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
176 des_set_key(des, (const uint8_t *) key);
179 #elif defined(USE_GNUTLS)
182 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
184 static void setup_des_key(const unsigned char *key_56,
185 gcry_cipher_hd_t *des)
189 /* Expand the 56-bit key to 64-bits */
190 extend_key_56_to_64(key_56, key);
192 /* Set the key parity to odd */
193 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
196 gcry_cipher_setkey(*des, key, sizeof(key));
199 #elif defined(USE_NSS)
202 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
203 * the expanded key. The caller is responsible for giving 64 bit of valid
204 * data is IN and (at least) 64 bit large buffer as OUT.
206 static bool encrypt_des(const unsigned char *in, unsigned char *out,
207 const unsigned char *key_56)
209 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
210 char key[8]; /* expanded 64 bit key */
212 PK11SymKey *symkey = NULL;
213 SECItem *param = NULL;
214 PK11Context *ctx = NULL;
215 int out_len; /* not used, required by NSS */
218 /* use internal slot for DES encryption (requires NSS to be initialized) */
219 PK11SlotInfo *slot = PK11_GetInternalKeySlot();
223 /* Expand the 56-bit key to 64-bits */
224 extend_key_56_to_64(key_56, key);
226 /* Set the key parity to odd */
227 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
230 key_item.data = (unsigned char *)key;
231 key_item.len = sizeof(key);
232 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
237 /* Create the DES encryption context */
238 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
241 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
245 /* Perform the encryption */
246 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
247 (unsigned char *)in, /* inbuflen */ 8)
248 && SECSuccess == PK11_Finalize(ctx))
249 rv = /* all OK */ TRUE;
254 PK11_DestroyContext(ctx, PR_TRUE);
256 PK11_FreeSymKey(symkey);
258 SECITEM_FreeItem(param, PR_TRUE);
263 #elif defined(USE_MBEDTLS)
265 static bool encrypt_des(const unsigned char *in, unsigned char *out,
266 const unsigned char *key_56)
268 mbedtls_des_context ctx;
271 /* Expand the 56-bit key to 64-bits */
272 extend_key_56_to_64(key_56, key);
274 /* Set the key parity to odd */
275 mbedtls_des_key_set_parity((unsigned char *) key);
277 /* Perform the encryption */
278 mbedtls_des_init(&ctx);
279 mbedtls_des_setkey_enc(&ctx, (unsigned char *) key);
280 return mbedtls_des_crypt_ecb(&ctx, in, out) == 0;
283 #elif defined(USE_SECTRANSP)
285 static bool encrypt_des(const unsigned char *in, unsigned char *out,
286 const unsigned char *key_56)
292 /* Expand the 56-bit key to 64-bits */
293 extend_key_56_to_64(key_56, key);
295 /* Set the key parity to odd */
296 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
298 /* Perform the encryption */
299 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
300 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
301 8 /* outbuflen */, &out_len);
303 return err == kCCSuccess;
306 #elif defined(USE_OS400CRYPTO)
308 static bool encrypt_des(const unsigned char *in, unsigned char *out,
309 const unsigned char *key_56)
312 _CIPHER_Control_T ctl;
314 /* Setup the cipher control structure */
315 ctl.Func_ID = ENCRYPT_ONLY;
316 ctl.Data_Len = sizeof(key);
318 /* Expand the 56-bit key to 64-bits */
319 extend_key_56_to_64(key_56, ctl.Crypto_Key);
321 /* Set the key parity to odd */
322 Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
324 /* Perform the encryption */
325 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
330 #elif defined(USE_WIN32_CRYPTO)
332 static bool encrypt_des(const unsigned char *in, unsigned char *out,
333 const unsigned char *key_56)
344 /* Acquire the crypto provider */
345 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
346 CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
349 /* Setup the key blob structure */
350 memset(&blob, 0, sizeof(blob));
351 blob.hdr.bType = PLAINTEXTKEYBLOB;
352 blob.hdr.bVersion = 2;
353 blob.hdr.aiKeyAlg = CALG_DES;
354 blob.len = sizeof(blob.key);
356 /* Expand the 56-bit key to 64-bits */
357 extend_key_56_to_64(key_56, blob.key);
359 /* Set the key parity to odd */
360 Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
363 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
364 CryptReleaseContext(hprov, 0);
371 /* Perform the encryption */
372 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
374 CryptDestroyKey(hkey);
375 CryptReleaseContext(hprov, 0);
380 #endif /* defined(USE_WIN32_CRYPTO) */
383 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
384 * 8 byte plaintext is encrypted with each key and the resulting 24
385 * bytes are stored in the results array.
387 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
388 const unsigned char *plaintext,
389 unsigned char *results)
391 #if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
394 setup_des_key(keys, DESKEY(ks));
395 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
396 DESKEY(ks), DES_ENCRYPT);
398 setup_des_key(keys + 7, DESKEY(ks));
399 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
400 DESKEY(ks), DES_ENCRYPT);
402 setup_des_key(keys + 14, DESKEY(ks));
403 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
404 DESKEY(ks), DES_ENCRYPT);
405 #elif defined(USE_GNUTLS_NETTLE)
407 setup_des_key(keys, &des);
408 des_encrypt(&des, 8, results, plaintext);
409 setup_des_key(keys + 7, &des);
410 des_encrypt(&des, 8, results + 8, plaintext);
411 setup_des_key(keys + 14, &des);
412 des_encrypt(&des, 8, results + 16, plaintext);
413 #elif defined(USE_GNUTLS)
414 gcry_cipher_hd_t des;
416 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
417 setup_des_key(keys, &des);
418 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
419 gcry_cipher_close(des);
421 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
422 setup_des_key(keys + 7, &des);
423 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
424 gcry_cipher_close(des);
426 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
427 setup_des_key(keys + 14, &des);
428 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
429 gcry_cipher_close(des);
430 #elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
431 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
432 encrypt_des(plaintext, results, keys);
433 encrypt_des(plaintext, results + 8, keys + 7);
434 encrypt_des(plaintext, results + 16, keys + 14);
439 * Set up lanmanager hashed password
441 CURLcode Curl_ntlm_core_mk_lm_hash(struct Curl_easy *data,
442 const char *password,
443 unsigned char *lmbuffer /* 21 bytes */)
446 unsigned char pw[14];
447 static const unsigned char magic[] = {
448 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
450 size_t len = CURLMIN(strlen(password), 14);
452 Curl_strntoupper((char *)pw, password, len);
453 memset(&pw[len], 0, 14 - len);
456 * The LanManager hashed password needs to be created using the
457 * password in the network encoding not the host encoding.
459 result = Curl_convert_to_network(data, (char *)pw, 14);
464 /* Create LanManager hashed password. */
466 #if defined(USE_OPENSSL) || defined(USE_WOLFSSL)
469 setup_des_key(pw, DESKEY(ks));
470 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
471 DESKEY(ks), DES_ENCRYPT);
473 setup_des_key(pw + 7, DESKEY(ks));
474 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
475 DESKEY(ks), DES_ENCRYPT);
476 #elif defined(USE_GNUTLS_NETTLE)
478 setup_des_key(pw, &des);
479 des_encrypt(&des, 8, lmbuffer, magic);
480 setup_des_key(pw + 7, &des);
481 des_encrypt(&des, 8, lmbuffer + 8, magic);
482 #elif defined(USE_GNUTLS)
483 gcry_cipher_hd_t des;
485 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
486 setup_des_key(pw, &des);
487 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
488 gcry_cipher_close(des);
490 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
491 setup_des_key(pw + 7, &des);
492 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
493 gcry_cipher_close(des);
494 #elif defined(USE_NSS) || defined(USE_MBEDTLS) || defined(USE_SECTRANSP) \
495 || defined(USE_OS400CRYPTO) || defined(USE_WIN32_CRYPTO)
496 encrypt_des(magic, lmbuffer, pw);
497 encrypt_des(magic, lmbuffer + 8, pw + 7);
500 memset(lmbuffer + 16, 0, 21 - 16);
506 #ifdef USE_NTRESPONSES
507 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
511 for(i = 0; i < srclen; i++) {
512 dest[2 * i] = (unsigned char)src[i];
513 dest[2 * i + 1] = '\0';
517 #if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
519 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
520 const char *src, size_t srclen)
523 for(i = 0; i < srclen; i++) {
524 dest[2 * i] = (unsigned char)(Curl_raw_toupper(src[i]));
525 dest[2 * i + 1] = '\0';
529 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
532 * Set up nt hashed passwords
535 CURLcode Curl_ntlm_core_mk_nt_hash(struct Curl_easy *data,
536 const char *password,
537 unsigned char *ntbuffer /* 21 bytes */)
539 size_t len = strlen(password);
542 if(len > SIZE_T_MAX/2) /* avoid integer overflow */
543 return CURLE_OUT_OF_MEMORY;
544 pw = len ? malloc(len * 2) : (unsigned char *)strdup("");
546 return CURLE_OUT_OF_MEMORY;
548 ascii_to_unicode_le(pw, password, len);
551 * The NT hashed password needs to be created using the password in the
552 * network encoding not the host encoding.
554 result = Curl_convert_to_network(data, (char *)pw, len * 2);
558 /* Create NT hashed password. */
559 Curl_md4it(ntbuffer, pw, 2 * len);
561 memset(ntbuffer + 16, 0, 21 - 16);
568 #if defined(USE_NTLM_V2) && !defined(USE_WINDOWS_SSPI)
570 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
571 * (uppercase UserName + Domain) as the data
573 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
574 const char *domain, size_t domlen,
575 unsigned char *ntlmhash,
576 unsigned char *ntlmv2hash)
578 /* Unicode representation */
580 unsigned char *identity;
581 CURLcode result = CURLE_OK;
583 /* we do the length checks below separately to avoid integer overflow risk
584 on extreme data lengths */
585 if((userlen > SIZE_T_MAX/2) ||
586 (domlen > SIZE_T_MAX/2) ||
587 ((userlen + domlen) > SIZE_T_MAX/2))
588 return CURLE_OUT_OF_MEMORY;
590 identity_len = (userlen + domlen) * 2;
591 identity = malloc(identity_len);
594 return CURLE_OUT_OF_MEMORY;
596 ascii_uppercase_to_unicode_le(identity, user, userlen);
597 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
599 result = Curl_hmacit(Curl_HMAC_MD5, ntlmhash, 16, identity, identity_len,
607 * Curl_ntlm_core_mk_ntlmv2_resp()
609 * This creates the NTLMv2 response as set in the ntlm type-3 message.
613 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
614 * challenge_client [in] - The client nonce (8 bytes)
615 * ntlm [in] - The ntlm data struct being used to read TargetInfo
616 and Server challenge received in the type-2 message
617 * ntresp [out] - The address where a pointer to newly allocated
618 * memory holding the NTLMv2 response.
619 * ntresp_len [out] - The length of the output message.
621 * Returns CURLE_OK on success.
623 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
624 unsigned char *challenge_client,
625 struct ntlmdata *ntlm,
626 unsigned char **ntresp,
627 unsigned int *ntresp_len)
629 /* NTLMv2 response structure :
630 ------------------------------------------------------------------------------
632 ------BLOB--------------------------------------------------------------------
633 16 Signature 0x01010000
634 20 Reserved long (0x00000000)
635 24 Timestamp LE, 64-bit signed value representing the number of
636 tenths of a microsecond since January 1, 1601.
637 32 Client Nonce 8 bytes
639 44 Target Info N bytes (from the type-2 message)
641 ------------------------------------------------------------------------------
644 unsigned int len = 0;
645 unsigned char *ptr = NULL;
646 unsigned char hmac_output[HMAC_MD5_LENGTH];
649 CURLcode result = CURLE_OK;
651 #if CURL_SIZEOF_CURL_OFF_T < 8
652 #error "this section needs 64bit support to work"
655 /* Calculate the timestamp */
657 char *force_timestamp = getenv("CURL_FORCETIME");
659 tw = CURL_OFF_T_C(11644473600) * 10000000;
662 tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
664 /* Calculate the response len */
665 len = HMAC_MD5_LENGTH + NTLMv2_BLOB_LEN;
667 /* Allocate the response */
668 ptr = calloc(1, len);
670 return CURLE_OUT_OF_MEMORY;
672 /* Create the BLOB structure */
673 msnprintf((char *)ptr + HMAC_MD5_LENGTH, NTLMv2_BLOB_LEN,
674 "%c%c%c%c" /* NTLMv2_BLOB_SIGNATURE */
675 "%c%c%c%c", /* Reserved = 0 */
676 NTLMv2_BLOB_SIGNATURE[0], NTLMv2_BLOB_SIGNATURE[1],
677 NTLMv2_BLOB_SIGNATURE[2], NTLMv2_BLOB_SIGNATURE[3],
680 Curl_write64_le(tw, ptr + 24);
681 memcpy(ptr + 32, challenge_client, 8);
682 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
684 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
685 memcpy(ptr + 8, &ntlm->nonce[0], 8);
686 result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, HMAC_MD5_LENGTH, ptr + 8,
687 NTLMv2_BLOB_LEN + 8, hmac_output);
693 /* Concatenate the HMAC MD5 output with the BLOB */
694 memcpy(ptr, hmac_output, HMAC_MD5_LENGTH);
696 /* Return the response */
704 * Curl_ntlm_core_mk_lmv2_resp()
706 * This creates the LMv2 response as used in the ntlm type-3 message.
710 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
711 * challenge_client [in] - The client nonce (8 bytes)
712 * challenge_client [in] - The server challenge (8 bytes)
713 * lmresp [out] - The LMv2 response (24 bytes)
715 * Returns CURLE_OK on success.
717 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
718 unsigned char *challenge_client,
719 unsigned char *challenge_server,
720 unsigned char *lmresp)
722 unsigned char data[16];
723 unsigned char hmac_output[16];
724 CURLcode result = CURLE_OK;
726 memcpy(&data[0], challenge_server, 8);
727 memcpy(&data[8], challenge_client, 8);
729 result = Curl_hmacit(Curl_HMAC_MD5, ntlmv2hash, 16, &data[0], 16,
734 /* Concatenate the HMAC MD5 output with the client nonce */
735 memcpy(lmresp, hmac_output, 16);
736 memcpy(lmresp + 16, challenge_client, 8);
741 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
743 #endif /* USE_NTRESPONSES */
745 #endif /* USE_CURL_NTLM_CORE */