dnssec_keyscan.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package dns
  2. import (
  3. "bufio"
  4. "crypto"
  5. "crypto/ecdsa"
  6. "crypto/ed25519"
  7. "crypto/rsa"
  8. "io"
  9. "math/big"
  10. "strconv"
  11. "strings"
  12. )
  13. // NewPrivateKey returns a PrivateKey by parsing the string s.
  14. // s should be in the same form of the BIND private key files.
  15. func (k *DNSKEY) NewPrivateKey(s string) (crypto.PrivateKey, error) {
  16. if s == "" || s[len(s)-1] != '\n' { // We need a closing newline
  17. return k.ReadPrivateKey(strings.NewReader(s+"\n"), "")
  18. }
  19. return k.ReadPrivateKey(strings.NewReader(s), "")
  20. }
  21. // ReadPrivateKey reads a private key from the io.Reader q. The string file is
  22. // only used in error reporting.
  23. // The public key must be known, because some cryptographic algorithms embed
  24. // the public inside the privatekey.
  25. func (k *DNSKEY) ReadPrivateKey(q io.Reader, file string) (crypto.PrivateKey, error) {
  26. m, err := parseKey(q, file)
  27. if m == nil {
  28. return nil, err
  29. }
  30. if _, ok := m["private-key-format"]; !ok {
  31. return nil, ErrPrivKey
  32. }
  33. if m["private-key-format"] != "v1.2" && m["private-key-format"] != "v1.3" {
  34. return nil, ErrPrivKey
  35. }
  36. // TODO(mg): check if the pubkey matches the private key
  37. algo, err := strconv.ParseUint(strings.SplitN(m["algorithm"], " ", 2)[0], 10, 8)
  38. if err != nil {
  39. return nil, ErrPrivKey
  40. }
  41. switch uint8(algo) {
  42. case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512:
  43. priv, err := readPrivateKeyRSA(m)
  44. if err != nil {
  45. return nil, err
  46. }
  47. pub := k.publicKeyRSA()
  48. if pub == nil {
  49. return nil, ErrKey
  50. }
  51. priv.PublicKey = *pub
  52. return priv, nil
  53. case ECDSAP256SHA256, ECDSAP384SHA384:
  54. priv, err := readPrivateKeyECDSA(m)
  55. if err != nil {
  56. return nil, err
  57. }
  58. pub := k.publicKeyECDSA()
  59. if pub == nil {
  60. return nil, ErrKey
  61. }
  62. priv.PublicKey = *pub
  63. return priv, nil
  64. case ED25519:
  65. return readPrivateKeyED25519(m)
  66. default:
  67. return nil, ErrAlg
  68. }
  69. }
  70. // Read a private key (file) string and create a public key. Return the private key.
  71. func readPrivateKeyRSA(m map[string]string) (*rsa.PrivateKey, error) {
  72. p := new(rsa.PrivateKey)
  73. p.Primes = []*big.Int{nil, nil}
  74. for k, v := range m {
  75. switch k {
  76. case "modulus", "publicexponent", "privateexponent", "prime1", "prime2":
  77. v1, err := fromBase64([]byte(v))
  78. if err != nil {
  79. return nil, err
  80. }
  81. switch k {
  82. case "modulus":
  83. p.PublicKey.N = new(big.Int).SetBytes(v1)
  84. case "publicexponent":
  85. i := new(big.Int).SetBytes(v1)
  86. p.PublicKey.E = int(i.Int64()) // int64 should be large enough
  87. case "privateexponent":
  88. p.D = new(big.Int).SetBytes(v1)
  89. case "prime1":
  90. p.Primes[0] = new(big.Int).SetBytes(v1)
  91. case "prime2":
  92. p.Primes[1] = new(big.Int).SetBytes(v1)
  93. }
  94. case "exponent1", "exponent2", "coefficient":
  95. // not used in Go (yet)
  96. case "created", "publish", "activate":
  97. // not used in Go (yet)
  98. }
  99. }
  100. return p, nil
  101. }
  102. func readPrivateKeyECDSA(m map[string]string) (*ecdsa.PrivateKey, error) {
  103. p := new(ecdsa.PrivateKey)
  104. p.D = new(big.Int)
  105. // TODO: validate that the required flags are present
  106. for k, v := range m {
  107. switch k {
  108. case "privatekey":
  109. v1, err := fromBase64([]byte(v))
  110. if err != nil {
  111. return nil, err
  112. }
  113. p.D.SetBytes(v1)
  114. case "created", "publish", "activate":
  115. /* not used in Go (yet) */
  116. }
  117. }
  118. return p, nil
  119. }
  120. func readPrivateKeyED25519(m map[string]string) (ed25519.PrivateKey, error) {
  121. var p ed25519.PrivateKey
  122. // TODO: validate that the required flags are present
  123. for k, v := range m {
  124. switch k {
  125. case "privatekey":
  126. p1, err := fromBase64([]byte(v))
  127. if err != nil {
  128. return nil, err
  129. }
  130. if len(p1) != ed25519.SeedSize {
  131. return nil, ErrPrivKey
  132. }
  133. p = ed25519.NewKeyFromSeed(p1)
  134. case "created", "publish", "activate":
  135. /* not used in Go (yet) */
  136. }
  137. }
  138. return p, nil
  139. }
  140. // parseKey reads a private key from r. It returns a map[string]string,
  141. // with the key-value pairs, or an error when the file is not correct.
  142. func parseKey(r io.Reader, file string) (map[string]string, error) {
  143. m := make(map[string]string)
  144. var k string
  145. c := newKLexer(r)
  146. for l, ok := c.Next(); ok; l, ok = c.Next() {
  147. // It should alternate
  148. switch l.value {
  149. case zKey:
  150. k = l.token
  151. case zValue:
  152. if k == "" {
  153. return nil, &ParseError{file, "no private key seen", l}
  154. }
  155. m[strings.ToLower(k)] = l.token
  156. k = ""
  157. }
  158. }
  159. // Surface any read errors from r.
  160. if err := c.Err(); err != nil {
  161. return nil, &ParseError{file: file, err: err.Error()}
  162. }
  163. return m, nil
  164. }
  165. type klexer struct {
  166. br io.ByteReader
  167. readErr error
  168. line int
  169. column int
  170. key bool
  171. eol bool // end-of-line
  172. }
  173. func newKLexer(r io.Reader) *klexer {
  174. br, ok := r.(io.ByteReader)
  175. if !ok {
  176. br = bufio.NewReaderSize(r, 1024)
  177. }
  178. return &klexer{
  179. br: br,
  180. line: 1,
  181. key: true,
  182. }
  183. }
  184. func (kl *klexer) Err() error {
  185. if kl.readErr == io.EOF {
  186. return nil
  187. }
  188. return kl.readErr
  189. }
  190. // readByte returns the next byte from the input
  191. func (kl *klexer) readByte() (byte, bool) {
  192. if kl.readErr != nil {
  193. return 0, false
  194. }
  195. c, err := kl.br.ReadByte()
  196. if err != nil {
  197. kl.readErr = err
  198. return 0, false
  199. }
  200. // delay the newline handling until the next token is delivered,
  201. // fixes off-by-one errors when reporting a parse error.
  202. if kl.eol {
  203. kl.line++
  204. kl.column = 0
  205. kl.eol = false
  206. }
  207. if c == '\n' {
  208. kl.eol = true
  209. } else {
  210. kl.column++
  211. }
  212. return c, true
  213. }
  214. func (kl *klexer) Next() (lex, bool) {
  215. var (
  216. l lex
  217. str strings.Builder
  218. commt bool
  219. )
  220. for x, ok := kl.readByte(); ok; x, ok = kl.readByte() {
  221. l.line, l.column = kl.line, kl.column
  222. switch x {
  223. case ':':
  224. if commt || !kl.key {
  225. break
  226. }
  227. kl.key = false
  228. // Next token is a space, eat it
  229. kl.readByte()
  230. l.value = zKey
  231. l.token = str.String()
  232. return l, true
  233. case ';':
  234. commt = true
  235. case '\n':
  236. if commt {
  237. // Reset a comment
  238. commt = false
  239. }
  240. if kl.key && str.Len() == 0 {
  241. // ignore empty lines
  242. break
  243. }
  244. kl.key = true
  245. l.value = zValue
  246. l.token = str.String()
  247. return l, true
  248. default:
  249. if commt {
  250. break
  251. }
  252. str.WriteByte(x)
  253. }
  254. }
  255. if kl.readErr != nil && kl.readErr != io.EOF {
  256. // Don't return any tokens after a read error occurs.
  257. return lex{value: zEOF}, false
  258. }
  259. if str.Len() > 0 {
  260. // Send remainder
  261. l.value = zValue
  262. l.token = str.String()
  263. return l, true
  264. }
  265. return lex{value: zEOF}, false
  266. }