118c71
diff -up libsoup-2.62.3/libsoup/soup-auth-ntlm.c.4 libsoup-2.62.3/libsoup/soup-auth-ntlm.c
118c71
--- libsoup-2.62.3/libsoup/soup-auth-ntlm.c.4	2021-03-12 07:30:03.366088932 +0100
118c71
+++ libsoup-2.62.3/libsoup/soup-auth-ntlm.c	2021-03-12 07:30:25.296043405 +0100
118c71
@@ -12,6 +12,8 @@
118c71
 #include <errno.h>
118c71
 #include <stdlib.h>
118c71
 #include <string.h>
118c71
+#include <stdio.h>
118c71
+
118c71
 #include <glib.h>
118c71
 
118c71
 #include "soup-auth-ntlm.h"
118c71
@@ -26,14 +28,20 @@ static char       *soup_ntlm_request
118c71
 static gboolean    soup_ntlm_parse_challenge   (const char  *challenge,
118c71
 						char       **nonce,
118c71
 						char       **default_domain,
118c71
-						gboolean    *ntlmv2_session);
118c71
-static char       *soup_ntlm_response          (const char  *nonce, 
118c71
+						gboolean    *ntlmv2_session,
118c71
+						gboolean    *negotiate_target,
118c71
+						char		**target_info,
118c71
+						size_t		*target_info_sz);
118c71
+static char       *soup_ntlm_response          (const char  *nonce,
118c71
 						const char  *user,
118c71
 						guchar       nt_hash[21],
118c71
 						guchar       lm_hash[21],
118c71
 						const char  *host, 
118c71
 						const char  *domain,
118c71
-						gboolean     ntlmv2_session);
118c71
+						gboolean     ntlmv2_session,
118c71
+						gboolean     negotiate_target,
118c71
+						const char	*target_info,
118c71
+						size_t		target_info_sz);
118c71
 
118c71
 typedef enum {
118c71
 	SOUP_NTLM_NEW,
118c71
@@ -49,6 +57,9 @@ typedef struct {
118c71
 	char *nonce;
118c71
 	char *response_header;
118c71
 	gboolean ntlmv2_session;
118c71
+	gboolean negotiate_target;
118c71
+	char *target_info;
118c71
+	size_t target_info_sz;
118c71
 } SoupNTLMConnectionState;
118c71
 
118c71
 typedef enum {
118c71
@@ -280,6 +291,7 @@ soup_auth_ntlm_free_connection_state (So
118c71
 
118c71
 	g_free (conn->nonce);
118c71
 	g_free (conn->response_header);
118c71
+	g_free (conn->target_info);
118c71
 	g_slice_free (SoupNTLMConnectionState, conn);
118c71
 }
118c71
 
118c71
@@ -339,7 +351,8 @@ soup_auth_ntlm_update_connection (SoupCo
118c71
 
118c71
 	if (!soup_ntlm_parse_challenge (auth_header + 5, &conn->nonce,
118c71
 					priv->domain ? NULL : &priv->domain,
118c71
-					&conn->ntlmv2_session)) {
118c71
+					&conn->ntlmv2_session, &conn->negotiate_target,
118c71
+					&conn->target_info, &conn->target_info_sz)) {
118c71
 		conn->state = SOUP_NTLM_FAILED;
118c71
 		return FALSE;
118c71
 	}
118c71
@@ -521,7 +534,10 @@ soup_auth_ntlm_get_connection_authorizat
118c71
 						     priv->lm_hash,
118c71
 						     NULL,
118c71
 						     priv->domain,
118c71
-						     conn->ntlmv2_session);
118c71
+						     conn->ntlmv2_session,
118c71
+							 conn->negotiate_target,
118c71
+							 conn->target_info,
118c71
+							 conn->target_info_sz);
118c71
 		}
118c71
 		g_clear_pointer (&conn->nonce, g_free);
118c71
 		conn->state = SOUP_NTLM_SENT_RESPONSE;
