示例#1
0
        public void DecodeFromCBORObject(CBORObject obj)
        {
            if (obj.Count != 5)
            {
                throw new CoseException("Invalid MAC structure");
            }

            //  Protected values.
            if (obj[0].Type == CBORType.ByteString)
            {
                byte[] data = obj[0].GetByteString();
                if (data.Length > 0)
                {
                    objProtected = CBORObject.DecodeFromBytes(data);
                    if (objProtected.Type != CBORType.Map)
                    {
                        throw new CoseException("Invalid MAC Structure");
                    }
                }
                else
                {
                    objProtected = CBORObject.NewMap();
                }
            }
            else
            {
                throw new CoseException("Invalid MAC structure");
            }

            //  Unprotected attributes
            if (obj[1].Type == PeterO.Cbor.CBORType.Map)
            {
                objUnprotected = obj[1];
            }
            else
            {
                throw new CoseException("Invalid MAC Structure");
            }

            // Plain Text
            if (obj[2].Type == CBORType.ByteString)
            {
                rgbContent = obj[2].GetByteString();
            }
            else if (!obj[2].IsNull)                 // Detached content - will need to get externally
            {
                throw new CoseException("Invalid MAC Structure");
            }

            // Authentication tag
            if (obj[3].Type == CBORType.ByteString)
            {
                rgbTag = obj[3].GetByteString();
            }
            else
            {
                throw new CoseException("Invalid MAC Structure");
            }


            // Recipients
            if (obj[4].Type == CBORType.Array)
            {
                // An array of recipients to be processed
                for (int i = 0; i < obj[4].Count; i++)
                {
                    Recipient recip = new Recipient();
                    recip.DecodeFromCBORObject(obj[4][i]);
                    recipientList.Add(recip);
                }
            }
            else
            {
                throw new CoseException("Invalid MAC Structure");
            }
        }