-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate.js
179 lines (145 loc) · 4.32 KB
/
certificate.js
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
'use strict';
const nacl = require('tweetnacl');
const {
decode,
types: { uint32be, uint16be, buffer },
} = require('binary-data');
const debug = require('debug')('dnscrypt');
module.exports = {
validate,
parse,
validateCertificate,
};
const CERT_MAGIC = 0x444e5343;
const EncodedCertificate = {
certMagic: uint32be,
esVersion: uint16be,
minProtocol: uint16be,
signature: buffer(64),
resolverPk: buffer(32),
clientMagic: buffer(8),
serial: uint32be,
ts: {
start: uint32be,
end: uint32be,
},
};
/**
* @typedef {Object} Certificate
* @property {number} certMagic Certificate magic number <0x44 0x4e 0x53 0x43>.
* @property {number} esVersion A cryptographic construction to use with the certificate.
* @property {number} minProtocol Minimal supported version of DNSCrypt protocol.
* @property {Buffer} signature 64-byte signature using the Ed25519 algorithm and the provider secret key.
* @property {Buffer} resolverPk The resolver short-term public key.
* @property {Buffer} clientMagic The first 8 bytes of a client query.
* @property {number} serial 4 byte serial number.
* @property {Object} ts
* @property {number} ts.start The date the certificate is valid from.
* @property {number} ts.end The date the certificate is valid until (inclusive).
*/
/**
* Validate certificate.
* @param {Certificate} certificate Parsed certificate.
* @param {Buffer} signed Signed message.
* @param {Buffer} publicKey Resolver public key.
* @returns {boolean}
*/
function validate(certificate, signed, publicKey) {
if (certificate.certMagic !== CERT_MAGIC) {
return false;
}
if (certificate.minProtocol !== 0) {
return false;
}
// We are support only X25519-XSalsa20Poly1305 due to tweetnacl limitation.
if (certificate.esVersion !== 1) {
return false;
}
if (Buffer.compare(certificate.clientMagic, certificate.resolverPk.slice(0, 8)) !== 0) {
return false;
}
const tsnow = (Date.now() / 1e3) >>> 0;
if (!(certificate.ts.start <= tsnow && certificate.ts.end >= tsnow)) {
return false;
}
// Check Ed25519 signature.
if (!nacl.sign.detached.verify(signed, certificate.signature, publicKey)) {
return false;
}
return true;
}
/**
* Parse certificate.
* @param {Buffer} encodedCertificate
* @returns {Certificate}
*/
function parse(encodedCertificate) {
if (!Buffer.isBuffer(encodedCertificate)) {
return null;
}
try {
/** @type {Certificate} */
const certificate = decode(encodedCertificate, EncodedCertificate);
return certificate;
} catch (error) {
debug('invalid certificate', error);
return null;
}
}
/**
* Handle remote certificate.
* @param {Object} response DNS response.
* @param {Object} resolver DNSCrypt server config.
* @returns {Certificate}
*/
function validateCertificate(response, resolver) {
if (response.type !== 'response') {
throw new Error('Invalid DNS response');
}
if (response.rcode !== 'NOERROR') {
throw new Error('Invalid DNS response');
}
if (response.answers.length < response.questions.length) {
throw new Error('Invalid DNS response');
}
const record = response.answers.find(
answer => answer.type === 'TXT' && answer.name === resolver.providerName
);
if (!record) {
throw new Error('Invalid DNS response');
}
/** @type {Buffer[]} */
const encodedCertificates = Array.isArray(record.data) ? record.data : [record.data];
if (encodedCertificates.length === 0) {
throw new Error('Invalid DNS response');
}
// Resolver public key.
const publicKey = Buffer.from(resolver.pk, 'hex');
const reducer = (previous, { certificate }) => {
if (!previous) {
return certificate;
}
return certificate.serial > previous.serial ? certificate : previous;
};
const mapper = bytes => {
const certificate = parse(bytes);
if (certificate === null) {
return null;
}
const signed = bytes.slice(4 + 2 + 2 + 64);
return {
certificate,
signed,
};
};
const remoteCertificate = encodedCertificates
.map(bytes => mapper(bytes))
.filter(Boolean)
.filter(({ certificate, signed }) => validate(certificate, signed, publicKey))
.reduce(reducer, null);
if (remoteCertificate === null) {
throw new Error('No valid certificates');
}
debug('got certificate', remoteCertificate);
return remoteCertificate;
}