Skip to contentSkip to conversation

NTLMv2 patch corrected

Openvpn-devel Mirek Zajic 3 participants 13 messages Jan 15, 2008 – May 9, 2008

lore
Share & tools
lore
#0 lore raw

I'm sending the patch again. Now it should be correctly diffed against version 2.1.

Configuration is done with standard http-proxy command in config file, with 'ntlm2' setting for auth method, for. ex.:
http-proxy 192.168.0.1 8080 stdin ntlm2

Username to proxy can be passed among with auth. domain name, for ex. DOMAIN\username

Please give me some feedback about testing or about any problems with ntlm.

Miroslav Zajic
Nextsoft s.r.o.

ntlm.c 2008-01-15 11:25:00.077161600 +0100 renamed from ntlm.c 2008-01-13 21:25:52.915264000 +0100 +2 -0
diff -Naur openvpn-2.1/ntlm.c openvpn-NTLMv2-2.1/ntlm.c
@@ -3,6 +3,8 @@
 *
 *  Copyright (C) 2004 William Preston
 *
+ *  *NTLMv2 support and domain name parsing by Miroslav Zajic, Nextsoft s.r.o.*
+ *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
@@ -41,146 +43,332 @@
 

#include "memdbg.h"

+
+
static void
create_des_keys(const unsigned char *hash, unsigned char *key) {

-  key[0] = hash[0];
-  key[1] = ((hash[0]&1)<<7)|(hash[1]>>1);
-  key[2] = ((hash[1]&3)<<6)|(hash[2]>>2);
-  key[3] = ((hash[2]&7)<<5)|(hash[3]>>3);
-  key[4] = ((hash[3]&15)<<4)|(hash[4]>>4);
-  key[5] = ((hash[4]&31)<<3)|(hash[5]>>5);
-  key[6] = ((hash[5]&63)<<2)|(hash[6]>>6);
-  key[7] = ((hash[6]&127)<<1);
-  des_set_odd_parity((des_cblock *)key);
+    key[0] = hash[0];
+    key[1] = ((hash[0]&1)<<7)|(hash[1]>>1);
+    key[2] = ((hash[1]&3)<<6)|(hash[2]>>2);
+    key[3] = ((hash[2]&7)<<5)|(hash[3]>>3);
+    key[4] = ((hash[3]&15)<<4)|(hash[4]>>4);
+    key[5] = ((hash[4]&31)<<3)|(hash[5]>>5);
+    key[6] = ((hash[5]&63)<<2)|(hash[6]>>6);
+    key[7] = ((hash[6]&127)<<1);
+    des_set_odd_parity((des_cblock *)key);

}

+
static void
gen_md4_hash (const char* data, int data_len, char *result) {
- /* result is 16 byte md4 hash */
+ /* result is 16 byte md4 hash */
+
+ MD4_CTX c;
+ char md[16];
+

+    MD4_Init (&c);
+    MD4_Update (&c, data, data_len);
+    MD4_Final ((unsigned char *)md, &c);

+
+ memcpy (result, md, 16);
+}
+
+
+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int key_len,char *result) +{
+ unsigned int len;
+

+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

+}
+
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+ /* Copies 8 bytes long timestamp into "timestamp" buffer.
+ * Timestamp is Little-endian, 64-bit signed value representing the number of tenths of a microsecond since January 1, 1601.
+ */
+

+    unsigned char bufA[8]; /* Buffer for 64-bit computing */
+    unsigned char bufB[8]; /* Second buffer  */
+    int a, b, c, tmp, tmp2, carry;
+
+    /* set 64-bit buffer A with current time (seconds since 00:00:00 1.1.1970) */
+    *(unsigned int *)bufA =     (unsigned int)time(NULL);
+    *(unsigned int *)&bufA[4] = (unsigned int)0;
+
+    /* 64-bit adition of 0x02B6109100 (seconds between 00:00:00 1.1.1601 and 00:00:00 1.1.1970) */
+    *(unsigned int *)bufB =     (unsigned int)0xB6109100;
+    *(unsigned int *)&bufB[4] = (unsigned int)0x00000002;
+
+    carry=0;
+    for (a=0; a<8; a++){ /* lame adition */
+        tmp = (unsigned int)bufA[a] + (unsigned int)bufB[a] + carry;
+        bufA[a] = tmp & 0xFF;
+        carry = (tmp & 0xFF00) >> 8;
+    }
+   +    /* 64-bit multiply by 10000000 = 0x989680 (converting seconds to tenths of microseconds) */
+    *(unsigned int *)bufB =           (unsigned int)0x00989680;
+    *(unsigned int *)&bufB[4] =       (unsigned int)0x00000000;
+    *(unsigned int *)timestamp =      (unsigned int)0x00000000;
+    *(unsigned int *)&timestamp[4] =  (unsigned int)0x00000000;
+
+    for (a=0; a<8; a++){ /* lame multiply */
+        for (b=0; b<8; b++){
+            tmp = (unsigned int)bufA[a] * (unsigned int)bufB[b];
+            carry=0;
+            for (c=b; (c<8-a) && (c < b + 3); c++){
+                tmp2 = ((tmp & (0xFF << ((c-b) * 8))) >> ((c-b) * 8)) + (unsigned int)timestamp[a+c] + carry;
+                timestamp[a+c] = tmp2 & 0xFF;
+                carry = (tmp2 & 0xFF00) >> 8;

+ }
+ }
+ }
+}
+

- MD4_CTX c;
- char md[16];
+static void
+gen_nonce (unsigned char *nonce)
+{
+ /* Generates 8 random bytes to be used as client nonce */
+ int i;
+
+ for(i=0;i<8;i++);
+ nonce[i] = (unsigned char)random();
+}

-  MD4_Init (&c);
-  MD4_Update (&c, data, data_len);
-  MD4_Final ((unsigned char *)md, &c);

- memcpy (result, md, 16);
+unsigned char *my_strupr(unsigned char *str)
+{
+ /* converts string to uppercase in place */
+

+    unsigned char *tmp;
+    tmp = str;
+    do
+    {
+        if (*str >= 'a' && *str <= 'z')
+            *str -= 32;
+    }
+    while (*(++str));
+    return tmp;

}

+
static int
unicodize (char *dst, const char *src)
{

-  /* not really unicode... */
-  int i = 0;
-  do

- {
- dst[i++] = *src;
- dst[i++] = 0;
- }
- while (*src++);

+    /* not really unicode... */
+    int i = 0;
+    do

+ {
+ dst[i++] = *src;
+ dst[i++] = 0;
+ }
+ while (*src++);

- return i;
+ return i;
}

+
+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned char *msg_buf, int *msg_bufpos) +{

+    /* Adds security buffer data to a message and sets security buffer's offset and length */
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = *msg_bufpos;
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;

+}
+
+
+
+
+
const char *
ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc) {
- struct buffer out = alloc_buf_gc (96, gc);
- /* try a minimal NTLM handshake

-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   * This message contains only the NTLMSSP signature,
-   * the NTLM message type,
-   * and the minimal set of flags (Negotiate NTLM and Negotiate OEM).
-   *
-   */

- buf_printf (&out, "%s", "TlRMTVNTUAABAAAAAgIAAA==");
- return (BSTR (&out));
+ struct buffer out = alloc_buf_gc (96, gc);
+ /* try a minimal NTLM handshake

+     *
+     * http://davenport.sourceforge.net/ntlm.html
+     *
+     * This message contains only the NTLMSSP signature,
+     * the NTLM message type,
+     * and the minimal set of flags (Negotiate NTLM and Negotiate OEM).
+     *
+     */

+ buf_printf (&out, "%s", "TlRMTVNTUAABAAAAAgIAAA==");
+ return (BSTR (&out));
}

+
const char *
ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc) {

-  char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */
-  char buf2[128]; /* decoded reply from proxy */
-  char phase3[146];
-
-  char md4_hash[21];
-  char challenge[8], response[24];
-  int i, ret_val, buflen;
-  des_cblock key1, key2, key3;
-  des_key_schedule sched1, sched2, sched3;
-
-  /* try a minimal NTLM handshake
-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   */

- ASSERT (strlen (p->up.username) > 0);
- ASSERT (strlen (p->up.password) > 0);
-
- /* fill 1st 16 bytes with md4 hash, disregard terminating null */
- gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash); -
- /* pad to 21 bytes */
- memset (md4_hash + 16, 0, 5);
-
- ret_val = base64_decode( phase_2, (void *)buf2);
- /* we can be sure that phase_2 is less than 128
- * therefore buf2 needs to be (3/4 * 128) */
-

-  /* extract the challenge from bytes 24-31 */
-  for (i=0; i<8; i++)
-  {
-    challenge[i] = buf2[i+24];
-  }
-
-  create_des_keys ((unsigned char *)md4_hash, key1);
-  des_set_key_unchecked ((des_cblock *)key1, sched1);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)response, sched1, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
-  des_set_key_unchecked ((des_cblock *)key2, sched2);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[8]), sched2, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
-  des_set_key_unchecked ((des_cblock *)key3, sched3);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[16]), sched3, DES_ENCRYPT);

-
- /* clear reply */
- memset (phase3, 0, sizeof (phase3));
-
- strcpy (phase3, "NTLMSSP\0");
- phase3[8] = 3; /* type 3 */
-
- buflen = 0x58 + strlen (p->up.username);
- if (buflen > (int) sizeof (phase3))
- buflen = sizeof (phase3);
-

-  phase3[0x10] = buflen; /* lm not used */
-  phase3[0x20] = buflen; /* default domain (i.e. proxy's domain) */
-  phase3[0x30] = buflen; /* no workstation name supplied */
-  phase3[0x38] = buflen; /* no session key */
-
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);

-
- phase3[0x3c] = 0x02; /* negotiate oem */
- phase3[0x3d] = 0x02; /* negotiate ntlm */
+ /* NTLM handshake

+     *
+     * http://davenport.sourceforge.net/ntlm.html
+     *
+     */

+ + char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */
+ char buf2[128]; /* decoded reply from proxy */
+ unsigned char phase3[464];
+

+    char md4_hash[21];
+    char challenge[8], ntlm_response[24];
+    int i, ret_val;
+    des_cblock key1, key2, key3;
+    des_key_schedule sched1, sched2, sched3;
+
+    char ntlmv2_response[144];
+    char userdomain_u[256]; /* for uppercase unicode username and domain */
+    char userdomain[128];   /* the same as previous but ascii */
+    char ntlmv2_hash[16];
+    char ntlmv2_hmacmd5[16];
+    char *ntlmv2_blob = ntlmv2_response + 16; /* inside ntlmv2_response, length: 128 */
+    int ntlmv2_blob_size=0;
+    int phase3_bufpos = 0x40; /* offset to next security buffer data to be added */
+    int len;
+
+    char domain[128];
+    char username[128];
+    char *separator;

