2018-04-18 18:47:10 +02:00
|
|
|
package dnsstamps
|
2018-01-20 13:56:26 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/binary"
|
2018-04-01 16:35:32 +02:00
|
|
|
"encoding/hex"
|
2018-01-20 14:13:11 +01:00
|
|
|
"errors"
|
2018-01-20 13:56:26 +01:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-02-02 15:07:12 +01:00
|
|
|
"strconv"
|
2018-01-20 14:13:11 +01:00
|
|
|
"strings"
|
2018-01-20 13:56:26 +01:00
|
|
|
)
|
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
const DefaultPort = 443
|
|
|
|
|
|
|
|
type ServerInformalProperties uint64
|
|
|
|
|
|
|
|
const (
|
|
|
|
ServerInformalPropertyDNSSEC = ServerInformalProperties(1) << 0
|
|
|
|
ServerInformalPropertyNoLog = ServerInformalProperties(1) << 1
|
|
|
|
ServerInformalPropertyNoFilter = ServerInformalProperties(1) << 2
|
|
|
|
)
|
|
|
|
|
2018-01-25 14:31:18 +01:00
|
|
|
type StampProtoType uint8
|
|
|
|
|
|
|
|
const (
|
2018-04-01 16:35:32 +02:00
|
|
|
StampProtoTypePlain = StampProtoType(0x00)
|
|
|
|
StampProtoTypeDNSCrypt = StampProtoType(0x01)
|
|
|
|
StampProtoTypeDoH = StampProtoType(0x02)
|
2018-04-18 18:47:10 +02:00
|
|
|
StampProtoTypeTLS = StampProtoType(0x03)
|
2018-01-25 14:31:18 +01:00
|
|
|
)
|
|
|
|
|
2018-01-31 09:42:56 +01:00
|
|
|
func (stampProtoType *StampProtoType) String() string {
|
|
|
|
switch *stampProtoType {
|
|
|
|
case StampProtoTypePlain:
|
|
|
|
return "Plain"
|
2018-04-01 16:35:32 +02:00
|
|
|
case StampProtoTypeDNSCrypt:
|
|
|
|
return "DNSCrypt"
|
2018-01-31 09:42:56 +01:00
|
|
|
case StampProtoTypeDoH:
|
|
|
|
return "DoH"
|
|
|
|
default:
|
|
|
|
panic("Unexpected protocol")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:56:26 +01:00
|
|
|
type ServerStamp struct {
|
2018-04-14 15:03:21 +02:00
|
|
|
ServerAddrStr string
|
|
|
|
ServerPk []uint8
|
|
|
|
Hashes [][]uint8
|
|
|
|
ProviderName string
|
|
|
|
Path string
|
|
|
|
Props ServerInformalProperties
|
|
|
|
Proto StampProtoType
|
2018-01-20 13:56:26 +01:00
|
|
|
}
|
|
|
|
|
2018-04-01 16:35:32 +02:00
|
|
|
func NewDNSCryptServerStampFromLegacy(serverAddrStr string, serverPkStr string, providerName string, props ServerInformalProperties) (ServerStamp, error) {
|
|
|
|
if net.ParseIP(serverAddrStr) != nil {
|
|
|
|
serverAddrStr = fmt.Sprintf("%s:%d", serverAddrStr, DefaultPort)
|
|
|
|
}
|
|
|
|
serverPk, err := hex.DecodeString(strings.Replace(serverPkStr, ":", "", -1))
|
2018-04-18 19:06:50 +02:00
|
|
|
if err != nil || len(serverPk) != 32 {
|
2018-04-01 16:35:32 +02:00
|
|
|
return ServerStamp{}, fmt.Errorf("Unsupported public key: [%s]", serverPkStr)
|
|
|
|
}
|
|
|
|
return ServerStamp{
|
2018-04-14 15:03:21 +02:00
|
|
|
ServerAddrStr: serverAddrStr,
|
|
|
|
ServerPk: serverPk,
|
|
|
|
ProviderName: providerName,
|
|
|
|
Props: props,
|
|
|
|
Proto: StampProtoTypeDNSCrypt,
|
2018-04-01 16:35:32 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:56:26 +01:00
|
|
|
func NewServerStampFromString(stampStr string) (ServerStamp, error) {
|
2018-01-22 12:00:42 +01:00
|
|
|
if !strings.HasPrefix(stampStr, "sdns://") && !strings.HasPrefix(stampStr, "dnsc://") {
|
2018-01-26 20:38:31 +01:00
|
|
|
return ServerStamp{}, errors.New("Stamps are expected to start with sdns://")
|
2018-01-20 14:13:11 +01:00
|
|
|
}
|
|
|
|
bin, err := base64.RawURLEncoding.DecodeString(stampStr[7:])
|
|
|
|
if err != nil {
|
2018-01-26 20:38:31 +01:00
|
|
|
return ServerStamp{}, err
|
2018-01-20 14:13:11 +01:00
|
|
|
}
|
2018-01-26 20:38:31 +01:00
|
|
|
if len(bin) < 1 {
|
|
|
|
return ServerStamp{}, errors.New("Stamp is too short")
|
2018-01-20 14:13:11 +01:00
|
|
|
}
|
2018-04-01 16:35:32 +02:00
|
|
|
if bin[0] == uint8(StampProtoTypeDNSCrypt) {
|
|
|
|
return newDNSCryptServerStamp(bin)
|
|
|
|
} else if bin[0] == uint8(StampProtoTypeDoH) {
|
2018-01-26 22:59:16 +01:00
|
|
|
return newDoHServerStamp(bin)
|
2018-01-26 20:38:31 +01:00
|
|
|
}
|
|
|
|
return ServerStamp{}, errors.New("Unsupported stamp version or protocol")
|
|
|
|
}
|
|
|
|
|
2018-04-01 16:35:32 +02:00
|
|
|
// id(u8)=0x01 props addrLen(1) serverAddr pkStrlen(1) pkStr providerNameLen(1) providerName
|
|
|
|
|
|
|
|
func newDNSCryptServerStamp(bin []byte) (ServerStamp, error) {
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp := ServerStamp{Proto: StampProtoTypeDNSCrypt}
|
2018-04-01 16:35:32 +02:00
|
|
|
if len(bin) < 66 {
|
|
|
|
return stamp, errors.New("Stamp is too short")
|
|
|
|
}
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.Props = ServerInformalProperties(binary.LittleEndian.Uint64(bin[1:9]))
|
2018-04-01 16:35:32 +02:00
|
|
|
binLen := len(bin)
|
|
|
|
pos := 9
|
|
|
|
|
|
|
|
len := int(bin[pos])
|
|
|
|
if 1+len >= binLen-pos {
|
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.ServerAddrStr = string(bin[pos : pos+len])
|
2018-04-01 16:35:32 +02:00
|
|
|
pos += len
|
2018-04-14 15:03:21 +02:00
|
|
|
if net.ParseIP(strings.TrimRight(strings.TrimLeft(stamp.ServerAddrStr, "["), "]")) != nil {
|
|
|
|
stamp.ServerAddrStr = fmt.Sprintf("%s:%d", stamp.ServerAddrStr, DefaultPort)
|
2018-04-01 16:35:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
len = int(bin[pos])
|
|
|
|
if 1+len >= binLen-pos {
|
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.ServerPk = bin[pos : pos+len]
|
2018-04-01 16:35:32 +02:00
|
|
|
pos += len
|
|
|
|
|
|
|
|
len = int(bin[pos])
|
|
|
|
if len >= binLen-pos {
|
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.ProviderName = string(bin[pos : pos+len])
|
2018-04-01 16:35:32 +02:00
|
|
|
pos += len
|
|
|
|
|
|
|
|
if pos != binLen {
|
|
|
|
return stamp, errors.New("Invalid stamp (garbage after end)")
|
|
|
|
}
|
|
|
|
return stamp, nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:38:31 +01:00
|
|
|
// id(u8)=0x02 props addrLen(1) serverAddr hashLen(1) hash providerNameLen(1) providerName pathLen(1) path
|
|
|
|
|
|
|
|
func newDoHServerStamp(bin []byte) (ServerStamp, error) {
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp := ServerStamp{Proto: StampProtoTypeDoH}
|
2018-02-04 11:04:29 +01:00
|
|
|
if len(bin) < 22 {
|
2018-03-02 02:17:07 +01:00
|
|
|
return stamp, errors.New("Stamp is too short")
|
2018-02-04 11:04:29 +01:00
|
|
|
}
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.Props = ServerInformalProperties(binary.LittleEndian.Uint64(bin[1:9]))
|
2018-01-26 20:38:31 +01:00
|
|
|
binLen := len(bin)
|
|
|
|
pos := 9
|
|
|
|
|
|
|
|
len := int(bin[pos])
|
2018-02-04 11:04:29 +01:00
|
|
|
if 1+len >= binLen-pos {
|
2018-01-26 20:38:31 +01:00
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.ServerAddrStr = string(bin[pos : pos+len])
|
2018-01-26 20:38:31 +01:00
|
|
|
pos += len
|
|
|
|
|
2018-01-31 18:16:54 +01:00
|
|
|
for {
|
|
|
|
vlen := int(bin[pos])
|
|
|
|
len = vlen & ^0x80
|
2018-02-04 11:04:29 +01:00
|
|
|
if 1+len >= binLen-pos {
|
2018-01-31 18:16:54 +01:00
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-02-05 11:30:10 +01:00
|
|
|
if len > 0 {
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.Hashes = append(stamp.Hashes, bin[pos:pos+len])
|
2018-02-05 11:30:10 +01:00
|
|
|
}
|
2018-01-31 18:16:54 +01:00
|
|
|
pos += len
|
|
|
|
if vlen&0x80 != 0x80 {
|
|
|
|
break
|
|
|
|
}
|
2018-01-26 20:38:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
len = int(bin[pos])
|
2018-02-04 11:04:29 +01:00
|
|
|
if 1+len >= binLen-pos {
|
2018-01-26 20:38:31 +01:00
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.ProviderName = string(bin[pos : pos+len])
|
2018-01-26 20:38:31 +01:00
|
|
|
pos += len
|
|
|
|
|
|
|
|
len = int(bin[pos])
|
|
|
|
if len >= binLen-pos {
|
|
|
|
return stamp, errors.New("Invalid stamp")
|
|
|
|
}
|
|
|
|
pos++
|
2018-04-14 15:03:21 +02:00
|
|
|
stamp.Path = string(bin[pos : pos+len])
|
2018-01-26 20:38:31 +01:00
|
|
|
pos += len
|
|
|
|
|
|
|
|
if pos != binLen {
|
|
|
|
return stamp, errors.New("Invalid stamp (garbage after end)")
|
|
|
|
}
|
2018-03-28 11:52:04 +02:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
if net.ParseIP(strings.TrimRight(strings.TrimLeft(stamp.ServerAddrStr, "["), "]")) != nil {
|
|
|
|
stamp.ServerAddrStr = fmt.Sprintf("%s:%d", stamp.ServerAddrStr, DefaultPort)
|
2018-03-28 11:52:04 +02:00
|
|
|
}
|
|
|
|
|
2018-01-26 20:38:31 +01:00
|
|
|
return stamp, nil
|
|
|
|
}
|
|
|
|
|
2018-01-20 13:56:26 +01:00
|
|
|
func (stamp *ServerStamp) String() string {
|
2018-04-14 15:03:21 +02:00
|
|
|
if stamp.Proto == StampProtoTypeDNSCrypt {
|
2018-04-01 16:35:32 +02:00
|
|
|
return stamp.dnsCryptString()
|
2018-04-14 15:03:21 +02:00
|
|
|
} else if stamp.Proto == StampProtoTypeDoH {
|
2018-01-26 20:38:31 +01:00
|
|
|
return stamp.dohString()
|
|
|
|
}
|
2018-04-14 16:30:22 +02:00
|
|
|
panic("Unsupported protocol")
|
2018-01-26 20:38:31 +01:00
|
|
|
}
|
|
|
|
|
2018-04-01 16:35:32 +02:00
|
|
|
func (stamp *ServerStamp) dnsCryptString() string {
|
|
|
|
bin := make([]uint8, 9)
|
|
|
|
bin[0] = uint8(StampProtoTypeDNSCrypt)
|
2018-04-14 15:03:21 +02:00
|
|
|
binary.LittleEndian.PutUint64(bin[1:9], uint64(stamp.Props))
|
2018-04-01 16:35:32 +02:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
serverAddrStr := stamp.ServerAddrStr
|
2018-04-01 16:35:32 +02:00
|
|
|
if strings.HasSuffix(serverAddrStr, ":"+strconv.Itoa(DefaultPort)) {
|
|
|
|
serverAddrStr = serverAddrStr[:len(serverAddrStr)-1-len(strconv.Itoa(DefaultPort))]
|
|
|
|
}
|
|
|
|
bin = append(bin, uint8(len(serverAddrStr)))
|
|
|
|
bin = append(bin, []uint8(serverAddrStr)...)
|
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
bin = append(bin, uint8(len(stamp.ServerPk)))
|
|
|
|
bin = append(bin, stamp.ServerPk...)
|
2018-04-01 16:35:32 +02:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
bin = append(bin, uint8(len(stamp.ProviderName)))
|
|
|
|
bin = append(bin, []uint8(stamp.ProviderName)...)
|
2018-04-01 16:35:32 +02:00
|
|
|
|
|
|
|
str := base64.RawURLEncoding.EncodeToString(bin)
|
|
|
|
|
|
|
|
return "sdns://" + str
|
|
|
|
}
|
|
|
|
|
2018-01-26 20:38:31 +01:00
|
|
|
func (stamp *ServerStamp) dohString() string {
|
|
|
|
bin := make([]uint8, 9)
|
|
|
|
bin[0] = uint8(StampProtoTypeDoH)
|
2018-04-14 15:03:21 +02:00
|
|
|
binary.LittleEndian.PutUint64(bin[1:9], uint64(stamp.Props))
|
2018-01-26 20:38:31 +01:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
serverAddrStr := stamp.ServerAddrStr
|
2018-02-02 15:07:12 +01:00
|
|
|
if strings.HasSuffix(serverAddrStr, ":"+strconv.Itoa(DefaultPort)) {
|
|
|
|
serverAddrStr = serverAddrStr[:len(serverAddrStr)-1-len(strconv.Itoa(DefaultPort))]
|
2018-01-30 16:10:46 +01:00
|
|
|
}
|
|
|
|
bin = append(bin, uint8(len(serverAddrStr)))
|
|
|
|
bin = append(bin, []uint8(serverAddrStr)...)
|
2018-01-26 20:38:31 +01:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
last := len(stamp.Hashes) - 1
|
|
|
|
for i, hash := range stamp.Hashes {
|
2018-01-31 18:16:54 +01:00
|
|
|
vlen := len(hash)
|
|
|
|
if i < last {
|
|
|
|
vlen |= 0x80
|
|
|
|
}
|
|
|
|
bin = append(bin, uint8(vlen))
|
|
|
|
bin = append(bin, hash...)
|
|
|
|
}
|
2018-01-26 20:38:31 +01:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
bin = append(bin, uint8(len(stamp.ProviderName)))
|
|
|
|
bin = append(bin, []uint8(stamp.ProviderName)...)
|
2018-01-26 20:38:31 +01:00
|
|
|
|
2018-04-14 15:03:21 +02:00
|
|
|
bin = append(bin, uint8(len(stamp.Path)))
|
|
|
|
bin = append(bin, []uint8(stamp.Path)...)
|
2018-01-26 20:38:31 +01:00
|
|
|
|
|
|
|
str := base64.RawURLEncoding.EncodeToString(bin)
|
|
|
|
|
|
|
|
return "sdns://" + str
|
|
|
|
}
|