public void noAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
        }
Esempio n. 2
0
        public void TestSuccessful()
        {
            var keyStr = "AAECAwQFBgcICQoLDA0ODw==";

            var pipeline = PipelineFactory.CreateEmptySendPipeline();

            var em = new EncryptMessage
            {
                EncryptionKey = keyStr
            };

            pipeline.AddComponent(em, PipelineStage.Encode);
            var message = MessageHelper.Create("<test></test>");

            var    output = pipeline.Execute(message);
            var    result = DecryptMessage(output.BodyPart.Data, keyStr);
            string re;

            using (var sr = new StreamReader(result))
            {
                re = sr.ReadToEnd();
            }

            re = re.Replace("\0", "");
            Assert.AreEqual("<test></test>", re);
        }
Esempio n. 3
0
        public void EncryptCompressed()
        {
            string         msg            = "Ths is some content";
            EncryptMessage encryptMessage = new EncryptMessage();

            encryptMessage.SetContent(msg);
            JWK encryptionKey = JWK.GenerateKey("A128GCM");

            // encryptMessage.AddAttribute(HeaderKeys.EncryptionAlgorithm, CBORObject.FromObject(EncryptionAlgorithm), Attributes.PROTECTED);

            Recipient recipient = new Recipient(encryptionKey);

            encryptMessage.AddRecipient(recipient);
            // recipient.ClearUnprotected();
            if (recipient.RecipientType == RecipientType.Direct && encryptionKey.ContainsName("alg"))
            {
                encryptMessage.AddAttribute("enc", encryptionKey.AsString("alg"), Attributes.PROTECTED);
            }
            else
            {
                encryptMessage.AddAttribute("enc", "A128GCM", Attributes.PROTECTED);
            }

            msg = encryptMessage.EncodeCompressed();
        }
        public void noContent()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.Encrypt(rgbKey128);
        }
Esempio n. 5
0
        public void TestDecodeUnknown()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            // msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED);
            msg.SetContent(rgbContent);

            CBORObject obj = CBORObject.NewMap();

            obj.Add("kty", "oct");
            obj.Add("k", Encoding.UTF8.GetString(Base64.Encode(rgbKey128)));

            JWK key = new JWK(obj);

            Recipient recipient = new Recipient(key, "dir");

            msg.AddRecipient(recipient);
            string rgbMsg = msg.Encode();

            JoseException e = Assert.ThrowsException <JoseException>(() =>
                                                                     msg = (EncryptMessage)Message.DecodeFromString(rgbMsg));

            Assert.AreEqual(e.Message, ("Message was not tagged and no default tagging option given"));
        }
Esempio n. 6
0
        public static Message DecodeFromString(string messageData)
        {
            JSON message = new JSON();

            //  We need to figure out if this is the compact or one of the JSON encoded versions.
            //  We guess this is going to be based on the first character - either it is a '{' or something else

            if (messageData[0] == '{')
            {
                message = JSON.Parse(messageData);
            }
            else
            {
                //  Split the string based on periods
                string[] rgData = messageData.Split('.');

                if (rgData.Length == 3)
                {
                    message = new JSON();

                    if (rgData[1].Length > 0)
                    {
                        message.Add("payload", rgData[1]);
                    }

                    JSON signature = new JSON();
                    signature.Add("protected", rgData[0]);
                    signature.Add("signature", rgData[2]);

                    JSON sigs = new JSON();
                    sigs.Add(signature);
                    message.Add("signatures", sigs);
                }
                else if (rgData.Length == 5)
                {
                    message = new JSON();
                    message.Add("protected", rgData[0]);
                    message.Add("iv", rgData[2]);
                    message.Add("ciphertext", rgData[3]);
                    message.Add("tag", rgData[4]);

                    JSON recip = new JSON();
                    recip.Add("encrypted_key", rgData[1]);

                    JSON recips = new JSON();
                    recips.Add(recip);

                    message.Add("recipients", recips);
                }
            }

            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }
