sig0.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package dns
  2. import (
  3. "crypto"
  4. "crypto/dsa"
  5. "crypto/ecdsa"
  6. "crypto/rsa"
  7. "encoding/binary"
  8. "math/big"
  9. "strings"
  10. "time"
  11. )
  12. // Sign signs a dns.Msg. It fills the signature with the appropriate data.
  13. // The SIG record should have the SignerName, KeyTag, Algorithm, Inception
  14. // and Expiration set.
  15. func (rr *SIG) Sign(k crypto.Signer, m *Msg) ([]byte, error) {
  16. if k == nil {
  17. return nil, ErrPrivKey
  18. }
  19. if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
  20. return nil, ErrKey
  21. }
  22. rr.Header().Rrtype = TypeSIG
  23. rr.Header().Class = ClassANY
  24. rr.Header().Ttl = 0
  25. rr.Header().Name = "."
  26. rr.OrigTtl = 0
  27. rr.TypeCovered = 0
  28. rr.Labels = 0
  29. buf := make([]byte, m.Len()+rr.len())
  30. mbuf, err := m.PackBuffer(buf)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if &buf[0] != &mbuf[0] {
  35. return nil, ErrBuf
  36. }
  37. off, err := PackRR(rr, buf, len(mbuf), nil, false)
  38. if err != nil {
  39. return nil, err
  40. }
  41. buf = buf[:off:cap(buf)]
  42. hash, ok := AlgorithmToHash[rr.Algorithm]
  43. if !ok {
  44. return nil, ErrAlg
  45. }
  46. hasher := hash.New()
  47. // Write SIG rdata
  48. hasher.Write(buf[len(mbuf)+1+2+2+4+2:])
  49. // Write message
  50. hasher.Write(buf[:len(mbuf)])
  51. signature, err := sign(k, hasher.Sum(nil), hash, rr.Algorithm)
  52. if err != nil {
  53. return nil, err
  54. }
  55. rr.Signature = toBase64(signature)
  56. buf = append(buf, signature...)
  57. if len(buf) > int(^uint16(0)) {
  58. return nil, ErrBuf
  59. }
  60. // Adjust sig data length
  61. rdoff := len(mbuf) + 1 + 2 + 2 + 4
  62. rdlen := binary.BigEndian.Uint16(buf[rdoff:])
  63. rdlen += uint16(len(signature))
  64. binary.BigEndian.PutUint16(buf[rdoff:], rdlen)
  65. // Adjust additional count
  66. adc := binary.BigEndian.Uint16(buf[10:])
  67. adc++
  68. binary.BigEndian.PutUint16(buf[10:], adc)
  69. return buf, nil
  70. }
  71. // Verify validates the message buf using the key k.
  72. // It's assumed that buf is a valid message from which rr was unpacked.
  73. func (rr *SIG) Verify(k *KEY, buf []byte) error {
  74. if k == nil {
  75. return ErrKey
  76. }
  77. if rr.KeyTag == 0 || len(rr.SignerName) == 0 || rr.Algorithm == 0 {
  78. return ErrKey
  79. }
  80. var hash crypto.Hash
  81. switch rr.Algorithm {
  82. case DSA, RSASHA1:
  83. hash = crypto.SHA1
  84. case RSASHA256, ECDSAP256SHA256:
  85. hash = crypto.SHA256
  86. case ECDSAP384SHA384:
  87. hash = crypto.SHA384
  88. case RSASHA512:
  89. hash = crypto.SHA512
  90. default:
  91. return ErrAlg
  92. }
  93. hasher := hash.New()
  94. buflen := len(buf)
  95. qdc := binary.BigEndian.Uint16(buf[4:])
  96. anc := binary.BigEndian.Uint16(buf[6:])
  97. auc := binary.BigEndian.Uint16(buf[8:])
  98. adc := binary.BigEndian.Uint16(buf[10:])
  99. offset := 12
  100. var err error
  101. for i := uint16(0); i < qdc && offset < buflen; i++ {
  102. _, offset, err = UnpackDomainName(buf, offset)
  103. if err != nil {
  104. return err
  105. }
  106. // Skip past Type and Class
  107. offset += 2 + 2
  108. }
  109. for i := uint16(1); i < anc+auc+adc && offset < buflen; i++ {
  110. _, offset, err = UnpackDomainName(buf, offset)
  111. if err != nil {
  112. return err
  113. }
  114. // Skip past Type, Class and TTL
  115. offset += 2 + 2 + 4
  116. if offset+1 >= buflen {
  117. continue
  118. }
  119. var rdlen uint16
  120. rdlen = binary.BigEndian.Uint16(buf[offset:])
  121. offset += 2
  122. offset += int(rdlen)
  123. }
  124. if offset >= buflen {
  125. return &Error{err: "overflowing unpacking signed message"}
  126. }
  127. // offset should be just prior to SIG
  128. bodyend := offset
  129. // owner name SHOULD be root
  130. _, offset, err = UnpackDomainName(buf, offset)
  131. if err != nil {
  132. return err
  133. }
  134. // Skip Type, Class, TTL, RDLen
  135. offset += 2 + 2 + 4 + 2
  136. sigstart := offset
  137. // Skip Type Covered, Algorithm, Labels, Original TTL
  138. offset += 2 + 1 + 1 + 4
  139. if offset+4+4 >= buflen {
  140. return &Error{err: "overflow unpacking signed message"}
  141. }
  142. expire := binary.BigEndian.Uint32(buf[offset:])
  143. offset += 4
  144. incept := binary.BigEndian.Uint32(buf[offset:])
  145. offset += 4
  146. now := uint32(time.Now().Unix())
  147. if now < incept || now > expire {
  148. return ErrTime
  149. }
  150. // Skip key tag
  151. offset += 2
  152. var signername string
  153. signername, offset, err = UnpackDomainName(buf, offset)
  154. if err != nil {
  155. return err
  156. }
  157. // If key has come from the DNS name compression might
  158. // have mangled the case of the name
  159. if strings.ToLower(signername) != strings.ToLower(k.Header().Name) {
  160. return &Error{err: "signer name doesn't match key name"}
  161. }
  162. sigend := offset
  163. hasher.Write(buf[sigstart:sigend])
  164. hasher.Write(buf[:10])
  165. hasher.Write([]byte{
  166. byte((adc - 1) << 8),
  167. byte(adc - 1),
  168. })
  169. hasher.Write(buf[12:bodyend])
  170. hashed := hasher.Sum(nil)
  171. sig := buf[sigend:]
  172. switch k.Algorithm {
  173. case DSA:
  174. pk := k.publicKeyDSA()
  175. sig = sig[1:]
  176. r := big.NewInt(0)
  177. r.SetBytes(sig[:len(sig)/2])
  178. s := big.NewInt(0)
  179. s.SetBytes(sig[len(sig)/2:])
  180. if pk != nil {
  181. if dsa.Verify(pk, hashed, r, s) {
  182. return nil
  183. }
  184. return ErrSig
  185. }
  186. case RSASHA1, RSASHA256, RSASHA512:
  187. pk := k.publicKeyRSA()
  188. if pk != nil {
  189. return rsa.VerifyPKCS1v15(pk, hash, hashed, sig)
  190. }
  191. case ECDSAP256SHA256, ECDSAP384SHA384:
  192. pk := k.publicKeyECDSA()
  193. r := big.NewInt(0)
  194. r.SetBytes(sig[:len(sig)/2])
  195. s := big.NewInt(0)
  196. s.SetBytes(sig[len(sig)/2:])
  197. if pk != nil {
  198. if ecdsa.Verify(pk, hashed, r, s) {
  199. return nil
  200. }
  201. return ErrSig
  202. }
  203. }
  204. return ErrKeyAlg
  205. }