Ejemplo n.º 1
0
        public void VerifyThrowsIfIncorrectIntegerAlgorithm(int incorrectAlg)
        {
            CoseSigner signer = GetCoseSigner(DefaultKey, DefaultHash);

            // Template header
            signer.ProtectedHeaders.Add(new CoseHeaderLabel(42), 42);
            string hexTemplateHeaders = "47A20126182A182A";
            string hexCborMessage     = Sign(s_sampleContent, signer).ByteArrayToHex();

            // Creaft a encoded protected map that replaces the "Template value" map.
            var writer = new CborWriter();

            writer.WriteStartMap(1);
            writer.WriteInt32(1);
            writer.WriteInt32(incorrectAlg);
            writer.WriteEndMap();
            byte[] newMap = writer.Encode();

            writer.Reset();
            writer.WriteByteString(newMap);
            string hexNewMap = writer.Encode().ByteArrayToHex();

            hexCborMessage = ReplaceFirst(hexCborMessage, hexTemplateHeaders, hexNewMap);

            CoseMessage msg = Decode(ByteUtils.HexToByteArray(hexCborMessage));

            Assert.Throws <CryptographicException>(() => Verify(msg, DefaultKey, s_sampleContent));
        }
        public static void EncodeSpan_DestinationTooSmall_ShouldThrowArgumentException(object value)
        {
            var writer = new CborWriter();

            Helpers.WriteValue(writer, value);

            byte[] encoding = writer.Encode();
            byte[] target   = new byte[encoding.Length - 1];

            Assert.Throws <ArgumentException>(() => writer.Encode(target));
            Assert.All(target, b => Assert.Equal(0, b));
        }
        public static void EncodeSpan_HappyPath(object value)
        {
            var writer = new CborWriter();

            Helpers.WriteValue(writer, value);

            byte[] target       = new byte[writer.BytesWritten];
            int    bytesWritten = writer.Encode(target);

            byte[] encoding = writer.Encode();

            Assert.Equal(encoding.Length, bytesWritten);
            Assert.Equal(encoding, target);
        }
Ejemplo n.º 4
0
        public void SetEncodedValue_KnownHeaders_ThrowIf_IncorrectValue(SetValueMethod method)
        {
            if (method == SetValueMethod.AddShortcut)
            {
                return;
            }

            var writer = new CborWriter();

            writer.WriteNull();
            byte[] encodedNullValue = writer.Encode();

            var map = new CoseHeaderMap();

            // only accepts int or tstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.Algorithm, encodedNullValue, method));
            // [ +label ] (non-empty array)
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, encodedNullValue, method));
            writer.Reset();
            writer.WriteStartArray(0);
            writer.WriteEndArray();
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.CriticalHeaders, writer.Encode(), method));
            // tstr / uint
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.ContentType, encodedNullValue, method));
            // bstr
            Assert.Throws <ArgumentException>("value", () => SetEncodedValue(map, CoseHeaderLabel.KeyIdentifier, encodedNullValue, method));
        }
Ejemplo n.º 5
0
            static ReadOnlyMemory <byte> EncodeAndReset(CborWriter writer)
            {
                ReadOnlyMemory <byte> encodedValue = writer.Encode();

                writer.Reset();
                return(encodedValue);
            }
Ejemplo n.º 6
0
        public static void WriteMap_DuplicateKeys_StrictConformance_ShouldBeRecoverableError(CborConformanceMode mode, object dupeKey)
        {
            byte[] expected = PerformWrite(attemptDuplicateWrite: false);
            byte[] actual   = PerformWrite(attemptDuplicateWrite: true);
            Assert.Equal(expected.ByteArrayToHex(), actual.ByteArrayToHex());

            byte[] PerformWrite(bool attemptDuplicateWrite)
            {
                var writer = new CborWriter(mode);

                writer.WriteStartMap(2);
                Helpers.WriteValue(writer, dupeKey);
                writer.WriteInt32(0);

                if (attemptDuplicateWrite)
                {
                    Assert.Throws <InvalidOperationException>(() => Helpers.WriteValue(writer, dupeKey));
                }

                // wrap dupe key in an array to satisfy key sorting & uniqueness constraints
                Helpers.WriteValue(writer, new object[] { dupeKey });
                writer.WriteInt32(0);
                writer.WriteEndMap();

                return(writer.Encode());
            }
        }
