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