76
76
int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
78
78
int *sel = offset2ptr(*pval, it->utype);
82
82
/* Given an ASN1_ITEM CHOICE type set
83
83
* the selector value, return old value.
86
86
int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
89
89
sel = offset2ptr(*pval, it->utype);
95
95
/* Do reference counting. The value 'op' decides what to do.
96
96
* if it is +1 then the count is incremented. If op is 0 count is
101
101
int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
103
103
const ASN1_AUX *aux;
105
if(it->itype != ASN1_ITYPE_SEQUENCE) return 0;
105
if ((it->itype != ASN1_ITYPE_SEQUENCE)
106
&& (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
107
if(!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) return 0;
109
if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
108
111
lck = offset2ptr(*pval, aux->ref_offset);
113
117
ret = CRYPTO_add(lck, op, aux->ref_lock);
115
119
fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
119
123
fprintf(stderr, "%s, bad reference count\n", it->sname);
124
128
static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
126
130
const ASN1_AUX *aux;
127
if(!pval || !*pval) return NULL;
129
if(!aux || !(aux->flags & ASN1_AFLG_ENCODING)) return NULL;
134
if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
130
136
return offset2ptr(*pval, aux->enc_offset);
133
139
void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
135
141
ASN1_ENCODING *enc;
136
142
enc = asn1_get_enc_ptr(pval, it);
140
147
enc->modified = 1;
144
151
void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
146
153
ASN1_ENCODING *enc;
147
154
enc = asn1_get_enc_ptr(pval, it);
149
if(enc->enc) OPENSSL_free(enc->enc);
158
OPENSSL_free(enc->enc);
152
161
enc->modified = 1;
156
int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it)
165
int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
158
168
ASN1_ENCODING *enc;
159
169
enc = asn1_get_enc_ptr(pval, it);
162
if(enc->enc) OPENSSL_free(enc->enc);
174
OPENSSL_free(enc->enc);
163
175
enc->enc = OPENSSL_malloc(inlen);
164
if(!enc->enc) return 0;
165
178
memcpy(enc->enc, in, inlen);
166
179
enc->len = inlen;
167
180
enc->modified = 0;
172
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it)
185
int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
174
188
ASN1_ENCODING *enc;
175
189
enc = asn1_get_enc_ptr(pval, it);
176
if(!enc || enc->modified) return 0;
190
if (!enc || enc->modified)
178
194
memcpy(*out, enc->enc, enc->len);
179
195
*out += enc->len;
181
if(len) *len = enc->len;
185
202
/* Given an ASN1_TEMPLATE get a pointer to a field */
186
203
ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
188
205
ASN1_VALUE **pvaltmp;
189
if(tt->flags & ASN1_TFLG_COMBINE) return pval;
206
if (tt->flags & ASN1_TFLG_COMBINE)
190
208
pvaltmp = offset2ptr(*pval, tt->offset);
191
209
/* NOTE for BOOLEAN types the field is just a plain
192
210
* int so we can't return int **, so settle for
198
216
/* Handle ANY DEFINED BY template, find the selector, look up
199
217
* the relevant ASN1_TEMPLATE in the table and return it.
202
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr)
220
const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
204
223
const ASN1_ADB *adb;
205
224
const ASN1_ADB_TABLE *atbl;
207
226
ASN1_VALUE **sfld;
209
if(!(tt->flags & ASN1_TFLG_ADB_MASK)) return tt;
228
if (!(tt->flags & ASN1_TFLG_ADB_MASK))
211
231
/* Else ANY DEFINED BY ... get the table */
212
232
adb = ASN1_ADB_ptr(tt->item);
240
for(atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
241
if(atbl->value == selector) return &atbl->tt;
262
for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
263
if (atbl->value == selector)
243
266
/* FIXME: need to search application table too */
245
268
/* No match, return default type */
246
if(!adb->default_tt) goto err;
269
if (!adb->default_tt)
247
271
return adb->default_tt;
250
274
/* FIXME: should log the value or OID of unsupported type */
251
if(nullerr) ASN1err(ASN1_F_ASN1_DO_ADB, ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
276
ASN1err(ASN1_F_ASN1_DO_ADB,
277
ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);