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.Append(id, issueDate, expireDate);

            // hash function
            cr.Append((byte)((byte)hashFunction << 4));
            this.hashFunction = hashFunction;

            // CA Name
            this.name = authorityName;
            cr.Append((byte)(authorityName.Length), Encoding.ASCII.GetBytes(authorityName));

            // 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);


            publicRawData = cr.ToArray();

            privateRawData = DC.Merge(dRSAKey.D, dRSAKey.DP, dRSAKey.DQ, dRSAKey.InverseQ, dRSAKey.P, dRSAKey.Q);
        }
 public override byte[] Serialize(bool includePrivate = false)
 {
     if (includePrivate)
     {
         return(DC.Merge(publicRawData, signature, privateRawData));
     }
     else
     {
         return(DC.Merge(publicRawData, 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 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 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);
    }