dns/keygen.go

113 lines
3.4 KiB
Go
Raw Normal View History

2011-01-11 02:10:15 +11:00
package dns
import (
2011-07-24 07:43:43 +10:00
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
2011-12-10 07:45:57 +11:00
"crypto/rsa"
"math/big"
"strconv"
2011-01-11 02:10:15 +11:00
)
2011-01-27 19:29:11 +11:00
// Empty interface that is used as a wrapper around all possible
2011-01-18 07:10:48 +11:00
// private key implementations from the crypto package.
type PrivateKey interface{}
2011-09-09 03:35:02 +10:00
// Generate generates a DNSKEY of the given bit size.
2011-01-27 19:29:11 +11:00
// The public part is put inside the DNSKEY record.
// The Algorithm in the key must be set as this will define
// what kind of DNSKEY will be generated.
2011-09-09 03:35:02 +10:00
// The ECDSA algorithms imply a fixed keysize, in that case
// bits should be set to the size of the algorithm.
2011-11-03 09:06:54 +11:00
func (r *RR_DNSKEY) Generate(bits int) (PrivateKey, error) {
switch r.Algorithm {
2011-12-10 07:45:57 +11:00
case RSAMD5, RSASHA1, RSASHA256, RSASHA1NSEC3SHA1:
if bits < 512 || bits > 4096 {
2011-03-25 21:19:35 +11:00
return nil, ErrKeySize
}
2012-03-19 08:47:06 +11:00
// TODO: check these limits
2011-07-09 01:27:44 +10:00
case RSASHA512:
if bits < 1024 || bits > 4096 {
2011-03-25 21:19:35 +11:00
return nil, ErrKeySize
}
2012-03-03 01:28:22 +11:00
case ECDSAP256SHA256Y:
2011-07-24 07:43:43 +10:00
if bits != 256 {
return nil, ErrKeySize
}
2012-03-03 01:28:22 +11:00
case ECDSAP384SHA384Y:
2011-07-24 07:43:43 +10:00
if bits != 384 {
return nil, ErrKeySize
}
}
2011-01-11 02:10:15 +11:00
switch r.Algorithm {
2011-11-03 09:06:54 +11:00
case RSAMD5, RSASHA1, RSASHA256, RSASHA512, RSASHA1NSEC3SHA1:
priv, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
2011-07-24 07:43:43 +10:00
r.setPublicKeyRSA(priv.PublicKey.E, priv.PublicKey.N)
return priv, nil
2012-03-03 01:28:22 +11:00
case ECDSAP256SHA256Y, ECDSAP384SHA384Y:
2012-01-20 22:24:05 +11:00
var c elliptic.Curve
2011-07-24 07:43:43 +10:00
switch r.Algorithm {
2012-03-03 01:28:22 +11:00
case ECDSAP256SHA256Y:
2011-07-24 07:43:43 +10:00
c = elliptic.P256()
2012-03-03 01:28:22 +11:00
case ECDSAP384SHA384Y:
2011-07-24 07:43:43 +10:00
c = elliptic.P384()
}
priv, err := ecdsa.GenerateKey(c, rand.Reader)
if err != nil {
return nil, err
}
r.setPublicKeyCurve(priv.PublicKey.X, priv.PublicKey.Y)
return priv, nil
2011-07-08 18:41:07 +10:00
default:
return nil, ErrAlg
}
return nil, nil // Dummy return
2011-01-11 02:10:15 +11:00
}
2011-01-15 22:18:18 +11:00
2012-01-16 02:09:17 +11:00
// PrivateKeyString converts a PrivateKey to a string. This
2011-01-18 07:10:48 +11:00
// string has the same format as the private-key-file of BIND9 (Private-key-format: v1.3).
// It needs some info from the key (hashing, keytag), so its a method of the RR_DNSKEY.
2012-01-16 02:09:17 +11:00
func (r *RR_DNSKEY) PrivateKeyString(p PrivateKey) (s string) {
switch t := p.(type) {
case *rsa.PrivateKey:
2012-01-12 23:01:43 +11:00
algorithm := strconv.Itoa(int(r.Algorithm)) + " (" + Alg_str[r.Algorithm] + ")"
modulus := unpackBase64(t.PublicKey.N.Bytes())
2011-01-18 06:29:40 +11:00
e := big.NewInt(int64(t.PublicKey.E))
2011-01-18 04:13:52 +11:00
publicExponent := unpackBase64(e.Bytes())
privateExponent := unpackBase64(t.D.Bytes())
prime1 := unpackBase64(t.Primes[0].Bytes())
prime2 := unpackBase64(t.Primes[1].Bytes())
// Calculate Exponent1/2 and Coefficient as per: http://en.wikipedia.org/wiki/RSA#Using_the_Chinese_remainder_algorithm
// and from: http://code.google.com/p/go/issues/detail?id=987
one := big.NewInt(1)
minusone := big.NewInt(-1)
p_1 := big.NewInt(0).Sub(t.Primes[0], one)
q_1 := big.NewInt(0).Sub(t.Primes[1], one)
exp1 := big.NewInt(0).Mod(t.D, p_1)
exp2 := big.NewInt(0).Mod(t.D, q_1)
coeff := big.NewInt(0).Exp(t.Primes[1], minusone, t.Primes[0])
2011-01-15 22:18:18 +11:00
exponent1 := unpackBase64(exp1.Bytes())
exponent2 := unpackBase64(exp2.Bytes())
coefficient := unpackBase64(coeff.Bytes())
2011-01-15 22:18:18 +11:00
s = "Private-key-format: v1.3\n" +
"Algorithm: " + algorithm + "\n" +
"Modules: " + modulus + "\n" +
"PublicExponent: " + publicExponent + "\n" +
"PrivateExponent: " + privateExponent + "\n" +
"Prime1: " + prime1 + "\n" +
"Prime2: " + prime2 + "\n" +
"Exponent1: " + exponent1 + "\n" +
"Exponent2: " + exponent2 + "\n" +
"Coefficient: " + coefficient + "\n"
2011-07-24 07:43:43 +10:00
case *ecdsa.PrivateKey:
2012-01-16 02:09:17 +11:00
s = "TODO"
}
return
2011-01-15 22:18:18 +11:00
}