New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make TsigVerify check time after signature per rfc2845bis #1135
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package dns | |
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
@@ -50,3 +52,58 @@ func TestTsigCase(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
} | ||
|
||
const ( | ||
// A template wire-format DNS message (in hex form) containing a TSIG RR. | ||
// Its time signed field will be filled by tests. | ||
wireMsg = "c60028000001000000010001076578616d706c6503636f6d00000600010161c00c0001000100000e100004c0000201077465" + | ||
"73746b65790000fa00ff00000000003d0b686d61632d73686132353600" + | ||
"%012x" + // placeholder for the "time signed" field | ||
"012c00208cf23e0081d915478a182edcea7ff48ad102948e6c7ef8e887536957d1fa5616c60000000000" | ||
// A secret (in base64 format) with which the TSIG in wireMsg will be validated | ||
testSecret = "NoTCJU+DMqFWywaPyxSijrDEA/eC3nK0xi3AMEZuPVk=" | ||
// the 'time signed' field value that would make the TSIG RR valid with testSecret | ||
timeSigned uint64 = 1594855491 | ||
) | ||
|
||
func TestTsigErrors(t *testing.T) { | ||
// Helper shortcut to build wire-format test message. | ||
// TsigVerify can modify the slice, so we need to create a new one for each test case below. | ||
buildMsgData := func(tm uint64) []byte { | ||
msgData, err := hex.DecodeString(fmt.Sprintf(wireMsg, tm)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return msgData | ||
} | ||
|
||
checkError := func(expected, actual error) { | ||
if actual == nil { | ||
t.Fatal("expected an error, but got nil") | ||
} | ||
if actual != expected { | ||
t.Fatalf("expected an error '%v' but got '%v'", expected, actual) | ||
} | ||
} | ||
|
||
// the signature is valid but 'time signed' is too far from the "current time". | ||
checkError(ErrTime, tsigVerify(buildMsgData(timeSigned), testSecret, "", false, timeSigned+301)) | ||
checkError(ErrTime, tsigVerify(buildMsgData(timeSigned), testSecret, "", false, timeSigned-301)) | ||
|
||
// the signature is invalid and 'time signed' is too far. | ||
// the signature should be checked first, so we should see ErrSig. | ||
checkError(ErrSig, tsigVerify(buildMsgData(timeSigned+301), testSecret, "", false, timeSigned)) | ||
|
||
// tweak the algorithm name in the wire data, resulting in the "unknown algorithm" error. | ||
msgData := buildMsgData(timeSigned) | ||
garbage := []byte("bogus") | ||
copy(msgData[67:67+len(garbage)], garbage) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can just be: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks, updated it as suggested. |
||
checkError(ErrKeyAlg, tsigVerify(msgData, testSecret, "", false, timeSigned)) | ||
|
||
// call TsigVerify with a message that doesn't contain a TSIG | ||
msgData, _, err := stripTsig(buildMsgData(timeSigned)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
checkError(ErrNoSig, tsigVerify(msgData, testSecret, "", false, timeSigned)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditch this because it doesn’t match the rest of the tests and just include only the
if actual != expected
check directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you mean instead of
do something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jinmeiib Exactly that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tmthrgd okay, updated as such.