dns/tlsa.go

80 lines
1.9 KiB
Go

package dns
import (
"crypto/sha256"
"crypto/sha512"
"crypto/x509"
"encoding/hex"
"io"
"net"
"strconv"
)
// TLSA support functions
// certToTLSACert returns the hex data suitable for inclusion in a TLSA record
func certToTLSACert(selector, matchingType uint8, cert *x509.Certificate) string {
switch matchingType {
case 0:
switch selector {
case 0:
return hex.EncodeToString(cert.Raw)
case 1:
return hex.EncodeToString(cert.RawSubjectPublicKeyInfo)
}
case 1:
h := sha256.New()
switch selector {
case 0:
return hex.EncodeToString(cert.Raw)
case 1:
io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
return hex.EncodeToString(h.Sum(nil))
}
case 2:
h := sha512.New()
switch selector {
case 0:
return hex.EncodeToString(cert.Raw)
case 1:
io.WriteString(h, string(cert.RawSubjectPublicKeyInfo))
return hex.EncodeToString(h.Sum(nil))
}
}
return ""
}
// Sign creates a TLSA record from a SSL certificate.
func (r *RR_TLSA) Sign(usage, selector, matchingType int, cert *x509.Certificate) error {
r.Hdr.Rrtype = TypeTLSA
r.Usage = uint8(usage)
r.Selector = uint8(selector)
r.MatchingType = uint8(matchingType)
r.Certificate = certToTLSACert(r.Selector, r.MatchingType, cert)
return nil
}
// Verify verifies a TLSA record against a SSL certificate. If it is OK
// a nil error is returned.
func (r *RR_TLSA) Verify(cert *x509.Certificate) error {
if r.Certificate == certToTLSACert(r.Selector, r.MatchingType, cert) {
return nil
}
return ErrSig // ErrSig, really?
}
// TLSAName returns the ownername of a TLSA resource record as per the
// rules specified in RFC 6698, Section 3. When an erros occurs the
// empty string is returned.
func TLSAName(name, service, network string) string {
if !IsFqdn(name) {
return ""
}
p, e := net.LookupPort(network, service)
if e != nil {
return ""
}
return "_" + strconv.Itoa(p) + "_" + network + "." + name
}