mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2025-06-27 05:23:13 +00:00
Rename
This commit is contained in:
41
Esyur/Security/Authority/AlienAuthentication.cs
Normal file
41
Esyur/Security/Authority/AlienAuthentication.cs
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class AlienAuthentication : Authentication
|
||||
{
|
||||
public AlienAuthentication() :
|
||||
base(AuthenticationType.Alien)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
62
Esyur/Security/Authority/Authentication.cs
Normal file
62
Esyur/Security/Authority/Authentication.cs
Normal file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class Authentication
|
||||
{
|
||||
AuthenticationType type;
|
||||
|
||||
|
||||
public string Username { get; set; }
|
||||
public Certificate Certificate { get; set; }
|
||||
public string Domain { get; set; }
|
||||
|
||||
public string FullName => Username + "@" + Domain;
|
||||
|
||||
public Source Source { get; } = new Source();
|
||||
|
||||
public AuthenticationState State
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public AuthenticationType Type
|
||||
{
|
||||
get => type;
|
||||
}
|
||||
|
||||
public Authentication(AuthenticationType type)
|
||||
{
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
}
|
42
Esyur/Security/Authority/AuthenticationState.cs
Normal file
42
Esyur/Security/Authority/AuthenticationState.cs
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public enum AuthenticationState : int
|
||||
{
|
||||
Denied = 0x1,
|
||||
Succeeded = 0x2,
|
||||
Blocked = 0x4,
|
||||
Rejected = 0x8,
|
||||
NeedsUpdate = 0x10,
|
||||
NotFound = 0x20
|
||||
}
|
||||
}
|
40
Esyur/Security/Authority/AuthenticationType.cs
Normal file
40
Esyur/Security/Authority/AuthenticationType.cs
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public enum AuthenticationType
|
||||
{
|
||||
Host,
|
||||
CoHost,
|
||||
Client,
|
||||
Alien
|
||||
}
|
||||
}
|
202
Esyur/Security/Authority/CACertificate.cs
Normal file
202
Esyur/Security/Authority/CACertificate.cs
Normal file
@ -0,0 +1,202 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Misc;
|
||||
using Esyur.Security.Cryptography;
|
||||
using Esyur.Security.Integrity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class CACertificate : Certificate
|
||||
{
|
||||
|
||||
string name;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get { return name; }
|
||||
}
|
||||
|
||||
public CACertificate(byte[] data, uint offset, uint length, bool privateKeyIncluded = false)
|
||||
:base(0, DateTime.MinValue, DateTime.MinValue, HashFunctionType.MD5)
|
||||
{
|
||||
|
||||
uint oOffset = offset;
|
||||
|
||||
this.id = DC.GetUInt64(data, offset);
|
||||
offset += 8;
|
||||
this.issueDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
this.expireDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
this.hashFunction = (HashFunctionType)(data[offset++] >> 4);
|
||||
|
||||
|
||||
this.name = (Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]));
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
|
||||
var aea = (AsymetricEncryptionAlgorithmType)(data[offset] >> 5);
|
||||
|
||||
if (aea == AsymetricEncryptionAlgorithmType.RSA)
|
||||
{
|
||||
var key = new RSAParameters();
|
||||
uint exponentLength = (uint)data[offset++] & 0x1F;
|
||||
|
||||
key.Exponent = DC.Clip(data, offset, exponentLength);
|
||||
|
||||
offset += exponentLength;
|
||||
|
||||
uint keySize = DC.GetUInt16(data, offset);
|
||||
offset += 2;
|
||||
|
||||
key.Modulus = DC.Clip(data, offset, keySize);
|
||||
|
||||
offset += keySize;
|
||||
|
||||
// copy cert data
|
||||
this.publicRawData = new byte[offset - oOffset];
|
||||
Buffer.BlockCopy(data, (int)oOffset, publicRawData, 0, publicRawData.Length);
|
||||
|
||||
if (privateKeyIncluded)
|
||||
{
|
||||
uint privateKeyLength = (keySize * 3) + (keySize / 2);
|
||||
uint halfKeySize = keySize / 2;
|
||||
|
||||
privateRawData = DC.Clip(data, offset, privateKeyLength);
|
||||
|
||||
key.D = DC.Clip(data, offset, keySize);
|
||||
offset += keySize;
|
||||
|
||||
key.DP = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
key.DQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
|
||||
key.InverseQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
key.P = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
key.Q = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
}
|
||||
|
||||
// setup rsa
|
||||
this.rsa = RSA.Create();// new RSACryptoServiceProvider();
|
||||
this.rsa.ImportParameters(key);
|
||||
}
|
||||
}
|
||||
|
||||
public CACertificate(ulong id, string authorityName, DateTime issueDate, DateTime expireDate,
|
||||
HashFunctionType hashFunction = HashFunctionType.SHA1, uint ip = 0, byte[] ip6 = null)
|
||||
: base(id, issueDate, expireDate, hashFunction)
|
||||
{
|
||||
// assign type
|
||||
|
||||
BinaryList cr = new BinaryList();
|
||||
|
||||
// make header
|
||||
|
||||
cr.AddUInt64(id)
|
||||
.AddDateTime(issueDate)
|
||||
.AddDateTime(expireDate);
|
||||
|
||||
|
||||
// hash function
|
||||
cr.AddUInt8((byte)((byte)hashFunction << 4));
|
||||
this.hashFunction = hashFunction;
|
||||
|
||||
// CA Name
|
||||
this.name = authorityName;
|
||||
cr.AddUInt8((byte)(authorityName.Length))
|
||||
.AddUInt8Array(Encoding.ASCII.GetBytes(authorityName));
|
||||
|
||||
// public key
|
||||
rsa = RSA.Create();// new RSACryptoServiceProvider(2048);
|
||||
rsa.KeySize = 2048;
|
||||
RSAParameters dRSAKey = rsa.ExportParameters(true);
|
||||
|
||||
|
||||
cr.AddUInt8((byte)dRSAKey.Exponent.Length)
|
||||
.AddUInt8Array(dRSAKey.Exponent)
|
||||
.AddUInt16((ushort)dRSAKey.Modulus.Length)
|
||||
.AddUInt8Array(dRSAKey.Modulus);
|
||||
|
||||
|
||||
publicRawData = cr.ToArray();
|
||||
|
||||
privateRawData = DC.Merge(dRSAKey.D, dRSAKey.DP, dRSAKey.DQ, dRSAKey.InverseQ, dRSAKey.P, dRSAKey.Q);
|
||||
|
||||
}
|
||||
|
||||
public override bool Save(string filename, bool includePrivate = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (includePrivate)
|
||||
File.WriteAllBytes(filename, new BinaryList()
|
||||
.AddUInt8((byte)CertificateType.CAPrivate)
|
||||
.AddUInt8Array(publicRawData)
|
||||
.AddUInt8Array(privateRawData)
|
||||
.ToArray());
|
||||
else
|
||||
File.WriteAllBytes(filename, new BinaryList()
|
||||
.AddUInt8((byte)CertificateType.CAPublic)
|
||||
.AddUInt8Array(publicRawData).ToArray());
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] Serialize(bool includePrivate = false)
|
||||
{
|
||||
if (includePrivate)
|
||||
return new BinaryList()
|
||||
.AddUInt8Array(publicRawData)
|
||||
.AddUInt8Array(privateRawData)
|
||||
.ToArray();
|
||||
else
|
||||
return publicRawData;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
222
Esyur/Security/Authority/Certificate.cs
Normal file
222
Esyur/Security/Authority/Certificate.cs
Normal file
@ -0,0 +1,222 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Misc;
|
||||
using Esyur.Security.Cryptography;
|
||||
using Esyur.Security.Integrity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public abstract class Certificate
|
||||
{
|
||||
protected DateTime issueDate, expireDate;
|
||||
protected RSA rsa;
|
||||
protected Aes aes;
|
||||
|
||||
protected byte[] publicRawData;
|
||||
protected byte[] privateRawData;
|
||||
protected ulong id;
|
||||
protected HashFunctionType hashFunction;
|
||||
|
||||
public Certificate(ulong id, DateTime issueDate, DateTime expireDate, HashFunctionType hashFunction)
|
||||
{
|
||||
this.id = id;
|
||||
this.issueDate = issueDate;
|
||||
this.expireDate = expireDate;
|
||||
this.hashFunction = hashFunction;
|
||||
}
|
||||
|
||||
public ulong Id
|
||||
{
|
||||
get { return id; }
|
||||
}
|
||||
|
||||
public AsymetricEncryptionAlgorithmType AsymetricEncryptionAlgorithm
|
||||
{
|
||||
get { return AsymetricEncryptionAlgorithmType.RSA; }
|
||||
}
|
||||
|
||||
public byte[] AsymetricEncrypt(byte[] message)
|
||||
{
|
||||
return rsa.Encrypt(message, RSAEncryptionPadding.OaepSHA512);
|
||||
}
|
||||
|
||||
|
||||
public byte[] AsymetricEncrypt(byte[] message, uint offset, uint length)
|
||||
{
|
||||
if (message.Length != length)
|
||||
return rsa.Encrypt(DC.Clip(message, offset, length), RSAEncryptionPadding.OaepSHA512);
|
||||
else
|
||||
return rsa.Encrypt(message, RSAEncryptionPadding.OaepSHA512);
|
||||
}
|
||||
|
||||
public byte[] AsymetricDecrypt(byte[] message)
|
||||
{
|
||||
try
|
||||
{
|
||||
return rsa.Decrypt(message, RSAEncryptionPadding.OaepSHA512);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Global.Log("Certificate", LogType.Error, ex.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] AsymetricDecrypt(byte[] message, uint offset, uint length)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (message.Length != length)
|
||||
return rsa.Decrypt(DC.Clip(message, offset, length), RSAEncryptionPadding.OaepSHA512);
|
||||
else
|
||||
return rsa.Decrypt(message, RSAEncryptionPadding.OaepSHA512);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Global.Log("Certificate", LogType.Error, ex.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] SymetricEncrypt(byte[] message, uint offset, uint length)
|
||||
{
|
||||
byte[] rt = null;
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
||||
cs.Write(message, (int)offset, (int)length);
|
||||
|
||||
rt = ms.ToArray();
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
public byte[] SymetricEncrypt(byte[] message)
|
||||
{
|
||||
return SymetricEncrypt(message, 0, (uint)message.Length);
|
||||
}
|
||||
|
||||
public byte[] SymetricDecrypt(byte[] message, uint offset, uint length)
|
||||
{
|
||||
byte[] rt = null;
|
||||
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
using (CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
|
||||
cs.Write(message, (int)offset, (int)length);
|
||||
|
||||
rt = ms.ToArray();
|
||||
}
|
||||
|
||||
return rt;
|
||||
}
|
||||
|
||||
public byte[] SymetricDecrypt(byte[] message)
|
||||
{
|
||||
return SymetricDecrypt(message, 0, (uint)message.Length);
|
||||
}
|
||||
|
||||
public byte[] Sign(byte[] message)
|
||||
{
|
||||
return Sign(message, 0, (uint)message.Length);
|
||||
}
|
||||
|
||||
public byte[] Sign(byte[] message, uint offset, uint length)
|
||||
{
|
||||
if (hashFunction == HashFunctionType.SHA1)
|
||||
return rsa.SignData(message, (int)offset, (int)length, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);
|
||||
else if (hashFunction == HashFunctionType.MD5)
|
||||
return rsa.SignData(message, (int)offset, (int)length, HashAlgorithmName.MD5, RSASignaturePadding.Pkcs1);
|
||||
else if (hashFunction == HashFunctionType.SHA256)
|
||||
return rsa.SignData(message, (int)offset, (int)length, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
|
||||
else if (hashFunction == HashFunctionType.SHA384)
|
||||
return rsa.SignData(message, (int)offset, (int)length, HashAlgorithmName.SHA384, RSASignaturePadding.Pkcs1);
|
||||
else if (hashFunction == HashFunctionType.SHA512)
|
||||
return rsa.SignData(message, (int)offset, (int)length, HashAlgorithmName.SHA512, RSASignaturePadding.Pkcs1);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool InitializeSymetricCipher(SymetricEncryptionAlgorithmType algorithm, int keyLength, byte[] key, byte[] iv)
|
||||
{
|
||||
if (algorithm == SymetricEncryptionAlgorithmType.AES)
|
||||
{
|
||||
if (keyLength == 0) // 128 bit
|
||||
{
|
||||
aes = Aes.Create();
|
||||
aes.Mode = CipherMode.CBC;
|
||||
aes.Padding = PaddingMode.PKCS7;
|
||||
aes.Key = key;
|
||||
aes.IV = iv;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public abstract bool Save(string filename, bool includePrivate = false);
|
||||
public abstract byte[] Serialize(bool includePrivate = false);
|
||||
|
||||
public static Certificate Load(string filename)
|
||||
{
|
||||
byte[] ar = File.ReadAllBytes(filename);
|
||||
var t = (CertificateType)ar[0];
|
||||
|
||||
switch (t)
|
||||
{
|
||||
case CertificateType.CAPublic:
|
||||
return new CACertificate(ar, 1, (uint)ar.Length - 1);
|
||||
case CertificateType.CAPrivate:
|
||||
return new CACertificate(ar, 1, (uint)ar.Length - 1, true);
|
||||
case CertificateType.DomainPublic:
|
||||
return new DomainCertificate(ar, 1, (uint)ar.Length - 1);
|
||||
case CertificateType.DomainPrivate:
|
||||
return new DomainCertificate(ar, 1, (uint)ar.Length - 1, true);
|
||||
case CertificateType.UserPublic:
|
||||
return new UserCertificate(ar, 1, (uint)ar.Length - 1);
|
||||
case CertificateType.UserPrivate:
|
||||
return new UserCertificate(ar, 1, (uint)ar.Length - 1, true);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
42
Esyur/Security/Authority/CertificateType.cs
Normal file
42
Esyur/Security/Authority/CertificateType.cs
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public enum CertificateType
|
||||
{
|
||||
CAPublic = 0,
|
||||
CAPrivate,
|
||||
DomainPublic,
|
||||
DomainPrivate,
|
||||
UserPublic,
|
||||
UserPrivate
|
||||
}
|
||||
}
|
72
Esyur/Security/Authority/ClientAuthentication.cs
Normal file
72
Esyur/Security/Authority/ClientAuthentication.cs
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class ClientAuthentication : Authentication
|
||||
{
|
||||
/*
|
||||
string username;
|
||||
byte[] password;
|
||||
string domain;
|
||||
byte[] token;
|
||||
UserCertificate certificate;
|
||||
|
||||
public string Username => username;
|
||||
public byte[] Password => password;
|
||||
//public string Domain => domain;
|
||||
public byte[] Token => token;
|
||||
|
||||
public byte[] Nounce { get; set; }
|
||||
*/
|
||||
|
||||
public ClientAuthentication()
|
||||
:base(AuthenticationType.Client)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
public ClientAuthentication(byte[] token)
|
||||
: base(AuthenticationType.Client)
|
||||
{
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public ClientAuthentication(string username, byte[] password)
|
||||
: base(AuthenticationType.Client)
|
||||
{
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
//this.domain = domain;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
41
Esyur/Security/Authority/CoHostAuthentication.cs
Normal file
41
Esyur/Security/Authority/CoHostAuthentication.cs
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class CoHostAuthentication : Authentication
|
||||
{
|
||||
public CoHostAuthentication()
|
||||
: base(AuthenticationType.CoHost)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
250
Esyur/Security/Authority/DomainCertificate.cs
Normal file
250
Esyur/Security/Authority/DomainCertificate.cs
Normal file
@ -0,0 +1,250 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using Esyur.Misc;
|
||||
using Esyur.Security.Cryptography;
|
||||
using Esyur.Security.Integrity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class DomainCertificate : Certificate
|
||||
{
|
||||
uint ip;
|
||||
byte[] ip6;
|
||||
string domain;
|
||||
|
||||
//CACertificate ca;
|
||||
string caName;
|
||||
ulong caId;
|
||||
byte[] signature;
|
||||
|
||||
string authorityName;
|
||||
|
||||
public string AuthorityName
|
||||
{
|
||||
get { return authorityName; }
|
||||
}
|
||||
|
||||
public string Domain
|
||||
{
|
||||
get { return domain; }
|
||||
}
|
||||
|
||||
public byte[] Signature
|
||||
{
|
||||
get { return signature; }
|
||||
}
|
||||
|
||||
public uint IPAddress
|
||||
{
|
||||
get { return ip; }
|
||||
}
|
||||
|
||||
public byte[] IPv6Address
|
||||
{
|
||||
get { return ip6; }
|
||||
}
|
||||
|
||||
public DomainCertificate(byte[] data, uint offset, uint length, bool privateKeyIncluded = false)
|
||||
:base(0, DateTime.MinValue, DateTime.MinValue, HashFunctionType.MD5)
|
||||
{
|
||||
var oOffset = offset;
|
||||
|
||||
this.id = DC.GetUInt64(data, offset);
|
||||
offset += 8;
|
||||
|
||||
// load IPs
|
||||
this.ip = DC.GetUInt32(data, offset);
|
||||
offset += 4;
|
||||
this.ip6 = DC.Clip(data, offset, 16);
|
||||
|
||||
offset += 16;
|
||||
|
||||
this.issueDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
this.expireDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
|
||||
this.domain = Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]);
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
this.authorityName = (Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]));
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
caId = DC.GetUInt64(data, offset);
|
||||
offset += 8;
|
||||
|
||||
var aea = (AsymetricEncryptionAlgorithmType)(data[offset] >> 5);
|
||||
|
||||
if (aea == AsymetricEncryptionAlgorithmType.RSA)
|
||||
{
|
||||
var key = new RSAParameters();
|
||||
uint exponentLength = (uint)data[offset++] & 0x1F;
|
||||
|
||||
key.Exponent = DC.Clip(data, offset, exponentLength);
|
||||
offset += exponentLength;
|
||||
|
||||
uint keySize = DC.GetUInt16(data, offset);
|
||||
offset += 2;
|
||||
|
||||
key.Modulus = DC.Clip(data, offset, keySize);
|
||||
|
||||
offset += keySize;
|
||||
|
||||
// copy cert data
|
||||
publicRawData = new byte[offset - oOffset];
|
||||
Buffer.BlockCopy(data, (int)oOffset, publicRawData, 0, publicRawData.Length);
|
||||
|
||||
if (privateKeyIncluded)
|
||||
{
|
||||
|
||||
uint privateKeyLength = (keySize * 3) + (keySize / 2);
|
||||
privateRawData = DC.Clip(data, offset, privateKeyLength);
|
||||
|
||||
uint halfKeySize = keySize / 2;
|
||||
|
||||
key.D = DC.Clip(data, offset, keySize);
|
||||
offset += keySize;
|
||||
key.DP = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.DQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.InverseQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.P = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.Q = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
|
||||
}
|
||||
|
||||
// setup rsa
|
||||
rsa = RSA.Create();// new RSACryptoServiceProvider();
|
||||
rsa.ImportParameters(key);
|
||||
|
||||
this.signature = DC.Clip(data, offset, length - (offset - oOffset));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public DomainCertificate(ulong id, string domain, CACertificate authority, DateTime issueDate,
|
||||
DateTime expireDate, HashFunctionType hashFunction = HashFunctionType.SHA1, uint ip = 0, byte[] ip6 = null)
|
||||
: base (id, issueDate, expireDate, hashFunction)
|
||||
{
|
||||
// assign type
|
||||
|
||||
var cr = new BinaryList();
|
||||
|
||||
// id
|
||||
cr.AddUInt64(id);
|
||||
|
||||
// ip
|
||||
this.ip = ip;
|
||||
this.ip6 = ip6;
|
||||
|
||||
cr.AddUInt32(ip);
|
||||
|
||||
|
||||
if (ip6?.Length == 16)
|
||||
cr.AddUInt8Array(ip6);
|
||||
else
|
||||
cr.AddUInt8Array(new byte[16]);
|
||||
|
||||
|
||||
cr.AddDateTime(issueDate)
|
||||
.AddDateTime(expireDate);
|
||||
|
||||
// domain
|
||||
this.domain = domain;
|
||||
cr.AddUInt8((byte)(domain.Length))
|
||||
.AddUInt8Array(Encoding.ASCII.GetBytes(domain));
|
||||
|
||||
// CA
|
||||
this.caName = authority.Name;
|
||||
cr.AddUInt8((byte)(authority.Name.Length))
|
||||
.AddUInt8Array(Encoding.ASCII.GetBytes(authority.Name));
|
||||
|
||||
this.authorityName = authority.Name;
|
||||
|
||||
// CA Index
|
||||
//co.KeyIndex = authority.KeyIndex;
|
||||
this.caId = authority.Id;
|
||||
cr.AddUInt64(caId);
|
||||
|
||||
|
||||
// public key
|
||||
rsa = RSA.Create();// new RSACryptoServiceProvider(2048);
|
||||
rsa.KeySize = 2048;
|
||||
RSAParameters dRSAKey = rsa.ExportParameters(true);
|
||||
cr.AddUInt8((byte)dRSAKey.Exponent.Length)
|
||||
.AddUInt8Array(dRSAKey.Exponent)
|
||||
.AddUInt16((ushort)dRSAKey.Modulus.Length)
|
||||
.AddUInt8Array(dRSAKey.Modulus);
|
||||
|
||||
|
||||
publicRawData = cr.ToArray();
|
||||
|
||||
// private key
|
||||
this.privateRawData = DC.Merge(dRSAKey.D, dRSAKey.DP, dRSAKey.DQ, dRSAKey.InverseQ, dRSAKey.P, dRSAKey.Q);
|
||||
|
||||
this.signature = authority.Sign(publicRawData);
|
||||
|
||||
}
|
||||
|
||||
public override bool Save(string filename, bool includePrivate = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (includePrivate)
|
||||
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData));
|
||||
else
|
||||
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature));
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] Serialize(bool includePrivate = false)
|
||||
{
|
||||
if (includePrivate)
|
||||
return DC.Merge(publicRawData, signature, privateRawData);
|
||||
else
|
||||
return DC.Merge(publicRawData, signature);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
43
Esyur/Security/Authority/HostAuthentication.cs
Normal file
43
Esyur/Security/Authority/HostAuthentication.cs
Normal file
@ -0,0 +1,43 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class HostAuthentication : Authentication
|
||||
{
|
||||
|
||||
public HostAuthentication()
|
||||
:base(AuthenticationType.Host)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
61
Esyur/Security/Authority/Session.cs
Normal file
61
Esyur/Security/Authority/Session.cs
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Net;
|
||||
using Esyur.Resource;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class Session
|
||||
{
|
||||
public Authentication LocalAuthentication => localAuth;
|
||||
public Authentication RemoteAuthentication => remoteAuth;
|
||||
|
||||
// public Source Source { get; }
|
||||
public byte[] Id { get; set; }
|
||||
public DateTime Creation { get; }
|
||||
public DateTime Modification { get; }
|
||||
public KeyList<string, object> Variables {get;} = new KeyList<string, object>();
|
||||
|
||||
//KeyList<string, object> Variables { get; }
|
||||
//IStore Store { get; }
|
||||
|
||||
//string id;
|
||||
Authentication localAuth, remoteAuth;
|
||||
//string domain;
|
||||
|
||||
public Session(Authentication localAuthentication, Authentication remoteAuthentication)
|
||||
{
|
||||
|
||||
this.localAuth = localAuthentication;
|
||||
this.remoteAuth = remoteAuthentication;
|
||||
}
|
||||
}
|
||||
}
|
59
Esyur/Security/Authority/Source.cs
Normal file
59
Esyur/Security/Authority/Source.cs
Normal file
@ -0,0 +1,59 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class Source
|
||||
{
|
||||
|
||||
//string id;
|
||||
KeyList<SourceAttributeType, object> attributes;
|
||||
|
||||
public string Id { get; set; }
|
||||
|
||||
public KeyList<SourceAttributeType, object> Attributes
|
||||
{
|
||||
get => attributes;
|
||||
}
|
||||
|
||||
public Source(string id, KeyList<SourceAttributeType, object> attributes)
|
||||
{
|
||||
Id = id;
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
public Source()
|
||||
{
|
||||
attributes = new KeyList<SourceAttributeType, object>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
80
Esyur/Security/Authority/SourceAttributeType.cs
Normal file
80
Esyur/Security/Authority/SourceAttributeType.cs
Normal file
@ -0,0 +1,80 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public enum SourceAttributeType
|
||||
{
|
||||
Mobility, // Stationary/Mobile
|
||||
CPU, // Arc, Speed, Cores
|
||||
IPv4, // IPv4, IPv6 Address
|
||||
IPv6, // IPv4, IPv6 Address
|
||||
Port, // TCP or UDP port
|
||||
Proxy, // Proxy
|
||||
Route, // Trace Root
|
||||
Location, // Lon, Lat, Alt, Accuracy
|
||||
OS, // OS name, version, distro, kernel
|
||||
Application, // lib version, app version
|
||||
Network, // Bandwidth, MAC, IP, Route
|
||||
Display, // Screen WxH
|
||||
Media, // AudioIn, AudioOut, VideoIn,
|
||||
Identity, // IMEI, IMSI, Manufacture
|
||||
}
|
||||
/*
|
||||
public class SourceAttribute
|
||||
{
|
||||
SourceAttributeType type;
|
||||
Structure value;
|
||||
|
||||
public SourceAttributeType Type
|
||||
{
|
||||
get
|
||||
{
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
public Structure Value
|
||||
{
|
||||
get
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public SourceAttribute(SourceAttributeType type, Structure value)
|
||||
{
|
||||
this.type = type;
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
260
Esyur/Security/Authority/UserCertificate.cs
Normal file
260
Esyur/Security/Authority/UserCertificate.cs
Normal file
@ -0,0 +1,260 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using Esyur.Security.Cryptography;
|
||||
using Esyur.Security.Integrity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Authority
|
||||
{
|
||||
public class UserCertificate : Certificate
|
||||
{
|
||||
uint ip;
|
||||
byte[] ip6;
|
||||
byte[] signature;
|
||||
string domain;
|
||||
string username;
|
||||
ulong domainId;
|
||||
|
||||
public ulong DomainId
|
||||
{
|
||||
get { return domainId; }
|
||||
}
|
||||
|
||||
public string Username
|
||||
{
|
||||
get { return username; }
|
||||
}
|
||||
|
||||
public string Domain
|
||||
{
|
||||
get { return domain; }
|
||||
}
|
||||
|
||||
public byte[] Signature
|
||||
{
|
||||
get { return signature; }
|
||||
}
|
||||
|
||||
public uint IPAddress
|
||||
{
|
||||
get { return ip; }
|
||||
}
|
||||
|
||||
public byte[] IPv6Address
|
||||
{
|
||||
get { return ip6; }
|
||||
}
|
||||
|
||||
public UserCertificate(byte[] data, uint offset, uint length, bool privateKeyIncluded = false)
|
||||
: base(0, DateTime.MinValue, DateTime.MinValue, HashFunctionType.MD5)
|
||||
{
|
||||
var oOffset = offset;
|
||||
|
||||
this.id = DC.GetUInt64(data, offset);
|
||||
offset += 8;
|
||||
|
||||
// load IPs
|
||||
this.ip = DC.GetUInt32(data, offset);
|
||||
offset += 4;
|
||||
ip6 = DC.Clip(data, offset, 16);
|
||||
offset += 16;
|
||||
|
||||
this.issueDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
this.expireDate = DC.GetDateTime(data, offset);
|
||||
offset += 8;
|
||||
|
||||
this.domainId = DC.GetUInt64(data, offset);
|
||||
offset += 8;
|
||||
|
||||
this.domain = Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]);
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
this.username = Encoding.ASCII.GetString(data, (int)offset + 1, data[offset]);
|
||||
offset += (uint)data[offset] + 1;
|
||||
|
||||
// Hash Function
|
||||
this.hashFunction = (HashFunctionType)(data[offset++] >> 4);
|
||||
|
||||
// Public Key Encryption Algorithm
|
||||
var aea = (AsymetricEncryptionAlgorithmType)(data[offset] >> 5);
|
||||
|
||||
if (aea == AsymetricEncryptionAlgorithmType.RSA)
|
||||
{
|
||||
|
||||
var key = new RSAParameters();
|
||||
|
||||
uint exponentLength = (uint)data[offset++] & 0x1F;
|
||||
|
||||
key.Exponent = DC.Clip(data, offset, exponentLength);
|
||||
offset += exponentLength;
|
||||
|
||||
|
||||
uint keySize = DC.GetUInt16(data, offset);
|
||||
offset += 2;
|
||||
|
||||
key.Modulus = DC.Clip(data, offset, keySize);
|
||||
|
||||
offset += keySize;
|
||||
|
||||
// copy cert data
|
||||
this.publicRawData = new byte[offset - oOffset];
|
||||
Buffer.BlockCopy(data, (int)oOffset, publicRawData, 0, publicRawData.Length);
|
||||
|
||||
|
||||
if (privateKeyIncluded)
|
||||
{
|
||||
uint privateKeyLength = (keySize * 3) + (keySize / 2);
|
||||
uint halfKeySize = keySize / 2;
|
||||
|
||||
this.privateRawData = DC.Clip(data, offset, privateKeyLength);
|
||||
|
||||
key.D = DC.Clip(data, offset, keySize);
|
||||
offset += keySize;
|
||||
key.DP = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.DQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.InverseQ = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.P = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
key.Q = DC.Clip(data, offset, halfKeySize);
|
||||
offset += halfKeySize;
|
||||
}
|
||||
|
||||
// setup rsa
|
||||
this.rsa = RSA.Create();// new RSACryptoServiceProvider();
|
||||
this.rsa.ImportParameters(key);
|
||||
|
||||
this.signature = DC.Clip(data, offset, length - (offset - oOffset));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public UserCertificate(ulong id, string username, DomainCertificate domainCertificate, DateTime issueDate,
|
||||
DateTime expireDate, HashFunctionType hashFunction = HashFunctionType.SHA1, uint ip = 0, byte[] ip6 = null)
|
||||
: base(id, issueDate, expireDate, hashFunction)
|
||||
{
|
||||
// assign type
|
||||
var cr = new BinaryList();
|
||||
|
||||
//id
|
||||
cr.AddUInt64(id);
|
||||
|
||||
// ip
|
||||
this.ip = ip;
|
||||
this.ip6 = ip6;
|
||||
|
||||
cr.AddUInt32(ip);
|
||||
|
||||
|
||||
if (ip6?.Length == 16)
|
||||
cr.AddUInt8Array(ip6);
|
||||
else
|
||||
cr.AddUInt8Array(new byte[16]);
|
||||
|
||||
|
||||
// dates
|
||||
this.issueDate = DateTime.UtcNow;
|
||||
this.expireDate = expireDate;
|
||||
|
||||
cr.AddDateTime(issueDate)
|
||||
.AddDateTime(expireDate);
|
||||
|
||||
|
||||
// domain
|
||||
this.domainId = domainCertificate.Id;
|
||||
cr.AddUInt64(domainCertificate.Id);
|
||||
this.domain = domainCertificate.Domain;
|
||||
cr.AddUInt8((byte)domainCertificate.Domain.Length)
|
||||
.AddUInt8Array(Encoding.ASCII.GetBytes(domainCertificate.Domain));
|
||||
|
||||
|
||||
// username
|
||||
this.username = username;
|
||||
|
||||
cr.AddUInt8((byte)(username.Length))
|
||||
.AddUInt8Array(Encoding.ASCII.GetBytes(username));
|
||||
|
||||
// hash function (SHA1)
|
||||
cr.AddUInt8((byte)((byte)hashFunction << 4));// (byte)0x10);
|
||||
|
||||
// public key
|
||||
|
||||
rsa = RSA.Create();// new RSACryptoServiceProvider(2048);
|
||||
rsa.KeySize = 2048;
|
||||
// write public certificate file
|
||||
|
||||
var key = rsa.ExportParameters(true);
|
||||
publicRawData = new BinaryList().AddUInt8((byte)key.Exponent.Length)
|
||||
.AddUInt8Array(key.Exponent)
|
||||
.AddUInt16((ushort)key.Modulus.Length)
|
||||
.AddUInt8Array(key.Modulus).ToArray();
|
||||
|
||||
|
||||
// sign it
|
||||
this.signature = domainCertificate.Sign(publicRawData);
|
||||
|
||||
|
||||
// store private info
|
||||
privateRawData = DC.Merge(key.D, key.DP, key.DQ, key.InverseQ, key.P, key.Q, signature);
|
||||
|
||||
}
|
||||
|
||||
public override bool Save(string filename, bool includePrivate = false)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (includePrivate)
|
||||
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData));
|
||||
else
|
||||
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature));
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override byte[] Serialize(bool includePrivate = false)
|
||||
{
|
||||
if (includePrivate)
|
||||
return DC.Merge(publicRawData, signature, privateRawData);
|
||||
else
|
||||
return DC.Merge(publicRawData, signature);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Cryptography
|
||||
{
|
||||
// Enums
|
||||
public enum AsymetricEncryptionAlgorithmType
|
||||
{
|
||||
RSA = 0,
|
||||
DSA = 1,
|
||||
ECDSA = 2
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Cryptography
|
||||
{
|
||||
public enum SymetricEncryptionAlgorithmType
|
||||
{
|
||||
AES = 0,
|
||||
Blowfish = 1,
|
||||
DES = 2
|
||||
}
|
||||
}
|
41
Esyur/Security/Integrity/HashFunctionType.cs
Normal file
41
Esyur/Security/Integrity/HashFunctionType.cs
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Integrity
|
||||
{
|
||||
public enum HashFunctionType
|
||||
{
|
||||
MD5 = 0,
|
||||
SHA1,
|
||||
SHA256,
|
||||
SHA384,
|
||||
SHA512
|
||||
}
|
||||
}
|
157
Esyur/Security/Integrity/SHA256.cs
Normal file
157
Esyur/Security/Integrity/SHA256.cs
Normal file
@ -0,0 +1,157 @@
|
||||
using Esyur.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esyur.Security.Integrity
|
||||
{
|
||||
public static class SHA256
|
||||
{
|
||||
|
||||
static uint RROT(uint n, int d)
|
||||
{
|
||||
return (n >> d) | (n << (32 - d));
|
||||
}
|
||||
|
||||
public static byte[] Compute(byte[] msg)
|
||||
{
|
||||
/*
|
||||
Note 1: All variables are 32 bit unsigned integers and addition is calculated modulo 2^32
|
||||
Note 2: For each round, there is one round constant k[i] and one entry in the message schedule array w[i], 0 ≤ i ≤ 63
|
||||
Note 3: The compression function uses 8 working variables, a through h
|
||||
Note 4: Big-endian convention is used when expressing the constants in this pseudocode,
|
||||
and when parsing message block data from bytes to words, for example,
|
||||
the first word of the input message "abc" after padding is 0x61626380
|
||||
*/
|
||||
|
||||
// Initialize hash values:
|
||||
// (first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
|
||||
|
||||
var hash = new uint[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
|
||||
|
||||
// Initialize array of round constants:
|
||||
// (first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
|
||||
var k = new uint[] {
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
|
||||
|
||||
|
||||
|
||||
// Pre-processing:
|
||||
// begin with the original message of length L bits
|
||||
ulong L = (ulong)msg.Length * 8;
|
||||
|
||||
// append a single '1' bit
|
||||
// append K '0' bits, where K is the minimum number >= 0 such that L + 1 + K + 64 is a multiple of 512
|
||||
|
||||
var K = 512 - ((L + 1 + 64) % 512);
|
||||
|
||||
if (K == 512)
|
||||
K = 0;
|
||||
|
||||
var paddingLength = (K + 1) / 8;
|
||||
var paddingBytes = new byte[paddingLength];
|
||||
paddingBytes[0] = 0x80;
|
||||
|
||||
var data = new BinaryList().AddUInt8Array(msg).AddUInt8Array(paddingBytes).AddUInt64(L).ToArray();
|
||||
|
||||
|
||||
|
||||
// append L as a 64-bit big-endian integer, making the total post-processed length a multiple of 512 bits
|
||||
|
||||
// Process the message in successive 512-bit chunks:
|
||||
// break message into 512-bit chunks
|
||||
// for each chunk
|
||||
|
||||
for (var chunk = 0; chunk < data.Length; chunk += 64)
|
||||
{
|
||||
// create a 64-entry message schedule array w[0..63] of 32-bit words
|
||||
// (The initial values in w[0..63] don't matter, so many implementations zero them here)
|
||||
// copy chunk into first 16 words w[0..15] of the message schedule array
|
||||
|
||||
var w = new uint[64];
|
||||
for (var i = 0; i < 16; i++)
|
||||
w[i] = data.GetUInt32((uint)(chunk + (i * 4)));
|
||||
|
||||
//for(var i = 16; i < 64; i++)
|
||||
// w[i] = 0;
|
||||
|
||||
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
|
||||
// for i from 16 to 63
|
||||
// s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
|
||||
// s1 := (w[i-2] rightrotate 17) xor (w[i-2] rightrotate 19) xor (w[i-2] rightshift 10)
|
||||
// w[i] := w[i-16] + s0 + w[i-7] + s1
|
||||
|
||||
for (var i = 16; i < 64; i++)
|
||||
{
|
||||
var s0 = SHA256.RROT(w[i - 15], 7) ^ SHA256.RROT(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
||||
var s1 = SHA256.RROT(w[i - 2], 17) ^ SHA256.RROT(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
||||
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
||||
}
|
||||
|
||||
// Initialize working variables to current hash value:
|
||||
var a = hash[0];
|
||||
var b = hash[1];
|
||||
var c = hash[2];
|
||||
var d = hash[3];
|
||||
var e = hash[4];
|
||||
var f = hash[5];
|
||||
var g = hash[6];
|
||||
var h = hash[7];
|
||||
|
||||
|
||||
// Compression function main loop:
|
||||
for (var i = 0; i < 64; i++)
|
||||
{
|
||||
var S1 = SHA256.RROT(e, 6) ^ SHA256.RROT(e, 11) ^ SHA256.RROT(e, 25);
|
||||
var ch = (e & f) ^ ((~e) & g);
|
||||
var temp1 = h + S1 + ch + k[i] + w[i];
|
||||
var S0 = SHA256.RROT(a, 2) ^ SHA256.RROT(a, 13) ^ SHA256.RROT(a, 22);
|
||||
var maj = (a & b) ^ (a & c) ^ (b & c);
|
||||
uint temp2 = S0 + maj;
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = (d + temp1) >> 0;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = (temp1 + temp2) >> 0;
|
||||
}
|
||||
|
||||
// Add the compressed chunk to the current hash value:
|
||||
|
||||
hash[0] = (hash[0] + a) >> 0;
|
||||
hash[1] = (hash[1] + b) >> 0;
|
||||
hash[2] = (hash[2] + c) >> 0;
|
||||
hash[3] = (hash[3] + d) >> 0;
|
||||
hash[4] = (hash[4] + e) >> 0;
|
||||
hash[5] = (hash[5] + f) >> 0;
|
||||
hash[6] = (hash[6] + g) >> 0;
|
||||
hash[7] = (hash[7] + h) >> 0;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Produce the final hash value (big-endian):
|
||||
//digest := hash := h0 append h1 append h2 append h3 append h4 append h5 append h6 append h7
|
||||
|
||||
var results = new BinaryList();
|
||||
for (var i = 0; i < 8; i++)
|
||||
results.AddUInt32(hash[i]);
|
||||
|
||||
|
||||
return results.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
40
Esyur/Security/Membership/IDomain.cs
Normal file
40
Esyur/Security/Membership/IDomain.cs
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Resource;
|
||||
using Esyur.Security.Authority;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Authority
|
||||
{
|
||||
public interface IDomain : IResource
|
||||
{
|
||||
string Name { get; }
|
||||
DomainCertificate Certificate { get; }
|
||||
}
|
||||
}
|
74
Esyur/Security/Membership/IMembership.cs
Normal file
74
Esyur/Security/Membership/IMembership.cs
Normal file
@ -0,0 +1,74 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Esyur.Data;
|
||||
using Esyur.Net.IIP;
|
||||
using Esyur.Core;
|
||||
using Esyur.Security.Authority;
|
||||
using Esyur.Resource;
|
||||
|
||||
namespace Esyur.Security.Membership
|
||||
{
|
||||
public interface IMembership:IResource
|
||||
{
|
||||
//IUser[] GetUsers(QueryFilter<string> user);
|
||||
|
||||
//bool AddCertificate(Certificate certificate);
|
||||
|
||||
//CACertificate[] GetCACertificates(string authority);
|
||||
//UserCertificate[] GetUserCertificate(string user, string domain);
|
||||
//DomainCertificate[] GetDomainCertificates(string domain);
|
||||
|
||||
|
||||
AsyncReply<bool> UserExists(string username, string domain);
|
||||
AsyncReply<byte[]> GetPassword(string username, string domain);
|
||||
AsyncReply<bool> Login(Session session);
|
||||
AsyncReply<bool> Logout(Session session);
|
||||
|
||||
//ClientAuthentication Authenticate(string username, byte[] credentials, int flag);
|
||||
//HostAuthentication Authenticate(DomainCertificate domainCertificate);
|
||||
//CoHostAuthentication Authenticate(DomainCertificate hostCertificate, int hostId);
|
||||
|
||||
/*
|
||||
object GetUserInfo(User user, string field);
|
||||
object[] GetUserInfo(User user, string[] fields);
|
||||
|
||||
bool SetUserInfo(User user, string field, object value);
|
||||
bool SetUserInfo(User user, KeyList<string, object> info);
|
||||
*/
|
||||
|
||||
//bool AddUser(User user, KeyList<string, object> info);
|
||||
//bool RemoveUser(string username);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
41
Esyur/Security/Membership/IUser.cs
Normal file
41
Esyur/Security/Membership/IUser.cs
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Core;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Membership
|
||||
{
|
||||
public interface IUser
|
||||
{
|
||||
string Username
|
||||
{
|
||||
get;
|
||||
}
|
||||
}
|
||||
}
|
50
Esyur/Security/Permissions/ActionType.cs
Normal file
50
Esyur/Security/Permissions/ActionType.cs
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Permissions
|
||||
{
|
||||
public enum ActionType
|
||||
{
|
||||
Attach,
|
||||
Delete,
|
||||
Execute,
|
||||
GetProperty,
|
||||
SetProperty,
|
||||
CreateResource,
|
||||
UpdateAttributes,
|
||||
InquireAttributes,
|
||||
AddParent,
|
||||
RemoveParent,
|
||||
AddChild,
|
||||
RemoveChild,
|
||||
Rename,
|
||||
ReceiveEvent
|
||||
}
|
||||
}
|
56
Esyur/Security/Permissions/IPermissionsManager.cs
Normal file
56
Esyur/Security/Permissions/IPermissionsManager.cs
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Net;
|
||||
using Esyur.Resource;
|
||||
using Esyur.Resource.Template;
|
||||
using Esyur.Security.Authority;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esyur.Security.Permissions
|
||||
{
|
||||
public interface IPermissionsManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Check for permission.
|
||||
/// </summary>
|
||||
/// <param name="resource">IResource.</param>
|
||||
/// <param name="session">Caller sessions.</param>
|
||||
/// <param name="action">Action type</param>
|
||||
/// <param name="member">Function, property or event to check for permission.</param>
|
||||
/// <param name="inquirer">Permission inquirer object.</param>
|
||||
/// <returns>Allowed or denined.</returns>
|
||||
Ruling Applicable(IResource resource, Session session, ActionType action, MemberTemplate member, object inquirer = null);
|
||||
|
||||
bool Initialize(Structure settings, IResource resource);
|
||||
|
||||
Structure Settings { get; }
|
||||
}
|
||||
}
|
13
Esyur/Security/Permissions/Ruling.cs
Normal file
13
Esyur/Security/Permissions/Ruling.cs
Normal file
@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esyur.Security.Permissions
|
||||
{
|
||||
public enum Ruling
|
||||
{
|
||||
Denied,
|
||||
Allowed,
|
||||
DontCare
|
||||
}
|
||||
}
|
52
Esyur/Security/Permissions/StorePermissionsManager.cs
Normal file
52
Esyur/Security/Permissions/StorePermissionsManager.cs
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Resource;
|
||||
using Esyur.Resource.Template;
|
||||
using Esyur.Security.Authority;
|
||||
|
||||
namespace Esyur.Security.Permissions
|
||||
{
|
||||
public class StorePermissionsManager : IPermissionsManager
|
||||
{
|
||||
Structure settings;
|
||||
|
||||
public Structure Settings => settings;
|
||||
|
||||
public Ruling Applicable(IResource resource, Session session, ActionType action, MemberTemplate member, object inquirer = null)
|
||||
{
|
||||
return resource.Instance.Store.Instance.Applicable(session, action, member, inquirer);
|
||||
}
|
||||
|
||||
public bool Initialize(Structure settings, IResource resource)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
126
Esyur/Security/Permissions/UserPermissionsManager.cs
Normal file
126
Esyur/Security/Permissions/UserPermissionsManager.cs
Normal file
@ -0,0 +1,126 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Esyur.Data;
|
||||
using Esyur.Core;
|
||||
using Esyur.Resource;
|
||||
using Esyur.Resource.Template;
|
||||
using Esyur.Security.Authority;
|
||||
|
||||
namespace Esyur.Security.Permissions
|
||||
{
|
||||
public class UserPermissionsManager : IPermissionsManager
|
||||
{
|
||||
IResource resource;
|
||||
Structure settings;
|
||||
|
||||
public Structure Settings => settings;
|
||||
|
||||
public Ruling Applicable(IResource resource, Session session, ActionType action, MemberTemplate member, object inquirer)
|
||||
{
|
||||
Structure userPermissions = null;
|
||||
|
||||
if (settings.ContainsKey(session.RemoteAuthentication.FullName))
|
||||
userPermissions = settings[session.RemoteAuthentication.FullName] as Structure;
|
||||
else if (settings.ContainsKey("public"))
|
||||
userPermissions = settings["public"] as Structure;
|
||||
else
|
||||
return Ruling.Denied;
|
||||
|
||||
if (action == ActionType.Attach)// || action == ActionType.Delete)
|
||||
{
|
||||
if ((string)userPermissions["_attach"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.Delete)
|
||||
{
|
||||
if ((string)userPermissions["_delete"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action== ActionType.InquireAttributes)
|
||||
{
|
||||
if ((string)userPermissions["_get_attributes"] == "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.UpdateAttributes)
|
||||
{
|
||||
if ((string)userPermissions["_set_attributes"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.AddChild)
|
||||
{
|
||||
if ((string)userPermissions["_add_child"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.RemoveChild)
|
||||
{
|
||||
if ((string)userPermissions["_remove_child"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.AddParent)
|
||||
{
|
||||
if ((string)userPermissions["_add_parent"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.RemoveParent)
|
||||
{
|
||||
if ((string)userPermissions["_remove_parent"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (action == ActionType.Rename)
|
||||
{
|
||||
if ((string)userPermissions["_rename"] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
else if (userPermissions.ContainsKey(member?.Name))
|
||||
{
|
||||
Structure methodPermissions = userPermissions[member.Name] as Structure;
|
||||
if ((string)methodPermissions[action.ToString()] != "yes")
|
||||
return Ruling.Denied;
|
||||
}
|
||||
|
||||
return Ruling.DontCare;
|
||||
}
|
||||
|
||||
public UserPermissionsManager()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UserPermissionsManager(Structure settings)
|
||||
{
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public bool Initialize(Structure settings, IResource resource)
|
||||
{
|
||||
this.resource = resource;
|
||||
this.settings = settings;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user