1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 http://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"
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
34 #if !defined(USE_WINDOWS_SSPI) || defined(USE_WIN32_CRYPTO)
39 # include <openssl/des.h>
40 # ifndef OPENSSL_NO_MD4
41 # include <openssl/md4.h>
43 # include <openssl/md5.h>
44 # include <openssl/ssl.h>
45 # include <openssl/rand.h>
48 # ifndef OPENSSL_NO_MD4
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
62 # define DESKEYARG(x) x
64 # define DESKEYARG(x) *x
68 #elif defined(USE_GNUTLS_NETTLE)
70 # include <nettle/des.h>
71 # include <nettle/md4.h>
73 #elif defined(USE_GNUTLS)
76 # define MD5_DIGEST_LENGTH 16
77 # define MD4_DIGEST_LENGTH 16
79 #elif defined(USE_NSS)
84 # include "curl_md4.h"
85 # define MD5_DIGEST_LENGTH MD5_LENGTH
87 #elif defined(USE_DARWINSSL)
89 # include <CommonCrypto/CommonCryptor.h>
90 # include <CommonCrypto/CommonDigest.h>
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>
98 # error "Can't compile NTLM support without a crypto library."
102 #include "non-ascii.h"
104 #include "curl_memory.h"
105 #include "curl_ntlm_core.h"
106 #include "curl_md5.h"
107 #include "curl_hmac.h"
108 #include "warnless.h"
109 #include "curl_endian.h"
111 #define _MPRINTF_REPLACE /* use our functions only */
112 #include <curl/mprintf.h>
114 /* The last #include file should be: */
115 #include "memdebug.h"
117 #define NTLM_HMAC_MD5_LEN (16)
118 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
119 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
122 * Turns a 56-bit key into being 64-bit wide.
124 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
127 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
128 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
129 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
130 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
131 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
132 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
133 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
138 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
139 * key schedule ks is also set.
141 static void setup_des_key(const unsigned char *key_56,
142 DES_key_schedule DESKEYARG(ks))
146 extend_key_56_to_64(key_56, (char *) key);
147 DES_set_odd_parity(&key);
148 DES_set_key(&key, ks);
151 #elif defined(USE_GNUTLS_NETTLE)
153 static void setup_des_key(const unsigned char *key_56,
157 extend_key_56_to_64(key_56, key);
158 des_set_key(des, (const uint8_t*)key);
161 #elif defined(USE_GNUTLS)
164 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
166 static void setup_des_key(const unsigned char *key_56,
167 gcry_cipher_hd_t *des)
170 extend_key_56_to_64(key_56, key);
171 gcry_cipher_setkey(*des, key, 8);
174 #elif defined(USE_NSS)
177 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
178 * the expanded key. The caller is responsible for giving 64 bit of valid
179 * data is IN and (at least) 64 bit large buffer as OUT.
181 static bool encrypt_des(const unsigned char *in, unsigned char *out,
182 const unsigned char *key_56)
184 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
185 PK11SlotInfo *slot = NULL;
186 char key[8]; /* expanded 64 bit key */
188 PK11SymKey *symkey = NULL;
189 SECItem *param = NULL;
190 PK11Context *ctx = NULL;
191 int out_len; /* not used, required by NSS */
194 /* use internal slot for DES encryption (requires NSS to be initialized) */
195 slot = PK11_GetInternalKeySlot();
199 /* expand the 56 bit key to 64 bit and wrap by NSS */
200 extend_key_56_to_64(key_56, key);
201 key_item.data = (unsigned char *)key;
202 key_item.len = /* hard-wired */ 8;
203 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
208 /* create DES encryption context */
209 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
212 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
216 /* perform the encryption */
217 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
218 (unsigned char *)in, /* inbuflen */ 8)
219 && SECSuccess == PK11_Finalize(ctx))
220 rv = /* all OK */ TRUE;
225 PK11_DestroyContext(ctx, PR_TRUE);
227 PK11_FreeSymKey(symkey);
229 SECITEM_FreeItem(param, PR_TRUE);
234 #elif defined(USE_DARWINSSL)
236 static bool encrypt_des(const unsigned char *in, unsigned char *out,
237 const unsigned char *key_56)
243 extend_key_56_to_64(key_56, key);
244 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
245 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
246 8 /* outbuflen */, &out_len);
247 return err == kCCSuccess;
250 #elif defined(USE_OS400CRYPTO)
252 static bool encrypt_des(const unsigned char *in, unsigned char *out,
253 const unsigned char *key_56)
256 _CIPHER_Control_T ctl;
258 ctl.Func_ID = ENCRYPT_ONLY;
260 extend_key_56_to_64(key_56, ctl.Crypto_Key);
261 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
265 #elif defined(USE_WIN32_CRYPTO)
267 static bool encrypt_des(const unsigned char *in, unsigned char *out,
268 const unsigned char *key_56)
279 /* Acquire the crypto provider */
280 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
281 CRYPT_VERIFYCONTEXT))
284 memset(&blob, 0, sizeof(blob));
285 extend_key_56_to_64(key_56, blob.key);
286 blob.hdr.bType = PLAINTEXTKEYBLOB;
287 blob.hdr.bVersion = 2;
288 blob.hdr.aiKeyAlg = CALG_DES;
289 blob.len = sizeof(blob.key);
292 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
293 CryptReleaseContext(hprov, 0);
300 /* Perform the encryption */
301 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
303 CryptDestroyKey(hkey);
304 CryptReleaseContext(hprov, 0);
309 #endif /* defined(USE_WIN32_CRYPTO) */
312 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
313 * 8 byte plaintext is encrypted with each key and the resulting 24
314 * bytes are stored in the results array.
316 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
317 const unsigned char *plaintext,
318 unsigned char *results)
323 setup_des_key(keys, DESKEY(ks));
324 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
325 DESKEY(ks), DES_ENCRYPT);
327 setup_des_key(keys + 7, DESKEY(ks));
328 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
329 DESKEY(ks), DES_ENCRYPT);
331 setup_des_key(keys + 14, DESKEY(ks));
332 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
333 DESKEY(ks), DES_ENCRYPT);
334 #elif defined(USE_GNUTLS_NETTLE)
336 setup_des_key(keys, &des);
337 des_encrypt(&des, 8, results, plaintext);
338 setup_des_key(keys + 7, &des);
339 des_encrypt(&des, 8, results + 8, plaintext);
340 setup_des_key(keys + 14, &des);
341 des_encrypt(&des, 8, results + 16, plaintext);
342 #elif defined(USE_GNUTLS)
343 gcry_cipher_hd_t des;
345 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
346 setup_des_key(keys, &des);
347 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
348 gcry_cipher_close(des);
350 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
351 setup_des_key(keys + 7, &des);
352 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
353 gcry_cipher_close(des);
355 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
356 setup_des_key(keys + 14, &des);
357 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
358 gcry_cipher_close(des);
359 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
360 || defined(USE_WIN32_CRYPTO)
361 encrypt_des(plaintext, results, keys);
362 encrypt_des(plaintext, results + 8, keys + 7);
363 encrypt_des(plaintext, results + 16, keys + 14);
368 * Set up lanmanager hashed password
370 CURLcode Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
371 const char *password,
372 unsigned char *lmbuffer /* 21 bytes */)
375 unsigned char pw[14];
376 static const unsigned char magic[] = {
377 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
379 size_t len = CURLMIN(strlen(password), 14);
381 Curl_strntoupper((char *)pw, password, len);
382 memset(&pw[len], 0, 14 - len);
385 * The LanManager hashed password needs to be created using the
386 * password in the network encoding not the host encoding.
388 result = Curl_convert_to_network(data, (char *)pw, 14);
393 /* Create LanManager hashed password. */
398 setup_des_key(pw, DESKEY(ks));
399 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
400 DESKEY(ks), DES_ENCRYPT);
402 setup_des_key(pw + 7, DESKEY(ks));
403 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
404 DESKEY(ks), DES_ENCRYPT);
405 #elif defined(USE_GNUTLS_NETTLE)
407 setup_des_key(pw, &des);
408 des_encrypt(&des, 8, lmbuffer, magic);
409 setup_des_key(pw + 7, &des);
410 des_encrypt(&des, 8, lmbuffer + 8, magic);
411 #elif defined(USE_GNUTLS)
412 gcry_cipher_hd_t des;
414 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
415 setup_des_key(pw, &des);
416 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
417 gcry_cipher_close(des);
419 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
420 setup_des_key(pw + 7, &des);
421 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
422 gcry_cipher_close(des);
423 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
424 || defined(USE_WIN32_CRYPTO)
425 encrypt_des(magic, lmbuffer, pw);
426 encrypt_des(magic, lmbuffer + 8, pw + 7);
429 memset(lmbuffer + 16, 0, 21 - 16);
436 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
440 for(i = 0; i < srclen; i++) {
441 dest[2 * i] = (unsigned char)src[i];
442 dest[2 * i + 1] = '\0';
446 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
448 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
449 const char *src, size_t srclen)
452 for(i = 0; i < srclen; i++) {
453 dest[2 * i] = (unsigned char)(toupper(src[i]));
454 dest[2 * i + 1] = '\0';
458 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
461 * Set up nt hashed passwords
463 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
464 const char *password,
465 unsigned char *ntbuffer /* 21 bytes */)
467 size_t len = strlen(password);
468 unsigned char *pw = malloc(len * 2);
471 return CURLE_OUT_OF_MEMORY;
473 ascii_to_unicode_le(pw, password, len);
476 * The NT hashed password needs to be created using the password in the
477 * network encoding not the host encoding.
479 result = Curl_convert_to_network(data, (char *)pw, len * 2);
484 /* Create NT hashed password. */
488 MD4_Update(&MD4pw, pw, 2 * len);
489 MD4_Final(ntbuffer, &MD4pw);
490 #elif defined(USE_GNUTLS_NETTLE)
491 struct md4_ctx MD4pw;
493 md4_update(&MD4pw, (unsigned int)(2 * len), pw);
494 md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
495 #elif defined(USE_GNUTLS)
497 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
498 gcry_md_write(MD4pw, pw, 2 * len);
499 memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
500 gcry_md_close(MD4pw);
501 #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
502 Curl_md4it(ntbuffer, pw, 2 * len);
503 #elif defined(USE_DARWINSSL)
504 (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
505 #elif defined(USE_WIN32_CRYPTO)
507 if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
508 CRYPT_VERIFYCONTEXT)) {
510 if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
512 CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
513 CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
514 CryptDestroyHash(hhash);
516 CryptReleaseContext(hprov, 0);
520 memset(ntbuffer + 16, 0, 21 - 16);
528 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
530 /* This returns the HMAC MD5 digest */
531 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
532 const unsigned char *data, unsigned int datalen,
533 unsigned char *output)
535 HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
538 return CURLE_OUT_OF_MEMORY;
540 /* Update the digest with the given challenge */
541 Curl_HMAC_update(ctxt, data, datalen);
543 /* Finalise the digest */
544 Curl_HMAC_final(ctxt, output);
549 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
550 * (uppercase UserName + Domain) as the data
552 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
553 const char *domain, size_t domlen,
554 unsigned char *ntlmhash,
555 unsigned char *ntlmv2hash)
557 /* Unicode representation */
558 size_t identity_len = (userlen + domlen) * 2;
559 unsigned char *identity = malloc(identity_len);
560 CURLcode result = CURLE_OK;
563 return CURLE_OUT_OF_MEMORY;
565 ascii_uppercase_to_unicode_le(identity, user, userlen);
566 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
568 result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
571 Curl_safefree(identity);
577 * Curl_ntlm_core_mk_ntlmv2_resp()
579 * This creates the NTLMv2 response as set in the ntlm type-3 message.
583 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
584 * challenge_client [in] - The client nonce (8 bytes)
585 * ntlm [in] - The ntlm data struct being used to read TargetInfo
586 and Server challenge received in the type-2 message
587 * ntresp [out] - The address where a pointer to newly allocated
588 * memory holding the NTLMv2 response.
589 * ntresp_len [out] - The length of the output message.
591 * Returns CURLE_OK on success.
593 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
594 unsigned char *challenge_client,
595 struct ntlmdata *ntlm,
596 unsigned char **ntresp,
597 unsigned int *ntresp_len)
599 /* NTLMv2 response structure :
600 ------------------------------------------------------------------------------
602 ------BLOB--------------------------------------------------------------------
603 16 Signature 0x01010000
604 20 Reserved long (0x00000000)
605 24 Timestamp LE, 64-bit signed value representing the number of
606 tenths of a microsecond since January 1, 1601.
607 32 Client Nonce 8 bytes
609 44 Target Info N bytes (from the type-2 message)
611 ------------------------------------------------------------------------------
614 unsigned int len = 0;
615 unsigned char *ptr = NULL;
616 unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
617 #if defined(HAVE_LONGLONG)
622 CURLcode result = CURLE_OK;
624 /* Calculate the timestamp */
626 char *force_timestamp = getenv("CURL_FORCETIME");
628 tw = 11644473600ULL * 10000000ULL;
631 tw = ((long long)time(NULL) + 11644473600ULL) * 10000000ULL;
633 /* Calculate the response len */
634 len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
636 /* Allocate the response */
639 return CURLE_OUT_OF_MEMORY;
643 /* Create the BLOB structure */
644 snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
645 NTLMv2_BLOB_SIGNATURE
646 "%c%c%c%c", /* Reserved = 0 */
649 Curl_write64_le(tw, ptr + 24);
650 memcpy(ptr + 32, challenge_client, 8);
651 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
653 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
654 memcpy(ptr + 8, &ntlm->nonce[0], 8);
655 result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
656 NTLMv2_BLOB_LEN + 8, hmac_output);
663 /* Concatenate the HMAC MD5 output with the BLOB */
664 memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
666 /* Return the response */
674 * Curl_ntlm_core_mk_lmv2_resp()
676 * This creates the LMv2 response as used in the ntlm type-3 message.
680 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
681 * challenge_client [in] - The client nonce (8 bytes)
682 * challenge_client [in] - The server challenge (8 bytes)
683 * lmresp [out] - The LMv2 response (24 bytes)
685 * Returns CURLE_OK on success.
687 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
688 unsigned char *challenge_client,
689 unsigned char *challenge_server,
690 unsigned char *lmresp)
692 unsigned char data[16];
693 unsigned char hmac_output[16];
694 CURLcode result = CURLE_OK;
696 memcpy(&data[0], challenge_server, 8);
697 memcpy(&data[8], challenge_client, 8);
699 result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
703 /* Concatenate the HMAC MD5 output with the client nonce */
704 memcpy(lmresp, hmac_output, 16);
705 memcpy(lmresp+16, challenge_client, 8);
710 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
712 #endif /* USE_NTRESPONSES */
714 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
716 #endif /* USE_NTLM */