sig0.go 5.0 KB

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