-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbns-prove
executable file
·180 lines (138 loc) · 3.21 KB
/
bns-prove
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
#!/usr/bin/env node
'use strict';
process.title = 'bns-prove';
process.on('uncaughtException', (err) => {
console.error(err.message);
process.exit(1);
});
const pkg = require('../package.json');
const constants = require('../lib/constants');
const dnssec = require('../lib/dnssec');
const Resolver = require('../lib/resolver/stub');
const Ownership = require('../lib/ownership');
const util = require('../lib/util');
const wire = require('../lib/wire');
const {
keyFlags,
classes,
types
} = constants;
const {
Record,
TXTRecord
} = wire;
let hex = false;
let base64 = false;
let secure = false;
let hardened = false;
let lifespan = 365 * 24 * 60 * 60;
let dir = '.';
let name = null;
let txt = null;
for (let i = 2; i < process.argv.length; i++) {
const arg = process.argv[i];
const next = i !== process.argv.length - 1
? process.argv[i + 1]
: '';
switch (arg) {
case '-x': {
hex = true;
break;
}
case '-b': {
base64 = true;
break;
}
case '-s': {
secure = true;
break;
}
case '-r': {
hardened = true;
break;
}
case '-t': {
lifespan = util.parseU32(lifespan);
i += 1;
break;
}
case '-K': {
if (!next)
throw new Error('Invalid directory.');
dir = next;
i += 1;
break;
}
case '-h':
case '--help':
case '-?':
case '-v': {
console.log(`bns-prove ${pkg.version}`);
process.exit(0);
break;
}
default: {
if (!name) {
if (!util.isName(arg))
throw new Error('Invalid name.');
name = util.fqdn(arg.toLowerCase());
} else if (!txt) {
txt = arg;
}
break;
}
}
}
if (!name || name === '.')
throw new Error('No name provided.');
if (!txt)
throw new Error('No text provided.');
(async () => {
const ctx = new Ownership();
ctx.Resolver = Resolver;
ctx.secure = secure;
const proof = await ctx.prove(name, true);
if (hardened && ctx.isWeak(proof))
throw new Error('Cannot use RSA-1024 with hardening enabled.');
const zone = proof.zones[proof.zones.length - 1];
zone.claim.length = 0;
for (const key of zone.keys) {
if (key.type !== types.DNSKEY)
continue;
const kd = key.data;
if (!(kd.flags & keyFlags.ZONE))
continue;
if (kd.flags & keyFlags.SEP)
continue;
if (kd.flags & keyFlags.REVOKE)
continue;
if (!ctx.verifyKey(key, hardened))
continue;
const priv = await dnssec.readPrivateAsync(dir, key);
if (!priv)
continue;
const rr = new Record();
const rd = new TXTRecord();
rr.name = name;
rr.type = types.TXT;
rr.class = classes.IN;
rr.ttl = 3600;
rr.data = rd;
rd.txt.push(txt);
const sig = dnssec.sign(key, priv, [rr], lifespan);
zone.claim.push(rr);
zone.claim.push(sig);
break;
}
if (zone.claim.length === 0)
throw new Error('Could not find suitable key to sign with.');
if (hex)
process.stdout.write(proof.toHex() + '\n');
else if (base64)
process.stdout.write(proof.toBase64() + '\n');
else
process.stdout.write(proof.toString() + '\n');
})().catch((err) => {
console.error(err.message);
process.exit(1);
});