Example #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.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);
        }
Example #2
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.Append(id);

            // ip
            this.ip  = ip;
            this.ip6 = ip6;

            cr.Append(ip);


            if (ip6?.Length == 16)
            {
                cr.Append(ip6);
            }
            else
            {
                cr.Append(new byte[16]);
            }


            cr.Append(issueDate, expireDate);

            // domain
            this.domain = domain;
            cr.Append((byte)(domain.Length), Encoding.ASCII.GetBytes(domain));

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

            this.authorityName = authority.Name;

            // CA Index
            //co.KeyIndex = authority.KeyIndex;
            this.caId = authority.Id;
            cr.Append(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);


            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);
        }
Example #3
0
        public ResourceTemplate(Type type)
        {
            // set guid

            var typeName = Encoding.UTF8.GetBytes(type.FullName);
            var hash     = SHA256.Create().ComputeHash(typeName).Clip(0, 16);

            classId   = new Guid(hash);
            className = type.FullName;


#if NETSTANDARD1_5
            PropertyInfo[] propsInfo   = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            EventInfo[]    eventsInfo  = type.GetTypeInfo().GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            MethodInfo[]   methodsInfo = type.GetTypeInfo().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
#else
            PropertyInfo[] propsInfo   = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            EventInfo[]    eventsInfo  = type.GetEvents(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            MethodInfo[]   methodsInfo = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
#endif

            //byte currentIndex = 0;

            byte i = 0;

            foreach (var pi in propsInfo)
            {
                var ps = (ResourceProperty[])pi.GetCustomAttributes(typeof(ResourceProperty), true);
                if (ps.Length > 0)
                {
                    var pt = new PropertyTemplate(this, i++, pi.Name, ps[0].ReadExpansion, ps[0].WriteExpansion, ps[0].Storage);
                    properties.Add(pt);
                }
            }

            i = 0;

            foreach (var ei in eventsInfo)
            {
                var es = (ResourceEvent[])ei.GetCustomAttributes(typeof(ResourceEvent), true);
                if (es.Length > 0)
                {
                    var et = new EventTemplate(this, i++, ei.Name, es[0].Expansion);
                    events.Add(et);
                }
            }

            i = 0;
            foreach (MethodInfo mi in methodsInfo)
            {
                var fs = (ResourceFunction[])mi.GetCustomAttributes(typeof(ResourceFunction), true);
                if (fs.Length > 0)
                {
                    var ft = new FunctionTemplate(this, i++, mi.Name, mi.ReturnType == typeof(void), fs[0].Expansion);
                    functions.Add(ft);
                }
            }

            // append signals
            for (i = 0; i < events.Count; i++)
            {
                members.Add(events[i]);
            }
            // append slots
            for (i = 0; i < functions.Count; i++)
            {
                members.Add(functions[i]);
            }
            // append properties
            for (i = 0; i < properties.Count; i++)
            {
                members.Add(properties[i]);
            }

            // bake it binarily
            var b = new BinaryList();
            b.Append(classId);
            b.Append((byte)className.Length, className);
            b.Append(version);
            b.Append((ushort)members.Count);
            foreach (var ft in functions)
            {
                b.Append(ft.Compose());
            }
            foreach (var pt in properties)
            {
                b.Append(pt.Compose());
            }
            foreach (var et in events)
            {
                b.Append(et.Compose());
            }
            content = b.ToArray();
        }
Example #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.Append(id);

            // ip
            this.ip  = ip;
            this.ip6 = ip6;

            cr.Append(ip);


            if (ip6?.Length == 16)
            {
                cr.Append(ip6);
            }
            else
            {
                cr.Append(new byte[16]);
            }


            // dates
            this.issueDate  = DateTime.UtcNow;
            this.expireDate = expireDate;

            cr.Append(issueDate, expireDate);


            // domain
            this.domainId = domainCertificate.Id;
            cr.Append(domainCertificate.Id);
            this.domain = domainCertificate.Domain;
            cr.Append((byte)domainCertificate.Domain.Length, Encoding.ASCII.GetBytes(domainCertificate.Domain));


            // username
            this.username = username;

            cr.Append((byte)(username.Length), Encoding.ASCII.GetBytes(username));

            // hash function (SHA1)
            cr.Append((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 = BinaryList.ToBytes((byte)key.Exponent.Length, key.Exponent, (ushort)key.Modulus.Length, key.Modulus);


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