Example #1
0
        static Com.AugustCellars.JOSE.Recipient GetRecipient(CBORObject control)
        {
            Com.AugustCellars.JOSE.JWK key;

            if (!control.ContainsKey("alg"))
            {
                throw new Exception("Recipient missing alg field");
            }

            if (control.ContainsKey("key"))
            {
                key = new Com.AugustCellars.JOSE.JWK(control["key"]);
            }
            else if (control.ContainsKey("pwd"))
            {
                key = new Com.AugustCellars.JOSE.JWK();
                key.Add("kty", "oct");
                key.Add("k", Com.AugustCellars.JOSE.Message.base64urlencode(Encoding.UTF8.GetBytes(control["pwd"].AsString())));
            }
            else
            {
                throw new Exception("No key defined for a recipient");
            }

            Com.AugustCellars.JOSE.Recipient recipient = new Com.AugustCellars.JOSE.Recipient(key, control["alg"].AsString());

            //  Double check that alg is the same as in the attributes

            recipient.ClearProtected();
            recipient.ClearUnprotected();

            if (control.ContainsKey("protected"))
            {
                AddAttributes(recipient, control["protected"], 0);
            }
            if (control.ContainsKey("unprotected"))
            {
                AddAttributes(recipient, control["unprotected"], 1);
            }

            if (control.ContainsKey("sender_key"))
            {
                Com.AugustCellars.JOSE.JWK myKey = new Com.AugustCellars.JOSE.JWK(control["sender_key"]);
                recipient.SetSenderKey(myKey);
            }

            return(recipient);
        }