smimea.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package dns
  2. import (
  3. "crypto/sha256"
  4. "crypto/x509"
  5. "encoding/hex"
  6. )
  7. // Sign creates a SMIMEA record from an SSL certificate.
  8. func (r *SMIMEA) Sign(usage, selector, matchingType int, cert *x509.Certificate) (err error) {
  9. r.Hdr.Rrtype = TypeSMIMEA
  10. r.Usage = uint8(usage)
  11. r.Selector = uint8(selector)
  12. r.MatchingType = uint8(matchingType)
  13. r.Certificate, err = CertificateToDANE(r.Selector, r.MatchingType, cert)
  14. return err
  15. }
  16. // Verify verifies a SMIMEA record against an SSL certificate. If it is OK
  17. // a nil error is returned.
  18. func (r *SMIMEA) Verify(cert *x509.Certificate) error {
  19. c, err := CertificateToDANE(r.Selector, r.MatchingType, cert)
  20. if err != nil {
  21. return err // Not also ErrSig?
  22. }
  23. if r.Certificate == c {
  24. return nil
  25. }
  26. return ErrSig // ErrSig, really?
  27. }
  28. // SMIMEAName returns the ownername of a SMIMEA resource record as per the
  29. // format specified in RFC 'draft-ietf-dane-smime-12' Section 2 and 3
  30. func SMIMEAName(email, domain string) (string, error) {
  31. hasher := sha256.New()
  32. hasher.Write([]byte(email))
  33. // RFC Section 3: "The local-part is hashed using the SHA2-256
  34. // algorithm with the hash truncated to 28 octets and
  35. // represented in its hexadecimal representation to become the
  36. // left-most label in the prepared domain name"
  37. return hex.EncodeToString(hasher.Sum(nil)[:28]) + "." + "_smimecert." + domain, nil
  38. }