I am having a problem using the signedCMS.decode routine. See the code below.
The error seems to occur when the file size is too big in this case 11MB.
private static void RemoveZfoSignature(string zfoFileName)
{
byte[] fileContents = File.ReadAllBytes(zfoFileName);
var contentInfo = new ContentInfo(fileContents);
var signedCms = new SignedCms(contentInfo);
// This line throws the error 100% of the time
signedCms.Decode(fileContents);
signedCms.RemoveSignature(0);
byte[] outfile = signedCms.ContentInfo.Content;
string outFileName = zfoFileName.Replace(".zfo", "_tmp.zfo");
File.WriteAllBytes(outFileName, outfile);
}
Here is the exact error:
"System.Security.Cryptography.CryptographicException: ASN1 out of memory. at System.Security.Cryptography.Pkcs.SignedCms.OpenToDecode(Byte[] encodedMessage, ContentInfo contentInfo, Boolean detached) at System.Security.Cryptography.Pkcs.SignedCms.Decode(Byte[] encodedMessage) at ConsoleApplication2.Program.RemoveZfoSignature(String zfoFileName) in C:\\Users\\\\Documents\\Visual Studio 2008\\Projects\\ConsoleApplication2\\ConsoleApplication2\\Program.cs:line 30"
Any idea on how to fix this?
I’ve updated the code now to look like this, but now its failing on the removeSignature saying The CMS Message is not signed.
/// <summary>
/// Removes the ZFO signature from the ZFO, so that it is possible to extract attachments.
/// </summary>
/// <param name="zfoFileName">
/// The zfo file name.
/// </param>
private static void RemoveZfoSignature(string zfoFileName)
{
string outFileName = zfoFileName.Replace(".zfo", "_tmp.zfo");
FileStream inFile = null;
FileStream outFile = null;
inFile = File.Open(zfoFileName, FileMode.Open);
outFile = File.Create(outFileName);
LargeCMS.CMS cms = new LargeCMS.CMS();
cms.Decode(inFile, outFile);
// Clean up
if (inFile != null) { inFile.Close(); }
if (outFile != null) { outFile.Close(); }
byte[] fileContents = File.ReadAllBytes(outFileName);
var contentInfo = new ContentInfo(fileContents);
var signedCms = new SignedCms(contentInfo);
//signedCms.Decode(fileContents);
signedCms.RemoveSignature(0);
byte[] outfileContent = signedCms.ContentInfo.Content;
File.WriteAllBytes(outFileName, outfileContent);
}
As per this page:
The only resolution seems to be calling the native, lower level functions listed on that page. See here for an example.
All the following code comes from that page (duplicated here in case that page ever goes down):
File form1.cs:
File cms.cs: