145 lines
4.4 KiB
C#
145 lines
4.4 KiB
C#
// Decompiled with JetBrains decompiler
|
|
// Type: Rei.Fs.Webdav.WsseClient
|
|
// Assembly: Rei.Fs.Webdav, Version=1.13.2.18288, Culture=neutral, PublicKeyToken=null
|
|
// MVID: D30DD1E3-8520-48B5-AAE5-C87970350A82
|
|
// Assembly location: F:\Eigene Dateien\Dropbox\portable Collection\Progs\CarotDAV\Rei.Fs.Webdav.dll
|
|
|
|
using Microsoft.VisualBasic.CompilerServices;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Rei.Fs.Webdav
|
|
{
|
|
public class WsseClient : IAuthenticationModule
|
|
{
|
|
private static RNGCryptoServiceProvider Rnd = new RNGCryptoServiceProvider();
|
|
internal const string AuthType = "WSSE";
|
|
internal const string Signature = "WSSE";
|
|
internal const int NonceLength = 16;
|
|
private static byte[] AsciiHex;
|
|
|
|
public Authorization Authenticate(string challenge, WebRequest request, ICredentials credentials)
|
|
{
|
|
if (!WsseClient.ContainsSignatureInChallenge(challenge, "WSSE"))
|
|
return (Authorization) null;
|
|
return this.InnerAuthenticate(request, credentials);
|
|
}
|
|
|
|
public string AuthenticationType
|
|
{
|
|
get
|
|
{
|
|
return "WSSE";
|
|
}
|
|
}
|
|
|
|
public bool CanPreAuthenticate
|
|
{
|
|
get
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public Authorization PreAuthenticate(WebRequest request, ICredentials credentials)
|
|
{
|
|
return this.InnerAuthenticate(request, credentials);
|
|
}
|
|
|
|
private Authorization InnerAuthenticate(WebRequest request, ICredentials credentials)
|
|
{
|
|
Authorization authorization;
|
|
try
|
|
{
|
|
if (credentials == null)
|
|
{
|
|
authorization = (Authorization) null;
|
|
}
|
|
else
|
|
{
|
|
NetworkCredential credential = credentials.GetCredential(request.RequestUri, "WSSE");
|
|
if (credential == null)
|
|
{
|
|
authorization = (Authorization) null;
|
|
}
|
|
else
|
|
{
|
|
ICredentialPolicy credentialPolicy = AuthenticationManager.CredentialPolicy;
|
|
if (credentialPolicy != null && !credentialPolicy.ShouldSendCredential(request.RequestUri, request, credential, (IAuthenticationModule) this))
|
|
{
|
|
authorization = (Authorization) null;
|
|
}
|
|
else
|
|
{
|
|
request.Headers.Add("X-WSSE", WsseClient.GetAuthString(credential, Encoding.UTF8));
|
|
authorization = new Authorization("WSSE profile=\"UsernameToken\"", true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ProjectData.SetProjectError(ex);
|
|
authorization = (Authorization) null;
|
|
ProjectData.ClearProjectError();
|
|
}
|
|
return authorization;
|
|
}
|
|
|
|
public static string GetAuthString(NetworkCredential credential, Encoding encoding)
|
|
{
|
|
byte[] nonce = WsseClient.CreateNonce();
|
|
string str = DateTime.Now.ToUniversalTime().ToString(DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern) + "Z";
|
|
byte[] buffer = new byte[checked (nonce.Length + encoding.GetByteCount(str + credential.Password) - 1 + 1)];
|
|
nonce.CopyTo((Array) buffer, 0);
|
|
encoding.GetBytes(str + credential.Password).CopyTo((Array) buffer, nonce.Length);
|
|
string base64String = Convert.ToBase64String(SHA1.Create().ComputeHash(buffer));
|
|
return string.Join("", new string[13]
|
|
{
|
|
"UsernameToken ",
|
|
"Username=\"",
|
|
credential.UserName,
|
|
"\", ",
|
|
"PasswordDigest=\"",
|
|
base64String,
|
|
"\", ",
|
|
"Nonce=\"",
|
|
Convert.ToBase64String(nonce),
|
|
"\", ",
|
|
"Created=\"",
|
|
str,
|
|
"\""
|
|
});
|
|
}
|
|
|
|
private static byte[] CreateNonce()
|
|
{
|
|
byte[] data = new byte[16];
|
|
WsseClient.Rnd.GetBytes(data);
|
|
return data;
|
|
}
|
|
|
|
private static bool ContainsSignatureInChallenge(string challenge, string signature)
|
|
{
|
|
int num1;
|
|
for (int startIndex = 0; startIndex < challenge.Length; startIndex = checked (num1 + 1))
|
|
{
|
|
int num2 = challenge.IndexOf("\"", startIndex);
|
|
if (num2 < 0)
|
|
num2 = challenge.Length;
|
|
if (challenge.Substring(startIndex, checked (num2 - startIndex)).Contains(signature))
|
|
return true;
|
|
if (checked (num2 + 1) >= challenge.Length)
|
|
return false;
|
|
num1 = challenge.IndexOf("\"", checked (num2 + 1));
|
|
if (num1 < 0)
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
}
|