public void Enumerate() { var map = new CoseHeaderMap(); SetValue(map, CoseHeaderLabel.Algorithm, (int)ECDsaAlgorithm.ES256, default(SetValueMethod)); SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, GetDummyCritHeaderValue(), default(SetValueMethod)); SetValue(map, CoseHeaderLabel.ContentType, ContentTypeDummyValue, default(SetValueMethod)); SetValue(map, CoseHeaderLabel.KeyIdentifier, s_sampleContent, default(SetValueMethod)); var writer = new CborWriter(); int currentHeader = KnownHeaderAlg; foreach (KeyValuePair <CoseHeaderLabel, CoseHeaderValue> kvp in map) { CoseHeaderLabel label = kvp.Key; CoseHeaderValue value = kvp.Value; 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), _ => throw new InvalidOperationException() }; AssertExtensions.SequenceEqual(expectedValue.Span, value.EncodedValue.Span); currentHeader++; } Assert.Equal(KnownHeaderKid + 1, currentHeader);
public void FromBytesThrowsBufferTooSmall(ContentTestCase @case) { byte[] content = GetDummyContent(@case); CoseHeaderValue headerValue = CoseHeaderValue.FromBytes(content.AsSpan()); Memory <byte> buffer = new byte[content.Length - 1]; Assert.Throws <ArgumentException>("destination", () => headerValue.GetValueAsBytes(buffer.Span)); }
public void GetValueAsStringSucceeds(string value) { var writer = new CborWriter(); writer.WriteTextString(value); CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); Assert.Equal(value, headerValue.GetValueAsString()); }
public void GetValueAsInt32Succeeds(int value) { var writer = new CborWriter(); writer.WriteInt32(value); CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); Assert.Equal(value, headerValue.GetValueAsInt32()); }
public void FromStringSucceeds(string value) { var writer = new CborWriter(); writer.WriteTextString(value); CoseHeaderValue headerValue = CoseHeaderValue.FromString(value); AssertExtensions.SequenceEqual(writer.Encode(), headerValue.EncodedValue.Span); Assert.Equal(value, headerValue.GetValueAsString()); }
public void FromInt32Succeeds(int value) { var writer = new CborWriter(); writer.WriteInt32(value); CoseHeaderValue headerValue = CoseHeaderValue.FromInt32(value); AssertExtensions.SequenceEqual(writer.Encode(), headerValue.EncodedValue.Span); Assert.Equal(value, headerValue.GetValueAsInt32()); }
public void GetValueAsInt32Overflows(long value) { var writer = new CborWriter(); writer.WriteInt64(value); CoseHeaderValue headerValue = CoseHeaderValue.FromEncodedValue(writer.Encode()); Exception ex = Assert.Throws <InvalidOperationException>(() => headerValue.GetValueAsInt32()); Assert.IsType <OverflowException>(ex.InnerException); }
public void SignWithCborNegativeIntegerRepresentationAlgorithmHeaderValue(ulong value) { var writer = new CborWriter(); writer.WriteCborNegativeIntegerRepresentation(value); ReadOnlySpan <byte> encodedValue = writer.Encode(); CoseHeaderMap protectedHeaders = new CoseHeaderMap(); protectedHeaders[CoseHeaderLabel.Algorithm] = CoseHeaderValue.FromEncodedValue(encodedValue); Assert.Throws <CryptographicException>(() => Sign(s_sampleContent, DefaultKey, DefaultHash, protectedHeaders)); }
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); }
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(); }
public void CoseHeaderValue_GetHashCode() { CoseHeaderValue value1 = default; CoseHeaderValue value2 = new CoseHeaderValue(); int value1HashCode = value1.GetHashCode(); Assert.Equal(value1HashCode, value2.GetHashCode()); value1 = CoseHeaderValue.FromInt32(0); value2 = CoseHeaderValue.FromInt32(0); Assert.Equal(value1.GetHashCode(), value2.GetHashCode()); value1 = default; Assert.NotEqual(value1.GetHashCode(), value2.GetHashCode()); value1 = new CoseHeaderValue(); Assert.NotEqual(value1.GetHashCode(), value2.GetHashCode()); }
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)); } } }
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)); } }
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)); } }
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); }
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()); } }
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); }