Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLSA support #92

Merged
merged 2 commits into from Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -374,6 +374,17 @@ The options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support en
}
```

#### `TLSA`

``` js
{
usage: 3,
selector: 1,
matchingType: 1,
certificate: Buffer
}
```

#### `TXT`

``` js
Expand Down
55 changes: 55 additions & 0 deletions index.js
Expand Up @@ -1409,6 +1409,60 @@ rnaptr.encodingLength = function (data) {
name.encodingLength(data.replacement) + 6
}

const rtlsa = exports.tlsa = {}

rtlsa.encode = function (cert, buf, offset) {
if (!buf) buf = Buffer.alloc(rtlsa.encodingLength(cert))
if (!offset) offset = 0
const oldOffset = offset

const certdata = cert.certificate
if (!Buffer.isBuffer(certdata)) {
throw new Error('Certificate must be a Buffer')
}

offset += 2 // Leave space for length
buf.writeUInt8(cert.usage, offset)
offset += 1
buf.writeUInt8(cert.selector, offset)
offset += 1
buf.writeUInt8(cert.matchingType, offset)
offset += 1
certdata.copy(buf, offset, 0, certdata.length)
offset += certdata.length

rtlsa.encode.bytes = offset - oldOffset
buf.writeUInt16BE(rtlsa.encode.bytes - 2, oldOffset)
return buf
}

rtlsa.encode.bytes = 0

rtlsa.decode = function (buf, offset) {
if (!offset) offset = 0
const oldOffset = offset

const cert = {}
const length = buf.readUInt16BE(offset)
offset += 2
cert.usage = buf.readUInt8(offset)
offset += 1
cert.selector = buf.readUInt8(offset)
offset += 1
cert.matchingType = buf.readUInt8(offset)
offset += 1
cert.certificate = buf.slice(offset, oldOffset + length + 2)
offset += cert.certificate.length
rtlsa.decode.bytes = offset - oldOffset
return cert
}

rtlsa.decode.bytes = 0

rtlsa.encodingLength = function (cert) {
return 5 + Buffer.byteLength(cert.certificate)
}

const renc = exports.record = function (type) {
switch (type.toUpperCase()) {
case 'A': return ra
Expand All @@ -1433,6 +1487,7 @@ const renc = exports.record = function (type) {
case 'SSHFP': return rsshfp
case 'DS': return rds
case 'NAPTR': return rnaptr
case 'TLSA': return rtlsa
}
return runknown
}
Expand Down
10 changes: 10 additions & 0 deletions test.js
Expand Up @@ -568,6 +568,16 @@ tape('naptr', function (t) {
t.end()
})

tape('tlsa', function (t) {
testEncoder(t, packet.tlsa, {
usage: 3,
selector: 1,
matchingType: 1,
certificate: Buffer.from([0, 1, 2, 3, 4, 5])
})
t.end()
})

tape('unpack', function (t) {
const buf = Buffer.from([
0x00, 0x79,
Expand Down