| 1 | package dnsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ |
| 2 | |
| 3 | import ( |
| 4 | "cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/" |
| 5 | "crypto/ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/" |
| 6 | "crypto/ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/" |
| 7 | "crypto/rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/" |
| 8 | "encoding/binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/" |
| 9 | "math/bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/" |
| 10 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 11 | ) |
| 12 | |
| 13 | // Sign signs a dns.Msg. It fills the signature with the appropriate data. |
| 14 | // The SIG record should have the SignerName, KeyTag, Algorithm, Inception |
| 15 | // and Expiration set. |
| 16 | func (rr *SIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SIG#) Signscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SIG#Sign().(k cryptoscip-go gomod github.com/golang/go/src go1.25.0 crypto/.Signerscip-go gomod github.com/golang/go/src go1.25.0 crypto/Signer#, m *Msgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#) ([]byte, error) { |
| 17 | if k == nil { |
| 18 | return nil, ErrPrivKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrPrivKey. |
| 19 | } |
| 20 | if rr.KeyTagscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#KeyTag. == 0 || rr.SignerNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#SignerName. == "" || rr.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Algorithm. == 0 { |
| 21 | return nil, ErrKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKey. |
| 22 | } |
| 23 | |
| 24 | rr.Hdrscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Hdr. = RR_Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#{Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Name.: ".", Rrtypescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Rrtype.: TypeSIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/TypeSIG., Classscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Class.: ClassANYscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ClassANY., Ttlscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Ttl.: 0} |
| 25 | rr.OrigTtlscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#OrigTtl., rr.TypeCoveredscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#TypeCovered., rr.Labelsscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Labels. = 0, 0, 0 |
| 26 | |
| 27 | buf := make([]byte, m.Lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#Len().()+Lenscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Len().(rr)) |
| 28 | mbuf, err := m.PackBufferscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Msg#PackBuffer().(buf) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | if &buf[0] != &mbuf[0] { |
| 33 | return nil, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 34 | } |
| 35 | off, err := PackRRscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/PackRR().(rr, buf, len(mbuf), nil, false) |
| 36 | if err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | buf = buf[:off:cap(buf)] |
| 40 | |
| 41 | h, cryptohash, err := hashFromAlgorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/hashFromAlgorithm().(rr.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Algorithm.) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | |
| 46 | // Write SIG rdata |
| 47 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf[len(mbuf)+1+2+2+4+2:]) |
| 48 | // Write message |
| 49 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf[:len(mbuf)]) |
| 50 | |
| 51 | signature, err := signscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/sign().(k, h.Sumscip-go gomod github.com/golang/go/src go1.25.0 hash/Hash#Sum().(nil), cryptohash, rr.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Algorithm.) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | rr.Signaturescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Signature. = toBase64scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/toBase64().(signature) |
| 57 | |
| 58 | buf = append(buf, signature...) |
| 59 | if len(buf) > int(^uint16(0)) { |
| 60 | return nil, ErrBufscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrBuf. |
| 61 | } |
| 62 | // Adjust sig data length |
| 63 | rdoff := len(mbuf) + 1 + 2 + 2 + 4 |
| 64 | rdlen := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[rdoff:]) |
| 65 | rdlen += uint16(len(signature)) |
| 66 | binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#PutUint16().(buf[rdoff:], rdlen) |
| 67 | // Adjust additional count |
| 68 | adc := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[10:]) |
| 69 | adc++ |
| 70 | binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..PutUint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#PutUint16().(buf[10:], adc) |
| 71 | return buf, nil |
| 72 | } |
| 73 | |
| 74 | // Verify validates the message buf using the key k. |
| 75 | // It's assumed that buf is a valid message from which rr was unpacked. |
| 76 | func (rr *SIGscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SIG#) Verifyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/SIG#Verify().(k *KEYscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/KEY#, buf []byte) error { |
| 77 | if k == nil { |
| 78 | return ErrKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKey. |
| 79 | } |
| 80 | if rr.KeyTagscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#KeyTag. == 0 || rr.SignerNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#SignerName. == "" || rr.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Algorithm. == 0 { |
| 81 | return ErrKeyscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKey. |
| 82 | } |
| 83 | |
| 84 | h, cryptohash, err := hashFromAlgorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/hashFromAlgorithm().(rr.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RRSIG#Algorithm.) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | |
| 89 | buflen := len(buf) |
| 90 | qdc := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[4:]) |
| 91 | anc := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[6:]) |
| 92 | auc := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[8:]) |
| 93 | adc := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[10:]) |
| 94 | offset := headerSizescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/headerSize. |
| 95 | for i := uint16(0); i < qdc && offset < buflen; i++ { |
| 96 | _, offset, err = UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(buf, offset) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | // Skip past Type and Class |
| 101 | offset += 2 + 2 |
| 102 | } |
| 103 | for i := uint16(1); i < anc+auc+adc && offset < buflen; i++ { |
| 104 | _, offset, err = UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(buf, offset) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | // Skip past Type, Class and TTL |
| 109 | offset += 2 + 2 + 4 |
| 110 | if offset+1 >= buflen { |
| 111 | continue |
| 112 | } |
| 113 | rdlen := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint16scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint16().(buf[offset:]) |
| 114 | offset += 2 |
| 115 | offset += int(rdlen) |
| 116 | } |
| 117 | if offset >= buflen { |
| 118 | return &Errorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#{errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#err.: "overflowing unpacking signed message"} |
| 119 | } |
| 120 | |
| 121 | // offset should be just prior to SIG |
| 122 | bodyend := offset |
| 123 | // owner name SHOULD be root |
| 124 | _, offset, err = UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(buf, offset) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | // Skip Type, Class, TTL, RDLen |
| 129 | offset += 2 + 2 + 4 + 2 |
| 130 | sigstart := offset |
| 131 | // Skip Type Covered, Algorithm, Labels, Original TTL |
| 132 | offset += 2 + 1 + 1 + 4 |
| 133 | if offset+4+4 >= buflen { |
| 134 | return &Errorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#{errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#err.: "overflow unpacking signed message"} |
| 135 | } |
| 136 | expire := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint32scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint32().(buf[offset:]) |
| 137 | offset += 4 |
| 138 | incept := binaryscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/.BigEndianscip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/BigEndian..Uint32scip-go gomod github.com/golang/go/src go1.25.0 `encoding/binary`/bigEndian#Uint32().(buf[offset:]) |
| 139 | offset += 4 |
| 140 | now := uint32(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().().Unixscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Unix().()) |
| 141 | if now < incept || now > expire { |
| 142 | return ErrTimescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrTime. |
| 143 | } |
| 144 | // Skip key tag |
| 145 | offset += 2 |
| 146 | var signername string |
| 147 | signername, offset, err = UnpackDomainNamescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/UnpackDomainName().(buf, offset) |
| 148 | if err != nil { |
| 149 | return err |
| 150 | } |
| 151 | // If key has come from the DNS name compression might |
| 152 | // have mangled the case of the name |
| 153 | if !equalscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/equal().(signername, k.Headerscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/KEY#Header().().Namescip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RR_Header#Name.) { |
| 154 | return &Errorscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#{errscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/Error#err.: "signer name doesn't match key name"} |
| 155 | } |
| 156 | sigend := offset |
| 157 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf[sigstart:sigend]) |
| 158 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf[:10]) |
| 159 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().([]byte{ |
| 160 | byte((adc - 1) << 8), |
| 161 | byte(adc - 1), |
| 162 | }) |
| 163 | h.Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(buf[12:bodyend]) |
| 164 | |
| 165 | hashed := h.Sumscip-go gomod github.com/golang/go/src go1.25.0 hash/Hash#Sum().(nil) |
| 166 | sig := buf[sigend:] |
| 167 | switch k.Algorithmscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/DNSKEY#Algorithm. { |
| 168 | case RSASHA1scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RSASHA1., RSASHA256scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RSASHA256., RSASHA512scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/RSASHA512.: |
| 169 | pk := k.publicKeyRSAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/DNSKEY#publicKeyRSA().() |
| 170 | if pk != nil { |
| 171 | return rsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/.VerifyPKCS1v15scip-go gomod github.com/golang/go/src go1.25.0 `crypto/rsa`/VerifyPKCS1v15().(pk, cryptohash, hashed, sig) |
| 172 | } |
| 173 | case ECDSAP256SHA256scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ECDSAP256SHA256., ECDSAP384SHA384scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ECDSAP384SHA384.: |
| 174 | pk := k.publicKeyECDSAscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/DNSKEY#publicKeyECDSA().() |
| 175 | r := new(bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#SetBytes().(sig[:len(sig)/2]) |
| 176 | s := new(bigscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/.Intscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#).SetBytesscip-go gomod github.com/golang/go/src go1.25.0 `math/big`/Int#SetBytes().(sig[len(sig)/2:]) |
| 177 | if pk != nil { |
| 178 | if ecdsascip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/.Verifyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ecdsa`/Verify().(pk, hashed, r, s) { |
| 179 | return nil |
| 180 | } |
| 181 | return ErrSigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrSig. |
| 182 | } |
| 183 | case ED25519scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ED25519.: |
| 184 | pk := k.publicKeyED25519scip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/DNSKEY#publicKeyED25519().() |
| 185 | if pk != nil { |
| 186 | if ed25519scip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/.Verifyscip-go gomod github.com/golang/go/src go1.25.0 `crypto/ed25519`/Verify().(pk, hashed, sig) { |
| 187 | return nil |
| 188 | } |
| 189 | return ErrSigscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrSig. |
| 190 | } |
| 191 | } |
| 192 | return ErrKeyAlgscip-go gomod github.com/miekg/dns . `github.com/miekg/dns`/ErrKeyAlg. |
| 193 | } |