dnssec.go 18 KB

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