dnssec.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. package dns
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/dsa"
  6. "crypto/ecdsa"
  7. "crypto/elliptic"
  8. _ "crypto/md5"
  9. "crypto/rand"
  10. "crypto/rsa"
  11. _ "crypto/sha1"
  12. _ "crypto/sha256"
  13. _ "crypto/sha512"
  14. "encoding/asn1"
  15. "encoding/hex"
  16. "math/big"
  17. "sort"
  18. "strings"
  19. "time"
  20. )
  21. // DNSSEC encryption algorithm codes.
  22. const (
  23. _ uint8 = iota
  24. RSAMD5
  25. DH
  26. DSA
  27. _ // Skip 4, RFC 6725, section 2.1
  28. RSASHA1
  29. DSANSEC3SHA1
  30. RSASHA1NSEC3SHA1
  31. RSASHA256
  32. _ // Skip 9, RFC 6725, section 2.1
  33. RSASHA512
  34. _ // Skip 11, RFC 6725, section 2.1
  35. ECCGOST
  36. ECDSAP256SHA256
  37. ECDSAP384SHA384
  38. INDIRECT uint8 = 252
  39. PRIVATEDNS uint8 = 253 // Private (experimental keys)
  40. PRIVATEOID uint8 = 254
  41. )
  42. // Map for algorithm names.
  43. var AlgorithmToString = map[uint8]string{
  44. RSAMD5: "RSAMD5",
  45. DH: "DH",
  46. DSA: "DSA",
  47. RSASHA1: "RSASHA1",
  48. DSANSEC3SHA1: "DSA-NSEC3-SHA1",
  49. RSASHA1NSEC3SHA1: "RSASHA1-NSEC3-SHA1",
  50. RSASHA256: "RSASHA256",
  51. RSASHA512: "RSASHA512",
  52. ECCGOST: "ECC-GOST",
  53. ECDSAP256SHA256: "ECDSAP256SHA256",
  54. ECDSAP384SHA384: "ECDSAP384SHA384",
  55. INDIRECT: "INDIRECT",
  56. PRIVATEDNS: "PRIVATEDNS",
  57. PRIVATEOID: "PRIVATEOID",
  58. }
  59. // Map of algorithm strings.
  60. var StringToAlgorithm = reverseInt8(AlgorithmToString)
  61. // Map of algorithm crypto hashes.
  62. var AlgorithmToHash = map[uint8]crypto.Hash{
  63. RSAMD5: crypto.MD5, // Deprecated in RFC 6725
  64. RSASHA1: crypto.SHA1,
  65. RSASHA1NSEC3SHA1: crypto.SHA1,
  66. RSASHA256: crypto.SHA256,
  67. ECDSAP256SHA256: crypto.SHA256,
  68. ECDSAP384SHA384: crypto.SHA384,
  69. RSASHA512: crypto.SHA512,
  70. }
  71. // DNSSEC hashing algorithm codes.
  72. const (
  73. _ uint8 = iota
  74. SHA1 // RFC 4034
  75. SHA256 // RFC 4509
  76. GOST94 // RFC 5933
  77. SHA384 // Experimental
  78. SHA512 // Experimental
  79. )
  80. // Map for hash names.
  81. var HashToString = map[uint8]string{
  82. SHA1: "SHA1",
  83. SHA256: "SHA256",
  84. GOST94: "GOST94",
  85. SHA384: "SHA384",
  86. SHA512: "SHA512",
  87. }
  88. // Map of hash strings.
  89. var StringToHash = reverseInt8(HashToString)
  90. // DNSKEY flag values.
  91. const (
  92. SEP = 1
  93. REVOKE = 1 << 7
  94. ZONE = 1 << 8
  95. )
  96. // The RRSIG needs to be converted to wireformat with some of
  97. // the rdata (the signature) missing. Use this struct to ease
  98. // the conversion (and re-use the pack/unpack functions).
  99. type rrsigWireFmt struct {
  100. TypeCovered uint16
  101. Algorithm uint8
  102. Labels uint8
  103. OrigTtl uint32
  104. Expiration uint32
  105. Inception uint32
  106. KeyTag uint16
  107. SignerName string `dns:"domain-name"`
  108. /* No Signature */
  109. }
  110. // Used for converting DNSKEY's rdata to wirefmt.
  111. type dnskeyWireFmt struct {
  112. Flags uint16
  113. Protocol uint8
  114. Algorithm uint8
  115. PublicKey string `dns:"base64"`
  116. /* Nothing is left out */
  117. }
  118. func divRoundUp(a, b int) int {
  119. return (a + b - 1) / b
  120. }
  121. // KeyTag calculates the keytag (or key-id) of the DNSKEY.
  122. func (k *DNSKEY) KeyTag() uint16 {
  123. if k == nil {
  124. return 0
  125. }
  126. var keytag int
  127. switch k.Algorithm {
  128. case RSAMD5:
  129. // Look at the bottom two bytes of the modules, which the last
  130. // item in the pubkey. We could do this faster by looking directly
  131. // at the base64 values. But I'm lazy.
  132. modulus, _ := fromBase64([]byte(k.PublicKey))
  133. if len(modulus) > 1 {
  134. x, _ := unpackUint16(modulus, len(modulus)-2)
  135. keytag = int(x)
  136. }
  137. default:
  138. keywire := new(dnskeyWireFmt)
  139. keywire.Flags = k.Flags
  140. keywire.Protocol = k.Protocol
  141. keywire.Algorithm = k.Algorithm
  142. keywire.PublicKey = k.PublicKey
  143. wire := make([]byte, DefaultMsgSize)
  144. n, err := PackStruct(keywire, wire, 0)
  145. if err != nil {
  146. return 0
  147. }
  148. wire = wire[:n]
  149. for i, v := range wire {
  150. if i&1 != 0 {
  151. keytag += int(v) // must be larger than uint32
  152. } else {
  153. keytag += int(v) << 8
  154. }
  155. }
  156. keytag += (keytag >> 16) & 0xFFFF
  157. keytag &= 0xFFFF
  158. }
  159. return uint16(keytag)
  160. }
  161. // ToDS converts a DNSKEY record to a DS record.
  162. func (k *DNSKEY) ToDS(h uint8) *DS {
  163. if k == nil {
  164. return nil
  165. }
  166. ds := new(DS)
  167. ds.Hdr.Name = k.Hdr.Name
  168. ds.Hdr.Class = k.Hdr.Class
  169. ds.Hdr.Rrtype = TypeDS
  170. ds.Hdr.Ttl = k.Hdr.Ttl
  171. ds.Algorithm = k.Algorithm
  172. ds.DigestType = h
  173. ds.KeyTag = k.KeyTag()
  174. keywire := new(dnskeyWireFmt)
  175. keywire.Flags = k.Flags
  176. keywire.Protocol = k.Protocol
  177. keywire.Algorithm = k.Algorithm
  178. keywire.PublicKey = k.PublicKey
  179. wire := make([]byte, DefaultMsgSize)
  180. n, err := PackStruct(keywire, wire, 0)
  181. if err != nil {
  182. return nil
  183. }
  184. wire = wire[:n]
  185. owner := make([]byte, 255)
  186. off, err1 := PackDomainName(strings.ToLower(k.Hdr.Name), owner, 0, nil, false)
  187. if err1 != nil {
  188. return nil
  189. }
  190. owner = owner[:off]
  191. // RFC4034:
  192. // digest = digest_algorithm( DNSKEY owner name | DNSKEY RDATA);
  193. // "|" denotes concatenation
  194. // DNSKEY RDATA = Flags | Protocol | Algorithm | Public Key.
  195. // digest buffer
  196. digest := append(owner, wire...) // another copy
  197. var hash crypto.Hash
  198. switch h {
  199. case SHA1:
  200. hash = crypto.SHA1
  201. case SHA256:
  202. hash = crypto.SHA256
  203. case SHA384:
  204. hash = crypto.SHA384
  205. case SHA512:
  206. hash = crypto.SHA512
  207. default:
  208. return nil
  209. }
  210. s := hash.New()
  211. s.Write(digest)
  212. ds.Digest = hex.EncodeToString(s.Sum(nil))
  213. return ds
  214. }
  215. // ToCDNSKEY converts a DNSKEY record to a CDNSKEY record.
  216. func (k *DNSKEY) ToCDNSKEY() *CDNSKEY {
  217. c := &CDNSKEY{DNSKEY: *k}
  218. c.Hdr = *k.Hdr.copyHeader()
  219. c.Hdr.Rrtype = TypeCDNSKEY
  220. return c
  221. }
  222. // ToCDS converts a DS record to a CDS record.
  223. func (d *DS) ToCDS() *CDS {
  224. c := &CDS{DS: *d}
  225. c.Hdr = *d.Hdr.copyHeader()
  226. c.Hdr.Rrtype = TypeCDS
  227. return c
  228. }
  229. // Sign signs an RRSet. The signature needs to be filled in with the values:
  230. // Inception, Expiration, KeyTag, SignerName and Algorithm. The rest is copied
  231. // from the RRset. Sign returns a non-nill error when the signing went OK.
  232. // There is no check if RRSet is a proper (RFC 2181) RRSet. If OrigTTL is non
  233. // zero, it is used as-is, otherwise the TTL of the RRset is used as the
  234. // OrigTTL.
  235. func (rr *RRSIG) Sign(k crypto.Signer, rrset []RR) error {
  236. if k == nil {
  237. return ErrPrivKey
  238. }
  239. // s.Inception and s.Expiration may be 0 (rollover etc.), the rest must be set
  240. if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
  241. return ErrKey
  242. }
  243. rr.Hdr.Rrtype = TypeRRSIG
  244. rr.Hdr.Name = rrset[0].Header().Name
  245. rr.Hdr.Class = rrset[0].Header().Class
  246. if rr.OrigTtl == 0 { // If set don't override
  247. rr.OrigTtl = rrset[0].Header().Ttl
  248. }
  249. rr.TypeCovered = rrset[0].Header().Rrtype
  250. rr.Labels = uint8(CountLabel(rrset[0].Header().Name))
  251. if strings.HasPrefix(rrset[0].Header().Name, "*") {
  252. rr.Labels-- // wildcard, remove from label count
  253. }
  254. sigwire := new(rrsigWireFmt)
  255. sigwire.TypeCovered = rr.TypeCovered
  256. sigwire.Algorithm = rr.Algorithm
  257. sigwire.Labels = rr.Labels
  258. sigwire.OrigTtl = rr.OrigTtl
  259. sigwire.Expiration = rr.Expiration
  260. sigwire.Inception = rr.Inception
  261. sigwire.KeyTag = rr.KeyTag
  262. // For signing, lowercase this name
  263. sigwire.SignerName = strings.ToLower(rr.SignerName)
  264. // Create the desired binary blob
  265. signdata := make([]byte, DefaultMsgSize)
  266. n, err := PackStruct(sigwire, signdata, 0)
  267. if err != nil {
  268. return err
  269. }
  270. signdata = signdata[:n]
  271. wire, err := rawSignatureData(rrset, rr)
  272. if err != nil {
  273. return err
  274. }
  275. signdata = append(signdata, wire...)
  276. hash, ok := AlgorithmToHash[rr.Algorithm]
  277. if !ok {
  278. return ErrAlg
  279. }
  280. h := hash.New()
  281. h.Write(signdata)
  282. signature, err := sign(k, h.Sum(nil), hash, rr.Algorithm)
  283. if err != nil {
  284. return err
  285. }
  286. rr.Signature = toBase64(signature)
  287. return nil
  288. }
  289. func sign(k crypto.Signer, hashed []byte, hash crypto.Hash, alg uint8) ([]byte, error) {
  290. signature, err := k.Sign(rand.Reader, hashed, hash)
  291. if err != nil {
  292. return nil, err
  293. }
  294. switch alg {
  295. case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512:
  296. return signature, nil
  297. case ECDSAP256SHA256, ECDSAP384SHA384:
  298. ecdsaSignature := &struct {
  299. R, S *big.Int
  300. }{}
  301. if _, err := asn1.Unmarshal(signature, ecdsaSignature); err != nil {
  302. return nil, err
  303. }
  304. var intlen int
  305. switch alg {
  306. case ECDSAP256SHA256:
  307. intlen = 32
  308. case ECDSAP384SHA384:
  309. intlen = 48
  310. }
  311. signature := intToBytes(ecdsaSignature.R, intlen)
  312. signature = append(signature, intToBytes(ecdsaSignature.S, intlen)...)
  313. return signature, nil
  314. // There is no defined interface for what a DSA backed crypto.Signer returns
  315. case DSA, DSANSEC3SHA1:
  316. // t := divRoundUp(divRoundUp(p.PublicKey.Y.BitLen(), 8)-64, 8)
  317. // signature := []byte{byte(t)}
  318. // signature = append(signature, intToBytes(r1, 20)...)
  319. // signature = append(signature, intToBytes(s1, 20)...)
  320. // rr.Signature = signature
  321. }
  322. return nil, ErrAlg
  323. }
  324. // Verify validates an RRSet with the signature and key. This is only the
  325. // cryptographic test, the signature validity period must be checked separately.
  326. // This function copies the rdata of some RRs (to lowercase domain names) for the validation to work.
  327. func (rr *RRSIG) Verify(k *DNSKEY, rrset []RR) error {
  328. // First the easy checks
  329. if !IsRRset(rrset) {
  330. return ErrRRset
  331. }
  332. if rr.KeyTag != k.KeyTag() {
  333. return ErrKey
  334. }
  335. if rr.Hdr.Class != k.Hdr.Class {
  336. return ErrKey
  337. }
  338. if rr.Algorithm != k.Algorithm {
  339. return ErrKey
  340. }
  341. if strings.ToLower(rr.SignerName) != strings.ToLower(k.Hdr.Name) {
  342. return ErrKey
  343. }
  344. if k.Protocol != 3 {
  345. return ErrKey
  346. }
  347. // IsRRset checked that we have at least one RR and that the RRs in
  348. // the set have consistent type, class, and name. Also check that type and
  349. // class matches the RRSIG record.
  350. if rrset[0].Header().Class != rr.Hdr.Class {
  351. return ErrRRset
  352. }
  353. if rrset[0].Header().Rrtype != rr.TypeCovered {
  354. return ErrRRset
  355. }
  356. // RFC 4035 5.3.2. Reconstructing the Signed Data
  357. // Copy the sig, except the rrsig data
  358. sigwire := new(rrsigWireFmt)
  359. sigwire.TypeCovered = rr.TypeCovered
  360. sigwire.Algorithm = rr.Algorithm
  361. sigwire.Labels = rr.Labels
  362. sigwire.OrigTtl = rr.OrigTtl
  363. sigwire.Expiration = rr.Expiration
  364. sigwire.Inception = rr.Inception
  365. sigwire.KeyTag = rr.KeyTag
  366. sigwire.SignerName = strings.ToLower(rr.SignerName)
  367. // Create the desired binary blob
  368. signeddata := make([]byte, DefaultMsgSize)
  369. n, err := PackStruct(sigwire, signeddata, 0)
  370. if err != nil {
  371. return err
  372. }
  373. signeddata = signeddata[:n]
  374. wire, err := rawSignatureData(rrset, rr)
  375. if err != nil {
  376. return err
  377. }
  378. signeddata = append(signeddata, wire...)
  379. sigbuf := rr.sigBuf() // Get the binary signature data
  380. if rr.Algorithm == PRIVATEDNS { // PRIVATEOID
  381. // TODO(miek)
  382. // remove the domain name and assume its ours?
  383. }
  384. hash, ok := AlgorithmToHash[rr.Algorithm]
  385. if !ok {
  386. return ErrAlg
  387. }
  388. switch rr.Algorithm {
  389. case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512, RSAMD5:
  390. // TODO(mg): this can be done quicker, ie. cache the pubkey data somewhere??
  391. pubkey := k.publicKeyRSA() // Get the key
  392. if pubkey == nil {
  393. return ErrKey
  394. }
  395. h := hash.New()
  396. h.Write(signeddata)
  397. return rsa.VerifyPKCS1v15(pubkey, hash, h.Sum(nil), sigbuf)
  398. case ECDSAP256SHA256, ECDSAP384SHA384:
  399. pubkey := k.publicKeyECDSA()
  400. if pubkey == nil {
  401. return ErrKey
  402. }
  403. // Split sigbuf into the r and s coordinates
  404. r := new(big.Int).SetBytes(sigbuf[:len(sigbuf)/2])
  405. s := new(big.Int).SetBytes(sigbuf[len(sigbuf)/2:])
  406. h := hash.New()
  407. h.Write(signeddata)
  408. if ecdsa.Verify(pubkey, h.Sum(nil), r, s) {
  409. return nil
  410. }
  411. return ErrSig
  412. default:
  413. return ErrAlg
  414. }
  415. }
  416. // ValidityPeriod uses RFC1982 serial arithmetic to calculate
  417. // if a signature period is valid. If t is the zero time, the
  418. // current time is taken other t is. Returns true if the signature
  419. // is valid at the given time, otherwise returns false.
  420. func (rr *RRSIG) ValidityPeriod(t time.Time) bool {
  421. var utc int64
  422. if t.IsZero() {
  423. utc = time.Now().UTC().Unix()
  424. } else {
  425. utc = t.UTC().Unix()
  426. }
  427. modi := (int64(rr.Inception) - utc) / year68
  428. mode := (int64(rr.Expiration) - utc) / year68
  429. ti := int64(rr.Inception) + (modi * year68)
  430. te := int64(rr.Expiration) + (mode * year68)
  431. return ti <= utc && utc <= te
  432. }
  433. // Return the signatures base64 encodedig sigdata as a byte slice.
  434. func (rr *RRSIG) sigBuf() []byte {
  435. sigbuf, err := fromBase64([]byte(rr.Signature))
  436. if err != nil {
  437. return nil
  438. }
  439. return sigbuf
  440. }
  441. // publicKeyRSA returns the RSA public key from a DNSKEY record.
  442. func (k *DNSKEY) publicKeyRSA() *rsa.PublicKey {
  443. keybuf, err := fromBase64([]byte(k.PublicKey))
  444. if err != nil {
  445. return nil
  446. }
  447. // RFC 2537/3110, section 2. RSA Public KEY Resource Records
  448. // Length is in the 0th byte, unless its zero, then it
  449. // it in bytes 1 and 2 and its a 16 bit number
  450. explen := uint16(keybuf[0])
  451. keyoff := 1
  452. if explen == 0 {
  453. explen = uint16(keybuf[1])<<8 | uint16(keybuf[2])
  454. keyoff = 3
  455. }
  456. pubkey := new(rsa.PublicKey)
  457. pubkey.N = big.NewInt(0)
  458. shift := uint64((explen - 1) * 8)
  459. expo := uint64(0)
  460. for i := int(explen - 1); i > 0; i-- {
  461. expo += uint64(keybuf[keyoff+i]) << shift
  462. shift -= 8
  463. }
  464. // Remainder
  465. expo += uint64(keybuf[keyoff])
  466. if expo > 2<<31 {
  467. // Larger expo than supported.
  468. // println("dns: F5 primes (or larger) are not supported")
  469. return nil
  470. }
  471. pubkey.E = int(expo)
  472. pubkey.N.SetBytes(keybuf[keyoff+int(explen):])
  473. return pubkey
  474. }
  475. // publicKeyECDSA returns the Curve public key from the DNSKEY record.
  476. func (k *DNSKEY) publicKeyECDSA() *ecdsa.PublicKey {
  477. keybuf, err := fromBase64([]byte(k.PublicKey))
  478. if err != nil {
  479. return nil
  480. }
  481. pubkey := new(ecdsa.PublicKey)
  482. switch k.Algorithm {
  483. case ECDSAP256SHA256:
  484. pubkey.Curve = elliptic.P256()
  485. if len(keybuf) != 64 {
  486. // wrongly encoded key
  487. return nil
  488. }
  489. case ECDSAP384SHA384:
  490. pubkey.Curve = elliptic.P384()
  491. if len(keybuf) != 96 {
  492. // Wrongly encoded key
  493. return nil
  494. }
  495. }
  496. pubkey.X = big.NewInt(0)
  497. pubkey.X.SetBytes(keybuf[:len(keybuf)/2])
  498. pubkey.Y = big.NewInt(0)
  499. pubkey.Y.SetBytes(keybuf[len(keybuf)/2:])
  500. return pubkey
  501. }
  502. func (k *DNSKEY) publicKeyDSA() *dsa.PublicKey {
  503. keybuf, err := fromBase64([]byte(k.PublicKey))
  504. if err != nil {
  505. return nil
  506. }
  507. if len(keybuf) < 22 {
  508. return nil
  509. }
  510. t, keybuf := int(keybuf[0]), keybuf[1:]
  511. size := 64 + t*8
  512. q, keybuf := keybuf[:20], keybuf[20:]
  513. if len(keybuf) != 3*size {
  514. return nil
  515. }
  516. p, keybuf := keybuf[:size], keybuf[size:]
  517. g, y := keybuf[:size], keybuf[size:]
  518. pubkey := new(dsa.PublicKey)
  519. pubkey.Parameters.Q = big.NewInt(0).SetBytes(q)
  520. pubkey.Parameters.P = big.NewInt(0).SetBytes(p)
  521. pubkey.Parameters.G = big.NewInt(0).SetBytes(g)
  522. pubkey.Y = big.NewInt(0).SetBytes(y)
  523. return pubkey
  524. }
  525. type wireSlice [][]byte
  526. func (p wireSlice) Len() int { return len(p) }
  527. func (p wireSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  528. func (p wireSlice) Less(i, j int) bool {
  529. _, ioff, _ := UnpackDomainName(p[i], 0)
  530. _, joff, _ := UnpackDomainName(p[j], 0)
  531. return bytes.Compare(p[i][ioff+10:], p[j][joff+10:]) < 0
  532. }
  533. // Return the raw signature data.
  534. func rawSignatureData(rrset []RR, s *RRSIG) (buf []byte, err error) {
  535. wires := make(wireSlice, len(rrset))
  536. for i, r := range rrset {
  537. r1 := r.copy()
  538. r1.Header().Ttl = s.OrigTtl
  539. labels := SplitDomainName(r1.Header().Name)
  540. // 6.2. Canonical RR Form. (4) - wildcards
  541. if len(labels) > int(s.Labels) {
  542. // Wildcard
  543. r1.Header().Name = "*." + strings.Join(labels[len(labels)-int(s.Labels):], ".") + "."
  544. }
  545. // RFC 4034: 6.2. Canonical RR Form. (2) - domain name to lowercase
  546. r1.Header().Name = strings.ToLower(r1.Header().Name)
  547. // 6.2. Canonical RR Form. (3) - domain rdata to lowercase.
  548. // NS, MD, MF, CNAME, SOA, MB, MG, MR, PTR,
  549. // HINFO, MINFO, MX, RP, AFSDB, RT, SIG, PX, NXT, NAPTR, KX,
  550. // SRV, DNAME, A6
  551. //
  552. // RFC 6840 - Clarifications and Implementation Notes for DNS Security (DNSSEC):
  553. // Section 6.2 of [RFC4034] also erroneously lists HINFO as a record
  554. // that needs conversion to lowercase, and twice at that. Since HINFO
  555. // records contain no domain names, they are not subject to case
  556. // conversion.
  557. switch x := r1.(type) {
  558. case *NS:
  559. x.Ns = strings.ToLower(x.Ns)
  560. case *CNAME:
  561. x.Target = strings.ToLower(x.Target)
  562. case *SOA:
  563. x.Ns = strings.ToLower(x.Ns)
  564. x.Mbox = strings.ToLower(x.Mbox)
  565. case *MB:
  566. x.Mb = strings.ToLower(x.Mb)
  567. case *MG:
  568. x.Mg = strings.ToLower(x.Mg)
  569. case *MR:
  570. x.Mr = strings.ToLower(x.Mr)
  571. case *PTR:
  572. x.Ptr = strings.ToLower(x.Ptr)
  573. case *MINFO:
  574. x.Rmail = strings.ToLower(x.Rmail)
  575. x.Email = strings.ToLower(x.Email)
  576. case *MX:
  577. x.Mx = strings.ToLower(x.Mx)
  578. case *NAPTR:
  579. x.Replacement = strings.ToLower(x.Replacement)
  580. case *KX:
  581. x.Exchanger = strings.ToLower(x.Exchanger)
  582. case *SRV:
  583. x.Target = strings.ToLower(x.Target)
  584. case *DNAME:
  585. x.Target = strings.ToLower(x.Target)
  586. }
  587. // 6.2. Canonical RR Form. (5) - origTTL
  588. wire := make([]byte, r1.len()+1) // +1 to be safe(r)
  589. off, err1 := PackRR(r1, wire, 0, nil, false)
  590. if err1 != nil {
  591. return nil, err1
  592. }
  593. wire = wire[:off]
  594. wires[i] = wire
  595. }
  596. sort.Sort(wires)
  597. for i, wire := range wires {
  598. if i > 0 && bytes.Equal(wire, wires[i-1]) {
  599. continue
  600. }
  601. buf = append(buf, wire...)
  602. }
  603. return buf, nil
  604. }