1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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"
25 #if defined(USE_NTLM) && !defined(USE_WINDOWS_SSPI)
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
37 # include <openssl/des.h>
38 # ifndef OPENSSL_NO_MD4
39 # include <openssl/md4.h>
41 # include <openssl/md5.h>
42 # include <openssl/ssl.h>
43 # include <openssl/rand.h>
46 # ifndef OPENSSL_NO_MD4
53 # if (OPENSSL_VERSION_NUMBER < 0x00907001L)
54 # define DES_key_schedule des_key_schedule
55 # define DES_cblock des_cblock
56 # define DES_set_odd_parity des_set_odd_parity
57 # define DES_set_key des_set_key
58 # define DES_ecb_encrypt des_ecb_encrypt
60 # define DESKEYARG(x) x
62 # define DESKEYARG(x) *x
66 #elif defined(USE_GNUTLS_NETTLE)
68 # include <nettle/des.h>
69 # include <nettle/md4.h>
71 #elif defined(USE_GNUTLS)
74 # define MD5_DIGEST_LENGTH 16
75 # define MD4_DIGEST_LENGTH 16
77 #elif defined(USE_NSS)
82 # include "curl_md4.h"
83 # define MD5_DIGEST_LENGTH MD5_LENGTH
85 #elif defined(USE_DARWINSSL)
87 # include <CommonCrypto/CommonCryptor.h>
88 # include <CommonCrypto/CommonDigest.h>
91 # error "Can't compile NTLM support without a crypto library."
95 #include "non-ascii.h"
97 #include "curl_memory.h"
98 #include "curl_ntlm_core.h"
100 #include "curl_hmac.h"
101 #include "warnless.h"
103 #define _MPRINTF_REPLACE /* use our functions only */
104 #include <curl/mprintf.h>
106 /* The last #include file should be: */
107 #include "memdebug.h"
109 #define NTLM_HMAC_MD5_LEN (16)
110 #define NTLMv2_BLOB_SIGNATURE "\x01\x01\x00\x00"
111 #define NTLMv2_BLOB_LEN (44 -16 + ntlm->target_info_len + 4)
115 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key. The
116 * key schedule ks is also set.
118 static void setup_des_key(const unsigned char *key_56,
119 DES_key_schedule DESKEYARG(ks))
124 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
125 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
126 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
127 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
128 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
129 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
130 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
132 DES_set_odd_parity(&key);
133 DES_set_key(&key, ks);
136 #else /* defined(USE_SSLEAY) */
139 * Turns a 56 bit key into the 64 bit, odd parity key. Used by GnuTLS and NSS.
141 static void extend_key_56_to_64(const unsigned char *key_56, char *key)
144 key[1] = (unsigned char)(((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1));
145 key[2] = (unsigned char)(((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2));
146 key[3] = (unsigned char)(((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3));
147 key[4] = (unsigned char)(((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4));
148 key[5] = (unsigned char)(((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5));
149 key[6] = (unsigned char)(((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6));
150 key[7] = (unsigned char) ((key_56[6] << 1) & 0xFF);
153 #if defined(USE_GNUTLS_NETTLE)
155 static void setup_des_key(const unsigned char *key_56,
159 extend_key_56_to_64(key_56, key);
160 des_set_key(des, (const uint8_t*)key);
163 #elif defined(USE_GNUTLS)
166 * Turns a 56 bit key into the 64 bit, odd parity key and sets the key.
168 static void setup_des_key(const unsigned char *key_56,
169 gcry_cipher_hd_t *des)
172 extend_key_56_to_64(key_56, key);
173 gcry_cipher_setkey(*des, key, 8);
176 #elif defined(USE_NSS)
179 * Expands a 56 bit key KEY_56 to 64 bit and encrypts 64 bit of data, using
180 * the expanded key. The caller is responsible for giving 64 bit of valid
181 * data is IN and (at least) 64 bit large buffer as OUT.
183 static bool encrypt_des(const unsigned char *in, unsigned char *out,
184 const unsigned char *key_56)
186 const CK_MECHANISM_TYPE mech = CKM_DES_ECB; /* DES cipher in ECB mode */
187 PK11SlotInfo *slot = NULL;
188 char key[8]; /* expanded 64 bit key */
190 PK11SymKey *symkey = NULL;
191 SECItem *param = NULL;
192 PK11Context *ctx = NULL;
193 int out_len; /* not used, required by NSS */
196 /* use internal slot for DES encryption (requires NSS to be initialized) */
197 slot = PK11_GetInternalKeySlot();
201 /* expand the 56 bit key to 64 bit and wrap by NSS */
202 extend_key_56_to_64(key_56, key);
203 key_item.data = (unsigned char *)key;
204 key_item.len = /* hard-wired */ 8;
205 symkey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, CKA_ENCRYPT,
210 /* create DES encryption context */
211 param = PK11_ParamFromIV(mech, /* no IV in ECB mode */ NULL);
214 ctx = PK11_CreateContextBySymKey(mech, CKA_ENCRYPT, symkey, param);
218 /* perform the encryption */
219 if(SECSuccess == PK11_CipherOp(ctx, out, &out_len, /* outbuflen */ 8,
220 (unsigned char *)in, /* inbuflen */ 8)
221 && SECSuccess == PK11_Finalize(ctx))
222 rv = /* all OK */ TRUE;
227 PK11_DestroyContext(ctx, PR_TRUE);
229 PK11_FreeSymKey(symkey);
231 SECITEM_FreeItem(param, PR_TRUE);
236 #elif defined(USE_DARWINSSL)
238 static bool encrypt_des(const unsigned char *in, unsigned char *out,
239 const unsigned char *key_56)
245 extend_key_56_to_64(key_56, key);
246 err = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionECBMode, key,
247 kCCKeySizeDES, NULL, in, 8 /* inbuflen */, out,
248 8 /* outbuflen */, &out_len);
249 return err == kCCSuccess;
252 #endif /* defined(USE_DARWINSSL) */
254 #endif /* defined(USE_SSLEAY) */
257 * takes a 21 byte array and treats it as 3 56-bit DES keys. The
258 * 8 byte plaintext is encrypted with each key and the resulting 24
259 * bytes are stored in the results array.
261 void Curl_ntlm_core_lm_resp(const unsigned char *keys,
262 const unsigned char *plaintext,
263 unsigned char *results)
268 setup_des_key(keys, DESKEY(ks));
269 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
270 DESKEY(ks), DES_ENCRYPT);
272 setup_des_key(keys + 7, DESKEY(ks));
273 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 8),
274 DESKEY(ks), DES_ENCRYPT);
276 setup_des_key(keys + 14, DESKEY(ks));
277 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results + 16),
278 DESKEY(ks), DES_ENCRYPT);
279 #elif defined(USE_GNUTLS_NETTLE)
281 setup_des_key(keys, &des);
282 des_encrypt(&des, 8, results, plaintext);
283 setup_des_key(keys + 7, &des);
284 des_encrypt(&des, 8, results + 8, plaintext);
285 setup_des_key(keys + 14, &des);
286 des_encrypt(&des, 8, results + 16, plaintext);
287 #elif defined(USE_GNUTLS)
288 gcry_cipher_hd_t des;
290 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
291 setup_des_key(keys, &des);
292 gcry_cipher_encrypt(des, results, 8, plaintext, 8);
293 gcry_cipher_close(des);
295 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
296 setup_des_key(keys + 7, &des);
297 gcry_cipher_encrypt(des, results + 8, 8, plaintext, 8);
298 gcry_cipher_close(des);
300 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
301 setup_des_key(keys + 14, &des);
302 gcry_cipher_encrypt(des, results + 16, 8, plaintext, 8);
303 gcry_cipher_close(des);
304 #elif defined(USE_NSS) || defined(USE_DARWINSSL)
305 encrypt_des(plaintext, results, keys);
306 encrypt_des(plaintext, results + 8, keys + 7);
307 encrypt_des(plaintext, results + 16, keys + 14);
312 * Set up lanmanager hashed password
314 void Curl_ntlm_core_mk_lm_hash(struct SessionHandle *data,
315 const char *password,
316 unsigned char *lmbuffer /* 21 bytes */)
319 unsigned char pw[14];
320 static const unsigned char magic[] = {
321 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25 /* i.e. KGS!@#$% */
323 size_t len = CURLMIN(strlen(password), 14);
325 Curl_strntoupper((char *)pw, password, len);
326 memset(&pw[len], 0, 14 - len);
329 * The LanManager hashed password needs to be created using the
330 * password in the network encoding not the host encoding.
332 res = Curl_convert_to_network(data, (char *)pw, 14);
337 /* Create LanManager hashed password. */
342 setup_des_key(pw, DESKEY(ks));
343 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
344 DESKEY(ks), DES_ENCRYPT);
346 setup_des_key(pw + 7, DESKEY(ks));
347 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer + 8),
348 DESKEY(ks), DES_ENCRYPT);
349 #elif defined(USE_GNUTLS_NETTLE)
351 setup_des_key(pw, &des);
352 des_encrypt(&des, 8, lmbuffer, magic);
353 setup_des_key(pw + 7, &des);
354 des_encrypt(&des, 8, lmbuffer + 8, magic);
355 #elif defined(USE_GNUTLS)
356 gcry_cipher_hd_t des;
358 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
359 setup_des_key(pw, &des);
360 gcry_cipher_encrypt(des, lmbuffer, 8, magic, 8);
361 gcry_cipher_close(des);
363 gcry_cipher_open(&des, GCRY_CIPHER_DES, GCRY_CIPHER_MODE_ECB, 0);
364 setup_des_key(pw + 7, &des);
365 gcry_cipher_encrypt(des, lmbuffer + 8, 8, magic, 8);
366 gcry_cipher_close(des);
367 #elif defined(USE_NSS) || defined(USE_DARWINSSL)
368 encrypt_des(magic, lmbuffer, pw);
369 encrypt_des(magic, lmbuffer + 8, pw + 7);
372 memset(lmbuffer + 16, 0, 21 - 16);
377 static void ascii_to_unicode_le(unsigned char *dest, const char *src,
381 for(i = 0; i < srclen; i++) {
382 dest[2 * i] = (unsigned char)src[i];
383 dest[2 * i + 1] = '\0';
387 static void ascii_uppercase_to_unicode_le(unsigned char *dest,
388 const char *src, size_t srclen)
391 for(i = 0; i < srclen; i++) {
392 dest[2 * i] = (unsigned char)(toupper(src[i]));
393 dest[2 * i + 1] = '\0';
397 static void write32_le(const int value, unsigned char *buffer)
399 buffer[0] = (char)(value & 0x000000FF);
400 buffer[1] = (char)((value & 0x0000FF00) >> 8);
401 buffer[2] = (char)((value & 0x00FF0000) >> 16);
402 buffer[3] = (char)((value & 0xFF000000) >> 24);
405 #if defined(HAVE_LONGLONG)
406 static void write64_le(const long long value, unsigned char *buffer)
408 static void write64_le(const __int64 value, unsigned char *buffer)
411 write32_le((int)value, buffer);
412 write32_le((int)(value >> 32), buffer + 4);
416 * Set up nt hashed passwords
418 CURLcode Curl_ntlm_core_mk_nt_hash(struct SessionHandle *data,
419 const char *password,
420 unsigned char *ntbuffer /* 21 bytes */)
422 size_t len = strlen(password);
423 unsigned char *pw = malloc(len * 2);
426 return CURLE_OUT_OF_MEMORY;
428 ascii_to_unicode_le(pw, password, len);
431 * The NT hashed password needs to be created using the password in the
432 * network encoding not the host encoding.
434 result = Curl_convert_to_network(data, (char *)pw, len * 2);
439 /* Create NT hashed password. */
443 MD4_Update(&MD4pw, pw, 2 * len);
444 MD4_Final(ntbuffer, &MD4pw);
445 #elif defined(USE_GNUTLS_NETTLE)
446 struct md4_ctx MD4pw;
448 md4_update(&MD4pw, (unsigned int)(2 * len), pw);
449 md4_digest(&MD4pw, MD4_DIGEST_SIZE, ntbuffer);
450 #elif defined(USE_GNUTLS)
452 gcry_md_open(&MD4pw, GCRY_MD_MD4, 0);
453 gcry_md_write(MD4pw, pw, 2 * len);
454 memcpy (ntbuffer, gcry_md_read (MD4pw, 0), MD4_DIGEST_LENGTH);
455 gcry_md_close(MD4pw);
456 #elif defined(USE_NSS)
457 Curl_md4it(ntbuffer, pw, 2 * len);
458 #elif defined(USE_DARWINSSL)
459 (void)CC_MD4(pw, (CC_LONG)(2 * len), ntbuffer);
462 memset(ntbuffer + 16, 0, 21 - 16);
470 /* This returns the HMAC MD5 digest */
471 CURLcode Curl_hmac_md5(const unsigned char *key, unsigned int keylen,
472 const unsigned char *data, unsigned int datalen,
473 unsigned char *output)
475 HMAC_context *ctxt = Curl_HMAC_init(Curl_HMAC_MD5, key, keylen);
478 return CURLE_OUT_OF_MEMORY;
480 /* Update the digest with the given challenge */
481 Curl_HMAC_update(ctxt, data, datalen);
483 /* Finalise the digest */
484 Curl_HMAC_final(ctxt, output);
489 /* This creates the NTLMv2 hash by using NTLM hash as the key and Unicode
490 * (uppercase UserName + Domain) as the data
492 CURLcode Curl_ntlm_core_mk_ntlmv2_hash(const char *user, size_t userlen,
493 const char *domain, size_t domlen,
494 unsigned char *ntlmhash,
495 unsigned char *ntlmv2hash)
497 /* Unicode representation */
498 size_t identity_len = (userlen + domlen) * 2;
499 unsigned char *identity = malloc(identity_len);
500 CURLcode res = CURLE_OK;
503 return CURLE_OUT_OF_MEMORY;
505 ascii_uppercase_to_unicode_le(identity, user, userlen);
506 ascii_to_unicode_le(identity + (userlen << 1), domain, domlen);
508 res = Curl_hmac_md5(ntlmhash, 16, identity, curlx_uztoui(identity_len),
511 Curl_safefree(identity);
517 * Curl_ntlm_core_mk_ntlmv2_resp()
519 * This creates the NTLMv2 response as set in the ntlm type-3 message.
523 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
524 * challenge_client [in] - The client nonce (8 bytes)
525 * ntlm [in] - The ntlm data struct being used to read TargetInfo
526 and Server challenge received in the type-2 message
527 * ntresp [out] - The address where a pointer to newly allocated
528 * memory holding the NTLMv2 response.
529 * ntresp_len [out] - The length of the output message.
531 * Returns CURLE_OK on success.
533 CURLcode Curl_ntlm_core_mk_ntlmv2_resp(unsigned char *ntlmv2hash,
534 unsigned char *challenge_client,
535 struct ntlmdata *ntlm,
536 unsigned char **ntresp,
537 unsigned int *ntresp_len)
539 /* NTLMv2 response structure :
540 ------------------------------------------------------------------------------
542 ------BLOB--------------------------------------------------------------------
543 16 Signature 0x01010000
544 20 Reserved long (0x00000000)
545 24 Timestamp LE, 64-bit signed value representing the number of
546 tenths of a microsecond since January 1, 1601.
547 32 Client Nonce 8 bytes
549 44 Target Info N bytes (from the type-2 message)
551 ------------------------------------------------------------------------------
554 unsigned int len = 0;
555 unsigned char *ptr = NULL;
556 unsigned char hmac_output[NTLM_HMAC_MD5_LEN];
557 #if defined(HAVE_LONGLONG)
562 CURLcode res = CURLE_OK;
564 /* Calculate the timestamp */
565 #if defined(HAVE_LONGLONG)
566 #if defined(DEBUGBUILD)
567 tw = 11644473600ULL * 10000000ULL;
569 tw = ((long long)time(NULL) + 11644473600ULL) * 10000000ULL;
572 #if defined(DEBUGBUILD)
573 tw = 11644473600ui64 * 10000000ui64;
575 tw = ((__int64)time(NULL) + 11644473600ui64) * 10000000ui64;
578 /* Calculate the response len */
579 len = NTLM_HMAC_MD5_LEN + NTLMv2_BLOB_LEN;
581 /* Allocate the response */
584 return CURLE_OUT_OF_MEMORY;
588 /* Create the BLOB structure */
589 snprintf((char *)ptr + NTLM_HMAC_MD5_LEN, NTLMv2_BLOB_LEN,
590 NTLMv2_BLOB_SIGNATURE
591 "%c%c%c%c", /* Reserved = 0 */
594 write64_le(tw, ptr + 24);
595 memcpy(ptr + 32, challenge_client, 8);
596 memcpy(ptr + 44, ntlm->target_info, ntlm->target_info_len);
598 /* Concatenate the Type 2 challenge with the BLOB and do HMAC MD5 */
599 memcpy(ptr + 8, &ntlm->nonce[0], 8);
600 res = Curl_hmac_md5(ntlmv2hash, NTLM_HMAC_MD5_LEN, ptr + 8,
601 NTLMv2_BLOB_LEN + 8, hmac_output);
607 /* Concatenate the HMAC MD5 output with the BLOB */
608 memcpy(ptr, hmac_output, NTLM_HMAC_MD5_LEN);
610 /* Return the response */
618 * Curl_ntlm_core_mk_lmv2_resp()
620 * This creates the LMv2 response as used in the ntlm type-3 message.
624 * ntlmv2hash [in] - The ntlmv2 hash (16 bytes)
625 * challenge_client [in] - The client nonce (8 bytes)
626 * challenge_client [in] - The server challenge (8 bytes)
627 * lmresp [out] - The LMv2 response (24 bytes)
629 * Returns CURLE_OK on success.
631 CURLcode Curl_ntlm_core_mk_lmv2_resp(unsigned char *ntlmv2hash,
632 unsigned char *challenge_client,
633 unsigned char *challenge_server,
634 unsigned char *lmresp)
636 unsigned char data[16];
637 unsigned char hmac_output[16];
638 CURLcode res = CURLE_OK;
640 memcpy(&data[0], challenge_server, 8);
641 memcpy(&data[8], challenge_client, 8);
643 res = Curl_hmac_md5(ntlmv2hash, 16, &data[0], 16, hmac_output);
647 /* Concatenate the HMAC MD5 output with the client nonce */
648 memcpy(lmresp, hmac_output, 16);
649 memcpy(lmresp+16, challenge_client, 8);
654 #endif /* USE_NTRESPONSES */
656 #endif /* USE_NTLM && !USE_WINDOWS_SSPI */