118c71
@@ -647,14 +663,20 @@ typedef struct {
118c71
 #define NTLM_CHALLENGE_NONCE_OFFSET         24
118c71
 #define NTLM_CHALLENGE_NONCE_LENGTH          8
118c71
 #define NTLM_CHALLENGE_DOMAIN_STRING_OFFSET 12
118c71
+#define NTLM_CHALLENGE_TARGET_INFORMATION_OFFSET      40
118c71
 
118c71
 #define NTLM_CHALLENGE_FLAGS_OFFSET         20
118c71
 #define NTLM_FLAGS_NEGOTIATE_NTLMV2 0x00080000
118c71
+#define NTLM_FLAGS_NEGOTIATE_TARGET_INFORMATION 0x00800000
118c71
+#define NTLM_FLAGS_REQUEST_TARGET 0x00000004
118c71
 
118c71
 #define NTLM_RESPONSE_HEADER "NTLMSSP\x00\x03\x00\x00\x00"
118c71
 #define NTLM_RESPONSE_FLAGS 0x8201
118c71
+#define NTLM_RESPONSE_TARGET_INFORMATION_OFFSET 44
118c71
 #define NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY 0x00080000
118c71
 
118c71
+#define HMAC_MD5_LENGTH                     16
118c71
+
118c71
 typedef struct {
118c71
         guchar     header[12];
118c71
 
118c71
@@ -686,10 +708,14 @@ static gboolean
118c71
 soup_ntlm_parse_challenge (const char *challenge,
118c71
 			   char      **nonce,
118c71
 			   char      **default_domain,
118c71
-			   gboolean   *ntlmv2_session)
118c71
+			   gboolean   *ntlmv2_session,
118c71
+			   gboolean   *negotiate_target,
118c71
+			   char		**target_info,
118c71
+			   size_t	*target_info_sz)
118c71
 {
118c71
 	gsize clen;
118c71
 	NTLMString domain;
118c71
+	NTLMString target;
118c71
 	guchar *chall;
118c71
 	guint32 flags;
118c71
 
118c71
@@ -703,6 +729,14 @@ soup_ntlm_parse_challenge (const char *c
118c71
 	memcpy (&flags, chall + NTLM_CHALLENGE_FLAGS_OFFSET, sizeof(flags));
118c71
 	flags = GUINT_FROM_LE (flags);
118c71
 	*ntlmv2_session = (flags & NTLM_FLAGS_NEGOTIATE_NTLMV2) ? TRUE : FALSE;
118c71
+	/* To know if NTLMv2 responses should be calculated */
118c71
+	*negotiate_target = (flags & NTLM_FLAGS_NEGOTIATE_TARGET_INFORMATION ) ? TRUE : FALSE;
118c71
+        if (*negotiate_target) {
118c71
+            if (clen < NTLM_CHALLENGE_TARGET_INFORMATION_OFFSET + sizeof (target)) {
118c71
+                g_free (chall);
118c71
+                return FALSE;
118c71
+            }
118c71
+        }
118c71
 
118c71
 	if (default_domain) {
118c71
 		memcpy (&domain, chall + NTLM_CHALLENGE_DOMAIN_STRING_OFFSET, sizeof (domain));
118c71
@@ -723,6 +757,19 @@ soup_ntlm_parse_challenge (const char *c
118c71
 		*nonce = g_memdup (chall + NTLM_CHALLENGE_NONCE_OFFSET,
118c71
 				   NTLM_CHALLENGE_NONCE_LENGTH);
118c71
 	}
118c71
+	/* For NTLMv2 response */
118c71
+	if (*negotiate_target && target_info) {
118c71
+		memcpy (&target, chall + NTLM_CHALLENGE_TARGET_INFORMATION_OFFSET, sizeof (target));
118c71
+		target.length = GUINT16_FROM_LE (target.length);
118c71
+		target.offset = GUINT16_FROM_LE (target.offset);
118c71
+
118c71
+		if (clen < target.length + target.offset) {
118c71
+			g_free (chall);
118c71
+			return FALSE;
118c71
+		}
118c71
+		*target_info = g_memdup (chall + target.offset, target.length);
118c71
+		*target_info_sz = target.length;
118c71
+	}
118c71
 
118c71
 	g_free (chall);
118c71
 	return TRUE;
118c71
@@ -761,6 +808,115 @@ calc_ntlm2_session_response (const char
118c71
 	calc_response (nt_hash, ntlmv2_hash, nt_resp);
118c71
 }
118c71
 
118c71
+/* Compute HMAC-MD5 with Glib function*/
118c71
+static void
118c71
+calc_hmac_md5 (unsigned char *hmac, const guchar *key, gsize key_sz, const guchar *data, gsize data_sz)
118c71
+{
118c71
+	char *hmac_hex, *hex_pos;
118c71
+	size_t count;
118c71
+
118c71
+	hmac_hex = g_compute_hmac_for_data(G_CHECKSUM_MD5, key, key_sz, data, data_sz);
118c71
+	hex_pos = hmac_hex;
118c71
+	for (count = 0; count < HMAC_MD5_LENGTH; count++)
118c71
+	{
118c71
+		/* The 'hh' sscanf format modifier is C99, so we enable it on
118c71
+		 * non-Windows or if __USE_MINGW_ANSI_STDIO is enabled or`
118c71
+		 * if we are building on Visual Studio 2015 or later
118c71
+		 */
118c71
+#if !defined (G_OS_WIN32) || (__USE_MINGW_ANSI_STDIO == 1) || (_MSC_VER >= 1900)
118c71
+		sscanf(hex_pos, "%2hhx", &hmac[count]);
118c71
+#else
118c71
+		unsigned int tmp_hmac;
118c71
+		sscanf(hex_pos, "%2x", &tmp_hmac);
118c71
+		hmac[count] = (guint8)tmp_hmac;
118c71
+#endif
118c71
+
118c71
+		hex_pos += 2;
118c71
+	}
118c71
+	g_free(hmac_hex);
118c71
+}
118c71
+
118c71
+static void
118c71
+calc_ntlmv2_response (const char *user, const char *domain,
118c71
+						const guchar *nt_hash, const gsize nt_hash_sz,
118c71
+						const guchar *nonce,
118c71
+						const char *target_info, size_t target_info_sz,
118c71
+						guchar *lm_resp, size_t lm_resp_sz,
118c71
+						guchar *nt_resp, size_t nt_resp_sz)
118c71
+{
118c71
+	const unsigned char blob_signature[] = {0x01,0x01,0x00,0x00};
118c71
+	const unsigned char blob_reserved[] = {0x00,0x00,0x00,0x00};
118c71
+	gint64 blob_timestamp;
118c71
+	unsigned char client_nonce[8];
118c71
+	const unsigned char blob_unknown[] = {0x00,0x00,0x00,0x00};
118c71
+
118c71
+	unsigned char ntv2_hash[HMAC_MD5_LENGTH];
118c71
+	guchar *nonce_blob, *blob, *p_blob;
118c71
+	unsigned char nonce_blob_hash[HMAC_MD5_LENGTH];
118c71
+	unsigned char nonce_client_nonce[16], nonce_client_nonce_hash[HMAC_MD5_LENGTH];
118c71
+	gchar *user_uppercase, *user_domain, *user_domain_conv;
118c71
+	gsize user_domain_conv_sz;
118c71
+	size_t blob_sz;
118c71
+	int i;
118c71
+
118c71
+	/* create HMAC-MD5 hash of Unicode uppercase username and Unicode domain */
118c71
+	user_uppercase = g_utf8_strup (user, strlen (user));
118c71
+	user_domain = g_strconcat (user_uppercase, domain, NULL);
118c71
+	user_domain_conv = g_convert (user_domain, -1, "UCS-2LE", "UTF-8", NULL, &user_domain_conv_sz, NULL);
118c71
+	calc_hmac_md5 (ntv2_hash, nt_hash, nt_hash_sz, (const guchar *)user_domain_conv, user_domain_conv_sz);
118c71
+	g_free (user_uppercase);
118c71
+	g_free (user_domain);
118c71
+	g_free (user_domain_conv);
118c71
+
118c71
+	/* create random client nonce */
118c71
+	for (i = 0; i < sizeof (client_nonce); i++)
118c71
+	{
118c71
+		client_nonce[i] = g_random_int();
118c71
+	}
118c71
+
118c71
+	/* create timestamp for blob
118c71
+	 * LE, 64-bit signed value, number of tenths of a ms since January 1, 1601.*/
118c71
+	blob_timestamp = GINT64_TO_LE(((unsigned long)time(NULL) + 11644473600) * 10000000);
118c71
+
118c71
+	/* create blob */
118c71
+	blob_sz = sizeof (blob_signature) + sizeof (blob_reserved) +
118c71
+			sizeof (blob_timestamp) + sizeof (client_nonce) +
118c71
+			sizeof (blob_unknown) + target_info_sz;
118c71
+	p_blob = blob = g_malloc (blob_sz);
118c71
+	memset (blob, 0, blob_sz);
118c71
+	memcpy (p_blob, blob_signature, sizeof (blob_signature));
118c71
+	memcpy (p_blob += sizeof (blob_signature), blob_reserved, sizeof (blob_reserved));
118c71
+	memcpy (p_blob += sizeof (blob_reserved), &blob_timestamp, sizeof (blob_timestamp));
118c71
+	memcpy (p_blob += sizeof (blob_timestamp), client_nonce, sizeof (client_nonce));
118c71
+	memcpy (p_blob += sizeof (client_nonce), blob_unknown, sizeof (blob_unknown));
118c71
+	memcpy (p_blob += sizeof (blob_unknown), target_info, target_info_sz);
118c71
+
118c71
+	/* create HMAC-MD5 hash of concatenated nonce and blob */
118c71
+	nonce_blob = g_malloc (NTLM_CHALLENGE_NONCE_LENGTH + blob_sz);
118c71
+	memcpy (nonce_blob, nonce, NTLM_CHALLENGE_NONCE_LENGTH);
118c71
+	memcpy (nonce_blob + NTLM_CHALLENGE_NONCE_LENGTH, blob, blob_sz);
118c71
+	calc_hmac_md5 (nonce_blob_hash, (const guchar *)ntv2_hash, (gsize) sizeof (ntv2_hash), (const guchar *) nonce_blob, (gsize) NTLM_CHALLENGE_NONCE_LENGTH + blob_sz);
118c71
+	g_free (nonce_blob);
118c71
+
118c71
+	/* create NTv2 response */
118c71
+	memset (nt_resp, 0, nt_resp_sz);
118c71
+	memcpy (nt_resp, nonce_blob_hash, sizeof (nonce_blob_hash));
118c71
+	memcpy (nt_resp + sizeof (nonce_blob_hash), blob, blob_sz);
118c71
+
118c71
+	g_free (blob);
118c71
+
118c71
+	/* LMv2
118c71
+	 * create HMAC-MD5 hash of concatenated nonce and client nonce
118c71
+	 */
118c71
+	memcpy (nonce_client_nonce, nonce, NTLM_CHALLENGE_NONCE_LENGTH);
118c71
+	memcpy (nonce_client_nonce + NTLM_CHALLENGE_NONCE_LENGTH, client_nonce, sizeof (client_nonce));
118c71
+	calc_hmac_md5 (nonce_client_nonce_hash, (const guchar *) ntv2_hash, (gsize) sizeof (ntv2_hash), (const guchar *) nonce_client_nonce, (gsize) NTLM_CHALLENGE_NONCE_LENGTH + sizeof (client_nonce));
118c71
+
118c71
+	/* create LMv2 response */
118c71
+	memset (lm_resp, 0, lm_resp_sz);
118c71
+	memcpy (lm_resp, nonce_client_nonce_hash, sizeof (nonce_client_nonce_hash));
118c71
+	memcpy (lm_resp + sizeof (nonce_client_nonce_hash), client_nonce, sizeof (client_nonce));
118c71
+}
118c71
 
118c71
 static char *
118c71
 soup_ntlm_response (const char *nonce, 
118c71
@@ -769,23 +925,45 @@ soup_ntlm_response (const char *nonce,
118c71
 		    guchar      lm_hash[21],
118c71
 		    const char *host, 
118c71
 		    const char *domain,
118c71
-		    gboolean    ntlmv2_session)
118c71
+		    gboolean    ntlmv2_session,
118c71
+		    gboolean    negotiate_target,
118c71
+		    const char	*target_info,
118c71
+		    size_t	target_info_sz)
118c71
 {
118c71
+
118c71
 	int offset;
118c71
-	gsize hlen, dlen, ulen;
118c71
-	guchar lm_resp[24], nt_resp[24];
118c71
+	gsize hlen, dlen, ulen, nt_resp_sz;
118c71
+	guchar lm_resp[24], *nt_resp;
118c71
 	char *user_conv, *host_conv, *domain_conv;
118c71
 	NTLMResponse resp;
118c71
 	char *out, *p;
118c71
 	int state, save;
118c71
 
118c71
-	if (ntlmv2_session) {
118c71
+	if (negotiate_target)
118c71
+	{
118c71
+		/* nonce_blob_hash 16 + blob_signature 4 + blob_reserved 4 +
118c71
+		 * blob_timestamp 8 + client_nonce 8 + blob_unknown 4 +
118c71
+		 * target_info*/
118c71
+		nt_resp_sz = NTLM_RESPONSE_TARGET_INFORMATION_OFFSET + target_info_sz;
118c71
+	} else {
118c71
+		nt_resp_sz = 24;
118c71
+	}
118c71
+	nt_resp = g_malloc (nt_resp_sz);
118c71
+
118c71
+	if (ntlmv2_session && !negotiate_target) {
118c71
 		calc_ntlm2_session_response (nonce, nt_hash, lm_hash,
118c71
 					     lm_resp, sizeof(lm_resp), nt_resp);
118c71
-	} else {
118c71
-		/* Compute a regular response */
118c71
+	} else if (!negotiate_target){
118c71
+		/* Compute a regular NTLMv1 response */
118c71
 		calc_response (nt_hash, (guchar *) nonce, nt_resp);
118c71
 		calc_response (lm_hash, (guchar *) nonce, lm_resp);
118c71
+	} else {
118c71
+		calc_ntlmv2_response (user, domain,
118c71
+					nt_hash, 21,
118c71
+					(guchar *) nonce,
118c71
+					target_info, target_info_sz,
118c71
+					lm_resp, sizeof (lm_resp),
118c71
+					nt_resp, (size_t) nt_resp_sz);
118c71
 	}
118c71
 
118c71
 	memset (&resp, 0, sizeof (resp));
118c71
@@ -793,7 +971,8 @@ soup_ntlm_response (const char *nonce,
118c71
 	resp.flags = GUINT32_TO_LE (NTLM_RESPONSE_FLAGS);
118c71
 	if (ntlmv2_session)
118c71
 		resp.flags |= GUINT32_TO_LE (NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY);
118c71
-
118c71
+	if (negotiate_target)
118c71
+			resp.flags |= GUINT32_TO_LE (NTLM_FLAGS_REQUEST_TARGET);
118c71
 	offset = sizeof (resp);
118c71
 
118c71
 	if (!host)
118c71
@@ -807,10 +986,10 @@ soup_ntlm_response (const char *nonce,
118c71
 	ntlm_set_string (&resp.user, &offset, ulen);
118c71
 	ntlm_set_string (&resp.host, &offset, hlen);
118c71
 	ntlm_set_string (&resp.lm_resp, &offset, sizeof (lm_resp));
118c71
-	ntlm_set_string (&resp.nt_resp, &offset, sizeof (nt_resp));
118c71
+	ntlm_set_string (&resp.nt_resp, &offset, nt_resp_sz);
118c71
 
118c71
 	out = g_malloc (((offset + 3) * 4) / 3 + 6);
118c71
-	strncpy (out, "NTLM ", 5);
118c71
+	memcpy (out, "NTLM ", 5);
118c71
 	p = out + 5;
118c71
 
118c71
 	state = save = 0;
118c71
@@ -825,7 +1004,7 @@ soup_ntlm_response (const char *nonce,
118c71
 				   FALSE, p, &state, &save);
118c71
 	p += g_base64_encode_step (lm_resp, sizeof (lm_resp), 
118c71
 				   FALSE, p, &state, &save);
118c71
-	p += g_base64_encode_step (nt_resp, sizeof (nt_resp), 
118c71
+	p += g_base64_encode_step (nt_resp, nt_resp_sz,
118c71
 				   FALSE, p, &state, &save);
118c71
 	p += g_base64_encode_close (FALSE, p, &state, &save);
118c71
 	*p = '\0';
118c71
@@ -833,6 +1012,7 @@ soup_ntlm_response (const char *nonce,
118c71
 	g_free (domain_conv);
118c71
 	g_free (user_conv);
118c71
 	g_free (host_conv);
118c71
+	g_free (nt_resp);
118c71
 
118c71
 	return out;
118c71
 }