1
/***************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2011, 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
***************************************************************************/
30
* http://davenport.sourceforge.net/ntlm.html
31
* http://www.innovation.ch/java/ntlm.html
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
57
#elif defined(USE_GNUTLS)
61
# define MD5_DIGEST_LENGTH 16
62
# define MD4_DIGEST_LENGTH 16
64
#elif defined(USE_NSS)
70
# include "curl_md4.h"
71
# define MD5_DIGEST_LENGTH MD5_LENGTH
73
#elif defined(USE_WINDOWS_SSPI)
74
# include "curl_sspi.h"
76
# error "Can't compile NTLM support without a crypto library."
80
#include "non-ascii.h"
82
#include "curl_base64.h"
83
#include "curl_ntlm_core.h"
84
#include "curl_gethostname.h"
85
#include "curl_memory.h"
87
#define BUILDING_CURL_NTLM_MSGS_C
88
#include "curl_ntlm_msgs.h"
90
#define _MPRINTF_REPLACE /* use our functions only */
91
#include <curl/mprintf.h>
93
/* The last #include file should be: */
96
/* Hostname buffer size */
97
#define HOSTNAME_MAX 1024
99
/* "NTLMSSP" signature is always in ASCII regardless of the platform */
100
#define NTLMSSP_SIGNATURE "\x4e\x54\x4c\x4d\x53\x53\x50"
102
#define SHORTPAIR(x) ((x) & 0xff), (((x) >> 8) & 0xff)
103
#define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8) & 0xff), \
104
(((x) >> 16) & 0xff), (((x) >> 24) & 0xff)
107
# define DEBUG_OUT(x) x
108
static void ntlm_print_flags(FILE *handle, unsigned long flags)
110
if(flags & NTLMFLAG_NEGOTIATE_UNICODE)
111
fprintf(handle, "NTLMFLAG_NEGOTIATE_UNICODE ");
112
if(flags & NTLMFLAG_NEGOTIATE_OEM)
113
fprintf(handle, "NTLMFLAG_NEGOTIATE_OEM ");
114
if(flags & NTLMFLAG_REQUEST_TARGET)
115
fprintf(handle, "NTLMFLAG_REQUEST_TARGET ");
117
fprintf(handle, "NTLMFLAG_UNKNOWN_3 ");
118
if(flags & NTLMFLAG_NEGOTIATE_SIGN)
119
fprintf(handle, "NTLMFLAG_NEGOTIATE_SIGN ");
120
if(flags & NTLMFLAG_NEGOTIATE_SEAL)
121
fprintf(handle, "NTLMFLAG_NEGOTIATE_SEAL ");
122
if(flags & NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE)
123
fprintf(handle, "NTLMFLAG_NEGOTIATE_DATAGRAM_STYLE ");
124
if(flags & NTLMFLAG_NEGOTIATE_LM_KEY)
125
fprintf(handle, "NTLMFLAG_NEGOTIATE_LM_KEY ");
126
if(flags & NTLMFLAG_NEGOTIATE_NETWARE)
127
fprintf(handle, "NTLMFLAG_NEGOTIATE_NETWARE ");
128
if(flags & NTLMFLAG_NEGOTIATE_NTLM_KEY)
129
fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM_KEY ");
131
fprintf(handle, "NTLMFLAG_UNKNOWN_10 ");
132
if(flags & NTLMFLAG_NEGOTIATE_ANONYMOUS)
133
fprintf(handle, "NTLMFLAG_NEGOTIATE_ANONYMOUS ");
134
if(flags & NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED)
135
fprintf(handle, "NTLMFLAG_NEGOTIATE_DOMAIN_SUPPLIED ");
136
if(flags & NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED)
137
fprintf(handle, "NTLMFLAG_NEGOTIATE_WORKSTATION_SUPPLIED ");
138
if(flags & NTLMFLAG_NEGOTIATE_LOCAL_CALL)
139
fprintf(handle, "NTLMFLAG_NEGOTIATE_LOCAL_CALL ");
140
if(flags & NTLMFLAG_NEGOTIATE_ALWAYS_SIGN)
141
fprintf(handle, "NTLMFLAG_NEGOTIATE_ALWAYS_SIGN ");
142
if(flags & NTLMFLAG_TARGET_TYPE_DOMAIN)
143
fprintf(handle, "NTLMFLAG_TARGET_TYPE_DOMAIN ");
144
if(flags & NTLMFLAG_TARGET_TYPE_SERVER)
145
fprintf(handle, "NTLMFLAG_TARGET_TYPE_SERVER ");
146
if(flags & NTLMFLAG_TARGET_TYPE_SHARE)
147
fprintf(handle, "NTLMFLAG_TARGET_TYPE_SHARE ");
148
if(flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY)
149
fprintf(handle, "NTLMFLAG_NEGOTIATE_NTLM2_KEY ");
150
if(flags & NTLMFLAG_REQUEST_INIT_RESPONSE)
151
fprintf(handle, "NTLMFLAG_REQUEST_INIT_RESPONSE ");
152
if(flags & NTLMFLAG_REQUEST_ACCEPT_RESPONSE)
153
fprintf(handle, "NTLMFLAG_REQUEST_ACCEPT_RESPONSE ");
154
if(flags & NTLMFLAG_REQUEST_NONNT_SESSION_KEY)
155
fprintf(handle, "NTLMFLAG_REQUEST_NONNT_SESSION_KEY ");
156
if(flags & NTLMFLAG_NEGOTIATE_TARGET_INFO)
157
fprintf(handle, "NTLMFLAG_NEGOTIATE_TARGET_INFO ");
159
fprintf(handle, "NTLMFLAG_UNKNOWN_24 ");
161
fprintf(handle, "NTLMFLAG_UNKNOWN_25 ");
163
fprintf(handle, "NTLMFLAG_UNKNOWN_26 ");
165
fprintf(handle, "NTLMFLAG_UNKNOWN_27 ");
167
fprintf(handle, "NTLMFLAG_UNKNOWN_28 ");
168
if(flags & NTLMFLAG_NEGOTIATE_128)
169
fprintf(handle, "NTLMFLAG_NEGOTIATE_128 ");
170
if(flags & NTLMFLAG_NEGOTIATE_KEY_EXCHANGE)
171
fprintf(handle, "NTLMFLAG_NEGOTIATE_KEY_EXCHANGE ");
172
if(flags & NTLMFLAG_NEGOTIATE_56)
173
fprintf(handle, "NTLMFLAG_NEGOTIATE_56 ");
176
static void ntlm_print_hex(FILE *handle, const char *buf, size_t len)
180
fprintf(stderr, "0x");
182
fprintf(stderr, "%02.2x", (unsigned int)*p++);
185
# define DEBUG_OUT(x) Curl_nop_stmt
188
#ifndef USE_WINDOWS_SSPI
190
* This function converts from the little endian format used in the
191
* incoming package to whatever endian format we're using natively.
192
* Argument is a pointer to a 4 byte buffer.
194
static unsigned int readint_le(unsigned char *buf)
196
return ((unsigned int)buf[0]) | ((unsigned int)buf[1] << 8) |
197
((unsigned int)buf[2] << 16) | ((unsigned int)buf[3] << 24);
202
NTLM message structure notes:
204
A 'short' is a 'network short', a little-endian 16-bit unsigned value.
206
A 'long' is a 'network long', a little-endian, 32-bit unsigned value.
208
A 'security buffer' represents a triplet used to point to a buffer,
209
consisting of two shorts and one long:
211
1. A 'short' containing the length of the buffer content in bytes.
212
2. A 'short' containing the allocated space for the buffer in bytes.
213
3. A 'long' containing the offset to the start of the buffer in bytes,
214
from the beginning of the NTLM message.
218
* Curl_ntlm_decode_type2_message()
220
* This is used to decode a ntlm type-2 message received from a: HTTP, SMTP
221
* or POP3 server. The message is first decoded from a base64 string into a
222
* raw ntlm message and checked for validity before the appropriate data for
223
* creating a type-3 message is written to the given ntlm data structure.
227
* data [in] - Pointer to session handle.
228
* header [in] - Pointer to the input buffer.
229
* ntlm [in] - Pointer to ntlm data struct being used and modified.
231
* Returns CURLE_OK on success.
233
CURLcode Curl_ntlm_decode_type2_message(struct SessionHandle *data,
235
struct ntlmdata* ntlm)
237
#ifndef USE_WINDOWS_SSPI
238
static const char type2_marker[] = { 0x02, 0x00, 0x00, 0x00 };
241
/* NTLM type-2 message structure:
243
Index Description Content
244
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
246
8 NTLM Message Type long (0x02000000)
247
12 Target Name security buffer
250
(32) Context 8 bytes (two consecutive longs) (*)
251
(40) Target Information security buffer (*)
252
(48) OS Version Structure 8 bytes (*)
253
32 (48) (56) Start of data block (*)
258
unsigned char *buffer = NULL;
261
#if defined(CURL_DISABLE_VERBOSE_STRINGS) || defined(USE_WINDOWS_SSPI)
265
error = Curl_base64_decode(header, &buffer, &size);
270
infof(data, "NTLM handshake failure (unhandled condition)\n");
271
return CURLE_REMOTE_ACCESS_DENIED;
274
#ifdef USE_WINDOWS_SSPI
275
ntlm->type_2 = malloc(size + 1);
276
if(ntlm->type_2 == NULL) {
278
return CURLE_OUT_OF_MEMORY;
280
ntlm->n_type_2 = (unsigned long)size;
281
memcpy(ntlm->type_2, buffer, size);
286
(memcmp(buffer, NTLMSSP_SIGNATURE, 8) != 0) ||
287
(memcmp(buffer + 8, type2_marker, sizeof(type2_marker)) != 0)) {
288
/* This was not a good enough type-2 message */
290
infof(data, "NTLM handshake failure (bad type-2 message)\n");
291
return CURLE_REMOTE_ACCESS_DENIED;
294
ntlm->flags = readint_le(&buffer[20]);
295
memcpy(ntlm->nonce, &buffer[24], 8);
298
fprintf(stderr, "**** TYPE2 header flags=0x%08.8lx ", ntlm->flags);
299
ntlm_print_flags(stderr, ntlm->flags);
300
fprintf(stderr, "\n nonce=");
301
ntlm_print_hex(stderr, (char *)ntlm->nonce, 8);
302
fprintf(stderr, "\n****\n");
303
fprintf(stderr, "**** Header %s\n ", header);
311
#ifdef USE_WINDOWS_SSPI
312
void Curl_ntlm_sspi_cleanup(struct ntlmdata *ntlm)
318
if(ntlm->has_handles) {
319
s_pSecFn->DeleteSecurityContext(&ntlm->c_handle);
320
s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
321
ntlm->has_handles = 0;
323
if(ntlm->p_identity) {
324
if(ntlm->identity.User) free(ntlm->identity.User);
325
if(ntlm->identity.Password) free(ntlm->identity.Password);
326
if(ntlm->identity.Domain) free(ntlm->identity.Domain);
327
ntlm->p_identity = NULL;
332
#ifndef USE_WINDOWS_SSPI
333
/* copy the source to the destination and fill in zeroes in every
334
other destination byte! */
335
static void unicodecpy(unsigned char *dest,
336
const char *src, size_t length)
339
for(i = 0; i < length; i++) {
340
dest[2 * i] = (unsigned char)src[i];
341
dest[2 * i + 1] = '\0';
347
* Curl_ntlm_create_type1_message()
349
* This is used to generate an already encoded NTLM type-1 message ready
350
* for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
351
* using the appropriate compile time crypo API.
355
* userp [in] - The user name in the format User or Domain\User.
356
* passdwp [in] - The user's password.
357
* ntlm [in/out] - The ntlm data struct being used and modified.
358
* outptr [in/out] - The adress where a pointer to newly allocated memory
359
* holding the result will be stored upon completion.
361
* Returns CURLE_OK on success.
363
CURLcode Curl_ntlm_create_type1_message(const char *userp,
365
struct ntlmdata *ntlm,
368
/* NTLM type-1 message structure:
370
Index Description Content
371
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
373
8 NTLM Message Type long (0x01000000)
375
(16) Supplied Domain security buffer (*)
376
(24) Supplied Workstation security buffer (*)
377
(32) OS Version Structure 8 bytes (*)
378
(32) (40) Start of data block (*)
382
unsigned char ntlmbuf[NTLM_BUFSIZE];
383
size_t base64_sz = 0;
386
#ifdef USE_WINDOWS_SSPI
390
SECURITY_STATUS status;
392
const char *dest = "";
394
const char *domain = "";
397
size_t passwdlen = 0;
398
TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
400
Curl_ntlm_sspi_cleanup(ntlm);
402
user = strchr(userp, '\\');
404
user = strchr(userp, '/');
408
domlen = user - userp;
418
userlen = strlen(user);
421
passwdlen = strlen(passwdp);
424
/* note: initialize all of this before doing the mallocs so that
425
* it can be cleaned up later without leaking memory.
427
ntlm->p_identity = &ntlm->identity;
428
memset(ntlm->p_identity, 0, sizeof(*ntlm->p_identity));
429
if((ntlm->identity.User = (unsigned char *)strdup(user)) == NULL)
430
return CURLE_OUT_OF_MEMORY;
432
ntlm->identity.UserLength = (unsigned long)userlen;
433
if((ntlm->identity.Password = (unsigned char *)strdup(passwdp)) == NULL)
434
return CURLE_OUT_OF_MEMORY;
436
ntlm->identity.PasswordLength = (unsigned long)strlen(passwdp);
437
if((ntlm->identity.Domain = malloc(domlen + 1)) == NULL)
438
return CURLE_OUT_OF_MEMORY;
440
strncpy((char *)ntlm->identity.Domain, domain, domlen);
441
ntlm->identity.Domain[domlen] = '\0';
442
ntlm->identity.DomainLength = (unsigned long)domlen;
443
ntlm->identity.Flags = SEC_WINNT_AUTH_IDENTITY_ANSI;
446
ntlm->p_identity = NULL;
448
status = s_pSecFn->AcquireCredentialsHandleA(NULL, (void *)"NTLM",
449
SECPKG_CRED_OUTBOUND, NULL,
450
ntlm->p_identity, NULL, NULL,
451
&ntlm->handle, &tsDummy);
452
if(status != SEC_E_OK)
453
return CURLE_OUT_OF_MEMORY;
455
desc.ulVersion = SECBUFFER_VERSION;
457
desc.pBuffers = &buf;
458
buf.cbBuffer = NTLM_BUFSIZE;
459
buf.BufferType = SECBUFFER_TOKEN;
460
buf.pvBuffer = ntlmbuf;
462
status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle, NULL,
464
ISC_REQ_CONFIDENTIALITY |
465
ISC_REQ_REPLAY_DETECT |
467
0, SECURITY_NETWORK_DREP,
469
&ntlm->c_handle, &desc,
472
if(status == SEC_I_COMPLETE_AND_CONTINUE ||
473
status == SEC_I_CONTINUE_NEEDED)
474
s_pSecFn->CompleteAuthToken(&ntlm->c_handle, &desc);
475
else if(status != SEC_E_OK) {
476
s_pSecFn->FreeCredentialsHandle(&ntlm->handle);
477
return CURLE_RECV_ERROR;
480
ntlm->has_handles = 1;
485
const char *host = ""; /* empty */
486
const char *domain = ""; /* empty */
490
size_t domoff = hostoff + hostlen; /* This is 0: remember that host and
497
#define NTLM2FLAG NTLMFLAG_NEGOTIATE_NTLM2_KEY
501
snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
502
NTLMSSP_SIGNATURE "%c"
503
"\x01%c%c%c" /* 32-bit type = 1 */
504
"%c%c%c%c" /* 32-bit NTLM flag field */
505
"%c%c" /* domain length */
506
"%c%c" /* domain allocated space */
507
"%c%c" /* domain name offset */
508
"%c%c" /* 2 zeroes */
509
"%c%c" /* host length */
510
"%c%c" /* host allocated space */
511
"%c%c" /* host name offset */
512
"%c%c" /* 2 zeroes */
514
"%s", /* domain string */
515
0, /* trailing zero */
516
0, 0, 0, /* part of type-1 long */
518
LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
519
NTLMFLAG_REQUEST_TARGET |
520
NTLMFLAG_NEGOTIATE_NTLM_KEY |
522
NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
531
host, /* this is empty */
532
domain /* this is empty */);
534
/* Initial packet length */
535
size = 32 + hostlen + domlen;
540
fprintf(stderr, "* TYPE1 header flags=0x%02.2x%02.2x%02.2x%02.2x "
542
LONGQUARTET(NTLMFLAG_NEGOTIATE_OEM |
543
NTLMFLAG_REQUEST_TARGET |
544
NTLMFLAG_NEGOTIATE_NTLM_KEY |
546
NTLMFLAG_NEGOTIATE_ALWAYS_SIGN),
547
NTLMFLAG_NEGOTIATE_OEM |
548
NTLMFLAG_REQUEST_TARGET |
549
NTLMFLAG_NEGOTIATE_NTLM_KEY |
551
NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
552
ntlm_print_flags(stderr,
553
NTLMFLAG_NEGOTIATE_OEM |
554
NTLMFLAG_REQUEST_TARGET |
555
NTLMFLAG_NEGOTIATE_NTLM_KEY |
557
NTLMFLAG_NEGOTIATE_ALWAYS_SIGN);
558
fprintf(stderr, "\n****\n");
561
/* Return with binary blob encoded into base64 */
562
return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, &base64_sz);
566
* Curl_ntlm_create_type3_message()
568
* This is used to generate an already encoded NTLM type-3 message ready
569
* for sending to the recipient, be it a: HTTP, SMTP or POP3 server,
570
* using the appropriate compile time crypo API.
574
* data [in] - The session handle.
575
* userp [in] - The user name in the format User or Domain\User.
576
* passdwp [in] - The user's password.
577
* ntlm [in/out] - The ntlm data struct being used and modified.
578
* outptr [in/out] - The adress where a pointer to newly allocated memory
579
* holding the result will be stored upon completion.
581
* Returns CURLE_OK on success.
583
CURLcode Curl_ntlm_create_type3_message(struct SessionHandle *data,
586
struct ntlmdata *ntlm,
589
/* NTLM type-3 message structure:
591
Index Description Content
592
0 NTLMSSP Signature Null-terminated ASCII "NTLMSSP"
594
8 NTLM Message Type long (0x03000000)
595
12 LM/LMv2 Response security buffer
596
20 NTLM/NTLMv2 Response security buffer
597
28 Target Name security buffer
598
36 User Name security buffer
599
44 Workstation Name security buffer
600
(52) Session Key security buffer (*)
602
(64) OS Version Structure 8 bytes (*)
603
52 (64) (72) Start of data block
607
unsigned char ntlmbuf[NTLM_BUFSIZE];
608
size_t base64_sz = 0;
611
#ifdef USE_WINDOWS_SSPI
612
const char *dest = "";
615
SecBufferDesc type_2_desc;
616
SecBufferDesc type_3_desc;
617
SECURITY_STATUS status;
619
TimeStamp tsDummy; /* For Windows 9x compatibility of SSPI calls */
625
type_2_desc.ulVersion = type_3_desc.ulVersion = SECBUFFER_VERSION;
626
type_2_desc.cBuffers = type_3_desc.cBuffers = 1;
627
type_2_desc.pBuffers = &type_2;
628
type_3_desc.pBuffers = &type_3;
630
type_2.BufferType = SECBUFFER_TOKEN;
631
type_2.pvBuffer = ntlm->type_2;
632
type_2.cbBuffer = ntlm->n_type_2;
633
type_3.BufferType = SECBUFFER_TOKEN;
634
type_3.pvBuffer = ntlmbuf;
635
type_3.cbBuffer = NTLM_BUFSIZE;
637
status = s_pSecFn->InitializeSecurityContextA(&ntlm->handle,
640
ISC_REQ_CONFIDENTIALITY |
641
ISC_REQ_REPLAY_DETECT |
643
0, SECURITY_NETWORK_DREP,
648
if(status != SEC_E_OK)
649
return CURLE_RECV_ERROR;
651
size = type_3.cbBuffer;
653
Curl_ntlm_sspi_cleanup(ntlm);
657
unsigned char lmresp[24]; /* fixed-size */
660
unsigned char ntresp[24]; /* fixed-size */
662
bool unicode = (ntlm->flags & NTLMFLAG_NEGOTIATE_UNICODE) ? TRUE : FALSE;
663
char host[HOSTNAME_MAX + 1] = "";
665
const char *domain = "";
674
user = strchr(userp, '\\');
676
user = strchr(userp, '/');
680
domlen = (user - domain);
687
userlen = strlen(user);
689
if(Curl_gethostname(host, HOSTNAME_MAX)) {
690
infof(data, "gethostname() failed, continuing without!");
694
/* If the workstation if configured with a full DNS name (i.e.
695
* workstation.somewhere.net) gethostname() returns the fully qualified
696
* name, which NTLM doesn't like.
698
char *dot = strchr(host, '.');
701
hostlen = strlen(host);
706
userlen = userlen * 2;
707
hostlen = hostlen * 2;
711
/* We don't support NTLM2 if we don't have USE_NTRESPONSES */
712
if(ntlm->flags & NTLMFLAG_NEGOTIATE_NTLM2_KEY) {
713
unsigned char ntbuffer[0x18];
714
unsigned char tmp[0x18];
715
unsigned char md5sum[MD5_DIGEST_LENGTH];
716
unsigned char entropy[8];
718
/* Need to create 8 bytes random data */
721
Curl_ossl_seed(data); /* Initiate the seed if not already done */
722
RAND_bytes(entropy, 8);
723
#elif defined(USE_GNUTLS)
725
Curl_gtls_seed(data); /* Initiate the seed if not already done */
726
gcry_randomize(entropy, 8, GCRY_STRONG_RANDOM);
727
#elif defined(USE_NSS)
730
Curl_nss_seed(data); /* Initiate the seed if not already done */
731
PK11_GenerateRandom(entropy, 8);
734
/* 8 bytes random data as challenge in lmresp */
735
memcpy(lmresp, entropy, 8);
738
memset(lmresp + 8, 0, 0x10);
740
/* Fill tmp with challenge(nonce?) + entropy */
741
memcpy(tmp, &ntlm->nonce[0], 8);
742
memcpy(tmp + 8, entropy, 8);
746
MD5_Update(&MD5pw, tmp, 16);
747
MD5_Final(md5sum, &MD5pw);
748
#elif defined(USE_GNUTLS)
749
gcry_md_open(&MD5pw, GCRY_MD_MD5, 0);
750
gcry_md_write(MD5pw, tmp, MD5_DIGEST_LENGTH);
751
memcpy(md5sum, gcry_md_read (MD5pw, 0), MD5_DIGEST_LENGTH);
752
gcry_md_close(MD5pw);
753
#elif defined(USE_NSS)
754
MD5pw = PK11_CreateDigestContext(SEC_OID_MD5);
755
PK11_DigestOp(MD5pw, tmp, 16);
756
PK11_DigestFinal(MD5pw, md5sum, &outlen, MD5_DIGEST_LENGTH);
757
PK11_DestroyContext(MD5pw, PR_TRUE);
760
/* We shall only use the first 8 bytes of md5sum, but the des
761
code in Curl_ntlm_core_lm_resp only encrypt the first 8 bytes */
762
if(CURLE_OUT_OF_MEMORY ==
763
Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
764
return CURLE_OUT_OF_MEMORY;
765
Curl_ntlm_core_lm_resp(ntbuffer, md5sum, ntresp);
767
/* End of NTLM2 Session code */
774
unsigned char ntbuffer[0x18];
776
unsigned char lmbuffer[0x18];
779
if(CURLE_OUT_OF_MEMORY ==
780
Curl_ntlm_core_mk_nt_hash(data, passwdp, ntbuffer))
781
return CURLE_OUT_OF_MEMORY;
782
Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], ntresp);
785
Curl_ntlm_core_mk_lm_hash(data, passwdp, lmbuffer);
786
Curl_ntlm_core_lm_resp(lmbuffer, &ntlm->nonce[0], lmresp);
787
/* A safer but less compatible alternative is:
788
* Curl_ntlm_core_lm_resp(ntbuffer, &ntlm->nonce[0], lmresp);
789
* See http://davenport.sourceforge.net/ntlm.html#ntlmVersion2 */
792
lmrespoff = 64; /* size of the message header */
794
ntrespoff = lmrespoff + 0x18;
795
domoff = ntrespoff + 0x18;
797
domoff = lmrespoff + 0x18;
799
useroff = domoff + domlen;
800
hostoff = useroff + userlen;
802
/* Create the big type-3 message binary blob */
803
size = snprintf((char *)ntlmbuf, NTLM_BUFSIZE,
804
NTLMSSP_SIGNATURE "%c"
805
"\x03%c%c%c" /* 32-bit type = 3 */
807
"%c%c" /* LanManager length */
808
"%c%c" /* LanManager allocated space */
809
"%c%c" /* LanManager offset */
810
"%c%c" /* 2 zeroes */
812
"%c%c" /* NT-response length */
813
"%c%c" /* NT-response allocated space */
814
"%c%c" /* NT-response offset */
815
"%c%c" /* 2 zeroes */
817
"%c%c" /* domain length */
818
"%c%c" /* domain allocated space */
819
"%c%c" /* domain name offset */
820
"%c%c" /* 2 zeroes */
822
"%c%c" /* user length */
823
"%c%c" /* user allocated space */
824
"%c%c" /* user offset */
825
"%c%c" /* 2 zeroes */
827
"%c%c" /* host length */
828
"%c%c" /* host allocated space */
829
"%c%c" /* host offset */
830
"%c%c" /* 2 zeroes */
832
"%c%c" /* session key length (unknown purpose) */
833
"%c%c" /* session key allocated space (unknown purpose) */
834
"%c%c" /* session key offset (unknown purpose) */
835
"%c%c" /* 2 zeroes */
837
"%c%c%c%c", /* flags */
842
/* LanManager response */
845
0, /* zero termination */
846
0, 0, 0, /* type-3 long, the 24 upper bits */
848
SHORTPAIR(0x18), /* LanManager response length, twice */
850
SHORTPAIR(lmrespoff),
854
SHORTPAIR(0x18), /* NT-response length, twice */
856
SHORTPAIR(ntrespoff),
884
LONGQUARTET(ntlm->flags));
886
DEBUGASSERT(size == 64);
887
DEBUGASSERT(size == (size_t)lmrespoff);
889
/* We append the binary hashes */
890
if(size < (NTLM_BUFSIZE - 0x18)) {
891
memcpy(&ntlmbuf[size], lmresp, 0x18);
896
fprintf(stderr, "**** TYPE3 header lmresp=");
897
ntlm_print_hex(stderr, (char *)&ntlmbuf[lmrespoff], 0x18);
901
if(size < (NTLM_BUFSIZE - 0x18)) {
902
DEBUGASSERT(size == (size_t)ntrespoff);
903
memcpy(&ntlmbuf[size], ntresp, 0x18);
908
fprintf(stderr, "\n ntresp=");
909
ntlm_print_hex(stderr, (char *)&ntlmbuf[ntrespoff], 0x18);
915
fprintf(stderr, "\n flags=0x%02.2x%02.2x%02.2x%02.2x 0x%08.8x ",
916
LONGQUARTET(ntlm->flags), ntlm->flags);
917
ntlm_print_flags(stderr, ntlm->flags);
918
fprintf(stderr, "\n****\n");
921
/* Make sure that the domain, user and host strings fit in the
922
buffer before we copy them there. */
923
if(size + userlen + domlen + hostlen >= NTLM_BUFSIZE) {
924
failf(data, "user + domain + host name too big");
925
return CURLE_OUT_OF_MEMORY;
928
DEBUGASSERT(size == domoff);
930
unicodecpy(&ntlmbuf[size], domain, domlen / 2);
932
memcpy(&ntlmbuf[size], domain, domlen);
936
DEBUGASSERT(size == useroff);
938
unicodecpy(&ntlmbuf[size], user, userlen / 2);
940
memcpy(&ntlmbuf[size], user, userlen);
944
DEBUGASSERT(size == hostoff);
946
unicodecpy(&ntlmbuf[size], host, hostlen / 2);
948
memcpy(&ntlmbuf[size], host, hostlen);
952
/* Convert domain, user, and host to ASCII but leave the rest as-is */
953
res = Curl_convert_to_network(data, (char *)&ntlmbuf[domoff],
956
return CURLE_CONV_FAILED;
960
/* Return with binary blob encoded into base64 */
961
return Curl_base64_encode(NULL, (char *)ntlmbuf, size, outptr, &base64_sz);
964
#endif /* USE_NTLM */