2020-11-19 14:06:15 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_net_DNSPacket_h__
|
|
|
|
#define mozilla_net_DNSPacket_h__
|
|
|
|
|
2021-02-04 08:05:45 +00:00
|
|
|
#include "mozilla/Maybe.h"
|
2020-11-19 14:06:15 +00:00
|
|
|
#include "mozilla/Result.h"
|
2021-01-26 17:44:45 +00:00
|
|
|
#include "pk11pub.h"
|
|
|
|
#include "ScopedNSSTypes.h"
|
2021-02-24 16:37:03 +00:00
|
|
|
#include "nsIDNSService.h"
|
2020-11-19 14:06:15 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class DOHresp {
|
|
|
|
public:
|
|
|
|
nsresult Add(uint32_t TTL, unsigned char const* dns, unsigned int index,
|
|
|
|
uint16_t len, bool aLocalAllowed);
|
|
|
|
nsTArray<NetAddr> mAddresses;
|
|
|
|
uint32_t mTtl = UINT32_MAX;
|
|
|
|
};
|
|
|
|
|
|
|
|
// the values map to RFC1035 type identifiers
|
|
|
|
enum TrrType {
|
|
|
|
TRRTYPE_A = 1,
|
|
|
|
TRRTYPE_NS = 2,
|
|
|
|
TRRTYPE_CNAME = 5,
|
|
|
|
TRRTYPE_AAAA = 28,
|
|
|
|
TRRTYPE_OPT = 41,
|
|
|
|
TRRTYPE_TXT = 16,
|
|
|
|
TRRTYPE_HTTPSSVC = nsIDNSService::RESOLVE_TYPE_HTTPSSVC, // 65
|
|
|
|
};
|
|
|
|
|
2021-02-18 12:50:03 +00:00
|
|
|
enum class DNSPacketStatus : uint8_t {
|
|
|
|
Unknown = 0,
|
|
|
|
Success,
|
|
|
|
KeyNotAvailable,
|
|
|
|
KeyNotUsable,
|
|
|
|
EncodeError,
|
|
|
|
EncryptError,
|
|
|
|
DecodeError,
|
|
|
|
DecryptError,
|
|
|
|
};
|
|
|
|
|
2020-11-19 14:06:15 +00:00
|
|
|
class DNSPacket {
|
|
|
|
public:
|
2021-01-26 14:19:09 +00:00
|
|
|
DNSPacket() = default;
|
|
|
|
virtual ~DNSPacket() = default;
|
|
|
|
|
2021-02-09 14:22:56 +00:00
|
|
|
Result<uint8_t, nsresult> GetRCode() const;
|
|
|
|
|
2020-11-19 14:06:15 +00:00
|
|
|
// Called in order to feed data into the buffer.
|
|
|
|
nsresult OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream,
|
|
|
|
uint64_t aOffset, const uint32_t aCount);
|
|
|
|
|
|
|
|
// Encodes the name request into a buffer that represents a DNS packet
|
2021-01-26 14:19:09 +00:00
|
|
|
virtual nsresult EncodeRequest(nsCString& aBody, const nsACString& aHost,
|
|
|
|
uint16_t aType, bool aDisableECS);
|
2020-11-19 14:06:15 +00:00
|
|
|
|
|
|
|
// Decodes the DNS response and extracts the responses, additional records,
|
|
|
|
// etc. XXX: This should probably be refactored to reduce the number of
|
|
|
|
// output parameters and have a common format for different record types.
|
2021-01-26 14:19:09 +00:00
|
|
|
virtual nsresult Decode(
|
2020-11-19 14:06:15 +00:00
|
|
|
nsCString& aHost, enum TrrType aType, nsCString& aCname,
|
2021-02-09 14:22:56 +00:00
|
|
|
bool aAllowRFC1918, DOHresp& aResp, TypeRecordResultType& aTypeResult,
|
2020-11-19 14:06:15 +00:00
|
|
|
nsClassHashtable<nsCStringHashKey, DOHresp>& aAdditionalRecords,
|
|
|
|
uint32_t& aTTL);
|
|
|
|
|
2021-02-18 12:50:03 +00:00
|
|
|
DNSPacketStatus PacketStatus() const { return mStatus; }
|
|
|
|
|
2021-01-26 14:19:09 +00:00
|
|
|
protected:
|
2020-11-19 14:06:15 +00:00
|
|
|
// Never accept larger DOH responses than this as that would indicate
|
|
|
|
// something is wrong. Typical ones are much smaller.
|
|
|
|
static const unsigned int MAX_SIZE = 3200;
|
|
|
|
|
2021-01-26 14:19:04 +00:00
|
|
|
nsresult PassQName(unsigned int& index, const unsigned char* aBuffer);
|
|
|
|
nsresult GetQname(nsACString& aQname, unsigned int& aIndex,
|
|
|
|
const unsigned char* aBuffer);
|
2020-11-19 14:06:15 +00:00
|
|
|
nsresult ParseSvcParam(unsigned int svcbIndex, uint16_t key,
|
2021-01-26 14:19:04 +00:00
|
|
|
SvcFieldValue& field, uint16_t length,
|
|
|
|
const unsigned char* aBuffer);
|
|
|
|
nsresult DecodeInternal(
|
|
|
|
nsCString& aHost, enum TrrType aType, nsCString& aCname,
|
2021-02-09 14:22:56 +00:00
|
|
|
bool aAllowRFC1918, DOHresp& aResp, TypeRecordResultType& aTypeResult,
|
2021-01-26 14:19:04 +00:00
|
|
|
nsClassHashtable<nsCStringHashKey, DOHresp>& aAdditionalRecords,
|
|
|
|
uint32_t& aTTL, const unsigned char* aBuffer, uint32_t aLen);
|
2020-11-19 14:06:15 +00:00
|
|
|
|
2021-02-18 12:50:03 +00:00
|
|
|
void SetDNSPacketStatus(DNSPacketStatus aStatus) {
|
|
|
|
if (mStatus == DNSPacketStatus::Unknown ||
|
|
|
|
mStatus == DNSPacketStatus::Success) {
|
|
|
|
mStatus = aStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 14:06:15 +00:00
|
|
|
// The response buffer.
|
|
|
|
unsigned char mResponse[MAX_SIZE]{};
|
|
|
|
unsigned int mBodySize = 0;
|
2021-02-18 12:50:03 +00:00
|
|
|
DNSPacketStatus mStatus = DNSPacketStatus::Unknown;
|
2020-11-19 14:06:15 +00:00
|
|
|
};
|
|
|
|
|
2021-01-26 14:19:09 +00:00
|
|
|
class ODoHDNSPacket final : public DNSPacket {
|
|
|
|
public:
|
2021-02-04 08:05:45 +00:00
|
|
|
ODoHDNSPacket() = default;
|
2021-01-26 14:19:09 +00:00
|
|
|
virtual ~ODoHDNSPacket();
|
|
|
|
|
2021-03-19 08:29:14 +00:00
|
|
|
static bool ParseODoHConfigs(Span<const uint8_t> aData,
|
2021-01-26 14:19:09 +00:00
|
|
|
nsTArray<ObliviousDoHConfig>& aOut);
|
|
|
|
|
|
|
|
virtual nsresult EncodeRequest(nsCString& aBody, const nsACString& aHost,
|
|
|
|
uint16_t aType, bool aDisableECS) override;
|
|
|
|
|
|
|
|
virtual nsresult Decode(
|
|
|
|
nsCString& aHost, enum TrrType aType, nsCString& aCname,
|
2021-02-09 14:22:56 +00:00
|
|
|
bool aAllowRFC1918, DOHresp& aResp, TypeRecordResultType& aTypeResult,
|
2021-01-26 14:19:09 +00:00
|
|
|
nsClassHashtable<nsCStringHashKey, DOHresp>& aAdditionalRecords,
|
|
|
|
uint32_t& aTTL) override;
|
|
|
|
|
|
|
|
protected:
|
2021-01-26 17:44:45 +00:00
|
|
|
bool EncryptDNSQuery(const nsACString& aQuery, uint16_t aPaddingLen,
|
|
|
|
const ObliviousDoHConfig& aConfig,
|
|
|
|
ObliviousDoHMessage& aOut);
|
|
|
|
bool DecryptDNSResponse();
|
|
|
|
|
|
|
|
HpkeContext* mContext = nullptr;
|
|
|
|
UniqueSECItem mPlainQuery;
|
2021-02-04 08:05:45 +00:00
|
|
|
// This struct indicates the range of decrypted responses stored in mResponse.
|
|
|
|
struct DecryptedResponseRange {
|
|
|
|
uint16_t mStart = 0;
|
|
|
|
uint16_t mLength = 0;
|
|
|
|
};
|
|
|
|
Maybe<DecryptedResponseRange> mDecryptedResponseRange;
|
2021-01-26 14:19:09 +00:00
|
|
|
};
|
|
|
|
|
2020-11-19 14:06:15 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_net_DNSPacket_h__
|