예제 #1
0
        internal static CoseHeaderMap GetHeaderMapWithAlgorithm(CoseAlgorithm algorithm = CoseAlgorithm.ES256)
        {
            var protectedHeaders = new CoseHeaderMap();

            protectedHeaders.SetValue(CoseHeaderLabel.Algorithm, (int)algorithm);
            return(protectedHeaders);
        }
예제 #2
0
        public void SetValue_KnownHeaders_ThrowIf_IncorrectValue()
        {
            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <InvalidOperationException>(() => map.SetValue(CoseHeaderLabel.Algorithm, ReadOnlySpan <byte> .Empty));
            // [ +label ] (non-empty array) Not yet properly supported.
            //Assert.Throws<NotSupportedException>(() => map.SetValue(CoseHeaderLabel.Critical, ReadOnlySpan<byte>.Empty));
            // tstr / uint
            Assert.Throws <InvalidOperationException>(() => map.SetValue(CoseHeaderLabel.ContentType, -1));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetValue(CoseHeaderLabel.KeyIdentifier, "foo"));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetValue(CoseHeaderLabel.IV, "foo"));
            // bstr
            Assert.Throws <InvalidOperationException>(() => map.SetValue(CoseHeaderLabel.PartialIV, "foo"));
        }
예제 #3
0
        public void Enumerate()
        {
            var map = new CoseHeaderMap();

            map.SetValue(CoseHeaderLabel.Algorithm, (int)ECDsaAlgorithm.ES256);
            map.SetEncodedValue(CoseHeaderLabel.Critical, GetDummyCritHeaderValue());
            map.SetValue(CoseHeaderLabel.ContentType, ContentTypeDummyValue);
            map.SetValue(CoseHeaderLabel.KeyIdentifier, s_sampleContent);
            map.SetValue(CoseHeaderLabel.IV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.PartialIV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.CounterSignature, ReadOnlySpan <byte> .Empty);

            var writer        = new CborWriter();
            int currentHeader = KnownHeaderAlg;

            foreach ((CoseHeaderLabel label, ReadOnlyMemory <byte> encodedValue) in map)
            {
                Assert.Equal(new CoseHeaderLabel(currentHeader), label);
                ReadOnlyMemory <byte> expectedValue = currentHeader switch
                {
                    KnownHeaderAlg => EncodeInt32((int)ECDsaAlgorithm.ES256, writer),
                    KnownHeaderCrit => GetDummyCritHeaderValue(),
                    KnownHeaderContentType => EncodeString(ContentTypeDummyValue, writer),
                    KnownHeaderKid => EncodeBytes(s_sampleContent, writer),
                    KnownHeaderIV or KnownHeaderPartialIV or KnownHeaderCounterSignature => EncodeBytes(ReadOnlySpan <byte> .Empty, writer),
                    _ => throw new InvalidOperationException()
                };
                AssertExtensions.SequenceEqual(expectedValue.Span, encodedValue.Span);
                currentHeader++;
            }
            Assert.Equal(KnownHeaderCounterSignature + 1, currentHeader);
        public void SignVerifyWithCustomHeaders(ECDsaAlgorithm algorithm)
        {
            var protectedHeaders = new CoseHeaderMap();

            protectedHeaders.SetValue(CoseHeaderLabel.Algorithm, (int)algorithm);

            CoseHeaderMap unprotectedHeaders = new CoseHeaderMap();

            unprotectedHeaders.SetValue(CoseHeaderLabel.ContentType, ContentTypeDummyValue);

            ECDsa             key           = ECDsaKeys[algorithm];
            HashAlgorithmName hashAlgorithm = GetHashAlgorithmNameFromCoseAlgorithm((int)algorithm);

            byte[] message = CoseSign1Message.Sign(s_sampleContent, key, hashAlgorithm, protectedHeaders, unprotectedHeaders);
            Assert.True(CoseMessage.DecodeSign1(message).Verify(key));
        }
예제 #5
0
        public void SetValue_GetValue_KnownCoseHeaderLabel()
        {
            var map = new CoseHeaderMap();

            map.SetValue(CoseHeaderLabel.Algorithm, (int)ECDsaAlgorithm.ES256);
            map.SetValue(CoseHeaderLabel.Critical, GetDummyCritHeaderValue());
            map.SetValue(CoseHeaderLabel.ContentType, ContentTypeDummyValue);
            map.SetValue(CoseHeaderLabel.KeyIdentifier, s_sampleContent);
            map.SetValue(CoseHeaderLabel.IV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.PartialIV, ReadOnlySpan <byte> .Empty);
            map.SetValue(CoseHeaderLabel.CounterSignature, ReadOnlySpan <byte> .Empty);

            Assert.Equal((int)ECDsaAlgorithm.ES256, map.GetValueAsInt32(CoseHeaderLabel.Algorithm));
            AssertExtensions.SequenceEqual(GetDummyCritHeaderValue(), map.GetValueAsBytes(CoseHeaderLabel.Critical));
            Assert.Equal(ContentTypeDummyValue, map.GetValueAsString(CoseHeaderLabel.ContentType));
            AssertExtensions.SequenceEqual(s_sampleContent, map.GetValueAsBytes(CoseHeaderLabel.KeyIdentifier));
            AssertExtensions.SequenceEqual(ReadOnlySpan <byte> .Empty, map.GetValueAsBytes(CoseHeaderLabel.IV));
            AssertExtensions.SequenceEqual(ReadOnlySpan <byte> .Empty, map.GetValueAsBytes(CoseHeaderLabel.PartialIV));
            AssertExtensions.SequenceEqual(ReadOnlySpan <byte> .Empty, map.GetValueAsBytes(CoseHeaderLabel.CounterSignature));
        }
        public void SignVerifyWithCustomCoseHeaderMaps()
        {
            foreach ((AsymmetricAlgorithm key, HashAlgorithmName hashAlgorithm, CoseAlgorithm algorithm) in GetKeyHashAlgorithmTriplet())
            {
                var protectedHeaders = new CoseHeaderMap();
                protectedHeaders.SetValue(CoseHeaderLabel.Algorithm, (int)algorithm);

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

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

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

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

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