dnssec_keyscan.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package dns
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/dsa"
  6. "crypto/ecdsa"
  7. "crypto/rsa"
  8. "io"
  9. "math/big"
  10. "strconv"
  11. "strings"
  12. "golang.org/x/crypto/ed25519"
  13. )
  14. // NewPrivateKey returns a PrivateKey by parsing the string s.
  15. // s should be in the same form of the BIND private key files.
  16. func (k *DNSKEY) NewPrivateKey(s string) (crypto.PrivateKey, error) {
  17. if s == "" || s[len(s)-1] != '\n' { // We need a closing newline
  18. return k.ReadPrivateKey(strings.NewReader(s+"\n"), "")
  19. }
  20. return k.ReadPrivateKey(strings.NewReader(s), "")
  21. }
  22. // ReadPrivateKey reads a private key from the io.Reader q. The string file is
  23. // only used in error reporting.
  24. // The public key must be known, because some cryptographic algorithms embed
  25. // the public inside the privatekey.
  26. func (k *DNSKEY) ReadPrivateKey(q io.Reader, file string) (crypto.PrivateKey, error) {
  27. m, err := parseKey(q, file)
  28. if m == nil {
  29. return nil, err
  30. }
  31. if _, ok := m["private-key-format"]; !ok {
  32. return nil, ErrPrivKey
  33. }
  34. if m["private-key-format"] != "v1.2" && m["private-key-format"] != "v1.3" {
  35. return nil, ErrPrivKey
  36. }
  37. // TODO(mg): check if the pubkey matches the private key
  38. algo, err := strconv.ParseUint(strings.SplitN(m["algorithm"], " ", 2)[0], 10, 8)
  39. if err != nil {
  40. return nil, ErrPrivKey
  41. }
  42. switch uint8(algo) {
  43. case DSA:
  44. priv, err := readPrivateKeyDSA(m)
  45. if err != nil {
  46. return nil, err
  47. }
  48. pub := k.publicKeyDSA()
  49. if pub == nil {
  50. return nil, ErrKey
  51. }
  52. priv.PublicKey = *pub
  53. return priv, nil
  54. case RSAMD5:
  55. fallthrough
  56. case RSASHA1:
  57. fallthrough
  58. case RSASHA1NSEC3SHA1:
  59. fallthrough
  60. case RSASHA256:
  61. fallthrough
  62. case RSASHA512:
  63. priv, err := readPrivateKeyRSA(m)
  64. if err != nil {
  65. return nil, err
  66. }
  67. pub := k.publicKeyRSA()
  68. if pub == nil {
  69. return nil, ErrKey
  70. }
  71. priv.PublicKey = *pub
  72. return priv, nil
  73. case ECCGOST:
  74. return nil, ErrPrivKey
  75. case ECDSAP256SHA256:
  76. fallthrough
  77. case ECDSAP384SHA384:
  78. priv, err := readPrivateKeyECDSA(m)
  79. if err != nil {
  80. return nil, err
  81. }
  82. pub := k.publicKeyECDSA()
  83. if pub == nil {
  84. return nil, ErrKey
  85. }
  86. priv.PublicKey = *pub
  87. return priv, nil
  88. case ED25519:
  89. return readPrivateKeyED25519(m)
  90. default:
  91. return nil, ErrPrivKey
  92. }
  93. }
  94. // Read a private key (file) string and create a public key. Return the private key.
  95. func readPrivateKeyRSA(m map[string]string) (*rsa.PrivateKey, error) {
  96. p := new(rsa.PrivateKey)
  97. p.Primes = []*big.Int{nil, nil}
  98. for k, v := range m {
  99. switch k {
  100. case "modulus", "publicexponent", "privateexponent", "prime1", "prime2":
  101. v1, err := fromBase64([]byte(v))
  102. if err != nil {
  103. return nil, err
  104. }
  105. switch k {
  106. case "modulus":
  107. p.PublicKey.N = big.NewInt(0)
  108. p.PublicKey.N.SetBytes(v1)
  109. case "publicexponent":
  110. i := big.NewInt(0)
  111. i.SetBytes(v1)
  112. p.PublicKey.E = int(i.Int64()) // int64 should be large enough
  113. case "privateexponent":
  114. p.D = big.NewInt(0)
  115. p.D.SetBytes(v1)
  116. case "prime1":
  117. p.Primes[0] = big.NewInt(0)
  118. p.Primes[0].SetBytes(v1)
  119. case "prime2":
  120. p.Primes[1] = big.NewInt(0)
  121. p.Primes[1].SetBytes(v1)
  122. }
  123. case "exponent1", "exponent2", "coefficient":
  124. // not used in Go (yet)
  125. case "created", "publish", "activate":
  126. // not used in Go (yet)
  127. }
  128. }
  129. return p, nil
  130. }
  131. func readPrivateKeyDSA(m map[string]string) (*dsa.PrivateKey, error) {
  132. p := new(dsa.PrivateKey)
  133. p.X = big.NewInt(0)
  134. for k, v := range m {
  135. switch k {
  136. case "private_value(x)":
  137. v1, err := fromBase64([]byte(v))
  138. if err != nil {
  139. return nil, err
  140. }
  141. p.X.SetBytes(v1)
  142. case "created", "publish", "activate":
  143. /* not used in Go (yet) */
  144. }
  145. }
  146. return p, nil
  147. }
  148. func readPrivateKeyECDSA(m map[string]string) (*ecdsa.PrivateKey, error) {
  149. p := new(ecdsa.PrivateKey)
  150. p.D = big.NewInt(0)
  151. // TODO: validate that the required flags are present
  152. for k, v := range m {
  153. switch k {
  154. case "privatekey":
  155. v1, err := fromBase64([]byte(v))
  156. if err != nil {
  157. return nil, err
  158. }
  159. p.D.SetBytes(v1)
  160. case "created", "publish", "activate":
  161. /* not used in Go (yet) */
  162. }
  163. }
  164. return p, nil
  165. }
  166. func readPrivateKeyED25519(m map[string]string) (ed25519.PrivateKey, error) {
  167. var p ed25519.PrivateKey
  168. // TODO: validate that the required flags are present
  169. for k, v := range m {
  170. switch k {
  171. case "privatekey":
  172. p1, err := fromBase64([]byte(v))
  173. if err != nil {
  174. return nil, err
  175. }
  176. if len(p1) != 32 {
  177. return nil, ErrPrivKey
  178. }
  179. // RFC 8080 and Golang's x/crypto/ed25519 differ as to how the
  180. // private keys are represented. RFC 8080 specifies that private
  181. // keys be stored solely as the seed value (p1 above) while the
  182. // ed25519 package represents them as the seed value concatenated
  183. // to the public key, which is derived from the seed value.
  184. //
  185. // ed25519.GenerateKey reads exactly 32 bytes from the passed in
  186. // io.Reader and uses them as the seed. It also derives the
  187. // public key and produces a compatible private key.
  188. _, p, err = ed25519.GenerateKey(bytes.NewReader(p1))
  189. if err != nil {
  190. return nil, err
  191. }
  192. case "created", "publish", "activate":
  193. /* not used in Go (yet) */
  194. }
  195. }
  196. return p, nil
  197. }
  198. // parseKey reads a private key from r. It returns a map[string]string,
  199. // with the key-value pairs, or an error when the file is not correct.
  200. func parseKey(r io.Reader, file string) (map[string]string, error) {
  201. s, cancel := scanInit(r)
  202. m := make(map[string]string)
  203. c := make(chan lex)
  204. k := ""
  205. defer func() {
  206. cancel()
  207. // zlexer can send up to two tokens, the next one and possibly 1 remainders.
  208. // Do a non-blocking read.
  209. _, ok := <-c
  210. _, ok = <-c
  211. if !ok {
  212. // too bad
  213. }
  214. }()
  215. // Start the lexer
  216. go klexer(s, c)
  217. for l := range c {
  218. // It should alternate
  219. switch l.value {
  220. case zKey:
  221. k = l.token
  222. case zValue:
  223. if k == "" {
  224. return nil, &ParseError{file, "no private key seen", l}
  225. }
  226. //println("Setting", strings.ToLower(k), "to", l.token, "b")
  227. m[strings.ToLower(k)] = l.token
  228. k = ""
  229. }
  230. }
  231. return m, nil
  232. }
  233. // klexer scans the sourcefile and returns tokens on the channel c.
  234. func klexer(s *scan, c chan lex) {
  235. var l lex
  236. str := "" // Hold the current read text
  237. commt := false
  238. key := true
  239. x, err := s.tokenText()
  240. defer close(c)
  241. for err == nil {
  242. l.column = s.position.Column
  243. l.line = s.position.Line
  244. switch x {
  245. case ':':
  246. if commt {
  247. break
  248. }
  249. l.token = str
  250. if key {
  251. l.value = zKey
  252. c <- l
  253. // Next token is a space, eat it
  254. s.tokenText()
  255. key = false
  256. str = ""
  257. } else {
  258. l.value = zValue
  259. }
  260. case ';':
  261. commt = true
  262. case '\n':
  263. if commt {
  264. // Reset a comment
  265. commt = false
  266. }
  267. l.value = zValue
  268. l.token = str
  269. c <- l
  270. str = ""
  271. commt = false
  272. key = true
  273. default:
  274. if commt {
  275. break
  276. }
  277. str += string(x)
  278. }
  279. x, err = s.tokenText()
  280. }
  281. if len(str) > 0 {
  282. // Send remainder
  283. l.token = str
  284. l.value = zValue
  285. c <- l
  286. }
  287. }