예제 #1
0
        public void GetValueAsBytesSucceeds(ContentTestCase @case)
        {
            byte[] content = GetDummyContent(@case);
            var    writer  = new CborWriter();

            writer.WriteByteString(content);

            CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode());

            AssertExtensions.SequenceEqual(content, headerValue.GetValueAsBytes());

            Span <byte> buffer = new byte[content.Length];
            int         length = headerValue.GetValueAsBytes(buffer);

            Assert.Equal(content.Length, length);
            AssertExtensions.SequenceEqual(content, buffer);
        }
예제 #2
0
        public void FromEncodedValue(byte[] encodedValue)
        {
            var headerValue = CoseHeaderValue.FromEncodedValue(encodedValue);

            AssertExtensions.SequenceEqual(encodedValue, headerValue.EncodedValue.Span);

            // make sure is readable.
            var reader = new CborReader(headerValue.EncodedValue);

            reader.SkipValue();

            headerValue = CoseHeaderValue.FromEncodedValue(encodedValue.AsSpan());
            AssertExtensions.SequenceEqual(encodedValue, headerValue.EncodedValue.Span);

            // make sure is readable.
            reader = new CborReader(headerValue.EncodedValue);
            reader.SkipValue();
        }
예제 #3
0
        public void GetValueAsBytesBeingIndefiniteLengthSucceeds(ContentTestCase @case)
        {
            Verify(0);
            Verify(1);
            Verify(10);

            void Verify(int repetitions)
            {
                byte[] content = GetDummyContent(@case);
                var    writer  = new CborWriter();

                writer.WriteStartIndefiniteLengthByteString();
                for (int i = 0; i < repetitions; i++)
                {
                    writer.WriteByteString(content);
                }
                writer.WriteEndIndefiniteLengthByteString();

                int expectedLength = content.Length * repetitions;

                CoseHeaderValue     headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode());
                ReadOnlySpan <byte> result      = headerValue.GetValueAsBytes();

                Assert.Equal(expectedLength, result.Length);

                for (int i = 0; i < expectedLength; i += content.Length)
                {
                    AssertExtensions.SequenceEqual(content, result.Slice(i, content.Length));
                }

                Span <byte> buffer = new byte[expectedLength];
                int         length = headerValue.GetValueAsBytes(buffer);

                Assert.Equal(expectedLength, length);

                for (int i = 0; i < expectedLength; i += content.Length)
                {
                    AssertExtensions.SequenceEqual(content, buffer.Slice(i, content.Length));
                }
            }
        }
예제 #4
0
        public void GetValueAsThrows(byte[] encodedValue, GetValueAs method)
        {
            CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(encodedValue);

            if (method == GetValueAs.Int32)
            {
                Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsInt32());
            }
            else if (method == GetValueAs.String)
            {
                Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsString());
            }
            else if (method == GetValueAs.Bytes)
            {
                Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsBytes());
            }
            else
            {
                Assert.Equal(GetValueAs.BytesSpan, method);
                Memory <byte> buffer = new byte[1024]; // big enough to not throw ArgumentException.
                Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsBytes(buffer.Span));
            }
        }