+
+ bool ntlmv2_enabled = (p->auth_method == HTTP_AUTH_NTLM2);
+
+
+ ASSERT (strlen (p->up.username) > 0);
+ ASSERT (strlen (p->up.password) > 0);
+ + /* username parsing */
+ separator = strchr(p->up.username, '\\');
+ if (separator == NULL) {

+        strncpy(username, p->up.username, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        domain[0]=0;
+    } else {
+        strncpy(username, separator+1, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        len = separator - p->up.username;
+        if (len > sizeof(domain) - 1) len = sizeof(domain) - 1;
+        strncpy(domain, p->up.username,  len);
+        domain[len]=0;

+ }
+
+
+ /* fill 1st 16 bytes with md4 hash, disregard terminating null */
+ gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash);
+
+ /* pad to 21 bytes */
+ memset (md4_hash + 16, 0, 5);
+
+ ret_val = base64_decode( phase_2, (void *)buf2);
+ /* we can be sure that phase_2 is less than 128
+ * therefore buf2 needs to be (3/4 * 128) */
+

+    /* extract the challenge from bytes 24-31 */
+    for (i=0; i<8; i++)
+    {

+ challenge[i] = buf2[i+24];
+ }
+
+ if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+ + /* NTLMv2 hash */
+ my_strupr(strcpy(userdomain, username));
+ if (strlen(username) + strlen(domain) < sizeof(userdomain))
+ strcat(userdomain, domain); + unicodize (userdomain_u, userdomain);
+ gen_hmac_md5(userdomain_u, 2 * strlen(userdomain), md4_hash, 16, ntlmv2_hash);
+

+        /* NTLMv2 Blob */
+        memset(ntlmv2_blob, 0, 128);                /* Clear blob buffer */
+        ntlmv2_blob[0x00]=1;                        /* Signature */
+        ntlmv2_blob[0x01]=1;                        /* Signature */
+        ntlmv2_blob[0x04]=0;                        /* Reserved */
+        gen_timestamp(&ntlmv2_blob[0x08]);          /* 64-bit Timestamp */
+        gen_nonce(&ntlmv2_blob[0x10]);              /* 64-bit Client Nonce */
+        ntlmv2_blob[0x18]=0;                        /* Unknown, zero should work */
+
+        /* Add target information block to the blob */
+        unsigned short tib_len;
+        if (( *((long *)&buf2[0x14]) & 0x00800000) == 0x00800000){ /* Check for Target Information block */
+            tib_len = *((unsigned short *)&buf2[0x28]); /* Get Target Information block size */
+            if (tib_len > 96) tib_len = 96;
+            char *tib_ptr = buf2 + buf2[0x2c]; /* Get Target Information block pointer */
+            memcpy(&ntlmv2_blob[0x1c], tib_ptr, tib_len); /* Copy Target Information block into the blob */

+ } else {
+ tib_len = 0;
+ }
+
+ ntlmv2_blob[0x1c + tib_len] = 0; /* Unknown, zero works */
+
+ /* Get blob length */
+ ntlmv2_blob_size = 0x20 + tib_len;
+
+ /* Add challenge from message 2 */
+ memcpy(&ntlmv2_response[8], challenge, 8);
+
+ /* hmac-md5 */
+ gen_hmac_md5(&ntlmv2_response[8], ntlmv2_blob_size + 8, ntlmv2_hash, 16, ntlmv2_hmacmd5);
+ + /* Add hmac-md5 result to the blob */
+ memcpy(ntlmv2_response, ntlmv2_hmacmd5, 16); /* Note: This overwrites challenge previously written at ntlmv2_response[8..15] */
+ + } else { /* Generate NTLM response */
+

+        create_des_keys ((unsigned char *)md4_hash, key1);
+        des_set_key_unchecked ((des_cblock *)key1, sched1);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)ntlm_response, sched1, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
+        des_set_key_unchecked ((des_cblock *)key2, sched2);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[8]), sched2, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
+        des_set_key_unchecked ((des_cblock *)key3, sched3);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[16]), sched3, DES_ENCRYPT);

+ }
+ + + memset (phase3, 0, sizeof (phase3)); /* clear reply */
+
+ strcpy (phase3, "NTLMSSP\0"); /* signature */
+ phase3[8] = 3; /* type 3 */
+
+ if (ntlmv2_enabled){ /* NTLMv2 response */
+ add_security_buffer(0x14, ntlmv2_response, ntlmv2_blob_size + 16, phase3, &phase3_bufpos);
+ }else{ /* NTLM response */
+ add_security_buffer(0x14, ntlm_response, 24, phase3, &phase3_bufpos);
+ }
+ + /* username in ascii */
+ add_security_buffer(0x24, username, strlen (username), phase3, &phase3_bufpos);
+
+ /* Set domain. If <domain> is empty, default domain will be used (i.e. proxy's domain) */
+ add_security_buffer(0x1c, domain, strlen (domain), phase3, &phase3_bufpos);
+ +

+    /* other security buffers will be empty */
+    phase3[0x10] = phase3_bufpos; /* lm not used */
+    phase3[0x30] = phase3_bufpos; /* no workstation name supplied */
+    phase3[0x38] = phase3_bufpos; /* no session key */

+ + /* flags */
+ phase3[0x3c] = 0x02; /* negotiate oem */
+ phase3[0x3d] = 0x02; /* negotiate ntlm */

- return ((const char *)make_base64_string2 ((unsigned char *)phase3, buflen, gc));
+ return ((const char *)make_base64_string2 ((unsigned char *)phase3, phase3_bufpos, gc));
}

#else

proxy.c 2008-01-15 11:46:12.737156800 +0100 renamed from proxy.c 2008-01-13 21:25:52.945307200 +0100 +5 -3
diff -Naur openvpn-2.1/proxy.c openvpn-NTLMv2-2.1/proxy.c
@@ -294,19 +294,21 @@
    p->auth_method = HTTP_AUTH_BASIC;
      else if (!strcmp (o->auth_method_string, "ntlm"))
    p->auth_method = HTTP_AUTH_NTLM;
+      else if (!strcmp (o->auth_method_string, "ntlm2"))
+    p->auth_method = HTTP_AUTH_NTLM2;
      else
-    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', or 'ntlm' methods are currently supported",
+    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', 'ntlm', or 'ntlm2' methods are currently supported",
         o->auth_method_string);
    }
 
-  /* only basic and NTLM authentication supported so far */
-  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM)
+  /* only basic and NTLM/NTLMv2 authentication supported so far */
+  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
    {
      get_user_pass_http (p, true);
    }
 

#if !NTLM
- if (p->auth_method == HTTP_AUTH_NTLM)
+ if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2) msg (M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built
without NTLM Proxy support.");
#endif

@@ -374,6 +376,12 @@

#if NTLM
case HTTP_AUTH_NTLM:
+ case HTTP_AUTH_NTLM2:

+      /* keep-alive connection */
+      openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+      if (!send_line_crlf (sd, buf))
+    goto error;
+
      openvpn_snprintf (buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
            ntlm_phase_1 (p, &gc));
      msg (D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
@@ -411,7 +419,7 @@
      msg (D_PROXY, "Proxy requires authentication");

      /* check for NTLM */
-      if (p->auth_method == HTTP_AUTH_NTLM)
+      if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
        {
#if NTLM
          /* look for the phase 2 response */
@@ -456,6 +464,12 @@
          if (!send_line_crlf (sd, buf))
            goto error;
+          /* keep-alive connection */
+          openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+          if (!send_line_crlf (sd, buf))
+            goto error;
+
+         
          /* send HOST etc, */
          openvpn_sleep (1);
          openvpn_snprintf (buf, sizeof(buf), "Host: %s", host);
proxy.h 2008-01-15 11:25:00.097190400 +0100 renamed from proxy.h 2008-01-13 21:25:52.975350400 +0100 +0 -0
diff -Naur openvpn-2.1/proxy.h openvpn-NTLMv2-2.1/proxy.h
@@ -59,6 +59,7 @@

#define HTTP_AUTH_BASIC 1
#define HTTP_AUTH_NTLM 2
#define HTTP_AUTH_N 3
+#define HTTP_AUTH_NTLM2 4

struct http_proxy_options {
const char *server;

#1 lore raw
Quoted 2 lines · Mirek Zajic

I'm sending the patch again. Now it should be correctly diffed against version 2.1.

Great!

But you have change the indentation, so your changes cannot be reviewed. For example, the first function create_des_keys, you did not modify it, but still
it modified.

Please make sure that only your changes are shown when you send code for review.

Alon.

#2 lore raw

Please also don't keep changing the subject...
Just reply to the same thread.

Quoted 16 lines · Alon Bar-Lev
Quoted 2 lines · Mirek Zajic

I'm sending the patch again. Now it should be correctly diffed against version 2.1.

Great!

But you have change the indentation, so your changes cannot be reviewed. For example, the first function create_des_keys, you did not modify it, but still
it modified.

Please make sure that only your changes are shown when you send code for review.

Alon.

#3 lore raw

Thank you for info. I wanted to use equal indentation rules in the whole file. I'm sending another patch without these indentation changes.

Also, this code was originally written for x86 processors with 32-bit environment. Different wordsize or endian may cause trouble when porting to other platforms. Does anybody want to use this feature with some strange processor?

Miroslav Zajic
Nextsoft s.r.o.

ntlm.c 2008-01-15 13:26:07.537256000 +0100 renamed from ntlm.c 2008-01-13 21:25:52.915264000 +0100 +2 -0
diff -Naur openvpn-2.1/ntlm.c openvpn-NTLMv2-2.1/ntlm.c
@@ -3,6 +3,8 @@
 *
 *  Copyright (C) 2004 William Preston
 *
+ *  *NTLMv2 support and domain name parsing by Miroslav Zajic, Nextsoft s.r.o.*
+ *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
@@ -70,6 +72,88 @@
  memcpy (result, md, 16);

}

+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int key_len,char *result) +{
+ unsigned int len;
+

+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+ /* Copies 8 bytes long timestamp into "timestamp" buffer.
+ * Timestamp is Little-endian, 64-bit signed value representing the number of tenths of a microsecond since January 1, 1601.
+ */
+

+    unsigned char bufA[8]; /* Buffer for 64-bit computing */
+    unsigned char bufB[8]; /* Second buffer  */
+    int a, b, c, tmp, tmp2, carry;
+
+    /* set 64-bit buffer A with current time (seconds since 00:00:00 1.1.1970) */
+    *(unsigned int *)bufA =     (unsigned int)time(NULL);
+    *(unsigned int *)&bufA[4] = (unsigned int)0;
+
+    /* 64-bit adition of 0x02B6109100 (seconds between 00:00:00 1.1.1601 and 00:00:00 1.1.1970) */
+    *(unsigned int *)bufB =     (unsigned int)0xB6109100;
+    *(unsigned int *)&bufB[4] = (unsigned int)0x00000002;
+
+    carry=0;
+    for (a=0; a<8; a++){ /* lame adition */
+        tmp = (unsigned int)bufA[a] + (unsigned int)bufB[a] + carry;
+        bufA[a] = tmp & 0xFF;
+        carry = (tmp & 0xFF00) >> 8;
+    }
+   +    /* 64-bit multiply by 10000000 = 0x989680 (converting seconds to tenths of microseconds) */
+    *(unsigned int *)bufB =           (unsigned int)0x00989680;
+    *(unsigned int *)&bufB[4] =       (unsigned int)0x00000000;
+    *(unsigned int *)timestamp =      (unsigned int)0x00000000;
+    *(unsigned int *)&timestamp[4] =  (unsigned int)0x00000000;
+
+    for (a=0; a<8; a++){ /* lame multiply */
+        for (b=0; b<8; b++){
+            tmp = (unsigned int)bufA[a] * (unsigned int)bufB[b];
+            carry=0;
+            for (c=b; (c<8-a) && (c < b + 3); c++){
+                tmp2 = ((tmp & (0xFF << ((c-b) * 8))) >> ((c-b) * 8)) + (unsigned int)timestamp[a+c] + carry;
+                timestamp[a+c] = tmp2 & 0xFF;
+                carry = (tmp2 & 0xFF00) >> 8;

+ }
+ }
+ }
+}
+
+static void
+gen_nonce (unsigned char *nonce)
+{
+ /* Generates 8 random bytes to be used as client nonce */
+ int i;
+
+ for(i=0;i<8;i++);
+ nonce[i] = (unsigned char)random();
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{
+ /* converts string to uppercase in place */
+

+    unsigned char *tmp;
+    tmp = str;
+    do
+    {
+        if (*str >= 'a' && *str <= 'z')
+            *str -= 32;
+    }
+    while (*(++str));
+    return tmp;

+}
+
static int
unicodize (char *dst, const char *src)
{
@@ -85,6 +169,17 @@
return i;
}

+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned char *msg_buf, int *msg_bufpos) +{

+    /* Adds security buffer data to a message and sets security buffer's offset and length */
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = *msg_bufpos;
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;

+}
+
const char *
ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc)
{
@@ -105,23 +200,56 @@
const char *
ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc) {
+ /* NTLM handshake

+     *
+     * http://davenport.sourceforge.net/ntlm.html
+     *
+     */

+
char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */ char buf2[128]; /* decoded reply from proxy */
- char phase3[146];
+ unsigned char phase3[464];

char md4_hash[21];

-  char challenge[8], response[24];
-  int i, ret_val, buflen;
+  char challenge[8], ntlm_response[24];
+  int i, ret_val;

des_cblock key1, key2, key3;
des_key_schedule sched1, sched2, sched3;

- /* try a minimal NTLM handshake

-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   */
+    char ntlmv2_response[144];
+    char userdomain_u[256]; /* for uppercase unicode username and domain */
+    char userdomain[128];   /* the same as previous but ascii */
+    char ntlmv2_hash[16];
+    char ntlmv2_hmacmd5[16];
+    char *ntlmv2_blob = ntlmv2_response + 16; /* inside ntlmv2_response, length: 128 */
+    int ntlmv2_blob_size=0;
+    int phase3_bufpos = 0x40; /* offset to next security buffer data to be added */
+    int len;
+
+    char domain[128];
+    char username[128];
+    char *separator;

+
+ bool ntlmv2_enabled = (p->auth_method == HTTP_AUTH_NTLM2);
+
ASSERT (strlen (p->up.username) > 0);
ASSERT (strlen (p->up.password) > 0);
+ + /* username parsing */
+ separator = strchr(p->up.username, '\\');
+ if (separator == NULL) {

+        strncpy(username, p->up.username, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        domain[0]=0;
+    } else {
+        strncpy(username, separator+1, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        len = separator - p->up.username;
+        if (len > sizeof(domain) - 1) len = sizeof(domain) - 1;
+        strncpy(domain, p->up.username,  len);
+        domain[len]=0;

+ }
+

/* fill 1st 16 bytes with md4 hash, disregard terminating null */ gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash);
@@ -139,48 +267,93 @@

    challenge[i] = buf2[i+24];

}

-  create_des_keys ((unsigned char *)md4_hash, key1);
-  des_set_key_unchecked ((des_cblock *)key1, sched1);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)response, sched1, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
-  des_set_key_unchecked ((des_cblock *)key2, sched2);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[8]), sched2, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
-  des_set_key_unchecked ((des_cblock *)key3, sched3);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[16]), sched3, DES_ENCRYPT);