Esempio n. 7
0
        public new CBORObject EncodeToCBORObject()
        {
            CBORObject cborBodyAttributes = null;

            byte[] rgbBody = null;

            if (m_msgToSign != null)
            {
                if (m_msgToSign.GetType() == typeof(EncryptMessage))
                {
                    EncryptMessage msg = (EncryptMessage)m_msgToSign;
                    msg.Encrypt();
                    CBORObject obj = msg.EncodeToCBORObject();
                    if (obj[1].Type != CBORType.ByteString)
                    {
                        throw new Exception("Internal error");
                    }
                    if (obj[3].Type != CBORType.ByteString)
                    {
                        throw new Exception("Internal error");
                    }
                    rgbBody            = obj[3].GetByteString();
                    cborBodyAttributes = obj[1];
                }
            }
            else if (m_signerToSign != null)
            {
                CBORObject obj = m_signerToSign.EncodeToCBORObject();
            }


            return(base.EncodeToCBORObject(cborBodyAttributes.GetByteString(), rgbBody));
        }
        public void TestSuccessful()
        {
            var keyStr = "AAECAwQFBgcICQoLDA0ODw==";

              var pipeline = PipelineFactory.CreateEmptySendPipeline();

            var em = new EncryptMessage
            {
               EncryptionKey = keyStr
            };

            pipeline.AddComponent(em, PipelineStage.Encode);
            var message = MessageHelper.Create("<test></test>");

            var output = pipeline.Execute(message);
            var result = DecryptMessage(output.BodyPart.Data, keyStr);
            string re;
            using (var sr = new StreamReader(result))
            {
                re = sr.ReadToEnd();
            }

            re = re.Replace("\0", "");
            Assert.AreEqual("<test></test>",re);
        }
        public void incorrectKeySize()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey256);
        }
        public void unsupportedAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, true);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
        }
        public void unknownAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), true);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
        }
Esempio n. 12
0
        public void nullKey()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.SetContent(strContent);
            msg.Encrypt();
        }
        public void incorrectIV()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
        }
Esempio n. 14
0
        public void testSetContent_byteArr()
        {
            byte[]  rgbData  = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
            Message instance = new EncryptMessage();

            instance.SetContent(rgbData);

            byte[] result = instance.GetContent();
            Assert.AreEqual(result, (rgbData));
        }
Esempio n. 15
0
        public void noAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Esempio n. 16
0
        public void noContent()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Esempio n. 17
0
        public void EncryptNoRecipients()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Encrypt());

            Assert.AreEqual(e.Message, ("No recipients supplied"));
        }
Esempio n. 18
0
        public static Message DecodeFromJSON(JSON message)
        {
            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }
Esempio n. 19
0
        public void TestRecipientListCount()
        {
            EncryptMessage msg = new EncryptMessage();

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

            Recipient r = new Recipient();

            msg.AddRecipient(r);
            Assert.AreEqual(msg.RecipientList.Count, (1));
        }
Esempio n. 20
0
        public void unsupportedAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, Attributes.PROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Esempio n. 21
0
        public void unknownAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attributes.PROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Esempio n. 22
0
        public void incorrectIV()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), Attributes.UNPROTECTED);
            msg.SetContent(strContent);
            Recipient r = new Recipient(key128, AlgorithmValues.Direct);

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Esempio n. 23
0
        public void EncryptNoAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddRecipient(recipient128);

            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Encrypt());

            Assert.AreEqual(e.Message, ("No Algorithm Specified"));
        }
Esempio n. 24
0
        public void testHasContent()
        {
            Message instance  = new EncryptMessage();
            Boolean expResult = false;
            Boolean result    = instance.HasContent();

            Assert.AreEqual(expResult, (expResult));

            instance.SetContent(new byte[10]);
            result = instance.HasContent();
            Assert.AreEqual(result, (true));
        }
Esempio n. 25
0
        public void testSetContent_String()
        {
            String strData = "12345678";

            byte[] rgbData = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56 };

            Message instance = new EncryptMessage();

            instance.SetContent(strData);
            byte[] result = instance.GetContent();
            Assert.AreEqual(result, (rgbData));
        }
        public void encryptNoEmitContent()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
            CBORObject cn = msg.EncodeToCBORObject();


            Assert.IsTrue(cn[2].IsNull);
        }
Esempio n. 27
0
        public void EncryptUnknownAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddRecipient(recipient128);

            msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Encrypt());

            Assert.AreEqual(e.Message, ("Unknown Algorithm Specified"));
        }
Esempio n. 28
0
        public void EncryptUnsupportedAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddRecipient(recipient128);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Encrypt());

            Assert.AreEqual(e.Message, ("Incorrect key size" /*"Unsupported Algorithm Specified"*/));
        }
Esempio n. 29
0
        public void testDecodeFromBytes_byteArr_MessageTag()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), Attributes.PROTECTED);
            msg.SetContent(rgbContent);
            msg.Encrypt(rgbKey128);
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg);
            Assert.AreEqual(false, (msg.HasContent()));
        }
Esempio n. 30
0
        public void EncryptIncorrectIV()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddRecipient(recipient128);

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV128), Attributes.UNPROTECTED);
            msg.SetContent(rgbContent);
            CoseException e = Assert.ThrowsException <CoseException>(() =>
                                                                     msg.Encrypt());

            Assert.AreEqual(e.Message, ("IV size is incorrect."));
        }
        public void roundTrip()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.AddAttribute(HeaderKeys.IV, CBORObject.FromObject(rgbIV96), false);
            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg);
            msg.Decrypt(rgbKey128);

            Assert.AreEqual <string>(msg.GetContentAsString(), strContent);
        }