-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Security.Cryptography.Pkcs.ContentInfo loads all data into memory #47410
Comments
Tagging subscribers to this area: @bartonjs, @vcsjones, @krwq Issue DetailsBackground and MotivationCurrently, if SignedCMS is detached, it has to load all content into memory before verify signature. var content = File.ReadAllBytes(@"Path\To\BigSizeFile");
var cms = new SignedCms(new ContentInfo(content),true);
cms.Decode(p7sData);
cms.CheckSignature(true); Proposed APIname System.Security.Cryptography.Pkcs
{
public class ContentInfo
{
+ public ContentInfo (Stream content, bool detached)
}
} Usage ExamplesAlternative DesignsRisks
|
Intriguing. I was trying to come up with a clever way of allowing the caller to just specify the hash, as a more generalized solution, but it doesn't work well with the data model since the digest algorithm is specified in a different place than the content (it'd be a late exception if the hash-only content was a different length than the DigestAlgorithm property specified). What are your expectations for the ContentInto streamContent = ...;
SignedCms cms = new SignedCms(streamContent, true);
cms.DigestAlgorithm = s_sha256Oid;
cms.Sign(s_sha256Signer);
cms.DigestAlgorithm = s_sha1Oid;
// This needs to re-read the stream.
// What if it wasn't seekable?
// What if it was, but didn't start at position 0?
// What if it doesn't produce the same data both times?
cms.Sign(s_sha1Signer); For verification we could solve the multiple-read problem by using the document's "what digest algorithms will I be using on this?" value to compute all needed digests in parallel up front... but that'd require a fair bit of restructuring (though, FWIW, I believe that's what Windows CMS does). We could also try to change signing to compute digests for all known algorithms during the first signature, but that'd be on average wasteful... or just say something like non-seekable streams can only be signed once (exception on second signer) and that seekable streams will be set to the Position value they were at when signing started. But these sorts of questions are things we need to think about before adding this sort of new paradigm to an existing API. |
I switched to BouncyCastle and it works perfectly. |
Marked this for 7.0. I figure if we don't do it by then we can just close it. Latent thoughts: namespace System.Security.Cryptography.Pkcs
{
partial class ContentInfo
{
public ContentInfo (Stream content, bool detached) {}
public Stream? ContentStream { get; }
}
} It's probably a breaking change to make the existing Content property be declared nullable, so we could make the Stream ctor set it to Array.Empty. Draining the stream is where things become complicated.
In a sense, just parallel computing the MD5/SHA1/SHA-2-256/SHA-2-384/SHA-2-512 hashes once during "content capture" sounds nice. But there are a lot of corner mutability cases that are present in SignedCms for compat reasons, and they make that a little weird. EnvelopedCms consumes the same ContentInfo type, but it should only need to read the data once... and always has to fully load the encrypted (or decrypted) contents into memory, so there are fewer issues there. |
My 2c: Hm. It's unnecessary computational resources, and it locks us in to extending that pattern for SHA-3-256..SHA-3-512 if/when SHA3 is added and the relevant CMS specs are updated, and whatever might be around in 20 years.
I think this aligns closest with what I was thinking. A single signer / encryptor is, in my experience, the most common. I would propose though that we make it |
As this was once planned for .NET 7 in #64488: Any chance that we get this in .NET 10? |
Background and Motivation
Currently, if SignedCMS is detached, it has to load all content into memory before verify signature.
Proposed API
Usage Examples
Alternative Designs
Risks
The text was updated successfully, but these errors were encountered: