Debian Codegraph @

dnssec_keygen.go

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