xref: /freebsd/contrib/ldns/dnssec.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*
2  * dnssec.c
3  *
4  * contains the cryptographic function needed for DNSSEC in ldns
5  * The crypto library used is openssl
6  *
7  * (c) NLnet Labs, 2004-2008
8  *
9  * See the file LICENSE for the license
10  */
11 
12 #include <ldns/config.h>
13 
14 #include <ldns/ldns.h>
15 #include <ldns/dnssec.h>
16 
17 #include <strings.h>
18 #include <time.h>
19 
20 #ifdef HAVE_SSL
21 #include <openssl/ssl.h>
22 #include <openssl/evp.h>
23 #include <openssl/rand.h>
24 #include <openssl/err.h>
25 #include <openssl/md5.h>
26 #endif
27 
28 ldns_rr *
29 ldns_dnssec_get_rrsig_for_name_and_type(const ldns_rdf *name,
30                                         const ldns_rr_type type,
31                                         const ldns_rr_list *rrs)
32 {
33 	size_t i;
34 	ldns_rr *candidate;
35 
36 	if (!name || !rrs) {
37 		return NULL;
38 	}
39 
40 	for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
41 		candidate = ldns_rr_list_rr(rrs, i);
42 		if (ldns_rr_get_type(candidate) == LDNS_RR_TYPE_RRSIG) {
43 			if (ldns_dname_compare(ldns_rr_owner(candidate),
44 			                       name) == 0 &&
45 			    ldns_rdf2rr_type(ldns_rr_rrsig_typecovered(candidate))
46 			    == type
47 			    ) {
48 				return candidate;
49 			}
50 		}
51 	}
52 
53 	return NULL;
54 }
55 
56 ldns_rr *
57 ldns_dnssec_get_dnskey_for_rrsig(const ldns_rr *rrsig,
58 						   const ldns_rr_list *rrs)
59 {
60 	size_t i;
61 	ldns_rr *candidate;
62 
63 	if (!rrsig || !rrs) {
64 		return NULL;
65 	}
66 
67 	for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
68 		candidate = ldns_rr_list_rr(rrs, i);
69 		if (ldns_rr_get_type(candidate) == LDNS_RR_TYPE_DNSKEY) {
70 			if (ldns_dname_compare(ldns_rr_owner(candidate),
71 			                       ldns_rr_rrsig_signame(rrsig)) == 0 &&
72 			    ldns_rdf2native_int16(ldns_rr_rrsig_keytag(rrsig)) ==
73 			    ldns_calc_keytag(candidate)
74 			    ) {
75 				return candidate;
76 			}
77 		}
78 	}
79 
80 	return NULL;
81 }
82 
83 ldns_rdf *
84 ldns_nsec_get_bitmap(ldns_rr *nsec) {
85 	if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC) {
86 		return ldns_rr_rdf(nsec, 1);
87 	} else if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC3) {
88 		return ldns_rr_rdf(nsec, 5);
89 	} else {
90 		return NULL;
91 	}
92 }
93 
94 /*return the owner name of the closest encloser for name from the list of rrs */
95 /* this is NOT the hash, but the original name! */
96 ldns_rdf *
97 ldns_dnssec_nsec3_closest_encloser(ldns_rdf *qname,
98                                    ATTR_UNUSED(ldns_rr_type qtype),
99                                    ldns_rr_list *nsec3s)
100 {
101 	/* remember parameters, they must match */
102 	uint8_t algorithm;
103 	uint32_t iterations;
104 	uint8_t salt_length;
105 	uint8_t *salt;
106 
107 	ldns_rdf *sname, *hashed_sname, *tmp;
108 	bool flag;
109 
110 	bool exact_match_found;
111 	bool in_range_found;
112 
113 	ldns_status status;
114 	ldns_rdf *zone_name;
115 
116 	size_t nsec_i;
117 	ldns_rr *nsec;
118 	ldns_rdf *result = NULL;
119 
120 	if (!qname || !nsec3s || ldns_rr_list_rr_count(nsec3s) < 1) {
121 		return NULL;
122 	}
123 
124 	nsec = ldns_rr_list_rr(nsec3s, 0);
125 	algorithm = ldns_nsec3_algorithm(nsec);
126 	salt_length = ldns_nsec3_salt_length(nsec);
127 	salt = ldns_nsec3_salt_data(nsec);
128 	iterations = ldns_nsec3_iterations(nsec);
129 
130 	sname = ldns_rdf_clone(qname);
131 
132 	flag = false;
133 
134 	zone_name = ldns_dname_left_chop(ldns_rr_owner(nsec));
135 
136 	/* algorithm from nsec3-07 8.3 */
137 	while (ldns_dname_label_count(sname) > 0) {
138 		exact_match_found = false;
139 		in_range_found = false;
140 
141 		hashed_sname = ldns_nsec3_hash_name(sname,
142 									 algorithm,
143 									 iterations,
144 									 salt_length,
145 									 salt);
146 
147 		status = ldns_dname_cat(hashed_sname, zone_name);
148                 if(status != LDNS_STATUS_OK) {
149 	                LDNS_FREE(salt);
150 	                ldns_rdf_deep_free(zone_name);
151 	                ldns_rdf_deep_free(sname);
152                         return NULL;
153                 }
154 
155 		for (nsec_i = 0; nsec_i < ldns_rr_list_rr_count(nsec3s); nsec_i++) {
156 			nsec = ldns_rr_list_rr(nsec3s, nsec_i);
157 
158 			/* check values of iterations etc! */
159 
160 			/* exact match? */
161 			if (ldns_dname_compare(ldns_rr_owner(nsec), hashed_sname) == 0) {
162 			 	exact_match_found = true;
163 			} else if (ldns_nsec_covers_name(nsec, hashed_sname)) {
164 				in_range_found = true;
165 			}
166 
167 		}
168 		if (!exact_match_found && in_range_found) {
169 			flag = true;
170 		} else if (exact_match_found && flag) {
171 			result = ldns_rdf_clone(sname);
172 			/* RFC 5155: 8.3. 2.** "The proof is complete" */
173 			ldns_rdf_deep_free(hashed_sname);
174 			goto done;
175 		} else if (exact_match_found && !flag) {
176 			/* error! */
177 			ldns_rdf_deep_free(hashed_sname);
178 			goto done;
179 		} else {
180 			flag = false;
181 		}
182 
183 		ldns_rdf_deep_free(hashed_sname);
184 		tmp = sname;
185 		sname = ldns_dname_left_chop(sname);
186 		ldns_rdf_deep_free(tmp);
187 	}
188 
189 	done:
190 	LDNS_FREE(salt);
191 	ldns_rdf_deep_free(zone_name);
192 	ldns_rdf_deep_free(sname);
193 
194 	return result;
195 }
196 
197 bool
198 ldns_dnssec_pkt_has_rrsigs(const ldns_pkt *pkt)
199 {
200 	size_t i;
201 	for (i = 0; i < ldns_pkt_ancount(pkt); i++) {
202 		if (ldns_rr_get_type(ldns_rr_list_rr(ldns_pkt_answer(pkt), i)) ==
203 		    LDNS_RR_TYPE_RRSIG) {
204 			return true;
205 		}
206 	}
207 	for (i = 0; i < ldns_pkt_nscount(pkt); i++) {
208 		if (ldns_rr_get_type(ldns_rr_list_rr(ldns_pkt_authority(pkt), i)) ==
209 		    LDNS_RR_TYPE_RRSIG) {
210 			return true;
211 		}
212 	}
213 	return false;
214 }
215 
216 ldns_rr_list *
217 ldns_dnssec_pkt_get_rrsigs_for_name_and_type(const ldns_pkt *pkt,
218 									ldns_rdf *name,
219 									ldns_rr_type type)
220 {
221 	uint16_t t_netorder;
222 	ldns_rr_list *sigs;
223 	ldns_rr_list *sigs_covered;
224 	ldns_rdf *rdf_t;
225 
226 	sigs = ldns_pkt_rr_list_by_name_and_type(pkt,
227 									 name,
228 									 LDNS_RR_TYPE_RRSIG,
229 									 LDNS_SECTION_ANY_NOQUESTION
230 									 );
231 
232 	t_netorder = htons(type); /* rdf are in network order! */
233 	rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE, LDNS_RDF_SIZE_WORD, &t_netorder);
234 	sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0);
235 
236 	ldns_rdf_free(rdf_t);
237 	ldns_rr_list_deep_free(sigs);
238 
239 	return sigs_covered;
240 
241 }
242 
243 ldns_rr_list *
244 ldns_dnssec_pkt_get_rrsigs_for_type(const ldns_pkt *pkt, ldns_rr_type type)
245 {
246 	uint16_t t_netorder;
247 	ldns_rr_list *sigs;
248 	ldns_rr_list *sigs_covered;
249 	ldns_rdf *rdf_t;
250 
251 	sigs = ldns_pkt_rr_list_by_type(pkt,
252 	                                LDNS_RR_TYPE_RRSIG,
253 	                                LDNS_SECTION_ANY_NOQUESTION
254 							  );
255 
256 	t_netorder = htons(type); /* rdf are in network order! */
257 	rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE,
258 					 2,
259 					 &t_netorder);
260 	sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0);
261 
262 	ldns_rdf_free(rdf_t);
263 	ldns_rr_list_deep_free(sigs);
264 
265 	return sigs_covered;
266 
267 }
268 
269 /* used only on the public key RR */
270 uint16_t
271 ldns_calc_keytag(const ldns_rr *key)
272 {
273 	uint16_t ac16;
274 	ldns_buffer *keybuf;
275 	size_t keysize;
276 
277 	if (!key) {
278 		return 0;
279 	}
280 
281 	if (ldns_rr_get_type(key) != LDNS_RR_TYPE_DNSKEY &&
282 	    ldns_rr_get_type(key) != LDNS_RR_TYPE_KEY
283 	    ) {
284 		return 0;
285 	}
286 
287 	/* rdata to buf - only put the rdata in a buffer */
288 	keybuf = ldns_buffer_new(LDNS_MIN_BUFLEN); /* grows */
289 	if (!keybuf) {
290 		return 0;
291 	}
292 	(void)ldns_rr_rdata2buffer_wire(keybuf, key);
293 	/* the current pos in the buffer is the keysize */
294 	keysize= ldns_buffer_position(keybuf);
295 
296 	ac16 = ldns_calc_keytag_raw(ldns_buffer_begin(keybuf), keysize);
297 	ldns_buffer_free(keybuf);
298 	return ac16;
299 }
300 
301 uint16_t ldns_calc_keytag_raw(uint8_t* key, size_t keysize)
302 {
303 	unsigned int i;
304 	uint32_t ac32;
305 	uint16_t ac16;
306 
307 	if(keysize < 4) {
308 		return 0;
309 	}
310 	/* look at the algorithm field, copied from 2535bis */
311 	if (key[3] == LDNS_RSAMD5) {
312 		ac16 = 0;
313 		if (keysize > 4) {
314 			memmove(&ac16, key + keysize - 3, 2);
315 		}
316 		ac16 = ntohs(ac16);
317 		return (uint16_t) ac16;
318 	} else {
319 		ac32 = 0;
320 		for (i = 0; (size_t)i < keysize; ++i) {
321 			ac32 += (i & 1) ? key[i] : key[i] << 8;
322 		}
323 		ac32 += (ac32 >> 16) & 0xFFFF;
324 		return (uint16_t) (ac32 & 0xFFFF);
325 	}
326 }
327 
328 #ifdef HAVE_SSL
329 DSA *
330 ldns_key_buf2dsa(ldns_buffer *key)
331 {
332 	return ldns_key_buf2dsa_raw((unsigned char*)ldns_buffer_begin(key),
333 						   ldns_buffer_position(key));
334 }
335 
336 DSA *
337 ldns_key_buf2dsa_raw(unsigned char* key, size_t len)
338 {
339 	uint8_t T;
340 	uint16_t length;
341 	uint16_t offset;
342 	DSA *dsa;
343 	BIGNUM *Q; BIGNUM *P;
344 	BIGNUM *G; BIGNUM *Y;
345 
346 	if(len == 0)
347 		return NULL;
348 	T = (uint8_t)key[0];
349 	length = (64 + T * 8);
350 	offset = 1;
351 
352 	if (T > 8) {
353 		return NULL;
354 	}
355 	if(len < (size_t)1 + SHA_DIGEST_LENGTH + 3*length)
356 		return NULL;
357 
358 	Q = BN_bin2bn(key+offset, SHA_DIGEST_LENGTH, NULL);
359 	offset += SHA_DIGEST_LENGTH;
360 
361 	P = BN_bin2bn(key+offset, (int)length, NULL);
362 	offset += length;
363 
364 	G = BN_bin2bn(key+offset, (int)length, NULL);
365 	offset += length;
366 
367 	Y = BN_bin2bn(key+offset, (int)length, NULL);
368 	offset += length;
369 
370 	/* create the key and set its properties */
371 	if(!Q || !P || !G || !Y || !(dsa = DSA_new())) {
372 		BN_free(Q);
373 		BN_free(P);
374 		BN_free(G);
375 		BN_free(Y);
376 		return NULL;
377 	}
378 #ifndef S_SPLINT_S
379 	dsa->p = P;
380 	dsa->q = Q;
381 	dsa->g = G;
382 	dsa->pub_key = Y;
383 #endif /* splint */
384 
385 	return dsa;
386 }
387 
388 RSA *
389 ldns_key_buf2rsa(ldns_buffer *key)
390 {
391 	return ldns_key_buf2rsa_raw((unsigned char*)ldns_buffer_begin(key),
392 						   ldns_buffer_position(key));
393 }
394 
395 RSA *
396 ldns_key_buf2rsa_raw(unsigned char* key, size_t len)
397 {
398 	uint16_t offset;
399 	uint16_t exp;
400 	uint16_t int16;
401 	RSA *rsa;
402 	BIGNUM *modulus;
403 	BIGNUM *exponent;
404 
405 	if (len == 0)
406 		return NULL;
407 	if (key[0] == 0) {
408 		if(len < 3)
409 			return NULL;
410 		/* need some smart comment here XXX*/
411 		/* the exponent is too large so it's places
412 		 * futher...???? */
413 		memmove(&int16, key+1, 2);
414 		exp = ntohs(int16);
415 		offset = 3;
416 	} else {
417 		exp = key[0];
418 		offset = 1;
419 	}
420 
421 	/* key length at least one */
422 	if(len < (size_t)offset + exp + 1)
423 		return NULL;
424 
425 	/* Exponent */
426 	exponent = BN_new();
427 	if(!exponent) return NULL;
428 	(void) BN_bin2bn(key+offset, (int)exp, exponent);
429 	offset += exp;
430 
431 	/* Modulus */
432 	modulus = BN_new();
433 	if(!modulus) {
434 		BN_free(exponent);
435 		return NULL;
436 	}
437 	/* length of the buffer must match the key length! */
438 	(void) BN_bin2bn(key+offset, (int)(len - offset), modulus);
439 
440 	rsa = RSA_new();
441 	if(!rsa) {
442 		BN_free(exponent);
443 		BN_free(modulus);
444 		return NULL;
445 	}
446 #ifndef S_SPLINT_S
447 	rsa->n = modulus;
448 	rsa->e = exponent;
449 #endif /* splint */
450 
451 	return rsa;
452 }
453 
454 int
455 ldns_digest_evp(unsigned char* data, unsigned int len, unsigned char* dest,
456 	const EVP_MD* md)
457 {
458 	EVP_MD_CTX* ctx;
459 	ctx = EVP_MD_CTX_create();
460 	if(!ctx)
461 		return false;
462 	if(!EVP_DigestInit_ex(ctx, md, NULL) ||
463 		!EVP_DigestUpdate(ctx, data, len) ||
464 		!EVP_DigestFinal_ex(ctx, dest, NULL)) {
465 		EVP_MD_CTX_destroy(ctx);
466 		return false;
467 	}
468 	EVP_MD_CTX_destroy(ctx);
469 	return true;
470 }
471 #endif /* HAVE_SSL */
472 
473 ldns_rr *
474 ldns_key_rr2ds(const ldns_rr *key, ldns_hash h)
475 {
476 	ldns_rdf *tmp;
477 	ldns_rr *ds;
478 	uint16_t keytag;
479 	uint8_t  sha1hash;
480 	uint8_t *digest;
481 	ldns_buffer *data_buf;
482 #ifdef USE_GOST
483 	const EVP_MD* md = NULL;
484 #endif
485 
486 	if (ldns_rr_get_type(key) != LDNS_RR_TYPE_DNSKEY) {
487 		return NULL;
488 	}
489 
490 	ds = ldns_rr_new();
491 	if (!ds) {
492 		return NULL;
493 	}
494 	ldns_rr_set_type(ds, LDNS_RR_TYPE_DS);
495 	ldns_rr_set_owner(ds, ldns_rdf_clone(
496 								  ldns_rr_owner(key)));
497 	ldns_rr_set_ttl(ds, ldns_rr_ttl(key));
498 	ldns_rr_set_class(ds, ldns_rr_get_class(key));
499 
500 	switch(h) {
501 	default:
502 	case LDNS_SHA1:
503 		digest = LDNS_XMALLOC(uint8_t, LDNS_SHA1_DIGEST_LENGTH);
504 		if (!digest) {
505 			ldns_rr_free(ds);
506 			return NULL;
507 		}
508 		break;
509 	case LDNS_SHA256:
510 		digest = LDNS_XMALLOC(uint8_t, LDNS_SHA256_DIGEST_LENGTH);
511 		if (!digest) {
512 			ldns_rr_free(ds);
513 			return NULL;
514 		}
515 		break;
516 	case LDNS_HASH_GOST:
517 #ifdef USE_GOST
518 		(void)ldns_key_EVP_load_gost_id();
519 		md = EVP_get_digestbyname("md_gost94");
520 		if(!md) {
521 			ldns_rr_free(ds);
522 			return NULL;
523 		}
524 		digest = LDNS_XMALLOC(uint8_t, EVP_MD_size(md));
525 		if (!digest) {
526 			ldns_rr_free(ds);
527 			return NULL;
528 		}
529                 break;
530 #else
531 		/* not implemented */
532 		ldns_rr_free(ds);
533 		return NULL;
534 #endif
535 	case LDNS_SHA384:
536 #ifdef USE_ECDSA
537 		digest = LDNS_XMALLOC(uint8_t, SHA384_DIGEST_LENGTH);
538 		if (!digest) {
539 			ldns_rr_free(ds);
540 			return NULL;
541 		}
542                 break;
543 #else
544 		/* not implemented */
545 		ldns_rr_free(ds);
546 		return NULL;
547 #endif
548 	}
549 
550 	data_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN);
551 	if (!data_buf) {
552 		LDNS_FREE(digest);
553 		ldns_rr_free(ds);
554 		return NULL;
555 	}
556 
557 	/* keytag */
558 	keytag = htons(ldns_calc_keytag((ldns_rr*)key));
559 	tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT16,
560 						   sizeof(uint16_t),
561 						   &keytag);
562 	ldns_rr_push_rdf(ds, tmp);
563 
564 	/* copy the algorithm field */
565 	if ((tmp = ldns_rr_rdf(key, 2)) == NULL) {
566 		LDNS_FREE(digest);
567 		ldns_buffer_free(data_buf);
568 		ldns_rr_free(ds);
569 		return NULL;
570 	} else {
571 		ldns_rr_push_rdf(ds, ldns_rdf_clone( tmp ));
572 	}
573 
574 	/* digest hash type */
575 	sha1hash = (uint8_t)h;
576 	tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8,
577 						   sizeof(uint8_t),
578 						   &sha1hash);
579 	ldns_rr_push_rdf(ds, tmp);
580 
581 	/* digest */
582 	/* owner name */
583 	tmp = ldns_rdf_clone(ldns_rr_owner(key));
584 	ldns_dname2canonical(tmp);
585 	if (ldns_rdf2buffer_wire(data_buf, tmp) != LDNS_STATUS_OK) {
586 		LDNS_FREE(digest);
587 		ldns_buffer_free(data_buf);
588 		ldns_rr_free(ds);
589 		ldns_rdf_deep_free(tmp);
590 		return NULL;
591 	}
592 	ldns_rdf_deep_free(tmp);
593 
594 	/* all the rdata's */
595 	if (ldns_rr_rdata2buffer_wire(data_buf,
596 							(ldns_rr*)key) != LDNS_STATUS_OK) {
597 		LDNS_FREE(digest);
598 		ldns_buffer_free(data_buf);
599 		ldns_rr_free(ds);
600 		return NULL;
601 	}
602 	switch(h) {
603 	case LDNS_SHA1:
604 		(void) ldns_sha1((unsigned char *) ldns_buffer_begin(data_buf),
605 		                 (unsigned int) ldns_buffer_position(data_buf),
606 		                 (unsigned char *) digest);
607 
608 		tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX,
609 		                            LDNS_SHA1_DIGEST_LENGTH,
610 		                            digest);
611 		ldns_rr_push_rdf(ds, tmp);
612 
613 		break;
614 	case LDNS_SHA256:
615 		(void) ldns_sha256((unsigned char *) ldns_buffer_begin(data_buf),
616 		                   (unsigned int) ldns_buffer_position(data_buf),
617 		                   (unsigned char *) digest);
618 		tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX,
619 		                            LDNS_SHA256_DIGEST_LENGTH,
620 		                            digest);
621 		ldns_rr_push_rdf(ds, tmp);
622 		break;
623 	case LDNS_HASH_GOST:
624 #ifdef USE_GOST
625 		if(!ldns_digest_evp((unsigned char *) ldns_buffer_begin(data_buf),
626 				(unsigned int) ldns_buffer_position(data_buf),
627 				(unsigned char *) digest, md)) {
628 			LDNS_FREE(digest);
629 			ldns_buffer_free(data_buf);
630 			ldns_rr_free(ds);
631 			return NULL;
632 		}
633 		tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX,
634 		                            (size_t)EVP_MD_size(md),
635 		                            digest);
636 		ldns_rr_push_rdf(ds, tmp);
637 #endif
638 		break;
639 	case LDNS_SHA384:
640 #ifdef USE_ECDSA
641 		(void) SHA384((unsigned char *) ldns_buffer_begin(data_buf),
642 		                 (unsigned int) ldns_buffer_position(data_buf),
643 		                 (unsigned char *) digest);
644 		tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX,
645 		                            SHA384_DIGEST_LENGTH,
646 		                            digest);
647 		ldns_rr_push_rdf(ds, tmp);
648 #endif
649 		break;
650 	}
651 
652 	LDNS_FREE(digest);
653 	ldns_buffer_free(data_buf);
654 	return ds;
655 }
656 
657 /* From RFC3845:
658  *
659  * 2.1.2.  The List of Type Bit Map(s) Field
660  *
661  *    The RR type space is split into 256 window blocks, each representing
662  *    the low-order 8 bits of the 16-bit RR type space.  Each block that
663  *    has at least one active RR type is encoded using a single octet
664  *    window number (from 0 to 255), a single octet bitmap length (from 1
665  *    to 32) indicating the number of octets used for the window block's
666  *    bitmap, and up to 32 octets (256 bits) of bitmap.
667  *
668  *    Window blocks are present in the NSEC RR RDATA in increasing
669  *    numerical order.
670  *
671  *    "|" denotes concatenation
672  *
673  *    Type Bit Map(s) Field = ( Window Block # | Bitmap Length | Bitmap ) +
674  *
675  *    <cut>
676  *
677  *    Blocks with no types present MUST NOT be included.  Trailing zero
678  *    octets in the bitmap MUST be omitted.  The length of each block's
679  *    bitmap is determined by the type code with the largest numerical
680  *    value within that block, among the set of RR types present at the
681  *    NSEC RR's owner name.  Trailing zero octets not specified MUST be
682  *    interpreted as zero octets.
683  */
684 ldns_rdf *
685 ldns_dnssec_create_nsec_bitmap(ldns_rr_type rr_type_list[],
686                                size_t size,
687                                ldns_rr_type nsec_type)
688 {
689 	uint8_t  window;		/*  most significant octet of type */
690 	uint8_t  subtype;		/* least significant octet of type */
691 	uint16_t windows[256]		/* Max subtype per window */
692 #ifndef S_SPLINT_S
693 	                      = { 0 }	/* Initialize ALL elements with 0 */
694 #endif
695 	                             ;
696 	ldns_rr_type* d;	/* used to traverse rr_type_list*/
697 	size_t i;		/* used to traverse windows array */
698 
699 	size_t sz;			/* size needed for type bitmap rdf */
700 	uint8_t* data = NULL;		/* rdf data */
701 	uint8_t* dptr;			/* used to itraverse rdf data */
702 	ldns_rdf* rdf;			/* bitmap rdf to return */
703 
704 	if (nsec_type != LDNS_RR_TYPE_NSEC &&
705 	    nsec_type != LDNS_RR_TYPE_NSEC3) {
706 		return NULL;
707 	}
708 
709 	/* Which other windows need to be in the bitmap rdf?
710 	 */
711 	for (d = rr_type_list; d < rr_type_list + size; d++) {
712 		window  = *d >> 8;
713 		subtype = *d & 0xff;
714 		if (windows[window] < subtype) {
715 			windows[window] = subtype;
716 		}
717 	}
718 
719 	/* How much space do we need in the rdf for those windows?
720 	 */
721 	sz = 0;
722 	for (i = 0; i < 256; i++) {
723 		if (windows[i]) {
724 			sz += windows[i] / 8 + 3;
725 		}
726 	}
727 	if (sz > 0) {
728 		/* Format rdf data according RFC3845 Section 2.1.2 (see above)
729 		 */
730 		dptr = data = LDNS_CALLOC(uint8_t, sz);
731 		if (!data) {
732 			return NULL;
733 		}
734 		for (i = 0; i < 256; i++) {
735 			if (windows[i]) {
736 				*dptr++ = (uint8_t)i;
737 				*dptr++ = (uint8_t)(windows[i] / 8 + 1);
738 
739 				/* Now let windows[i] index the bitmap
740 				 * within data
741 				 */
742 				windows[i] = (uint16_t)(dptr - data);
743 
744 				dptr += dptr[-1];
745 			}
746 		}
747 	}
748 
749 	/* Set the bits?
750 	 */
751 	for (d = rr_type_list; d < rr_type_list + size; d++) {
752 		subtype = *d & 0xff;
753 		data[windows[*d >> 8] + subtype/8] |= (0x80 >> (subtype % 8));
754 	}
755 
756 	/* Allocate and return rdf structure for the data
757 	 */
758 	rdf = ldns_rdf_new(LDNS_RDF_TYPE_BITMAP, sz, data);
759 	if (!rdf) {
760 		LDNS_FREE(data);
761 		return NULL;
762 	}
763 	return rdf;
764 }
765 
766 int
767 ldns_dnssec_rrsets_contains_type(ldns_dnssec_rrsets *rrsets,
768                                  ldns_rr_type type)
769 {
770 	ldns_dnssec_rrsets *cur_rrset = rrsets;
771 	while (cur_rrset) {
772 		if (cur_rrset->type == type) {
773 			return 1;
774 		}
775 		cur_rrset = cur_rrset->next;
776 	}
777 	return 0;
778 }
779 
780 ldns_rr *
781 ldns_dnssec_create_nsec(ldns_dnssec_name *from,
782                         ldns_dnssec_name *to,
783                         ldns_rr_type nsec_type)
784 {
785 	ldns_rr *nsec_rr;
786 	ldns_rr_type types[65536];
787 	size_t type_count = 0;
788 	ldns_dnssec_rrsets *cur_rrsets;
789 	int on_delegation_point;
790 
791 	if (!from || !to || (nsec_type != LDNS_RR_TYPE_NSEC)) {
792 		return NULL;
793 	}
794 
795 	nsec_rr = ldns_rr_new();
796 	ldns_rr_set_type(nsec_rr, nsec_type);
797 	ldns_rr_set_owner(nsec_rr, ldns_rdf_clone(ldns_dnssec_name_name(from)));
798 	ldns_rr_push_rdf(nsec_rr, ldns_rdf_clone(ldns_dnssec_name_name(to)));
799 
800 	on_delegation_point = ldns_dnssec_rrsets_contains_type(
801 			from->rrsets, LDNS_RR_TYPE_NS)
802 		&& !ldns_dnssec_rrsets_contains_type(
803 			from->rrsets, LDNS_RR_TYPE_SOA);
804 
805 	cur_rrsets = from->rrsets;
806 	while (cur_rrsets) {
807 		/* Do not include non-authoritative rrsets on the delegation point
808 		 * in the type bitmap */
809 		if ((on_delegation_point && (
810 				cur_rrsets->type == LDNS_RR_TYPE_NS
811 			     || cur_rrsets->type == LDNS_RR_TYPE_DS))
812 			|| (!on_delegation_point &&
813 				cur_rrsets->type != LDNS_RR_TYPE_RRSIG
814 			     && cur_rrsets->type != LDNS_RR_TYPE_NSEC)) {
815 
816 			types[type_count] = cur_rrsets->type;
817 			type_count++;
818 		}
819 		cur_rrsets = cur_rrsets->next;
820 
821 	}
822 	types[type_count] = LDNS_RR_TYPE_RRSIG;
823 	type_count++;
824 	types[type_count] = LDNS_RR_TYPE_NSEC;
825 	type_count++;
826 
827 	ldns_rr_push_rdf(nsec_rr, ldns_dnssec_create_nsec_bitmap(types,
828 	                               type_count,
829 	                               nsec_type));
830 
831 	return nsec_rr;
832 }
833 
834 ldns_rr *
835 ldns_dnssec_create_nsec3(ldns_dnssec_name *from,
836 					ldns_dnssec_name *to,
837 					ldns_rdf *zone_name,
838 					uint8_t algorithm,
839 					uint8_t flags,
840 					uint16_t iterations,
841 					uint8_t salt_length,
842 					uint8_t *salt)
843 {
844 	ldns_rr *nsec_rr;
845 	ldns_rr_type types[65536];
846 	size_t type_count = 0;
847 	ldns_dnssec_rrsets *cur_rrsets;
848 	ldns_status status;
849 	int on_delegation_point;
850 
851 	if (!from) {
852 		return NULL;
853 	}
854 
855 	nsec_rr = ldns_rr_new_frm_type(LDNS_RR_TYPE_NSEC3);
856 	ldns_rr_set_owner(nsec_rr,
857 	                  ldns_nsec3_hash_name(ldns_dnssec_name_name(from),
858 	                  algorithm,
859 	                  iterations,
860 	                  salt_length,
861 	                  salt));
862 	status = ldns_dname_cat(ldns_rr_owner(nsec_rr), zone_name);
863         if(status != LDNS_STATUS_OK) {
864                 ldns_rr_free(nsec_rr);
865                 return NULL;
866         }
867 	ldns_nsec3_add_param_rdfs(nsec_rr,
868 	                          algorithm,
869 	                          flags,
870 	                          iterations,
871 	                          salt_length,
872 	                          salt);
873 
874 	on_delegation_point = ldns_dnssec_rrsets_contains_type(
875 			from->rrsets, LDNS_RR_TYPE_NS)
876 		&& !ldns_dnssec_rrsets_contains_type(
877 			from->rrsets, LDNS_RR_TYPE_SOA);
878 	cur_rrsets = from->rrsets;
879 	while (cur_rrsets) {
880 		/* Do not include non-authoritative rrsets on the delegation point
881 		 * in the type bitmap. Potentionally not skipping insecure
882 		 * delegation should have been done earlier, in function
883 		 * ldns_dnssec_zone_create_nsec3s, or even earlier in:
884 		 * ldns_dnssec_zone_sign_nsec3_flg .
885 		 */
886 		if ((on_delegation_point && (
887 				cur_rrsets->type == LDNS_RR_TYPE_NS
888 			     || cur_rrsets->type == LDNS_RR_TYPE_DS))
889 			|| (!on_delegation_point &&
890 				cur_rrsets->type != LDNS_RR_TYPE_RRSIG)) {
891 
892 			types[type_count] = cur_rrsets->type;
893 			type_count++;
894 		}
895 		cur_rrsets = cur_rrsets->next;
896 	}
897 	/* always add rrsig type if this is not an unsigned
898 	 * delegation
899 	 */
900 	if (type_count > 0 &&
901 	    !(type_count == 1 && types[0] == LDNS_RR_TYPE_NS)) {
902 		types[type_count] = LDNS_RR_TYPE_RRSIG;
903 		type_count++;
904 	}
905 
906 	/* leave next rdata empty if they weren't precomputed yet */
907 	if (to && to->hashed_name) {
908 		(void) ldns_rr_set_rdf(nsec_rr,
909 		                       ldns_rdf_clone(to->hashed_name),
910 		                       4);
911 	} else {
912 		(void) ldns_rr_set_rdf(nsec_rr, NULL, 4);
913 	}
914 
915 	ldns_rr_push_rdf(nsec_rr,
916 	                 ldns_dnssec_create_nsec_bitmap(types,
917 	                 type_count,
918 	                 LDNS_RR_TYPE_NSEC3));
919 
920 	return nsec_rr;
921 }
922 
923 ldns_rr *
924 ldns_create_nsec(ldns_rdf *cur_owner, ldns_rdf *next_owner, ldns_rr_list *rrs)
925 {
926 	/* we do not do any check here - garbage in, garbage out */
927 
928 	/* the the start and end names - get the type from the
929 	 * before rrlist */
930 
931 	/* inefficient, just give it a name, a next name, and a list of rrs */
932 	/* we make 1 big uberbitmap first, then windows */
933 	/* todo: make something more efficient :) */
934 	uint16_t i;
935 	ldns_rr *i_rr;
936 	uint16_t i_type;
937 
938 	ldns_rr *nsec = NULL;
939 	ldns_rr_type i_type_list[65536];
940 	size_t type_count = 0;
941 
942 	nsec = ldns_rr_new();
943 	ldns_rr_set_type(nsec, LDNS_RR_TYPE_NSEC);
944 	ldns_rr_set_owner(nsec, ldns_rdf_clone(cur_owner));
945 	ldns_rr_push_rdf(nsec, ldns_rdf_clone(next_owner));
946 
947 	for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
948 		i_rr = ldns_rr_list_rr(rrs, i);
949 		if (ldns_rdf_compare(cur_owner,
950 						 ldns_rr_owner(i_rr)) == 0) {
951 			i_type = ldns_rr_get_type(i_rr);
952 			if (i_type != LDNS_RR_TYPE_RRSIG && i_type != LDNS_RR_TYPE_NSEC) {
953 				if (type_count == 0 || i_type_list[type_count-1] != i_type) {
954 					i_type_list[type_count] = i_type;
955 					type_count++;
956 				}
957 			}
958 		}
959 	}
960 
961 	i_type_list[type_count] = LDNS_RR_TYPE_RRSIG;
962 	type_count++;
963 	i_type_list[type_count] = LDNS_RR_TYPE_NSEC;
964 	type_count++;
965 
966 	ldns_rr_push_rdf(nsec,
967 				  ldns_dnssec_create_nsec_bitmap(i_type_list,
968 						type_count, LDNS_RR_TYPE_NSEC));
969 
970 	return nsec;
971 }
972 
973 ldns_rdf *
974 ldns_nsec3_hash_name(ldns_rdf *name,
975 				 uint8_t algorithm,
976 				 uint16_t iterations,
977 				 uint8_t salt_length,
978 				 uint8_t *salt)
979 {
980 	size_t hashed_owner_str_len;
981 	ldns_rdf *cann;
982 	ldns_rdf *hashed_owner;
983 	unsigned char *hashed_owner_str;
984 	char *hashed_owner_b32;
985 	size_t hashed_owner_b32_len;
986 	uint32_t cur_it;
987 	/* define to contain the largest possible hash, which is
988 	 * sha1 at the moment */
989 	unsigned char hash[LDNS_SHA1_DIGEST_LENGTH];
990 	ldns_status status;
991 
992 	/* TODO: mnemonic list for hash algs SHA-1, default to 1 now (sha1) */
993 	if (algorithm != LDNS_SHA1) {
994 		return NULL;
995 	}
996 
997 	/* prepare the owner name according to the draft section bla */
998 	cann = ldns_rdf_clone(name);
999 	if(!cann) {
1000 #ifdef STDERR_MSGS
1001 		fprintf(stderr, "Memory error\n");
1002 #endif
1003 		return NULL;
1004 	}
1005 	ldns_dname2canonical(cann);
1006 
1007 	hashed_owner_str_len = salt_length + ldns_rdf_size(cann);
1008 	hashed_owner_str = LDNS_XMALLOC(unsigned char, hashed_owner_str_len);
1009         if(!hashed_owner_str) {
1010 	        ldns_rdf_deep_free(cann);
1011                 return NULL;
1012         }
1013 	memcpy(hashed_owner_str, ldns_rdf_data(cann), ldns_rdf_size(cann));
1014 	memcpy(hashed_owner_str + ldns_rdf_size(cann), salt, salt_length);
1015 	ldns_rdf_deep_free(cann);
1016 
1017 	for (cur_it = iterations + 1; cur_it > 0; cur_it--) {
1018 		(void) ldns_sha1((unsigned char *) hashed_owner_str,
1019 		                 (unsigned int) hashed_owner_str_len, hash);
1020 
1021 		LDNS_FREE(hashed_owner_str);
1022 		hashed_owner_str_len = salt_length + LDNS_SHA1_DIGEST_LENGTH;
1023 		hashed_owner_str = LDNS_XMALLOC(unsigned char, hashed_owner_str_len);
1024 		if (!hashed_owner_str) {
1025 			return NULL;
1026 		}
1027 		memcpy(hashed_owner_str, hash, LDNS_SHA1_DIGEST_LENGTH);
1028 		memcpy(hashed_owner_str + LDNS_SHA1_DIGEST_LENGTH, salt, salt_length);
1029 		hashed_owner_str_len = LDNS_SHA1_DIGEST_LENGTH + salt_length;
1030 	}
1031 
1032 	LDNS_FREE(hashed_owner_str);
1033 	hashed_owner_str = hash;
1034 	hashed_owner_str_len = LDNS_SHA1_DIGEST_LENGTH;
1035 
1036 	hashed_owner_b32 = LDNS_XMALLOC(char,
1037                   ldns_b32_ntop_calculate_size(hashed_owner_str_len) + 1);
1038         if(!hashed_owner_b32) {
1039                 return NULL;
1040         }
1041         hashed_owner_b32_len = (size_t) ldns_b32_ntop_extended_hex(
1042                 (uint8_t *) hashed_owner_str,
1043                 hashed_owner_str_len,
1044                 hashed_owner_b32,
1045                 ldns_b32_ntop_calculate_size(hashed_owner_str_len)+1);
1046 	if (hashed_owner_b32_len < 1) {
1047 #ifdef STDERR_MSGS
1048 		fprintf(stderr, "Error in base32 extended hex encoding ");
1049 		fprintf(stderr, "of hashed owner name (name: ");
1050 		ldns_rdf_print(stderr, name);
1051 		fprintf(stderr, ", return code: %u)\n",
1052 		        (unsigned int) hashed_owner_b32_len);
1053 #endif
1054 		LDNS_FREE(hashed_owner_b32);
1055 		return NULL;
1056 	}
1057 	hashed_owner_b32[hashed_owner_b32_len] = '\0';
1058 
1059 	status = ldns_str2rdf_dname(&hashed_owner, hashed_owner_b32);
1060 	if (status != LDNS_STATUS_OK) {
1061 #ifdef STDERR_MSGS
1062 		fprintf(stderr, "Error creating rdf from %s\n", hashed_owner_b32);
1063 #endif
1064 		LDNS_FREE(hashed_owner_b32);
1065 		return NULL;
1066 	}
1067 
1068 	LDNS_FREE(hashed_owner_b32);
1069 	return hashed_owner;
1070 }
1071 
1072 void
1073 ldns_nsec3_add_param_rdfs(ldns_rr *rr,
1074 					 uint8_t algorithm,
1075 					 uint8_t flags,
1076 					 uint16_t iterations,
1077 					 uint8_t salt_length,
1078 					 uint8_t *salt)
1079 {
1080 	ldns_rdf *salt_rdf = NULL;
1081 	uint8_t *salt_data = NULL;
1082 	ldns_rdf *old;
1083 
1084 	old = ldns_rr_set_rdf(rr,
1085 	                      ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8,
1086 	                                            1, (void*)&algorithm),
1087 	                      0);
1088 	if (old) ldns_rdf_deep_free(old);
1089 
1090 	old = ldns_rr_set_rdf(rr,
1091 	                      ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8,
1092 	                                            1, (void*)&flags),
1093 	                      1);
1094 	if (old) ldns_rdf_deep_free(old);
1095 
1096 	old = ldns_rr_set_rdf(rr,
1097                           ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16,
1098                                                 iterations),
1099 	                      2);
1100 	if (old) ldns_rdf_deep_free(old);
1101 
1102 	salt_data = LDNS_XMALLOC(uint8_t, salt_length + 1);
1103         if(!salt_data) {
1104                 /* no way to return error */
1105                 return;
1106         }
1107 	salt_data[0] = salt_length;
1108 	memcpy(salt_data + 1, salt, salt_length);
1109 	salt_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_NSEC3_SALT,
1110 							   salt_length + 1,
1111 							   salt_data);
1112         if(!salt_rdf) {
1113                 LDNS_FREE(salt_data);
1114                 /* no way to return error */
1115                 return;
1116         }
1117 
1118 	old = ldns_rr_set_rdf(rr, salt_rdf, 3);
1119 	if (old) ldns_rdf_deep_free(old);
1120 	LDNS_FREE(salt_data);
1121 }
1122 
1123 static int
1124 rr_list_delegation_only(ldns_rdf *origin, ldns_rr_list *rr_list)
1125 {
1126 	size_t i;
1127 	ldns_rr *cur_rr;
1128 	if (!origin || !rr_list) return 0;
1129 	for (i = 0; i < ldns_rr_list_rr_count(rr_list); i++) {
1130 		cur_rr = ldns_rr_list_rr(rr_list, i);
1131 		if (ldns_dname_compare(ldns_rr_owner(cur_rr), origin) == 0) {
1132 			return 0;
1133 		}
1134 		if (ldns_rr_get_type(cur_rr) != LDNS_RR_TYPE_NS) {
1135 			return 0;
1136 		}
1137 	}
1138 	return 1;
1139 }
1140 
1141 /* this will NOT return the NSEC3  completed, you will have to run the
1142    finalize function on the rrlist later! */
1143 ldns_rr *
1144 ldns_create_nsec3(ldns_rdf *cur_owner,
1145                   ldns_rdf *cur_zone,
1146                   ldns_rr_list *rrs,
1147                   uint8_t algorithm,
1148                   uint8_t flags,
1149                   uint16_t iterations,
1150                   uint8_t salt_length,
1151                   uint8_t *salt,
1152                   bool emptynonterminal)
1153 {
1154 	size_t i;
1155 	ldns_rr *i_rr;
1156 	uint16_t i_type;
1157 
1158 	ldns_rr *nsec = NULL;
1159 	ldns_rdf *hashed_owner = NULL;
1160 
1161 	ldns_status status;
1162 
1163     ldns_rr_type i_type_list[1024];
1164 	size_t type_count = 0;
1165 
1166 	hashed_owner = ldns_nsec3_hash_name(cur_owner,
1167 								 algorithm,
1168 								 iterations,
1169 								 salt_length,
1170 								 salt);
1171 	status = ldns_dname_cat(hashed_owner, cur_zone);
1172         if(status != LDNS_STATUS_OK) {
1173 		ldns_rdf_deep_free(hashed_owner);
1174                 return NULL;
1175 	}
1176 	nsec = ldns_rr_new_frm_type(LDNS_RR_TYPE_NSEC3);
1177         if(!nsec) {
1178 		ldns_rdf_deep_free(hashed_owner);
1179                 return NULL;
1180 	}
1181 	ldns_rr_set_type(nsec, LDNS_RR_TYPE_NSEC3);
1182 	ldns_rr_set_owner(nsec, hashed_owner);
1183 
1184 	ldns_nsec3_add_param_rdfs(nsec,
1185 						 algorithm,
1186 						 flags,
1187 						 iterations,
1188 						 salt_length,
1189 						 salt);
1190 	(void) ldns_rr_set_rdf(nsec, NULL, 4);
1191 
1192 
1193 	for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) {
1194 		i_rr = ldns_rr_list_rr(rrs, i);
1195 		if (ldns_rdf_compare(cur_owner,
1196 						 ldns_rr_owner(i_rr)) == 0) {
1197 			i_type = ldns_rr_get_type(i_rr);
1198 			if (type_count == 0 || i_type_list[type_count-1] != i_type) {
1199 				i_type_list[type_count] = i_type;
1200 				type_count++;
1201 			}
1202 		}
1203 	}
1204 
1205 	/* add RRSIG anyway, but only if this is not an ENT or
1206 	 * an unsigned delegation */
1207 	if (!emptynonterminal && !rr_list_delegation_only(cur_zone, rrs)) {
1208 		i_type_list[type_count] = LDNS_RR_TYPE_RRSIG;
1209 		type_count++;
1210 	}
1211 
1212 	/* and SOA if owner == zone */
1213 	if (ldns_dname_compare(cur_zone, cur_owner) == 0) {
1214 		i_type_list[type_count] = LDNS_RR_TYPE_SOA;
1215 		type_count++;
1216 	}
1217 
1218 	ldns_rr_push_rdf(nsec,
1219 				  ldns_dnssec_create_nsec_bitmap(i_type_list,
1220 						type_count, LDNS_RR_TYPE_NSEC3));
1221 
1222 	return nsec;
1223 }
1224 
1225 uint8_t
1226 ldns_nsec3_algorithm(const ldns_rr *nsec3_rr)
1227 {
1228 	if (nsec3_rr &&
1229 	      (ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 ||
1230 	       ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM)
1231 	    && (ldns_rr_rdf(nsec3_rr, 0) != NULL)
1232 	    && ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 0)) > 0) {
1233 		return ldns_rdf2native_int8(ldns_rr_rdf(nsec3_rr, 0));
1234 	}
1235 	return 0;
1236 }
1237 
1238 uint8_t
1239 ldns_nsec3_flags(const ldns_rr *nsec3_rr)
1240 {
1241 	if (nsec3_rr &&
1242 	      (ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 ||
1243 	       ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM)
1244 	    && (ldns_rr_rdf(nsec3_rr, 1) != NULL)
1245 	    && ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 1)) > 0) {
1246 		return ldns_rdf2native_int8(ldns_rr_rdf(nsec3_rr, 1));
1247 	}
1248 	return 0;
1249 }
1250 
1251 bool
1252 ldns_nsec3_optout(const ldns_rr *nsec3_rr)
1253 {
1254 	return (ldns_nsec3_flags(nsec3_rr) & LDNS_NSEC3_VARS_OPTOUT_MASK);
1255 }
1256 
1257 uint16_t
1258 ldns_nsec3_iterations(const ldns_rr *nsec3_rr)
1259 {
1260 	if (nsec3_rr &&
1261 	      (ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 ||
1262 	       ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM)
1263 	    && (ldns_rr_rdf(nsec3_rr, 2) != NULL)
1264 	    && ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 2)) > 0) {
1265 		return ldns_rdf2native_int16(ldns_rr_rdf(nsec3_rr, 2));
1266 	}
1267 	return 0;
1268 
1269 }
1270 
1271 ldns_rdf *
1272 ldns_nsec3_salt(const ldns_rr *nsec3_rr)
1273 {
1274 	if (nsec3_rr &&
1275 	      (ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 ||
1276 	       ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM)
1277 	    ) {
1278 		return ldns_rr_rdf(nsec3_rr, 3);
1279 	}
1280 	return NULL;
1281 }
1282 
1283 uint8_t
1284 ldns_nsec3_salt_length(const ldns_rr *nsec3_rr)
1285 {
1286 	ldns_rdf *salt_rdf = ldns_nsec3_salt(nsec3_rr);
1287 	if (salt_rdf && ldns_rdf_size(salt_rdf) > 0) {
1288 		return (uint8_t) ldns_rdf_data(salt_rdf)[0];
1289 	}
1290 	return 0;
1291 }
1292 
1293 /* allocs data, free with LDNS_FREE() */
1294 uint8_t *
1295 ldns_nsec3_salt_data(const ldns_rr *nsec3_rr)
1296 {
1297 	uint8_t salt_length;
1298 	uint8_t *salt;
1299 
1300 	ldns_rdf *salt_rdf = ldns_nsec3_salt(nsec3_rr);
1301 	if (salt_rdf && ldns_rdf_size(salt_rdf) > 0) {
1302 	    	salt_length = ldns_rdf_data(salt_rdf)[0];
1303 		salt = LDNS_XMALLOC(uint8_t, salt_length);
1304                 if(!salt) return NULL;
1305 		memcpy(salt, &ldns_rdf_data(salt_rdf)[1], salt_length);
1306 		return salt;
1307 	}
1308 	return NULL;
1309 }
1310 
1311 ldns_rdf *
1312 ldns_nsec3_next_owner(const ldns_rr *nsec3_rr)
1313 {
1314 	if (!nsec3_rr || ldns_rr_get_type(nsec3_rr) != LDNS_RR_TYPE_NSEC3) {
1315 		return NULL;
1316 	} else {
1317 		return ldns_rr_rdf(nsec3_rr, 4);
1318 	}
1319 }
1320 
1321 ldns_rdf *
1322 ldns_nsec3_bitmap(const ldns_rr *nsec3_rr)
1323 {
1324 	if (!nsec3_rr || ldns_rr_get_type(nsec3_rr) != LDNS_RR_TYPE_NSEC3) {
1325 		return NULL;
1326 	} else {
1327 		return ldns_rr_rdf(nsec3_rr, 5);
1328 	}
1329 }
1330 
1331 ldns_rdf *
1332 ldns_nsec3_hash_name_frm_nsec3(const ldns_rr *nsec, ldns_rdf *name)
1333 {
1334 	uint8_t algorithm;
1335 	uint16_t iterations;
1336 	uint8_t salt_length;
1337 	uint8_t *salt = 0;
1338 
1339 	ldns_rdf *hashed_owner;
1340 
1341 	algorithm = ldns_nsec3_algorithm(nsec);
1342 	salt_length = ldns_nsec3_salt_length(nsec);
1343 	salt = ldns_nsec3_salt_data(nsec);
1344 	iterations = ldns_nsec3_iterations(nsec);
1345 
1346 	hashed_owner = ldns_nsec3_hash_name(name,
1347 								 algorithm,
1348 								 iterations,
1349 								 salt_length,
1350 								 salt);
1351 
1352 	LDNS_FREE(salt);
1353 	return hashed_owner;
1354 }
1355 
1356 bool
1357 ldns_nsec_bitmap_covers_type(const  ldns_rdf* bitmap, ldns_rr_type type)
1358 {
1359 	uint8_t* dptr;
1360 	uint8_t* dend;
1361 
1362 	/* From RFC3845 Section 2.1.2:
1363 	 *
1364 	 *	"The RR type space is split into 256 window blocks, each re-
1365 	 *	 presenting the low-order 8 bits of the 16-bit RR type space."
1366 	 */
1367 	uint8_t  window = type >> 8;
1368 	uint8_t subtype = type & 0xff;
1369 
1370 	if (! bitmap) {
1371 		return false;
1372 	}
1373 	assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP);
1374 
1375 	dptr = ldns_rdf_data(bitmap);
1376 	dend = ldns_rdf_data(bitmap) + ldns_rdf_size(bitmap);
1377 
1378 	/* Type Bitmap = ( Window Block # | Bitmap Length | Bitmap ) +
1379 	 *                 dptr[0]          dptr[1]         dptr[2:]
1380 	 */
1381 	while (dptr < dend && dptr[0] <= window) {
1382 
1383 		if (dptr[0] == window && subtype / 8 < dptr[1] &&
1384 				dptr + dptr[1] + 2 <= dend) {
1385 
1386 			return dptr[2 + subtype / 8] & (0x80 >> (subtype % 8));
1387 		}
1388 		dptr += dptr[1] + 2; /* next window */
1389 	}
1390 	return false;
1391 }
1392 
1393 ldns_status
1394 ldns_nsec_bitmap_set_type(ldns_rdf* bitmap, ldns_rr_type type)
1395 {
1396 	uint8_t* dptr;
1397 	uint8_t* dend;
1398 
1399 	/* From RFC3845 Section 2.1.2:
1400 	 *
1401 	 *	"The RR type space is split into 256 window blocks, each re-
1402 	 *	 presenting the low-order 8 bits of the 16-bit RR type space."
1403 	 */
1404 	uint8_t  window = type >> 8;
1405 	uint8_t subtype = type & 0xff;
1406 
1407 	if (! bitmap) {
1408 		return false;
1409 	}
1410 	assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP);
1411 
1412 	dptr = ldns_rdf_data(bitmap);
1413 	dend = ldns_rdf_data(bitmap) + ldns_rdf_size(bitmap);
1414 
1415 	/* Type Bitmap = ( Window Block # | Bitmap Length | Bitmap ) +
1416 	 *                 dptr[0]          dptr[1]         dptr[2:]
1417 	 */
1418 	while (dptr < dend && dptr[0] <= window) {
1419 
1420 		if (dptr[0] == window && subtype / 8 < dptr[1] &&
1421 				dptr + dptr[1] + 2 <= dend) {
1422 
1423 			dptr[2 + subtype / 8] |= (0x80 >> (subtype % 8));
1424 			return LDNS_STATUS_OK;
1425 		}
1426 		dptr += dptr[1] + 2; /* next window */
1427 	}
1428 	return LDNS_STATUS_TYPE_NOT_IN_BITMAP;
1429 }
1430 
1431 ldns_status
1432 ldns_nsec_bitmap_clear_type(ldns_rdf* bitmap, ldns_rr_type type)
1433 {
1434 	uint8_t* dptr;
1435 	uint8_t* dend;
1436 
1437 	/* From RFC3845 Section 2.1.2:
1438 	 *
1439 	 *	"The RR type space is split into 256 window blocks, each re-
1440 	 *	 presenting the low-order 8 bits of the 16-bit RR type space."
1441 	 */
1442 	uint8_t  window = type >> 8;
1443 	uint8_t subtype = type & 0xff;
1444 
1445 	if (! bitmap) {
1446 		return false;
1447 	}
1448 
1449 	assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP);
1450 
1451 	dptr = ldns_rdf_data(bitmap);
1452 	dend = ldns_rdf_data(bitmap) + ldns_rdf_size(bitmap);
1453 
1454 	/* Type Bitmap = ( Window Block # | Bitmap Length | Bitmap ) +
1455 	 *                 dptr[0]          dptr[1]         dptr[2:]
1456 	 */
1457 	while (dptr < dend && dptr[0] <= window) {
1458 
1459 		if (dptr[0] == window && subtype / 8 < dptr[1] &&
1460 				dptr + dptr[1] + 2 <= dend) {
1461 
1462 			dptr[2 + subtype / 8] &= ~(0x80 >> (subtype % 8));
1463 			return LDNS_STATUS_OK;
1464 		}
1465 		dptr += dptr[1] + 2; /* next window */
1466 	}
1467 	return LDNS_STATUS_TYPE_NOT_IN_BITMAP;
1468 }
1469 
1470 
1471 bool
1472 ldns_nsec_covers_name(const ldns_rr *nsec, const ldns_rdf *name)
1473 {
1474 	ldns_rdf *nsec_owner = ldns_rr_owner(nsec);
1475 	ldns_rdf *hash_next;
1476 	char *next_hash_str;
1477 	ldns_rdf *nsec_next = NULL;
1478 	ldns_status status;
1479 	ldns_rdf *chopped_dname;
1480 	bool result;
1481 
1482 	if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC) {
1483 		if (ldns_rr_rdf(nsec, 0) != NULL) {
1484 			nsec_next = ldns_rdf_clone(ldns_rr_rdf(nsec, 0));
1485 		} else {
1486 			return false;
1487 		}
1488 	} else if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC3) {
1489 		hash_next = ldns_nsec3_next_owner(nsec);
1490 		next_hash_str = ldns_rdf2str(hash_next);
1491 		nsec_next = ldns_dname_new_frm_str(next_hash_str);
1492 		LDNS_FREE(next_hash_str);
1493 		chopped_dname = ldns_dname_left_chop(nsec_owner);
1494 		status = ldns_dname_cat(nsec_next, chopped_dname);
1495 		ldns_rdf_deep_free(chopped_dname);
1496 		if (status != LDNS_STATUS_OK) {
1497 			printf("error catting: %s\n", ldns_get_errorstr_by_id(status));
1498 		}
1499 	} else {
1500 		ldns_rdf_deep_free(nsec_next);
1501 		return false;
1502 	}
1503 
1504 	/* in the case of the last nsec */
1505 	if(ldns_dname_compare(nsec_owner, nsec_next) > 0) {
1506 		result = (ldns_dname_compare(nsec_owner, name) <= 0 ||
1507 				ldns_dname_compare(name, nsec_next) < 0);
1508 	} else if(ldns_dname_compare(nsec_owner, nsec_next) < 0) {
1509 		result = (ldns_dname_compare(nsec_owner, name) <= 0 &&
1510 		          ldns_dname_compare(name, nsec_next) < 0);
1511 	} else {
1512 		result = true;
1513 	}
1514 
1515 	ldns_rdf_deep_free(nsec_next);
1516 	return result;
1517 }
1518 
1519 #ifdef HAVE_SSL
1520 /* sig may be null - if so look in the packet */
1521 
1522 ldns_status
1523 ldns_pkt_verify_time(ldns_pkt *p, ldns_rr_type t, ldns_rdf *o,
1524 		ldns_rr_list *k, ldns_rr_list *s,
1525 		time_t check_time, ldns_rr_list *good_keys)
1526 {
1527 	ldns_rr_list *rrset;
1528 	ldns_rr_list *sigs;
1529 	ldns_rr_list *sigs_covered;
1530 	ldns_rdf *rdf_t;
1531 	ldns_rr_type t_netorder;
1532 
1533 	if (!k) {
1534 		return LDNS_STATUS_ERR;
1535 		/* return LDNS_STATUS_CRYPTO_NO_DNSKEY; */
1536 	}
1537 
1538 	if (t == LDNS_RR_TYPE_RRSIG) {
1539 		/* we don't have RRSIG(RRSIG) (yet? ;-) ) */
1540 		return LDNS_STATUS_ERR;
1541 	}
1542 
1543 	if (s) {
1544 		/* if s is not NULL, the sigs are given to use */
1545 		sigs = s;
1546 	} else {
1547 		/* otherwise get them from the packet */
1548 		sigs = ldns_pkt_rr_list_by_name_and_type(p, o,
1549 				LDNS_RR_TYPE_RRSIG,
1550 				LDNS_SECTION_ANY_NOQUESTION);
1551 		if (!sigs) {
1552 			/* no sigs */
1553 			return LDNS_STATUS_ERR;
1554 			/* return LDNS_STATUS_CRYPTO_NO_RRSIG; */
1555 		}
1556 	}
1557 
1558 	/* rrsig are subtyped, so now we need to find the correct
1559 	 * sigs for the type t
1560 	 */
1561 	t_netorder = htons(t); /* rdf are in network order! */
1562 	/* a type identifier is a 16-bit number, so the size is 2 bytes */
1563 	rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE, 2, &t_netorder);
1564 
1565 	sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0);
1566 	ldns_rdf_free(rdf_t);
1567 	if (! sigs_covered) {
1568 		if (! s) {
1569 			ldns_rr_list_deep_free(sigs);
1570 		}
1571 		return LDNS_STATUS_ERR;
1572 	}
1573 	ldns_rr_list_deep_free(sigs_covered);
1574 
1575 	rrset = ldns_pkt_rr_list_by_name_and_type(p, o, t,
1576 			LDNS_SECTION_ANY_NOQUESTION);
1577 	if (!rrset) {
1578 		if (! s) {
1579 			ldns_rr_list_deep_free(sigs);
1580 		}
1581 		return LDNS_STATUS_ERR;
1582 	}
1583 	return ldns_verify_time(rrset, sigs, k, check_time, good_keys);
1584 }
1585 
1586 ldns_status
1587 ldns_pkt_verify(ldns_pkt *p, ldns_rr_type t, ldns_rdf *o,
1588 		ldns_rr_list *k, ldns_rr_list *s, ldns_rr_list *good_keys)
1589 {
1590 	return ldns_pkt_verify_time(p, t, o, k, s, ldns_time(NULL), good_keys);
1591 }
1592 #endif /* HAVE_SSL */
1593 
1594 ldns_status
1595 ldns_dnssec_chain_nsec3_list(ldns_rr_list *nsec3_rrs)
1596 {
1597 	size_t i;
1598 	char *next_nsec_owner_str;
1599 	ldns_rdf *next_nsec_owner_label;
1600 	ldns_rdf *next_nsec_rdf;
1601 	ldns_status status = LDNS_STATUS_OK;
1602 
1603 	for (i = 0; i < ldns_rr_list_rr_count(nsec3_rrs); i++) {
1604 		if (i == ldns_rr_list_rr_count(nsec3_rrs) - 1) {
1605 			next_nsec_owner_label =
1606 				ldns_dname_label(ldns_rr_owner(ldns_rr_list_rr(nsec3_rrs,
1607 													  0)), 0);
1608 			next_nsec_owner_str = ldns_rdf2str(next_nsec_owner_label);
1609 			if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1610 			    == '.') {
1611 				next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1612 					= '\0';
1613 			}
1614 			status = ldns_str2rdf_b32_ext(&next_nsec_rdf,
1615 									next_nsec_owner_str);
1616 			if (!ldns_rr_set_rdf(ldns_rr_list_rr(nsec3_rrs, i),
1617 							 next_nsec_rdf, 4)) {
1618 				/* todo: error */
1619 			}
1620 
1621 			ldns_rdf_deep_free(next_nsec_owner_label);
1622 			LDNS_FREE(next_nsec_owner_str);
1623 		} else {
1624 			next_nsec_owner_label =
1625 				ldns_dname_label(ldns_rr_owner(ldns_rr_list_rr(nsec3_rrs,
1626 													  i + 1)),
1627 							  0);
1628 			next_nsec_owner_str = ldns_rdf2str(next_nsec_owner_label);
1629 			if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1630 			    == '.') {
1631 				next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1632 					= '\0';
1633 			}
1634 			status = ldns_str2rdf_b32_ext(&next_nsec_rdf,
1635 									next_nsec_owner_str);
1636 			ldns_rdf_deep_free(next_nsec_owner_label);
1637 			LDNS_FREE(next_nsec_owner_str);
1638 			if (!ldns_rr_set_rdf(ldns_rr_list_rr(nsec3_rrs, i),
1639 							 next_nsec_rdf, 4)) {
1640 				/* todo: error */
1641 			}
1642 		}
1643 	}
1644 	return status;
1645 }
1646 
1647 int
1648 qsort_rr_compare_nsec3(const void *a, const void *b)
1649 {
1650 	const ldns_rr *rr1 = * (const ldns_rr **) a;
1651 	const ldns_rr *rr2 = * (const ldns_rr **) b;
1652 	if (rr1 == NULL && rr2 == NULL) {
1653 		return 0;
1654 	}
1655 	if (rr1 == NULL) {
1656 		return -1;
1657 	}
1658 	if (rr2 == NULL) {
1659 		return 1;
1660 	}
1661 	return ldns_rdf_compare(ldns_rr_owner(rr1), ldns_rr_owner(rr2));
1662 }
1663 
1664 void
1665 ldns_rr_list_sort_nsec3(ldns_rr_list *unsorted)
1666 {
1667 	qsort(unsorted->_rrs,
1668 	      ldns_rr_list_rr_count(unsorted),
1669 	      sizeof(ldns_rr *),
1670 	      qsort_rr_compare_nsec3);
1671 }
1672 
1673 int
1674 ldns_dnssec_default_add_to_signatures( ATTR_UNUSED(ldns_rr *sig)
1675 				     , ATTR_UNUSED(void *n)
1676 				     )
1677 {
1678 	return LDNS_SIGNATURE_LEAVE_ADD_NEW;
1679 }
1680 
1681 int
1682 ldns_dnssec_default_leave_signatures( ATTR_UNUSED(ldns_rr *sig)
1683 				    , ATTR_UNUSED(void *n)
1684 				    )
1685 {
1686 	return LDNS_SIGNATURE_LEAVE_NO_ADD;
1687 }
1688 
1689 int
1690 ldns_dnssec_default_delete_signatures( ATTR_UNUSED(ldns_rr *sig)
1691 				     , ATTR_UNUSED(void *n)
1692 				     )
1693 {
1694 	return LDNS_SIGNATURE_REMOVE_NO_ADD;
1695 }
1696 
1697 int
1698 ldns_dnssec_default_replace_signatures( ATTR_UNUSED(ldns_rr *sig)
1699 				      , ATTR_UNUSED(void *n)
1700 				      )
1701 {
1702 	return LDNS_SIGNATURE_REMOVE_ADD_NEW;
1703 }
1704 
1705 #ifdef HAVE_SSL
1706 ldns_rdf *
1707 ldns_convert_dsa_rrsig_asn12rdf(const ldns_buffer *sig,
1708 						  const long sig_len)
1709 {
1710 	ldns_rdf *sigdata_rdf;
1711 	DSA_SIG *dsasig;
1712 	unsigned char *dsasig_data = (unsigned char*)ldns_buffer_begin(sig);
1713 	size_t byte_offset;
1714 
1715 	dsasig = d2i_DSA_SIG(NULL,
1716 					 (const unsigned char **)&dsasig_data,
1717 					 sig_len);
1718 	if (!dsasig) {
1719                 DSA_SIG_free(dsasig);
1720 		return NULL;
1721 	}
1722 
1723 	dsasig_data = LDNS_XMALLOC(unsigned char, 41);
1724         if(!dsasig_data) {
1725                 DSA_SIG_free(dsasig);
1726                 return NULL;
1727         }
1728 	dsasig_data[0] = 0;
1729 	byte_offset = (size_t) (20 - BN_num_bytes(dsasig->r));
1730 	if (byte_offset > 20) {
1731                 DSA_SIG_free(dsasig);
1732                 LDNS_FREE(dsasig_data);
1733 		return NULL;
1734 	}
1735 	memset(&dsasig_data[1], 0, byte_offset);
1736 	BN_bn2bin(dsasig->r, &dsasig_data[1 + byte_offset]);
1737 	byte_offset = (size_t) (20 - BN_num_bytes(dsasig->s));
1738 	if (byte_offset > 20) {
1739                 DSA_SIG_free(dsasig);
1740                 LDNS_FREE(dsasig_data);
1741 		return NULL;
1742 	}
1743 	memset(&dsasig_data[21], 0, byte_offset);
1744 	BN_bn2bin(dsasig->s, &dsasig_data[21 + byte_offset]);
1745 
1746 	sigdata_rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, 41, dsasig_data);
1747         if(!sigdata_rdf) {
1748                 LDNS_FREE(dsasig_data);
1749         }
1750 	DSA_SIG_free(dsasig);
1751 
1752 	return sigdata_rdf;
1753 }
1754 
1755 ldns_status
1756 ldns_convert_dsa_rrsig_rdf2asn1(ldns_buffer *target_buffer,
1757 						  const ldns_rdf *sig_rdf)
1758 {
1759 	/* the EVP api wants the DER encoding of the signature... */
1760 	BIGNUM *R, *S;
1761 	DSA_SIG *dsasig;
1762 	unsigned char *raw_sig = NULL;
1763 	int raw_sig_len;
1764 
1765         if(ldns_rdf_size(sig_rdf) < 1 + 2*SHA_DIGEST_LENGTH)
1766                 return LDNS_STATUS_SYNTAX_RDATA_ERR;
1767 	/* extract the R and S field from the sig buffer */
1768 	R = BN_new();
1769 	if(!R) return LDNS_STATUS_MEM_ERR;
1770 	(void) BN_bin2bn((unsigned char *) ldns_rdf_data(sig_rdf) + 1,
1771 	                 SHA_DIGEST_LENGTH, R);
1772 	S = BN_new();
1773 	if(!S) {
1774 		BN_free(R);
1775 		return LDNS_STATUS_MEM_ERR;
1776 	}
1777 	(void) BN_bin2bn((unsigned char *) ldns_rdf_data(sig_rdf) + 21,
1778 	                 SHA_DIGEST_LENGTH, S);
1779 
1780 	dsasig = DSA_SIG_new();
1781 	if (!dsasig) {
1782 		BN_free(R);
1783 		BN_free(S);
1784 		return LDNS_STATUS_MEM_ERR;
1785 	}
1786 
1787 	dsasig->r = R;
1788 	dsasig->s = S;
1789 
1790 	raw_sig_len = i2d_DSA_SIG(dsasig, &raw_sig);
1791 	if (raw_sig_len < 0) {
1792 		DSA_SIG_free(dsasig);
1793 		free(raw_sig);
1794 		return LDNS_STATUS_SSL_ERR;
1795 	}
1796 	if (ldns_buffer_reserve(target_buffer, (size_t) raw_sig_len)) {
1797 		ldns_buffer_write(target_buffer, raw_sig, (size_t)raw_sig_len);
1798 	}
1799 
1800 	DSA_SIG_free(dsasig);
1801 	free(raw_sig);
1802 
1803 	return ldns_buffer_status(target_buffer);
1804 }
1805 
1806 #ifdef USE_ECDSA
1807 #ifndef S_SPLINT_S
1808 ldns_rdf *
1809 ldns_convert_ecdsa_rrsig_asn12rdf(const ldns_buffer *sig, const long sig_len)
1810 {
1811         ECDSA_SIG* ecdsa_sig;
1812 	unsigned char *data = (unsigned char*)ldns_buffer_begin(sig);
1813         ldns_rdf* rdf;
1814 	ecdsa_sig = d2i_ECDSA_SIG(NULL, (const unsigned char **)&data, sig_len);
1815         if(!ecdsa_sig) return NULL;
1816 
1817         /* "r | s". */
1818         data = LDNS_XMALLOC(unsigned char,
1819                 BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s));
1820         if(!data) {
1821                 ECDSA_SIG_free(ecdsa_sig);
1822                 return NULL;
1823         }
1824         BN_bn2bin(ecdsa_sig->r, data);
1825         BN_bn2bin(ecdsa_sig->s, data+BN_num_bytes(ecdsa_sig->r));
1826 	rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, (size_t)(
1827 		BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s)), data);
1828         ECDSA_SIG_free(ecdsa_sig);
1829         return rdf;
1830 }
1831 
1832 ldns_status
1833 ldns_convert_ecdsa_rrsig_rdf2asn1(ldns_buffer *target_buffer,
1834         const ldns_rdf *sig_rdf)
1835 {
1836         ECDSA_SIG* sig;
1837 	int raw_sig_len;
1838         long bnsize = (long)ldns_rdf_size(sig_rdf) / 2;
1839         /* if too short, or not even length, do not bother */
1840         if(bnsize < 16 || (size_t)bnsize*2 != ldns_rdf_size(sig_rdf))
1841                 return LDNS_STATUS_ERR;
1842 
1843         /* use the raw data to parse two evenly long BIGNUMs, "r | s". */
1844         sig = ECDSA_SIG_new();
1845         if(!sig) return LDNS_STATUS_MEM_ERR;
1846         sig->r = BN_bin2bn((const unsigned char*)ldns_rdf_data(sig_rdf),
1847                 bnsize, sig->r);
1848         sig->s = BN_bin2bn((const unsigned char*)ldns_rdf_data(sig_rdf)+bnsize,
1849                 bnsize, sig->s);
1850         if(!sig->r || !sig->s) {
1851                 ECDSA_SIG_free(sig);
1852                 return LDNS_STATUS_MEM_ERR;
1853         }
1854 
1855 	raw_sig_len = i2d_ECDSA_SIG(sig, NULL);
1856 	if (ldns_buffer_reserve(target_buffer, (size_t) raw_sig_len)) {
1857                 unsigned char* pp = (unsigned char*)
1858 			ldns_buffer_current(target_buffer);
1859 	        raw_sig_len = i2d_ECDSA_SIG(sig, &pp);
1860                 ldns_buffer_skip(target_buffer, (ssize_t) raw_sig_len);
1861 	}
1862         ECDSA_SIG_free(sig);
1863 
1864 	return ldns_buffer_status(target_buffer);
1865 }
1866 
1867 #endif /* S_SPLINT_S */
1868 #endif /* USE_ECDSA */
1869 #endif /* HAVE_SSL */
1870