title: How to use SEC_ASN1_SAVE?
body: <p>I need to get full DER encoded item from <code>SEQUENCE</code>. But I've got <code>Segmentation fault: 11</code>. What is wrong?</p>
  
  <p>If I comment line with <code>SEC_ASN1_SAVE</code> it works ok for <code>version</code>, but I need to get <code>derVersion</code> </p>
  
  <pre><code>typedef struct {
      SecAsn1Item derVersion;
      SecAsn1Item version;
  } ASN1_TEST;
  
  const SecAsn1Template kTestTemplate[] = {
      {SEC_ASN1_SEQUENCE, 0, NULL, sizeof(ASN1_TEST)},
      {SEC_ASN1_SAVE, offsetof(ASN1_TEST, derVersion)},
      {SEC_ASN1_INTEGER, offsetof(ASN1_TEST, version)},
      {0}
  };
  
  void ParseASN1(){
      const char der[] = "\x30\x05\x02\x03\x01\x00\x01";
  
      SecAsn1CoderRef coder = NULL;
      OSStatus status = SecAsn1CoderCreate(&amp;coder);
      if (status) {
          THROW_EXCEPTION("SecAsn1CoderCreate");
      }
  
      ASN1_TEST test;
      status = SecAsn1Decode(
                    coder,
                    (CK_BYTE_PTR)der, 7,
                    kTestTemplate,
                    &amp;test
                    );
      if (status) {
          SecAsn1CoderRelease(coder);
  
          THROW_EXCEPTION("SecAsn1Decode");
      }
  
      fprintf(stdout, "Test.version: %lu\n", test.version.Length);
      fprintf(stdout, "Test.derVersion: %lu\n", test.derVersion.Length);
  
      SecAsn1CoderRelease(coder);
  }
  </code></pre>
  
  <p>Here are some links with code I found.</p>
  
  <p><a href="https://opensource.apple.com/source/Security/Security-55179.1/libsecurity_asn1/Security/X509Templates.h.auto.html" rel="nofollow noreferrer">NSS_TBSCertificate structure</a></p>
  
  <p><a href="https://opensource.apple.com/source/Security/Security-55179.1/libsecurity_asn1/Security/X509Templates.c.auto.html" rel="nofollow noreferrer">kSecAsn1TBSCertificateTemplate template</a></p>