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>
107 ldns_rdf *sname, *hashed_sname, *tmp;
110 bool exact_match_found;
138 exact_match_found =
false;
139 in_range_found =
false;
162 exact_match_found =
true;
164 in_range_found =
true;
168 if (!exact_match_found && in_range_found) {
170 }
else if (exact_match_found && flag) {
175 }
else if (exact_match_found && !flag) {
232 t_netorder = htons(type);
256 t_netorder = htons(type);
294 keysize= ldns_buffer_position(keybuf);
314 memmove(&ac16, key + keysize - 3, 2);
317 return (uint16_t) ac16;
320 for (i = 0; (size_t)i < keysize; ++i) {
321 ac32 += (i & 1) ? key[i] : key[i] << 8;
323 ac32 += (ac32 >> 16) & 0xFFFF;
324 return (uint16_t) (ac32 & 0xFFFF);
333 ldns_buffer_position(key));
343 BIGNUM *Q; BIGNUM *P;
344 BIGNUM *G; BIGNUM *Y;
349 length = (64 + T * 8);
355 if(len < (
size_t)1 + SHA_DIGEST_LENGTH + 3*length)
358 Q = BN_bin2bn(key+offset, SHA_DIGEST_LENGTH, NULL);
359 offset += SHA_DIGEST_LENGTH;
361 P = BN_bin2bn(key+offset, (
int)length, NULL);
364 G = BN_bin2bn(key+offset, (
int)length, NULL);
367 Y = BN_bin2bn(key+offset, (
int)length, NULL);
371 if(!Q || !P || !G || !Y || !(dsa = DSA_new())) {
392 ldns_buffer_position(key));
413 memmove(&int16, key+1, 2);
422 if(len < (
size_t)offset + exp + 1)
427 if(!exponent)
return NULL;
428 (void) BN_bin2bn(key+offset, (
int)exp, exponent);
438 (void) BN_bin2bn(key+offset, (
int)(len - offset), modulus);
459 ctx = EVP_MD_CTX_create();
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);
468 EVP_MD_CTX_destroy(ctx);
483 const EVP_MD* md = NULL;
519 md = EVP_get_digestbyname(
"md_gost94");
575 sha1hash = (uint8_t)h;
604 (void)
ldns_sha1((
unsigned char *) ldns_buffer_begin(data_buf),
605 (
unsigned int) ldns_buffer_position(data_buf),
606 (
unsigned char *) digest);
615 (void)
ldns_sha256((
unsigned char *) ldns_buffer_begin(data_buf),
616 (
unsigned int) ldns_buffer_position(data_buf),
617 (
unsigned char *) digest);
626 (
unsigned int) ldns_buffer_position(data_buf),
627 (
unsigned char *) digest, md)) {
634 (
size_t)EVP_MD_size(md),
641 (void) SHA384((
unsigned char *) ldns_buffer_begin(data_buf),
642 (
unsigned int) ldns_buffer_position(data_buf),
643 (
unsigned char *) digest);
645 SHA384_DIGEST_LENGTH,
668 uint8_t *data = NULL;
669 uint8_t cur_data[32];
670 uint8_t cur_window = 0;
671 uint8_t cur_window_max = 0;
672 uint16_t cur_data_size = 0;
680 for (i = 0; i < size; i++) {
681 if (i_type < rr_type_list[i])
682 i_type = rr_type_list[i];
684 if (i_type < nsec_type) {
688 bm_len = i_type / 8 + 2;
690 if(!bitmap)
return NULL;
691 for (i = 0; i < bm_len; i++) {
695 for (i = 0; i < size; i++) {
696 i_type = rr_type_list[i];
698 (
int) (7 - (i_type % 8)),
703 memset(cur_data, 0, 32);
704 for (i = 0; i < bm_len; i++) {
705 if (i / 32 > cur_window) {
707 if (cur_window_max > 0) {
711 cur_data_size + cur_window_max + 3);
716 data[cur_data_size] = cur_window;
717 data[cur_data_size + 1] = cur_window_max + 1;
718 memcpy(data + cur_data_size + 2,
721 cur_data_size += cur_window_max + 3;
725 memset(cur_data, 0, 32);
727 cur_data[i%32] = bitmap[i];
729 cur_window_max = i%32;
732 if (cur_window_max > 0 || cur_data[0] != 0) {
736 cur_data_size + cur_window_max + 3);
741 data[cur_data_size] = cur_window;
742 data[cur_data_size + 1] = cur_window_max + 1;
743 memcpy(data + cur_data_size + 2, cur_data, cur_window_max+1);
744 cur_data_size += cur_window_max + 3;
762 if (cur_rrset->
type == type) {
765 cur_rrset = cur_rrset->
next;
777 size_t type_count = 0;
779 int on_delegation_point;
795 cur_rrsets = from->
rrsets;
799 if ((on_delegation_point && (
802 || (!on_delegation_point &&
806 types[type_count] = cur_rrsets->
type;
809 cur_rrsets = cur_rrsets->
next;
836 size_t type_count = 0;
839 int on_delegation_point;
868 cur_rrsets = from->
rrsets;
876 if ((on_delegation_point && (
879 || (!on_delegation_point &&
882 types[type_count] = cur_rrsets->
type;
885 cur_rrsets = cur_rrsets->
next;
890 if (type_count > 0 &&
930 size_t type_count = 0;
943 if (type_count == 0 || i_type_list[type_count-1] != i_type) {
944 i_type_list[type_count] = i_type;
970 size_t hashed_owner_str_len;
973 unsigned char *hashed_owner_str;
974 char *hashed_owner_b32;
975 size_t hashed_owner_b32_len;
990 fprintf(stderr,
"Memory error\n");
996 hashed_owner_str =
LDNS_XMALLOC(
unsigned char, hashed_owner_str_len);
997 if(!hashed_owner_str) {
1002 memcpy(hashed_owner_str +
ldns_rdf_size(cann), salt, salt_length);
1005 for (cur_it = iterations + 1; cur_it > 0; cur_it--) {
1006 (void)
ldns_sha1((
unsigned char *) hashed_owner_str,
1007 (
unsigned int) hashed_owner_str_len, hash);
1011 hashed_owner_str =
LDNS_XMALLOC(
unsigned char, hashed_owner_str_len);
1012 if (!hashed_owner_str) {
1015 memcpy(hashed_owner_str, hash, LDNS_SHA1_DIGEST_LENGTH);
1016 memcpy(hashed_owner_str + LDNS_SHA1_DIGEST_LENGTH, salt, salt_length);
1017 hashed_owner_str_len = LDNS_SHA1_DIGEST_LENGTH + salt_length;
1021 hashed_owner_str = hash;
1025 ldns_b32_ntop_calculate_size(hashed_owner_str_len) + 1);
1026 if(!hashed_owner_b32) {
1030 (uint8_t *) hashed_owner_str,
1031 hashed_owner_str_len,
1033 ldns_b32_ntop_calculate_size(hashed_owner_str_len)+1);
1034 if (hashed_owner_b32_len < 1) {
1035 fprintf(stderr,
"Error in base32 extended hex encoding ");
1036 fprintf(stderr,
"of hashed owner name (name: ");
1038 fprintf(stderr,
", return code: %u)\n",
1039 (
unsigned int) hashed_owner_b32_len);
1043 hashed_owner_b32[hashed_owner_b32_len] =
'\0';
1047 fprintf(stderr,
"Error creating rdf from %s\n", hashed_owner_b32);
1053 return hashed_owner;
1060 uint16_t iterations,
1061 uint8_t salt_length,
1065 uint8_t *salt_data = NULL;
1070 1, (
void*)&algorithm),
1091 salt_data[0] = salt_length;
1092 memcpy(salt_data + 1, salt, salt_length);
1112 if (!origin || !rr_list)
return 0;
1133 uint16_t iterations,
1134 uint8_t salt_length,
1136 bool emptynonterminal)
1148 size_t type_count = 0;
1182 if (type_count == 0 || i_type_list[type_count-1] != i_type) {
1183 i_type_list[type_count] = i_type;
1191 if (!emptynonterminal && !rr_list_delegation_only(cur_zone, rrs)) {
1281 uint8_t salt_length;
1288 if(!salt)
return NULL;
1319 uint16_t iterations;
1320 uint8_t salt_length;
1337 return hashed_owner;
1343 uint8_t window_block_nr;
1344 uint8_t bitmap_length;
1350 if (nsec_bitmap == NULL) {
1355 window_block_nr = data[pos];
1356 bitmap_length = data[pos + 1];
1359 for (bit_pos = 0; bit_pos < (bitmap_length) * 8; bit_pos++) {
1361 cur_type = 256 * (uint16_t) window_block_nr + bit_pos;
1362 if (cur_type == type) {
1368 pos += (uint16_t) bitmap_length;
1378 char *next_hash_str;
1461 t_netorder = htons(t);
1467 if (! sigs_covered) {
1498 char *next_nsec_owner_str;
1505 next_nsec_owner_label =
1508 next_nsec_owner_str =
ldns_rdf2str(next_nsec_owner_label);
1509 if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1511 next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1515 next_nsec_owner_str);
1517 next_nsec_rdf, 4)) {
1524 next_nsec_owner_label =
1528 next_nsec_owner_str =
ldns_rdf2str(next_nsec_owner_label);
1529 if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1531 next_nsec_owner_str[strlen(next_nsec_owner_str) - 1]
1535 next_nsec_owner_str);
1539 next_nsec_rdf, 4)) {
1552 if (rr1 == NULL && rr2 == NULL) {
1567 qsort(unsorted->
_rrs,
1612 unsigned char *dsasig_data = (
unsigned char*)ldns_buffer_begin(sig);
1615 dsasig = d2i_DSA_SIG(NULL,
1616 (
const unsigned char **)&dsasig_data,
1619 DSA_SIG_free(dsasig);
1625 DSA_SIG_free(dsasig);
1629 byte_offset = (size_t) (20 - BN_num_bytes(dsasig->r));
1630 if (byte_offset > 20) {
1631 DSA_SIG_free(dsasig);
1635 memset(&dsasig_data[1], 0, byte_offset);
1636 BN_bn2bin(dsasig->r, &dsasig_data[1 + byte_offset]);
1637 byte_offset = (size_t) (20 - BN_num_bytes(dsasig->s));
1638 if (byte_offset > 20) {
1639 DSA_SIG_free(dsasig);
1643 memset(&dsasig_data[21], 0, byte_offset);
1644 BN_bn2bin(dsasig->s, &dsasig_data[21 + byte_offset]);
1650 DSA_SIG_free(dsasig);
1662 unsigned char *raw_sig = NULL;
1670 (void) BN_bin2bn((
unsigned char *)
ldns_rdf_data(sig_rdf) + 1,
1671 SHA_DIGEST_LENGTH,
R);
1677 (void) BN_bin2bn((
unsigned char *)
ldns_rdf_data(sig_rdf) + 21,
1678 SHA_DIGEST_LENGTH, S);
1680 dsasig = DSA_SIG_new();
1690 raw_sig_len = i2d_DSA_SIG(dsasig, &raw_sig);
1691 if (raw_sig_len < 0) {
1692 DSA_SIG_free(dsasig);
1697 ldns_buffer_write(target_buffer, raw_sig, (
size_t)raw_sig_len);
1700 DSA_SIG_free(dsasig);
1703 return ldns_buffer_status(target_buffer);
1711 ECDSA_SIG* ecdsa_sig;
1712 unsigned char *data = (
unsigned char*)ldns_buffer_begin(sig);
1714 ecdsa_sig = d2i_ECDSA_SIG(NULL, (
const unsigned char **)&data, sig_len);
1715 if(!ecdsa_sig)
return NULL;
1719 BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s));
1721 ECDSA_SIG_free(ecdsa_sig);
1724 BN_bn2bin(ecdsa_sig->r, data);
1725 BN_bn2bin(ecdsa_sig->s, data+BN_num_bytes(ecdsa_sig->r));
1727 BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s)), data);
1728 ECDSA_SIG_free(ecdsa_sig);
1740 if(bnsize < 16 || (
size_t)bnsize*2 !=
ldns_rdf_size(sig_rdf))
1744 sig = ECDSA_SIG_new();
1746 sig->r = BN_bin2bn((
const unsigned char*)
ldns_rdf_data(sig_rdf),
1748 sig->s = BN_bin2bn((
const unsigned char*)
ldns_rdf_data(sig_rdf)+bnsize,
1750 if(!sig->r || !sig->s) {
1751 ECDSA_SIG_free(sig);
1755 raw_sig_len = i2d_ECDSA_SIG(sig, NULL);
1757 unsigned char* pp = (
unsigned char*)
1758 ldns_buffer_current(target_buffer);
1759 raw_sig_len = i2d_ECDSA_SIG(sig, &pp);
1760 ldns_buffer_skip(target_buffer, (ssize_t) raw_sig_len);
1762 ECDSA_SIG_free(sig);
1764 return ldns_buffer_status(target_buffer);