From c1e94b5d699b11cb531c706e05656b1e060b169a Mon Sep 17 00:00:00 2001 From: kyleaedwards Date: Sat, 12 Aug 2017 02:03:45 -0500 Subject: [PATCH] support CAA records --- README.md | 13 +++++++++++-- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.js | 5 +++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5105895..59e7566 100644 --- a/README.md +++ b/README.md @@ -189,9 +189,18 @@ Currently the different available records are } ``` +#### `CAA` + +``` js +{ + flags: 128, // octet + tag: 'issue|issuewild|iodef', + value: 'ca.example.net' +} +``` + If you need another one, open an issue and we'll try to add it. ## License -MIT - +MIT \ No newline at end of file diff --git a/index.js b/index.js index 1f4e9a9..017d218 100644 --- a/index.js +++ b/index.js @@ -325,6 +325,62 @@ rsrv.encodingLength = function (data) { return 8 + name.encodingLength(data.target) } +var rcaa = exports.caa = {} + +rcaa.ISSUER_CRITICAL = 1 << 7 + +rcaa.encode = function (data, buf, offset) { + var len = rcaa.encodingLength(data) + + if (!buf) buf = Buffer.allocUnsafe(rcaa.encodingLength(data)) + if (!offset) offset = 0 + + if (data.issuerCritical) { + data.flags = rcaa.ISSUER_CRITICAL + } + + buf.writeUInt16BE(len - 2, offset) + offset += 2 + buf.writeUInt8(data.flags, offset) + offset += 1 + string.encode(data.tag, buf, offset) + offset += string.encode.bytes + buf.write(data.value, offset) + offset += Buffer.byteLength(data.value) + + rcaa.encode.bytes = len + return buf +} + +rcaa.encode.bytes = 0 + +rcaa.decode = function (buf, offset) { + if (!offset) offset = 0 + + var len = buf.readUInt16BE(offset) + offset += 2 + + var oldOffset = offset + var data = {} + data.flags = buf.readUInt8(offset) + offset += 1 + data.tag = string.decode(buf, offset) + offset += string.decode.bytes + data.value = buf.toString('utf-8', offset, oldOffset + len) + + data.issuerCritical = !!(data.flags & rcaa.ISSUER_CRITICAL) + + rcaa.decode.bytes = len + 2 + + return data +} + +rcaa.decode.bytes = 0 + +rcaa.encodingLength = function (data) { + return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2 +} + var ra = exports.a = {} ra.encode = function (host, buf, offset) { @@ -396,6 +452,7 @@ var renc = exports.record = function (type) { case 'AAAA': return raaaa case 'SRV': return rsrv case 'HINFO': return rhinfo + case 'CAA': return rcaa } return runknown } diff --git a/test.js b/test.js index 245a8ca..0db58a6 100644 --- a/test.js +++ b/test.js @@ -45,6 +45,11 @@ tape('srv', function (t) { t.end() }) +tape('caa', function (t) { + testEncoder(t, packet.caa, {flags: 128, tag: 'issue', value: 'letsencrypt.org', issuerCritical: true}) + t.end() +}) + tape('a', function (t) { testEncoder(t, packet.a, '127.0.0.1') t.end()