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 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); }
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 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); }
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 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); }
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 noContent() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.Encrypt(rgbKey128); }
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")); }
public void nullKey() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, Attributes.PROTECTED); msg.SetContent(strContent); msg.Encrypt(); }
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); }
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 unknownAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, CBORObject.FromObject("Unknown"), true); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void unsupportedAlgorithm() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.HMAC_SHA_256, true); msg.SetContent(strContent); msg.Encrypt(rgbKey128); }
public void incorrectKeySize() { EncryptMessage msg = new EncryptMessage(); msg.AddAttribute(HeaderKeys.Algorithm, AlgorithmValues.AES_GCM_128, true); msg.SetContent(strContent); msg.Encrypt(rgbKey256); }
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(); }
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(); }
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")); }
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(); }
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); }
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 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); }
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 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)); }
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"]); } }