dnssec.go 18 KB

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