-
- /* clear reply */
- memset (phase3, 0, sizeof (phase3));
-
- strcpy (phase3, "NTLMSSP\0");
- phase3[8] = 3; /* type 3 */
-
- buflen = 0x58 + strlen (p->up.username);
- if (buflen > (int) sizeof (phase3))
- buflen = sizeof (phase3);
-

-  phase3[0x10] = buflen; /* lm not used */
-  phase3[0x20] = buflen; /* default domain (i.e. proxy's domain) */
-  phase3[0x30] = buflen; /* no workstation name supplied */
-  phase3[0x38] = buflen; /* no session key */
-
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);

-
+ if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+ + /* NTLMv2 hash */
+ my_strupr(strcpy(userdomain, username));
+ if (strlen(username) + strlen(domain) < sizeof(userdomain))
+ strcat(userdomain, domain); + unicodize (userdomain_u, userdomain);
+ gen_hmac_md5(userdomain_u, 2 * strlen(userdomain), md4_hash, 16, ntlmv2_hash);
+

+        /* NTLMv2 Blob */
+        memset(ntlmv2_blob, 0, 128);                /* Clear blob buffer */
+        ntlmv2_blob[0x00]=1;                        /* Signature */
+        ntlmv2_blob[0x01]=1;                        /* Signature */
+        ntlmv2_blob[0x04]=0;                        /* Reserved */
+        gen_timestamp(&ntlmv2_blob[0x08]);          /* 64-bit Timestamp */
+        gen_nonce(&ntlmv2_blob[0x10]);              /* 64-bit Client Nonce */
+        ntlmv2_blob[0x18]=0;                        /* Unknown, zero should work */
+
+        /* Add target information block to the blob */
+        unsigned short tib_len;
+        if (( *((long *)&buf2[0x14]) & 0x00800000) == 0x00800000){ /* Check for Target Information block */
+            tib_len = *((unsigned short *)&buf2[0x28]); /* Get Target Information block size */
+            if (tib_len > 96) tib_len = 96;
+            char *tib_ptr = buf2 + buf2[0x2c]; /* Get Target Information block pointer */
+            memcpy(&ntlmv2_blob[0x1c], tib_ptr, tib_len); /* Copy Target Information block into the blob */

+ } else {
+ tib_len = 0;
+ }
+
+ ntlmv2_blob[0x1c + tib_len] = 0; /* Unknown, zero works */
+
+ /* Get blob length */
+ ntlmv2_blob_size = 0x20 + tib_len;
+
+ /* Add challenge from message 2 */
+ memcpy(&ntlmv2_response[8], challenge, 8);
+
+ /* hmac-md5 */
+ gen_hmac_md5(&ntlmv2_response[8], ntlmv2_blob_size + 8, ntlmv2_hash, 16, ntlmv2_hmacmd5);
+ + /* Add hmac-md5 result to the blob */
+ memcpy(ntlmv2_response, ntlmv2_hmacmd5, 16); /* Note: This overwrites challenge previously written at ntlmv2_response[8..15] */
+ + } else { /* Generate NTLM response */
+

+        create_des_keys ((unsigned char *)md4_hash, key1);
+        des_set_key_unchecked ((des_cblock *)key1, sched1);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)ntlm_response, sched1, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
+        des_set_key_unchecked ((des_cblock *)key2, sched2);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[8]), sched2, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
+        des_set_key_unchecked ((des_cblock *)key3, sched3);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[16]), sched3, DES_ENCRYPT);

+ }
+ + + memset (phase3, 0, sizeof (phase3)); /* clear reply */
+
+ strcpy (phase3, "NTLMSSP\0"); /* signature */
+ phase3[8] = 3; /* type 3 */
+
+ if (ntlmv2_enabled){ /* NTLMv2 response */
+ add_security_buffer(0x14, ntlmv2_response, ntlmv2_blob_size + 16, phase3, &phase3_bufpos);
+ }else{ /* NTLM response */
+ add_security_buffer(0x14, ntlm_response, 24, phase3, &phase3_bufpos);
+ }
+ + /* username in ascii */
+ add_security_buffer(0x24, username, strlen (username), phase3, &phase3_bufpos);
+
+ /* Set domain. If <domain> is empty, default domain will be used (i.e. proxy's domain) */
+ add_security_buffer(0x1c, domain, strlen (domain), phase3, &phase3_bufpos);
+ +

+    /* other security buffers will be empty */
+    phase3[0x10] = phase3_bufpos; /* lm not used */
+    phase3[0x30] = phase3_bufpos; /* no workstation name supplied */
+    phase3[0x38] = phase3_bufpos; /* no session key */

+ + /* flags */
phase3[0x3c] = 0x02; /* negotiate oem */
phase3[0x3d] = 0x02; /* negotiate ntlm */

- return ((const char *)make_base64_string2 ((unsigned char *)phase3, buflen, gc));
+ return ((const char *)make_base64_string2 ((unsigned char *)phase3, phase3_bufpos, gc)); }

#else

proxy.c 2008-01-15 11:46:12.737156800 +0100 renamed from proxy.c 2008-01-13 21:25:52.945307200 +0100 +5 -3
diff -Naur openvpn-2.1/proxy.c openvpn-NTLMv2-2.1/proxy.c
@@ -294,19 +294,21 @@
    p->auth_method = HTTP_AUTH_BASIC;
      else if (!strcmp (o->auth_method_string, "ntlm"))
    p->auth_method = HTTP_AUTH_NTLM;
+      else if (!strcmp (o->auth_method_string, "ntlm2"))
+    p->auth_method = HTTP_AUTH_NTLM2;
      else
-    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', or 'ntlm' methods are currently supported",
+    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', 'ntlm', or 'ntlm2' methods are currently supported",
         o->auth_method_string);
    }
 
-  /* only basic and NTLM authentication supported so far */
-  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM)
+  /* only basic and NTLM/NTLMv2 authentication supported so far */
+  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
    {
      get_user_pass_http (p, true);
    }
 

