Example #1
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 noAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.SetContent(strContent);
            msg.Encrypt(rgbKey128);
        }
        public void noContent()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true);
            msg.Encrypt(rgbKey128);
        }
Example #4
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 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);
        }
        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);
        }
Example #9
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();
        }
Example #10
0
        public void noAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

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

            msg.AddRecipient(r);
            msg.Encrypt();
        }
Example #11
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"));
        }
Example #12
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();
        }
Example #13
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();
        }
Example #14
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"));
        }
Example #15
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();
        }
Example #16
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"));
        }
Example #17
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"*/));
        }
Example #18
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()));
        }
        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);
        }
Example #20
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);
        }
Example #22
0
        public void encryptNoEmitContent()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

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

            msg.AddRecipient(r);
            msg.Encrypt();
            CBORObject cn = msg.EncodeToCBORObject();


            Assert.IsTrue(cn[2].IsNull);
        }
        public void nullKeyForDecrypt()
        {
            EncryptMessage msg = new EncryptMessage(true, true);

            //        thrown.expect(CoseException.class);
            //        thrown.expectMessage("No Encrypted Content Specified");

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

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.Decrypt(null);
        }
        public void roundTripDetached()
        {
            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);

            byte[] content = msg.GetEncryptedContent();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.SetEncryptedContent(content);
            msg.Decrypt(rgbKey128);
        }
Example #25
0
        public void roundTrip()
        {
            EncryptMessage msg = new EncryptMessage();

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

            msg.AddRecipient(r);
            msg.Encrypt();
            byte[] rgbMsg = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgbMsg);
            r   = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);

            Assert.AreEqual <string>(msg.GetContentAsString(), strContent);
        }
Example #26
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();
                    rgbBody = msg.GetEncryptedContent();
                }
                else if (m_msgToSign.GetType() == typeof(Encrypt0Message))
                {
                    Encrypt0Message msg = (Encrypt0Message)m_msgToSign;
                    rgbBody = msg.GetEncryptedContent();
                    if (rgbBody == null)
                    {
                        throw new CoseException("Need to encrypt message before countersignatures can be processed.");
                    }
                }
                cborBodyAttributes = m_msgToSign.ProtectedMap;
            }
            else if (m_signerToSign != null)
            {
                CBORObject obj = m_signerToSign.EncodeToCBORObject();
                cborBodyAttributes = m_signerToSign.ProtectedMap;
            }
            else
            {
                throw new CoseException("Internal state error - NYI");
            }


            CBORObject signed = base.EncodeToCBORObject(cborBodyAttributes.EncodeToBytes(), rgbBody);

            return(signed[2]);
        }
Example #27
0
        public void roundTripDetached()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

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

            msg.AddRecipient(r);
            msg.Encrypt();

            byte[] content = msg.GetEncryptedContent();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            msg.SetEncryptedContent(content);
            r = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);
        }
Example #28
0
        public void noContentForDecrypt()
        {
            EncryptMessage msg = new EncryptMessage(true, false);

            //        thrown.expect(CoseException.class);
            //        thrown.expectMessage("No Enveloped Content Specified");

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

            msg.AddRecipient(r);
            msg.Encrypt();

            byte[] rgb = msg.EncodeToBytes();

            msg = (EncryptMessage)Message.DecodeFromBytes(rgb);
            r   = msg.RecipientList[0];
            r.SetKey(key128);
            msg.Decrypt(r);
        }
Example #29
0
        public void TestRoundTrip3()
        {
            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);
            msg.AddRecipient(recipient128);
            msg.Encrypt();

            List <Recipient> rList = msg.RecipientList;

            Assert.AreEqual(rList.Count(), (1));

            CBORObject rgbMsg = msg.EncodeToCBORObject();

            msg = (EncryptMessage)Message.DecodeFromCBOR(rgbMsg, Tags.Encrypt);
            Recipient r = msg.RecipientList[0];

            r.SetKey(cnKey128);
            byte[] contentNew = msg.Decrypt(r);

            CollectionAssert.AreEqual(contentNew, (rgbContent));
        }