Skip to content

Commit f14f483

Browse files
authored
Add TLSA support (#92)
* add tlsa record support * add test for tlsa and update readme
1 parent ec4d317 commit f14f483

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ The options `PADDING`, `CLIENT_SUBNET`, `TCP_KEEPALIVE` and `KEY_TAG` support en
374374
}
375375
```
376376

377+
#### `TLSA`
378+
379+
``` js
380+
{
381+
usage: 3,
382+
selector: 1,
383+
matchingType: 1,
384+
certificate: Buffer
385+
}
386+
```
387+
377388
#### `TXT`
378389

379390
``` js

index.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1409,6 +1409,60 @@ rnaptr.encodingLength = function (data) {
14091409
name.encodingLength(data.replacement) + 6
14101410
}
14111411

1412+
const rtlsa = exports.tlsa = {}
1413+
1414+
rtlsa.encode = function (cert, buf, offset) {
1415+
if (!buf) buf = Buffer.alloc(rtlsa.encodingLength(cert))
1416+
if (!offset) offset = 0
1417+
const oldOffset = offset
1418+
1419+
const certdata = cert.certificate
1420+
if (!Buffer.isBuffer(certdata)) {
1421+
throw new Error('Certificate must be a Buffer')
1422+
}
1423+
1424+
offset += 2 // Leave space for length
1425+
buf.writeUInt8(cert.usage, offset)
1426+
offset += 1
1427+
buf.writeUInt8(cert.selector, offset)
1428+
offset += 1
1429+
buf.writeUInt8(cert.matchingType, offset)
1430+
offset += 1
1431+
certdata.copy(buf, offset, 0, certdata.length)
1432+
offset += certdata.length
1433+
1434+
rtlsa.encode.bytes = offset - oldOffset
1435+
buf.writeUInt16BE(rtlsa.encode.bytes - 2, oldOffset)
1436+
return buf
1437+
}
1438+
1439+
rtlsa.encode.bytes = 0
1440+
1441+
rtlsa.decode = function (buf, offset) {
1442+
if (!offset) offset = 0
1443+
const oldOffset = offset
1444+
1445+
const cert = {}
1446+
const length = buf.readUInt16BE(offset)
1447+
offset += 2
1448+
cert.usage = buf.readUInt8(offset)
1449+
offset += 1
1450+
cert.selector = buf.readUInt8(offset)
1451+
offset += 1
1452+
cert.matchingType = buf.readUInt8(offset)
1453+
offset += 1
1454+
cert.certificate = buf.slice(offset, oldOffset + length + 2)
1455+
offset += cert.certificate.length
1456+
rtlsa.decode.bytes = offset - oldOffset
1457+
return cert
1458+
}
1459+
1460+
rtlsa.decode.bytes = 0
1461+
1462+
rtlsa.encodingLength = function (cert) {
1463+
return 5 + Buffer.byteLength(cert.certificate)
1464+
}
1465+
14121466
const renc = exports.record = function (type) {
14131467
switch (type.toUpperCase()) {
14141468
case 'A': return ra
@@ -1433,6 +1487,7 @@ const renc = exports.record = function (type) {
14331487
case 'SSHFP': return rsshfp
14341488
case 'DS': return rds
14351489
case 'NAPTR': return rnaptr
1490+
case 'TLSA': return rtlsa
14361491
}
14371492
return runknown
14381493
}

test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,16 @@ tape('naptr', function (t) {
568568
t.end()
569569
})
570570

571+
tape('tlsa', function (t) {
572+
testEncoder(t, packet.tlsa, {
573+
usage: 3,
574+
selector: 1,
575+
matchingType: 1,
576+
certificate: Buffer.from([0, 1, 2, 3, 4, 5])
577+
})
578+
t.end()
579+
})
580+
571581
tape('unpack', function (t) {
572582
const buf = Buffer.from([
573583
0x00, 0x79,

0 commit comments

Comments
 (0)