コード例 #1
0
        public void Verify(int algorithm, string hexCborMessage)
        {
            foreach (bool usePublicOnlyKey in new[] { false, true })
            {
                CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage));

                bool verified;
                if (Enum.IsDefined(typeof(ECDsaAlgorithm), algorithm))
                {
                    var   ecdsaAlgorithm = (ECDsaAlgorithm)algorithm;
                    ECDsa key            = usePublicOnlyKey ? ECDsaKeysWithoutPrivateKey[ecdsaAlgorithm] : ECDsaKeys[ecdsaAlgorithm];
                    verified = msg.Verify(key);
                }
                else
                {
                    RSA key = usePublicOnlyKey ? RSAKeyWithoutPrivateKey : RSAKey;
                    verified = msg.Verify(key);
                }

                Assert.True(verified, "CoseSign1Message.Verify(key)");
                AssertExtensions.SequenceEqual(s_sampleContent, msg.Content.GetValueOrDefault().Span);

                Assert.True(msg.ProtectedHeaders.TryGetEncodedValue(CoseHeaderLabel.Algorithm, out ReadOnlyMemory <byte> encodedAlg),
                            "Algorithm header must be protected");

                Assert.Equal(algorithm, new CborReader(encodedAlg).ReadInt32());
            }
        }
コード例 #2
0
        internal override bool Verify(CoseSign1Message msg, AsymmetricAlgorithm key, byte[] content)
        {
            if (content == null)
            {
                return(msg.Verify(key, (Stream)null !));
            }

            using Stream stream = GetTestStream(content);
            return(msg.Verify(key, stream));
        }
        internal override bool Verify(CoseSign1Message msg, AsymmetricAlgorithm key, byte[] content)
        {
            Assert.True(!OnlySupportsDetachedContent || msg.Content == null);

            if (msg.Content != null)
            {
                return(key is ECDsa ecdsa?msg.Verify(ecdsa) : msg.Verify((RSA)key));
            }
            else
            {
                return(key is ECDsa ecdsa?msg.Verify(ecdsa, content) : msg.Verify((RSA)key, content));
            }
        }
コード例 #4
0
        public void VerifyReturnsFalseWithDataNotMatchingSignature()
        {
            string           encodedMsg = "D28445A201260300A10442313154546869732069732074686520636F6E74656E742E58406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B";
            CoseSign1Message msg        = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(encodedMsg));

            Assert.True(msg.Verify(DefaultKey), "msg.Verify(ES256)");

            encodedMsg = ReplaceFirst(encodedMsg, "45A201260300", "45A201260301");
            msg        = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(encodedMsg));
            Assert.False(msg.Verify(DefaultKey), "msg.Verify(ES256) - Corrupt protected header");

            encodedMsg = ReplaceFirst(encodedMsg, "546869732069732074686520636F6E74656E742E", "546869732069732074686520636F6E74656E743E");
            msg        = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(encodedMsg));
            Assert.False(msg.Verify(DefaultKey), "msg.Verify(ES256) - Corrupt content");
コード例 #5
0
        public void DecodeVerifyDetachedContent()
        {
            CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("D28445A201260300A104423131F658406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B"));

            Assert.Null(msg.Content);
            Assert.True(msg.Verify(DefaultKey, s_sampleContent));
        }
コード例 #6
0
        public void SignVerifyRSA(RSAAlgorithm algorithm)
        {
            HashAlgorithmName hashAlgorithm = GetHashAlgorithmNameFromCoseAlgorithm((int)algorithm);

            byte[]           coseMessageBytes = CoseSign1Message.Sign(s_sampleContent, RSAKey, hashAlgorithm);
            CoseSign1Message msg = CoseMessage.DecodeSign1(coseMessageBytes);

            Assert.True(msg.Verify(RSAKey));
        }
コード例 #7
0
        public void SignVerifyECDsa(ECDsaAlgorithm algorithm)
        {
            ECDsa             ecdsa         = ECDsaKeys[algorithm];
            HashAlgorithmName hashAlgorithm = GetHashAlgorithmNameFromCoseAlgorithm((int)algorithm);

            byte[]           coseMessageBytes = CoseSign1Message.Sign(s_sampleContent, ecdsa, hashAlgorithm);
            CoseSign1Message msg = CoseMessage.DecodeSign1(coseMessageBytes);

            Assert.True(msg.Verify(ecdsa));
        }
コード例 #8
0
        public void VerifyWithUnreadableStream()
        {
            using Stream stream = GetTestStream(s_sampleContent);
            byte[] encodedMsg = CoseSign1Message.Sign(stream, DefaultKey, DefaultHash);

            CoseSign1Message msg = CoseMessage.DecodeSign1(encodedMsg);

            using Stream unseekableStream = GetTestStream(s_sampleContent, StreamKind.Unreadable);
            Assert.Throws <ArgumentException>("detachedContent", () => msg.Verify(DefaultKey, unseekableStream));
        }
コード例 #9
0
        public void VerifyReturnsFalseWithWrongSignature(string hexCborMessage)
        {
            CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage));

            Assert.False(msg.Verify(DefaultKey));
        }
コード例 #10
0
        public void DecodeVerifyUntagged()
        {
            CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("8445A201260300A10442313154546869732069732074686520636F6E74656E742E58406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B"));

            Assert.True(msg.Verify(DefaultKey));
        }