2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00
This commit is contained in:
2019-08-07 05:18:27 +03:00
parent 2caae61910
commit 8d06fd05ad
74 changed files with 2302 additions and 1336 deletions

View File

@ -23,7 +23,7 @@ SOFTWARE.
*/
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Misc;
using Esiur.Security.Cryptography;
using Esiur.Security.Integrity;
@ -131,16 +131,20 @@ namespace Esiur.Security.Authority
BinaryList cr = new BinaryList();
// make header
cr.Append(id, issueDate, expireDate);
cr.AddUInt64(id)
.AddDateTime(issueDate)
.AddDateTime(expireDate);
// hash function
cr.Append((byte)((byte)hashFunction << 4));
cr.AddUInt8((byte)((byte)hashFunction << 4));
this.hashFunction = hashFunction;
// CA Name
this.name = authorityName;
cr.Append((byte)(authorityName.Length), Encoding.ASCII.GetBytes(authorityName));
cr.AddUInt8((byte)(authorityName.Length))
.AddUInt8Array(Encoding.ASCII.GetBytes(authorityName));
// public key
rsa = RSA.Create();// new RSACryptoServiceProvider(2048);
@ -148,14 +152,16 @@ namespace Esiur.Security.Authority
RSAParameters dRSAKey = rsa.ExportParameters(true);
cr.Append((byte)dRSAKey.Exponent.Length, dRSAKey.Exponent, (ushort)dRSAKey.Modulus.Length, dRSAKey.Modulus);
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)
@ -163,9 +169,15 @@ namespace Esiur.Security.Authority
try
{
if (includePrivate)
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.CAPrivate, publicRawData, privateRawData));
File.WriteAllBytes(filename, new BinaryList()
.AddUInt8((byte)CertificateType.CAPrivate)
.AddUInt8Array(publicRawData)
.AddUInt8Array(privateRawData)
.ToArray());
else
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.CAPublic, publicRawData));
File.WriteAllBytes(filename, new BinaryList()
.AddUInt8((byte)CertificateType.CAPublic)
.AddUInt8Array(publicRawData).ToArray());
return true;
}
@ -178,7 +190,10 @@ namespace Esiur.Security.Authority
public override byte[] Serialize(bool includePrivate = false)
{
if (includePrivate)
return BinaryList.ToBytes(publicRawData, privateRawData);
return new BinaryList()
.AddUInt8Array(publicRawData)
.AddUInt8Array(privateRawData)
.ToArray();
else
return publicRawData;
}

View File

@ -23,7 +23,7 @@ SOFTWARE.
*/
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Misc;
using Esiur.Security.Cryptography;
using Esiur.Security.Integrity;

View File

@ -166,44 +166,50 @@ namespace Esiur.Security.Authority
var cr = new BinaryList();
// id
cr.Append(id);
cr.AddUInt64(id);
// ip
this.ip = ip;
this.ip6 = ip6;
cr.Append(ip);
cr.AddUInt32(ip);
if (ip6?.Length == 16)
cr.Append(ip6);
cr.AddUInt8Array(ip6);
else
cr.Append(new byte[16]);
cr.AddUInt8Array(new byte[16]);
cr.Append(issueDate, expireDate);
cr.AddDateTime(issueDate)
.AddDateTime(expireDate);
// domain
this.domain = domain;
cr.Append((byte)(domain.Length), Encoding.ASCII.GetBytes(domain));
cr.AddUInt8((byte)(domain.Length))
.AddUInt8Array(Encoding.ASCII.GetBytes(domain));
// CA
this.caName = authority.Name;
cr.Append((byte)(authority.Name.Length), Encoding.ASCII.GetBytes(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.Append(caId);
cr.AddUInt64(caId);
// public key
rsa = RSA.Create();// new RSACryptoServiceProvider(2048);
rsa.KeySize = 2048;
RSAParameters dRSAKey = rsa.ExportParameters(true);
cr.Append((byte)dRSAKey.Exponent.Length, dRSAKey.Exponent, (ushort)dRSAKey.Modulus.Length, dRSAKey.Modulus, AsymetricEncryptionAlgorithmType.RSA);
cr.AddUInt8((byte)dRSAKey.Exponent.Length)
.AddUInt8Array(dRSAKey.Exponent)
.AddUInt16((ushort)dRSAKey.Modulus.Length)
.AddUInt8Array(dRSAKey.Modulus);
publicRawData = cr.ToArray();
@ -220,9 +226,9 @@ namespace Esiur.Security.Authority
try
{
if (includePrivate)
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPrivate, publicRawData, signature, privateRawData));
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData));
else
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPublic, publicRawData, signature));
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature));
return true;
}
@ -235,9 +241,9 @@ namespace Esiur.Security.Authority
public override byte[] Serialize(bool includePrivate = false)
{
if (includePrivate)
return BinaryList.ToBytes(publicRawData, signature, privateRawData);
return DC.Merge(publicRawData, signature, privateRawData);
else
return BinaryList.ToBytes(publicRawData, signature);
return DC.Merge(publicRawData, signature);
}
}