#if !NTLM
- if (p->auth_method == HTTP_AUTH_NTLM)
+ if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2) msg (M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built
without NTLM Proxy support.");
#endif

@@ -374,6 +376,12 @@

#if NTLM
case HTTP_AUTH_NTLM:
+ case HTTP_AUTH_NTLM2:

+      /* keep-alive connection */
+      openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+      if (!send_line_crlf (sd, buf))
+    goto error;
+
      openvpn_snprintf (buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
            ntlm_phase_1 (p, &gc));
      msg (D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
@@ -411,7 +419,7 @@
      msg (D_PROXY, "Proxy requires authentication");

      /* check for NTLM */
-      if (p->auth_method == HTTP_AUTH_NTLM)
+      if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
        {
#if NTLM
          /* look for the phase 2 response */
@@ -456,6 +464,12 @@
          if (!send_line_crlf (sd, buf))
            goto error;
+          /* keep-alive connection */
+          openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+          if (!send_line_crlf (sd, buf))
+            goto error;
+
+         
          /* send HOST etc, */
          openvpn_sleep (1);
          openvpn_snprintf (buf, sizeof(buf), "Host: %s", host);
proxy.h 2008-01-15 11:25:00.097190400 +0100 renamed from proxy.h 2008-01-13 21:25:52.975350400 +0100 +0 -0
diff -Naur openvpn-2.1/proxy.h openvpn-NTLMv2-2.1/proxy.h
@@ -59,6 +59,7 @@

#define HTTP_AUTH_BASIC 1
#define HTTP_AUTH_NTLM 2
#define HTTP_AUTH_N 3
+#define HTTP_AUTH_NTLM2 4

struct http_proxy_options {
const char *server;

#4 lore raw
Quoted 7 lines · Mirek Zajic

Thank you for info. I wanted to use equal indentation rules in the whole file. I'm sending another patch without these indentation changes.

Also, this code was originally written for x86 processors with 32-bit environment. Different wordsize or endian may cause trouble when porting to other platforms. Does anybody want to use this feature with some strange processor?

Sure!
This should be architecture independent.

Why have you added the keep alive?
If you think this is required, I think you should add a new option for this unrelated to ntlm. Maybe selecting ntlm should enable it... But I am not sure these are related.

Some general none logical comments bellow...
After you will have architecture independent code, please resend.

Alon.

Review

Quoted 11 lines
(quoted diff) +2 -0
+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int

key_len,char *result)

(quoted diff) +8 -0
+{
+    unsigned int len;
+
+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

No return codes?

Review

Quoted 12 lines
(quoted diff) +7 -0
+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+    /* Copies 8 bytes long timestamp into "timestamp" buffer.
+     * Timestamp is Little-endian, 64-bit signed value representing the

number of tenths of a microsecond since January 1, 1601.

(quoted diff) +4 -0
+     */
+
+    unsigned char bufA[8]; /* Buffer for 64-bit computing */
+    unsigned char bufB[8]; /* Second buffer  */

What is first buffer and second buffer, please find proper names.

Quoted 5 lines
(quoted diff) +3 -0
+    int a, b, c, tmp, tmp2, carry;
+
+    /* set 64-bit buffer A with current time (seconds since 00:00:00

1.1.1970) */
+ *(unsigned int *)bufA = (unsigned int)time(NULL);

Use openvpn_time.

Quoted 28 lines
(quoted diff) +3 -0
+    *(unsigned int *)&bufA[4] = (unsigned int)0;
+
+    /* 64-bit adition of 0x02B6109100 (seconds between 00:00:00

1.1.1601 and 00:00:00 1.1.1970) */

(quoted diff) +11 -0
+    *(unsigned int *)bufB =     (unsigned int)0xB6109100;
+    *(unsigned int *)&bufB[4] = (unsigned int)0x00000002;
+
+    carry=0;
+    for (a=0; a<8; a++){ /* lame adition */
+        tmp = (unsigned int)bufA[a] + (unsigned int)bufB[a] + carry;
+        bufA[a] = tmp & 0xFF;
+        carry = (tmp & 0xFF00) >> 8;
+    }
+
+    /* 64-bit multiply by 10000000 = 0x989680 (converting seconds to

tenths of microseconds) */

(quoted diff) +11 -0
+    *(unsigned int *)bufB =           (unsigned int)0x00989680;
+    *(unsigned int *)&bufB[4] =       (unsigned int)0x00000000;
+    *(unsigned int *)timestamp =      (unsigned int)0x00000000;
+    *(unsigned int *)&timestamp[4] =  (unsigned int)0x00000000;
+
+    for (a=0; a<8; a++){ /* lame multiply */
+        for (b=0; b<8; b++){
+            tmp = (unsigned int)bufA[a] * (unsigned int)bufB[b];
+            carry=0;
+            for (c=b; (c<8-a) && (c < b + 3); c++){
+                tmp2 = ((tmp & (0xFF << ((c-b) * 8))) >> ((c-b) * 8)) +

(unsigned int)timestamp[a+c] + carry;

Review

Quoted 6 lines
(quoted diff) +6 -0
+                timestamp[a+c] = tmp2 & 0xFF;
+                carry = (tmp2 & 0xFF00) >> 8;
+            }
+        }
+    }
+}

I believe that the above can be achieved using 64bit ops... long long on none MS compiler and LONGLONG or something similar on Microsoft ones.

Review

Quoted 9 lines
(quoted diff) +9 -0
+
+static void
+gen_nonce (unsigned char *nonce)
+{
+    /* Generates 8 random bytes to be used as client nonce */
+    int i;
+
+    for(i=0;i<8;i++);
+        nonce[i] = (unsigned char)random();

Use get_random.

Review

Quoted 8 lines
(quoted diff) +8 -0
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{
+    /* converts string to uppercase in place */
+
+    unsigned char *tmp;
+    tmp = str;

Use toupper

Quoted 13 lines
(quoted diff) +9 -0
+    do
+    {
+        if (*str >= 'a' && *str <= 'z')
+            *str -= 32;
+    }
+    while (*(++str));
+    return tmp;
+}
+
 static int
 unicodize (char *dst, const char *src)
 {

Review line 175

Quoted 7 lines
(quoted diff) +3 -0
@@ -85,6 +169,17 @@
   return i;
 }
 
+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned
 char *msg_buf, int *msg_bufpos)
+{

Well, this is not architecture independent.

Quoted 17 lines
(quoted diff) +1 -0
+    /* Adds security buffer data to a message and sets security

buffer's offset and length */

(quoted diff) +7 -7
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = *msg_bufpos;
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;
+}
+
 
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */

Review

Quoted 10 lines
(quoted diff) +6 -4
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);
-
+    if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+
+        /* NTLMv2 hash */
+        my_strupr(strcpy(userdomain, username));
+        if (strlen(username) + strlen(domain) < sizeof(userdomain))
+            strcat(userdomain, domain);

And if not?

#5 lore raw

The keep-alive is enabled only when ntlm or ntlm2 is used. The reason is that proxy may close the connection before ntlm messages are sent. It is the correct way to always use keep-alive when ntlm is used.

I'm going to fix the architecture independency and other things and I'll send the patch later.

Miroslav Zajic
Nextsoft s.r.o.

Alon Bar-Lev napsal(a):

Quoted 195 lines · Mirek Zajic
Quoted 9 lines

Thank you for info. I wanted to use equal indentation rules in the whole file. I'm sending another patch without these indentation changes.

Also, this code was originally written for x86 processors with 32-bit environment. Different wordsize or endian may cause trouble when porting to other platforms. Does anybody want to use this feature with some strange processor?

Sure!
This should be architecture independent.

Why have you added the keep alive?
If you think this is required, I think you should add a new option for this unrelated to ntlm. Maybe selecting ntlm should enable it... But I am not sure these are related.

Some general none logical comments bellow...
After you will have architecture independent code, please resend.

Alon.

Review

Quoted 12 lines
(quoted diff) +2 -0
+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int

key_len,char *result)

(quoted diff) +8 -0
+{
+    unsigned int len;
+
+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

No return codes?

Review

Quoted 13 lines
(quoted diff) +7 -0
+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+    /* Copies 8 bytes long timestamp into "timestamp" buffer.
+     * Timestamp is Little-endian, 64-bit signed value representing the

number of tenths of a microsecond since January 1, 1601.

(quoted diff) +4 -0
+     */
+
+    unsigned char bufA[8]; /* Buffer for 64-bit computing */
+    unsigned char bufB[8]; /* Second buffer  */

What is first buffer and second buffer, please find proper names.

Quoted 6 lines
(quoted diff) +3 -0
+    int a, b, c, tmp, tmp2, carry;
+
+    /* set 64-bit buffer A with current time (seconds since 00:00:00

1.1.1970) */
+ *(unsigned int *)bufA = (unsigned int)time(NULL);

Use openvpn_time.

Quoted 29 lines
(quoted diff) +3 -0
+    *(unsigned int *)&bufA[4] = (unsigned int)0;
+
+    /* 64-bit adition of 0x02B6109100 (seconds between 00:00:00

1.1.1601 and 00:00:00 1.1.1970) */

(quoted diff) +11 -0
+    *(unsigned int *)bufB =     (unsigned int)0xB6109100;
+    *(unsigned int *)&bufB[4] = (unsigned int)0x00000002;
+
+    carry=0;
+    for (a=0; a<8; a++){ /* lame adition */
+        tmp = (unsigned int)bufA[a] + (unsigned int)bufB[a] + carry;
+        bufA[a] = tmp & 0xFF;
+        carry = (tmp & 0xFF00) >> 8;
+    }
+
+    /* 64-bit multiply by 10000000 = 0x989680 (converting seconds to

tenths of microseconds) */

(quoted diff) +11 -0
+    *(unsigned int *)bufB =           (unsigned int)0x00989680;
+    *(unsigned int *)&bufB[4] =       (unsigned int)0x00000000;
+    *(unsigned int *)timestamp =      (unsigned int)0x00000000;
+    *(unsigned int *)&timestamp[4] =  (unsigned int)0x00000000;
+
+    for (a=0; a<8; a++){ /* lame multiply */
+        for (b=0; b<8; b++){
+            tmp = (unsigned int)bufA[a] * (unsigned int)bufB[b];
+            carry=0;
+            for (c=b; (c<8-a) && (c < b + 3); c++){
+                tmp2 = ((tmp & (0xFF << ((c-b) * 8))) >> ((c-b) * 8)) +

(unsigned int)timestamp[a+c] + carry;

Review

Quoted 6 lines
(quoted diff) +6 -0
+                timestamp[a+c] = tmp2 & 0xFF;
+                carry = (tmp2 & 0xFF00) >> 8;
+            }
+        }
+    }
+}

I believe that the above can be achieved using 64bit ops... long long on none MS compiler and LONGLONG or something similar on Microsoft ones.

Review

Quoted 10 lines
(quoted diff) +9 -0
+
+static void
+gen_nonce (unsigned char *nonce)
+{
+    /* Generates 8 random bytes to be used as client nonce */
+    int i;
+
+    for(i=0;i<8;i++);
+        nonce[i] = (unsigned char)random();

Use get_random.

Review

Quoted 9 lines
(quoted diff) +8 -0
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{
+    /* converts string to uppercase in place */
+
+    unsigned char *tmp;
+    tmp = str;

Use toupper

Quoted 13 lines
(quoted diff) +9 -0
+    do
+    {
+        if (*str >= 'a' && *str <= 'z')
+            *str -= 32;
+    }
+    while (*(++str));
+    return tmp;
+}
+
 static int
 unicodize (char *dst, const char *src)
 {

Review line 176

Quoted 8 lines
(quoted diff) +3 -0
@@ -85,6 +169,17 @@
   return i;
 }
 
+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned
 char *msg_buf, int *msg_bufpos)
+{
 

Well, this is not architecture independent.

Quoted 20 lines
(quoted diff) +1 -0
+    /* Adds security buffer data to a message and sets security

buffer's offset and length */

(quoted diff) +7 -7
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = *msg_bufpos;
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;
+}
+
 
 
 
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */

Review

Quoted 10 lines
(quoted diff) +6 -4
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);
-
+    if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+
+        /* NTLMv2 hash */
+        my_strupr(strcpy(userdomain, username));
+        if (strlen(username) + strlen(domain) < sizeof(userdomain))
+            strcat(userdomain, domain);

And if not?

#6 lore raw
In reply to Alon Bar-Lev

Hello,
I have finally made some changes to the NTLMv2 patch according to Alon's comments. Now it should also be architecture independent.

Miroslav Zajic
NextSoft s.r.o.

ntlm.c 2008-03-12 11:36:40.687500000 +0100 renamed from ntlm.c 2008-01-13 21:25:52.915264000 +0100 +2 -0
diff -Naur openvpn-2.1/ntlm.c openvpn-NTLMv2-predelane-2.1/ntlm.c
@@ -3,6 +3,8 @@
 *
 *  Copyright (C) 2004 William Preston
 *
+ *  *NTLMv2 support and domain name parsing by Miroslav Zajic, Nextsoft s.r.o.*
+ *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
@@ -70,6 +72,61 @@
  memcpy (result, md, 16);

}

+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int key_len,char *result) +{
+ unsigned int len;
+

+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+ /* Copies 8 bytes long timestamp into "timestamp" buffer.
+ * Timestamp is Little-endian, 64-bit signed value representing the number of tenths of a microsecond since January 1, 1601.
+ */
+
+ UINTEGER64 timestamp_ull;
+
+ timestamp_ull = openvpn_time(NULL);
+ timestamp_ull = (timestamp_ull + UINT64(11644473600)) * UINT64(10000000);
+

+    /* store little endian value */
+    timestamp[0]=  timestamp_ull & UINT64(0xFF);
+    timestamp[1]= (timestamp_ull & UINT64(0xFF00)) >> 8;
+    timestamp[2]= (timestamp_ull & UINT64(0xFF0000)) >> 16;
+    timestamp[3]= (timestamp_ull & UINT64(0xFF000000)) >> 24;
+    timestamp[4]= (timestamp_ull & UINT64(0xFF00000000)) >> 32;
+    timestamp[5]= (timestamp_ull & UINT64(0xFF0000000000)) >> 40;
+    timestamp[6]= (timestamp_ull & UINT64(0xFF000000000000)) >> 48;
+    timestamp[7]= (timestamp_ull & UINT64(0xFF00000000000000)) >> 52;

+}
+
+static void
+gen_nonce (unsigned char *nonce)
+{
+ /* Generates 8 random bytes to be used as client nonce */
+ int i;
+
+ for(i=0;i<8;i++){
+ nonce[i] = (unsigned char)get_random();
+ }
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{
+ /* converts string to uppercase in place */
+ unsigned char *tmp = str;;
+
+ do *str = toupper(*str); while (*(++str));
+ return tmp;
+}
+
static int
unicodize (char *dst, const char *src)
{
@@ -85,6 +142,18 @@
return i;
}

+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned char *msg_buf, int *msg_bufpos) +{

+    /* Adds security buffer data to a message and sets security buffer's offset and length */
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = (unsigned char)(*msg_bufpos & 0xff);
+    msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos & 0xff00) >> 8);
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;

