Esempio n. 1
0
        public JSON EncodeToJSON(byte[] body)
        {
            JSON   obj          = new JSON();
            string strProtected = "";

            if (objProtected.Count > 0)
            {
                strProtected = Message.base64urlencode(UTF8Encoding.UTF8.GetBytes(objProtected.ToString()));
                obj.Add("protected", strProtected);
            }

            if (objUnprotected.Count > 0)
            {
                obj.Add("header", objUnprotected);                           // Add unprotected attributes
            }
            String str = "";

            if (objProtected.ContainsKey("b64") && objProtected["b64"].AsBoolean() == false)
            {
                str += strProtected + "." + UTF8Encoding.UTF8.GetString(body);
            }
            else
            {
                str += strProtected + "." + Message.base64urlencode(body);
            }

            obj.Add("signature", Sign(UTF8Encoding.UTF8.GetBytes(str)));

            return(obj);
        }
Esempio n. 2
0
        public static Message DecodeFromString(string messageData)
        {
            JSON message = new JSON();

            //  We need to figure out if this is the compact or one of the JSON encoded versions.
            //  We guess this is going to be based on the first character - either it is a '{' or something else

            if (messageData[0] == '{')
            {
                message = JSON.Parse(messageData);
            }
            else
            {
                //  Split the string based on periods
                string[] rgData = messageData.Split('.');

                if (rgData.Length == 3)
                {
                    message = new JSON();

                    if (rgData[1].Length > 0)
                    {
                        message.Add("payload", rgData[1]);
                    }

                    JSON signature = new JSON();
                    signature.Add("protected", rgData[0]);
                    signature.Add("signature", rgData[2]);

                    JSON sigs = new JSON();
                    sigs.Add(signature);
                    message.Add("signatures", sigs);
                }
                else if (rgData.Length == 5)
                {
                    message = new JSON();
                    message.Add("protected", rgData[0]);
                    message.Add("iv", rgData[2]);
                    message.Add("ciphertext", rgData[3]);
                    message.Add("tag", rgData[4]);

                    JSON recip = new JSON();
                    recip.Add("encrypted_key", rgData[1]);

                    JSON recips = new JSON();
                    recips.Add(recip);

                    message.Add("recipients", recips);
                }
            }

            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }
Esempio n. 3
0
 public void AddUnprotected(string name, JSON value)
 {
     if (objProtected.ContainsKey(name))
     {
         objProtected.Remove(name);
     }
     if (objUnprotected.ContainsKey(name))
     {
         objUnprotected[name] = value;
     }
     else
     {
         objUnprotected.Add(name, value);
     }
 }
Esempio n. 4
0
        public void SetClaim(string claim, JSON value)
        {
            switch (claim)
            {
            case "iss":
            case "sub":
            case "aud":
                if (value.nodeType != JsonType.text)
                {
                    throw new JwtException("Claim value type is incorrect for the claim");
                }
                break;

            case "exp":
            case "nbf":
            case "iat":
                if (value.nodeType != JsonType.number)
                {
                    throw new JwtException("Claim value type is incorrect for the claim");
                }
                break;

            case "jti":
                if (value.nodeType != JsonType.text)
                {
                    throw new JwtException("Claim value type is incorrect for the claim");
                }
                break;

            default:
                //  We don't know how to check this
                break;
            }

            claims.Add(claim, value);
        }
Esempio n. 5
0
        public JSON EncodeToJSON()
        {
            JSON obj = new JSON();

            if (objUnprotected.Count > 0)
            {
                obj.Add("unprotected", objUnprotected);                           // Add unprotected attributes
            }
            //  Look at the world of base64 encoded bodies.
            //   If any signer has the b64 false, then all of them need to.
            //   Then change our body if needed

            int  b64Found = 0;
            bool b64Value = true;

            foreach (Signer key in signerList)
            {
                JSON attr = key.FindAttribute("b64", true);
                if (attr != null)
                {
                    if (b64Found == 0)
                    {
                        b64Value = attr.AsBoolean();
                    }
                    else if (b64Value != attr.AsBoolean())
                    {
                        throw new JOSE_Exception("Not all signers using the same value for b64");
                    }
                    b64Found += 1;
                }
            }

            if (b64Value)
            {
                obj.Add("payload", base64urlencode(payload));
            }
            else
            {
                if (b64Found != signerList.Count)
                {
                    throw new JOSE_Exception("Not all signers using the same value for b64");
                }
                obj.Add("payload", UTF8Encoding.UTF8.GetString(payload));
            }

            if ((signerList.Count == 1) && !forceAsArray)
            {
                JSON recipient = signerList[0].EncodeToJSON(payload);

                foreach (KeyValuePair <string, JSON> pair in recipient.map)
                {
                    obj.Add(pair.Key, pair.Value);
                }
            }
            else if (signerList.Count > 0)
            {
                JSON signers = new JSON();

                foreach (Signer key in signerList)
                {
                    signers.Add(key.EncodeToJSON(payload));
                }
                obj.Add("signatures", signers);
            }

            return(obj);
        }