示例#1
0
        public void ReEncodeWithDuplicateHeaderBetweenProtectedAndUnprotected()
        {
            // Algorithm header is duplicated. It is a special case because it is mandatory that the header exists in the protected map.
            CoseSigner  signer = GetCoseSigner(DefaultKey, DefaultHash);
            CoseMessage msg    = Decode(Sign(s_sampleContent, signer));

            GetSigningHeaderMap(msg, getProtectedMap: false).Add(CoseHeaderLabel.Algorithm, (int)DefaultAlgorithm);
            AllEncodeOverloadsShouldThrow(msg);

            // other known header is duplicate.
            CoseHeaderMap protectedHeaders = GetEmptyHeaderMap();

            protectedHeaders.Add(CoseHeaderLabel.ContentType, ContentTypeDummyValue);

            signer = GetCoseSigner(DefaultKey, DefaultHash, protectedHeaders);
            msg    = Decode(Sign(s_sampleContent, signer));

            GetSigningHeaderMap(msg, getProtectedMap: false).Add(CoseHeaderLabel.ContentType, ContentTypeDummyValue);
            AllEncodeOverloadsShouldThrow(msg);

            // not-known int header is duplicate.
            var myLabel = new CoseHeaderLabel(42);

            protectedHeaders = GetEmptyHeaderMap();
            protectedHeaders.Add(myLabel, 42);

            signer = GetCoseSigner(DefaultKey, DefaultHash, protectedHeaders);
            msg    = Decode(Sign(s_sampleContent, signer));

            GetSigningHeaderMap(msg, getProtectedMap: false).Add(myLabel, 42);
            AllEncodeOverloadsShouldThrow(msg);

            // not-known tstr header is duplicate.
            myLabel          = new CoseHeaderLabel("42");
            protectedHeaders = GetEmptyHeaderMap();
            protectedHeaders.Add(myLabel, 42);

            signer = GetCoseSigner(DefaultKey, DefaultHash, protectedHeaders);
            msg    = Decode(Sign(s_sampleContent, signer));

            GetSigningHeaderMap(msg, getProtectedMap: false).Add(myLabel, 42);
            AllEncodeOverloadsShouldThrow(msg);
        }
示例#2
0
        public void MultiSign_ReEncodeWithDuplicateHeaderBetweenProtectedAndUnprotected_BodyProtected()
        {
            if (MessageKind != CoseMessageKind.MultiSign)
            {
                return;
            }

            // known header is duplicate.
            CoseSigner    signer           = GetCoseSigner(DefaultKey, DefaultHash);
            CoseHeaderMap protectedHeaders = GetEmptyHeaderMap();

            protectedHeaders.Add(CoseHeaderLabel.ContentType, ContentTypeDummyValue);

            CoseMessage msg = Decode(Sign(s_sampleContent, signer, protectedHeaders));

            msg.UnprotectedHeaders.Add(CoseHeaderLabel.ContentType, ContentTypeDummyValue);
            AllEncodeOverloadsShouldThrow(msg);

            // not-known int header is duplicate.
            var myLabel = new CoseHeaderLabel(42);

            protectedHeaders = GetEmptyHeaderMap();
            protectedHeaders.Add(myLabel, 42);

            signer = GetCoseSigner(DefaultKey, DefaultHash);
            msg    = Decode(Sign(s_sampleContent, signer, protectedHeaders));

            msg.UnprotectedHeaders.Add(myLabel, 42);
            AllEncodeOverloadsShouldThrow(msg);

            // not-known tstr header is duplicate.
            myLabel          = new CoseHeaderLabel("42");
            protectedHeaders = GetEmptyHeaderMap();
            protectedHeaders.Add(myLabel, 42);

            signer = GetCoseSigner(DefaultKey, DefaultHash);
            msg    = Decode(Sign(s_sampleContent, signer, protectedHeaders));

            msg.UnprotectedHeaders.Add(myLabel, 42);
            AllEncodeOverloadsShouldThrow(msg);
        }
示例#3
0
        public void SetValue_InvalidCoseHeaderValue()
        {
            CoseHeaderLabel[] labelsToTest =
            {
                new CoseHeaderLabel("foo"),
                new CoseHeaderLabel(42),
                CoseHeaderLabel.Algorithm,
                CoseHeaderLabel.ContentType,
                CoseHeaderLabel.CriticalHeaders,
                CoseHeaderLabel.KeyIdentifier
            };

            foreach (CoseHeaderLabel label in labelsToTest)
            {
                var map = new CoseHeaderMap();

                Assert.Throws <ArgumentException>("value", () => map.Add(label, new CoseHeaderValue()));
                Assert.Throws <ArgumentException>("value", () => map[label] = new CoseHeaderValue());

                Assert.Throws <ArgumentException>("value", () => map.Add(label, default(CoseHeaderValue)));
                Assert.Throws <ArgumentException>("value", () => map[label] = default(CoseHeaderValue));
            }
        }
示例#4
0
        public void SignVerifyWithCustomCoseHeaderMaps()
        {
            foreach ((AsymmetricAlgorithm key, HashAlgorithmName hashAlgorithm, CoseAlgorithm algorithm, RSASignaturePadding? padding)
                     in GetKeyHashAlgorithmPaddingQuadruplet())
            {
                var protectedHeaders = GetEmptyHeaderMap();
                protectedHeaders.Add(CoseHeaderLabel.Algorithm, (int)algorithm);

                CoseHeaderMap unprotectedHeaders = new CoseHeaderMap();
                unprotectedHeaders.Add(CoseHeaderLabel.ContentType, ContentTypeDummyValue);

                ReadOnlySpan <byte> encodedMsg = Sign(s_sampleContent, key, hashAlgorithm, protectedHeaders, unprotectedHeaders, padding);

                List <(CoseHeaderLabel, ReadOnlyMemory <byte>)>?expectedProtectedHeaders   = GetExpectedProtectedHeaders(algorithm);
                List <(CoseHeaderLabel, ReadOnlyMemory <byte>)>?expectedUnprotectedHeaders = GetEmptyExpectedHeaders();
                AddEncoded(expectedUnprotectedHeaders, CoseHeaderLabel.ContentType, ContentTypeDummyValue);

                AssertCoseSignMessage(encodedMsg, s_sampleContent, key, algorithm, expectedProtectedHeaders, expectedUnprotectedHeaders);

                CoseMessage decodedMsg = Decode(encodedMsg);
                Assert.True(Verify(decodedMsg, key, s_sampleContent));
            }
        }