+}
+
const char *
ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc)
{
@@ -105,23 +174,56 @@
const char *
ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc) {
+ /* NTLM handshake

+     *
+     * http://davenport.sourceforge.net/ntlm.html
+     *
+     */

+
char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */ char buf2[128]; /* decoded reply from proxy */
- char phase3[146];
+ unsigned char phase3[464];

char md4_hash[21];

-  char challenge[8], response[24];
-  int i, ret_val, buflen;
+  char challenge[8], ntlm_response[24];
+  int i, ret_val;

des_cblock key1, key2, key3;
des_key_schedule sched1, sched2, sched3;

- /* try a minimal NTLM handshake

-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   */
+    char ntlmv2_response[144];
+    char userdomain_u[256]; /* for uppercase unicode username and domain */
+    char userdomain[128];   /* the same as previous but ascii */
+    char ntlmv2_hash[16];
+    char ntlmv2_hmacmd5[16];
+    char *ntlmv2_blob = ntlmv2_response + 16; /* inside ntlmv2_response, length: 128 */
+    int ntlmv2_blob_size=0;
+    int phase3_bufpos = 0x40; /* offset to next security buffer data to be added */
+    int len;
+
+    char domain[128];
+    char username[128];
+    char *separator;

+
+ bool ntlmv2_enabled = (p->auth_method == HTTP_AUTH_NTLM2);
+
ASSERT (strlen (p->up.username) > 0);
ASSERT (strlen (p->up.password) > 0);
+ + /* username parsing */
+ separator = strchr(p->up.username, '\\');
+ if (separator == NULL) {

+        strncpy(username, p->up.username, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        domain[0]=0;
+    } else {
+        strncpy(username, separator+1, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        len = separator - p->up.username;
+        if (len > sizeof(domain) - 1) len = sizeof(domain) - 1;
+        strncpy(domain, p->up.username,  len);
+        domain[len]=0;

+ }
+

/* fill 1st 16 bytes with md4 hash, disregard terminating null */ gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash);
@@ -139,48 +241,95 @@

    challenge[i] = buf2[i+24];

}

-  create_des_keys ((unsigned char *)md4_hash, key1);
-  des_set_key_unchecked ((des_cblock *)key1, sched1);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)response, sched1, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
-  des_set_key_unchecked ((des_cblock *)key2, sched2);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[8]), sched2, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
-  des_set_key_unchecked ((des_cblock *)key3, sched3);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[16]), sched3, DES_ENCRYPT);

-
- /* clear reply */
- memset (phase3, 0, sizeof (phase3));
-
- strcpy (phase3, "NTLMSSP\0");
- phase3[8] = 3; /* type 3 */
-
- buflen = 0x58 + strlen (p->up.username);
- if (buflen > (int) sizeof (phase3))
- buflen = sizeof (phase3);
-

-  phase3[0x10] = buflen; /* lm not used */
-  phase3[0x20] = buflen; /* default domain (i.e. proxy's domain) */
-  phase3[0x30] = buflen; /* no workstation name supplied */
-  phase3[0x38] = buflen; /* no session key */
-
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);

-
+ if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+ + /* NTLMv2 hash */
+ my_strupr(strcpy(userdomain, username));
+ if (strlen(username) + strlen(domain) < sizeof(userdomain))
+ strcat(userdomain, domain);
+ else
+ msg (M_INFO, "Warning: Username or domain too long");
+ unicodize (userdomain_u, userdomain);
+ gen_hmac_md5(userdomain_u, 2 * strlen(userdomain), md4_hash, 16, ntlmv2_hash);
+

+        /* NTLMv2 Blob */
+        memset(ntlmv2_blob, 0, 128);                /* Clear blob buffer */
+        ntlmv2_blob[0x00]=1;                        /* Signature */
+        ntlmv2_blob[0x01]=1;                        /* Signature */
+        ntlmv2_blob[0x04]=0;                        /* Reserved */
+        gen_timestamp(&ntlmv2_blob[0x08]);          /* 64-bit Timestamp */
+        gen_nonce(&ntlmv2_blob[0x10]);              /* 64-bit Client Nonce */
+        ntlmv2_blob[0x18]=0;                        /* Unknown, zero should work */
+
+        /* Add target information block to the blob */
+        int tib_len;
+        if (( *((long *)&buf2[0x14]) & 0x00800000) == 0x00800000){ /* Check for Target Information block */
+            tib_len = buf2[0x28];/* Get Target Information block size */
+            if (tib_len > 96) tib_len = 96;
+            char *tib_ptr = buf2 + buf2[0x2c]; /* Get Target Information block pointer */
+            memcpy(&ntlmv2_blob[0x1c], tib_ptr, tib_len); /* Copy Target Information block into the blob */

+ } else {
+ tib_len = 0;
+ }
+
+ ntlmv2_blob[0x1c + tib_len] = 0; /* Unknown, zero works */
+
+ /* Get blob length */
+ ntlmv2_blob_size = 0x20 + tib_len;
+
+ /* Add challenge from message 2 */
+ memcpy(&ntlmv2_response[8], challenge, 8);
+
+ /* hmac-md5 */
+ gen_hmac_md5(&ntlmv2_response[8], ntlmv2_blob_size + 8, ntlmv2_hash, 16, ntlmv2_hmacmd5);
+ + /* Add hmac-md5 result to the blob */
+ memcpy(ntlmv2_response, ntlmv2_hmacmd5, 16); /* Note: This overwrites challenge previously written at ntlmv2_response[8..15] */
+ + } else { /* Generate NTLM response */
+

+        create_des_keys ((unsigned char *)md4_hash, key1);
+        des_set_key_unchecked ((des_cblock *)key1, sched1);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)ntlm_response, sched1, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
+        des_set_key_unchecked ((des_cblock *)key2, sched2);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[8]), sched2, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
+        des_set_key_unchecked ((des_cblock *)key3, sched3);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[16]), sched3, DES_ENCRYPT);

+ }
+ + + memset (phase3, 0, sizeof (phase3)); /* clear reply */
+
+ strcpy (phase3, "NTLMSSP\0"); /* signature */
+ phase3[8] = 3; /* type 3 */
+
+ if (ntlmv2_enabled){ /* NTLMv2 response */
+ add_security_buffer(0x14, ntlmv2_response, ntlmv2_blob_size + 16, phase3, &phase3_bufpos);
+ }else{ /* NTLM response */
+ add_security_buffer(0x14, ntlm_response, 24, phase3, &phase3_bufpos);
+ }
+ + /* username in ascii */
+ add_security_buffer(0x24, username, strlen (username), phase3, &phase3_bufpos);
+
+ /* Set domain. If <domain> is empty, default domain will be used (i.e. proxy's domain) */
+ add_security_buffer(0x1c, domain, strlen (domain), phase3, &phase3_bufpos);
+ +

+    /* other security buffers will be empty */
+    phase3[0x10] = phase3_bufpos; /* lm not used */
+    phase3[0x30] = phase3_bufpos; /* no workstation name supplied */
+    phase3[0x38] = phase3_bufpos; /* no session key */

+ + /* flags */
phase3[0x3c] = 0x02; /* negotiate oem */
phase3[0x3d] = 0x02; /* negotiate ntlm */

- return ((const char *)make_base64_string2 ((unsigned char *)phase3, buflen, gc));
+ return ((const char *)make_base64_string2 ((unsigned char *)phase3, phase3_bufpos, gc)); }

#else

ntlm.h 2008-03-12 11:35:36.296875000 +0100 renamed from ntlm.h 2008-01-13 21:25:52.935292800 +0100 +0 -0
diff -Naur openvpn-2.1/ntlm.h openvpn-NTLMv2-predelane-2.1/ntlm.h
@@ -3,6 +3,18 @@
 

#if NTLM

+/* 64bit datatype macros */
+#ifdef _MSC_VER
+ /* MS compilers */
+#define UINTEGER64 __int64
+#define UINT64(c) c ## Ui64
+#else
+ /* Non MS compilers */
+#define UINTEGER64 unsigned long long
+#define UINT64(c) c ## LL
+#endif
+
+
const char *ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc); const char *ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc);

proxy.c 2008-01-15 11:46:12.737156800 +0100 renamed from proxy.c 2008-01-13 21:25:52.945307200 +0100 +5 -3
diff -Naur openvpn-2.1/proxy.c openvpn-NTLMv2-predelane-2.1/proxy.c
@@ -294,19 +294,21 @@
    p->auth_method = HTTP_AUTH_BASIC;
      else if (!strcmp (o->auth_method_string, "ntlm"))
    p->auth_method = HTTP_AUTH_NTLM;
+      else if (!strcmp (o->auth_method_string, "ntlm2"))
+    p->auth_method = HTTP_AUTH_NTLM2;
      else
-    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', or 'ntlm' methods are currently supported",
+    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', 'ntlm', or 'ntlm2' methods are currently supported",
         o->auth_method_string);
    }
 
-  /* only basic and NTLM authentication supported so far */
-  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM)
+  /* only basic and NTLM/NTLMv2 authentication supported so far */
+  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
    {
      get_user_pass_http (p, true);
    }
 

#if !NTLM
- if (p->auth_method == HTTP_AUTH_NTLM)
+ if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2) msg (M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built
without NTLM Proxy support.");
#endif

@@ -374,6 +376,12 @@

#if NTLM
case HTTP_AUTH_NTLM:
+ case HTTP_AUTH_NTLM2:

+      /* keep-alive connection */
+      openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+      if (!send_line_crlf (sd, buf))
+    goto error;
+
      openvpn_snprintf (buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
            ntlm_phase_1 (p, &gc));
      msg (D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
@@ -411,7 +419,7 @@
      msg (D_PROXY, "Proxy requires authentication");

      /* check for NTLM */
-      if (p->auth_method == HTTP_AUTH_NTLM)
+      if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
        {
#if NTLM
          /* look for the phase 2 response */
@@ -456,6 +464,12 @@
          if (!send_line_crlf (sd, buf))
            goto error;
+          /* keep-alive connection */
+          openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+          if (!send_line_crlf (sd, buf))
+            goto error;
+
+         
          /* send HOST etc, */
          openvpn_sleep (1);
          openvpn_snprintf (buf, sizeof(buf), "Host: %s", host);
proxy.h 2008-01-15 11:25:00.097190400 +0100 renamed from proxy.h 2008-01-13 21:25:52.975350400 +0100 +0 -0
diff -Naur openvpn-2.1/proxy.h openvpn-NTLMv2-predelane-2.1/proxy.h
@@ -59,6 +59,7 @@

#define HTTP_AUTH_BASIC 1
#define HTTP_AUTH_NTLM 2
#define HTTP_AUTH_N 3
+#define HTTP_AUTH_NTLM2 4

struct http_proxy_options {
const char *server;

#7 lore raw
Quoted 6 lines · Mirek Zajic

Hello,
I have finally made some changes to the NTLMv2 patch according to Alon's comments. Now it should also be architecture independent.

Miroslav Zajic
NextSoft s.r.o.

Hi!
Great!
Some more comments... :)

Quoted 3 lines

+ timestamp_ull = openvpn_time(NULL);
+ timestamp_ull = (timestamp_ull + UINT64(11644473600)) *
UINT64(10000000);

Just noticed that openvpn has: openvpn_gettimeofday() which works also for WIN32... Maybe use this one?

Quoted 1 line

+ msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos & 0xff00) >> 8);

always & such with 0xff.

Quoted 8 lines

diff -Naur openvpn-2.1/ntlm.h openvpn-NTLMv2-predelane-2.1/ntlm.h
--- openvpn-2.1/ntlm.h 2008-01-13 21:25:52.935292800 +0100
+++ openvpn-NTLMv2-predelane-2.1/ntlm.h 2008-03-12 11:35:36.296875000
+0100
@@ -3,6 +3,18 @@

  #if NTLM
Quoted 12 lines

+/* 64bit datatype macros */
+#ifdef _MSC_VER
+ /* MS compilers */
+#define UINTEGER64 __int64
+#define UINT64(c) c ## Ui64
+#else
+ /* Non MS compilers */
+#define UINTEGER64 unsigned long long
+#define UINT64(c) c ## LL
+#endif
+

Why not use long long for both platforms, it is already used in common.h... Also you can use ull suffix for both platforms.

Anyway... this belongs to the .c file, it is not used by any other files. But I don't think it is required at all.

Alon.

#8 lore raw

Thans for your comments. There are some things that have to be explained before I'll make the changes, please read bellow.

Alon Bar-Lev napsal(a):

Quoted 15 lines · Mirek Zajic
Quoted 8 lines

Hello,
I have finally made some changes to the NTLMv2 patch according to Alon's comments. Now it should also be architecture independent.

Miroslav Zajic
NextSoft s.r.o.

Hi!
Great!
Some more comments... :)

