@@ -155,6 +155,117 @@ func TestSignVerify(t *testing.T) {
155155 }
156156}
157157
158+ // Test if RRSIG.Verify() conforms to RFC 4035 Section 5.3.1
159+ func TestShouldNotVerifyInvalidSig (t * testing.T ) {
160+ // The RRSIG RR and the RRset MUST have the same owner name
161+ rrNameMismatch := getSoa ()
162+ rrNameMismatch .Hdr .Name = "example.com."
163+
164+ // ... and the same class
165+ rrClassMismatch := getSoa ()
166+ rrClassMismatch .Hdr .Class = ClassCHAOS
167+
168+ // The RRSIG RR's Type Covered field MUST equal the RRset's type.
169+ rrTypeMismatch := getSoa ()
170+ rrTypeMismatch .Hdr .Rrtype = TypeA
171+
172+ // The number of labels in the RRset owner name MUST be greater than
173+ // or equal to the value in the RRSIG RR's Labels field.
174+ rrLabelLessThan := getSoa ()
175+ rrLabelLessThan .Hdr .Name = "nl."
176+
177+ // Time checks are done in ValidityPeriod
178+
179+ // With this key
180+ key := new (DNSKEY )
181+ key .Hdr .Rrtype = TypeDNSKEY
182+ key .Hdr .Name = "miek.nl."
183+ key .Hdr .Class = ClassINET
184+ key .Hdr .Ttl = 14400
185+ key .Flags = 256
186+ key .Protocol = 3
187+ key .Algorithm = RSASHA256
188+ privkey , _ := key .Generate (512 )
189+
190+ normalSoa := getSoa ()
191+
192+ // Fill in the normal values of the Sig, before signing
193+ sig := new (RRSIG )
194+ sig .Hdr = RR_Header {"miek.nl." , TypeRRSIG , ClassINET , 14400 , 0 }
195+ sig .TypeCovered = TypeSOA
196+ sig .Labels = uint8 (CountLabel (normalSoa .Hdr .Name ))
197+ sig .OrigTtl = normalSoa .Hdr .Ttl
198+ sig .Expiration = 1296534305 // date -u '+%s' -d"2011-02-01 04:25:05"
199+ sig .Inception = 1293942305 // date -u '+%s' -d"2011-01-02 04:25:05"
200+ sig .KeyTag = key .KeyTag () // Get the keyfrom the Key
201+ sig .SignerName = key .Hdr .Name
202+ sig .Algorithm = RSASHA256
203+
204+ for i , rr := range []RR {rrNameMismatch , rrClassMismatch , rrTypeMismatch , rrLabelLessThan } {
205+ if i != 0 { // Just for the rrNameMismatch case, we need the name to mismatch
206+ sig := sig .copy ().(* RRSIG )
207+ sig .SignerName = rr .Header ().Name
208+ sig .Hdr .Name = rr .Header ().Name
209+ key := key .copy ().(* DNSKEY )
210+ key .Hdr .Name = rr .Header ().Name
211+ }
212+
213+ if err := sig .signAsIs (privkey .(* rsa.PrivateKey ), []RR {rr }); err != nil {
214+ t .Error ("failure to sign the record:" , err )
215+ continue
216+ }
217+
218+ if err := sig .Verify (key , []RR {rr }); err == nil {
219+ t .Error ("should not validate: " , rr )
220+ continue
221+ } else {
222+ t .Logf ("expected failure: %v for RR name %s, class %d, type %d, rrsig labels %d" , err , rr .Header ().Name , rr .Header ().Class , rr .Header ().Rrtype , CountLabel (rr .Header ().Name ))
223+ }
224+ }
225+
226+ // The RRSIG RR's Signer's Name field MUST be the name of the zone that contains the RRset.
227+ // The RRSIG RR's Signer's Name, Algorithm, and Key Tag fields MUST match the owner name,
228+ // algorithm, and key tag for some DNSKEY RR in the zone's apex DNSKEY RRset.
229+ sigMismatchName := sig .copy ().(* RRSIG )
230+ sigMismatchName .SignerName = "example.com."
231+ soaMismatchName := getSoa ()
232+ soaMismatchName .Hdr .Name = "example.com."
233+ keyMismatchName := key .copy ().(* DNSKEY )
234+ keyMismatchName .Hdr .Name = "example.com."
235+ if err := sigMismatchName .signAsIs (privkey .(* rsa.PrivateKey ), []RR {soaMismatchName }); err != nil {
236+ t .Error ("failure to sign the record:" , err )
237+ } else if err := sigMismatchName .Verify (keyMismatchName , []RR {soaMismatchName }); err == nil {
238+ t .Error ("should not validate: " , soaMismatchName , ", RRSIG's signer's name does not match the owner name" )
239+ } else {
240+ t .Logf ("expected failure: %v for signer %s and owner %s" , err , sigMismatchName .SignerName , sigMismatchName .Hdr .Name )
241+ }
242+
243+ sigMismatchAlgo := sig .copy ().(* RRSIG )
244+ sigMismatchAlgo .Algorithm = RSASHA1
245+ sigMismatchKeyTag := sig .copy ().(* RRSIG )
246+ sigMismatchKeyTag .KeyTag = 12345
247+ for _ , sigMismatch := range []* RRSIG {sigMismatchAlgo , sigMismatchKeyTag } {
248+ if err := sigMismatch .Sign (privkey .(* rsa.PrivateKey ), []RR {normalSoa }); err != nil {
249+ t .Error ("failure to sign the record:" , err )
250+ } else if err := sigMismatch .Verify (key , []RR {normalSoa }); err == nil {
251+ t .Error ("should not validate: " , normalSoa )
252+ } else {
253+ t .Logf ("expected failure: %v for signer %s algo %d keytag %d" , err , sigMismatch .SignerName , sigMismatch .Algorithm , sigMismatch .KeyTag )
254+ }
255+ }
256+
257+ // The matching DNSKEY RR MUST have the Zone Flag bit (DNSKEY RDATA Flag bit 7) set.
258+ keyZoneBitWrong := key .copy ().(* DNSKEY )
259+ keyZoneBitWrong .Flags = key .Flags &^ ZONE
260+ if err := sig .Sign (privkey .(* rsa.PrivateKey ), []RR {normalSoa }); err != nil {
261+ t .Error ("failure to sign the record:" , err )
262+ } else if err := sig .Verify (keyZoneBitWrong , []RR {normalSoa }); err == nil {
263+ t .Error ("should not validate: " , normalSoa )
264+ } else {
265+ t .Logf ("expected failure: %v for key flags %d" , err , keyZoneBitWrong .Flags )
266+ }
267+ }
268+
158269func Test65534 (t * testing.T ) {
159270 t6 := new (RFC3597 )
160271 t6 .Hdr = RR_Header {"miek.nl." , 65534 , ClassINET , 14400 , 0 }
0 commit comments