1ae8c6e27Sflorian /**
2ae8c6e27Sflorian  * str2wire.c - read txt presentation of RRs
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * (c) NLnet Labs, 2005-2006
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * See the file LICENSE for the license
7ae8c6e27Sflorian  */
8ae8c6e27Sflorian 
9ae8c6e27Sflorian /**
10ae8c6e27Sflorian  * \file
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Parses text to wireformat.
13ae8c6e27Sflorian  */
14ae8c6e27Sflorian #include "config.h"
15ae8c6e27Sflorian #include "sldns/str2wire.h"
16ae8c6e27Sflorian #include "sldns/wire2str.h"
17ae8c6e27Sflorian #include "sldns/sbuffer.h"
18ae8c6e27Sflorian #include "sldns/parse.h"
19ae8c6e27Sflorian #include "sldns/parseutil.h"
20ae8c6e27Sflorian #include <ctype.h>
21ae8c6e27Sflorian #ifdef HAVE_TIME_H
22ae8c6e27Sflorian #include <time.h>
23ae8c6e27Sflorian #endif
24ae8c6e27Sflorian #ifdef HAVE_NETDB_H
25ae8c6e27Sflorian #include <netdb.h>
26ae8c6e27Sflorian #endif
27ae8c6e27Sflorian 
28a1a7ba80Sflorian /** bits for the offset */
29a1a7ba80Sflorian #define RET_OFFSET_MASK (((unsigned)(~LDNS_WIREPARSE_MASK))>>LDNS_WIREPARSE_SHIFT)
30ae8c6e27Sflorian /** return an error */
31a1a7ba80Sflorian #define RET_ERR(e, off) ((int)(((e)&LDNS_WIREPARSE_MASK)|(((off)&RET_OFFSET_MASK)<<LDNS_WIREPARSE_SHIFT)))
32ae8c6e27Sflorian /** Move parse error but keep its ID */
33ae8c6e27Sflorian #define RET_ERR_SHIFT(e, move) RET_ERR(LDNS_WIREPARSE_ERROR(e), LDNS_WIREPARSE_OFFSET(e)+(move));
34ae8c6e27Sflorian 
35ae8c6e27Sflorian /*
36ae8c6e27Sflorian  * No special care is taken, all dots are translated into
37ae8c6e27Sflorian  * label separators.
38ae8c6e27Sflorian  * @param rel: true if the domain is not absolute (not terminated in .).
39ae8c6e27Sflorian  * 	The output is then still terminated with a '0' rootlabel.
40ae8c6e27Sflorian  */
sldns_str2wire_dname_buf_rel(const char * str,uint8_t * buf,size_t * olen,int * rel)41ae8c6e27Sflorian static int sldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf,
42ae8c6e27Sflorian 	size_t* olen, int* rel)
43ae8c6e27Sflorian {
44ae8c6e27Sflorian 	size_t len;
45ae8c6e27Sflorian 
46ae8c6e27Sflorian 	const char *s;
47ae8c6e27Sflorian 	uint8_t *q, *pq, label_len;
48ae8c6e27Sflorian 
49ae8c6e27Sflorian 	if(rel) *rel = 0;
50ae8c6e27Sflorian 	len = strlen((char*)str);
51ae8c6e27Sflorian 	/* octet representation can make strings a lot longer than actual length */
52ae8c6e27Sflorian 	if (len > LDNS_MAX_DOMAINLEN * 4) {
53ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0);
54ae8c6e27Sflorian 	}
55ae8c6e27Sflorian 	if (0 == len) {
56ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0);
57ae8c6e27Sflorian 	}
58ae8c6e27Sflorian 
59ae8c6e27Sflorian 	/* root label */
60ae8c6e27Sflorian 	if (1 == len && *str == '.') {
61ae8c6e27Sflorian 		if(*olen < 1)
62ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
63ae8c6e27Sflorian 		buf[0] = 0;
64ae8c6e27Sflorian 		*olen = 1;
65ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
66ae8c6e27Sflorian 	}
67ae8c6e27Sflorian 
68ae8c6e27Sflorian 	/* get on with the rest */
69ae8c6e27Sflorian 
70ae8c6e27Sflorian 	/* s is on the current character in the string
71ae8c6e27Sflorian          * pq points to where the labellength is going to go
72ae8c6e27Sflorian          * label_len keeps track of the current label's length
73ae8c6e27Sflorian 	 * q builds the dname inside the buf array
74ae8c6e27Sflorian 	 */
75ae8c6e27Sflorian 	len = 0;
76ae8c6e27Sflorian 	if(*olen < 1)
77ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0);
78ae8c6e27Sflorian 	q = buf+1;
79ae8c6e27Sflorian 	pq = buf;
80ae8c6e27Sflorian 	label_len = 0;
81ae8c6e27Sflorian 	for (s = str; *s; s++, q++) {
82ae8c6e27Sflorian 		if (q >= buf + *olen)
83ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
84d32eb43cSflorian 		if (q >= buf + LDNS_MAX_DOMAINLEN)
85ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
86ae8c6e27Sflorian 		switch (*s) {
87ae8c6e27Sflorian 		case '.':
88ae8c6e27Sflorian 			if (label_len > LDNS_MAX_LABELLEN) {
89ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
90ae8c6e27Sflorian 			}
91ae8c6e27Sflorian 			if (label_len == 0) {
92ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
93ae8c6e27Sflorian 			}
94ae8c6e27Sflorian 			len += label_len + 1;
95ae8c6e27Sflorian 			*q = 0;
96ae8c6e27Sflorian 			*pq = label_len;
97ae8c6e27Sflorian 			label_len = 0;
98ae8c6e27Sflorian 			pq = q;
99ae8c6e27Sflorian 			break;
100ae8c6e27Sflorian 		case '\\':
101ae8c6e27Sflorian 			/* octet value or literal char */
102ae8c6e27Sflorian 			s += 1;
103ae8c6e27Sflorian 			if (!sldns_parse_escape(q, &s)) {
104ae8c6e27Sflorian 				*q = 0;
105ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf);
106ae8c6e27Sflorian 			}
107ae8c6e27Sflorian 			s -= 1;
108ae8c6e27Sflorian 			label_len++;
109ae8c6e27Sflorian 			break;
110ae8c6e27Sflorian 		default:
111ae8c6e27Sflorian 			*q = (uint8_t)*s;
112ae8c6e27Sflorian 			label_len++;
113ae8c6e27Sflorian 		}
114ae8c6e27Sflorian 	}
115ae8c6e27Sflorian 
116ae8c6e27Sflorian 	/* add root label if last char was not '.' */
117ae8c6e27Sflorian 	if(label_len != 0) {
118ae8c6e27Sflorian 		if(rel) *rel = 1;
119ae8c6e27Sflorian 		if (q >= buf + *olen)
120ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf);
121d32eb43cSflorian 		if (q >= buf + LDNS_MAX_DOMAINLEN) {
122ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf);
123ae8c6e27Sflorian 		}
124ae8c6e27Sflorian                 if (label_len > LDNS_MAX_LABELLEN) {
125ae8c6e27Sflorian                         return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf);
126ae8c6e27Sflorian                 }
127ae8c6e27Sflorian                 if (label_len == 0) { /* label_len 0 but not . at end? */
128ae8c6e27Sflorian                         return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf);
129ae8c6e27Sflorian                 }
130ae8c6e27Sflorian 		len += label_len + 1;
131ae8c6e27Sflorian 		*pq = label_len;
132ae8c6e27Sflorian 		*q = 0;
133ae8c6e27Sflorian 	}
134ae8c6e27Sflorian 	len++;
135ae8c6e27Sflorian 	*olen = len;
136ae8c6e27Sflorian 
137ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
138ae8c6e27Sflorian }
139ae8c6e27Sflorian 
sldns_str2wire_dname_buf(const char * str,uint8_t * buf,size_t * len)140ae8c6e27Sflorian int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len)
141ae8c6e27Sflorian {
142ae8c6e27Sflorian 	return sldns_str2wire_dname_buf_rel(str, buf, len, NULL);
143ae8c6e27Sflorian }
144ae8c6e27Sflorian 
sldns_str2wire_dname_buf_origin(const char * str,uint8_t * buf,size_t * len,uint8_t * origin,size_t origin_len)145ae8c6e27Sflorian int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len,
146ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
147ae8c6e27Sflorian {
148ae8c6e27Sflorian 	size_t dlen = *len;
149ae8c6e27Sflorian 	int rel = 0;
150ae8c6e27Sflorian 	int s = sldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel);
151ae8c6e27Sflorian 	if(s) return s;
152ae8c6e27Sflorian 
153ae8c6e27Sflorian 	if(rel && origin && dlen > 0) {
15457403691Sflorian 		if((unsigned)dlen >= 0x00ffffffU ||
15557403691Sflorian 			(unsigned)origin_len >= 0x00ffffffU)
15657403691Sflorian 			/* guard against integer overflow in addition */
15757403691Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_GENERAL, *len);
158ae8c6e27Sflorian 		if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN)
159ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW,
160ae8c6e27Sflorian 				LDNS_MAX_DOMAINLEN);
161ae8c6e27Sflorian 		if(dlen + origin_len - 1 > *len)
162ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
163ae8c6e27Sflorian 				*len);
164ae8c6e27Sflorian 		memmove(buf+dlen-1, origin, origin_len);
165ae8c6e27Sflorian 		*len = dlen + origin_len - 1;
166ae8c6e27Sflorian 	} else
167ae8c6e27Sflorian 		*len = dlen;
168ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
169ae8c6e27Sflorian }
170ae8c6e27Sflorian 
sldns_str2wire_dname(const char * str,size_t * len)171ae8c6e27Sflorian uint8_t* sldns_str2wire_dname(const char* str, size_t* len)
172ae8c6e27Sflorian {
173ae8c6e27Sflorian 	uint8_t dname[LDNS_MAX_DOMAINLEN+1];
174ae8c6e27Sflorian 	*len = sizeof(dname);
175ae8c6e27Sflorian 	if(sldns_str2wire_dname_buf(str, dname, len) == 0) {
17657403691Sflorian 		uint8_t* r;
17757403691Sflorian 		if(*len > sizeof(dname)) return NULL;
17857403691Sflorian 		r = (uint8_t*)malloc(*len);
179ae8c6e27Sflorian 		if(r) return memcpy(r, dname, *len);
180ae8c6e27Sflorian 	}
181ae8c6e27Sflorian 	*len = 0;
182ae8c6e27Sflorian 	return NULL;
183ae8c6e27Sflorian }
184ae8c6e27Sflorian 
185ae8c6e27Sflorian /** read owner name */
186ae8c6e27Sflorian static int
rrinternal_get_owner(sldns_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)187ae8c6e27Sflorian rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len,
188ae8c6e27Sflorian 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
189ae8c6e27Sflorian 	size_t prev_len, char* token, size_t token_len)
190ae8c6e27Sflorian {
191ae8c6e27Sflorian 	/* split the rr in its parts -1 signals trouble */
192ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
193ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
194ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
195ae8c6e27Sflorian 	}
196ae8c6e27Sflorian 
19757403691Sflorian 	if(token_len < 2) /* make sure there is space to read "@" or "" */
19857403691Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
19957403691Sflorian 			sldns_buffer_position(strbuf));
200da8c8390Sflorian 	if(token[0]=='@' && token[1]=='\0') {
201ae8c6e27Sflorian 		uint8_t* tocopy;
202ae8c6e27Sflorian 		if (origin) {
203ae8c6e27Sflorian 			*dname_len = origin_len;
204ae8c6e27Sflorian 			tocopy = origin;
205ae8c6e27Sflorian 		} else if (prev) {
206ae8c6e27Sflorian 			*dname_len = prev_len;
207ae8c6e27Sflorian 			tocopy = prev;
208ae8c6e27Sflorian 		} else {
209ae8c6e27Sflorian 			/* default to root */
210ae8c6e27Sflorian 			*dname_len = 1;
211ae8c6e27Sflorian 			tocopy = (uint8_t*)"\0";
212ae8c6e27Sflorian 		}
213ae8c6e27Sflorian 		if(*len < *dname_len)
214ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
215ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
216ae8c6e27Sflorian 		memmove(rr, tocopy, *dname_len);
217ae8c6e27Sflorian 	} else if(*token == '\0') {
218ae8c6e27Sflorian 		/* no ownername was given, try prev, if that fails
219ae8c6e27Sflorian 		 * origin, else default to root */
220ae8c6e27Sflorian 		uint8_t* tocopy;
221ae8c6e27Sflorian 		if(prev) {
222ae8c6e27Sflorian 			*dname_len = prev_len;
223ae8c6e27Sflorian 			tocopy = prev;
224ae8c6e27Sflorian 		} else if(origin) {
225ae8c6e27Sflorian 			*dname_len = origin_len;
226ae8c6e27Sflorian 			tocopy = origin;
227ae8c6e27Sflorian 		} else {
228ae8c6e27Sflorian 			*dname_len = 1;
229ae8c6e27Sflorian 			tocopy = (uint8_t*)"\0";
230ae8c6e27Sflorian 		}
231ae8c6e27Sflorian 		if(*len < *dname_len)
232ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
233ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
234ae8c6e27Sflorian 		memmove(rr, tocopy, *dname_len);
235ae8c6e27Sflorian 	} else {
236ae8c6e27Sflorian 		size_t dlen = *len;
237ae8c6e27Sflorian 		int s = sldns_str2wire_dname_buf_origin(token, rr, &dlen,
238ae8c6e27Sflorian 			origin, origin_len);
239ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s,
240ae8c6e27Sflorian 			sldns_buffer_position(strbuf)-strlen(token));
241ae8c6e27Sflorian 		*dname_len = dlen;
242ae8c6e27Sflorian 	}
243ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
244ae8c6e27Sflorian }
245ae8c6e27Sflorian 
246ae8c6e27Sflorian /** read ttl */
247ae8c6e27Sflorian static int
rrinternal_get_ttl(sldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint32_t * ttl,uint32_t default_ttl)248ae8c6e27Sflorian rrinternal_get_ttl(sldns_buffer* strbuf, char* token, size_t token_len,
249ae8c6e27Sflorian 	int* not_there, uint32_t* ttl, uint32_t default_ttl)
250ae8c6e27Sflorian {
251ae8c6e27Sflorian 	const char* endptr;
2527a05b9dfSflorian 	int overflow;
253ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
254ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TTL,
255ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
256ae8c6e27Sflorian 	}
2577a05b9dfSflorian 	*ttl = (uint32_t) sldns_str2period(token, &endptr, &overflow);
2587a05b9dfSflorian 	if(overflow) {
2597a05b9dfSflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW,
2607a05b9dfSflorian 			sldns_buffer_position(strbuf));
2617a05b9dfSflorian 	}
262ae8c6e27Sflorian 
263ae8c6e27Sflorian 	if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) {
264ae8c6e27Sflorian 		*not_there = 1;
265ae8c6e27Sflorian 		/* ah, it's not there or something */
266ae8c6e27Sflorian 		if (default_ttl == 0) {
267ae8c6e27Sflorian 			*ttl = LDNS_DEFAULT_TTL;
268ae8c6e27Sflorian 		} else {
269ae8c6e27Sflorian 			*ttl = default_ttl;
270ae8c6e27Sflorian 		}
271ae8c6e27Sflorian 	}
272ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
273ae8c6e27Sflorian }
274ae8c6e27Sflorian 
275ae8c6e27Sflorian /** read class */
276ae8c6e27Sflorian static int
rrinternal_get_class(sldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint16_t * cl)277ae8c6e27Sflorian rrinternal_get_class(sldns_buffer* strbuf, char* token, size_t token_len,
278ae8c6e27Sflorian 	int* not_there, uint16_t* cl)
279ae8c6e27Sflorian {
280ae8c6e27Sflorian 	/* if 'not_there' then we got token from previous parse routine */
281ae8c6e27Sflorian 	if(!*not_there) {
282ae8c6e27Sflorian 		/* parse new token for class */
283ae8c6e27Sflorian 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
284ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_CLASS,
285ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
286ae8c6e27Sflorian 		}
287ae8c6e27Sflorian 	} else *not_there = 0;
288ae8c6e27Sflorian 	*cl = sldns_get_rr_class_by_name(token);
289ae8c6e27Sflorian 	/* class can be left out too, assume IN, current token must be type */
290ae8c6e27Sflorian 	if(*cl == 0 && strcmp(token, "CLASS0") != 0) {
291ae8c6e27Sflorian 		*not_there = 1;
292ae8c6e27Sflorian 		*cl = LDNS_RR_CLASS_IN;
293ae8c6e27Sflorian 	}
294ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
295ae8c6e27Sflorian }
296ae8c6e27Sflorian 
297ae8c6e27Sflorian /** read type */
298ae8c6e27Sflorian static int
rrinternal_get_type(sldns_buffer * strbuf,char * token,size_t token_len,int * not_there,uint16_t * tp)299ae8c6e27Sflorian rrinternal_get_type(sldns_buffer* strbuf, char* token, size_t token_len,
300ae8c6e27Sflorian 	int* not_there, uint16_t* tp)
301ae8c6e27Sflorian {
302ae8c6e27Sflorian 	/* if 'not_there' then we got token from previous parse routine */
303ae8c6e27Sflorian 	if(!*not_there) {
304ae8c6e27Sflorian 		/* parse new token for type */
305ae8c6e27Sflorian 		if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) {
306ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
307ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
308ae8c6e27Sflorian 		}
309ae8c6e27Sflorian 	}
310ae8c6e27Sflorian 	*tp = sldns_get_rr_type_by_name(token);
311ae8c6e27Sflorian 	if(*tp == 0 && strcmp(token, "TYPE0") != 0) {
312ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
313ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
314ae8c6e27Sflorian 	}
315ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
316ae8c6e27Sflorian }
317ae8c6e27Sflorian 
318ae8c6e27Sflorian /** put type, class, ttl into rr buffer */
319ae8c6e27Sflorian static int
rrinternal_write_typeclassttl(sldns_buffer * strbuf,uint8_t * rr,size_t len,size_t dname_len,uint16_t tp,uint16_t cl,uint32_t ttl,int question)320ae8c6e27Sflorian rrinternal_write_typeclassttl(sldns_buffer* strbuf, uint8_t* rr, size_t len,
321ae8c6e27Sflorian 	size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question)
322ae8c6e27Sflorian {
323ae8c6e27Sflorian 	if(question) {
324ae8c6e27Sflorian 		/* question is : name, type, class */
325ae8c6e27Sflorian 		if(dname_len + 4 > len)
326ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
327ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
328ae8c6e27Sflorian 		sldns_write_uint16(rr+dname_len, tp);
329ae8c6e27Sflorian 		sldns_write_uint16(rr+dname_len+2, cl);
330ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
331ae8c6e27Sflorian 	}
332ae8c6e27Sflorian 
333ae8c6e27Sflorian 	/* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */
334ae8c6e27Sflorian 	if(dname_len + 10 > len)
335ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
336ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
337ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len, tp);
338ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+2, cl);
339ae8c6e27Sflorian 	sldns_write_uint32(rr+dname_len+4, ttl);
340ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */
341ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
342ae8c6e27Sflorian }
343ae8c6e27Sflorian 
344ae8c6e27Sflorian /** find delimiters for type */
345ae8c6e27Sflorian static const char*
rrinternal_get_delims(sldns_rdf_type rdftype,size_t r_cnt,size_t r_max)346ae8c6e27Sflorian rrinternal_get_delims(sldns_rdf_type rdftype, size_t r_cnt, size_t r_max)
347ae8c6e27Sflorian {
348ae8c6e27Sflorian 	switch(rdftype) {
349ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64        :
350ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX        : /* These rdf types may con- */
351ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LOC        : /* tain whitespace, only if */
352ae8c6e27Sflorian 	case LDNS_RDF_TYPE_WKS        : /* it is the last rd field. */
353ae8c6e27Sflorian 	case LDNS_RDF_TYPE_IPSECKEY   :
354ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC       :	if (r_cnt == r_max - 1) {
355ae8c6e27Sflorian 						return "\n";
356ae8c6e27Sflorian 					}
357ae8c6e27Sflorian 					break;
358ae8c6e27Sflorian 	default                       :	break;
359ae8c6e27Sflorian 	}
360ae8c6e27Sflorian 	return "\n\t ";
361ae8c6e27Sflorian }
362ae8c6e27Sflorian 
363ae8c6e27Sflorian /* Syntactic sugar for sldns_rr_new_frm_str_internal */
364ae8c6e27Sflorian static int
sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type)365ae8c6e27Sflorian sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type)
366ae8c6e27Sflorian {
367ae8c6e27Sflorian 	return  rdf_type == LDNS_RDF_TYPE_STR ||
368ae8c6e27Sflorian 		rdf_type == LDNS_RDF_TYPE_LONG_STR;
369ae8c6e27Sflorian }
370ae8c6e27Sflorian 
371ae8c6e27Sflorian /** see if rdata is quoted */
372ae8c6e27Sflorian static int
rrinternal_get_quoted(sldns_buffer * strbuf,const char ** delimiters,sldns_rdf_type rdftype)373ae8c6e27Sflorian rrinternal_get_quoted(sldns_buffer* strbuf, const char** delimiters,
374ae8c6e27Sflorian 	sldns_rdf_type rdftype)
375ae8c6e27Sflorian {
376ae8c6e27Sflorian 	if(sldns_rdf_type_maybe_quoted(rdftype) &&
377ae8c6e27Sflorian 		sldns_buffer_remaining(strbuf) > 0) {
378ae8c6e27Sflorian 
379ae8c6e27Sflorian 		/* skip spaces */
380ae8c6e27Sflorian 		while(sldns_buffer_remaining(strbuf) > 0 &&
3817a05b9dfSflorian 			(*(sldns_buffer_current(strbuf)) == ' ' ||
3827a05b9dfSflorian 			*(sldns_buffer_current(strbuf)) == '\t')) {
383ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, 1);
384ae8c6e27Sflorian 		}
385ae8c6e27Sflorian 
386ae8c6e27Sflorian 		if(sldns_buffer_remaining(strbuf) > 0 &&
387ae8c6e27Sflorian 			*(sldns_buffer_current(strbuf)) == '\"') {
388ae8c6e27Sflorian 			*delimiters = "\"\0";
389ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, 1);
390ae8c6e27Sflorian 			return 1;
391ae8c6e27Sflorian 		}
392ae8c6e27Sflorian 	}
393ae8c6e27Sflorian 	return 0;
394ae8c6e27Sflorian }
395ae8c6e27Sflorian 
396ae8c6e27Sflorian /** spool hex data into rdata */
397ae8c6e27Sflorian 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)398ae8c6e27Sflorian rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len,
399ae8c6e27Sflorian 	size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size)
400ae8c6e27Sflorian {
401ae8c6e27Sflorian 	char* p = token;
402ae8c6e27Sflorian 	while(*p) {
403ae8c6e27Sflorian 		if(isspace((unsigned char)*p)) {
404ae8c6e27Sflorian 			p++;
405ae8c6e27Sflorian 			continue;
406ae8c6e27Sflorian 		}
407ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*p))
408ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
409ae8c6e27Sflorian 				p-token);
410ae8c6e27Sflorian 		if(*cur_hex_data_size >= hex_data_size)
411ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
412ae8c6e27Sflorian 				p-token);
413ae8c6e27Sflorian 		/* extra robust check */
414ae8c6e27Sflorian 		if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len)
415ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
416ae8c6e27Sflorian 				p-token);
417ae8c6e27Sflorian 		/* see if 16s or 1s */
418ae8c6e27Sflorian 		if( ((*cur_hex_data_size)&1) == 0) {
419ae8c6e27Sflorian 			rr[rr_cur_len+(*cur_hex_data_size)/2] =
420ae8c6e27Sflorian 				(uint8_t)sldns_hexdigit_to_int(*p)*16;
421ae8c6e27Sflorian 		} else {
422ae8c6e27Sflorian 			rr[rr_cur_len+(*cur_hex_data_size)/2] +=
423ae8c6e27Sflorian 				(uint8_t)sldns_hexdigit_to_int(*p);
424ae8c6e27Sflorian 		}
425ae8c6e27Sflorian 		p++;
426ae8c6e27Sflorian 		(*cur_hex_data_size)++;
427ae8c6e27Sflorian 	}
428ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
429ae8c6e27Sflorian }
430ae8c6e27Sflorian 
431ae8c6e27Sflorian /** read unknown rr type format */
432ae8c6e27Sflorian static int
rrinternal_parse_unknown(sldns_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)433ae8c6e27Sflorian rrinternal_parse_unknown(sldns_buffer* strbuf, char* token, size_t token_len,
434ae8c6e27Sflorian         uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos)
435ae8c6e27Sflorian {
436ae8c6e27Sflorian 	const char* delim = "\n\t ";
437ae8c6e27Sflorian 	size_t hex_data_size, cur_hex_data_size;
438ae8c6e27Sflorian 	/* go back to before \#
439ae8c6e27Sflorian 	 * and skip it while setting delimiters better
440ae8c6e27Sflorian 	 */
441ae8c6e27Sflorian 	sldns_buffer_set_position(strbuf, pre_data_pos);
442ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1)
443ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_GENERAL; /* should not fail */
444ae8c6e27Sflorian 	/* read rdata octet length */
445ae8c6e27Sflorian 	if(sldns_bget_token(strbuf, token, delim, token_len) == -1) {
446ae8c6e27Sflorian 		/* something goes very wrong here */
447ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
448ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
449ae8c6e27Sflorian 	}
450ae8c6e27Sflorian 	hex_data_size = (size_t)atoi(token);
451ae8c6e27Sflorian 	if(hex_data_size > LDNS_MAX_RDFLEN ||
452ae8c6e27Sflorian 		*rr_cur_len + hex_data_size > *rr_len) {
453ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
454ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
455ae8c6e27Sflorian 	}
456ae8c6e27Sflorian 	/* copy hex chars into hex str (2 chars per byte) */
457ae8c6e27Sflorian 	hex_data_size *= 2;
458ae8c6e27Sflorian 	cur_hex_data_size = 0;
459ae8c6e27Sflorian 	while(cur_hex_data_size < hex_data_size) {
460ae8c6e27Sflorian 		int status;
461ae8c6e27Sflorian 		ssize_t c = sldns_bget_token(strbuf, token, delim, token_len);
462ae8c6e27Sflorian 		if((status = rrinternal_spool_hex(token, rr, *rr_len,
463ae8c6e27Sflorian 			*rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0)
464ae8c6e27Sflorian 			return RET_ERR_SHIFT(status,
465ae8c6e27Sflorian 				sldns_buffer_position(strbuf)-strlen(token));
466ae8c6e27Sflorian 		if(c == -1) {
467ae8c6e27Sflorian 			if(cur_hex_data_size != hex_data_size)
468ae8c6e27Sflorian 				return RET_ERR(
469ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_SYNTAX_RDATA,
470ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
471ae8c6e27Sflorian 			break;
472ae8c6e27Sflorian 		}
473ae8c6e27Sflorian 	}
474ae8c6e27Sflorian 	*rr_cur_len += hex_data_size/2;
475ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
476ae8c6e27Sflorian }
477ae8c6e27Sflorian 
478ae8c6e27Sflorian /** parse normal RR rdata element */
479ae8c6e27Sflorian static int
rrinternal_parse_rdf(sldns_buffer * strbuf,char * token,size_t token_len,uint8_t * rr,size_t rr_len,size_t * rr_cur_len,sldns_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)480ae8c6e27Sflorian rrinternal_parse_rdf(sldns_buffer* strbuf, char* token, size_t token_len,
481ae8c6e27Sflorian 	uint8_t* rr, size_t rr_len, size_t* rr_cur_len, sldns_rdf_type rdftype,
482ae8c6e27Sflorian 	uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len,
483ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
484ae8c6e27Sflorian {
485ae8c6e27Sflorian 	size_t len;
486ae8c6e27Sflorian 	int status;
487ae8c6e27Sflorian 
488ae8c6e27Sflorian 	switch(rdftype) {
489ae8c6e27Sflorian 	case LDNS_RDF_TYPE_DNAME:
490ae8c6e27Sflorian 		/* check if the origin should be used or concatenated */
491ae8c6e27Sflorian 		if(strcmp(token, "@") == 0) {
492ae8c6e27Sflorian 			uint8_t* tocopy;
493ae8c6e27Sflorian 			size_t copylen;
494ae8c6e27Sflorian 			if(origin) {
495ae8c6e27Sflorian 				copylen = origin_len;
496ae8c6e27Sflorian 				tocopy = origin;
497ae8c6e27Sflorian 			} else if(rr_type == LDNS_RR_TYPE_SOA) {
498ae8c6e27Sflorian 				copylen = dname_len;
499ae8c6e27Sflorian 				tocopy = rr; /* copy rr owner name */
500ae8c6e27Sflorian 			} else {
501ae8c6e27Sflorian 				copylen = 1;
502ae8c6e27Sflorian 				tocopy = (uint8_t*)"\0";
503ae8c6e27Sflorian 			}
504ae8c6e27Sflorian 			if((*rr_cur_len) + copylen > rr_len)
505ae8c6e27Sflorian 				return RET_ERR(
506ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
507ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
508ae8c6e27Sflorian 			memmove(rr+*rr_cur_len, tocopy, copylen);
509ae8c6e27Sflorian 			(*rr_cur_len) += copylen;
510ae8c6e27Sflorian 		} else {
511ae8c6e27Sflorian 			size_t dlen = rr_len - (*rr_cur_len);
512ae8c6e27Sflorian 			int s = sldns_str2wire_dname_buf_origin(token,
513ae8c6e27Sflorian 				rr+*rr_cur_len, &dlen, origin, origin_len);
514ae8c6e27Sflorian 			if(s) return RET_ERR_SHIFT(s,
515ae8c6e27Sflorian 				sldns_buffer_position(strbuf)-strlen(token));
516ae8c6e27Sflorian 			(*rr_cur_len) += dlen;
517ae8c6e27Sflorian 		}
518ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
519ae8c6e27Sflorian 
520ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX:
521ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64:
522ae8c6e27Sflorian 		/* When this is the last rdata field, then the
523ae8c6e27Sflorian 		 * rest should be read in (cause then these
524ae8c6e27Sflorian 		 * rdf types may contain spaces). */
525ae8c6e27Sflorian 		if(r_cnt == r_max - 1) {
526ae8c6e27Sflorian 			size_t tlen = strlen(token);
527ae8c6e27Sflorian 			(void)sldns_bget_token(strbuf, token+tlen, "\n",
528ae8c6e27Sflorian 				token_len - tlen);
529ae8c6e27Sflorian 		}
530ae8c6e27Sflorian 		break;
531ae8c6e27Sflorian 	default:
532ae8c6e27Sflorian 		break;
533ae8c6e27Sflorian 	}
534ae8c6e27Sflorian 
535ae8c6e27Sflorian 	len = rr_len - (*rr_cur_len);
536ae8c6e27Sflorian 	if((status=sldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len,
537ae8c6e27Sflorian 		rdftype)) != 0)
538ae8c6e27Sflorian 		return RET_ERR_SHIFT(status,
539ae8c6e27Sflorian 			sldns_buffer_position(strbuf)-strlen(token));
540ae8c6e27Sflorian 	*rr_cur_len += len;
541ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
542ae8c6e27Sflorian }
543ae8c6e27Sflorian 
544ae8c6e27Sflorian /**
545ae8c6e27Sflorian  * Parse one rdf token.  Takes care of quotes and parenthesis.
546ae8c6e27Sflorian  */
547ae8c6e27Sflorian static int
sldns_parse_rdf_token(sldns_buffer * strbuf,char * token,size_t token_len,int * quoted,int * parens,size_t * pre_data_pos,const char * delimiters,sldns_rdf_type rdftype,size_t * token_strlen)548ae8c6e27Sflorian sldns_parse_rdf_token(sldns_buffer* strbuf, char* token, size_t token_len,
549ae8c6e27Sflorian 	int* quoted, int* parens, size_t* pre_data_pos,
550ae8c6e27Sflorian 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
551ae8c6e27Sflorian {
552ae8c6e27Sflorian 	size_t slen;
553ae8c6e27Sflorian 
554a1a7ba80Sflorian 	/* skip spaces and tabs */
555ae8c6e27Sflorian 	while(sldns_buffer_remaining(strbuf) > 0 && !*quoted &&
556a1a7ba80Sflorian 		(*(sldns_buffer_current(strbuf)) == ' ' ||
557a1a7ba80Sflorian 		*(sldns_buffer_current(strbuf)) == '\t')) {
558ae8c6e27Sflorian 		sldns_buffer_skip(strbuf, 1);
559ae8c6e27Sflorian 	}
560ae8c6e27Sflorian 
561ae8c6e27Sflorian 	*pre_data_pos = sldns_buffer_position(strbuf);
562ae8c6e27Sflorian 	if(sldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters,
563ae8c6e27Sflorian 		token_len, parens, (*quoted)?NULL:" \t") == -1) {
564ae8c6e27Sflorian 		return 0;
565ae8c6e27Sflorian 	}
566ae8c6e27Sflorian 	slen = strlen(token);
567ae8c6e27Sflorian 	/* check if not quoted yet, and we have encountered quotes */
568ae8c6e27Sflorian 	if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
569ae8c6e27Sflorian 		slen >= 2 &&
570ae8c6e27Sflorian 		(token[0] == '"' || token[0] == '\'') &&
571ae8c6e27Sflorian 		(token[slen-1] == '"' || token[slen-1] == '\'')) {
572ae8c6e27Sflorian 		/* move token two smaller (quotes) with endnull */
573ae8c6e27Sflorian 		memmove(token, token+1, slen-2);
574ae8c6e27Sflorian 		token[slen-2] = 0;
575ae8c6e27Sflorian 		slen -= 2;
576ae8c6e27Sflorian 		*quoted = 1;
577ae8c6e27Sflorian 	} else if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) &&
578ae8c6e27Sflorian 		slen >= 2 &&
579ae8c6e27Sflorian 		(token[0] == '"' || token[0] == '\'')) {
580ae8c6e27Sflorian 		/* got the start quote (remove it) but read remainder
581ae8c6e27Sflorian 		 * of quoted string as well into remainder of token */
582ae8c6e27Sflorian 		memmove(token, token+1, slen-1);
583ae8c6e27Sflorian 		token[slen-1] = 0;
584ae8c6e27Sflorian 		slen -= 1;
585ae8c6e27Sflorian 		*quoted = 1;
586ae8c6e27Sflorian 		/* rewind buffer over skipped whitespace */
587ae8c6e27Sflorian 		while(sldns_buffer_position(strbuf) > 0 &&
588ae8c6e27Sflorian 			(sldns_buffer_current(strbuf)[-1] == ' ' ||
589ae8c6e27Sflorian 			sldns_buffer_current(strbuf)[-1] == '\t')) {
590ae8c6e27Sflorian 			sldns_buffer_skip(strbuf, -1);
591ae8c6e27Sflorian 		}
592ae8c6e27Sflorian 		if(sldns_bget_token_par(strbuf, token+slen,
593ae8c6e27Sflorian 			"\"", token_len-slen,
594ae8c6e27Sflorian 			parens, NULL) == -1) {
595ae8c6e27Sflorian 			return 0;
596ae8c6e27Sflorian 		}
597ae8c6e27Sflorian 		slen = strlen(token);
598ae8c6e27Sflorian 	}
599ae8c6e27Sflorian 	*token_strlen = slen;
600ae8c6e27Sflorian 	return 1;
601ae8c6e27Sflorian }
602ae8c6e27Sflorian 
603ae8c6e27Sflorian /** Add space and one more rdf token onto the existing token string. */
604ae8c6e27Sflorian static int
sldns_affix_token(sldns_buffer * strbuf,char * token,size_t * token_len,int * quoted,int * parens,size_t * pre_data_pos,const char * delimiters,sldns_rdf_type rdftype,size_t * token_strlen)605ae8c6e27Sflorian sldns_affix_token(sldns_buffer* strbuf, char* token, size_t* token_len,
606ae8c6e27Sflorian 	int* quoted, int* parens, size_t* pre_data_pos,
607ae8c6e27Sflorian 	const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen)
608ae8c6e27Sflorian {
609ae8c6e27Sflorian 	size_t addlen = *token_len - *token_strlen;
610ae8c6e27Sflorian 	size_t addstrlen = 0;
611ae8c6e27Sflorian 
612ae8c6e27Sflorian 	/* add space */
613a1a7ba80Sflorian 	/* when addlen < 2, the token buffer is full considering the NULL byte
614a1a7ba80Sflorian 	 * from strlen and will lead to buffer overflow with the second
6157a05b9dfSflorian 	 * assignment below. */
616a1a7ba80Sflorian 	if(addlen < 2) return 0;
617ae8c6e27Sflorian 	token[*token_strlen] = ' ';
618ae8c6e27Sflorian 	token[++(*token_strlen)] = 0;
619ae8c6e27Sflorian 
620ae8c6e27Sflorian 	/* read another token */
621ae8c6e27Sflorian 	addlen = *token_len - *token_strlen;
622ae8c6e27Sflorian 	if(!sldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted,
623ae8c6e27Sflorian 		parens, pre_data_pos, delimiters, rdftype, &addstrlen))
624ae8c6e27Sflorian 		return 0;
625ae8c6e27Sflorian 	(*token_strlen) += addstrlen;
626ae8c6e27Sflorian 	return 1;
627ae8c6e27Sflorian }
628ae8c6e27Sflorian 
sldns_str2wire_svcparam_key_cmp(const void * a,const void * b)629411c5950Sflorian static int sldns_str2wire_svcparam_key_cmp(const void *a, const void *b)
630411c5950Sflorian {
631411c5950Sflorian 	return sldns_read_uint16(*(uint8_t**) a)
632411c5950Sflorian 	     - sldns_read_uint16(*(uint8_t**) b);
633411c5950Sflorian }
634411c5950Sflorian 
635411c5950Sflorian /**
636411c5950Sflorian  * Add constraints to the SVCB RRs which involve the whole set
637411c5950Sflorian  */
sldns_str2wire_check_svcbparams(uint8_t * rdata,uint16_t rdata_len)638411c5950Sflorian static int sldns_str2wire_check_svcbparams(uint8_t* rdata, uint16_t rdata_len)
639411c5950Sflorian {
640411c5950Sflorian 	size_t   nparams = 0, i;
641411c5950Sflorian 	uint8_t  new_rdata[LDNS_MAX_RDFLEN];
642411c5950Sflorian 	uint8_t* new_rdata_ptr = new_rdata;
643411c5950Sflorian 	uint8_t* svcparams[MAX_NUMBER_OF_SVCPARAMS];
644411c5950Sflorian 	uint8_t* rdata_ptr = rdata;
645411c5950Sflorian 	uint16_t rdata_remaining = rdata_len;
646411c5950Sflorian 
647411c5950Sflorian 	/* find the SvcParams */
648411c5950Sflorian 	while (rdata_remaining) {
649411c5950Sflorian 		uint16_t svcbparam_len;
650411c5950Sflorian 
651411c5950Sflorian 		svcparams[nparams] = rdata_ptr;
652411c5950Sflorian 		if (rdata_remaining < 4)
653411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
654411c5950Sflorian 		svcbparam_len = sldns_read_uint16(rdata_ptr + 2);
655411c5950Sflorian 		rdata_remaining -= 4;
656411c5950Sflorian 		rdata_ptr += 4;
657411c5950Sflorian 
658411c5950Sflorian 		if (rdata_remaining < svcbparam_len)
659411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCPARAM_BROKEN_RDATA;
660411c5950Sflorian 		rdata_remaining -= svcbparam_len;
661411c5950Sflorian 		rdata_ptr += svcbparam_len;
662411c5950Sflorian 
663411c5950Sflorian 		nparams += 1;
664411c5950Sflorian 		if (nparams >= MAX_NUMBER_OF_SVCPARAMS)
665411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_TOO_MANY_PARAMS;
666411c5950Sflorian 	}
667411c5950Sflorian 
668411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
669411c5950Sflorian 	 *
670411c5950Sflorian 	 *     In wire format, the keys are represented by their numeric
671411c5950Sflorian 	 *     values in network byte order, concatenated in ascending order.
672411c5950Sflorian 	 */
673411c5950Sflorian 	qsort((void *)svcparams
674411c5950Sflorian 	     ,nparams
675411c5950Sflorian 	     ,sizeof(uint8_t*)
676411c5950Sflorian 	     ,sldns_str2wire_svcparam_key_cmp);
677411c5950Sflorian 
678411c5950Sflorian 
6797a05b9dfSflorian 	/* The code below revolves around semantic errors in the SVCParam set.
680411c5950Sflorian 	 * So long as we do not distinguish between running Unbound as a primary
681411c5950Sflorian 	 * or as a secondary, we default to secondary behavior and we ignore the
6827a05b9dfSflorian 	 * semantic errors. */
683411c5950Sflorian 
684411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
685411c5950Sflorian 	{
686411c5950Sflorian 		uint8_t* mandatory = NULL;
687411c5950Sflorian 		/* In draft-ietf-dnsop-svcb-https-06 Section 7:
688411c5950Sflorian 		 *
689411c5950Sflorian 		 *     Keys (...) MUST NOT appear more than once.
690411c5950Sflorian 		 *
691411c5950Sflorian 		 * If they key has already been seen, we have a duplicate
692411c5950Sflorian 		 */
693411c5950Sflorian 		for(i=0; i < nparams; i++) {
694411c5950Sflorian 			uint16_t key = sldns_read_uint16(svcparams[i]);
695411c5950Sflorian 			if(i + 1 < nparams && key == sldns_read_uint16(svcparams[i+1]))
696411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_DUPLICATE_KEYS;
697411c5950Sflorian 			if(key == SVCB_KEY_MANDATORY)
698411c5950Sflorian 				mandatory = svcparams[i];
699411c5950Sflorian 		}
700411c5950Sflorian 
701d500c338Sflorian 		/* Verify that all the SvcParamKeys in mandatory are present */
702411c5950Sflorian 		if(mandatory) {
703411c5950Sflorian 			/* Divide by sizeof(uint16_t)*/
704411c5950Sflorian 			uint16_t mandatory_nkeys = sldns_read_uint16(mandatory + 2) / sizeof(uint16_t);
705411c5950Sflorian 
706411c5950Sflorian 			/* Guaranteed by sldns_str2wire_svcparam_key_value */
707411c5950Sflorian 			assert(mandatory_nkeys > 0);
708411c5950Sflorian 
709411c5950Sflorian 			for(i=0; i < mandatory_nkeys; i++) {
710411c5950Sflorian 				uint16_t mandatory_key = sldns_read_uint16(
711411c5950Sflorian 					mandatory
712411c5950Sflorian 					+ 2 * sizeof(uint16_t)
713411c5950Sflorian 					+ i * sizeof(uint16_t));
714411c5950Sflorian 				uint8_t found = 0;
715411c5950Sflorian 				size_t j;
716411c5950Sflorian 
717411c5950Sflorian 				for(j=0; j < nparams; j++) {
718411c5950Sflorian 					if(mandatory_key == sldns_read_uint16(svcparams[j])) {
719411c5950Sflorian 						found = 1;
720411c5950Sflorian 						break;
721411c5950Sflorian 					}
722411c5950Sflorian 				}
723411c5950Sflorian 
724411c5950Sflorian 				if(!found)
725411c5950Sflorian 					return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_MISSING_PARAM;
726411c5950Sflorian 			}
727411c5950Sflorian 		}
728411c5950Sflorian 	}
729411c5950Sflorian #endif
730411c5950Sflorian 	/* Write rdata in correct order */
731411c5950Sflorian 	for (i = 0; i < nparams; i++) {
732411c5950Sflorian 		uint16_t svcparam_len = sldns_read_uint16(svcparams[i] + 2)
733411c5950Sflorian 		                      + 2 * sizeof(uint16_t);
734411c5950Sflorian 
735411c5950Sflorian 		if ((unsigned)(new_rdata_ptr - new_rdata) + svcparam_len > sizeof(new_rdata))
736411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
737411c5950Sflorian 
738411c5950Sflorian 		memcpy(new_rdata_ptr, svcparams[i], svcparam_len);
739411c5950Sflorian 		new_rdata_ptr += svcparam_len;
740411c5950Sflorian 	}
741411c5950Sflorian 	memcpy(rdata, new_rdata, rdata_len);
742411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
743411c5950Sflorian }
744411c5950Sflorian 
745ae8c6e27Sflorian /** parse rdata from string into rr buffer(-remainder after dname). */
746ae8c6e27Sflorian static int
rrinternal_parse_rdata(sldns_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)747ae8c6e27Sflorian rrinternal_parse_rdata(sldns_buffer* strbuf, char* token, size_t token_len,
748ae8c6e27Sflorian 	uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type,
749ae8c6e27Sflorian 	uint8_t* origin, size_t origin_len)
750ae8c6e27Sflorian {
751ae8c6e27Sflorian 	const sldns_rr_descriptor *desc = sldns_rr_descript((uint16_t)rr_type);
752ae8c6e27Sflorian 	size_t r_cnt, r_min, r_max;
753ae8c6e27Sflorian 	size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen;
754ae8c6e27Sflorian 	int was_unknown_rr_format = 0, parens = 0, status, quoted;
755ae8c6e27Sflorian 	const char* delimiters;
756ae8c6e27Sflorian 	sldns_rdf_type rdftype;
757ae8c6e27Sflorian 	/* a desc is always returned */
758ae8c6e27Sflorian 	if(!desc) return LDNS_WIREPARSE_ERR_GENERAL;
759ae8c6e27Sflorian 	r_max = sldns_rr_descriptor_maximum(desc);
760ae8c6e27Sflorian 	r_min = sldns_rr_descriptor_minimum(desc);
761ae8c6e27Sflorian 	/* robust check */
762ae8c6e27Sflorian 	if(rr_cur_len > *rr_len)
763ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
764ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
765ae8c6e27Sflorian 
766ae8c6e27Sflorian 	/* because number of fields can be variable, we can't rely on
767ae8c6e27Sflorian 	 * _maximum() only */
768ae8c6e27Sflorian 	for(r_cnt=0; r_cnt < r_max; r_cnt++) {
769ae8c6e27Sflorian 		rdftype = sldns_rr_descriptor_field_type(desc, r_cnt);
770ae8c6e27Sflorian 		delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max);
771ae8c6e27Sflorian 		quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype);
772ae8c6e27Sflorian 
773ae8c6e27Sflorian 		if(!sldns_parse_rdf_token(strbuf, token, token_len, &quoted,
774ae8c6e27Sflorian 			&parens, &pre_data_pos, delimiters, rdftype,
775ae8c6e27Sflorian 			&token_strlen))
776ae8c6e27Sflorian 			break;
777ae8c6e27Sflorian 
778ae8c6e27Sflorian 		/* rfc3597 specifies that any type can be represented
779ae8c6e27Sflorian 		 * with \# method, which can contain spaces...
780ae8c6e27Sflorian 		 * it does specify size though... */
781ae8c6e27Sflorian 
782ae8c6e27Sflorian 		/* unknown RR data */
783ae8c6e27Sflorian 		if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 &&
7847a05b9dfSflorian 			!quoted && (token_strlen == 2 || token[2]==' ' ||
7857a05b9dfSflorian 			token[2]=='\t')) {
786ae8c6e27Sflorian 			was_unknown_rr_format = 1;
787ae8c6e27Sflorian 			if((status=rrinternal_parse_unknown(strbuf, token,
788ae8c6e27Sflorian 				token_len, rr, rr_len, &rr_cur_len,
789ae8c6e27Sflorian 				pre_data_pos)) != 0)
790ae8c6e27Sflorian 				return status;
791ae8c6e27Sflorian 		} else if(token_strlen > 0 || quoted) {
792ae8c6e27Sflorian 			if(rdftype == LDNS_RDF_TYPE_HIP) {
793ae8c6e27Sflorian 				/* affix the HIT and PK fields, with a space */
794ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
795ae8c6e27Sflorian 					&token_len, &quoted, &parens,
796ae8c6e27Sflorian 					&pre_data_pos, delimiters,
797ae8c6e27Sflorian 					rdftype, &token_strlen))
798ae8c6e27Sflorian 					break;
799ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
800ae8c6e27Sflorian 					&token_len, &quoted, &parens,
801ae8c6e27Sflorian 					&pre_data_pos, delimiters,
802ae8c6e27Sflorian 					rdftype, &token_strlen))
803ae8c6e27Sflorian 					break;
804ae8c6e27Sflorian 			} else if(rdftype == LDNS_RDF_TYPE_INT16_DATA &&
805ae8c6e27Sflorian 				strcmp(token, "0")!=0) {
806ae8c6e27Sflorian 				/* affix len and b64 fields */
807ae8c6e27Sflorian 				if(!sldns_affix_token(strbuf, token,
808ae8c6e27Sflorian 					&token_len, &quoted, &parens,
809ae8c6e27Sflorian 					&pre_data_pos, delimiters,
810ae8c6e27Sflorian 					rdftype, &token_strlen))
811ae8c6e27Sflorian 					break;
812ae8c6e27Sflorian 			}
813ae8c6e27Sflorian 
814ae8c6e27Sflorian 			/* normal RR */
815ae8c6e27Sflorian 			if((status=rrinternal_parse_rdf(strbuf, token,
816ae8c6e27Sflorian 				token_len, rr, *rr_len, &rr_cur_len, rdftype,
817ae8c6e27Sflorian 				rr_type, r_cnt, r_max, dname_len, origin,
818ae8c6e27Sflorian 				origin_len)) != 0) {
819ae8c6e27Sflorian 				return status;
820ae8c6e27Sflorian 			}
821ae8c6e27Sflorian 		}
822ae8c6e27Sflorian 	}
823ae8c6e27Sflorian 	if(!was_unknown_rr_format && r_cnt+1 < r_min) {
824ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE,
825ae8c6e27Sflorian 			sldns_buffer_position(strbuf));
826ae8c6e27Sflorian 	}
827ae8c6e27Sflorian 	while(parens != 0) {
828ae8c6e27Sflorian 		/* read remainder, must be "" */
829ae8c6e27Sflorian 		if(sldns_bget_token_par(strbuf, token, "\n", token_len,
830ae8c6e27Sflorian 			&parens, " \t") == -1) {
831ae8c6e27Sflorian 			if(parens != 0)
832ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
833ae8c6e27Sflorian 					sldns_buffer_position(strbuf));
834ae8c6e27Sflorian 			break;
835ae8c6e27Sflorian 		}
836ae8c6e27Sflorian 		if(strcmp(token, "") != 0)
837ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS,
838ae8c6e27Sflorian 				sldns_buffer_position(strbuf));
839ae8c6e27Sflorian 	}
840ae8c6e27Sflorian 	/* write rdata length */
841ae8c6e27Sflorian 	sldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10));
842ae8c6e27Sflorian 	*rr_len = rr_cur_len;
843411c5950Sflorian 	/* SVCB/HTTPS handling  */
844411c5950Sflorian 	if (rr_type == LDNS_RR_TYPE_SVCB || rr_type == LDNS_RR_TYPE_HTTPS) {
845411c5950Sflorian 		size_t rdata_len = rr_cur_len - dname_len - 10;
846411c5950Sflorian 		uint8_t *rdata = rr+dname_len + 10;
847411c5950Sflorian 
848411c5950Sflorian 		/* skip 1st rdata field SvcPriority (uint16_t) */
849411c5950Sflorian 		if (rdata_len < sizeof(uint16_t))
850411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
851411c5950Sflorian 
852411c5950Sflorian 		rdata_len -= sizeof(uint16_t);
853411c5950Sflorian 		rdata += sizeof(uint16_t);
854411c5950Sflorian 
855411c5950Sflorian 		/* skip 2nd rdata field dname */
856411c5950Sflorian 		while (rdata_len && *rdata != 0) {
857411c5950Sflorian 			uint8_t label_len;
858411c5950Sflorian 
859411c5950Sflorian 			if (*rdata & 0xC0)
860411c5950Sflorian 				return LDNS_WIREPARSE_ERR_OK;
861411c5950Sflorian 
862411c5950Sflorian 			label_len = *rdata + 1;
863411c5950Sflorian 			if (rdata_len < label_len)
864411c5950Sflorian 				return LDNS_WIREPARSE_ERR_OK;
865411c5950Sflorian 
866411c5950Sflorian 			rdata_len -= label_len;
867411c5950Sflorian 			rdata += label_len;
868411c5950Sflorian 		}
869411c5950Sflorian 		/* The root label is one more character, so smaller
870411c5950Sflorian 		 * than 1 + 1 means no Svcparam Keys */
871411c5950Sflorian 		if (rdata_len < 2 || *rdata != 0)
872411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
873411c5950Sflorian 
874411c5950Sflorian 		rdata_len -= 1;
875411c5950Sflorian 		rdata += 1;
876411c5950Sflorian 		return sldns_str2wire_check_svcbparams(rdata, rdata_len);
877411c5950Sflorian 
878411c5950Sflorian 	}
879ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
880ae8c6e27Sflorian }
881ae8c6e27Sflorian 
882ae8c6e27Sflorian /*
883ae8c6e27Sflorian  * trailing spaces are allowed
884ae8c6e27Sflorian  * leading spaces are not allowed
885ae8c6e27Sflorian  * allow ttl to be optional
886ae8c6e27Sflorian  * class is optional too
887ae8c6e27Sflorian  * if ttl is missing, and default_ttl is 0, use DEF_TTL
888ae8c6e27Sflorian  * allow ttl to be written as 1d3h
889ae8c6e27Sflorian  * So the RR should look like. e.g.
890ae8c6e27Sflorian  * miek.nl. 3600 IN MX 10 elektron.atoom.net
891ae8c6e27Sflorian  * or
892ae8c6e27Sflorian  * miek.nl. 1h IN MX 10 elektron.atoom.net
893ae8c6e27Sflorian  * or
894ae8c6e27Sflorian  * miek.nl. IN MX 10 elektron.atoom.net
895ae8c6e27Sflorian  */
896ae8c6e27Sflorian static int
sldns_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)897ae8c6e27Sflorian sldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len,
898ae8c6e27Sflorian 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
899ae8c6e27Sflorian 	size_t origin_len, uint8_t* prev, size_t prev_len, int question)
900ae8c6e27Sflorian {
901ae8c6e27Sflorian 	int status;
902ae8c6e27Sflorian 	int not_there = 0;
903ae8c6e27Sflorian 	char token[LDNS_MAX_RDFLEN+1];
904ae8c6e27Sflorian 	uint32_t ttl = 0;
905ae8c6e27Sflorian 	uint16_t tp = 0, cl = 0;
906ae8c6e27Sflorian 	size_t ddlen = 0;
907ae8c6e27Sflorian 
908ae8c6e27Sflorian 	/* string in buffer */
909ae8c6e27Sflorian 	sldns_buffer strbuf;
910ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
911ae8c6e27Sflorian 	if(!dname_len) dname_len = &ddlen;
912ae8c6e27Sflorian 
913ae8c6e27Sflorian 	/* parse the owner */
914ae8c6e27Sflorian 	if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin,
915ae8c6e27Sflorian 		origin_len, prev, prev_len, token, sizeof(token))) != 0)
916ae8c6e27Sflorian 		return status;
917ae8c6e27Sflorian 
918ae8c6e27Sflorian 	/* parse the [ttl] [class] <type> */
919ae8c6e27Sflorian 	if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token),
920ae8c6e27Sflorian 		&not_there, &ttl, default_ttl)) != 0)
921ae8c6e27Sflorian 		return status;
922ae8c6e27Sflorian 	if((status=rrinternal_get_class(&strbuf, token, sizeof(token),
923ae8c6e27Sflorian 		&not_there, &cl)) != 0)
924ae8c6e27Sflorian 		return status;
925ae8c6e27Sflorian 	if((status=rrinternal_get_type(&strbuf, token, sizeof(token),
926ae8c6e27Sflorian 		&not_there, &tp)) != 0)
927ae8c6e27Sflorian 		return status;
928ae8c6e27Sflorian 	/* put ttl, class, type into the rr result */
929ae8c6e27Sflorian 	if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl,
930ae8c6e27Sflorian 		ttl, question)) != 0)
931ae8c6e27Sflorian 		return status;
932ae8c6e27Sflorian 	/* for a question-RR we are done, no rdata */
933ae8c6e27Sflorian 	if(question) {
934ae8c6e27Sflorian 		*len = *dname_len + 4;
935ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
936ae8c6e27Sflorian 	}
937ae8c6e27Sflorian 
938ae8c6e27Sflorian 	/* rdata */
939ae8c6e27Sflorian 	if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token),
940ae8c6e27Sflorian 		rr, len, *dname_len, tp, origin, origin_len)) != 0)
941ae8c6e27Sflorian 		return status;
942ae8c6e27Sflorian 
943ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
944ae8c6e27Sflorian }
945ae8c6e27Sflorian 
sldns_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)946ae8c6e27Sflorian int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len,
947ae8c6e27Sflorian 	size_t* dname_len, uint32_t default_ttl, uint8_t* origin,
948ae8c6e27Sflorian 	size_t origin_len, uint8_t* prev, size_t prev_len)
949ae8c6e27Sflorian {
950ae8c6e27Sflorian 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
951ae8c6e27Sflorian 		default_ttl, origin, origin_len, prev, prev_len, 0);
952ae8c6e27Sflorian }
953ae8c6e27Sflorian 
sldns_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)954ae8c6e27Sflorian int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len,
955ae8c6e27Sflorian 	size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev,
956ae8c6e27Sflorian 	size_t prev_len)
957ae8c6e27Sflorian {
958ae8c6e27Sflorian 	return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len,
959ae8c6e27Sflorian 		0, origin, origin_len, prev, prev_len, 1);
960ae8c6e27Sflorian }
961ae8c6e27Sflorian 
sldns_wirerr_get_type(uint8_t * rr,size_t len,size_t dname_len)962ae8c6e27Sflorian uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len)
963ae8c6e27Sflorian {
964ae8c6e27Sflorian 	if(len < dname_len+2)
965ae8c6e27Sflorian 		return 0;
966ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len);
967ae8c6e27Sflorian }
968ae8c6e27Sflorian 
sldns_wirerr_get_class(uint8_t * rr,size_t len,size_t dname_len)969ae8c6e27Sflorian uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len)
970ae8c6e27Sflorian {
971ae8c6e27Sflorian 	if(len < dname_len+4)
972ae8c6e27Sflorian 		return 0;
973ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len+2);
974ae8c6e27Sflorian }
975ae8c6e27Sflorian 
sldns_wirerr_get_ttl(uint8_t * rr,size_t len,size_t dname_len)976ae8c6e27Sflorian uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len)
977ae8c6e27Sflorian {
978ae8c6e27Sflorian 	if(len < dname_len+8)
979ae8c6e27Sflorian 		return 0;
980ae8c6e27Sflorian 	return sldns_read_uint32(rr+dname_len+4);
981ae8c6e27Sflorian }
982ae8c6e27Sflorian 
sldns_wirerr_get_rdatalen(uint8_t * rr,size_t len,size_t dname_len)983ae8c6e27Sflorian uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len)
984ae8c6e27Sflorian {
985ae8c6e27Sflorian 	if(len < dname_len+10)
986ae8c6e27Sflorian 		return 0;
987ae8c6e27Sflorian 	return sldns_read_uint16(rr+dname_len+8);
988ae8c6e27Sflorian }
989ae8c6e27Sflorian 
sldns_wirerr_get_rdata(uint8_t * rr,size_t len,size_t dname_len)990ae8c6e27Sflorian uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len)
991ae8c6e27Sflorian {
992ae8c6e27Sflorian 	if(len < dname_len+10)
993ae8c6e27Sflorian 		return NULL;
994ae8c6e27Sflorian 	return rr+dname_len+10;
995ae8c6e27Sflorian }
996ae8c6e27Sflorian 
sldns_wirerr_get_rdatawl(uint8_t * rr,size_t len,size_t dname_len)997ae8c6e27Sflorian uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len)
998ae8c6e27Sflorian {
999ae8c6e27Sflorian 	if(len < dname_len+10)
1000ae8c6e27Sflorian 		return NULL;
1001ae8c6e27Sflorian 	return rr+dname_len+8;
1002ae8c6e27Sflorian }
1003ae8c6e27Sflorian 
sldns_get_errorstr_parse(int e)1004ae8c6e27Sflorian const char* sldns_get_errorstr_parse(int e)
1005ae8c6e27Sflorian {
1006ae8c6e27Sflorian 	sldns_lookup_table *lt;
1007ae8c6e27Sflorian 	lt = sldns_lookup_by_id(sldns_wireparse_errors, LDNS_WIREPARSE_ERROR(e));
1008ae8c6e27Sflorian 	return lt?lt->name:"unknown error";
1009ae8c6e27Sflorian }
1010ae8c6e27Sflorian 
1011ae8c6e27Sflorian /* Strip whitespace from the start and the end of <line>.  */
1012ae8c6e27Sflorian char *
sldns_strip_ws(char * line)1013ae8c6e27Sflorian sldns_strip_ws(char *line)
1014ae8c6e27Sflorian {
1015ae8c6e27Sflorian         char *s = line, *e;
1016ae8c6e27Sflorian 
1017ae8c6e27Sflorian         for (s = line; *s && isspace((unsigned char)*s); s++)
1018ae8c6e27Sflorian                 ;
1019ae8c6e27Sflorian         for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--)
1020ae8c6e27Sflorian                 ;
1021ae8c6e27Sflorian         *e = 0;
1022ae8c6e27Sflorian         return s;
1023ae8c6e27Sflorian }
1024ae8c6e27Sflorian 
sldns_fp2wire_rr_buf(FILE * in,uint8_t * rr,size_t * len,size_t * dname_len,struct sldns_file_parse_state * parse_state)1025ae8c6e27Sflorian int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len,
1026ae8c6e27Sflorian 	struct sldns_file_parse_state* parse_state)
1027ae8c6e27Sflorian {
1028ae8c6e27Sflorian 	char line[LDNS_RR_BUF_SIZE+1];
1029ae8c6e27Sflorian 	ssize_t size;
1030ae8c6e27Sflorian 
1031ae8c6e27Sflorian 	/* read an entire line in from the file */
1032ae8c6e27Sflorian 	if((size = sldns_fget_token_l(in, line, LDNS_PARSE_SKIP_SPACE,
1033ae8c6e27Sflorian 		LDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL))
1034ae8c6e27Sflorian 		== -1) {
1035ae8c6e27Sflorian 		/* if last line was empty, we are now at feof, which is not
1036ae8c6e27Sflorian 		 * always a parse error (happens when for instance last line
1037ae8c6e27Sflorian 		 * was a comment)
1038ae8c6e27Sflorian 		 */
1039ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX;
1040ae8c6e27Sflorian 	}
1041ae8c6e27Sflorian 
1042ae8c6e27Sflorian 	/* we can have the situation, where we've read ok, but still got
1043ae8c6e27Sflorian 	 * no bytes to play with, in this case size is 0 */
1044ae8c6e27Sflorian 	if(size == 0) {
1045ae8c6e27Sflorian 		if(*len > 0)
1046ae8c6e27Sflorian 			rr[0] = 0;
1047ae8c6e27Sflorian 		*len = 0;
1048ae8c6e27Sflorian 		*dname_len = 0;
1049ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1050ae8c6e27Sflorian 	}
1051ae8c6e27Sflorian 
1052ae8c6e27Sflorian 	if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) {
1053ae8c6e27Sflorian 		int s;
1054ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1055ae8c6e27Sflorian 		*len = 0;
1056ae8c6e27Sflorian 		*dname_len = 0;
1057ae8c6e27Sflorian 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1058ae8c6e27Sflorian 		parse_state->origin_len = sizeof(parse_state->origin);
1059ae8c6e27Sflorian 		s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8),
1060ae8c6e27Sflorian 			parse_state->origin, &parse_state->origin_len);
1061ae8c6e27Sflorian 		if(s) parse_state->origin_len = 0;
1062ae8c6e27Sflorian 		return s;
1063ae8c6e27Sflorian 	} else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) {
1064ae8c6e27Sflorian 		const char* end = NULL;
10657a05b9dfSflorian 		int overflow = 0;
1066ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1067ae8c6e27Sflorian 		*len = 0;
1068ae8c6e27Sflorian 		*dname_len = 0;
1069ae8c6e27Sflorian 		if(!parse_state) return LDNS_WIREPARSE_ERR_OK;
1070ae8c6e27Sflorian 		parse_state->default_ttl = sldns_str2period(
10717a05b9dfSflorian 			sldns_strip_ws(line+5), &end, &overflow);
10727a05b9dfSflorian 		if(overflow)
10737a05b9dfSflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1074ae8c6e27Sflorian 	} else if (strncmp(line, "$INCLUDE", 8) == 0) {
1075ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1076ae8c6e27Sflorian 		*len = 0;
1077ae8c6e27Sflorian 		*dname_len = 0;
1078ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INCLUDE;
1079ae8c6e27Sflorian 	} else if (strncmp(line, "$", 1) == 0) {
1080ae8c6e27Sflorian 		strlcpy((char*)rr, line, *len);
1081ae8c6e27Sflorian 		*len = 0;
1082ae8c6e27Sflorian 		*dname_len = 0;
1083ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INCLUDE;
1084ae8c6e27Sflorian 	} else {
1085ae8c6e27Sflorian 		int r = sldns_str2wire_rr_buf(line, rr, len, dname_len,
1086ae8c6e27Sflorian 			parse_state?parse_state->default_ttl:0,
1087ae8c6e27Sflorian 			(parse_state&&parse_state->origin_len)?
1088ae8c6e27Sflorian 				parse_state->origin:NULL,
1089ae8c6e27Sflorian 			parse_state?parse_state->origin_len:0,
1090ae8c6e27Sflorian 			(parse_state&&parse_state->prev_rr_len)?
1091ae8c6e27Sflorian 				parse_state->prev_rr:NULL,
1092ae8c6e27Sflorian 			parse_state?parse_state->prev_rr_len:0);
1093ae8c6e27Sflorian 		if(r == LDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 &&
1094ae8c6e27Sflorian 			parse_state &&
1095ae8c6e27Sflorian 			(*dname_len) <= sizeof(parse_state->prev_rr)) {
1096ae8c6e27Sflorian 			memmove(parse_state->prev_rr, rr, *dname_len);
1097ae8c6e27Sflorian 			parse_state->prev_rr_len = (*dname_len);
1098ae8c6e27Sflorian 		}
1099411c5950Sflorian 		if(r == LDNS_WIREPARSE_ERR_OK && parse_state) {
1100411c5950Sflorian 			parse_state->default_ttl = sldns_wirerr_get_ttl(
1101411c5950Sflorian 				rr, *len, *dname_len);
1102411c5950Sflorian 		}
1103ae8c6e27Sflorian 		return r;
1104ae8c6e27Sflorian 	}
1105ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1106ae8c6e27Sflorian }
1107ae8c6e27Sflorian 
1108411c5950Sflorian static int
sldns_str2wire_svcparam_key_lookup(const char * key,size_t key_len)1109411c5950Sflorian sldns_str2wire_svcparam_key_lookup(const char *key, size_t key_len)
1110411c5950Sflorian {
1111411c5950Sflorian 	char buf[64];
1112411c5950Sflorian 	char *endptr;
1113411c5950Sflorian 	unsigned long int key_value;
1114411c5950Sflorian 
1115411c5950Sflorian 	if (key_len >= 4  && key_len <= 8 && !strncmp(key, "key", 3)) {
1116411c5950Sflorian 		memcpy(buf, key + 3, key_len - 3);
1117411c5950Sflorian 		buf[key_len - 3] = 0;
1118411c5950Sflorian 		key_value = strtoul(buf, &endptr, 10);
1119411c5950Sflorian 
1120411c5950Sflorian 		if (endptr > buf	/* digits seen */
1121411c5950Sflorian 		&& *endptr == 0		/* no non-digit chars after digits */
1122411c5950Sflorian 		&&  key_value <= 65535)	/* no overflow */
1123411c5950Sflorian 			return key_value;
1124411c5950Sflorian 
1125411c5950Sflorian 	} else switch (key_len) {
1126d500c338Sflorian 	case 3:
1127d500c338Sflorian 		if (!strncmp(key, "ech", key_len))
1128d500c338Sflorian 			return SVCB_KEY_ECH;
1129411c5950Sflorian 		break;
1130411c5950Sflorian 
1131d500c338Sflorian 	case 4:
1132d500c338Sflorian 		if (!strncmp(key, "alpn", key_len))
1133411c5950Sflorian 			return SVCB_KEY_ALPN;
1134d500c338Sflorian 		if (!strncmp(key, "port", key_len))
1135411c5950Sflorian 			return SVCB_KEY_PORT;
1136411c5950Sflorian 		break;
1137411c5950Sflorian 
1138d500c338Sflorian 	case 7:
1139d500c338Sflorian 		if (!strncmp(key, "dohpath", key_len))
1140d500c338Sflorian 			return SVCB_KEY_DOHPATH;
1141411c5950Sflorian 		break;
1142411c5950Sflorian 
1143d500c338Sflorian 	case 8:
1144d500c338Sflorian 		if (!strncmp(key, "ipv4hint", key_len))
1145411c5950Sflorian 			return SVCB_KEY_IPV4HINT;
1146d500c338Sflorian 		if (!strncmp(key, "ipv6hint", key_len))
1147411c5950Sflorian 			return SVCB_KEY_IPV6HINT;
1148411c5950Sflorian 		break;
1149411c5950Sflorian 
1150d500c338Sflorian 	case 9:
1151d500c338Sflorian 		if (!strncmp(key, "mandatory", key_len))
1152d500c338Sflorian 			return SVCB_KEY_MANDATORY;
1153d500c338Sflorian 		if (!strncmp(key, "echconfig", key_len))
1154d500c338Sflorian 			return SVCB_KEY_ECH; /* allow "echconfig" as well as "ech" */
1155d500c338Sflorian 		break;
1156d500c338Sflorian 
1157d500c338Sflorian 	case 15:
1158d500c338Sflorian 		if (!strncmp(key, "no-default-alpn", key_len))
1159d500c338Sflorian 			return SVCB_KEY_NO_DEFAULT_ALPN;
1160411c5950Sflorian 		break;
1161411c5950Sflorian 
1162411c5950Sflorian 	default:
1163411c5950Sflorian 		break;
1164411c5950Sflorian 	}
1165411c5950Sflorian 
1166411c5950Sflorian 	/* Although the returned value might be used by the caller,
1167411c5950Sflorian 	 * the parser has erred, so the zone will not be loaded.
1168411c5950Sflorian 	 */
1169411c5950Sflorian 	return -1;
1170411c5950Sflorian }
1171411c5950Sflorian 
1172411c5950Sflorian static int
sldns_str2wire_svcparam_port(const char * val,uint8_t * rd,size_t * rd_len)1173411c5950Sflorian sldns_str2wire_svcparam_port(const char* val, uint8_t* rd, size_t* rd_len)
1174411c5950Sflorian {
1175411c5950Sflorian 	unsigned long int port;
1176411c5950Sflorian 	char *endptr;
1177411c5950Sflorian 
1178411c5950Sflorian 	if (*rd_len < 6)
1179411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1180411c5950Sflorian 
1181411c5950Sflorian 	port = strtoul(val, &endptr, 10);
1182411c5950Sflorian 
1183411c5950Sflorian 	if (endptr > val	/* digits seen */
1184411c5950Sflorian 	&& *endptr == 0		/* no non-digit chars after digits */
1185411c5950Sflorian 	&&  port <= 65535) {	/* no overflow */
1186411c5950Sflorian 
1187411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_PORT);
1188411c5950Sflorian 		sldns_write_uint16(rd + 2, sizeof(uint16_t));
1189411c5950Sflorian 		sldns_write_uint16(rd + 4, port);
1190411c5950Sflorian 		*rd_len = 6;
1191411c5950Sflorian 
1192411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1193411c5950Sflorian 	}
1194411c5950Sflorian 
1195411c5950Sflorian 	return LDNS_WIREPARSE_ERR_SVCB_PORT_VALUE_SYNTAX;
1196411c5950Sflorian }
1197411c5950Sflorian 
1198411c5950Sflorian static int
sldns_str2wire_svcbparam_ipv4hint(const char * val,uint8_t * rd,size_t * rd_len)1199411c5950Sflorian sldns_str2wire_svcbparam_ipv4hint(const char* val, uint8_t* rd, size_t* rd_len)
1200411c5950Sflorian {
1201411c5950Sflorian 	size_t count;
1202411c5950Sflorian 	char ip_str[INET_ADDRSTRLEN+1];
1203411c5950Sflorian 	char *next_ip_str;
1204411c5950Sflorian 	size_t i;
1205411c5950Sflorian 
1206411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1207411c5950Sflorian 		if (val[i] == ',')
1208411c5950Sflorian 			count += 1;
1209411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1210411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_IPV4_TOO_MANY_ADDRESSES;
1211411c5950Sflorian 		}
1212411c5950Sflorian 	}
1213411c5950Sflorian 
1214411c5950Sflorian 	if (*rd_len < (LDNS_IP4ADDRLEN * count) + 4)
1215411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1216411c5950Sflorian 
1217411c5950Sflorian 	/* count is number of comma's in val + 1; so the actual number of IPv4
1218411c5950Sflorian 	 * addresses in val
1219411c5950Sflorian 	 */
1220411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_IPV4HINT);
1221411c5950Sflorian 	sldns_write_uint16(rd + 2, LDNS_IP4ADDRLEN * count);
1222411c5950Sflorian 	*rd_len = 4;
1223411c5950Sflorian 
1224411c5950Sflorian 	while (count) {
1225411c5950Sflorian 		if (!(next_ip_str = strchr(val, ','))) {
1226411c5950Sflorian 			if (inet_pton(AF_INET, val, rd + *rd_len) != 1)
1227411c5950Sflorian 				break;
1228411c5950Sflorian 			*rd_len += LDNS_IP4ADDRLEN;
1229411c5950Sflorian 
1230411c5950Sflorian 			assert(count == 1);
1231411c5950Sflorian 
1232411c5950Sflorian 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1233411c5950Sflorian 			break;
1234411c5950Sflorian 
1235411c5950Sflorian 		else {
1236411c5950Sflorian 			memcpy(ip_str, val, next_ip_str - val);
1237411c5950Sflorian 			ip_str[next_ip_str - val] = 0;
1238411c5950Sflorian 			if (inet_pton(AF_INET, ip_str, rd + *rd_len) != 1) {
1239411c5950Sflorian 				break;
1240411c5950Sflorian 			}
1241411c5950Sflorian 			*rd_len += LDNS_IP4ADDRLEN;
1242411c5950Sflorian 
1243411c5950Sflorian 			val = next_ip_str + 1;
1244411c5950Sflorian 		}
1245411c5950Sflorian 		count--;
1246411c5950Sflorian 	}
1247411c5950Sflorian 	if (count) /* verify that we parsed all values */
1248411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1249411c5950Sflorian 
1250411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1251411c5950Sflorian }
1252411c5950Sflorian 
1253411c5950Sflorian static int
sldns_str2wire_svcbparam_ipv6hint(const char * val,uint8_t * rd,size_t * rd_len)1254411c5950Sflorian sldns_str2wire_svcbparam_ipv6hint(const char* val, uint8_t* rd, size_t* rd_len)
1255411c5950Sflorian {
1256411c5950Sflorian 	size_t count;
1257411c5950Sflorian 	char ip_str[INET6_ADDRSTRLEN+1];
1258411c5950Sflorian 	char *next_ip_str;
1259411c5950Sflorian 	size_t i;
1260411c5950Sflorian 
1261411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1262411c5950Sflorian 		if (val[i] == ',')
1263411c5950Sflorian 			count += 1;
1264411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1265411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_IPV6_TOO_MANY_ADDRESSES;
1266411c5950Sflorian 		}
1267411c5950Sflorian 	}
1268411c5950Sflorian 
1269411c5950Sflorian 	if (*rd_len < (LDNS_IP6ADDRLEN * count) + 4)
1270411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1271411c5950Sflorian 
1272411c5950Sflorian 	/* count is number of comma's in val + 1; so the actual number of IPv6
1273411c5950Sflorian 	 * addresses in val
1274411c5950Sflorian 	 */
1275411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_IPV6HINT);
1276411c5950Sflorian 	sldns_write_uint16(rd + 2, LDNS_IP6ADDRLEN * count);
1277411c5950Sflorian 	*rd_len = 4;
1278411c5950Sflorian 
1279411c5950Sflorian 	while (count) {
1280411c5950Sflorian 		if (!(next_ip_str = strchr(val, ','))) {
1281411c5950Sflorian 			if (inet_pton(AF_INET6, val, rd + *rd_len) != 1)
1282411c5950Sflorian 				break;
1283411c5950Sflorian 			*rd_len += LDNS_IP6ADDRLEN;
1284411c5950Sflorian 
1285411c5950Sflorian 			assert(count == 1);
1286411c5950Sflorian 
1287411c5950Sflorian 		} else if (next_ip_str - val >= (int)sizeof(ip_str))
1288411c5950Sflorian 			break;
1289411c5950Sflorian 
1290411c5950Sflorian 		else {
1291411c5950Sflorian 			memcpy(ip_str, val, next_ip_str - val);
1292411c5950Sflorian 			ip_str[next_ip_str - val] = 0;
1293411c5950Sflorian 			if (inet_pton(AF_INET6, ip_str, rd + *rd_len) != 1) {
1294411c5950Sflorian 				break;
1295411c5950Sflorian 			}
1296411c5950Sflorian 			*rd_len += LDNS_IP6ADDRLEN;
1297411c5950Sflorian 
1298411c5950Sflorian 			val = next_ip_str + 1;
1299411c5950Sflorian 		}
1300411c5950Sflorian 		count--;
1301411c5950Sflorian 	}
1302411c5950Sflorian 	if (count) /* verify that we parsed all values */
1303411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1304411c5950Sflorian 
1305411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1306411c5950Sflorian }
1307411c5950Sflorian 
1308411c5950Sflorian /* compare function used for sorting uint16_t's */
1309411c5950Sflorian static int
sldns_network_uint16_cmp(const void * a,const void * b)1310411c5950Sflorian sldns_network_uint16_cmp(const void *a, const void *b)
1311411c5950Sflorian {
1312411c5950Sflorian 	return ((int)sldns_read_uint16(a)) - ((int)sldns_read_uint16(b));
1313411c5950Sflorian }
1314411c5950Sflorian 
1315411c5950Sflorian static int
sldns_str2wire_svcbparam_mandatory(const char * val,uint8_t * rd,size_t * rd_len)1316411c5950Sflorian sldns_str2wire_svcbparam_mandatory(const char* val, uint8_t* rd, size_t* rd_len)
1317411c5950Sflorian {
1318411c5950Sflorian 	size_t i, count, val_len;
1319411c5950Sflorian 	char* next_key;
1320411c5950Sflorian 
1321411c5950Sflorian 	val_len = strlen(val);
1322411c5950Sflorian 
1323411c5950Sflorian 	for (i = 0, count = 1; val[i]; i++) {
1324411c5950Sflorian 		if (val[i] == ',')
1325411c5950Sflorian 			count += 1;
1326411c5950Sflorian 		if (count > SVCB_MAX_COMMA_SEPARATED_VALUES) {
1327411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_TOO_MANY_KEYS;
1328411c5950Sflorian 		}
1329411c5950Sflorian 	}
1330411c5950Sflorian 	if (sizeof(uint16_t) * (count + 2) > *rd_len)
1331411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1332411c5950Sflorian 
1333411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_MANDATORY);
1334411c5950Sflorian 	sldns_write_uint16(rd + 2, sizeof(uint16_t) * count);
1335411c5950Sflorian 	*rd_len = 4;
1336411c5950Sflorian 
1337411c5950Sflorian 	while (1) {
1338411c5950Sflorian 		int svcparamkey;
1339411c5950Sflorian 
1340411c5950Sflorian 		if (!(next_key = strchr(val, ','))) {
1341411c5950Sflorian 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, val_len);
1342411c5950Sflorian 
1343411c5950Sflorian 			if (svcparamkey < 0) {
1344411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1345411c5950Sflorian 			}
1346411c5950Sflorian 
1347411c5950Sflorian 			sldns_write_uint16(rd + *rd_len, svcparamkey);
1348411c5950Sflorian 			*rd_len += 2;
1349411c5950Sflorian 			break;
1350411c5950Sflorian 		} else {
1351411c5950Sflorian 			svcparamkey = sldns_str2wire_svcparam_key_lookup(val, next_key - val);
1352411c5950Sflorian 
1353411c5950Sflorian 			if (svcparamkey < 0) {
1354411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1355411c5950Sflorian 			}
1356411c5950Sflorian 
1357411c5950Sflorian 			sldns_write_uint16(rd + *rd_len,
1358411c5950Sflorian 				svcparamkey);
1359411c5950Sflorian 			*rd_len += 2;
1360411c5950Sflorian 		}
1361411c5950Sflorian 
1362411c5950Sflorian 		val_len -= next_key - val + 1;
1363411c5950Sflorian 		val = next_key + 1; /* skip the comma */
1364411c5950Sflorian 	}
1365411c5950Sflorian 
1366411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 7:
1367411c5950Sflorian 	 *
1368411c5950Sflorian 	 *    "In wire format, the keys are represented by their numeric
1369411c5950Sflorian 	 *     values in network byte order, concatenated in ascending order."
1370411c5950Sflorian 	 */
1371411c5950Sflorian 	qsort((void *)(rd + 4), count, sizeof(uint16_t), sldns_network_uint16_cmp);
1372411c5950Sflorian 
13737a05b9dfSflorian 	/* The code below revolves around semantic errors in the SVCParam set.
1374411c5950Sflorian 	 * So long as we do not distinguish between running Unbound as a primary
1375411c5950Sflorian 	 * or as a secondary, we default to secondary behavior and we ignore the
1376411c5950Sflorian 	 * semantic errors. */
1377411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1378411c5950Sflorian 	/* In draft-ietf-dnsop-svcb-https-06 Section 8
1379411c5950Sflorian 	 * automatically mandatory MUST NOT appear in its own value-list
1380411c5950Sflorian 	 */
1381411c5950Sflorian 	if (sldns_read_uint16(rd + 4) == SVCB_KEY_MANDATORY)
1382411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_IN_MANDATORY;
1383411c5950Sflorian 
1384411c5950Sflorian 	/* Guarantee key uniqueness. After the sort we only need to
1385411c5950Sflorian 	 * compare neighbouring keys */
1386411c5950Sflorian 	if (count > 1) {
1387411c5950Sflorian 		for (i = 0; i < count - 1; i++) {
1388411c5950Sflorian 			uint8_t* current_pos = (rd + 4 + (sizeof(uint16_t) * i));
1389411c5950Sflorian 			uint16_t key = sldns_read_uint16(current_pos);
1390411c5950Sflorian 
1391411c5950Sflorian 			if (key == sldns_read_uint16(current_pos + 2)) {
1392411c5950Sflorian 				return LDNS_WIREPARSE_ERR_SVCB_MANDATORY_DUPLICATE_KEY;
1393411c5950Sflorian 			}
1394411c5950Sflorian 		}
1395411c5950Sflorian 	}
1396411c5950Sflorian #endif
1397411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1398411c5950Sflorian }
1399411c5950Sflorian 
1400411c5950Sflorian static int
sldns_str2wire_svcbparam_ech_value(const char * val,uint8_t * rd,size_t * rd_len)1401411c5950Sflorian sldns_str2wire_svcbparam_ech_value(const char* val, uint8_t* rd, size_t* rd_len)
1402411c5950Sflorian {
1403411c5950Sflorian 	uint8_t buffer[LDNS_MAX_RDFLEN];
1404411c5950Sflorian 	int wire_len;
1405411c5950Sflorian 
1406411c5950Sflorian 	/* single 0 represents empty buffer */
1407411c5950Sflorian 	if(strcmp(val, "0") == 0) {
1408411c5950Sflorian 		if (*rd_len < 4)
1409411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1410411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1411411c5950Sflorian 		sldns_write_uint16(rd + 2, 0);
1412411c5950Sflorian 
1413411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1414411c5950Sflorian 	}
1415411c5950Sflorian 
1416411c5950Sflorian 	wire_len = sldns_b64_pton(val, buffer, LDNS_MAX_RDFLEN);
1417411c5950Sflorian 
1418411c5950Sflorian 	if (wire_len <= 0) {
1419411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1420411c5950Sflorian 	} else if ((unsigned)wire_len + 4 > *rd_len) {
1421411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1422411c5950Sflorian 	} else {
1423411c5950Sflorian 		sldns_write_uint16(rd, SVCB_KEY_ECH);
1424411c5950Sflorian 		sldns_write_uint16(rd + 2, wire_len);
1425411c5950Sflorian 		memcpy(rd + 4, buffer, wire_len);
1426411c5950Sflorian 		*rd_len = 4 + wire_len;
1427411c5950Sflorian 
1428411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1429411c5950Sflorian 	}
1430411c5950Sflorian }
1431411c5950Sflorian 
1432411c5950Sflorian static const char*
sldns_str2wire_svcbparam_parse_next_unescaped_comma(const char * val)1433411c5950Sflorian sldns_str2wire_svcbparam_parse_next_unescaped_comma(const char *val)
1434411c5950Sflorian {
1435411c5950Sflorian 	while (*val) {
1436411c5950Sflorian 		/* Only return when the comma is not escaped*/
1437411c5950Sflorian 		if (*val == '\\'){
1438411c5950Sflorian 			++val;
1439411c5950Sflorian 			if (!*val)
1440411c5950Sflorian 				break;
1441411c5950Sflorian 		} else if (*val == ',')
1442411c5950Sflorian 				return val;
1443411c5950Sflorian 
1444411c5950Sflorian 		val++;
1445411c5950Sflorian 	}
1446411c5950Sflorian 	return NULL;
1447411c5950Sflorian }
1448411c5950Sflorian 
1449411c5950Sflorian /* The source is already properly unescaped, this double unescaping is purely to allow for
1450a1a7ba80Sflorian  * comma's in comma separated alpn lists.
1451411c5950Sflorian  *
1452411c5950Sflorian  * In draft-ietf-dnsop-svcb-https-06 Section 7:
1453411c5950Sflorian  * To enable simpler parsing, this SvcParamValue MUST NOT contain escape sequences.
1454411c5950Sflorian  */
1455411c5950Sflorian static size_t
sldns_str2wire_svcbparam_parse_copy_unescaped(uint8_t * dst,const char * src,size_t len)1456411c5950Sflorian sldns_str2wire_svcbparam_parse_copy_unescaped(uint8_t *dst,
1457411c5950Sflorian 	const char *src, size_t len)
1458411c5950Sflorian {
1459411c5950Sflorian 	uint8_t *orig_dst = dst;
1460411c5950Sflorian 
1461411c5950Sflorian 	while (len) {
1462411c5950Sflorian 		if (*src == '\\') {
1463411c5950Sflorian 			src++;
1464411c5950Sflorian 			len--;
1465411c5950Sflorian 			if (!len)
1466411c5950Sflorian 				break;
1467411c5950Sflorian 		}
1468411c5950Sflorian 		*dst++ = *src++;
1469411c5950Sflorian 		len--;
1470411c5950Sflorian 	}
1471411c5950Sflorian 	return (size_t)(dst - orig_dst);
1472411c5950Sflorian }
1473411c5950Sflorian 
1474411c5950Sflorian static int
sldns_str2wire_svcbparam_alpn_value(const char * val,uint8_t * rd,size_t * rd_len)1475411c5950Sflorian sldns_str2wire_svcbparam_alpn_value(const char* val,
1476411c5950Sflorian 	uint8_t* rd, size_t* rd_len)
1477411c5950Sflorian {
1478411c5950Sflorian 	uint8_t     unescaped_dst[LDNS_MAX_RDFLEN];
1479411c5950Sflorian 	uint8_t    *dst = unescaped_dst;
1480411c5950Sflorian 	const char *next_str;
1481411c5950Sflorian 	size_t      str_len;
1482411c5950Sflorian 	size_t      dst_len;
1483411c5950Sflorian 	size_t      val_len;
1484411c5950Sflorian 
1485411c5950Sflorian 	val_len = strlen(val);
1486411c5950Sflorian 
1487411c5950Sflorian 	if (val_len > sizeof(unescaped_dst)) {
1488411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1489411c5950Sflorian 	}
1490411c5950Sflorian 	while (val_len) {
1491411c5950Sflorian 		size_t key_len;
1492411c5950Sflorian 
1493411c5950Sflorian 		str_len = (next_str = sldns_str2wire_svcbparam_parse_next_unescaped_comma(val))
1494411c5950Sflorian 		        ? (size_t)(next_str - val) : val_len;
1495411c5950Sflorian 
1496411c5950Sflorian 		if (str_len > 255) {
1497411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_ALPN_KEY_TOO_LARGE;
1498411c5950Sflorian 		}
1499411c5950Sflorian 
1500411c5950Sflorian 		key_len = sldns_str2wire_svcbparam_parse_copy_unescaped(dst + 1, val, str_len);
1501411c5950Sflorian 		*dst++ = key_len;
1502411c5950Sflorian 		 dst  += key_len;
1503411c5950Sflorian 
1504411c5950Sflorian 		if (!next_str)
1505411c5950Sflorian 			break;
1506411c5950Sflorian 
1507411c5950Sflorian 		/* skip the comma in the next iteration */
1508411c5950Sflorian 		val_len -= next_str - val + 1;
1509411c5950Sflorian 		val = next_str + 1;
1510411c5950Sflorian 	}
1511411c5950Sflorian 	dst_len = dst - unescaped_dst;
1512411c5950Sflorian 	if (*rd_len < 4 + dst_len)
1513411c5950Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1514411c5950Sflorian 	sldns_write_uint16(rd, SVCB_KEY_ALPN);
1515411c5950Sflorian 	sldns_write_uint16(rd + 2, dst_len);
1516411c5950Sflorian 	memcpy(rd + 4, unescaped_dst, dst_len);
1517411c5950Sflorian 	*rd_len = 4 + dst_len;
1518411c5950Sflorian 
1519411c5950Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1520411c5950Sflorian }
1521411c5950Sflorian 
1522411c5950Sflorian static int
sldns_str2wire_svcbparam_dohpath_value(const char * val,uint8_t * rd,size_t * rd_len)1523d500c338Sflorian sldns_str2wire_svcbparam_dohpath_value(const char* val,
1524d500c338Sflorian 	uint8_t* rd, size_t* rd_len)
1525d500c338Sflorian {
1526d500c338Sflorian 	size_t val_len;
1527d500c338Sflorian 
1528d500c338Sflorian 	/* RFC6570#section-2.1
1529d500c338Sflorian 	 * "The characters outside of expressions in a URI Template string are
1530d500c338Sflorian 	 * intended to be copied literally"
1531d500c338Sflorian 	 * Practically this means we do not have to look for "double escapes"
1532d500c338Sflorian 	 * like in the alpn value list.
1533d500c338Sflorian 	 */
1534d500c338Sflorian 
1535d500c338Sflorian 	val_len = strlen(val);
1536d500c338Sflorian 
1537d500c338Sflorian 	if (*rd_len < 4 + val_len) {
1538d500c338Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1539d500c338Sflorian 	}
1540d500c338Sflorian 
1541d500c338Sflorian 	sldns_write_uint16(rd, SVCB_KEY_DOHPATH);
1542d500c338Sflorian 	sldns_write_uint16(rd + 2, val_len);
1543d500c338Sflorian 	memcpy(rd + 4, val, val_len);
1544d500c338Sflorian 	*rd_len = 4 + val_len;
1545d500c338Sflorian 
1546d500c338Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1547d500c338Sflorian }
1548d500c338Sflorian 
1549d500c338Sflorian static int
sldns_str2wire_svcparam_value(const char * key,size_t key_len,const char * val,uint8_t * rd,size_t * rd_len)1550411c5950Sflorian sldns_str2wire_svcparam_value(const char *key, size_t key_len,
1551411c5950Sflorian 	const char *val, uint8_t* rd, size_t* rd_len)
1552411c5950Sflorian {
1553411c5950Sflorian 	size_t str_len;
1554411c5950Sflorian 	int svcparamkey = sldns_str2wire_svcparam_key_lookup(key, key_len);
1555411c5950Sflorian 
1556411c5950Sflorian 	if (svcparamkey < 0) {
1557411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_UNKNOWN_KEY;
1558411c5950Sflorian 	}
1559411c5950Sflorian 
1560411c5950Sflorian 	/* key without value */
1561411c5950Sflorian 	if (val == NULL) {
1562411c5950Sflorian 		switch (svcparamkey) {
1563411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1564411c5950Sflorian 		case SVCB_KEY_MANDATORY:
1565411c5950Sflorian 		case SVCB_KEY_ALPN:
1566411c5950Sflorian 		case SVCB_KEY_PORT:
1567411c5950Sflorian 		case SVCB_KEY_IPV4HINT:
1568411c5950Sflorian 		case SVCB_KEY_IPV6HINT:
1569d500c338Sflorian 		case SVCB_KEY_DOHPATH:
1570411c5950Sflorian 			return LDNS_WIREPARSE_ERR_SVCB_MISSING_PARAM;
1571411c5950Sflorian #endif
1572411c5950Sflorian 		default:
1573411c5950Sflorian 			if (*rd_len < 4)
1574411c5950Sflorian 				return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1575411c5950Sflorian 			sldns_write_uint16(rd, svcparamkey);
1576411c5950Sflorian 			sldns_write_uint16(rd + 2, 0);
1577411c5950Sflorian 			*rd_len = 4;
1578411c5950Sflorian 
1579411c5950Sflorian 			return LDNS_WIREPARSE_ERR_OK;
1580411c5950Sflorian 		}
1581411c5950Sflorian 	}
1582411c5950Sflorian 
1583411c5950Sflorian 	/* value is non-empty */
1584411c5950Sflorian 	switch (svcparamkey) {
1585411c5950Sflorian 	case SVCB_KEY_PORT:
1586411c5950Sflorian 		return sldns_str2wire_svcparam_port(val, rd, rd_len);
1587411c5950Sflorian 	case SVCB_KEY_IPV4HINT:
1588411c5950Sflorian 		return sldns_str2wire_svcbparam_ipv4hint(val, rd, rd_len);
1589411c5950Sflorian 	case SVCB_KEY_IPV6HINT:
1590411c5950Sflorian 		return sldns_str2wire_svcbparam_ipv6hint(val, rd, rd_len);
1591411c5950Sflorian 	case SVCB_KEY_MANDATORY:
1592411c5950Sflorian 		return sldns_str2wire_svcbparam_mandatory(val, rd, rd_len);
1593411c5950Sflorian #ifdef SVCB_SEMANTIC_ERRORS
1594411c5950Sflorian 	case SVCB_KEY_NO_DEFAULT_ALPN:
1595411c5950Sflorian 		return LDNS_WIREPARSE_ERR_SVCB_NO_DEFAULT_ALPN_VALUE;
1596411c5950Sflorian #endif
1597411c5950Sflorian 	case SVCB_KEY_ECH:
1598411c5950Sflorian 		return sldns_str2wire_svcbparam_ech_value(val, rd, rd_len);
1599411c5950Sflorian 	case SVCB_KEY_ALPN:
1600411c5950Sflorian 		return sldns_str2wire_svcbparam_alpn_value(val, rd, rd_len);
1601d500c338Sflorian 	case SVCB_KEY_DOHPATH:
1602d500c338Sflorian 		return sldns_str2wire_svcbparam_dohpath_value(val, rd, rd_len);
1603411c5950Sflorian 	default:
1604411c5950Sflorian 		str_len = strlen(val);
1605411c5950Sflorian 		if (*rd_len < 4 + str_len)
1606411c5950Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1607411c5950Sflorian 		sldns_write_uint16(rd, svcparamkey);
1608411c5950Sflorian 		sldns_write_uint16(rd + 2, str_len);
1609411c5950Sflorian 		memcpy(rd + 4, val, str_len);
1610411c5950Sflorian 		*rd_len = 4 + str_len;
1611411c5950Sflorian 
1612411c5950Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1613411c5950Sflorian 	}
1614411c5950Sflorian 
1615411c5950Sflorian 	return LDNS_WIREPARSE_ERR_GENERAL;
1616411c5950Sflorian }
1617411c5950Sflorian 
sldns_str2wire_svcparam_buf(const char * str,uint8_t * rd,size_t * rd_len)16186d5a1597Sflorian static int sldns_str2wire_svcparam_buf(const char* str, uint8_t* rd, size_t* rd_len)
1619411c5950Sflorian {
1620411c5950Sflorian 	const char* eq_pos;
1621411c5950Sflorian 	char unescaped_val[LDNS_MAX_RDFLEN];
1622411c5950Sflorian 	char* val_out = unescaped_val;
1623411c5950Sflorian 	const char* val_in;
1624411c5950Sflorian 
1625411c5950Sflorian 	eq_pos = strchr(str, '=');
1626411c5950Sflorian 
1627411c5950Sflorian 	/* case: key=value */
1628411c5950Sflorian 	if (eq_pos != NULL && eq_pos[1]) {
1629411c5950Sflorian 		val_in = eq_pos + 1;
1630411c5950Sflorian 
1631411c5950Sflorian 		/* unescape characters and "" blocks */
1632411c5950Sflorian 		if (*val_in == '"') {
1633411c5950Sflorian 			val_in++;
1634411c5950Sflorian 			while (*val_in != '"'
16357a05b9dfSflorian 			&& (size_t)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1636411c5950Sflorian 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1637411c5950Sflorian 				val_out++;
1638411c5950Sflorian 			}
1639411c5950Sflorian 		} else {
16407a05b9dfSflorian 			while ((size_t)(val_out - unescaped_val + 1) < sizeof(unescaped_val)
1641411c5950Sflorian 			&& sldns_parse_char( (uint8_t*) val_out, &val_in)) {
1642411c5950Sflorian 				val_out++;
1643411c5950Sflorian 			}
1644411c5950Sflorian 		}
1645411c5950Sflorian 		*val_out = 0;
1646411c5950Sflorian 
1647411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, eq_pos - str,
1648411c5950Sflorian 		        unescaped_val[0] ? unescaped_val : NULL, rd, rd_len);
1649411c5950Sflorian 	}
1650411c5950Sflorian 	/* case: key= */
1651411c5950Sflorian 	else if (eq_pos != NULL && !(eq_pos[1])) {
1652411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, eq_pos - str, NULL, rd, rd_len);
1653411c5950Sflorian 	}
1654411c5950Sflorian 	/* case: key */
1655411c5950Sflorian 	else {
1656411c5950Sflorian 		return sldns_str2wire_svcparam_value(str, strlen(str), NULL, rd, rd_len);
1657411c5950Sflorian 	}
1658411c5950Sflorian }
1659411c5950Sflorian 
sldns_str2wire_rdf_buf(const char * str,uint8_t * rd,size_t * len,sldns_rdf_type rdftype)1660ae8c6e27Sflorian int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len,
1661ae8c6e27Sflorian 	sldns_rdf_type rdftype)
1662ae8c6e27Sflorian {
1663ae8c6e27Sflorian 	switch (rdftype) {
1664ae8c6e27Sflorian 	case LDNS_RDF_TYPE_DNAME:
1665ae8c6e27Sflorian 		return sldns_str2wire_dname_buf(str, rd, len);
1666ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT8:
1667ae8c6e27Sflorian 		return sldns_str2wire_int8_buf(str, rd, len);
1668ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT16:
1669ae8c6e27Sflorian 		return sldns_str2wire_int16_buf(str, rd, len);
1670ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT32:
1671ae8c6e27Sflorian 		return sldns_str2wire_int32_buf(str, rd, len);
1672ae8c6e27Sflorian 	case LDNS_RDF_TYPE_A:
1673ae8c6e27Sflorian 		return sldns_str2wire_a_buf(str, rd, len);
1674ae8c6e27Sflorian 	case LDNS_RDF_TYPE_AAAA:
1675ae8c6e27Sflorian 		return sldns_str2wire_aaaa_buf(str, rd, len);
1676ae8c6e27Sflorian 	case LDNS_RDF_TYPE_STR:
1677ae8c6e27Sflorian 		return sldns_str2wire_str_buf(str, rd, len);
1678ae8c6e27Sflorian 	case LDNS_RDF_TYPE_APL:
1679ae8c6e27Sflorian 		return sldns_str2wire_apl_buf(str, rd, len);
1680ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B64:
1681ae8c6e27Sflorian 		return sldns_str2wire_b64_buf(str, rd, len);
1682ae8c6e27Sflorian 	case LDNS_RDF_TYPE_B32_EXT:
1683ae8c6e27Sflorian 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1684ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HEX:
1685ae8c6e27Sflorian 		return sldns_str2wire_hex_buf(str, rd, len);
1686ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC:
1687ae8c6e27Sflorian 		return sldns_str2wire_nsec_buf(str, rd, len);
1688ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TYPE:
1689ae8c6e27Sflorian 		return sldns_str2wire_type_buf(str, rd, len);
1690ae8c6e27Sflorian 	case LDNS_RDF_TYPE_CLASS:
1691ae8c6e27Sflorian 		return sldns_str2wire_class_buf(str, rd, len);
1692ae8c6e27Sflorian 	case LDNS_RDF_TYPE_CERT_ALG:
1693ae8c6e27Sflorian 		return sldns_str2wire_cert_alg_buf(str, rd, len);
1694ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ALG:
1695ae8c6e27Sflorian 		return sldns_str2wire_alg_buf(str, rd, len);
1696ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TIME:
1697ae8c6e27Sflorian 		return sldns_str2wire_time_buf(str, rd, len);
1698ae8c6e27Sflorian 	case LDNS_RDF_TYPE_PERIOD:
1699ae8c6e27Sflorian 		return sldns_str2wire_period_buf(str, rd, len);
1700ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TSIGTIME:
1701ae8c6e27Sflorian 		return sldns_str2wire_tsigtime_buf(str, rd, len);
1702ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LOC:
1703ae8c6e27Sflorian 		return sldns_str2wire_loc_buf(str, rd, len);
1704ae8c6e27Sflorian 	case LDNS_RDF_TYPE_WKS:
1705ae8c6e27Sflorian 		return sldns_str2wire_wks_buf(str, rd, len);
1706ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSAP:
1707ae8c6e27Sflorian 		return sldns_str2wire_nsap_buf(str, rd, len);
1708ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ATMA:
1709ae8c6e27Sflorian 		return sldns_str2wire_atma_buf(str, rd, len);
1710ae8c6e27Sflorian 	case LDNS_RDF_TYPE_IPSECKEY:
1711ae8c6e27Sflorian 		return sldns_str2wire_ipseckey_buf(str, rd, len);
1712ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC3_SALT:
1713ae8c6e27Sflorian 		return sldns_str2wire_nsec3_salt_buf(str, rd, len);
1714ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER:
1715ae8c6e27Sflorian 		return sldns_str2wire_b32_ext_buf(str, rd, len);
1716ae8c6e27Sflorian 	case LDNS_RDF_TYPE_ILNP64:
1717ae8c6e27Sflorian 		return sldns_str2wire_ilnp64_buf(str, rd, len);
1718ae8c6e27Sflorian 	case LDNS_RDF_TYPE_EUI48:
1719ae8c6e27Sflorian 		return sldns_str2wire_eui48_buf(str, rd, len);
1720ae8c6e27Sflorian 	case LDNS_RDF_TYPE_EUI64:
1721ae8c6e27Sflorian 		return sldns_str2wire_eui64_buf(str, rd, len);
1722ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TAG:
1723ae8c6e27Sflorian 		return sldns_str2wire_tag_buf(str, rd, len);
1724ae8c6e27Sflorian 	case LDNS_RDF_TYPE_LONG_STR:
1725ae8c6e27Sflorian 		return sldns_str2wire_long_str_buf(str, rd, len);
1726ae8c6e27Sflorian 	case LDNS_RDF_TYPE_TSIGERROR:
1727ae8c6e27Sflorian 		return sldns_str2wire_tsigerror_buf(str, rd, len);
1728ae8c6e27Sflorian 	case LDNS_RDF_TYPE_HIP:
1729ae8c6e27Sflorian 		return sldns_str2wire_hip_buf(str, rd, len);
1730ae8c6e27Sflorian 	case LDNS_RDF_TYPE_INT16_DATA:
1731ae8c6e27Sflorian 		return sldns_str2wire_int16_data_buf(str, rd, len);
1732411c5950Sflorian 	case LDNS_RDF_TYPE_SVCPARAM:
1733411c5950Sflorian 		return sldns_str2wire_svcparam_buf(str, rd, len);
1734ae8c6e27Sflorian 	case LDNS_RDF_TYPE_UNKNOWN:
1735ae8c6e27Sflorian 	case LDNS_RDF_TYPE_SERVICE:
1736ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_NOT_IMPL;
1737ae8c6e27Sflorian 	case LDNS_RDF_TYPE_NONE:
1738ae8c6e27Sflorian 	default:
1739ae8c6e27Sflorian 		break;
1740ae8c6e27Sflorian 	}
1741ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_GENERAL;
1742ae8c6e27Sflorian }
1743ae8c6e27Sflorian 
sldns_str2wire_int8_buf(const char * str,uint8_t * rd,size_t * len)1744ae8c6e27Sflorian int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len)
1745ae8c6e27Sflorian {
1746ae8c6e27Sflorian 	char* end;
1747ae8c6e27Sflorian 	uint8_t r = (uint8_t)strtol((char*)str, &end, 10);
1748ae8c6e27Sflorian 	if(*end != 0)
1749ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1750ae8c6e27Sflorian 	if(*len < 1)
1751ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1752ae8c6e27Sflorian 	rd[0] = r;
1753ae8c6e27Sflorian 	*len = 1;
1754ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1755ae8c6e27Sflorian }
1756ae8c6e27Sflorian 
sldns_str2wire_int16_buf(const char * str,uint8_t * rd,size_t * len)1757ae8c6e27Sflorian int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len)
1758ae8c6e27Sflorian {
1759ae8c6e27Sflorian 	char* end;
1760ae8c6e27Sflorian 	uint16_t r = (uint16_t)strtol((char*)str, &end, 10);
1761ae8c6e27Sflorian 	if(*end != 0)
1762ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1763ae8c6e27Sflorian 	if(*len < 2)
1764ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1765ae8c6e27Sflorian 	sldns_write_uint16(rd, r);
1766ae8c6e27Sflorian 	*len = 2;
1767ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1768ae8c6e27Sflorian }
1769ae8c6e27Sflorian 
sldns_str2wire_int32_buf(const char * str,uint8_t * rd,size_t * len)1770ae8c6e27Sflorian int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len)
1771ae8c6e27Sflorian {
1772ae8c6e27Sflorian 	char* end;
1773ae8c6e27Sflorian 	uint32_t r;
1774ae8c6e27Sflorian 	errno = 0; /* must set to zero before call,
1775ae8c6e27Sflorian 			note race condition on errno */
1776ae8c6e27Sflorian 	if(*str == '-')
1777ae8c6e27Sflorian 		r = (uint32_t)strtol((char*)str, &end, 10);
1778ae8c6e27Sflorian 	else	r = (uint32_t)strtoul((char*)str, &end, 10);
1779ae8c6e27Sflorian 	if(*end != 0)
1780ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str);
1781ae8c6e27Sflorian 	if(errno == ERANGE)
1782ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW;
1783ae8c6e27Sflorian 	if(*len < 4)
1784ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1785ae8c6e27Sflorian 	sldns_write_uint32(rd, r);
1786ae8c6e27Sflorian 	*len = 4;
1787ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1788ae8c6e27Sflorian }
1789ae8c6e27Sflorian 
sldns_str2wire_a_buf(const char * str,uint8_t * rd,size_t * len)1790ae8c6e27Sflorian int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len)
1791ae8c6e27Sflorian {
1792ae8c6e27Sflorian 	struct in_addr address;
1793ae8c6e27Sflorian 	if(inet_pton(AF_INET, (char*)str, &address) != 1)
1794ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP4;
1795ae8c6e27Sflorian 	if(*len < sizeof(address))
1796ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1797ae8c6e27Sflorian 	memmove(rd, &address, sizeof(address));
1798ae8c6e27Sflorian 	*len = sizeof(address);
1799ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1800ae8c6e27Sflorian }
1801ae8c6e27Sflorian 
sldns_str2wire_aaaa_buf(const char * str,uint8_t * rd,size_t * len)1802ae8c6e27Sflorian int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len)
1803ae8c6e27Sflorian {
1804ae8c6e27Sflorian #ifdef AF_INET6
1805ae8c6e27Sflorian 	uint8_t address[LDNS_IP6ADDRLEN + 1];
1806ae8c6e27Sflorian 	if(inet_pton(AF_INET6, (char*)str, address) != 1)
1807ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_IP6;
1808ae8c6e27Sflorian 	if(*len < LDNS_IP6ADDRLEN)
1809ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1810ae8c6e27Sflorian 	memmove(rd, address, LDNS_IP6ADDRLEN);
1811ae8c6e27Sflorian 	*len = LDNS_IP6ADDRLEN;
1812ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1813ae8c6e27Sflorian #else
1814ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_NOT_IMPL;
1815ae8c6e27Sflorian #endif
1816ae8c6e27Sflorian }
1817ae8c6e27Sflorian 
sldns_str2wire_str_buf(const char * str,uint8_t * rd,size_t * len)1818ae8c6e27Sflorian int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len)
1819ae8c6e27Sflorian {
1820ae8c6e27Sflorian 	uint8_t ch = 0;
1821ae8c6e27Sflorian 	size_t sl = 0;
1822ae8c6e27Sflorian 	const char* s = str;
1823ae8c6e27Sflorian 	/* skip length byte */
1824ae8c6e27Sflorian 	if(*len < 1)
1825ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1826ae8c6e27Sflorian 
1827ae8c6e27Sflorian 	/* read characters */
1828ae8c6e27Sflorian 	while(sldns_parse_char(&ch, &s)) {
1829ae8c6e27Sflorian 		if(sl >= 255)
1830ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, s-str);
183157403691Sflorian 		if(*len < sl+2)
1832ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1833ae8c6e27Sflorian 				s-str);
1834ae8c6e27Sflorian 		rd[++sl] = ch;
1835ae8c6e27Sflorian 	}
1836ae8c6e27Sflorian 	if(!s)
1837ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
1838ae8c6e27Sflorian 	rd[0] = (uint8_t)sl;
1839ae8c6e27Sflorian 	*len = sl+1;
1840ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1841ae8c6e27Sflorian }
1842ae8c6e27Sflorian 
sldns_str2wire_apl_buf(const char * str,uint8_t * rd,size_t * len)1843ae8c6e27Sflorian int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len)
1844ae8c6e27Sflorian {
1845ae8c6e27Sflorian 	const char *my_str = str;
1846ae8c6e27Sflorian 
1847ae8c6e27Sflorian 	char my_ip_str[64];
1848ae8c6e27Sflorian 	size_t ip_str_len;
1849ae8c6e27Sflorian 
1850ae8c6e27Sflorian 	uint16_t family;
1851ae8c6e27Sflorian 	int negation;
1852ae8c6e27Sflorian 	size_t adflength = 0;
1853ae8c6e27Sflorian 	uint8_t data[16+4];
1854ae8c6e27Sflorian 	uint8_t prefix;
1855ae8c6e27Sflorian 	size_t i;
1856ae8c6e27Sflorian 
1857ae8c6e27Sflorian 	if(*my_str == '\0') {
1858ae8c6e27Sflorian 		/* empty APL element, no data, no string */
1859ae8c6e27Sflorian 		*len = 0;
1860ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1861ae8c6e27Sflorian 	}
1862ae8c6e27Sflorian 
1863ae8c6e27Sflorian 	/* [!]afi:address/prefix */
1864ae8c6e27Sflorian 	if (strlen(my_str) < 2
1865ae8c6e27Sflorian 			|| strchr(my_str, ':') == NULL
1866ae8c6e27Sflorian 			|| strchr(my_str, '/') == NULL
1867ae8c6e27Sflorian 			|| strchr(my_str, ':') > strchr(my_str, '/')) {
1868ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1869ae8c6e27Sflorian 	}
1870ae8c6e27Sflorian 
1871ae8c6e27Sflorian 	if (my_str[0] == '!') {
1872ae8c6e27Sflorian 		negation = 1;
1873ae8c6e27Sflorian 		my_str += 1;
1874ae8c6e27Sflorian 	} else {
1875ae8c6e27Sflorian 		negation = 0;
1876ae8c6e27Sflorian 	}
1877ae8c6e27Sflorian 
1878ae8c6e27Sflorian 	family = (uint16_t) atoi(my_str);
1879ae8c6e27Sflorian 
1880ae8c6e27Sflorian 	my_str = strchr(my_str, ':') + 1;
1881ae8c6e27Sflorian 
1882ae8c6e27Sflorian 	/* need ip addr and only ip addr for inet_pton */
1883ae8c6e27Sflorian 	ip_str_len = (size_t) (strchr(my_str, '/') - my_str);
1884ae8c6e27Sflorian 	if(ip_str_len+1 > sizeof(my_ip_str))
1885ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1886ae8c6e27Sflorian 	(void)strlcpy(my_ip_str, my_str, sizeof(my_ip_str));
1887ae8c6e27Sflorian 	my_ip_str[ip_str_len] = 0;
1888ae8c6e27Sflorian 
1889ae8c6e27Sflorian 	if (family == 1) {
1890ae8c6e27Sflorian 		/* ipv4 */
1891ae8c6e27Sflorian 		if(inet_pton(AF_INET, my_ip_str, data+4) == 0)
1892ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1893ae8c6e27Sflorian 		for (i = 0; i < 4; i++) {
1894ae8c6e27Sflorian 			if (data[i+4] != 0) {
1895ae8c6e27Sflorian 				adflength = i + 1;
1896ae8c6e27Sflorian 			}
1897ae8c6e27Sflorian 		}
1898ae8c6e27Sflorian 	} else if (family == 2) {
1899ae8c6e27Sflorian 		/* ipv6 */
1900ae8c6e27Sflorian 		if (inet_pton(AF_INET6, my_ip_str, data+4) == 0)
1901ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
1902ae8c6e27Sflorian 		for (i = 0; i < 16; i++) {
1903ae8c6e27Sflorian 			if (data[i+4] != 0) {
1904ae8c6e27Sflorian 				adflength = i + 1;
1905ae8c6e27Sflorian 			}
1906ae8c6e27Sflorian 		}
1907ae8c6e27Sflorian 	} else {
1908ae8c6e27Sflorian 		/* unknown family */
1909ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
1910ae8c6e27Sflorian 	}
1911ae8c6e27Sflorian 
1912ae8c6e27Sflorian 	my_str = strchr(my_str, '/') + 1;
1913ae8c6e27Sflorian 	prefix = (uint8_t) atoi(my_str);
1914ae8c6e27Sflorian 
1915ae8c6e27Sflorian 	sldns_write_uint16(data, family);
1916ae8c6e27Sflorian 	data[2] = prefix;
1917ae8c6e27Sflorian 	data[3] = (uint8_t)adflength;
1918ae8c6e27Sflorian 	if (negation) {
1919ae8c6e27Sflorian 		/* set bit 1 of byte 3 */
1920ae8c6e27Sflorian 		data[3] = data[3] | 0x80;
1921ae8c6e27Sflorian 	}
1922ae8c6e27Sflorian 
1923ae8c6e27Sflorian 	if(*len < 4+adflength)
1924ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1925ae8c6e27Sflorian 	memmove(rd, data, 4+adflength);
1926ae8c6e27Sflorian 	*len = 4+adflength;
1927ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1928ae8c6e27Sflorian }
1929ae8c6e27Sflorian 
sldns_str2wire_b64_buf(const char * str,uint8_t * rd,size_t * len)1930ae8c6e27Sflorian int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len)
1931ae8c6e27Sflorian {
1932ae8c6e27Sflorian 	size_t sz = sldns_b64_pton_calculate_size(strlen(str));
1933ae8c6e27Sflorian 	int n;
1934ae8c6e27Sflorian 	if(strcmp(str, "0") == 0) {
1935ae8c6e27Sflorian 		*len = 0;
1936ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
1937ae8c6e27Sflorian 	}
1938ae8c6e27Sflorian 	if(*len < sz)
1939ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1940ae8c6e27Sflorian 	n = sldns_b64_pton(str, rd, *len);
1941ae8c6e27Sflorian 	if(n < 0)
1942ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
1943ae8c6e27Sflorian 	*len = (size_t)n;
1944ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1945ae8c6e27Sflorian }
1946ae8c6e27Sflorian 
sldns_str2wire_b32_ext_buf(const char * str,uint8_t * rd,size_t * len)1947ae8c6e27Sflorian int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len)
1948ae8c6e27Sflorian {
1949ae8c6e27Sflorian 	size_t slen = strlen(str);
1950ae8c6e27Sflorian 	size_t sz = sldns_b32_pton_calculate_size(slen);
1951ae8c6e27Sflorian 	int n;
1952ae8c6e27Sflorian 	if(*len < 1+sz)
1953ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
1954ae8c6e27Sflorian 	rd[0] = (uint8_t)sz;
1955ae8c6e27Sflorian 	n = sldns_b32_pton_extended_hex(str, slen, rd+1, *len-1);
1956ae8c6e27Sflorian 	if(n < 0)
1957ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT;
1958ae8c6e27Sflorian 	*len = (size_t)n+1;
1959ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
1960ae8c6e27Sflorian }
1961ae8c6e27Sflorian 
1962ae8c6e27Sflorian /** see if the string ends, or ends in whitespace */
1963ae8c6e27Sflorian static int
sldns_is_last_of_string(const char * str)1964ae8c6e27Sflorian sldns_is_last_of_string(const char* str)
1965ae8c6e27Sflorian {
1966ae8c6e27Sflorian 	if(*str == 0) return 1;
1967ae8c6e27Sflorian 	while(isspace((unsigned char)*str))
1968ae8c6e27Sflorian 		str++;
1969ae8c6e27Sflorian 	if(*str == 0) return 1;
1970ae8c6e27Sflorian 	return 0;
1971ae8c6e27Sflorian }
1972ae8c6e27Sflorian 
sldns_str2wire_hex_buf(const char * str,uint8_t * rd,size_t * len)1973ae8c6e27Sflorian int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len)
1974ae8c6e27Sflorian {
1975ae8c6e27Sflorian 	const char* s = str;
1976ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
1977ae8c6e27Sflorian 	while(*s) {
1978ae8c6e27Sflorian 		if(isspace((unsigned char)*s)) {
1979ae8c6e27Sflorian 			s++;
1980ae8c6e27Sflorian 			continue;
1981ae8c6e27Sflorian 		}
1982ae8c6e27Sflorian 		if(dlen == 0 && *s == '0' && sldns_is_last_of_string(s+1)) {
1983ae8c6e27Sflorian 			*len = 0;
1984ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_OK;
1985ae8c6e27Sflorian 		}
1986ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
1987ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1988ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
1989ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
1990ae8c6e27Sflorian 				s-str);
1991ae8c6e27Sflorian 		if((dlen&1)==0)
1992ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
1993ae8c6e27Sflorian 		else	rd[dlen/2] += (uint8_t)sldns_hexdigit_to_int(*s++);
1994ae8c6e27Sflorian 		dlen++;
1995ae8c6e27Sflorian 	}
1996ae8c6e27Sflorian 	if((dlen&1)!=0)
1997ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
1998ae8c6e27Sflorian 	*len = dlen/2;
1999ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2000ae8c6e27Sflorian }
2001ae8c6e27Sflorian 
sldns_str2wire_nsec_buf(const char * str,uint8_t * rd,size_t * len)2002ae8c6e27Sflorian int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len)
2003ae8c6e27Sflorian {
2004ae8c6e27Sflorian 	const char *delim = "\n\t ";
2005ae8c6e27Sflorian 	char token[64]; /* for a type name */
2006ae8c6e27Sflorian 	size_t type_count = 0;
2007ae8c6e27Sflorian 	int block;
2008ae8c6e27Sflorian 	size_t used = 0;
2009ae8c6e27Sflorian 	uint16_t maxtype = 0;
2010ae8c6e27Sflorian 	uint8_t typebits[8192]; /* 65536 bits */
2011ae8c6e27Sflorian 	uint8_t window_in_use[256];
2012ae8c6e27Sflorian 
2013ae8c6e27Sflorian 	/* string in buffer */
2014ae8c6e27Sflorian 	sldns_buffer strbuf;
2015ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2016ae8c6e27Sflorian 
2017ae8c6e27Sflorian 	/* parse the types */
2018ae8c6e27Sflorian 	memset(typebits, 0, sizeof(typebits));
2019ae8c6e27Sflorian 	memset(window_in_use, 0, sizeof(window_in_use));
2020ae8c6e27Sflorian 	while(sldns_buffer_remaining(&strbuf) > 0 &&
2021ae8c6e27Sflorian 		sldns_bget_token(&strbuf, token, delim, sizeof(token)) != -1) {
2022ae8c6e27Sflorian 		uint16_t t = sldns_get_rr_type_by_name(token);
2023ae8c6e27Sflorian 		if(token[0] == 0)
2024ae8c6e27Sflorian 			continue;
2025ae8c6e27Sflorian 		if(t == 0 && strcmp(token, "TYPE0") != 0)
2026ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE,
2027ae8c6e27Sflorian 				sldns_buffer_position(&strbuf));
2028ae8c6e27Sflorian 		typebits[t/8] |= (0x80>>(t%8));
2029ae8c6e27Sflorian 		window_in_use[t/256] = 1;
2030ae8c6e27Sflorian 		type_count++;
2031ae8c6e27Sflorian 		if(t > maxtype) maxtype = t;
2032ae8c6e27Sflorian 	}
2033ae8c6e27Sflorian 
2034ae8c6e27Sflorian 	/* empty NSEC bitmap */
2035ae8c6e27Sflorian 	if(type_count == 0) {
2036ae8c6e27Sflorian 		*len = 0;
2037ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
2038ae8c6e27Sflorian 	}
2039ae8c6e27Sflorian 
2040ae8c6e27Sflorian 	/* encode windows {u8 windowblock, u8 bitmaplength, 0-32u8 bitmap},
2041ae8c6e27Sflorian 	 * block is 0-255 upper octet of types, length if 0-32. */
2042ae8c6e27Sflorian 	for(block = 0; block <= (int)maxtype/256; block++) {
2043ae8c6e27Sflorian 		int i, blocklen = 0;
2044ae8c6e27Sflorian 		if(!window_in_use[block])
2045ae8c6e27Sflorian 			continue;
2046ae8c6e27Sflorian 		for(i=0; i<32; i++) {
2047ae8c6e27Sflorian 			if(typebits[block*32+i] != 0)
2048ae8c6e27Sflorian 				blocklen = i+1;
2049ae8c6e27Sflorian 		}
2050ae8c6e27Sflorian 		if(blocklen == 0)
2051ae8c6e27Sflorian 			continue; /* empty window should have been !in_use */
2052ae8c6e27Sflorian 		if(used+blocklen+2 > *len)
2053ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2054ae8c6e27Sflorian 		rd[used+0] = (uint8_t)block;
2055ae8c6e27Sflorian 		rd[used+1] = (uint8_t)blocklen;
2056ae8c6e27Sflorian 		for(i=0; i<blocklen; i++) {
2057ae8c6e27Sflorian 			rd[used+2+i] = typebits[block*32+i];
2058ae8c6e27Sflorian 		}
2059ae8c6e27Sflorian 		used += blocklen+2;
2060ae8c6e27Sflorian 	}
2061ae8c6e27Sflorian 	*len = used;
2062ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2063ae8c6e27Sflorian }
2064ae8c6e27Sflorian 
sldns_str2wire_type_buf(const char * str,uint8_t * rd,size_t * len)2065ae8c6e27Sflorian int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len)
2066ae8c6e27Sflorian {
2067ae8c6e27Sflorian 	uint16_t t = sldns_get_rr_type_by_name(str);
2068ae8c6e27Sflorian 	if(t == 0 && strcmp(str, "TYPE0") != 0)
2069ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_TYPE;
2070ae8c6e27Sflorian 	if(*len < 2)
2071ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2072ae8c6e27Sflorian 	sldns_write_uint16(rd, t);
2073ae8c6e27Sflorian 	*len = 2;
2074ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2075ae8c6e27Sflorian }
2076ae8c6e27Sflorian 
sldns_str2wire_class_buf(const char * str,uint8_t * rd,size_t * len)2077ae8c6e27Sflorian int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len)
2078ae8c6e27Sflorian {
2079ae8c6e27Sflorian 	uint16_t c = sldns_get_rr_class_by_name(str);
2080ae8c6e27Sflorian 	if(c == 0 && strcmp(str, "CLASS0") != 0)
2081ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_CLASS;
2082ae8c6e27Sflorian 	if(*len < 2)
2083ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2084ae8c6e27Sflorian 	sldns_write_uint16(rd, c);
2085ae8c6e27Sflorian 	*len = 2;
2086ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2087ae8c6e27Sflorian }
2088ae8c6e27Sflorian 
2089ae8c6e27Sflorian /* An certificate alg field can either be specified as a 8 bits number
2090ae8c6e27Sflorian  * or by its symbolic name. Handle both */
sldns_str2wire_cert_alg_buf(const char * str,uint8_t * rd,size_t * len)2091ae8c6e27Sflorian int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len)
2092ae8c6e27Sflorian {
2093ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_cert_algorithms,
2094ae8c6e27Sflorian 		str);
2095ae8c6e27Sflorian 	if(*len < 2)
2096ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2097ae8c6e27Sflorian 	if(lt) {
2098ae8c6e27Sflorian 		sldns_write_uint16(rd, (uint16_t)lt->id);
2099ae8c6e27Sflorian 	} else {
2100ae8c6e27Sflorian 		int s = sldns_str2wire_int16_buf(str, rd, len);
2101ae8c6e27Sflorian 		if(s) return s;
2102ae8c6e27Sflorian 		if(sldns_read_uint16(rd) == 0)
2103ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM;
2104ae8c6e27Sflorian 	}
2105ae8c6e27Sflorian 	*len = 2;
2106ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2107ae8c6e27Sflorian }
2108ae8c6e27Sflorian 
2109ae8c6e27Sflorian /* An alg field can either be specified as a 8 bits number
2110ae8c6e27Sflorian  * or by its symbolic name. Handle both */
sldns_str2wire_alg_buf(const char * str,uint8_t * rd,size_t * len)2111ae8c6e27Sflorian int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len)
2112ae8c6e27Sflorian {
2113ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_algorithms, str);
2114ae8c6e27Sflorian 	if(*len < 1)
2115ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2116ae8c6e27Sflorian 	if(lt) {
2117ae8c6e27Sflorian 		rd[0] = (uint8_t)lt->id;
2118ae8c6e27Sflorian 		*len = 1;
2119ae8c6e27Sflorian 	} else {
2120ae8c6e27Sflorian 		/* try as-is (a number) */
2121ae8c6e27Sflorian 		return sldns_str2wire_int8_buf(str, rd, len);
2122ae8c6e27Sflorian 	}
2123ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2124ae8c6e27Sflorian }
2125ae8c6e27Sflorian 
sldns_str2wire_tsigerror_buf(const char * str,uint8_t * rd,size_t * len)2126ae8c6e27Sflorian int sldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len)
2127ae8c6e27Sflorian {
2128ae8c6e27Sflorian 	sldns_lookup_table *lt = sldns_lookup_by_name(sldns_tsig_errors, str);
2129ae8c6e27Sflorian 	if(*len < 2)
2130ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2131ae8c6e27Sflorian 	if(lt) {
2132ae8c6e27Sflorian 		sldns_write_uint16(rd, (uint16_t)lt->id);
2133ae8c6e27Sflorian 		*len = 2;
2134ae8c6e27Sflorian 	} else {
2135ae8c6e27Sflorian 		/* try as-is (a number) */
2136ae8c6e27Sflorian 		return sldns_str2wire_int16_buf(str, rd, len);
2137ae8c6e27Sflorian 	}
2138ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2139ae8c6e27Sflorian }
2140ae8c6e27Sflorian 
sldns_str2wire_time_buf(const char * str,uint8_t * rd,size_t * len)2141ae8c6e27Sflorian int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len)
2142ae8c6e27Sflorian {
2143ae8c6e27Sflorian 	/* convert a time YYYYDDMMHHMMSS to wireformat */
2144ae8c6e27Sflorian 	struct tm tm;
2145ae8c6e27Sflorian 	if(*len < 4)
2146ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2147ae8c6e27Sflorian 
2148ae8c6e27Sflorian 	/* Try to scan the time... */
2149ae8c6e27Sflorian 	memset(&tm, 0, sizeof(tm));
2150ae8c6e27Sflorian 	if (strlen(str) == 14 && sscanf(str, "%4d%2d%2d%2d%2d%2d",
2151ae8c6e27Sflorian 		&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,
2152ae8c6e27Sflorian 		&tm.tm_min, &tm.tm_sec) == 6) {
2153ae8c6e27Sflorian 	   	tm.tm_year -= 1900;
2154ae8c6e27Sflorian 	   	tm.tm_mon--;
2155ae8c6e27Sflorian 	   	/* Check values */
2156ae8c6e27Sflorian 		if (tm.tm_year < 70)
2157ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2158ae8c6e27Sflorian 		if (tm.tm_mon < 0 || tm.tm_mon > 11)
2159ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2160ae8c6e27Sflorian 		if (tm.tm_mday < 1 || tm.tm_mday > 31)
2161ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2162ae8c6e27Sflorian 		if (tm.tm_hour < 0 || tm.tm_hour > 23)
2163ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2164ae8c6e27Sflorian 		if (tm.tm_min < 0 || tm.tm_min > 59)
2165ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2166ae8c6e27Sflorian 		if (tm.tm_sec < 0 || tm.tm_sec > 59)
2167ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_SYNTAX_TIME;
2168ae8c6e27Sflorian 
2169ae8c6e27Sflorian 		sldns_write_uint32(rd, (uint32_t)sldns_mktime_from_utc(&tm));
2170ae8c6e27Sflorian 	} else {
2171ae8c6e27Sflorian 		/* handle it as 32 bits timestamp */
2172ae8c6e27Sflorian 		char *end;
2173ae8c6e27Sflorian 		uint32_t l = (uint32_t)strtol((char*)str, &end, 10);
2174ae8c6e27Sflorian 		if(*end != 0)
2175ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME,
2176ae8c6e27Sflorian 				end-(char*)str);
2177ae8c6e27Sflorian 		sldns_write_uint32(rd, l);
2178ae8c6e27Sflorian 	}
2179ae8c6e27Sflorian 	*len = 4;
2180ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2181ae8c6e27Sflorian }
2182ae8c6e27Sflorian 
sldns_str2wire_tsigtime_buf(const char * str,uint8_t * rd,size_t * len)2183ae8c6e27Sflorian int sldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len)
2184ae8c6e27Sflorian {
2185ae8c6e27Sflorian 	char* end;
2186ae8c6e27Sflorian 	uint64_t t = (uint64_t)strtol((char*)str, &end, 10);
2187ae8c6e27Sflorian 	uint16_t high;
2188ae8c6e27Sflorian 	uint32_t low;
2189ae8c6e27Sflorian 	if(*end != 0)
2190ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME, end-str);
2191ae8c6e27Sflorian 	if(*len < 6)
2192ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2193ae8c6e27Sflorian 	high = (uint16_t)(t>>32);
2194ae8c6e27Sflorian 	low = (uint32_t)(t);
2195ae8c6e27Sflorian 	sldns_write_uint16(rd, high);
2196ae8c6e27Sflorian 	sldns_write_uint32(rd+2, low);
2197ae8c6e27Sflorian 	*len = 6;
2198ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2199ae8c6e27Sflorian }
2200ae8c6e27Sflorian 
sldns_str2wire_period_buf(const char * str,uint8_t * rd,size_t * len)2201ae8c6e27Sflorian int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len)
2202ae8c6e27Sflorian {
2203ae8c6e27Sflorian 	const char* end;
22047a05b9dfSflorian 	int overflow;
22057a05b9dfSflorian 	uint32_t p = sldns_str2period(str, &end, &overflow);
2206ae8c6e27Sflorian 	if(*end != 0)
2207ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_PERIOD, end-str);
22087a05b9dfSflorian 	if(overflow)
22097a05b9dfSflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW,
22107a05b9dfSflorian 			end-str);
2211ae8c6e27Sflorian 	if(*len < 4)
2212ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2213ae8c6e27Sflorian 	sldns_write_uint32(rd, p);
2214ae8c6e27Sflorian 	*len = 4;
2215ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2216ae8c6e27Sflorian }
2217ae8c6e27Sflorian 
2218ae8c6e27Sflorian /** read "<digits>[.<digits>][mM]" into mantissa exponent format for LOC type */
2219ae8c6e27Sflorian static int
loc_parse_cm(char * my_str,char ** endstr,uint8_t * m,uint8_t * e)2220ae8c6e27Sflorian loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e)
2221ae8c6e27Sflorian {
2222ae8c6e27Sflorian 	uint32_t meters = 0, cm = 0, val;
2223411c5950Sflorian 	char* cm_endstr;
2224ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2225ae8c6e27Sflorian 		my_str++;
2226ae8c6e27Sflorian 	}
2227ae8c6e27Sflorian 	meters = (uint32_t)strtol(my_str, &my_str, 10);
2228ae8c6e27Sflorian 	if (*my_str == '.') {
2229ae8c6e27Sflorian 		my_str++;
2230411c5950Sflorian 		cm = (uint32_t)strtol(my_str, &cm_endstr, 10);
2231411c5950Sflorian 		if(cm_endstr == my_str + 1)
2232411c5950Sflorian 			cm *= 10;
2233411c5950Sflorian 		my_str = cm_endstr;
2234ae8c6e27Sflorian 	}
2235ae8c6e27Sflorian 	if (meters >= 1) {
2236ae8c6e27Sflorian 		*e = 2;
2237ae8c6e27Sflorian 		val = meters;
2238ae8c6e27Sflorian 	} else	{
2239ae8c6e27Sflorian 		*e = 0;
2240ae8c6e27Sflorian 		val = cm;
2241ae8c6e27Sflorian 	}
2242ae8c6e27Sflorian 	while(val >= 10) {
2243ae8c6e27Sflorian 		(*e)++;
2244ae8c6e27Sflorian 		val /= 10;
2245ae8c6e27Sflorian 	}
2246ae8c6e27Sflorian 	*m = (uint8_t)val;
2247ae8c6e27Sflorian 
2248ae8c6e27Sflorian 	if (*e > 9)
2249ae8c6e27Sflorian 		return 0;
2250ae8c6e27Sflorian 	if (*my_str == 'm' || *my_str == 'M') {
2251ae8c6e27Sflorian 		my_str++;
2252ae8c6e27Sflorian 	}
2253ae8c6e27Sflorian 	*endstr = my_str;
2254ae8c6e27Sflorian 	return 1;
2255ae8c6e27Sflorian }
2256ae8c6e27Sflorian 
sldns_str2wire_loc_buf(const char * str,uint8_t * rd,size_t * len)2257ae8c6e27Sflorian int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len)
2258ae8c6e27Sflorian {
2259ae8c6e27Sflorian 	uint32_t latitude = 0;
2260ae8c6e27Sflorian 	uint32_t longitude = 0;
2261ae8c6e27Sflorian 	uint32_t altitude = 0;
2262ae8c6e27Sflorian 
2263ae8c6e27Sflorian 	uint32_t equator = (uint32_t)1<<31; /* 2**31 */
2264ae8c6e27Sflorian 
2265ae8c6e27Sflorian 	/* only support version 0 */
2266ae8c6e27Sflorian 	uint32_t h = 0;
2267ae8c6e27Sflorian 	uint32_t m = 0;
2268ae8c6e27Sflorian 	uint8_t size_b = 1, size_e = 2;
2269ae8c6e27Sflorian 	uint8_t horiz_pre_b = 1, horiz_pre_e = 6;
2270ae8c6e27Sflorian 	uint8_t vert_pre_b = 1, vert_pre_e = 3;
2271ae8c6e27Sflorian 
2272ae8c6e27Sflorian 	double s = 0.0;
2273ae8c6e27Sflorian 	int northerness;
2274ae8c6e27Sflorian 	int easterness;
2275ae8c6e27Sflorian 
2276ae8c6e27Sflorian 	char *my_str = (char *) str;
2277ae8c6e27Sflorian 
2278ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2279ae8c6e27Sflorian 		h = (uint32_t) strtol(my_str, &my_str, 10);
2280ae8c6e27Sflorian 	} else {
2281ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2282ae8c6e27Sflorian 	}
2283ae8c6e27Sflorian 
2284ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2285ae8c6e27Sflorian 		my_str++;
2286ae8c6e27Sflorian 	}
2287ae8c6e27Sflorian 
2288ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2289ae8c6e27Sflorian 		m = (uint32_t) strtol(my_str, &my_str, 10);
2290ae8c6e27Sflorian 	} else if (*my_str == 'N' || *my_str == 'S') {
2291ae8c6e27Sflorian 		goto north;
2292ae8c6e27Sflorian 	} else {
2293ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2294ae8c6e27Sflorian 	}
2295ae8c6e27Sflorian 
2296ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2297ae8c6e27Sflorian 		my_str++;
2298ae8c6e27Sflorian 	}
2299ae8c6e27Sflorian 
2300ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2301ae8c6e27Sflorian 		s = strtod(my_str, &my_str);
2302ae8c6e27Sflorian 	}
2303ae8c6e27Sflorian 
2304ae8c6e27Sflorian 	/* skip blanks before northerness */
2305ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2306ae8c6e27Sflorian 		my_str++;
2307ae8c6e27Sflorian 	}
2308ae8c6e27Sflorian 
2309ae8c6e27Sflorian north:
2310ae8c6e27Sflorian 	if (*my_str == 'N') {
2311ae8c6e27Sflorian 		northerness = 1;
2312ae8c6e27Sflorian 	} else if (*my_str == 'S') {
2313ae8c6e27Sflorian 		northerness = 0;
2314ae8c6e27Sflorian 	} else {
2315ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2316ae8c6e27Sflorian 	}
2317ae8c6e27Sflorian 
2318ae8c6e27Sflorian 	my_str++;
2319ae8c6e27Sflorian 
2320ae8c6e27Sflorian 	/* store number */
2321ae8c6e27Sflorian 	s = 1000.0 * s;
2322ae8c6e27Sflorian 	/* add a little to make floor in conversion a round */
2323ae8c6e27Sflorian 	s += 0.0005;
2324ae8c6e27Sflorian 	latitude = (uint32_t) s;
2325ae8c6e27Sflorian 	latitude += 1000 * 60 * m;
2326ae8c6e27Sflorian 	latitude += 1000 * 60 * 60 * h;
2327ae8c6e27Sflorian 	if (northerness) {
2328ae8c6e27Sflorian 		latitude = equator + latitude;
2329ae8c6e27Sflorian 	} else {
2330ae8c6e27Sflorian 		latitude = equator - latitude;
2331ae8c6e27Sflorian 	}
2332ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2333ae8c6e27Sflorian 		my_str++;
2334ae8c6e27Sflorian 	}
2335ae8c6e27Sflorian 
2336ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2337ae8c6e27Sflorian 		h = (uint32_t) strtol(my_str, &my_str, 10);
2338ae8c6e27Sflorian 	} else {
2339ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2340ae8c6e27Sflorian 	}
2341ae8c6e27Sflorian 
2342ae8c6e27Sflorian 	while (isblank((unsigned char) *my_str)) {
2343ae8c6e27Sflorian 		my_str++;
2344ae8c6e27Sflorian 	}
2345ae8c6e27Sflorian 
2346ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2347ae8c6e27Sflorian 		m = (uint32_t) strtol(my_str, &my_str, 10);
2348ae8c6e27Sflorian 	} else if (*my_str == 'E' || *my_str == 'W') {
2349ae8c6e27Sflorian 		goto east;
2350ae8c6e27Sflorian 	} else {
2351ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2352ae8c6e27Sflorian 	}
2353ae8c6e27Sflorian 
2354ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2355ae8c6e27Sflorian 		my_str++;
2356ae8c6e27Sflorian 	}
2357ae8c6e27Sflorian 
2358ae8c6e27Sflorian 	if (isdigit((unsigned char) *my_str)) {
2359ae8c6e27Sflorian 		s = strtod(my_str, &my_str);
2360ae8c6e27Sflorian 	}
2361ae8c6e27Sflorian 
2362ae8c6e27Sflorian 	/* skip blanks before easterness */
2363ae8c6e27Sflorian 	while (isblank((unsigned char)*my_str)) {
2364ae8c6e27Sflorian 		my_str++;
2365ae8c6e27Sflorian 	}
2366ae8c6e27Sflorian 
2367ae8c6e27Sflorian east:
2368ae8c6e27Sflorian 	if (*my_str == 'E') {
2369ae8c6e27Sflorian 		easterness = 1;
2370ae8c6e27Sflorian 	} else if (*my_str == 'W') {
2371ae8c6e27Sflorian 		easterness = 0;
2372ae8c6e27Sflorian 	} else {
2373ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2374ae8c6e27Sflorian 	}
2375ae8c6e27Sflorian 
2376ae8c6e27Sflorian 	my_str++;
2377ae8c6e27Sflorian 
2378ae8c6e27Sflorian 	/* store number */
2379ae8c6e27Sflorian 	s *= 1000.0;
2380ae8c6e27Sflorian 	/* add a little to make floor in conversion a round */
2381ae8c6e27Sflorian 	s += 0.0005;
2382ae8c6e27Sflorian 	longitude = (uint32_t) s;
2383ae8c6e27Sflorian 	longitude += 1000 * 60 * m;
2384ae8c6e27Sflorian 	longitude += 1000 * 60 * 60 * h;
2385ae8c6e27Sflorian 
2386ae8c6e27Sflorian 	if (easterness) {
2387ae8c6e27Sflorian 		longitude += equator;
2388ae8c6e27Sflorian 	} else {
2389ae8c6e27Sflorian 		longitude = equator - longitude;
2390ae8c6e27Sflorian 	}
2391ae8c6e27Sflorian 
2392ae8c6e27Sflorian 	altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 +
2393ae8c6e27Sflorian 		10000000.0 + 0.5);
2394ae8c6e27Sflorian 	if (*my_str == 'm' || *my_str == 'M') {
2395ae8c6e27Sflorian 		my_str++;
2396ae8c6e27Sflorian 	}
2397ae8c6e27Sflorian 
2398ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2399ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e))
2400ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2401ae8c6e27Sflorian 	}
2402ae8c6e27Sflorian 
2403ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2404ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e))
2405ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2406ae8c6e27Sflorian 	}
2407ae8c6e27Sflorian 
2408ae8c6e27Sflorian 	if (strlen(my_str) > 0) {
2409ae8c6e27Sflorian 		if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e))
2410ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_INVALID_STR;
2411ae8c6e27Sflorian 	}
2412ae8c6e27Sflorian 
2413ae8c6e27Sflorian 	if(*len < 16)
2414ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2415ae8c6e27Sflorian 	rd[0] = 0;
2416ae8c6e27Sflorian 	rd[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f);
2417ae8c6e27Sflorian 	rd[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f);
2418ae8c6e27Sflorian 	rd[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f);
2419ae8c6e27Sflorian 	sldns_write_uint32(rd + 4, latitude);
2420ae8c6e27Sflorian 	sldns_write_uint32(rd + 8, longitude);
2421ae8c6e27Sflorian 	sldns_write_uint32(rd + 12, altitude);
2422ae8c6e27Sflorian 	*len = 16;
2423ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2424ae8c6e27Sflorian }
2425ae8c6e27Sflorian 
2426ae8c6e27Sflorian static void
ldns_tolower_str(char * s)2427ae8c6e27Sflorian ldns_tolower_str(char* s)
2428ae8c6e27Sflorian {
2429ae8c6e27Sflorian 	if(s) {
2430ae8c6e27Sflorian 		while(*s) {
2431ae8c6e27Sflorian 			*s = (char)tolower((unsigned char)*s);
2432ae8c6e27Sflorian 			s++;
2433ae8c6e27Sflorian 		}
2434ae8c6e27Sflorian 	}
2435ae8c6e27Sflorian }
2436ae8c6e27Sflorian 
sldns_str2wire_wks_buf(const char * str,uint8_t * rd,size_t * len)2437ae8c6e27Sflorian int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len)
2438ae8c6e27Sflorian {
2439ae8c6e27Sflorian 	int rd_len = 1;
2440ae8c6e27Sflorian 	int have_proto = 0;
2441ae8c6e27Sflorian 	char token[50], proto_str[50];
2442ae8c6e27Sflorian 	sldns_buffer strbuf;
2443ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2444ae8c6e27Sflorian 	proto_str[0]=0;
2445ae8c6e27Sflorian 
2446ae8c6e27Sflorian 	/* check we have one byte for proto */
2447ae8c6e27Sflorian 	if(*len < 1)
2448ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2449ae8c6e27Sflorian 
2450ae8c6e27Sflorian 	while(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) > 0) {
2451ae8c6e27Sflorian 		ldns_tolower_str(token);
2452ae8c6e27Sflorian 		if(!have_proto) {
2453ae8c6e27Sflorian 			struct protoent *p = getprotobyname(token);
2454ae8c6e27Sflorian 			have_proto = 1;
2455ae8c6e27Sflorian 			if(p) rd[0] = (uint8_t)p->p_proto;
2456ae8c6e27Sflorian 			else if(strcasecmp(token, "tcp")==0) rd[0]=6;
2457ae8c6e27Sflorian 			else if(strcasecmp(token, "udp")==0) rd[0]=17;
2458ae8c6e27Sflorian 			else rd[0] = (uint8_t)atoi(token);
2459ae8c6e27Sflorian 			(void)strlcpy(proto_str, token, sizeof(proto_str));
2460ae8c6e27Sflorian 		} else {
2461ae8c6e27Sflorian 			int serv_port;
2462*54cc57acSflorian 			if(atoi(token) != 0) serv_port=atoi(token);
2463*54cc57acSflorian 			else if(strcmp(token, "0") == 0) serv_port=0;
2464ae8c6e27Sflorian 			else if(strcasecmp(token, "domain")==0) serv_port=53;
2465ae8c6e27Sflorian 			else {
2466*54cc57acSflorian 				struct servent *serv = getservbyname(token, proto_str);
2467*54cc57acSflorian 				if(serv) serv_port=(int)ntohs((uint16_t)serv->s_port);
2468*54cc57acSflorian 				else {
2469ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2470ae8c6e27Sflorian 					endservent();
2471ae8c6e27Sflorian #endif
2472ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2473ae8c6e27Sflorian 					endprotoent();
2474ae8c6e27Sflorian #endif
2475ae8c6e27Sflorian 					return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2476ae8c6e27Sflorian 						sldns_buffer_position(&strbuf));
2477ae8c6e27Sflorian 				}
2478*54cc57acSflorian 			}
2479ae8c6e27Sflorian 			if(serv_port < 0 || serv_port > 65535) {
2480ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2481ae8c6e27Sflorian 				endservent();
2482ae8c6e27Sflorian #endif
2483ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2484ae8c6e27Sflorian 				endprotoent();
2485ae8c6e27Sflorian #endif
2486ae8c6e27Sflorian 				return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX,
2487ae8c6e27Sflorian 					sldns_buffer_position(&strbuf));
2488ae8c6e27Sflorian 			}
2489ae8c6e27Sflorian 			if(rd_len < 1+serv_port/8+1) {
2490ae8c6e27Sflorian 				/* bitmap is larger, init new bytes at 0 */
2491ae8c6e27Sflorian 				if(*len < 1+(size_t)serv_port/8+1) {
2492ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2493ae8c6e27Sflorian 					endservent();
2494ae8c6e27Sflorian #endif
2495ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2496ae8c6e27Sflorian 					endprotoent();
2497ae8c6e27Sflorian #endif
2498ae8c6e27Sflorian 					return RET_ERR(
2499ae8c6e27Sflorian 					LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2500ae8c6e27Sflorian 					sldns_buffer_position(&strbuf));
2501ae8c6e27Sflorian 				}
2502ae8c6e27Sflorian 				memset(rd+rd_len, 0, 1+(size_t)serv_port/8+1-rd_len);
2503ae8c6e27Sflorian 				rd_len = 1+serv_port/8+1;
2504ae8c6e27Sflorian 			}
2505ae8c6e27Sflorian 			rd[1+ serv_port/8] |= (1 << (7 - serv_port % 8));
2506ae8c6e27Sflorian 		}
2507ae8c6e27Sflorian 	}
2508ae8c6e27Sflorian 	*len = (size_t)rd_len;
2509ae8c6e27Sflorian 
2510ae8c6e27Sflorian #ifdef HAVE_ENDSERVENT
2511ae8c6e27Sflorian 	endservent();
2512ae8c6e27Sflorian #endif
2513ae8c6e27Sflorian #ifdef HAVE_ENDPROTOENT
2514ae8c6e27Sflorian 	endprotoent();
2515ae8c6e27Sflorian #endif
2516ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2517ae8c6e27Sflorian }
2518ae8c6e27Sflorian 
sldns_str2wire_nsap_buf(const char * str,uint8_t * rd,size_t * len)2519ae8c6e27Sflorian int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len)
2520ae8c6e27Sflorian {
2521ae8c6e27Sflorian 	const char* s = str;
2522ae8c6e27Sflorian 	size_t slen;
2523ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
2524ae8c6e27Sflorian 
2525ae8c6e27Sflorian 	/* just a hex string with optional dots? */
2526ae8c6e27Sflorian 	if (s[0] != '0' || s[1] != 'x')
2527ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_INVALID_STR;
2528ae8c6e27Sflorian 	s += 2;
2529ae8c6e27Sflorian 	slen = strlen(s);
2530ae8c6e27Sflorian 	if(slen > LDNS_MAX_RDFLEN*2)
2531ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2532ae8c6e27Sflorian 	while(*s) {
2533ae8c6e27Sflorian 		if(isspace((unsigned char)*s) || *s == '.') {
2534ae8c6e27Sflorian 			s++;
2535ae8c6e27Sflorian 			continue;
2536ae8c6e27Sflorian 		}
2537ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
2538ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2539ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
2540ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2541ae8c6e27Sflorian 				s-str);
2542ae8c6e27Sflorian 		if((dlen&1)==0)
2543ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2544ae8c6e27Sflorian 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2545ae8c6e27Sflorian 		dlen++;
2546ae8c6e27Sflorian 	}
2547ae8c6e27Sflorian 	if((dlen&1)!=0)
2548ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2549ae8c6e27Sflorian 	*len = dlen/2;
2550ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2551ae8c6e27Sflorian }
2552ae8c6e27Sflorian 
sldns_str2wire_atma_buf(const char * str,uint8_t * rd,size_t * len)2553ae8c6e27Sflorian int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len)
2554ae8c6e27Sflorian {
2555ae8c6e27Sflorian 	const char* s = str;
2556ae8c6e27Sflorian 	size_t slen = strlen(str);
2557ae8c6e27Sflorian 	size_t dlen = 0; /* number of hexdigits parsed */
2558ae8c6e27Sflorian 
2559ae8c6e27Sflorian 	/* just a hex string with optional dots? */
2560ae8c6e27Sflorian 	/* notimpl e.164 format */
2561ae8c6e27Sflorian 	if(slen > LDNS_MAX_RDFLEN*2)
2562ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2563ae8c6e27Sflorian 	while(*s) {
2564ae8c6e27Sflorian 		if(isspace((unsigned char)*s) || *s == '.') {
2565ae8c6e27Sflorian 			s++;
2566ae8c6e27Sflorian 			continue;
2567ae8c6e27Sflorian 		}
2568ae8c6e27Sflorian 		if(!isxdigit((unsigned char)*s))
2569ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2570ae8c6e27Sflorian 		if(*len < dlen/2 + 1)
2571ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2572ae8c6e27Sflorian 				s-str);
2573ae8c6e27Sflorian 		if((dlen&1)==0)
2574ae8c6e27Sflorian 			rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16;
2575ae8c6e27Sflorian 		else	rd[dlen/2] += sldns_hexdigit_to_int(*s++);
2576ae8c6e27Sflorian 		dlen++;
2577ae8c6e27Sflorian 	}
2578ae8c6e27Sflorian 	if((dlen&1)!=0)
2579ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str);
2580ae8c6e27Sflorian 	*len = dlen/2;
2581ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2582ae8c6e27Sflorian }
2583ae8c6e27Sflorian 
sldns_str2wire_ipseckey_buf(const char * str,uint8_t * rd,size_t * len)2584ae8c6e27Sflorian int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len)
2585ae8c6e27Sflorian {
2586ae8c6e27Sflorian 	size_t gwlen = 0, keylen = 0;
2587ae8c6e27Sflorian 	int s;
2588ae8c6e27Sflorian 	uint8_t gwtype;
2589ae8c6e27Sflorian 	char token[512];
2590ae8c6e27Sflorian 	sldns_buffer strbuf;
2591ae8c6e27Sflorian 	sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str));
2592ae8c6e27Sflorian 
2593ae8c6e27Sflorian 	if(*len < 3)
2594ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2595ae8c6e27Sflorian 	/* precedence */
2596ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2597ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2598ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2599ae8c6e27Sflorian 	rd[0] = (uint8_t)atoi(token);
2600ae8c6e27Sflorian 	/* gateway_type */
2601ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2602ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2603ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2604ae8c6e27Sflorian 	rd[1] = (uint8_t)atoi(token);
2605ae8c6e27Sflorian 	gwtype = rd[1];
2606ae8c6e27Sflorian 	/* algorithm */
2607ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2608ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2609ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2610ae8c6e27Sflorian 	rd[2] = (uint8_t)atoi(token);
2611ae8c6e27Sflorian 
2612ae8c6e27Sflorian 	/* gateway */
2613ae8c6e27Sflorian 	if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0)
2614ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2615ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2616ae8c6e27Sflorian 	if(gwtype == 0) {
2617ae8c6e27Sflorian 		/* NOGATEWAY */
2618ae8c6e27Sflorian 		if(strcmp(token, ".") != 0)
2619ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2620ae8c6e27Sflorian 				sldns_buffer_position(&strbuf));
2621ae8c6e27Sflorian 		gwlen = 0;
2622ae8c6e27Sflorian 	} else if(gwtype == 1) {
2623ae8c6e27Sflorian 		/* IP4 */
2624ae8c6e27Sflorian 		gwlen = *len - 3;
2625ae8c6e27Sflorian 		s = sldns_str2wire_a_buf(token, rd+3, &gwlen);
2626ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2627ae8c6e27Sflorian 	} else if(gwtype == 2) {
2628ae8c6e27Sflorian 		/* IP6 */
2629ae8c6e27Sflorian 		gwlen = *len - 3;
2630ae8c6e27Sflorian 		s = sldns_str2wire_aaaa_buf(token, rd+3, &gwlen);
2631ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2632ae8c6e27Sflorian 	} else if(gwtype == 3) {
2633ae8c6e27Sflorian 		/* DNAME */
2634ae8c6e27Sflorian 		gwlen = *len - 3;
2635ae8c6e27Sflorian 		s = sldns_str2wire_dname_buf(token, rd+3, &gwlen);
2636ae8c6e27Sflorian 		if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2637ae8c6e27Sflorian 	} else {
2638ae8c6e27Sflorian 		/* unknown gateway type */
2639ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR,
2640ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2641ae8c6e27Sflorian 	}
2642ae8c6e27Sflorian 	/* double check for size */
2643ae8c6e27Sflorian 	if(*len < 3 + gwlen)
2644ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL,
2645ae8c6e27Sflorian 			sldns_buffer_position(&strbuf));
2646ae8c6e27Sflorian 
2647ae8c6e27Sflorian 	/* publickey in remainder of strbuf */
2648ae8c6e27Sflorian 	keylen = *len - 3 - gwlen;
2649ae8c6e27Sflorian 	s = sldns_str2wire_b64_buf((const char*)sldns_buffer_current(&strbuf),
2650ae8c6e27Sflorian 		rd+3+gwlen, &keylen);
2651ae8c6e27Sflorian 	if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf));
2652ae8c6e27Sflorian 
2653ae8c6e27Sflorian 	*len = 3 + gwlen + keylen;
2654ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2655ae8c6e27Sflorian }
2656ae8c6e27Sflorian 
sldns_str2wire_nsec3_salt_buf(const char * str,uint8_t * rd,size_t * len)2657ae8c6e27Sflorian int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len)
2658ae8c6e27Sflorian {
2659ae8c6e27Sflorian 	int i, salt_length_str = (int)strlen(str);
2660ae8c6e27Sflorian 	if (salt_length_str == 1 && str[0] == '-') {
2661ae8c6e27Sflorian 		salt_length_str = 0;
2662ae8c6e27Sflorian 	} else if (salt_length_str % 2 != 0) {
2663ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2664ae8c6e27Sflorian 	}
2665ae8c6e27Sflorian 	if (salt_length_str > 512)
2666ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_HEX;
2667ae8c6e27Sflorian 	if(*len < 1+(size_t)salt_length_str / 2)
2668ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2669ae8c6e27Sflorian 	rd[0] = (uint8_t) (salt_length_str / 2);
2670ae8c6e27Sflorian 	for (i = 0; i < salt_length_str; i += 2) {
2671ae8c6e27Sflorian 		if (isxdigit((unsigned char)str[i]) &&
2672ae8c6e27Sflorian 			isxdigit((unsigned char)str[i+1])) {
2673ae8c6e27Sflorian 			rd[1+i/2] = (uint8_t)(sldns_hexdigit_to_int(str[i])*16
2674ae8c6e27Sflorian 				+ sldns_hexdigit_to_int(str[i+1]));
2675ae8c6e27Sflorian 		} else {
2676ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, i);
2677ae8c6e27Sflorian 		}
2678ae8c6e27Sflorian 	}
2679ae8c6e27Sflorian 	*len = 1 + (size_t)rd[0];
2680ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2681ae8c6e27Sflorian }
2682ae8c6e27Sflorian 
sldns_str2wire_ilnp64_buf(const char * str,uint8_t * rd,size_t * len)2683ae8c6e27Sflorian int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len)
2684ae8c6e27Sflorian {
2685ae8c6e27Sflorian 	unsigned int a, b, c, d;
2686ae8c6e27Sflorian 	uint16_t shorts[4];
2687ae8c6e27Sflorian 	int l;
2688ae8c6e27Sflorian 	if(*len < sizeof(shorts))
2689ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2690ae8c6e27Sflorian 
2691ae8c6e27Sflorian 	if (sscanf(str, "%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 ||
2692ae8c6e27Sflorian 			l != (int)strlen(str) || /* more data to read */
2693ae8c6e27Sflorian 			strpbrk(str, "+-")       /* signed hexes */
2694ae8c6e27Sflorian 			)
2695ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_ILNP64;
2696ae8c6e27Sflorian 	shorts[0] = htons(a);
2697ae8c6e27Sflorian 	shorts[1] = htons(b);
2698ae8c6e27Sflorian 	shorts[2] = htons(c);
2699ae8c6e27Sflorian 	shorts[3] = htons(d);
2700ae8c6e27Sflorian 	memmove(rd, &shorts, sizeof(shorts));
2701ae8c6e27Sflorian 	*len = sizeof(shorts);
2702ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2703ae8c6e27Sflorian }
2704ae8c6e27Sflorian 
sldns_str2wire_eui48_buf(const char * str,uint8_t * rd,size_t * len)2705ae8c6e27Sflorian int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len)
2706ae8c6e27Sflorian {
2707ae8c6e27Sflorian 	unsigned int a, b, c, d, e, f;
2708ae8c6e27Sflorian 	int l;
2709ae8c6e27Sflorian 
2710ae8c6e27Sflorian 	if(*len < 6)
2711ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2712ae8c6e27Sflorian 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x%n",
2713ae8c6e27Sflorian 			&a, &b, &c, &d, &e, &f, &l) != 6 ||
2714ae8c6e27Sflorian 			l != (int)strlen(str))
2715ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI48;
2716ae8c6e27Sflorian 	rd[0] = a;
2717ae8c6e27Sflorian 	rd[1] = b;
2718ae8c6e27Sflorian 	rd[2] = c;
2719ae8c6e27Sflorian 	rd[3] = d;
2720ae8c6e27Sflorian 	rd[4] = e;
2721ae8c6e27Sflorian 	rd[5] = f;
2722ae8c6e27Sflorian 	*len = 6;
2723ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2724ae8c6e27Sflorian }
2725ae8c6e27Sflorian 
sldns_str2wire_eui64_buf(const char * str,uint8_t * rd,size_t * len)2726ae8c6e27Sflorian int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len)
2727ae8c6e27Sflorian {
2728ae8c6e27Sflorian 	unsigned int a, b, c, d, e, f, g, h;
2729ae8c6e27Sflorian 	int l;
2730ae8c6e27Sflorian 
2731ae8c6e27Sflorian 	if(*len < 8)
2732ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2733ae8c6e27Sflorian 	if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n",
2734ae8c6e27Sflorian 			&a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 ||
2735ae8c6e27Sflorian 			l != (int)strlen(str))
2736ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_EUI64;
2737ae8c6e27Sflorian 	rd[0] = a;
2738ae8c6e27Sflorian 	rd[1] = b;
2739ae8c6e27Sflorian 	rd[2] = c;
2740ae8c6e27Sflorian 	rd[3] = d;
2741ae8c6e27Sflorian 	rd[4] = e;
2742ae8c6e27Sflorian 	rd[5] = f;
2743ae8c6e27Sflorian 	rd[6] = g;
2744ae8c6e27Sflorian 	rd[7] = h;
2745ae8c6e27Sflorian 	*len = 8;
2746ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2747ae8c6e27Sflorian }
2748ae8c6e27Sflorian 
sldns_str2wire_tag_buf(const char * str,uint8_t * rd,size_t * len)2749ae8c6e27Sflorian int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len)
2750ae8c6e27Sflorian {
2751ae8c6e27Sflorian 	size_t slen = strlen(str);
2752ae8c6e27Sflorian 	const char* ptr;
2753ae8c6e27Sflorian 
2754ae8c6e27Sflorian 	if (slen > 255)
2755ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_TAG;
2756ae8c6e27Sflorian 	if(*len < slen+1)
2757ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2758ae8c6e27Sflorian 	for (ptr = str; *ptr; ptr++) {
2759ae8c6e27Sflorian 		if(!isalnum((unsigned char)*ptr))
2760ae8c6e27Sflorian 			return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TAG, ptr-str);
2761ae8c6e27Sflorian 	}
2762ae8c6e27Sflorian 	rd[0] = (uint8_t)slen;
2763ae8c6e27Sflorian 	memmove(rd+1, str, slen);
2764ae8c6e27Sflorian 	*len = slen+1;
2765ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2766ae8c6e27Sflorian }
2767ae8c6e27Sflorian 
sldns_str2wire_long_str_buf(const char * str,uint8_t * rd,size_t * len)2768ae8c6e27Sflorian int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len)
2769ae8c6e27Sflorian {
2770ae8c6e27Sflorian 	uint8_t ch = 0;
2771ae8c6e27Sflorian 	const char* pstr = str;
2772ae8c6e27Sflorian 	size_t length = 0;
2773ae8c6e27Sflorian 
2774ae8c6e27Sflorian 	/* Fill data with parsed bytes */
2775ae8c6e27Sflorian 	while (sldns_parse_char(&ch, &pstr)) {
2776ae8c6e27Sflorian 		if(*len < length+1)
2777ae8c6e27Sflorian 			return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2778ae8c6e27Sflorian 		rd[length++] = ch;
2779ae8c6e27Sflorian 	}
2780ae8c6e27Sflorian 	if(!pstr)
2781ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE;
2782ae8c6e27Sflorian 	*len = length;
2783ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2784ae8c6e27Sflorian }
2785ae8c6e27Sflorian 
sldns_str2wire_hip_buf(const char * str,uint8_t * rd,size_t * len)2786ae8c6e27Sflorian int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len)
2787ae8c6e27Sflorian {
2788ae8c6e27Sflorian 	char* s, *end;
2789ae8c6e27Sflorian 	int e;
2790ae8c6e27Sflorian 	size_t hitlen, pklen = 0;
2791ae8c6e27Sflorian 	/* presentation format:
2792ae8c6e27Sflorian 	 * 	pk-algo HIThex pubkeybase64
2793ae8c6e27Sflorian 	 * wireformat:
2794ae8c6e27Sflorian 	 * 	hitlen[1byte] pkalgo[1byte] pubkeylen[2byte] [hit] [pubkey] */
2795ae8c6e27Sflorian 	if(*len < 4)
2796ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2797ae8c6e27Sflorian 
2798ae8c6e27Sflorian 	/* read PK algorithm */
2799ae8c6e27Sflorian 	rd[1] = (uint8_t)strtol((char*)str, &s, 10);
2800ae8c6e27Sflorian 	if(*s != ' ')
2801ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2802ae8c6e27Sflorian 	s++;
2803ae8c6e27Sflorian 	while(*s == ' ')
2804ae8c6e27Sflorian 		s++;
2805ae8c6e27Sflorian 
2806ae8c6e27Sflorian 	/* read HIT hex tag */
2807ae8c6e27Sflorian 	/* zero terminate the tag (replace later) */
2808ae8c6e27Sflorian 	end = strchr(s, ' ');
2809ae8c6e27Sflorian 	if(!end) return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, s-(char*)str);
2810ae8c6e27Sflorian 	*end = 0;
2811ae8c6e27Sflorian 	hitlen = *len - 4;
2812ae8c6e27Sflorian 	if((e = sldns_str2wire_hex_buf(s, rd+4, &hitlen)) != 0) {
2813ae8c6e27Sflorian 		*end = ' ';
2814ae8c6e27Sflorian 		return RET_ERR_SHIFT(e, s-(char*)str);
2815ae8c6e27Sflorian 	}
2816ae8c6e27Sflorian 	if(hitlen > 255) {
2817ae8c6e27Sflorian 		*end = ' ';
2818ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+255*2);
2819ae8c6e27Sflorian 	}
2820ae8c6e27Sflorian 	rd[0] = (uint8_t)hitlen;
2821ae8c6e27Sflorian 	*end = ' ';
2822ae8c6e27Sflorian 	s = end+1;
2823ae8c6e27Sflorian 
2824ae8c6e27Sflorian 	/* read pubkey base64 sequence */
2825ae8c6e27Sflorian 	pklen = *len - 4 - hitlen;
2826ae8c6e27Sflorian 	if((e = sldns_str2wire_b64_buf(s, rd+4+hitlen, &pklen)) != 0)
2827ae8c6e27Sflorian 		return RET_ERR_SHIFT(e, s-(char*)str);
2828ae8c6e27Sflorian 	if(pklen > 65535)
2829ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+65535);
2830ae8c6e27Sflorian 	sldns_write_uint16(rd+2, (uint16_t)pklen);
2831ae8c6e27Sflorian 
2832ae8c6e27Sflorian 	*len = 4 + hitlen + pklen;
2833ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2834ae8c6e27Sflorian }
2835ae8c6e27Sflorian 
sldns_str2wire_int16_data_buf(const char * str,uint8_t * rd,size_t * len)2836ae8c6e27Sflorian int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len)
2837ae8c6e27Sflorian {
2838ae8c6e27Sflorian 	char* s;
2839ae8c6e27Sflorian 	int n;
2840ae8c6e27Sflorian 	n = strtol(str, &s, 10);
284157403691Sflorian 	if(n < 0) /* negative number not allowed */
284257403691Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX;
2843ae8c6e27Sflorian 	if(*len < ((size_t)n)+2)
2844ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL;
2845ae8c6e27Sflorian 	if(n > 65535)
2846ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW;
2847ae8c6e27Sflorian 
2848ae8c6e27Sflorian 	if(n == 0) {
2849ae8c6e27Sflorian 		sldns_write_uint16(rd, 0);
2850ae8c6e27Sflorian 		*len = 2;
2851ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_OK;
2852ae8c6e27Sflorian 	}
2853ae8c6e27Sflorian 	if(*s != ' ')
2854ae8c6e27Sflorian 		return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str);
2855ae8c6e27Sflorian 	s++;
2856ae8c6e27Sflorian 	while(*s == ' ')
2857ae8c6e27Sflorian 		s++;
2858ae8c6e27Sflorian 
2859ae8c6e27Sflorian 	n = sldns_b64_pton(s, rd+2, (*len)-2);
2860ae8c6e27Sflorian 	if(n < 0)
2861ae8c6e27Sflorian 		return LDNS_WIREPARSE_ERR_SYNTAX_B64;
2862ae8c6e27Sflorian 	sldns_write_uint16(rd, (uint16_t)n);
2863ae8c6e27Sflorian 	*len = ((size_t)n)+2;
2864ae8c6e27Sflorian 	return LDNS_WIREPARSE_ERR_OK;
2865ae8c6e27Sflorian }
2866