Quoted 9 lines
Quoted 4 lines

+ timestamp_ull = openvpn_time(NULL);
+ timestamp_ull = (timestamp_ull + UINT64(11644473600)) *
UINT64(10000000);

Just noticed that openvpn has: openvpn_gettimeofday() which works also for WIN32... Maybe use this one?

openvpn_time works fine, why to change?

Quoted 5 lines
Quoted 2 lines

+ msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos & 0xff00) >> 8);

always & such with 0xff.

Please explain, I don't understand

Quoted 23 lines
Quoted 9 lines

diff -Naur openvpn-2.1/ntlm.h openvpn-NTLMv2-predelane-2.1/ntlm.h
--- openvpn-2.1/ntlm.h 2008-01-13 21:25:52.935292800 +0100
+++ openvpn-NTLMv2-predelane-2.1/ntlm.h 2008-03-12 11:35:36.296875000
+0100
@@ -3,6 +3,18 @@

  #if NTLM
Quoted 13 lines

+/* 64bit datatype macros */
+#ifdef _MSC_VER
+ /* MS compilers */
+#define UINTEGER64 __int64
+#define UINT64(c) c ## Ui64
+#else
+ /* Non MS compilers */
+#define UINTEGER64 unsigned long long
+#define UINT64(c) c ## LL
+#endif
+

Quoted 8 lines

Why not use long long for both platforms, it is already used in common.h... Also you can use ull suffix for both platforms.

Anyway... this belongs to the .c file, it is not used by any other files. But I don't think it is required at all.

Alon.

Some Microsoft compilers doesn't support long long, but they support __int64. The same is with LL/Ui64 postfix. I think that this is correct. But I'm not sure about the location. Maybe that it should be placed in some common header (common.h?) for further usage by other modules.

Thanks
Miroslav Zajic
NextSoft s.r.o.

#9 lore raw
Quoted 6 lines · Mirek Zajic
Quoted 4 lines

Just noticed that openvpn has: openvpn_gettimeofday() which works also for WIN32... Maybe use this one?

openvpn_time works fine, why to change?

I remember you used gettimeofday in previous versions... I missed the openvpn_gettimeofday() availability... Maybe the protocol do require more precise timing... I truly don't know.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

+ msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos & 0xff00) >> 8);

always & such with 0xff.

Please explain, I don't understand

msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos >> 8) & 0xff);

Quoted 4 lines

Some Microsoft compilers doesn't support long long, but they support
__int64. The same is with LL/Ui64 postfix. I think that this is correct. But I'm not sure about the location. Maybe that it should be placed in some common header (common.h?) for further usage by other modules.

This is James call... But currently OpenVPN cannot be compiled on these compilers anyway... So adding these macros is not required.

Anyway... This is about as far as I can help.

Alon.

#10 lore raw

I'm sending NTLMv2 patch again. I hope that everything is correct now :-) Again thanks to Alon for his feedback.

Miroslav Zajic
NextSoft s.r.o.

P.S. I moved int64 macros to ntlm.c. I left them unchanged, becouse they could be useful in future.

ntlm.c 2008-03-13 15:29:31.109375000 +0100 renamed from ntlm.c 2008-01-13 21:25:52.915264000 +0100 +2 -0
diff -Naur openvpn-2.1/ntlm.c openvpn-NTLMv2-predelane-2.1/ntlm.c
@@ -3,6 +3,8 @@
 *
 *  Copyright (C) 2004 William Preston
 *
+ *  *NTLMv2 support and domain name parsing by Miroslav Zajic, NextSoft s.r.o.*
+ *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
@@ -41,6 +43,21 @@
 

#include "memdbg.h"

+
+/* 64bit datatype macros */
+#ifdef _MSC_VER
+ /* MS compilers */
+# define UINTEGER64 __int64
+# define UINT64(c) c ## Ui64
+#else
+ /* Non MS compilers */
+# define UINTEGER64 unsigned long long
+# define UINT64(c) c ## LL
+#endif
+
+
+
+
static void
create_des_keys(const unsigned char *hash, unsigned char *key) {
@@ -70,6 +87,61 @@
memcpy (result, md, 16);
}

+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int key_len,char *result) +{
+ unsigned int len;
+

+    HMAC_CTX c;
+    HMAC_Init (&c, key, key_len, EVP_md5());
+    HMAC_Update (&c, data, data_len);
+    HMAC_Final (&c, result, &len);
+    HMAC_CTX_cleanup(&c);

+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{
+ /* Copies 8 bytes long timestamp into "timestamp" buffer.
+ * Timestamp is Little-endian, 64-bit signed value representing the number of tenths of a microsecond since January 1, 1601.
+ */
+
+ UINTEGER64 timestamp_ull;
+
+ timestamp_ull = openvpn_time(NULL);
+ timestamp_ull = (timestamp_ull + UINT64(11644473600)) * UINT64(10000000);
+

+    /* store little endian value */
+    timestamp[0]= timestamp_ull & UINT64(0xFF);
+    timestamp[1]= (timestamp_ull  >> 8)  & UINT64(0xFF);
+    timestamp[2]= (timestamp_ull  >> 16) & UINT64(0xFF);
+    timestamp[3]= (timestamp_ull  >> 24) & UINT64(0xFF);
+    timestamp[4]= (timestamp_ull  >> 32) & UINT64(0xFF);
+    timestamp[5]= (timestamp_ull  >> 40) & UINT64(0xFF);
+    timestamp[6]= (timestamp_ull  >> 48) & UINT64(0xFF);
+    timestamp[7]= (timestamp_ull  >> 56) & UINT64(0xFF);

+}
+
+static void
+gen_nonce (unsigned char *nonce)
+{
+ /* Generates 8 random bytes to be used as client nonce */
+ int i;
+
+ for(i=0;i<8;i++){
+ nonce[i] = (unsigned char)get_random();
+ }
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{
+ /* converts string to uppercase in place */
+ unsigned char *tmp = str;;
+
+ do *str = toupper(*str); while (*(++str));
+ return tmp;
+}
+
static int
unicodize (char *dst, const char *src)
{
@@ -85,6 +157,18 @@
return i;
}

+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned char *msg_buf, int *msg_bufpos) +{

+    /* Adds security buffer data to a message and sets security buffer's offset and length */
+    msg_buf[sb_offset] = (unsigned char)length;
+    msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+    msg_buf[sb_offset + 4] = (unsigned char)(*msg_bufpos & 0xff);
+    msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos >> 8) & 0xff);
+    memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+    *msg_bufpos += length;

+}
+
const char *
ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc)
{
@@ -105,23 +189,56 @@
const char *
ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc) {
+ /* NTLM handshake

+     *
+     * http://davenport.sourceforge.net/ntlm.html
+     *
+     */

+
char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */ char buf2[128]; /* decoded reply from proxy */
- char phase3[146];
+ unsigned char phase3[464];

char md4_hash[21];

-  char challenge[8], response[24];
-  int i, ret_val, buflen;
+  char challenge[8], ntlm_response[24];
+  int i, ret_val;

des_cblock key1, key2, key3;
des_key_schedule sched1, sched2, sched3;

- /* try a minimal NTLM handshake

-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   */
+    char ntlmv2_response[144];
+    char userdomain_u[256]; /* for uppercase unicode username and domain */
+    char userdomain[128];   /* the same as previous but ascii */
+    char ntlmv2_hash[16];
+    char ntlmv2_hmacmd5[16];
+    char *ntlmv2_blob = ntlmv2_response + 16; /* inside ntlmv2_response, length: 128 */
+    int ntlmv2_blob_size=0;
+    int phase3_bufpos = 0x40; /* offset to next security buffer data to be added */
+    int len;
+
+    char domain[128];
+    char username[128];
+    char *separator;

+
+ bool ntlmv2_enabled = (p->auth_method == HTTP_AUTH_NTLM2);
+
ASSERT (strlen (p->up.username) > 0);
ASSERT (strlen (p->up.password) > 0);
+ + /* username parsing */
+ separator = strchr(p->up.username, '\\');
+ if (separator == NULL) {

+        strncpy(username, p->up.username, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        domain[0]=0;
+    } else {
+        strncpy(username, separator+1, sizeof(username)-1);
+        username[sizeof(username)-1]=0;
+        len = separator - p->up.username;
+        if (len > sizeof(domain) - 1) len = sizeof(domain) - 1;
+        strncpy(domain, p->up.username,  len);
+        domain[len]=0;

+ }
+

/* fill 1st 16 bytes with md4 hash, disregard terminating null */ gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash);
@@ -139,48 +256,95 @@

    challenge[i] = buf2[i+24];

}

-  create_des_keys ((unsigned char *)md4_hash, key1);
-  des_set_key_unchecked ((des_cblock *)key1, sched1);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)response, sched1, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
-  des_set_key_unchecked ((des_cblock *)key2, sched2);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[8]), sched2, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
-  des_set_key_unchecked ((des_cblock *)key3, sched3);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[16]), sched3, DES_ENCRYPT);

-
- /* clear reply */
- memset (phase3, 0, sizeof (phase3));
-
- strcpy (phase3, "NTLMSSP\0");
- phase3[8] = 3; /* type 3 */
-
- buflen = 0x58 + strlen (p->up.username);
- if (buflen > (int) sizeof (phase3))
- buflen = sizeof (phase3);
-

-  phase3[0x10] = buflen; /* lm not used */
-  phase3[0x20] = buflen; /* default domain (i.e. proxy's domain) */
-  phase3[0x30] = buflen; /* no workstation name supplied */
-  phase3[0x38] = buflen; /* no session key */
-
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);

-
+ if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+ + /* NTLMv2 hash */
+ my_strupr(strcpy(userdomain, username));
+ if (strlen(username) + strlen(domain) < sizeof(userdomain))
+ strcat(userdomain, domain);
+ else
+ msg (M_INFO, "Warning: Username or domain too long");
+ unicodize (userdomain_u, userdomain);
+ gen_hmac_md5(userdomain_u, 2 * strlen(userdomain), md4_hash, 16, ntlmv2_hash);
+

