Beispiel #1
0
        public static Message DecodeFromBytes(byte[] messageData, Tags defaultTag = Tags.Unknown)
        {
            CBORObject messageObject = CBORObject.DecodeFromBytes(messageData);

            if (messageObject.Type != CBORType.Array)
            {
                throw new CoseException("Message is not a COSE security message.");
            }

            if (messageObject.IsTagged)
            {
                if (messageObject.GetTags().Count() != 1)
                {
                    throw new CoseException("Malformed message - too many tags");
                }

                if (defaultTag == Tags.Unknown)
                {
                    defaultTag = (COSE.Tags)messageObject.OutermostTag.intValue();
                }
                else if (defaultTag != (COSE.Tags)messageObject.OutermostTag.intValue())
                {
                    throw new CoseException("Passed in tag does not match actual tag");
                }
            }

            switch (defaultTag)
            {
            case Tags.Unknown:
                throw new CoseException("Message was not tagged and no default tagging option given");

            case Tags.Signed:
                SignMessage sig = new SignMessage();
                sig.DecodeFromCBORObject(messageObject);
                return(sig);

            case Tags.Signed0:
                Sign0Message sig0 = new Sign0Message();
                sig0.DecodeFromCBORObject(messageObject);
                return(sig0);

            case Tags.MAC: {
                MACMessage mac = new MACMessage();
                mac.DecodeFromCBORObject(messageObject);
                return(mac);
            }

            case Tags.MAC0:
                MAC0Message mac0 = new MAC0Message();
                mac0.DecodeFromCBORObject(messageObject);
                return(mac0);

            case Tags.Enveloped:         // It is an encrytion message
                EnvelopedMessage enc = new EnvelopedMessage();

                enc.DecodeFromCBORObject(messageObject);
                return(enc);

            case Tags.Encrypted:
                EncryptMessage enc0 = new EncryptMessage();
                enc0.DecodeFromCBORObject(messageObject);
                return(enc0);

            default:
                throw new CoseException("Message is not recognized as a COSE security message.");
            }
        }