View File

@ -22,7 +22,7 @@ SOFTWARE.
*/
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Net;
using Esiur.Resource;
using System;

View File

@ -170,42 +170,45 @@ namespace Esiur.Security.Authority
var cr = new BinaryList();
//id
cr.Append(id);
cr.AddUInt64(id);
// ip
this.ip = ip;
this.ip6 = ip6;
cr.Append(ip);
cr.AddUInt32(ip);
if (ip6?.Length == 16)
cr.Append(ip6);
cr.AddUInt8Array(ip6);
else
cr.Append(new byte[16]);
cr.AddUInt8Array(new byte[16]);
// dates
this.issueDate = DateTime.UtcNow;
this.expireDate = expireDate;
cr.Append(issueDate, expireDate);
cr.AddDateTime(issueDate)
.AddDateTime(expireDate);
// domain
this.domainId = domainCertificate.Id;
cr.Append(domainCertificate.Id);
cr.AddUInt64(domainCertificate.Id);
this.domain = domainCertificate.Domain;
cr.Append((byte)domainCertificate.Domain.Length, Encoding.ASCII.GetBytes(domainCertificate.Domain));
cr.AddUInt8((byte)domainCertificate.Domain.Length)
.AddUInt8Array(Encoding.ASCII.GetBytes(domainCertificate.Domain));
// username
this.username = username;
cr.Append((byte)(username.Length), Encoding.ASCII.GetBytes(username));
cr.AddUInt8((byte)(username.Length))
.AddUInt8Array(Encoding.ASCII.GetBytes(username));
// hash function (SHA1)
cr.Append((byte)((byte)hashFunction << 4));// (byte)0x10);
cr.AddUInt8((byte)((byte)hashFunction << 4));// (byte)0x10);
// public key
@ -214,7 +217,10 @@ namespace Esiur.Security.Authority
// write public certificate file
var key = rsa.ExportParameters(true);
publicRawData = BinaryList.ToBytes((byte)key.Exponent.Length, key.Exponent, (ushort)key.Modulus.Length, key.Modulus);
publicRawData = new BinaryList().AddUInt8((byte)key.Exponent.Length)
.AddUInt8Array(key.Exponent)
.AddUInt16((ushort)key.Modulus.Length)
.AddUInt8Array(key.Modulus).ToArray();
// sign it
@ -231,9 +237,9 @@ namespace Esiur.Security.Authority
try
{
if (includePrivate)
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPrivate, publicRawData, signature, privateRawData));
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPrivate }, publicRawData, signature, privateRawData));
else
File.WriteAllBytes(filename, BinaryList.ToBytes((byte)CertificateType.DomainPublic, publicRawData, signature));
File.WriteAllBytes(filename, DC.Merge(new byte[] { (byte)CertificateType.DomainPublic }, publicRawData, signature));
return true;
}
@ -246,9 +252,9 @@ namespace Esiur.Security.Authority
public override byte[] Serialize(bool includePrivate = false)
{
if (includePrivate)
return BinaryList.ToBytes(publicRawData, signature, privateRawData);
return DC.Merge(publicRawData, signature, privateRawData);
else
return BinaryList.ToBytes(publicRawData, signature);
return DC.Merge(publicRawData, signature);
}
}
}