Exemple #1
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"));
        }
Exemple #2
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();
        }
Exemple #3
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();
        }
Exemple #4
0
        public void noAlgorithm()
        {
            EncryptMessage msg = new EncryptMessage();

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

            msg.AddRecipient(r);
            msg.Encrypt();
        }
        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));
        }
Exemple #6
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();
        }
Exemple #7
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();
        }
Exemple #8
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();
        }
        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"));
        }
        public void EncryptNoContent()
        {
            EncryptMessage msg = new EncryptMessage();

            msg.AddRecipient(recipient128);

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

            Assert.AreEqual(e.Message, ("No Content Specified"));
        }
        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"));
        }
        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"*/));
        }
        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."));
        }
Exemple #14
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);
        }
Exemple #15
0
        static void BuildCompact(JSON control, KeySet keys)
        {
            //  Encrypted or Signed?
            if (control.ContainsKey("signing"))
            {
                JOSE.SignMessage sign   = new JOSE.SignMessage();
                JOSE.Signer      signer = new JOSE.Signer(keys[0]);

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

                JSON xx = control["signing"]["protected"];
                foreach (string key in xx.Keys)
                {
                    signer.AddProtected(key, xx[key]);
                }

                string output = sign.EncodeCompact();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
            else if (control.ContainsKey("encrypting_key"))
            {
                JOSE.EncryptMessage enc = new EncryptMessage();
                JSON xx = control["encrypting_content"]["protected"];
                foreach (string key in xx.Keys)
                {
                    enc.AddProtected(key, xx[key]);
                }

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

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


                string output = enc.EncodeCompact();

                Message msg = Message.DecodeFromString(output);

                CheckMessage(msg, keys[0], control["input"]);
            }
        }
Exemple #16
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"]);
            }
        }
Exemple #17
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);
        }
Exemple #18
0
        public void nullKeyForDecrypt()
        {
            EncryptMessage msg = new EncryptMessage(true, true);

            //        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);
            msg.Decrypt(null);
        }
Exemple #19
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);
        }
        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));
        }
Exemple #21
0
        static Message ProcessEnveloped(CBORObject control, ref bool fDirty)
        {
            CBORObject input   = control["input"];
            CBORObject encrypt = input["enveloped"];

            EncryptMessage msg = new EncryptMessage();

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

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

            if (encrypt.ContainsKey("alg"))
            {
                encrypt.Remove(CBORObject.FromObject("alg"));
            }

            if ((!encrypt.ContainsKey("recipients")) || (encrypt["recipients"].Type != CBORType.Array))
            {
                throw new Exception("Missing or malformed recipients");
            }

            foreach (CBORObject recipient in encrypt["recipients"].Values)
            {
                msg.AddRecipient(GetRecipient(recipient));
            }

            {
                msg.Encode();

                CBORObject intermediates = Program.GetSection(control, "intermediates");

//                SetField(intermediates, "AAD_hex", msg.getAADBytes(), ref fDirty);
//                SetField(intermediates, "CEK_hex", msg.getCEK(), ref fDirty);

                CBORObject rList = Program.GetSection(intermediates, "recipients");

//                SaveRecipientDebug(msg.RecipientList, rList, ref fDirty);
            }


#if false
            //  If we want this to fail, look at the different failure methods.
            if (input.ContainsKey("failures"))
            {
                msgOut = ProcessFailures(msgOut, input["failures"], 2);
            }
#endif

            return(msg);
        }