Пример #1
0
        public BaseMessage(XmlDocument message, Settings settings, MessageAction messageAction)
        {
            this.Message       = message;
            this.MessageAction = messageAction;

            this.Settings    = settings;
            this.signMessage = SignMessage.Create(this.Settings);

            Dictionary <string, MessageType> types = new Dictionary <string, MessageType>();

            types.Add("InvoiceRequest", MessageType.Invoice);
            types.Add("BusinessPremiseRequest", MessageType.BusinessPremise);
            types.Add("EchoRequest", MessageType.Echo);

            string root = this.Message.DocumentElement.LocalName;

            if (types.ContainsKey(root))
            {
                this.MessageType = types[root];
            }
            else
            {
                this.MessageType = MessageType.Unknown;
            }
        }
Пример #2
0
        public void testSign()
        {
            SignMessage instance = new SignMessage();

            instance.Encode();
            // TODO review the generated test code and remove the default call to fail.
            /// fail("The test case is a prototype.");
        }
Пример #3
0
        public void testEncodeCBORObject()
        {
            SignMessage instance  = new SignMessage();
            CBORObject  expResult = null;
            CBORObject  result    = instance.EncodeToCBORObject();

            assertEquals(expResult, result);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }
Пример #4
0
        static Message ProcessSign(CBORObject control, ref bool fDirty)
        {
            CBORObject input = control["input"];
            CBORObject sign  = input["sign"];
            CBORObject signers;

            SignMessage msg = new SignMessage();

            if (!input.ContainsKey("plaintext"))
            {
                throw new Exception("missing plaintext field");
            }
            msg.SetContent(input["plaintext"].AsString());

            if (sign.ContainsKey("protected"))
            {
                AddAttributes(msg, sign["protected"], 0);
            }
            if (sign.ContainsKey("unprotected"))
            {
                AddAttributes(msg, sign["unprotected"], 1);
            }
            if (sign.ContainsKey("unsent"))
            {
                AddAttributes(msg, sign["unsent"], 2);
            }

            if ((!sign.ContainsKey("signers")) || (sign["signers"].Type != CBORType.Array))
            {
                throw new Exception("Missing or malformed recipients");
            }
            foreach (CBORObject recip in sign["signers"].Values)
            {
                msg.AddSigner(GetSigner(recip));
            }

            {
                msg.Encode();

                signers = Program.GetSection(Program.GetSection(control, "intermediates"), "signers", CBORType.Array);


                for (int iSigner = 0; iSigner < msg.SignerList.Count; iSigner++)
                {
                    while (signers.Count < msg.SignerList.Count)
                    {
                        signers.Add(CBORObject.NewMap());
                    }

                    Program.SetField(signers[iSigner], "ToBeSign", msg.SignerList[iSigner].ToBeSigned, ref fDirty);
                }
            }

            return(msg);
        }
Пример #5
0
        public void testValidate()
        {
            Signer      signerToUse = null;
            SignMessage instance    = new SignMessage();
            Boolean     expResult   = false;
            Boolean     result      = instance.Validate(signerToUse);

            Assert.AreEqual(expResult, (result));
            // TODO review the generated test code and remove the default call to fail.
            // fail("The test case is a prototype.");
        }
Пример #6
0
        public void testGetSignerCount()
        {
            SignMessage msg = new SignMessage();

            Assert.AreEqual(msg.SignerList.Count, (0));

            Signer r = new Signer();

            msg.AddSigner(r);
            Assert.AreEqual(msg.SignerList.Count, (1));
        }
Пример #7
0
        public void testGetSigner()
        {
            int         iSigner   = 0;
            SignMessage instance  = new SignMessage();
            Signer      expResult = null;
            Signer      result    = instance.getSigner(iSigner);

            assertEquals(expResult, result);
            // TODO review the generated test code and remove the default call to fail.
            fail("The test case is a prototype.");
        }
