dnssec.go 19 KB

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