Пример #1
0
    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);
    }
Пример #2
0
    public static byte[] HistoryComposer(KeyList <PropertyTemplate, PropertyValue[]> history,
                                         DistributedConnection connection, bool prependLength = false)
    {
        //@TODO:Test
        var rt = new BinaryList();

        for (var i = 0; i < history.Count; i++)
        {
            rt.AddUInt8(history.Keys.ElementAt(i).Index)
            .AddUInt8Array(Codec.Compose(history.Values.ElementAt(i), connection));
        }

        if (prependLength)
        {
            rt.InsertInt32(0, rt.Length);
        }

        return(rt.ToArray());
    }
Пример #3
0
    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);
    }
Пример #4
0
    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);
    }