1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | simple ASN1 routines
|
---|
4 | Copyright (C) Andrew Tridgell 2001
|
---|
5 |
|
---|
6 | This program is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 3 of the License, or
|
---|
9 | (at your option) any later version.
|
---|
10 |
|
---|
11 | This program is distributed in the hope that it will be useful,
|
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | GNU General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include "replace.h"
|
---|
21 | #include "system/locale.h"
|
---|
22 | #include "lib/util/asn1.h"
|
---|
23 | #include "lib/util/debug.h"
|
---|
24 | #include "lib/util/samba_util.h"
|
---|
25 |
|
---|
26 | struct nesting {
|
---|
27 | off_t start;
|
---|
28 | size_t taglen; /* for parsing */
|
---|
29 | struct nesting *next;
|
---|
30 | };
|
---|
31 |
|
---|
32 |
|
---|
33 | struct asn1_data {
|
---|
34 | uint8_t *data;
|
---|
35 | size_t length;
|
---|
36 | off_t ofs;
|
---|
37 | struct nesting *nesting;
|
---|
38 | bool has_error;
|
---|
39 | };
|
---|
40 |
|
---|
41 | /* allocate an asn1 structure */
|
---|
42 | struct asn1_data *asn1_init(TALLOC_CTX *mem_ctx)
|
---|
43 | {
|
---|
44 | struct asn1_data *ret = talloc_zero(mem_ctx, struct asn1_data);
|
---|
45 | if (ret == NULL) {
|
---|
46 | DEBUG(0,("asn1_init failed! out of memory\n"));
|
---|
47 | }
|
---|
48 | return ret;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /* free an asn1 structure */
|
---|
52 | void asn1_free(struct asn1_data *data)
|
---|
53 | {
|
---|
54 | talloc_free(data);
|
---|
55 | }
|
---|
56 |
|
---|
57 | bool asn1_has_error(const struct asn1_data *data)
|
---|
58 | {
|
---|
59 | return data->has_error;
|
---|
60 | }
|
---|
61 |
|
---|
62 | void asn1_set_error(struct asn1_data *data)
|
---|
63 | {
|
---|
64 | data->has_error = true;
|
---|
65 | }
|
---|
66 |
|
---|
67 | bool asn1_has_nesting(const struct asn1_data *data)
|
---|
68 | {
|
---|
69 | return data->nesting != NULL;
|
---|
70 | }
|
---|
71 |
|
---|
72 | off_t asn1_current_ofs(const struct asn1_data *data)
|
---|
73 | {
|
---|
74 | return data->ofs;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* write to the ASN1 buffer, advancing the buffer pointer */
|
---|
78 | bool asn1_write(struct asn1_data *data, const void *p, int len)
|
---|
79 | {
|
---|
80 | if (data->has_error) return false;
|
---|
81 |
|
---|
82 | if ((len < 0) || (data->ofs + (size_t)len < data->ofs)) {
|
---|
83 | data->has_error = true;
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if (data->length < data->ofs+len) {
|
---|
88 | uint8_t *newp;
|
---|
89 | newp = talloc_realloc(data, data->data, uint8_t, data->ofs+len);
|
---|
90 | if (!newp) {
|
---|
91 | data->has_error = true;
|
---|
92 | return false;
|
---|
93 | }
|
---|
94 | data->data = newp;
|
---|
95 | data->length = data->ofs+len;
|
---|
96 | }
|
---|
97 | memcpy(data->data + data->ofs, p, len);
|
---|
98 | data->ofs += len;
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /* useful fn for writing a uint8_t */
|
---|
103 | bool asn1_write_uint8(struct asn1_data *data, uint8_t v)
|
---|
104 | {
|
---|
105 | return asn1_write(data, &v, 1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* push a tag onto the asn1 data buffer. Used for nested structures */
|
---|
109 | bool asn1_push_tag(struct asn1_data *data, uint8_t tag)
|
---|
110 | {
|
---|
111 | struct nesting *nesting;
|
---|
112 |
|
---|
113 | if (!asn1_write_uint8(data, tag)) {
|
---|
114 | return false;
|
---|
115 | }
|
---|
116 | nesting = talloc(data, struct nesting);
|
---|
117 | if (!nesting) {
|
---|
118 | data->has_error = true;
|
---|
119 | return false;
|
---|
120 | }
|
---|
121 |
|
---|
122 | nesting->start = data->ofs;
|
---|
123 | nesting->next = data->nesting;
|
---|
124 | data->nesting = nesting;
|
---|
125 | return asn1_write_uint8(data, 0xff);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /* pop a tag */
|
---|
129 | bool asn1_pop_tag(struct asn1_data *data)
|
---|
130 | {
|
---|
131 | struct nesting *nesting;
|
---|
132 | size_t len;
|
---|
133 |
|
---|
134 | if (data->has_error) {
|
---|
135 | return false;
|
---|
136 | }
|
---|
137 |
|
---|
138 | nesting = data->nesting;
|
---|
139 |
|
---|
140 | if (!nesting) {
|
---|
141 | data->has_error = true;
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 | len = data->ofs - (nesting->start+1);
|
---|
145 | /* yes, this is ugly. We don't know in advance how many bytes the length
|
---|
146 | of a tag will take, so we assumed 1 byte. If we were wrong then we
|
---|
147 | need to correct our mistake */
|
---|
148 | if (len > 0xFFFFFF) {
|
---|
149 | data->data[nesting->start] = 0x84;
|
---|
150 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
151 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
152 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
153 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
154 | memmove(data->data+nesting->start+5, data->data+nesting->start+1, len);
|
---|
155 | data->data[nesting->start+1] = (len>>24) & 0xFF;
|
---|
156 | data->data[nesting->start+2] = (len>>16) & 0xFF;
|
---|
157 | data->data[nesting->start+3] = (len>>8) & 0xFF;
|
---|
158 | data->data[nesting->start+4] = len&0xff;
|
---|
159 | } else if (len > 0xFFFF) {
|
---|
160 | data->data[nesting->start] = 0x83;
|
---|
161 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
162 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
163 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
164 | memmove(data->data+nesting->start+4, data->data+nesting->start+1, len);
|
---|
165 | data->data[nesting->start+1] = (len>>16) & 0xFF;
|
---|
166 | data->data[nesting->start+2] = (len>>8) & 0xFF;
|
---|
167 | data->data[nesting->start+3] = len&0xff;
|
---|
168 | } else if (len > 255) {
|
---|
169 | data->data[nesting->start] = 0x82;
|
---|
170 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
171 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
172 | memmove(data->data+nesting->start+3, data->data+nesting->start+1, len);
|
---|
173 | data->data[nesting->start+1] = len>>8;
|
---|
174 | data->data[nesting->start+2] = len&0xff;
|
---|
175 | } else if (len > 127) {
|
---|
176 | data->data[nesting->start] = 0x81;
|
---|
177 | if (!asn1_write_uint8(data, 0)) return false;
|
---|
178 | memmove(data->data+nesting->start+2, data->data+nesting->start+1, len);
|
---|
179 | data->data[nesting->start+1] = len;
|
---|
180 | } else {
|
---|
181 | data->data[nesting->start] = len;
|
---|
182 | }
|
---|
183 |
|
---|
184 | data->nesting = nesting->next;
|
---|
185 | talloc_free(nesting);
|
---|
186 | return true;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /* "i" is the one's complement representation, as is the normal result of an
|
---|
190 | * implicit signed->unsigned conversion */
|
---|
191 |
|
---|
192 | static bool push_int_bigendian(struct asn1_data *data, unsigned int i, bool negative)
|
---|
193 | {
|
---|
194 | uint8_t lowest = i & 0xFF;
|
---|
195 |
|
---|
196 | i = i >> 8;
|
---|
197 | if (i != 0)
|
---|
198 | if (!push_int_bigendian(data, i, negative))
|
---|
199 | return false;
|
---|
200 |
|
---|
201 | if (data->nesting->start+1 == data->ofs) {
|
---|
202 |
|
---|
203 | /* We did not write anything yet, looking at the highest
|
---|
204 | * valued byte */
|
---|
205 |
|
---|
206 | if (negative) {
|
---|
207 | /* Don't write leading 0xff's */
|
---|
208 | if (lowest == 0xFF)
|
---|
209 | return true;
|
---|
210 |
|
---|
211 | if ((lowest & 0x80) == 0) {
|
---|
212 | /* The only exception for a leading 0xff is if
|
---|
213 | * the highest bit is 0, which would indicate
|
---|
214 | * a positive value */
|
---|
215 | if (!asn1_write_uint8(data, 0xff))
|
---|
216 | return false;
|
---|
217 | }
|
---|
218 | } else {
|
---|
219 | if (lowest & 0x80) {
|
---|
220 | /* The highest bit of a positive integer is 1,
|
---|
221 | * this would indicate a negative number. Push
|
---|
222 | * a 0 to indicate a positive one */
|
---|
223 | if (!asn1_write_uint8(data, 0))
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | return asn1_write_uint8(data, lowest);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* write an Integer without the tag framing. Needed for example for the LDAP
|
---|
233 | * Abandon Operation */
|
---|
234 |
|
---|
235 | bool asn1_write_implicit_Integer(struct asn1_data *data, int i)
|
---|
236 | {
|
---|
237 | if (data->has_error) {
|
---|
238 | return false;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (i == -1) {
|
---|
242 | /* -1 is special as it consists of all-0xff bytes. In
|
---|
243 | push_int_bigendian this is the only case that is not
|
---|
244 | properly handled, as all 0xff bytes would be handled as
|
---|
245 | leading ones to be ignored. */
|
---|
246 | return asn1_write_uint8(data, 0xff);
|
---|
247 | } else {
|
---|
248 | return push_int_bigendian(data, i, i<0);
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | /* write an integer */
|
---|
254 | bool asn1_write_Integer(struct asn1_data *data, int i)
|
---|
255 | {
|
---|
256 | if (!asn1_push_tag(data, ASN1_INTEGER)) return false;
|
---|
257 | if (!asn1_write_implicit_Integer(data, i)) return false;
|
---|
258 | return asn1_pop_tag(data);
|
---|
259 | }
|
---|
260 |
|
---|
261 | /* write a BIT STRING */
|
---|
262 | bool asn1_write_BitString(struct asn1_data *data, const void *p, size_t length, uint8_t padding)
|
---|
263 | {
|
---|
264 | if (!asn1_push_tag(data, ASN1_BIT_STRING)) return false;
|
---|
265 | if (!asn1_write_uint8(data, padding)) return false;
|
---|
266 | if (!asn1_write(data, p, length)) return false;
|
---|
267 | return asn1_pop_tag(data);
|
---|
268 | }
|
---|
269 |
|
---|
270 | bool ber_write_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *OID)
|
---|
271 | {
|
---|
272 | unsigned int v, v2;
|
---|
273 | const char *p = (const char *)OID;
|
---|
274 | char *newp;
|
---|
275 | int i;
|
---|
276 |
|
---|
277 | if (!isdigit(*p)) return false;
|
---|
278 | v = strtoul(p, &newp, 10);
|
---|
279 | if (newp[0] != '.') return false;
|
---|
280 | p = newp + 1;
|
---|
281 |
|
---|
282 | if (!isdigit(*p)) return false;
|
---|
283 | v2 = strtoul(p, &newp, 10);
|
---|
284 | if (newp[0] != '.') return false;
|
---|
285 | p = newp + 1;
|
---|
286 |
|
---|
287 | /*the ber representation can't use more space than the string one */
|
---|
288 | *blob = data_blob_talloc(mem_ctx, NULL, strlen(OID));
|
---|
289 | if (!blob->data) return false;
|
---|
290 |
|
---|
291 | blob->data[0] = 40*v + v2;
|
---|
292 |
|
---|
293 | i = 1;
|
---|
294 | while (*p) {
|
---|
295 | if (!isdigit(*p)) return false;
|
---|
296 | v = strtoul(p, &newp, 10);
|
---|
297 | if (newp[0] == '.') {
|
---|
298 | p = newp + 1;
|
---|
299 | /* check for empty last component */
|
---|
300 | if (!*p) return false;
|
---|
301 | } else if (newp[0] == '\0') {
|
---|
302 | p = newp;
|
---|
303 | } else {
|
---|
304 | data_blob_free(blob);
|
---|
305 | return false;
|
---|
306 | }
|
---|
307 | if (v >= (1<<28)) blob->data[i++] = (0x80 | ((v>>28)&0x7f));
|
---|
308 | if (v >= (1<<21)) blob->data[i++] = (0x80 | ((v>>21)&0x7f));
|
---|
309 | if (v >= (1<<14)) blob->data[i++] = (0x80 | ((v>>14)&0x7f));
|
---|
310 | if (v >= (1<<7)) blob->data[i++] = (0x80 | ((v>>7)&0x7f));
|
---|
311 | blob->data[i++] = (v&0x7f);
|
---|
312 | }
|
---|
313 |
|
---|
314 | blob->length = i;
|
---|
315 |
|
---|
316 | return true;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Serialize partial OID string.
|
---|
321 | * Partial OIDs are in the form:
|
---|
322 | * 1:2.5.6:0x81
|
---|
323 | * 1:2.5.6:0x8182
|
---|
324 | */
|
---|
325 | bool ber_write_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB *blob, const char *partial_oid)
|
---|
326 | {
|
---|
327 | TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
|
---|
328 | char *oid = talloc_strdup(tmp_ctx, partial_oid);
|
---|
329 | char *p;
|
---|
330 |
|
---|
331 | /* truncate partial part so ber_write_OID_String() works */
|
---|
332 | p = strchr(oid, ':');
|
---|
333 | if (p) {
|
---|
334 | *p = '\0';
|
---|
335 | p++;
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (!ber_write_OID_String(mem_ctx, blob, oid)) {
|
---|
339 | talloc_free(tmp_ctx);
|
---|
340 | return false;
|
---|
341 | }
|
---|
342 |
|
---|
343 | /* Add partially encoded sub-identifier */
|
---|
344 | if (p) {
|
---|
345 | DATA_BLOB tmp_blob = strhex_to_data_blob(tmp_ctx, p);
|
---|
346 | if (!data_blob_append(mem_ctx, blob, tmp_blob.data,
|
---|
347 | tmp_blob.length)) {
|
---|
348 | talloc_free(tmp_ctx);
|
---|
349 | return false;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | talloc_free(tmp_ctx);
|
---|
354 |
|
---|
355 | return true;
|
---|
356 | }
|
---|
357 |
|
---|
358 | /* write an object ID to a ASN1 buffer */
|
---|
359 | bool asn1_write_OID(struct asn1_data *data, const char *OID)
|
---|
360 | {
|
---|
361 | DATA_BLOB blob;
|
---|
362 |
|
---|
363 | if (!asn1_push_tag(data, ASN1_OID)) return false;
|
---|
364 |
|
---|
365 | if (!ber_write_OID_String(NULL, &blob, OID)) {
|
---|
366 | data->has_error = true;
|
---|
367 | return false;
|
---|
368 | }
|
---|
369 |
|
---|
370 | if (!asn1_write(data, blob.data, blob.length)) {
|
---|
371 | data_blob_free(&blob);
|
---|
372 | data->has_error = true;
|
---|
373 | return false;
|
---|
374 | }
|
---|
375 | data_blob_free(&blob);
|
---|
376 | return asn1_pop_tag(data);
|
---|
377 | }
|
---|
378 |
|
---|
379 | /* write an octet string */
|
---|
380 | bool asn1_write_OctetString(struct asn1_data *data, const void *p, size_t length)
|
---|
381 | {
|
---|
382 | if (!asn1_push_tag(data, ASN1_OCTET_STRING)) return false;
|
---|
383 | if (!asn1_write(data, p, length)) return false;
|
---|
384 | return asn1_pop_tag(data);
|
---|
385 | }
|
---|
386 |
|
---|
387 | /* write a LDAP string */
|
---|
388 | bool asn1_write_LDAPString(struct asn1_data *data, const char *s)
|
---|
389 | {
|
---|
390 | return asn1_write(data, s, strlen(s));
|
---|
391 | }
|
---|
392 |
|
---|
393 | /* write a LDAP string from a DATA_BLOB */
|
---|
394 | bool asn1_write_DATA_BLOB_LDAPString(struct asn1_data *data, const DATA_BLOB *s)
|
---|
395 | {
|
---|
396 | return asn1_write(data, s->data, s->length);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /* write a general string */
|
---|
400 | bool asn1_write_GeneralString(struct asn1_data *data, const char *s)
|
---|
401 | {
|
---|
402 | if (!asn1_push_tag(data, ASN1_GENERAL_STRING)) return false;
|
---|
403 | if (!asn1_write_LDAPString(data, s)) return false;
|
---|
404 | return asn1_pop_tag(data);
|
---|
405 | }
|
---|
406 |
|
---|
407 | bool asn1_write_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
---|
408 | {
|
---|
409 | if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
|
---|
410 | if (!asn1_write(data, blob->data, blob->length)) return false;
|
---|
411 | return asn1_pop_tag(data);
|
---|
412 | }
|
---|
413 |
|
---|
414 | /* write a BOOLEAN */
|
---|
415 | bool asn1_write_BOOLEAN(struct asn1_data *data, bool v)
|
---|
416 | {
|
---|
417 | if (!asn1_push_tag(data, ASN1_BOOLEAN)) return false;
|
---|
418 | if (!asn1_write_uint8(data, v ? 0xFF : 0)) return false;
|
---|
419 | return asn1_pop_tag(data);
|
---|
420 | }
|
---|
421 |
|
---|
422 | bool asn1_read_BOOLEAN(struct asn1_data *data, bool *v)
|
---|
423 | {
|
---|
424 | uint8_t tmp = 0;
|
---|
425 | if (!asn1_start_tag(data, ASN1_BOOLEAN)) return false;
|
---|
426 | *v = false;
|
---|
427 | if (!asn1_read_uint8(data, &tmp)) return false;
|
---|
428 | if (tmp == 0xFF) {
|
---|
429 | *v = true;
|
---|
430 | }
|
---|
431 | return asn1_end_tag(data);
|
---|
432 | }
|
---|
433 |
|
---|
434 | /* write a BOOLEAN in a simple context */
|
---|
435 | bool asn1_write_BOOLEAN_context(struct asn1_data *data, bool v, int context)
|
---|
436 | {
|
---|
437 | if (!asn1_push_tag(data, ASN1_CONTEXT_SIMPLE(context))) return false;
|
---|
438 | if (!asn1_write_uint8(data, v ? 0xFF : 0)) return false;
|
---|
439 | return asn1_pop_tag(data);
|
---|
440 | }
|
---|
441 |
|
---|
442 | bool asn1_read_BOOLEAN_context(struct asn1_data *data, bool *v, int context)
|
---|
443 | {
|
---|
444 | uint8_t tmp = 0;
|
---|
445 | if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(context))) return false;
|
---|
446 | *v = false;
|
---|
447 | if (!asn1_read_uint8(data, &tmp)) return false;
|
---|
448 | if (tmp == 0xFF) {
|
---|
449 | *v = true;
|
---|
450 | }
|
---|
451 | return asn1_end_tag(data);
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* check a BOOLEAN */
|
---|
455 | bool asn1_check_BOOLEAN(struct asn1_data *data, bool v)
|
---|
456 | {
|
---|
457 | uint8_t b = 0;
|
---|
458 |
|
---|
459 | if (!asn1_read_uint8(data, &b)) return false;
|
---|
460 | if (b != ASN1_BOOLEAN) {
|
---|
461 | data->has_error = true;
|
---|
462 | return false;
|
---|
463 | }
|
---|
464 | if (!asn1_read_uint8(data, &b)) return false;
|
---|
465 | if (b != v) {
|
---|
466 | data->has_error = true;
|
---|
467 | return false;
|
---|
468 | }
|
---|
469 | return !data->has_error;
|
---|
470 | }
|
---|
471 |
|
---|
472 |
|
---|
473 | /* load a struct asn1_data structure with a lump of data, ready to be parsed */
|
---|
474 | bool asn1_load(struct asn1_data *data, DATA_BLOB blob)
|
---|
475 | {
|
---|
476 | ZERO_STRUCTP(data);
|
---|
477 | data->data = (uint8_t *)talloc_memdup(data, blob.data, blob.length);
|
---|
478 | if (!data->data) {
|
---|
479 | data->has_error = true;
|
---|
480 | return false;
|
---|
481 | }
|
---|
482 | data->length = blob.length;
|
---|
483 | return true;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /* Peek into an ASN1 buffer, not advancing the pointer */
|
---|
487 | bool asn1_peek(struct asn1_data *data, void *p, int len)
|
---|
488 | {
|
---|
489 | if (data->has_error)
|
---|
490 | return false;
|
---|
491 |
|
---|
492 | if (len < 0 || data->ofs + len < data->ofs || data->ofs + len < len)
|
---|
493 | return false;
|
---|
494 |
|
---|
495 | if (data->ofs + len > data->length) {
|
---|
496 | /* we need to mark the buffer as consumed, so the caller knows
|
---|
497 | this was an out of data error, and not a decode error */
|
---|
498 | data->ofs = data->length;
|
---|
499 | return false;
|
---|
500 | }
|
---|
501 |
|
---|
502 | memcpy(p, data->data + data->ofs, len);
|
---|
503 | return true;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /* read from a ASN1 buffer, advancing the buffer pointer */
|
---|
507 | bool asn1_read(struct asn1_data *data, void *p, int len)
|
---|
508 | {
|
---|
509 | if (!asn1_peek(data, p, len)) {
|
---|
510 | data->has_error = true;
|
---|
511 | return false;
|
---|
512 | }
|
---|
513 |
|
---|
514 | data->ofs += len;
|
---|
515 | return true;
|
---|
516 | }
|
---|
517 |
|
---|
518 | /* read a uint8_t from a ASN1 buffer */
|
---|
519 | bool asn1_read_uint8(struct asn1_data *data, uint8_t *v)
|
---|
520 | {
|
---|
521 | return asn1_read(data, v, 1);
|
---|
522 | }
|
---|
523 |
|
---|
524 | bool asn1_peek_uint8(struct asn1_data *data, uint8_t *v)
|
---|
525 | {
|
---|
526 | return asn1_peek(data, v, 1);
|
---|
527 | }
|
---|
528 |
|
---|
529 | bool asn1_peek_tag(struct asn1_data *data, uint8_t tag)
|
---|
530 | {
|
---|
531 | uint8_t b;
|
---|
532 |
|
---|
533 | if (asn1_tag_remaining(data) <= 0) {
|
---|
534 | return false;
|
---|
535 | }
|
---|
536 |
|
---|
537 | if (!asn1_peek_uint8(data, &b))
|
---|
538 | return false;
|
---|
539 |
|
---|
540 | return (b == tag);
|
---|
541 | }
|
---|
542 |
|
---|
543 | /*
|
---|
544 | * just get the needed size the tag would consume
|
---|
545 | */
|
---|
546 | static bool asn1_peek_tag_needed_size(struct asn1_data *data, uint8_t tag,
|
---|
547 | size_t *size)
|
---|
548 | {
|
---|
549 | off_t start_ofs = data->ofs;
|
---|
550 | uint8_t b;
|
---|
551 | size_t taglen = 0;
|
---|
552 |
|
---|
553 | if (data->has_error) {
|
---|
554 | return false;
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (!asn1_read_uint8(data, &b)) {
|
---|
558 | data->ofs = start_ofs;
|
---|
559 | data->has_error = false;
|
---|
560 | return false;
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (b != tag) {
|
---|
564 | data->ofs = start_ofs;
|
---|
565 | data->has_error = false;
|
---|
566 | return false;
|
---|
567 | }
|
---|
568 |
|
---|
569 | if (!asn1_read_uint8(data, &b)) {
|
---|
570 | data->ofs = start_ofs;
|
---|
571 | data->has_error = false;
|
---|
572 | return false;
|
---|
573 | }
|
---|
574 |
|
---|
575 | if (b & 0x80) {
|
---|
576 | int n = b & 0x7f;
|
---|
577 | if (!asn1_read_uint8(data, &b)) {
|
---|
578 | data->ofs = start_ofs;
|
---|
579 | data->has_error = false;
|
---|
580 | return false;
|
---|
581 | }
|
---|
582 | if (n > 4) {
|
---|
583 | /*
|
---|
584 | * We should not allow more than 4 bytes
|
---|
585 | * for the encoding of the tag length.
|
---|
586 | *
|
---|
587 | * Otherwise we'd overflow the taglen
|
---|
588 | * variable on 32 bit systems.
|
---|
589 | */
|
---|
590 | data->ofs = start_ofs;
|
---|
591 | data->has_error = false;
|
---|
592 | return false;
|
---|
593 | }
|
---|
594 | taglen = b;
|
---|
595 | while (n > 1) {
|
---|
596 | if (!asn1_read_uint8(data, &b)) {
|
---|
597 | data->ofs = start_ofs;
|
---|
598 | data->has_error = false;
|
---|
599 | return false;
|
---|
600 | }
|
---|
601 | taglen = (taglen << 8) | b;
|
---|
602 | n--;
|
---|
603 | }
|
---|
604 | } else {
|
---|
605 | taglen = b;
|
---|
606 | }
|
---|
607 |
|
---|
608 | *size = (data->ofs - start_ofs) + taglen;
|
---|
609 |
|
---|
610 | data->ofs = start_ofs;
|
---|
611 | data->has_error = false;
|
---|
612 | return true;
|
---|
613 | }
|
---|
614 |
|
---|
615 | /* start reading a nested asn1 structure */
|
---|
616 | bool asn1_start_tag(struct asn1_data *data, uint8_t tag)
|
---|
617 | {
|
---|
618 | uint8_t b;
|
---|
619 | struct nesting *nesting;
|
---|
620 |
|
---|
621 | if (!asn1_read_uint8(data, &b))
|
---|
622 | return false;
|
---|
623 |
|
---|
624 | if (b != tag) {
|
---|
625 | data->has_error = true;
|
---|
626 | return false;
|
---|
627 | }
|
---|
628 | nesting = talloc(data, struct nesting);
|
---|
629 | if (!nesting) {
|
---|
630 | data->has_error = true;
|
---|
631 | return false;
|
---|
632 | }
|
---|
633 |
|
---|
634 | if (!asn1_read_uint8(data, &b)) {
|
---|
635 | return false;
|
---|
636 | }
|
---|
637 |
|
---|
638 | if (b & 0x80) {
|
---|
639 | int n = b & 0x7f;
|
---|
640 | if (!asn1_read_uint8(data, &b))
|
---|
641 | return false;
|
---|
642 | nesting->taglen = b;
|
---|
643 | while (n > 1) {
|
---|
644 | if (!asn1_read_uint8(data, &b))
|
---|
645 | return false;
|
---|
646 | nesting->taglen = (nesting->taglen << 8) | b;
|
---|
647 | n--;
|
---|
648 | }
|
---|
649 | } else {
|
---|
650 | nesting->taglen = b;
|
---|
651 | }
|
---|
652 | nesting->start = data->ofs;
|
---|
653 | nesting->next = data->nesting;
|
---|
654 | data->nesting = nesting;
|
---|
655 | if (asn1_tag_remaining(data) == -1) {
|
---|
656 | return false;
|
---|
657 | }
|
---|
658 | return !data->has_error;
|
---|
659 | }
|
---|
660 |
|
---|
661 | /* stop reading a tag */
|
---|
662 | bool asn1_end_tag(struct asn1_data *data)
|
---|
663 | {
|
---|
664 | struct nesting *nesting;
|
---|
665 |
|
---|
666 | /* make sure we read it all */
|
---|
667 | if (asn1_tag_remaining(data) != 0) {
|
---|
668 | data->has_error = true;
|
---|
669 | return false;
|
---|
670 | }
|
---|
671 |
|
---|
672 | nesting = data->nesting;
|
---|
673 |
|
---|
674 | if (!nesting) {
|
---|
675 | data->has_error = true;
|
---|
676 | return false;
|
---|
677 | }
|
---|
678 |
|
---|
679 | data->nesting = nesting->next;
|
---|
680 | talloc_free(nesting);
|
---|
681 | return true;
|
---|
682 | }
|
---|
683 |
|
---|
684 | /* work out how many bytes are left in this nested tag */
|
---|
685 | int asn1_tag_remaining(struct asn1_data *data)
|
---|
686 | {
|
---|
687 | int remaining;
|
---|
688 | if (data->has_error) {
|
---|
689 | return -1;
|
---|
690 | }
|
---|
691 |
|
---|
692 | if (!data->nesting) {
|
---|
693 | data->has_error = true;
|
---|
694 | return -1;
|
---|
695 | }
|
---|
696 | remaining = data->nesting->taglen - (data->ofs - data->nesting->start);
|
---|
697 | if (remaining > (data->length - data->ofs)) {
|
---|
698 | data->has_error = true;
|
---|
699 | return -1;
|
---|
700 | }
|
---|
701 | return remaining;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /**
|
---|
705 | * Internal implementation for reading binary OIDs
|
---|
706 | * Reading is done as far in the buffer as valid OID
|
---|
707 | * till buffer ends or not valid sub-identifier is found.
|
---|
708 | */
|
---|
709 | static bool _ber_read_OID_String_impl(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
|
---|
710 | char **OID, size_t *bytes_eaten)
|
---|
711 | {
|
---|
712 | int i;
|
---|
713 | uint8_t *b;
|
---|
714 | unsigned int v;
|
---|
715 | char *tmp_oid = NULL;
|
---|
716 |
|
---|
717 | if (blob.length < 2) return false;
|
---|
718 |
|
---|
719 | b = blob.data;
|
---|
720 |
|
---|
721 | tmp_oid = talloc_asprintf(mem_ctx, "%u", b[0]/40);
|
---|
722 | if (!tmp_oid) goto nomem;
|
---|
723 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", b[0]%40);
|
---|
724 | if (!tmp_oid) goto nomem;
|
---|
725 |
|
---|
726 | if (bytes_eaten != NULL) {
|
---|
727 | *bytes_eaten = 0;
|
---|
728 | }
|
---|
729 |
|
---|
730 | for(i = 1, v = 0; i < blob.length; i++) {
|
---|
731 | v = (v<<7) | (b[i]&0x7f);
|
---|
732 | if ( ! (b[i] & 0x80)) {
|
---|
733 | tmp_oid = talloc_asprintf_append_buffer(tmp_oid, ".%u", v);
|
---|
734 | v = 0;
|
---|
735 | if (bytes_eaten)
|
---|
736 | *bytes_eaten = i+1;
|
---|
737 | }
|
---|
738 | if (!tmp_oid) goto nomem;
|
---|
739 | }
|
---|
740 |
|
---|
741 | *OID = tmp_oid;
|
---|
742 | return true;
|
---|
743 |
|
---|
744 | nomem:
|
---|
745 | return false;
|
---|
746 | }
|
---|
747 |
|
---|
748 | /* read an object ID from a data blob */
|
---|
749 | bool ber_read_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob, char **OID)
|
---|
750 | {
|
---|
751 | size_t bytes_eaten;
|
---|
752 |
|
---|
753 | if (!_ber_read_OID_String_impl(mem_ctx, blob, OID, &bytes_eaten))
|
---|
754 | return false;
|
---|
755 |
|
---|
756 | return (bytes_eaten == blob.length);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /**
|
---|
760 | * Deserialize partial OID string.
|
---|
761 | * Partial OIDs are in the form:
|
---|
762 | * 1:2.5.6:0x81
|
---|
763 | * 1:2.5.6:0x8182
|
---|
764 | */
|
---|
765 | bool ber_read_partial_OID_String(TALLOC_CTX *mem_ctx, DATA_BLOB blob,
|
---|
766 | char **partial_oid)
|
---|
767 | {
|
---|
768 | size_t bytes_left;
|
---|
769 | size_t bytes_eaten;
|
---|
770 | char *identifier = NULL;
|
---|
771 | char *tmp_oid = NULL;
|
---|
772 |
|
---|
773 | if (!_ber_read_OID_String_impl(mem_ctx, blob, &tmp_oid, &bytes_eaten))
|
---|
774 | return false;
|
---|
775 |
|
---|
776 | if (bytes_eaten < blob.length) {
|
---|
777 | bytes_left = blob.length - bytes_eaten;
|
---|
778 | identifier = hex_encode_talloc(mem_ctx, &blob.data[bytes_eaten], bytes_left);
|
---|
779 | if (!identifier) goto nomem;
|
---|
780 |
|
---|
781 | *partial_oid = talloc_asprintf_append_buffer(tmp_oid, ":0x%s", identifier);
|
---|
782 | if (!*partial_oid) goto nomem;
|
---|
783 | TALLOC_FREE(identifier);
|
---|
784 | } else {
|
---|
785 | *partial_oid = tmp_oid;
|
---|
786 | }
|
---|
787 |
|
---|
788 | return true;
|
---|
789 |
|
---|
790 | nomem:
|
---|
791 | TALLOC_FREE(identifier);
|
---|
792 | TALLOC_FREE(tmp_oid);
|
---|
793 | return false;
|
---|
794 | }
|
---|
795 |
|
---|
796 | /* read an object ID from a ASN1 buffer */
|
---|
797 | bool asn1_read_OID(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **OID)
|
---|
798 | {
|
---|
799 | DATA_BLOB blob;
|
---|
800 | int len;
|
---|
801 |
|
---|
802 | if (!asn1_start_tag(data, ASN1_OID)) return false;
|
---|
803 |
|
---|
804 | len = asn1_tag_remaining(data);
|
---|
805 | if (len < 0) {
|
---|
806 | data->has_error = true;
|
---|
807 | return false;
|
---|
808 | }
|
---|
809 |
|
---|
810 | blob = data_blob(NULL, len);
|
---|
811 | if (!blob.data) {
|
---|
812 | data->has_error = true;
|
---|
813 | return false;
|
---|
814 | }
|
---|
815 |
|
---|
816 | if (!asn1_read(data, blob.data, len)) return false;
|
---|
817 | if (!asn1_end_tag(data)) {
|
---|
818 | data_blob_free(&blob);
|
---|
819 | return false;
|
---|
820 | }
|
---|
821 |
|
---|
822 | if (!ber_read_OID_String(mem_ctx, blob, OID)) {
|
---|
823 | data->has_error = true;
|
---|
824 | data_blob_free(&blob);
|
---|
825 | return false;
|
---|
826 | }
|
---|
827 |
|
---|
828 | data_blob_free(&blob);
|
---|
829 | return true;
|
---|
830 | }
|
---|
831 |
|
---|
832 | /* check that the next object ID is correct */
|
---|
833 | bool asn1_check_OID(struct asn1_data *data, const char *OID)
|
---|
834 | {
|
---|
835 | char *id;
|
---|
836 |
|
---|
837 | if (!asn1_read_OID(data, data, &id)) return false;
|
---|
838 |
|
---|
839 | if (strcmp(id, OID) != 0) {
|
---|
840 | talloc_free(id);
|
---|
841 | data->has_error = true;
|
---|
842 | return false;
|
---|
843 | }
|
---|
844 | talloc_free(id);
|
---|
845 | return true;
|
---|
846 | }
|
---|
847 |
|
---|
848 | /* read a LDAPString from a ASN1 buffer */
|
---|
849 | bool asn1_read_LDAPString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
---|
850 | {
|
---|
851 | int len;
|
---|
852 | len = asn1_tag_remaining(data);
|
---|
853 | if (len < 0) {
|
---|
854 | data->has_error = true;
|
---|
855 | return false;
|
---|
856 | }
|
---|
857 | *s = talloc_array(mem_ctx, char, len+1);
|
---|
858 | if (! *s) {
|
---|
859 | data->has_error = true;
|
---|
860 | return false;
|
---|
861 | }
|
---|
862 | (*s)[len] = 0;
|
---|
863 | return asn1_read(data, *s, len);
|
---|
864 | }
|
---|
865 |
|
---|
866 |
|
---|
867 | /* read a GeneralString from a ASN1 buffer */
|
---|
868 | bool asn1_read_GeneralString(struct asn1_data *data, TALLOC_CTX *mem_ctx, char **s)
|
---|
869 | {
|
---|
870 | if (!asn1_start_tag(data, ASN1_GENERAL_STRING)) return false;
|
---|
871 | if (!asn1_read_LDAPString(data, mem_ctx, s)) return false;
|
---|
872 | return asn1_end_tag(data);
|
---|
873 | }
|
---|
874 |
|
---|
875 |
|
---|
876 | /* read a octet string blob */
|
---|
877 | bool asn1_read_OctetString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob)
|
---|
878 | {
|
---|
879 | int len;
|
---|
880 | ZERO_STRUCTP(blob);
|
---|
881 | if (!asn1_start_tag(data, ASN1_OCTET_STRING)) return false;
|
---|
882 | len = asn1_tag_remaining(data);
|
---|
883 | if (len < 0) {
|
---|
884 | data->has_error = true;
|
---|
885 | return false;
|
---|
886 | }
|
---|
887 | *blob = data_blob_talloc(mem_ctx, NULL, len+1);
|
---|
888 | if (!blob->data || blob->length < len) {
|
---|
889 | data->has_error = true;
|
---|
890 | return false;
|
---|
891 | }
|
---|
892 | if (!asn1_read(data, blob->data, len)) goto err;
|
---|
893 | if (!asn1_end_tag(data)) goto err;
|
---|
894 | blob->length--;
|
---|
895 | blob->data[len] = 0;
|
---|
896 | return true;
|
---|
897 |
|
---|
898 | err:
|
---|
899 |
|
---|
900 | data_blob_free(blob);
|
---|
901 | *blob = data_blob_null;
|
---|
902 | return false;
|
---|
903 | }
|
---|
904 |
|
---|
905 | bool asn1_read_ContextSimple(struct asn1_data *data, uint8_t num, DATA_BLOB *blob)
|
---|
906 | {
|
---|
907 | int len;
|
---|
908 | ZERO_STRUCTP(blob);
|
---|
909 | if (!asn1_start_tag(data, ASN1_CONTEXT_SIMPLE(num))) return false;
|
---|
910 | len = asn1_tag_remaining(data);
|
---|
911 | if (len < 0) {
|
---|
912 | data->has_error = true;
|
---|
913 | return false;
|
---|
914 | }
|
---|
915 | *blob = data_blob(NULL, len);
|
---|
916 | if ((len != 0) && (!blob->data)) {
|
---|
917 | data->has_error = true;
|
---|
918 | return false;
|
---|
919 | }
|
---|
920 | if (!asn1_read(data, blob->data, len)) return false;
|
---|
921 | return asn1_end_tag(data);
|
---|
922 | }
|
---|
923 |
|
---|
924 | /* read an integer without tag*/
|
---|
925 | bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
|
---|
926 | {
|
---|
927 | uint8_t b;
|
---|
928 | bool first_byte = true;
|
---|
929 | *i = 0;
|
---|
930 |
|
---|
931 | while (!data->has_error && asn1_tag_remaining(data)>0) {
|
---|
932 | if (!asn1_read_uint8(data, &b)) return false;
|
---|
933 | if (first_byte) {
|
---|
934 | if (b & 0x80) {
|
---|
935 | /* Number is negative.
|
---|
936 | Set i to -1 for sign extend. */
|
---|
937 | *i = -1;
|
---|
938 | }
|
---|
939 | first_byte = false;
|
---|
940 | }
|
---|
941 | *i = (*i << 8) + b;
|
---|
942 | }
|
---|
943 | return !data->has_error;
|
---|
944 |
|
---|
945 | }
|
---|
946 |
|
---|
947 | /* read an integer */
|
---|
948 | bool asn1_read_Integer(struct asn1_data *data, int *i)
|
---|
949 | {
|
---|
950 | *i = 0;
|
---|
951 |
|
---|
952 | if (!asn1_start_tag(data, ASN1_INTEGER)) return false;
|
---|
953 | if (!asn1_read_implicit_Integer(data, i)) return false;
|
---|
954 | return asn1_end_tag(data);
|
---|
955 | }
|
---|
956 |
|
---|
957 | /* read a BIT STRING */
|
---|
958 | bool asn1_read_BitString(struct asn1_data *data, TALLOC_CTX *mem_ctx, DATA_BLOB *blob, uint8_t *padding)
|
---|
959 | {
|
---|
960 | int len;
|
---|
961 | ZERO_STRUCTP(blob);
|
---|
962 | if (!asn1_start_tag(data, ASN1_BIT_STRING)) return false;
|
---|
963 | len = asn1_tag_remaining(data);
|
---|
964 | if (len < 0) {
|
---|
965 | data->has_error = true;
|
---|
966 | return false;
|
---|
967 | }
|
---|
968 | if (!asn1_read_uint8(data, padding)) return false;
|
---|
969 |
|
---|
970 | *blob = data_blob_talloc(mem_ctx, NULL, len+1);
|
---|
971 | if (!blob->data || blob->length < len) {
|
---|
972 | data->has_error = true;
|
---|
973 | return false;
|
---|
974 | }
|
---|
975 | if (asn1_read(data, blob->data, len - 1)) {
|
---|
976 | blob->length--;
|
---|
977 | blob->data[len] = 0;
|
---|
978 | asn1_end_tag(data);
|
---|
979 | }
|
---|
980 |
|
---|
981 | if (data->has_error) {
|
---|
982 | data_blob_free(blob);
|
---|
983 | *blob = data_blob_null;
|
---|
984 | *padding = 0;
|
---|
985 | return false;
|
---|
986 | }
|
---|
987 | return true;
|
---|
988 | }
|
---|
989 |
|
---|
990 | /* read an integer */
|
---|
991 | bool asn1_read_enumerated(struct asn1_data *data, int *v)
|
---|
992 | {
|
---|
993 | *v = 0;
|
---|
994 |
|
---|
995 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
---|
996 | while (!data->has_error && asn1_tag_remaining(data)>0) {
|
---|
997 | uint8_t b;
|
---|
998 | if (!asn1_read_uint8(data, &b)) {
|
---|
999 | return false;
|
---|
1000 | }
|
---|
1001 | *v = (*v << 8) + b;
|
---|
1002 | }
|
---|
1003 | return asn1_end_tag(data);
|
---|
1004 | }
|
---|
1005 |
|
---|
1006 | /* check a enumerated value is correct */
|
---|
1007 | bool asn1_check_enumerated(struct asn1_data *data, int v)
|
---|
1008 | {
|
---|
1009 | uint8_t b;
|
---|
1010 | if (!asn1_start_tag(data, ASN1_ENUMERATED)) return false;
|
---|
1011 | if (!asn1_read_uint8(data, &b)) return false;
|
---|
1012 | if (!asn1_end_tag(data)) return false;
|
---|
1013 |
|
---|
1014 | if (v != b)
|
---|
1015 | data->has_error = false;
|
---|
1016 |
|
---|
1017 | return !data->has_error;
|
---|
1018 | }
|
---|
1019 |
|
---|
1020 | /* write an enumerated value to the stream */
|
---|
1021 | bool asn1_write_enumerated(struct asn1_data *data, uint8_t v)
|
---|
1022 | {
|
---|
1023 | if (!asn1_push_tag(data, ASN1_ENUMERATED)) return false;
|
---|
1024 | if (!asn1_write_uint8(data, v)) return false;
|
---|
1025 | return asn1_pop_tag(data);
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | /*
|
---|
1029 | Get us the data just written without copying
|
---|
1030 | */
|
---|
1031 | bool asn1_blob(const struct asn1_data *asn1, DATA_BLOB *blob)
|
---|
1032 | {
|
---|
1033 | if (asn1->has_error) {
|
---|
1034 | return false;
|
---|
1035 | }
|
---|
1036 | if (asn1->nesting != NULL) {
|
---|
1037 | return false;
|
---|
1038 | }
|
---|
1039 | blob->data = asn1->data;
|
---|
1040 | blob->length = asn1->length;
|
---|
1041 | return true;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 | bool asn1_extract_blob(struct asn1_data *asn1, TALLOC_CTX *mem_ctx,
|
---|
1045 | DATA_BLOB *pblob)
|
---|
1046 | {
|
---|
1047 | DATA_BLOB blob;
|
---|
1048 |
|
---|
1049 | if (!asn1_blob(asn1, &blob)) {
|
---|
1050 | return false;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | *pblob = (DATA_BLOB) { .length = blob.length };
|
---|
1054 | pblob->data = talloc_move(mem_ctx, &blob.data);
|
---|
1055 |
|
---|
1056 | /*
|
---|
1057 | * Stop access from here on
|
---|
1058 | */
|
---|
1059 | asn1->has_error = true;
|
---|
1060 |
|
---|
1061 | return true;
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /*
|
---|
1065 | Fill in an asn1 struct without making a copy
|
---|
1066 | */
|
---|
1067 | void asn1_load_nocopy(struct asn1_data *data, uint8_t *buf, size_t len)
|
---|
1068 | {
|
---|
1069 | ZERO_STRUCTP(data);
|
---|
1070 | data->data = buf;
|
---|
1071 | data->length = len;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | int asn1_peek_full_tag(DATA_BLOB blob, uint8_t tag, size_t *packet_size)
|
---|
1075 | {
|
---|
1076 | struct asn1_data asn1;
|
---|
1077 | size_t size;
|
---|
1078 | bool ok;
|
---|
1079 |
|
---|
1080 | ZERO_STRUCT(asn1);
|
---|
1081 | asn1.data = blob.data;
|
---|
1082 | asn1.length = blob.length;
|
---|
1083 |
|
---|
1084 | ok = asn1_peek_tag_needed_size(&asn1, tag, &size);
|
---|
1085 | if (!ok) {
|
---|
1086 | return EMSGSIZE;
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | if (size > blob.length) {
|
---|
1090 | *packet_size = size;
|
---|
1091 | return EAGAIN;
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | *packet_size = size;
|
---|
1095 | return 0;
|
---|
1096 | }
|
---|