Esempio n. 1
0
        public static Either <FormatError, SealedBiscuit> Make(Block authority, List <Block> blocks, byte[] secret)
        {
            try
            {
                using HMACSHA256 hmac = new HMACSHA256(secret);
                Format.Schema.Block b = authority.Serialize();
                using MemoryStream stream = new MemoryStream();
                b.WriteTo(stream);
                byte[] authorityData = stream.ToArray();

                List <byte> toHash = new List <byte>(authorityData);

                List <byte[]> blocksData = new List <byte[]>();
                foreach (Block bl in blocks)
                {
                    Format.Schema.Block b2 = bl.Serialize();
                    using MemoryStream stream2 = new MemoryStream();
                    b2.WriteTo(stream2);
                    toHash.AddRange(stream2.ToArray());
                    blocksData.Add(stream2.ToArray());
                }

                byte[] signature = hmac.ComputeHash(toHash.ToArray());// sha256_HMAC.doFinal();
                return(new SealedBiscuit(authorityData, blocksData, signature));
            }
            catch (Exception e)
            {
                return(new SerializationError(e.ToString()));
            }
        }
Esempio n. 2
0
        public Either <FormatError, SerializedBiscuit> Append(RNGCryptoServiceProvider rng, KeyPair keypair, Block block)
        {
            Format.Schema.Block b = block.Serialize();
            try
            {
                MemoryStream stream = new MemoryStream();
                b.WriteTo(stream);
                byte[] data = stream.ToArray();

                TokenSignature signature = this.signature.Sign(rng, keypair, data);

                List <RistrettoElement> keys = new List <RistrettoElement>();
                keys.AddRange(this.keys);
                keys.Add(keypair.PublicKey);

                List <byte[]> blocks = new List <byte[]>();
                blocks.AddRange(this.blocks);
                blocks.Add(data);

                return(new Right(new SerializedBiscuit(authority, blocks, keys, signature)));
            }
            catch (IOException e)
            {
                return(new Left(new SerializationError(e.ToString())));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Serializes a Block to its Protobuf representation
        /// </summary>
        /// <returns></returns>
        public Format.Schema.Block Serialize()
        {
            Format.Schema.Block block = new Format.Schema.Block()
            {
                Index = (uint)this.Index,
            };

            block.Symbols.AddRange(this.Symbols.Symbols);

            if (this.Context.Any())
            {
                block.Context = this.Context;
            }

            foreach (var fact in Facts)
            {
                block.FactsV1.Add(fact.Serialize());
            }


            foreach (Rule rule in this.Rules)
            {
                block.RulesV1.Add(rule.Serialize());
            }

            foreach (Check check in this.Checks)
            {
                block.ChecksV1.Add(check.Serialize());
            }

            block.Version = SerializedBiscuit.MAX_SCHEMA_VERSION;
            return(block);
        }
Esempio n. 4
0
 /// <summary>
 /// Deserializes a Block from a byte array
 /// </summary>
 /// <param name="slice"></param>
 /// <returns></returns>
 static public Either <FormatError, Block> FromBytes(byte[] slice)
 {
     try
     {
         Format.Schema.Block data = Format.Schema.Block.Parser.ParseFrom(slice);
         return(Block.Deserialize(data));
     }
     catch (InvalidProtocolBufferException e)
     {
         return(new DeserializationError(e.ToString()));
     }
 }
Esempio n. 5
0
 public Either <FormatError, byte[]> ToBytes()
 {
     Format.Schema.Block b = this.Serialize();
     try
     {
         using MemoryStream stream           = new MemoryStream();
         using CodedOutputStream codedStream = new CodedOutputStream(stream);
         b.WriteTo(codedStream);
         codedStream.Flush();
         return(stream.ToArray());
     }
     catch (IOException e)
     {
         return(new SerializationError(e.ToString()));
     }
 }
Esempio n. 6
0
        static public Either <FormatError, SerializedBiscuit> Make(RNGCryptoServiceProvider rng, KeyPair root, Block authority)
        {
            Format.Schema.Block b = authority.Serialize();
            try
            {
                using MemoryStream stream = new MemoryStream();
                b.WriteTo(stream);
                byte[] data = stream.ToArray();

                TokenSignature          signature = new TokenSignature(rng, root, data);
                List <RistrettoElement> keys      = new List <RistrettoElement>
                {
                    root.PublicKey
                };

                return(new SerializedBiscuit(data, new List <byte[]>(), keys, signature));
            }
            catch (IOException e)
            {
                return(new SerializationError(e.ToString()));
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Deserializes a block from its Protobuf representation
        /// </summary>
        /// <param name="b"></param>
        /// <returns></returns>
        static public Either <FormatError, Block> Deserialize(Format.Schema.Block b)
        {
            uint version = b.Version;

            if (version > SerializedBiscuit.MAX_SCHEMA_VERSION)
            {
                return(new VersionError(SerializedBiscuit.MAX_SCHEMA_VERSION, version));
            }

            SymbolTable symbols = new SymbolTable();

            foreach (string s in b.Symbols)
            {
                symbols.Add(s);
            }

            List <Fact>  facts  = new List <Fact>();
            List <Rule>  rules  = new List <Rule>();
            List <Check> checks = new List <Check>();

            if (version == 0)
            {
                foreach (Format.Schema.FactV0 fact in b.FactsV0)
                {
                    Either <FormatError, Fact> res = Fact.DeserializeV0(fact);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        facts.Add(res.Right);
                    }
                }


                foreach (Format.Schema.RuleV0 rule in b.RulesV0)
                {
                    Either <FormatError, Rule> res = Rule.DeserializeV0(rule);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        rules.Add(res.Right);
                    }
                }


                foreach (Format.Schema.CaveatV0 caveat in b.CaveatsV0)
                {
                    Either <FormatError, Check> res = Check.DeserializeV0(caveat);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        checks.Add(res.Right);
                    }
                }
            }
            else
            {
                foreach (Format.Schema.FactV1 fact in b.FactsV1)
                {
                    Either <FormatError, Fact> res = Fact.DeserializeV1(fact);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        facts.Add(res.Right);
                    }
                }


                foreach (Format.Schema.RuleV1 rule in b.RulesV1)
                {
                    Either <FormatError, Rule> res = Rule.DeserializeV1(rule);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        rules.Add(res.Right);
                    }
                }


                foreach (Format.Schema.CheckV1 check in b.ChecksV1)
                {
                    Either <FormatError, Check> res = Check.DeserializeV1(check);
                    if (res.IsLeft)
                    {
                        return(res.Left);
                    }
                    else
                    {
                        checks.Add(res.Right);
                    }
                }
            }

            return(new Right(new Block(b.Index, symbols, b.Context, facts, rules, checks)));
        }