dnssec_keyscan.go 6.4 KB

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