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
Esiur/Security/Authority/AlienAuthentication.cs
Normal file
41
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public class AlienAuthentication : Authentication
|
||||
{
|
||||
public AlienAuthentication() :
|
||||
base(AuthenticationType.Alien)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
66
Esiur/Security/Authority/Authentication.cs
Normal file
66
Esiur/Security/Authority/Authentication.cs
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
|
||||
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 static Esiur.Net.Packets.IIPAuthPacket;
|
||||
|
||||
namespace Esiur.Security.Authority
|
||||
{
|
||||
public class Authentication
|
||||
{
|
||||
AuthenticationType type;
|
||||
|
||||
public AuthenticationMethod Method { get; set; }
|
||||
|
||||
public ulong TokenIndex { get; set; }
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
14
Esiur/Security/Authority/AuthenticationMethod.cs
Normal file
14
Esiur/Security/Authority/AuthenticationMethod.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Security.Authority
|
||||
{
|
||||
public enum AuthenticationMethod : byte
|
||||
{
|
||||
None,
|
||||
Certificate,
|
||||
Credentials,
|
||||
Token
|
||||
}
|
||||
}
|
42
Esiur/Security/Authority/AuthenticationState.cs
Normal file
42
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public enum AuthenticationState : int
|
||||
{
|
||||
Denied = 0x1,
|
||||
Succeeded = 0x2,
|
||||
Blocked = 0x4,
|
||||
Rejected = 0x8,
|
||||
NeedsUpdate = 0x10,
|
||||
NotFound = 0x20
|
||||
}
|
||||
}
|
40
Esiur/Security/Authority/AuthenticationType.cs
Normal file
40
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public enum AuthenticationType
|
||||
{
|
||||
Host,
|
||||
CoHost,
|
||||
Client,
|
||||
Alien
|
||||
}
|
||||
}
|
202
Esiur/Security/Authority/CACertificate.cs
Normal file
202
Esiur/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 Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Security.Cryptography;
|
||||
using Esiur.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 Esiur.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
Esiur/Security/Authority/Certificate.cs
Normal file
222
Esiur/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 Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Security.Cryptography;
|
||||
using Esiur.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 Esiur.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
Esiur/Security/Authority/CertificateType.cs
Normal file
42
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public enum CertificateType
|
||||
{
|
||||
CAPublic = 0,
|
||||
CAPrivate,
|
||||
DomainPublic,
|
||||
DomainPrivate,
|
||||
UserPublic,
|
||||
UserPrivate
|
||||
}
|
||||
}
|
72
Esiur/Security/Authority/ClientAuthentication.cs
Normal file
72
Esiur/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 Esiur.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
Esiur/Security/Authority/CoHostAuthentication.cs
Normal file
41
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public class CoHostAuthentication : Authentication
|
||||
{
|
||||
public CoHostAuthentication()
|
||||
: base(AuthenticationType.CoHost)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
250
Esiur/Security/Authority/DomainCertificate.cs
Normal file
250
Esiur/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 Esiur.Data;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Security.Cryptography;
|
||||
using Esiur.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 Esiur.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
Esiur/Security/Authority/HostAuthentication.cs
Normal file
43
Esiur/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 Esiur.Security.Authority
|
||||
{
|
||||
public class HostAuthentication : Authentication
|
||||
{
|
||||
|
||||
public HostAuthentication()
|
||||
:base(AuthenticationType.Host)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
61
Esiur/Security/Authority/Session.cs
Normal file
61
Esiur/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 Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Net;
|
||||
using Esiur.Resource;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esiur.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
Esiur/Security/Authority/Source.cs
Normal file
59
Esiur/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 Esiur.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esiur.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
Esiur/Security/Authority/SourceAttributeType.cs
Normal file
80
Esiur/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 Esiur.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esiur.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
Esiur/Security/Authority/UserCertificate.cs
Normal file
260
Esiur/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 Esiur.Data;
|
||||
using Esiur.Security.Cryptography;
|
||||
using Esiur.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 Esiur.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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user