2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00
This commit is contained in:
2024-06-21 16:37:48 +03:00
parent c04fc29810
commit 3a89a108db
49 changed files with 2095 additions and 1373 deletions

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets.HTTP
{
public enum HTTPComposeOption : int
{
AllCalculateLength,
AllDontCalculateLength,
SpecifiedHeadersOnly,
DataOnly
}
}

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets.HTTP
{
public struct HTTPCookie
{
public string Name;
public string Value;
public DateTime Expires;
public string Path;
public bool HttpOnly;
public string Domain;
public HTTPCookie(string name, string value)
{
Name = name;
Value = value;
Path = null;
Expires = DateTime.MinValue;
HttpOnly = false;
Domain = null;
}
public HTTPCookie(string name, string value, DateTime expires)
{
Name = name;
Value = value;
Expires = expires;
HttpOnly = false;
Domain = null;
Path = null;
}
public override string ToString()
{
//Set-Cookie: ckGeneric=CookieBody; expires=Sun, 30-Dec-2001 21:00:00 GMT; domain=.com.au; path=/
//Set-Cookie: SessionID=another; expires=Fri, 29 Jun 2006 20:47:11 UTC; path=/
var cookie = Name + "=" + Value;
if (Expires.Ticks != 0)
cookie += "; expires=" + Expires.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
if (Domain != null)
cookie += "; domain=" + Domain;
if (Path != null)
cookie += "; path=" + Path;
if (HttpOnly)
cookie += "; HttpOnly";
return cookie;
}
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets.HTTP
{
public enum HTTPMethod : byte
{
GET,
POST,
HEAD,
PUT,
DELETE,
OPTIONS,
TRACE,
CONNECT,
UNKNOWN
}
}

View File

@ -32,22 +32,10 @@ using Esiur.Data;
using System.Net;
using System.Text.Json.Serialization;
namespace Esiur.Net.Packets;
namespace Esiur.Net.Packets.HTTP;
public class HTTPRequestPacket : Packet
{
public enum HTTPMethod : byte
{
GET,
POST,
HEAD,
PUT,
DELETE,
OPTIONS,
TRACE,
CONNECT,
UNKNOWN
}
public StringKeyList Query;
public HTTPMethod Method;
@ -59,12 +47,12 @@ public class HTTPRequestPacket : Packet
public StringKeyList Cookies; // String
public string URL; /// With query
public string Filename; /// Without query
//public byte[] PostContents;
public KeyList<string, object> PostForms;
public byte[] Message;
private HTTPMethod getMethod(string method)
private HTTPMethod GetMethod(string method)
{
switch (method.ToLower())
{
@ -127,7 +115,7 @@ public class HTTPRequestPacket : Packet
Headers = new StringKeyList();
sMethod = sLines[0].Split(' ');
Method = getMethod(sMethod[0].Trim());
Method = GetMethod(sMethod[0].Trim());
if (sMethod.Length == 3)
{
@ -163,7 +151,7 @@ public class HTTPRequestPacket : Packet
for (int i = 1; i < sLines.Length; i++)
{
if (sLines[i] == String.Empty)
if (sLines[i] == string.Empty)
{
// Invalid header
return 0;
@ -191,14 +179,14 @@ public class HTTPRequestPacket : Packet
string[] splitCookie = cookie.Split('=');
splitCookie[0] = splitCookie[0].Trim();
splitCookie[1] = splitCookie[1].Trim();
if (!(Cookies.ContainsKey(splitCookie[0].Trim())))
if (!Cookies.ContainsKey(splitCookie[0].Trim()))
Cookies.Add(splitCookie[0], splitCookie[1]);
}
else
{
if (!(Cookies.ContainsKey(cookie.Trim())))
if (!Cookies.ContainsKey(cookie.Trim()))
{
Cookies.Add(cookie.Trim(), String.Empty);
Cookies.Add(cookie.Trim(), string.Empty);
}
}
}
@ -222,7 +210,7 @@ public class HTTPRequestPacket : Packet
}
else
{
if (!(Query.ContainsKey(WebUtility.UrlDecode(S))))
if (!Query.ContainsKey(WebUtility.UrlDecode(S)))
{
Query.Add(WebUtility.UrlDecode(S), null);
}
@ -236,7 +224,7 @@ public class HTTPRequestPacket : Packet
try
{
uint postSize = uint.Parse((string)Headers["content-length"]);
uint postSize = uint.Parse(Headers["content-length"]);
// check limit
if (postSize > data.Length - headerSize)
@ -298,7 +286,7 @@ public class HTTPRequestPacket : Packet
else
{
//PostForms.Add(Headers["content-type"], Encoding.Default.GetString( ));
Message = DC.Clip(data, headerSize, postSize);
Message = data.Clip(headerSize, postSize);
}
return headerSize + postSize;

View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets.HTTP
{
public enum HTTPResponseCode : int
{
Switching = 101,
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
PreconditionFailed = 412,
UnsupportedMediaType = 415,
InternalServerError = 500,
NotImplemented = 501,
}
}

View File

@ -28,98 +28,15 @@ using System.Text;
using Esiur.Misc;
using Esiur.Data;
namespace Esiur.Net.Packets;
namespace Esiur.Net.Packets.HTTP;
public class HTTPResponsePacket : Packet
{
public enum ComposeOptions : int
{
AllCalculateLength,
AllDontCalculateLength,
SpecifiedHeadersOnly,
DataOnly
}
public enum ResponseCode : int
{
Switching = 101,
OK = 200,
Created = 201,
Accepted = 202,
NoContent = 204,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
PreconditionFailed = 412,
UnsupportedMediaType = 415,
InternalServerError = 500,
NotImplemented = 501,
}
public struct HTTPCookie
{
public string Name;
public string Value;
public DateTime Expires;
public string Path;
public bool HttpOnly;
public string Domain;
public HTTPCookie(string name, string value)
{
this.Name = name;
this.Value = value;
this.Path = null;
this.Expires = DateTime.MinValue;
this.HttpOnly = false;
this.Domain = null;
}
public HTTPCookie(string name, string value, DateTime expires)
{
this.Name = name;
this.Value = value;
this.Expires = expires;
this.HttpOnly = false;
this.Domain = null;
this.Path = null;
}
public override string ToString()
{
//Set-Cookie: ckGeneric=CookieBody; expires=Sun, 30-Dec-2001 21:00:00 GMT; domain=.com.au; path=/
//Set-Cookie: SessionID=another; expires=Fri, 29 Jun 2006 20:47:11 UTC; path=/
var cookie = Name + "=" + Value;
if (Expires.Ticks != 0)
cookie += "; expires=" + Expires.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
if (Domain != null)
cookie += "; domain=" + Domain;
if (Path != null)
cookie += "; path=" + Path;
if (HttpOnly)
cookie += "; HttpOnly";
return cookie;
}
}
public StringKeyList Headers { get; } = new StringKeyList(true);
public string Version { get; set; } = "HTTP/1.1";
public byte[] Message;
public ResponseCode Number { get; set; } = ResponseCode.OK;
public HTTPResponseCode Number { get; set; } = HTTPResponseCode.OK;
public string Text;
public List<HTTPCookie> Cookies { get; } = new List<HTTPCookie>();
@ -134,11 +51,11 @@ public class HTTPResponsePacket : Packet
+ "\n\tMessage: " + (Message != null ? Message.Length.ToString() : "NULL");
}
private string MakeHeader(ComposeOptions options)
private string MakeHeader(HTTPComposeOption options)
{
string header = $"{Version} {(int)Number} {Text}\r\nServer: Esiur {Global.Version}\r\nDate: {DateTime.Now.ToUniversalTime().ToString("r")}\r\n";
if (options == ComposeOptions.AllCalculateLength)
if (options == HTTPComposeOption.AllCalculateLength)
Headers["Content-Length"] = Message?.Length.ToString() ?? "0";
foreach (var kv in Headers)
@ -158,16 +75,16 @@ public class HTTPResponsePacket : Packet
}
public bool Compose(ComposeOptions options)
public bool Compose(HTTPComposeOption options)
{
List<byte> msg = new List<byte>();
if (options != ComposeOptions.DataOnly)
if (options != HTTPComposeOption.DataOnly)
{
msg.AddRange(Encoding.UTF8.GetBytes(MakeHeader(options)));
}
if (options != ComposeOptions.SpecifiedHeadersOnly)
if (options != HTTPComposeOption.SpecifiedHeadersOnly)
{
if (Message != null)
msg.AddRange(Message);
@ -180,7 +97,7 @@ public class HTTPResponsePacket : Packet
public override bool Compose()
{
return Compose(ComposeOptions.AllDontCalculateLength);
return Compose(HTTPComposeOption.AllDontCalculateLength);
}
public override long Parse(byte[] data, uint offset, uint ends)
@ -212,7 +129,7 @@ public class HTTPResponsePacket : Packet
if (sMethod.Length == 3)
{
Version = sMethod[0].Trim();
Number = (ResponseCode)(Convert.ToInt32(sMethod[1].Trim()));
Number = (HTTPResponseCode)Convert.ToInt32(sMethod[1].Trim());
Text = sMethod[2];
}
@ -220,7 +137,7 @@ public class HTTPResponsePacket : Packet
for (int i = 1; i < sLines.Length; i++)
{
if (sLines[i] == String.Empty)
if (sLines[i] == string.Empty)
{
// Invalid header
return 0;
@ -279,7 +196,7 @@ public class HTTPResponsePacket : Packet
try
{
uint contentLength = uint.Parse((string)Headers["content-length"]);
uint contentLength = uint.Parse(Headers["content-length"]);
// check limit
if (contentLength > data.Length - headerSize)
@ -287,7 +204,7 @@ public class HTTPResponsePacket : Packet
return contentLength - (data.Length - headerSize);
}
Message = DC.Clip(data, offset, contentLength);
Message = data.Clip(offset, contentLength);
return headerSize + contentLength;

View File

@ -0,0 +1,25 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public static class IIPAuthExtensions
{
public static IIPAuthPacketIAuthFormat GetIAuthFormat(this object value)
{
if (value is string)
return IIPAuthPacketIAuthFormat.Text;
else if (value is int || value is uint
|| value is byte || value is sbyte
|| value is short || value is ushort
|| value is long || value is ulong)
return IIPAuthPacketIAuthFormat.Number;
else if (value.GetType().IsArray)
return IIPAuthPacketIAuthFormat.Choice;
throw new Exception("Unknown IAuth format");
}
}
}

View File

@ -26,55 +26,47 @@ using Esiur.Data;
using Esiur.Security.Authority;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Common;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace Esiur.Net.Packets;
class IIPAuthPacket : Packet
public class IIPAuthPacket : Packet
{
public enum IIPAuthPacketCommand : byte
{
Action = 0,
Declare = 0x1,
Acknowledge = 0x2,
Report = 0x3,
DeclareEncrypted = 0x5,
AcknowledgeEncrypted = 0x6
}
public enum IIPAuthPacketAction : byte
{
AuthenticateHash = 0x0,
AuthenticatePublicHash = 0x1,
AuthenticatePrivateHash,
EstablishRequest,
EstablishReply,
TwoFactorAuthtenticate,
NewConnection = 0x20,
ResumeConnection,
ConnectionEstablished = 0x28
}
public IIPAuthPacketCommand Command
{
get;
set;
}
public IIPAuthPacketInitialize Initialization
{
get;
set;
}
public IIPAuthPacketAcknowledge Acknowledgement
{
get;
set;
}
public IIPAuthPacketAction Action
{
get;
set;
}
public byte ErrorCode { get; set; }
public string ErrorMessage { get; set; }
public IIPAuthPacketEvent Event
{
get;
set;
}
public AuthenticationMethod LocalMethod
{
@ -82,71 +74,45 @@ class IIPAuthPacket : Packet
set;
}
public byte[] SourceInfo
{
get;
set;
}
public byte[] Hash
{
get;
set;
}
public byte[] SessionId
{
get;
set;
}
public AuthenticationMethod RemoteMethod
{
get;
set;
}
public string Domain
public byte ErrorCode
{
get;
set;
}
public long CertificateId
{
get; set;
}
public string LocalUsername
public string Message
{
get;
set;
}
public string RemoteUsername
public IIPAuthPacketPublicKeyAlgorithm PublicKeyAlgorithm
{
get;
set;
}
public byte[] LocalPassword
{
get;
set;
}
public byte[] RemotePassword
public IIPAuthPacketHashAlgorithm HashAlgorithm
{
get;
set;
}
public byte[] LocalToken
public byte[] Certificate
{
get;
set;
}
public byte[] RemoteToken
public byte[] Challenge
{
get;
set;
@ -158,19 +124,27 @@ class IIPAuthPacket : Packet
set;
}
public byte[] LocalNonce
public byte[] SessionId
{
get;
set;
}
public byte[] RemoteNonce
public TransmissionType? DataType
{
get;
set;
}
public ulong RemoteTokenIndex { get; set; }
// IAuth Reference
public uint Reference
{
get;
set;
}
private uint dataLengthNeeded;
@ -199,174 +173,178 @@ class IIPAuthPacket : Packet
Command = (IIPAuthPacketCommand)(data[offset] >> 6);
if (Command == IIPAuthPacketCommand.Action)
if (Command == IIPAuthPacketCommand.Initialize)
{
Action = (IIPAuthPacketAction)(data[offset++] & 0x3f);
if (Action == IIPAuthPacketAction.AuthenticateHash)
{
if (NotEnough(offset, ends, 32))
return -dataLengthNeeded;
Hash = data.Clip(offset, 32);
//var hash = new byte[32];
//Buffer.BlockCopy(data, (int)offset, hash, 0, 32);
//Hash = hash;
offset += 32;
}
else if (Action == IIPAuthPacketAction.NewConnection)
{
if (NotEnough(offset, ends, 2))
return -dataLengthNeeded;
var length = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, length))
return -dataLengthNeeded;
SourceInfo = data.Clip(offset, length);
//var sourceInfo = new byte[length];
//Buffer.BlockCopy(data, (int)offset, sourceInfo, 0, length);
//SourceInfo = sourceInfo;
offset += 32;
}
else if (Action == IIPAuthPacketAction.ResumeConnection
|| Action == IIPAuthPacketAction.ConnectionEstablished)
{
//var sessionId = new byte[32];
if (NotEnough(offset, ends, 32))
return -dataLengthNeeded;
SessionId = data.Clip(offset, 32);
//Buffer.BlockCopy(data, (int)offset, sessionId, 0, 32);
//SessionId = sessionId;
offset += 32;
}
}
else if (Command == IIPAuthPacketCommand.Declare)
{
RemoteMethod = (AuthenticationMethod)((data[offset] >> 4) & 0x3);
LocalMethod = (AuthenticationMethod)((data[offset] >> 2) & 0x3);
var encrypt = ((data[offset++] & 0x2) == 0x2);
LocalMethod = (AuthenticationMethod)(data[offset] >> 4 & 0x3);
RemoteMethod = (AuthenticationMethod)(data[offset] >> 2 & 0x3);
Initialization = (IIPAuthPacketInitialize)(data[offset++] & 0xFC); // remove last two reserved LSBs
if (NotEnough(offset, ends, 1))
return -dataLengthNeeded;
var domainLength = data[offset++];
if (NotEnough(offset, ends, domainLength))
return -dataLengthNeeded;
(var size, DataType) = TransmissionType.Parse(data, offset, ends);
var domain = data.GetString(offset, domainLength);
Domain = domain;
offset += domainLength;
if (RemoteMethod == AuthenticationMethod.Certificate)
{
}
else if (RemoteMethod == AuthenticationMethod.Credentials)
{
if (LocalMethod == AuthenticationMethod.None)
{
if (NotEnough(offset, ends, 33))
return -dataLengthNeeded;
//var remoteNonce = new byte[32];
//Buffer.BlockCopy(data, (int)offset, remoteNonce, 0, 32);
//RemoteNonce = remoteNonce;
RemoteNonce = data.Clip(offset, 32);
offset += 32;
var length = data[offset++];
if (NotEnough(offset, ends, length))
return -dataLengthNeeded;
RemoteUsername = data.GetString(offset, length);
if (DataType == null)
return -(int)size;
offset += length;
}
}
else if (RemoteMethod == AuthenticationMethod.Token)
{
if (LocalMethod == AuthenticationMethod.None)
{
if (NotEnough(offset, ends, 37))
return -dataLengthNeeded;
offset += (uint)size;
RemoteNonce = data.Clip(offset, 32);
offset += 32;
RemoteTokenIndex = data.GetUInt64(offset, Endian.Little);
offset += 8;
}
}
if (encrypt)
{
if (NotEnough(offset, ends, 2))
return -dataLengthNeeded;
var keyLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, keyLength))
return -dataLengthNeeded;
//var key = new byte[keyLength];
//Buffer.BlockCopy(data, (int)offset, key, 0, keyLength);
//AsymetricEncryptionKey = key;
AsymetricEncryptionKey = data.Clip(offset, keyLength);
offset += keyLength;
}
}
else if (Command == IIPAuthPacketCommand.Acknowledge)
{
RemoteMethod = (AuthenticationMethod)((data[offset] >> 4) & 0x3);
LocalMethod = (AuthenticationMethod)((data[offset] >> 2) & 0x3);
var encrypt = ((data[offset++] & 0x2) == 0x2);
if (RemoteMethod == AuthenticationMethod.None)
LocalMethod = (AuthenticationMethod)(data[offset] >> 4 & 0x3);
RemoteMethod = (AuthenticationMethod)(data[offset] >> 2 & 0x3);
Acknowledgement = (IIPAuthPacketAcknowledge)(data[offset++] & 0xFC); // remove last two reserved LSBs
if (NotEnough(offset, ends, 1))
return -dataLengthNeeded;
(var size, DataType) = TransmissionType.Parse(data, offset, ends);
if (DataType == null)
return -(int)size;
offset += (uint)size;
}
else if (Command == IIPAuthPacketCommand.Action)
{
Action = (IIPAuthPacketAction)data[offset++]; // (IIPAuthPacketAction)(data[offset++] & 0x3f);
if (Action == IIPAuthPacketAction.AuthenticateHash
|| Action == IIPAuthPacketAction.AuthenticatePublicHash
|| Action == IIPAuthPacketAction.AuthenticatePrivateHash
|| Action == IIPAuthPacketAction.AuthenticatePublicPrivateHash)
{
if (LocalMethod == AuthenticationMethod.None)
{
// do nothing
}
if (NotEnough(offset, ends, 3))
return -dataLengthNeeded;
HashAlgorithm = (IIPAuthPacketHashAlgorithm)data[offset++];
var hashLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, hashLength))
return -dataLengthNeeded;
Challenge = data.Clip(offset, hashLength);
offset += hashLength;
}
else if (RemoteMethod == AuthenticationMethod.Credentials
|| RemoteMethod == AuthenticationMethod.Token)
else if (Action == IIPAuthPacketAction.AuthenticatePrivateHashCert
|| Action == IIPAuthPacketAction.AuthenticatePublicPrivateHashCert)
{
if (LocalMethod == AuthenticationMethod.None)
{
if (NotEnough(offset, ends, 32))
return -dataLengthNeeded;
if (NotEnough(offset, ends, 3))
return -dataLengthNeeded;
RemoteNonce = data.Clip(offset, 32);
offset += 32;
HashAlgorithm = (IIPAuthPacketHashAlgorithm)data[offset++];
}
var hashLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, hashLength))
return -dataLengthNeeded;
Challenge = data.Clip(offset, hashLength);
offset += hashLength;
if (NotEnough(offset, ends, 2))
return -dataLengthNeeded;
var certLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, certLength))
return -dataLengthNeeded;
Certificate = data.Clip(offset, certLength);
offset += certLength;
}
else if (Action == IIPAuthPacketAction.IAuthPlain)
{
if (NotEnough(offset, ends, 5))
return -dataLengthNeeded;
Reference = data.GetUInt32(offset, Endian.Little);
offset += 4;
(var size, DataType) = TransmissionType.Parse(data, offset, ends);
if (DataType == null)
return -(int)size;
offset += (uint)size;
}
else if (Action == IIPAuthPacketAction.IAuthHashed)
{
if (NotEnough(offset, ends, 7))
return -dataLengthNeeded;
Reference = data.GetUInt32(offset, Endian.Little);
offset += 4;
HashAlgorithm = (IIPAuthPacketHashAlgorithm)data[offset++];
var cl = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, cl))
return -dataLengthNeeded;
Challenge = data.Clip(offset, cl);
offset += cl;
}
else if (Action == IIPAuthPacketAction.IAuthEncrypted)
{
if (NotEnough(offset, ends, 7))
return -dataLengthNeeded;
Reference = data.GetUInt32(offset, Endian.Little);
offset += 4;
PublicKeyAlgorithm = (IIPAuthPacketPublicKeyAlgorithm)data[offset++];
var cl = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, cl))
return -dataLengthNeeded;
Challenge = data.Clip(offset, cl);
offset += cl;
}
else if (Action == IIPAuthPacketAction.EstablishNewSession)
{
// Nothing here
}
else if (Action == IIPAuthPacketAction.EstablishResumeSession)
{
if (NotEnough(offset, ends, 1))
return -dataLengthNeeded;
var sessionLength = data[offset++];
if (NotEnough(offset, ends, sessionLength))
return -dataLengthNeeded;
SessionId = data.Clip(offset, sessionLength);
offset += sessionLength;
}
if (encrypt)
else if (Action == IIPAuthPacketAction.EncryptKeyExchange)
{
if (NotEnough(offset, ends, 2))
return -dataLengthNeeded;
@ -378,33 +356,86 @@ class IIPAuthPacket : Packet
if (NotEnough(offset, ends, keyLength))
return -dataLengthNeeded;
//var key = new byte[keyLength];
//Buffer.BlockCopy(data, (int)offset, key, 0, keyLength);
//AsymetricEncryptionKey = key;
AsymetricEncryptionKey = data.Clip(offset, keyLength);
offset += keyLength;
}
else if (Action == IIPAuthPacketAction.RegisterEndToEndKey
|| Action == IIPAuthPacketAction.RegisterHomomorphic)
{
if (NotEnough(offset, ends, 3))
return -dataLengthNeeded;
PublicKeyAlgorithm = (IIPAuthPacketPublicKeyAlgorithm)data[offset++];
var keyLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, keyLength))
return -dataLengthNeeded;
AsymetricEncryptionKey = data.Clip(offset, keyLength);
offset += keyLength;
}
}
else if (Command == IIPAuthPacketCommand.Error)
else if (Command == IIPAuthPacketCommand.Event)
{
if (NotEnough(offset, ends, 4))
return -dataLengthNeeded;
offset++;
ErrorCode = data[offset++];
Event = (IIPAuthPacketEvent)data[offset++];
if (Event == IIPAuthPacketEvent.ErrorTerminate
|| Event == IIPAuthPacketEvent.ErrorMustEncrypt
|| Event == IIPAuthPacketEvent.ErrorRetry)
{
if (NotEnough(offset, ends, 3))
return -dataLengthNeeded;
ErrorCode = data[offset++];
var msgLength = data.GetUInt16(offset, Endian.Little);
offset += 2;
if (NotEnough(offset, ends, msgLength))
return -dataLengthNeeded;
var cl = data.GetUInt16(offset, Endian.Little);
offset += 2;
Message = data.GetString(offset, msgLength);
if (NotEnough(offset, ends, cl))
return -dataLengthNeeded;
offset += msgLength;
}
else if (Event == IIPAuthPacketEvent.IndicationEstablished)
{
if (NotEnough(offset, ends, 1))
return -dataLengthNeeded;
ErrorMessage = data.GetString(offset, cl);
offset += cl;
var sessionLength = data[offset++];
if (NotEnough(offset, ends, sessionLength))
return -dataLengthNeeded;
SessionId = data.Clip(offset, sessionLength);
offset += sessionLength;
}
else if (Event == IIPAuthPacketEvent.IAuthPlain
|| Event == IIPAuthPacketEvent.IAuthHashed
|| Event == IIPAuthPacketEvent.IAuthEncrypted)
{
if (NotEnough(offset, ends, 1))
return -dataLengthNeeded;
(var size, DataType) = TransmissionType.Parse(data, offset, ends);
if (DataType == null)
return -(int)size;
offset += (uint)size;
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketAcknowledge : byte
{
NoAuthNoAuth = 0x40, // 0b01000000,
NoAuthCredentials = 0x44, // 0b01000100,
NoAuthToken = 0x48, //0b01001000,
NoAuthCertificate = 0x4c, //0b01001100,
CredentialsNoAuth = 0x50, //0b01010000,
CredentialsCredentials = 0x54, //0b01010100,
CredentialsToken = 0x58, //0b01011000,
CredentialsCertificate = 0x5c, //0b01011100,
TokenNoAuth = 0x60, //0b01100000,
TokenCredentials = 0x64, //0b01100100,
TokenToken = 0x68, //0b01101000,
TokenCertificate = 0x6c, //0b01101100,
CertificateNoAuth = 0x70, //0b01110000,
CertificateCredentials = 0x74, //0b01110100,
CertificateToken = 0x78, //0b01111000,
CertificateCertificate = 0x7c, // 0b01111100,
}
}

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketAction : byte
{
AuthenticateHash = 0x80,
AuthenticatePublicHash = 0x81,
AuthenticatePrivateHash = 0x82,
AuthenticatePublicPrivateHash = 0x83,
AuthenticatePrivateHashCert = 0x88,
AuthenticatePublicPrivateHashCert = 0x89,
IAuthPlain = 0x90,
IAuthHashed = 0x91,
IAuthEncrypted = 0x92,
EstablishNewSession = 0x98,
EstablishResumeSession = 0x99,
EncryptKeyExchange = 0xA0,
RegisterEndToEndKey = 0xA8,
RegisterHomomorphic = 0xA9,
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketCommand : byte
{
Initialize = 0x0,
Acknowledge = 0x1,
Action = 0x2,
Event = 0x3,
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketEvent : byte
{
ErrorTerminate = 0xC0,
ErrorMustEncrypt = 0xC1,
ErrorRetry = 0xC2,
IndicationEstablished = 0xC8,
IAuthPlain = 0xD0,
IAuthHashed = 0xD1,
IAuthEncrypted = 0xD2
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketHashAlgorithm
{
SHA256,
SHA3,
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketHeader
{
Version = 0,
Domain = 1,
SupportedAuthentications = 2,
SupportedHashAlgorithms = 3,
SupportedCiphers = 4,
SupportedCompression = 5,
SupportedPersonalAuth = 6,
Nonce = 7,
Username = 8,
TokenIndex = 9,
CertificateId = 10,
CachedCertificates = 11,
CipherType = 12,
CipherKey = 13,
SoftwareIdentity = 14,
Referrer = 15,
Time = 16,
Certificate = 17,
IPv4 = 18
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketIAuthDestination
{
Self = 0,
Device = 1, // logged in device
Email = 2,
SMS = 3,
App = 4, // Authenticator app
ThirdParty = 5, // usualy a second person
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketIAuthFormat
{
None = 0,
Number = 1,
Text = 2,
LowercaseText = 3,
Choice = 4,
Photo = 5,
Signature = 6,
Fingerprint = 7,
}
}

View File

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketIAuthHeader : byte
{
Reference = 0,
Destination = 1,
Clue = 2,
RequiredFormat = 3,
ContentFormat = 4,
Content = 5,
Timeout = 6,
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketInitialize
{
NoAuthNoAuth = 0x0, //0b00000000,
NoAuthCredentials = 0x4, //0b00000100,
NoAuthToken = 0x8, //0b00001000,
NoAuthCertificate = 0xC, //0b00001100,
CredentialsNoAuth = 0x10, //0b00010000,
CredentialsCredentials = 0x14, //0b00010100,
CredentialsToken = 0x18, //0b00011000,
CredentialsCertificate = 0x1c, //0b00011100,
TokenNoAuth = 0x20, //0b00100000,
TokenCredentials = 0x24, //0b00100100,
TokenToken = 0x28, //0b00101000,
TokenCertificate = 0x2c, //0b00101100,
CertificateNoAuth = 0x30, //0b00110000,
CertificateCredentials = 0x34,// 0b00110100,
CertificateToken = 0x38, //0b00111000,
CertificateCertificate = 0x3c, //0b00111100,
}
}

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPAuthPacketPublicKeyAlgorithm
{
RSA = 0,
CKKS = 1,
}
}

View File

@ -25,7 +25,6 @@ SOFTWARE.
using Esiur.Data;
using Esiur.Core;
using Esiur.Misc;
using Esiur.Net.Packets;
using System;
using System.Collections.Generic;
using System.Linq;
@ -62,81 +61,6 @@ class IIPPacket : Packet
return rt;
}
public enum IIPPacketCommand : byte
{
Event = 0,
Request,
Reply,
Report,
}
public enum IIPPacketEvent : byte
{
// Event Manage
ResourceReassigned = 0,
ResourceDestroyed,
ChildAdded,
ChildRemoved,
Renamed,
// Event Invoke
PropertyUpdated = 0x10,
EventOccurred,
// Attribute
AttributesUpdated = 0x18
}
public enum IIPPacketAction : byte
{
// Request Manage
AttachResource = 0,
ReattachResource,
DetachResource,
CreateResource,
DeleteResource,
AddChild,
RemoveChild,
RenameResource,
// Request Inquire
TemplateFromClassName = 0x8,
TemplateFromClassId,
TemplateFromResourceId,
QueryLink,
ResourceHistory,
ResourceChildren,
ResourceParents,
LinkTemplates,
// Request Invoke
InvokeFunction = 0x10,
Reserved,
Listen,
Unlisten,
SetProperty,
// Request Attribute
GetAllAttributes = 0x18,
UpdateAllAttributes,
ClearAllAttributes,
GetAttributes,
UpdateAttributes,
ClearAttributes,
// Static calling
KeepAlive = 0x20,
ProcedureCall,
StaticCall
}
public enum IIPPacketReport : byte
{
ManagementError,
ExecutionError,
ProgressReport = 0x8,
ChunkStream = 0x9
}
public IIPPacketReport Report
@ -648,7 +572,7 @@ class IIPPacket : Packet
if (NotEnough(offset, ends, cl))
return -dataLengthNeeded;
Procedure = data.GetString(offset, cl);
offset += cl;
@ -662,7 +586,8 @@ class IIPPacket : Packet
offset += (uint)size;
} else if (Action == IIPPacketAction.StaticCall)
}
else if (Action == IIPPacketAction.StaticCall)
{
if (NotEnough(offset, ends, 18))
return -dataLengthNeeded;

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPPacketAction : byte
{
// Request Manage
AttachResource = 0,
ReattachResource,
DetachResource,
CreateResource,
DeleteResource,
AddChild,
RemoveChild,
RenameResource,
// Request Inquire
TemplateFromClassName = 0x8,
TemplateFromClassId,
TemplateFromResourceId,
QueryLink,
ResourceHistory,
ResourceChildren,
ResourceParents,
LinkTemplates,
// Request Invoke
InvokeFunction = 0x10,
Reserved,
Listen,
Unlisten,
SetProperty,
// Request Attribute
GetAllAttributes = 0x18,
UpdateAllAttributes,
ClearAllAttributes,
GetAttributes,
UpdateAttributes,
ClearAttributes,
// Static calling
KeepAlive = 0x20,
ProcedureCall,
StaticCall
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPPacketCommand : byte
{
Event = 0,
Request,
Reply,
Report,
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPPacketEvent : byte
{
// Event Manage
ResourceReassigned = 0,
ResourceDestroyed,
ChildAdded,
ChildRemoved,
Renamed,
// Event Invoke
PropertyUpdated = 0x10,
EventOccurred,
// Attribute
AttributesUpdated = 0x18
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net.Packets
{
public enum IIPPacketReport : byte
{
ManagementError,
ExecutionError,
ProgressReport = 0x8,
ChunkStream = 0x9
}
}

View File

@ -29,7 +29,7 @@ using System.Text;
using Esiur.Misc;
using Esiur.Data;
namespace Esiur.Net.Packets;
namespace Esiur.Net.Packets.WebSocket;
public class WebsocketPacket : Packet
{
public enum WSOpcode : byte
@ -84,17 +84,17 @@ public class WebsocketPacket : Packet
(byte)Opcode));
// calculate length
if (Message.Length > UInt16.MaxValue)
if (Message.Length > ushort.MaxValue)
// 4 bytes
{
pkt.Add((byte)((Mask ? 0x80 : 0x0) | 127));
pkt.AddRange(DC.ToBytes((UInt64)Message.LongCount(), Endian.Big));
pkt.AddRange(((ulong)Message.LongCount()).ToBytes(Endian.Big));
}
else if (Message.Length > 125)
// 2 bytes
{
pkt.Add((byte)((Mask ? 0x80 : 0x0) | 126));
pkt.AddRange(DC.ToBytes((UInt16)Message.Length, Endian.Big));
pkt.AddRange(((ushort)Message.Length).ToBytes(Endian.Big));
}
else
{
@ -118,7 +118,7 @@ public class WebsocketPacket : Packet
try
{
long needed = 2;
var length = (ends - offset);
var length = ends - offset;
if (length < needed)
{
//Console.WriteLine("stage 1 " + needed);
@ -126,13 +126,13 @@ public class WebsocketPacket : Packet
}
uint oOffset = offset;
FIN = ((data[offset] & 0x80) == 0x80);
RSV1 = ((data[offset] & 0x40) == 0x40);
RSV2 = ((data[offset] & 0x20) == 0x20);
RSV3 = ((data[offset] & 0x10) == 0x10);
FIN = (data[offset] & 0x80) == 0x80;
RSV1 = (data[offset] & 0x40) == 0x40;
RSV2 = (data[offset] & 0x20) == 0x20;
RSV3 = (data[offset] & 0x10) == 0x10;
Opcode = (WSOpcode)(data[offset++] & 0xF);
Mask = ((data[offset] & 0x80) == 0x80);
PayloadLength = (long)(data[offset++] & 0x7F);
Mask = (data[offset] & 0x80) == 0x80;
PayloadLength = data[offset++] & 0x7F;
if (Mask)
needed += 4;
@ -192,23 +192,23 @@ public class WebsocketPacket : Packet
MaskKey[2] = data[offset++];
MaskKey[3] = data[offset++];
Message = DC.Clip(data, offset, (uint)PayloadLength);
Message = data.Clip(offset, (uint)PayloadLength);
//var aMask = BitConverter.GetBytes(MaskKey);
for (int i = 0; i < Message.Length; i++)
Message[i] = (byte)(Message[i] ^ MaskKey[i % 4]);
}
else
Message = DC.Clip(data, offset, (uint)PayloadLength);
Message = data.Clip(offset, (uint)PayloadLength);
return (offset - oOffset) + (int)PayloadLength;
return offset - oOffset + (int)PayloadLength;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.WriteLine(offset + "::" + DC.ToHex(data));
Console.WriteLine(offset + "::" + data.ToHex());
throw ex;
}
}