<?xml version="1.0" encoding="US-ASCII"?>
  <?xml-stylesheet type="text/xsl" href="rfc2629.xslt" ?>

<!DOCTYPE rfc SYSTEM "rfc2629.dtd" [
]>

<?rfc compact="yes"?>
<?rfc subcompact="no"?>
<?rfc toc="yes"?>
<?rfc tocdepth="4"?>
<?rfc sortrefs="yes"?>
<?rfc symrefs="yes"?>

<rfc ipr="trust200902" number="8427" category="info" submissionType="IETF" consensus="yes">

  <front>
    <title abbrev="DNS in JSON">Representing DNS Messages in JSON</title>

    <author initials="P" surname="Hoffman" fullname="Paul Hoffman">
      <organization>ICANN</organization>
      <address>
        <email>paul.hoffman@icann.org</email>
      </address>
    </author>

    <date year="2018" month="July" />

    <area>Internet</area>


    <abstract>


<t>Some applications use DNS messages, or parts of DNS messages, as
data. For example, a system that captures DNS queries and responses
might want to be able to easily search them without having to decode
the messages each time. Another example is a system that puts together
DNS queries and responses from message parts. This document describes
a general format for DNS message data in JSON. Specific profiles of
the format in this document can be described in other documents for
specific applications and usage scenarios.</t>



    </abstract>


  </front>

  <middle>


<section anchor="introduction" title="Introduction">

<t>The DNS message format is defined in <xref target="RFC1035"/>. DNS queries and DNS responses have exactly the
same structure. Many of the field names and data type names given in <xref target="RFC1035"/> are commonly used in
discussions of DNS. For example, it is common to hear things like "the query
had a QNAME of 'example.com'" or "the RDATA has a simple structure".</t> 

<t>There are hundreds of data-interchange formats for serializing structured data. Currently, JSON
<xref target="RFC8259"/> is quite popular for many types of data, particularly data that has named subfields and
optional parts.</t>

<t>This document uses JSON to describe DNS messages. It also defines how to
describe a paired DNS query and response and how to stream DNS objects.</t>

<section anchor="design" title="Design of the Format">

<t>There are many ways to design a data format. This document uses a specific design methodology based
on the DNS format.</t>

<t><list style="symbols">
  <t>The format is based on JSON objects in order to allow a writer to include or exclude parts of the
format at will. No object members are ever required.</t>
  <t>This format is purposely overly general. A protocol or application that uses this format is expected
to use only a subset of the items defined here; it is expected to define its own profile from this format.</t>
  <t>The format allows transformation through JSON that would permit
  re-creation of the wire content of the message.</t>
  <t>All members whose values are always 16 bits or shorter are represented by JSON numbers with
no minus sign, no fractional part (except in fields that are specifically noted below), and no
exponent part. One-bit values are represented as JSON numbers whose values are either 0 or 1. See
Section 6 of <xref target="RFC8259"/> for more detail on JSON numbers.</t>
  <t>The JSON representation of the objects described in this document is limited to the UTF-8 codepoints
from U+0000 to U+007F.
This is done to prevent
an attempt to use a different encoding such as UTF-8 for octets in names or data.</t>
  <t>Items that have string values can have "HEX" appended to their names to indicate 
a non-ASCII encoding of the value.
Names that end in "HEX" have values stored in base16 encoding (hex with uppercase letters) defined
in <xref target="RFC4648"/>. This is particularly useful for RDATA that is binary.</t>
  <t>All field names in this format are used as in <xref target="RFC1035"/>, including their capitalization.
Names not defined in <xref target="RFC1035"/> generally use "camel case".</t>
  <t>The same data may be represented in multiple object members multiple times. For example, there is
a member for the octets of the DNS message header, and there are members for each named part of the
header. A message object can thus inadvertently have inconsistent data, such as a header member
whose value does not match the value of the first bits in the entire message member.</t>
  <t>It is acceptable that there are multiple ways to represent the same
  data. This is done so that application designers can choose what fields are best for them and even so that they are able to allow
multiple representations. That is, there is no "better" way to represent DNS data, so this
design doesn't prefer specific representations.</t>
  <t>The design explicitly allows for the description of malformed DNS messages. This is important for
