| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha256" |
| 5 |
"crypto/sha512" |
| 6 |
"crypto/x509" |
| 7 |
"encoding/hex" |
| 8 |
"errors" |
| 9 |
) |
| 10 |
|
| 11 |
// certificateToDANE converts a certificate to a hex string as used in the TLSA or SMIMEA records. |
| 12 |
func certificateToDANE(selector, matchingType uint8, cert *x509.Certificate) (string, error) { |
| 13 |
// move to dnsutils? |
| 14 |
switch matchingType { |
| 15 |
case 0: |
| 16 |
switch selector { |
| 17 |
case 0: |
| 18 |
return hex.EncodeToString(cert.Raw), nil |
| 19 |
case 1: |
| 20 |
return hex.EncodeToString(cert.RawSubjectPublicKeyInfo), nil |
| 21 |
} |
| 22 |
case 1: |
| 23 |
h := sha256.New() |
| 24 |
switch selector { |
| 25 |
case 0: |
| 26 |
h.Write(cert.Raw) |
| 27 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 28 |
case 1: |
| 29 |
h.Write(cert.RawSubjectPublicKeyInfo) |
| 30 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 31 |
} |
| 32 |
case 2: |
| 33 |
h := sha512.New() |
| 34 |
switch selector { |
| 35 |
case 0: |
| 36 |
h.Write(cert.Raw) |
| 37 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 38 |
case 1: |
| 39 |
h.Write(cert.RawSubjectPublicKeyInfo) |
| 40 |
return hex.EncodeToString(h.Sum(nil)), nil |
| 41 |
} |
| 42 |
} |
| 43 |
return "", errors.New("dns: bad MatchingType or Selector") |
| 44 |
} |