1 /**
2  * str2wire.c - read txt presentation of RRs
3  *
4  * (c) NLnet Labs, 2005-2006
5  *
6  * See the file LICENSE for the license
7  */
8 
9 /**
10  * \file
11  *
12  * Parses text to wireformat.
13  */
14 #include "config.h"
15 #include "gldns/str2wire.h"
16 #include "gldns/wire2str.h"
17 #include "gldns/gbuffer.h"
18 #include "gldns/parse.h"
19 #include "gldns/parseutil.h"
20 #include <ctype.h>
21 #ifdef HAVE_TIME_H
22 #include <time.h>
23 #endif
24 #ifdef HAVE_NETDB_H
25 #include <netdb.h>
26 #endif
27 
28 /** return an error */
29 #define RET_ERR(e, off) ((int)((e)|((off)<<GLDNS_WIREPARSE_SHIFT)))
30 /** Move parse error but keep its ID */
31 #define RET_ERR_SHIFT(e, move) RET_ERR(GLDNS_WIREPARSE_ERROR(e), GLDNS_WIREPARSE_OFFSET(e)+(move));
32 #define GLDNS_IP6ADDRLEN      (128/8)
33 
34 /*
35  * No special care is taken, all dots are translated into
36  * label separators.
37  * @param rel: true if the domain is not absolute (not terminated in .).
38  * 	The output is then still terminated with a '0' rootlabel.
39  */
gldns_str2wire_dname_buf_rel(const char * str,uint8_t * buf,size_t * olen,int * rel)40 static int gldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf,
41 	size_t* olen, int* rel)
42 {
43 	size_t len;
44 
45 	const char *s;
46 	uint8_t *q, *pq, label_len;
47 
48 	if(rel) *rel = 0;
49 	len = strlen((char*)str);
50 	/* octet representation can make strings a lot longer than actual length */
51 	if (len > GLDNS_MAX_DOMAINLEN * 4) {
52 		return RET_ERR(GLDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0);
53 	}
54 	if (0 == len) {
55 		return RET_ERR(GLDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0);
56 	}
57 
58 	/* root label */
59 	if (1 == len && *str == '.') {
60 		if(*olen < 1)
61 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
62 		buf[0] = 0;
63 		*olen = 1;
64 		return GLDNS_WIREPARSE_ERR_OK;
65 	}
66 
67 	/* get on with the rest */
68 
69 	/* s is on the current character in the string
70          * pq points to where the labellength is going to go
71          * label_len keeps track of the current label's length
72 	 * q builds the dname inside the buf array
73 	 */
74 	len = 0;
75 	if(*olen < 1)
76 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
77 	q = buf+1;
78 	pq = buf;
79 	label_len = 0;
80 	for (s = str; *s; s++, q++) {
81 		if (q >= buf + *olen)
82 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
83 		if (q > buf + GLDNS_MAX_DOMAINLEN)
84 			return RET_ERR(GLDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
85 		switch (*s) {
86 		case '.':
87 			if (label_len > GLDNS_MAX_LABELLEN) {
88 				return RET_ERR(GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
89 			}
90 			if (label_len == 0) {
91 				return RET_ERR(GLDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
92 			}
93 			len += label_len + 1;
94 			*q = 0;
95 			*pq = label_len;
96 			label_len = 0;
97 			pq = q;
98 			break;
99 		case '\\':
100 			/* octet value or literal char */
101 			s += 1;
102 			if (!gldns_parse_escape(q, &s)) {
103 				*q = 0;
104 				return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf);
105 			}
106 			s -= 1;
107 			label_len++;
108 			break;
109 		default:
110 			*q = (uint8_t)*s;
111 			label_len++;
112 		}
113 	}
114 
115 	/* add root label if last char was not '.' */
116 	if(label_len != 0) {
117 		if(rel) *rel = 1;
118 		if (q >= buf + *olen)
119 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
120 		if (q > buf + GLDNS_MAX_DOMAINLEN) {
121 			return RET_ERR(GLDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
122 		}
123                 if (label_len > GLDNS_MAX_LABELLEN) {
124                         return RET_ERR(GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
125                 }
126                 if (label_len == 0) { /* label_len 0 but not . at end? */
127                         return RET_ERR(GLDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
128                 }
129 		len += label_len + 1;
130 		*pq = label_len;
131 		*q = 0;
132 	}
133 	len++;
134 	*olen = len;
135 
136 	return GLDNS_WIREPARSE_ERR_OK;
137 }
138 
gldns_str2wire_dname_buf(const char * str,uint8_t * buf,size_t * len)139 int gldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len)
140 {
141 	return gldns_str2wire_dname_buf_rel(str, buf, len, NULL);
142 }
143 
gldns_str2wire_dname_buf_origin(const char * str,uint8_t * buf,size_t * len,uint8_t * origin,size_t origin_len)144 int gldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
145 	uint8_t* origin, size_t origin_len)
146 {
147 	size_t dlen = *len;
148 	int rel = 0;
149 	int s = gldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel);
150 	if(s) return s;
151 
152 	if(rel && origin && dlen > 0) {
153 		if(dlen + origin_len - 1 > GLDNS_MAX_DOMAINLEN)
154 			return RET_ERR(GLDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
155 				GLDNS_MAX_DOMAINLEN);
156 		if(dlen + origin_len - 1 > *len)
157 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
158 				*len);
159 		memmove(buf+dlen-1, origin, origin_len);
160 		*len = dlen + origin_len - 1;
161 	} else
162 		*len = dlen;
163 	return GLDNS_WIREPARSE_ERR_OK;
164 }
165 
gldns_str2wire_dname(const char * str,size_t * len)166 uint8_t* gldns_str2wire_dname(const char* str, size_t* len)
167 {
168 	uint8_t dname[GLDNS_MAX_DOMAINLEN+1];
169 	*len = sizeof(dname);
170 	if(gldns_str2wire_dname_buf(str, dname, len) == 0) {
171 		uint8_t* r = (uint8_t*)malloc(*len);
172 		if(r) return memcpy(r, dname, *len);
173 	}
174 	*len = 0;
175 	return NULL;
176 }
177 
178 /** read owner name */
179 static int
rrinternal_get_owner(gldns_buffer * strbuf,uint8_t * rr,size_t * len,size_t * dname_len,uint8_t * origin,size_t origin_len,uint8_t * prev,size_t prev_len,char * token,size_t token_len)180 rrinternal_get_owner(gldns_buffer* strbuf, uint8_t* rr, size_t* len,
181 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
182 	size_t prev_len, char* token, size_t token_len)
183 {
184 	/* split the rr in its parts -1 signals trouble */
185 	if(gldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
186 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX,
187 			gldns_buffer_position(strbuf));
188 	}
189 
190 	if(strcmp(token, "@") == 0) {
191 		uint8_t* tocopy;
192 		if (origin) {
193 			*dname_len = origin_len;
194 			tocopy = origin;
195 		} else if (prev) {
196 			*dname_len = prev_len;
197 			tocopy = prev;
198 		} else {
199 			/* default to root */
200 			*dname_len = 1;
201 			tocopy = (uint8_t*)"\0";
202 		}
203 		if(*len < *dname_len)
204 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
205 				gldns_buffer_position(strbuf));
206 		memmove(rr, tocopy, *dname_len);
207 	} else if(*token == '\0') {
208 		/* no ownername was given, try prev, if that fails
209 		 * origin, else default to root */
210 		uint8_t* tocopy;
211 		if(prev) {
212 			*dname_len = prev_len;
213 			tocopy = prev;
214 		} else if(origin) {
215 			*dname_len = origin_len;
216 			tocopy = origin;
217 		} else {
218 			*dname_len = 1;
219 			tocopy = (uint8_t*)"\0";
220 		}
221 		if(*len < *dname_len)
222 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
223 				gldns_buffer_position(strbuf));
224 		memmove(rr, tocopy, *dname_len);
225 	} else {
226 		size_t dlen = *len;
227 		int s = gldns_str2wire_dname_buf_origin(token, rr, &dlen,
228 			origin, origin_len);
229 		if(s) return RET_ERR_SHIFT(s,
230 			gldns_buffer_position(strbuf)-strlen(token));
231 		*dname_len = dlen;
232 	}
233 	return GLDNS_WIREPARSE_ERR_OK;
234 }
235 
236 /** read ttl */
237 static int
rrinternal_get_ttl(gldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint32_t * ttl,uint32_t default_ttl)238 rrinternal_get_ttl(gldns_buffer* strbuf, char* token, size_t token_len,
239 	int* not_there, uint32_t* ttl, uint32_t default_ttl)
240 {
241 	const char* endptr;
242 	if(gldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
243 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TTL,
244 			gldns_buffer_position(strbuf));
245 	}
246 	*ttl = (uint32_t) gldns_str2period(token, &endptr);
247 
248 	if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) {
249 		*not_there = 1;
250 		/* ah, it's not there or something */
251 		if (default_ttl == 0) {
252 			*ttl = GLDNS_DEFAULT_TTL;
253 		} else {
254 			*ttl = default_ttl;
255 		}
256 	}
257 	return GLDNS_WIREPARSE_ERR_OK;
258 }
259 
260 /** read class */
261 static int
rrinternal_get_class(gldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint16_t * cl)262 rrinternal_get_class(gldns_buffer* strbuf, char* token, size_t token_len,
263 	int* not_there, uint16_t* cl)
264 {
265 	/* if 'not_there' then we got token from previous parse routine */
266 	if(!*not_there) {
267 		/* parse new token for class */
268 		if(gldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
269 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_CLASS,
270 				gldns_buffer_position(strbuf));
271 		}
272 	} else *not_there = 0;
273 	*cl = gldns_get_rr_class_by_name(token);
274 	/* class can be left out too, assume IN, current token must be type */
275 	if(*cl == 0 && strcmp(token, "CLASS0") != 0) {
276 		*not_there = 1;
277 		*cl = GLDNS_RR_CLASS_IN;
278 	}
279 	return GLDNS_WIREPARSE_ERR_OK;
280 }
281 
282 /** read type */
283 static int
rrinternal_get_type(gldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint16_t * tp)284 rrinternal_get_type(gldns_buffer* strbuf, char* token, size_t token_len,
285 	int* not_there, uint16_t* tp)
286 {
287 	/* if 'not_there' then we got token from previous parse routine */
288 	if(!*not_there) {
289 		/* parse new token for type */
290 		if(gldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
291 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TYPE,
292 				gldns_buffer_position(strbuf));
293 		}
294 	}
295 	*tp = gldns_get_rr_type_by_name(token);
296 	if(*tp == 0 && strcmp(token, "TYPE0") != 0) {
297 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TYPE,
298 			gldns_buffer_position(strbuf));
299 	}
300 	return GLDNS_WIREPARSE_ERR_OK;
301 }
302 
303 /** put type, class, ttl into rr buffer */
304 static int
rrinternal_write_typeclassttl(gldns_buffer * strbuf,uint8_t * rr,size_t len,size_t dname_len,uint16_t tp,uint16_t cl,uint32_t ttl,int question)305 rrinternal_write_typeclassttl(gldns_buffer* strbuf, uint8_t* rr, size_t len,
306 	size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question)
307 {
308 	if(question) {
309 		/* question is : name, type, class */
310 		if(dname_len + 4 > len)
311 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
312 				gldns_buffer_position(strbuf));
313 		gldns_write_uint16(rr+dname_len, tp);
314 		gldns_write_uint16(rr+dname_len+2, cl);
315 		return GLDNS_WIREPARSE_ERR_OK;
316 	}
317 
318 	/* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */
319 	if(dname_len + 10 > len)
320 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
321 			gldns_buffer_position(strbuf));
322 	gldns_write_uint16(rr+dname_len, tp);
323 	gldns_write_uint16(rr+dname_len+2, cl);
324 	gldns_write_uint32(rr+dname_len+4, ttl);
325 	gldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */
326 	return GLDNS_WIREPARSE_ERR_OK;
327 }
328 
329 /** find delimiters for type */
330 static const char*
rrinternal_get_delims(gldns_rdf_type rdftype,size_t r_cnt,size_t r_max)331 rrinternal_get_delims(gldns_rdf_type rdftype, size_t r_cnt, size_t r_max)
332 {
333 	switch(rdftype) {
334 	case GLDNS_RDF_TYPE_B64        :
335 	case GLDNS_RDF_TYPE_HEX        : /* These rdf types may con- */
336 	case GLDNS_RDF_TYPE_LOC        : /* tain whitespace, only if */
337 	case GLDNS_RDF_TYPE_WKS        : /* it is the last rd field. */
338 	case GLDNS_RDF_TYPE_IPSECKEY   :
339 	case GLDNS_RDF_TYPE_NSEC       :	if (r_cnt == r_max - 1) {
340 						return "\n";
341 					}
342 					break;
343 	default                       :	break;
344 	}
345 	return "\n\t ";
346 }
347 
348 /* Syntactic sugar for gldns_rr_new_frm_str_internal */
349 static int
gldns_rdf_type_maybe_quoted(gldns_rdf_type rdf_type)350 gldns_rdf_type_maybe_quoted(gldns_rdf_type rdf_type)
351 {
352 	return  rdf_type == GLDNS_RDF_TYPE_STR ||
353 		rdf_type == GLDNS_RDF_TYPE_LONG_STR;
354 }
355 
356 /** see if rdata is quoted */
357 static int
rrinternal_get_quoted(gldns_buffer * strbuf,const char ** delimiters,gldns_rdf_type rdftype)358 rrinternal_get_quoted(gldns_buffer* strbuf, const char** delimiters,
359 	gldns_rdf_type rdftype)
360 {
361 	if(gldns_rdf_type_maybe_quoted(rdftype) &&
362 		gldns_buffer_remaining(strbuf) > 0) {
363 
364 		/* skip spaces */
365 		while(gldns_buffer_remaining(strbuf) > 0 &&
366 			*(gldns_buffer_current(strbuf)) == ' ') {
367 			gldns_buffer_skip(strbuf, 1);
368 		}
369 
370 		if(gldns_buffer_remaining(strbuf) > 0 &&
371 			*(gldns_buffer_current(strbuf)) == '\"') {
372 			*delimiters = "\"\0";
373 			gldns_buffer_skip(strbuf, 1);
374 			return 1;
375 		}
376 	}
377 	return 0;
378 }
379 
380 /** spool hex data into rdata */
381 static int
rrinternal_spool_hex(char * token,uint8_t * rr,size_t rr_len,size_t rr_cur_len,size_t * cur_hex_data_size,size_t hex_data_size)382 rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len,
383 	size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size)
384 {
385 	char* p = token;
386 	while(*p) {
387 		if(isspace((unsigned char)*p)) {
388 			p++;
389 			continue;
390 		}
391 		if(!isxdigit((unsigned char)*p))
392 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_RDATA,
393 				p-token);
394 		if(*cur_hex_data_size >= hex_data_size)
395 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_RDATA,
396 				p-token);
397 		/* extra robust check */
398 		if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len)
399 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
400 				p-token);
401 		/* see if 16s or 1s */
402 		if( ((*cur_hex_data_size)&1) == 0) {
403 			rr[rr_cur_len+(*cur_hex_data_size)/2] =
404 				(uint8_t)gldns_hexdigit_to_int(*p)*16;
405 		} else {
406 			rr[rr_cur_len+(*cur_hex_data_size)/2] +=
407 				(uint8_t)gldns_hexdigit_to_int(*p);
408 		}
409 		p++;
410 		(*cur_hex_data_size)++;
411 	}
412 	return GLDNS_WIREPARSE_ERR_OK;
413 }
414 
415 /** read unknown rr type format */
416 static int
rrinternal_parse_unknown(gldns_buffer * strbuf,char * token,size_t token_len,uint8_t * rr,size_t * rr_len,size_t * rr_cur_len,size_t pre_data_pos)417 rrinternal_parse_unknown(gldns_buffer* strbuf, char* token, size_t token_len,
418         uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos)
419 {
420 	const char* delim = "\n\t ";
421 	size_t hex_data_size, cur_hex_data_size;
422 	/* go back to before \#
423 	 * and skip it while setting delimiters better
424 	 */
425 	gldns_buffer_set_position(strbuf, pre_data_pos);
426 	if(gldns_bget_token(strbuf, token, delim, token_len) == -1)
427 		return GLDNS_WIREPARSE_ERR_GENERAL; /* should not fail */
428 	/* read rdata octet length */
429 	if(gldns_bget_token(strbuf, token, delim, token_len) == -1) {
430 		/* something goes very wrong here */
431 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_RDATA,
432 			gldns_buffer_position(strbuf));
433 	}
434 	hex_data_size = (size_t)atoi(token);
435 	if(hex_data_size > GLDNS_MAX_RDFLEN ||
436 		*rr_cur_len + hex_data_size > *rr_len) {
437 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
438 			gldns_buffer_position(strbuf));
439 	}
440 	/* copy hex chars into hex str (2 chars per byte) */
441 	hex_data_size *= 2;
442 	cur_hex_data_size = 0;
443 	while(cur_hex_data_size < hex_data_size) {
444 		int status;
445 		ssize_t c = gldns_bget_token(strbuf, token, delim, token_len);
446 		if((status = rrinternal_spool_hex(token, rr, *rr_len,
447 			*rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0)
448 			return RET_ERR_SHIFT(status,
449 				gldns_buffer_position(strbuf)-strlen(token));
450 		if(c == -1) {
451 			if(cur_hex_data_size != hex_data_size)
452 				return RET_ERR(
453 					GLDNS_WIREPARSE_ERR_SYNTAX_RDATA,
454 					gldns_buffer_position(strbuf));
455 			break;
456 		}
457 	}
458 	*rr_cur_len += hex_data_size/2;
459 	return GLDNS_WIREPARSE_ERR_OK;
460 }
461 
462 /** parse normal RR rdata element */
463 static int
rrinternal_parse_rdf(gldns_buffer * strbuf,char * token,size_t token_len,uint8_t * rr,size_t rr_len,size_t * rr_cur_len,gldns_rdf_type rdftype,uint16_t rr_type,size_t r_cnt,size_t r_max,size_t dname_len,uint8_t * origin,size_t origin_len)464 rrinternal_parse_rdf(gldns_buffer* strbuf, char* token, size_t token_len,
465 	uint8_t* rr, size_t rr_len, size_t* rr_cur_len, gldns_rdf_type rdftype,
466 	uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len,
467 	uint8_t* origin, size_t origin_len)
468 {
469 	size_t len;
470 	int status;
471 
472 	switch(rdftype) {
473 	case GLDNS_RDF_TYPE_DNAME:
474 		/* check if the origin should be used or concatenated */
475 		if(strcmp(token, "@") == 0) {
476 			uint8_t* tocopy;
477 			size_t copylen;
478 			if(origin) {
479 				copylen = origin_len;
480 				tocopy = origin;
481 			} else if(rr_type == GLDNS_RR_TYPE_SOA) {
482 				copylen = dname_len;
483 				tocopy = rr; /* copy rr owner name */
484 			} else {
485 				copylen = 1;
486 				tocopy = (uint8_t*)"\0";
487 			}
488 			if((*rr_cur_len) + copylen > rr_len)
489 				return RET_ERR(
490 					GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
491 					gldns_buffer_position(strbuf));
492 			memmove(rr+*rr_cur_len, tocopy, copylen);
493 			(*rr_cur_len) += copylen;
494 		} else {
495 			size_t dlen = rr_len - (*rr_cur_len);
496 			int s = gldns_str2wire_dname_buf_origin(token,
497 				rr+*rr_cur_len, &dlen, origin, origin_len);
498 			if(s) return RET_ERR_SHIFT(s,
499 				gldns_buffer_position(strbuf)-strlen(token));
500 			(*rr_cur_len) += dlen;
501 		}
502 		return GLDNS_WIREPARSE_ERR_OK;
503 
504 	case GLDNS_RDF_TYPE_HEX:
505 	case GLDNS_RDF_TYPE_B64:
506 		/* When this is the last rdata field, then the
507 		 * rest should be read in (cause then these
508 		 * rdf types may contain spaces). */
509 		if(r_cnt == r_max - 1) {
510 			size_t tlen = strlen(token);
511 			(void)gldns_bget_token(strbuf, token+tlen, "\n",
512 				token_len - tlen);
513 		}
514 		break;
515 	default:
516 		break;
517 	}
518 
519 	len = rr_len - (*rr_cur_len);
520 	if((status=gldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len,
521 		rdftype)) != 0)
522 		return RET_ERR_SHIFT(status,
523 			gldns_buffer_position(strbuf)-strlen(token));
524 	*rr_cur_len += len;
525 	return GLDNS_WIREPARSE_ERR_OK;
526 }
527 
528 /**
529  * Parse one rdf token.  Takes care of quotes and parenthesis.
530  */
531 static int
gldns_parse_rdf_token(gldns_buffer * strbuf,char * token,size_t token_len,int * quoted,int * parens,size_t * pre_data_pos,const char * delimiters,gldns_rdf_type rdftype,size_t * token_strlen)532 gldns_parse_rdf_token(gldns_buffer* strbuf, char* token, size_t token_len,
533 	int* quoted, int* parens, size_t* pre_data_pos,
534 	const char* delimiters, gldns_rdf_type rdftype, size_t* token_strlen)
535 {
536 	size_t slen;
537 
538 	/* skip spaces */
539 	while(gldns_buffer_remaining(strbuf) > 0 && !*quoted &&
540 		*(gldns_buffer_current(strbuf)) == ' ') {
541 		gldns_buffer_skip(strbuf, 1);
542 	}
543 
544 	*pre_data_pos = gldns_buffer_position(strbuf);
545 	if(gldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters,
546 		token_len, parens, (*quoted)?NULL:" \t") == -1) {
547 		return 0;
548 	}
549 	slen = strlen(token);
550 	/* check if not quoted yet, and we have encountered quotes */
551 	if(!*quoted && gldns_rdf_type_maybe_quoted(rdftype) &&
552 		slen >= 2 &&
553 		(token[0] == '"' || token[0] == '\'') &&
554 		(token[slen-1] == '"' || token[slen-1] == '\'')) {
555 		/* move token two smaller (quotes) with endnull */
556 		memmove(token, token+1, slen-2);
557 		token[slen-2] = 0;
558 		slen -= 2;
559 		*quoted = 1;
560 	} else if(!*quoted && gldns_rdf_type_maybe_quoted(rdftype) &&
561 		slen >= 2 &&
562 		(token[0] == '"' || token[0] == '\'')) {
563 		/* got the start quote (remove it) but read remainder
564 		 * of quoted string as well into remainder of token */
565 		memmove(token, token+1, slen-1);
566 		token[slen-1] = 0;
567 		slen -= 1;
568 		*quoted = 1;
569 		/* rewind buffer over skipped whitespace */
570 		while(gldns_buffer_position(strbuf) > 0 &&
571 			(gldns_buffer_current(strbuf)[-1] == ' ' ||
572 			gldns_buffer_current(strbuf)[-1] == '\t')) {
573 			gldns_buffer_skip(strbuf, -1);
574 		}
575 		if(gldns_bget_token_par(strbuf, token+slen,
576 			"\"", token_len-slen,
577 			parens, NULL) == -1) {
578 			return 0;
579 		}
580 		slen = strlen(token);
581 	}
582 	*token_strlen = slen;
583 	return 1;
584 }
585 
586 /** Add space and one more rdf token onto the existing token string. */
587 static int
gldns_affix_token(gldns_buffer * strbuf,char * token,size_t * token_len,int * quoted,int * parens,size_t * pre_data_pos,const char * delimiters,gldns_rdf_type rdftype,size_t * token_strlen)588 gldns_affix_token(gldns_buffer* strbuf, char* token, size_t* token_len,
589 	int* quoted, int* parens, size_t* pre_data_pos,
590 	const char* delimiters, gldns_rdf_type rdftype, size_t* token_strlen)
591 {
592 	size_t addlen = *token_len - *token_strlen;
593 	size_t addstrlen = 0;
594 
595 	/* add space */
596 	if(addlen < 1) return 0;
597 	token[*token_strlen] = ' ';
598 	token[++(*token_strlen)] = 0;
599 
600 	/* read another token */
601 	addlen = *token_len - *token_strlen;
602 	if(!gldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted,
603 		parens, pre_data_pos, delimiters, rdftype, &addstrlen))
604 		return 0;
605 	(*token_strlen) += addstrlen;
606 	return 1;
607 }
608 
609 /** parse rdata from string into rr buffer(-remainder after dname). */
610 static int
rrinternal_parse_rdata(gldns_buffer * strbuf,char * token,size_t token_len,uint8_t * rr,size_t * rr_len,size_t dname_len,uint16_t rr_type,uint8_t * origin,size_t origin_len)611 rrinternal_parse_rdata(gldns_buffer* strbuf, char* token, size_t token_len,
612 	uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type,
613 	uint8_t* origin, size_t origin_len)
614 {
615 	const gldns_rr_descriptor *desc = gldns_rr_descript((uint16_t)rr_type);
616 	size_t r_cnt, r_min, r_max;
617 	size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen;
618 	int was_unknown_rr_format = 0, parens = 0, status, quoted;
619 	const char* delimiters;
620 	gldns_rdf_type rdftype;
621 	/* a desc is always returned */
622 	if(!desc) return GLDNS_WIREPARSE_ERR_GENERAL;
623 	r_max = gldns_rr_descriptor_maximum(desc);
624 	r_min = gldns_rr_descriptor_minimum(desc);
625 	/* robust check */
626 	if(rr_cur_len > *rr_len)
627 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
628 			gldns_buffer_position(strbuf));
629 
630 	/* because number of fields can be variable, we can't rely on
631 	 * _maximum() only */
632 	for(r_cnt=0; r_cnt < r_max; r_cnt++) {
633 		rdftype = gldns_rr_descriptor_field_type(desc, r_cnt);
634 		delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max);
635 		quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype);
636 
637 		if(!gldns_parse_rdf_token(strbuf, token, token_len, &quoted,
638 			&parens, &pre_data_pos, delimiters, rdftype,
639 			&token_strlen))
640 			break;
641 
642 		/* rfc3597 specifies that any type can be represented
643 		 * with \# method, which can contain spaces...
644 		 * it does specify size though... */
645 
646 		/* unknown RR data */
647 		if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 &&
648 			!quoted && (token_strlen == 2 || token[2]==' ')) {
649 			was_unknown_rr_format = 1;
650 			if((status=rrinternal_parse_unknown(strbuf, token,
651 				token_len, rr, rr_len, &rr_cur_len,
652 				pre_data_pos)) != 0)
653 				return status;
654 		} else if(token_strlen > 0 || quoted) {
655 			if(rdftype == GLDNS_RDF_TYPE_HIP) {
656 				/* affix the HIT and PK fields, with a space */
657 				if(!gldns_affix_token(strbuf, token,
658 					&token_len, &quoted, &parens,
659 					&pre_data_pos, delimiters,
660 					rdftype, &token_strlen))
661 					break;
662 				if(!gldns_affix_token(strbuf, token,
663 					&token_len, &quoted, &parens,
664 					&pre_data_pos, delimiters,
665 					rdftype, &token_strlen))
666 					break;
667 			} else if(rdftype == GLDNS_RDF_TYPE_INT16_DATA &&
668 				strcmp(token, "0")!=0) {
669 				/* affix len and b64 fields */
670 				if(!gldns_affix_token(strbuf, token,
671 					&token_len, &quoted, &parens,
672 					&pre_data_pos, delimiters,
673 					rdftype, &token_strlen))
674 					break;
675 			}
676 
677 			/* normal RR */
678 			if((status=rrinternal_parse_rdf(strbuf, token,
679 				token_len, rr, *rr_len, &rr_cur_len, rdftype,
680 				rr_type, r_cnt, r_max, dname_len, origin,
681 				origin_len)) != 0) {
682 				return status;
683 			}
684 		}
685 	}
686 	if(!was_unknown_rr_format && r_cnt+1 < r_min) {
687 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE,
688 			gldns_buffer_position(strbuf));
689 	}
690 	while(parens != 0) {
691 		/* read remainder, must be "" */
692 		if(gldns_bget_token_par(strbuf, token, "\n", token_len,
693 			&parens, " \t") == -1) {
694 			if(parens != 0)
695 				return RET_ERR(GLDNS_WIREPARSE_ERR_PARENTHESIS,
696 					gldns_buffer_position(strbuf));
697 			break;
698 		}
699 		if(strcmp(token, "") != 0)
700 			return RET_ERR(GLDNS_WIREPARSE_ERR_PARENTHESIS,
701 				gldns_buffer_position(strbuf));
702 	}
703 	/* write rdata length */
704 	gldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10));
705 	*rr_len = rr_cur_len;
706 	return GLDNS_WIREPARSE_ERR_OK;
707 }
708 
709 /*
710  * trailing spaces are allowed
711  * leading spaces are not allowed
712  * allow ttl to be optional
713  * class is optional too
714  * if ttl is missing, and default_ttl is 0, use DEF_TTL
715  * allow ttl to be written as 1d3h
716  * So the RR should look like. e.g.
717  * miek.nl. 3600 IN MX 10 elektron.atoom.net
718  * or
719  * miek.nl. 1h IN MX 10 elektron.atoom.net
720  * or
721  * miek.nl. IN MX 10 elektron.atoom.net
722  */
723 static int
gldns_str2wire_rr_buf_internal(const char * str,uint8_t * rr,size_t * len,size_t * dname_len,uint32_t default_ttl,uint8_t * origin,size_t origin_len,uint8_t * prev,size_t prev_len,int question)724 gldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len,
725 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
726 	size_t origin_len, uint8_t* prev, size_t prev_len, int question)
727 {
728 	int status;
729 	int not_there = 0;
730 	char token[GLDNS_MAX_RDFLEN+1];
731 	uint32_t ttl = 0;
732 	uint16_t tp = 0, cl = 0;
733 	size_t ddlen = 0;
734 
735 	/* string in buffer */
736 	gldns_buffer strbuf;
737 	gldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
738 	if(!dname_len) dname_len = &ddlen;
739 
740 	/* parse the owner */
741 	if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin,
742 		origin_len, prev, prev_len, token, sizeof(token))) != 0)
743 		return status;
744 
745 	/* parse the [ttl] [class] <type> */
746 	if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token),
747 		&not_there, &ttl, default_ttl)) != 0)
748 		return status;
749 	if((status=rrinternal_get_class(&strbuf, token, sizeof(token),
750 		&not_there, &cl)) != 0)
751 		return status;
752 	if((status=rrinternal_get_type(&strbuf, token, sizeof(token),
753 		&not_there, &tp)) != 0)
754 		return status;
755 	/* put ttl, class, type into the rr result */
756 	if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl,
757 		ttl, question)) != 0)
758 		return status;
759 	/* for a question-RR we are done, no rdata */
760 	if(question) {
761 		*len = *dname_len + 4;
762 		return GLDNS_WIREPARSE_ERR_OK;
763 	}
764 
765 	/* rdata */
766 	if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token),
767 		rr, len, *dname_len, tp, origin, origin_len)) != 0)
768 		return status;
769 
770 	return GLDNS_WIREPARSE_ERR_OK;
771 }
772 
gldns_str2wire_rr_buf(const char * str,uint8_t * rr,size_t * len,size_t * dname_len,uint32_t default_ttl,uint8_t * origin,size_t origin_len,uint8_t * prev,size_t prev_len)773 int gldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
774 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
775 	size_t origin_len, uint8_t* prev, size_t prev_len)
776 {
777 	return gldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
778 		default_ttl, origin, origin_len, prev, prev_len, 0);
779 }
780 
gldns_str2wire_rr_question_buf(const char * str,uint8_t * rr,size_t * len,size_t * dname_len,uint8_t * origin,size_t origin_len,uint8_t * prev,size_t prev_len)781 int gldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
782 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
783 	size_t prev_len)
784 {
785 	return gldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
786 		0, origin, origin_len, prev, prev_len, 1);
787 }
788 
gldns_wirerr_get_type(uint8_t * rr,size_t len,size_t dname_len)789 uint16_t gldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len)
790 {
791 	if(len < dname_len+2)
792 		return 0;
793 	return gldns_read_uint16(rr+dname_len);
794 }
795 
gldns_wirerr_get_class(uint8_t * rr,size_t len,size_t dname_len)796 uint16_t gldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len)
797 {
798 	if(len < dname_len+4)
799 		return 0;
800 	return gldns_read_uint16(rr+dname_len+2);
801 }
802 
gldns_wirerr_get_ttl(uint8_t * rr,size_t len,size_t dname_len)803 uint32_t gldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len)
804 {
805 	if(len < dname_len+8)
806 		return 0;
807 	return gldns_read_uint32(rr+dname_len+4);
808 }
809 
gldns_wirerr_get_rdatalen(uint8_t * rr,size_t len,size_t dname_len)810 uint16_t gldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len)
811 {
812 	if(len < dname_len+10)
813 		return 0;
814 	return gldns_read_uint16(rr+dname_len+8);
815 }
816 
gldns_wirerr_get_rdata(uint8_t * rr,size_t len,size_t dname_len)817 uint8_t* gldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len)
818 {
819 	if(len < dname_len+10)
820 		return NULL;
821 	return rr+dname_len+10;
822 }
823 
gldns_wirerr_get_rdatawl(uint8_t * rr,size_t len,size_t dname_len)824 uint8_t* gldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len)
825 {
826 	if(len < dname_len+10)
827 		return NULL;
828 	return rr+dname_len+8;
829 }
830 
gldns_get_errorstr_parse(int e)831 const char* gldns_get_errorstr_parse(int e)
832 {
833 	gldns_lookup_table *lt;
834 	lt = gldns_lookup_by_id(gldns_wireparse_errors, GLDNS_WIREPARSE_ERROR(e));
835 	return lt?lt->name:"unknown error";
836 }
837 
838 /* Strip whitespace from the start and the end of <line>.  */
839 char *
gldns_strip_ws(char * line)840 gldns_strip_ws(char *line)
841 {
842         char *s = line, *e;
843 
844         for (s = line; *s && isspace((unsigned char)*s); s++)
845                 ;
846         for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
847                 ;
848         *e = 0;
849         return s;
850 }
851 
gldns_fp2wire_rr_buf(FILE * in,uint8_t * rr,size_t * len,size_t * dname_len,struct gldns_file_parse_state * parse_state)852 int gldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
853 	struct gldns_file_parse_state* parse_state)
854 {
855 	char line[GLDNS_RR_BUF_SIZE+1];
856 	ssize_t size;
857 
858 	/* read an entire line in from the file */
859 	if((size = gldns_fget_token_l(in, line, GLDNS_PARSE_SKIP_SPACE,
860 		GLDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL))
861 		== -1) {
862 		/* if last line was empty, we are now at feof, which is not
863 		 * always a parse error (happens when for instance last line
864 		 * was a comment)
865 		 */
866 		return GLDNS_WIREPARSE_ERR_SYNTAX;
867 	}
868 
869 	/* we can have the situation, where we've read ok, but still got
870 	 * no bytes to play with, in this case size is 0 */
871 	if(size == 0) {
872 		if(*len > 0)
873 			rr[0] = 0;
874 		*len = 0;
875 		*dname_len = 0;
876 		return GLDNS_WIREPARSE_ERR_OK;
877 	}
878 
879 	if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
880 		int s;
881 		strlcpy((char*)rr, line, *len);
882 		*len = 0;
883 		*dname_len = 0;
884 		if(!parse_state) return GLDNS_WIREPARSE_ERR_OK;
885 		parse_state->origin_len = sizeof(parse_state->origin);
886 		s = gldns_str2wire_dname_buf(gldns_strip_ws(line+8),
887 			parse_state->origin, &parse_state->origin_len);
888 		if(s) parse_state->origin_len = 0;
889 		return s;
890 	} else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
891 		const char* end = NULL;
892 		strlcpy((char*)rr, line, *len);
893 		*len = 0;
894 		*dname_len = 0;
895 		if(!parse_state) return GLDNS_WIREPARSE_ERR_OK;
896 		parse_state->default_ttl = gldns_str2period(
897 			gldns_strip_ws(line+5), &end);
898 	} else if (strncmp(line, "$INCLUDE", 8) == 0) {
899 		strlcpy((char*)rr, line, *len);
900 		*len = 0;
901 		*dname_len = 0;
902 		return GLDNS_WIREPARSE_ERR_INCLUDE;
903 	} else if (strncmp(line, "$", 1) == 0) {
904 		strlcpy((char*)rr, line, *len);
905 		*len = 0;
906 		*dname_len = 0;
907 		return GLDNS_WIREPARSE_ERR_INCLUDE;
908 	} else {
909 		int r = gldns_str2wire_rr_buf(line, rr, len, dname_len,
910 			parse_state?parse_state->default_ttl:0,
911 			(parse_state&&parse_state->origin_len)?
912 				parse_state->origin:NULL,
913 			parse_state?parse_state->origin_len:0,
914 			(parse_state&&parse_state->prev_rr_len)?
915 				parse_state->prev_rr:NULL,
916 			parse_state?parse_state->prev_rr_len:0);
917 		if(r == GLDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 &&
918 			parse_state &&
919 			(*dname_len) <= sizeof(parse_state->prev_rr)) {
920 			memmove(parse_state->prev_rr, rr, *dname_len);
921 			parse_state->prev_rr_len = (*dname_len);
922 		}
923 		return r;
924 	}
925 	return GLDNS_WIREPARSE_ERR_OK;
926 }
927 
gldns_str2wire_rdf_buf(const char * str,uint8_t * rd,size_t * len,gldns_rdf_type rdftype)928 int gldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
929 	gldns_rdf_type rdftype)
930 {
931 	switch (rdftype) {
932 	case GLDNS_RDF_TYPE_DNAME:
933 		return gldns_str2wire_dname_buf(str, rd, len);
934 	case GLDNS_RDF_TYPE_INT8:
935 		return gldns_str2wire_int8_buf(str, rd, len);
936 	case GLDNS_RDF_TYPE_INT16:
937 		return gldns_str2wire_int16_buf(str, rd, len);
938 	case GLDNS_RDF_TYPE_INT32:
939 		return gldns_str2wire_int32_buf(str, rd, len);
940 	case GLDNS_RDF_TYPE_A:
941 		return gldns_str2wire_a_buf(str, rd, len);
942 	case GLDNS_RDF_TYPE_AAAA:
943 		return gldns_str2wire_aaaa_buf(str, rd, len);
944 	case GLDNS_RDF_TYPE_STR:
945 		return gldns_str2wire_str_buf(str, rd, len);
946 	case GLDNS_RDF_TYPE_APL:
947 		return gldns_str2wire_apl_buf(str, rd, len);
948 	case GLDNS_RDF_TYPE_B64:
949 		return gldns_str2wire_b64_buf(str, rd, len);
950 	case GLDNS_RDF_TYPE_B32_EXT:
951 		return gldns_str2wire_b32_ext_buf(str, rd, len);
952 	case GLDNS_RDF_TYPE_HEX:
953 		return gldns_str2wire_hex_buf(str, rd, len);
954 	case GLDNS_RDF_TYPE_NSEC:
955 		return gldns_str2wire_nsec_buf(str, rd, len);
956 	case GLDNS_RDF_TYPE_TYPE:
957 		return gldns_str2wire_type_buf(str, rd, len);
958 	case GLDNS_RDF_TYPE_CLASS:
959 		return gldns_str2wire_class_buf(str, rd, len);
960 	case GLDNS_RDF_TYPE_CERT_ALG:
961 		return gldns_str2wire_cert_alg_buf(str, rd, len);
962 	case GLDNS_RDF_TYPE_ALG:
963 		return gldns_str2wire_alg_buf(str, rd, len);
964 	case GLDNS_RDF_TYPE_TIME:
965 		return gldns_str2wire_time_buf(str, rd, len);
966 	case GLDNS_RDF_TYPE_PERIOD:
967 		return gldns_str2wire_period_buf(str, rd, len);
968 	case GLDNS_RDF_TYPE_TSIGTIME:
969 		return gldns_str2wire_tsigtime_buf(str, rd, len);
970 	case GLDNS_RDF_TYPE_LOC:
971 		return gldns_str2wire_loc_buf(str, rd, len);
972 	case GLDNS_RDF_TYPE_WKS:
973 		return gldns_str2wire_wks_buf(str, rd, len);
974 	case GLDNS_RDF_TYPE_NSAP:
975 		return gldns_str2wire_nsap_buf(str, rd, len);
976 	case GLDNS_RDF_TYPE_ATMA:
977 		return gldns_str2wire_atma_buf(str, rd, len);
978 	case GLDNS_RDF_TYPE_IPSECKEY:
979 		return gldns_str2wire_ipseckey_buf(str, rd, len);
980 	case GLDNS_RDF_TYPE_NSEC3_SALT:
981 		return gldns_str2wire_nsec3_salt_buf(str, rd, len);
982 	case GLDNS_RDF_TYPE_NSEC3_NEXT_OWNER:
983 		return gldns_str2wire_b32_ext_buf(str, rd, len);
984 	case GLDNS_RDF_TYPE_ILNP64:
985 		return gldns_str2wire_ilnp64_buf(str, rd, len);
986 	case GLDNS_RDF_TYPE_EUI48:
987 		return gldns_str2wire_eui48_buf(str, rd, len);
988 	case GLDNS_RDF_TYPE_EUI64:
989 		return gldns_str2wire_eui64_buf(str, rd, len);
990 	case GLDNS_RDF_TYPE_TAG:
991 		return gldns_str2wire_tag_buf(str, rd, len);
992 	case GLDNS_RDF_TYPE_LONG_STR:
993 		return gldns_str2wire_long_str_buf(str, rd, len);
994 	case GLDNS_RDF_TYPE_TSIGERROR:
995 		return gldns_str2wire_tsigerror_buf(str, rd, len);
996 	case GLDNS_RDF_TYPE_HIP:
997 		return gldns_str2wire_hip_buf(str, rd, len);
998 	case GLDNS_RDF_TYPE_INT16_DATA:
999 		return gldns_str2wire_int16_data_buf(str, rd, len);
1000 	case GLDNS_RDF_TYPE_AMTRELAY:
1001 		return gldns_str2wire_amtrelay_buf(str, rd, len);
1002 	case GLDNS_RDF_TYPE_UNKNOWN:
1003 	case GLDNS_RDF_TYPE_SERVICE:
1004 		return GLDNS_WIREPARSE_ERR_NOT_IMPL;
1005 	case GLDNS_RDF_TYPE_NONE:
1006 	default:
1007 		break;
1008 	}
1009 	return GLDNS_WIREPARSE_ERR_GENERAL;
1010 }
1011 
gldns_str2wire_int8_buf(const char * str,uint8_t * rd,size_t * len)1012 int gldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len)
1013 {
1014 	char* end;
1015 	uint8_t r = (uint8_t)strtol((char*)str, &end, 10);
1016 	if(*end != 0)
1017 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1018 	if(*len < 1)
1019 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1020 	rd[0] = r;
1021 	*len = 1;
1022 	return GLDNS_WIREPARSE_ERR_OK;
1023 }
1024 
gldns_str2wire_int16_buf(const char * str,uint8_t * rd,size_t * len)1025 int gldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len)
1026 {
1027 	char* end;
1028 	uint16_t r = (uint16_t)strtol((char*)str, &end, 10);
1029 	if(*end != 0)
1030 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1031 	if(*len < 2)
1032 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1033 	gldns_write_uint16(rd, r);
1034 	*len = 2;
1035 	return GLDNS_WIREPARSE_ERR_OK;
1036 }
1037 
gldns_str2wire_int32_buf(const char * str,uint8_t * rd,size_t * len)1038 int gldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len)
1039 {
1040 	char* end;
1041 	uint32_t r;
1042 	errno = 0; /* must set to zero before call,
1043 			note race condition on errno */
1044 	if(*str == '-')
1045 		r = (uint32_t)strtol((char*)str, &end, 10);
1046 	else	r = (uint32_t)strtoul((char*)str, &end, 10);
1047 	if(*end != 0)
1048 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1049 	if(errno == ERANGE)
1050 		return GLDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1051 	if(*len < 4)
1052 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1053 	gldns_write_uint32(rd, r);
1054 	*len = 4;
1055 	return GLDNS_WIREPARSE_ERR_OK;
1056 }
1057 
gldns_str2wire_a_buf(const char * str,uint8_t * rd,size_t * len)1058 int gldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len)
1059 {
1060 	struct in_addr address;
1061 	if(inet_pton(AF_INET, (char*)str, &address) != 1)
1062 		return GLDNS_WIREPARSE_ERR_SYNTAX_IP4;
1063 	if(*len < sizeof(address))
1064 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1065 	memmove(rd, &address, sizeof(address));
1066 	*len = sizeof(address);
1067 	return GLDNS_WIREPARSE_ERR_OK;
1068 }
1069 
gldns_str2wire_aaaa_buf(const char * str,uint8_t * rd,size_t * len)1070 int gldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len)
1071 {
1072 #ifdef AF_INET6
1073 	uint8_t address[GLDNS_IP6ADDRLEN + 1];
1074 	if(inet_pton(AF_INET6, (char*)str, address) != 1)
1075 		return GLDNS_WIREPARSE_ERR_SYNTAX_IP6;
1076 	if(*len < GLDNS_IP6ADDRLEN)
1077 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1078 	memmove(rd, address, GLDNS_IP6ADDRLEN);
1079 	*len = GLDNS_IP6ADDRLEN;
1080 	return GLDNS_WIREPARSE_ERR_OK;
1081 #else
1082 	return GLDNS_WIREPARSE_ERR_NOT_IMPL;
1083 #endif
1084 }
1085 
gldns_str2wire_str_buf(const char * str,uint8_t * rd,size_t * len)1086 int gldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len)
1087 {
1088 	uint8_t ch = 0;
1089 	size_t sl = 0;
1090 	const char* s = str;
1091 	/* skip length byte */
1092 	if(*len < 1)
1093 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1094 
1095 	/* read characters */
1096 	while(gldns_parse_char(&ch, &s)) {
1097 		if(sl >= 255)
1098 			return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR, s-str);
1099 		if(*len < sl+1)
1100 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1101 				s-str);
1102 		rd[++sl] = ch;
1103 	}
1104 	if(!s)
1105 		return GLDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
1106 	rd[0] = (uint8_t)sl;
1107 	*len = sl+1;
1108 	return GLDNS_WIREPARSE_ERR_OK;
1109 }
1110 
gldns_str2wire_apl_buf(const char * str,uint8_t * rd,size_t * len)1111 int gldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len)
1112 {
1113 	const char *my_str = str;
1114 
1115 	char my_ip_str[64];
1116 	size_t ip_str_len;
1117 
1118 	uint16_t family;
1119 	int negation;
1120 	size_t adflength = 0;
1121 	uint8_t data[16+4];
1122 	uint8_t prefix;
1123 	size_t i;
1124 
1125 	if(*my_str == '\0') {
1126 		/* empty APL element, no data, no string */
1127 		*len = 0;
1128 		return GLDNS_WIREPARSE_ERR_OK;
1129 	}
1130 
1131 	/* [!]afi:address/prefix */
1132 	if (strlen(my_str) < 2
1133 			|| strchr(my_str, ':') == NULL
1134 			|| strchr(my_str, '/') == NULL
1135 			|| strchr(my_str, ':') > strchr(my_str, '/')) {
1136 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1137 	}
1138 
1139 	if (my_str[0] == '!') {
1140 		negation = 1;
1141 		my_str += 1;
1142 	} else {
1143 		negation = 0;
1144 	}
1145 
1146 	family = (uint16_t) atoi(my_str);
1147 
1148 	my_str = strchr(my_str, ':') + 1;
1149 
1150 	/* need ip addr and only ip addr for inet_pton */
1151 	ip_str_len = (size_t) (strchr(my_str, '/') - my_str);
1152 	if(ip_str_len+1 > sizeof(my_ip_str))
1153 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1154 	(void)strlcpy(my_ip_str, my_str, sizeof(my_ip_str));
1155 	my_ip_str[ip_str_len] = 0;
1156 
1157 	if (family == 1) {
1158 		/* ipv4 */
1159 		if(inet_pton(AF_INET, my_ip_str, data+4) == 0)
1160 			return GLDNS_WIREPARSE_ERR_INVALID_STR;
1161 		for (i = 0; i < 4; i++) {
1162 			if (data[i+4] != 0) {
1163 				adflength = i + 1;
1164 			}
1165 		}
1166 	} else if (family == 2) {
1167 		/* ipv6 */
1168 		if (inet_pton(AF_INET6, my_ip_str, data+4) == 0)
1169 			return GLDNS_WIREPARSE_ERR_INVALID_STR;
1170 		for (i = 0; i < 16; i++) {
1171 			if (data[i+4] != 0) {
1172 				adflength = i + 1;
1173 			}
1174 		}
1175 	} else {
1176 		/* unknown family */
1177 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1178 	}
1179 
1180 	my_str = strchr(my_str, '/') + 1;
1181 	prefix = (uint8_t) atoi(my_str);
1182 
1183 	gldns_write_uint16(data, family);
1184 	data[2] = prefix;
1185 	data[3] = (uint8_t)adflength;
1186 	if (negation) {
1187 		/* set bit 1 of byte 3 */
1188 		data[3] = data[3] | 0x80;
1189 	}
1190 
1191 	if(*len < 4+adflength)
1192 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1193 	memmove(rd, data, 4+adflength);
1194 	*len = 4+adflength;
1195 	return GLDNS_WIREPARSE_ERR_OK;
1196 }
1197 
gldns_str2wire_b64_buf(const char * str,uint8_t * rd,size_t * len)1198 int gldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len)
1199 {
1200 	size_t sz = gldns_b64_pton_calculate_size(strlen(str));
1201 	int n;
1202 	if(strcmp(str, "0") == 0) {
1203 		*len = 0;
1204 		return GLDNS_WIREPARSE_ERR_OK;
1205 	}
1206 	if(*len < sz)
1207 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1208 	n = gldns_b64_pton(str, rd, *len);
1209 	if(n < 0)
1210 		return GLDNS_WIREPARSE_ERR_SYNTAX_B64;
1211 	*len = (size_t)n;
1212 	return GLDNS_WIREPARSE_ERR_OK;
1213 }
1214 
gldns_str2wire_b32_ext_buf(const char * str,uint8_t * rd,size_t * len)1215 int gldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len)
1216 {
1217 	size_t slen = strlen(str);
1218 	size_t sz = gldns_b32_pton_calculate_size(slen);
1219 	int n;
1220 	if(*len < 1+sz)
1221 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1222 	rd[0] = (uint8_t)sz;
1223 	n = gldns_b32_pton_extended_hex(str, slen, rd+1, *len-1);
1224 	if(n < 0)
1225 		return GLDNS_WIREPARSE_ERR_SYNTAX_B32_EXT;
1226 	*len = (size_t)n+1;
1227 	return GLDNS_WIREPARSE_ERR_OK;
1228 }
1229 
1230 /** see if the string ends, or ends in whitespace */
1231 static int
gldns_is_last_of_string(const char * str)1232 gldns_is_last_of_string(const char* str)
1233 {
1234 	if(*str == 0) return 1;
1235 	while(isspace((unsigned char)*str))
1236 		str++;
1237 	if(*str == 0) return 1;
1238 	return 0;
1239 }
1240 
gldns_str2wire_hex_buf(const char * str,uint8_t * rd,size_t * len)1241 int gldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len)
1242 {
1243 	const char* s = str;
1244 	size_t dlen = 0; /* number of hexdigits parsed */
1245 	while(*s) {
1246 		if(isspace((unsigned char)*s)) {
1247 			s++;
1248 			continue;
1249 		}
1250 		if(dlen == 0 && *s == '0' && gldns_is_last_of_string(s+1)) {
1251 			*len = 0;
1252 			return GLDNS_WIREPARSE_ERR_OK;
1253 		}
1254 		if(!isxdigit((unsigned char)*s))
1255 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1256 		if(*len < dlen/2 + 1)
1257 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1258 				s-str);
1259 		if((dlen&1)==0)
1260 			rd[dlen/2] = (uint8_t)gldns_hexdigit_to_int(*s++) * 16;
1261 		else	rd[dlen/2] += (uint8_t)gldns_hexdigit_to_int(*s++);
1262 		dlen++;
1263 	}
1264 	if((dlen&1)!=0)
1265 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1266 	*len = dlen/2;
1267 	return GLDNS_WIREPARSE_ERR_OK;
1268 }
1269 
gldns_str2wire_nsec_buf(const char * str,uint8_t * rd,size_t * len)1270 int gldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len)
1271 {
1272 	const char *delim = "\n\t ";
1273 	char token[64]; /* for a type name */
1274 	size_t type_count = 0;
1275 	int block;
1276 	size_t used = 0;
1277 	uint16_t maxtype = 0;
1278 	uint8_t typebits[8192]; /* 65536 bits */
1279 	uint8_t window_in_use[256];
1280 
1281 	/* string in buffer */
1282 	gldns_buffer strbuf;
1283 	gldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
1284 
1285 	/* parse the types */
1286 	memset(typebits, 0, sizeof(typebits));
1287 	memset(window_in_use, 0, sizeof(window_in_use));
1288 	while(gldns_buffer_remaining(&strbuf) > 0 &&
1289 		gldns_bget_token(&strbuf, token, delim, sizeof(token)) != -1) {
1290 		uint16_t t = gldns_get_rr_type_by_name(token);
1291 		if(token[0] == 0)
1292 			continue;
1293 		if(t == 0 && strcmp(token, "TYPE0") != 0)
1294 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TYPE,
1295 				gldns_buffer_position(&strbuf));
1296 		typebits[t/8] |= (0x80>>(t%8));
1297 		window_in_use[t/256] = 1;
1298 		type_count++;
1299 		if(t > maxtype) maxtype = t;
1300 	}
1301 
1302 	/* empty NSEC bitmap */
1303 	if(type_count == 0) {
1304 		*len = 0;
1305 		return GLDNS_WIREPARSE_ERR_OK;
1306 	}
1307 
1308 	/* encode windows {u8 windowblock, u8 bitmaplength, 0-32u8 bitmap},
1309 	 * block is 0-255 upper octet of types, length if 0-32. */
1310 	for(block = 0; block <= (int)maxtype/256; block++) {
1311 		int i, blocklen = 0;
1312 		if(!window_in_use[block])
1313 			continue;
1314 		for(i=0; i<32; i++) {
1315 			if(typebits[block*32+i] != 0)
1316 				blocklen = i+1;
1317 		}
1318 		if(blocklen == 0)
1319 			continue; /* empty window should have been !in_use */
1320 		if(used+blocklen+2 > *len)
1321 			return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1322 		rd[used+0] = (uint8_t)block;
1323 		rd[used+1] = (uint8_t)blocklen;
1324 		for(i=0; i<blocklen; i++) {
1325 			rd[used+2+i] = typebits[block*32+i];
1326 		}
1327 		used += blocklen+2;
1328 	}
1329 	*len = used;
1330 	return GLDNS_WIREPARSE_ERR_OK;
1331 }
1332 
gldns_str2wire_type_buf(const char * str,uint8_t * rd,size_t * len)1333 int gldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len)
1334 {
1335 	uint16_t t = gldns_get_rr_type_by_name(str);
1336 	if(t == 0 && strcmp(str, "TYPE0") != 0)
1337 		return GLDNS_WIREPARSE_ERR_SYNTAX_TYPE;
1338 	if(*len < 2)
1339 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1340 	gldns_write_uint16(rd, t);
1341 	*len = 2;
1342 	return GLDNS_WIREPARSE_ERR_OK;
1343 }
1344 
gldns_str2wire_class_buf(const char * str,uint8_t * rd,size_t * len)1345 int gldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len)
1346 {
1347 	uint16_t c = gldns_get_rr_class_by_name(str);
1348 	if(c == 0 && strcmp(str, "CLASS0") != 0)
1349 		return GLDNS_WIREPARSE_ERR_SYNTAX_CLASS;
1350 	if(*len < 2)
1351 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1352 	gldns_write_uint16(rd, c);
1353 	*len = 2;
1354 	return GLDNS_WIREPARSE_ERR_OK;
1355 }
1356 
1357 /* An certificate alg field can either be specified as a 8 bits number
1358  * or by its symbolic name. Handle both */
gldns_str2wire_cert_alg_buf(const char * str,uint8_t * rd,size_t * len)1359 int gldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len)
1360 {
1361 	gldns_lookup_table *lt = gldns_lookup_by_name(gldns_cert_algorithms,
1362 		str);
1363 	if(*len < 2)
1364 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1365 	if(lt) {
1366 		gldns_write_uint16(rd, (uint16_t)lt->id);
1367 	} else {
1368 		int s = gldns_str2wire_int16_buf(str, rd, len);
1369 		if(s) return s;
1370 		if(gldns_read_uint16(rd) == 0)
1371 			return GLDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM;
1372 	}
1373 	*len = 2;
1374 	return GLDNS_WIREPARSE_ERR_OK;
1375 }
1376 
1377 /* An alg field can either be specified as a 8 bits number
1378  * or by its symbolic name. Handle both */
gldns_str2wire_alg_buf(const char * str,uint8_t * rd,size_t * len)1379 int gldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len)
1380 {
1381 	gldns_lookup_table *lt = gldns_lookup_by_name(gldns_algorithms, str);
1382 	if(*len < 1)
1383 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1384 	if(lt) {
1385 		rd[0] = (uint8_t)lt->id;
1386 		*len = 1;
1387 	} else {
1388 		/* try as-is (a number) */
1389 		return gldns_str2wire_int8_buf(str, rd, len);
1390 	}
1391 	return GLDNS_WIREPARSE_ERR_OK;
1392 }
1393 
gldns_str2wire_tsigerror_buf(const char * str,uint8_t * rd,size_t * len)1394 int gldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len)
1395 {
1396 	gldns_lookup_table *lt = gldns_lookup_by_name(gldns_tsig_errors, str);
1397 	if(*len < 2)
1398 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1399 	if(lt) {
1400 		gldns_write_uint16(rd, (uint16_t)lt->id);
1401 		*len = 2;
1402 	} else {
1403 		/* try as-is (a number) */
1404 		return gldns_str2wire_int16_buf(str, rd, len);
1405 	}
1406 	return GLDNS_WIREPARSE_ERR_OK;
1407 }
1408 
gldns_str2wire_time_buf(const char * str,uint8_t * rd,size_t * len)1409 int gldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len)
1410 {
1411 	/* convert a time YYYYDDMMHHMMSS to wireformat */
1412 	struct tm tm;
1413 	if(*len < 4)
1414 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1415 
1416 	/* Try to scan the time... */
1417 	memset(&tm, 0, sizeof(tm));
1418 	if (strlen(str) == 14 && sscanf(str, "%4d%2d%2d%2d%2d%2d",
1419 		&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
1420 		&tm.tm_min, &tm.tm_sec) == 6) {
1421 	   	tm.tm_year -= 1900;
1422 	   	tm.tm_mon--;
1423 	   	/* Check values */
1424 		if (tm.tm_year < 70)
1425 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1426 		if (tm.tm_mon < 0 || tm.tm_mon > 11)
1427 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1428 		if (tm.tm_mday < 1 || tm.tm_mday > 31)
1429 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1430 		if (tm.tm_hour < 0 || tm.tm_hour > 23)
1431 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1432 		if (tm.tm_min < 0 || tm.tm_min > 59)
1433 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1434 		if (tm.tm_sec < 0 || tm.tm_sec > 59)
1435 			return GLDNS_WIREPARSE_ERR_SYNTAX_TIME;
1436 
1437 		gldns_write_uint32(rd, (uint32_t)gldns_mktime_from_utc(&tm));
1438 	} else {
1439 		/* handle it as 32 bits timestamp */
1440 		char *end;
1441 		uint32_t l = (uint32_t)strtol((char*)str, &end, 10);
1442 		if(*end != 0)
1443 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TIME,
1444 				end-(char*)str);
1445 		gldns_write_uint32(rd, l);
1446 	}
1447 	*len = 4;
1448 	return GLDNS_WIREPARSE_ERR_OK;
1449 }
1450 
gldns_str2wire_tsigtime_buf(const char * str,uint8_t * rd,size_t * len)1451 int gldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len)
1452 {
1453 	char* end;
1454 	uint64_t t = (uint64_t)strtol((char*)str, &end, 10);
1455 	uint16_t high;
1456 	uint32_t low;
1457 	if(*end != 0)
1458 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TIME, end-str);
1459 	if(*len < 6)
1460 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1461 	high = (uint16_t)(t>>32);
1462 	low = (uint32_t)(t);
1463 	gldns_write_uint16(rd, high);
1464 	gldns_write_uint32(rd+2, low);
1465 	*len = 6;
1466 	return GLDNS_WIREPARSE_ERR_OK;
1467 }
1468 
gldns_str2wire_period_buf(const char * str,uint8_t * rd,size_t * len)1469 int gldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len)
1470 {
1471 	const char* end;
1472 	uint32_t p = gldns_str2period(str, &end);
1473 	if(*end != 0)
1474 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_PERIOD, end-str);
1475 	if(*len < 4)
1476 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1477 	gldns_write_uint32(rd, p);
1478 	*len = 4;
1479 	return GLDNS_WIREPARSE_ERR_OK;
1480 }
1481 
1482 /** read "<digits>[.<digits>][mM]" into mantissa exponent format for LOC type */
1483 static int
loc_parse_cm(char * my_str,char ** endstr,uint8_t * m,uint8_t * e)1484 loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e)
1485 {
1486 	uint32_t meters = 0, cm = 0, val;
1487 	while (isblank((unsigned char)*my_str)) {
1488 		my_str++;
1489 	}
1490 	meters = (uint32_t)strtol(my_str, &my_str, 10);
1491 	if (*my_str == '.') {
1492 		my_str++;
1493 		cm = (uint32_t)strtol(my_str, &my_str, 10);
1494 	}
1495 	if (meters >= 1) {
1496 		*e = 2;
1497 		val = meters;
1498 	} else	{
1499 		*e = 0;
1500 		val = cm;
1501 	}
1502 	while(val >= 10) {
1503 		(*e)++;
1504 		val /= 10;
1505 	}
1506 	*m = (uint8_t)val;
1507 
1508 	if (*e > 9)
1509 		return 0;
1510 	if (*my_str == 'm' || *my_str == 'M') {
1511 		my_str++;
1512 	}
1513 	*endstr = my_str;
1514 	return 1;
1515 }
1516 
gldns_str2wire_loc_buf(const char * str,uint8_t * rd,size_t * len)1517 int gldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len)
1518 {
1519 	uint32_t latitude = 0;
1520 	uint32_t longitude = 0;
1521 	uint32_t altitude = 0;
1522 
1523 	uint32_t equator = (uint32_t)1<<31; /* 2**31 */
1524 
1525 	/* only support version 0 */
1526 	uint32_t h = 0;
1527 	uint32_t m = 0;
1528 	uint8_t size_b = 1, size_e = 2;
1529 	uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
1530 	uint8_t vert_pre_b = 1, vert_pre_e = 3;
1531 
1532 	double s = 0.0;
1533 	int northerness;
1534 	int easterness;
1535 
1536 	char *my_str = (char *) str;
1537 
1538 	if (isdigit((unsigned char) *my_str)) {
1539 		h = (uint32_t) strtol(my_str, &my_str, 10);
1540 	} else {
1541 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1542 	}
1543 
1544 	while (isblank((unsigned char) *my_str)) {
1545 		my_str++;
1546 	}
1547 
1548 	if (isdigit((unsigned char) *my_str)) {
1549 		m = (uint32_t) strtol(my_str, &my_str, 10);
1550 	} else if (*my_str == 'N' || *my_str == 'S') {
1551 		goto north;
1552 	} else {
1553 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1554 	}
1555 
1556 	while (isblank((unsigned char) *my_str)) {
1557 		my_str++;
1558 	}
1559 
1560 	if (isdigit((unsigned char) *my_str)) {
1561 		s = strtod(my_str, &my_str);
1562 	}
1563 
1564 	/* skip blanks before northerness */
1565 	while (isblank((unsigned char) *my_str)) {
1566 		my_str++;
1567 	}
1568 
1569 north:
1570 	if (*my_str == 'N') {
1571 		northerness = 1;
1572 	} else if (*my_str == 'S') {
1573 		northerness = 0;
1574 	} else {
1575 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1576 	}
1577 
1578 	my_str++;
1579 
1580 	/* store number */
1581 	s = 1000.0 * s;
1582 	/* add a little to make floor in conversion a round */
1583 	s += 0.0005;
1584 	latitude = (uint32_t) s;
1585 	latitude += 1000 * 60 * m;
1586 	latitude += 1000 * 60 * 60 * h;
1587 	if (northerness) {
1588 		latitude = equator + latitude;
1589 	} else {
1590 		latitude = equator - latitude;
1591 	}
1592 	while (isblank((unsigned char)*my_str)) {
1593 		my_str++;
1594 	}
1595 
1596 	if (isdigit((unsigned char) *my_str)) {
1597 		h = (uint32_t) strtol(my_str, &my_str, 10);
1598 	} else {
1599 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1600 	}
1601 
1602 	while (isblank((unsigned char) *my_str)) {
1603 		my_str++;
1604 	}
1605 
1606 	if (isdigit((unsigned char) *my_str)) {
1607 		m = (uint32_t) strtol(my_str, &my_str, 10);
1608 	} else if (*my_str == 'E' || *my_str == 'W') {
1609 		goto east;
1610 	} else {
1611 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1612 	}
1613 
1614 	while (isblank((unsigned char)*my_str)) {
1615 		my_str++;
1616 	}
1617 
1618 	if (isdigit((unsigned char) *my_str)) {
1619 		s = strtod(my_str, &my_str);
1620 	}
1621 
1622 	/* skip blanks before easterness */
1623 	while (isblank((unsigned char)*my_str)) {
1624 		my_str++;
1625 	}
1626 
1627 east:
1628 	if (*my_str == 'E') {
1629 		easterness = 1;
1630 	} else if (*my_str == 'W') {
1631 		easterness = 0;
1632 	} else {
1633 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1634 	}
1635 
1636 	my_str++;
1637 
1638 	/* store number */
1639 	s *= 1000.0;
1640 	/* add a little to make floor in conversion a round */
1641 	s += 0.0005;
1642 	longitude = (uint32_t) s;
1643 	longitude += 1000 * 60 * m;
1644 	longitude += 1000 * 60 * 60 * h;
1645 
1646 	if (easterness) {
1647 		longitude += equator;
1648 	} else {
1649 		longitude = equator - longitude;
1650 	}
1651 
1652 	altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
1653 		10000000.0 + 0.5);
1654 	if (*my_str == 'm' || *my_str == 'M') {
1655 		my_str++;
1656 	}
1657 
1658 	if (strlen(my_str) > 0) {
1659 		if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
1660 			return GLDNS_WIREPARSE_ERR_INVALID_STR;
1661 	}
1662 
1663 	if (strlen(my_str) > 0) {
1664 		if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
1665 			return GLDNS_WIREPARSE_ERR_INVALID_STR;
1666 	}
1667 
1668 	if (strlen(my_str) > 0) {
1669 		if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
1670 			return GLDNS_WIREPARSE_ERR_INVALID_STR;
1671 	}
1672 
1673 	if(*len < 16)
1674 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1675 	rd[0] = 0;
1676 	rd[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
1677 	rd[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
1678 	rd[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
1679 	gldns_write_uint32(rd + 4, latitude);
1680 	gldns_write_uint32(rd + 8, longitude);
1681 	gldns_write_uint32(rd + 12, altitude);
1682 	*len = 16;
1683 	return GLDNS_WIREPARSE_ERR_OK;
1684 }
1685 
1686 static void
ldns_tolower_str(char * s)1687 ldns_tolower_str(char* s)
1688 {
1689 	if(s) {
1690 		while(*s) {
1691 			*s = (char)tolower((unsigned char)*s);
1692 			s++;
1693 		}
1694 	}
1695 }
1696 
gldns_str2wire_wks_buf(const char * str,uint8_t * rd,size_t * len)1697 int gldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len)
1698 {
1699 	int rd_len = 1;
1700 	int have_proto = 0;
1701 	char token[50], proto_str[50];
1702 	gldns_buffer strbuf;
1703 	gldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
1704 	proto_str[0]=0;
1705 
1706 	/* check we have one byte for proto */
1707 	if(*len < 1)
1708 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1709 
1710 	while(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) > 0) {
1711 		ldns_tolower_str(token);
1712 		if(!have_proto) {
1713 			struct protoent *p = getprotobyname(token);
1714 			have_proto = 1;
1715 			if(p) rd[0] = (uint8_t)p->p_proto;
1716 			else if(strcasecmp(token, "tcp")==0) rd[0]=6;
1717 			else if(strcasecmp(token, "udp")==0) rd[0]=17;
1718 			else rd[0] = (uint8_t)atoi(token);
1719 			(void)strlcpy(proto_str, token, sizeof(proto_str));
1720 		} else {
1721 			int serv_port;
1722 			struct servent *serv = getservbyname(token, proto_str);
1723 			if(serv) serv_port=(int)ntohs((uint16_t)serv->s_port);
1724 			else if(strcasecmp(token, "domain")==0) serv_port=53;
1725 			else {
1726 				serv_port = atoi(token);
1727 				if(serv_port == 0 && strcmp(token, "0") != 0) {
1728 #ifdef HAVE_ENDSERVENT
1729 					endservent();
1730 #endif
1731 #ifdef HAVE_ENDPROTOENT
1732 					endprotoent();
1733 #endif
1734 					return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX,
1735 						gldns_buffer_position(&strbuf));
1736 				}
1737 				if(serv_port < 0 || serv_port > 65535) {
1738 #ifdef HAVE_ENDSERVENT
1739 					endservent();
1740 #endif
1741 #ifdef HAVE_ENDPROTOENT
1742 					endprotoent();
1743 #endif
1744 					return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX,
1745 						gldns_buffer_position(&strbuf));
1746 				}
1747 			}
1748 			if(rd_len < 1+serv_port/8+1) {
1749 				/* bitmap is larger, init new bytes at 0 */
1750 				if(*len < 1+(size_t)serv_port/8+1) {
1751 #ifdef HAVE_ENDSERVENT
1752 					endservent();
1753 #endif
1754 #ifdef HAVE_ENDPROTOENT
1755 					endprotoent();
1756 #endif
1757 					return RET_ERR(
1758 					GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1759 					gldns_buffer_position(&strbuf));
1760 				}
1761 				memset(rd+rd_len, 0, 1+(size_t)serv_port/8+1-rd_len);
1762 				rd_len = 1+serv_port/8+1;
1763 			}
1764 			rd[1+ serv_port/8] |= (1 << (7 - serv_port % 8));
1765 		}
1766 	}
1767 	*len = (size_t)rd_len;
1768 
1769 #ifdef HAVE_ENDSERVENT
1770 	endservent();
1771 #endif
1772 #ifdef HAVE_ENDPROTOENT
1773 	endprotoent();
1774 #endif
1775 	return GLDNS_WIREPARSE_ERR_OK;
1776 }
1777 
gldns_str2wire_nsap_buf(const char * str,uint8_t * rd,size_t * len)1778 int gldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len)
1779 {
1780 	const char* s = str;
1781 	size_t slen;
1782 	size_t dlen = 0; /* number of hexdigits parsed */
1783 
1784 	/* just a hex string with optional dots? */
1785 	if (s[0] != '0' || s[1] != 'x')
1786 		return GLDNS_WIREPARSE_ERR_INVALID_STR;
1787 	s += 2;
1788 	slen = strlen(s);
1789 	if(slen > GLDNS_MAX_RDFLEN*2)
1790 		return GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
1791 	while(*s) {
1792 		if(isspace((unsigned char)*s) || *s == '.') {
1793 			s++;
1794 			continue;
1795 		}
1796 		if(!isxdigit((unsigned char)*s))
1797 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1798 		if(*len < dlen/2 + 1)
1799 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1800 				s-str);
1801 		if((dlen&1)==0)
1802 			rd[dlen/2] = (uint8_t)gldns_hexdigit_to_int(*s++) * 16;
1803 		else	rd[dlen/2] += gldns_hexdigit_to_int(*s++);
1804 		dlen++;
1805 	}
1806 	if((dlen&1)!=0)
1807 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1808 	*len = dlen/2;
1809 	return GLDNS_WIREPARSE_ERR_OK;
1810 }
1811 
gldns_str2wire_atma_buf(const char * str,uint8_t * rd,size_t * len)1812 int gldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len)
1813 {
1814 	const char* s = str;
1815 	size_t slen = strlen(str);
1816 	size_t dlen = 0; /* number of hexdigits parsed */
1817 
1818 	/* just a hex string with optional dots? */
1819 	/* notimpl e.164 format */
1820 	if(slen > GLDNS_MAX_RDFLEN*2)
1821 		return GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
1822 	while(*s) {
1823 		if(isspace((unsigned char)*s) || *s == '.') {
1824 			s++;
1825 			continue;
1826 		}
1827 		if(!isxdigit((unsigned char)*s))
1828 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1829 		if(*len < dlen/2 + 1)
1830 			return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1831 				s-str);
1832 		if((dlen&1)==0)
1833 			rd[dlen/2] = (uint8_t)gldns_hexdigit_to_int(*s++) * 16;
1834 		else	rd[dlen/2] += gldns_hexdigit_to_int(*s++);
1835 		dlen++;
1836 	}
1837 	if((dlen&1)!=0)
1838 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1839 	*len = dlen/2;
1840 	return GLDNS_WIREPARSE_ERR_OK;
1841 }
1842 
gldns_str2wire_ipseckey_buf(const char * str,uint8_t * rd,size_t * len)1843 int gldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len)
1844 {
1845 	size_t gwlen = 0, keylen = 0;
1846 	int s;
1847 	uint8_t gwtype;
1848 	char token[512];
1849 	gldns_buffer strbuf;
1850 	gldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
1851 
1852 	if(*len < 3)
1853 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1854 	/* precedence */
1855 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
1856 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1857 			gldns_buffer_position(&strbuf));
1858 	rd[0] = (uint8_t)atoi(token);
1859 	/* gateway_type */
1860 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
1861 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1862 			gldns_buffer_position(&strbuf));
1863 	rd[1] = (uint8_t)atoi(token);
1864 	gwtype = rd[1];
1865 	/* algorithm */
1866 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
1867 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1868 			gldns_buffer_position(&strbuf));
1869 	rd[2] = (uint8_t)atoi(token);
1870 
1871 	/* gateway */
1872 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
1873 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1874 			gldns_buffer_position(&strbuf));
1875 	if(gwtype == 0) {
1876 		/* NOGATEWAY */
1877 		if(strcmp(token, ".") != 0)
1878 			return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1879 				gldns_buffer_position(&strbuf));
1880 		gwlen = 0;
1881 	} else if(gwtype == 1) {
1882 		/* IP4 */
1883 		gwlen = *len - 3;
1884 		s = gldns_str2wire_a_buf(token, rd+3, &gwlen);
1885 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
1886 	} else if(gwtype == 2) {
1887 		/* IP6 */
1888 		gwlen = *len - 3;
1889 		s = gldns_str2wire_aaaa_buf(token, rd+3, &gwlen);
1890 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
1891 	} else if(gwtype == 3) {
1892 		/* DNAME */
1893 		gwlen = *len - 3;
1894 		s = gldns_str2wire_dname_buf(token, rd+3, &gwlen);
1895 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
1896 	} else {
1897 		/* unknown gateway type */
1898 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
1899 			gldns_buffer_position(&strbuf));
1900 	}
1901 	/* double check for size */
1902 	if(*len < 3 + gwlen)
1903 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1904 			gldns_buffer_position(&strbuf));
1905 
1906 	/* publickey in remainder of strbuf */
1907 	keylen = *len - 3 - gwlen;
1908 	s = gldns_str2wire_b64_buf((const char*)gldns_buffer_current(&strbuf),
1909 		rd+3+gwlen, &keylen);
1910 	if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
1911 
1912 	*len = 3 + gwlen + keylen;
1913 	return GLDNS_WIREPARSE_ERR_OK;
1914 }
1915 
gldns_str2wire_nsec3_salt_buf(const char * str,uint8_t * rd,size_t * len)1916 int gldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len)
1917 {
1918 	int i, salt_length_str = (int)strlen(str);
1919 	if (salt_length_str == 1 && str[0] == '-') {
1920 		salt_length_str = 0;
1921 	} else if (salt_length_str % 2 != 0) {
1922 		return GLDNS_WIREPARSE_ERR_SYNTAX_HEX;
1923 	}
1924 	if (salt_length_str > 512)
1925 		return GLDNS_WIREPARSE_ERR_SYNTAX_HEX;
1926 	if(*len < 1+(size_t)salt_length_str / 2)
1927 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1928 	rd[0] = (uint8_t) (salt_length_str / 2);
1929 	for (i = 0; i < salt_length_str; i += 2) {
1930 		if (isxdigit((unsigned char)str[i]) &&
1931 			isxdigit((unsigned char)str[i+1])) {
1932 			rd[1+i/2] = (uint8_t)(gldns_hexdigit_to_int(str[i])*16
1933 				+ gldns_hexdigit_to_int(str[i+1]));
1934 		} else {
1935 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_HEX, i);
1936 		}
1937 	}
1938 	*len = 1 + (size_t)rd[0];
1939 	return GLDNS_WIREPARSE_ERR_OK;
1940 }
1941 
gldns_str2wire_ilnp64_buf(const char * str,uint8_t * rd,size_t * len)1942 int gldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len)
1943 {
1944 	unsigned int a, b, c, d;
1945 	uint16_t shorts[4];
1946 	int l;
1947 	if(*len < sizeof(shorts))
1948 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1949 
1950 	if (sscanf(str, "%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 ||
1951 			l != (int)strlen(str) || /* more data to read */
1952 			strpbrk(str, "+-")       /* signed hexes */
1953 			)
1954 		return GLDNS_WIREPARSE_ERR_SYNTAX_ILNP64;
1955 	shorts[0] = htons(a);
1956 	shorts[1] = htons(b);
1957 	shorts[2] = htons(c);
1958 	shorts[3] = htons(d);
1959 	memmove(rd, &shorts, sizeof(shorts));
1960 	*len = sizeof(shorts);
1961 	return GLDNS_WIREPARSE_ERR_OK;
1962 }
1963 
gldns_str2wire_eui48_buf(const char * str,uint8_t * rd,size_t * len)1964 int gldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len)
1965 {
1966 	unsigned int a, b, c, d, e, f;
1967 	int l;
1968 
1969 	if(*len < 6)
1970 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1971 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x%n",
1972 			&a, &b, &c, &d, &e, &f, &l) != 6 ||
1973 			l != (int)strlen(str))
1974 		return GLDNS_WIREPARSE_ERR_SYNTAX_EUI48;
1975 	rd[0] = a;
1976 	rd[1] = b;
1977 	rd[2] = c;
1978 	rd[3] = d;
1979 	rd[4] = e;
1980 	rd[5] = f;
1981 	*len = 6;
1982 	return GLDNS_WIREPARSE_ERR_OK;
1983 }
1984 
gldns_str2wire_eui64_buf(const char * str,uint8_t * rd,size_t * len)1985 int gldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len)
1986 {
1987 	unsigned int a, b, c, d, e, f, g, h;
1988 	int l;
1989 
1990 	if(*len < 8)
1991 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1992 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n",
1993 			&a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 ||
1994 			l != (int)strlen(str))
1995 		return GLDNS_WIREPARSE_ERR_SYNTAX_EUI64;
1996 	rd[0] = a;
1997 	rd[1] = b;
1998 	rd[2] = c;
1999 	rd[3] = d;
2000 	rd[4] = e;
2001 	rd[5] = f;
2002 	rd[6] = g;
2003 	rd[7] = h;
2004 	*len = 8;
2005 	return GLDNS_WIREPARSE_ERR_OK;
2006 }
2007 
gldns_str2wire_tag_buf(const char * str,uint8_t * rd,size_t * len)2008 int gldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len)
2009 {
2010 	size_t slen = strlen(str);
2011 	const char* ptr;
2012 
2013 	if (slen > 255)
2014 		return GLDNS_WIREPARSE_ERR_SYNTAX_TAG;
2015 	if(*len < slen+1)
2016 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2017 	for (ptr = str; *ptr; ptr++) {
2018 		if(!isalnum((unsigned char)*ptr))
2019 			return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_TAG, ptr-str);
2020 	}
2021 	rd[0] = (uint8_t)slen;
2022 	memmove(rd+1, str, slen);
2023 	*len = slen+1;
2024 	return GLDNS_WIREPARSE_ERR_OK;
2025 }
2026 
gldns_str2wire_long_str_buf(const char * str,uint8_t * rd,size_t * len)2027 int gldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len)
2028 {
2029 	uint8_t ch = 0;
2030 	const char* pstr = str;
2031 	size_t length = 0;
2032 
2033 	/* Fill data with parsed bytes */
2034 	while (gldns_parse_char(&ch, &pstr)) {
2035 		if(*len < length+1)
2036 			return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2037 		rd[length++] = ch;
2038 	}
2039 	if(!pstr)
2040 		return GLDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
2041 	*len = length;
2042 	return GLDNS_WIREPARSE_ERR_OK;
2043 }
2044 
gldns_str2wire_hip_buf(const char * str,uint8_t * rd,size_t * len)2045 int gldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len)
2046 {
2047 	char* s, *end;
2048 	int e;
2049 	size_t hitlen, pklen = 0;
2050 	/* presentation format:
2051 	 * 	pk-algo HIThex pubkeybase64
2052 	 * wireformat:
2053 	 * 	hitlen[1byte] pkalgo[1byte] pubkeylen[2byte] [hit] [pubkey] */
2054 	if(*len < 4)
2055 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2056 
2057 	/* read PK algorithm */
2058 	rd[1] = (uint8_t)strtol((char*)str, &s, 10);
2059 	if(*s != ' ')
2060 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2061 	s++;
2062 	while(*s == ' ')
2063 		s++;
2064 
2065 	/* read HIT hex tag */
2066 	/* zero terminate the tag (replace later) */
2067 	end = strchr(s, ' ');
2068 	if(!end) return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX, s-(char*)str);
2069 	*end = 0;
2070 	hitlen = *len - 4;
2071 	if((e = gldns_str2wire_hex_buf(s, rd+4, &hitlen)) != 0) {
2072 		*end = ' ';
2073 		return RET_ERR_SHIFT(e, s-(char*)str);
2074 	}
2075 	if(hitlen > 255) {
2076 		*end = ' ';
2077 		return RET_ERR(GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+255*2);
2078 	}
2079 	rd[0] = (uint8_t)hitlen;
2080 	*end = ' ';
2081 	s = end+1;
2082 
2083 	/* read pubkey base64 sequence */
2084 	pklen = *len - 4 - hitlen;
2085 	if((e = gldns_str2wire_b64_buf(s, rd+4+hitlen, &pklen)) != 0)
2086 		return RET_ERR_SHIFT(e, s-(char*)str);
2087 	if(pklen > 65535)
2088 		return RET_ERR(GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+65535);
2089 	gldns_write_uint16(rd+2, (uint16_t)pklen);
2090 
2091 	*len = 4 + hitlen + pklen;
2092 	return GLDNS_WIREPARSE_ERR_OK;
2093 }
2094 
gldns_str2wire_int16_data_buf(const char * str,uint8_t * rd,size_t * len)2095 int gldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len)
2096 {
2097 	char* s;
2098 	int n;
2099 	n = strtol(str, &s, 10);
2100 	if(*len < ((size_t)n)+2)
2101 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2102 	if(n > 65535)
2103 		return GLDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2104 
2105 	if(n == 0) {
2106 		gldns_write_uint16(rd, 0);
2107 		*len = 2;
2108 		return GLDNS_WIREPARSE_ERR_OK;
2109 	}
2110 	if(*s != ' ')
2111 		return RET_ERR(GLDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2112 	s++;
2113 	while(*s == ' ')
2114 		s++;
2115 
2116 	n = gldns_b64_pton(s, rd+2, (*len)-2);
2117 	if(n < 0)
2118 		return GLDNS_WIREPARSE_ERR_SYNTAX_B64;
2119 	gldns_write_uint16(rd, (uint16_t)n);
2120 	*len = ((size_t)n)+2;
2121 	return GLDNS_WIREPARSE_ERR_OK;
2122 }
2123 
gldns_str2wire_amtrelay_buf(const char * str,uint8_t * rd,size_t * len)2124 int gldns_str2wire_amtrelay_buf(const char* str, uint8_t* rd, size_t* len)
2125 {
2126 	size_t relay_len = 0;
2127 	int s;
2128 	uint8_t relay_type;
2129 	char token[512];
2130 	gldns_buffer strbuf;
2131 	gldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2132 
2133 	if(*len < 2)
2134 		return GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2135 	/* precedence */
2136 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2137 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2138 			gldns_buffer_position(&strbuf));
2139 	rd[0] = (uint8_t)atoi(token);
2140 	/* discovery_optional */
2141 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2142 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2143 			gldns_buffer_position(&strbuf));
2144 	if ((token[0] != '0' && token[0] != '1') || token[1] != 0)
2145 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2146 			gldns_buffer_position(&strbuf));
2147 
2148 	rd[1] = *token == '1' ? 0x80 : 0x00;
2149 	/* relay_type */
2150 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2151 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2152 			gldns_buffer_position(&strbuf));
2153 	relay_type = (uint8_t)atoi(token);
2154 	if (relay_type > 0x7F)
2155 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2156 			gldns_buffer_position(&strbuf));
2157 	rd[1] |= relay_type;
2158 
2159 	if (relay_type == 0) {
2160 		*len = 2;
2161 		return GLDNS_WIREPARSE_ERR_OK;
2162 	}
2163 	/* relay */
2164 	if(gldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2165 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2166 			gldns_buffer_position(&strbuf));
2167 	if(relay_type == 1) {
2168 		/* IP4 */
2169 		relay_len = *len - 2;
2170 		s = gldns_str2wire_a_buf(token, rd+2, &relay_len);
2171 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
2172 	} else if(relay_type == 2) {
2173 		/* IP6 */
2174 		relay_len = *len - 2;
2175 		s = gldns_str2wire_aaaa_buf(token, rd+2, &relay_len);
2176 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
2177 	} else if(relay_type == 3) {
2178 		/* DNAME */
2179 		relay_len = *len - 2;
2180 		s = gldns_str2wire_dname_buf(token, rd+2, &relay_len);
2181 		if(s) return RET_ERR_SHIFT(s, gldns_buffer_position(&strbuf));
2182 	} else {
2183 		/* unknown gateway type */
2184 		return RET_ERR(GLDNS_WIREPARSE_ERR_INVALID_STR,
2185 			gldns_buffer_position(&strbuf));
2186 	}
2187 	/* double check for size */
2188 	if(*len < 2 + relay_len)
2189 		return RET_ERR(GLDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2190 			gldns_buffer_position(&strbuf));
2191 
2192 	*len = 2 + relay_len;
2193 	return GLDNS_WIREPARSE_ERR_OK;
2194 }
2195 
2196 
2197