systems that are logging messages seen on the wire, particularly messages that might be used as part
of an attack. A few examples of malformed DNS messages include:  <list style="symbols">
      <t>a resource record (RR) that has an RDLENGTH of 4 but an RDATA whose length is longer than 4 (if it is the last RR in a message)</t>
      <t>a DNS message whose QDCOUNT is 0</t>
      <t>a DNS message whose ANCOUNT is large but there are insufficient bytes after the header</t>
      <t>a DNS message whose length is less than 12 octets, meaning it doesn't even have a full header</t>
    </list></t>
  <t>An object in this format can have zero or more of the members defined
  here; that is, no members
are required by the format itself. Instead, profiles that use this format might have requirements
for mandatory members, optional members, and prohibited members from the format. Also, this format
does not prohibit members that are not defined in this format; profiles of the format are free to
add new members in the profile.</t>
  <t>This document defines DNS messages, not the zone files described in <xref target="RFC1035"/>. A different
specification could be written to extend it to represent zone files. Note that DNS zone
files allow escaping of octet values using "\DDD" notation, but this specification does
not allow that; when encoding from a zone file to this JSON format, you need to do a
conversion for many types of values.</t>
</list></t>

</section>


<section anchor="terminology" title="Terminology">
        <t>
    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
    NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "NOT RECOMMENDED",
    "MAY", and "OPTIONAL" in this document are to be interpreted as
    described in BCP&nbsp;14 <xref target="RFC2119"/> <xref target="RFC8174"/> 
    when, and only when, they appear in all capitals, as shown here.
        </t>
</section>
</section>
<section anchor="json-format-for-dns-messages" title="JSON Format for DNS Messages">

<t>The following gives all of the members defined for a DNS message. It is organized approximately by
levels of the DNS message.</t>

<section anchor="message-object-members" title="Message Object Members">

<t><list style="symbols">
  <t>ID - Integer whose value is 0 to 65535</t>
  <t>QR - Boolean</t>
  <t>Opcode - Integer whose value is 0 to 15</t>
  <t>AA - Boolean</t>
  <t>TC - Boolean</t>
  <t>RD - Boolean</t>
  <t>RA - Boolean</t>
  <t>AD - Boolean</t>
  <t>CD - Boolean</t>
  <t>RCODE - Integer whose value is 0 to 15</t>
  <t>QDCOUNT - Integer whose value is 0 to 65535</t>
  <t>ANCOUNT - Integer whose value is 0 to 65535</t>
  <t>NSCOUNT - Integer whose value is 0 to 65535</t>
  <t>ARCOUNT - Integer whose value is 0 to 65535</t>
  <t>QNAME - String of the name of the first Question section of the message; see <xref target="names"/> for a description of the contents</t>
  <t>compressedQNAME - Object that describes the name with two optional values: "isCompressed" (with a value of 0 for no and 1 for yes)
and "length" (with an integer giving the length in the message)</t>
  <t>QTYPE - Integer whose value is 0 to 65535, of the QTYPE of the first Question section of the message</t>

  <t>QTYPEname - String whose value is from the IANA "Resource Record (RR) TYPEs" registry or has the format in <xref target="RFC3597"/>; this is case sensitive, so "AAAA", not "aaaa"</t>
  <t>QCLASS - Integer whose value is 0 to 65535, of the QCLASS of the first Question section of the message</t>
  <t>QCLASSname - String whose value is "IN", "CH", or "HS" or that has the format in <xref target="RFC3597"/></t>
  <t>questionRRs - Array of zero or more resource records or rrSet objects in the Question section</t>
  <t>answerRRs - Array of zero or more resource records or rrSet objects in the Answer section</t>
  <t>authorityRRs - Array of zero or more resource records or rrSet objects in the Authority section</t>
  <t>additionalRRs - Array of zero or more resource records or rrSet objects in the Additional section</t>
</list></t>

</section>
<section anchor="resource-record-object-members" title="Resource Record Object Members">

<t>A resource record is represented as an object with the following members.</t>

<t><list style="symbols">
  <t>NAME - String of the NAME field of the resource record; see <xref target="names"/> for a description of the contents</t>
  <t>compressedNAME - Object that describes the name with two optional values: "isCompressed" (with a value of 0 for no and 1 for yes)
