title: Problems when P/Invoking CertCreateSelfSignCertificate
body: <p>I was following <a href="http://blogs.msdn.com/b/dcook/archive/2014/05/16/9143036.aspx" rel="nofollow">this article (in it there is a link to a .cs file at the bottom of the page)</a> to generate a self-signed <a href="https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate2%28v=vs.110%29.aspx" rel="nofollow">X509Certificate2</a>. The code in the article works but now I want to extend it. I am trying to pass the optional argument, <code>_In_opt_ PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm</code>, into <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa376039%28v=vs.85%29.aspx" rel="nofollow">CertCreateSelfSignCertificate</a>.</p>
  
  <p>I have created this structure for it:</p>
  
  <pre><code>struct CryptoApiBlob
  {
      public Int32 cbData;
      public IntPtr pbData;
  }
  
  struct CryptAlgorithmIdentifier {
      public String pszObjId;
      public CryptoApiBlob Parameters;
  }
  </code></pre>
  
  <p>The code I am trying to use to create it is:</p>
  
  <pre><code>CryptAlgorithmIdentifier algorithm = new CryptAlgorithmIdentifier { pszObjId = "szOID_NIST_AES256_CBC", Parameters = new CryptoApiBlob { cbData = 0 } };
  algorithmPointer = Marshal.AllocHGlobal(Marshal.SizeOf(algorithm));
  Marshal.StructureToPtr(algorithm, algorithmPointer, false);
  </code></pre>
  
  <p>I then pass the <code>algorithmPointer</code> into the method.</p>
  
  <p>I get this error when I try to pass it into <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa376039%28v=vs.85%29.aspx" rel="nofollow">CertCreateSelfSignCertificate</a>:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll</p>
    
    <p>Additional information: ASN1 bad arguments to function call.
    (Exception from HRESULT: 0x80093109)</p>
  </blockquote>
  
  <p>Does anyone happen to know why this happens, or can see any problems with the way I've defined my structure or allocated it in memory?</p>