Пример #8
0
        static void BuildCompact(CBORObject control, JwkSet keys)
        {
            //  Encrypted or Signed?
            if (control.ContainsKey("signing"))
            {
                SignMessage sign   = new SignMessage();
                Signer      signer = new Signer(keys[0]);

                sign.SetContent(control["input"]["payload"].AsString());
                sign.AddSigner(signer);

                CBORObject xx = control["signing"]["protected"];
                foreach (CBORObject key in xx.Keys)
                {
                    signer.AddAttribute(key, xx[key], Attributes.PROTECTED);
                }

                string output = sign.EncodeCompressed();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
            else if (control.ContainsKey("encrypting_key"))
            {
                EncryptMessage enc = new EncryptMessage();
                CBORObject     xx  = control["encrypting_content"]["protected"];
                foreach (CBORObject key in xx.Keys)
                {
                    enc.AddAttribute(key, xx[key], Attributes.PROTECTED);
                }

                Recipient recip = new Recipient(keys[0], control["input"]["alg"].AsString(), enc);

                enc.AddRecipient(recip);
                enc.SetContent(control["input"]["plaintext"].AsString());

                string output = enc.EncodeCompressed();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
        }
Пример #9
0
        static void CheckMessage(Message msg, JWK key, CBORObject input)
        {
            if (msg.GetType() == typeof(EncryptMessage))
            {
                EncryptMessage enc = (EncryptMessage)msg;

                Recipient recipient = enc.RecipientList[0];
                recipient.SetKey(key);

                try {
                    enc.Decrypt(recipient);
                }
                catch (Exception e) { Console.WriteLine("Failed to decrypt " + e.ToString()); return; }

                if (enc.GetContentAsString() != input["plaintext"].AsString())
                {
                    Console.WriteLine("Plain text does not match");
                }
            }
            else if (msg.GetType() == typeof(SignMessage))
            {
                SignMessage sig = (SignMessage)msg;

                try {
                    try {
                        sig.GetContentAsString();
                    }
                    catch (System.Exception) {
                        sig.SetContent(input["payload"].AsString());
                    }
                    sig.Validate(key);

                    if (sig.GetContentAsString() != input["payload"].AsString())
                    {
                        Console.WriteLine("Plain text does not match");
                    }
                }
                catch (Exception e) { Console.WriteLine("Failed to verify " + e.ToString()); return; }
            }
        }
Пример #10
0
        static void CheckMessage(Message msg, Key key, JSON input)
        {
            if (msg.GetType() == typeof(EncryptMessage))
            {
                EncryptMessage enc = (EncryptMessage)msg;

                try {
                    enc.Decrypt(key);
                }
                catch (Exception e) { Console.WriteLine("Failed to decrypt " + e.ToString()); return; }

                if (enc.GetContentAsString() != input["plaintext"].AsString())
                {
                    Console.WriteLine("Plain text does not match");
                }
            }
            else if (msg.GetType() == typeof(SignMessage))
            {
                SignMessage sig = (SignMessage)msg;

                try {
                    try {
                        sig.GetContentAsString();
                    }
                    catch (System.Exception) {
                        sig.SetContent(input["payload"].AsString());
                    }
                    sig.Verify(key);

                    if (sig.GetContentAsString() != input["payload"].AsString())
                    {
                        Console.WriteLine("Plain text does not match");
                    }
                }
                catch (Exception e) { Console.WriteLine("Failed to verify " + e.ToString()); return; }
            }
        }
Пример #11
0
        static bool ValidateSigned(CBORObject cnControl)
        {
            CBORObject cnInput = cnControl["input"];
            CBORObject cnMessage;
            CBORObject cnSigners;
            bool       fFailBody = false;

            fFailBody = HasFailMarker(cnControl);

            try
            {
                cnMessage = cnInput["sign"];
                cnSigners = cnMessage["signers"];

                foreach (string format in Formats)
                {
                    if (!cnControl["output"].ContainsKey(format))
                    {
                        continue;
                    }

                    string rgb;
                    if (format == "compact")
                    {
                        rgb = cnControl["output"][format].AsString();
                    }
                    else
                    {
                        rgb = cnControl["output"][format].ToJSONString();
                    }

                    int i = 0;
                    foreach (CBORObject cnSigner in cnSigners.Values)
                    {
                        SignMessage signMsg = null;

                        try {
                            Message msg = Message.DecodeFromString(rgb);
                            signMsg = (SignMessage)msg;
                        }
                        catch (Exception e) {
                            if (fFailBody)
                            {
                                return(true);
                            }
                            throw e;
                        }

                        // SetReceivingAttributes(signMsg, cnMessage);

                        JWK    cnKey   = GetKey(cnSigner["key"]);
                        Signer hSigner = signMsg.SignerList[i];

                        SetReceivingAttributes(hSigner, cnSigner);

                        hSigner.SetKey(cnKey);

                        bool fFailSigner = HasFailMarker(cnSigner);

                        try {
                            bool f = signMsg.Validate(hSigner);
                            if (!f && !(fFailBody || fFailSigner))
                            {
                                return(false);
                            }
                        }
                        catch (Exception) {
                            if (!fFailBody && !fFailSigner)
                            {
                                return(false);
                            }
                        }

                        i++;
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(true);
        }