32 #ifndef CURL_DISABLE_HTTP
51 #define _MPRINTF_REPLACE
54 #include <openssl/des.h>
55 #include <openssl/md4.h>
56 #include <openssl/ssl.h>
58 #if OPENSSL_VERSION_NUMBER < 0x00907001L
59 #define DES_key_schedule des_key_schedule
60 #define DES_cblock des_cblock
61 #define DES_set_odd_parity des_set_odd_parity
62 #define DES_set_key des_set_key
63 #define DES_ecb_encrypt des_ecb_encrypt
67 #define DESKEYARG(x) x
70 #define DESKEYARG(x) *x
80 #undef USE_NTRESPONSES
104 while(*header && isspace((
int)*header))
108 unsigned char buffer[256];
109 header += strlen(
"NTLM");
111 while(*header && isspace((
int)*header))
135 memcpy(ntlm->
nonce, &buffer[24], 8);
154 static void setup_des_key(
unsigned char *key_56,
155 DES_key_schedule DESKEYARG(ks))
160 key[1] = ((key_56[0] << 7) & 0xFF) | (key_56[1] >> 1);
161 key[2] = ((key_56[1] << 6) & 0xFF) | (key_56[2] >> 2);
162 key[3] = ((key_56[2] << 5) & 0xFF) | (key_56[3] >> 3);
163 key[4] = ((key_56[3] << 4) & 0xFF) | (key_56[4] >> 4);
164 key[5] = ((key_56[4] << 3) & 0xFF) | (key_56[5] >> 5);
165 key[6] = ((key_56[5] << 2) & 0xFF) | (key_56[6] >> 6);
166 key[7] = (key_56[6] << 1) & 0xFF;
168 DES_set_odd_parity(&key);
169 DES_set_key(&key, ks);
177 static void calc_resp(
unsigned char *
keys,
178 unsigned char *plaintext,
179 unsigned char *results)
183 setup_des_key(keys, DESKEY(ks));
184 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) results,
185 DESKEY(ks), DES_ENCRYPT);
187 setup_des_key(keys+7, DESKEY(ks));
188 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+8),
189 DESKEY(ks), DES_ENCRYPT);
191 setup_des_key(keys+14, DESKEY(ks));
192 DES_ecb_encrypt((DES_cblock*) plaintext, (DES_cblock*) (results+16),
193 DESKEY(ks), DES_ENCRYPT);
200 unsigned char *
nonce,
201 unsigned char *lmresp
202 #ifdef USE_NTRESPONSES
203 ,
unsigned char *ntresp
207 unsigned char lmbuffer[21];
208 #ifdef USE_NTRESPONSES
209 unsigned char ntbuffer[21];
212 static const unsigned char magic[] = {
213 0x4B, 0x47, 0x53, 0x21, 0x40, 0x23, 0x24, 0x25
216 size_t len = strlen(password);
219 pw = malloc(len<7?14:len*2);
226 for (i=0; i<
len; i++)
227 pw[i] = toupper(password[i]);
236 setup_des_key(pw, DESKEY(ks));
237 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)lmbuffer,
238 DESKEY(ks), DES_ENCRYPT);
240 setup_des_key(pw+7, DESKEY(ks));
241 DES_ecb_encrypt((DES_cblock *)magic, (DES_cblock *)(lmbuffer+8),
242 DESKEY(ks), DES_ENCRYPT);
244 memset(lmbuffer+16, 0, 5);
247 calc_resp(lmbuffer, nonce, lmresp);
249 #ifdef USE_NTRESPONSES
254 len = strlen(password);
256 for (i=0; i<
len; i++) {
257 pw[2*
i] = password[
i];
265 memset(ntbuffer+16, 0, 8);
268 calc_resp(ntbuffer, nonce, ntresp);
274 #define SHORTPAIR(x) ((x) & 0xff), ((x) >> 8)
275 #define LONGQUARTET(x) ((x) & 0xff), (((x) >> 8)&0xff), \
276 (((x) >>16)&0xff), ((x)>>24)
283 const char *domain=
"";
285 int domlen=(
int)strlen(domain);
286 int hostlen = (
int)strlen(host);
291 unsigned char ntlmbuf[256];
325 switch(ntlm->
state) {
329 domoff = hostoff + hostlen;
344 snprintf((
char *)ntlmbuf,
sizeof(ntlmbuf),
"NTLMSSP%c"
376 size = 32 + hostlen + domlen;
383 *allocuserpwd = aprintf(
"%sAuthorization: NTLM %s\r\n",
415 unsigned char lmresp[0x18];
416 #ifdef USE_NTRESPONSES
417 unsigned char ntresp[0x18];
422 user = strchr(userp,
'\\');
424 user = strchr(userp,
'/');
428 domlen = user - domain;
433 userlen = strlen(user);
435 mkhash(passwdp, &ntlm->
nonce[0], lmresp
436 #ifdef USE_NTRESPONSES
442 useroff = domoff + domlen;
443 hostoff = useroff + userlen;
444 lmrespoff = hostoff + hostlen;
445 ntrespoff = lmrespoff + 0x18;
448 size =
snprintf((
char *)ntlmbuf,
sizeof(ntlmbuf),
493 SHORTPAIR(lmrespoff),
496 #ifdef USE_NTRESPONSES
503 SHORTPAIR(ntrespoff),
519 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
527 ntlmbuf[62]=ntlmbuf[63]=0;
529 memcpy(&ntlmbuf[size], domain, domlen);
532 memcpy(&ntlmbuf[size], user, userlen);
536 if(size < ((
int)
sizeof(ntlmbuf) - 0x18)) {
537 memcpy(&ntlmbuf[size], lmresp, 0x18);
541 #ifdef USE_NTRESPONSES
542 if(size < ((
int)
sizeof(ntlmbuf) - 0x18)) {
543 memcpy(&ntlmbuf[size], ntresp, 0x18);
548 ntlmbuf[56] = size & 0xff;
549 ntlmbuf[57] = size >> 8;
556 *allocuserpwd = aprintf(
"%sAuthorization: NTLM %s\r\n",
CURLntlm Curl_input_ntlm(struct connectdata *conn, bool proxy, char *header)
void MD4_Update(MD4_CTX *context, const unsigned char *input, unsigned int inputLen)
size_t Curl_base64_encode(const char *inp, size_t insize, char **outptr)
size_t Curl_base64_decode(const char *src, char *dest)
void MD4_Final(MD4_CTX *context, unsigned char digest[16])
void Curl_http_auth_stage(struct SessionHandle *data, int stage)
struct ntlmdata proxyntlm
#define NTLMFLAG_NEGOTIATE_OEM
struct SessionHandle * data
#define checkprefix(a, b)
CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy, bool *ready)
void Curl_safefree(void *ptr)
#define NTLMFLAG_NEGOTIATE_NTLM_KEY
struct connectdata::dynamically_allocated_data allocptr
void MD4_Init(MD4_CTX *context)
idCVar password("password","", CVAR_GAME|CVAR_NOCHEAT,"client password used when connecting")