dnssec.go 18 KB

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