and "length" (with an integer giving the length in the message)</t>
  <t>TYPE - Integer whose value is 0 to 65535</t>
  <t>TYPEname - String whose value is from the IANA "Resource Record (RR) TYPEs" registry or
  has the format in <xref target="RFC3597"/>; this is case sensitive, so "AAAA", not "aaaa"</t>
  <t>CLASS - Integer whose value is 0 to 65535</t>
  <t>CLASSname - String whose value is "IN", "CH", or "HS" or has the format in <xref target="RFC3597"/></t>
  <t>TTL - Integer whose value is -2147483648 to 2147483647 (it will only be 0 to 2147483647 in normal circumstances)</t>
  <t>RDLENGTH - Integer whose value is 0 to 65535. Applications using this format are unlikely to use this value directly, and instead calculate the value from the RDATA.</t>
  <t>RDATAHEX - Hex-encoded string (base16 encoding, described in <xref target="RFC4648"/>)
of the octets of the RDATA field of the resource record.
The data in some common RDATA fields are also described in their own members;
see <xref target="specificrdata"/></t>
  <t>rrSet - List of objects that have RDLENGTH and RDATA members</t>
</list></t>

<t>A Question section can be expressed as a resource record. When doing so, the TTL, RDLENGTH,
and RDATA members make no sense.</t>

</section>
<section anchor="specificrdata" title="Specific RDATA Field Members">

<t>The following are common RDATA types and how to specify them as JSON members. The name of the member
contains the name of the RDATA type. The data type for each of these members is a string.
Each name is prefaced with "rdata" to prevent a name collision with fields that might later be
defined that have the same name as the raw type name.</t>

<t><list style="symbols">
  <t>rdataA - IPv4 address, such as "192.168.33.44"</t>
  <t>rdataAAAA - IPv6 address, such as "fe80::a65e:60ff:fed6:8aaf", as defined in <xref target="RFC5952"/></t>
  <t>rdataCNAME - A domain name</t>
  <t>rdataDNAME - A domain name</t>
  <t>rdataNS - A domain name</t>
  <t>rdataPTR - A domain name</t>
  <t>rdataTXT - A text value</t>
</list></t>

<t>In addition, each of the following members has a value that is a space-separated string that
matches the display format definition in the RFC that defines that RDATA type. It is not
expected that every receiving application will know how to parse these values.</t>

<t>rdataCDNSKEY,
rdataCDS,
rdataCSYNC,
rdataDNSKEY,
rdataHIP,
rdataIPSECKEY,
rdataKEY,
rdataMX,
rdataNSEC,
rdataNSEC3,
rdataNSEC3PARAM,
rdataOPENPGPKEY,
rdataRRSIG,
rdataSMIMEA,
rdataSPF,
rdataSRV,
rdataSSHFP,
rdataTLSA</t>

</section>
<section anchor="the-message-and-its-parts-as-octets" title="The Message and Its Parts as Octets">

<t>The following can be members of a message object. These members are all encoded
in base16 encoding, described in <xref target="RFC4648"/>. All these items are strings.</t>

<t><list style="symbols">
  <t>messageOctetsHEX - The octets of the message</t>
  <t>headerOctetsHEX - The first 12 octets of the message (or fewer, if the message is truncated)</t>
  <t>questionOctetsHEX - The octets of the Question section</t>
  <t>answerOctetsHEX - The octets of the Answer section</t>
  <t>authorityOctetsHEX - The octets of the Authority section</t>
  <t>additionalOctetsHEX - The octets of the Additional section</t>
</list></t>

<t>The following can be a member of a resource record object.</t>

<t><list style="symbols">
  <t>rrOctetsHEX - The octets of a particular resource record</t>
</list></t>

<t>The items in this section are useful in applications to canonically reproduce what appeared on the wire.
For example, an application that is converting wire-format requests and responses might do decompression
of names, but the system reading the converted data may want to be sure the decompression was done
correctly. Such a system would need to see the part of the message where the decompressed labels resided,
such as in one of the items in this section.</t>

</section>
<section anchor="additional-message-object-members" title="Additional Message Object Members">

<t>The following are members that might appear in a message object:</t>

