title: Is it possible to extract the content of a PKCS#7 signed file using System.Security.Cryptography.Pkcs.SignedCms?
body: <p>I found this post related to pkcs#7 decrypting which suggest that there is no out of the box way to decrypt a signed pkcs#7 file. <a href="https://stackoverflow.com/questions/1503974/decrypting-pkcs7-encrypted-data-in-c-sharp">Decrypting PKCS#7 encrypted data in C#</a></p>
  
  <p>However, I also saw this post and I managed to validate using the SignedCms class. <a href="http://blogs.msdn.com/b/shawnfa/archive/2006/02/27/539990.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/b/shawnfa/archive/2006/02/27/539990.aspx</a></p>
  
  <p>At the end of the post though, extracting the data using <strong>cms.ContentInfo.Content</strong> seems to return the signed file (.zip.p7) instead of the actual content (.zip). Am I missing something here? Using bouncy castle, I was able to get the actual data but would rather not use a third party if it's not necessary.</p>
  
  <pre><code>static void Main(string[] args)
  {
      var encodedFile = File.ReadAllBytes(InPath);
  
      var signedData = new SignedCms();
      signedData.Decode(encodedFile);
      signedData.CheckSignature(true);
      if (!Verify(signedData))
          throw new Exception("No valid cert was found");
  
      var trueContent = new CmsSignedData(File.ReadAllBytes(InPath)).SignedContent;
  
      using (var str = new MemoryStream())
      {
          trueContent.Write(str);
          var zip = new ZipArchive(str, ZipArchiveMode.Read);
          zip.ExtractToDirectory(OutPath);
      }
      //using (var str = new MemoryStream(signedData.ContentInfo.Content))
      //{
      //    var zip = new ZipArchive(str, ZipArchiveMode.Read);
      //    zip.ExtractToDirectory(OutPath);
      //}
  
  }
  
  static bool Verify(SignedCms signedData)
  {
      var myCetificates = new X509Store(StoreName.My, StoreLocation.LocalMachine);
      myCetificates.Open(OpenFlags.ReadOnly);
      var certs = signedData.Certificates;
  
      return (from X509Certificate2 cert in certs
          select myCetificates.Certificates.Cast&lt;X509Certificate2&gt;()
              .Any(crt =&gt; crt.Thumbprint == cert.Thumbprint))
          .Any();
  
  }   
  </code></pre>
  
  <p>UPDATE: Will get back with the base64 sample file. Need to come up with one that doesn't contain sensitive information.</p>
  
  <p>UPDATE: Here is the base 64 version of a file I can extract using bouncy castle but not using the ContentInfo.Content property. I've added as a comment on the code how I would extract SignedCms directly.</p>
  
  <p>MIIFfQYJKoZIhvcNAQcCoIIFbjCCBWoCAQExCzAJBgUrDgMCGgUAMIHOBgEAoIHIBIHFUEsDBBQAAAAIAGRuWEggPN74JwAAADsAAAAMAAAAdGVzdGRhdGEudHh0bcixDQAgCEXB3sQd/gq6EVFaLHz7hwHgysM/uobpvMCDOUppdbkTUEsBAj8AFAAAAAgAZG5YSCA83vgnAAAAOwAAAAwAJAAAAAAAAAAgAAAAAAAAAHRlc3RkYXRhLnR4dAoAIAAAAAAAAQAYAOAo0XGdbtEBktqpaZ1u0QGS2qlpnW7RAVBLBQYAAAAAAQABAF4AAABRAAAAAACgggL7MIIC9zCCAd+gAwIBAgIQN3SCtKd9Hp1BDqeyqVr+tjANBgkqhkiG9w0BAQsFADASMRAwDgYDVQQDEwdmb28uYmFyMB4XDTE2MDIyNDAwNDg0MFoXDTM5MTIzMTIzNTk1OVowFDESMBAGA1UEAxMJcGtjczdzaWduMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh5KDyFhPcxueQ1vJ0yQpcAyrSHLKw/Y2K0qhZfa7W3A6q33/i8VLl0XOFCCJs+iwcJcC+iCOIw+fFkRUEj9d7Z1sKwBCcZMolkZtNvtdXOgphV6R3g6/QNZgiQ3FFqHgp7+5OAdtBEsfsoowOClnVqZAmXnXW3Pl6Lsx0wxI8A7huytqboKRqrbRz02xO9rR1Ism0g5uQBtB2rpqGQk6/1ti/UYCp9fx7pxvWhe+a+oNIq7+ijAHPNLC+bwQTGd3LhwkzMSdONpY/utdbqCqToq/IbQ7KM0NRExZL/egPCsyJ2GGBQOVCveKkzdsW5V8p2XljcI5Sq7V8lVtqR3unwIDAQABo0cwRTBDBgNVHQEEPDA6gBBZ/6xjvqs3BLMBnQj5XmOJoRQwEjEQMA4GA1UEAxMHZm9vLmJhcoIQCiUdhpq5qrlA3FT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