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