1 #include <ldns/config.h>
5 #include <ldns/dnssec.h>
6 #include <ldns/dnssec_sign.h>
12 /* this entire file is rather useless when you don't have
15 #include <openssl/ssl.h>
16 #include <openssl/evp.h>
17 #include <openssl/rand.h>
18 #include <openssl/err.h>
19 #include <openssl/md5.h>
23 ldns_create_empty_rrsig(ldns_rr_list
*rrset
,
24 ldns_key
*current_key
)
27 ldns_rr_class orig_class
;
33 label_count
= ldns_dname_label_count(ldns_rr_owner(ldns_rr_list_rr(rrset
,
35 /* RFC4035 2.2: not counting the leftmost label if it is a wildcard */
36 if(ldns_dname_is_wildcard(ldns_rr_owner(ldns_rr_list_rr(rrset
, 0))))
39 current_sig
= ldns_rr_new_frm_type(LDNS_RR_TYPE_RRSIG
);
41 /* set the type on the new signature */
42 orig_ttl
= ldns_rr_ttl(ldns_rr_list_rr(rrset
, 0));
43 orig_class
= ldns_rr_get_class(ldns_rr_list_rr(rrset
, 0));
45 ldns_rr_set_ttl(current_sig
, orig_ttl
);
46 ldns_rr_set_class(current_sig
, orig_class
);
47 ldns_rr_set_owner(current_sig
,
50 ldns_rr_list_rr(rrset
,
53 /* fill in what we know of the signature */
55 /* set the orig_ttl */
56 (void)ldns_rr_rrsig_set_origttl(
58 ldns_native2rdf_int32(LDNS_RDF_TYPE_INT32
,
60 /* the signers name */
61 signame
= ldns_rdf_clone(ldns_key_pubkey_owner(current_key
));
62 ldns_dname2canonical(signame
);
63 (void)ldns_rr_rrsig_set_signame(
66 /* label count - get it from the first rr in the rr_list */
67 (void)ldns_rr_rrsig_set_labels(
69 ldns_native2rdf_int8(LDNS_RDF_TYPE_INT8
,
71 /* inception, expiration */
73 if (ldns_key_inception(current_key
) != 0) {
74 (void)ldns_rr_rrsig_set_inception(
76 ldns_native2rdf_int32(
78 ldns_key_inception(current_key
)));
80 (void)ldns_rr_rrsig_set_inception(
82 ldns_native2rdf_int32(LDNS_RDF_TYPE_TIME
, now
));
84 if (ldns_key_expiration(current_key
) != 0) {
85 (void)ldns_rr_rrsig_set_expiration(
87 ldns_native2rdf_int32(
89 ldns_key_expiration(current_key
)));
91 (void)ldns_rr_rrsig_set_expiration(
93 ldns_native2rdf_int32(
95 now
+ LDNS_DEFAULT_EXP_TIME
));
98 (void)ldns_rr_rrsig_set_keytag(
100 ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16
,
101 ldns_key_keytag(current_key
)));
103 (void)ldns_rr_rrsig_set_algorithm(
105 ldns_native2rdf_int8(
107 ldns_key_algorithm(current_key
)));
109 (void)ldns_rr_rrsig_set_typecovered(
111 ldns_native2rdf_int16(
113 ldns_rr_get_type(ldns_rr_list_rr(rrset
,
120 ldns_sign_public_buffer(ldns_buffer
*sign_buf
, ldns_key
*current_key
)
122 ldns_rdf
*b64rdf
= NULL
;
124 switch(ldns_key_algorithm(current_key
)) {
126 case LDNS_SIGN_DSA_NSEC3
:
127 b64rdf
= ldns_sign_public_evp(
129 ldns_key_evp_key(current_key
),
132 case LDNS_SIGN_RSASHA1
:
133 case LDNS_SIGN_RSASHA1_NSEC3
:
134 b64rdf
= ldns_sign_public_evp(
136 ldns_key_evp_key(current_key
),
140 case LDNS_SIGN_RSASHA256
:
141 b64rdf
= ldns_sign_public_evp(
143 ldns_key_evp_key(current_key
),
146 case LDNS_SIGN_RSASHA512
:
147 b64rdf
= ldns_sign_public_evp(
149 ldns_key_evp_key(current_key
),
152 #endif /* USE_SHA2 */
154 case LDNS_SIGN_ECC_GOST
:
155 b64rdf
= ldns_sign_public_evp(
157 ldns_key_evp_key(current_key
),
158 EVP_get_digestbyname("md_gost94"));
160 #endif /* USE_GOST */
162 case LDNS_SIGN_ECDSAP256SHA256
:
163 b64rdf
= ldns_sign_public_evp(
165 ldns_key_evp_key(current_key
),
168 case LDNS_SIGN_ECDSAP384SHA384
:
169 b64rdf
= ldns_sign_public_evp(
171 ldns_key_evp_key(current_key
),
175 case LDNS_SIGN_RSAMD5
:
176 b64rdf
= ldns_sign_public_evp(
178 ldns_key_evp_key(current_key
),
182 /* do _you_ know this alg? */
183 printf("unknown algorithm, ");
184 printf("is the one used available on this system?\n");
192 * use this function to sign with a public/private key alg
193 * return the created signatures
196 ldns_sign_public(ldns_rr_list
*rrset
, ldns_key_list
*keys
)
198 ldns_rr_list
*signatures
;
199 ldns_rr_list
*rrset_clone
;
200 ldns_rr
*current_sig
;
202 ldns_key
*current_key
;
205 ldns_buffer
*sign_buf
;
208 if (!rrset
|| ldns_rr_list_rr_count(rrset
) < 1 || !keys
) {
214 signatures
= ldns_rr_list_new();
216 /* prepare a signature and add all the know data
217 * prepare the rrset. Sign this together. */
218 rrset_clone
= ldns_rr_list_clone(rrset
);
223 /* make it canonical */
224 for(i
= 0; i
< ldns_rr_list_rr_count(rrset_clone
); i
++) {
225 ldns_rr_set_ttl(ldns_rr_list_rr(rrset_clone
, i
),
226 ldns_rr_ttl(ldns_rr_list_rr(rrset
, 0)));
227 ldns_rr2canonical(ldns_rr_list_rr(rrset_clone
, i
));
230 ldns_rr_list_sort(rrset_clone
);
233 key_count
< ldns_key_list_key_count(keys
);
235 if (!ldns_key_use(ldns_key_list_key(keys
, key_count
))) {
238 sign_buf
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
240 ldns_rr_list_free(rrset_clone
);
241 ldns_rr_list_free(signatures
);
242 ldns_rdf_free(new_owner
);
247 current_key
= ldns_key_list_key(keys
, key_count
);
248 /* sign all RRs with keys that have ZSKbit, !SEPbit.
249 sign DNSKEY RRs with keys that have ZSKbit&SEPbit */
250 if (ldns_key_flags(current_key
) & LDNS_KEY_ZONE_KEY
) {
251 current_sig
= ldns_create_empty_rrsig(rrset_clone
,
254 /* right now, we have: a key, a semi-sig and an rrset. For
255 * which we can create the sig and base64 encode that and
256 * add that to the signature */
258 if (ldns_rrsig2buffer_wire(sign_buf
, current_sig
)
260 ldns_buffer_free(sign_buf
);
262 ldns_rr_list_deep_free(rrset_clone
);
263 ldns_rr_free(current_sig
);
264 ldns_rr_list_deep_free(signatures
);
268 /* add the rrset in sign_buf */
269 if (ldns_rr_list2buffer_wire(sign_buf
, rrset_clone
)
271 ldns_buffer_free(sign_buf
);
272 ldns_rr_list_deep_free(rrset_clone
);
273 ldns_rr_free(current_sig
);
274 ldns_rr_list_deep_free(signatures
);
278 b64rdf
= ldns_sign_public_buffer(sign_buf
, current_key
);
281 /* signing went wrong */
282 ldns_rr_list_deep_free(rrset_clone
);
283 ldns_rr_free(current_sig
);
284 ldns_rr_list_deep_free(signatures
);
288 ldns_rr_rrsig_set_sig(current_sig
, b64rdf
);
290 /* push the signature to the signatures list */
291 ldns_rr_list_push_rr(signatures
, current_sig
);
293 ldns_buffer_free(sign_buf
); /* restart for the next key */
295 ldns_rr_list_deep_free(rrset_clone
);
303 * \param[in] to_sign The ldns_buffer containing raw data that is
305 * \param[in] key The DSA key structure to sign with
306 * \return ldns_rdf for the RRSIG ldns_rr
309 ldns_sign_public_dsa(ldns_buffer
*to_sign
, DSA
*key
)
311 unsigned char *sha1_hash
;
312 ldns_rdf
*sigdata_rdf
;
319 b64sig
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
324 sha1_hash
= SHA1((unsigned char*)ldns_buffer_begin(to_sign
),
325 ldns_buffer_position(to_sign
), NULL
);
327 ldns_buffer_free(b64sig
);
331 sig
= DSA_do_sign(sha1_hash
, SHA_DIGEST_LENGTH
, key
);
333 ldns_buffer_free(b64sig
);
337 data
= LDNS_XMALLOC(uint8_t, 1 + 2 * SHA_DIGEST_LENGTH
);
339 ldns_buffer_free(b64sig
);
345 pad
= 20 - (size_t) BN_num_bytes(sig
->r
);
347 memset(data
+ 1, 0, pad
);
349 BN_bn2bin(sig
->r
, (unsigned char *) (data
+ 1) + pad
);
351 pad
= 20 - (size_t) BN_num_bytes(sig
->s
);
353 memset(data
+ 1 + SHA_DIGEST_LENGTH
, 0, pad
);
355 BN_bn2bin(sig
->s
, (unsigned char *) (data
+ 1 + SHA_DIGEST_LENGTH
+ pad
));
357 sigdata_rdf
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
,
358 1 + 2 * SHA_DIGEST_LENGTH
,
361 ldns_buffer_free(b64sig
);
371 ldns_pkey_is_ecdsa(EVP_PKEY
* pkey
)
375 if(EVP_PKEY_type(pkey
->type
) != EVP_PKEY_EC
)
377 ec
= EVP_PKEY_get1_EC_KEY(pkey
);
378 g
= EC_KEY_get0_group(ec
);
383 if(EC_GROUP_get_curve_name(g
) == NID_secp224r1
||
384 EC_GROUP_get_curve_name(g
) == NID_X9_62_prime256v1
||
385 EC_GROUP_get_curve_name(g
) == NID_secp384r1
) {
389 /* downref the eckey, the original is still inside the pkey */
394 #endif /* USE_ECDSA */
397 ldns_sign_public_evp(ldns_buffer
*to_sign
,
399 const EVP_MD
*digest_type
)
402 ldns_rdf
*sigdata_rdf
;
405 const EVP_MD
*md_type
;
409 b64sig
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
414 /* initializes a signing context */
415 md_type
= digest_type
;
417 /* unknown message difest */
418 ldns_buffer_free(b64sig
);
422 EVP_MD_CTX_init(&ctx
);
423 r
= EVP_SignInit(&ctx
, md_type
);
425 r
= EVP_SignUpdate(&ctx
, (unsigned char*)
426 ldns_buffer_begin(to_sign
),
427 ldns_buffer_position(to_sign
));
429 ldns_buffer_free(b64sig
);
433 r
= EVP_SignFinal(&ctx
, (unsigned char*)
434 ldns_buffer_begin(b64sig
), &siglen
, key
);
436 ldns_buffer_free(b64sig
);
440 ldns_buffer_free(b64sig
);
444 /* unfortunately, OpenSSL output is differenct from DNS DSA format */
446 if (EVP_PKEY_type(key
->type
) == EVP_PKEY_DSA
) {
447 sigdata_rdf
= ldns_convert_dsa_rrsig_asn12rdf(b64sig
, siglen
);
449 } else if(EVP_PKEY_type(key
->type
) == EVP_PKEY_EC
&&
450 ldns_pkey_is_ecdsa(key
)) {
451 sigdata_rdf
= ldns_convert_ecdsa_rrsig_asn12rdf(b64sig
, siglen
);
454 /* ok output for other types is the same */
455 sigdata_rdf
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, siglen
,
456 ldns_buffer_begin(b64sig
));
459 ldns_buffer_free(b64sig
);
460 EVP_MD_CTX_cleanup(&ctx
);
465 ldns_sign_public_rsasha1(ldns_buffer
*to_sign
, RSA
*key
)
467 unsigned char *sha1_hash
;
469 ldns_rdf
*sigdata_rdf
;
474 b64sig
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
479 sha1_hash
= SHA1((unsigned char*)ldns_buffer_begin(to_sign
),
480 ldns_buffer_position(to_sign
), NULL
);
482 ldns_buffer_free(b64sig
);
486 result
= RSA_sign(NID_sha1
, sha1_hash
, SHA_DIGEST_LENGTH
,
487 (unsigned char*)ldns_buffer_begin(b64sig
),
490 ldns_buffer_free(b64sig
);
494 sigdata_rdf
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, siglen
,
495 ldns_buffer_begin(b64sig
));
496 ldns_buffer_free(b64sig
); /* can't free this buffer ?? */
501 ldns_sign_public_rsamd5(ldns_buffer
*to_sign
, RSA
*key
)
503 unsigned char *md5_hash
;
505 ldns_rdf
*sigdata_rdf
;
508 b64sig
= ldns_buffer_new(LDNS_MAX_PACKETLEN
);
513 md5_hash
= MD5((unsigned char*)ldns_buffer_begin(to_sign
),
514 ldns_buffer_position(to_sign
), NULL
);
516 ldns_buffer_free(b64sig
);
520 RSA_sign(NID_md5
, md5_hash
, MD5_DIGEST_LENGTH
,
521 (unsigned char*)ldns_buffer_begin(b64sig
),
524 sigdata_rdf
= ldns_rdf_new_frm_data(LDNS_RDF_TYPE_B64
, siglen
,
525 ldns_buffer_begin(b64sig
));
526 ldns_buffer_free(b64sig
);
529 #endif /* HAVE_SSL */
532 * Pushes all rrs from the rrsets of type A and AAAA on gluelist.
535 ldns_dnssec_addresses_on_glue_list(
536 ldns_dnssec_rrsets
*cur_rrset
,
537 ldns_rr_list
*glue_list
)
539 ldns_dnssec_rrs
*cur_rrs
;
541 if (cur_rrset
->type
== LDNS_RR_TYPE_A
542 || cur_rrset
->type
== LDNS_RR_TYPE_AAAA
) {
543 for (cur_rrs
= cur_rrset
->rrs
;
545 cur_rrs
= cur_rrs
->next
) {
547 if (!ldns_rr_list_push_rr(glue_list
,
549 return LDNS_STATUS_MEM_ERR
;
550 /* ldns_rr_list_push_rr()
551 * returns false when unable
552 * to increase the capacity
553 * of the ldsn_rr_list
559 cur_rrset
= cur_rrset
->next
;
561 return LDNS_STATUS_OK
;
565 * Marks the names in the zone that are occluded. Those names will be skipped
566 * when walking the tree with the ldns_dnssec_name_node_next_nonglue()
567 * function. But watch out! Names that are partially occluded (like glue with
568 * the same name as the delegation) will not be marked and should specifically
569 * be taken into account seperately.
571 * When glue_list is given (not NULL), in the process of marking the names, all
572 * glue resource records will be pushed to that list, even glue at delegation names.
574 * \param[in] zone the zone in which to mark the names
575 * \param[in] glue_list the list to which to push the glue rrs
576 * \return LDNS_STATUS_OK on success, an error code otherwise
579 ldns_dnssec_zone_mark_and_get_glue(ldns_dnssec_zone
*zone
,
580 ldns_rr_list
*glue_list
)
583 ldns_dnssec_name
*name
;
585 ldns_rdf
*cut
= NULL
; /* keeps track of zone cuts */
586 /* When the cut is caused by a delegation, below_delegation will be 1.
587 * When caused by a DNAME, below_delegation will be 0.
589 int below_delegation
= -1; /* init suppresses comiler warning */
592 if (!zone
|| !zone
->names
) {
593 return LDNS_STATUS_NULL
;
595 for (node
= ldns_rbtree_first(zone
->names
);
596 node
!= LDNS_RBTREE_NULL
;
597 node
= ldns_rbtree_next(node
)) {
598 name
= (ldns_dnssec_name
*) node
->data
;
599 owner
= ldns_dnssec_name_name(name
);
602 /* The previous node was a zone cut, or a subdomain
603 * below a zone cut. Is this node (still) a subdomain
604 * below the cut? Then the name is occluded. Unless
605 * the name contains a SOA, after which we are
606 * authoritative again.
608 * FIXME! If there are labels in between the SOA and
609 * the cut, going from the authoritative space (below
610 * the SOA) up into occluded space again, will not be
611 * detected with the contruct below!
613 if (ldns_dname_is_subdomain(owner
, cut
) &&
614 !ldns_dnssec_rrsets_contains_type(
615 name
->rrsets
, LDNS_RR_TYPE_SOA
)) {
617 if (below_delegation
&& glue_list
) {
618 s
= ldns_dnssec_addresses_on_glue_list(
619 name
->rrsets
, glue_list
);
620 if (s
!= LDNS_STATUS_OK
) {
624 name
->is_glue
= true; /* Mark occluded name! */
631 /* The node is not below a zone cut. Is it a zone cut itself?
632 * Everything below a SOA is authoritative of course; Except
633 * when the name also contains a DNAME :).
635 if (ldns_dnssec_rrsets_contains_type(
636 name
->rrsets
, LDNS_RR_TYPE_NS
)
637 && !ldns_dnssec_rrsets_contains_type(
638 name
->rrsets
, LDNS_RR_TYPE_SOA
)) {
640 below_delegation
= 1;
641 if (glue_list
) { /* record glue on the zone cut */
642 s
= ldns_dnssec_addresses_on_glue_list(
643 name
->rrsets
, glue_list
);
644 if (s
!= LDNS_STATUS_OK
) {
648 } else if (ldns_dnssec_rrsets_contains_type(
649 name
->rrsets
, LDNS_RR_TYPE_DNAME
)) {
651 below_delegation
= 0;
654 return LDNS_STATUS_OK
;
658 * Marks the names in the zone that are occluded. Those names will be skipped
659 * when walking the tree with the ldns_dnssec_name_node_next_nonglue()
660 * function. But watch out! Names that are partially occluded (like glue with
661 * the same name as the delegation) will not be marked and should specifically
662 * be taken into account seperately.
664 * \param[in] zone the zone in which to mark the names
665 * \return LDNS_STATUS_OK on success, an error code otherwise
668 ldns_dnssec_zone_mark_glue(ldns_dnssec_zone
*zone
)
670 return ldns_dnssec_zone_mark_and_get_glue(zone
, NULL
);
674 ldns_dnssec_name_node_next_nonglue(ldns_rbnode_t
*node
)
676 ldns_rbnode_t
*next_node
= NULL
;
677 ldns_dnssec_name
*next_name
= NULL
;
680 if (node
== LDNS_RBTREE_NULL
) {
685 if (next_node
== LDNS_RBTREE_NULL
) {
688 next_name
= (ldns_dnssec_name
*)next_node
->data
;
689 if (!next_name
->is_glue
) {
692 next_node
= ldns_rbtree_next(next_node
);
700 ldns_dnssec_zone_create_nsecs(ldns_dnssec_zone
*zone
,
701 ldns_rr_list
*new_rrs
)
704 ldns_rbnode_t
*first_node
, *cur_node
, *next_node
;
705 ldns_dnssec_name
*cur_name
, *next_name
;
708 ldns_dnssec_rrsets
*soa
;
710 /* the TTL of NSEC rrs should be set to the minimum TTL of
711 * the zone SOA (RFC4035 Section 2.3)
713 soa
= ldns_dnssec_name_find_rrset(zone
->soa
, LDNS_RR_TYPE_SOA
);
715 /* did the caller actually set it? if not,
716 * fall back to default ttl
718 if (soa
&& soa
->rrs
&& soa
->rrs
->rr
719 && (ldns_rr_rdf(soa
->rrs
->rr
, 6) != NULL
)) {
720 nsec_ttl
= ldns_rdf2native_int32(ldns_rr_rdf(soa
->rrs
->rr
, 6));
722 nsec_ttl
= LDNS_DEFAULT_TTL
;
725 first_node
= ldns_dnssec_name_node_next_nonglue(
726 ldns_rbtree_first(zone
->names
));
727 cur_node
= first_node
;
729 next_node
= ldns_dnssec_name_node_next_nonglue(
730 ldns_rbtree_next(cur_node
));
735 while (cur_node
&& next_node
) {
736 cur_name
= (ldns_dnssec_name
*)cur_node
->data
;
737 next_name
= (ldns_dnssec_name
*)next_node
->data
;
738 nsec_rr
= ldns_dnssec_create_nsec(cur_name
,
741 ldns_rr_set_ttl(nsec_rr
, nsec_ttl
);
742 if(ldns_dnssec_name_add_rr(cur_name
, nsec_rr
)!=LDNS_STATUS_OK
){
743 ldns_rr_free(nsec_rr
);
744 return LDNS_STATUS_ERR
;
746 ldns_rr_list_push_rr(new_rrs
, nsec_rr
);
747 cur_node
= next_node
;
749 next_node
= ldns_dnssec_name_node_next_nonglue(
750 ldns_rbtree_next(cur_node
));
754 if (cur_node
&& !next_node
) {
755 cur_name
= (ldns_dnssec_name
*)cur_node
->data
;
756 next_name
= (ldns_dnssec_name
*)first_node
->data
;
757 nsec_rr
= ldns_dnssec_create_nsec(cur_name
,
760 ldns_rr_set_ttl(nsec_rr
, nsec_ttl
);
761 if(ldns_dnssec_name_add_rr(cur_name
, nsec_rr
)!=LDNS_STATUS_OK
){
762 ldns_rr_free(nsec_rr
);
763 return LDNS_STATUS_ERR
;
765 ldns_rr_list_push_rr(new_rrs
, nsec_rr
);
770 return LDNS_STATUS_OK
;
774 /* in dnssec_zone.c */
775 extern int ldns_dname_compare_v(const void *a
, const void *b
);
778 ldns_dnssec_zone_create_nsec3s_mkmap(ldns_dnssec_zone
*zone
,
779 ldns_rr_list
*new_rrs
,
787 ldns_rbnode_t
*first_name_node
;
788 ldns_rbnode_t
*current_name_node
;
789 ldns_dnssec_name
*current_name
;
790 ldns_status result
= LDNS_STATUS_OK
;
792 ldns_rr_list
*nsec3_list
;
794 ldns_dnssec_rrsets
*soa
;
795 ldns_rbnode_t
*hashmap_node
;
797 if (!zone
|| !new_rrs
|| !zone
->names
) {
798 return LDNS_STATUS_ERR
;
801 /* the TTL of NSEC rrs should be set to the minimum TTL of
802 * the zone SOA (RFC4035 Section 2.3)
804 soa
= ldns_dnssec_name_find_rrset(zone
->soa
, LDNS_RR_TYPE_SOA
);
806 /* did the caller actually set it? if not,
807 * fall back to default ttl
809 if (soa
&& soa
->rrs
&& soa
->rrs
->rr
810 && ldns_rr_rdf(soa
->rrs
->rr
, 6) != NULL
) {
811 nsec_ttl
= ldns_rdf2native_int32(ldns_rr_rdf(soa
->rrs
->rr
, 6));
813 nsec_ttl
= LDNS_DEFAULT_TTL
;
817 if ((*map
= ldns_rbtree_create(ldns_dname_compare_v
))
822 nsec3_list
= ldns_rr_list_new();
824 first_name_node
= ldns_dnssec_name_node_next_nonglue(
825 ldns_rbtree_first(zone
->names
));
827 current_name_node
= first_name_node
;
829 while (current_name_node
&&
830 current_name_node
!= LDNS_RBTREE_NULL
) {
831 current_name
= (ldns_dnssec_name
*) current_name_node
->data
;
832 nsec_rr
= ldns_dnssec_create_nsec3(current_name
,
840 /* by default, our nsec based generator adds rrsigs
841 * remove the bitmap for empty nonterminals */
842 if (!current_name
->rrsets
) {
843 ldns_rdf_deep_free(ldns_rr_pop_rdf(nsec_rr
));
845 ldns_rr_set_ttl(nsec_rr
, nsec_ttl
);
846 result
= ldns_dnssec_name_add_rr(current_name
, nsec_rr
);
847 ldns_rr_list_push_rr(new_rrs
, nsec_rr
);
848 ldns_rr_list_push_rr(nsec3_list
, nsec_rr
);
850 hashmap_node
= LDNS_MALLOC(ldns_rbnode_t
);
851 if (hashmap_node
&& ldns_rr_owner(nsec_rr
)) {
852 hashmap_node
->key
= ldns_dname_label(
853 ldns_rr_owner(nsec_rr
), 0);
854 if (hashmap_node
->key
) {
855 hashmap_node
->data
= current_name
->name
;
856 (void) ldns_rbtree_insert(
861 current_name_node
= ldns_dnssec_name_node_next_nonglue(
862 ldns_rbtree_next(current_name_node
));
864 if (result
!= LDNS_STATUS_OK
) {
865 ldns_rr_list_free(nsec3_list
);
869 ldns_rr_list_sort_nsec3(nsec3_list
);
870 result
= ldns_dnssec_chain_nsec3_list(nsec3_list
);
871 ldns_rr_list_free(nsec3_list
);
877 ldns_dnssec_zone_create_nsec3s(ldns_dnssec_zone
*zone
,
878 ldns_rr_list
*new_rrs
,
885 return ldns_dnssec_zone_create_nsec3s_mkmap(zone
, new_rrs
, algorithm
,
886 flags
, iterations
, salt_length
, salt
, NULL
);
889 #endif /* HAVE_SSL */
892 ldns_dnssec_remove_signatures( ldns_dnssec_rrs
*signatures
893 , ATTR_UNUSED(ldns_key_list
*key_list
)
894 , int (*func
)(ldns_rr
*, void *)
898 ldns_dnssec_rrs
*base_rrs
= signatures
;
899 ldns_dnssec_rrs
*cur_rr
= base_rrs
;
900 ldns_dnssec_rrs
*prev_rr
= NULL
;
901 ldns_dnssec_rrs
*next_rr
;
907 switch(func(NULL
, arg
)) {
908 case LDNS_SIGNATURE_LEAVE_ADD_NEW
:
909 case LDNS_SIGNATURE_REMOVE_ADD_NEW
:
911 case LDNS_SIGNATURE_LEAVE_NO_ADD
:
912 case LDNS_SIGNATURE_REMOVE_NO_ADD
:
913 ldns_key_list_set_use(key_list
, false);
916 fprintf(stderr
, "[XX] unknown return value from callback\n");
921 (void)func(cur_rr
->rr
, arg
);
924 next_rr
= cur_rr
->next
;
926 switch (func(cur_rr
->rr
, arg
)) {
927 case LDNS_SIGNATURE_LEAVE_ADD_NEW
:
930 case LDNS_SIGNATURE_LEAVE_NO_ADD
:
931 keytag
= ldns_rdf2native_int16(
932 ldns_rr_rrsig_keytag(cur_rr
->rr
));
933 for (i
= 0; i
< ldns_key_list_key_count(key_list
); i
++) {
934 if (ldns_key_keytag(ldns_key_list_key(key_list
, i
)) ==
936 ldns_key_set_use(ldns_key_list_key(key_list
, i
),
942 case LDNS_SIGNATURE_REMOVE_NO_ADD
:
943 keytag
= ldns_rdf2native_int16(
944 ldns_rr_rrsig_keytag(cur_rr
->rr
));
945 for (i
= 0; i
< ldns_key_list_key_count(key_list
); i
++) {
946 if (ldns_key_keytag(ldns_key_list_key(key_list
, i
))
948 ldns_key_set_use(ldns_key_list_key(key_list
, i
),
953 prev_rr
->next
= next_rr
;
959 case LDNS_SIGNATURE_REMOVE_ADD_NEW
:
961 prev_rr
->next
= next_rr
;
968 fprintf(stderr
, "[XX] unknown return value from callback\n");
979 ldns_dnssec_zone_create_rrsigs(ldns_dnssec_zone
*zone
,
980 ldns_rr_list
*new_rrs
,
981 ldns_key_list
*key_list
,
982 int (*func
)(ldns_rr
*, void*),
985 return ldns_dnssec_zone_create_rrsigs_flg(zone
, new_rrs
, key_list
,
989 /** If there are KSKs use only them and mark ZSKs unused */
991 ldns_key_list_filter_for_dnskey(ldns_key_list
*key_list
)
995 for(i
=0; i
<ldns_key_list_key_count(key_list
); i
++)
996 if((ldns_key_flags(ldns_key_list_key(key_list
, i
))&LDNS_KEY_SEP_KEY
)) {
1002 for(i
=0; i
<ldns_key_list_key_count(key_list
); i
++)
1003 if(!(ldns_key_flags(ldns_key_list_key(key_list
, i
))&LDNS_KEY_SEP_KEY
))
1004 ldns_key_set_use(ldns_key_list_key(key_list
, i
), 0);
1007 /** If there are no ZSKs use KSK as ZSK */
1009 ldns_key_list_filter_for_non_dnskey(ldns_key_list
*key_list
)
1013 for(i
=0; i
<ldns_key_list_key_count(key_list
); i
++)
1014 if(!(ldns_key_flags(ldns_key_list_key(key_list
, i
))&LDNS_KEY_SEP_KEY
)) {
1020 /* else filter all KSKs */
1021 for(i
=0; i
<ldns_key_list_key_count(key_list
); i
++)
1022 if((ldns_key_flags(ldns_key_list_key(key_list
, i
))&LDNS_KEY_SEP_KEY
))
1023 ldns_key_set_use(ldns_key_list_key(key_list
, i
), 0);
1027 ldns_dnssec_zone_create_rrsigs_flg( ldns_dnssec_zone
*zone
1028 , ldns_rr_list
*new_rrs
1029 , ldns_key_list
*key_list
1030 , int (*func
)(ldns_rr
*, void*)
1035 ldns_status result
= LDNS_STATUS_OK
;
1037 ldns_rbnode_t
*cur_node
;
1038 ldns_rr_list
*rr_list
;
1040 ldns_dnssec_name
*cur_name
;
1041 ldns_dnssec_rrsets
*cur_rrset
;
1042 ldns_dnssec_rrs
*cur_rr
;
1044 ldns_rr_list
*siglist
;
1048 int on_delegation_point
= 0; /* handle partially occluded names */
1050 ldns_rr_list
*pubkey_list
= ldns_rr_list_new();
1051 for (i
= 0; i
<ldns_key_list_key_count(key_list
); i
++) {
1052 ldns_rr_list_push_rr( pubkey_list
1053 , ldns_key2rr(ldns_key_list_key(
1057 /* TODO: callback to see is list should be signed */
1058 /* TODO: remove 'old' signatures from signature list */
1059 cur_node
= ldns_rbtree_first(zone
->names
);
1060 while (cur_node
!= LDNS_RBTREE_NULL
) {
1061 cur_name
= (ldns_dnssec_name
*) cur_node
->data
;
1063 if (!cur_name
->is_glue
) {
1064 on_delegation_point
= ldns_dnssec_rrsets_contains_type(
1065 cur_name
->rrsets
, LDNS_RR_TYPE_NS
)
1066 && !ldns_dnssec_rrsets_contains_type(
1067 cur_name
->rrsets
, LDNS_RR_TYPE_SOA
);
1068 cur_rrset
= cur_name
->rrsets
;
1070 /* reset keys to use */
1071 ldns_key_list_set_use(key_list
, true);
1073 /* walk through old sigs, remove the old,
1074 and mark which keys (not) to use) */
1075 cur_rrset
->signatures
=
1076 ldns_dnssec_remove_signatures(cur_rrset
->signatures
,
1080 if(!(flags
&LDNS_SIGN_DNSKEY_WITH_ZSK
) &&
1081 cur_rrset
->type
== LDNS_RR_TYPE_DNSKEY
)
1082 ldns_key_list_filter_for_dnskey(key_list
);
1084 if(cur_rrset
->type
!= LDNS_RR_TYPE_DNSKEY
)
1085 ldns_key_list_filter_for_non_dnskey(key_list
);
1087 /* TODO: just set count to zero? */
1088 rr_list
= ldns_rr_list_new();
1090 cur_rr
= cur_rrset
->rrs
;
1092 ldns_rr_list_push_rr(rr_list
, cur_rr
->rr
);
1093 cur_rr
= cur_rr
->next
;
1096 /* only sign non-delegation RRsets */
1097 /* (glue should have been marked earlier,
1098 * except on the delegation points itself) */
1099 if (!on_delegation_point
||
1100 ldns_rr_list_type(rr_list
)
1101 == LDNS_RR_TYPE_DS
||
1102 ldns_rr_list_type(rr_list
)
1103 == LDNS_RR_TYPE_NSEC
||
1104 ldns_rr_list_type(rr_list
)
1105 == LDNS_RR_TYPE_NSEC3
) {
1106 siglist
= ldns_sign_public(rr_list
, key_list
);
1107 for (i
= 0; i
< ldns_rr_list_rr_count(siglist
); i
++) {
1108 if (cur_rrset
->signatures
) {
1109 result
= ldns_dnssec_rrs_add_rr(cur_rrset
->signatures
,
1110 ldns_rr_list_rr(siglist
,
1113 cur_rrset
->signatures
= ldns_dnssec_rrs_new();
1114 cur_rrset
->signatures
->rr
=
1115 ldns_rr_list_rr(siglist
, i
);
1118 ldns_rr_list_push_rr(new_rrs
,
1119 ldns_rr_list_rr(siglist
,
1123 ldns_rr_list_free(siglist
);
1126 ldns_rr_list_free(rr_list
);
1128 cur_rrset
= cur_rrset
->next
;
1132 ldns_key_list_set_use(key_list
, true);
1133 cur_name
->nsec_signatures
=
1134 ldns_dnssec_remove_signatures(cur_name
->nsec_signatures
,
1138 ldns_key_list_filter_for_non_dnskey(key_list
);
1140 rr_list
= ldns_rr_list_new();
1141 ldns_rr_list_push_rr(rr_list
, cur_name
->nsec
);
1142 siglist
= ldns_sign_public(rr_list
, key_list
);
1144 for (i
= 0; i
< ldns_rr_list_rr_count(siglist
); i
++) {
1145 if (cur_name
->nsec_signatures
) {
1146 result
= ldns_dnssec_rrs_add_rr(cur_name
->nsec_signatures
,
1147 ldns_rr_list_rr(siglist
, i
));
1149 cur_name
->nsec_signatures
= ldns_dnssec_rrs_new();
1150 cur_name
->nsec_signatures
->rr
=
1151 ldns_rr_list_rr(siglist
, i
);
1154 ldns_rr_list_push_rr(new_rrs
,
1155 ldns_rr_list_rr(siglist
, i
));
1159 ldns_rr_list_free(siglist
);
1160 ldns_rr_list_free(rr_list
);
1162 cur_node
= ldns_rbtree_next(cur_node
);
1165 ldns_rr_list_deep_free(pubkey_list
);
1170 ldns_dnssec_zone_sign(ldns_dnssec_zone
*zone
,
1171 ldns_rr_list
*new_rrs
,
1172 ldns_key_list
*key_list
,
1173 int (*func
)(ldns_rr
*, void *),
1176 return ldns_dnssec_zone_sign_flg(zone
, new_rrs
, key_list
, func
, arg
, 0);
1180 ldns_dnssec_zone_sign_flg(ldns_dnssec_zone
*zone
,
1181 ldns_rr_list
*new_rrs
,
1182 ldns_key_list
*key_list
,
1183 int (*func
)(ldns_rr
*, void *),
1187 ldns_status result
= LDNS_STATUS_OK
;
1189 if (!zone
|| !new_rrs
|| !key_list
) {
1190 return LDNS_STATUS_ERR
;
1193 /* zone is already sorted */
1194 result
= ldns_dnssec_zone_mark_glue(zone
);
1195 if (result
!= LDNS_STATUS_OK
) {
1199 /* check whether we need to add nsecs */
1200 if (zone
->names
&& !((ldns_dnssec_name
*)zone
->names
->root
->data
)->nsec
) {
1201 result
= ldns_dnssec_zone_create_nsecs(zone
, new_rrs
);
1202 if (result
!= LDNS_STATUS_OK
) {
1207 result
= ldns_dnssec_zone_create_rrsigs_flg(zone
,
1218 ldns_dnssec_zone_sign_nsec3(ldns_dnssec_zone
*zone
,
1219 ldns_rr_list
*new_rrs
,
1220 ldns_key_list
*key_list
,
1221 int (*func
)(ldns_rr
*, void *),
1225 uint16_t iterations
,
1226 uint8_t salt_length
,
1229 return ldns_dnssec_zone_sign_nsec3_flg_mkmap(zone
, new_rrs
, key_list
,
1230 func
, arg
, algorithm
, flags
, iterations
, salt_length
, salt
, 0,
1235 ldns_dnssec_zone_sign_nsec3_flg_mkmap(ldns_dnssec_zone
*zone
,
1236 ldns_rr_list
*new_rrs
,
1237 ldns_key_list
*key_list
,
1238 int (*func
)(ldns_rr
*, void *),
1242 uint16_t iterations
,
1243 uint8_t salt_length
,
1246 ldns_rbtree_t
**map
)
1248 ldns_rr
*nsec3
, *nsec3param
;
1249 ldns_status result
= LDNS_STATUS_OK
;
1251 /* zone is already sorted */
1252 result
= ldns_dnssec_zone_mark_glue(zone
);
1253 if (result
!= LDNS_STATUS_OK
) {
1257 /* TODO if there are already nsec3s presents and their
1258 * parameters are the same as these, we don't have to recreate
1261 /* add empty nonterminals */
1262 result
= ldns_dnssec_zone_add_empty_nonterminals(zone
);
1263 if (result
!= LDNS_STATUS_OK
) {
1267 nsec3
= ((ldns_dnssec_name
*)zone
->names
->root
->data
)->nsec
;
1268 if (nsec3
&& ldns_rr_get_type(nsec3
) == LDNS_RR_TYPE_NSEC3
) {
1269 /* no need to recreate */
1271 if (!ldns_dnssec_zone_find_rrset(zone
,
1273 LDNS_RR_TYPE_NSEC3PARAM
)) {
1274 /* create and add the nsec3param rr */
1276 ldns_rr_new_frm_type(LDNS_RR_TYPE_NSEC3PARAM
);
1277 ldns_rr_set_owner(nsec3param
,
1278 ldns_rdf_clone(zone
->soa
->name
));
1279 ldns_nsec3_add_param_rdfs(nsec3param
,
1285 /* always set bit 7 of the flags to zero, according to
1286 * rfc5155 section 11. The bits are counted from right to left,
1287 * so bit 7 in rfc5155 is bit 0 in ldns */
1288 ldns_set_bit(ldns_rdf_data(ldns_rr_rdf(nsec3param
, 1)), 0, 0);
1289 result
= ldns_dnssec_zone_add_rr(zone
, nsec3param
);
1290 if (result
!= LDNS_STATUS_OK
) {
1293 ldns_rr_list_push_rr(new_rrs
, nsec3param
);
1295 result
= ldns_dnssec_zone_create_nsec3s_mkmap(zone
,
1303 if (result
!= LDNS_STATUS_OK
) {
1308 result
= ldns_dnssec_zone_create_rrsigs_flg(zone
,
1320 ldns_dnssec_zone_sign_nsec3_flg(ldns_dnssec_zone
*zone
,
1321 ldns_rr_list
*new_rrs
,
1322 ldns_key_list
*key_list
,
1323 int (*func
)(ldns_rr
*, void *),
1327 uint16_t iterations
,
1328 uint8_t salt_length
,
1332 return ldns_dnssec_zone_sign_nsec3_flg_mkmap(zone
, new_rrs
, key_list
,
1333 func
, arg
, algorithm
, flags
, iterations
, salt_length
, salt
,
1338 ldns_zone_sign(const ldns_zone
*zone
, ldns_key_list
*key_list
)
1340 ldns_dnssec_zone
*dnssec_zone
;
1341 ldns_zone
*signed_zone
;
1342 ldns_rr_list
*new_rrs
;
1345 signed_zone
= ldns_zone_new();
1346 dnssec_zone
= ldns_dnssec_zone_new();
1348 (void) ldns_dnssec_zone_add_rr(dnssec_zone
, ldns_zone_soa(zone
));
1349 ldns_zone_set_soa(signed_zone
, ldns_rr_clone(ldns_zone_soa(zone
)));
1351 for (i
= 0; i
< ldns_rr_list_rr_count(ldns_zone_rrs(zone
)); i
++) {
1352 (void) ldns_dnssec_zone_add_rr(dnssec_zone
,
1353 ldns_rr_list_rr(ldns_zone_rrs(zone
),
1355 ldns_zone_push_rr(signed_zone
,
1356 ldns_rr_clone(ldns_rr_list_rr(ldns_zone_rrs(zone
),
1360 new_rrs
= ldns_rr_list_new();
1361 (void) ldns_dnssec_zone_sign(dnssec_zone
,
1364 ldns_dnssec_default_replace_signatures
,
1367 for (i
= 0; i
< ldns_rr_list_rr_count(new_rrs
); i
++) {
1368 ldns_rr_list_push_rr(ldns_zone_rrs(signed_zone
),
1369 ldns_rr_clone(ldns_rr_list_rr(new_rrs
, i
)));
1372 ldns_rr_list_deep_free(new_rrs
);
1373 ldns_dnssec_zone_free(dnssec_zone
);
1379 ldns_zone_sign_nsec3(ldns_zone
*zone
, ldns_key_list
*key_list
, uint8_t algorithm
, uint8_t flags
, uint16_t iterations
, uint8_t salt_length
, uint8_t *salt
)
1381 ldns_dnssec_zone
*dnssec_zone
;
1382 ldns_zone
*signed_zone
;
1383 ldns_rr_list
*new_rrs
;
1386 signed_zone
= ldns_zone_new();
1387 dnssec_zone
= ldns_dnssec_zone_new();
1389 (void) ldns_dnssec_zone_add_rr(dnssec_zone
, ldns_zone_soa(zone
));
1390 ldns_zone_set_soa(signed_zone
, ldns_rr_clone(ldns_zone_soa(zone
)));
1392 for (i
= 0; i
< ldns_rr_list_rr_count(ldns_zone_rrs(zone
)); i
++) {
1393 (void) ldns_dnssec_zone_add_rr(dnssec_zone
,
1394 ldns_rr_list_rr(ldns_zone_rrs(zone
),
1396 ldns_zone_push_rr(signed_zone
,
1397 ldns_rr_clone(ldns_rr_list_rr(ldns_zone_rrs(zone
),
1401 new_rrs
= ldns_rr_list_new();
1402 (void) ldns_dnssec_zone_sign_nsec3(dnssec_zone
,
1405 ldns_dnssec_default_replace_signatures
,
1413 for (i
= 0; i
< ldns_rr_list_rr_count(new_rrs
); i
++) {
1414 ldns_rr_list_push_rr(ldns_zone_rrs(signed_zone
),
1415 ldns_rr_clone(ldns_rr_list_rr(new_rrs
, i
)));
1418 ldns_rr_list_deep_free(new_rrs
);
1419 ldns_dnssec_zone_free(dnssec_zone
);
1423 #endif /* HAVE_SSL */