dnssec.go 19 KB

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