dnssec_keygen.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package dns
  2. import (
  3. "crypto"
  4. "crypto/ecdsa"
  5. "crypto/ed25519"
  6. "crypto/elliptic"
  7. "crypto/rand"
  8. "crypto/rsa"
  9. "math/big"
  10. )
  11. // Generate generates a DNSKEY of the given bit size.
  12. // The public part is put inside the DNSKEY record.
  13. // The Algorithm in the key must be set as this will define
  14. // what kind of DNSKEY will be generated.
  15. // The ECDSA algorithms imply a fixed keysize, in that case
  16. // bits should be set to the size of the algorithm.
  17. func (k *DNSKEY) Generate(bits int) (crypto.PrivateKey, error) {
  18. switch k.Algorithm {
  19. case RSASHA1, RSASHA256, RSASHA1NSEC3SHA1:
  20. if bits < 512 || bits > 4096 {
  21. return nil, ErrKeySize
  22. }
  23. case RSASHA512:
  24. if bits < 1024 || bits > 4096 {
  25. return nil, ErrKeySize
  26. }
  27. case ECDSAP256SHA256:
  28. if bits != 256 {
  29. return nil, ErrKeySize
  30. }
  31. case ECDSAP384SHA384:
  32. if bits != 384 {
  33. return nil, ErrKeySize
  34. }
  35. case ED25519:
  36. if bits != 256 {
  37. return nil, ErrKeySize
  38. }
  39. default:
  40. return nil, ErrAlg
  41. }
  42. switch k.Algorithm {
  43. case RSASHA1, RSASHA256, RSASHA512, RSASHA1NSEC3SHA1:
  44. priv, err := rsa.GenerateKey(rand.Reader, bits)
  45. if err != nil {
  46. return nil, err
  47. }
  48. k.setPublicKeyRSA(priv.PublicKey.E, priv.PublicKey.N)
  49. return priv, nil
  50. case ECDSAP256SHA256, ECDSAP384SHA384:
  51. var c elliptic.Curve
  52. switch k.Algorithm {
  53. case ECDSAP256SHA256:
  54. c = elliptic.P256()
  55. case ECDSAP384SHA384:
  56. c = elliptic.P384()
  57. }
  58. priv, err := ecdsa.GenerateKey(c, rand.Reader)
  59. if err != nil {
  60. return nil, err
  61. }
  62. k.setPublicKeyECDSA(priv.PublicKey.X, priv.PublicKey.Y)
  63. return priv, nil
  64. case ED25519:
  65. pub, priv, err := ed25519.GenerateKey(rand.Reader)
  66. if err != nil {
  67. return nil, err
  68. }
  69. k.setPublicKeyED25519(pub)
  70. return priv, nil
  71. default:
  72. return nil, ErrAlg
  73. }
  74. }
  75. // Set the public key (the value E and N)
  76. func (k *DNSKEY) setPublicKeyRSA(_E int, _N *big.Int) bool {
  77. if _E == 0 || _N == nil {
  78. return false
  79. }
  80. buf := exponentToBuf(_E)
  81. buf = append(buf, _N.Bytes()...)
  82. k.PublicKey = toBase64(buf)
  83. return true
  84. }
  85. // Set the public key for Elliptic Curves
  86. func (k *DNSKEY) setPublicKeyECDSA(_X, _Y *big.Int) bool {
  87. if _X == nil || _Y == nil {
  88. return false
  89. }
  90. var intlen int
  91. switch k.Algorithm {
  92. case ECDSAP256SHA256:
  93. intlen = 32
  94. case ECDSAP384SHA384:
  95. intlen = 48
  96. }
  97. k.PublicKey = toBase64(curveToBuf(_X, _Y, intlen))
  98. return true
  99. }
  100. // Set the public key for Ed25519
  101. func (k *DNSKEY) setPublicKeyED25519(_K ed25519.PublicKey) bool {
  102. if _K == nil {
  103. return false
  104. }
  105. k.PublicKey = toBase64(_K)
  106. return true
  107. }
  108. // Set the public key (the values E and N) for RSA
  109. // RFC 3110: Section 2. RSA Public KEY Resource Records
  110. func exponentToBuf(_E int) []byte {
  111. var buf []byte
  112. i := big.NewInt(int64(_E)).Bytes()
  113. if len(i) < 256 {
  114. buf = make([]byte, 1, 1+len(i))
  115. buf[0] = uint8(len(i))
  116. } else {
  117. buf = make([]byte, 3, 3+len(i))
  118. buf[0] = 0
  119. buf[1] = uint8(len(i) >> 8)
  120. buf[2] = uint8(len(i))
  121. }
  122. buf = append(buf, i...)
  123. return buf
  124. }
  125. // Set the public key for X and Y for Curve. The two
  126. // values are just concatenated.
  127. func curveToBuf(_X, _Y *big.Int, intlen int) []byte {
  128. buf := intToBytes(_X, intlen)
  129. buf = append(buf, intToBytes(_Y, intlen)...)
  130. return buf
  131. }