1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/* ! \file */
#include <config.h>
#include <atf-c.h>
#include <stdbool.h>
#include <unistd.h>
#include <isc/file.h>
#include <isc/util.h>
#include <isc/stdio.h>
#include <isc/string.h>
#include <dst/dst.h>
#include <dst/result.h>
#include "../dst_internal.h"
#include "dnstest.h"
ATF_TC(sig);
ATF_TC_HEAD(sig, tc) {
atf_tc_set_md_var(tc, "descr", "signature ineffability");
}
/*
* Read sig in file at path to buf.
*/
static isc_result_t
sig_fromfile(const char *path, isc_buffer_t *buf) {
isc_result_t result;
size_t rval, len;
FILE *fp = NULL;
unsigned char val;
char *p, *data;
off_t size;
result = isc_stdio_open(path, "rb", &fp);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = isc_file_getsizefd(fileno(fp), &size);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
data = isc_mem_get(mctx, (size + 1));
ATF_REQUIRE(data != NULL);
len = (size_t)size;
p = data;
while (len != 0U) {
result = isc_stdio_read(p, 1, len, fp, &rval);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
len -= rval;
p += rval;
}
isc_stdio_close(fp);
p = data;
len = size;
while (len > 0U) {
if ((*p == '\r') || (*p == '\n')) {
++p;
--len;
continue;
} else if (len < 2U)
goto err;
if (('0' <= *p) && (*p <= '9')) {
val = *p - '0';
} else if (('A' <= *p) && (*p <= 'F')) {
val = *p - 'A' + 10;
} else {
result = ISC_R_BADHEX;
goto err;
}
++p;
val <<= 4;
--len;
if (('0' <= *p) && (*p <= '9')) {
val |= (*p - '0');
} else if (('A' <= *p) && (*p <= 'F')) {
val |= (*p - 'A' + 10);
} else {
result = ISC_R_BADHEX;
goto err;
}
++p;
--len;
isc_buffer_putuint8(buf, val);
}
result = ISC_R_SUCCESS;
err:
isc_mem_put(mctx, data, size + 1);
return (result);
}
static void
check_sig(const char *datapath, const char *sigpath, const char *keyname,
dns_keytag_t id, dns_secalg_t alg, int type, bool expect)
{
isc_result_t result;
size_t rval, len;
FILE *fp;
dst_key_t *key = NULL;
unsigned char sig[512];
unsigned char *p;
unsigned char *data;
off_t size;
isc_buffer_t b;
isc_buffer_t databuf, sigbuf;
isc_region_t datareg, sigreg;
dns_fixedname_t fname;
dns_name_t *name;
dst_context_t *ctx = NULL;
/*
* Read data from file in a form usable by dst_verify.
*/
result = isc_stdio_open(datapath, "rb", &fp);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = isc_file_getsizefd(fileno(fp), &size);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
data = isc_mem_get(mctx, (size + 1));
ATF_REQUIRE(data != NULL);
p = data;
len = (size_t)size;
do {
result = isc_stdio_read(p, 1, len, fp, &rval);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
len -= rval;
p += rval;
} while (len);
isc_stdio_close(fp);
/*
* Read key from file in a form usable by dst_verify.
*/
name = dns_fixedname_initname(&fname);
isc_buffer_constinit(&b, keyname, strlen(keyname));
isc_buffer_add(&b, strlen(keyname));
result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dst_key_fromfile(name, id, alg, type, "testdata/dst",
mctx, &key);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
isc_buffer_init(&databuf, data, (unsigned int)size);
isc_buffer_add(&databuf, (unsigned int)size);
isc_buffer_usedregion(&databuf, &datareg);
memset(sig, 0, sizeof(sig));
isc_buffer_init(&sigbuf, sig, sizeof(sig));
/*
* Read precomputed signature from file in a form usable by dst_verify.
*/
result = sig_fromfile(sigpath, &sigbuf);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
/*
* Verify that the key signed the data.
*/
isc_buffer_remainingregion(&sigbuf, &sigreg);
result = dst_context_create3(key, mctx, DNS_LOGCATEGORY_GENERAL,
false, &ctx);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dst_context_adddata(ctx, &datareg);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
result = dst_context_verify(ctx, &sigreg);
ATF_REQUIRE((expect && (result == ISC_R_SUCCESS)) ||
(!expect && (result != ISC_R_SUCCESS)));
isc_mem_put(mctx, data, size + 1);
dst_context_destroy(&ctx);
dst_key_free(&key);
return;
}
ATF_TC_BODY(sig, tc) {
isc_result_t result;
UNUSED(tc);
result = dns_test_begin(NULL, false);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
struct {
const char *datapath;
const char *sigpath;
const char *keyname;
dns_keytag_t keyid;
dns_secalg_t alg;
bool expect;
} testcases[] = {
{
"testdata/dst/test1.data",
"testdata/dst/test1.dsasig",
"test.", 23616, DST_ALG_DSA, true
},
{
"testdata/dst/test1.data",
"testdata/dst/test1.rsasig",
"test.", 54622, DST_ALG_RSAMD5, true
},
{
/* wrong sig */
"testdata/dst/test1.data",
"testdata/dst/test1.dsasig",
"test.", 54622, DST_ALG_RSAMD5, false
},
{
/* wrong data */
"testdata/dst/test2.data",
"testdata/dst/test1.dsasig",
"test.", 23616, DST_ALG_DSA, false
},
};
unsigned int i;
for (i = 0; i < (sizeof(testcases)/sizeof(testcases[0])); i++) {
if (!dst_algorithm_supported(testcases[i].alg)) {
continue;
}
check_sig(testcases[i].datapath,
testcases[i].sigpath,
testcases[i].keyname,
testcases[i].keyid,
testcases[i].alg,
DST_TYPE_PRIVATE|DST_TYPE_PUBLIC,
testcases[i].expect);
}
dns_test_end();
}
/*
* Main
*/
ATF_TP_ADD_TCS(tp) {
ATF_TP_ADD_TC(tp, sig);
return (atf_no_error());
}
|