dnssec_keygen.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package dns
  2. import (
  3. "crypto"
  4. "crypto/dsa"
  5. "crypto/ecdsa"
  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 DSA, DSANSEC3SHA1:
  20. if bits != 1024 {
  21. return nil, ErrKeySize
  22. }
  23. case RSAMD5, RSASHA1, RSASHA256, RSASHA1NSEC3SHA1:
  24. if bits < 512 || bits > 4096 {
  25. return nil, ErrKeySize
  26. }
  27. case RSASHA512:
  28. if bits < 1024 || bits > 4096 {
  29. return nil, ErrKeySize
  30. }
  31. case ECDSAP256SHA256:
  32. if bits != 256 {
  33. return nil, ErrKeySize
  34. }
  35. case ECDSAP384SHA384:
  36. if bits != 384 {
  37. return nil, ErrKeySize
  38. }
  39. }
  40. switch k.Algorithm {
  41. case DSA, DSANSEC3SHA1:
  42. params := new(dsa.Parameters)
  43. if err := dsa.GenerateParameters(params, rand.Reader, dsa.L1024N160); err != nil {
  44. return nil, err
  45. }
  46. priv := new(dsa.PrivateKey)
  47. priv.PublicKey.Parameters = *params
  48. err := dsa.GenerateKey(priv, rand.Reader)
  49. if err != nil {
  50. return nil, err
  51. }
  52. k.setPublicKeyDSA(params.Q, params.P, params.G, priv.PublicKey.Y)
  53. return priv, nil
  54. case RSAMD5, RSASHA1, RSASHA256, RSASHA512, RSASHA1NSEC3SHA1:
  55. priv, err := rsa.GenerateKey(rand.Reader, bits)
  56. if err != nil {
  57. return nil, err
  58. }
  59. k.setPublicKeyRSA(priv.PublicKey.E, priv.PublicKey.N)
  60. return priv, nil
  61. case ECDSAP256SHA256, ECDSAP384SHA384:
  62. var c elliptic.Curve
  63. switch k.Algorithm {
  64. case ECDSAP256SHA256:
  65. c = elliptic.P256()
  66. case ECDSAP384SHA384:
  67. c = elliptic.P384()
  68. }
  69. priv, err := ecdsa.GenerateKey(c, rand.Reader)
  70. if err != nil {
  71. return nil, err
  72. }
  73. k.setPublicKeyECDSA(priv.PublicKey.X, priv.PublicKey.Y)
  74. return priv, nil
  75. default:
  76. return nil, ErrAlg
  77. }
  78. }
  79. // Set the public key (the value E and N)
  80. func (k *DNSKEY) setPublicKeyRSA(_E int, _N *big.Int) bool {
  81. if _E == 0 || _N == nil {
  82. return false
  83. }
  84. buf := exponentToBuf(_E)
  85. buf = append(buf, _N.Bytes()...)
  86. k.PublicKey = toBase64(buf)
  87. return true
  88. }
  89. // Set the public key for Elliptic Curves
  90. func (k *DNSKEY) setPublicKeyECDSA(_X, _Y *big.Int) bool {
  91. if _X == nil || _Y == nil {
  92. return false
  93. }
  94. var intlen int
  95. switch k.Algorithm {
  96. case ECDSAP256SHA256:
  97. intlen = 32
  98. case ECDSAP384SHA384:
  99. intlen = 48
  100. }
  101. k.PublicKey = toBase64(curveToBuf(_X, _Y, intlen))
  102. return true
  103. }
  104. // Set the public key for DSA
  105. func (k *DNSKEY) setPublicKeyDSA(_Q, _P, _G, _Y *big.Int) bool {
  106. if _Q == nil || _P == nil || _G == nil || _Y == nil {
  107. return false
  108. }
  109. buf := dsaToBuf(_Q, _P, _G, _Y)
  110. k.PublicKey = toBase64(buf)
  111. return true
  112. }
  113. // Set the public key (the values E and N) for RSA
  114. // RFC 3110: Section 2. RSA Public KEY Resource Records
  115. func exponentToBuf(_E int) []byte {
  116. var buf []byte
  117. i := big.NewInt(int64(_E))
  118. if len(i.Bytes()) < 256 {
  119. buf = make([]byte, 1)
  120. buf[0] = uint8(len(i.Bytes()))
  121. } else {
  122. buf = make([]byte, 3)
  123. buf[0] = 0
  124. buf[1] = uint8(len(i.Bytes()) >> 8)
  125. buf[2] = uint8(len(i.Bytes()))
  126. }
  127. buf = append(buf, i.Bytes()...)
  128. return buf
  129. }
  130. // Set the public key for X and Y for Curve. The two
  131. // values are just concatenated.
  132. func curveToBuf(_X, _Y *big.Int, intlen int) []byte {
  133. buf := intToBytes(_X, intlen)
  134. buf = append(buf, intToBytes(_Y, intlen)...)
  135. return buf
  136. }
  137. // Set the public key for X and Y for Curve. The two
  138. // values are just concatenated.
  139. func dsaToBuf(_Q, _P, _G, _Y *big.Int) []byte {
  140. t := divRoundUp(divRoundUp(_G.BitLen(), 8)-64, 8)
  141. buf := []byte{byte(t)}
  142. buf = append(buf, intToBytes(_Q, 20)...)
  143. buf = append(buf, intToBytes(_P, 64+t*8)...)
  144. buf = append(buf, intToBytes(_G, 64+t*8)...)
  145. buf = append(buf, intToBytes(_Y, 64+t*8)...)
  146. return buf
  147. }