+        /* NTLMv2 Blob */
+        memset(ntlmv2_blob, 0, 128);                /* Clear blob buffer */
+        ntlmv2_blob[0x00]=1;                        /* Signature */
+        ntlmv2_blob[0x01]=1;                        /* Signature */
+        ntlmv2_blob[0x04]=0;                        /* Reserved */
+        gen_timestamp(&ntlmv2_blob[0x08]);          /* 64-bit Timestamp */
+        gen_nonce(&ntlmv2_blob[0x10]);              /* 64-bit Client Nonce */
+        ntlmv2_blob[0x18]=0;                        /* Unknown, zero should work */
+
+        /* Add target information block to the blob */
+        int tib_len;
+        if (( *((long *)&buf2[0x14]) & 0x00800000) == 0x00800000){ /* Check for Target Information block */
+            tib_len = buf2[0x28];/* Get Target Information block size */
+            if (tib_len > 96) tib_len = 96;
+            char *tib_ptr = buf2 + buf2[0x2c]; /* Get Target Information block pointer */
+            memcpy(&ntlmv2_blob[0x1c], tib_ptr, tib_len); /* Copy Target Information block into the blob */

+ } else {
+ tib_len = 0;
+ }
+
+ ntlmv2_blob[0x1c + tib_len] = 0; /* Unknown, zero works */
+
+ /* Get blob length */
+ ntlmv2_blob_size = 0x20 + tib_len;
+
+ /* Add challenge from message 2 */
+ memcpy(&ntlmv2_response[8], challenge, 8);
+
+ /* hmac-md5 */
+ gen_hmac_md5(&ntlmv2_response[8], ntlmv2_blob_size + 8, ntlmv2_hash, 16, ntlmv2_hmacmd5);
+ + /* Add hmac-md5 result to the blob */
+ memcpy(ntlmv2_response, ntlmv2_hmacmd5, 16); /* Note: This overwrites challenge previously written at ntlmv2_response[8..15] */
+ + } else { /* Generate NTLM response */
+

+        create_des_keys ((unsigned char *)md4_hash, key1);
+        des_set_key_unchecked ((des_cblock *)key1, sched1);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)ntlm_response, sched1, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
+        des_set_key_unchecked ((des_cblock *)key2, sched2);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[8]), sched2, DES_ENCRYPT);
+
+        create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
+        des_set_key_unchecked ((des_cblock *)key3, sched3);
+        des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[16]), sched3, DES_ENCRYPT);

+ }
+ + + memset (phase3, 0, sizeof (phase3)); /* clear reply */
+
+ strcpy (phase3, "NTLMSSP\0"); /* signature */
+ phase3[8] = 3; /* type 3 */
+
+ if (ntlmv2_enabled){ /* NTLMv2 response */
+ add_security_buffer(0x14, ntlmv2_response, ntlmv2_blob_size + 16, phase3, &phase3_bufpos);
+ }else{ /* NTLM response */
+ add_security_buffer(0x14, ntlm_response, 24, phase3, &phase3_bufpos);
+ }
+ + /* username in ascii */
+ add_security_buffer(0x24, username, strlen (username), phase3, &phase3_bufpos);
+
+ /* Set domain. If <domain> is empty, default domain will be used (i.e. proxy's domain) */
+ add_security_buffer(0x1c, domain, strlen (domain), phase3, &phase3_bufpos);
+ +

+    /* other security buffers will be empty */
+    phase3[0x10] = phase3_bufpos; /* lm not used */
+    phase3[0x30] = phase3_bufpos; /* no workstation name supplied */
+    phase3[0x38] = phase3_bufpos; /* no session key */

+ + /* flags */
phase3[0x3c] = 0x02; /* negotiate oem */
phase3[0x3d] = 0x02; /* negotiate ntlm */

- return ((const char *)make_base64_string2 ((unsigned char *)phase3, buflen, gc));
+ return ((const char *)make_base64_string2 ((unsigned char *)phase3, phase3_bufpos, gc)); }

#else

proxy.c 2008-01-15 11:46:12.737156800 +0100 renamed from proxy.c 2008-01-13 21:25:52.945307200 +0100 +5 -3
diff -Naur openvpn-2.1/proxy.c openvpn-NTLMv2-predelane-2.1/proxy.c
@@ -294,19 +294,21 @@
    p->auth_method = HTTP_AUTH_BASIC;
      else if (!strcmp (o->auth_method_string, "ntlm"))
    p->auth_method = HTTP_AUTH_NTLM;
+      else if (!strcmp (o->auth_method_string, "ntlm2"))
+    p->auth_method = HTTP_AUTH_NTLM2;
      else
-    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', or 'ntlm' methods are currently supported",
+    msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', 'ntlm', or 'ntlm2' methods are currently supported",
         o->auth_method_string);
    }
 
-  /* only basic and NTLM authentication supported so far */
-  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM)
+  /* only basic and NTLM/NTLMv2 authentication supported so far */
+  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
    {
      get_user_pass_http (p, true);
    }
 

