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