summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_utl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_utl.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_utl.c128
1 files changed, 77 insertions, 51 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_utl.c b/src/lib/libcrypto/asn1/tasn_utl.c
index 8996ce8c13..34d520b180 100644
--- a/src/lib/libcrypto/asn1/tasn_utl.c
+++ b/src/lib/libcrypto/asn1/tasn_utl.c
@@ -3,7 +3,7 @@
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved. 6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
@@ -74,23 +74,23 @@
74 */ 74 */
75 75
76int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) 76int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
77{ 77 {
78 int *sel = offset2ptr(*pval, it->utype); 78 int *sel = offset2ptr(*pval, it->utype);
79 return *sel; 79 return *sel;
80} 80 }
81 81
82/* Given an ASN1_ITEM CHOICE type set 82/* Given an ASN1_ITEM CHOICE type set
83 * the selector value, return old value. 83 * the selector value, return old value.
84 */ 84 */
85 85
86int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) 86int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
87{ 87 {
88 int *sel, ret; 88 int *sel, ret;
89 sel = offset2ptr(*pval, it->utype); 89 sel = offset2ptr(*pval, it->utype);
90 ret = *sel; 90 ret = *sel;
91 *sel = value; 91 *sel = value;
92 return ret; 92 return ret;
93} 93 }
94 94
95/* Do reference counting. The value 'op' decides what to do. 95/* Do reference counting. The value 'op' decides what to do.
96 * if it is +1 then the count is incremented. If op is 0 count is 96 * if it is +1 then the count is incremented. If op is 0 count is
@@ -99,114 +99,134 @@ int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
99 */ 99 */
100 100
101int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 101int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
102{ 102 {
103 const ASN1_AUX *aux; 103 const ASN1_AUX *aux;
104 int *lck, ret; 104 int *lck, ret;
105 if(it->itype != ASN1_ITYPE_SEQUENCE) return 0; 105 if ((it->itype != ASN1_ITYPE_SEQUENCE)
106 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
107 return 0;
106 aux = it->funcs; 108 aux = it->funcs;
107 if(!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) return 0; 109 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
110 return 0;
108 lck = offset2ptr(*pval, aux->ref_offset); 111 lck = offset2ptr(*pval, aux->ref_offset);
109 if(op == 0) { 112 if (op == 0)
113 {
110 *lck = 1; 114 *lck = 1;
111 return 1; 115 return 1;
112 } 116 }
113 ret = CRYPTO_add(lck, op, aux->ref_lock); 117 ret = CRYPTO_add(lck, op, aux->ref_lock);
114#ifdef REF_PRINT 118#ifdef REF_PRINT
115 fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck); 119 fprintf(stderr, "%s: Reference Count: %d\n", it->sname, *lck);
116#endif 120#endif
117#ifdef REF_CHECK 121#ifdef REF_CHECK
118 if(ret < 0) 122 if (ret < 0)
119 fprintf(stderr, "%s, bad reference count\n", it->sname); 123 fprintf(stderr, "%s, bad reference count\n", it->sname);
120#endif 124#endif
121 return ret; 125 return ret;
122} 126 }
123 127
124static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) 128static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
125{ 129 {
126 const ASN1_AUX *aux; 130 const ASN1_AUX *aux;
127 if(!pval || !*pval) return NULL; 131 if (!pval || !*pval)
132 return NULL;
128 aux = it->funcs; 133 aux = it->funcs;
129 if(!aux || !(aux->flags & ASN1_AFLG_ENCODING)) return NULL; 134 if (!aux || !(aux->flags & ASN1_AFLG_ENCODING))
135 return NULL;
130 return offset2ptr(*pval, aux->enc_offset); 136 return offset2ptr(*pval, aux->enc_offset);
131} 137 }
132 138
133void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) 139void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
134{ 140 {
135 ASN1_ENCODING *enc; 141 ASN1_ENCODING *enc;
136 enc = asn1_get_enc_ptr(pval, it); 142 enc = asn1_get_enc_ptr(pval, it);
137 if(enc) { 143 if (enc)
144 {
138 enc->enc = NULL; 145 enc->enc = NULL;
139 enc->len = 0; 146 enc->len = 0;
140 enc->modified = 1; 147 enc->modified = 1;
148 }
141 } 149 }
142}
143 150
144void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 151void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
145{ 152 {
146 ASN1_ENCODING *enc; 153 ASN1_ENCODING *enc;
147 enc = asn1_get_enc_ptr(pval, it); 154 enc = asn1_get_enc_ptr(pval, it);
148 if(enc) { 155 if (enc)
149 if(enc->enc) OPENSSL_free(enc->enc); 156 {
157 if (enc->enc)
158 OPENSSL_free(enc->enc);
150 enc->enc = NULL; 159 enc->enc = NULL;
151 enc->len = 0; 160 enc->len = 0;
152 enc->modified = 1; 161 enc->modified = 1;
162 }
153 } 163 }
154}
155 164
156int asn1_enc_save(ASN1_VALUE **pval, unsigned char *in, int inlen, const ASN1_ITEM *it) 165int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
157{ 166 const ASN1_ITEM *it)
167 {
158 ASN1_ENCODING *enc; 168 ASN1_ENCODING *enc;
159 enc = asn1_get_enc_ptr(pval, it); 169 enc = asn1_get_enc_ptr(pval, it);
160 if(!enc) return 1; 170 if (!enc)
171 return 1;
161 172
162 if(enc->enc) OPENSSL_free(enc->enc); 173 if (enc->enc)
174 OPENSSL_free(enc->enc);
163 enc->enc = OPENSSL_malloc(inlen); 175 enc->enc = OPENSSL_malloc(inlen);
164 if(!enc->enc) return 0; 176 if (!enc->enc)
177 return 0;
165 memcpy(enc->enc, in, inlen); 178 memcpy(enc->enc, in, inlen);
166 enc->len = inlen; 179 enc->len = inlen;
167 enc->modified = 0; 180 enc->modified = 0;
168 181
169 return 1; 182 return 1;
170} 183 }
171 184
172int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it) 185int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
173{ 186 const ASN1_ITEM *it)
187 {
174 ASN1_ENCODING *enc; 188 ASN1_ENCODING *enc;
175 enc = asn1_get_enc_ptr(pval, it); 189 enc = asn1_get_enc_ptr(pval, it);
176 if(!enc || enc->modified) return 0; 190 if (!enc || enc->modified)
177 if(out) { 191 return 0;
192 if (out)
193 {
178 memcpy(*out, enc->enc, enc->len); 194 memcpy(*out, enc->enc, enc->len);
179 *out += enc->len; 195 *out += enc->len;
180 } 196 }
181 if(len) *len = enc->len; 197 if (len)
198 *len = enc->len;
182 return 1; 199 return 1;
183} 200 }
184 201
185/* Given an ASN1_TEMPLATE get a pointer to a field */ 202/* Given an ASN1_TEMPLATE get a pointer to a field */
186ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 203ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
187{ 204 {
188 ASN1_VALUE **pvaltmp; 205 ASN1_VALUE **pvaltmp;
189 if(tt->flags & ASN1_TFLG_COMBINE) return pval; 206 if (tt->flags & ASN1_TFLG_COMBINE)
207 return pval;
190 pvaltmp = offset2ptr(*pval, tt->offset); 208 pvaltmp = offset2ptr(*pval, tt->offset);
191 /* NOTE for BOOLEAN types the field is just a plain 209 /* NOTE for BOOLEAN types the field is just a plain
192 * int so we can't return int **, so settle for 210 * int so we can't return int **, so settle for
193 * (int *). 211 * (int *).
194 */ 212 */
195 return pvaltmp; 213 return pvaltmp;
196} 214 }
197 215
198/* Handle ANY DEFINED BY template, find the selector, look up 216/* Handle ANY DEFINED BY template, find the selector, look up
199 * the relevant ASN1_TEMPLATE in the table and return it. 217 * the relevant ASN1_TEMPLATE in the table and return it.
200 */ 218 */
201 219
202const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr) 220const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
203{ 221 int nullerr)
222 {
204 const ASN1_ADB *adb; 223 const ASN1_ADB *adb;
205 const ASN1_ADB_TABLE *atbl; 224 const ASN1_ADB_TABLE *atbl;
206 long selector; 225 long selector;
207 ASN1_VALUE **sfld; 226 ASN1_VALUE **sfld;
208 int i; 227 int i;
209 if(!(tt->flags & ASN1_TFLG_ADB_MASK)) return tt; 228 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
229 return tt;
210 230
211 /* Else ANY DEFINED BY ... get the table */ 231 /* Else ANY DEFINED BY ... get the table */
212 adb = ASN1_ADB_ptr(tt->item); 232 adb = ASN1_ADB_ptr(tt->item);
@@ -215,16 +235,18 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
215 sfld = offset2ptr(*pval, adb->offset); 235 sfld = offset2ptr(*pval, adb->offset);
216 236
217 /* Check if NULL */ 237 /* Check if NULL */
218 if(!sfld) { 238 if (!sfld)
219 if(!adb->null_tt) goto err; 239 {
240 if (!adb->null_tt)
241 goto err;
220 return adb->null_tt; 242 return adb->null_tt;
221 } 243 }
222 244
223 /* Convert type to a long: 245 /* Convert type to a long:
224 * NB: don't check for NID_undef here because it 246 * NB: don't check for NID_undef here because it
225 * might be a legitimate value in the table 247 * might be a legitimate value in the table
226 */ 248 */
227 if(tt->flags & ASN1_TFLG_ADB_OID) 249 if (tt->flags & ASN1_TFLG_ADB_OID)
228 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 250 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
229 else 251 else
230 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 252 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
@@ -237,17 +259,21 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int
237 * linear search. 259 * linear search.
238 */ 260 */
239 261
240 for(atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++) 262 for (atbl = adb->tbl, i = 0; i < adb->tblcount; i++, atbl++)
241 if(atbl->value == selector) return &atbl->tt; 263 if (atbl->value == selector)
264 return &atbl->tt;
242 265
243 /* FIXME: need to search application table too */ 266 /* FIXME: need to search application table too */
244 267
245 /* No match, return default type */ 268 /* No match, return default type */
246 if(!adb->default_tt) goto err; 269 if (!adb->default_tt)
270 goto err;
247 return adb->default_tt; 271 return adb->default_tt;
248 272
249 err: 273 err:
250 /* FIXME: should log the value or OID of unsupported type */ 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); 275 if (nullerr)
276 ASN1err(ASN1_F_ASN1_DO_ADB,
277 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
252 return NULL; 278 return NULL;
253} 279 }