| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
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 |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#include |
|
15
|
|
|
|
|
|
|
#include |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#include |
|
18
|
|
|
|
|
|
|
#include |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#ifdef HAVE_SSL |
|
21
|
|
|
|
|
|
|
#include |
|
22
|
|
|
|
|
|
|
#include |
|
23
|
|
|
|
|
|
|
#include |
|
24
|
|
|
|
|
|
|
#include |
|
25
|
|
|
|
|
|
|
#include |
|
26
|
|
|
|
|
|
|
#endif |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
ldns_rr * |
|
29
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (!name || !rrs) { |
|
|
|
0
|
|
|
|
|
|
|
37
|
0
|
|
|
|
|
|
return NULL; |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) { |
|
41
|
0
|
|
|
|
|
|
candidate = ldns_rr_list_rr(rrs, i); |
|
42
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(candidate) == LDNS_RR_TYPE_RRSIG) { |
|
43
|
0
|
0
|
|
|
|
|
if (ldns_dname_compare(ldns_rr_owner(candidate), |
|
44
|
0
|
0
|
|
|
|
|
name) == 0 && |
|
45
|
0
|
|
|
|
|
|
ldns_rdf2rr_type(ldns_rr_rrsig_typecovered(candidate)) |
|
46
|
|
|
|
|
|
|
== type |
|
47
|
|
|
|
|
|
|
) { |
|
48
|
0
|
|
|
|
|
|
return candidate; |
|
49
|
|
|
|
|
|
|
} |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
|
|
53
|
0
|
|
|
|
|
|
return NULL; |
|
54
|
|
|
|
|
|
|
} |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
ldns_rr * |
|
57
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (!rrsig || !rrs) { |
|
|
|
0
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
return NULL; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) { |
|
68
|
0
|
|
|
|
|
|
candidate = ldns_rr_list_rr(rrs, i); |
|
69
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(candidate) == LDNS_RR_TYPE_DNSKEY) { |
|
70
|
0
|
0
|
|
|
|
|
if (ldns_dname_compare(ldns_rr_owner(candidate), |
|
71
|
0
|
0
|
|
|
|
|
ldns_rr_rrsig_signame(rrsig)) == 0 && |
|
72
|
0
|
|
|
|
|
|
ldns_rdf2native_int16(ldns_rr_rrsig_keytag(rrsig)) == |
|
73
|
0
|
|
|
|
|
|
ldns_calc_keytag(candidate) |
|
74
|
|
|
|
|
|
|
) { |
|
75
|
0
|
|
|
|
|
|
return candidate; |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
} |
|
78
|
|
|
|
|
|
|
} |
|
79
|
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return NULL; |
|
81
|
|
|
|
|
|
|
} |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
ldns_rdf * |
|
84
|
0
|
|
|
|
|
|
ldns_nsec_get_bitmap(ldns_rr *nsec) { |
|
85
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC) { |
|
86
|
0
|
|
|
|
|
|
return ldns_rr_rdf(nsec, 1); |
|
87
|
0
|
0
|
|
|
|
|
} else if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC3) { |
|
88
|
0
|
|
|
|
|
|
return ldns_rr_rdf(nsec, 5); |
|
89
|
|
|
|
|
|
|
} else { |
|
90
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
ldns_rdf *result = NULL; |
|
119
|
|
|
|
|
|
|
|
|
120
|
0
|
0
|
|
|
|
|
if (!qname || !nsec3s || ldns_rr_list_rr_count(nsec3s) < 1) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
return NULL; |
|
122
|
|
|
|
|
|
|
} |
|
123
|
|
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
nsec = ldns_rr_list_rr(nsec3s, 0); |
|
125
|
0
|
|
|
|
|
|
algorithm = ldns_nsec3_algorithm(nsec); |
|
126
|
0
|
|
|
|
|
|
salt_length = ldns_nsec3_salt_length(nsec); |
|
127
|
0
|
|
|
|
|
|
salt = ldns_nsec3_salt_data(nsec); |
|
128
|
0
|
|
|
|
|
|
iterations = ldns_nsec3_iterations(nsec); |
|
129
|
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
sname = ldns_rdf_clone(qname); |
|
131
|
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
flag = false; |
|
133
|
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
zone_name = ldns_dname_left_chop(ldns_rr_owner(nsec)); |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
/* algorithm from nsec3-07 8.3 */ |
|
137
|
0
|
0
|
|
|
|
|
while (ldns_dname_label_count(sname) > 0) { |
|
138
|
0
|
|
|
|
|
|
exact_match_found = false; |
|
139
|
0
|
|
|
|
|
|
in_range_found = false; |
|
140
|
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
hashed_sname = ldns_nsec3_hash_name(sname, |
|
142
|
|
|
|
|
|
|
algorithm, |
|
143
|
|
|
|
|
|
|
iterations, |
|
144
|
|
|
|
|
|
|
salt_length, |
|
145
|
|
|
|
|
|
|
salt); |
|
146
|
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
|
status = ldns_dname_cat(hashed_sname, zone_name); |
|
148
|
0
|
0
|
|
|
|
|
if(status != LDNS_STATUS_OK) { |
|
149
|
0
|
|
|
|
|
|
LDNS_FREE(salt); |
|
150
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(zone_name); |
|
151
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(sname); |
|
152
|
0
|
|
|
|
|
|
return NULL; |
|
153
|
|
|
|
|
|
|
} |
|
154
|
|
|
|
|
|
|
|
|
155
|
0
|
0
|
|
|
|
|
for (nsec_i = 0; nsec_i < ldns_rr_list_rr_count(nsec3s); nsec_i++) { |
|
156
|
0
|
|
|
|
|
|
nsec = ldns_rr_list_rr(nsec3s, nsec_i); |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
/* check values of iterations etc! */ |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
/* exact match? */ |
|
161
|
0
|
0
|
|
|
|
|
if (ldns_dname_compare(ldns_rr_owner(nsec), hashed_sname) == 0) { |
|
162
|
0
|
|
|
|
|
|
exact_match_found = true; |
|
163
|
0
|
0
|
|
|
|
|
} else if (ldns_nsec_covers_name(nsec, hashed_sname)) { |
|
164
|
0
|
|
|
|
|
|
in_range_found = true; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
} |
|
168
|
0
|
0
|
|
|
|
|
if (!exact_match_found && in_range_found) { |
|
|
|
0
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
|
flag = true; |
|
170
|
0
|
0
|
|
|
|
|
} else if (exact_match_found && flag) { |
|
|
|
0
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
result = ldns_rdf_clone(sname); |
|
172
|
|
|
|
|
|
|
/* RFC 5155: 8.3. 2.** "The proof is complete" */ |
|
173
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(hashed_sname); |
|
174
|
0
|
|
|
|
|
|
goto done; |
|
175
|
0
|
0
|
|
|
|
|
} else if (exact_match_found && !flag) { |
|
|
|
0
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
/* error! */ |
|
177
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(hashed_sname); |
|
178
|
0
|
|
|
|
|
|
goto done; |
|
179
|
|
|
|
|
|
|
} else { |
|
180
|
0
|
|
|
|
|
|
flag = false; |
|
181
|
|
|
|
|
|
|
} |
|
182
|
|
|
|
|
|
|
|
|
183
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(hashed_sname); |
|
184
|
0
|
|
|
|
|
|
tmp = sname; |
|
185
|
0
|
|
|
|
|
|
sname = ldns_dname_left_chop(sname); |
|
186
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(tmp); |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
done: |
|
190
|
0
|
|
|
|
|
|
LDNS_FREE(salt); |
|
191
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(zone_name); |
|
192
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(sname); |
|
193
|
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
|
return result; |
|
195
|
|
|
|
|
|
|
} |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
bool |
|
198
|
0
|
|
|
|
|
|
ldns_dnssec_pkt_has_rrsigs(const ldns_pkt *pkt) |
|
199
|
|
|
|
|
|
|
{ |
|
200
|
|
|
|
|
|
|
size_t i; |
|
201
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_pkt_ancount(pkt); i++) { |
|
202
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(ldns_rr_list_rr(ldns_pkt_answer(pkt), i)) == |
|
203
|
|
|
|
|
|
|
LDNS_RR_TYPE_RRSIG) { |
|
204
|
0
|
|
|
|
|
|
return true; |
|
205
|
|
|
|
|
|
|
} |
|
206
|
|
|
|
|
|
|
} |
|
207
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_pkt_nscount(pkt); i++) { |
|
208
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(ldns_rr_list_rr(ldns_pkt_authority(pkt), i)) == |
|
209
|
|
|
|
|
|
|
LDNS_RR_TYPE_RRSIG) { |
|
210
|
0
|
|
|
|
|
|
return true; |
|
211
|
|
|
|
|
|
|
} |
|
212
|
|
|
|
|
|
|
} |
|
213
|
0
|
|
|
|
|
|
return false; |
|
214
|
|
|
|
|
|
|
} |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
ldns_rr_list * |
|
217
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
t_netorder = htons(type); /* rdf are in network order! */ |
|
233
|
0
|
|
|
|
|
|
rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE, LDNS_RDF_SIZE_WORD, &t_netorder); |
|
234
|
0
|
|
|
|
|
|
sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0); |
|
235
|
|
|
|
|
|
|
|
|
236
|
0
|
|
|
|
|
|
ldns_rdf_free(rdf_t); |
|
237
|
0
|
|
|
|
|
|
ldns_rr_list_deep_free(sigs); |
|
238
|
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
return sigs_covered; |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
} |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
ldns_rr_list * |
|
244
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
sigs = ldns_pkt_rr_list_by_type(pkt, |
|
252
|
|
|
|
|
|
|
LDNS_RR_TYPE_RRSIG, |
|
253
|
|
|
|
|
|
|
LDNS_SECTION_ANY_NOQUESTION |
|
254
|
|
|
|
|
|
|
); |
|
255
|
|
|
|
|
|
|
|
|
256
|
0
|
|
|
|
|
|
t_netorder = htons(type); /* rdf are in network order! */ |
|
257
|
0
|
|
|
|
|
|
rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE, |
|
258
|
|
|
|
|
|
|
2, |
|
259
|
|
|
|
|
|
|
&t_netorder); |
|
260
|
0
|
|
|
|
|
|
sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0); |
|
261
|
|
|
|
|
|
|
|
|
262
|
0
|
|
|
|
|
|
ldns_rdf_free(rdf_t); |
|
263
|
0
|
|
|
|
|
|
ldns_rr_list_deep_free(sigs); |
|
264
|
|
|
|
|
|
|
|
|
265
|
0
|
|
|
|
|
|
return sigs_covered; |
|
266
|
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
} |
|
268
|
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
/* used only on the public key RR */ |
|
270
|
|
|
|
|
|
|
uint16_t |
|
271
|
19
|
|
|
|
|
|
ldns_calc_keytag(const ldns_rr *key) |
|
272
|
|
|
|
|
|
|
{ |
|
273
|
|
|
|
|
|
|
uint16_t ac16; |
|
274
|
|
|
|
|
|
|
ldns_buffer *keybuf; |
|
275
|
|
|
|
|
|
|
size_t keysize; |
|
276
|
|
|
|
|
|
|
|
|
277
|
19
|
50
|
|
|
|
|
if (!key) { |
|
278
|
0
|
|
|
|
|
|
return 0; |
|
279
|
|
|
|
|
|
|
} |
|
280
|
|
|
|
|
|
|
|
|
281
|
19
|
|
|
|
|
|
if (ldns_rr_get_type(key) != LDNS_RR_TYPE_DNSKEY && |
|
282
|
0
|
|
|
|
|
|
ldns_rr_get_type(key) != LDNS_RR_TYPE_KEY |
|
283
|
|
|
|
|
|
|
) { |
|
284
|
0
|
|
|
|
|
|
return 0; |
|
285
|
|
|
|
|
|
|
} |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
/* rdata to buf - only put the rdata in a buffer */ |
|
288
|
19
|
|
|
|
|
|
keybuf = ldns_buffer_new(LDNS_MIN_BUFLEN); /* grows */ |
|
289
|
19
|
50
|
|
|
|
|
if (!keybuf) { |
|
290
|
0
|
|
|
|
|
|
return 0; |
|
291
|
|
|
|
|
|
|
} |
|
292
|
19
|
|
|
|
|
|
(void)ldns_rr_rdata2buffer_wire(keybuf, key); |
|
293
|
|
|
|
|
|
|
/* the current pos in the buffer is the keysize */ |
|
294
|
19
|
|
|
|
|
|
keysize= ldns_buffer_position(keybuf); |
|
295
|
|
|
|
|
|
|
|
|
296
|
19
|
|
|
|
|
|
ac16 = ldns_calc_keytag_raw(ldns_buffer_begin(keybuf), keysize); |
|
297
|
19
|
|
|
|
|
|
ldns_buffer_free(keybuf); |
|
298
|
19
|
|
|
|
|
|
return ac16; |
|
299
|
|
|
|
|
|
|
} |
|
300
|
|
|
|
|
|
|
|
|
301
|
19
|
|
|
|
|
|
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
|
19
|
50
|
|
|
|
|
if(keysize < 4) { |
|
308
|
0
|
|
|
|
|
|
return 0; |
|
309
|
|
|
|
|
|
|
} |
|
310
|
|
|
|
|
|
|
/* look at the algorithm field, copied from 2535bis */ |
|
311
|
19
|
50
|
|
|
|
|
if (key[3] == LDNS_RSAMD5) { |
|
312
|
0
|
|
|
|
|
|
ac16 = 0; |
|
313
|
0
|
0
|
|
|
|
|
if (keysize > 4) { |
|
314
|
0
|
|
|
|
|
|
memmove(&ac16, key + keysize - 3, 2); |
|
315
|
|
|
|
|
|
|
} |
|
316
|
0
|
|
|
|
|
|
ac16 = ntohs(ac16); |
|
317
|
0
|
|
|
|
|
|
return (uint16_t) ac16; |
|
318
|
|
|
|
|
|
|
} else { |
|
319
|
19
|
|
|
|
|
|
ac32 = 0; |
|
320
|
4531
|
100
|
|
|
|
|
for (i = 0; (size_t)i < keysize; ++i) { |
|
321
|
4512
|
100
|
|
|
|
|
ac32 += (i & 1) ? key[i] : key[i] << 8; |
|
322
|
|
|
|
|
|
|
} |
|
323
|
19
|
|
|
|
|
|
ac32 += (ac32 >> 16) & 0xFFFF; |
|
324
|
19
|
|
|
|
|
|
return (uint16_t) (ac32 & 0xFFFF); |
|
325
|
|
|
|
|
|
|
} |
|
326
|
|
|
|
|
|
|
} |
|
327
|
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
#ifdef HAVE_SSL |
|
329
|
|
|
|
|
|
|
DSA * |
|
330
|
0
|
|
|
|
|
|
ldns_key_buf2dsa(ldns_buffer *key) |
|
331
|
|
|
|
|
|
|
{ |
|
332
|
0
|
|
|
|
|
|
return ldns_key_buf2dsa_raw((unsigned char*)ldns_buffer_begin(key), |
|
333
|
|
|
|
|
|
|
ldns_buffer_position(key)); |
|
334
|
|
|
|
|
|
|
} |
|
335
|
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
DSA * |
|
337
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if(len == 0) |
|
347
|
0
|
|
|
|
|
|
return NULL; |
|
348
|
0
|
|
|
|
|
|
T = (uint8_t)key[0]; |
|
349
|
0
|
|
|
|
|
|
length = (64 + T * 8); |
|
350
|
0
|
|
|
|
|
|
offset = 1; |
|
351
|
|
|
|
|
|
|
|
|
352
|
0
|
0
|
|
|
|
|
if (T > 8) { |
|
353
|
0
|
|
|
|
|
|
return NULL; |
|
354
|
|
|
|
|
|
|
} |
|
355
|
0
|
0
|
|
|
|
|
if(len < (size_t)1 + SHA_DIGEST_LENGTH + 3*length) |
|
356
|
0
|
|
|
|
|
|
return NULL; |
|
357
|
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
Q = BN_bin2bn(key+offset, SHA_DIGEST_LENGTH, NULL); |
|
359
|
0
|
|
|
|
|
|
offset += SHA_DIGEST_LENGTH; |
|
360
|
|
|
|
|
|
|
|
|
361
|
0
|
|
|
|
|
|
P = BN_bin2bn(key+offset, (int)length, NULL); |
|
362
|
0
|
|
|
|
|
|
offset += length; |
|
363
|
|
|
|
|
|
|
|
|
364
|
0
|
|
|
|
|
|
G = BN_bin2bn(key+offset, (int)length, NULL); |
|
365
|
0
|
|
|
|
|
|
offset += length; |
|
366
|
|
|
|
|
|
|
|
|
367
|
0
|
|
|
|
|
|
Y = BN_bin2bn(key+offset, (int)length, NULL); |
|
368
|
0
|
|
|
|
|
|
offset += length; |
|
369
|
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
/* create the key and set its properties */ |
|
371
|
0
|
0
|
|
|
|
|
if(!Q || !P || !G || !Y || !(dsa = DSA_new())) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
372
|
0
|
|
|
|
|
|
BN_free(Q); |
|
373
|
0
|
|
|
|
|
|
BN_free(P); |
|
374
|
0
|
|
|
|
|
|
BN_free(G); |
|
375
|
0
|
|
|
|
|
|
BN_free(Y); |
|
376
|
0
|
|
|
|
|
|
return NULL; |
|
377
|
|
|
|
|
|
|
} |
|
378
|
|
|
|
|
|
|
#ifndef S_SPLINT_S |
|
379
|
0
|
|
|
|
|
|
dsa->p = P; |
|
380
|
0
|
|
|
|
|
|
dsa->q = Q; |
|
381
|
0
|
|
|
|
|
|
dsa->g = G; |
|
382
|
0
|
|
|
|
|
|
dsa->pub_key = Y; |
|
383
|
|
|
|
|
|
|
#endif /* splint */ |
|
384
|
|
|
|
|
|
|
|
|
385
|
0
|
|
|
|
|
|
return dsa; |
|
386
|
|
|
|
|
|
|
} |
|
387
|
|
|
|
|
|
|
|
|
388
|
|
|
|
|
|
|
RSA * |
|
389
|
0
|
|
|
|
|
|
ldns_key_buf2rsa(ldns_buffer *key) |
|
390
|
|
|
|
|
|
|
{ |
|
391
|
0
|
|
|
|
|
|
return ldns_key_buf2rsa_raw((unsigned char*)ldns_buffer_begin(key), |
|
392
|
|
|
|
|
|
|
ldns_buffer_position(key)); |
|
393
|
|
|
|
|
|
|
} |
|
394
|
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
RSA * |
|
396
|
3
|
|
|
|
|
|
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
|
3
|
50
|
|
|
|
|
if (len == 0) |
|
406
|
0
|
|
|
|
|
|
return NULL; |
|
407
|
3
|
50
|
|
|
|
|
if (key[0] == 0) { |
|
408
|
0
|
0
|
|
|
|
|
if(len < 3) |
|
409
|
0
|
|
|
|
|
|
return NULL; |
|
410
|
|
|
|
|
|
|
/* need some smart comment here XXX*/ |
|
411
|
|
|
|
|
|
|
/* the exponent is too large so it's places |
|
412
|
|
|
|
|
|
|
* futher...???? */ |
|
413
|
0
|
|
|
|
|
|
memmove(&int16, key+1, 2); |
|
414
|
0
|
|
|
|
|
|
exp = ntohs(int16); |
|
415
|
0
|
|
|
|
|
|
offset = 3; |
|
416
|
|
|
|
|
|
|
} else { |
|
417
|
3
|
|
|
|
|
|
exp = key[0]; |
|
418
|
3
|
|
|
|
|
|
offset = 1; |
|
419
|
|
|
|
|
|
|
} |
|
420
|
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
/* key length at least one */ |
|
422
|
3
|
50
|
|
|
|
|
if(len < (size_t)offset + exp + 1) |
|
423
|
0
|
|
|
|
|
|
return NULL; |
|
424
|
|
|
|
|
|
|
|
|
425
|
|
|
|
|
|
|
/* Exponent */ |
|
426
|
3
|
|
|
|
|
|
exponent = BN_new(); |
|
427
|
3
|
50
|
|
|
|
|
if(!exponent) return NULL; |
|
428
|
3
|
|
|
|
|
|
(void) BN_bin2bn(key+offset, (int)exp, exponent); |
|
429
|
3
|
|
|
|
|
|
offset += exp; |
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
/* Modulus */ |
|
432
|
3
|
|
|
|
|
|
modulus = BN_new(); |
|
433
|
3
|
50
|
|
|
|
|
if(!modulus) { |
|
434
|
0
|
|
|
|
|
|
BN_free(exponent); |
|
435
|
0
|
|
|
|
|
|
return NULL; |
|
436
|
|
|
|
|
|
|
} |
|
437
|
|
|
|
|
|
|
/* length of the buffer must match the key length! */ |
|
438
|
3
|
|
|
|
|
|
(void) BN_bin2bn(key+offset, (int)(len - offset), modulus); |
|
439
|
|
|
|
|
|
|
|
|
440
|
3
|
|
|
|
|
|
rsa = RSA_new(); |
|
441
|
3
|
50
|
|
|
|
|
if(!rsa) { |
|
442
|
0
|
|
|
|
|
|
BN_free(exponent); |
|
443
|
0
|
|
|
|
|
|
BN_free(modulus); |
|
444
|
0
|
|
|
|
|
|
return NULL; |
|
445
|
|
|
|
|
|
|
} |
|
446
|
|
|
|
|
|
|
#ifndef S_SPLINT_S |
|
447
|
3
|
|
|
|
|
|
rsa->n = modulus; |
|
448
|
3
|
|
|
|
|
|
rsa->e = exponent; |
|
449
|
|
|
|
|
|
|
#endif /* splint */ |
|
450
|
|
|
|
|
|
|
|
|
451
|
3
|
|
|
|
|
|
return rsa; |
|
452
|
|
|
|
|
|
|
} |
|
453
|
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
int |
|
455
|
2
|
|
|
|
|
|
ldns_digest_evp(unsigned char* data, unsigned int len, unsigned char* dest, |
|
456
|
|
|
|
|
|
|
const EVP_MD* md) |
|
457
|
|
|
|
|
|
|
{ |
|
458
|
|
|
|
|
|
|
EVP_MD_CTX* ctx; |
|
459
|
2
|
|
|
|
|
|
ctx = EVP_MD_CTX_create(); |
|
460
|
2
|
50
|
|
|
|
|
if(!ctx) |
|
461
|
0
|
|
|
|
|
|
return false; |
|
462
|
4
|
|
|
|
|
|
if(!EVP_DigestInit_ex(ctx, md, NULL) || |
|
463
|
4
|
50
|
|
|
|
|
!EVP_DigestUpdate(ctx, data, len) || |
|
464
|
2
|
|
|
|
|
|
!EVP_DigestFinal_ex(ctx, dest, NULL)) { |
|
465
|
0
|
|
|
|
|
|
EVP_MD_CTX_destroy(ctx); |
|
466
|
0
|
|
|
|
|
|
return false; |
|
467
|
|
|
|
|
|
|
} |
|
468
|
2
|
|
|
|
|
|
EVP_MD_CTX_destroy(ctx); |
|
469
|
2
|
|
|
|
|
|
return true; |
|
470
|
|
|
|
|
|
|
} |
|
471
|
|
|
|
|
|
|
#endif /* HAVE_SSL */ |
|
472
|
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
ldns_rr * |
|
474
|
10
|
|
|
|
|
|
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
|
10
|
|
|
|
|
|
const EVP_MD* md = NULL; |
|
484
|
|
|
|
|
|
|
#endif |
|
485
|
|
|
|
|
|
|
|
|
486
|
10
|
50
|
|
|
|
|
if (ldns_rr_get_type(key) != LDNS_RR_TYPE_DNSKEY) { |
|
487
|
0
|
|
|
|
|
|
return NULL; |
|
488
|
|
|
|
|
|
|
} |
|
489
|
|
|
|
|
|
|
|
|
490
|
10
|
|
|
|
|
|
ds = ldns_rr_new(); |
|
491
|
10
|
50
|
|
|
|
|
if (!ds) { |
|
492
|
0
|
|
|
|
|
|
return NULL; |
|
493
|
|
|
|
|
|
|
} |
|
494
|
10
|
|
|
|
|
|
ldns_rr_set_type(ds, LDNS_RR_TYPE_DS); |
|
495
|
10
|
|
|
|
|
|
ldns_rr_set_owner(ds, ldns_rdf_clone( |
|
496
|
10
|
|
|
|
|
|
ldns_rr_owner(key))); |
|
497
|
10
|
|
|
|
|
|
ldns_rr_set_ttl(ds, ldns_rr_ttl(key)); |
|
498
|
10
|
|
|
|
|
|
ldns_rr_set_class(ds, ldns_rr_get_class(key)); |
|
499
|
|
|
|
|
|
|
|
|
500
|
10
|
|
|
|
|
|
switch(h) { |
|
501
|
|
|
|
|
|
|
default: |
|
502
|
|
|
|
|
|
|
case LDNS_SHA1: |
|
503
|
3
|
|
|
|
|
|
digest = LDNS_XMALLOC(uint8_t, LDNS_SHA1_DIGEST_LENGTH); |
|
504
|
3
|
50
|
|
|
|
|
if (!digest) { |
|
505
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
506
|
0
|
|
|
|
|
|
return NULL; |
|
507
|
|
|
|
|
|
|
} |
|
508
|
3
|
|
|
|
|
|
break; |
|
509
|
|
|
|
|
|
|
case LDNS_SHA256: |
|
510
|
3
|
|
|
|
|
|
digest = LDNS_XMALLOC(uint8_t, LDNS_SHA256_DIGEST_LENGTH); |
|
511
|
3
|
50
|
|
|
|
|
if (!digest) { |
|
512
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
513
|
0
|
|
|
|
|
|
return NULL; |
|
514
|
|
|
|
|
|
|
} |
|
515
|
3
|
|
|
|
|
|
break; |
|
516
|
|
|
|
|
|
|
case LDNS_HASH_GOST: |
|
517
|
|
|
|
|
|
|
#ifdef USE_GOST |
|
518
|
2
|
|
|
|
|
|
(void)ldns_key_EVP_load_gost_id(); |
|
519
|
2
|
|
|
|
|
|
md = EVP_get_digestbyname("md_gost94"); |
|
520
|
2
|
50
|
|
|
|
|
if(!md) { |
|
521
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
522
|
0
|
|
|
|
|
|
return NULL; |
|
523
|
|
|
|
|
|
|
} |
|
524
|
2
|
|
|
|
|
|
digest = LDNS_XMALLOC(uint8_t, EVP_MD_size(md)); |
|
525
|
2
|
50
|
|
|
|
|
if (!digest) { |
|
526
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
527
|
0
|
|
|
|
|
|
return NULL; |
|
528
|
|
|
|
|
|
|
} |
|
529
|
2
|
|
|
|
|
|
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
|
2
|
|
|
|
|
|
digest = LDNS_XMALLOC(uint8_t, SHA384_DIGEST_LENGTH); |
|
538
|
2
|
50
|
|
|
|
|
if (!digest) { |
|
539
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
540
|
0
|
|
|
|
|
|
return NULL; |
|
541
|
|
|
|
|
|
|
} |
|
542
|
2
|
|
|
|
|
|
break; |
|
543
|
|
|
|
|
|
|
#else |
|
544
|
|
|
|
|
|
|
/* not implemented */ |
|
545
|
|
|
|
|
|
|
ldns_rr_free(ds); |
|
546
|
|
|
|
|
|
|
return NULL; |
|
547
|
|
|
|
|
|
|
#endif |
|
548
|
|
|
|
|
|
|
} |
|
549
|
|
|
|
|
|
|
|
|
550
|
10
|
|
|
|
|
|
data_buf = ldns_buffer_new(LDNS_MAX_PACKETLEN); |
|
551
|
10
|
50
|
|
|
|
|
if (!data_buf) { |
|
552
|
0
|
|
|
|
|
|
LDNS_FREE(digest); |
|
553
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
554
|
0
|
|
|
|
|
|
return NULL; |
|
555
|
|
|
|
|
|
|
} |
|
556
|
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
/* keytag */ |
|
558
|
10
|
|
|
|
|
|
keytag = htons(ldns_calc_keytag((ldns_rr*)key)); |
|
559
|
10
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT16, |
|
560
|
|
|
|
|
|
|
sizeof(uint16_t), |
|
561
|
|
|
|
|
|
|
&keytag); |
|
562
|
10
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
563
|
|
|
|
|
|
|
|
|
564
|
|
|
|
|
|
|
/* copy the algorithm field */ |
|
565
|
10
|
50
|
|
|
|
|
if ((tmp = ldns_rr_rdf(key, 2)) == NULL) { |
|
566
|
0
|
|
|
|
|
|
LDNS_FREE(digest); |
|
567
|
0
|
|
|
|
|
|
ldns_buffer_free(data_buf); |
|
568
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
569
|
0
|
|
|
|
|
|
return NULL; |
|
570
|
|
|
|
|
|
|
} else { |
|
571
|
10
|
|
|
|
|
|
ldns_rr_push_rdf(ds, ldns_rdf_clone( tmp )); |
|
572
|
|
|
|
|
|
|
} |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
/* digest hash type */ |
|
575
|
10
|
|
|
|
|
|
sha1hash = (uint8_t)h; |
|
576
|
10
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8, |
|
577
|
|
|
|
|
|
|
sizeof(uint8_t), |
|
578
|
|
|
|
|
|
|
&sha1hash); |
|
579
|
10
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
580
|
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
/* digest */ |
|
582
|
|
|
|
|
|
|
/* owner name */ |
|
583
|
10
|
|
|
|
|
|
tmp = ldns_rdf_clone(ldns_rr_owner(key)); |
|
584
|
10
|
|
|
|
|
|
ldns_dname2canonical(tmp); |
|
585
|
10
|
50
|
|
|
|
|
if (ldns_rdf2buffer_wire(data_buf, tmp) != LDNS_STATUS_OK) { |
|
586
|
0
|
|
|
|
|
|
LDNS_FREE(digest); |
|
587
|
0
|
|
|
|
|
|
ldns_buffer_free(data_buf); |
|
588
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
589
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(tmp); |
|
590
|
0
|
|
|
|
|
|
return NULL; |
|
591
|
|
|
|
|
|
|
} |
|
592
|
10
|
|
|
|
|
|
ldns_rdf_deep_free(tmp); |
|
593
|
|
|
|
|
|
|
|
|
594
|
|
|
|
|
|
|
/* all the rdata's */ |
|
595
|
10
|
50
|
|
|
|
|
if (ldns_rr_rdata2buffer_wire(data_buf, |
|
596
|
|
|
|
|
|
|
(ldns_rr*)key) != LDNS_STATUS_OK) { |
|
597
|
0
|
|
|
|
|
|
LDNS_FREE(digest); |
|
598
|
0
|
|
|
|
|
|
ldns_buffer_free(data_buf); |
|
599
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
600
|
0
|
|
|
|
|
|
return NULL; |
|
601
|
|
|
|
|
|
|
} |
|
602
|
10
|
|
|
|
|
|
switch(h) { |
|
603
|
|
|
|
|
|
|
case LDNS_SHA1: |
|
604
|
3
|
|
|
|
|
|
(void) ldns_sha1((unsigned char *) ldns_buffer_begin(data_buf), |
|
605
|
3
|
|
|
|
|
|
(unsigned int) ldns_buffer_position(data_buf), |
|
606
|
|
|
|
|
|
|
(unsigned char *) digest); |
|
607
|
|
|
|
|
|
|
|
|
608
|
3
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX, |
|
609
|
|
|
|
|
|
|
LDNS_SHA1_DIGEST_LENGTH, |
|
610
|
|
|
|
|
|
|
digest); |
|
611
|
3
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
612
|
|
|
|
|
|
|
|
|
613
|
3
|
|
|
|
|
|
break; |
|
614
|
|
|
|
|
|
|
case LDNS_SHA256: |
|
615
|
3
|
|
|
|
|
|
(void) ldns_sha256((unsigned char *) ldns_buffer_begin(data_buf), |
|
616
|
3
|
|
|
|
|
|
(unsigned int) ldns_buffer_position(data_buf), |
|
617
|
|
|
|
|
|
|
(unsigned char *) digest); |
|
618
|
3
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX, |
|
619
|
|
|
|
|
|
|
LDNS_SHA256_DIGEST_LENGTH, |
|
620
|
|
|
|
|
|
|
digest); |
|
621
|
3
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
622
|
3
|
|
|
|
|
|
break; |
|
623
|
|
|
|
|
|
|
case LDNS_HASH_GOST: |
|
624
|
|
|
|
|
|
|
#ifdef USE_GOST |
|
625
|
2
|
50
|
|
|
|
|
if(!ldns_digest_evp((unsigned char *) ldns_buffer_begin(data_buf), |
|
626
|
2
|
|
|
|
|
|
(unsigned int) ldns_buffer_position(data_buf), |
|
627
|
|
|
|
|
|
|
(unsigned char *) digest, md)) { |
|
628
|
0
|
|
|
|
|
|
LDNS_FREE(digest); |
|
629
|
0
|
|
|
|
|
|
ldns_buffer_free(data_buf); |
|
630
|
0
|
|
|
|
|
|
ldns_rr_free(ds); |
|
631
|
0
|
|
|
|
|
|
return NULL; |
|
632
|
|
|
|
|
|
|
} |
|
633
|
2
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX, |
|
634
|
2
|
|
|
|
|
|
(size_t)EVP_MD_size(md), |
|
635
|
|
|
|
|
|
|
digest); |
|
636
|
2
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
637
|
|
|
|
|
|
|
#endif |
|
638
|
2
|
|
|
|
|
|
break; |
|
639
|
|
|
|
|
|
|
case LDNS_SHA384: |
|
640
|
|
|
|
|
|
|
#ifdef USE_ECDSA |
|
641
|
2
|
|
|
|
|
|
(void) SHA384((unsigned char *) ldns_buffer_begin(data_buf), |
|
642
|
2
|
|
|
|
|
|
(unsigned int) ldns_buffer_position(data_buf), |
|
643
|
|
|
|
|
|
|
(unsigned char *) digest); |
|
644
|
2
|
|
|
|
|
|
tmp = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_HEX, |
|
645
|
|
|
|
|
|
|
SHA384_DIGEST_LENGTH, |
|
646
|
|
|
|
|
|
|
digest); |
|
647
|
2
|
|
|
|
|
|
ldns_rr_push_rdf(ds, tmp); |
|
648
|
|
|
|
|
|
|
#endif |
|
649
|
2
|
|
|
|
|
|
break; |
|
650
|
|
|
|
|
|
|
} |
|
651
|
|
|
|
|
|
|
|
|
652
|
10
|
|
|
|
|
|
LDNS_FREE(digest); |
|
653
|
10
|
|
|
|
|
|
ldns_buffer_free(data_buf); |
|
654
|
10
|
|
|
|
|
|
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
|
|
|
|
|
|
|
* |
|
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
|
3
|
|
|
|
|
|
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
|
3
|
|
|
|
|
|
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
|
3
|
|
|
|
|
|
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
|
3
|
50
|
|
|
|
|
if (nsec_type != LDNS_RR_TYPE_NSEC && |
|
|
|
0
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
nsec_type != LDNS_RR_TYPE_NSEC3) { |
|
706
|
0
|
|
|
|
|
|
return NULL; |
|
707
|
|
|
|
|
|
|
} |
|
708
|
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
/* Which other windows need to be in the bitmap rdf? |
|
710
|
|
|
|
|
|
|
*/ |
|
711
|
12
|
100
|
|
|
|
|
for (d = rr_type_list; d < rr_type_list + size; d++) { |
|
712
|
9
|
|
|
|
|
|
window = *d >> 8; |
|
713
|
9
|
|
|
|
|
|
subtype = *d & 0xff; |
|
714
|
9
|
50
|
|
|
|
|
if (windows[window] < subtype) { |
|
715
|
9
|
|
|
|
|
|
windows[window] = subtype; |
|
716
|
|
|
|
|
|
|
} |
|
717
|
|
|
|
|
|
|
} |
|
718
|
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
/* How much space do we need in the rdf for those windows? |
|
720
|
|
|
|
|
|
|
*/ |
|
721
|
3
|
|
|
|
|
|
sz = 0; |
|
722
|
771
|
100
|
|
|
|
|
for (i = 0; i < 256; i++) { |
|
723
|
768
|
100
|
|
|
|
|
if (windows[i]) { |
|
724
|
3
|
|
|
|
|
|
sz += windows[i] / 8 + 3; |
|
725
|
|
|
|
|
|
|
} |
|
726
|
|
|
|
|
|
|
} |
|
727
|
3
|
50
|
|
|
|
|
if (sz > 0) { |
|
728
|
|
|
|
|
|
|
/* Format rdf data according RFC3845 Section 2.1.2 (see above) |
|
729
|
|
|
|
|
|
|
*/ |
|
730
|
3
|
|
|
|
|
|
dptr = data = LDNS_CALLOC(uint8_t, sz); |
|
731
|
3
|
50
|
|
|
|
|
if (!data) { |
|
732
|
0
|
|
|
|
|
|
return NULL; |
|
733
|
|
|
|
|
|
|
} |
|
734
|
771
|
100
|
|
|
|
|
for (i = 0; i < 256; i++) { |
|
735
|
768
|
100
|
|
|
|
|
if (windows[i]) { |
|
736
|
3
|
|
|
|
|
|
*dptr++ = (uint8_t)i; |
|
737
|
3
|
|
|
|
|
|
*dptr++ = (uint8_t)(windows[i] / 8 + 1); |
|
738
|
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
/* Now let windows[i] index the bitmap |
|
740
|
|
|
|
|
|
|
* within data |
|
741
|
|
|
|
|
|
|
*/ |
|
742
|
3
|
|
|
|
|
|
windows[i] = (uint16_t)(dptr - data); |
|
743
|
|
|
|
|
|
|
|
|
744
|
3
|
|
|
|
|
|
dptr += dptr[-1]; |
|
745
|
|
|
|
|
|
|
} |
|
746
|
|
|
|
|
|
|
} |
|
747
|
|
|
|
|
|
|
} |
|
748
|
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
/* Set the bits? |
|
750
|
|
|
|
|
|
|
*/ |
|
751
|
12
|
100
|
|
|
|
|
for (d = rr_type_list; d < rr_type_list + size; d++) { |
|
752
|
9
|
|
|
|
|
|
subtype = *d & 0xff; |
|
753
|
9
|
|
|
|
|
|
data[windows[*d >> 8] + subtype/8] |= (0x80 >> (subtype % 8)); |
|
754
|
|
|
|
|
|
|
} |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
|
|
|
/* Allocate and return rdf structure for the data |
|
757
|
|
|
|
|
|
|
*/ |
|
758
|
3
|
|
|
|
|
|
rdf = ldns_rdf_new(LDNS_RDF_TYPE_BITMAP, sz, data); |
|
759
|
3
|
50
|
|
|
|
|
if (!rdf) { |
|
760
|
0
|
|
|
|
|
|
LDNS_FREE(data); |
|
761
|
0
|
|
|
|
|
|
return NULL; |
|
762
|
|
|
|
|
|
|
} |
|
763
|
3
|
|
|
|
|
|
return rdf; |
|
764
|
|
|
|
|
|
|
} |
|
765
|
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
int |
|
767
|
0
|
|
|
|
|
|
ldns_dnssec_rrsets_contains_type(ldns_dnssec_rrsets *rrsets, |
|
768
|
|
|
|
|
|
|
ldns_rr_type type) |
|
769
|
|
|
|
|
|
|
{ |
|
770
|
0
|
|
|
|
|
|
ldns_dnssec_rrsets *cur_rrset = rrsets; |
|
771
|
0
|
0
|
|
|
|
|
while (cur_rrset) { |
|
772
|
0
|
0
|
|
|
|
|
if (cur_rrset->type == type) { |
|
773
|
0
|
|
|
|
|
|
return 1; |
|
774
|
|
|
|
|
|
|
} |
|
775
|
0
|
|
|
|
|
|
cur_rrset = cur_rrset->next; |
|
776
|
|
|
|
|
|
|
} |
|
777
|
0
|
|
|
|
|
|
return 0; |
|
778
|
|
|
|
|
|
|
} |
|
779
|
|
|
|
|
|
|
|
|
780
|
|
|
|
|
|
|
ldns_rr * |
|
781
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
size_t type_count = 0; |
|
788
|
|
|
|
|
|
|
ldns_dnssec_rrsets *cur_rrsets; |
|
789
|
|
|
|
|
|
|
int on_delegation_point; |
|
790
|
|
|
|
|
|
|
|
|
791
|
0
|
0
|
|
|
|
|
if (!from || !to || (nsec_type != LDNS_RR_TYPE_NSEC)) { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
792
|
0
|
|
|
|
|
|
return NULL; |
|
793
|
|
|
|
|
|
|
} |
|
794
|
|
|
|
|
|
|
|
|
795
|
0
|
|
|
|
|
|
nsec_rr = ldns_rr_new(); |
|
796
|
0
|
|
|
|
|
|
ldns_rr_set_type(nsec_rr, nsec_type); |
|
797
|
0
|
|
|
|
|
|
ldns_rr_set_owner(nsec_rr, ldns_rdf_clone(ldns_dnssec_name_name(from))); |
|
798
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec_rr, ldns_rdf_clone(ldns_dnssec_name_name(to))); |
|
799
|
|
|
|
|
|
|
|
|
800
|
0
|
|
|
|
|
|
on_delegation_point = ldns_dnssec_rrsets_contains_type( |
|
801
|
|
|
|
|
|
|
from->rrsets, LDNS_RR_TYPE_NS) |
|
802
|
0
|
0
|
|
|
|
|
&& !ldns_dnssec_rrsets_contains_type( |
|
|
|
0
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
from->rrsets, LDNS_RR_TYPE_SOA); |
|
804
|
|
|
|
|
|
|
|
|
805
|
0
|
|
|
|
|
|
cur_rrsets = from->rrsets; |
|
806
|
0
|
0
|
|
|
|
|
while (cur_rrsets) { |
|
807
|
|
|
|
|
|
|
/* Do not include non-authoritative rrsets on the delegation point |
|
808
|
|
|
|
|
|
|
* in the type bitmap */ |
|
809
|
0
|
0
|
|
|
|
|
if ((on_delegation_point && ( |
|
|
|
0
|
|
|
|
|
|
|
810
|
0
|
|
|
|
|
|
cur_rrsets->type == LDNS_RR_TYPE_NS |
|
811
|
0
|
0
|
|
|
|
|
|| cur_rrsets->type == LDNS_RR_TYPE_DS)) |
|
812
|
0
|
0
|
|
|
|
|
|| (!on_delegation_point && |
|
|
|
0
|
|
|
|
|
|
|
813
|
0
|
|
|
|
|
|
cur_rrsets->type != LDNS_RR_TYPE_RRSIG |
|
814
|
0
|
0
|
|
|
|
|
&& cur_rrsets->type != LDNS_RR_TYPE_NSEC)) { |
|
815
|
|
|
|
|
|
|
|
|
816
|
0
|
|
|
|
|
|
types[type_count] = cur_rrsets->type; |
|
817
|
0
|
|
|
|
|
|
type_count++; |
|
818
|
|
|
|
|
|
|
} |
|
819
|
0
|
|
|
|
|
|
cur_rrsets = cur_rrsets->next; |
|
820
|
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
} |
|
822
|
0
|
|
|
|
|
|
types[type_count] = LDNS_RR_TYPE_RRSIG; |
|
823
|
0
|
|
|
|
|
|
type_count++; |
|
824
|
0
|
|
|
|
|
|
types[type_count] = LDNS_RR_TYPE_NSEC; |
|
825
|
0
|
|
|
|
|
|
type_count++; |
|
826
|
|
|
|
|
|
|
|
|
827
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec_rr, ldns_dnssec_create_nsec_bitmap(types, |
|
828
|
|
|
|
|
|
|
type_count, |
|
829
|
|
|
|
|
|
|
nsec_type)); |
|
830
|
|
|
|
|
|
|
|
|
831
|
0
|
|
|
|
|
|
return nsec_rr; |
|
832
|
|
|
|
|
|
|
} |
|
833
|
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
ldns_rr * |
|
835
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
size_t type_count = 0; |
|
847
|
|
|
|
|
|
|
ldns_dnssec_rrsets *cur_rrsets; |
|
848
|
|
|
|
|
|
|
ldns_status status; |
|
849
|
|
|
|
|
|
|
int on_delegation_point; |
|
850
|
|
|
|
|
|
|
|
|
851
|
0
|
0
|
|
|
|
|
if (!from) { |
|
852
|
0
|
|
|
|
|
|
return NULL; |
|
853
|
|
|
|
|
|
|
} |
|
854
|
|
|
|
|
|
|
|
|
855
|
0
|
|
|
|
|
|
nsec_rr = ldns_rr_new_frm_type(LDNS_RR_TYPE_NSEC3); |
|
856
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
status = ldns_dname_cat(ldns_rr_owner(nsec_rr), zone_name); |
|
863
|
0
|
0
|
|
|
|
|
if(status != LDNS_STATUS_OK) { |
|
864
|
0
|
|
|
|
|
|
ldns_rr_free(nsec_rr); |
|
865
|
0
|
|
|
|
|
|
return NULL; |
|
866
|
|
|
|
|
|
|
} |
|
867
|
0
|
|
|
|
|
|
ldns_nsec3_add_param_rdfs(nsec_rr, |
|
868
|
|
|
|
|
|
|
algorithm, |
|
869
|
|
|
|
|
|
|
flags, |
|
870
|
|
|
|
|
|
|
iterations, |
|
871
|
|
|
|
|
|
|
salt_length, |
|
872
|
|
|
|
|
|
|
salt); |
|
873
|
|
|
|
|
|
|
|
|
874
|
0
|
|
|
|
|
|
on_delegation_point = ldns_dnssec_rrsets_contains_type( |
|
875
|
|
|
|
|
|
|
from->rrsets, LDNS_RR_TYPE_NS) |
|
876
|
0
|
0
|
|
|
|
|
&& !ldns_dnssec_rrsets_contains_type( |
|
|
|
0
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
from->rrsets, LDNS_RR_TYPE_SOA); |
|
878
|
0
|
|
|
|
|
|
cur_rrsets = from->rrsets; |
|
879
|
0
|
0
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if ((on_delegation_point && ( |
|
|
|
0
|
|
|
|
|
|
|
887
|
0
|
|
|
|
|
|
cur_rrsets->type == LDNS_RR_TYPE_NS |
|
888
|
0
|
0
|
|
|
|
|
|| cur_rrsets->type == LDNS_RR_TYPE_DS)) |
|
889
|
0
|
0
|
|
|
|
|
|| (!on_delegation_point && |
|
|
|
0
|
|
|
|
|
|
|
890
|
0
|
|
|
|
|
|
cur_rrsets->type != LDNS_RR_TYPE_RRSIG)) { |
|
891
|
|
|
|
|
|
|
|
|
892
|
0
|
|
|
|
|
|
types[type_count] = cur_rrsets->type; |
|
893
|
0
|
|
|
|
|
|
type_count++; |
|
894
|
|
|
|
|
|
|
} |
|
895
|
0
|
|
|
|
|
|
cur_rrsets = cur_rrsets->next; |
|
896
|
|
|
|
|
|
|
} |
|
897
|
|
|
|
|
|
|
/* always add rrsig type if this is not an unsigned |
|
898
|
|
|
|
|
|
|
* delegation |
|
899
|
|
|
|
|
|
|
*/ |
|
900
|
0
|
0
|
|
|
|
|
if (type_count > 0 && |
|
|
|
0
|
|
|
|
|
|
|
901
|
0
|
0
|
|
|
|
|
!(type_count == 1 && types[0] == LDNS_RR_TYPE_NS)) { |
|
902
|
0
|
|
|
|
|
|
types[type_count] = LDNS_RR_TYPE_RRSIG; |
|
903
|
0
|
|
|
|
|
|
type_count++; |
|
904
|
|
|
|
|
|
|
} |
|
905
|
|
|
|
|
|
|
|
|
906
|
|
|
|
|
|
|
/* leave next rdata empty if they weren't precomputed yet */ |
|
907
|
0
|
0
|
|
|
|
|
if (to && to->hashed_name) { |
|
|
|
0
|
|
|
|
|
|
|
908
|
0
|
|
|
|
|
|
(void) ldns_rr_set_rdf(nsec_rr, |
|
909
|
0
|
|
|
|
|
|
ldns_rdf_clone(to->hashed_name), |
|
910
|
|
|
|
|
|
|
4); |
|
911
|
|
|
|
|
|
|
} else { |
|
912
|
0
|
|
|
|
|
|
(void) ldns_rr_set_rdf(nsec_rr, NULL, 4); |
|
913
|
|
|
|
|
|
|
} |
|
914
|
|
|
|
|
|
|
|
|
915
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec_rr, |
|
916
|
0
|
|
|
|
|
|
ldns_dnssec_create_nsec_bitmap(types, |
|
917
|
|
|
|
|
|
|
type_count, |
|
918
|
|
|
|
|
|
|
LDNS_RR_TYPE_NSEC3)); |
|
919
|
|
|
|
|
|
|
|
|
920
|
0
|
|
|
|
|
|
return nsec_rr; |
|
921
|
|
|
|
|
|
|
} |
|
922
|
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
ldns_rr * |
|
924
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
ldns_rr *nsec = NULL; |
|
939
|
|
|
|
|
|
|
ldns_rr_type i_type_list[65536]; |
|
940
|
0
|
|
|
|
|
|
size_t type_count = 0; |
|
941
|
|
|
|
|
|
|
|
|
942
|
0
|
|
|
|
|
|
nsec = ldns_rr_new(); |
|
943
|
0
|
|
|
|
|
|
ldns_rr_set_type(nsec, LDNS_RR_TYPE_NSEC); |
|
944
|
0
|
|
|
|
|
|
ldns_rr_set_owner(nsec, ldns_rdf_clone(cur_owner)); |
|
945
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec, ldns_rdf_clone(next_owner)); |
|
946
|
|
|
|
|
|
|
|
|
947
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) { |
|
948
|
0
|
|
|
|
|
|
i_rr = ldns_rr_list_rr(rrs, i); |
|
949
|
0
|
0
|
|
|
|
|
if (ldns_rdf_compare(cur_owner, |
|
950
|
0
|
|
|
|
|
|
ldns_rr_owner(i_rr)) == 0) { |
|
951
|
0
|
|
|
|
|
|
i_type = ldns_rr_get_type(i_rr); |
|
952
|
0
|
0
|
|
|
|
|
if (i_type != LDNS_RR_TYPE_RRSIG && i_type != LDNS_RR_TYPE_NSEC) { |
|
|
|
0
|
|
|
|
|
|
|
953
|
0
|
0
|
|
|
|
|
if (type_count == 0 || i_type_list[type_count-1] != i_type) { |
|
|
|
0
|
|
|
|
|
|
|
954
|
0
|
|
|
|
|
|
i_type_list[type_count] = i_type; |
|
955
|
0
|
|
|
|
|
|
type_count++; |
|
956
|
|
|
|
|
|
|
} |
|
957
|
|
|
|
|
|
|
} |
|
958
|
|
|
|
|
|
|
} |
|
959
|
|
|
|
|
|
|
} |
|
960
|
|
|
|
|
|
|
|
|
961
|
0
|
|
|
|
|
|
i_type_list[type_count] = LDNS_RR_TYPE_RRSIG; |
|
962
|
0
|
|
|
|
|
|
type_count++; |
|
963
|
0
|
|
|
|
|
|
i_type_list[type_count] = LDNS_RR_TYPE_NSEC; |
|
964
|
0
|
|
|
|
|
|
type_count++; |
|
965
|
|
|
|
|
|
|
|
|
966
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec, |
|
967
|
0
|
|
|
|
|
|
ldns_dnssec_create_nsec_bitmap(i_type_list, |
|
968
|
|
|
|
|
|
|
type_count, LDNS_RR_TYPE_NSEC)); |
|
969
|
|
|
|
|
|
|
|
|
970
|
0
|
|
|
|
|
|
return nsec; |
|
971
|
|
|
|
|
|
|
} |
|
972
|
|
|
|
|
|
|
|
|
973
|
|
|
|
|
|
|
ldns_rdf * |
|
974
|
1
|
|
|
|
|
|
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
|
1
|
50
|
|
|
|
|
if (algorithm != LDNS_SHA1) { |
|
994
|
0
|
|
|
|
|
|
return NULL; |
|
995
|
|
|
|
|
|
|
} |
|
996
|
|
|
|
|
|
|
|
|
997
|
|
|
|
|
|
|
/* prepare the owner name according to the draft section bla */ |
|
998
|
1
|
|
|
|
|
|
cann = ldns_rdf_clone(name); |
|
999
|
1
|
50
|
|
|
|
|
if(!cann) { |
|
1000
|
|
|
|
|
|
|
#ifdef STDERR_MSGS |
|
1001
|
|
|
|
|
|
|
fprintf(stderr, "Memory error\n"); |
|
1002
|
|
|
|
|
|
|
#endif |
|
1003
|
0
|
|
|
|
|
|
return NULL; |
|
1004
|
|
|
|
|
|
|
} |
|
1005
|
1
|
|
|
|
|
|
ldns_dname2canonical(cann); |
|
1006
|
|
|
|
|
|
|
|
|
1007
|
1
|
|
|
|
|
|
hashed_owner_str_len = salt_length + ldns_rdf_size(cann); |
|
1008
|
1
|
|
|
|
|
|
hashed_owner_str = LDNS_XMALLOC(unsigned char, hashed_owner_str_len); |
|
1009
|
1
|
50
|
|
|
|
|
if(!hashed_owner_str) { |
|
1010
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(cann); |
|
1011
|
0
|
|
|
|
|
|
return NULL; |
|
1012
|
|
|
|
|
|
|
} |
|
1013
|
1
|
|
|
|
|
|
memcpy(hashed_owner_str, ldns_rdf_data(cann), ldns_rdf_size(cann)); |
|
1014
|
1
|
|
|
|
|
|
memcpy(hashed_owner_str + ldns_rdf_size(cann), salt, salt_length); |
|
1015
|
1
|
|
|
|
|
|
ldns_rdf_deep_free(cann); |
|
1016
|
|
|
|
|
|
|
|
|
1017
|
2
|
100
|
|
|
|
|
for (cur_it = iterations + 1; cur_it > 0; cur_it--) { |
|
1018
|
1
|
|
|
|
|
|
(void) ldns_sha1((unsigned char *) hashed_owner_str, |
|
1019
|
|
|
|
|
|
|
(unsigned int) hashed_owner_str_len, hash); |
|
1020
|
|
|
|
|
|
|
|
|
1021
|
1
|
|
|
|
|
|
LDNS_FREE(hashed_owner_str); |
|
1022
|
1
|
|
|
|
|
|
hashed_owner_str_len = salt_length + LDNS_SHA1_DIGEST_LENGTH; |
|
1023
|
1
|
|
|
|
|
|
hashed_owner_str = LDNS_XMALLOC(unsigned char, hashed_owner_str_len); |
|
1024
|
1
|
50
|
|
|
|
|
if (!hashed_owner_str) { |
|
1025
|
0
|
|
|
|
|
|
return NULL; |
|
1026
|
|
|
|
|
|
|
} |
|
1027
|
1
|
|
|
|
|
|
memcpy(hashed_owner_str, hash, LDNS_SHA1_DIGEST_LENGTH); |
|
1028
|
1
|
|
|
|
|
|
memcpy(hashed_owner_str + LDNS_SHA1_DIGEST_LENGTH, salt, salt_length); |
|
1029
|
1
|
|
|
|
|
|
hashed_owner_str_len = LDNS_SHA1_DIGEST_LENGTH + salt_length; |
|
1030
|
|
|
|
|
|
|
} |
|
1031
|
|
|
|
|
|
|
|
|
1032
|
1
|
|
|
|
|
|
LDNS_FREE(hashed_owner_str); |
|
1033
|
1
|
|
|
|
|
|
hashed_owner_str = hash; |
|
1034
|
1
|
|
|
|
|
|
hashed_owner_str_len = LDNS_SHA1_DIGEST_LENGTH; |
|
1035
|
|
|
|
|
|
|
|
|
1036
|
1
|
|
|
|
|
|
hashed_owner_b32 = LDNS_XMALLOC(char, |
|
1037
|
|
|
|
|
|
|
ldns_b32_ntop_calculate_size(hashed_owner_str_len) + 1); |
|
1038
|
1
|
50
|
|
|
|
|
if(!hashed_owner_b32) { |
|
1039
|
0
|
|
|
|
|
|
return NULL; |
|
1040
|
|
|
|
|
|
|
} |
|
1041
|
1
|
|
|
|
|
|
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
|
1
|
|
|
|
|
|
ldns_b32_ntop_calculate_size(hashed_owner_str_len)+1); |
|
1046
|
1
|
50
|
|
|
|
|
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
|
0
|
|
|
|
|
|
LDNS_FREE(hashed_owner_b32); |
|
1055
|
0
|
|
|
|
|
|
return NULL; |
|
1056
|
|
|
|
|
|
|
} |
|
1057
|
1
|
|
|
|
|
|
hashed_owner_b32[hashed_owner_b32_len] = '\0'; |
|
1058
|
|
|
|
|
|
|
|
|
1059
|
1
|
|
|
|
|
|
status = ldns_str2rdf_dname(&hashed_owner, hashed_owner_b32); |
|
1060
|
1
|
50
|
|
|
|
|
if (status != LDNS_STATUS_OK) { |
|
1061
|
|
|
|
|
|
|
#ifdef STDERR_MSGS |
|
1062
|
|
|
|
|
|
|
fprintf(stderr, "Error creating rdf from %s\n", hashed_owner_b32); |
|
1063
|
|
|
|
|
|
|
#endif |
|
1064
|
0
|
|
|
|
|
|
LDNS_FREE(hashed_owner_b32); |
|
1065
|
0
|
|
|
|
|
|
return NULL; |
|
1066
|
|
|
|
|
|
|
} |
|
1067
|
|
|
|
|
|
|
|
|
1068
|
1
|
|
|
|
|
|
LDNS_FREE(hashed_owner_b32); |
|
1069
|
1
|
|
|
|
|
|
return hashed_owner; |
|
1070
|
|
|
|
|
|
|
} |
|
1071
|
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
void |
|
1073
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
ldns_rdf *salt_rdf = NULL; |
|
1081
|
0
|
|
|
|
|
|
uint8_t *salt_data = NULL; |
|
1082
|
|
|
|
|
|
|
ldns_rdf *old; |
|
1083
|
|
|
|
|
|
|
|
|
1084
|
0
|
|
|
|
|
|
old = ldns_rr_set_rdf(rr, |
|
1085
|
0
|
|
|
|
|
|
ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8, |
|
1086
|
|
|
|
|
|
|
1, (void*)&algorithm), |
|
1087
|
|
|
|
|
|
|
0); |
|
1088
|
0
|
0
|
|
|
|
|
if (old) ldns_rdf_deep_free(old); |
|
1089
|
|
|
|
|
|
|
|
|
1090
|
0
|
|
|
|
|
|
old = ldns_rr_set_rdf(rr, |
|
1091
|
0
|
|
|
|
|
|
ldns_rdf_new_frm_data(LDNS_RDF_TYPE_INT8, |
|
1092
|
|
|
|
|
|
|
1, (void*)&flags), |
|
1093
|
|
|
|
|
|
|
1); |
|
1094
|
0
|
0
|
|
|
|
|
if (old) ldns_rdf_deep_free(old); |
|
1095
|
|
|
|
|
|
|
|
|
1096
|
0
|
|
|
|
|
|
old = ldns_rr_set_rdf(rr, |
|
1097
|
0
|
|
|
|
|
|
ldns_native2rdf_int16(LDNS_RDF_TYPE_INT16, |
|
1098
|
|
|
|
|
|
|
iterations), |
|
1099
|
|
|
|
|
|
|
2); |
|
1100
|
0
|
0
|
|
|
|
|
if (old) ldns_rdf_deep_free(old); |
|
1101
|
|
|
|
|
|
|
|
|
1102
|
0
|
|
|
|
|
|
salt_data = LDNS_XMALLOC(uint8_t, salt_length + 1); |
|
1103
|
0
|
0
|
|
|
|
|
if(!salt_data) { |
|
1104
|
|
|
|
|
|
|
/* no way to return error */ |
|
1105
|
0
|
|
|
|
|
|
return; |
|
1106
|
|
|
|
|
|
|
} |
|
1107
|
0
|
|
|
|
|
|
salt_data[0] = salt_length; |
|
1108
|
0
|
|
|
|
|
|
memcpy(salt_data + 1, salt, salt_length); |
|
1109
|
0
|
|
|
|
|
|
salt_rdf = ldns_rdf_new_frm_data(LDNS_RDF_TYPE_NSEC3_SALT, |
|
1110
|
0
|
|
|
|
|
|
salt_length + 1, |
|
1111
|
|
|
|
|
|
|
salt_data); |
|
1112
|
0
|
0
|
|
|
|
|
if(!salt_rdf) { |
|
1113
|
0
|
|
|
|
|
|
LDNS_FREE(salt_data); |
|
1114
|
|
|
|
|
|
|
/* no way to return error */ |
|
1115
|
0
|
|
|
|
|
|
return; |
|
1116
|
|
|
|
|
|
|
} |
|
1117
|
|
|
|
|
|
|
|
|
1118
|
0
|
|
|
|
|
|
old = ldns_rr_set_rdf(rr, salt_rdf, 3); |
|
1119
|
0
|
0
|
|
|
|
|
if (old) ldns_rdf_deep_free(old); |
|
1120
|
0
|
|
|
|
|
|
LDNS_FREE(salt_data); |
|
1121
|
|
|
|
|
|
|
} |
|
1122
|
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
static int |
|
1124
|
0
|
|
|
|
|
|
rr_list_delegation_only(ldns_rdf *origin, ldns_rr_list *rr_list) |
|
1125
|
|
|
|
|
|
|
{ |
|
1126
|
|
|
|
|
|
|
size_t i; |
|
1127
|
|
|
|
|
|
|
ldns_rr *cur_rr; |
|
1128
|
0
|
0
|
|
|
|
|
if (!origin || !rr_list) return 0; |
|
|
|
0
|
|
|
|
|
|
|
1129
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(rr_list); i++) { |
|
1130
|
0
|
|
|
|
|
|
cur_rr = ldns_rr_list_rr(rr_list, i); |
|
1131
|
0
|
0
|
|
|
|
|
if (ldns_dname_compare(ldns_rr_owner(cur_rr), origin) == 0) { |
|
1132
|
0
|
|
|
|
|
|
return 0; |
|
1133
|
|
|
|
|
|
|
} |
|
1134
|
0
|
0
|
|
|
|
|
if (ldns_rr_get_type(cur_rr) != LDNS_RR_TYPE_NS) { |
|
1135
|
0
|
|
|
|
|
|
return 0; |
|
1136
|
|
|
|
|
|
|
} |
|
1137
|
|
|
|
|
|
|
} |
|
1138
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
ldns_rr *nsec = NULL; |
|
1159
|
0
|
|
|
|
|
|
ldns_rdf *hashed_owner = NULL; |
|
1160
|
|
|
|
|
|
|
|
|
1161
|
|
|
|
|
|
|
ldns_status status; |
|
1162
|
|
|
|
|
|
|
|
|
1163
|
|
|
|
|
|
|
ldns_rr_type i_type_list[1024]; |
|
1164
|
0
|
|
|
|
|
|
size_t type_count = 0; |
|
1165
|
|
|
|
|
|
|
|
|
1166
|
0
|
|
|
|
|
|
hashed_owner = ldns_nsec3_hash_name(cur_owner, |
|
1167
|
|
|
|
|
|
|
algorithm, |
|
1168
|
|
|
|
|
|
|
iterations, |
|
1169
|
|
|
|
|
|
|
salt_length, |
|
1170
|
|
|
|
|
|
|
salt); |
|
1171
|
0
|
|
|
|
|
|
status = ldns_dname_cat(hashed_owner, cur_zone); |
|
1172
|
0
|
0
|
|
|
|
|
if(status != LDNS_STATUS_OK) { |
|
1173
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(hashed_owner); |
|
1174
|
0
|
|
|
|
|
|
return NULL; |
|
1175
|
|
|
|
|
|
|
} |
|
1176
|
0
|
|
|
|
|
|
nsec = ldns_rr_new_frm_type(LDNS_RR_TYPE_NSEC3); |
|
1177
|
0
|
0
|
|
|
|
|
if(!nsec) { |
|
1178
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(hashed_owner); |
|
1179
|
0
|
|
|
|
|
|
return NULL; |
|
1180
|
|
|
|
|
|
|
} |
|
1181
|
0
|
|
|
|
|
|
ldns_rr_set_type(nsec, LDNS_RR_TYPE_NSEC3); |
|
1182
|
0
|
|
|
|
|
|
ldns_rr_set_owner(nsec, hashed_owner); |
|
1183
|
|
|
|
|
|
|
|
|
1184
|
0
|
|
|
|
|
|
ldns_nsec3_add_param_rdfs(nsec, |
|
1185
|
|
|
|
|
|
|
algorithm, |
|
1186
|
|
|
|
|
|
|
flags, |
|
1187
|
|
|
|
|
|
|
iterations, |
|
1188
|
|
|
|
|
|
|
salt_length, |
|
1189
|
|
|
|
|
|
|
salt); |
|
1190
|
0
|
|
|
|
|
|
(void) ldns_rr_set_rdf(nsec, NULL, 4); |
|
1191
|
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
|
|
1193
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(rrs); i++) { |
|
1194
|
0
|
|
|
|
|
|
i_rr = ldns_rr_list_rr(rrs, i); |
|
1195
|
0
|
0
|
|
|
|
|
if (ldns_rdf_compare(cur_owner, |
|
1196
|
0
|
|
|
|
|
|
ldns_rr_owner(i_rr)) == 0) { |
|
1197
|
0
|
|
|
|
|
|
i_type = ldns_rr_get_type(i_rr); |
|
1198
|
0
|
0
|
|
|
|
|
if (type_count == 0 || i_type_list[type_count-1] != i_type) { |
|
|
|
0
|
|
|
|
|
|
|
1199
|
0
|
|
|
|
|
|
i_type_list[type_count] = i_type; |
|
1200
|
0
|
|
|
|
|
|
type_count++; |
|
1201
|
|
|
|
|
|
|
} |
|
1202
|
|
|
|
|
|
|
} |
|
1203
|
|
|
|
|
|
|
} |
|
1204
|
|
|
|
|
|
|
|
|
1205
|
|
|
|
|
|
|
/* add RRSIG anyway, but only if this is not an ENT or |
|
1206
|
|
|
|
|
|
|
* an unsigned delegation */ |
|
1207
|
0
|
0
|
|
|
|
|
if (!emptynonterminal && !rr_list_delegation_only(cur_zone, rrs)) { |
|
|
|
0
|
|
|
|
|
|
|
1208
|
0
|
|
|
|
|
|
i_type_list[type_count] = LDNS_RR_TYPE_RRSIG; |
|
1209
|
0
|
|
|
|
|
|
type_count++; |
|
1210
|
|
|
|
|
|
|
} |
|
1211
|
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
/* and SOA if owner == zone */ |
|
1213
|
0
|
0
|
|
|
|
|
if (ldns_dname_compare(cur_zone, cur_owner) == 0) { |
|
1214
|
0
|
|
|
|
|
|
i_type_list[type_count] = LDNS_RR_TYPE_SOA; |
|
1215
|
0
|
|
|
|
|
|
type_count++; |
|
1216
|
|
|
|
|
|
|
} |
|
1217
|
|
|
|
|
|
|
|
|
1218
|
0
|
|
|
|
|
|
ldns_rr_push_rdf(nsec, |
|
1219
|
0
|
|
|
|
|
|
ldns_dnssec_create_nsec_bitmap(i_type_list, |
|
1220
|
|
|
|
|
|
|
type_count, LDNS_RR_TYPE_NSEC3)); |
|
1221
|
|
|
|
|
|
|
|
|
1222
|
0
|
|
|
|
|
|
return nsec; |
|
1223
|
|
|
|
|
|
|
} |
|
1224
|
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
uint8_t |
|
1226
|
2
|
|
|
|
|
|
ldns_nsec3_algorithm(const ldns_rr *nsec3_rr) |
|
1227
|
|
|
|
|
|
|
{ |
|
1228
|
4
|
|
|
|
|
|
if (nsec3_rr && |
|
1229
|
2
|
0
|
|
|
|
|
(ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 || |
|
1230
|
0
|
|
|
|
|
|
ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM) |
|
1231
|
2
|
50
|
|
|
|
|
&& (ldns_rr_rdf(nsec3_rr, 0) != NULL) |
|
1232
|
2
|
50
|
|
|
|
|
&& ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 0)) > 0) { |
|
1233
|
2
|
|
|
|
|
|
return ldns_rdf2native_int8(ldns_rr_rdf(nsec3_rr, 0)); |
|
1234
|
|
|
|
|
|
|
} |
|
1235
|
0
|
|
|
|
|
|
return 0; |
|
1236
|
|
|
|
|
|
|
} |
|
1237
|
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
uint8_t |
|
1239
|
2
|
|
|
|
|
|
ldns_nsec3_flags(const ldns_rr *nsec3_rr) |
|
1240
|
|
|
|
|
|
|
{ |
|
1241
|
4
|
|
|
|
|
|
if (nsec3_rr && |
|
1242
|
2
|
0
|
|
|
|
|
(ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 || |
|
1243
|
0
|
|
|
|
|
|
ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM) |
|
1244
|
2
|
50
|
|
|
|
|
&& (ldns_rr_rdf(nsec3_rr, 1) != NULL) |
|
1245
|
2
|
50
|
|
|
|
|
&& ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 1)) > 0) { |
|
1246
|
2
|
|
|
|
|
|
return ldns_rdf2native_int8(ldns_rr_rdf(nsec3_rr, 1)); |
|
1247
|
|
|
|
|
|
|
} |
|
1248
|
0
|
|
|
|
|
|
return 0; |
|
1249
|
|
|
|
|
|
|
} |
|
1250
|
|
|
|
|
|
|
|
|
1251
|
|
|
|
|
|
|
bool |
|
1252
|
1
|
|
|
|
|
|
ldns_nsec3_optout(const ldns_rr *nsec3_rr) |
|
1253
|
|
|
|
|
|
|
{ |
|
1254
|
1
|
|
|
|
|
|
return (ldns_nsec3_flags(nsec3_rr) & LDNS_NSEC3_VARS_OPTOUT_MASK); |
|
1255
|
|
|
|
|
|
|
} |
|
1256
|
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
uint16_t |
|
1258
|
2
|
|
|
|
|
|
ldns_nsec3_iterations(const ldns_rr *nsec3_rr) |
|
1259
|
|
|
|
|
|
|
{ |
|
1260
|
4
|
|
|
|
|
|
if (nsec3_rr && |
|
1261
|
2
|
0
|
|
|
|
|
(ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 || |
|
1262
|
0
|
|
|
|
|
|
ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM) |
|
1263
|
2
|
50
|
|
|
|
|
&& (ldns_rr_rdf(nsec3_rr, 2) != NULL) |
|
1264
|
2
|
50
|
|
|
|
|
&& ldns_rdf_size(ldns_rr_rdf(nsec3_rr, 2)) > 0) { |
|
1265
|
2
|
|
|
|
|
|
return ldns_rdf2native_int16(ldns_rr_rdf(nsec3_rr, 2)); |
|
1266
|
|
|
|
|
|
|
} |
|
1267
|
0
|
|
|
|
|
|
return 0; |
|
1268
|
|
|
|
|
|
|
|
|
1269
|
|
|
|
|
|
|
} |
|
1270
|
|
|
|
|
|
|
|
|
1271
|
|
|
|
|
|
|
ldns_rdf * |
|
1272
|
3
|
|
|
|
|
|
ldns_nsec3_salt(const ldns_rr *nsec3_rr) |
|
1273
|
|
|
|
|
|
|
{ |
|
1274
|
6
|
|
|
|
|
|
if (nsec3_rr && |
|
1275
|
3
|
0
|
|
|
|
|
(ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3 || |
|
1276
|
0
|
|
|
|
|
|
ldns_rr_get_type(nsec3_rr) == LDNS_RR_TYPE_NSEC3PARAM) |
|
1277
|
|
|
|
|
|
|
) { |
|
1278
|
3
|
|
|
|
|
|
return ldns_rr_rdf(nsec3_rr, 3); |
|
1279
|
|
|
|
|
|
|
} |
|
1280
|
0
|
|
|
|
|
|
return NULL; |
|
1281
|
|
|
|
|
|
|
} |
|
1282
|
|
|
|
|
|
|
|
|
1283
|
|
|
|
|
|
|
uint8_t |
|
1284
|
2
|
|
|
|
|
|
ldns_nsec3_salt_length(const ldns_rr *nsec3_rr) |
|
1285
|
|
|
|
|
|
|
{ |
|
1286
|
2
|
|
|
|
|
|
ldns_rdf *salt_rdf = ldns_nsec3_salt(nsec3_rr); |
|
1287
|
2
|
50
|
|
|
|
|
if (salt_rdf && ldns_rdf_size(salt_rdf) > 0) { |
|
|
|
50
|
|
|
|
|
|
|
1288
|
2
|
|
|
|
|
|
return (uint8_t) ldns_rdf_data(salt_rdf)[0]; |
|
1289
|
|
|
|
|
|
|
} |
|
1290
|
0
|
|
|
|
|
|
return 0; |
|
1291
|
|
|
|
|
|
|
} |
|
1292
|
|
|
|
|
|
|
|
|
1293
|
|
|
|
|
|
|
/* allocs data, free with LDNS_FREE() */ |
|
1294
|
|
|
|
|
|
|
uint8_t * |
|
1295
|
1
|
|
|
|
|
|
ldns_nsec3_salt_data(const ldns_rr *nsec3_rr) |
|
1296
|
|
|
|
|
|
|
{ |
|
1297
|
|
|
|
|
|
|
uint8_t salt_length; |
|
1298
|
|
|
|
|
|
|
uint8_t *salt; |
|
1299
|
|
|
|
|
|
|
|
|
1300
|
1
|
|
|
|
|
|
ldns_rdf *salt_rdf = ldns_nsec3_salt(nsec3_rr); |
|
1301
|
1
|
50
|
|
|
|
|
if (salt_rdf && ldns_rdf_size(salt_rdf) > 0) { |
|
|
|
50
|
|
|
|
|
|
|
1302
|
1
|
|
|
|
|
|
salt_length = ldns_rdf_data(salt_rdf)[0]; |
|
1303
|
1
|
|
|
|
|
|
salt = LDNS_XMALLOC(uint8_t, salt_length); |
|
1304
|
1
|
50
|
|
|
|
|
if(!salt) return NULL; |
|
1305
|
1
|
|
|
|
|
|
memcpy(salt, &ldns_rdf_data(salt_rdf)[1], salt_length); |
|
1306
|
1
|
|
|
|
|
|
return salt; |
|
1307
|
|
|
|
|
|
|
} |
|
1308
|
0
|
|
|
|
|
|
return NULL; |
|
1309
|
|
|
|
|
|
|
} |
|
1310
|
|
|
|
|
|
|
|
|
1311
|
|
|
|
|
|
|
ldns_rdf * |
|
1312
|
2
|
|
|
|
|
|
ldns_nsec3_next_owner(const ldns_rr *nsec3_rr) |
|
1313
|
|
|
|
|
|
|
{ |
|
1314
|
2
|
50
|
|
|
|
|
if (!nsec3_rr || ldns_rr_get_type(nsec3_rr) != LDNS_RR_TYPE_NSEC3) { |
|
|
|
50
|
|
|
|
|
|
|
1315
|
0
|
|
|
|
|
|
return NULL; |
|
1316
|
|
|
|
|
|
|
} else { |
|
1317
|
2
|
|
|
|
|
|
return ldns_rr_rdf(nsec3_rr, 4); |
|
1318
|
|
|
|
|
|
|
} |
|
1319
|
|
|
|
|
|
|
} |
|
1320
|
|
|
|
|
|
|
|
|
1321
|
|
|
|
|
|
|
ldns_rdf * |
|
1322
|
2
|
|
|
|
|
|
ldns_nsec3_bitmap(const ldns_rr *nsec3_rr) |
|
1323
|
|
|
|
|
|
|
{ |
|
1324
|
2
|
50
|
|
|
|
|
if (!nsec3_rr || ldns_rr_get_type(nsec3_rr) != LDNS_RR_TYPE_NSEC3) { |
|
|
|
50
|
|
|
|
|
|
|
1325
|
0
|
|
|
|
|
|
return NULL; |
|
1326
|
|
|
|
|
|
|
} else { |
|
1327
|
2
|
|
|
|
|
|
return ldns_rr_rdf(nsec3_rr, 5); |
|
1328
|
|
|
|
|
|
|
} |
|
1329
|
|
|
|
|
|
|
} |
|
1330
|
|
|
|
|
|
|
|
|
1331
|
|
|
|
|
|
|
ldns_rdf * |
|
1332
|
1
|
|
|
|
|
|
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
|
1
|
|
|
|
|
|
uint8_t *salt = 0; |
|
1338
|
|
|
|
|
|
|
|
|
1339
|
|
|
|
|
|
|
ldns_rdf *hashed_owner; |
|
1340
|
|
|
|
|
|
|
|
|
1341
|
1
|
|
|
|
|
|
algorithm = ldns_nsec3_algorithm(nsec); |
|
1342
|
1
|
|
|
|
|
|
salt_length = ldns_nsec3_salt_length(nsec); |
|
1343
|
1
|
|
|
|
|
|
salt = ldns_nsec3_salt_data(nsec); |
|
1344
|
1
|
|
|
|
|
|
iterations = ldns_nsec3_iterations(nsec); |
|
1345
|
|
|
|
|
|
|
|
|
1346
|
1
|
|
|
|
|
|
hashed_owner = ldns_nsec3_hash_name(name, |
|
1347
|
|
|
|
|
|
|
algorithm, |
|
1348
|
|
|
|
|
|
|
iterations, |
|
1349
|
|
|
|
|
|
|
salt_length, |
|
1350
|
|
|
|
|
|
|
salt); |
|
1351
|
|
|
|
|
|
|
|
|
1352
|
1
|
|
|
|
|
|
LDNS_FREE(salt); |
|
1353
|
1
|
|
|
|
|
|
return hashed_owner; |
|
1354
|
|
|
|
|
|
|
} |
|
1355
|
|
|
|
|
|
|
|
|
1356
|
|
|
|
|
|
|
bool |
|
1357
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uint8_t window = type >> 8; |
|
1368
|
0
|
|
|
|
|
|
uint8_t subtype = type & 0xff; |
|
1369
|
|
|
|
|
|
|
|
|
1370
|
0
|
0
|
|
|
|
|
if (! bitmap) { |
|
1371
|
0
|
|
|
|
|
|
return false; |
|
1372
|
|
|
|
|
|
|
} |
|
1373
|
|
|
|
|
|
|
assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP); |
|
1374
|
|
|
|
|
|
|
|
|
1375
|
0
|
|
|
|
|
|
dptr = ldns_rdf_data(bitmap); |
|
1376
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
while (dptr < dend && dptr[0] <= window) { |
|
|
|
0
|
|
|
|
|
|
|
1382
|
|
|
|
|
|
|
|
|
1383
|
0
|
0
|
|
|
|
|
if (dptr[0] == window && subtype / 8 < dptr[1] && |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1384
|
0
|
|
|
|
|
|
dptr + dptr[1] + 2 <= dend) { |
|
1385
|
|
|
|
|
|
|
|
|
1386
|
0
|
|
|
|
|
|
return dptr[2 + subtype / 8] & (0x80 >> (subtype % 8)); |
|
1387
|
|
|
|
|
|
|
} |
|
1388
|
0
|
|
|
|
|
|
dptr += dptr[1] + 2; /* next window */ |
|
1389
|
|
|
|
|
|
|
} |
|
1390
|
0
|
|
|
|
|
|
return false; |
|
1391
|
|
|
|
|
|
|
} |
|
1392
|
|
|
|
|
|
|
|
|
1393
|
|
|
|
|
|
|
ldns_status |
|
1394
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uint8_t window = type >> 8; |
|
1405
|
0
|
|
|
|
|
|
uint8_t subtype = type & 0xff; |
|
1406
|
|
|
|
|
|
|
|
|
1407
|
0
|
0
|
|
|
|
|
if (! bitmap) { |
|
1408
|
0
|
|
|
|
|
|
return false; |
|
1409
|
|
|
|
|
|
|
} |
|
1410
|
|
|
|
|
|
|
assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP); |
|
1411
|
|
|
|
|
|
|
|
|
1412
|
0
|
|
|
|
|
|
dptr = ldns_rdf_data(bitmap); |
|
1413
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
while (dptr < dend && dptr[0] <= window) { |
|
|
|
0
|
|
|
|
|
|
|
1419
|
|
|
|
|
|
|
|
|
1420
|
0
|
0
|
|
|
|
|
if (dptr[0] == window && subtype / 8 < dptr[1] && |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1421
|
0
|
|
|
|
|
|
dptr + dptr[1] + 2 <= dend) { |
|
1422
|
|
|
|
|
|
|
|
|
1423
|
0
|
|
|
|
|
|
dptr[2 + subtype / 8] |= (0x80 >> (subtype % 8)); |
|
1424
|
0
|
|
|
|
|
|
return LDNS_STATUS_OK; |
|
1425
|
|
|
|
|
|
|
} |
|
1426
|
0
|
|
|
|
|
|
dptr += dptr[1] + 2; /* next window */ |
|
1427
|
|
|
|
|
|
|
} |
|
1428
|
0
|
|
|
|
|
|
return LDNS_STATUS_TYPE_NOT_IN_BITMAP; |
|
1429
|
|
|
|
|
|
|
} |
|
1430
|
|
|
|
|
|
|
|
|
1431
|
|
|
|
|
|
|
ldns_status |
|
1432
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
uint8_t window = type >> 8; |
|
1443
|
0
|
|
|
|
|
|
uint8_t subtype = type & 0xff; |
|
1444
|
|
|
|
|
|
|
|
|
1445
|
0
|
0
|
|
|
|
|
if (! bitmap) { |
|
1446
|
0
|
|
|
|
|
|
return false; |
|
1447
|
|
|
|
|
|
|
} |
|
1448
|
|
|
|
|
|
|
|
|
1449
|
|
|
|
|
|
|
assert(ldns_rdf_get_type(bitmap) == LDNS_RDF_TYPE_BITMAP); |
|
1450
|
|
|
|
|
|
|
|
|
1451
|
0
|
|
|
|
|
|
dptr = ldns_rdf_data(bitmap); |
|
1452
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
while (dptr < dend && dptr[0] <= window) { |
|
|
|
0
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
|
|
1459
|
0
|
0
|
|
|
|
|
if (dptr[0] == window && subtype / 8 < dptr[1] && |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
1460
|
0
|
|
|
|
|
|
dptr + dptr[1] + 2 <= dend) { |
|
1461
|
|
|
|
|
|
|
|
|
1462
|
0
|
|
|
|
|
|
dptr[2 + subtype / 8] &= ~(0x80 >> (subtype % 8)); |
|
1463
|
0
|
|
|
|
|
|
return LDNS_STATUS_OK; |
|
1464
|
|
|
|
|
|
|
} |
|
1465
|
0
|
|
|
|
|
|
dptr += dptr[1] + 2; /* next window */ |
|
1466
|
|
|
|
|
|
|
} |
|
1467
|
0
|
|
|
|
|
|
return LDNS_STATUS_TYPE_NOT_IN_BITMAP; |
|
1468
|
|
|
|
|
|
|
} |
|
1469
|
|
|
|
|
|
|
|
|
1470
|
|
|
|
|
|
|
|
|
1471
|
|
|
|
|
|
|
bool |
|
1472
|
2
|
|
|
|
|
|
ldns_nsec_covers_name(const ldns_rr *nsec, const ldns_rdf *name) |
|
1473
|
|
|
|
|
|
|
{ |
|
1474
|
2
|
|
|
|
|
|
ldns_rdf *nsec_owner = ldns_rr_owner(nsec); |
|
1475
|
|
|
|
|
|
|
ldns_rdf *hash_next; |
|
1476
|
|
|
|
|
|
|
char *next_hash_str; |
|
1477
|
2
|
|
|
|
|
|
ldns_rdf *nsec_next = NULL; |
|
1478
|
|
|
|
|
|
|
ldns_status status; |
|
1479
|
|
|
|
|
|
|
ldns_rdf *chopped_dname; |
|
1480
|
|
|
|
|
|
|
bool result; |
|
1481
|
|
|
|
|
|
|
|
|
1482
|
2
|
100
|
|
|
|
|
if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC) { |
|
1483
|
1
|
50
|
|
|
|
|
if (ldns_rr_rdf(nsec, 0) != NULL) { |
|
1484
|
1
|
|
|
|
|
|
nsec_next = ldns_rdf_clone(ldns_rr_rdf(nsec, 0)); |
|
1485
|
|
|
|
|
|
|
} else { |
|
1486
|
0
|
|
|
|
|
|
return false; |
|
1487
|
|
|
|
|
|
|
} |
|
1488
|
1
|
50
|
|
|
|
|
} else if (ldns_rr_get_type(nsec) == LDNS_RR_TYPE_NSEC3) { |
|
1489
|
1
|
|
|
|
|
|
hash_next = ldns_nsec3_next_owner(nsec); |
|
1490
|
1
|
|
|
|
|
|
next_hash_str = ldns_rdf2str(hash_next); |
|
1491
|
1
|
|
|
|
|
|
nsec_next = ldns_dname_new_frm_str(next_hash_str); |
|
1492
|
1
|
|
|
|
|
|
LDNS_FREE(next_hash_str); |
|
1493
|
1
|
|
|
|
|
|
chopped_dname = ldns_dname_left_chop(nsec_owner); |
|
1494
|
1
|
|
|
|
|
|
status = ldns_dname_cat(nsec_next, chopped_dname); |
|
1495
|
1
|
|
|
|
|
|
ldns_rdf_deep_free(chopped_dname); |
|
1496
|
1
|
50
|
|
|
|
|
if (status != LDNS_STATUS_OK) { |
|
1497
|
1
|
|
|
|
|
|
printf("error catting: %s\n", ldns_get_errorstr_by_id(status)); |
|
1498
|
|
|
|
|
|
|
} |
|
1499
|
|
|
|
|
|
|
} else { |
|
1500
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(nsec_next); |
|
1501
|
0
|
|
|
|
|
|
return false; |
|
1502
|
|
|
|
|
|
|
} |
|
1503
|
|
|
|
|
|
|
|
|
1504
|
|
|
|
|
|
|
/* in the case of the last nsec */ |
|
1505
|
2
|
50
|
|
|
|
|
if(ldns_dname_compare(nsec_owner, nsec_next) > 0) { |
|
1506
|
0
|
|
|
|
|
|
result = (ldns_dname_compare(nsec_owner, name) <= 0 || |
|
1507
|
0
|
|
|
|
|
|
ldns_dname_compare(name, nsec_next) < 0); |
|
1508
|
2
|
50
|
|
|
|
|
} else if(ldns_dname_compare(nsec_owner, nsec_next) < 0) { |
|
1509
|
4
|
|
|
|
|
|
result = (ldns_dname_compare(nsec_owner, name) <= 0 && |
|
1510
|
2
|
|
|
|
|
|
ldns_dname_compare(name, nsec_next) < 0); |
|
1511
|
|
|
|
|
|
|
} else { |
|
1512
|
0
|
|
|
|
|
|
result = true; |
|
1513
|
|
|
|
|
|
|
} |
|
1514
|
|
|
|
|
|
|
|
|
1515
|
2
|
|
|
|
|
|
ldns_rdf_deep_free(nsec_next); |
|
1516
|
2
|
|
|
|
|
|
return result; |
|
1517
|
|
|
|
|
|
|
} |
|
1518
|
|
|
|
|
|
|
|
|
1519
|
|
|
|
|
|
|
#ifdef HAVE_SSL |
|
1520
|
|
|
|
|
|
|
/* sig may be null - if so look in the packet */ |
|
1521
|
|
|
|
|
|
|
|
|
1522
|
|
|
|
|
|
|
ldns_status |
|
1523
|
0
|
|
|
|
|
|
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
|
0
|
0
|
|
|
|
|
if (!k) { |
|
1534
|
0
|
|
|
|
|
|
return LDNS_STATUS_ERR; |
|
1535
|
|
|
|
|
|
|
/* return LDNS_STATUS_CRYPTO_NO_DNSKEY; */ |
|
1536
|
|
|
|
|
|
|
} |
|
1537
|
|
|
|
|
|
|
|
|
1538
|
0
|
0
|
|
|
|
|
if (t == LDNS_RR_TYPE_RRSIG) { |
|
1539
|
|
|
|
|
|
|
/* we don't have RRSIG(RRSIG) (yet? ;-) ) */ |
|
1540
|
0
|
|
|
|
|
|
return LDNS_STATUS_ERR; |
|
1541
|
|
|
|
|
|
|
} |
|
1542
|
|
|
|
|
|
|
|
|
1543
|
0
|
0
|
|
|
|
|
if (s) { |
|
1544
|
|
|
|
|
|
|
/* if s is not NULL, the sigs are given to use */ |
|
1545
|
0
|
|
|
|
|
|
sigs = s; |
|
1546
|
|
|
|
|
|
|
} else { |
|
1547
|
|
|
|
|
|
|
/* otherwise get them from the packet */ |
|
1548
|
0
|
|
|
|
|
|
sigs = ldns_pkt_rr_list_by_name_and_type(p, o, |
|
1549
|
|
|
|
|
|
|
LDNS_RR_TYPE_RRSIG, |
|
1550
|
|
|
|
|
|
|
LDNS_SECTION_ANY_NOQUESTION); |
|
1551
|
0
|
0
|
|
|
|
|
if (!sigs) { |
|
1552
|
|
|
|
|
|
|
/* no sigs */ |
|
1553
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
rdf_t = ldns_rdf_new(LDNS_RDF_TYPE_TYPE, 2, &t_netorder); |
|
1564
|
|
|
|
|
|
|
|
|
1565
|
0
|
|
|
|
|
|
sigs_covered = ldns_rr_list_subtype_by_rdf(sigs, rdf_t, 0); |
|
1566
|
0
|
|
|
|
|
|
ldns_rdf_free(rdf_t); |
|
1567
|
0
|
0
|
|
|
|
|
if (! sigs_covered) { |
|
1568
|
0
|
0
|
|
|
|
|
if (! s) { |
|
1569
|
0
|
|
|
|
|
|
ldns_rr_list_deep_free(sigs); |
|
1570
|
|
|
|
|
|
|
} |
|
1571
|
0
|
|
|
|
|
|
return LDNS_STATUS_ERR; |
|
1572
|
|
|
|
|
|
|
} |
|
1573
|
0
|
|
|
|
|
|
ldns_rr_list_deep_free(sigs_covered); |
|
1574
|
|
|
|
|
|
|
|
|
1575
|
0
|
|
|
|
|
|
rrset = ldns_pkt_rr_list_by_name_and_type(p, o, t, |
|
1576
|
|
|
|
|
|
|
LDNS_SECTION_ANY_NOQUESTION); |
|
1577
|
0
|
0
|
|
|
|
|
if (!rrset) { |
|
1578
|
0
|
0
|
|
|
|
|
if (! s) { |
|
1579
|
0
|
|
|
|
|
|
ldns_rr_list_deep_free(sigs); |
|
1580
|
|
|
|
|
|
|
} |
|
1581
|
0
|
|
|
|
|
|
return LDNS_STATUS_ERR; |
|
1582
|
|
|
|
|
|
|
} |
|
1583
|
0
|
|
|
|
|
|
return ldns_verify_time(rrset, sigs, k, check_time, good_keys); |
|
1584
|
|
|
|
|
|
|
} |
|
1585
|
|
|
|
|
|
|
|
|
1586
|
|
|
|
|
|
|
ldns_status |
|
1587
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
ldns_status status = LDNS_STATUS_OK; |
|
1602
|
|
|
|
|
|
|
|
|
1603
|
0
|
0
|
|
|
|
|
for (i = 0; i < ldns_rr_list_rr_count(nsec3_rrs); i++) { |
|
1604
|
0
|
0
|
|
|
|
|
if (i == ldns_rr_list_rr_count(nsec3_rrs) - 1) { |
|
1605
|
0
|
|
|
|
|
|
next_nsec_owner_label = |
|
1606
|
0
|
|
|
|
|
|
ldns_dname_label(ldns_rr_owner(ldns_rr_list_rr(nsec3_rrs, |
|
1607
|
|
|
|
|
|
|
0)), 0); |
|
1608
|
0
|
|
|
|
|
|
next_nsec_owner_str = ldns_rdf2str(next_nsec_owner_label); |
|
1609
|
0
|
0
|
|
|
|
|
if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1] |
|
1610
|
0
|
|
|
|
|
|
== '.') { |
|
1611
|
0
|
|
|
|
|
|
next_nsec_owner_str[strlen(next_nsec_owner_str) - 1] |
|
1612
|
0
|
|
|
|
|
|
= '\0'; |
|
1613
|
|
|
|
|
|
|
} |
|
1614
|
0
|
|
|
|
|
|
status = ldns_str2rdf_b32_ext(&next_nsec_rdf, |
|
1615
|
|
|
|
|
|
|
next_nsec_owner_str); |
|
1616
|
0
|
|
|
|
|
|
if (!ldns_rr_set_rdf(ldns_rr_list_rr(nsec3_rrs, i), |
|
1617
|
|
|
|
|
|
|
next_nsec_rdf, 4)) { |
|
1618
|
|
|
|
|
|
|
/* todo: error */ |
|
1619
|
|
|
|
|
|
|
} |
|
1620
|
|
|
|
|
|
|
|
|
1621
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(next_nsec_owner_label); |
|
1622
|
0
|
|
|
|
|
|
LDNS_FREE(next_nsec_owner_str); |
|
1623
|
|
|
|
|
|
|
} else { |
|
1624
|
0
|
|
|
|
|
|
next_nsec_owner_label = |
|
1625
|
0
|
|
|
|
|
|
ldns_dname_label(ldns_rr_owner(ldns_rr_list_rr(nsec3_rrs, |
|
1626
|
|
|
|
|
|
|
i + 1)), |
|
1627
|
|
|
|
|
|
|
0); |
|
1628
|
0
|
|
|
|
|
|
next_nsec_owner_str = ldns_rdf2str(next_nsec_owner_label); |
|
1629
|
0
|
0
|
|
|
|
|
if (next_nsec_owner_str[strlen(next_nsec_owner_str) - 1] |
|
1630
|
0
|
|
|
|
|
|
== '.') { |
|
1631
|
0
|
|
|
|
|
|
next_nsec_owner_str[strlen(next_nsec_owner_str) - 1] |
|
1632
|
0
|
|
|
|
|
|
= '\0'; |
|
1633
|
|
|
|
|
|
|
} |
|
1634
|
0
|
|
|
|
|
|
status = ldns_str2rdf_b32_ext(&next_nsec_rdf, |
|
1635
|
|
|
|
|
|
|
next_nsec_owner_str); |
|
1636
|
0
|
|
|
|
|
|
ldns_rdf_deep_free(next_nsec_owner_label); |
|
1637
|
0
|
|
|
|
|
|
LDNS_FREE(next_nsec_owner_str); |
|
1638
|
0
|
|
|
|
|
|
if (!ldns_rr_set_rdf(ldns_rr_list_rr(nsec3_rrs, i), |
|
1639
|
|
|
|
|
|
|
next_nsec_rdf, 4)) { |
|
1640
|
|
|
|
|
|
|
/* todo: error */ |
|
1641
|
|
|
|
|
|
|
} |
|
1642
|
|
|
|
|
|
|
} |
|
1643
|
|
|
|
|
|
|
} |
|
1644
|
0
|
|
|
|
|
|
return status; |
|
1645
|
|
|
|
|
|
|
} |
|
1646
|
|
|
|
|
|
|
|
|
1647
|
|
|
|
|
|
|
int |
|
1648
|
0
|
|
|
|
|
|
qsort_rr_compare_nsec3(const void *a, const void *b) |
|
1649
|
|
|
|
|
|
|
{ |
|
1650
|
0
|
|
|
|
|
|
const ldns_rr *rr1 = * (const ldns_rr **) a; |
|
1651
|
0
|
|
|
|
|
|
const ldns_rr *rr2 = * (const ldns_rr **) b; |
|
1652
|
0
|
0
|
|
|
|
|
if (rr1 == NULL && rr2 == NULL) { |
|
|
|
0
|
|
|
|
|
|
|
1653
|
0
|
|
|
|
|
|
return 0; |
|
1654
|
|
|
|
|
|
|
} |
|
1655
|
0
|
0
|
|
|
|
|
if (rr1 == NULL) { |
|
1656
|
0
|
|
|
|
|
|
return -1; |
|
1657
|
|
|
|
|
|
|
} |
|
1658
|
0
|
0
|
|
|
|
|
if (rr2 == NULL) { |
|
1659
|
0
|
|
|
|
|
|
return 1; |
|
1660
|
|
|
|
|
|
|
} |
|
1661
|
0
|
|
|
|
|
|
return ldns_rdf_compare(ldns_rr_owner(rr1), ldns_rr_owner(rr2)); |
|
1662
|
|
|
|
|
|
|
} |
|
1663
|
|
|
|
|
|
|
|
|
1664
|
|
|
|
|
|
|
void |
|
1665
|
0
|
|
|
|
|
|
ldns_rr_list_sort_nsec3(ldns_rr_list *unsorted) |
|
1666
|
|
|
|
|
|
|
{ |
|
1667
|
0
|
|
|
|
|
|
qsort(unsorted->_rrs, |
|
1668
|
|
|
|
|
|
|
ldns_rr_list_rr_count(unsorted), |
|
1669
|
|
|
|
|
|
|
sizeof(ldns_rr *), |
|
1670
|
|
|
|
|
|
|
qsort_rr_compare_nsec3); |
|
1671
|
0
|
|
|
|
|
|
} |
|
1672
|
|
|
|
|
|
|
|
|
1673
|
|
|
|
|
|
|
int |
|
1674
|
0
|
|
|
|
|
|
ldns_dnssec_default_add_to_signatures( ATTR_UNUSED(ldns_rr *sig) |
|
1675
|
|
|
|
|
|
|
, ATTR_UNUSED(void *n) |
|
1676
|
|
|
|
|
|
|
) |
|
1677
|
|
|
|
|
|
|
{ |
|
1678
|
0
|
|
|
|
|
|
return LDNS_SIGNATURE_LEAVE_ADD_NEW; |
|
1679
|
|
|
|
|
|
|
} |
|
1680
|
|
|
|
|
|
|
|
|
1681
|
|
|
|
|
|
|
int |
|
1682
|
0
|
|
|
|
|
|
ldns_dnssec_default_leave_signatures( ATTR_UNUSED(ldns_rr *sig) |
|
1683
|
|
|
|
|
|
|
, ATTR_UNUSED(void *n) |
|
1684
|
|
|
|
|
|
|
) |
|
1685
|
|
|
|
|
|
|
{ |
|
1686
|
0
|
|
|
|
|
|
return LDNS_SIGNATURE_LEAVE_NO_ADD; |
|
1687
|
|
|
|
|
|
|
} |
|
1688
|
|
|
|
|
|
|
|
|
1689
|
|
|
|
|
|
|
int |
|
1690
|
0
|
|
|
|
|
|
ldns_dnssec_default_delete_signatures( ATTR_UNUSED(ldns_rr *sig) |
|
1691
|
|
|
|
|
|
|
, ATTR_UNUSED(void *n) |
|
1692
|
|
|
|
|
|
|
) |
|
1693
|
|
|
|
|
|
|
{ |
|
1694
|
0
|
|
|
|
|
|
return LDNS_SIGNATURE_REMOVE_NO_ADD; |
|
1695
|
|
|
|
|
|
|
} |
|
1696
|
|
|
|
|
|
|
|
|
1697
|
|
|
|
|
|
|
int |
|
1698
|
0
|
|
|
|
|
|
ldns_dnssec_default_replace_signatures( ATTR_UNUSED(ldns_rr *sig) |
|
1699
|
|
|
|
|
|
|
, ATTR_UNUSED(void *n) |
|
1700
|
|
|
|
|
|
|
) |
|
1701
|
|
|
|
|
|
|
{ |
|
1702
|
0
|
|
|
|
|
|
return LDNS_SIGNATURE_REMOVE_ADD_NEW; |
|
1703
|
|
|
|
|
|
|
} |
|
1704
|
|
|
|
|
|
|
|
|
1705
|
|
|
|
|
|
|
#ifdef HAVE_SSL |
|
1706
|
|
|
|
|
|
|
ldns_rdf * |
|
1707
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
unsigned char *dsasig_data = (unsigned char*)ldns_buffer_begin(sig); |
|
1713
|
|
|
|
|
|
|
size_t byte_offset; |
|
1714
|
|
|
|
|
|
|
|
|
1715
|
0
|
|
|
|
|
|
dsasig = d2i_DSA_SIG(NULL, |
|
1716
|
|
|
|
|
|
|
(const unsigned char **)&dsasig_data, |
|
1717
|
|
|
|
|
|
|
sig_len); |
|
1718
|
0
|
0
|
|
|
|
|
if (!dsasig) { |
|
1719
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1720
|
0
|
|
|
|
|
|
return NULL; |
|
1721
|
|
|
|
|
|
|
} |
|
1722
|
|
|
|
|
|
|
|
|
1723
|
0
|
|
|
|
|
|
dsasig_data = LDNS_XMALLOC(unsigned char, 41); |
|
1724
|
0
|
0
|
|
|
|
|
if(!dsasig_data) { |
|
1725
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1726
|
0
|
|
|
|
|
|
return NULL; |
|
1727
|
|
|
|
|
|
|
} |
|
1728
|
0
|
|
|
|
|
|
dsasig_data[0] = 0; |
|
1729
|
0
|
|
|
|
|
|
byte_offset = (size_t) (20 - BN_num_bytes(dsasig->r)); |
|
1730
|
0
|
0
|
|
|
|
|
if (byte_offset > 20) { |
|
1731
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1732
|
0
|
|
|
|
|
|
LDNS_FREE(dsasig_data); |
|
1733
|
0
|
|
|
|
|
|
return NULL; |
|
1734
|
|
|
|
|
|
|
} |
|
1735
|
0
|
|
|
|
|
|
memset(&dsasig_data[1], 0, byte_offset); |
|
1736
|
0
|
|
|
|
|
|
BN_bn2bin(dsasig->r, &dsasig_data[1 + byte_offset]); |
|
1737
|
0
|
|
|
|
|
|
byte_offset = (size_t) (20 - BN_num_bytes(dsasig->s)); |
|
1738
|
0
|
0
|
|
|
|
|
if (byte_offset > 20) { |
|
1739
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1740
|
0
|
|
|
|
|
|
LDNS_FREE(dsasig_data); |
|
1741
|
0
|
|
|
|
|
|
return NULL; |
|
1742
|
|
|
|
|
|
|
} |
|
1743
|
0
|
|
|
|
|
|
memset(&dsasig_data[21], 0, byte_offset); |
|
1744
|
0
|
|
|
|
|
|
BN_bn2bin(dsasig->s, &dsasig_data[21 + byte_offset]); |
|
1745
|
|
|
|
|
|
|
|
|
1746
|
0
|
|
|
|
|
|
sigdata_rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, 41, dsasig_data); |
|
1747
|
0
|
0
|
|
|
|
|
if(!sigdata_rdf) { |
|
1748
|
0
|
|
|
|
|
|
LDNS_FREE(dsasig_data); |
|
1749
|
|
|
|
|
|
|
} |
|
1750
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1751
|
|
|
|
|
|
|
|
|
1752
|
0
|
|
|
|
|
|
return sigdata_rdf; |
|
1753
|
|
|
|
|
|
|
} |
|
1754
|
|
|
|
|
|
|
|
|
1755
|
|
|
|
|
|
|
ldns_status |
|
1756
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
unsigned char *raw_sig = NULL; |
|
1763
|
|
|
|
|
|
|
int raw_sig_len; |
|
1764
|
|
|
|
|
|
|
|
|
1765
|
0
|
0
|
|
|
|
|
if(ldns_rdf_size(sig_rdf) < 1 + 2*SHA_DIGEST_LENGTH) |
|
1766
|
0
|
|
|
|
|
|
return LDNS_STATUS_SYNTAX_RDATA_ERR; |
|
1767
|
|
|
|
|
|
|
/* extract the R and S field from the sig buffer */ |
|
1768
|
0
|
|
|
|
|
|
R = BN_new(); |
|
1769
|
0
|
0
|
|
|
|
|
if(!R) return LDNS_STATUS_MEM_ERR; |
|
1770
|
0
|
|
|
|
|
|
(void) BN_bin2bn((unsigned char *) ldns_rdf_data(sig_rdf) + 1, |
|
1771
|
|
|
|
|
|
|
SHA_DIGEST_LENGTH, R); |
|
1772
|
0
|
|
|
|
|
|
S = BN_new(); |
|
1773
|
0
|
0
|
|
|
|
|
if(!S) { |
|
1774
|
0
|
|
|
|
|
|
BN_free(R); |
|
1775
|
0
|
|
|
|
|
|
return LDNS_STATUS_MEM_ERR; |
|
1776
|
|
|
|
|
|
|
} |
|
1777
|
0
|
|
|
|
|
|
(void) BN_bin2bn((unsigned char *) ldns_rdf_data(sig_rdf) + 21, |
|
1778
|
|
|
|
|
|
|
SHA_DIGEST_LENGTH, S); |
|
1779
|
|
|
|
|
|
|
|
|
1780
|
0
|
|
|
|
|
|
dsasig = DSA_SIG_new(); |
|
1781
|
0
|
0
|
|
|
|
|
if (!dsasig) { |
|
1782
|
0
|
|
|
|
|
|
BN_free(R); |
|
1783
|
0
|
|
|
|
|
|
BN_free(S); |
|
1784
|
0
|
|
|
|
|
|
return LDNS_STATUS_MEM_ERR; |
|
1785
|
|
|
|
|
|
|
} |
|
1786
|
|
|
|
|
|
|
|
|
1787
|
0
|
|
|
|
|
|
dsasig->r = R; |
|
1788
|
0
|
|
|
|
|
|
dsasig->s = S; |
|
1789
|
|
|
|
|
|
|
|
|
1790
|
0
|
|
|
|
|
|
raw_sig_len = i2d_DSA_SIG(dsasig, &raw_sig); |
|
1791
|
0
|
0
|
|
|
|
|
if (raw_sig_len < 0) { |
|
1792
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1793
|
0
|
|
|
|
|
|
free(raw_sig); |
|
1794
|
0
|
|
|
|
|
|
return LDNS_STATUS_SSL_ERR; |
|
1795
|
|
|
|
|
|
|
} |
|
1796
|
0
|
0
|
|
|
|
|
if (ldns_buffer_reserve(target_buffer, (size_t) raw_sig_len)) { |
|
1797
|
0
|
|
|
|
|
|
ldns_buffer_write(target_buffer, raw_sig, (size_t)raw_sig_len); |
|
1798
|
|
|
|
|
|
|
} |
|
1799
|
|
|
|
|
|
|
|
|
1800
|
0
|
|
|
|
|
|
DSA_SIG_free(dsasig); |
|
1801
|
0
|
|
|
|
|
|
free(raw_sig); |
|
1802
|
|
|
|
|
|
|
|
|
1803
|
0
|
|
|
|
|
|
return ldns_buffer_status(target_buffer); |
|
1804
|
|
|
|
|
|
|
} |
|
1805
|
|
|
|
|
|
|
|
|
1806
|
|
|
|
|
|
|
#ifdef USE_ECDSA |
|
1807
|
|
|
|
|
|
|
#ifndef S_SPLINT_S |
|
1808
|
|
|
|
|
|
|
ldns_rdf * |
|
1809
|
0
|
|
|
|
|
|
ldns_convert_ecdsa_rrsig_asn12rdf(const ldns_buffer *sig, const long sig_len) |
|
1810
|
|
|
|
|
|
|
{ |
|
1811
|
|
|
|
|
|
|
ECDSA_SIG* ecdsa_sig; |
|
1812
|
0
|
|
|
|
|
|
unsigned char *data = (unsigned char*)ldns_buffer_begin(sig); |
|
1813
|
|
|
|
|
|
|
ldns_rdf* rdf; |
|
1814
|
0
|
|
|
|
|
|
ecdsa_sig = d2i_ECDSA_SIG(NULL, (const unsigned char **)&data, sig_len); |
|
1815
|
0
|
0
|
|
|
|
|
if(!ecdsa_sig) return NULL; |
|
1816
|
|
|
|
|
|
|
|
|
1817
|
|
|
|
|
|
|
/* "r | s". */ |
|
1818
|
0
|
|
|
|
|
|
data = LDNS_XMALLOC(unsigned char, |
|
1819
|
|
|
|
|
|
|
BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s)); |
|
1820
|
0
|
0
|
|
|
|
|
if(!data) { |
|
1821
|
0
|
|
|
|
|
|
ECDSA_SIG_free(ecdsa_sig); |
|
1822
|
0
|
|
|
|
|
|
return NULL; |
|
1823
|
|
|
|
|
|
|
} |
|
1824
|
0
|
|
|
|
|
|
BN_bn2bin(ecdsa_sig->r, data); |
|
1825
|
0
|
|
|
|
|
|
BN_bn2bin(ecdsa_sig->s, data+BN_num_bytes(ecdsa_sig->r)); |
|
1826
|
0
|
|
|
|
|
|
rdf = ldns_rdf_new(LDNS_RDF_TYPE_B64, (size_t)( |
|
1827
|
0
|
|
|
|
|
|
BN_num_bytes(ecdsa_sig->r) + BN_num_bytes(ecdsa_sig->s)), data); |
|
1828
|
0
|
|
|
|
|
|
ECDSA_SIG_free(ecdsa_sig); |
|
1829
|
0
|
|
|
|
|
|
return rdf; |
|
1830
|
|
|
|
|
|
|
} |
|
1831
|
|
|
|
|
|
|
|
|
1832
|
|
|
|
|
|
|
ldns_status |
|
1833
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
|
long bnsize = (long)ldns_rdf_size(sig_rdf) / 2; |
|
1839
|
|
|
|
|
|
|
/* if too short, or not even length, do not bother */ |
|
1840
|
0
|
0
|
|
|
|
|
if(bnsize < 16 || (size_t)bnsize*2 != ldns_rdf_size(sig_rdf)) |
|
|
|
0
|
|
|
|
|
|
|
1841
|
0
|
|
|
|
|
|
return LDNS_STATUS_ERR; |
|
1842
|
|
|
|
|
|
|
|
|
1843
|
|
|
|
|
|
|
/* use the raw data to parse two evenly long BIGNUMs, "r | s". */ |
|
1844
|
0
|
|
|
|
|
|
sig = ECDSA_SIG_new(); |
|
1845
|
0
|
0
|
|
|
|
|
if(!sig) return LDNS_STATUS_MEM_ERR; |
|
1846
|
0
|
|
|
|
|
|
sig->r = BN_bin2bn((const unsigned char*)ldns_rdf_data(sig_rdf), |
|
1847
|
|
|
|
|
|
|
bnsize, sig->r); |
|
1848
|
0
|
|
|
|
|
|
sig->s = BN_bin2bn((const unsigned char*)ldns_rdf_data(sig_rdf)+bnsize, |
|
1849
|
|
|
|
|
|
|
bnsize, sig->s); |
|
1850
|
0
|
0
|
|
|
|
|
if(!sig->r || !sig->s) { |
|
|
|
0
|
|
|
|
|
|
|
1851
|
0
|
|
|
|
|
|
ECDSA_SIG_free(sig); |
|
1852
|
0
|
|
|
|
|
|
return LDNS_STATUS_MEM_ERR; |
|
1853
|
|
|
|
|
|
|
} |
|
1854
|
|
|
|
|
|
|
|
|
1855
|
0
|
|
|
|
|
|
raw_sig_len = i2d_ECDSA_SIG(sig, NULL); |
|
1856
|
0
|
0
|
|
|
|
|
if (ldns_buffer_reserve(target_buffer, (size_t) raw_sig_len)) { |
|
1857
|
0
|
|
|
|
|
|
unsigned char* pp = (unsigned char*) |
|
1858
|
0
|
|
|
|
|
|
ldns_buffer_current(target_buffer); |
|
1859
|
0
|
|
|
|
|
|
raw_sig_len = i2d_ECDSA_SIG(sig, &pp); |
|
1860
|
0
|
|
|
|
|
|
ldns_buffer_skip(target_buffer, (ssize_t) raw_sig_len); |
|
1861
|
|
|
|
|
|
|
} |
|
1862
|
0
|
|
|
|
|
|
ECDSA_SIG_free(sig); |
|
1863
|
|
|
|
|
|
|
|
|
1864
|
0
|
|
|
|
|
|
return ldns_buffer_status(target_buffer); |
|
1865
|
|
|
|
|
|
|
} |
|
1866
|
|
|
|
|
|
|
|
|
1867
|
|
|
|
|
|
|
#endif /* S_SPLINT_S */ |
|
1868
|
|
|
|
|
|
|
#endif /* USE_ECDSA */ |
|
1869
|
|
|
|
|
|
|
#endif /* HAVE_SSL */ |