| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/mldsa" |
| 8 |
"crypto/rsa" |
| 9 |
"math/big" |
| 10 |
"strconv" |
| 11 |
|
| 12 |
"codeberg.org/miekg/dns/internal/unpack" |
| 13 |
) |
| 14 |
|
| 15 |
const format = "Private-key-format: v1.3\n" |
| 16 |
|
| 17 |
var bigIntOne = big.NewInt(1) |
| 18 |
|
| 19 |
// PrivateKeyString converts a PrivateKey to a string. This string has the same |
| 20 |
// format as the private-key-file of BIND9 (Private-key-format: v1.3). |
| 21 |
// It needs some info from the key (the algorithm), so its a method of the DNSKEY. |
| 22 |
// It supports *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey and *mldsa.PrivateKey. |
| 23 |
func (k *DNSKEY) PrivateKeyString(p crypto.PrivateKey) string { |
| 24 |
algorithm := strconv.Itoa(int(k.Algorithm)) |
| 25 |
algorithm += " (" + AlgorithmToString[k.Algorithm] + ")" |
| 26 |
|
| 27 |
switch p := p.(type) { |
| 28 |
case *rsa.PrivateKey: |
| 29 |
modulus := unpack.Base64(p.N.Bytes()) |
| 30 |
e := big.NewInt(int64(p.E)) |
| 31 |
publicExponent := unpack.Base64(e.Bytes()) |
| 32 |
privateExponent := unpack.Base64(p.D.Bytes()) |
| 33 |
prime1 := unpack.Base64(p.Primes[0].Bytes()) |
| 34 |
prime2 := unpack.Base64(p.Primes[1].Bytes()) |
| 35 |
// Calculate Exponent1/2 and Coefficient as per: http://en.wikipedia.org/wiki/RSA#Using_the_Chinese_remainder_algorithm |
| 36 |
// and from: http://code.google.com/p/go/issues/detail?id=987 |
| 37 |
p1 := new(big.Int).Sub(p.Primes[0], bigIntOne) |
| 38 |
q1 := new(big.Int).Sub(p.Primes[1], bigIntOne) |
| 39 |
exp1 := new(big.Int).Mod(p.D, p1) |
| 40 |
exp2 := new(big.Int).Mod(p.D, q1) |
| 41 |
coeff := new(big.Int).ModInverse(p.Primes[1], p.Primes[0]) |
| 42 |
|
| 43 |
exponent1 := unpack.Base64(exp1.Bytes()) |
| 44 |
exponent2 := unpack.Base64(exp2.Bytes()) |
| 45 |
coefficient := unpack.Base64(coeff.Bytes()) |
| 46 |
|
| 47 |
return format + |
| 48 |
"Algorithm: " + algorithm + "\n" + |
| 49 |
"Modulus: " + modulus + "\n" + |
| 50 |
"PublicExponent: " + publicExponent + "\n" + |
| 51 |
"PrivateExponent: " + privateExponent + "\n" + |
| 52 |
"Prime1: " + prime1 + "\n" + |
| 53 |
"Prime2: " + prime2 + "\n" + |
| 54 |
"Exponent1: " + exponent1 + "\n" + |
| 55 |
"Exponent2: " + exponent2 + "\n" + |
| 56 |
"Coefficient: " + coefficient + "\n" |
| 57 |
|
| 58 |
case *ecdsa.PrivateKey: |
| 59 |
var intlen int |
| 60 |
switch k.Algorithm { |
| 61 |
case ECDSAP256SHA256: |
| 62 |
intlen = 32 |
| 63 |
case ECDSAP384SHA384: |
| 64 |
intlen = 48 |
| 65 |
} |
| 66 |
private := unpack.Base64(intToBytes(p.D, intlen)) |
| 67 |
return format + |
| 68 |
"Algorithm: " + algorithm + "\n" + |
| 69 |
"PrivateKey: " + private + "\n" |
| 70 |
|
| 71 |
case ed25519.PrivateKey: |
| 72 |
private := unpack.Base64(p.Seed()) |
| 73 |
return format + |
| 74 |
"Algorithm: " + algorithm + "\n" + |
| 75 |
"PrivateKey: " + private + "\n" |
| 76 |
|
| 77 |
case *mldsa.PrivateKey: |
| 78 |
private := unpack.Base64(p.Bytes()) |
| 79 |
return format + |
| 80 |
"Algorithm: " + algorithm + "\n" + |
| 81 |
"PrivateKey: " + private + "\n" |
| 82 |
|
| 83 |
default: |
| 84 |
return "" |
| 85 |
} |
| 86 |
} |