void asn1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
{
    if (tt->flags & ASN1_TFLG_SK_MASK) {
        STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
        int i;

        for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
            ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i);

            ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item));
        }
        sk_ASN1_VALUE_free(sk);
        *pval = NULL;
    } else {
        ASN1_item_ex_free(pval, ASN1_ITEM_ptr(tt->item));
    }
}