예제 #5
0
        public void SignWithAllCborTypesAsHeaderValue(bool useProtectedMap, byte[] encodedValue)
        {
            var myLabel = new CoseHeaderLabel(42);

            CoseHeaderMap protectedHeaders   = GetHeaderMapWithAlgorithm(DefaultAlgorithm);
            CoseHeaderMap unprotectedHeaders = GetEmptyHeaderMap();

            (useProtectedMap ? protectedHeaders : unprotectedHeaders)[myLabel] = CoseHeaderValue.FromEncodedValue(encodedValue);

            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedProtectedHeaders   = GetExpectedProtectedHeaders(DefaultAlgorithm);
            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedUnprotectedHeaders = GetEmptyExpectedHeaders();

            (useProtectedMap ? expectedProtectedHeaders : expectedUnprotectedHeaders).Add((myLabel, encodedValue));

            ReadOnlySpan <byte> encodedMessage = Sign(s_sampleContent, DefaultKey, DefaultHash, protectedHeaders, unprotectedHeaders);

            AssertCoseSignMessage(encodedMessage, s_sampleContent, DefaultKey, DefaultAlgorithm, expectedProtectedHeaders, expectedUnprotectedHeaders);

            // Verify it is transported correctly.
            CoseMessage           message           = Decode(encodedMessage);
            ReadOnlyMemory <byte> roundtrippedValue = GetSigningHeaderMap(message, useProtectedMap)[myLabel].EncodedValue;

            AssertExtensions.SequenceEqual(encodedValue, roundtrippedValue.Span);
        }
예제 #6
0
        public void GetValueAsStringBeingIndefiniteLengthSucceeds(string value)
        {
            Verify(0);
            Verify(1);
            Verify(10);

            void Verify(int repetitions)
            {
                var writer = new CborWriter();

                writer.WriteStartIndefiniteLengthTextString();
                for (int i = 0; i < repetitions; i++)
                {
                    writer.WriteTextString(value);
                }
                writer.WriteEndIndefiniteLengthTextString();

                CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode());

                string expectedValue = string.Join("", Enumerable.Repeat(value, repetitions));

                Assert.Equal(expectedValue, headerValue.GetValueAsString());
            }
        }
예제 #7
0
        public void MultiSign_SignWithAllCborTypesAsHeaderValue_BodyHeaders(bool useProtectedMap, byte[] encodedValue)
        {
            if (MessageKind != CoseMessageKind.MultiSign)
            {
                return;
            }

            var myLabel = new CoseHeaderLabel(42);

            CoseHeaderMap signProtectedHeaders   = GetHeaderMapWithAlgorithm(DefaultAlgorithm);
            CoseHeaderMap signUnprotectedHeaders = GetEmptyHeaderMap();

            CoseHeaderMap bodyProtectedHeaders   = GetEmptyHeaderMap();
            CoseHeaderMap bodyUnprotectedHeaders = GetEmptyHeaderMap();

            (useProtectedMap ? bodyProtectedHeaders : bodyUnprotectedHeaders)[myLabel] = CoseHeaderValue.FromEncodedValue(encodedValue);

            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedSignProtectedHeaders   = GetExpectedProtectedHeaders(DefaultAlgorithm);
            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedSignUnprotectedHeaders = GetEmptyExpectedHeaders();

            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedProtectedHeaders   = GetEmptyExpectedHeaders();
            List <(CoseHeaderLabel, ReadOnlyMemory <byte>)> expectedUnprotectedHeaders = GetEmptyExpectedHeaders();

            (useProtectedMap ? expectedProtectedHeaders : expectedUnprotectedHeaders).Add((myLabel, encodedValue));

            CoseSigner          signer         = GetCoseSigner(DefaultKey, DefaultHash, signProtectedHeaders, signUnprotectedHeaders);
            ReadOnlySpan <byte> encodedMessage = Sign(s_sampleContent, signer, bodyProtectedHeaders, bodyUnprotectedHeaders);

            AssertCoseSignMessage(
                encodedMessage,
                s_sampleContent,
                DefaultKey,
                DefaultAlgorithm,
                expectedSignProtectedHeaders,
                expectedSignUnprotectedHeaders,
                null,
                expectedProtectedHeaders,
                expectedUnprotectedHeaders);

            // Verify it is transported correctly.
            CoseMessage           message           = Decode(encodedMessage);
            ReadOnlyMemory <byte> roundtrippedValue = (useProtectedMap ? message.ProtectedHeaders : message.UnprotectedHeaders)[myLabel].EncodedValue;

            AssertExtensions.SequenceEqual(encodedValue, roundtrippedValue.Span);
        }