<t><list style="symbols">
  <t>dateString - The date that the message was sent or received, given as a string in the standard
format described in <xref target="RFC3339"/> and refined by Section 3.3 of <xref target="RFC4287"/>.</t>



  <t>dateSeconds - The date that the message was sent or received, given as a JSON number that is the
number of seconds since 1970-01-01T00:00Z in UTC time; this number can be fractional. This number must have no minus sign, can have an optional fractional part, and can have no exponent part.</t>
  <t>comment - An unstructured comment as a string.</t>
</list></t>

</section>
<section anchor="names" title="Name Fields">

<t>Names are represented by JSON strings. The rules for how names are encoded are described in <xref target="design"/>.
(To recap: it is limited to the UTF-8 codepoints from U+0000 to U+007F.)
The contents of these fields are always uncompressed; that is, after <xref target="RFC1035"/>, name compression has been removed.</t>

<t>There are two encodings for names:</t>

<t><list style="symbols">
  <t>If the member name does not end in "HEX", the value is a domain name encoded as
DNS labels consisting of UTF-8 codepoints from U+0000 to U+007F.
Within a label, codepoints above U+007F and the codepoint U+002E (ASCII period)
MUST be expressed using JSON's escaping rules within this set of codepoints.
Separation between labels is indicated with a period (codepoint U+002E).
Internationalized Domain Name (IDN) labels are always expressed in their A-label form, as described in <xref target="RFC5890"/>.</t>
  <t>If the member name ends in "HEX", the value is the wire format for an entire domain name stored in
base16 encoding, which is described in <xref target="RFC4648"/>.</t>
</list></t>

</section>
</section>
<section anchor="json-format-for-a-paired-dns-query-and-response" title="JSON Format for a Paired DNS Query and Response">

<t>A paired DNS query and response is represented as an object. Two optional members of this object are
named "queryMessage" and "responseMessage", and each has a value that is a message object. This
design was chosen (as compared to the more obvious array of two values) so that a paired DNS query
and response could be differentiated from a stream of DNS messages whose length happens to be two.</t>

</section>
<section anchor="streaming-dns-objects" title="Streaming DNS Objects">

<t>Streaming of DNS objects is performed using JSON text sequences <xref target="RFC7464"/>.</t>

</section>
<section anchor="examples" title="Examples">

<section anchor="example-of-the-format-of-a-dns-query" title="Example of the Format of a DNS Query">

<t>The following is an example of a query for the A record of example.com.</t>

<figure><artwork><![CDATA[
{ "ID": 19678, "QR": 0, "Opcode": 0,
  "AA": 0, "TC": 0, "RD": 0, "RA": 0, "AD": 0, "CD": 0, "RCODE": 0,
  "QDCOUNT": 1, "ANCOUNT": 0, "NSCOUNT": 0, "ARCOUNT": 0,
  "QNAME": "example.com", "QTYPE": 1, "QCLASS": 1
}
]]></artwork></figure>

<t>As stated earlier, all members of an object are optional. This example object
could have one or more of the following members as well:</t>

<figure><artwork><![CDATA[
"answerRRs": []
"authorityOctetsHEX": ""
"comment": "Something pithy goes here"
"dateSeconds": 1408504748.657783
"headerOctetsHEX": "4CDE00000001000000000000"
"QNAMEHEX": "076578616D706C6503636F6D00",
"compressedQNAME": { "isCompressed": 0 },
"messageOctetsHEX":
     "4CDE00000001000000000000076578616D706C6503636F6D0000010001"
"questionOctetsHEX": "076578616D706C6503636F6D0000010001"
"questionRRs": [ { "NAMEHEX": "076578616D706C6503636F6D00",
               "TYPE": 1, "CLASS": 1, "hostNAME" : "example.com." } ]
"questionRRs": [ { "NAME": "example.com.", "TYPE": 1,
               "CLASS": 1, } ]
]]></artwork></figure>

<t>(Note that this is an incomplete list of what else could be in the object.)</t>

</section>
<section anchor="example-of-the-format-of-a-paired-dns-query-and-response" title="Example of the Format of a Paired DNS Query and Response">

<t>The following is a paired DNS query and response for a query for the A record of example.com.</t>

