XML signing: support detached signature for URI reference
Please support this. The norwegian standard for message exchange in health care depend heavily upon this feature. Roughly 200M messages are sent every year.
The standard ebXML Rammeverk v1.1 (norwegian) is built on top of ebXML Message Service Specification. Version 2.0. (OASIS).
This is actually supported in .NET Framework - but not .NET Core.
See implemenation in .NET Framework.
https://github.com/dotnet/runtime/blob/fbf7924aa0449a49a25352b7cdd7e81b4333301e/src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Reference.cs#L445-L448
My dirty hack using reflection
I'm not proud of this. It works, and it had to be done!
// reflection setup
FieldInfo ReferenceTypeRefTargetTypeFieldInfo = typeof(Reference).GetField("_refTargetType", BindingFlags.NonPublic | BindingFlags.Instance);
FieldInfo ReferenceTypeRefTargetFieldInfo = typeof(Reference).GetField("_refTarget", BindingFlags.NonPublic | BindingFlags.Instance);
object StreamReferenceTargetType = Enum.Parse(typeof(Reference).Assembly.GetType("System.Security.Cryptography.Xml.ReferenceTargetType"), "Stream"); // enum value
Reference reference = ...; // resolved from deserialized SignedXml object
MemoryStream memoryStream = ...; bytes for the given reference, manually resolved
// hack: detached URI reference were allowed in .NET Framework, but is removed in .NET Core
// fix: resolve the bytes manually and set the reference object to be a stream reference
ReferenceTypeRefTargetTypeFieldInfo.SetValue(reference, StreamReferenceTargetType);
ReferenceTypeRefTargetFieldInfo.SetValue(reference, memoryStream);
The .NET Framework implementation requires setting a registry key to enable detached signatures, because they're dangerous. As part of porting to .NET Core we removed everything from crypto-XML that required a registry change to enable.
We could contemplate adding support for custom resolvers to allow someone to opt-in on a per-document basis, but that requires an API change in an area that we're not investing in. If anyone from the larger community wants to try prototyping such a feature to show how external references can be resolved securely, then do an API proposal and PR, that's probably something we'd take.
Did you ever find a solution to this other than the reflection hack @espenrl?
We have stumbled upon the same problem because of the norwegian standard for message exchange in health care.
Detached XML signatures are not deemed necessarily dangerous in the eIDAS world and are widely supported in different Java frameworks. The good news is that hack is working even in the newest .NET stacks, although it feels obviously a bit uneasy to rely on.
Did you ever find a solution to this other than the reflection hack @espenrl?
We have stumbled upon the same problem because of the norwegian standard for message exchange in health care.
The reflection hack is working like a charm. The application processed millions of messages an hour with no problem ever.
@espenrl I am in the same situation, but I cannot figure out how to do this:
Reference reference = ...; // resolved from deserialized SignedXml object
MemoryStream memoryStream = ...; bytes for the given reference, manually resolved
Can you please provide some example code?
Thanks
@espenrl I am in the same situation, but I cannot figure out how to do this:
Reference reference = ...; // resolved from deserialized SignedXml object MemoryStream memoryStream = ...; bytes for the given reference, manually resolvedCan you please provide some example code?
Thanks
The SignedXml instance (let's call it signedXml) may have many references to detached files, each reference has a specific URI (i.e. refUri), then you can find the reference based on refUri (or just get the first if there's only one):
Reference reference = signedXml.Signature.SignedInfo.References.Cast<Reference>()
.First(r => r.Uri.Equals(refUri))
The memoryStream must have the content bytes of the referenced file.
Nice use of reflection @espenrl !
My question then to the implementors - why not make "Content" a public property or method in the Reference object? If set manually by the client, the content won't have to be resolved using advanced and unsecure behind-the-scenes mechanisms.
With EBXML (mentioned above), the signature contains multiple references to MIME-multipart attachments. We read the MIME message with all its attachments and CID ids from the received mail, so we have the content already. We just need to put the content into the Reference object (as illustrated by Espens reflection method) to compute (or verify) the signature.
reference.Content = stream/byte-array ..would be great.
@frodegil I'd suggest writing API proposal with justification if you feel it should be public and contributing a patch if approved. I can certainly assist but otherwise that's currently not in our plans for now (also I'd recommend to consider using CMS as the alternative if possible).
As for detached signatures there are some security consideration of allowing that i.e.:
- associating signature with data can be done incorrectly by users (i.e. different valid signature used for the modified data)
- complicated validation process for multiple signatures - since data is transmitted separately it's more susceptible to man in the middle and implemented incorrectly
@krwq In my opinion it should be up to the application domain assuring that valid signatures reference the correct data, it just shouldn’t be a library concern. Also, the data is not necessarily transmitted separately, i.e. the OpenDocument signatures already correctly implemented by Office, or the eIDAS ASiC specifications: these are basically detached signatures embedded in .zip files that contain the signed data as well. By the way, detached signatures are supported and fully implemented in pretty much any other framework in different application environments.
Library generally shouldn't foster incorrect usages especially in space where security is crucial. In order to move this forward I suggest creating API proposal which explicitly mentions security concerns through xml docs or propose design which can make it reasonably secure. Note this is not area we currently invest in and therefore it would most likely need community contribution. Take into consideration that even reviewing design proposals and PRs takes some portion of time for us since our motivation is to have quality APIs and not supporting every possible feature.
Thank you @espenrl, your solution also works in the .NET Framework with a little modification.
Change the field names _refTarget and _refTargeType to m_refTarget and m_refTargetType:
FieldInfo ReferenceTypeRefTargetTypeFieldInfo = typeof(Reference).GetField("m_refTargetType", BindingFlags.NonPublic | BindingFlags.Instance); FieldInfo ReferenceTypeRefTargetFieldInfo = typeof(Reference).GetField("m_refTarget", BindingFlags.NonPublic | BindingFlags.Instance);
My situation is that I have to check a detached signature and even though I got it to work partly be enabling the registry settings needed, I still had a problem with users sending packages with filenames that are not allowed in Windows.
Using the Stream approach solved all issues :-)
While there has been a lot of back and forth about using reflection, no one ever showed interest in the pursuit of an API-based solution.
The investment in SignedXml required by this issue would be in contradiction with the contribution bar for the crypto-XML library (https://github.com/dotnet/runtime/tree/main/src/libraries/System.Security.Cryptography.Xml#readme), so I'm closing the issue.