Debian Codegraph @

dnssec_keyscan.go

raw
1 package dns
2
3 import (
4 "bufio"
5 "crypto"
6 "crypto/ecdsa"
7 "crypto/ed25519"
8 "crypto/mldsa"
9 "crypto/rsa"
10 "fmt"
11 "io"
12 "math/big"
13 "strconv"
14 "strings"
15
16 "codeberg.org/miekg/dns/internal/dnslex"
17 "codeberg.org/miekg/dns/internal/pack"
18 )
19
20 // NewPrivate returns a crypto.PrivateKey by parsing the string s.
21 // s should be in the same form of the BIND private key files.
22 func (k *DNSKEY) NewPrivate(s string) (crypto.PrivateKey, error) {
23 return k.readPrivate(strings.NewReader(s), "")
24 }
25
26 // readPrivate reads a private key from the io.Reader q. The string file is only used in error reporting.
27 // The public key must be known, because some cryptographic algorithms embed
28 // the public inside the privatekey.
29 func (k *DNSKEY) readPrivate(q io.Reader, file string) (crypto.PrivateKey, error) {
30 m, err := parseKey(q, file)
31 if m == nil {
32 return nil, err
33 }
34 if _, ok := m["private-key-format"]; !ok {
35 return nil, fmt.Errorf("private-key-format not found")
36 }
37 if m["private-key-format"] != "v1.2" && m["private-key-format"] != "v1.3" {
38 return nil, fmt.Errorf("private-key-format v1.2 or v.1.3 not found")
39 }
40
41 algostr, _, _ := strings.Cut(m["algorithm"], " ")
42 algo, err := strconv.ParseUint(algostr, 10, 8)
43 if err != nil {
44 return nil, err
45 }
46 switch uint8(algo) {
47 case RSASHA1, RSASHA1NSEC3SHA1, RSASHA256, RSASHA512:
48 priv, err := readPrivateKeyRSA(m)
49 if err != nil {
50 return nil, err
51 }
52 pub := k.publicKeyRSA()
53 if pub == nil {
54 return nil, ErrKey
55 }
56 priv.PublicKey = *pub
57 return priv, nil
58 case ECDSAP256SHA256, ECDSAP384SHA384:
59 priv, err := readPrivateKeyECDSA(m)
60 if err != nil {
61 return nil, err
62 }
63 pub := k.publicKeyECDSA()
64 if pub == nil {
65 return nil, ErrKey
66 }
67 priv.PublicKey = *pub
68 return priv, nil
69 case ED25519:
70 return readPrivateKeyED25519(m)
71 case MLDSA44:
72 return readPrivateKeyMLDSA44(m)
73 default:
74 return nil, ErrAlg
75 }
76 }
77
78 // Read a private key (file) string and create a public key. Return the private key.
79 func readPrivateKeyRSA(m map[string]string) (*rsa.PrivateKey, error) {
80 p := new(rsa.PrivateKey)
81 p.Primes = []*big.Int{nil, nil}
82 for k, v := range m {
83 switch k {
84 case "modulus", "publicexponent", "privateexponent", "prime1", "prime2":
85 v1, err := pack.Base64([]byte(v))
86 if err != nil {
87 return nil, err
88 }
89 switch k {
90 case "modulus":
91 p.N = new(big.Int).SetBytes(v1)
92 case "publicexponent":
93 i := new(big.Int).SetBytes(v1)
94 p.E = int(i.Int64()) // int64 should be large enough
95 case "privateexponent":
96 p.D = new(big.Int).SetBytes(v1)
97 case "prime1":
98 p.Primes[0] = new(big.Int).SetBytes(v1)
99 case "prime2":
100 p.Primes[1] = new(big.Int).SetBytes(v1)
101 }
102 case "exponent1", "exponent2", "coefficient":
103 // not used in Go (yet)
104 case "created", "publish", "activate":
105 // not used in Go (yet)
106 }
107 }
108 return p, nil
109 }
110
111 func readPrivateKeyECDSA(m map[string]string) (*ecdsa.PrivateKey, error) {
112 p := new(ecdsa.PrivateKey)
113 p.D = new(big.Int)
114 for k, v := range m {
115 switch k {
116 case "privatekey":
117 v1, err := pack.Base64([]byte(v))
118 if err != nil {
119 return nil, err
120 }
121 p.D.SetBytes(v1)
122 case "created", "publish", "activate":
123 /* not used in Go (yet) */
124 }
125 }
126 return p, nil
127 }
128
129 func readPrivateKeyED25519(m map[string]string) (ed25519.PrivateKey, error) {
130 var p ed25519.PrivateKey
131 for k, v := range m {
132 switch k {
133 case "privatekey":
134 p1, err := pack.Base64([]byte(v))
135 if err != nil {
136 return nil, err
137 }
138 if len(p1) != ed25519.SeedSize {
139 return nil, fmt.Errorf("ed25519 seed size error")
140 }
141 p = ed25519.NewKeyFromSeed(p1)
142 case "created", "publish", "activate":
143 /* not used in Go (yet) */
144 }
145 }
146 return p, nil
147 }
148
149 func readPrivateKeyMLDSA44(m map[string]string) (*mldsa.PrivateKey, error) {
150 var p *mldsa.PrivateKey
151 for k, v := range m {
152 switch k {
153 case "privatekey":
154 p1, err := pack.Base64([]byte(v))
155 if err != nil {
156 return nil, err
157 }
158 if len(p1) != mldsa.PrivateKeySize {
159 return nil, fmt.Errorf("mldsa seed size error")
160 }
161 if p, err = mldsa.NewPrivateKey(mldsa.MLDSA44(), p1); err != nil {
162 return nil, err
163 }
164 case "created", "publish", "activate":
165 /* not used in Go (yet) */
166 }
167 }
168 return p, nil
169 }
170
171 // parseKey reads a private key from r. It returns a map[string]string,
172 // with the key-value pairs, or an error when the file is not correct.
173 func parseKey(r io.Reader, file string) (map[string]string, error) {
174 m := make(map[string]string)
175 var k string
176
177 c := newKLexer(r)
178
179 for l, ok := c.Next(); ok; l, ok = c.Next() {
180 switch l.Value {
181 case zKey:
182 k = l.Token
183 case zValue:
184 if k == "" {
185 return nil, &ParseError{file: file, err: "no private key seen", lex: l}
186 }
187 m[strings.ToLower(k)] = l.Token
188 k = ""
189 }
190 }
191
192 if err := c.Err(); err != nil {
193 return nil, &ParseError{file: file, err: err.Error()}
194 }
195 return m, nil
196 }
197
198 type klexer struct {
199 br io.ByteReader
200
201 readErr error
202
203 line int
204 column int
205
206 key bool
207 eol bool // end-of-line
208 }
209
210 func newKLexer(r io.Reader) *klexer {
211 br, ok := r.(io.ByteReader)
212 if !ok {
213 br = bufio.NewReaderSize(r, 1024)
214 }
215
216 return &klexer{
217 br: br,
218 line: 1,
219 key: true,
220 }
221 }
222
223 func (kl *klexer) Err() error {
224 if kl.readErr == io.EOF {
225 return nil
226 }
227 return kl.readErr
228 }
229
230 // readByte returns the next byte from the input
231 func (kl *klexer) readByte() (byte, bool) {
232 if kl.readErr != nil {
233 return 0, false
234 }
235
236 c, err := kl.br.ReadByte()
237 if err != nil {
238 kl.readErr = err
239 return 0, false
240 }
241
242 // delay the newline handling until the next token is delivered,
243 // fixes off-by-one errors when reporting a parse error.
244 if kl.eol {
245 kl.line++
246 kl.column = 0
247 kl.eol = false
248 }
249
250 if c == '\n' {
251 kl.eol = true
252 } else {
253 kl.column++
254 }
255 return c, true
256 }
257
258 func (kl *klexer) Next() (dnslex.Lex, bool) {
259 var (
260 l dnslex.Lex
261 str strings.Builder
262 commt bool
263 )
264
265 for x, ok := kl.readByte(); ok; x, ok = kl.readByte() {
266 l.Line, l.Column = kl.line, kl.column
267
268 switch x {
269 case ':':
270 if commt || !kl.key {
271 break
272 }
273
274 kl.key = false
275
276 // Next token is a space, eat it
277 kl.readByte()
278
279 l.Value = zKey
280 l.Token = str.String()
281 return l, true
282 case ';':
283 commt = true
284 case '\n':
285 if commt {
286 // Reset a comment
287 commt = false
288 }
289
290 if kl.key && str.Len() == 0 {
291 // ignore empty lines
292 break
293 }
294
295 kl.key = true
296
297 l.Value = zValue
298 l.Token = str.String()
299 return l, true
300 default:
301 if commt {
302 break
303 }
304
305 str.WriteByte(x)
306 }
307 }
308
309 if kl.readErr != nil && kl.readErr != io.EOF {
310 // Don't return any tokens after a read error occurs.
311 return dnslex.Lex{Value: dnslex.EOF}, false
312 }
313
314 if str.Len() > 0 {
315 // Send remainder
316 l.Value = zValue
317 l.Token = str.String()
318 return l, true
319 }
320 return dnslex.Lex{Value: dnslex.EOF}, false
321 }