<figure><artwork><![CDATA[
{
  "queryMessage": { "ID": 32784, "QR": 0, "Opcode": 0, "AA": 0,
                   "TC": 0, "RD": 0, "RA": 0, "AD": 0, "CD": 0,
                   "RCODE": 0, "QDCOUNT": 1, "ANCOUNT": 0,
                   "NSCOUNT": 0, "ARCOUNT": 0,
                   "QNAME": "example.com.",
                   "QTYPE": 1, "QCLASS": 1 },
  "responseMessage": { "ID": 32784, "QR": 1, "AA": 1, "RCODE": 0,
                      "QDCOUNT": 1, "ANCOUNT": 1, "NSCOUNT": 1,
                      "ARCOUNT": 0,
                      "answerRRs": [ { "NAME": "example.com.",
                                       "TYPE": 1, "CLASS": 1,
                                       "TTL": 3600,
                                       "RDATAHEX": "C0000201" }, 
                                     { "NAME": "example.com.",
                                       "TYPE": 1, "CLASS": 1,
                                       "TTL": 3600, 
                                       "RDATAHEX": "C000AA01" } ],
                       "authorityRRs": [ { "NAME": "ns.example.com.",
                                           "TYPE": 1, "CLASS": 1,
                                           "TTL": 28800,
                                           "RDATAHEX": "CB007181" } ]
                    }
}
]]></artwork></figure>

<t>The Answer section could instead be given with an rrSet:</t>

<figure><artwork><![CDATA[
"answerRRs": [ { "NAME": "example.com.",
                 "TYPE": 1, "CLASS": 1,
                 "TTL": 3600, 
                 "rrSet": [ { "RDATAHEX": "C0000201" },
                            { "RDATAHEX": "C000AA01" } ] } ],
]]></artwork></figure>

<t>(Note that this is an incomplete list of what else could be in the Answer section.)</t>

</section>
</section>
<section anchor="local-format-policy" title="Local Format Policy">

<t>Systems using the format in this document will likely have policy about what must be in the
objects. Those policies are outside the scope of this document.</t>

<t>For example, passive DNS systems such as those described in <xref target="PASSIVE-DNS"/>
cover just DNS responses. Such a system might have a policy that makes QNAME, QTYPE, and answerRRs
mandatory. That document also describes two mandatory times that are not in this format, so the
policy would possibly also define those members and make them mandatory. The policy could also
define additional members that might appear in a record.</t>

<t>As another example, a program that uses this format for configuring what a test client sends on the
wire might have a policy of "each record object can have as few members as it wants; all unstated
members are filled in from previous records".</t>

</section>
<section anchor="ianacons" title="IANA Considerations">

<section anchor="mime-type-registration-of-applicationdnsjson" 
title="Media Type Registration of application/dns+json">

<figure><artwork><![CDATA[
Type name: application

Subtype name: dns+json

Required parameters: N/A
 
Optional parameters: N/A

Encoding considerations:  Encoding considerations are identical to
those specified for the "application/json" media type.

Security considerations:  This document specifies the security
considerations for the format.

Interoperability considerations:  This document specifies format of
conforming messages and the interpretation thereof.

Published specification:  This document

Applications that use this media type:  Systems that want to exchange
DNS messages

Fragment identifier considerations:  None

Additional information:  

  Deprecated alias names for this type: N/A

  Magic number(s):  N/A

  File extension(s):  This document uses the media type to refer to
  protocol messages and thus does not require a file extension.

  Macintosh file type code(s):  N/A

Person & email address to contact for further information:
Paul Hoffman, paul.hoffman@icann.org

Intended usage:  COMMON

Restrictions on usage:  N/A

Author:  Paul Hoffman, paul.hoffman@icann.org

Change controller:  Paul Hoffman, paul.hoffman@icann.org
]]></artwork></figure>


</section>
</section>
<section anchor="securitycons" title="Security Considerations">

<t>As described in <xref target="design"/>, a message object can  have inconsistent data, such as a message with an
ANCOUNT of 1 but that has either an empty answerRRs array or an answerRRs array that has two or more
RRs. Other examples of inconsistent data would be resource records whose RDLENGTH does not match the
length of the decoded value in the RDATAHEX member, a record whose various header fields do not
match the value in headerOctetsHEX, and so on. A reader of this format must never assume that all of
the data in an object are all consistent with each other.</t>

