public void DecodeVerifyDetachedContent() { CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("D28445A201260300A104423131F658406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B")); Assert.Null(msg.Content); Assert.True(msg.Verify(DefaultKey, s_sampleContent)); }
public void DecodeSign1_VerifyUntagged() { // https://github.com/cose-wg/Examples/blob/master/ecdsa-examples/ecdsa-sig-01.json minus first byte. CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("8445A201260300A10442313154546869732069732074686520636F6E74656E742E58406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B")); Assert.True(msg.VerifyEmbedded(DefaultKey)); }
public void TestVerify(int algorithm, string hexCborMessage) { ReplaceContentInHexCborMessage(ref hexCborMessage); foreach (bool useNonPrivateKey in new[] { false, true }) { CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); AsymmetricAlgorithm key = GetKeyHashPair <AsymmetricAlgorithm>((CoseAlgorithm)algorithm, useNonPrivateKey).Key; Assert.True(Verify(msg, key, s_sampleContent), "Varification failed."); if (UseDetachedContent) { Assert.Null(msg.Content); } else { 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()); } }
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()); } }
public void VerifyThrowsWithUnknownAlgorithm(string hexCborMessage) { ReplaceContentInHexCborMessage(ref hexCborMessage); CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); Assert.Throws <CryptographicException>(() => Verify(msg, DefaultKey, s_sampleContent)); }
public void DecodeSign1_VerifyDetachedContent() { // Content is replaced with CBOR null - https://github.com/cose-wg/Examples/blob/master/ecdsa-examples/ecdsa-sig-01.json. CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("D28445A201260300A104423131F658406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B")); Assert.Null(msg.Content); Assert.True(msg.VerifyDetached(DefaultKey, s_sampleContent)); }
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)); }
public async Task VerifyAsyncWithUnseekableStream() { using Stream stream = GetTestStream(s_sampleContent); byte[] encodedMsg = await CoseSign1Message.SignAsync(stream, DefaultKey, DefaultHash); CoseSign1Message msg = CoseMessage.DecodeSign1(encodedMsg); using Stream unseekableStream = GetTestStream(s_sampleContent, StreamKind.Unseekable); await Assert.ThrowsAsync <ArgumentException>("detachedContent", () => msg.VerifyAsync(DefaultKey, unseekableStream)); }
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)); }
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)); }
public void SignVerify() { foreach ((T key, HashAlgorithmName hashAlgorithm, CoseAlgorithm algorithm) in GetKeyHashAlgorithmTriplet()) { ReadOnlySpan <byte> encodedMsg = Sign(s_sampleContent, key, hashAlgorithm); AssertSign1Message(encodedMsg, s_sampleContent, key, algorithm); CoseSign1Message msg = CoseMessage.DecodeSign1(encodedMsg); Assert.True(Verify(msg, key)); } }
public void VerifyThrowsIfMessageWasEmbeddedAndContentWasSupplied() { if (!UseDetachedContent) { return; } CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("D28443A10126A10442313154546869732069732074686520636F6E74656E742E58408EB33E4CA31D1C465AB05AAC34CC6B23D58FEF5C083106C4D25A91AEF0B0117E2AF9A291AA32E14AB834DC56ED2A223444547E01F11D3B0916E5A4C345CACB36")); Assert.NotNull(msg.Content); Assert.Throws <InvalidOperationException>(() => Verify(msg, DefaultKey, s_sampleContent)); }
public void VerifyThrowsIfMessageWasDetachedAndContentWasNotSupplied() { if (UseDetachedContent) { return; } CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("D28445A201260300A104423131F658406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B")); Assert.Null(msg.Content); Assert.Throws <InvalidOperationException>(() => Verify(msg, DefaultKey, s_sampleContent)); }
public void DecodeSign1_IncorrectStructure() { var writer = new CborWriter(); writer.WriteStartArray(4); writer.WriteNull(); writer.WriteNull(); writer.WriteNull(); writer.WriteNull(); writer.WriteEndArray(); Assert.Throws <CryptographicException>(() => CoseMessage.DecodeSign1(writer.Encode())); }
public void VerifyReturnsFalseWithWrongSignature(string hexCborMessage, bool corruptContent) { if (corruptContent && UseDetachedContent) { return; } ReplaceContentInHexCborMessage(ref hexCborMessage); CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); Assert.False(Verify(msg, DefaultKey, s_sampleContent)); }
static void VerifyContentDetached(byte[] messageEncoded, byte[] expectedContent, bool isDetached) { CoseMessage messageDecoded = CoseMessage.DecodeSign1(messageEncoded); if (isDetached) { Assert.Null(messageDecoded.Content); } else { AssertExtensions.SequenceEqual(new ReadOnlySpan <byte>(expectedContent), messageDecoded.Content.GetValueOrDefault().Span); } }
public void DecodeSign1_ThrowsWithDuplicateHeaders(bool shouldContainInnerException, string hexCborMessage) { CryptographicException ex = Assert.Throws <CryptographicException>(() => CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage))); if (shouldContainInnerException) // if the duplicate headers were in one bucket the exception comes from CborReader because we use CborConformanceMode.Strict. { Assert.IsType <CborContentException>(ex.InnerException); } else { Assert.Null(ex.InnerException); } }
public void TestVerifyWithAssociatedData() { // https://github.com/cose-wg/Examples/blob/master/sign1-tests/sign-pass-02.json string hexCborMessage = "D28443A10126A10442313154546869732069732074686520636F6E74656E742E584010729CD711CB3813D8D8E944A8DA7111E7B258C9BDCA6135F7AE1ADBEE9509891267837E1E33BD36C150326AE62755C6BD8E540C3E8F92D7D225E8DB72B8820B"; ReplaceContentInHexCborMessage(ref hexCborMessage); CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); Assert.False(Verify(msg, DefaultKey, s_sampleContent)); byte[] associatedData = ByteUtils.HexToByteArray("11aa22bb33cc44dd55006699"); Assert.True(Verify(msg, DefaultKey, s_sampleContent, associatedData)); }
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");
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)); }
public void VerifyReturnsFalseWithDataNotMatchingSignature() { string hexCborMessage = "D28445A201260300A10442313154546869732069732074686520636F6E74656E742E58406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B"; ReplaceContentInHexCborMessage(ref hexCborMessage); CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); Assert.True(Verify(msg, DefaultKey, s_sampleContent), "Verification failed"); string hexCborMessageCorrupt1 = ReplaceFirst(hexCborMessage, "45A201260300", "45A201260301"); msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessageCorrupt1)); Assert.False(Verify(msg, DefaultKey, s_sampleContent), "Verification passed when should've failed - Corrupt protected header"); if (!UseDetachedContent) { string hexCborMessageCorrupt2 = ReplaceFirst(hexCborMessage, "546869732069732074686520636F6E74656E742E", "546869732069732074686520636F6E74656E743E"); msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessageCorrupt2)); Assert.False(Verify(msg, DefaultKey, s_sampleContent), "Verification passed when should've failed - Corrupt content"); }
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)); } }
public void SignWithAllCborTypesAsHeaderValue(bool useProtectedMap, byte[] encodedValue) { var myLabel = new CoseHeaderLabel(42); CoseHeaderMap protectedHeaders = GetHeaderMapWithAlgorithm(DefaultAlgorithm); CoseHeaderMap unprotectedHeaders = GetEmptyHeaderMap(); (useProtectedMap ? protectedHeaders : unprotectedHeaders).SetEncodedValue(myLabel, 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); AssertSign1Message(encodedMessage, s_sampleContent, DefaultKey, DefaultAlgorithm, expectedProtectedHeaders, expectedUnprotectedHeaders); // Verify it is transported correctly. CoseSign1Message message = CoseMessage.DecodeSign1(encodedMessage); ReadOnlyMemory <byte> roundtrippedValue = (useProtectedMap ? message.ProtectedHeaders : message.UnprotectedHeaders).GetEncodedValue(myLabel); AssertExtensions.SequenceEqual(encodedValue, roundtrippedValue.Span); }
public void DecodeVerifyUntagged() { CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray("8445A201260300A10442313154546869732069732074686520636F6E74656E742E58406520BBAF2081D7E0ED0F95F76EB0733D667005F7467CEC4B87B9381A6BA1EDE8E00DF29F32A37230F39A842A54821FDD223092819D7728EFB9D3A0080B75380B")); Assert.True(msg.Verify(DefaultKey)); }
public void DecodeSign1_IncorrectTag(string hexCborMessage) { Assert.Throws <CryptographicException>(() => CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage))); }
internal override CoseMessage Decode(byte[] cborPayload) => CoseMessage.DecodeSign1(cborPayload);
internal override CoseMessage Decode(ReadOnlySpan <byte> cborPayload) => CoseMessage.DecodeSign1(cborPayload);
public void VerifyReturnsFalseWithWrongSignature(string hexCborMessage) { CoseSign1Message msg = CoseMessage.DecodeSign1(ByteUtils.HexToByteArray(hexCborMessage)); Assert.False(msg.Verify(DefaultKey)); }