dnssec.go 19 KB

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