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 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"
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_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 #include "curl_printf.h"
112 /* The last #include files should be: */
113 #include "curl_memory.h"
114 #include "memdebug.h"
116 #define NTLM_HMAC_MD5_LEN (16)
117 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
118 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
121 * Turns a 56-bit key into being 64-bit wide.
123 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
126 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
127 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
128 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
129 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
130 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
131 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
132 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
137 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
138 * key schedule ks is also set.
140 static void setup_des_key(const unsigned char *key_56,
141 DES_key_schedule DESKEYARG(ks))
145 /* Expand the 56-bit key to 64-bits */
146 extend_key_56_to_64(key_56, (char *) &key);
148 /* Set the key parity to odd */
149 DES_set_odd_parity(&key);
152 DES_set_key(&key, ks);
155 #elif defined(USE_GNUTLS_NETTLE)
157 static void setup_des_key(const unsigned char *key_56,
162 /* Expand the 56-bit key to 64-bits */
163 extend_key_56_to_64(key_56, key);
165 /* Set the key parity to odd */
166 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
169 des_set_key(des, (const uint8_t *) key);
172 #elif defined(USE_GNUTLS)
175 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
177 static void setup_des_key(const unsigned char *key_56,
178 gcry_cipher_hd_t *des)
182 /* Expand the 56-bit key to 64-bits */
183 extend_key_56_to_64(key_56, key);
185 /* Set the key parity to odd */
186 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
189 gcry_cipher_setkey(*des, key, sizeof(key));
192 #elif defined(USE_NSS)
195 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
196 * the expanded key. The caller is responsible for giving 64 bit of valid
197 * data is IN and (at least) 64 bit large buffer as OUT.
199 static bool encrypt_des(const unsigned char *in, unsigned char *out,
200 const unsigned char *key_56)
202 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
203 PK11SlotInfo *slot = NULL;
204 char key[8]; /* expanded 64 bit key */
206 PK11SymKey *symkey = NULL;
207 SECItem *param = NULL;
208 PK11Context *ctx = NULL;
209 int out_len; /* not used, required by NSS */
212 /* use internal slot for DES encryption (requires NSS to be initialized) */
213 slot = PK11_GetInternalKeySlot();
217 /* Expand the 56-bit key to 64-bits */
218 extend_key_56_to_64(key_56, key);
220 /* Set the key parity to odd */
221 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
224 key_item.data = (unsigned char *)key;
225 key_item.len = sizeof(key);
226 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
231 /* Create the DES encryption context */
232 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
235 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
239 /* Perform the encryption */
240 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
241 (unsigned char *)in, /* inbuflen */ 8)
242 && SECSuccess == PK11_Finalize(ctx))
243 rv = /* all OK */ TRUE;
248 PK11_DestroyContext(ctx, PR_TRUE);
250 PK11_FreeSymKey(symkey);
252 SECITEM_FreeItem(param, PR_TRUE);
257 #elif defined(USE_DARWINSSL)
259 static bool encrypt_des(const unsigned char *in, unsigned char *out,
260 const unsigned char *key_56)
266 /* Expand the 56-bit key to 64-bits */
267 extend_key_56_to_64(key_56, key);
269 /* Set the key parity to odd */
270 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
272 /* Perform the encryption */
273 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
274 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
275 8 /* outbuflen */, &out_len);
277 return err == kCCSuccess;
280 #elif defined(USE_OS400CRYPTO)
282 static bool encrypt_des(const unsigned char *in, unsigned char *out,
283 const unsigned char *key_56)
286 _CIPHER_Control_T ctl;
288 /* Setup the cipher control structure */
289 ctl.Func_ID = ENCRYPT_ONLY;
290 ctl.Data_Len = sizeof(key);
292 /* Expand the 56-bit key to 64-bits */
293 extend_key_56_to_64(key_56, ctl.Crypto_Key);
295 /* Set the key parity to odd */
296 Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
298 /* Perform the encryption */
299 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
304 #elif defined(USE_WIN32_CRYPTO)
306 static bool encrypt_des(const unsigned char *in, unsigned char *out,
307 const unsigned char *key_56)
318 /* Acquire the crypto provider */
319 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
320 CRYPT_VERIFYCONTEXT))
323 /* Setup the key blob structure */
324 memset(&blob, 0, sizeof(blob));
325 blob.hdr.bType = PLAINTEXTKEYBLOB;
326 blob.hdr.bVersion = 2;
327 blob.hdr.aiKeyAlg = CALG_DES;
328 blob.len = sizeof(blob.key);
330 /* Expand the 56-bit key to 64-bits */
331 extend_key_56_to_64(key_56, blob.key);
333 /* Set the key parity to odd */
334 Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
337 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
338 CryptReleaseContext(hprov, 0);
345 /* Perform the encryption */
346 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
348 CryptDestroyKey(hkey);
349 CryptReleaseContext(hprov, 0);
354 #endif /* defined(USE_WIN32_CRYPTO) */
357 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
358 * 8 byte plaintext is encrypted with each key and the resulting 24
359 * bytes are stored in the results array.
361 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
362 const unsigned char *plaintext,
363 unsigned char *results)
368 setup_des_key(keys, DESKEY(ks));
369 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
370 DESKEY(ks), DES_ENCRYPT);
372 setup_des_key(keys + 7, DESKEY(ks));
373 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
374 DESKEY(ks), DES_ENCRYPT);
376 setup_des_key(keys + 14, DESKEY(ks));
377 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
378 DESKEY(ks), DES_ENCRYPT);
379 #elif defined(USE_GNUTLS_NETTLE)
381 setup_des_key(keys, &des);
382 des_encrypt(&des, 8, results, plaintext);
383 setup_des_key(keys + 7, &des);
384 des_encrypt(&des, 8, results + 8, plaintext);
385 setup_des_key(keys + 14, &des);
386 des_encrypt(&des, 8, results + 16, plaintext);
387 #elif defined(USE_GNUTLS)
388 gcry_cipher_hd_t des;
390 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
391 setup_des_key(keys, &des);
392 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
393 gcry_cipher_close(des);
395 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
396 setup_des_key(keys + 7, &des);
397 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
398 gcry_cipher_close(des);
400 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
401 setup_des_key(keys + 14, &des);
402 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
403 gcry_cipher_close(des);
404 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
405 || defined(USE_WIN32_CRYPTO)
406 encrypt_des(plaintext, results, keys);
407 encrypt_des(plaintext, results + 8, keys + 7);
408 encrypt_des(plaintext, results + 16, keys + 14);
413 * Set up lanmanager hashed password
415 CURLcode Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
416 const char *password,
417 unsigned char *lmbuffer /* 21 bytes */)
420 unsigned char pw[14];
421 static const unsigned char magic[] = {
422 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
424 size_t len = CURLMIN(strlen(password), 14);
426 Curl_strntoupper((char *)pw, password, len);
427 memset(&pw[len], 0, 14 - len);
430 * The LanManager hashed password needs to be created using the
431 * password in the network encoding not the host encoding.
433 result = Curl_convert_to_network(data, (char *)pw, 14);
438 /* Create LanManager hashed password. */
443 setup_des_key(pw, DESKEY(ks));
444 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
445 DESKEY(ks), DES_ENCRYPT);
447 setup_des_key(pw + 7, DESKEY(ks));
448 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
449 DESKEY(ks), DES_ENCRYPT);
450 #elif defined(USE_GNUTLS_NETTLE)
452 setup_des_key(pw, &des);
453 des_encrypt(&des, 8, lmbuffer, magic);
454 setup_des_key(pw + 7, &des);
455 des_encrypt(&des, 8, lmbuffer + 8, magic);
456 #elif defined(USE_GNUTLS)
457 gcry_cipher_hd_t des;
459 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
460 setup_des_key(pw, &des);
461 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
462 gcry_cipher_close(des);
464 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
465 setup_des_key(pw + 7, &des);
466 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
467 gcry_cipher_close(des);
468 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
469 || defined(USE_WIN32_CRYPTO)
470 encrypt_des(magic, lmbuffer, pw);
471 encrypt_des(magic, lmbuffer + 8, pw + 7);
474 memset(lmbuffer + 16, 0, 21 - 16);
481 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
485 for(i = 0; i < srclen; i++) {
486 dest[2 * i] = (unsigned char)src[i];
487 dest[2 * i + 1] = '\0';
491 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
493 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
494 const char *src, size_t srclen)
497 for(i = 0; i < srclen; i++) {
498 dest[2 * i] = (unsigned char)(toupper(src[i]));
499 dest[2 * i + 1] = '\0';
503 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
506 * Set up nt hashed passwords
509 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
510 const char *password,
511 unsigned char *ntbuffer /* 21 bytes */)
513 size_t len = strlen(password);
514 unsigned char *pw = malloc(len * 2);
517 return CURLE_OUT_OF_MEMORY;
519 ascii_to_unicode_le(pw, password, len);
522 * The NT hashed password needs to be created using the password in the
523 * network encoding not the host encoding.
525 result = Curl_convert_to_network(data, (char *)pw, len * 2);
530 /* Create NT hashed password. */
534 MD4_Update(&MD4pw, pw, 2 * len);
535 MD4_Final(ntbuffer, &MD4pw);
536 #elif defined(USE_GNUTLS_NETTLE)
537 struct md4_ctx MD4pw;
539 md4_update(&MD4pw, (unsigned int)(2 * len), pw);
540 md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
541 #elif defined(USE_GNUTLS)
543 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
544 gcry_md_write(MD4pw, pw, 2 * len);
545 memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
546 gcry_md_close(MD4pw);
547 #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
548 Curl_md4it(ntbuffer, pw, 2 * len);
549 #elif defined(USE_DARWINSSL)
550 (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
551 #elif defined(USE_WIN32_CRYPTO)
553 if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
554 CRYPT_VERIFYCONTEXT)) {
556 if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
558 CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
559 CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
560 CryptDestroyHash(hhash);
562 CryptReleaseContext(hprov, 0);
566 memset(ntbuffer + 16, 0, 21 - 16);
574 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
576 /* This returns the HMAC MD5 digest */
577 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
578 const unsigned char *data, unsigned int datalen,
579 unsigned char *output)
581 HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
584 return CURLE_OUT_OF_MEMORY;
586 /* Update the digest with the given challenge */
587 Curl_HMAC_update(ctxt, data, datalen);
589 /* Finalise the digest */
590 Curl_HMAC_final(ctxt, output);
595 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
596 * (uppercase UserName + Domain) as the data
598 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
599 const char *domain, size_t domlen,
600 unsigned char *ntlmhash,
601 unsigned char *ntlmv2hash)
603 /* Unicode representation */
604 size_t identity_len = (userlen + domlen) * 2;
605 unsigned char *identity = malloc(identity_len);
606 CURLcode result = CURLE_OK;
609 return CURLE_OUT_OF_MEMORY;
611 ascii_uppercase_to_unicode_le(identity, user, userlen);
612 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
614 result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
623 * Curl_ntlm_core_mk_ntlmv2_resp()
625 * This creates the NTLMv2 response as set in the ntlm type-3 message.
629 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
630 * challenge_client [in] - The client nonce (8 bytes)
631 * ntlm [in] - The ntlm data struct being used to read TargetInfo
632 and Server challenge received in the type-2 message
633 * ntresp [out] - The address where a pointer to newly allocated
634 * memory holding the NTLMv2 response.
635 * ntresp_len [out] - The length of the output message.
637 * Returns CURLE_OK on success.
639 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
640 unsigned char *challenge_client,
641 struct ntlmdata *ntlm,
642 unsigned char **ntresp,
643 unsigned int *ntresp_len)
645 /* NTLMv2 response structure :
646 ------------------------------------------------------------------------------
648 ------BLOB--------------------------------------------------------------------
649 16 Signature 0x01010000
650 20 Reserved long (0x00000000)
651 24 Timestamp LE, 64-bit signed value representing the number of
652 tenths of a microsecond since January 1, 1601.
653 32 Client Nonce 8 bytes
655 44 Target Info N bytes (from the type-2 message)
657 ------------------------------------------------------------------------------
660 unsigned int len = 0;
661 unsigned char *ptr = NULL;
662 unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
665 CURLcode result = CURLE_OK;
667 #if CURL_SIZEOF_CURL_OFF_T < 8
668 #error "this section needs 64bit support to work"
671 /* Calculate the timestamp */
673 char *force_timestamp = getenv("CURL_FORCETIME");
675 tw = CURL_OFF_T_C(11644473600) * 10000000;
678 tw = ((curl_off_t)time(NULL) + CURL_OFF_T_C(11644473600)) * 10000000;
680 /* Calculate the response len */
681 len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
683 /* Allocate the response */
686 return CURLE_OUT_OF_MEMORY;
690 /* Create the BLOB structure */
691 snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
692 NTLMv2_BLOB_SIGNATURE
693 "%c%c%c%c", /* Reserved = 0 */
696 Curl_write64_le(tw, ptr + 24);
697 memcpy(ptr + 32, challenge_client, 8);
698 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
700 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
701 memcpy(ptr + 8, &ntlm->nonce[0], 8);
702 result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
703 NTLMv2_BLOB_LEN + 8, hmac_output);
709 /* Concatenate the HMAC MD5 output with the BLOB */
710 memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
712 /* Return the response */
720 * Curl_ntlm_core_mk_lmv2_resp()
722 * This creates the LMv2 response as used in the ntlm type-3 message.
726 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
727 * challenge_client [in] - The client nonce (8 bytes)
728 * challenge_client [in] - The server challenge (8 bytes)
729 * lmresp [out] - The LMv2 response (24 bytes)
731 * Returns CURLE_OK on success.
733 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
734 unsigned char *challenge_client,
735 unsigned char *challenge_server,
736 unsigned char *lmresp)
738 unsigned char data[16];
739 unsigned char hmac_output[16];
740 CURLcode result = CURLE_OK;
742 memcpy(&data[0], challenge_server, 8);
743 memcpy(&data[8], challenge_client, 8);
745 result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
749 /* Concatenate the HMAC MD5 output with the client nonce */
750 memcpy(lmresp, hmac_output, 16);
751 memcpy(lmresp+16, challenge_client, 8);
756 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
758 #endif /* USE_NTRESPONSES */
760 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
762 #endif /* USE_NTLM */