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