#if !NTLM
- if (p->auth_method == HTTP_AUTH_NTLM)
+ if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2) msg (M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built
without NTLM Proxy support.");
#endif

@@ -374,6 +376,12 @@

#if NTLM
case HTTP_AUTH_NTLM:
+ case HTTP_AUTH_NTLM2:

+      /* keep-alive connection */
+      openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+      if (!send_line_crlf (sd, buf))
+    goto error;
+
      openvpn_snprintf (buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
            ntlm_phase_1 (p, &gc));
      msg (D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
@@ -411,7 +419,7 @@
      msg (D_PROXY, "Proxy requires authentication");

      /* check for NTLM */
-      if (p->auth_method == HTTP_AUTH_NTLM)
+      if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
        {
#if NTLM
          /* look for the phase 2 response */
@@ -456,6 +464,12 @@
          if (!send_line_crlf (sd, buf))
            goto error;
+          /* keep-alive connection */
+          openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+          if (!send_line_crlf (sd, buf))
+            goto error;
+
+         
          /* send HOST etc, */
          openvpn_sleep (1);
          openvpn_snprintf (buf, sizeof(buf), "Host: %s", host);
proxy.h 2008-01-15 11:25:00.097190400 +0100 renamed from proxy.h 2008-01-13 21:25:52.975350400 +0100 +0 -0
diff -Naur openvpn-2.1/proxy.h openvpn-NTLMv2-predelane-2.1/proxy.h
@@ -59,6 +59,7 @@

#define HTTP_AUTH_BASIC 1
#define HTTP_AUTH_NTLM 2
#define HTTP_AUTH_N 3
+#define HTTP_AUTH_NTLM2 4

struct http_proxy_options {
const char *server;

#11 lore raw
Quoted 10 lines · Mirek Zajic

I'm sending NTLMv2 patch again. I hope that everything is correct now :-) Again thanks to Alon for his feedback.

Miroslav Zajic
NextSoft s.r.o.

P.S. I moved int64 macros to ntlm.c. I left them unchanged, becouse they could be useful in future.

Mirek, can you send this patch as an attachment please? It's broken up with line-breaks.

Thanks,
James

#12 lore raw

Sending the patch as an attachment.

Miroslav Zajic
NextSoft s.r.o.

James Yonan napsal(a):

Quoted 12 lines
Quoted 10 lines · Mirek Zajic

I'm sending NTLMv2 patch again. I hope that everything is correct now :-) Again thanks to Alon for his feedback.

Miroslav Zajic
NextSoft s.r.o.

P.S. I moved int64 macros to ntlm.c. I left them unchanged, becouse they could be useful in future.

Quoted 6 lines

Mirek, can you send this patch as an attachment please? It's broken up with line-breaks.

Thanks,
James

ntlm.c +211 -47
diff -Naur openvpn-2.1/ntlm.c openvpn-NTLMv2-predelane-2.1/ntlm.c
@@ -3,6 +3,8 @@
  *
  *  Copyright (C) 2004 William Preston
  *
+ *  *NTLMv2 support and domain name parsing by Miroslav Zajic, Nextsoft s.r.o.*
+ *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
@@ -41,6 +43,21 @@
 
 #include "memdbg.h"
 
+
+/* 64bit datatype macros */
+#ifdef _MSC_VER 
+	/* MS compilers */
+#	define UINTEGER64 __int64
+#	define UINT64(c) c ## Ui64
+#else 
+	/* Non MS compilers */
+#	define UINTEGER64 unsigned long long
+#	define UINT64(c) c ## LL
+#endif
+
+
+
+
 static void
 create_des_keys(const unsigned char *hash, unsigned char *key)
 {
@@ -70,6 +87,61 @@
   memcpy (result, md, 16);
 }
 
+static void
+gen_hmac_md5 (const char* data, int data_len, const char* key, int key_len,char *result)
+{
+	unsigned int len;
+
+	HMAC_CTX c;
+	HMAC_Init (&c, key, key_len, EVP_md5());
+	HMAC_Update (&c, data, data_len);
+	HMAC_Final (&c, result, &len);
+	HMAC_CTX_cleanup(&c);
+}
+
+static void
+gen_timestamp (unsigned char *timestamp)
+{ 
+	/* Copies 8 bytes long timestamp into "timestamp" buffer. 
+	 * Timestamp is Little-endian, 64-bit signed value representing the number of tenths of a microsecond since January 1, 1601.
+	 */
+
+	UINTEGER64 timestamp_ull;
+
+	timestamp_ull = openvpn_time(NULL);
+	timestamp_ull = (timestamp_ull + UINT64(11644473600)) * UINT64(10000000);
+
+	/* store little endian value */
+	timestamp[0]= timestamp_ull & UINT64(0xFF);
+	timestamp[1]= (timestamp_ull  >> 8)  & UINT64(0xFF);
+	timestamp[2]= (timestamp_ull  >> 16) & UINT64(0xFF);
+	timestamp[3]= (timestamp_ull  >> 24) & UINT64(0xFF);
+	timestamp[4]= (timestamp_ull  >> 32) & UINT64(0xFF);
+	timestamp[5]= (timestamp_ull  >> 40) & UINT64(0xFF);
+	timestamp[6]= (timestamp_ull  >> 48) & UINT64(0xFF);
+	timestamp[7]= (timestamp_ull  >> 56) & UINT64(0xFF);
+}
+
+static void
+gen_nonce (unsigned char *nonce)
+{ 
+	/* Generates 8 random bytes to be used as client nonce */
+	int i;
+
+	for(i=0;i<8;i++){
+		nonce[i] = (unsigned char)get_random();
+	}
+}
+
+unsigned char *my_strupr(unsigned char *str)
+{ 
+	/* converts string to uppercase in place */
+	unsigned char *tmp = str;;
+
+	do *str = toupper(*str); while (*(++str));
+	return tmp;
+}
+
 static int
 unicodize (char *dst, const char *src)
 {
@@ -85,6 +157,18 @@
   return i;
 }
 
+static void
+add_security_buffer(int sb_offset, void *data, int length, unsigned char *msg_buf, int *msg_bufpos)
+{
+	/* Adds security buffer data to a message and sets security buffer's offset and length */
+	msg_buf[sb_offset] = (unsigned char)length;
+	msg_buf[sb_offset + 2] = msg_buf[sb_offset];
+	msg_buf[sb_offset + 4] = (unsigned char)(*msg_bufpos & 0xff);
+	msg_buf[sb_offset + 5] = (unsigned char)((*msg_bufpos >> 8) & 0xff);
+	memcpy(&msg_buf[*msg_bufpos], data, msg_buf[sb_offset]);
+	*msg_bufpos += length;
+}
+
 const char *
 ntlm_phase_1 (const struct http_proxy_info *p, struct gc_arena *gc)
 {
@@ -105,23 +189,56 @@
 const char *
 ntlm_phase_3 (const struct http_proxy_info *p, const char *phase_2, struct gc_arena *gc)
 {
+	/* NTLM handshake
+	 *
+	 * http://davenport.sourceforge.net/ntlm.html
+	 *
+	 */
+	
   char pwbuf[sizeof (p->up.password) * 2]; /* for unicode password */
   char buf2[128]; /* decoded reply from proxy */
-  char phase3[146];
+  unsigned char phase3[464];
 
   char md4_hash[21];
-  char challenge[8], response[24];
-  int i, ret_val, buflen;
+  char challenge[8], ntlm_response[24];
+  int i, ret_val;
   des_cblock key1, key2, key3;
   des_key_schedule sched1, sched2, sched3;
 
-  /* try a minimal NTLM handshake
-   *
-   * http://davenport.sourceforge.net/ntlm.html
-   *
-   */
+	char ntlmv2_response[144];
+	char userdomain_u[256]; /* for uppercase unicode username and domain */
+	char userdomain[128];   /* the same as previous but ascii */
+	char ntlmv2_hash[16];
+	char ntlmv2_hmacmd5[16];
+	char *ntlmv2_blob = ntlmv2_response + 16; /* inside ntlmv2_response, length: 128 */
+	int ntlmv2_blob_size=0;
+	int phase3_bufpos = 0x40; /* offset to next security buffer data to be added */
+	int len;
+
+	char domain[128];
+	char username[128];
+	char *separator;
+
+	bool ntlmv2_enabled = (p->auth_method == HTTP_AUTH_NTLM2);
+
   ASSERT (strlen (p->up.username) > 0);
   ASSERT (strlen (p->up.password) > 0);
+	
+	/* username parsing */
+	separator = strchr(p->up.username, '\\');
+	if (separator == NULL) {
+		strncpy(username, p->up.username, sizeof(username)-1);
+		username[sizeof(username)-1]=0;
+		domain[0]=0;
+	} else {
+		strncpy(username, separator+1, sizeof(username)-1);
+		username[sizeof(username)-1]=0;
+		len = separator - p->up.username;
+		if (len > sizeof(domain) - 1) len = sizeof(domain) - 1;
+		strncpy(domain, p->up.username,  len);
+		domain[len]=0;
+	}
+
 
   /* fill 1st 16 bytes with md4 hash, disregard terminating null */
   gen_md4_hash (pwbuf, unicodize (pwbuf, p->up.password) - 2, md4_hash);
@@ -139,48 +256,95 @@
     challenge[i] = buf2[i+24];
   }
 
-  create_des_keys ((unsigned char *)md4_hash, key1);
-  des_set_key_unchecked ((des_cblock *)key1, sched1);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)response, sched1, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
-  des_set_key_unchecked ((des_cblock *)key2, sched2);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[8]), sched2, DES_ENCRYPT);
-
-  create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
-  des_set_key_unchecked ((des_cblock *)key3, sched3);
-  des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(response[16]), sched3, DES_ENCRYPT);
-
-  /* clear reply */
-  memset (phase3, 0, sizeof (phase3));
-
-  strcpy (phase3, "NTLMSSP\0");
-  phase3[8] = 3; /* type 3 */
-
-  buflen = 0x58 + strlen (p->up.username);
-  if (buflen > (int) sizeof (phase3))
-    buflen = sizeof (phase3);
-
-  phase3[0x10] = buflen; /* lm not used */
-  phase3[0x20] = buflen; /* default domain (i.e. proxy's domain) */
-  phase3[0x30] = buflen; /* no workstation name supplied */
-  phase3[0x38] = buflen; /* no session key */
-
-  phase3[0x14] = 24; /* ntlm response is 24 bytes long */
-  phase3[0x16] = phase3[0x14];
-  phase3[0x18] = 0x40; /* ntlm offset */
-  memcpy (&(phase3[0x40]), response, 24);
-
-
-  phase3[0x24] = strlen (p->up.username); /* username in ascii */
-  phase3[0x26] = phase3[0x24];
-  phase3[0x28] = 0x58;
-  strncpy (&(phase3[0x58]), p->up.username, sizeof (phase3) - 0x58);
-
+	if (ntlmv2_enabled){ /* Generate NTLMv2 response */
+		
+		/* NTLMv2 hash */
+		my_strupr(strcpy(userdomain, username));
+		if (strlen(username) + strlen(domain) < sizeof(userdomain))
+			strcat(userdomain, domain);
+		else
+			msg (M_INFO, "Warning: Username or domain too long");
+		unicodize (userdomain_u, userdomain);
+		gen_hmac_md5(userdomain_u, 2 * strlen(userdomain), md4_hash, 16, ntlmv2_hash);
+
+		/* NTLMv2 Blob */
+		memset(ntlmv2_blob, 0, 128);                /* Clear blob buffer */ 
+		ntlmv2_blob[0x00]=1;                        /* Signature */
+		ntlmv2_blob[0x01]=1;                        /* Signature */
+		ntlmv2_blob[0x04]=0;                        /* Reserved */
+		gen_timestamp(&ntlmv2_blob[0x08]);          /* 64-bit Timestamp */
+		gen_nonce(&ntlmv2_blob[0x10]);              /* 64-bit Client Nonce */
+		ntlmv2_blob[0x18]=0;                        /* Unknown, zero should work */
+
+		/* Add target information block to the blob */
+		int tib_len;
+		if (( *((long *)&buf2[0x14]) & 0x00800000) == 0x00800000){ /* Check for Target Information block */
+			tib_len = buf2[0x28];/* Get Target Information block size */
+			if (tib_len > 96) tib_len = 96;
+			char *tib_ptr = buf2 + buf2[0x2c]; /* Get Target Information block pointer */
+			memcpy(&ntlmv2_blob[0x1c], tib_ptr, tib_len); /* Copy Target Information block into the blob */
+		} else {
+			tib_len = 0;
+		}
+
+		ntlmv2_blob[0x1c + tib_len] = 0;            /* Unknown, zero works */ 
+
+		/* Get blob length */
+		ntlmv2_blob_size = 0x20 + tib_len; 
+
+		/* Add challenge from message 2 */
+		memcpy(&ntlmv2_response[8], challenge, 8);
+
+		/* hmac-md5 */
+		gen_hmac_md5(&ntlmv2_response[8], ntlmv2_blob_size + 8, ntlmv2_hash, 16, ntlmv2_hmacmd5);
+		
+		/* Add hmac-md5 result to the blob */
+		memcpy(ntlmv2_response, ntlmv2_hmacmd5, 16); /* Note: This overwrites challenge previously written at ntlmv2_response[8..15] */
+	
+	} else { /* Generate NTLM response */
+
+		create_des_keys ((unsigned char *)md4_hash, key1);
+		des_set_key_unchecked ((des_cblock *)key1, sched1);
+		des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)ntlm_response, sched1, DES_ENCRYPT);
+
+		create_des_keys ((unsigned char *)&(md4_hash[7]), key2);
+		des_set_key_unchecked ((des_cblock *)key2, sched2);
+		des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[8]), sched2, DES_ENCRYPT);
+
+		create_des_keys ((unsigned char *)&(md4_hash[14]), key3);
+		des_set_key_unchecked ((des_cblock *)key3, sched3);
+		des_ecb_encrypt ((des_cblock *)challenge, (des_cblock *)&(ntlm_response[16]), sched3, DES_ENCRYPT);
+	}
+	
+	
+	memset (phase3, 0, sizeof (phase3)); /* clear reply */
+
+	strcpy (phase3, "NTLMSSP\0"); /* signature */
+	phase3[8] = 3; /* type 3 */
+
+	if (ntlmv2_enabled){ /* NTLMv2 response */
+		add_security_buffer(0x14, ntlmv2_response, ntlmv2_blob_size + 16, phase3, &phase3_bufpos);
+	}else{ /* NTLM response */
+		add_security_buffer(0x14, ntlm_response, 24, phase3, &phase3_bufpos);
+	}
+	
+	/* username in ascii */
+	add_security_buffer(0x24, username, strlen (username), phase3, &phase3_bufpos);
+
+	/* Set domain. If <domain> is empty, default domain will be used (i.e. proxy's domain) */ 
+	add_security_buffer(0x1c, domain, strlen (domain), phase3, &phase3_bufpos);
+	
+
+	/* other security buffers will be empty */
+	phase3[0x10] = phase3_bufpos; /* lm not used */
+	phase3[0x30] = phase3_bufpos; /* no workstation name supplied */
+	phase3[0x38] = phase3_bufpos; /* no session key */
+	
+	/* flags */
   phase3[0x3c] = 0x02; /* negotiate oem */
   phase3[0x3d] = 0x02; /* negotiate ntlm */
 
-  return ((const char *)make_base64_string2 ((unsigned char *)phase3, buflen, gc));
+  return ((const char *)make_base64_string2 ((unsigned char *)phase3, phase3_bufpos, gc));
 }
 
 #else
proxy.c +19 -5
diff -Naur openvpn-2.1/proxy.c openvpn-NTLMv2-predelane-2.1/proxy.c
@@ -294,19 +294,21 @@
 	p->auth_method = HTTP_AUTH_BASIC;
       else if (!strcmp (o->auth_method_string, "ntlm"))
 	p->auth_method = HTTP_AUTH_NTLM;
+      else if (!strcmp (o->auth_method_string, "ntlm2"))
+	p->auth_method = HTTP_AUTH_NTLM2;
       else
-	msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', or 'ntlm' methods are currently supported",
+	msg (M_FATAL, "ERROR: unknown HTTP authentication method: '%s' -- only the 'none', 'basic', 'ntlm', or 'ntlm2' methods are currently supported",
 	     o->auth_method_string);
     }
 
-  /* only basic and NTLM authentication supported so far */
-  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM)
+  /* only basic and NTLM/NTLMv2 authentication supported so far */
+  if (p->auth_method == HTTP_AUTH_BASIC || p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
     {
       get_user_pass_http (p, true);
     }
 
 #if !NTLM
-  if (p->auth_method == HTTP_AUTH_NTLM)
+  if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
     msg (M_FATAL, "Sorry, this version of " PACKAGE_NAME " was built without NTLM Proxy support.");
 #endif
 
@@ -374,6 +376,12 @@
 
 #if NTLM
     case HTTP_AUTH_NTLM:
+    case HTTP_AUTH_NTLM2:
+      /* keep-alive connection */
+      openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+      if (!send_line_crlf (sd, buf))
+	goto error;
+
       openvpn_snprintf (buf, sizeof(buf), "Proxy-Authorization: NTLM %s",
 			ntlm_phase_1 (p, &gc));
       msg (D_PROXY, "Attempting NTLM Proxy-Authorization phase 1");
@@ -411,7 +419,7 @@
       msg (D_PROXY, "Proxy requires authentication");
 
       /* check for NTLM */
-      if (p->auth_method == HTTP_AUTH_NTLM)
+      if (p->auth_method == HTTP_AUTH_NTLM || p->auth_method == HTTP_AUTH_NTLM2)
         {
 #if NTLM
           /* look for the phase 2 response */
@@ -456,6 +464,12 @@
           if (!send_line_crlf (sd, buf))
             goto error;
 
+          /* keep-alive connection */
+          openvpn_snprintf (buf, sizeof(buf), "Proxy-Connection: Keep-Alive");
+          if (!send_line_crlf (sd, buf))
+            goto error;
+
+          
           /* send HOST etc, */
           openvpn_sleep (1);
           openvpn_snprintf (buf, sizeof(buf), "Host: %s", host);
proxy.h +1 -0
diff -Naur openvpn-2.1/proxy.h openvpn-NTLMv2-predelane-2.1/proxy.h
@@ -59,6 +59,7 @@
 #define HTTP_AUTH_BASIC 1
 #define HTTP_AUTH_NTLM  2
 #define HTTP_AUTH_N     3
+#define HTTP_AUTH_NTLM2 4
 
 struct http_proxy_options {
   const char *server;