Exemple #1
0
        public bool Validate(Recipient recipientReceiver)
        {
            byte[] rgbKey = null;
            int    cbitKey;

            CBORObject alg = FindAttribute(HeaderKeys.Algorithm);

            if (alg.Type == CBORType.TextString)
            {
                switch (alg.AsString())
                {
                case "AES-CMAC-128/64":
                    cbitKey = 128;
                    break;

                case "AES-CMAC-256/64":
                    cbitKey = 256;
                    break;

                default:
                    throw new CoseException("Unknown Algorithm Specified");
                }
            }
            else if (alg.Type == CBORType.Integer)
            {
                switch ((AlgorithmValuesInt)alg.AsInt32())
                {
                case AlgorithmValuesInt.HMAC_SHA_256_64:
                case AlgorithmValuesInt.HMAC_SHA_256:
                    cbitKey = 256;
                    break;

                case AlgorithmValuesInt.HMAC_SHA_384: cbitKey = 384; break;

                case AlgorithmValuesInt.HMAC_SHA_512: cbitKey = 512; break;

                case AlgorithmValuesInt.AES_CBC_MAC_128_64:
                case AlgorithmValuesInt.AES_CBC_MAC_128_128:
                    cbitKey = 128;
                    break;

                case AlgorithmValuesInt.AES_CBC_MAC_256_64:
                case AlgorithmValuesInt.AES_CBC_MAC_256_128:
                    cbitKey = 256;
                    break;

                default:
                    throw new CoseException("MAC algorithm not recognized " + alg.AsInt32());
                }
            }
            else
            {
                throw new CoseException("Algorithm incorrectly encoded");
            }


            foreach (Recipient msgRecpient in _recipientList)
            {
                if (recipientReceiver == msgRecpient)
                {
                    try {
                        rgbKey = msgRecpient.Decrypt(cbitKey, alg);
                    }
                    catch (CoseException) { }
                }
                else if (recipientReceiver.RecipientList.Count > 0)
                {
                    try {
                        rgbKey = recipientReceiver.Decrypt(cbitKey, alg, recipientReceiver);
                    }
                    catch (CoseException) {
                    }
                }
                if (rgbKey != null)
                {
                    break;
                }
            }

            if (rgbKey == null)
            {
                throw new CoseException("Recipient not found");
            }

            byte[] rgbCheck;

            if (alg.Type == CBORType.TextString)
            {
                switch (alg.AsString())
                {
                case "AES-CMAC-128/64":
                case "AES-CMAC-256/64":
                    rgbCheck = AES_CMAC(alg, rgbKey);
                    break;

                default:
                    throw new CoseException("Unknown Algorithm Specified");
                }
            }
            else if (alg.Type == CBORType.Integer)
            {
                switch ((AlgorithmValuesInt)alg.AsInt32())
                {
                case AlgorithmValuesInt.HMAC_SHA_256:
                case AlgorithmValuesInt.HMAC_SHA_384:
                case AlgorithmValuesInt.HMAC_SHA_512:
                case AlgorithmValuesInt.HMAC_SHA_256_64:
                    rgbCheck = HMAC(alg, rgbKey);
                    break;

                case AlgorithmValuesInt.AES_CBC_MAC_128_64:
                case AlgorithmValuesInt.AES_CBC_MAC_128_128:
                case AlgorithmValuesInt.AES_CBC_MAC_256_64:
                case AlgorithmValuesInt.AES_CBC_MAC_256_128:
                    rgbCheck = AES_CBC_MAC(alg, rgbKey);
                    break;

                default:
                    throw new CoseException("MAC algorithm not recognized " + alg.AsInt32());
                }
            }
            else
            {
                throw new CoseException("Algorithm incorrectly encoded");
            }

            bool fReturn = true;

            for (int i = 0; i < rgbCheck.Length; i++)
            {
                fReturn &= (RgbTag[i] == rgbCheck[i]);
            }
            return(fReturn);
        }