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); }
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)); }
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"]); } }