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_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 #if defined(HAVE_BORINGSSL)
150 Curl_des_set_odd_parity((unsigned char *) &key, sizeof(key));
152 DES_set_odd_parity(&key);
156 DES_set_key(&key, ks);
159 #elif defined(USE_GNUTLS_NETTLE)
161 static void setup_des_key(const unsigned char *key_56,
166 /* Expand the 56-bit key to 64-bits */
167 extend_key_56_to_64(key_56, key);
169 /* Set the key parity to odd */
170 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
173 des_set_key(des, (const uint8_t *) key);
176 #elif defined(USE_GNUTLS)
179 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
181 static void setup_des_key(const unsigned char *key_56,
182 gcry_cipher_hd_t *des)
186 /* Expand the 56-bit key to 64-bits */
187 extend_key_56_to_64(key_56, key);
189 /* Set the key parity to odd */
190 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
193 gcry_cipher_setkey(*des, key, sizeof(key));
196 #elif defined(USE_NSS)
199 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
200 * the expanded key. The caller is responsible for giving 64 bit of valid
201 * data is IN and (at least) 64 bit large buffer as OUT.
203 static bool encrypt_des(const unsigned char *in, unsigned char *out,
204 const unsigned char *key_56)
206 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
207 PK11SlotInfo *slot = NULL;
208 char key[8]; /* expanded 64 bit key */
210 PK11SymKey *symkey = NULL;
211 SECItem *param = NULL;
212 PK11Context *ctx = NULL;
213 int out_len; /* not used, required by NSS */
216 /* use internal slot for DES encryption (requires NSS to be initialized) */
217 slot = PK11_GetInternalKeySlot();
221 /* Expand the 56-bit key to 64-bits */
222 extend_key_56_to_64(key_56, key);
224 /* Set the key parity to odd */
225 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
228 key_item.data = (unsigned char *)key;
229 key_item.len = sizeof(key);
230 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
235 /* Create the DES encryption context */
236 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
239 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
243 /* Perform the encryption */
244 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
245 (unsigned char *)in, /* inbuflen */ 8)
246 && SECSuccess == PK11_Finalize(ctx))
247 rv = /* all OK */ TRUE;
252 PK11_DestroyContext(ctx, PR_TRUE);
254 PK11_FreeSymKey(symkey);
256 SECITEM_FreeItem(param, PR_TRUE);
261 #elif defined(USE_DARWINSSL)
263 static bool encrypt_des(const unsigned char *in, unsigned char *out,
264 const unsigned char *key_56)
270 /* Expand the 56-bit key to 64-bits */
271 extend_key_56_to_64(key_56, key);
273 /* Set the key parity to odd */
274 Curl_des_set_odd_parity((unsigned char *) key, sizeof(key));
276 /* Perform the encryption */
277 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
278 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
279 8 /* outbuflen */, &out_len);
281 return err == kCCSuccess;
284 #elif defined(USE_OS400CRYPTO)
286 static bool encrypt_des(const unsigned char *in, unsigned char *out,
287 const unsigned char *key_56)
290 _CIPHER_Control_T ctl;
292 /* Setup the cipher control structure */
293 ctl.Func_ID = ENCRYPT_ONLY;
294 ctl.Data_Len = sizeof(key);
296 /* Expand the 56-bit key to 64-bits */
297 extend_key_56_to_64(key_56, ctl.Crypto_Key);
299 /* Set the key parity to odd */
300 Curl_des_set_odd_parity((unsigned char *) ctl.Crypto_Key, ctl.Data_Len);
302 /* Perform the encryption */
303 _CIPHER((_SPCPTR *) &out, &ctl, (_SPCPTR *) &in);
308 #elif defined(USE_WIN32_CRYPTO)
310 static bool encrypt_des(const unsigned char *in, unsigned char *out,
311 const unsigned char *key_56)
322 /* Acquire the crypto provider */
323 if(!CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
324 CRYPT_VERIFYCONTEXT))
327 /* Setup the key blob structure */
328 memset(&blob, 0, sizeof(blob));
329 blob.hdr.bType = PLAINTEXTKEYBLOB;
330 blob.hdr.bVersion = 2;
331 blob.hdr.aiKeyAlg = CALG_DES;
332 blob.len = sizeof(blob.key);
334 /* Expand the 56-bit key to 64-bits */
335 extend_key_56_to_64(key_56, blob.key);
337 /* Set the key parity to odd */
338 Curl_des_set_odd_parity((unsigned char *) blob.key, sizeof(blob.key));
341 if(!CryptImportKey(hprov, (BYTE *) &blob, sizeof(blob), 0, 0, &hkey)) {
342 CryptReleaseContext(hprov, 0);
349 /* Perform the encryption */
350 CryptEncrypt(hkey, 0, FALSE, 0, out, &len, len);
352 CryptDestroyKey(hkey);
353 CryptReleaseContext(hprov, 0);
358 #endif /* defined(USE_WIN32_CRYPTO) */
361 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
362 * 8 byte plaintext is encrypted with each key and the resulting 24
363 * bytes are stored in the results array.
365 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
366 const unsigned char *plaintext,
367 unsigned char *results)
372 setup_des_key(keys, DESKEY(ks));
373 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
374 DESKEY(ks), DES_ENCRYPT);
376 setup_des_key(keys + 7, DESKEY(ks));
377 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
378 DESKEY(ks), DES_ENCRYPT);
380 setup_des_key(keys + 14, DESKEY(ks));
381 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
382 DESKEY(ks), DES_ENCRYPT);
383 #elif defined(USE_GNUTLS_NETTLE)
385 setup_des_key(keys, &des);
386 des_encrypt(&des, 8, results, plaintext);
387 setup_des_key(keys + 7, &des);
388 des_encrypt(&des, 8, results + 8, plaintext);
389 setup_des_key(keys + 14, &des);
390 des_encrypt(&des, 8, results + 16, plaintext);
391 #elif defined(USE_GNUTLS)
392 gcry_cipher_hd_t des;
394 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
395 setup_des_key(keys, &des);
396 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
397 gcry_cipher_close(des);
399 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
400 setup_des_key(keys + 7, &des);
401 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
402 gcry_cipher_close(des);
404 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
405 setup_des_key(keys + 14, &des);
406 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
407 gcry_cipher_close(des);
408 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
409 || defined(USE_WIN32_CRYPTO)
410 encrypt_des(plaintext, results, keys);
411 encrypt_des(plaintext, results + 8, keys + 7);
412 encrypt_des(plaintext, results + 16, keys + 14);
417 * Set up lanmanager hashed password
419 CURLcode Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
420 const char *password,
421 unsigned char *lmbuffer /* 21 bytes */)
424 unsigned char pw[14];
425 static const unsigned char magic[] = {
426 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
428 size_t len = CURLMIN(strlen(password), 14);
430 Curl_strntoupper((char *)pw, password, len);
431 memset(&pw[len], 0, 14 - len);
434 * The LanManager hashed password needs to be created using the
435 * password in the network encoding not the host encoding.
437 result = Curl_convert_to_network(data, (char *)pw, 14);
442 /* Create LanManager hashed password. */
447 setup_des_key(pw, DESKEY(ks));
448 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
449 DESKEY(ks), DES_ENCRYPT);
451 setup_des_key(pw + 7, DESKEY(ks));
452 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
453 DESKEY(ks), DES_ENCRYPT);
454 #elif defined(USE_GNUTLS_NETTLE)
456 setup_des_key(pw, &des);
457 des_encrypt(&des, 8, lmbuffer, magic);
458 setup_des_key(pw + 7, &des);
459 des_encrypt(&des, 8, lmbuffer + 8, magic);
460 #elif defined(USE_GNUTLS)
461 gcry_cipher_hd_t des;
463 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
464 setup_des_key(pw, &des);
465 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
466 gcry_cipher_close(des);
468 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
469 setup_des_key(pw + 7, &des);
470 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
471 gcry_cipher_close(des);
472 #elif defined(USE_NSS) || defined(USE_DARWINSSL) || defined(USE_OS400CRYPTO) \
473 || defined(USE_WIN32_CRYPTO)
474 encrypt_des(magic, lmbuffer, pw);
475 encrypt_des(magic, lmbuffer + 8, pw + 7);
478 memset(lmbuffer + 16, 0, 21 - 16);
485 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
489 for(i = 0; i < srclen; i++) {
490 dest[2 * i] = (unsigned char)src[i];
491 dest[2 * i + 1] = '\0';
495 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
497 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
498 const char *src, size_t srclen)
501 for(i = 0; i < srclen; i++) {
502 dest[2 * i] = (unsigned char)(toupper(src[i]));
503 dest[2 * i + 1] = '\0';
507 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
510 * Set up nt hashed passwords
513 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
514 const char *password,
515 unsigned char *ntbuffer /* 21 bytes */)
517 size_t len = strlen(password);
518 unsigned char *pw = malloc(len * 2);
521 return CURLE_OUT_OF_MEMORY;
523 ascii_to_unicode_le(pw, password, len);
526 * The NT hashed password needs to be created using the password in the
527 * network encoding not the host encoding.
529 result = Curl_convert_to_network(data, (char *)pw, len * 2);
534 /* Create NT hashed password. */
538 MD4_Update(&MD4pw, pw, 2 * len);
539 MD4_Final(ntbuffer, &MD4pw);
540 #elif defined(USE_GNUTLS_NETTLE)
541 struct md4_ctx MD4pw;
543 md4_update(&MD4pw, (unsigned int)(2 * len), pw);
544 md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
545 #elif defined(USE_GNUTLS)
547 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
548 gcry_md_write(MD4pw, pw, 2 * len);
549 memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
550 gcry_md_close(MD4pw);
551 #elif defined(USE_NSS) || defined(USE_OS400CRYPTO)
552 Curl_md4it(ntbuffer, pw, 2 * len);
553 #elif defined(USE_DARWINSSL)
554 (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
555 #elif defined(USE_WIN32_CRYPTO)
557 if(CryptAcquireContext(&hprov, NULL, NULL, PROV_RSA_FULL,
558 CRYPT_VERIFYCONTEXT)) {
560 if(CryptCreateHash(hprov, CALG_MD4, 0, 0, &hhash)) {
562 CryptHashData(hhash, pw, (unsigned int)len * 2, 0);
563 CryptGetHashParam(hhash, HP_HASHVAL, ntbuffer, &length, 0);
564 CryptDestroyHash(hhash);
566 CryptReleaseContext(hprov, 0);
570 memset(ntbuffer + 16, 0, 21 - 16);
578 #if USE_NTLM_V2 && !defined(USE_WINDOWS_SSPI)
580 /* This returns the HMAC MD5 digest */
581 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
582 const unsigned char *data, unsigned int datalen,
583 unsigned char *output)
585 HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
588 return CURLE_OUT_OF_MEMORY;
590 /* Update the digest with the given challenge */
591 Curl_HMAC_update(ctxt, data, datalen);
593 /* Finalise the digest */
594 Curl_HMAC_final(ctxt, output);
599 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
600 * (uppercase UserName + Domain) as the data
602 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
603 const char *domain, size_t domlen,
604 unsigned char *ntlmhash,
605 unsigned char *ntlmv2hash)
607 /* Unicode representation */
608 size_t identity_len = (userlen + domlen) * 2;
609 unsigned char *identity = malloc(identity_len);
610 CURLcode result = CURLE_OK;
613 return CURLE_OUT_OF_MEMORY;
615 ascii_uppercase_to_unicode_le(identity, user, userlen);
616 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
618 result = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
627 * Curl_ntlm_core_mk_ntlmv2_resp()
629 * This creates the NTLMv2 response as set in the ntlm type-3 message.
633 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
634 * challenge_client [in] - The client nonce (8 bytes)
635 * ntlm [in] - The ntlm data struct being used to read TargetInfo
636 and Server challenge received in the type-2 message
637 * ntresp [out] - The address where a pointer to newly allocated
638 * memory holding the NTLMv2 response.
639 * ntresp_len [out] - The length of the output message.
641 * Returns CURLE_OK on success.
643 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
644 unsigned char *challenge_client,
645 struct ntlmdata *ntlm,
646 unsigned char **ntresp,
647 unsigned int *ntresp_len)
649 /* NTLMv2 response structure :
650 ------------------------------------------------------------------------------
652 ------BLOB--------------------------------------------------------------------
653 16 Signature 0x01010000
654 20 Reserved long (0x00000000)
655 24 Timestamp LE, 64-bit signed value representing the number of
656 tenths of a microsecond since January 1, 1601.
657 32 Client Nonce 8 bytes
659 44 Target Info N bytes (from the type-2 message)
661 ------------------------------------------------------------------------------
664 unsigned int len = 0;
665 unsigned char *ptr = NULL;
666 unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
667 #if defined(HAVE_LONGLONG)
672 CURLcode result = CURLE_OK;
674 /* Calculate the timestamp */
676 char *force_timestamp = getenv("CURL_FORCETIME");
678 tw = 11644473600ULL * 10000000ULL;
681 tw = ((long long)time(NULL) + 11644473600ULL) * 10000000ULL;
683 /* Calculate the response len */
684 len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
686 /* Allocate the response */
689 return CURLE_OUT_OF_MEMORY;
693 /* Create the BLOB structure */
694 snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
695 NTLMv2_BLOB_SIGNATURE
696 "%c%c%c%c", /* Reserved = 0 */
699 Curl_write64_le(tw, ptr + 24);
700 memcpy(ptr + 32, challenge_client, 8);
701 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
703 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
704 memcpy(ptr + 8, &ntlm->nonce[0], 8);
705 result = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
706 NTLMv2_BLOB_LEN + 8, hmac_output);
712 /* Concatenate the HMAC MD5 output with the BLOB */
713 memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
715 /* Return the response */
723 * Curl_ntlm_core_mk_lmv2_resp()
725 * This creates the LMv2 response as used in the ntlm type-3 message.
729 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
730 * challenge_client [in] - The client nonce (8 bytes)
731 * challenge_client [in] - The server challenge (8 bytes)
732 * lmresp [out] - The LMv2 response (24 bytes)
734 * Returns CURLE_OK on success.
736 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
737 unsigned char *challenge_client,
738 unsigned char *challenge_server,
739 unsigned char *lmresp)
741 unsigned char data[16];
742 unsigned char hmac_output[16];
743 CURLcode result = CURLE_OK;
745 memcpy(&data[0], challenge_server, 8);
746 memcpy(&data[8], challenge_client, 8);
748 result = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
752 /* Concatenate the HMAC MD5 output with the client nonce */
753 memcpy(lmresp, hmac_output, 16);
754 memcpy(lmresp+16, challenge_client, 8);
759 #endif /* USE_NTLM_V2 && !USE_WINDOWS_SSPI */
761 #endif /* USE_NTRESPONSES */
763 #endif /* !USE_WINDOWS_SSPI || USE_WIN32_CRYPTO */
765 #endif /* USE_NTLM */