Beispiel #1
0
 public void AddProtected(string name, JSON value)
 {
     if (objUnprotected.ContainsKey(name))
     {
         objUnprotected.Remove(name);
     }
     if (objProtected.ContainsKey(name))
     {
         objProtected[name] = value;
     }
     else
     {
         objProtected.Add(name, value);
     }
 }
Beispiel #2
0
        public string EncodeCompact()
        {
            JSON objBody;
            JSON objSigners = null;

            if (signerList.Count != 1)
            {
                throw new JOSE_Exception("Compact mode not supported if more than one signer");
            }
            if (objUnprotected.Count > 0)
            {
                throw new JOSE_Exception("Compact mode not supported if unprotected attributes exist");
            }

            ForceArray(true);
            objBody = EncodeToJSON();

            //  Base64 encoding says kill some messages


            if (objBody.ContainsKey("signatures"))
            {
                objSigners = objBody["signatures"][0];
            }

            string str = "";

            if (objSigners != null && objSigners.ContainsKey("protected"))
            {
                str += objSigners["protected"].AsString();
            }
            str += ".";
            if (objBody.ContainsKey("payload"))
            {
                if (objBody["payload"].AsString().Contains('.'))
                {
                    throw new Exception("Message cannot contain a period character");
                }
                str += objBody["payload"].AsString();
            }
            str += ".";
            if (objSigners != null && objSigners.ContainsKey("signature"))
            {
                str += objSigners["signature"].AsString();
            }

            return(str);
        }
Beispiel #3
0
        public SignMessage(JSON json)
        {
            //  Parse out the message from the JSON


            if (json.ContainsKey("signatures"))
            {
                JSON signers = json["signatures"];
                for (int i = 0; i < signers.Count; i++)
                {
                    Signer signer = new Signer(signers[i]);
                    signerList.Add(signer);
                }
            }
            else if (json.ContainsKey("signature"))
            {
                Signer signer = new Signer(json);
                signerList.Add(signer);
            }

            if (json.ContainsKey("payload"))
            {
                JSON b64 = signerList[0].FindAttribute("b64", true);
                if (b64 != null)
                {
                    if (b64.nodeType != JsonType.boolean)
                    {
                        throw new Exception("Invalid message");
                    }
                    if (b64.AsBoolean())
                    {
                        payloadB64 = UTF8Encoding.UTF8.GetBytes(json["payload"].AsString());
                        payload    = base64urldecode(json["payload"].AsString());
                    }
                    else
                    {
                        payload    = UTF8Encoding.UTF8.GetBytes(json["payload"].AsString());
                        payloadB64 = payload;
                    }
                }
                else
                {
                    payloadB64 = UTF8Encoding.UTF8.GetBytes(json["payload"].AsString());
                    payload    = base64urldecode(json["payload"].AsString());
                }
            }
        }
Beispiel #4
0
        public Signer(JSON jsonSigner)
        {
            if (jsonSigner.ContainsKey("protected"))
            {
                protectedB64 = UTF8Encoding.ASCII.GetBytes(jsonSigner["protected"].AsString());
                objProtected = JSON.Parse(UTF8Encoding.UTF8.GetString(Message.base64urldecode(jsonSigner["protected"].AsString())));
            }
            else
            {
                protectedB64 = new byte[0];
            }

            if (jsonSigner.ContainsKey("header"))
            {
                objUnprotected = jsonSigner["header"];
            }

            signature = Message.base64urldecode(jsonSigner["signature"].AsString());
        }
Beispiel #5
0
        public static Message DecodeFromJSON(JSON message)
        {
            if (message.ContainsKey("iv"))
            {
                EncryptMessage msgx = new EncryptMessage();
                msgx.DecodeFromJSON(message);
                return(msgx);
            }

            return(new SignMessage(message));
        }
Beispiel #6
0
 public Boolean ContainsName(string name)
 {
     return(json.ContainsKey(name));
 }