Ejemplo n.º 7
0
        private static ReadOnlyMemory <byte> Encode(CborWriter writer)
        {
            byte[] buffer = new byte[writer.BytesWritten];
            writer.Encode(buffer);

            return(buffer.AsMemory());
        }
Ejemplo n.º 8
0
        public static void WriteSimpleValue_InvalidValue_LaxConformance_ShouldSucceed(CborSimpleValue input, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter(CborConformanceLevel.Lax);

            writer.WriteSimpleValue(input);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
Ejemplo n.º 9
0
        public static byte[] ExportECDsaPublicKey(ECDsa ecDsa, HashAlgorithmName?hashAlgName)
        {
            ECParameters ecParams = ecDsa.ExportParameters(includePrivateParameters: false);
            var          writer   = new CborWriter(CborConformanceMode.Ctap2Canonical, convertIndefiniteLengthEncodings: true);

            WriteECParametersAsCosePublicKey(writer, ecParams, hashAlgName);
            return(writer.Encode());
        }
        public static void WriteTextString_IndefiniteLength_WithPatching_SingleValue_HappyPath(string[] chunkInputs, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter(convertIndefiniteLengthEncodings: true);

            Helpers.WriteChunkedTextString(writer, chunkInputs);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
Ejemplo n.º 11
0
        public static void WriteNull_SingleValue_HappyPath()
        {
            byte[] expectedEncoding = "f6".HexToByteArray();
            var    writer           = new CborWriter();

            writer.WriteNull();
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
        public static void WriteSingle_Ctap2Conformance_ShouldPreservePrecision(float input, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter(CborConformanceMode.Ctap2Canonical);

            writer.WriteSingle(input);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
        public static void WriteInt32_SingleValue_HappyPath(int input, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter();

            writer.WriteInt32(input);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
        public static void WriteCborNegativeIntegerRepresentation_SingleValue_HappyPath(ulong input, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter();

            writer.WriteCborNegativeIntegerRepresentation(input);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
        public static void WriteSingle_NonCtapConformance_ShouldMinimizePrecision(float input, string hexExpectedEncoding, CborConformanceMode mode)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter(mode);

            writer.WriteSingle(input);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
Ejemplo n.º 16
0
        public static void WriteMap_IndefiniteLength_WithPatching_NestedValues_HappyPath(object[] values, string expectedHexEncoding)
        {
            byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
            var    writer           = new CborWriter(convertIndefiniteLengthEncodings: true);

            Helpers.WriteMap(writer, values, useDefiniteLengthCollections: false);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
Ejemplo n.º 17
0
        public static void WriteMap_DuplicateKeys_LaxConformance_ShouldSucceed(object[] values, string expectedHexEncoding)
        {
            byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
            var    writer           = new CborWriter(CborConformanceMode.Lax);

            Helpers.WriteMap(writer, values);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
Ejemplo n.º 18
0
        public static void WriteMap_SimpleValues_ShouldSortKeysAccordingToConformanceMode(CborConformanceMode mode, object value, string expectedHexEncoding)
        {
            byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
            var    writer           = new CborWriter(mode);

            Helpers.WriteValue(writer, value);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
Ejemplo n.º 19
0
        public static void WriteUnixTimeSeconds_Long_SingleValue_HappyPath(long value, string expectedHexEncoding)
        {
            var writer = new CborWriter();

            writer.WriteUnixTimeSeconds(value);

            byte[] encoding = writer.Encode();
            AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
        }
Ejemplo n.º 20
0
        public static void WriteArray_NestedValues_HappyPath(object[] values, string expectedHexEncoding)
        {
            byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
            var    writer           = new CborWriter();

            Helpers.WriteArray(writer, values);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
Ejemplo n.º 21
0
        public static void WriteTag_SingleValue_HappyPath(ulong tag, object value, string hexExpectedEncoding)
        {
            byte[] expectedEncoding = hexExpectedEncoding.HexToByteArray();
            var    writer           = new CborWriter();

            writer.WriteTag((CborTag)tag);
            Helpers.WriteValue(writer, value);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
Ejemplo n.º 22
0
        [InlineData("3.9614081247908796757769715711", "c482381bc24c7fffffff7fffffff7fffffff")] // maximal number of fractional digits
        public static void WriteDecimal_SingleValue_HappyPath(string stringValue, string expectedHexEncoding)
        {
            decimal value  = decimal.Parse(stringValue, Globalization.CultureInfo.InvariantCulture);
            var     writer = new CborWriter();

            writer.WriteDecimal(value);
            byte[] encoding = writer.Encode();
            AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
        }
        public static void WriteEncodedValue_ContextScenaria_HappyPath(object value, bool useDefiniteLengthEncoding, string hexExpectedEncoding)
        {
            var writer = new CborWriter(convertIndefiniteLengthEncodings: useDefiniteLengthEncoding);

            Helpers.WriteValue(writer, value, useDefiniteLengthCollections: useDefiniteLengthEncoding);

            string hexEncoding = writer.Encode().ByteArrayToHex().ToLower();

            Assert.Equal(hexExpectedEncoding, hexEncoding);
        }
Ejemplo n.º 24
0
        public void GetValueAsStringSucceeds(string value)
        {
            var writer = new CborWriter();

            writer.WriteTextString(value);

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

            Assert.Equal(value, headerValue.GetValueAsString());
        }
Ejemplo n.º 25
0
        public void GetValueAsInt32Succeeds(int value)
        {
            var writer = new CborWriter();

            writer.WriteInt32(value);

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

            Assert.Equal(value, headerValue.GetValueAsInt32());
        }
Ejemplo n.º 26
0
        public static void WriteDateTimeOffset_SingleValue_HappyPath(string valueString, string expectedHexEncoding)
        {
            DateTimeOffset value  = DateTimeOffset.Parse(valueString);
            var            writer = new CborWriter();

            writer.WriteDateTimeOffset(value);

            byte[] encoding = writer.Encode();
            AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
        }
Ejemplo n.º 27
0
        public static void WriteMap_NestedValues_ShouldSortKeysAccordingToConformanceMode(string expectedHexEncoding, CborConformanceMode mode)
        {
            object[] value            = new object[] { Map, -1, 0, new object[] { Map, 3, 3, 2, 2, 1, 1 }, 0, "a", 0, 256, 0, new object[] { Map, 2, 2, 1, 1 }, 0 };
            byte[]   expectedEncoding = expectedHexEncoding.HexToByteArray();
            var      writer           = new CborWriter(mode);

            Helpers.WriteValue(writer, value);
            byte[] actualEncoding = writer.Encode();
            AssertHelper.HexEqual(expectedEncoding, actualEncoding);
        }
Ejemplo n.º 28
0
        public static void WriteByteString_IndefiniteLength_NoPatching_SingleValue_HappyPath(string[] hexChunkInputs, string hexExpectedEncoding)
        {
            byte[][] chunkInputs      = hexChunkInputs.Select(ch => ch.HexToByteArray()).ToArray();
            byte[]   expectedEncoding = hexExpectedEncoding.HexToByteArray();

            var writer = new CborWriter(convertIndefiniteLengthEncodings: false);

            Helpers.WriteChunkedByteString(writer, chunkInputs);
            AssertHelper.HexEqual(expectedEncoding, writer.Encode());
        }
Ejemplo n.º 29
0
        public static void WriteTag_NoValue_ShouldThrowInvalidOperationException(ulong[] tags)
        {
            var writer = new CborWriter();

            foreach (ulong tag in tags)
            {
                writer.WriteTag((CborTag)tag);
            }

            Assert.Throws <InvalidOperationException>(() => writer.Encode());
        }
Ejemplo n.º 30
0
        public static void WriteInteger_SingleValue_HappyPath(string valueString, string expectedHexEncoding)
        {
            BigInteger value = BigInteger.Parse(valueString);

            var writer = new CborWriter();

            writer.WriteBigInteger(value);

            byte[] encoding = writer.Encode();
            AssertHelper.HexEqual(expectedHexEncoding.HexToByteArray(), encoding);
        }