| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"fmt" |
| 6 |
|
| 7 |
"codeberg.org/miekg/dns/internal/pack" |
| 8 |
"codeberg.org/miekg/dns/internal/unpack" |
| 9 |
) |
| 10 |
|
| 11 |
// SIG0Sign signs a dns.Msg. It fills the signature with the appropriate data. |
| 12 |
// The SIG record should have the SignerName, KeyTag, Algorithm, Inception |
| 13 |
// and Expiration set. |
| 14 |
func SIG0Sign(m *Msg, k SIG0Signer, options *SIG0Option) error { |
| 15 |
if len(m.Data) == 0 { |
| 16 |
if err := m.Pack(); err != nil { |
| 17 |
return err |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
s := hasSIG0(m) |
| 22 |
if s == nil { |
| 23 |
return fmt.Errorf("%w: %s", ErrNoSIG0, "sign") |
| 24 |
} |
| 25 |
|
| 26 |
isPseudo := m.isPseudo() - 1 // should be 1 (only the tsig) or 2 (opt + tsig) |
| 27 |
arcount := setArcount(m, isPseudo) |
| 28 |
if arcount == -1 { |
| 29 |
return fmt.Errorf("%w: %s", ErrNoSIG0, "sign") |
| 30 |
} |
| 31 |
|
| 32 |
signature, err := k.Sign(s, m.Data, *options) |
| 33 |
if err != nil { |
| 34 |
return err |
| 35 |
} |
| 36 |
|
| 37 |
s.Signature = unpack.Base64(signature) |
| 38 |
|
| 39 |
sbuf := make([]byte, s.Len()) |
| 40 |
_, off, err := packRR(s, sbuf, 0, nil) |
| 41 |
if err != nil { |
| 42 |
return err |
| 43 |
} |
| 44 |
sbuf = sbuf[:off] |
| 45 |
m.Data = append(m.Data, sbuf...) |
| 46 |
|
| 47 |
pack.Uint16(uint16(arcount+1), m.Data, msgArcount) // and +1 after we done for the new and improved TSIG that is added |
| 48 |
return nil |
| 49 |
} |
| 50 |
|
| 51 |
// Verify validates the message buf using the key k. |
| 52 |
func SIG0Verify(m *Msg, y *KEY, k SIG0Signer, options *SIG0Option) error { |
| 53 |
if len(m.Data) == 0 { |
| 54 |
if err := m.Pack(); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
s := hasSIG0(m) |
| 60 |
if s == nil { |
| 61 |
return fmt.Errorf("%w: %s", ErrNoSIG0, "verify") |
| 62 |
} |
| 63 |
|
| 64 |
isPseudo := m.isPseudo() - 1 |
| 65 |
arcount := setArcount(m, isPseudo) |
| 66 |
if arcount == -1 { |
| 67 |
return fmt.Errorf("%w: %s", ErrNoSIG0, "verify") |
| 68 |
} |
| 69 |
|
| 70 |
err := k.Verify(s, m.Data, *options) |
| 71 |
|
| 72 |
pack.Uint16(uint16(arcount+1), m.Data, msgArcount) // restore arcount |
| 73 |
return err |
| 74 |
} |
| 75 |
|
| 76 |
// SIG0ption are options that are given to the signer and verifier. |
| 77 |
type SIG0Option struct{} |
| 78 |
|
| 79 |
// SIG0Signer defines an interface that allows for pluggeable signers and verifiers. |
| 80 |
type SIG0Signer interface { |
| 81 |
// Sign is passed the to-be-signed binary data extracted from the DNS message in p. It should return the signature or an error. |
| 82 |
Sign(s *SIG, p []byte, options SIG0Option) ([]byte, error) |
| 83 |
// Verify is passed the binary data with the TSIG octets and the TSIG RR. If the signature is valid it will return nil, otherwise an error. |
| 84 |
Verify(s *SIG, p []byte, options SIG0Option) error |
| 85 |
// Key returns the key to sign or verify with. |
| 86 |
Key() *KEY |
| 87 |
// Signer returns the crypto signer to sign or verify with. |
| 88 |
Signer() crypto.Signer |
| 89 |
} |
| 90 |
|
| 91 |
func hasSIG0(m *Msg) *SIG { |
| 92 |
for i := 0; i < len(m.Pseudo); i++ { |
| 93 |
if _, ok := m.Pseudo[i].(*SIG); ok { |
| 94 |
return m.Pseudo[i].(*SIG) |
| 95 |
} |
| 96 |
} |
| 97 |
return nil |
| 98 |
} |