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