2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-09-13 20:13:19 +00:00
This commit is contained in:
2024-06-22 03:50:54 +03:00
parent 8a30c92e19
commit 26794f08e7
32 changed files with 1508 additions and 595 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,5 @@
import 'package:esiur/src/Security/Membership/IMembership.dart';
import '../../Resource/Template/TemplateDescriber.dart';
import '../../Resource/IResource.dart';
@@ -31,4 +33,6 @@ class DistributedServer extends IResource {
@override
TemplateDescriber get template =>
TemplateDescriber("Esiur.Net.IIP.DistributedServer");
IMembership? membership;
}

View File

@@ -1,6 +1,6 @@
import 'NetworkBuffer.dart';
abstract mixin class INetworkReceiver<T>
abstract class INetworkReceiver<T>
{
void networkClose(T sender);
void networkReceive(T sender, NetworkBuffer buffer);

View File

@@ -21,51 +21,38 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'package:esiur/esiur.dart';
import '../../Data/DC.dart';
import 'IIPAuthPacketAction.dart';
import 'IIPAuthPacketCommand.dart';
import '../../Security/Authority/AuthenticationMethod.dart';
import 'IIPAuthPacketEvent.dart';
class IIPAuthPacket {
int command = 0;
int initialization = 0;
int acknowledgement = 0;
int action = 0;
int errorCode = 0;
String errorMessage = "";
int event = 0;
AuthenticationMethod localMethod = AuthenticationMethod.None;
DC? sourceInfo;
DC? hash;
DC? sessionId;
AuthenticationMethod remoteMethod = AuthenticationMethod.None;
String? domain;
int errorCode = 0;
String message = "";
int certificateId = 0;
String? localUsername;
String? remoteUsername;
DC? localPassword;
DC? remotePassword;
DC? localToken;
DC? remoteToken;
int publicKeyAlgorithm = 0;
int hashAlgorithm = 0;
DC? certificate;
DC? challenge;
DC? asymetricEncryptionKey;
DC? sessionId;
DC? localNonce;
TransmissionType? dataType;
DC? remoteNonce;
int remoteTokenIndex = 0;
int reference = 0;
int _dataLengthNeeded = 0;
@@ -82,157 +69,262 @@ class IIPAuthPacket {
}
int parse(DC data, int offset, int ends) {
var oOffset = offset;
if (_notEnough(offset, ends, 1)) return -_dataLengthNeeded;
if (_notEnough(offset, ends, 1))
return -_dataLengthNeeded;
command = (data[offset] >> 6);
if (command == IIPAuthPacketCommand.Action) {
action = (data[offset++] & 0x3f);
if (command == IIPAuthPacketCommand.Initialize) {
if (action == IIPAuthPacketAction.AuthenticateHash) {
if (_notEnough(offset, ends, 32)) return -_dataLengthNeeded;
localMethod = AuthenticationMethod.values[((data[offset] >> 4) & 0x3)];
remoteMethod = AuthenticationMethod.values[((data[offset] >> 2) & 0x3)];
hash = data.clip(offset, 32);
initialization = (data[offset++] & 0xFC); // remove last two reserved LSBs
//var hash = new byte[32];
//Buffer.BlockCopy(data, (int)offset, hash, 0, 32);
//Hash = hash;
if (_notEnough(offset, ends, 1))
return -_dataLengthNeeded;
offset += 32;
} else if (action == IIPAuthPacketAction.NewConnection) {
if (_notEnough(offset, ends, 2)) return -_dataLengthNeeded;
var parsed = TransmissionType.parse(data, offset, ends);
var length = data.getUint16(offset);
if (parsed.type == null)
return -parsed.size;
offset += 2;
dataType = parsed.type;
offset += parsed.size;
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.values[((data[offset] >> 4) & 0x3)];
localMethod = AuthenticationMethod.values[((data[offset] >> 2) & 0x3)];
var encrypt = ((data[offset++] & 0x2) == 0x2);
if (_notEnough(offset, ends, 1)) return -_dataLengthNeeded;
var domainLength = data[offset++];
if (_notEnough(offset, ends, domainLength)) return -_dataLengthNeeded;
var domain = data.getString(offset, domainLength);
this.domain = domain;
offset += domainLength;
if (remoteMethod == AuthenticationMethod.Credentials) {
if (localMethod == AuthenticationMethod.None) {
if (_notEnough(offset, ends, 33)) return -_dataLengthNeeded;
remoteNonce = data.clip(offset, 32);
offset += 32;
var length = data[offset++];
if (_notEnough(offset, ends, length)) return -_dataLengthNeeded;
remoteUsername = data.getString(offset, length);
offset += length;
}
} else if (remoteMethod == AuthenticationMethod.Token) {
if (localMethod == AuthenticationMethod.None) {
if (_notEnough(offset, ends, 40)) return -_dataLengthNeeded;
remoteNonce = data.clip(offset, 32);
offset += 32;
remoteTokenIndex = data.getUint64(offset);
offset += 8;
}
}
if (encrypt) {
if (_notEnough(offset, ends, 2)) return -_dataLengthNeeded;
var keyLength = data.getUint16(offset);
offset += 2;
if (_notEnough(offset, ends, keyLength)) return -_dataLengthNeeded;
asymetricEncryptionKey = data.clip(offset, keyLength);
offset += keyLength;
}
} else if (command == IIPAuthPacketCommand.Acknowledge) {
remoteMethod = AuthenticationMethod.values[((data[offset] >> 4) & 0x3)];
localMethod = AuthenticationMethod.values[((data[offset] >> 2) & 0x3)];
var encrypt = ((data[offset++] & 0x2) == 0x2);
if (remoteMethod == AuthenticationMethod.None) {
if (localMethod == AuthenticationMethod.None) {
// do nothing
}
} else if (remoteMethod == AuthenticationMethod.Credentials ||
remoteMethod == AuthenticationMethod.Token) {
if (localMethod == AuthenticationMethod.None) {
if (_notEnough(offset, ends, 32)) return -_dataLengthNeeded;
localMethod = AuthenticationMethod.values[((data[offset] >> 4) & 0x3)];
remoteMethod = AuthenticationMethod.values[((data[offset] >> 2) & 0x3)];
remoteNonce = data.clip(offset, 32);
offset += 32;
}
}
acknowledgement =
(data[offset++] & 0xFC); // remove last two reserved LSBs
if (encrypt) {
if (_notEnough(offset, ends, 2)) return -_dataLengthNeeded;
if (_notEnough(offset, ends, 1))
return -_dataLengthNeeded;
var parsed = TransmissionType.parse(data, offset, ends);
if (parsed.type == null)
return -parsed.size;
dataType = parsed.type;
offset += parsed.size;
} else if (command == IIPAuthPacketCommand.Action) {
action = (data[offset++]);
if (action == IIPAuthPacketAction.AuthenticateHash ||
action == IIPAuthPacketAction.AuthenticatePublicHash ||
action == IIPAuthPacketAction.AuthenticatePrivateHash ||
action == IIPAuthPacketAction.AuthenticatePublicPrivateHash) {
if (_notEnough(offset, ends, 3))
return -_dataLengthNeeded;
hashAlgorithm = data[offset++];
var hashLength = data.getUint16(offset);
offset += 2;
if (_notEnough(offset, ends, hashLength))
return -_dataLengthNeeded;
challenge = data.clip(offset, hashLength);
offset += hashLength;
} else if (action == IIPAuthPacketAction.AuthenticatePrivateHashCert ||
action == IIPAuthPacketAction.AuthenticatePublicPrivateHashCert) {
if (_notEnough(offset, ends, 3))
return -_dataLengthNeeded;
hashAlgorithm = data[offset++];
var hashLength = data.getUint16(offset);
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);
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);
offset += 4;
var parsed = TransmissionType.parse(data, offset, ends);
if (parsed.type == null)
return -parsed.size;
dataType = parsed.type;
offset += parsed.size;
} else if (action == IIPAuthPacketAction.IAuthHashed) {
if (_notEnough(offset, ends, 7))
return -_dataLengthNeeded;
reference = data.getUint32(offset);
offset += 4;
hashAlgorithm = data[offset++];
var cl = data.getUint16(offset);
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);
offset += 4;
publicKeyAlgorithm = data[offset++];
var cl = data.getUint16(offset);
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;
} else if (action == IIPAuthPacketAction.EncryptKeyExchange) {
if (_notEnough(offset, ends, 2))
return -_dataLengthNeeded;
var keyLength = data.getUint16(offset);
offset += 2;
if (_notEnough(offset, ends, keyLength)) return -_dataLengthNeeded;
if (_notEnough(offset, ends, keyLength))
return -_dataLengthNeeded;
asymetricEncryptionKey = data.clip(offset, keyLength);
offset += keyLength;
} else if (action == IIPAuthPacketAction.RegisterEndToEndKey ||
action == IIPAuthPacketAction.RegisterHomomorphic) {
if (_notEnough(offset, ends, 3))
return -_dataLengthNeeded;
publicKeyAlgorithm = data[offset++];
var keyLength = data.getUint16(offset);
offset += 2;
if (_notEnough(offset, ends, keyLength))
return -_dataLengthNeeded;
asymetricEncryptionKey = data.clip(offset, keyLength);
offset += keyLength;
}
} else if (command == IIPAuthPacketCommand.Error) {
if (_notEnough(offset, ends, 4)) return -_dataLengthNeeded;
} else if (command == IIPAuthPacketCommand.Event) {
offset++;
errorCode = data[offset++];
event = data[offset++];
var cl = data.getUint16(offset);
offset += 2;
if (event == IIPAuthPacketEvent.ErrorTerminate ||
event == IIPAuthPacketEvent.ErrorMustEncrypt ||
event == IIPAuthPacketEvent.ErrorRetry) {
if (_notEnough(offset, ends, cl)) return -_dataLengthNeeded;
if (_notEnough(offset, ends, 3))
return -_dataLengthNeeded;
errorMessage = data.getString(offset, cl);
offset += cl;
errorCode = data[offset++];
var msgLength = data.getUint16(offset);
offset += 2;
if (_notEnough(offset, ends, msgLength))
return -_dataLengthNeeded;
message = data.getString(offset, msgLength);
offset += msgLength;
} else if (event == IIPAuthPacketEvent.IndicationEstablished) {
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;
} else if (event == IIPAuthPacketEvent.IAuthPlain ||
event == IIPAuthPacketEvent.IAuthHashed ||
event == IIPAuthPacketEvent.IAuthEncrypted) {
if (_notEnough(offset, ends, 1))
return -_dataLengthNeeded;
var parsed = TransmissionType.parse(data, offset, ends);
if (parsed.type == null)
return -parsed.size;
dataType = parsed.type;
offset += parsed.size;
}
}
return offset - oOffset;

View File

@@ -0,0 +1,19 @@
class IIPAuthPacketAcknowledge
{
static const int NoAuthNoAuth = 0x40; // 0b01000000,
static const int NoAuthCredentials = 0x44; // 0b01000100,
static const int NoAuthToken = 0x48; //0b01001000,
static const int NoAuthCertificate = 0x4c; //0b01001100,
static const int CredentialsNoAuth = 0x50; //0b01010000,
static const int CredentialsCredentials = 0x54; //0b01010100,
static const int CredentialsToken = 0x58; //0b01011000,
static const int CredentialsCertificate = 0x5c; //0b01011100,
static const int TokenNoAuth = 0x60; //0b01100000,
static const int TokenCredentials = 0x64; //0b01100100,
static const int TokenToken = 0x68; //0b01101000,
static const int TokenCertificate = 0x6c; //0b01101100,
static const int CertificateNoAuth = 0x70; //0b01110000,
static const int CertificateCredentials = 0x74; //0b01110100,
static const int CertificateToken = 0x78; //0b01111000,
static const int CertificateCertificate = 0x7c; // 0b01111100,
}

View File

@@ -1,16 +1,27 @@
class IIPAuthPacketAction
{
// Authenticate
static const int AuthenticateHash = 0;
//Challenge,
//CertificateRequest,
//CertificateReply,
//EstablishRequest,
//EstablishReply
static const int NewConnection = 0x20;
static const int ResumeConnection = 0x21;
static const int ConnectionEstablished = 0x28;
static const int AuthenticateHash = 0x80;
static const int AuthenticatePublicHash = 0x81;
static const int AuthenticatePrivateHash = 0x82;
static const int AuthenticatePublicPrivateHash = 0x83;
static const int AuthenticatePrivateHashCert = 0x88;
static const int AuthenticatePublicPrivateHashCert = 0x89;
static const int IAuthPlain = 0x90;
static const int IAuthHashed = 0x91;
static const int IAuthEncrypted = 0x92;
static const int EstablishNewSession = 0x98;
static const int EstablishResumeSession = 0x99;
static const int EncryptKeyExchange = 0xA0;
static const int RegisterEndToEndKey = 0xA8;
static const int RegisterHomomorphic = 0xA9;
}

View File

@@ -1,7 +1,7 @@
class IIPAuthPacketCommand
{
static const int Action = 0;
static const int Declare = 1;
static const int Acknowledge = 2;
static const int Error = 3;
static const int Initialize = 0x0;
static const int Acknowledge = 0x1;
static const int Action = 0x2;
static const int Event = 0x3;
}

View File

@@ -0,0 +1,12 @@
class IIPAuthPacketEvent
{
static const int ErrorTerminate = 0xC0;
static const int ErrorMustEncrypt = 0xC1;
static const int ErrorRetry = 0xC2;
static const int IndicationEstablished = 0xC8;
static const int IAuthPlain = 0xD0;
static const int IAuthHashed = 0xD1;
static const int IAuthEncrypted = 0xD2;
}

View File

@@ -0,0 +1,6 @@
class IIPAuthPacketHashAlgorithm
{
static int SHA256 = 0x0;
static int SHA3 = 0x1;
}

View File

@@ -0,0 +1,25 @@
import '../../Data/IntType.dart';
class IIPAuthPacketHeader
{
static UInt8 Version = UInt8(0);
static UInt8 Domain = UInt8(1);
static UInt8 SupportedAuthentications = UInt8(2);
static UInt8 SupportedHashAlgorithms = UInt8(3);
static UInt8 SupportedCiphers = UInt8(4);
static UInt8 SupportedCompression = UInt8(5);
static UInt8 SupportedPersonalAuth = UInt8(6);
static UInt8 Nonce = UInt8(7);
static UInt8 Username = UInt8(8);
static UInt8 TokenIndex = UInt8(9);
static UInt8 CertificateId = UInt8(10);
static UInt8 CachedCertificates = UInt8(11);
static UInt8 CipherType = UInt8(12);
static UInt8 CipherKey = UInt8(13);
static UInt8 SoftwareIdentity = UInt8(14);
static UInt8 Referrer = UInt8(15);
static UInt8 Time = UInt8(16);
static UInt8 Certificate = UInt8(17);
static UInt8 IPv4 = UInt8(18);
}

View File

@@ -0,0 +1,9 @@
class IIPAuthPacketIAuthDestination
{
static int Self = 0;
static int Device = 1; // logged in device
static int Email = 2;
static int SMS = 3;
static int App = 4; // Authenticator app
static int ThirdParty = 5; // usualy a second person
}

View File

@@ -0,0 +1,12 @@
class IIPAuthPacketIAuthFormat
{
static int None = 0;
static int Number = 1;
static int Text = 2;
static int LowercaseText = 3;
static int Choice = 4;
static int Photo = 5;
static int Signature = 6;
static int Fingerprint = 7;
}

View File

@@ -0,0 +1,11 @@
class IIPAuthPacketIAuthHeader
{
static int Reference = 0;
static int Destination = 1;
static int Clue = 2;
static int RequiredFormat = 3;
static int ContentFormat = 4;
static int Content = 5;
static int Timeout = 6;
}

View File

@@ -0,0 +1,19 @@
class IIPAuthPacketInitialize
{
static int NoAuthNoAuth = 0x0; //0b00000000,
static int NoAuthCredentials = 0x4; //0b00000100,
static int NoAuthToken = 0x8; //0b00001000,
static int NoAuthCertificate = 0xC; //0b00001100,
static int CredentialsNoAuth = 0x10; //0b00010000,
static int CredentialsCredentials = 0x14; //0b00010100,
static int CredentialsToken = 0x18; //0b00011000,
static int CredentialsCertificate = 0x1c; //0b00011100,
static int TokenNoAuth = 0x20; //0b00100000,
static int TokenCredentials = 0x24; //0b00100100,
static int TokenToken = 0x28; //0b00101000,
static int TokenCertificate = 0x2c; //0b00101100,
static int CertificateNoAuth = 0x30; //0b00110000,
static int CertificateCredentials = 0x34; // 0b00110100,
static int CertificateToken = 0x38; //0b00111000,
static int CertificateCertificate = 0x3c; //0b00111100,
}

View File

@@ -0,0 +1,6 @@
class IIPAuthPacketPublicKeyAlgorithm
{
static int RSA = 0;
static int CKKS = 1;
}