<t>This document describes a format, not a profile of that format. Lack of profile can lead
to security issues. For example, if a system has a filter for JSON
representations of DNS packets, that filter needs to have the same
semantics for the output JSON as the consumer has. Unless the profile is quite tight,
this can lead to the producer being able to create fields with different
contents (using the HEX and regular formats), fields with malformed lengths, and so on.</t>

<t>Numbers in JSON do not have any bounds checking. Thus, integer values in a record might
have invalid values, such as an ID field whose value is greater than or equal to 2^16, a
QR field that has a value of 2, and so on.</t>

</section>
<section anchor="privacy-considerations" title="Privacy Considerations">

<t>The values that can be contained in this format may contain privacy-sensitive information.
For example, a profile of this format that is used for logging queries sent to recursive
resolvers might have source IP addresses that could identify the location of the person
who sent the DNS query.</t>

</section>

  </middle>

  <back>

    <references title='Normative References'>

<?rfc include="reference.RFC.2119" ?>
<?rfc include="reference.RFC.8174" ?>

<?rfc include="reference.RFC.1035" ?>

<?rfc include="reference.RFC.3339" ?>

<?rfc include="reference.RFC.3597" ?>

<?rfc include="reference.RFC.4287" ?>

<?rfc include="reference.RFC.4648" ?>

<?rfc include="reference.RFC.5890" ?>

<?rfc include="reference.RFC.5952" ?>

<?rfc include="reference.RFC.7464" ?>

<?rfc include="reference.RFC.8259" ?>

    </references>

    <references title='Informative References'>

<!-- draft-dulaunoy-dnsop-passive-dns-cof-03 Expired  -->

<reference anchor='PASSIVE-DNS'>
<front>
<title>Passive DNS - Common Output Format</title>

<author initials='A' surname='Dulaunoy' fullname='Alexandre Dulaunoy'>
    <organization />
</author>

<author initials='A' surname='Kaplan' fullname='Aaron Kaplan'>
    <organization />
</author>

<author initials='P' surname='Vixie' fullname='Paul Vixie'>
    <organization />
</author>

<author initials='H' surname='Stern' fullname='Henry Stern'>
    <organization />
</author>

<date month='June' day='13' year='2017' />

</front>

<seriesInfo name="Work in Progress,"
	    value="draft-dulaunoy-dnsop-passive-dns-cof-03" />


</reference>

<!-- draft-daley-dnsxml-00 Expired  -->

<reference anchor='DNSXML'>
<front>
<title>dnsxml - A standard XML representation of DNS data</title>

<author initials='J' surname='Daley' fullname='Jay Daley'>
    <organization />
</author>

<author initials='S' surname='Morris' fullname='Stephen Morris'>
    <organization />
</author>

<author initials='J' surname='Dickinson' fullname='John Dickinson'>
    <organization />
</author>

<date month='July' day='30' year='2013' />

</front>

<seriesInfo name='Work in Progress,' value='draft-daley-dnsxml-00' />

</reference>

<!-- draft-mohan-dns-query-xml-00 Expired  -->

<reference anchor='DNS-QUERY'>
<front>
<title>Representing DNS messages using XML</title>

<author initials='M' surname='Parthasarathy' fullname='Mohan Parthasarathy'>
    <organization />
</author>

<author initials='P' surname='Vixie' fullname='Paul Vixie'>
    <organization />
</author>

<date month='September' day='27' year='2011' />

</front>

<seriesInfo name='Work in Progress,' value='draft-mohan-dns-query-xml-00' />

</reference>

    </references>

<section anchor="acknowledgements" title="Acknowledgements" numbered="no">

<t>Some of the ideas in this document were inspired by earlier, abandoned
work such as <xref target="DNSXML"/>, <xref target="DNS-QUERY"/>, and <xref target="PASSIVE-DNS"/>. The
document was also inspired by early ideas from Stephane Bortzmeyer.
Many people in the Domain Name System Operations (DNSOP) and DNS
Over HTTPS (DOH) working groups contributed very useful ideas (even though this was not
a WG work item).</t>

</section>

  </back>


</rfc>

