| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
|
| 8 |
#include <openssl/asn1t.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
|
| 11 |
#include "internal.h" |
| 12 |
|
| 13 |
// Free up an ASN1 structure |
| 14 |
|
| 15 |
void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) { |
| 16 |
asn1_item_combine_free(&val, it, 0); |
| 17 |
} |
| 18 |
|
| 19 |
void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
| 20 |
asn1_item_combine_free(pval, it, 0); |
| 21 |
} |
| 22 |
|
| 23 |
void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, |
| 24 |
int combine) { |
| 25 |
const ASN1_TEMPLATE *tt = NULL, *seqtt; |
| 26 |
const ASN1_EXTERN_FUNCS *ef; |
| 27 |
int i; |
| 28 |
if (pval == NULL || it == NULL) { |
| 29 |
return; |
| 30 |
} |
| 31 |
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
switch (it->itype) { |
| 36 |
case ASN1_ITYPE_PRIMITIVE: |
| 37 |
if (it->templates) { |
| 38 |
ASN1_template_free(pval, it->templates); |
| 39 |
} else { |
| 40 |
ASN1_primitive_free(pval, it); |
| 41 |
} |
| 42 |
break; |
| 43 |
|
| 44 |
case ASN1_ITYPE_MSTRING: |
| 45 |
ASN1_primitive_free(pval, it); |
| 46 |
break; |
| 47 |
|
| 48 |
case ASN1_ITYPE_CHOICE: { |
| 49 |
const ASN1_AUX *aux = it->funcs; |
| 50 |
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
| 51 |
if (asn1_cb) { |
| 52 |
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
| 53 |
if (i == 2) { |
| 54 |
return; |
| 55 |
} |
| 56 |
} |
| 57 |
i = asn1_get_choice_selector(pval, it); |
| 58 |
if ((i >= 0) && (i < it->tcount)) { |
| 59 |
ASN1_VALUE **pchval; |
| 60 |
tt = it->templates + i; |
| 61 |
pchval = asn1_get_field_ptr(pval, tt); |
| 62 |
ASN1_template_free(pchval, tt); |
| 63 |
} |
| 64 |
if (asn1_cb) { |
| 65 |
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
| 66 |
} |
| 67 |
if (!combine) { |
| 68 |
OPENSSL_free(*pval); |
| 69 |
*pval = NULL; |
| 70 |
} |
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
case ASN1_ITYPE_EXTERN: |
| 75 |
ef = it->funcs; |
| 76 |
if (ef && ef->asn1_ex_free) { |
| 77 |
ef->asn1_ex_free(pval, it); |
| 78 |
} |
| 79 |
break; |
| 80 |
|
| 81 |
case ASN1_ITYPE_SEQUENCE: { |
| 82 |
if (!asn1_refcount_dec_and_test_zero(pval, it)) { |
| 83 |
return; |
| 84 |
} |
| 85 |
const ASN1_AUX *aux = it->funcs; |
| 86 |
ASN1_aux_cb *asn1_cb = aux != NULL ? aux->asn1_cb : NULL; |
| 87 |
if (asn1_cb) { |
| 88 |
i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); |
| 89 |
if (i == 2) { |
| 90 |
return; |
| 91 |
} |
| 92 |
} |
| 93 |
asn1_enc_free(pval, it); |
| 94 |
// If we free up as normal we will invalidate any ANY DEFINED BY |
| 95 |
// field and we wont be able to determine the type of the field it |
| 96 |
// defines. So free up in reverse order. |
| 97 |
tt = it->templates + it->tcount - 1; |
| 98 |
for (i = 0; i < it->tcount; tt--, i++) { |
| 99 |
ASN1_VALUE **pseqval; |
| 100 |
seqtt = asn1_do_adb(pval, tt, 0); |
| 101 |
if (!seqtt) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 105 |
ASN1_template_free(pseqval, seqtt); |
| 106 |
} |
| 107 |
if (asn1_cb) { |
| 108 |
asn1_cb(ASN1_OP_FREE_POST, pval, it, NULL); |
| 109 |
} |
| 110 |
if (!combine) { |
| 111 |
OPENSSL_free(*pval); |
| 112 |
*pval = NULL; |
| 113 |
} |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) { |
| 120 |
if (tt->flags & ASN1_TFLG_SK_MASK) { |
| 121 |
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
| 122 |
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
| 123 |
ASN1_VALUE *vtmp = sk_ASN1_VALUE_value(sk, i); |
| 124 |
ASN1_item_ex_free(&vtmp, ASN1_ITEM_ptr(tt->item)); |
| 125 |
} |
| 126 |
sk_ASN1_VALUE_free(sk); |
| 127 |
*pval = NULL; |
| 128 |
} else { |
| 129 |
asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item), |
| 130 |
tt->flags & ASN1_TFLG_COMBINE); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) { |
| 135 |
// Historically, |it->funcs| for primitive types contained an |
| 136 |
// |ASN1_PRIMITIVE_FUNCS| table of calbacks. |
| 137 |
assert(it->funcs == NULL); |
| 138 |
|
| 139 |
int utype = it->itype == ASN1_ITYPE_MSTRING ? -1 : it->utype; |
| 140 |
switch (utype) { |
| 141 |
case V_ASN1_OBJECT: |
| 142 |
ASN1_OBJECT_free((ASN1_OBJECT *)*pval); |
| 143 |
break; |
| 144 |
|
| 145 |
case V_ASN1_BOOLEAN: |
| 146 |
if (it) { |
| 147 |
*(ASN1_BOOLEAN *)pval = (ASN1_BOOLEAN)it->size; |
| 148 |
} else { |
| 149 |
*(ASN1_BOOLEAN *)pval = ASN1_BOOLEAN_NONE; |
| 150 |
} |
| 151 |
return; |
| 152 |
|
| 153 |
case V_ASN1_NULL: |
| 154 |
break; |
| 155 |
|
| 156 |
case V_ASN1_ANY: |
| 157 |
if (*pval != NULL) { |
| 158 |
asn1_type_cleanup((ASN1_TYPE *)*pval); |
| 159 |
OPENSSL_free(*pval); |
| 160 |
} |
| 161 |
break; |
| 162 |
|
| 163 |
default: |
| 164 |
ASN1_STRING_free((ASN1_STRING *)*pval); |
| 165 |
*pval = NULL; |
| 166 |
break; |
| 167 |
} |
| 168 |
*pval = NULL; |
| 169 |
} |