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